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