mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Wed Feb 26 09:45:12 2014 +0000
Revision:
106:ced8cbb51063
Parent:
76:aeb1df146756
Synchronized with git revision 4222735eff5868389433f0e9271976b39c8115cd

Full URL: https://github.com/mbedmicro/mbed/commit/4222735eff5868389433f0e9271976b39c8115cd/

[NUCLEO_xxx] Update STM32CubeF4 driver V1.0.0 + update license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /**
mbed_official 76:aeb1df146756 2 ******************************************************************************
mbed_official 76:aeb1df146756 3 * @file stm32f0xx_can.c
mbed_official 76:aeb1df146756 4 * @author MCD Application Team
mbed_official 76:aeb1df146756 5 * @version V1.3.0
mbed_official 76:aeb1df146756 6 * @date 16-January-2014
mbed_official 76:aeb1df146756 7 * @brief This file provides firmware functions to manage the following
mbed_official 76:aeb1df146756 8 * functionalities of the Controller area network (CAN) peripheral and
mbed_official 76:aeb1df146756 9 * applicable only for STM32F072 devices :
mbed_official 76:aeb1df146756 10 * + Initialization and Configuration
mbed_official 76:aeb1df146756 11 * + CAN Frames Transmission
mbed_official 76:aeb1df146756 12 * + CAN Frames Reception
mbed_official 76:aeb1df146756 13 * + Operation modes switch
mbed_official 76:aeb1df146756 14 * + Error management
mbed_official 76:aeb1df146756 15 * + Interrupts and flags
mbed_official 76:aeb1df146756 16 *
mbed_official 76:aeb1df146756 17 @verbatim
mbed_official 76:aeb1df146756 18
mbed_official 76:aeb1df146756 19 ===============================================================================
mbed_official 76:aeb1df146756 20 ##### How to use this driver #####
mbed_official 76:aeb1df146756 21 ===============================================================================
mbed_official 76:aeb1df146756 22 [..]
mbed_official 76:aeb1df146756 23 (#) Enable the CAN controller interface clock using
mbed_official 76:aeb1df146756 24 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
mbed_official 76:aeb1df146756 25 (#) CAN pins configuration:
mbed_official 76:aeb1df146756 26 (++) Enable the clock for the CAN GPIOs using the following function:
mbed_official 76:aeb1df146756 27 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);
mbed_official 76:aeb1df146756 28 (++) Connect the involved CAN pins to AF0 using the following function
mbed_official 76:aeb1df146756 29 GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_CANx);
mbed_official 76:aeb1df146756 30 (++) Configure these CAN pins in alternate function mode by calling
mbed_official 76:aeb1df146756 31 the function GPIO_Init();
mbed_official 76:aeb1df146756 32 (#) Initialise and configure the CAN using CAN_Init() and
mbed_official 76:aeb1df146756 33 CAN_FilterInit() functions.
mbed_official 76:aeb1df146756 34 (#) Transmit the desired CAN frame using CAN_Transmit() function.
mbed_official 76:aeb1df146756 35 (#) Check the transmission of a CAN frame using CAN_TransmitStatus() function.
mbed_official 76:aeb1df146756 36 (#) Cancel the transmission of a CAN frame using CAN_CancelTransmit() function.
mbed_official 76:aeb1df146756 37 (#) Receive a CAN frame using CAN_Recieve() function.
mbed_official 76:aeb1df146756 38 (#) Release the receive FIFOs using CAN_FIFORelease() function.
mbed_official 76:aeb1df146756 39 (#) Return the number of pending received frames using CAN_MessagePending() function.
mbed_official 76:aeb1df146756 40 (#) To control CAN events you can use one of the following two methods:
mbed_official 76:aeb1df146756 41 (++) Check on CAN flags using the CAN_GetFlagStatus() function.
mbed_official 76:aeb1df146756 42 (++) Use CAN interrupts through the function CAN_ITConfig() at initialization
mbed_official 76:aeb1df146756 43 phase and CAN_GetITStatus() function into interrupt routines to check
mbed_official 76:aeb1df146756 44 if the event has occurred or not.
mbed_official 76:aeb1df146756 45 After checking on a flag you should clear it using CAN_ClearFlag()
mbed_official 76:aeb1df146756 46 function. And after checking on an interrupt event you should clear it
mbed_official 76:aeb1df146756 47 using CAN_ClearITPendingBit() function.
mbed_official 76:aeb1df146756 48
mbed_official 76:aeb1df146756 49 @endverbatim
mbed_official 76:aeb1df146756 50 *
mbed_official 76:aeb1df146756 51 ******************************************************************************
mbed_official 76:aeb1df146756 52 * @attention
mbed_official 76:aeb1df146756 53 *
mbed_official 106:ced8cbb51063 54 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 76:aeb1df146756 55 *
mbed_official 106:ced8cbb51063 56 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 106:ced8cbb51063 57 * are permitted provided that the following conditions are met:
mbed_official 106:ced8cbb51063 58 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 106:ced8cbb51063 59 * this list of conditions and the following disclaimer.
mbed_official 106:ced8cbb51063 60 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 106:ced8cbb51063 61 * this list of conditions and the following disclaimer in the documentation
mbed_official 106:ced8cbb51063 62 * and/or other materials provided with the distribution.
mbed_official 106:ced8cbb51063 63 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 106:ced8cbb51063 64 * may be used to endorse or promote products derived from this software
mbed_official 106:ced8cbb51063 65 * without specific prior written permission.
mbed_official 76:aeb1df146756 66 *
mbed_official 106:ced8cbb51063 67 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 106:ced8cbb51063 68 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 106:ced8cbb51063 69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 106:ced8cbb51063 70 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 106:ced8cbb51063 71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 106:ced8cbb51063 72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 106:ced8cbb51063 73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 106:ced8cbb51063 74 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 106:ced8cbb51063 75 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 106:ced8cbb51063 76 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 76:aeb1df146756 77 *
mbed_official 76:aeb1df146756 78 ******************************************************************************
mbed_official 76:aeb1df146756 79 */
mbed_official 76:aeb1df146756 80
mbed_official 76:aeb1df146756 81 /* Includes ------------------------------------------------------------------*/
mbed_official 76:aeb1df146756 82 #include "stm32f0xx_can.h"
mbed_official 76:aeb1df146756 83 #include "stm32f0xx_rcc.h"
mbed_official 76:aeb1df146756 84
mbed_official 76:aeb1df146756 85 /** @addtogroup STM32F0xx_StdPeriph_Driver
mbed_official 76:aeb1df146756 86 * @{
mbed_official 76:aeb1df146756 87 */
mbed_official 76:aeb1df146756 88
mbed_official 76:aeb1df146756 89 /** @defgroup CAN
mbed_official 76:aeb1df146756 90 * @brief CAN driver modules
mbed_official 76:aeb1df146756 91 * @{
mbed_official 76:aeb1df146756 92 */
mbed_official 76:aeb1df146756 93 /* Private typedef -----------------------------------------------------------*/
mbed_official 76:aeb1df146756 94 /* Private define ------------------------------------------------------------*/
mbed_official 76:aeb1df146756 95
mbed_official 76:aeb1df146756 96 /* CAN Master Control Register bits */
mbed_official 76:aeb1df146756 97 #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
mbed_official 76:aeb1df146756 98
mbed_official 76:aeb1df146756 99 /* CAN Mailbox Transmit Request */
mbed_official 76:aeb1df146756 100 #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
mbed_official 76:aeb1df146756 101
mbed_official 76:aeb1df146756 102 /* CAN Filter Master Register bits */
mbed_official 76:aeb1df146756 103 #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
mbed_official 76:aeb1df146756 104
mbed_official 76:aeb1df146756 105 /* Time out for INAK bit */
mbed_official 76:aeb1df146756 106 #define INAK_TIMEOUT ((uint32_t)0x00FFFFFF)
mbed_official 76:aeb1df146756 107 /* Time out for SLAK bit */
mbed_official 76:aeb1df146756 108 #define SLAK_TIMEOUT ((uint32_t)0x00FFFFFF)
mbed_official 76:aeb1df146756 109
mbed_official 76:aeb1df146756 110 /* Flags in TSR register */
mbed_official 76:aeb1df146756 111 #define CAN_FLAGS_TSR ((uint32_t)0x08000000)
mbed_official 76:aeb1df146756 112 /* Flags in RF1R register */
mbed_official 76:aeb1df146756 113 #define CAN_FLAGS_RF1R ((uint32_t)0x04000000)
mbed_official 76:aeb1df146756 114 /* Flags in RF0R register */
mbed_official 76:aeb1df146756 115 #define CAN_FLAGS_RF0R ((uint32_t)0x02000000)
mbed_official 76:aeb1df146756 116 /* Flags in MSR register */
mbed_official 76:aeb1df146756 117 #define CAN_FLAGS_MSR ((uint32_t)0x01000000)
mbed_official 76:aeb1df146756 118 /* Flags in ESR register */
mbed_official 76:aeb1df146756 119 #define CAN_FLAGS_ESR ((uint32_t)0x00F00000)
mbed_official 76:aeb1df146756 120
mbed_official 76:aeb1df146756 121 /* Mailboxes definition */
mbed_official 76:aeb1df146756 122 #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
mbed_official 76:aeb1df146756 123 #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
mbed_official 76:aeb1df146756 124 #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
mbed_official 76:aeb1df146756 125
mbed_official 76:aeb1df146756 126 #define CAN_MODE_MASK ((uint32_t) 0x00000003)
mbed_official 76:aeb1df146756 127
mbed_official 76:aeb1df146756 128 /* Private macro -------------------------------------------------------------*/
mbed_official 76:aeb1df146756 129 /* Private variables ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 130 /* Private function prototypes -----------------------------------------------*/
mbed_official 76:aeb1df146756 131 /* Private functions ---------------------------------------------------------*/
mbed_official 76:aeb1df146756 132 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
mbed_official 76:aeb1df146756 133
mbed_official 76:aeb1df146756 134 /** @defgroup CAN_Private_Functions
mbed_official 76:aeb1df146756 135 * @{
mbed_official 76:aeb1df146756 136 */
mbed_official 76:aeb1df146756 137
mbed_official 76:aeb1df146756 138 /** @defgroup CAN_Group1 Initialization and Configuration functions
mbed_official 76:aeb1df146756 139 * @brief Initialization and Configuration functions
mbed_official 76:aeb1df146756 140 *
mbed_official 76:aeb1df146756 141 @verbatim
mbed_official 76:aeb1df146756 142 ===============================================================================
mbed_official 76:aeb1df146756 143 ##### Initialization and Configuration functions #####
mbed_official 76:aeb1df146756 144 ===============================================================================
mbed_official 76:aeb1df146756 145 [..] This section provides functions allowing to:
mbed_official 76:aeb1df146756 146 (+) Initialize the CAN peripherals : Prescaler, operating mode, the maximum
mbed_official 76:aeb1df146756 147 number of time quanta to perform resynchronization, the number of time
mbed_official 76:aeb1df146756 148 quanta in Bit Segment 1 and 2 and many other modes.
mbed_official 76:aeb1df146756 149 (+) Configure the CAN reception filter.
mbed_official 76:aeb1df146756 150 (+) Select the start bank filter for slave CAN.
mbed_official 76:aeb1df146756 151 (+) Enable or disable the Debug Freeze mode for CAN.
mbed_official 76:aeb1df146756 152 (+) Enable or disable the CAN Time Trigger Operation communication mode.
mbed_official 76:aeb1df146756 153
mbed_official 76:aeb1df146756 154 @endverbatim
mbed_official 76:aeb1df146756 155 * @{
mbed_official 76:aeb1df146756 156 */
mbed_official 76:aeb1df146756 157
mbed_official 76:aeb1df146756 158 /**
mbed_official 76:aeb1df146756 159 * @brief Deinitializes the CAN peripheral registers to their default reset values.
mbed_official 76:aeb1df146756 160 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 161 * @retval None.
mbed_official 76:aeb1df146756 162 */
mbed_official 76:aeb1df146756 163 void CAN_DeInit(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 164 {
mbed_official 76:aeb1df146756 165 /* Check the parameters */
mbed_official 76:aeb1df146756 166 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 167
mbed_official 76:aeb1df146756 168 /* Enable CAN reset state */
mbed_official 76:aeb1df146756 169 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, ENABLE);
mbed_official 76:aeb1df146756 170 /* Release CAN from reset state */
mbed_official 76:aeb1df146756 171 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, DISABLE);
mbed_official 76:aeb1df146756 172 }
mbed_official 76:aeb1df146756 173
mbed_official 76:aeb1df146756 174 /**
mbed_official 76:aeb1df146756 175 * @brief Initializes the CAN peripheral according to the specified
mbed_official 76:aeb1df146756 176 * parameters in the CAN_InitStruct.
mbed_official 76:aeb1df146756 177 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 178 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
mbed_official 76:aeb1df146756 179 * the configuration information for the CAN peripheral.
mbed_official 76:aeb1df146756 180 * @retval Constant indicates initialization succeed which will be
mbed_official 76:aeb1df146756 181 * CAN_InitStatus_Failed or CAN_InitStatus_Success.
mbed_official 76:aeb1df146756 182 */
mbed_official 76:aeb1df146756 183 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
mbed_official 76:aeb1df146756 184 {
mbed_official 76:aeb1df146756 185 uint8_t InitStatus = CAN_InitStatus_Failed;
mbed_official 76:aeb1df146756 186 uint32_t wait_ack = 0x00000000;
mbed_official 76:aeb1df146756 187
mbed_official 76:aeb1df146756 188 /* Check the parameters */
mbed_official 76:aeb1df146756 189 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 190 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
mbed_official 76:aeb1df146756 191 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
mbed_official 76:aeb1df146756 192 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
mbed_official 76:aeb1df146756 193 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
mbed_official 76:aeb1df146756 194 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
mbed_official 76:aeb1df146756 195 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
mbed_official 76:aeb1df146756 196 assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
mbed_official 76:aeb1df146756 197 assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
mbed_official 76:aeb1df146756 198 assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
mbed_official 76:aeb1df146756 199 assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
mbed_official 76:aeb1df146756 200 assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
mbed_official 76:aeb1df146756 201
mbed_official 76:aeb1df146756 202 /* Exit from sleep mode */
mbed_official 76:aeb1df146756 203 CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
mbed_official 76:aeb1df146756 204
mbed_official 76:aeb1df146756 205 /* Request initialisation */
mbed_official 76:aeb1df146756 206 CANx->MCR |= CAN_MCR_INRQ ;
mbed_official 76:aeb1df146756 207
mbed_official 76:aeb1df146756 208 /* Wait the acknowledge */
mbed_official 76:aeb1df146756 209 while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
mbed_official 76:aeb1df146756 210 {
mbed_official 76:aeb1df146756 211 wait_ack++;
mbed_official 76:aeb1df146756 212 }
mbed_official 76:aeb1df146756 213
mbed_official 76:aeb1df146756 214 /* Check acknowledge */
mbed_official 76:aeb1df146756 215 if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
mbed_official 76:aeb1df146756 216 {
mbed_official 76:aeb1df146756 217 InitStatus = CAN_InitStatus_Failed;
mbed_official 76:aeb1df146756 218 }
mbed_official 76:aeb1df146756 219 else
mbed_official 76:aeb1df146756 220 {
mbed_official 76:aeb1df146756 221 /* Set the time triggered communication mode */
mbed_official 76:aeb1df146756 222 if (CAN_InitStruct->CAN_TTCM == ENABLE)
mbed_official 76:aeb1df146756 223 {
mbed_official 76:aeb1df146756 224 CANx->MCR |= CAN_MCR_TTCM;
mbed_official 76:aeb1df146756 225 }
mbed_official 76:aeb1df146756 226 else
mbed_official 76:aeb1df146756 227 {
mbed_official 76:aeb1df146756 228 CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
mbed_official 76:aeb1df146756 229 }
mbed_official 76:aeb1df146756 230
mbed_official 76:aeb1df146756 231 /* Set the automatic bus-off management */
mbed_official 76:aeb1df146756 232 if (CAN_InitStruct->CAN_ABOM == ENABLE)
mbed_official 76:aeb1df146756 233 {
mbed_official 76:aeb1df146756 234 CANx->MCR |= CAN_MCR_ABOM;
mbed_official 76:aeb1df146756 235 }
mbed_official 76:aeb1df146756 236 else
mbed_official 76:aeb1df146756 237 {
mbed_official 76:aeb1df146756 238 CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
mbed_official 76:aeb1df146756 239 }
mbed_official 76:aeb1df146756 240
mbed_official 76:aeb1df146756 241 /* Set the automatic wake-up mode */
mbed_official 76:aeb1df146756 242 if (CAN_InitStruct->CAN_AWUM == ENABLE)
mbed_official 76:aeb1df146756 243 {
mbed_official 76:aeb1df146756 244 CANx->MCR |= CAN_MCR_AWUM;
mbed_official 76:aeb1df146756 245 }
mbed_official 76:aeb1df146756 246 else
mbed_official 76:aeb1df146756 247 {
mbed_official 76:aeb1df146756 248 CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
mbed_official 76:aeb1df146756 249 }
mbed_official 76:aeb1df146756 250
mbed_official 76:aeb1df146756 251 /* Set the no automatic retransmission */
mbed_official 76:aeb1df146756 252 if (CAN_InitStruct->CAN_NART == ENABLE)
mbed_official 76:aeb1df146756 253 {
mbed_official 76:aeb1df146756 254 CANx->MCR |= CAN_MCR_NART;
mbed_official 76:aeb1df146756 255 }
mbed_official 76:aeb1df146756 256 else
mbed_official 76:aeb1df146756 257 {
mbed_official 76:aeb1df146756 258 CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
mbed_official 76:aeb1df146756 259 }
mbed_official 76:aeb1df146756 260
mbed_official 76:aeb1df146756 261 /* Set the receive FIFO locked mode */
mbed_official 76:aeb1df146756 262 if (CAN_InitStruct->CAN_RFLM == ENABLE)
mbed_official 76:aeb1df146756 263 {
mbed_official 76:aeb1df146756 264 CANx->MCR |= CAN_MCR_RFLM;
mbed_official 76:aeb1df146756 265 }
mbed_official 76:aeb1df146756 266 else
mbed_official 76:aeb1df146756 267 {
mbed_official 76:aeb1df146756 268 CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
mbed_official 76:aeb1df146756 269 }
mbed_official 76:aeb1df146756 270
mbed_official 76:aeb1df146756 271 /* Set the transmit FIFO priority */
mbed_official 76:aeb1df146756 272 if (CAN_InitStruct->CAN_TXFP == ENABLE)
mbed_official 76:aeb1df146756 273 {
mbed_official 76:aeb1df146756 274 CANx->MCR |= CAN_MCR_TXFP;
mbed_official 76:aeb1df146756 275 }
mbed_official 76:aeb1df146756 276 else
mbed_official 76:aeb1df146756 277 {
mbed_official 76:aeb1df146756 278 CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
mbed_official 76:aeb1df146756 279 }
mbed_official 76:aeb1df146756 280
mbed_official 76:aeb1df146756 281 /* Set the bit timing register */
mbed_official 76:aeb1df146756 282 CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
mbed_official 76:aeb1df146756 283 ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
mbed_official 76:aeb1df146756 284 ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
mbed_official 76:aeb1df146756 285 ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
mbed_official 76:aeb1df146756 286 ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
mbed_official 76:aeb1df146756 287
mbed_official 76:aeb1df146756 288 /* Request leave initialisation */
mbed_official 76:aeb1df146756 289 CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
mbed_official 76:aeb1df146756 290
mbed_official 76:aeb1df146756 291 /* Wait the acknowledge */
mbed_official 76:aeb1df146756 292 wait_ack = 0;
mbed_official 76:aeb1df146756 293
mbed_official 76:aeb1df146756 294 while (((CANx->MSR & CAN_MSR_INAK) == (uint16_t)CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
mbed_official 76:aeb1df146756 295 {
mbed_official 76:aeb1df146756 296 wait_ack++;
mbed_official 76:aeb1df146756 297 }
mbed_official 76:aeb1df146756 298
mbed_official 76:aeb1df146756 299 /* ...and check acknowledged */
mbed_official 76:aeb1df146756 300 if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
mbed_official 76:aeb1df146756 301 {
mbed_official 76:aeb1df146756 302 InitStatus = CAN_InitStatus_Failed;
mbed_official 76:aeb1df146756 303 }
mbed_official 76:aeb1df146756 304 else
mbed_official 76:aeb1df146756 305 {
mbed_official 76:aeb1df146756 306 InitStatus = CAN_InitStatus_Success ;
mbed_official 76:aeb1df146756 307 }
mbed_official 76:aeb1df146756 308 }
mbed_official 76:aeb1df146756 309
mbed_official 76:aeb1df146756 310 /* At this step, return the status of initialization */
mbed_official 76:aeb1df146756 311 return InitStatus;
mbed_official 76:aeb1df146756 312 }
mbed_official 76:aeb1df146756 313
mbed_official 76:aeb1df146756 314 /**
mbed_official 76:aeb1df146756 315 * @brief Configures the CAN reception filter according to the specified
mbed_official 76:aeb1df146756 316 * parameters in the CAN_FilterInitStruct.
mbed_official 76:aeb1df146756 317 * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef structure that
mbed_official 76:aeb1df146756 318 * contains the configuration information.
mbed_official 76:aeb1df146756 319 * @retval None
mbed_official 76:aeb1df146756 320 */
mbed_official 76:aeb1df146756 321 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
mbed_official 76:aeb1df146756 322 {
mbed_official 76:aeb1df146756 323 uint32_t filter_number_bit_pos = 0;
mbed_official 76:aeb1df146756 324 /* Check the parameters */
mbed_official 76:aeb1df146756 325 assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
mbed_official 76:aeb1df146756 326 assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
mbed_official 76:aeb1df146756 327 assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
mbed_official 76:aeb1df146756 328 assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
mbed_official 76:aeb1df146756 329 assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
mbed_official 76:aeb1df146756 330
mbed_official 76:aeb1df146756 331 filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
mbed_official 76:aeb1df146756 332
mbed_official 76:aeb1df146756 333 /* Initialisation mode for the filter */
mbed_official 76:aeb1df146756 334 CAN->FMR |= FMR_FINIT;
mbed_official 76:aeb1df146756 335
mbed_official 76:aeb1df146756 336 /* Filter Deactivation */
mbed_official 76:aeb1df146756 337 CAN->FA1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 338
mbed_official 76:aeb1df146756 339 /* Filter Scale */
mbed_official 76:aeb1df146756 340 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
mbed_official 76:aeb1df146756 341 {
mbed_official 76:aeb1df146756 342 /* 16-bit scale for the filter */
mbed_official 76:aeb1df146756 343 CAN->FS1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 344
mbed_official 76:aeb1df146756 345 /* First 16-bit identifier and First 16-bit mask */
mbed_official 76:aeb1df146756 346 /* Or First 16-bit identifier and Second 16-bit identifier */
mbed_official 76:aeb1df146756 347 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
mbed_official 76:aeb1df146756 348 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
mbed_official 76:aeb1df146756 349 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
mbed_official 76:aeb1df146756 350
mbed_official 76:aeb1df146756 351 /* Second 16-bit identifier and Second 16-bit mask */
mbed_official 76:aeb1df146756 352 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
mbed_official 76:aeb1df146756 353 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
mbed_official 76:aeb1df146756 354 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
mbed_official 76:aeb1df146756 355 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
mbed_official 76:aeb1df146756 356 }
mbed_official 76:aeb1df146756 357
mbed_official 76:aeb1df146756 358 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
mbed_official 76:aeb1df146756 359 {
mbed_official 76:aeb1df146756 360 /* 32-bit scale for the filter */
mbed_official 76:aeb1df146756 361 CAN->FS1R |= filter_number_bit_pos;
mbed_official 76:aeb1df146756 362 /* 32-bit identifier or First 32-bit identifier */
mbed_official 76:aeb1df146756 363 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
mbed_official 76:aeb1df146756 364 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
mbed_official 76:aeb1df146756 365 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
mbed_official 76:aeb1df146756 366 /* 32-bit mask or Second 32-bit identifier */
mbed_official 76:aeb1df146756 367 CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
mbed_official 76:aeb1df146756 368 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
mbed_official 76:aeb1df146756 369 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
mbed_official 76:aeb1df146756 370 }
mbed_official 76:aeb1df146756 371
mbed_official 76:aeb1df146756 372 /* Filter Mode */
mbed_official 76:aeb1df146756 373 if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
mbed_official 76:aeb1df146756 374 {
mbed_official 76:aeb1df146756 375 /*Id/Mask mode for the filter*/
mbed_official 76:aeb1df146756 376 CAN->FM1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 377 }
mbed_official 76:aeb1df146756 378 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
mbed_official 76:aeb1df146756 379 {
mbed_official 76:aeb1df146756 380 /*Identifier list mode for the filter*/
mbed_official 76:aeb1df146756 381 CAN->FM1R |= (uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 382 }
mbed_official 76:aeb1df146756 383
mbed_official 76:aeb1df146756 384 /* Filter FIFO assignment */
mbed_official 76:aeb1df146756 385 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
mbed_official 76:aeb1df146756 386 {
mbed_official 76:aeb1df146756 387 /* FIFO 0 assignation for the filter */
mbed_official 76:aeb1df146756 388 CAN->FFA1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 389 }
mbed_official 76:aeb1df146756 390
mbed_official 76:aeb1df146756 391 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
mbed_official 76:aeb1df146756 392 {
mbed_official 76:aeb1df146756 393 /* FIFO 1 assignation for the filter */
mbed_official 76:aeb1df146756 394 CAN->FFA1R |= (uint32_t)filter_number_bit_pos;
mbed_official 76:aeb1df146756 395 }
mbed_official 76:aeb1df146756 396
mbed_official 76:aeb1df146756 397 /* Filter activation */
mbed_official 76:aeb1df146756 398 if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
mbed_official 76:aeb1df146756 399 {
mbed_official 76:aeb1df146756 400 CAN->FA1R |= filter_number_bit_pos;
mbed_official 76:aeb1df146756 401 }
mbed_official 76:aeb1df146756 402
mbed_official 76:aeb1df146756 403 /* Leave the initialisation mode for the filter */
mbed_official 76:aeb1df146756 404 CAN->FMR &= ~FMR_FINIT;
mbed_official 76:aeb1df146756 405 }
mbed_official 76:aeb1df146756 406
mbed_official 76:aeb1df146756 407 /**
mbed_official 76:aeb1df146756 408 * @brief Fills each CAN_InitStruct member with its default value.
mbed_official 76:aeb1df146756 409 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which ill be initialized.
mbed_official 76:aeb1df146756 410 * @retval None
mbed_official 76:aeb1df146756 411 */
mbed_official 76:aeb1df146756 412 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
mbed_official 76:aeb1df146756 413 {
mbed_official 76:aeb1df146756 414 /* Reset CAN init structure parameters values */
mbed_official 76:aeb1df146756 415
mbed_official 76:aeb1df146756 416 /* Initialize the time triggered communication mode */
mbed_official 76:aeb1df146756 417 CAN_InitStruct->CAN_TTCM = DISABLE;
mbed_official 76:aeb1df146756 418
mbed_official 76:aeb1df146756 419 /* Initialize the automatic bus-off management */
mbed_official 76:aeb1df146756 420 CAN_InitStruct->CAN_ABOM = DISABLE;
mbed_official 76:aeb1df146756 421
mbed_official 76:aeb1df146756 422 /* Initialize the automatic wake-up mode */
mbed_official 76:aeb1df146756 423 CAN_InitStruct->CAN_AWUM = DISABLE;
mbed_official 76:aeb1df146756 424
mbed_official 76:aeb1df146756 425 /* Initialize the no automatic retransmission */
mbed_official 76:aeb1df146756 426 CAN_InitStruct->CAN_NART = DISABLE;
mbed_official 76:aeb1df146756 427
mbed_official 76:aeb1df146756 428 /* Initialize the receive FIFO locked mode */
mbed_official 76:aeb1df146756 429 CAN_InitStruct->CAN_RFLM = DISABLE;
mbed_official 76:aeb1df146756 430
mbed_official 76:aeb1df146756 431 /* Initialize the transmit FIFO priority */
mbed_official 76:aeb1df146756 432 CAN_InitStruct->CAN_TXFP = DISABLE;
mbed_official 76:aeb1df146756 433
mbed_official 76:aeb1df146756 434 /* Initialize the CAN_Mode member */
mbed_official 76:aeb1df146756 435 CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
mbed_official 76:aeb1df146756 436
mbed_official 76:aeb1df146756 437 /* Initialize the CAN_SJW member */
mbed_official 76:aeb1df146756 438 CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
mbed_official 76:aeb1df146756 439
mbed_official 76:aeb1df146756 440 /* Initialize the CAN_BS1 member */
mbed_official 76:aeb1df146756 441 CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
mbed_official 76:aeb1df146756 442
mbed_official 76:aeb1df146756 443 /* Initialize the CAN_BS2 member */
mbed_official 76:aeb1df146756 444 CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
mbed_official 76:aeb1df146756 445
mbed_official 76:aeb1df146756 446 /* Initialize the CAN_Prescaler member */
mbed_official 76:aeb1df146756 447 CAN_InitStruct->CAN_Prescaler = 1;
mbed_official 76:aeb1df146756 448 }
mbed_official 76:aeb1df146756 449
mbed_official 76:aeb1df146756 450 /**
mbed_official 76:aeb1df146756 451 * @brief Select the start bank filter for slave CAN.
mbed_official 76:aeb1df146756 452 * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
mbed_official 76:aeb1df146756 453 * @retval None
mbed_official 76:aeb1df146756 454 */
mbed_official 76:aeb1df146756 455 void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
mbed_official 76:aeb1df146756 456 {
mbed_official 76:aeb1df146756 457 /* Check the parameters */
mbed_official 76:aeb1df146756 458 assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
mbed_official 76:aeb1df146756 459
mbed_official 76:aeb1df146756 460 /* Enter Initialisation mode for the filter */
mbed_official 76:aeb1df146756 461 CAN->FMR |= FMR_FINIT;
mbed_official 76:aeb1df146756 462
mbed_official 76:aeb1df146756 463 /* Select the start slave bank */
mbed_official 76:aeb1df146756 464 CAN->FMR &= (uint32_t)0xFFFFC0F1 ;
mbed_official 76:aeb1df146756 465 CAN->FMR |= (uint32_t)(CAN_BankNumber)<<8;
mbed_official 76:aeb1df146756 466
mbed_official 76:aeb1df146756 467 /* Leave Initialisation mode for the filter */
mbed_official 76:aeb1df146756 468 CAN->FMR &= ~FMR_FINIT;
mbed_official 76:aeb1df146756 469 }
mbed_official 76:aeb1df146756 470
mbed_official 76:aeb1df146756 471 /**
mbed_official 76:aeb1df146756 472 * @brief Enables or disables the DBG Freeze for CAN.
mbed_official 76:aeb1df146756 473 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 474 * @param NewState: new state of the CAN peripheral.
mbed_official 76:aeb1df146756 475 * This parameter can be: ENABLE (CAN reception/transmission is frozen
mbed_official 76:aeb1df146756 476 * during debug. Reception FIFOs can still be accessed/controlled normally)
mbed_official 76:aeb1df146756 477 * or DISABLE (CAN is working during debug).
mbed_official 76:aeb1df146756 478 * @retval None
mbed_official 76:aeb1df146756 479 */
mbed_official 76:aeb1df146756 480 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
mbed_official 76:aeb1df146756 481 {
mbed_official 76:aeb1df146756 482 /* Check the parameters */
mbed_official 76:aeb1df146756 483 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 484 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 485
mbed_official 76:aeb1df146756 486 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 487 {
mbed_official 76:aeb1df146756 488 /* Enable Debug Freeze */
mbed_official 76:aeb1df146756 489 CANx->MCR |= MCR_DBF;
mbed_official 76:aeb1df146756 490 }
mbed_official 76:aeb1df146756 491 else
mbed_official 76:aeb1df146756 492 {
mbed_official 76:aeb1df146756 493 /* Disable Debug Freeze */
mbed_official 76:aeb1df146756 494 CANx->MCR &= ~MCR_DBF;
mbed_official 76:aeb1df146756 495 }
mbed_official 76:aeb1df146756 496 }
mbed_official 76:aeb1df146756 497
mbed_official 76:aeb1df146756 498 /**
mbed_official 76:aeb1df146756 499 * @brief Enables or disables the CAN Time TriggerOperation communication mode.
mbed_official 76:aeb1df146756 500 * @note DLC must be programmed as 8 in order Time Stamp (2 bytes) to be
mbed_official 76:aeb1df146756 501 * sent over the CAN bus.
mbed_official 76:aeb1df146756 502 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 503 * @param NewState: Mode new state. This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 504 * When enabled, Time stamp (TIME[15:0]) value is sent in the last two
mbed_official 76:aeb1df146756 505 * data bytes of the 8-byte message: TIME[7:0] in data byte 6 and TIME[15:8]
mbed_official 76:aeb1df146756 506 * in data byte 7.
mbed_official 76:aeb1df146756 507 * @retval None
mbed_official 76:aeb1df146756 508 */
mbed_official 76:aeb1df146756 509 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
mbed_official 76:aeb1df146756 510 {
mbed_official 76:aeb1df146756 511 /* Check the parameters */
mbed_official 76:aeb1df146756 512 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 513 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 514 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 515 {
mbed_official 76:aeb1df146756 516 /* Enable the TTCM mode */
mbed_official 76:aeb1df146756 517 CANx->MCR |= CAN_MCR_TTCM;
mbed_official 76:aeb1df146756 518
mbed_official 76:aeb1df146756 519 /* Set TGT bits */
mbed_official 76:aeb1df146756 520 CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
mbed_official 76:aeb1df146756 521 CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
mbed_official 76:aeb1df146756 522 CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
mbed_official 76:aeb1df146756 523 }
mbed_official 76:aeb1df146756 524 else
mbed_official 76:aeb1df146756 525 {
mbed_official 76:aeb1df146756 526 /* Disable the TTCM mode */
mbed_official 76:aeb1df146756 527 CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
mbed_official 76:aeb1df146756 528
mbed_official 76:aeb1df146756 529 /* Reset TGT bits */
mbed_official 76:aeb1df146756 530 CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
mbed_official 76:aeb1df146756 531 CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
mbed_official 76:aeb1df146756 532 CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
mbed_official 76:aeb1df146756 533 }
mbed_official 76:aeb1df146756 534 }
mbed_official 76:aeb1df146756 535 /**
mbed_official 76:aeb1df146756 536 * @}
mbed_official 76:aeb1df146756 537 */
mbed_official 76:aeb1df146756 538
mbed_official 76:aeb1df146756 539
mbed_official 76:aeb1df146756 540 /** @defgroup CAN_Group2 CAN Frames Transmission functions
mbed_official 76:aeb1df146756 541 * @brief CAN Frames Transmission functions
mbed_official 76:aeb1df146756 542 *
mbed_official 76:aeb1df146756 543 @verbatim
mbed_official 76:aeb1df146756 544 ===============================================================================
mbed_official 76:aeb1df146756 545 ##### CAN Frames Transmission functions #####
mbed_official 76:aeb1df146756 546 ===============================================================================
mbed_official 76:aeb1df146756 547 [..] This section provides functions allowing to
mbed_official 76:aeb1df146756 548 (+) Initiate and transmit a CAN frame message (if there is an empty mailbox).
mbed_official 76:aeb1df146756 549 (+) Check the transmission status of a CAN Frame.
mbed_official 76:aeb1df146756 550 (+) Cancel a transmit request.
mbed_official 76:aeb1df146756 551
mbed_official 76:aeb1df146756 552 @endverbatim
mbed_official 76:aeb1df146756 553 * @{
mbed_official 76:aeb1df146756 554 */
mbed_official 76:aeb1df146756 555
mbed_official 76:aeb1df146756 556 /**
mbed_official 76:aeb1df146756 557 * @brief Initiates and transmits a CAN frame message.
mbed_official 76:aeb1df146756 558 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 559 * @param TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
mbed_official 76:aeb1df146756 560 * @retval The number of the mailbox that is used for transmission or
mbed_official 76:aeb1df146756 561 * CAN_TxStatus_NoMailBox if there is no empty mailbox.
mbed_official 76:aeb1df146756 562 */
mbed_official 76:aeb1df146756 563 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
mbed_official 76:aeb1df146756 564 {
mbed_official 76:aeb1df146756 565 uint8_t transmit_mailbox = 0;
mbed_official 76:aeb1df146756 566 /* Check the parameters */
mbed_official 76:aeb1df146756 567 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 568 assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
mbed_official 76:aeb1df146756 569 assert_param(IS_CAN_RTR(TxMessage->RTR));
mbed_official 76:aeb1df146756 570 assert_param(IS_CAN_DLC(TxMessage->DLC));
mbed_official 76:aeb1df146756 571
mbed_official 76:aeb1df146756 572 /* Select one empty transmit mailbox */
mbed_official 76:aeb1df146756 573 if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
mbed_official 76:aeb1df146756 574 {
mbed_official 76:aeb1df146756 575 transmit_mailbox = 0;
mbed_official 76:aeb1df146756 576 }
mbed_official 76:aeb1df146756 577 else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
mbed_official 76:aeb1df146756 578 {
mbed_official 76:aeb1df146756 579 transmit_mailbox = 1;
mbed_official 76:aeb1df146756 580 }
mbed_official 76:aeb1df146756 581 else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
mbed_official 76:aeb1df146756 582 {
mbed_official 76:aeb1df146756 583 transmit_mailbox = 2;
mbed_official 76:aeb1df146756 584 }
mbed_official 76:aeb1df146756 585 else
mbed_official 76:aeb1df146756 586 {
mbed_official 76:aeb1df146756 587 transmit_mailbox = CAN_TxStatus_NoMailBox;
mbed_official 76:aeb1df146756 588 }
mbed_official 76:aeb1df146756 589
mbed_official 76:aeb1df146756 590 if (transmit_mailbox != CAN_TxStatus_NoMailBox)
mbed_official 76:aeb1df146756 591 {
mbed_official 76:aeb1df146756 592 /* Set up the Id */
mbed_official 76:aeb1df146756 593 CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
mbed_official 76:aeb1df146756 594 if (TxMessage->IDE == CAN_Id_Standard)
mbed_official 76:aeb1df146756 595 {
mbed_official 76:aeb1df146756 596 assert_param(IS_CAN_STDID(TxMessage->StdId));
mbed_official 76:aeb1df146756 597 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
mbed_official 76:aeb1df146756 598 TxMessage->RTR);
mbed_official 76:aeb1df146756 599 }
mbed_official 76:aeb1df146756 600 else
mbed_official 76:aeb1df146756 601 {
mbed_official 76:aeb1df146756 602 assert_param(IS_CAN_EXTID(TxMessage->ExtId));
mbed_official 76:aeb1df146756 603 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
mbed_official 76:aeb1df146756 604 TxMessage->IDE | \
mbed_official 76:aeb1df146756 605 TxMessage->RTR);
mbed_official 76:aeb1df146756 606 }
mbed_official 76:aeb1df146756 607
mbed_official 76:aeb1df146756 608 /* Set up the DLC */
mbed_official 76:aeb1df146756 609 TxMessage->DLC &= (uint8_t)0x0000000F;
mbed_official 76:aeb1df146756 610 CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
mbed_official 76:aeb1df146756 611 CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
mbed_official 76:aeb1df146756 612
mbed_official 76:aeb1df146756 613 /* Set up the data field */
mbed_official 76:aeb1df146756 614 CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
mbed_official 76:aeb1df146756 615 ((uint32_t)TxMessage->Data[2] << 16) |
mbed_official 76:aeb1df146756 616 ((uint32_t)TxMessage->Data[1] << 8) |
mbed_official 76:aeb1df146756 617 ((uint32_t)TxMessage->Data[0]));
mbed_official 76:aeb1df146756 618 CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
mbed_official 76:aeb1df146756 619 ((uint32_t)TxMessage->Data[6] << 16) |
mbed_official 76:aeb1df146756 620 ((uint32_t)TxMessage->Data[5] << 8) |
mbed_official 76:aeb1df146756 621 ((uint32_t)TxMessage->Data[4]));
mbed_official 76:aeb1df146756 622 /* Request transmission */
mbed_official 76:aeb1df146756 623 CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
mbed_official 76:aeb1df146756 624 }
mbed_official 76:aeb1df146756 625 return transmit_mailbox;
mbed_official 76:aeb1df146756 626 }
mbed_official 76:aeb1df146756 627
mbed_official 76:aeb1df146756 628 /**
mbed_official 76:aeb1df146756 629 * @brief Checks the transmission status of a CAN Frame.
mbed_official 76:aeb1df146756 630 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 631 * @param TransmitMailbox: the number of the mailbox that is used for transmission.
mbed_official 76:aeb1df146756 632 * @retval CAN_TxStatus_Ok if the CAN driver transmits the message,
mbed_official 76:aeb1df146756 633 * CAN_TxStatus_Failed in an other case.
mbed_official 76:aeb1df146756 634 */
mbed_official 76:aeb1df146756 635 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
mbed_official 76:aeb1df146756 636 {
mbed_official 76:aeb1df146756 637 uint32_t state = 0;
mbed_official 76:aeb1df146756 638
mbed_official 76:aeb1df146756 639 /* Check the parameters */
mbed_official 76:aeb1df146756 640 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 641 assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
mbed_official 76:aeb1df146756 642
mbed_official 76:aeb1df146756 643 switch (TransmitMailbox)
mbed_official 76:aeb1df146756 644 {
mbed_official 76:aeb1df146756 645 case (CAN_TXMAILBOX_0):
mbed_official 76:aeb1df146756 646 state = CANx->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
mbed_official 76:aeb1df146756 647 break;
mbed_official 76:aeb1df146756 648 case (CAN_TXMAILBOX_1):
mbed_official 76:aeb1df146756 649 state = CANx->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
mbed_official 76:aeb1df146756 650 break;
mbed_official 76:aeb1df146756 651 case (CAN_TXMAILBOX_2):
mbed_official 76:aeb1df146756 652 state = CANx->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
mbed_official 76:aeb1df146756 653 break;
mbed_official 76:aeb1df146756 654 default:
mbed_official 76:aeb1df146756 655 state = CAN_TxStatus_Failed;
mbed_official 76:aeb1df146756 656 break;
mbed_official 76:aeb1df146756 657 }
mbed_official 76:aeb1df146756 658 switch (state)
mbed_official 76:aeb1df146756 659 {
mbed_official 76:aeb1df146756 660 /* transmit pending */
mbed_official 76:aeb1df146756 661 case (0x0): state = CAN_TxStatus_Pending;
mbed_official 76:aeb1df146756 662 break;
mbed_official 76:aeb1df146756 663 /* transmit failed */
mbed_official 76:aeb1df146756 664 case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
mbed_official 76:aeb1df146756 665 break;
mbed_official 76:aeb1df146756 666 case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
mbed_official 76:aeb1df146756 667 break;
mbed_official 76:aeb1df146756 668 case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
mbed_official 76:aeb1df146756 669 break;
mbed_official 76:aeb1df146756 670 /* transmit succeeded */
mbed_official 76:aeb1df146756 671 case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
mbed_official 76:aeb1df146756 672 break;
mbed_official 76:aeb1df146756 673 case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
mbed_official 76:aeb1df146756 674 break;
mbed_official 76:aeb1df146756 675 case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
mbed_official 76:aeb1df146756 676 break;
mbed_official 76:aeb1df146756 677 default: state = CAN_TxStatus_Failed;
mbed_official 76:aeb1df146756 678 break;
mbed_official 76:aeb1df146756 679 }
mbed_official 76:aeb1df146756 680 return (uint8_t) state;
mbed_official 76:aeb1df146756 681 }
mbed_official 76:aeb1df146756 682
mbed_official 76:aeb1df146756 683 /**
mbed_official 76:aeb1df146756 684 * @brief Cancels a transmit request.
mbed_official 76:aeb1df146756 685 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 686 * @param Mailbox: Mailbox number.
mbed_official 76:aeb1df146756 687 * @retval None
mbed_official 76:aeb1df146756 688 */
mbed_official 76:aeb1df146756 689 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
mbed_official 76:aeb1df146756 690 {
mbed_official 76:aeb1df146756 691 /* Check the parameters */
mbed_official 76:aeb1df146756 692 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 693 assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
mbed_official 76:aeb1df146756 694 /* abort transmission */
mbed_official 76:aeb1df146756 695 switch (Mailbox)
mbed_official 76:aeb1df146756 696 {
mbed_official 76:aeb1df146756 697 case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
mbed_official 76:aeb1df146756 698 break;
mbed_official 76:aeb1df146756 699 case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
mbed_official 76:aeb1df146756 700 break;
mbed_official 76:aeb1df146756 701 case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
mbed_official 76:aeb1df146756 702 break;
mbed_official 76:aeb1df146756 703 default:
mbed_official 76:aeb1df146756 704 break;
mbed_official 76:aeb1df146756 705 }
mbed_official 76:aeb1df146756 706 }
mbed_official 76:aeb1df146756 707 /**
mbed_official 76:aeb1df146756 708 * @}
mbed_official 76:aeb1df146756 709 */
mbed_official 76:aeb1df146756 710
mbed_official 76:aeb1df146756 711
mbed_official 76:aeb1df146756 712 /** @defgroup CAN_Group3 CAN Frames Reception functions
mbed_official 76:aeb1df146756 713 * @brief CAN Frames Reception functions
mbed_official 76:aeb1df146756 714 *
mbed_official 76:aeb1df146756 715 @verbatim
mbed_official 76:aeb1df146756 716 ===============================================================================
mbed_official 76:aeb1df146756 717 ##### CAN Frames Reception functions #####
mbed_official 76:aeb1df146756 718 ===============================================================================
mbed_official 76:aeb1df146756 719 [..] This section provides functions allowing to
mbed_official 76:aeb1df146756 720 (+) Receive a correct CAN frame.
mbed_official 76:aeb1df146756 721 (+) Release a specified receive FIFO (2 FIFOs are available).
mbed_official 76:aeb1df146756 722 (+) Return the number of the pending received CAN frames.
mbed_official 76:aeb1df146756 723
mbed_official 76:aeb1df146756 724 @endverbatim
mbed_official 76:aeb1df146756 725 * @{
mbed_official 76:aeb1df146756 726 */
mbed_official 76:aeb1df146756 727
mbed_official 76:aeb1df146756 728 /**
mbed_official 76:aeb1df146756 729 * @brief Receives a correct CAN frame.
mbed_official 76:aeb1df146756 730 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 731 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
mbed_official 76:aeb1df146756 732 * @param RxMessage: pointer to a structure receive frame which contains CAN Id,
mbed_official 76:aeb1df146756 733 * CAN DLC, CAN data and FMI number.
mbed_official 76:aeb1df146756 734 * @retval None
mbed_official 76:aeb1df146756 735 */
mbed_official 76:aeb1df146756 736 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
mbed_official 76:aeb1df146756 737 {
mbed_official 76:aeb1df146756 738 /* Check the parameters */
mbed_official 76:aeb1df146756 739 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 740 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 76:aeb1df146756 741 /* Get the Id */
mbed_official 76:aeb1df146756 742 RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
mbed_official 76:aeb1df146756 743 if (RxMessage->IDE == CAN_Id_Standard)
mbed_official 76:aeb1df146756 744 {
mbed_official 76:aeb1df146756 745 RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
mbed_official 76:aeb1df146756 746 }
mbed_official 76:aeb1df146756 747 else
mbed_official 76:aeb1df146756 748 {
mbed_official 76:aeb1df146756 749 RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
mbed_official 76:aeb1df146756 750 }
mbed_official 76:aeb1df146756 751
mbed_official 76:aeb1df146756 752 RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
mbed_official 76:aeb1df146756 753 /* Get the DLC */
mbed_official 76:aeb1df146756 754 RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
mbed_official 76:aeb1df146756 755 /* Get the FMI */
mbed_official 76:aeb1df146756 756 RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
mbed_official 76:aeb1df146756 757 /* Get the data field */
mbed_official 76:aeb1df146756 758 RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
mbed_official 76:aeb1df146756 759 RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
mbed_official 76:aeb1df146756 760 RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
mbed_official 76:aeb1df146756 761 RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
mbed_official 76:aeb1df146756 762 RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
mbed_official 76:aeb1df146756 763 RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
mbed_official 76:aeb1df146756 764 RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
mbed_official 76:aeb1df146756 765 RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
mbed_official 76:aeb1df146756 766 /* Release the FIFO */
mbed_official 76:aeb1df146756 767 /* Release FIFO0 */
mbed_official 76:aeb1df146756 768 if (FIFONumber == CAN_FIFO0)
mbed_official 76:aeb1df146756 769 {
mbed_official 76:aeb1df146756 770 CANx->RF0R |= CAN_RF0R_RFOM0;
mbed_official 76:aeb1df146756 771 }
mbed_official 76:aeb1df146756 772 /* Release FIFO1 */
mbed_official 76:aeb1df146756 773 else /* FIFONumber == CAN_FIFO1 */
mbed_official 76:aeb1df146756 774 {
mbed_official 76:aeb1df146756 775 CANx->RF1R |= CAN_RF1R_RFOM1;
mbed_official 76:aeb1df146756 776 }
mbed_official 76:aeb1df146756 777 }
mbed_official 76:aeb1df146756 778
mbed_official 76:aeb1df146756 779 /**
mbed_official 76:aeb1df146756 780 * @brief Releases the specified receive FIFO.
mbed_official 76:aeb1df146756 781 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 782 * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
mbed_official 76:aeb1df146756 783 * @retval None
mbed_official 76:aeb1df146756 784 */
mbed_official 76:aeb1df146756 785 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
mbed_official 76:aeb1df146756 786 {
mbed_official 76:aeb1df146756 787 /* Check the parameters */
mbed_official 76:aeb1df146756 788 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 789 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 76:aeb1df146756 790 /* Release FIFO0 */
mbed_official 76:aeb1df146756 791 if (FIFONumber == CAN_FIFO0)
mbed_official 76:aeb1df146756 792 {
mbed_official 76:aeb1df146756 793 CANx->RF0R |= CAN_RF0R_RFOM0;
mbed_official 76:aeb1df146756 794 }
mbed_official 76:aeb1df146756 795 /* Release FIFO1 */
mbed_official 76:aeb1df146756 796 else /* FIFONumber == CAN_FIFO1 */
mbed_official 76:aeb1df146756 797 {
mbed_official 76:aeb1df146756 798 CANx->RF1R |= CAN_RF1R_RFOM1;
mbed_official 76:aeb1df146756 799 }
mbed_official 76:aeb1df146756 800 }
mbed_official 76:aeb1df146756 801
mbed_official 76:aeb1df146756 802 /**
mbed_official 76:aeb1df146756 803 * @brief Returns the number of pending received messages.
mbed_official 76:aeb1df146756 804 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 805 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
mbed_official 76:aeb1df146756 806 * @retval NbMessage : which is the number of pending message.
mbed_official 76:aeb1df146756 807 */
mbed_official 76:aeb1df146756 808 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
mbed_official 76:aeb1df146756 809 {
mbed_official 76:aeb1df146756 810 uint8_t message_pending=0;
mbed_official 76:aeb1df146756 811 /* Check the parameters */
mbed_official 76:aeb1df146756 812 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 813 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 76:aeb1df146756 814 if (FIFONumber == CAN_FIFO0)
mbed_official 76:aeb1df146756 815 {
mbed_official 76:aeb1df146756 816 message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
mbed_official 76:aeb1df146756 817 }
mbed_official 76:aeb1df146756 818 else if (FIFONumber == CAN_FIFO1)
mbed_official 76:aeb1df146756 819 {
mbed_official 76:aeb1df146756 820 message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
mbed_official 76:aeb1df146756 821 }
mbed_official 76:aeb1df146756 822 else
mbed_official 76:aeb1df146756 823 {
mbed_official 76:aeb1df146756 824 message_pending = 0;
mbed_official 76:aeb1df146756 825 }
mbed_official 76:aeb1df146756 826 return message_pending;
mbed_official 76:aeb1df146756 827 }
mbed_official 76:aeb1df146756 828 /**
mbed_official 76:aeb1df146756 829 * @}
mbed_official 76:aeb1df146756 830 */
mbed_official 76:aeb1df146756 831
mbed_official 76:aeb1df146756 832
mbed_official 76:aeb1df146756 833 /** @defgroup CAN_Group4 CAN Operation modes functions
mbed_official 76:aeb1df146756 834 * @brief CAN Operation modes functions
mbed_official 76:aeb1df146756 835 *
mbed_official 76:aeb1df146756 836 @verbatim
mbed_official 76:aeb1df146756 837 ===============================================================================
mbed_official 76:aeb1df146756 838 ##### CAN Operation modes functions #####
mbed_official 76:aeb1df146756 839 ===============================================================================
mbed_official 76:aeb1df146756 840 [..] This section provides functions allowing to select the CAN Operation modes:
mbed_official 76:aeb1df146756 841 (+) sleep mode.
mbed_official 76:aeb1df146756 842 (+) normal mode.
mbed_official 76:aeb1df146756 843 (+) initialization mode.
mbed_official 76:aeb1df146756 844
mbed_official 76:aeb1df146756 845 @endverbatim
mbed_official 76:aeb1df146756 846 * @{
mbed_official 76:aeb1df146756 847 */
mbed_official 76:aeb1df146756 848
mbed_official 76:aeb1df146756 849
mbed_official 76:aeb1df146756 850 /**
mbed_official 76:aeb1df146756 851 * @brief Selects the CAN Operation mode.
mbed_official 76:aeb1df146756 852 * @param CAN_OperatingMode: CAN Operating Mode.
mbed_official 76:aeb1df146756 853 * This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
mbed_official 76:aeb1df146756 854 * @retval status of the requested mode which can be:
mbed_official 76:aeb1df146756 855 * - CAN_ModeStatus_Failed: CAN failed entering the specific mode
mbed_official 76:aeb1df146756 856 * - CAN_ModeStatus_Success: CAN Succeed entering the specific mode
mbed_official 76:aeb1df146756 857 */
mbed_official 76:aeb1df146756 858 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
mbed_official 76:aeb1df146756 859 {
mbed_official 76:aeb1df146756 860 uint8_t status = CAN_ModeStatus_Failed;
mbed_official 76:aeb1df146756 861
mbed_official 76:aeb1df146756 862 /* Timeout for INAK or also for SLAK bits*/
mbed_official 76:aeb1df146756 863 uint32_t timeout = INAK_TIMEOUT;
mbed_official 76:aeb1df146756 864
mbed_official 76:aeb1df146756 865 /* Check the parameters */
mbed_official 76:aeb1df146756 866 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 867 assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
mbed_official 76:aeb1df146756 868
mbed_official 76:aeb1df146756 869 if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
mbed_official 76:aeb1df146756 870 {
mbed_official 76:aeb1df146756 871 /* Request initialisation */
mbed_official 76:aeb1df146756 872 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
mbed_official 76:aeb1df146756 873
mbed_official 76:aeb1df146756 874 /* Wait the acknowledge */
mbed_official 76:aeb1df146756 875 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
mbed_official 76:aeb1df146756 876 {
mbed_official 76:aeb1df146756 877 timeout--;
mbed_official 76:aeb1df146756 878 }
mbed_official 76:aeb1df146756 879 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
mbed_official 76:aeb1df146756 880 {
mbed_official 76:aeb1df146756 881 status = CAN_ModeStatus_Failed;
mbed_official 76:aeb1df146756 882 }
mbed_official 76:aeb1df146756 883 else
mbed_official 76:aeb1df146756 884 {
mbed_official 76:aeb1df146756 885 status = CAN_ModeStatus_Success;
mbed_official 76:aeb1df146756 886 }
mbed_official 76:aeb1df146756 887 }
mbed_official 76:aeb1df146756 888 else if (CAN_OperatingMode == CAN_OperatingMode_Normal)
mbed_official 76:aeb1df146756 889 {
mbed_official 76:aeb1df146756 890 /* Request leave initialisation and sleep mode and enter Normal mode */
mbed_official 76:aeb1df146756 891 CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
mbed_official 76:aeb1df146756 892
mbed_official 76:aeb1df146756 893 /* Wait the acknowledge */
mbed_official 76:aeb1df146756 894 while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
mbed_official 76:aeb1df146756 895 {
mbed_official 76:aeb1df146756 896 timeout--;
mbed_official 76:aeb1df146756 897 }
mbed_official 76:aeb1df146756 898 if ((CANx->MSR & CAN_MODE_MASK) != 0)
mbed_official 76:aeb1df146756 899 {
mbed_official 76:aeb1df146756 900 status = CAN_ModeStatus_Failed;
mbed_official 76:aeb1df146756 901 }
mbed_official 76:aeb1df146756 902 else
mbed_official 76:aeb1df146756 903 {
mbed_official 76:aeb1df146756 904 status = CAN_ModeStatus_Success;
mbed_official 76:aeb1df146756 905 }
mbed_official 76:aeb1df146756 906 }
mbed_official 76:aeb1df146756 907 else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
mbed_official 76:aeb1df146756 908 {
mbed_official 76:aeb1df146756 909 /* Request Sleep mode */
mbed_official 76:aeb1df146756 910 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
mbed_official 76:aeb1df146756 911
mbed_official 76:aeb1df146756 912 /* Wait the acknowledge */
mbed_official 76:aeb1df146756 913 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
mbed_official 76:aeb1df146756 914 {
mbed_official 76:aeb1df146756 915 timeout--;
mbed_official 76:aeb1df146756 916 }
mbed_official 76:aeb1df146756 917 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
mbed_official 76:aeb1df146756 918 {
mbed_official 76:aeb1df146756 919 status = CAN_ModeStatus_Failed;
mbed_official 76:aeb1df146756 920 }
mbed_official 76:aeb1df146756 921 else
mbed_official 76:aeb1df146756 922 {
mbed_official 76:aeb1df146756 923 status = CAN_ModeStatus_Success;
mbed_official 76:aeb1df146756 924 }
mbed_official 76:aeb1df146756 925 }
mbed_official 76:aeb1df146756 926 else
mbed_official 76:aeb1df146756 927 {
mbed_official 76:aeb1df146756 928 status = CAN_ModeStatus_Failed;
mbed_official 76:aeb1df146756 929 }
mbed_official 76:aeb1df146756 930
mbed_official 76:aeb1df146756 931 return (uint8_t) status;
mbed_official 76:aeb1df146756 932 }
mbed_official 76:aeb1df146756 933
mbed_official 76:aeb1df146756 934 /**
mbed_official 76:aeb1df146756 935 * @brief Enters the Sleep (low power) mode.
mbed_official 76:aeb1df146756 936 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 937 * @retval CAN_Sleep_Ok if sleep entered, CAN_Sleep_Failed otherwise.
mbed_official 76:aeb1df146756 938 */
mbed_official 76:aeb1df146756 939 uint8_t CAN_Sleep(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 940 {
mbed_official 76:aeb1df146756 941 uint8_t sleepstatus = CAN_Sleep_Failed;
mbed_official 76:aeb1df146756 942
mbed_official 76:aeb1df146756 943 /* Check the parameters */
mbed_official 76:aeb1df146756 944 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 945
mbed_official 76:aeb1df146756 946 /* Request Sleep mode */
mbed_official 76:aeb1df146756 947 CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
mbed_official 76:aeb1df146756 948
mbed_official 76:aeb1df146756 949 /* Sleep mode status */
mbed_official 76:aeb1df146756 950 if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
mbed_official 76:aeb1df146756 951 {
mbed_official 76:aeb1df146756 952 /* Sleep mode not entered */
mbed_official 76:aeb1df146756 953 sleepstatus = CAN_Sleep_Ok;
mbed_official 76:aeb1df146756 954 }
mbed_official 76:aeb1df146756 955 /* return sleep mode status */
mbed_official 76:aeb1df146756 956 return (uint8_t)sleepstatus;
mbed_official 76:aeb1df146756 957 }
mbed_official 76:aeb1df146756 958
mbed_official 76:aeb1df146756 959 /**
mbed_official 76:aeb1df146756 960 * @brief Wakes up the CAN peripheral from sleep mode .
mbed_official 76:aeb1df146756 961 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 962 * @retval CAN_WakeUp_Ok if sleep mode left, CAN_WakeUp_Failed otherwise.
mbed_official 76:aeb1df146756 963 */
mbed_official 76:aeb1df146756 964 uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 965 {
mbed_official 76:aeb1df146756 966 uint32_t wait_slak = SLAK_TIMEOUT;
mbed_official 76:aeb1df146756 967 uint8_t wakeupstatus = CAN_WakeUp_Failed;
mbed_official 76:aeb1df146756 968
mbed_official 76:aeb1df146756 969 /* Check the parameters */
mbed_official 76:aeb1df146756 970 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 971
mbed_official 76:aeb1df146756 972 /* Wake up request */
mbed_official 76:aeb1df146756 973 CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
mbed_official 76:aeb1df146756 974
mbed_official 76:aeb1df146756 975 /* Sleep mode status */
mbed_official 76:aeb1df146756 976 while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
mbed_official 76:aeb1df146756 977 {
mbed_official 76:aeb1df146756 978 wait_slak--;
mbed_official 76:aeb1df146756 979 }
mbed_official 76:aeb1df146756 980 if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
mbed_official 76:aeb1df146756 981 {
mbed_official 76:aeb1df146756 982 /* wake up done : Sleep mode exited */
mbed_official 76:aeb1df146756 983 wakeupstatus = CAN_WakeUp_Ok;
mbed_official 76:aeb1df146756 984 }
mbed_official 76:aeb1df146756 985 /* return wakeup status */
mbed_official 76:aeb1df146756 986 return (uint8_t)wakeupstatus;
mbed_official 76:aeb1df146756 987 }
mbed_official 76:aeb1df146756 988 /**
mbed_official 76:aeb1df146756 989 * @}
mbed_official 76:aeb1df146756 990 */
mbed_official 76:aeb1df146756 991
mbed_official 76:aeb1df146756 992
mbed_official 76:aeb1df146756 993 /** @defgroup CAN_Group5 CAN Bus Error management functions
mbed_official 76:aeb1df146756 994 * @brief CAN Bus Error management functions
mbed_official 76:aeb1df146756 995 *
mbed_official 76:aeb1df146756 996 @verbatim
mbed_official 76:aeb1df146756 997 ===============================================================================
mbed_official 76:aeb1df146756 998 ##### CAN Bus Error management functions #####
mbed_official 76:aeb1df146756 999 ===============================================================================
mbed_official 76:aeb1df146756 1000 [..] This section provides functions allowing to
mbed_official 76:aeb1df146756 1001 (+) Return the CANx's last error code (LEC).
mbed_official 76:aeb1df146756 1002 (+) Return the CANx Receive Error Counter (REC).
mbed_official 76:aeb1df146756 1003 (+) Return the LSB of the 9-bit CANx Transmit Error Counter(TEC).
mbed_official 76:aeb1df146756 1004 [..]
mbed_official 76:aeb1df146756 1005 (@) If TEC is greater than 255, The CAN is in bus-off state.
mbed_official 76:aeb1df146756 1006 (@) If REC or TEC are greater than 96, an Error warning flag occurs.
mbed_official 76:aeb1df146756 1007 (@) If REC or TEC are greater than 127, an Error Passive Flag occurs.
mbed_official 76:aeb1df146756 1008
mbed_official 76:aeb1df146756 1009 @endverbatim
mbed_official 76:aeb1df146756 1010 * @{
mbed_official 76:aeb1df146756 1011 */
mbed_official 76:aeb1df146756 1012
mbed_official 76:aeb1df146756 1013 /**
mbed_official 76:aeb1df146756 1014 * @brief Returns the CANx's last error code (LEC).
mbed_official 76:aeb1df146756 1015 * @param CANx: where x can be 1 to select the CAN peripheral.
mbed_official 76:aeb1df146756 1016 * @retval Error code:
mbed_official 76:aeb1df146756 1017 * - CAN_ERRORCODE_NoErr: No Error
mbed_official 76:aeb1df146756 1018 * - CAN_ERRORCODE_StuffErr: Stuff Error
mbed_official 76:aeb1df146756 1019 * - CAN_ERRORCODE_FormErr: Form Error
mbed_official 76:aeb1df146756 1020 * - CAN_ERRORCODE_ACKErr : Acknowledgment Error
mbed_official 76:aeb1df146756 1021 * - CAN_ERRORCODE_BitRecessiveErr: Bit Recessive Error
mbed_official 76:aeb1df146756 1022 * - CAN_ERRORCODE_BitDominantErr: Bit Dominant Error
mbed_official 76:aeb1df146756 1023 * - CAN_ERRORCODE_CRCErr: CRC Error
mbed_official 76:aeb1df146756 1024 * - CAN_ERRORCODE_SoftwareSetErr: Software Set Error
mbed_official 76:aeb1df146756 1025 */
mbed_official 76:aeb1df146756 1026 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 1027 {
mbed_official 76:aeb1df146756 1028 uint8_t errorcode=0;
mbed_official 76:aeb1df146756 1029
mbed_official 76:aeb1df146756 1030 /* Check the parameters */
mbed_official 76:aeb1df146756 1031 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1032
mbed_official 76:aeb1df146756 1033 /* Get the error code*/
mbed_official 76:aeb1df146756 1034 errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
mbed_official 76:aeb1df146756 1035
mbed_official 76:aeb1df146756 1036 /* Return the error code*/
mbed_official 76:aeb1df146756 1037 return errorcode;
mbed_official 76:aeb1df146756 1038 }
mbed_official 76:aeb1df146756 1039
mbed_official 76:aeb1df146756 1040 /**
mbed_official 76:aeb1df146756 1041 * @brief Returns the CANx Receive Error Counter (REC).
mbed_official 76:aeb1df146756 1042 * @note In case of an error during reception, this counter is incremented
mbed_official 76:aeb1df146756 1043 * by 1 or by 8 depending on the error condition as defined by the CAN
mbed_official 76:aeb1df146756 1044 * standard. After every successful reception, the counter is
mbed_official 76:aeb1df146756 1045 * decremented by 1 or reset to 120 if its value was higher than 128.
mbed_official 76:aeb1df146756 1046 * When the counter value exceeds 127, the CAN controller enters the
mbed_official 76:aeb1df146756 1047 * error passive state.
mbed_official 76:aeb1df146756 1048 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1049 * @retval CAN Receive Error Counter.
mbed_official 76:aeb1df146756 1050 */
mbed_official 76:aeb1df146756 1051 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 1052 {
mbed_official 76:aeb1df146756 1053 uint8_t counter=0;
mbed_official 76:aeb1df146756 1054
mbed_official 76:aeb1df146756 1055 /* Check the parameters */
mbed_official 76:aeb1df146756 1056 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1057
mbed_official 76:aeb1df146756 1058 /* Get the Receive Error Counter*/
mbed_official 76:aeb1df146756 1059 counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
mbed_official 76:aeb1df146756 1060
mbed_official 76:aeb1df146756 1061 /* Return the Receive Error Counter*/
mbed_official 76:aeb1df146756 1062 return counter;
mbed_official 76:aeb1df146756 1063 }
mbed_official 76:aeb1df146756 1064
mbed_official 76:aeb1df146756 1065
mbed_official 76:aeb1df146756 1066 /**
mbed_official 76:aeb1df146756 1067 * @brief Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
mbed_official 76:aeb1df146756 1068 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1069 * @retval LSB of the 9-bit CAN Transmit Error Counter.
mbed_official 76:aeb1df146756 1070 */
mbed_official 76:aeb1df146756 1071 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
mbed_official 76:aeb1df146756 1072 {
mbed_official 76:aeb1df146756 1073 uint8_t counter=0;
mbed_official 76:aeb1df146756 1074
mbed_official 76:aeb1df146756 1075 /* Check the parameters */
mbed_official 76:aeb1df146756 1076 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1077
mbed_official 76:aeb1df146756 1078 /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
mbed_official 76:aeb1df146756 1079 counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
mbed_official 76:aeb1df146756 1080
mbed_official 76:aeb1df146756 1081 /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
mbed_official 76:aeb1df146756 1082 return counter;
mbed_official 76:aeb1df146756 1083 }
mbed_official 76:aeb1df146756 1084 /**
mbed_official 76:aeb1df146756 1085 * @}
mbed_official 76:aeb1df146756 1086 */
mbed_official 76:aeb1df146756 1087
mbed_official 76:aeb1df146756 1088 /** @defgroup CAN_Group6 Interrupts and flags management functions
mbed_official 76:aeb1df146756 1089 * @brief Interrupts and flags management functions
mbed_official 76:aeb1df146756 1090 *
mbed_official 76:aeb1df146756 1091 @verbatim
mbed_official 76:aeb1df146756 1092 ===============================================================================
mbed_official 76:aeb1df146756 1093 ##### Interrupts and flags management functions #####
mbed_official 76:aeb1df146756 1094 ===============================================================================
mbed_official 76:aeb1df146756 1095 [..] This section provides functions allowing to configure the CAN Interrupts
mbed_official 76:aeb1df146756 1096 and to get the status and clear flags and Interrupts pending bits.
mbed_official 76:aeb1df146756 1097 [..] The CAN provides 14 Interrupts sources and 15 Flags:
mbed_official 76:aeb1df146756 1098
mbed_official 76:aeb1df146756 1099 *** Flags ***
mbed_official 76:aeb1df146756 1100 =============
mbed_official 76:aeb1df146756 1101 [..] The 15 flags can be divided on 4 groups:
mbed_official 76:aeb1df146756 1102 (+) Transmit Flags:
mbed_official 76:aeb1df146756 1103 (++) CAN_FLAG_RQCP0.
mbed_official 76:aeb1df146756 1104 (++) CAN_FLAG_RQCP1.
mbed_official 76:aeb1df146756 1105 (++) CAN_FLAG_RQCP2: Request completed MailBoxes 0, 1 and 2 Flags
mbed_official 76:aeb1df146756 1106 Set when when the last request (transmit or abort) has
mbed_official 76:aeb1df146756 1107 been performed.
mbed_official 76:aeb1df146756 1108 (+) Receive Flags:
mbed_official 76:aeb1df146756 1109 (++) CAN_FLAG_FMP0.
mbed_official 76:aeb1df146756 1110 (++) CAN_FLAG_FMP1: FIFO 0 and 1 Message Pending Flags;
mbed_official 76:aeb1df146756 1111 Set to signal that messages are pending in the receive FIFO.
mbed_official 76:aeb1df146756 1112 These Flags are cleared only by hardware.
mbed_official 76:aeb1df146756 1113 (++) CAN_FLAG_FF0.
mbed_official 76:aeb1df146756 1114 (++) CAN_FLAG_FF1: FIFO 0 and 1 Full Flags;
mbed_official 76:aeb1df146756 1115 Set when three messages are stored in the selected FIFO.
mbed_official 76:aeb1df146756 1116 (++) CAN_FLAG_FOV0.
mbed_official 76:aeb1df146756 1117 (++) CAN_FLAG_FOV1: FIFO 0 and 1 Overrun Flags;
mbed_official 76:aeb1df146756 1118 Set when a new message has been received and passed the filter
mbed_official 76:aeb1df146756 1119 while the FIFO was full.
mbed_official 76:aeb1df146756 1120 (+) Operating Mode Flags:
mbed_official 76:aeb1df146756 1121 (++) CAN_FLAG_WKU: Wake up Flag;
mbed_official 76:aeb1df146756 1122 Set to signal that a SOF bit has been detected while the CAN
mbed_official 76:aeb1df146756 1123 hardware was in Sleep mode.
mbed_official 76:aeb1df146756 1124 (++) CAN_FLAG_SLAK: Sleep acknowledge Flag;
mbed_official 76:aeb1df146756 1125 Set to signal that the CAN has entered Sleep Mode.
mbed_official 76:aeb1df146756 1126 (+) Error Flags:
mbed_official 76:aeb1df146756 1127 (++) CAN_FLAG_EWG: Error Warning Flag;
mbed_official 76:aeb1df146756 1128 Set when the warning limit has been reached (Receive Error Counter
mbed_official 76:aeb1df146756 1129 or Transmit Error Counter greater than 96).
mbed_official 76:aeb1df146756 1130 This Flag is cleared only by hardware.
mbed_official 76:aeb1df146756 1131 (++) CAN_FLAG_EPV: Error Passive Flag;
mbed_official 76:aeb1df146756 1132 Set when the Error Passive limit has been reached (Receive Error
mbed_official 76:aeb1df146756 1133 Counter or Transmit Error Counter greater than 127).
mbed_official 76:aeb1df146756 1134 This Flag is cleared only by hardware.
mbed_official 76:aeb1df146756 1135 (++) CAN_FLAG_BOF: Bus-Off Flag;
mbed_official 76:aeb1df146756 1136 Set when CAN enters the bus-off state. The bus-off state is
mbed_official 76:aeb1df146756 1137 entered on TEC overflow, greater than 255.
mbed_official 76:aeb1df146756 1138 This Flag is cleared only by hardware.
mbed_official 76:aeb1df146756 1139 (++) CAN_FLAG_LEC: Last error code Flag;
mbed_official 76:aeb1df146756 1140 Set If a message has been transferred (reception or transmission)
mbed_official 76:aeb1df146756 1141 with error, and the error code is hold.
mbed_official 76:aeb1df146756 1142
mbed_official 76:aeb1df146756 1143 *** Interrupts ***
mbed_official 76:aeb1df146756 1144 ==================
mbed_official 76:aeb1df146756 1145 [..] The 14 interrupts can be divided on 4 groups:
mbed_official 76:aeb1df146756 1146 (+) Transmit interrupt:
mbed_official 76:aeb1df146756 1147 (++) CAN_IT_TME: Transmit mailbox empty Interrupt;
mbed_official 76:aeb1df146756 1148 If enabled, this interrupt source is pending when no transmit
mbed_official 76:aeb1df146756 1149 request are pending for Tx mailboxes.
mbed_official 76:aeb1df146756 1150 (+) Receive Interrupts:
mbed_official 76:aeb1df146756 1151 (++) CAN_IT_FMP0.
mbed_official 76:aeb1df146756 1152 (++) CAN_IT_FMP1: FIFO 0 and FIFO1 message pending Interrupts;
mbed_official 76:aeb1df146756 1153 If enabled, these interrupt sources are pending when messages
mbed_official 76:aeb1df146756 1154 are pending in the receive FIFO.
mbed_official 76:aeb1df146756 1155 The corresponding interrupt pending bits are cleared only by hardware.
mbed_official 76:aeb1df146756 1156 (++) CAN_IT_FF0.
mbed_official 76:aeb1df146756 1157 (++) CAN_IT_FF1: FIFO 0 and FIFO1 full Interrupts;
mbed_official 76:aeb1df146756 1158 If enabled, these interrupt sources are pending when three messages
mbed_official 76:aeb1df146756 1159 are stored in the selected FIFO.
mbed_official 76:aeb1df146756 1160 (++) CAN_IT_FOV0.
mbed_official 76:aeb1df146756 1161 (++) CAN_IT_FOV1: FIFO 0 and FIFO1 overrun Interrupts;
mbed_official 76:aeb1df146756 1162 If enabled, these interrupt sources are pending when a new message
mbed_official 76:aeb1df146756 1163 has been received and passed the filter while the FIFO was full.
mbed_official 76:aeb1df146756 1164 (+) Operating Mode Interrupts:
mbed_official 76:aeb1df146756 1165 (++) CAN_IT_WKU: Wake-up Interrupt;
mbed_official 76:aeb1df146756 1166 If enabled, this interrupt source is pending when a SOF bit has
mbed_official 76:aeb1df146756 1167 been detected while the CAN hardware was in Sleep mode.
mbed_official 76:aeb1df146756 1168 (++) CAN_IT_SLK: Sleep acknowledge Interrupt:
mbed_official 76:aeb1df146756 1169 If enabled, this interrupt source is pending when the CAN has
mbed_official 76:aeb1df146756 1170 entered Sleep Mode.
mbed_official 76:aeb1df146756 1171 (+) Error Interrupts:
mbed_official 76:aeb1df146756 1172 (++) CAN_IT_EWG: Error warning Interrupt;
mbed_official 76:aeb1df146756 1173 If enabled, this interrupt source is pending when the warning limit
mbed_official 76:aeb1df146756 1174 has been reached (Receive Error Counter or Transmit Error Counter=96).
mbed_official 76:aeb1df146756 1175 (++) CAN_IT_EPV: Error passive Interrupt;
mbed_official 76:aeb1df146756 1176 If enabled, this interrupt source is pending when the Error Passive
mbed_official 76:aeb1df146756 1177 limit has been reached (Receive Error Counter or Transmit Error Counter>127).
mbed_official 76:aeb1df146756 1178 (++) CAN_IT_BOF: Bus-off Interrupt;
mbed_official 76:aeb1df146756 1179 If enabled, this interrupt source is pending when CAN enters
mbed_official 76:aeb1df146756 1180 the bus-off state. The bus-off state is entered on TEC overflow,
mbed_official 76:aeb1df146756 1181 greater than 255.
mbed_official 76:aeb1df146756 1182 This Flag is cleared only by hardware.
mbed_official 76:aeb1df146756 1183 (++) CAN_IT_LEC: Last error code Interrupt;
mbed_official 76:aeb1df146756 1184 If enabled, this interrupt source is pending when a message has
mbed_official 76:aeb1df146756 1185 been transferred (reception or transmission) with error and the
mbed_official 76:aeb1df146756 1186 error code is hold.
mbed_official 76:aeb1df146756 1187 (++) CAN_IT_ERR: Error Interrupt;
mbed_official 76:aeb1df146756 1188 If enabled, this interrupt source is pending when an error condition
mbed_official 76:aeb1df146756 1189 is pending.
mbed_official 76:aeb1df146756 1190 [..] Managing the CAN controller events:
mbed_official 76:aeb1df146756 1191 The user should identify which mode will be used in his application to manage
mbed_official 76:aeb1df146756 1192 the CAN controller events: Polling mode or Interrupt mode.
mbed_official 76:aeb1df146756 1193 (+) In the Polling Mode it is advised to use the following functions:
mbed_official 76:aeb1df146756 1194 (++) CAN_GetFlagStatus() : to check if flags events occur.
mbed_official 76:aeb1df146756 1195 (++) CAN_ClearFlag() : to clear the flags events.
mbed_official 76:aeb1df146756 1196 (+) In the Interrupt Mode it is advised to use the following functions:
mbed_official 76:aeb1df146756 1197 (++) CAN_ITConfig() : to enable or disable the interrupt source.
mbed_official 76:aeb1df146756 1198 (++) CAN_GetITStatus() : to check if Interrupt occurs.
mbed_official 76:aeb1df146756 1199 (++) CAN_ClearITPendingBit() : to clear the Interrupt pending Bit
mbed_official 76:aeb1df146756 1200 (corresponding Flag).
mbed_official 76:aeb1df146756 1201 This function has no impact on CAN_IT_FMP0 and CAN_IT_FMP1 Interrupts
mbed_official 76:aeb1df146756 1202 pending bits since there are cleared only by hardware.
mbed_official 76:aeb1df146756 1203
mbed_official 76:aeb1df146756 1204 @endverbatim
mbed_official 76:aeb1df146756 1205 * @{
mbed_official 76:aeb1df146756 1206 */
mbed_official 76:aeb1df146756 1207 /**
mbed_official 76:aeb1df146756 1208 * @brief Enables or disables the specified CANx interrupts.
mbed_official 76:aeb1df146756 1209 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1210 * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
mbed_official 76:aeb1df146756 1211 * This parameter can be:
mbed_official 76:aeb1df146756 1212 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
mbed_official 76:aeb1df146756 1213 * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
mbed_official 76:aeb1df146756 1214 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
mbed_official 76:aeb1df146756 1215 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
mbed_official 76:aeb1df146756 1216 * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
mbed_official 76:aeb1df146756 1217 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
mbed_official 76:aeb1df146756 1218 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
mbed_official 76:aeb1df146756 1219 * @arg CAN_IT_WKU: Wake-up Interrupt
mbed_official 76:aeb1df146756 1220 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
mbed_official 76:aeb1df146756 1221 * @arg CAN_IT_EWG: Error warning Interrupt
mbed_official 76:aeb1df146756 1222 * @arg CAN_IT_EPV: Error passive Interrupt
mbed_official 76:aeb1df146756 1223 * @arg CAN_IT_BOF: Bus-off Interrupt
mbed_official 76:aeb1df146756 1224 * @arg CAN_IT_LEC: Last error code Interrupt
mbed_official 76:aeb1df146756 1225 * @arg CAN_IT_ERR: Error Interrupt
mbed_official 76:aeb1df146756 1226 * @param NewState: new state of the CAN interrupts.
mbed_official 76:aeb1df146756 1227 * This parameter can be: ENABLE or DISABLE.
mbed_official 76:aeb1df146756 1228 * @retval None
mbed_official 76:aeb1df146756 1229 */
mbed_official 76:aeb1df146756 1230 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
mbed_official 76:aeb1df146756 1231 {
mbed_official 76:aeb1df146756 1232 /* Check the parameters */
mbed_official 76:aeb1df146756 1233 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1234 assert_param(IS_CAN_IT(CAN_IT));
mbed_official 76:aeb1df146756 1235 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 76:aeb1df146756 1236
mbed_official 76:aeb1df146756 1237 if (NewState != DISABLE)
mbed_official 76:aeb1df146756 1238 {
mbed_official 76:aeb1df146756 1239 /* Enable the selected CANx interrupt */
mbed_official 76:aeb1df146756 1240 CANx->IER |= CAN_IT;
mbed_official 76:aeb1df146756 1241 }
mbed_official 76:aeb1df146756 1242 else
mbed_official 76:aeb1df146756 1243 {
mbed_official 76:aeb1df146756 1244 /* Disable the selected CANx interrupt */
mbed_official 76:aeb1df146756 1245 CANx->IER &= ~CAN_IT;
mbed_official 76:aeb1df146756 1246 }
mbed_official 76:aeb1df146756 1247 }
mbed_official 76:aeb1df146756 1248 /**
mbed_official 76:aeb1df146756 1249 * @brief Checks whether the specified CAN flag is set or not.
mbed_official 76:aeb1df146756 1250 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1251 * @param CAN_FLAG: specifies the flag to check.
mbed_official 76:aeb1df146756 1252 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1253 * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
mbed_official 76:aeb1df146756 1254 * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
mbed_official 76:aeb1df146756 1255 * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
mbed_official 76:aeb1df146756 1256 * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
mbed_official 76:aeb1df146756 1257 * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
mbed_official 76:aeb1df146756 1258 * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
mbed_official 76:aeb1df146756 1259 * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
mbed_official 76:aeb1df146756 1260 * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
mbed_official 76:aeb1df146756 1261 * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
mbed_official 76:aeb1df146756 1262 * @arg CAN_FLAG_WKU: Wake up Flag
mbed_official 76:aeb1df146756 1263 * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
mbed_official 76:aeb1df146756 1264 * @arg CAN_FLAG_EWG: Error Warning Flag
mbed_official 76:aeb1df146756 1265 * @arg CAN_FLAG_EPV: Error Passive Flag
mbed_official 76:aeb1df146756 1266 * @arg CAN_FLAG_BOF: Bus-Off Flag
mbed_official 76:aeb1df146756 1267 * @arg CAN_FLAG_LEC: Last error code Flag
mbed_official 76:aeb1df146756 1268 * @retval The new state of CAN_FLAG (SET or RESET).
mbed_official 76:aeb1df146756 1269 */
mbed_official 76:aeb1df146756 1270 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
mbed_official 76:aeb1df146756 1271 {
mbed_official 76:aeb1df146756 1272 FlagStatus bitstatus = RESET;
mbed_official 76:aeb1df146756 1273
mbed_official 76:aeb1df146756 1274 /* Check the parameters */
mbed_official 76:aeb1df146756 1275 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1276 assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
mbed_official 76:aeb1df146756 1277
mbed_official 76:aeb1df146756 1278
mbed_official 76:aeb1df146756 1279 if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1280 {
mbed_official 76:aeb1df146756 1281 /* Check the status of the specified CAN flag */
mbed_official 76:aeb1df146756 1282 if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1283 {
mbed_official 76:aeb1df146756 1284 /* CAN_FLAG is set */
mbed_official 76:aeb1df146756 1285 bitstatus = SET;
mbed_official 76:aeb1df146756 1286 }
mbed_official 76:aeb1df146756 1287 else
mbed_official 76:aeb1df146756 1288 {
mbed_official 76:aeb1df146756 1289 /* CAN_FLAG is reset */
mbed_official 76:aeb1df146756 1290 bitstatus = RESET;
mbed_official 76:aeb1df146756 1291 }
mbed_official 76:aeb1df146756 1292 }
mbed_official 76:aeb1df146756 1293 else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1294 {
mbed_official 76:aeb1df146756 1295 /* Check the status of the specified CAN flag */
mbed_official 76:aeb1df146756 1296 if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1297 {
mbed_official 76:aeb1df146756 1298 /* CAN_FLAG is set */
mbed_official 76:aeb1df146756 1299 bitstatus = SET;
mbed_official 76:aeb1df146756 1300 }
mbed_official 76:aeb1df146756 1301 else
mbed_official 76:aeb1df146756 1302 {
mbed_official 76:aeb1df146756 1303 /* CAN_FLAG is reset */
mbed_official 76:aeb1df146756 1304 bitstatus = RESET;
mbed_official 76:aeb1df146756 1305 }
mbed_official 76:aeb1df146756 1306 }
mbed_official 76:aeb1df146756 1307 else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1308 {
mbed_official 76:aeb1df146756 1309 /* Check the status of the specified CAN flag */
mbed_official 76:aeb1df146756 1310 if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1311 {
mbed_official 76:aeb1df146756 1312 /* CAN_FLAG is set */
mbed_official 76:aeb1df146756 1313 bitstatus = SET;
mbed_official 76:aeb1df146756 1314 }
mbed_official 76:aeb1df146756 1315 else
mbed_official 76:aeb1df146756 1316 {
mbed_official 76:aeb1df146756 1317 /* CAN_FLAG is reset */
mbed_official 76:aeb1df146756 1318 bitstatus = RESET;
mbed_official 76:aeb1df146756 1319 }
mbed_official 76:aeb1df146756 1320 }
mbed_official 76:aeb1df146756 1321 else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1322 {
mbed_official 76:aeb1df146756 1323 /* Check the status of the specified CAN flag */
mbed_official 76:aeb1df146756 1324 if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1325 {
mbed_official 76:aeb1df146756 1326 /* CAN_FLAG is set */
mbed_official 76:aeb1df146756 1327 bitstatus = SET;
mbed_official 76:aeb1df146756 1328 }
mbed_official 76:aeb1df146756 1329 else
mbed_official 76:aeb1df146756 1330 {
mbed_official 76:aeb1df146756 1331 /* CAN_FLAG is reset */
mbed_official 76:aeb1df146756 1332 bitstatus = RESET;
mbed_official 76:aeb1df146756 1333 }
mbed_official 76:aeb1df146756 1334 }
mbed_official 76:aeb1df146756 1335 else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
mbed_official 76:aeb1df146756 1336 {
mbed_official 76:aeb1df146756 1337 /* Check the status of the specified CAN flag */
mbed_official 76:aeb1df146756 1338 if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1339 {
mbed_official 76:aeb1df146756 1340 /* CAN_FLAG is set */
mbed_official 76:aeb1df146756 1341 bitstatus = SET;
mbed_official 76:aeb1df146756 1342 }
mbed_official 76:aeb1df146756 1343 else
mbed_official 76:aeb1df146756 1344 {
mbed_official 76:aeb1df146756 1345 /* CAN_FLAG is reset */
mbed_official 76:aeb1df146756 1346 bitstatus = RESET;
mbed_official 76:aeb1df146756 1347 }
mbed_official 76:aeb1df146756 1348 }
mbed_official 76:aeb1df146756 1349 /* Return the CAN_FLAG status */
mbed_official 76:aeb1df146756 1350 return bitstatus;
mbed_official 76:aeb1df146756 1351 }
mbed_official 76:aeb1df146756 1352
mbed_official 76:aeb1df146756 1353 /**
mbed_official 76:aeb1df146756 1354 * @brief Clears the CAN's pending flags.
mbed_official 76:aeb1df146756 1355 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1356 * @param CAN_FLAG: specifies the flag to clear.
mbed_official 76:aeb1df146756 1357 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1358 * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
mbed_official 76:aeb1df146756 1359 * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
mbed_official 76:aeb1df146756 1360 * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
mbed_official 76:aeb1df146756 1361 * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
mbed_official 76:aeb1df146756 1362 * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
mbed_official 76:aeb1df146756 1363 * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
mbed_official 76:aeb1df146756 1364 * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
mbed_official 76:aeb1df146756 1365 * @arg CAN_FLAG_WKU: Wake up Flag
mbed_official 76:aeb1df146756 1366 * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
mbed_official 76:aeb1df146756 1367 * @arg CAN_FLAG_LEC: Last error code Flag
mbed_official 76:aeb1df146756 1368 * @retval None
mbed_official 76:aeb1df146756 1369 */
mbed_official 76:aeb1df146756 1370 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
mbed_official 76:aeb1df146756 1371 {
mbed_official 76:aeb1df146756 1372 uint32_t flagtmp=0;
mbed_official 76:aeb1df146756 1373 /* Check the parameters */
mbed_official 76:aeb1df146756 1374 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1375 assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
mbed_official 76:aeb1df146756 1376
mbed_official 76:aeb1df146756 1377 if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
mbed_official 76:aeb1df146756 1378 {
mbed_official 76:aeb1df146756 1379 /* Clear the selected CAN flags */
mbed_official 76:aeb1df146756 1380 CANx->ESR = (uint32_t)RESET;
mbed_official 76:aeb1df146756 1381 }
mbed_official 76:aeb1df146756 1382 else /* MSR or TSR or RF0R or RF1R */
mbed_official 76:aeb1df146756 1383 {
mbed_official 76:aeb1df146756 1384 flagtmp = CAN_FLAG & 0x000FFFFF;
mbed_official 76:aeb1df146756 1385
mbed_official 76:aeb1df146756 1386 if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
mbed_official 76:aeb1df146756 1387 {
mbed_official 76:aeb1df146756 1388 /* Receive Flags */
mbed_official 76:aeb1df146756 1389 CANx->RF0R = (uint32_t)(flagtmp);
mbed_official 76:aeb1df146756 1390 }
mbed_official 76:aeb1df146756 1391 else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
mbed_official 76:aeb1df146756 1392 {
mbed_official 76:aeb1df146756 1393 /* Receive Flags */
mbed_official 76:aeb1df146756 1394 CANx->RF1R = (uint32_t)(flagtmp);
mbed_official 76:aeb1df146756 1395 }
mbed_official 76:aeb1df146756 1396 else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
mbed_official 76:aeb1df146756 1397 {
mbed_official 76:aeb1df146756 1398 /* Transmit Flags */
mbed_official 76:aeb1df146756 1399 CANx->TSR = (uint32_t)(flagtmp);
mbed_official 76:aeb1df146756 1400 }
mbed_official 76:aeb1df146756 1401 else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
mbed_official 76:aeb1df146756 1402 {
mbed_official 76:aeb1df146756 1403 /* Operating mode Flags */
mbed_official 76:aeb1df146756 1404 CANx->MSR = (uint32_t)(flagtmp);
mbed_official 76:aeb1df146756 1405 }
mbed_official 76:aeb1df146756 1406 }
mbed_official 76:aeb1df146756 1407 }
mbed_official 76:aeb1df146756 1408
mbed_official 76:aeb1df146756 1409 /**
mbed_official 76:aeb1df146756 1410 * @brief Checks whether the specified CANx interrupt has occurred or not.
mbed_official 76:aeb1df146756 1411 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1412 * @param CAN_IT: specifies the CAN interrupt source to check.
mbed_official 76:aeb1df146756 1413 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1414 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
mbed_official 76:aeb1df146756 1415 * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
mbed_official 76:aeb1df146756 1416 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
mbed_official 76:aeb1df146756 1417 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
mbed_official 76:aeb1df146756 1418 * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
mbed_official 76:aeb1df146756 1419 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
mbed_official 76:aeb1df146756 1420 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
mbed_official 76:aeb1df146756 1421 * @arg CAN_IT_WKU: Wake-up Interrupt
mbed_official 76:aeb1df146756 1422 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
mbed_official 76:aeb1df146756 1423 * @arg CAN_IT_EWG: Error warning Interrupt
mbed_official 76:aeb1df146756 1424 * @arg CAN_IT_EPV: Error passive Interrupt
mbed_official 76:aeb1df146756 1425 * @arg CAN_IT_BOF: Bus-off Interrupt
mbed_official 76:aeb1df146756 1426 * @arg CAN_IT_LEC: Last error code Interrupt
mbed_official 76:aeb1df146756 1427 * @arg CAN_IT_ERR: Error Interrupt
mbed_official 76:aeb1df146756 1428 * @retval The current state of CAN_IT (SET or RESET).
mbed_official 76:aeb1df146756 1429 */
mbed_official 76:aeb1df146756 1430 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
mbed_official 76:aeb1df146756 1431 {
mbed_official 76:aeb1df146756 1432 ITStatus itstatus = RESET;
mbed_official 76:aeb1df146756 1433 /* Check the parameters */
mbed_official 76:aeb1df146756 1434 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1435 assert_param(IS_CAN_IT(CAN_IT));
mbed_official 76:aeb1df146756 1436
mbed_official 76:aeb1df146756 1437 /* check the interrupt enable bit */
mbed_official 76:aeb1df146756 1438 if((CANx->IER & CAN_IT) != RESET)
mbed_official 76:aeb1df146756 1439 {
mbed_official 76:aeb1df146756 1440 /* in case the Interrupt is enabled, .... */
mbed_official 76:aeb1df146756 1441 switch (CAN_IT)
mbed_official 76:aeb1df146756 1442 {
mbed_official 76:aeb1df146756 1443 case CAN_IT_TME:
mbed_official 76:aeb1df146756 1444 /* Check CAN_TSR_RQCPx bits */
mbed_official 76:aeb1df146756 1445 itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);
mbed_official 76:aeb1df146756 1446 break;
mbed_official 76:aeb1df146756 1447 case CAN_IT_FMP0:
mbed_official 76:aeb1df146756 1448 /* Check CAN_RF0R_FMP0 bit */
mbed_official 76:aeb1df146756 1449 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);
mbed_official 76:aeb1df146756 1450 break;
mbed_official 76:aeb1df146756 1451 case CAN_IT_FF0:
mbed_official 76:aeb1df146756 1452 /* Check CAN_RF0R_FULL0 bit */
mbed_official 76:aeb1df146756 1453 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);
mbed_official 76:aeb1df146756 1454 break;
mbed_official 76:aeb1df146756 1455 case CAN_IT_FOV0:
mbed_official 76:aeb1df146756 1456 /* Check CAN_RF0R_FOVR0 bit */
mbed_official 76:aeb1df146756 1457 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);
mbed_official 76:aeb1df146756 1458 break;
mbed_official 76:aeb1df146756 1459 case CAN_IT_FMP1:
mbed_official 76:aeb1df146756 1460 /* Check CAN_RF1R_FMP1 bit */
mbed_official 76:aeb1df146756 1461 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);
mbed_official 76:aeb1df146756 1462 break;
mbed_official 76:aeb1df146756 1463 case CAN_IT_FF1:
mbed_official 76:aeb1df146756 1464 /* Check CAN_RF1R_FULL1 bit */
mbed_official 76:aeb1df146756 1465 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);
mbed_official 76:aeb1df146756 1466 break;
mbed_official 76:aeb1df146756 1467 case CAN_IT_FOV1:
mbed_official 76:aeb1df146756 1468 /* Check CAN_RF1R_FOVR1 bit */
mbed_official 76:aeb1df146756 1469 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);
mbed_official 76:aeb1df146756 1470 break;
mbed_official 76:aeb1df146756 1471 case CAN_IT_WKU:
mbed_official 76:aeb1df146756 1472 /* Check CAN_MSR_WKUI bit */
mbed_official 76:aeb1df146756 1473 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);
mbed_official 76:aeb1df146756 1474 break;
mbed_official 76:aeb1df146756 1475 case CAN_IT_SLK:
mbed_official 76:aeb1df146756 1476 /* Check CAN_MSR_SLAKI bit */
mbed_official 76:aeb1df146756 1477 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);
mbed_official 76:aeb1df146756 1478 break;
mbed_official 76:aeb1df146756 1479 case CAN_IT_EWG:
mbed_official 76:aeb1df146756 1480 /* Check CAN_ESR_EWGF bit */
mbed_official 76:aeb1df146756 1481 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);
mbed_official 76:aeb1df146756 1482 break;
mbed_official 76:aeb1df146756 1483 case CAN_IT_EPV:
mbed_official 76:aeb1df146756 1484 /* Check CAN_ESR_EPVF bit */
mbed_official 76:aeb1df146756 1485 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);
mbed_official 76:aeb1df146756 1486 break;
mbed_official 76:aeb1df146756 1487 case CAN_IT_BOF:
mbed_official 76:aeb1df146756 1488 /* Check CAN_ESR_BOFF bit */
mbed_official 76:aeb1df146756 1489 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);
mbed_official 76:aeb1df146756 1490 break;
mbed_official 76:aeb1df146756 1491 case CAN_IT_LEC:
mbed_official 76:aeb1df146756 1492 /* Check CAN_ESR_LEC bit */
mbed_official 76:aeb1df146756 1493 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);
mbed_official 76:aeb1df146756 1494 break;
mbed_official 76:aeb1df146756 1495 case CAN_IT_ERR:
mbed_official 76:aeb1df146756 1496 /* Check CAN_MSR_ERRI bit */
mbed_official 76:aeb1df146756 1497 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI);
mbed_official 76:aeb1df146756 1498 break;
mbed_official 76:aeb1df146756 1499 default:
mbed_official 76:aeb1df146756 1500 /* in case of error, return RESET */
mbed_official 76:aeb1df146756 1501 itstatus = RESET;
mbed_official 76:aeb1df146756 1502 break;
mbed_official 76:aeb1df146756 1503 }
mbed_official 76:aeb1df146756 1504 }
mbed_official 76:aeb1df146756 1505 else
mbed_official 76:aeb1df146756 1506 {
mbed_official 76:aeb1df146756 1507 /* in case the Interrupt is not enabled, return RESET */
mbed_official 76:aeb1df146756 1508 itstatus = RESET;
mbed_official 76:aeb1df146756 1509 }
mbed_official 76:aeb1df146756 1510
mbed_official 76:aeb1df146756 1511 /* Return the CAN_IT status */
mbed_official 76:aeb1df146756 1512 return itstatus;
mbed_official 76:aeb1df146756 1513 }
mbed_official 76:aeb1df146756 1514
mbed_official 76:aeb1df146756 1515 /**
mbed_official 76:aeb1df146756 1516 * @brief Clears the CANx's interrupt pending bits.
mbed_official 76:aeb1df146756 1517 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 76:aeb1df146756 1518 * @param CAN_IT: specifies the interrupt pending bit to clear.
mbed_official 76:aeb1df146756 1519 * This parameter can be one of the following values:
mbed_official 76:aeb1df146756 1520 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
mbed_official 76:aeb1df146756 1521 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
mbed_official 76:aeb1df146756 1522 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
mbed_official 76:aeb1df146756 1523 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
mbed_official 76:aeb1df146756 1524 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
mbed_official 76:aeb1df146756 1525 * @arg CAN_IT_WKU: Wake-up Interrupt
mbed_official 76:aeb1df146756 1526 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
mbed_official 76:aeb1df146756 1527 * @arg CAN_IT_EWG: Error warning Interrupt
mbed_official 76:aeb1df146756 1528 * @arg CAN_IT_EPV: Error passive Interrupt
mbed_official 76:aeb1df146756 1529 * @arg CAN_IT_BOF: Bus-off Interrupt
mbed_official 76:aeb1df146756 1530 * @arg CAN_IT_LEC: Last error code Interrupt
mbed_official 76:aeb1df146756 1531 * @arg CAN_IT_ERR: Error Interrupt
mbed_official 76:aeb1df146756 1532 * @retval None
mbed_official 76:aeb1df146756 1533 */
mbed_official 76:aeb1df146756 1534 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
mbed_official 76:aeb1df146756 1535 {
mbed_official 76:aeb1df146756 1536 /* Check the parameters */
mbed_official 76:aeb1df146756 1537 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 76:aeb1df146756 1538 assert_param(IS_CAN_CLEAR_IT(CAN_IT));
mbed_official 76:aeb1df146756 1539
mbed_official 76:aeb1df146756 1540 switch (CAN_IT)
mbed_official 76:aeb1df146756 1541 {
mbed_official 76:aeb1df146756 1542 case CAN_IT_TME:
mbed_official 76:aeb1df146756 1543 /* Clear CAN_TSR_RQCPx (rc_w1)*/
mbed_official 76:aeb1df146756 1544 CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;
mbed_official 76:aeb1df146756 1545 break;
mbed_official 76:aeb1df146756 1546 case CAN_IT_FF0:
mbed_official 76:aeb1df146756 1547 /* Clear CAN_RF0R_FULL0 (rc_w1)*/
mbed_official 76:aeb1df146756 1548 CANx->RF0R = CAN_RF0R_FULL0;
mbed_official 76:aeb1df146756 1549 break;
mbed_official 76:aeb1df146756 1550 case CAN_IT_FOV0:
mbed_official 76:aeb1df146756 1551 /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
mbed_official 76:aeb1df146756 1552 CANx->RF0R = CAN_RF0R_FOVR0;
mbed_official 76:aeb1df146756 1553 break;
mbed_official 76:aeb1df146756 1554 case CAN_IT_FF1:
mbed_official 76:aeb1df146756 1555 /* Clear CAN_RF1R_FULL1 (rc_w1)*/
mbed_official 76:aeb1df146756 1556 CANx->RF1R = CAN_RF1R_FULL1;
mbed_official 76:aeb1df146756 1557 break;
mbed_official 76:aeb1df146756 1558 case CAN_IT_FOV1:
mbed_official 76:aeb1df146756 1559 /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
mbed_official 76:aeb1df146756 1560 CANx->RF1R = CAN_RF1R_FOVR1;
mbed_official 76:aeb1df146756 1561 break;
mbed_official 76:aeb1df146756 1562 case CAN_IT_WKU:
mbed_official 76:aeb1df146756 1563 /* Clear CAN_MSR_WKUI (rc_w1)*/
mbed_official 76:aeb1df146756 1564 CANx->MSR = CAN_MSR_WKUI;
mbed_official 76:aeb1df146756 1565 break;
mbed_official 76:aeb1df146756 1566 case CAN_IT_SLK:
mbed_official 76:aeb1df146756 1567 /* Clear CAN_MSR_SLAKI (rc_w1)*/
mbed_official 76:aeb1df146756 1568 CANx->MSR = CAN_MSR_SLAKI;
mbed_official 76:aeb1df146756 1569 break;
mbed_official 76:aeb1df146756 1570 case CAN_IT_EWG:
mbed_official 76:aeb1df146756 1571 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 76:aeb1df146756 1572 CANx->MSR = CAN_MSR_ERRI;
mbed_official 76:aeb1df146756 1573 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
mbed_official 76:aeb1df146756 1574 break;
mbed_official 76:aeb1df146756 1575 case CAN_IT_EPV:
mbed_official 76:aeb1df146756 1576 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 76:aeb1df146756 1577 CANx->MSR = CAN_MSR_ERRI;
mbed_official 76:aeb1df146756 1578 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
mbed_official 76:aeb1df146756 1579 break;
mbed_official 76:aeb1df146756 1580 case CAN_IT_BOF:
mbed_official 76:aeb1df146756 1581 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 76:aeb1df146756 1582 CANx->MSR = CAN_MSR_ERRI;
mbed_official 76:aeb1df146756 1583 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
mbed_official 76:aeb1df146756 1584 break;
mbed_official 76:aeb1df146756 1585 case CAN_IT_LEC:
mbed_official 76:aeb1df146756 1586 /* Clear LEC bits */
mbed_official 76:aeb1df146756 1587 CANx->ESR = RESET;
mbed_official 76:aeb1df146756 1588 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 76:aeb1df146756 1589 CANx->MSR = CAN_MSR_ERRI;
mbed_official 76:aeb1df146756 1590 break;
mbed_official 76:aeb1df146756 1591 case CAN_IT_ERR:
mbed_official 76:aeb1df146756 1592 /*Clear LEC bits */
mbed_official 76:aeb1df146756 1593 CANx->ESR = RESET;
mbed_official 76:aeb1df146756 1594 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 76:aeb1df146756 1595 CANx->MSR = CAN_MSR_ERRI;
mbed_official 76:aeb1df146756 1596 /* @note BOFF, EPVF and EWGF Flags are cleared by hardware depending on the CAN Bus status*/
mbed_official 76:aeb1df146756 1597 break;
mbed_official 76:aeb1df146756 1598 default:
mbed_official 76:aeb1df146756 1599 break;
mbed_official 76:aeb1df146756 1600 }
mbed_official 76:aeb1df146756 1601 }
mbed_official 76:aeb1df146756 1602 /**
mbed_official 76:aeb1df146756 1603 * @}
mbed_official 76:aeb1df146756 1604 */
mbed_official 76:aeb1df146756 1605
mbed_official 76:aeb1df146756 1606 /**
mbed_official 76:aeb1df146756 1607 * @brief Checks whether the CAN interrupt has occurred or not.
mbed_official 76:aeb1df146756 1608 * @param CAN_Reg: specifies the CAN interrupt register to check.
mbed_official 76:aeb1df146756 1609 * @param It_Bit: specifies the interrupt source bit to check.
mbed_official 76:aeb1df146756 1610 * @retval The new state of the CAN Interrupt (SET or RESET).
mbed_official 76:aeb1df146756 1611 */
mbed_official 76:aeb1df146756 1612 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
mbed_official 76:aeb1df146756 1613 {
mbed_official 76:aeb1df146756 1614 ITStatus pendingbitstatus = RESET;
mbed_official 76:aeb1df146756 1615
mbed_official 76:aeb1df146756 1616 if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
mbed_official 76:aeb1df146756 1617 {
mbed_official 76:aeb1df146756 1618 /* CAN_IT is set */
mbed_official 76:aeb1df146756 1619 pendingbitstatus = SET;
mbed_official 76:aeb1df146756 1620 }
mbed_official 76:aeb1df146756 1621 else
mbed_official 76:aeb1df146756 1622 {
mbed_official 76:aeb1df146756 1623 /* CAN_IT is reset */
mbed_official 76:aeb1df146756 1624 pendingbitstatus = RESET;
mbed_official 76:aeb1df146756 1625 }
mbed_official 76:aeb1df146756 1626 return pendingbitstatus;
mbed_official 76:aeb1df146756 1627 }
mbed_official 76:aeb1df146756 1628
mbed_official 76:aeb1df146756 1629 /**
mbed_official 76:aeb1df146756 1630 * @}
mbed_official 76:aeb1df146756 1631 */
mbed_official 76:aeb1df146756 1632
mbed_official 76:aeb1df146756 1633 /**
mbed_official 76:aeb1df146756 1634 * @}
mbed_official 76:aeb1df146756 1635 */
mbed_official 76:aeb1df146756 1636
mbed_official 76:aeb1df146756 1637 /**
mbed_official 76:aeb1df146756 1638 * @}
mbed_official 76:aeb1df146756 1639 */
mbed_official 76:aeb1df146756 1640
mbed_official 76:aeb1df146756 1641 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/