Watchdog Timer

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Sep 28 20:15:09 2015 +0100
Revision:
635:ac7d6880524d
Parent:
633:7687fb9c4f91
Synchronized with git revision 9b7d23d47153c298a6d24de9a415202705889d11

Full URL: https://github.com/mbedmicro/mbed/commit/9b7d23d47153c298a6d24de9a415202705889d11/

Revert "[NUCLEO_F303K8] add support of the STM32F303K8"

Who changed what in which revision?

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