mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Sep 30 17:00:09 2015 +0100
Revision:
636:a11c0372f0ba
Parent:
631:825f75ca301e
Synchronized with git revision d29c98dae61be0946ddf3a3c641c7726056f9452

Full URL: https://github.com/mbedmicro/mbed/commit/d29c98dae61be0946ddf3a3c641c7726056f9452/

Added support for SAMW25

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