mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_DISCO_F407VG/stm32f4xx_hal_can.c@133:d4dda5c437f0, 2014-03-24 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Mar 24 17:45:07 2014 +0000
- Revision:
- 133:d4dda5c437f0
- Child:
- 242:7074e42da0b2
Synchronized with git revision 47b961246bed973fe4cb8932781ffc8025b78a61
Full URL: https://github.com/mbedmicro/mbed/commit/47b961246bed973fe4cb8932781ffc8025b78a61/
[STM32F4-Discovery (STM32F407VG)] initial port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 133:d4dda5c437f0 | 1 | /** |
mbed_official | 133:d4dda5c437f0 | 2 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 3 | * @file stm32f4xx_hal_can.c |
mbed_official | 133:d4dda5c437f0 | 4 | * @author MCD Application Team |
mbed_official | 133:d4dda5c437f0 | 5 | * @version V1.0.0 |
mbed_official | 133:d4dda5c437f0 | 6 | * @date 18-February-2014 |
mbed_official | 133:d4dda5c437f0 | 7 | * @brief This file provides firmware functions to manage the following |
mbed_official | 133:d4dda5c437f0 | 8 | * functionalities of the Controller Area Network (CAN) peripheral: |
mbed_official | 133:d4dda5c437f0 | 9 | * + Initialization and de-initialization functions |
mbed_official | 133:d4dda5c437f0 | 10 | * + IO operation functions |
mbed_official | 133:d4dda5c437f0 | 11 | * + Peripheral Control functions |
mbed_official | 133:d4dda5c437f0 | 12 | * + Peripheral State and Error functions |
mbed_official | 133:d4dda5c437f0 | 13 | * |
mbed_official | 133:d4dda5c437f0 | 14 | @verbatim |
mbed_official | 133:d4dda5c437f0 | 15 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 16 | ##### How to use this driver ##### |
mbed_official | 133:d4dda5c437f0 | 17 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 18 | [..] |
mbed_official | 133:d4dda5c437f0 | 19 | (#) Enable the CAN controller interface clock using |
mbed_official | 133:d4dda5c437f0 | 20 | __CAN1_CLK_ENABLE() for CAN1 and __CAN1_CLK_ENABLE() for CAN2 |
mbed_official | 133:d4dda5c437f0 | 21 | -@- In case you are using CAN2 only, you have to enable the CAN1 clock. |
mbed_official | 133:d4dda5c437f0 | 22 | |
mbed_official | 133:d4dda5c437f0 | 23 | (#) CAN pins configuration |
mbed_official | 133:d4dda5c437f0 | 24 | (++) Enable the clock for the CAN GPIOs using the following function: |
mbed_official | 133:d4dda5c437f0 | 25 | __GPIOx_CLK_ENABLE() |
mbed_official | 133:d4dda5c437f0 | 26 | (++) Connect and configure the involved CAN pins to AF9 using the |
mbed_official | 133:d4dda5c437f0 | 27 | following function HAL_GPIO_Init() |
mbed_official | 133:d4dda5c437f0 | 28 | |
mbed_official | 133:d4dda5c437f0 | 29 | (#) Initialise and configure the CAN using CAN_Init() function. |
mbed_official | 133:d4dda5c437f0 | 30 | |
mbed_official | 133:d4dda5c437f0 | 31 | (#) Transmit the desired CAN frame using HAL_CAN_Transmit() function. |
mbed_official | 133:d4dda5c437f0 | 32 | |
mbed_official | 133:d4dda5c437f0 | 33 | (#) Receive a CAN frame using HAL_CAN_Recieve() function. |
mbed_official | 133:d4dda5c437f0 | 34 | |
mbed_official | 133:d4dda5c437f0 | 35 | *** Polling mode IO operation *** |
mbed_official | 133:d4dda5c437f0 | 36 | ================================= |
mbed_official | 133:d4dda5c437f0 | 37 | [..] |
mbed_official | 133:d4dda5c437f0 | 38 | (+) Start the CAN peripheral transmission and wait the end of this operation |
mbed_official | 133:d4dda5c437f0 | 39 | using HAL_CAN_Transmit(), at this stage user can specify the value of timeout |
mbed_official | 133:d4dda5c437f0 | 40 | according to his end application |
mbed_official | 133:d4dda5c437f0 | 41 | (+) Start the CAN peripheral reception and wait the end of this operation |
mbed_official | 133:d4dda5c437f0 | 42 | using HAL_CAN_Receive(), at this stage user can specify the value of timeout |
mbed_official | 133:d4dda5c437f0 | 43 | according to his end application |
mbed_official | 133:d4dda5c437f0 | 44 | |
mbed_official | 133:d4dda5c437f0 | 45 | *** Interrupt mode IO operation *** |
mbed_official | 133:d4dda5c437f0 | 46 | =================================== |
mbed_official | 133:d4dda5c437f0 | 47 | [..] |
mbed_official | 133:d4dda5c437f0 | 48 | (+) Start the CAN peripheral transmission using HAL_CAN_Transmit_IT() |
mbed_official | 133:d4dda5c437f0 | 49 | (+) Start the CAN peripheral reception using HAL_CAN_Receive_IT() |
mbed_official | 133:d4dda5c437f0 | 50 | (+) Use HAL_CAN_IRQHandler() called under the used CAN Interrupt subroutine |
mbed_official | 133:d4dda5c437f0 | 51 | (+) At CAN end of transmission HAL_CAN_TxCpltCallback() function is executed and user can |
mbed_official | 133:d4dda5c437f0 | 52 | add his own code by customization of function pointer HAL_CAN_TxCpltCallback |
mbed_official | 133:d4dda5c437f0 | 53 | (+) In case of CAN Error, HAL_CAN_ErrorCallback() function is executed and user can |
mbed_official | 133:d4dda5c437f0 | 54 | add his own code by customization of function pointer HAL_CAN_ErrorCallback |
mbed_official | 133:d4dda5c437f0 | 55 | |
mbed_official | 133:d4dda5c437f0 | 56 | *** CAN HAL driver macros list *** |
mbed_official | 133:d4dda5c437f0 | 57 | ============================================= |
mbed_official | 133:d4dda5c437f0 | 58 | [..] |
mbed_official | 133:d4dda5c437f0 | 59 | Below the list of most used macros in CAN HAL driver. |
mbed_official | 133:d4dda5c437f0 | 60 | |
mbed_official | 133:d4dda5c437f0 | 61 | (+) __HAL_CAN_ENABLE_IT: Enable the specified CAN interrupts |
mbed_official | 133:d4dda5c437f0 | 62 | (+) __HAL_CAN_DISABLE_IT: Disable the specified CAN interrupts |
mbed_official | 133:d4dda5c437f0 | 63 | (+) __HAL_CAN_GET_IT_SOURCE: Check if the specified CAN interrupt source is enabled or disabled |
mbed_official | 133:d4dda5c437f0 | 64 | (+) __HAL_CAN_CLEAR_FLAG: Clear the CAN's pending flags |
mbed_official | 133:d4dda5c437f0 | 65 | (+) __HAL_CAN_GET_FLAG: Get the selected CAN's flag status |
mbed_official | 133:d4dda5c437f0 | 66 | |
mbed_official | 133:d4dda5c437f0 | 67 | [..] |
mbed_official | 133:d4dda5c437f0 | 68 | (@) You can refer to the CAN HAL driver header file for more useful macros |
mbed_official | 133:d4dda5c437f0 | 69 | |
mbed_official | 133:d4dda5c437f0 | 70 | @endverbatim |
mbed_official | 133:d4dda5c437f0 | 71 | |
mbed_official | 133:d4dda5c437f0 | 72 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 73 | * @attention |
mbed_official | 133:d4dda5c437f0 | 74 | * |
mbed_official | 133:d4dda5c437f0 | 75 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
mbed_official | 133:d4dda5c437f0 | 76 | * |
mbed_official | 133:d4dda5c437f0 | 77 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 133:d4dda5c437f0 | 78 | * are permitted provided that the following conditions are met: |
mbed_official | 133:d4dda5c437f0 | 79 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 133:d4dda5c437f0 | 80 | * this list of conditions and the following disclaimer. |
mbed_official | 133:d4dda5c437f0 | 81 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 133:d4dda5c437f0 | 82 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 133:d4dda5c437f0 | 83 | * and/or other materials provided with the distribution. |
mbed_official | 133:d4dda5c437f0 | 84 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 133:d4dda5c437f0 | 85 | * may be used to endorse or promote products derived from this software |
mbed_official | 133:d4dda5c437f0 | 86 | * without specific prior written permission. |
mbed_official | 133:d4dda5c437f0 | 87 | * |
mbed_official | 133:d4dda5c437f0 | 88 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 133:d4dda5c437f0 | 89 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 133:d4dda5c437f0 | 90 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 133:d4dda5c437f0 | 91 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 133:d4dda5c437f0 | 92 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 133:d4dda5c437f0 | 93 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 133:d4dda5c437f0 | 94 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 133:d4dda5c437f0 | 95 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 133:d4dda5c437f0 | 96 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 133:d4dda5c437f0 | 97 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 133:d4dda5c437f0 | 98 | * |
mbed_official | 133:d4dda5c437f0 | 99 | ****************************************************************************** |
mbed_official | 133:d4dda5c437f0 | 100 | */ |
mbed_official | 133:d4dda5c437f0 | 101 | |
mbed_official | 133:d4dda5c437f0 | 102 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 103 | #include "stm32f4xx_hal.h" |
mbed_official | 133:d4dda5c437f0 | 104 | |
mbed_official | 133:d4dda5c437f0 | 105 | /** @addtogroup STM32F4xx_HAL_Driver |
mbed_official | 133:d4dda5c437f0 | 106 | * @{ |
mbed_official | 133:d4dda5c437f0 | 107 | */ |
mbed_official | 133:d4dda5c437f0 | 108 | |
mbed_official | 133:d4dda5c437f0 | 109 | /** @defgroup CAN |
mbed_official | 133:d4dda5c437f0 | 110 | * @brief CAN driver modules |
mbed_official | 133:d4dda5c437f0 | 111 | * @{ |
mbed_official | 133:d4dda5c437f0 | 112 | */ |
mbed_official | 133:d4dda5c437f0 | 113 | |
mbed_official | 133:d4dda5c437f0 | 114 | #ifdef HAL_CAN_MODULE_ENABLED |
mbed_official | 133:d4dda5c437f0 | 115 | |
mbed_official | 133:d4dda5c437f0 | 116 | #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) |
mbed_official | 133:d4dda5c437f0 | 117 | |
mbed_official | 133:d4dda5c437f0 | 118 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 119 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 120 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 121 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 122 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 123 | static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber); |
mbed_official | 133:d4dda5c437f0 | 124 | static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan); |
mbed_official | 133:d4dda5c437f0 | 125 | /* Private functions ---------------------------------------------------------*/ |
mbed_official | 133:d4dda5c437f0 | 126 | |
mbed_official | 133:d4dda5c437f0 | 127 | /** @defgroup CAN_Private_Functions |
mbed_official | 133:d4dda5c437f0 | 128 | * @{ |
mbed_official | 133:d4dda5c437f0 | 129 | */ |
mbed_official | 133:d4dda5c437f0 | 130 | |
mbed_official | 133:d4dda5c437f0 | 131 | /** @defgroup CAN_Group1 Initialization and de-initialization functions |
mbed_official | 133:d4dda5c437f0 | 132 | * @brief Initialization and Configuration functions |
mbed_official | 133:d4dda5c437f0 | 133 | * |
mbed_official | 133:d4dda5c437f0 | 134 | @verbatim |
mbed_official | 133:d4dda5c437f0 | 135 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 136 | ##### Initialization and de-initialization functions ##### |
mbed_official | 133:d4dda5c437f0 | 137 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 138 | [..] This section provides functions allowing to: |
mbed_official | 133:d4dda5c437f0 | 139 | (+) Initialize and configure the CAN. |
mbed_official | 133:d4dda5c437f0 | 140 | (+) De-initialize the CAN. |
mbed_official | 133:d4dda5c437f0 | 141 | |
mbed_official | 133:d4dda5c437f0 | 142 | @endverbatim |
mbed_official | 133:d4dda5c437f0 | 143 | * @{ |
mbed_official | 133:d4dda5c437f0 | 144 | */ |
mbed_official | 133:d4dda5c437f0 | 145 | |
mbed_official | 133:d4dda5c437f0 | 146 | /** |
mbed_official | 133:d4dda5c437f0 | 147 | * @brief Initializes the CAN peripheral according to the specified |
mbed_official | 133:d4dda5c437f0 | 148 | * parameters in the CAN_InitStruct. |
mbed_official | 133:d4dda5c437f0 | 149 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 150 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 151 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 152 | */ |
mbed_official | 133:d4dda5c437f0 | 153 | HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 154 | { |
mbed_official | 133:d4dda5c437f0 | 155 | uint32_t InitStatus = 3; |
mbed_official | 133:d4dda5c437f0 | 156 | uint32_t timeout = 0; |
mbed_official | 133:d4dda5c437f0 | 157 | |
mbed_official | 133:d4dda5c437f0 | 158 | /* Check CAN handle */ |
mbed_official | 133:d4dda5c437f0 | 159 | if(hcan == NULL) |
mbed_official | 133:d4dda5c437f0 | 160 | { |
mbed_official | 133:d4dda5c437f0 | 161 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 162 | } |
mbed_official | 133:d4dda5c437f0 | 163 | |
mbed_official | 133:d4dda5c437f0 | 164 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 165 | assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance)); |
mbed_official | 133:d4dda5c437f0 | 166 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM)); |
mbed_official | 133:d4dda5c437f0 | 167 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM)); |
mbed_official | 133:d4dda5c437f0 | 168 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM)); |
mbed_official | 133:d4dda5c437f0 | 169 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART)); |
mbed_official | 133:d4dda5c437f0 | 170 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM)); |
mbed_official | 133:d4dda5c437f0 | 171 | assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP)); |
mbed_official | 133:d4dda5c437f0 | 172 | assert_param(IS_CAN_MODE(hcan->Init.Mode)); |
mbed_official | 133:d4dda5c437f0 | 173 | assert_param(IS_CAN_SJW(hcan->Init.SJW)); |
mbed_official | 133:d4dda5c437f0 | 174 | assert_param(IS_CAN_BS1(hcan->Init.BS1)); |
mbed_official | 133:d4dda5c437f0 | 175 | assert_param(IS_CAN_BS2(hcan->Init.BS2)); |
mbed_official | 133:d4dda5c437f0 | 176 | assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler)); |
mbed_official | 133:d4dda5c437f0 | 177 | |
mbed_official | 133:d4dda5c437f0 | 178 | |
mbed_official | 133:d4dda5c437f0 | 179 | if(hcan->State == HAL_CAN_STATE_RESET) |
mbed_official | 133:d4dda5c437f0 | 180 | { |
mbed_official | 133:d4dda5c437f0 | 181 | /* Init the low level hardware */ |
mbed_official | 133:d4dda5c437f0 | 182 | HAL_CAN_MspInit(hcan); |
mbed_official | 133:d4dda5c437f0 | 183 | } |
mbed_official | 133:d4dda5c437f0 | 184 | |
mbed_official | 133:d4dda5c437f0 | 185 | /* Initialize the CAN state*/ |
mbed_official | 133:d4dda5c437f0 | 186 | hcan->State = HAL_CAN_STATE_BUSY; |
mbed_official | 133:d4dda5c437f0 | 187 | |
mbed_official | 133:d4dda5c437f0 | 188 | /* Exit from sleep mode */ |
mbed_official | 133:d4dda5c437f0 | 189 | hcan->Instance->MCR &= (~(uint32_t)CAN_MCR_SLEEP); |
mbed_official | 133:d4dda5c437f0 | 190 | |
mbed_official | 133:d4dda5c437f0 | 191 | /* Request initialisation */ |
mbed_official | 133:d4dda5c437f0 | 192 | hcan->Instance->MCR |= CAN_MCR_INRQ ; |
mbed_official | 133:d4dda5c437f0 | 193 | |
mbed_official | 133:d4dda5c437f0 | 194 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 195 | timeout = HAL_GetTick() + 10; |
mbed_official | 133:d4dda5c437f0 | 196 | |
mbed_official | 133:d4dda5c437f0 | 197 | /* Wait the acknowledge */ |
mbed_official | 133:d4dda5c437f0 | 198 | while((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) |
mbed_official | 133:d4dda5c437f0 | 199 | { |
mbed_official | 133:d4dda5c437f0 | 200 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 201 | { |
mbed_official | 133:d4dda5c437f0 | 202 | hcan->State= HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 203 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 204 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 205 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 206 | } |
mbed_official | 133:d4dda5c437f0 | 207 | } |
mbed_official | 133:d4dda5c437f0 | 208 | |
mbed_official | 133:d4dda5c437f0 | 209 | /* Check acknowledge */ |
mbed_official | 133:d4dda5c437f0 | 210 | if ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) |
mbed_official | 133:d4dda5c437f0 | 211 | { |
mbed_official | 133:d4dda5c437f0 | 212 | InitStatus = CAN_INITSTATUS_FAILED; |
mbed_official | 133:d4dda5c437f0 | 213 | } |
mbed_official | 133:d4dda5c437f0 | 214 | else |
mbed_official | 133:d4dda5c437f0 | 215 | { |
mbed_official | 133:d4dda5c437f0 | 216 | /* Set the time triggered communication mode */ |
mbed_official | 133:d4dda5c437f0 | 217 | if (hcan->Init.TTCM == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 218 | { |
mbed_official | 133:d4dda5c437f0 | 219 | hcan->Instance->MCR |= CAN_MCR_TTCM; |
mbed_official | 133:d4dda5c437f0 | 220 | } |
mbed_official | 133:d4dda5c437f0 | 221 | else |
mbed_official | 133:d4dda5c437f0 | 222 | { |
mbed_official | 133:d4dda5c437f0 | 223 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TTCM; |
mbed_official | 133:d4dda5c437f0 | 224 | } |
mbed_official | 133:d4dda5c437f0 | 225 | |
mbed_official | 133:d4dda5c437f0 | 226 | /* Set the automatic bus-off management */ |
mbed_official | 133:d4dda5c437f0 | 227 | if (hcan->Init.ABOM == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 228 | { |
mbed_official | 133:d4dda5c437f0 | 229 | hcan->Instance->MCR |= CAN_MCR_ABOM; |
mbed_official | 133:d4dda5c437f0 | 230 | } |
mbed_official | 133:d4dda5c437f0 | 231 | else |
mbed_official | 133:d4dda5c437f0 | 232 | { |
mbed_official | 133:d4dda5c437f0 | 233 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_ABOM; |
mbed_official | 133:d4dda5c437f0 | 234 | } |
mbed_official | 133:d4dda5c437f0 | 235 | |
mbed_official | 133:d4dda5c437f0 | 236 | /* Set the automatic wake-up mode */ |
mbed_official | 133:d4dda5c437f0 | 237 | if (hcan->Init.AWUM == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 238 | { |
mbed_official | 133:d4dda5c437f0 | 239 | hcan->Instance->MCR |= CAN_MCR_AWUM; |
mbed_official | 133:d4dda5c437f0 | 240 | } |
mbed_official | 133:d4dda5c437f0 | 241 | else |
mbed_official | 133:d4dda5c437f0 | 242 | { |
mbed_official | 133:d4dda5c437f0 | 243 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_AWUM; |
mbed_official | 133:d4dda5c437f0 | 244 | } |
mbed_official | 133:d4dda5c437f0 | 245 | |
mbed_official | 133:d4dda5c437f0 | 246 | /* Set the no automatic retransmission */ |
mbed_official | 133:d4dda5c437f0 | 247 | if (hcan->Init.NART == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 248 | { |
mbed_official | 133:d4dda5c437f0 | 249 | hcan->Instance->MCR |= CAN_MCR_NART; |
mbed_official | 133:d4dda5c437f0 | 250 | } |
mbed_official | 133:d4dda5c437f0 | 251 | else |
mbed_official | 133:d4dda5c437f0 | 252 | { |
mbed_official | 133:d4dda5c437f0 | 253 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_NART; |
mbed_official | 133:d4dda5c437f0 | 254 | } |
mbed_official | 133:d4dda5c437f0 | 255 | |
mbed_official | 133:d4dda5c437f0 | 256 | /* Set the receive FIFO locked mode */ |
mbed_official | 133:d4dda5c437f0 | 257 | if (hcan->Init.RFLM == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 258 | { |
mbed_official | 133:d4dda5c437f0 | 259 | hcan->Instance->MCR |= CAN_MCR_RFLM; |
mbed_official | 133:d4dda5c437f0 | 260 | } |
mbed_official | 133:d4dda5c437f0 | 261 | else |
mbed_official | 133:d4dda5c437f0 | 262 | { |
mbed_official | 133:d4dda5c437f0 | 263 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_RFLM; |
mbed_official | 133:d4dda5c437f0 | 264 | } |
mbed_official | 133:d4dda5c437f0 | 265 | |
mbed_official | 133:d4dda5c437f0 | 266 | /* Set the transmit FIFO priority */ |
mbed_official | 133:d4dda5c437f0 | 267 | if (hcan->Init.TXFP == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 268 | { |
mbed_official | 133:d4dda5c437f0 | 269 | hcan->Instance->MCR |= CAN_MCR_TXFP; |
mbed_official | 133:d4dda5c437f0 | 270 | } |
mbed_official | 133:d4dda5c437f0 | 271 | else |
mbed_official | 133:d4dda5c437f0 | 272 | { |
mbed_official | 133:d4dda5c437f0 | 273 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TXFP; |
mbed_official | 133:d4dda5c437f0 | 274 | } |
mbed_official | 133:d4dda5c437f0 | 275 | |
mbed_official | 133:d4dda5c437f0 | 276 | /* Set the bit timing register */ |
mbed_official | 133:d4dda5c437f0 | 277 | hcan->Instance->BTR = (uint32_t)((uint32_t)hcan->Init.Mode) | \ |
mbed_official | 133:d4dda5c437f0 | 278 | ((uint32_t)hcan->Init.SJW) | \ |
mbed_official | 133:d4dda5c437f0 | 279 | ((uint32_t)hcan->Init.BS1) | \ |
mbed_official | 133:d4dda5c437f0 | 280 | ((uint32_t)hcan->Init.BS2) | \ |
mbed_official | 133:d4dda5c437f0 | 281 | ((uint32_t)hcan->Init.Prescaler - 1); |
mbed_official | 133:d4dda5c437f0 | 282 | |
mbed_official | 133:d4dda5c437f0 | 283 | /* Request leave initialisation */ |
mbed_official | 133:d4dda5c437f0 | 284 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_INRQ; |
mbed_official | 133:d4dda5c437f0 | 285 | |
mbed_official | 133:d4dda5c437f0 | 286 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 287 | timeout = HAL_GetTick() + 10; |
mbed_official | 133:d4dda5c437f0 | 288 | |
mbed_official | 133:d4dda5c437f0 | 289 | /* Wait the acknowledge */ |
mbed_official | 133:d4dda5c437f0 | 290 | while((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) |
mbed_official | 133:d4dda5c437f0 | 291 | { |
mbed_official | 133:d4dda5c437f0 | 292 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 293 | { |
mbed_official | 133:d4dda5c437f0 | 294 | hcan->State= HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 295 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 296 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 297 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 298 | } |
mbed_official | 133:d4dda5c437f0 | 299 | } |
mbed_official | 133:d4dda5c437f0 | 300 | |
mbed_official | 133:d4dda5c437f0 | 301 | /* Check acknowledged */ |
mbed_official | 133:d4dda5c437f0 | 302 | if ((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) |
mbed_official | 133:d4dda5c437f0 | 303 | { |
mbed_official | 133:d4dda5c437f0 | 304 | InitStatus = CAN_INITSTATUS_FAILED; |
mbed_official | 133:d4dda5c437f0 | 305 | } |
mbed_official | 133:d4dda5c437f0 | 306 | else |
mbed_official | 133:d4dda5c437f0 | 307 | { |
mbed_official | 133:d4dda5c437f0 | 308 | InitStatus = CAN_INITSTATUS_SUCCESS; |
mbed_official | 133:d4dda5c437f0 | 309 | } |
mbed_official | 133:d4dda5c437f0 | 310 | } |
mbed_official | 133:d4dda5c437f0 | 311 | |
mbed_official | 133:d4dda5c437f0 | 312 | if(InitStatus == CAN_INITSTATUS_SUCCESS) |
mbed_official | 133:d4dda5c437f0 | 313 | { |
mbed_official | 133:d4dda5c437f0 | 314 | /* Set CAN error code to none */ |
mbed_official | 133:d4dda5c437f0 | 315 | hcan->ErrorCode = HAL_CAN_ERROR_NONE; |
mbed_official | 133:d4dda5c437f0 | 316 | |
mbed_official | 133:d4dda5c437f0 | 317 | /* Initialize the CAN state */ |
mbed_official | 133:d4dda5c437f0 | 318 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 319 | |
mbed_official | 133:d4dda5c437f0 | 320 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 321 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 322 | } |
mbed_official | 133:d4dda5c437f0 | 323 | else |
mbed_official | 133:d4dda5c437f0 | 324 | { |
mbed_official | 133:d4dda5c437f0 | 325 | /* Initialize the CAN state */ |
mbed_official | 133:d4dda5c437f0 | 326 | hcan->State = HAL_CAN_STATE_ERROR; |
mbed_official | 133:d4dda5c437f0 | 327 | |
mbed_official | 133:d4dda5c437f0 | 328 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 329 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 330 | } |
mbed_official | 133:d4dda5c437f0 | 331 | } |
mbed_official | 133:d4dda5c437f0 | 332 | |
mbed_official | 133:d4dda5c437f0 | 333 | /** |
mbed_official | 133:d4dda5c437f0 | 334 | * @brief Configures the CAN reception filter according to the specified |
mbed_official | 133:d4dda5c437f0 | 335 | * parameters in the CAN_FilterInitStruct. |
mbed_official | 133:d4dda5c437f0 | 336 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 337 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 338 | * @param sFilterConfig: pointer to a CAN_FilterConfTypeDef structure that |
mbed_official | 133:d4dda5c437f0 | 339 | * contains the filter configuration information. |
mbed_official | 133:d4dda5c437f0 | 340 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 341 | */ |
mbed_official | 133:d4dda5c437f0 | 342 | HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef* hcan, CAN_FilterConfTypeDef* sFilterConfig) |
mbed_official | 133:d4dda5c437f0 | 343 | { |
mbed_official | 133:d4dda5c437f0 | 344 | uint32_t filternbrbitpos = 0; |
mbed_official | 133:d4dda5c437f0 | 345 | |
mbed_official | 133:d4dda5c437f0 | 346 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 347 | assert_param(IS_CAN_FILTER_NUMBER(sFilterConfig->FilterNumber)); |
mbed_official | 133:d4dda5c437f0 | 348 | assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode)); |
mbed_official | 133:d4dda5c437f0 | 349 | assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale)); |
mbed_official | 133:d4dda5c437f0 | 350 | assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment)); |
mbed_official | 133:d4dda5c437f0 | 351 | assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation)); |
mbed_official | 133:d4dda5c437f0 | 352 | assert_param(IS_CAN_BANKNUMBER(sFilterConfig->BankNumber)); |
mbed_official | 133:d4dda5c437f0 | 353 | |
mbed_official | 133:d4dda5c437f0 | 354 | filternbrbitpos = ((uint32_t)1) << sFilterConfig->FilterNumber; |
mbed_official | 133:d4dda5c437f0 | 355 | |
mbed_official | 133:d4dda5c437f0 | 356 | /* Initialisation mode for the filter */ |
mbed_official | 133:d4dda5c437f0 | 357 | CAN1->FMR |= (uint32_t)CAN_FMR_FINIT; |
mbed_official | 133:d4dda5c437f0 | 358 | |
mbed_official | 133:d4dda5c437f0 | 359 | /* Select the start slave bank */ |
mbed_official | 133:d4dda5c437f0 | 360 | CAN1->FMR &= ~((uint32_t)CAN_FMR_CAN2SB); |
mbed_official | 133:d4dda5c437f0 | 361 | CAN1->FMR |= (uint32_t)(sFilterConfig->BankNumber << 8); |
mbed_official | 133:d4dda5c437f0 | 362 | |
mbed_official | 133:d4dda5c437f0 | 363 | /* Filter Deactivation */ |
mbed_official | 133:d4dda5c437f0 | 364 | CAN1->FA1R &= ~(uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 365 | |
mbed_official | 133:d4dda5c437f0 | 366 | /* Filter Scale */ |
mbed_official | 133:d4dda5c437f0 | 367 | if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT) |
mbed_official | 133:d4dda5c437f0 | 368 | { |
mbed_official | 133:d4dda5c437f0 | 369 | /* 16-bit scale for the filter */ |
mbed_official | 133:d4dda5c437f0 | 370 | CAN1->FS1R &= ~(uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 371 | |
mbed_official | 133:d4dda5c437f0 | 372 | /* First 16-bit identifier and First 16-bit mask */ |
mbed_official | 133:d4dda5c437f0 | 373 | /* Or First 16-bit identifier and Second 16-bit identifier */ |
mbed_official | 133:d4dda5c437f0 | 374 | CAN1->sFilterRegister[sFilterConfig->FilterNumber].FR1 = |
mbed_official | 133:d4dda5c437f0 | 375 | ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16) | |
mbed_official | 133:d4dda5c437f0 | 376 | (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow); |
mbed_official | 133:d4dda5c437f0 | 377 | |
mbed_official | 133:d4dda5c437f0 | 378 | /* Second 16-bit identifier and Second 16-bit mask */ |
mbed_official | 133:d4dda5c437f0 | 379 | /* Or Third 16-bit identifier and Fourth 16-bit identifier */ |
mbed_official | 133:d4dda5c437f0 | 380 | CAN1->sFilterRegister[sFilterConfig->FilterNumber].FR2 = |
mbed_official | 133:d4dda5c437f0 | 381 | ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) | |
mbed_official | 133:d4dda5c437f0 | 382 | (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh); |
mbed_official | 133:d4dda5c437f0 | 383 | } |
mbed_official | 133:d4dda5c437f0 | 384 | |
mbed_official | 133:d4dda5c437f0 | 385 | if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT) |
mbed_official | 133:d4dda5c437f0 | 386 | { |
mbed_official | 133:d4dda5c437f0 | 387 | /* 32-bit scale for the filter */ |
mbed_official | 133:d4dda5c437f0 | 388 | CAN1->FS1R |= filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 389 | /* 32-bit identifier or First 32-bit identifier */ |
mbed_official | 133:d4dda5c437f0 | 390 | CAN1->sFilterRegister[sFilterConfig->FilterNumber].FR1 = |
mbed_official | 133:d4dda5c437f0 | 391 | ((0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh) << 16) | |
mbed_official | 133:d4dda5c437f0 | 392 | (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow); |
mbed_official | 133:d4dda5c437f0 | 393 | /* 32-bit mask or Second 32-bit identifier */ |
mbed_official | 133:d4dda5c437f0 | 394 | CAN1->sFilterRegister[sFilterConfig->FilterNumber].FR2 = |
mbed_official | 133:d4dda5c437f0 | 395 | ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) | |
mbed_official | 133:d4dda5c437f0 | 396 | (0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow); |
mbed_official | 133:d4dda5c437f0 | 397 | } |
mbed_official | 133:d4dda5c437f0 | 398 | |
mbed_official | 133:d4dda5c437f0 | 399 | /* Filter Mode */ |
mbed_official | 133:d4dda5c437f0 | 400 | if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK) |
mbed_official | 133:d4dda5c437f0 | 401 | { |
mbed_official | 133:d4dda5c437f0 | 402 | /*Id/Mask mode for the filter*/ |
mbed_official | 133:d4dda5c437f0 | 403 | CAN1->FM1R &= ~(uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 404 | } |
mbed_official | 133:d4dda5c437f0 | 405 | else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */ |
mbed_official | 133:d4dda5c437f0 | 406 | { |
mbed_official | 133:d4dda5c437f0 | 407 | /*Identifier list mode for the filter*/ |
mbed_official | 133:d4dda5c437f0 | 408 | CAN1->FM1R |= (uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 409 | } |
mbed_official | 133:d4dda5c437f0 | 410 | |
mbed_official | 133:d4dda5c437f0 | 411 | /* Filter FIFO assignment */ |
mbed_official | 133:d4dda5c437f0 | 412 | if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0) |
mbed_official | 133:d4dda5c437f0 | 413 | { |
mbed_official | 133:d4dda5c437f0 | 414 | /* FIFO 0 assignation for the filter */ |
mbed_official | 133:d4dda5c437f0 | 415 | CAN1->FFA1R &= ~(uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 416 | } |
mbed_official | 133:d4dda5c437f0 | 417 | |
mbed_official | 133:d4dda5c437f0 | 418 | if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO1) |
mbed_official | 133:d4dda5c437f0 | 419 | { |
mbed_official | 133:d4dda5c437f0 | 420 | /* FIFO 1 assignation for the filter */ |
mbed_official | 133:d4dda5c437f0 | 421 | CAN1->FFA1R |= (uint32_t)filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 422 | } |
mbed_official | 133:d4dda5c437f0 | 423 | |
mbed_official | 133:d4dda5c437f0 | 424 | /* Filter activation */ |
mbed_official | 133:d4dda5c437f0 | 425 | if (sFilterConfig->FilterActivation == ENABLE) |
mbed_official | 133:d4dda5c437f0 | 426 | { |
mbed_official | 133:d4dda5c437f0 | 427 | CAN1->FA1R |= filternbrbitpos; |
mbed_official | 133:d4dda5c437f0 | 428 | } |
mbed_official | 133:d4dda5c437f0 | 429 | |
mbed_official | 133:d4dda5c437f0 | 430 | /* Leave the initialisation mode for the filter */ |
mbed_official | 133:d4dda5c437f0 | 431 | CAN1->FMR &= ~((uint32_t)CAN_FMR_FINIT); |
mbed_official | 133:d4dda5c437f0 | 432 | |
mbed_official | 133:d4dda5c437f0 | 433 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 434 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 435 | } |
mbed_official | 133:d4dda5c437f0 | 436 | |
mbed_official | 133:d4dda5c437f0 | 437 | /** |
mbed_official | 133:d4dda5c437f0 | 438 | * @brief Deinitializes the CANx peripheral registers to their default reset values. |
mbed_official | 133:d4dda5c437f0 | 439 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 440 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 441 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 442 | */ |
mbed_official | 133:d4dda5c437f0 | 443 | HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 444 | { |
mbed_official | 133:d4dda5c437f0 | 445 | /* Check CAN handle */ |
mbed_official | 133:d4dda5c437f0 | 446 | if(hcan == NULL) |
mbed_official | 133:d4dda5c437f0 | 447 | { |
mbed_official | 133:d4dda5c437f0 | 448 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 449 | } |
mbed_official | 133:d4dda5c437f0 | 450 | |
mbed_official | 133:d4dda5c437f0 | 451 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 452 | assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance)); |
mbed_official | 133:d4dda5c437f0 | 453 | |
mbed_official | 133:d4dda5c437f0 | 454 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 455 | hcan->State = HAL_CAN_STATE_BUSY; |
mbed_official | 133:d4dda5c437f0 | 456 | |
mbed_official | 133:d4dda5c437f0 | 457 | /* DeInit the low level hardware */ |
mbed_official | 133:d4dda5c437f0 | 458 | HAL_CAN_MspDeInit(hcan); |
mbed_official | 133:d4dda5c437f0 | 459 | |
mbed_official | 133:d4dda5c437f0 | 460 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 461 | hcan->State = HAL_CAN_STATE_RESET; |
mbed_official | 133:d4dda5c437f0 | 462 | |
mbed_official | 133:d4dda5c437f0 | 463 | /* Release Lock */ |
mbed_official | 133:d4dda5c437f0 | 464 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 465 | |
mbed_official | 133:d4dda5c437f0 | 466 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 467 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 468 | } |
mbed_official | 133:d4dda5c437f0 | 469 | |
mbed_official | 133:d4dda5c437f0 | 470 | /** |
mbed_official | 133:d4dda5c437f0 | 471 | * @brief Initializes the CAN MSP. |
mbed_official | 133:d4dda5c437f0 | 472 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 473 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 474 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 475 | */ |
mbed_official | 133:d4dda5c437f0 | 476 | __weak void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 477 | { |
mbed_official | 133:d4dda5c437f0 | 478 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 133:d4dda5c437f0 | 479 | the HAL_CAN_MspInit could be implemented in the user file |
mbed_official | 133:d4dda5c437f0 | 480 | */ |
mbed_official | 133:d4dda5c437f0 | 481 | } |
mbed_official | 133:d4dda5c437f0 | 482 | |
mbed_official | 133:d4dda5c437f0 | 483 | /** |
mbed_official | 133:d4dda5c437f0 | 484 | * @brief DeInitializes the CAN MSP. |
mbed_official | 133:d4dda5c437f0 | 485 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 486 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 487 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 488 | */ |
mbed_official | 133:d4dda5c437f0 | 489 | __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 490 | { |
mbed_official | 133:d4dda5c437f0 | 491 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 133:d4dda5c437f0 | 492 | the HAL_CAN_MspDeInit could be implemented in the user file |
mbed_official | 133:d4dda5c437f0 | 493 | */ |
mbed_official | 133:d4dda5c437f0 | 494 | } |
mbed_official | 133:d4dda5c437f0 | 495 | |
mbed_official | 133:d4dda5c437f0 | 496 | /** |
mbed_official | 133:d4dda5c437f0 | 497 | * @} |
mbed_official | 133:d4dda5c437f0 | 498 | */ |
mbed_official | 133:d4dda5c437f0 | 499 | |
mbed_official | 133:d4dda5c437f0 | 500 | /** @defgroup CAN_Group2 IO operation functions |
mbed_official | 133:d4dda5c437f0 | 501 | * @brief IO operation functions |
mbed_official | 133:d4dda5c437f0 | 502 | * |
mbed_official | 133:d4dda5c437f0 | 503 | @verbatim |
mbed_official | 133:d4dda5c437f0 | 504 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 505 | ##### IO operation functions ##### |
mbed_official | 133:d4dda5c437f0 | 506 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 507 | [..] This section provides functions allowing to: |
mbed_official | 133:d4dda5c437f0 | 508 | (+) Transmit a CAN frame message. |
mbed_official | 133:d4dda5c437f0 | 509 | (+) Receive a CAN frame message. |
mbed_official | 133:d4dda5c437f0 | 510 | (+) Enter CAN peripheral in sleep mode. |
mbed_official | 133:d4dda5c437f0 | 511 | (+) Wake up the CAN peripheral from sleep mode. |
mbed_official | 133:d4dda5c437f0 | 512 | |
mbed_official | 133:d4dda5c437f0 | 513 | @endverbatim |
mbed_official | 133:d4dda5c437f0 | 514 | * @{ |
mbed_official | 133:d4dda5c437f0 | 515 | */ |
mbed_official | 133:d4dda5c437f0 | 516 | |
mbed_official | 133:d4dda5c437f0 | 517 | /** |
mbed_official | 133:d4dda5c437f0 | 518 | * @brief Initiates and transmits a CAN frame message. |
mbed_official | 133:d4dda5c437f0 | 519 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 520 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 521 | * @param Timeout: Specify Timeout value |
mbed_official | 133:d4dda5c437f0 | 522 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 523 | */ |
mbed_official | 133:d4dda5c437f0 | 524 | HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef* hcan, uint32_t Timeout) |
mbed_official | 133:d4dda5c437f0 | 525 | { |
mbed_official | 133:d4dda5c437f0 | 526 | uint32_t transmitmailbox = 5; |
mbed_official | 133:d4dda5c437f0 | 527 | |
mbed_official | 133:d4dda5c437f0 | 528 | uint32_t timeout; |
mbed_official | 133:d4dda5c437f0 | 529 | |
mbed_official | 133:d4dda5c437f0 | 530 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 531 | assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE)); |
mbed_official | 133:d4dda5c437f0 | 532 | assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR)); |
mbed_official | 133:d4dda5c437f0 | 533 | assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC)); |
mbed_official | 133:d4dda5c437f0 | 534 | |
mbed_official | 133:d4dda5c437f0 | 535 | /* Process locked */ |
mbed_official | 133:d4dda5c437f0 | 536 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 537 | |
mbed_official | 133:d4dda5c437f0 | 538 | if(hcan->State == HAL_CAN_STATE_BUSY_RX) |
mbed_official | 133:d4dda5c437f0 | 539 | { |
mbed_official | 133:d4dda5c437f0 | 540 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 541 | hcan->State = HAL_CAN_STATE_BUSY_TX_RX; |
mbed_official | 133:d4dda5c437f0 | 542 | } |
mbed_official | 133:d4dda5c437f0 | 543 | else |
mbed_official | 133:d4dda5c437f0 | 544 | { |
mbed_official | 133:d4dda5c437f0 | 545 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 546 | hcan->State = HAL_CAN_STATE_BUSY_TX; |
mbed_official | 133:d4dda5c437f0 | 547 | } |
mbed_official | 133:d4dda5c437f0 | 548 | |
mbed_official | 133:d4dda5c437f0 | 549 | /* Select one empty transmit mailbox */ |
mbed_official | 133:d4dda5c437f0 | 550 | if ((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) |
mbed_official | 133:d4dda5c437f0 | 551 | { |
mbed_official | 133:d4dda5c437f0 | 552 | transmitmailbox = 0; |
mbed_official | 133:d4dda5c437f0 | 553 | } |
mbed_official | 133:d4dda5c437f0 | 554 | else if ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) |
mbed_official | 133:d4dda5c437f0 | 555 | { |
mbed_official | 133:d4dda5c437f0 | 556 | transmitmailbox = 1; |
mbed_official | 133:d4dda5c437f0 | 557 | } |
mbed_official | 133:d4dda5c437f0 | 558 | else if ((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) |
mbed_official | 133:d4dda5c437f0 | 559 | { |
mbed_official | 133:d4dda5c437f0 | 560 | transmitmailbox = 2; |
mbed_official | 133:d4dda5c437f0 | 561 | } |
mbed_official | 133:d4dda5c437f0 | 562 | else |
mbed_official | 133:d4dda5c437f0 | 563 | { |
mbed_official | 133:d4dda5c437f0 | 564 | transmitmailbox = CAN_TXSTATUS_NOMAILBOX; |
mbed_official | 133:d4dda5c437f0 | 565 | } |
mbed_official | 133:d4dda5c437f0 | 566 | |
mbed_official | 133:d4dda5c437f0 | 567 | if (transmitmailbox != CAN_TXSTATUS_NOMAILBOX) |
mbed_official | 133:d4dda5c437f0 | 568 | { |
mbed_official | 133:d4dda5c437f0 | 569 | /* Set up the Id */ |
mbed_official | 133:d4dda5c437f0 | 570 | hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; |
mbed_official | 133:d4dda5c437f0 | 571 | if (hcan->pTxMsg->IDE == CAN_ID_STD) |
mbed_official | 133:d4dda5c437f0 | 572 | { |
mbed_official | 133:d4dda5c437f0 | 573 | assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId)); |
mbed_official | 133:d4dda5c437f0 | 574 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21) | \ |
mbed_official | 133:d4dda5c437f0 | 575 | hcan->pTxMsg->RTR); |
mbed_official | 133:d4dda5c437f0 | 576 | } |
mbed_official | 133:d4dda5c437f0 | 577 | else |
mbed_official | 133:d4dda5c437f0 | 578 | { |
mbed_official | 133:d4dda5c437f0 | 579 | assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId)); |
mbed_official | 133:d4dda5c437f0 | 580 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3) | \ |
mbed_official | 133:d4dda5c437f0 | 581 | hcan->pTxMsg->IDE | \ |
mbed_official | 133:d4dda5c437f0 | 582 | hcan->pTxMsg->RTR); |
mbed_official | 133:d4dda5c437f0 | 583 | } |
mbed_official | 133:d4dda5c437f0 | 584 | |
mbed_official | 133:d4dda5c437f0 | 585 | /* Set up the DLC */ |
mbed_official | 133:d4dda5c437f0 | 586 | hcan->pTxMsg->DLC &= (uint8_t)0x0000000F; |
mbed_official | 133:d4dda5c437f0 | 587 | hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; |
mbed_official | 133:d4dda5c437f0 | 588 | hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC; |
mbed_official | 133:d4dda5c437f0 | 589 | |
mbed_official | 133:d4dda5c437f0 | 590 | /* Set up the data field */ |
mbed_official | 133:d4dda5c437f0 | 591 | hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3] << 24) | |
mbed_official | 133:d4dda5c437f0 | 592 | ((uint32_t)hcan->pTxMsg->Data[2] << 16) | |
mbed_official | 133:d4dda5c437f0 | 593 | ((uint32_t)hcan->pTxMsg->Data[1] << 8) | |
mbed_official | 133:d4dda5c437f0 | 594 | ((uint32_t)hcan->pTxMsg->Data[0])); |
mbed_official | 133:d4dda5c437f0 | 595 | hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7] << 24) | |
mbed_official | 133:d4dda5c437f0 | 596 | ((uint32_t)hcan->pTxMsg->Data[6] << 16) | |
mbed_official | 133:d4dda5c437f0 | 597 | ((uint32_t)hcan->pTxMsg->Data[5] << 8) | |
mbed_official | 133:d4dda5c437f0 | 598 | ((uint32_t)hcan->pTxMsg->Data[4])); |
mbed_official | 133:d4dda5c437f0 | 599 | /* Request transmission */ |
mbed_official | 133:d4dda5c437f0 | 600 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; |
mbed_official | 133:d4dda5c437f0 | 601 | |
mbed_official | 133:d4dda5c437f0 | 602 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 603 | timeout = HAL_GetTick() + Timeout; |
mbed_official | 133:d4dda5c437f0 | 604 | |
mbed_official | 133:d4dda5c437f0 | 605 | /* Check End of transmission flag */ |
mbed_official | 133:d4dda5c437f0 | 606 | while(!(__HAL_CAN_TRANSMIT_STATUS(hcan, transmitmailbox))) |
mbed_official | 133:d4dda5c437f0 | 607 | { |
mbed_official | 133:d4dda5c437f0 | 608 | /* Check for the Timeout */ |
mbed_official | 133:d4dda5c437f0 | 609 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 133:d4dda5c437f0 | 610 | { |
mbed_official | 133:d4dda5c437f0 | 611 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 612 | { |
mbed_official | 133:d4dda5c437f0 | 613 | hcan->State = HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 614 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 615 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 616 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 617 | } |
mbed_official | 133:d4dda5c437f0 | 618 | } |
mbed_official | 133:d4dda5c437f0 | 619 | } |
mbed_official | 133:d4dda5c437f0 | 620 | if(hcan->State == HAL_CAN_STATE_BUSY_TX_RX) |
mbed_official | 133:d4dda5c437f0 | 621 | { |
mbed_official | 133:d4dda5c437f0 | 622 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 623 | hcan->State = HAL_CAN_STATE_BUSY_RX; |
mbed_official | 133:d4dda5c437f0 | 624 | |
mbed_official | 133:d4dda5c437f0 | 625 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 626 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 627 | } |
mbed_official | 133:d4dda5c437f0 | 628 | else |
mbed_official | 133:d4dda5c437f0 | 629 | { |
mbed_official | 133:d4dda5c437f0 | 630 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 631 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 632 | |
mbed_official | 133:d4dda5c437f0 | 633 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 634 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 635 | } |
mbed_official | 133:d4dda5c437f0 | 636 | |
mbed_official | 133:d4dda5c437f0 | 637 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 638 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 639 | } |
mbed_official | 133:d4dda5c437f0 | 640 | else |
mbed_official | 133:d4dda5c437f0 | 641 | { |
mbed_official | 133:d4dda5c437f0 | 642 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 643 | hcan->State = HAL_CAN_STATE_ERROR; |
mbed_official | 133:d4dda5c437f0 | 644 | |
mbed_official | 133:d4dda5c437f0 | 645 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 646 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 647 | } |
mbed_official | 133:d4dda5c437f0 | 648 | } |
mbed_official | 133:d4dda5c437f0 | 649 | |
mbed_official | 133:d4dda5c437f0 | 650 | /** |
mbed_official | 133:d4dda5c437f0 | 651 | * @brief Initiates and transmits a CAN frame message. |
mbed_official | 133:d4dda5c437f0 | 652 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 653 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 654 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 655 | */ |
mbed_official | 133:d4dda5c437f0 | 656 | HAL_StatusTypeDef HAL_CAN_Transmit_IT(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 657 | { |
mbed_official | 133:d4dda5c437f0 | 658 | uint32_t transmitmailbox = 5; |
mbed_official | 133:d4dda5c437f0 | 659 | uint32_t tmp = 0; |
mbed_official | 133:d4dda5c437f0 | 660 | |
mbed_official | 133:d4dda5c437f0 | 661 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 662 | assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE)); |
mbed_official | 133:d4dda5c437f0 | 663 | assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR)); |
mbed_official | 133:d4dda5c437f0 | 664 | assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC)); |
mbed_official | 133:d4dda5c437f0 | 665 | |
mbed_official | 133:d4dda5c437f0 | 666 | tmp = hcan->State; |
mbed_official | 133:d4dda5c437f0 | 667 | if((tmp == HAL_CAN_STATE_READY) || (tmp == HAL_CAN_STATE_BUSY_RX)) |
mbed_official | 133:d4dda5c437f0 | 668 | { |
mbed_official | 133:d4dda5c437f0 | 669 | /* Process Locked */ |
mbed_official | 133:d4dda5c437f0 | 670 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 671 | |
mbed_official | 133:d4dda5c437f0 | 672 | /* Select one empty transmit mailbox */ |
mbed_official | 133:d4dda5c437f0 | 673 | if((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) |
mbed_official | 133:d4dda5c437f0 | 674 | { |
mbed_official | 133:d4dda5c437f0 | 675 | transmitmailbox = 0; |
mbed_official | 133:d4dda5c437f0 | 676 | } |
mbed_official | 133:d4dda5c437f0 | 677 | else if((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) |
mbed_official | 133:d4dda5c437f0 | 678 | { |
mbed_official | 133:d4dda5c437f0 | 679 | transmitmailbox = 1; |
mbed_official | 133:d4dda5c437f0 | 680 | } |
mbed_official | 133:d4dda5c437f0 | 681 | else if((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2) |
mbed_official | 133:d4dda5c437f0 | 682 | { |
mbed_official | 133:d4dda5c437f0 | 683 | transmitmailbox = 2; |
mbed_official | 133:d4dda5c437f0 | 684 | } |
mbed_official | 133:d4dda5c437f0 | 685 | else |
mbed_official | 133:d4dda5c437f0 | 686 | { |
mbed_official | 133:d4dda5c437f0 | 687 | transmitmailbox = CAN_TXSTATUS_NOMAILBOX; |
mbed_official | 133:d4dda5c437f0 | 688 | } |
mbed_official | 133:d4dda5c437f0 | 689 | |
mbed_official | 133:d4dda5c437f0 | 690 | if(transmitmailbox != CAN_TXSTATUS_NOMAILBOX) |
mbed_official | 133:d4dda5c437f0 | 691 | { |
mbed_official | 133:d4dda5c437f0 | 692 | /* Set up the Id */ |
mbed_official | 133:d4dda5c437f0 | 693 | hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ; |
mbed_official | 133:d4dda5c437f0 | 694 | if(hcan->pTxMsg->IDE == CAN_ID_STD) |
mbed_official | 133:d4dda5c437f0 | 695 | { |
mbed_official | 133:d4dda5c437f0 | 696 | assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId)); |
mbed_official | 133:d4dda5c437f0 | 697 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21) | \ |
mbed_official | 133:d4dda5c437f0 | 698 | hcan->pTxMsg->RTR); |
mbed_official | 133:d4dda5c437f0 | 699 | } |
mbed_official | 133:d4dda5c437f0 | 700 | else |
mbed_official | 133:d4dda5c437f0 | 701 | { |
mbed_official | 133:d4dda5c437f0 | 702 | assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId)); |
mbed_official | 133:d4dda5c437f0 | 703 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3) | \ |
mbed_official | 133:d4dda5c437f0 | 704 | hcan->pTxMsg->IDE | \ |
mbed_official | 133:d4dda5c437f0 | 705 | hcan->pTxMsg->RTR); |
mbed_official | 133:d4dda5c437f0 | 706 | } |
mbed_official | 133:d4dda5c437f0 | 707 | |
mbed_official | 133:d4dda5c437f0 | 708 | /* Set up the DLC */ |
mbed_official | 133:d4dda5c437f0 | 709 | hcan->pTxMsg->DLC &= (uint8_t)0x0000000F; |
mbed_official | 133:d4dda5c437f0 | 710 | hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; |
mbed_official | 133:d4dda5c437f0 | 711 | hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC; |
mbed_official | 133:d4dda5c437f0 | 712 | |
mbed_official | 133:d4dda5c437f0 | 713 | /* Set up the data field */ |
mbed_official | 133:d4dda5c437f0 | 714 | hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3] << 24) | |
mbed_official | 133:d4dda5c437f0 | 715 | ((uint32_t)hcan->pTxMsg->Data[2] << 16) | |
mbed_official | 133:d4dda5c437f0 | 716 | ((uint32_t)hcan->pTxMsg->Data[1] << 8) | |
mbed_official | 133:d4dda5c437f0 | 717 | ((uint32_t)hcan->pTxMsg->Data[0])); |
mbed_official | 133:d4dda5c437f0 | 718 | hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7] << 24) | |
mbed_official | 133:d4dda5c437f0 | 719 | ((uint32_t)hcan->pTxMsg->Data[6] << 16) | |
mbed_official | 133:d4dda5c437f0 | 720 | ((uint32_t)hcan->pTxMsg->Data[5] << 8) | |
mbed_official | 133:d4dda5c437f0 | 721 | ((uint32_t)hcan->pTxMsg->Data[4])); |
mbed_official | 133:d4dda5c437f0 | 722 | |
mbed_official | 133:d4dda5c437f0 | 723 | if(hcan->State == HAL_CAN_STATE_BUSY_RX) |
mbed_official | 133:d4dda5c437f0 | 724 | { |
mbed_official | 133:d4dda5c437f0 | 725 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 726 | hcan->State = HAL_CAN_STATE_BUSY_TX_RX; |
mbed_official | 133:d4dda5c437f0 | 727 | } |
mbed_official | 133:d4dda5c437f0 | 728 | else |
mbed_official | 133:d4dda5c437f0 | 729 | { |
mbed_official | 133:d4dda5c437f0 | 730 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 731 | hcan->State = HAL_CAN_STATE_BUSY_TX; |
mbed_official | 133:d4dda5c437f0 | 732 | } |
mbed_official | 133:d4dda5c437f0 | 733 | |
mbed_official | 133:d4dda5c437f0 | 734 | /* Set CAN error code to none */ |
mbed_official | 133:d4dda5c437f0 | 735 | hcan->ErrorCode = HAL_CAN_ERROR_NONE; |
mbed_official | 133:d4dda5c437f0 | 736 | |
mbed_official | 133:d4dda5c437f0 | 737 | /* Process Unlocked */ |
mbed_official | 133:d4dda5c437f0 | 738 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 739 | |
mbed_official | 133:d4dda5c437f0 | 740 | /* Enable Error warning Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 741 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG); |
mbed_official | 133:d4dda5c437f0 | 742 | |
mbed_official | 133:d4dda5c437f0 | 743 | /* Enable Error passive Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 744 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EPV); |
mbed_official | 133:d4dda5c437f0 | 745 | |
mbed_official | 133:d4dda5c437f0 | 746 | /* Enable Bus-off Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 747 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_BOF); |
mbed_official | 133:d4dda5c437f0 | 748 | |
mbed_official | 133:d4dda5c437f0 | 749 | /* Enable Last error code Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 750 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_LEC); |
mbed_official | 133:d4dda5c437f0 | 751 | |
mbed_official | 133:d4dda5c437f0 | 752 | /* Enable Error Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 753 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 754 | |
mbed_official | 133:d4dda5c437f0 | 755 | /* Enable Transmit mailbox empty Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 756 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_TME); |
mbed_official | 133:d4dda5c437f0 | 757 | |
mbed_official | 133:d4dda5c437f0 | 758 | /* Request transmission */ |
mbed_official | 133:d4dda5c437f0 | 759 | hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ; |
mbed_official | 133:d4dda5c437f0 | 760 | } |
mbed_official | 133:d4dda5c437f0 | 761 | } |
mbed_official | 133:d4dda5c437f0 | 762 | else |
mbed_official | 133:d4dda5c437f0 | 763 | { |
mbed_official | 133:d4dda5c437f0 | 764 | return HAL_BUSY; |
mbed_official | 133:d4dda5c437f0 | 765 | } |
mbed_official | 133:d4dda5c437f0 | 766 | |
mbed_official | 133:d4dda5c437f0 | 767 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 768 | } |
mbed_official | 133:d4dda5c437f0 | 769 | |
mbed_official | 133:d4dda5c437f0 | 770 | /** |
mbed_official | 133:d4dda5c437f0 | 771 | * @brief Receives a correct CAN frame. |
mbed_official | 133:d4dda5c437f0 | 772 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 773 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 774 | * @param FIFONumber: FIFO Number value |
mbed_official | 133:d4dda5c437f0 | 775 | * @param Timeout: Specify Timeout value |
mbed_official | 133:d4dda5c437f0 | 776 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 777 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 778 | */ |
mbed_official | 133:d4dda5c437f0 | 779 | HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef* hcan, uint8_t FIFONumber, uint32_t Timeout) |
mbed_official | 133:d4dda5c437f0 | 780 | { |
mbed_official | 133:d4dda5c437f0 | 781 | uint32_t timeout; |
mbed_official | 133:d4dda5c437f0 | 782 | |
mbed_official | 133:d4dda5c437f0 | 783 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 784 | assert_param(IS_CAN_FIFO(FIFONumber)); |
mbed_official | 133:d4dda5c437f0 | 785 | |
mbed_official | 133:d4dda5c437f0 | 786 | /* Process locked */ |
mbed_official | 133:d4dda5c437f0 | 787 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 788 | |
mbed_official | 133:d4dda5c437f0 | 789 | if(hcan->State == HAL_CAN_STATE_BUSY_TX) |
mbed_official | 133:d4dda5c437f0 | 790 | { |
mbed_official | 133:d4dda5c437f0 | 791 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 792 | hcan->State = HAL_CAN_STATE_BUSY_TX_RX; |
mbed_official | 133:d4dda5c437f0 | 793 | } |
mbed_official | 133:d4dda5c437f0 | 794 | else |
mbed_official | 133:d4dda5c437f0 | 795 | { |
mbed_official | 133:d4dda5c437f0 | 796 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 797 | hcan->State = HAL_CAN_STATE_BUSY_RX; |
mbed_official | 133:d4dda5c437f0 | 798 | } |
mbed_official | 133:d4dda5c437f0 | 799 | |
mbed_official | 133:d4dda5c437f0 | 800 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 801 | timeout = HAL_GetTick() + Timeout; |
mbed_official | 133:d4dda5c437f0 | 802 | |
mbed_official | 133:d4dda5c437f0 | 803 | /* Check pending message */ |
mbed_official | 133:d4dda5c437f0 | 804 | while(__HAL_CAN_MSG_PENDING(hcan, FIFONumber) == 0) |
mbed_official | 133:d4dda5c437f0 | 805 | { |
mbed_official | 133:d4dda5c437f0 | 806 | /* Check for the Timeout */ |
mbed_official | 133:d4dda5c437f0 | 807 | if(Timeout != HAL_MAX_DELAY) |
mbed_official | 133:d4dda5c437f0 | 808 | { |
mbed_official | 133:d4dda5c437f0 | 809 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 810 | { |
mbed_official | 133:d4dda5c437f0 | 811 | hcan->State = HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 812 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 813 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 814 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 815 | } |
mbed_official | 133:d4dda5c437f0 | 816 | } |
mbed_official | 133:d4dda5c437f0 | 817 | } |
mbed_official | 133:d4dda5c437f0 | 818 | |
mbed_official | 133:d4dda5c437f0 | 819 | /* Get the Id */ |
mbed_official | 133:d4dda5c437f0 | 820 | hcan->pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR; |
mbed_official | 133:d4dda5c437f0 | 821 | if (hcan->pRxMsg->IDE == CAN_ID_STD) |
mbed_official | 133:d4dda5c437f0 | 822 | { |
mbed_official | 133:d4dda5c437f0 | 823 | hcan->pRxMsg->StdId = (uint32_t)0x000007FF & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21); |
mbed_official | 133:d4dda5c437f0 | 824 | } |
mbed_official | 133:d4dda5c437f0 | 825 | else |
mbed_official | 133:d4dda5c437f0 | 826 | { |
mbed_official | 133:d4dda5c437f0 | 827 | hcan->pRxMsg->ExtId = (uint32_t)0x1FFFFFFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3); |
mbed_official | 133:d4dda5c437f0 | 828 | } |
mbed_official | 133:d4dda5c437f0 | 829 | |
mbed_official | 133:d4dda5c437f0 | 830 | hcan->pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR; |
mbed_official | 133:d4dda5c437f0 | 831 | /* Get the DLC */ |
mbed_official | 133:d4dda5c437f0 | 832 | hcan->pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR; |
mbed_official | 133:d4dda5c437f0 | 833 | /* Get the FMI */ |
mbed_official | 133:d4dda5c437f0 | 834 | hcan->pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8); |
mbed_official | 133:d4dda5c437f0 | 835 | /* Get the data field */ |
mbed_official | 133:d4dda5c437f0 | 836 | hcan->pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR; |
mbed_official | 133:d4dda5c437f0 | 837 | hcan->pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8); |
mbed_official | 133:d4dda5c437f0 | 838 | hcan->pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16); |
mbed_official | 133:d4dda5c437f0 | 839 | hcan->pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24); |
mbed_official | 133:d4dda5c437f0 | 840 | hcan->pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR; |
mbed_official | 133:d4dda5c437f0 | 841 | hcan->pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8); |
mbed_official | 133:d4dda5c437f0 | 842 | hcan->pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16); |
mbed_official | 133:d4dda5c437f0 | 843 | hcan->pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24); |
mbed_official | 133:d4dda5c437f0 | 844 | |
mbed_official | 133:d4dda5c437f0 | 845 | /* Release the FIFO */ |
mbed_official | 133:d4dda5c437f0 | 846 | if(FIFONumber == CAN_FIFO0) |
mbed_official | 133:d4dda5c437f0 | 847 | { |
mbed_official | 133:d4dda5c437f0 | 848 | /* Release FIFO0 */ |
mbed_official | 133:d4dda5c437f0 | 849 | __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0); |
mbed_official | 133:d4dda5c437f0 | 850 | } |
mbed_official | 133:d4dda5c437f0 | 851 | else /* FIFONumber == CAN_FIFO1 */ |
mbed_official | 133:d4dda5c437f0 | 852 | { |
mbed_official | 133:d4dda5c437f0 | 853 | /* Release FIFO1 */ |
mbed_official | 133:d4dda5c437f0 | 854 | __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1); |
mbed_official | 133:d4dda5c437f0 | 855 | } |
mbed_official | 133:d4dda5c437f0 | 856 | |
mbed_official | 133:d4dda5c437f0 | 857 | if(hcan->State == HAL_CAN_STATE_BUSY_TX_RX) |
mbed_official | 133:d4dda5c437f0 | 858 | { |
mbed_official | 133:d4dda5c437f0 | 859 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 860 | hcan->State = HAL_CAN_STATE_BUSY_TX; |
mbed_official | 133:d4dda5c437f0 | 861 | |
mbed_official | 133:d4dda5c437f0 | 862 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 863 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 864 | } |
mbed_official | 133:d4dda5c437f0 | 865 | else |
mbed_official | 133:d4dda5c437f0 | 866 | { |
mbed_official | 133:d4dda5c437f0 | 867 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 868 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 869 | |
mbed_official | 133:d4dda5c437f0 | 870 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 871 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 872 | } |
mbed_official | 133:d4dda5c437f0 | 873 | |
mbed_official | 133:d4dda5c437f0 | 874 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 875 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 876 | } |
mbed_official | 133:d4dda5c437f0 | 877 | |
mbed_official | 133:d4dda5c437f0 | 878 | /** |
mbed_official | 133:d4dda5c437f0 | 879 | * @brief Receives a correct CAN frame. |
mbed_official | 133:d4dda5c437f0 | 880 | * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 881 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 882 | * @param FIFONumber: Specify the FIFO number |
mbed_official | 133:d4dda5c437f0 | 883 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 884 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 885 | */ |
mbed_official | 133:d4dda5c437f0 | 886 | HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber) |
mbed_official | 133:d4dda5c437f0 | 887 | { |
mbed_official | 133:d4dda5c437f0 | 888 | uint32_t tmp = 0; |
mbed_official | 133:d4dda5c437f0 | 889 | |
mbed_official | 133:d4dda5c437f0 | 890 | /* Check the parameters */ |
mbed_official | 133:d4dda5c437f0 | 891 | assert_param(IS_CAN_FIFO(FIFONumber)); |
mbed_official | 133:d4dda5c437f0 | 892 | |
mbed_official | 133:d4dda5c437f0 | 893 | tmp = hcan->State; |
mbed_official | 133:d4dda5c437f0 | 894 | if((tmp == HAL_CAN_STATE_READY) || (tmp == HAL_CAN_STATE_BUSY_TX)) |
mbed_official | 133:d4dda5c437f0 | 895 | { |
mbed_official | 133:d4dda5c437f0 | 896 | /* Process locked */ |
mbed_official | 133:d4dda5c437f0 | 897 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 898 | |
mbed_official | 133:d4dda5c437f0 | 899 | if(hcan->State == HAL_CAN_STATE_BUSY_TX) |
mbed_official | 133:d4dda5c437f0 | 900 | { |
mbed_official | 133:d4dda5c437f0 | 901 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 902 | hcan->State = HAL_CAN_STATE_BUSY_TX_RX; |
mbed_official | 133:d4dda5c437f0 | 903 | } |
mbed_official | 133:d4dda5c437f0 | 904 | else |
mbed_official | 133:d4dda5c437f0 | 905 | { |
mbed_official | 133:d4dda5c437f0 | 906 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 907 | hcan->State = HAL_CAN_STATE_BUSY_RX; |
mbed_official | 133:d4dda5c437f0 | 908 | } |
mbed_official | 133:d4dda5c437f0 | 909 | |
mbed_official | 133:d4dda5c437f0 | 910 | /* Set CAN error code to none */ |
mbed_official | 133:d4dda5c437f0 | 911 | hcan->ErrorCode = HAL_CAN_ERROR_NONE; |
mbed_official | 133:d4dda5c437f0 | 912 | |
mbed_official | 133:d4dda5c437f0 | 913 | /* Enable Error warning Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 914 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG); |
mbed_official | 133:d4dda5c437f0 | 915 | |
mbed_official | 133:d4dda5c437f0 | 916 | /* Enable Error passive Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 917 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EPV); |
mbed_official | 133:d4dda5c437f0 | 918 | |
mbed_official | 133:d4dda5c437f0 | 919 | /* Enable Bus-off Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 920 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_BOF); |
mbed_official | 133:d4dda5c437f0 | 921 | |
mbed_official | 133:d4dda5c437f0 | 922 | /* Enable Last error code Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 923 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_LEC); |
mbed_official | 133:d4dda5c437f0 | 924 | |
mbed_official | 133:d4dda5c437f0 | 925 | /* Enable Error Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 926 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 927 | |
mbed_official | 133:d4dda5c437f0 | 928 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 929 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 930 | |
mbed_official | 133:d4dda5c437f0 | 931 | if(FIFONumber == CAN_FIFO0) |
mbed_official | 133:d4dda5c437f0 | 932 | { |
mbed_official | 133:d4dda5c437f0 | 933 | /* Enable FIFO 0 message pending Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 934 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP0); |
mbed_official | 133:d4dda5c437f0 | 935 | } |
mbed_official | 133:d4dda5c437f0 | 936 | else |
mbed_official | 133:d4dda5c437f0 | 937 | { |
mbed_official | 133:d4dda5c437f0 | 938 | /* Enable FIFO 1 message pending Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 939 | __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FMP1); |
mbed_official | 133:d4dda5c437f0 | 940 | } |
mbed_official | 133:d4dda5c437f0 | 941 | |
mbed_official | 133:d4dda5c437f0 | 942 | } |
mbed_official | 133:d4dda5c437f0 | 943 | else |
mbed_official | 133:d4dda5c437f0 | 944 | { |
mbed_official | 133:d4dda5c437f0 | 945 | return HAL_BUSY; |
mbed_official | 133:d4dda5c437f0 | 946 | } |
mbed_official | 133:d4dda5c437f0 | 947 | |
mbed_official | 133:d4dda5c437f0 | 948 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 949 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 950 | } |
mbed_official | 133:d4dda5c437f0 | 951 | |
mbed_official | 133:d4dda5c437f0 | 952 | /** |
mbed_official | 133:d4dda5c437f0 | 953 | * @brief Enters the Sleep (low power) mode. |
mbed_official | 133:d4dda5c437f0 | 954 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 955 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 956 | * @retval HAL status. |
mbed_official | 133:d4dda5c437f0 | 957 | */ |
mbed_official | 133:d4dda5c437f0 | 958 | HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 959 | { |
mbed_official | 133:d4dda5c437f0 | 960 | uint32_t timeout; |
mbed_official | 133:d4dda5c437f0 | 961 | |
mbed_official | 133:d4dda5c437f0 | 962 | /* Process locked */ |
mbed_official | 133:d4dda5c437f0 | 963 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 964 | |
mbed_official | 133:d4dda5c437f0 | 965 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 966 | hcan->State = HAL_CAN_STATE_BUSY; |
mbed_official | 133:d4dda5c437f0 | 967 | |
mbed_official | 133:d4dda5c437f0 | 968 | /* Request Sleep mode */ |
mbed_official | 133:d4dda5c437f0 | 969 | hcan->Instance->MCR = (((hcan->Instance->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP); |
mbed_official | 133:d4dda5c437f0 | 970 | |
mbed_official | 133:d4dda5c437f0 | 971 | /* Sleep mode status */ |
mbed_official | 133:d4dda5c437f0 | 972 | if ((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK) |
mbed_official | 133:d4dda5c437f0 | 973 | { |
mbed_official | 133:d4dda5c437f0 | 974 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 975 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 976 | } |
mbed_official | 133:d4dda5c437f0 | 977 | |
mbed_official | 133:d4dda5c437f0 | 978 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 979 | timeout = HAL_GetTick() + 10; |
mbed_official | 133:d4dda5c437f0 | 980 | |
mbed_official | 133:d4dda5c437f0 | 981 | /* Wait the acknowledge */ |
mbed_official | 133:d4dda5c437f0 | 982 | while((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK) |
mbed_official | 133:d4dda5c437f0 | 983 | { |
mbed_official | 133:d4dda5c437f0 | 984 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 985 | { |
mbed_official | 133:d4dda5c437f0 | 986 | hcan->State = HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 987 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 988 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 989 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 990 | } |
mbed_official | 133:d4dda5c437f0 | 991 | } |
mbed_official | 133:d4dda5c437f0 | 992 | |
mbed_official | 133:d4dda5c437f0 | 993 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 994 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 995 | |
mbed_official | 133:d4dda5c437f0 | 996 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 997 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 998 | |
mbed_official | 133:d4dda5c437f0 | 999 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 1000 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 1001 | } |
mbed_official | 133:d4dda5c437f0 | 1002 | |
mbed_official | 133:d4dda5c437f0 | 1003 | /** |
mbed_official | 133:d4dda5c437f0 | 1004 | * @brief Wakes up the CAN peripheral from sleep mode, after that the CAN peripheral |
mbed_official | 133:d4dda5c437f0 | 1005 | * is in the normal mode. |
mbed_official | 133:d4dda5c437f0 | 1006 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1007 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1008 | * @retval HAL status. |
mbed_official | 133:d4dda5c437f0 | 1009 | */ |
mbed_official | 133:d4dda5c437f0 | 1010 | HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1011 | { |
mbed_official | 133:d4dda5c437f0 | 1012 | uint32_t timeout; |
mbed_official | 133:d4dda5c437f0 | 1013 | |
mbed_official | 133:d4dda5c437f0 | 1014 | /* Process locked */ |
mbed_official | 133:d4dda5c437f0 | 1015 | __HAL_LOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 1016 | |
mbed_official | 133:d4dda5c437f0 | 1017 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1018 | hcan->State = HAL_CAN_STATE_BUSY; |
mbed_official | 133:d4dda5c437f0 | 1019 | |
mbed_official | 133:d4dda5c437f0 | 1020 | /* Wake up request */ |
mbed_official | 133:d4dda5c437f0 | 1021 | hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_SLEEP; |
mbed_official | 133:d4dda5c437f0 | 1022 | |
mbed_official | 133:d4dda5c437f0 | 1023 | /* Get timeout */ |
mbed_official | 133:d4dda5c437f0 | 1024 | timeout = HAL_GetTick() + 10; |
mbed_official | 133:d4dda5c437f0 | 1025 | |
mbed_official | 133:d4dda5c437f0 | 1026 | /* Sleep mode status */ |
mbed_official | 133:d4dda5c437f0 | 1027 | while((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK) |
mbed_official | 133:d4dda5c437f0 | 1028 | { |
mbed_official | 133:d4dda5c437f0 | 1029 | if(HAL_GetTick() >= timeout) |
mbed_official | 133:d4dda5c437f0 | 1030 | { |
mbed_official | 133:d4dda5c437f0 | 1031 | hcan->State= HAL_CAN_STATE_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 1032 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 1033 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 1034 | return HAL_TIMEOUT; |
mbed_official | 133:d4dda5c437f0 | 1035 | } |
mbed_official | 133:d4dda5c437f0 | 1036 | } |
mbed_official | 133:d4dda5c437f0 | 1037 | if((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK) |
mbed_official | 133:d4dda5c437f0 | 1038 | { |
mbed_official | 133:d4dda5c437f0 | 1039 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 1040 | return HAL_ERROR; |
mbed_official | 133:d4dda5c437f0 | 1041 | } |
mbed_official | 133:d4dda5c437f0 | 1042 | |
mbed_official | 133:d4dda5c437f0 | 1043 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1044 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 1045 | |
mbed_official | 133:d4dda5c437f0 | 1046 | /* Process unlocked */ |
mbed_official | 133:d4dda5c437f0 | 1047 | __HAL_UNLOCK(hcan); |
mbed_official | 133:d4dda5c437f0 | 1048 | |
mbed_official | 133:d4dda5c437f0 | 1049 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 1050 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 1051 | } |
mbed_official | 133:d4dda5c437f0 | 1052 | |
mbed_official | 133:d4dda5c437f0 | 1053 | /** |
mbed_official | 133:d4dda5c437f0 | 1054 | * @brief Handles CAN interrupt request |
mbed_official | 133:d4dda5c437f0 | 1055 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1056 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1057 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 1058 | */ |
mbed_official | 133:d4dda5c437f0 | 1059 | void HAL_CAN_IRQHandler(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1060 | { |
mbed_official | 133:d4dda5c437f0 | 1061 | uint32_t tmp1 = 0, tmp2 = 0, tmp3 = 0; |
mbed_official | 133:d4dda5c437f0 | 1062 | |
mbed_official | 133:d4dda5c437f0 | 1063 | /* Check End of transmission flag */ |
mbed_official | 133:d4dda5c437f0 | 1064 | if(__HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_TME)) |
mbed_official | 133:d4dda5c437f0 | 1065 | { |
mbed_official | 133:d4dda5c437f0 | 1066 | tmp1 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_0); |
mbed_official | 133:d4dda5c437f0 | 1067 | tmp2 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_1); |
mbed_official | 133:d4dda5c437f0 | 1068 | tmp3 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_2); |
mbed_official | 133:d4dda5c437f0 | 1069 | if(tmp1 || tmp2 || tmp3) |
mbed_official | 133:d4dda5c437f0 | 1070 | { |
mbed_official | 133:d4dda5c437f0 | 1071 | /* Call transmit function */ |
mbed_official | 133:d4dda5c437f0 | 1072 | CAN_Transmit_IT(hcan); |
mbed_official | 133:d4dda5c437f0 | 1073 | } |
mbed_official | 133:d4dda5c437f0 | 1074 | } |
mbed_official | 133:d4dda5c437f0 | 1075 | |
mbed_official | 133:d4dda5c437f0 | 1076 | tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO0); |
mbed_official | 133:d4dda5c437f0 | 1077 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP0); |
mbed_official | 133:d4dda5c437f0 | 1078 | /* Check End of reception flag for FIFO0 */ |
mbed_official | 133:d4dda5c437f0 | 1079 | if((tmp1 != 0) && tmp2) |
mbed_official | 133:d4dda5c437f0 | 1080 | { |
mbed_official | 133:d4dda5c437f0 | 1081 | /* Call receive function */ |
mbed_official | 133:d4dda5c437f0 | 1082 | CAN_Receive_IT(hcan, CAN_FIFO0); |
mbed_official | 133:d4dda5c437f0 | 1083 | } |
mbed_official | 133:d4dda5c437f0 | 1084 | |
mbed_official | 133:d4dda5c437f0 | 1085 | tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO1); |
mbed_official | 133:d4dda5c437f0 | 1086 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP1); |
mbed_official | 133:d4dda5c437f0 | 1087 | /* Check End of reception flag for FIFO1 */ |
mbed_official | 133:d4dda5c437f0 | 1088 | if((tmp1 != 0) && tmp2) |
mbed_official | 133:d4dda5c437f0 | 1089 | { |
mbed_official | 133:d4dda5c437f0 | 1090 | /* Call receive function */ |
mbed_official | 133:d4dda5c437f0 | 1091 | CAN_Receive_IT(hcan, CAN_FIFO1); |
mbed_official | 133:d4dda5c437f0 | 1092 | } |
mbed_official | 133:d4dda5c437f0 | 1093 | |
mbed_official | 133:d4dda5c437f0 | 1094 | tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EWG); |
mbed_official | 133:d4dda5c437f0 | 1095 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EWG); |
mbed_official | 133:d4dda5c437f0 | 1096 | tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1097 | /* Check Error Warning Flag */ |
mbed_official | 133:d4dda5c437f0 | 1098 | if(tmp1 && tmp2 && tmp3) |
mbed_official | 133:d4dda5c437f0 | 1099 | { |
mbed_official | 133:d4dda5c437f0 | 1100 | /* Set CAN error code to EWG error */ |
mbed_official | 133:d4dda5c437f0 | 1101 | hcan->ErrorCode |= HAL_CAN_ERROR_EWG; |
mbed_official | 133:d4dda5c437f0 | 1102 | /* Clear Error Warning Flag */ |
mbed_official | 133:d4dda5c437f0 | 1103 | __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_EWG); |
mbed_official | 133:d4dda5c437f0 | 1104 | } |
mbed_official | 133:d4dda5c437f0 | 1105 | |
mbed_official | 133:d4dda5c437f0 | 1106 | tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EPV); |
mbed_official | 133:d4dda5c437f0 | 1107 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EPV); |
mbed_official | 133:d4dda5c437f0 | 1108 | tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1109 | /* Check Error Passive Flag */ |
mbed_official | 133:d4dda5c437f0 | 1110 | if(tmp1 && tmp2 && tmp3) |
mbed_official | 133:d4dda5c437f0 | 1111 | { |
mbed_official | 133:d4dda5c437f0 | 1112 | /* Set CAN error code to EPV error */ |
mbed_official | 133:d4dda5c437f0 | 1113 | hcan->ErrorCode |= HAL_CAN_ERROR_EPV; |
mbed_official | 133:d4dda5c437f0 | 1114 | /* Clear Error Passive Flag */ |
mbed_official | 133:d4dda5c437f0 | 1115 | __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_EPV); |
mbed_official | 133:d4dda5c437f0 | 1116 | } |
mbed_official | 133:d4dda5c437f0 | 1117 | |
mbed_official | 133:d4dda5c437f0 | 1118 | tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_BOF); |
mbed_official | 133:d4dda5c437f0 | 1119 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_BOF); |
mbed_official | 133:d4dda5c437f0 | 1120 | tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1121 | /* Check Bus-Off Flag */ |
mbed_official | 133:d4dda5c437f0 | 1122 | if(tmp1 && tmp2 && tmp3) |
mbed_official | 133:d4dda5c437f0 | 1123 | { |
mbed_official | 133:d4dda5c437f0 | 1124 | /* Set CAN error code to BOF error */ |
mbed_official | 133:d4dda5c437f0 | 1125 | hcan->ErrorCode |= HAL_CAN_ERROR_BOF; |
mbed_official | 133:d4dda5c437f0 | 1126 | /* Clear Bus-Off Flag */ |
mbed_official | 133:d4dda5c437f0 | 1127 | __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_BOF); |
mbed_official | 133:d4dda5c437f0 | 1128 | } |
mbed_official | 133:d4dda5c437f0 | 1129 | |
mbed_official | 133:d4dda5c437f0 | 1130 | tmp1 = HAL_IS_BIT_CLR(hcan->Instance->ESR, CAN_ESR_LEC); |
mbed_official | 133:d4dda5c437f0 | 1131 | tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_LEC); |
mbed_official | 133:d4dda5c437f0 | 1132 | tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1133 | /* Check Last error code Flag */ |
mbed_official | 133:d4dda5c437f0 | 1134 | if((!tmp1) && tmp2 && tmp3) |
mbed_official | 133:d4dda5c437f0 | 1135 | { |
mbed_official | 133:d4dda5c437f0 | 1136 | tmp1 = (hcan->Instance->ESR) & CAN_ESR_LEC; |
mbed_official | 133:d4dda5c437f0 | 1137 | switch(tmp1) |
mbed_official | 133:d4dda5c437f0 | 1138 | { |
mbed_official | 133:d4dda5c437f0 | 1139 | case(CAN_ESR_LEC_0): |
mbed_official | 133:d4dda5c437f0 | 1140 | /* Set CAN error code to STF error */ |
mbed_official | 133:d4dda5c437f0 | 1141 | hcan->ErrorCode |= HAL_CAN_ERROR_STF; |
mbed_official | 133:d4dda5c437f0 | 1142 | break; |
mbed_official | 133:d4dda5c437f0 | 1143 | case(CAN_ESR_LEC_1): |
mbed_official | 133:d4dda5c437f0 | 1144 | /* Set CAN error code to FOR error */ |
mbed_official | 133:d4dda5c437f0 | 1145 | hcan->ErrorCode |= HAL_CAN_ERROR_FOR; |
mbed_official | 133:d4dda5c437f0 | 1146 | break; |
mbed_official | 133:d4dda5c437f0 | 1147 | case(CAN_ESR_LEC_1 | CAN_ESR_LEC_0): |
mbed_official | 133:d4dda5c437f0 | 1148 | /* Set CAN error code to ACK error */ |
mbed_official | 133:d4dda5c437f0 | 1149 | hcan->ErrorCode |= HAL_CAN_ERROR_ACK; |
mbed_official | 133:d4dda5c437f0 | 1150 | break; |
mbed_official | 133:d4dda5c437f0 | 1151 | case(CAN_ESR_LEC_2): |
mbed_official | 133:d4dda5c437f0 | 1152 | /* Set CAN error code to BR error */ |
mbed_official | 133:d4dda5c437f0 | 1153 | hcan->ErrorCode |= HAL_CAN_ERROR_BR; |
mbed_official | 133:d4dda5c437f0 | 1154 | break; |
mbed_official | 133:d4dda5c437f0 | 1155 | case(CAN_ESR_LEC_2 | CAN_ESR_LEC_0): |
mbed_official | 133:d4dda5c437f0 | 1156 | /* Set CAN error code to BD error */ |
mbed_official | 133:d4dda5c437f0 | 1157 | hcan->ErrorCode |= HAL_CAN_ERROR_BD; |
mbed_official | 133:d4dda5c437f0 | 1158 | break; |
mbed_official | 133:d4dda5c437f0 | 1159 | case(CAN_ESR_LEC_2 | CAN_ESR_LEC_1): |
mbed_official | 133:d4dda5c437f0 | 1160 | /* Set CAN error code to CRC error */ |
mbed_official | 133:d4dda5c437f0 | 1161 | hcan->ErrorCode |= HAL_CAN_ERROR_CRC; |
mbed_official | 133:d4dda5c437f0 | 1162 | break; |
mbed_official | 133:d4dda5c437f0 | 1163 | default: |
mbed_official | 133:d4dda5c437f0 | 1164 | break; |
mbed_official | 133:d4dda5c437f0 | 1165 | } |
mbed_official | 133:d4dda5c437f0 | 1166 | |
mbed_official | 133:d4dda5c437f0 | 1167 | /* Clear Last error code Flag */ |
mbed_official | 133:d4dda5c437f0 | 1168 | hcan->Instance->ESR &= ~(CAN_ESR_LEC); |
mbed_official | 133:d4dda5c437f0 | 1169 | } |
mbed_official | 133:d4dda5c437f0 | 1170 | |
mbed_official | 133:d4dda5c437f0 | 1171 | /* Call the Error call Back in case of Errors */ |
mbed_official | 133:d4dda5c437f0 | 1172 | if(hcan->ErrorCode != HAL_CAN_ERROR_NONE) |
mbed_official | 133:d4dda5c437f0 | 1173 | { |
mbed_official | 133:d4dda5c437f0 | 1174 | /* Set the CAN state ready to be able to start again the process */ |
mbed_official | 133:d4dda5c437f0 | 1175 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 1176 | /* Call Error callback function */ |
mbed_official | 133:d4dda5c437f0 | 1177 | HAL_CAN_ErrorCallback(hcan); |
mbed_official | 133:d4dda5c437f0 | 1178 | } |
mbed_official | 133:d4dda5c437f0 | 1179 | } |
mbed_official | 133:d4dda5c437f0 | 1180 | |
mbed_official | 133:d4dda5c437f0 | 1181 | /** |
mbed_official | 133:d4dda5c437f0 | 1182 | * @brief Transmission complete callback in non blocking mode |
mbed_official | 133:d4dda5c437f0 | 1183 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1184 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1185 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 1186 | */ |
mbed_official | 133:d4dda5c437f0 | 1187 | __weak void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1188 | { |
mbed_official | 133:d4dda5c437f0 | 1189 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 133:d4dda5c437f0 | 1190 | the HAL_CAN_TxCpltCallback could be implemented in the user file |
mbed_official | 133:d4dda5c437f0 | 1191 | */ |
mbed_official | 133:d4dda5c437f0 | 1192 | } |
mbed_official | 133:d4dda5c437f0 | 1193 | |
mbed_official | 133:d4dda5c437f0 | 1194 | /** |
mbed_official | 133:d4dda5c437f0 | 1195 | * @brief Transmission complete callback in non blocking mode |
mbed_official | 133:d4dda5c437f0 | 1196 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1197 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1198 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 1199 | */ |
mbed_official | 133:d4dda5c437f0 | 1200 | __weak void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1201 | { |
mbed_official | 133:d4dda5c437f0 | 1202 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 133:d4dda5c437f0 | 1203 | the HAL_CAN_RxCpltCallback could be implemented in the user file |
mbed_official | 133:d4dda5c437f0 | 1204 | */ |
mbed_official | 133:d4dda5c437f0 | 1205 | } |
mbed_official | 133:d4dda5c437f0 | 1206 | |
mbed_official | 133:d4dda5c437f0 | 1207 | /** |
mbed_official | 133:d4dda5c437f0 | 1208 | * @brief Error CAN callback. |
mbed_official | 133:d4dda5c437f0 | 1209 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1210 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1211 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 1212 | */ |
mbed_official | 133:d4dda5c437f0 | 1213 | __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) |
mbed_official | 133:d4dda5c437f0 | 1214 | { |
mbed_official | 133:d4dda5c437f0 | 1215 | /* NOTE : This function Should not be modified, when the callback is needed, |
mbed_official | 133:d4dda5c437f0 | 1216 | the HAL_CAN_ErrorCallback could be implemented in the user file |
mbed_official | 133:d4dda5c437f0 | 1217 | */ |
mbed_official | 133:d4dda5c437f0 | 1218 | } |
mbed_official | 133:d4dda5c437f0 | 1219 | |
mbed_official | 133:d4dda5c437f0 | 1220 | /** |
mbed_official | 133:d4dda5c437f0 | 1221 | * @} |
mbed_official | 133:d4dda5c437f0 | 1222 | */ |
mbed_official | 133:d4dda5c437f0 | 1223 | |
mbed_official | 133:d4dda5c437f0 | 1224 | /** @defgroup CAN_Group3 Peripheral State and Error functions |
mbed_official | 133:d4dda5c437f0 | 1225 | * @brief CAN Peripheral State functions |
mbed_official | 133:d4dda5c437f0 | 1226 | * |
mbed_official | 133:d4dda5c437f0 | 1227 | @verbatim |
mbed_official | 133:d4dda5c437f0 | 1228 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 1229 | ##### Peripheral State and Error functions ##### |
mbed_official | 133:d4dda5c437f0 | 1230 | ============================================================================== |
mbed_official | 133:d4dda5c437f0 | 1231 | [..] |
mbed_official | 133:d4dda5c437f0 | 1232 | This subsection provides functions allowing to |
mbed_official | 133:d4dda5c437f0 | 1233 | (+) Check the CAN state. |
mbed_official | 133:d4dda5c437f0 | 1234 | (+) Check CAN Errors detected during interrupt process |
mbed_official | 133:d4dda5c437f0 | 1235 | |
mbed_official | 133:d4dda5c437f0 | 1236 | @endverbatim |
mbed_official | 133:d4dda5c437f0 | 1237 | * @{ |
mbed_official | 133:d4dda5c437f0 | 1238 | */ |
mbed_official | 133:d4dda5c437f0 | 1239 | |
mbed_official | 133:d4dda5c437f0 | 1240 | /** |
mbed_official | 133:d4dda5c437f0 | 1241 | * @brief return the CAN state |
mbed_official | 133:d4dda5c437f0 | 1242 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1243 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1244 | * @retval HAL state |
mbed_official | 133:d4dda5c437f0 | 1245 | */ |
mbed_official | 133:d4dda5c437f0 | 1246 | HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1247 | { |
mbed_official | 133:d4dda5c437f0 | 1248 | /* Return CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1249 | return hcan->State; |
mbed_official | 133:d4dda5c437f0 | 1250 | } |
mbed_official | 133:d4dda5c437f0 | 1251 | |
mbed_official | 133:d4dda5c437f0 | 1252 | /** |
mbed_official | 133:d4dda5c437f0 | 1253 | * @brief Return the CAN error code |
mbed_official | 133:d4dda5c437f0 | 1254 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1255 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1256 | * @retval CAN Error Code |
mbed_official | 133:d4dda5c437f0 | 1257 | */ |
mbed_official | 133:d4dda5c437f0 | 1258 | uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan) |
mbed_official | 133:d4dda5c437f0 | 1259 | { |
mbed_official | 133:d4dda5c437f0 | 1260 | return hcan->ErrorCode; |
mbed_official | 133:d4dda5c437f0 | 1261 | } |
mbed_official | 133:d4dda5c437f0 | 1262 | |
mbed_official | 133:d4dda5c437f0 | 1263 | /** |
mbed_official | 133:d4dda5c437f0 | 1264 | * @} |
mbed_official | 133:d4dda5c437f0 | 1265 | */ |
mbed_official | 133:d4dda5c437f0 | 1266 | /** |
mbed_official | 133:d4dda5c437f0 | 1267 | * @brief Initiates and transmits a CAN frame message. |
mbed_official | 133:d4dda5c437f0 | 1268 | * @param hcan: pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1269 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1270 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 1271 | */ |
mbed_official | 133:d4dda5c437f0 | 1272 | static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan) |
mbed_official | 133:d4dda5c437f0 | 1273 | { |
mbed_official | 133:d4dda5c437f0 | 1274 | /* Disable Transmit mailbox empty Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1275 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_TME); |
mbed_official | 133:d4dda5c437f0 | 1276 | |
mbed_official | 133:d4dda5c437f0 | 1277 | if(hcan->State == HAL_CAN_STATE_BUSY_TX) |
mbed_official | 133:d4dda5c437f0 | 1278 | { |
mbed_official | 133:d4dda5c437f0 | 1279 | /* Disable Error warning Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1280 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG); |
mbed_official | 133:d4dda5c437f0 | 1281 | |
mbed_official | 133:d4dda5c437f0 | 1282 | /* Disable Error passive Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1283 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EPV); |
mbed_official | 133:d4dda5c437f0 | 1284 | |
mbed_official | 133:d4dda5c437f0 | 1285 | /* Disable Bus-off Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1286 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_BOF); |
mbed_official | 133:d4dda5c437f0 | 1287 | |
mbed_official | 133:d4dda5c437f0 | 1288 | /* Disable Last error code Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1289 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_LEC); |
mbed_official | 133:d4dda5c437f0 | 1290 | |
mbed_official | 133:d4dda5c437f0 | 1291 | /* Disable Error Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1292 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1293 | } |
mbed_official | 133:d4dda5c437f0 | 1294 | |
mbed_official | 133:d4dda5c437f0 | 1295 | if(hcan->State == HAL_CAN_STATE_BUSY_TX_RX) |
mbed_official | 133:d4dda5c437f0 | 1296 | { |
mbed_official | 133:d4dda5c437f0 | 1297 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1298 | hcan->State = HAL_CAN_STATE_BUSY_RX; |
mbed_official | 133:d4dda5c437f0 | 1299 | } |
mbed_official | 133:d4dda5c437f0 | 1300 | else |
mbed_official | 133:d4dda5c437f0 | 1301 | { |
mbed_official | 133:d4dda5c437f0 | 1302 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1303 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 1304 | } |
mbed_official | 133:d4dda5c437f0 | 1305 | |
mbed_official | 133:d4dda5c437f0 | 1306 | /* Transmission complete callback */ |
mbed_official | 133:d4dda5c437f0 | 1307 | HAL_CAN_TxCpltCallback(hcan); |
mbed_official | 133:d4dda5c437f0 | 1308 | |
mbed_official | 133:d4dda5c437f0 | 1309 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 1310 | } |
mbed_official | 133:d4dda5c437f0 | 1311 | |
mbed_official | 133:d4dda5c437f0 | 1312 | /** |
mbed_official | 133:d4dda5c437f0 | 1313 | * @brief Receives a correct CAN frame. |
mbed_official | 133:d4dda5c437f0 | 1314 | * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains |
mbed_official | 133:d4dda5c437f0 | 1315 | * the configuration information for the specified CAN. |
mbed_official | 133:d4dda5c437f0 | 1316 | * @param FIFONumber: Specify the FIFO number |
mbed_official | 133:d4dda5c437f0 | 1317 | * @retval HAL status |
mbed_official | 133:d4dda5c437f0 | 1318 | * @retval None |
mbed_official | 133:d4dda5c437f0 | 1319 | */ |
mbed_official | 133:d4dda5c437f0 | 1320 | static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber) |
mbed_official | 133:d4dda5c437f0 | 1321 | { |
mbed_official | 133:d4dda5c437f0 | 1322 | /* Get the Id */ |
mbed_official | 133:d4dda5c437f0 | 1323 | hcan->pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR; |
mbed_official | 133:d4dda5c437f0 | 1324 | if (hcan->pRxMsg->IDE == CAN_ID_STD) |
mbed_official | 133:d4dda5c437f0 | 1325 | { |
mbed_official | 133:d4dda5c437f0 | 1326 | hcan->pRxMsg->StdId = (uint32_t)0x000007FF & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21); |
mbed_official | 133:d4dda5c437f0 | 1327 | } |
mbed_official | 133:d4dda5c437f0 | 1328 | else |
mbed_official | 133:d4dda5c437f0 | 1329 | { |
mbed_official | 133:d4dda5c437f0 | 1330 | hcan->pRxMsg->ExtId = (uint32_t)0x1FFFFFFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3); |
mbed_official | 133:d4dda5c437f0 | 1331 | } |
mbed_official | 133:d4dda5c437f0 | 1332 | |
mbed_official | 133:d4dda5c437f0 | 1333 | hcan->pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR; |
mbed_official | 133:d4dda5c437f0 | 1334 | /* Get the DLC */ |
mbed_official | 133:d4dda5c437f0 | 1335 | hcan->pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR; |
mbed_official | 133:d4dda5c437f0 | 1336 | /* Get the FMI */ |
mbed_official | 133:d4dda5c437f0 | 1337 | hcan->pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8); |
mbed_official | 133:d4dda5c437f0 | 1338 | /* Get the data field */ |
mbed_official | 133:d4dda5c437f0 | 1339 | hcan->pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR; |
mbed_official | 133:d4dda5c437f0 | 1340 | hcan->pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8); |
mbed_official | 133:d4dda5c437f0 | 1341 | hcan->pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16); |
mbed_official | 133:d4dda5c437f0 | 1342 | hcan->pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24); |
mbed_official | 133:d4dda5c437f0 | 1343 | hcan->pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR; |
mbed_official | 133:d4dda5c437f0 | 1344 | hcan->pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8); |
mbed_official | 133:d4dda5c437f0 | 1345 | hcan->pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16); |
mbed_official | 133:d4dda5c437f0 | 1346 | hcan->pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24); |
mbed_official | 133:d4dda5c437f0 | 1347 | /* Release the FIFO */ |
mbed_official | 133:d4dda5c437f0 | 1348 | /* Release FIFO0 */ |
mbed_official | 133:d4dda5c437f0 | 1349 | if (FIFONumber == CAN_FIFO0) |
mbed_official | 133:d4dda5c437f0 | 1350 | { |
mbed_official | 133:d4dda5c437f0 | 1351 | __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0); |
mbed_official | 133:d4dda5c437f0 | 1352 | |
mbed_official | 133:d4dda5c437f0 | 1353 | /* Disable FIFO 0 message pending Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1354 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FMP0); |
mbed_official | 133:d4dda5c437f0 | 1355 | } |
mbed_official | 133:d4dda5c437f0 | 1356 | /* Release FIFO1 */ |
mbed_official | 133:d4dda5c437f0 | 1357 | else /* FIFONumber == CAN_FIFO1 */ |
mbed_official | 133:d4dda5c437f0 | 1358 | { |
mbed_official | 133:d4dda5c437f0 | 1359 | __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1); |
mbed_official | 133:d4dda5c437f0 | 1360 | |
mbed_official | 133:d4dda5c437f0 | 1361 | /* Disable FIFO 1 message pending Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1362 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FMP1); |
mbed_official | 133:d4dda5c437f0 | 1363 | } |
mbed_official | 133:d4dda5c437f0 | 1364 | |
mbed_official | 133:d4dda5c437f0 | 1365 | if(hcan->State == HAL_CAN_STATE_BUSY_RX) |
mbed_official | 133:d4dda5c437f0 | 1366 | { |
mbed_official | 133:d4dda5c437f0 | 1367 | /* Disable Error warning Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1368 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG); |
mbed_official | 133:d4dda5c437f0 | 1369 | |
mbed_official | 133:d4dda5c437f0 | 1370 | /* Disable Error passive Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1371 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EPV); |
mbed_official | 133:d4dda5c437f0 | 1372 | |
mbed_official | 133:d4dda5c437f0 | 1373 | /* Disable Bus-off Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1374 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_BOF); |
mbed_official | 133:d4dda5c437f0 | 1375 | |
mbed_official | 133:d4dda5c437f0 | 1376 | /* Disable Last error code Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1377 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_LEC); |
mbed_official | 133:d4dda5c437f0 | 1378 | |
mbed_official | 133:d4dda5c437f0 | 1379 | /* Disable Error Interrupt */ |
mbed_official | 133:d4dda5c437f0 | 1380 | __HAL_CAN_DISABLE_IT(hcan, CAN_IT_ERR); |
mbed_official | 133:d4dda5c437f0 | 1381 | } |
mbed_official | 133:d4dda5c437f0 | 1382 | |
mbed_official | 133:d4dda5c437f0 | 1383 | if(hcan->State == HAL_CAN_STATE_BUSY_TX_RX) |
mbed_official | 133:d4dda5c437f0 | 1384 | { |
mbed_official | 133:d4dda5c437f0 | 1385 | /* Disable CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1386 | hcan->State = HAL_CAN_STATE_BUSY_TX; |
mbed_official | 133:d4dda5c437f0 | 1387 | } |
mbed_official | 133:d4dda5c437f0 | 1388 | else |
mbed_official | 133:d4dda5c437f0 | 1389 | { |
mbed_official | 133:d4dda5c437f0 | 1390 | /* Change CAN state */ |
mbed_official | 133:d4dda5c437f0 | 1391 | hcan->State = HAL_CAN_STATE_READY; |
mbed_official | 133:d4dda5c437f0 | 1392 | } |
mbed_official | 133:d4dda5c437f0 | 1393 | |
mbed_official | 133:d4dda5c437f0 | 1394 | /* Receive complete callback */ |
mbed_official | 133:d4dda5c437f0 | 1395 | HAL_CAN_RxCpltCallback(hcan); |
mbed_official | 133:d4dda5c437f0 | 1396 | |
mbed_official | 133:d4dda5c437f0 | 1397 | /* Return function status */ |
mbed_official | 133:d4dda5c437f0 | 1398 | return HAL_OK; |
mbed_official | 133:d4dda5c437f0 | 1399 | } |
mbed_official | 133:d4dda5c437f0 | 1400 | |
mbed_official | 133:d4dda5c437f0 | 1401 | /** |
mbed_official | 133:d4dda5c437f0 | 1402 | * @} |
mbed_official | 133:d4dda5c437f0 | 1403 | */ |
mbed_official | 133:d4dda5c437f0 | 1404 | #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */ |
mbed_official | 133:d4dda5c437f0 | 1405 | |
mbed_official | 133:d4dda5c437f0 | 1406 | #endif /* HAL_CAN_MODULE_ENABLED */ |
mbed_official | 133:d4dda5c437f0 | 1407 | /** |
mbed_official | 133:d4dda5c437f0 | 1408 | * @} |
mbed_official | 133:d4dda5c437f0 | 1409 | */ |
mbed_official | 133:d4dda5c437f0 | 1410 | |
mbed_official | 133:d4dda5c437f0 | 1411 | /** |
mbed_official | 133:d4dda5c437f0 | 1412 | * @} |
mbed_official | 133:d4dda5c437f0 | 1413 | */ |
mbed_official | 133:d4dda5c437f0 | 1414 | |
mbed_official | 133:d4dda5c437f0 | 1415 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |