mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Jun 24 14:45:08 2014 +0100
Revision:
237:f3da66175598
Child:
375:3d36234a1087
Synchronized with git revision 8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7

Full URL: https://github.com/mbedmicro/mbed/commit/8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7/

[NUCLEO_F334R8] Add platform files

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