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:
Mon Jan 27 14:30:07 2014 +0000
Revision:
76:aeb1df146756
Child:
106:ced8cbb51063
Synchronized with git revision a31ec9c5f7bcb5c8a1b2eced103f6a1dfa921abd

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

Add NUCLEO_L152RE

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