mbed library sources

Fork of mbed-src by mbed official

Committer:
moirans2
Date:
Wed Jan 14 20:53:08 2015 +0000
Revision:
445:9a3ffe6cfa19
Parent:
441:d2c15dda23c1
internal clock stm32L051

Who changed what in which revision?

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