mbed library sources modified for open wear

Dependents:   openwear-lifelogger-example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Dec 02 11:30:05 2013 +0000
Revision:
52:a51c77007319
Child:
70:c1fbde68b492
Synchronized with git revision 49df530ae72ce97ccc773d1f2c13b38e868e6abd

Full URL: https://github.com/mbedmicro/mbed/commit/49df530ae72ce97ccc773d1f2c13b38e868e6abd/

Add STMicroelectronics NUCLEO_F103RB target

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /**
mbed_official 52:a51c77007319 2 ******************************************************************************
mbed_official 52:a51c77007319 3 * @file stm32f10x_can.c
mbed_official 52:a51c77007319 4 * @author MCD Application Team
mbed_official 52:a51c77007319 5 * @version V3.5.0
mbed_official 52:a51c77007319 6 * @date 11-March-2011
mbed_official 52:a51c77007319 7 * @brief This file provides all the CAN firmware functions.
mbed_official 52:a51c77007319 8 ******************************************************************************
mbed_official 52:a51c77007319 9 * @attention
mbed_official 52:a51c77007319 10 *
mbed_official 52:a51c77007319 11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
mbed_official 52:a51c77007319 12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
mbed_official 52:a51c77007319 13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
mbed_official 52:a51c77007319 14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
mbed_official 52:a51c77007319 15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
mbed_official 52:a51c77007319 16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
mbed_official 52:a51c77007319 17 *
mbed_official 52:a51c77007319 18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
mbed_official 52:a51c77007319 19 ******************************************************************************
mbed_official 52:a51c77007319 20 */
mbed_official 52:a51c77007319 21
mbed_official 52:a51c77007319 22 /* Includes ------------------------------------------------------------------*/
mbed_official 52:a51c77007319 23 #include "stm32f10x_can.h"
mbed_official 52:a51c77007319 24 #include "stm32f10x_rcc.h"
mbed_official 52:a51c77007319 25
mbed_official 52:a51c77007319 26 /** @addtogroup STM32F10x_StdPeriph_Driver
mbed_official 52:a51c77007319 27 * @{
mbed_official 52:a51c77007319 28 */
mbed_official 52:a51c77007319 29
mbed_official 52:a51c77007319 30 /** @defgroup CAN
mbed_official 52:a51c77007319 31 * @brief CAN driver modules
mbed_official 52:a51c77007319 32 * @{
mbed_official 52:a51c77007319 33 */
mbed_official 52:a51c77007319 34
mbed_official 52:a51c77007319 35 /** @defgroup CAN_Private_TypesDefinitions
mbed_official 52:a51c77007319 36 * @{
mbed_official 52:a51c77007319 37 */
mbed_official 52:a51c77007319 38
mbed_official 52:a51c77007319 39 /**
mbed_official 52:a51c77007319 40 * @}
mbed_official 52:a51c77007319 41 */
mbed_official 52:a51c77007319 42
mbed_official 52:a51c77007319 43 /** @defgroup CAN_Private_Defines
mbed_official 52:a51c77007319 44 * @{
mbed_official 52:a51c77007319 45 */
mbed_official 52:a51c77007319 46
mbed_official 52:a51c77007319 47 /* CAN Master Control Register bits */
mbed_official 52:a51c77007319 48
mbed_official 52:a51c77007319 49 #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
mbed_official 52:a51c77007319 50
mbed_official 52:a51c77007319 51 /* CAN Mailbox Transmit Request */
mbed_official 52:a51c77007319 52 #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
mbed_official 52:a51c77007319 53
mbed_official 52:a51c77007319 54 /* CAN Filter Master Register bits */
mbed_official 52:a51c77007319 55 #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
mbed_official 52:a51c77007319 56
mbed_official 52:a51c77007319 57 /* Time out for INAK bit */
mbed_official 52:a51c77007319 58 #define INAK_TIMEOUT ((uint32_t)0x0000FFFF)
mbed_official 52:a51c77007319 59 /* Time out for SLAK bit */
mbed_official 52:a51c77007319 60 #define SLAK_TIMEOUT ((uint32_t)0x0000FFFF)
mbed_official 52:a51c77007319 61
mbed_official 52:a51c77007319 62
mbed_official 52:a51c77007319 63
mbed_official 52:a51c77007319 64 /* Flags in TSR register */
mbed_official 52:a51c77007319 65 #define CAN_FLAGS_TSR ((uint32_t)0x08000000)
mbed_official 52:a51c77007319 66 /* Flags in RF1R register */
mbed_official 52:a51c77007319 67 #define CAN_FLAGS_RF1R ((uint32_t)0x04000000)
mbed_official 52:a51c77007319 68 /* Flags in RF0R register */
mbed_official 52:a51c77007319 69 #define CAN_FLAGS_RF0R ((uint32_t)0x02000000)
mbed_official 52:a51c77007319 70 /* Flags in MSR register */
mbed_official 52:a51c77007319 71 #define CAN_FLAGS_MSR ((uint32_t)0x01000000)
mbed_official 52:a51c77007319 72 /* Flags in ESR register */
mbed_official 52:a51c77007319 73 #define CAN_FLAGS_ESR ((uint32_t)0x00F00000)
mbed_official 52:a51c77007319 74
mbed_official 52:a51c77007319 75 /* Mailboxes definition */
mbed_official 52:a51c77007319 76 #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
mbed_official 52:a51c77007319 77 #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
mbed_official 52:a51c77007319 78 #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
mbed_official 52:a51c77007319 79
mbed_official 52:a51c77007319 80
mbed_official 52:a51c77007319 81
mbed_official 52:a51c77007319 82 #define CAN_MODE_MASK ((uint32_t) 0x00000003)
mbed_official 52:a51c77007319 83 /**
mbed_official 52:a51c77007319 84 * @}
mbed_official 52:a51c77007319 85 */
mbed_official 52:a51c77007319 86
mbed_official 52:a51c77007319 87 /** @defgroup CAN_Private_Macros
mbed_official 52:a51c77007319 88 * @{
mbed_official 52:a51c77007319 89 */
mbed_official 52:a51c77007319 90
mbed_official 52:a51c77007319 91 /**
mbed_official 52:a51c77007319 92 * @}
mbed_official 52:a51c77007319 93 */
mbed_official 52:a51c77007319 94
mbed_official 52:a51c77007319 95 /** @defgroup CAN_Private_Variables
mbed_official 52:a51c77007319 96 * @{
mbed_official 52:a51c77007319 97 */
mbed_official 52:a51c77007319 98
mbed_official 52:a51c77007319 99 /**
mbed_official 52:a51c77007319 100 * @}
mbed_official 52:a51c77007319 101 */
mbed_official 52:a51c77007319 102
mbed_official 52:a51c77007319 103 /** @defgroup CAN_Private_FunctionPrototypes
mbed_official 52:a51c77007319 104 * @{
mbed_official 52:a51c77007319 105 */
mbed_official 52:a51c77007319 106
mbed_official 52:a51c77007319 107 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
mbed_official 52:a51c77007319 108
mbed_official 52:a51c77007319 109 /**
mbed_official 52:a51c77007319 110 * @}
mbed_official 52:a51c77007319 111 */
mbed_official 52:a51c77007319 112
mbed_official 52:a51c77007319 113 /** @defgroup CAN_Private_Functions
mbed_official 52:a51c77007319 114 * @{
mbed_official 52:a51c77007319 115 */
mbed_official 52:a51c77007319 116
mbed_official 52:a51c77007319 117 /**
mbed_official 52:a51c77007319 118 * @brief Deinitializes the CAN peripheral registers to their default reset values.
mbed_official 52:a51c77007319 119 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
mbed_official 52:a51c77007319 120 * @retval None.
mbed_official 52:a51c77007319 121 */
mbed_official 52:a51c77007319 122 void CAN_DeInit(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 123 {
mbed_official 52:a51c77007319 124 /* Check the parameters */
mbed_official 52:a51c77007319 125 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 126
mbed_official 52:a51c77007319 127 if (CANx == CAN1)
mbed_official 52:a51c77007319 128 {
mbed_official 52:a51c77007319 129 /* Enable CAN1 reset state */
mbed_official 52:a51c77007319 130 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
mbed_official 52:a51c77007319 131 /* Release CAN1 from reset state */
mbed_official 52:a51c77007319 132 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
mbed_official 52:a51c77007319 133 }
mbed_official 52:a51c77007319 134 else
mbed_official 52:a51c77007319 135 {
mbed_official 52:a51c77007319 136 /* Enable CAN2 reset state */
mbed_official 52:a51c77007319 137 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
mbed_official 52:a51c77007319 138 /* Release CAN2 from reset state */
mbed_official 52:a51c77007319 139 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
mbed_official 52:a51c77007319 140 }
mbed_official 52:a51c77007319 141 }
mbed_official 52:a51c77007319 142
mbed_official 52:a51c77007319 143 /**
mbed_official 52:a51c77007319 144 * @brief Initializes the CAN peripheral according to the specified
mbed_official 52:a51c77007319 145 * parameters in the CAN_InitStruct.
mbed_official 52:a51c77007319 146 * @param CANx: where x can be 1 or 2 to to select the CAN
mbed_official 52:a51c77007319 147 * peripheral.
mbed_official 52:a51c77007319 148 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
mbed_official 52:a51c77007319 149 * contains the configuration information for the
mbed_official 52:a51c77007319 150 * CAN peripheral.
mbed_official 52:a51c77007319 151 * @retval Constant indicates initialization succeed which will be
mbed_official 52:a51c77007319 152 * CAN_InitStatus_Failed or CAN_InitStatus_Success.
mbed_official 52:a51c77007319 153 */
mbed_official 52:a51c77007319 154 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
mbed_official 52:a51c77007319 155 {
mbed_official 52:a51c77007319 156 uint8_t InitStatus = CAN_InitStatus_Failed;
mbed_official 52:a51c77007319 157 uint32_t wait_ack = 0x00000000;
mbed_official 52:a51c77007319 158 /* Check the parameters */
mbed_official 52:a51c77007319 159 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 160 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
mbed_official 52:a51c77007319 161 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
mbed_official 52:a51c77007319 162 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
mbed_official 52:a51c77007319 163 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
mbed_official 52:a51c77007319 164 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
mbed_official 52:a51c77007319 165 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
mbed_official 52:a51c77007319 166 assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
mbed_official 52:a51c77007319 167 assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
mbed_official 52:a51c77007319 168 assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
mbed_official 52:a51c77007319 169 assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
mbed_official 52:a51c77007319 170 assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
mbed_official 52:a51c77007319 171
mbed_official 52:a51c77007319 172 /* Exit from sleep mode */
mbed_official 52:a51c77007319 173 CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
mbed_official 52:a51c77007319 174
mbed_official 52:a51c77007319 175 /* Request initialisation */
mbed_official 52:a51c77007319 176 CANx->MCR |= CAN_MCR_INRQ ;
mbed_official 52:a51c77007319 177
mbed_official 52:a51c77007319 178 /* Wait the acknowledge */
mbed_official 52:a51c77007319 179 while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
mbed_official 52:a51c77007319 180 {
mbed_official 52:a51c77007319 181 wait_ack++;
mbed_official 52:a51c77007319 182 }
mbed_official 52:a51c77007319 183
mbed_official 52:a51c77007319 184 /* Check acknowledge */
mbed_official 52:a51c77007319 185 if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
mbed_official 52:a51c77007319 186 {
mbed_official 52:a51c77007319 187 InitStatus = CAN_InitStatus_Failed;
mbed_official 52:a51c77007319 188 }
mbed_official 52:a51c77007319 189 else
mbed_official 52:a51c77007319 190 {
mbed_official 52:a51c77007319 191 /* Set the time triggered communication mode */
mbed_official 52:a51c77007319 192 if (CAN_InitStruct->CAN_TTCM == ENABLE)
mbed_official 52:a51c77007319 193 {
mbed_official 52:a51c77007319 194 CANx->MCR |= CAN_MCR_TTCM;
mbed_official 52:a51c77007319 195 }
mbed_official 52:a51c77007319 196 else
mbed_official 52:a51c77007319 197 {
mbed_official 52:a51c77007319 198 CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
mbed_official 52:a51c77007319 199 }
mbed_official 52:a51c77007319 200
mbed_official 52:a51c77007319 201 /* Set the automatic bus-off management */
mbed_official 52:a51c77007319 202 if (CAN_InitStruct->CAN_ABOM == ENABLE)
mbed_official 52:a51c77007319 203 {
mbed_official 52:a51c77007319 204 CANx->MCR |= CAN_MCR_ABOM;
mbed_official 52:a51c77007319 205 }
mbed_official 52:a51c77007319 206 else
mbed_official 52:a51c77007319 207 {
mbed_official 52:a51c77007319 208 CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
mbed_official 52:a51c77007319 209 }
mbed_official 52:a51c77007319 210
mbed_official 52:a51c77007319 211 /* Set the automatic wake-up mode */
mbed_official 52:a51c77007319 212 if (CAN_InitStruct->CAN_AWUM == ENABLE)
mbed_official 52:a51c77007319 213 {
mbed_official 52:a51c77007319 214 CANx->MCR |= CAN_MCR_AWUM;
mbed_official 52:a51c77007319 215 }
mbed_official 52:a51c77007319 216 else
mbed_official 52:a51c77007319 217 {
mbed_official 52:a51c77007319 218 CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
mbed_official 52:a51c77007319 219 }
mbed_official 52:a51c77007319 220
mbed_official 52:a51c77007319 221 /* Set the no automatic retransmission */
mbed_official 52:a51c77007319 222 if (CAN_InitStruct->CAN_NART == ENABLE)
mbed_official 52:a51c77007319 223 {
mbed_official 52:a51c77007319 224 CANx->MCR |= CAN_MCR_NART;
mbed_official 52:a51c77007319 225 }
mbed_official 52:a51c77007319 226 else
mbed_official 52:a51c77007319 227 {
mbed_official 52:a51c77007319 228 CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
mbed_official 52:a51c77007319 229 }
mbed_official 52:a51c77007319 230
mbed_official 52:a51c77007319 231 /* Set the receive FIFO locked mode */
mbed_official 52:a51c77007319 232 if (CAN_InitStruct->CAN_RFLM == ENABLE)
mbed_official 52:a51c77007319 233 {
mbed_official 52:a51c77007319 234 CANx->MCR |= CAN_MCR_RFLM;
mbed_official 52:a51c77007319 235 }
mbed_official 52:a51c77007319 236 else
mbed_official 52:a51c77007319 237 {
mbed_official 52:a51c77007319 238 CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
mbed_official 52:a51c77007319 239 }
mbed_official 52:a51c77007319 240
mbed_official 52:a51c77007319 241 /* Set the transmit FIFO priority */
mbed_official 52:a51c77007319 242 if (CAN_InitStruct->CAN_TXFP == ENABLE)
mbed_official 52:a51c77007319 243 {
mbed_official 52:a51c77007319 244 CANx->MCR |= CAN_MCR_TXFP;
mbed_official 52:a51c77007319 245 }
mbed_official 52:a51c77007319 246 else
mbed_official 52:a51c77007319 247 {
mbed_official 52:a51c77007319 248 CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
mbed_official 52:a51c77007319 249 }
mbed_official 52:a51c77007319 250
mbed_official 52:a51c77007319 251 /* Set the bit timing register */
mbed_official 52:a51c77007319 252 CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
mbed_official 52:a51c77007319 253 ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
mbed_official 52:a51c77007319 254 ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
mbed_official 52:a51c77007319 255 ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
mbed_official 52:a51c77007319 256 ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
mbed_official 52:a51c77007319 257
mbed_official 52:a51c77007319 258 /* Request leave initialisation */
mbed_official 52:a51c77007319 259 CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
mbed_official 52:a51c77007319 260
mbed_official 52:a51c77007319 261 /* Wait the acknowledge */
mbed_official 52:a51c77007319 262 wait_ack = 0;
mbed_official 52:a51c77007319 263
mbed_official 52:a51c77007319 264 while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
mbed_official 52:a51c77007319 265 {
mbed_official 52:a51c77007319 266 wait_ack++;
mbed_official 52:a51c77007319 267 }
mbed_official 52:a51c77007319 268
mbed_official 52:a51c77007319 269 /* ...and check acknowledged */
mbed_official 52:a51c77007319 270 if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
mbed_official 52:a51c77007319 271 {
mbed_official 52:a51c77007319 272 InitStatus = CAN_InitStatus_Failed;
mbed_official 52:a51c77007319 273 }
mbed_official 52:a51c77007319 274 else
mbed_official 52:a51c77007319 275 {
mbed_official 52:a51c77007319 276 InitStatus = CAN_InitStatus_Success ;
mbed_official 52:a51c77007319 277 }
mbed_official 52:a51c77007319 278 }
mbed_official 52:a51c77007319 279
mbed_official 52:a51c77007319 280 /* At this step, return the status of initialization */
mbed_official 52:a51c77007319 281 return InitStatus;
mbed_official 52:a51c77007319 282 }
mbed_official 52:a51c77007319 283
mbed_official 52:a51c77007319 284 /**
mbed_official 52:a51c77007319 285 * @brief Initializes the CAN peripheral according to the specified
mbed_official 52:a51c77007319 286 * parameters in the CAN_FilterInitStruct.
mbed_official 52:a51c77007319 287 * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef
mbed_official 52:a51c77007319 288 * structure that contains the configuration
mbed_official 52:a51c77007319 289 * information.
mbed_official 52:a51c77007319 290 * @retval None.
mbed_official 52:a51c77007319 291 */
mbed_official 52:a51c77007319 292 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
mbed_official 52:a51c77007319 293 {
mbed_official 52:a51c77007319 294 uint32_t filter_number_bit_pos = 0;
mbed_official 52:a51c77007319 295 /* Check the parameters */
mbed_official 52:a51c77007319 296 assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
mbed_official 52:a51c77007319 297 assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
mbed_official 52:a51c77007319 298 assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
mbed_official 52:a51c77007319 299 assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
mbed_official 52:a51c77007319 300 assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
mbed_official 52:a51c77007319 301
mbed_official 52:a51c77007319 302 filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
mbed_official 52:a51c77007319 303
mbed_official 52:a51c77007319 304 /* Initialisation mode for the filter */
mbed_official 52:a51c77007319 305 CAN1->FMR |= FMR_FINIT;
mbed_official 52:a51c77007319 306
mbed_official 52:a51c77007319 307 /* Filter Deactivation */
mbed_official 52:a51c77007319 308 CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 309
mbed_official 52:a51c77007319 310 /* Filter Scale */
mbed_official 52:a51c77007319 311 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
mbed_official 52:a51c77007319 312 {
mbed_official 52:a51c77007319 313 /* 16-bit scale for the filter */
mbed_official 52:a51c77007319 314 CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 315
mbed_official 52:a51c77007319 316 /* First 16-bit identifier and First 16-bit mask */
mbed_official 52:a51c77007319 317 /* Or First 16-bit identifier and Second 16-bit identifier */
mbed_official 52:a51c77007319 318 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
mbed_official 52:a51c77007319 319 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
mbed_official 52:a51c77007319 320 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
mbed_official 52:a51c77007319 321
mbed_official 52:a51c77007319 322 /* Second 16-bit identifier and Second 16-bit mask */
mbed_official 52:a51c77007319 323 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
mbed_official 52:a51c77007319 324 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
mbed_official 52:a51c77007319 325 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
mbed_official 52:a51c77007319 326 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
mbed_official 52:a51c77007319 327 }
mbed_official 52:a51c77007319 328
mbed_official 52:a51c77007319 329 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
mbed_official 52:a51c77007319 330 {
mbed_official 52:a51c77007319 331 /* 32-bit scale for the filter */
mbed_official 52:a51c77007319 332 CAN1->FS1R |= filter_number_bit_pos;
mbed_official 52:a51c77007319 333 /* 32-bit identifier or First 32-bit identifier */
mbed_official 52:a51c77007319 334 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
mbed_official 52:a51c77007319 335 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
mbed_official 52:a51c77007319 336 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
mbed_official 52:a51c77007319 337 /* 32-bit mask or Second 32-bit identifier */
mbed_official 52:a51c77007319 338 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
mbed_official 52:a51c77007319 339 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
mbed_official 52:a51c77007319 340 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
mbed_official 52:a51c77007319 341 }
mbed_official 52:a51c77007319 342
mbed_official 52:a51c77007319 343 /* Filter Mode */
mbed_official 52:a51c77007319 344 if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
mbed_official 52:a51c77007319 345 {
mbed_official 52:a51c77007319 346 /*Id/Mask mode for the filter*/
mbed_official 52:a51c77007319 347 CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 348 }
mbed_official 52:a51c77007319 349 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
mbed_official 52:a51c77007319 350 {
mbed_official 52:a51c77007319 351 /*Identifier list mode for the filter*/
mbed_official 52:a51c77007319 352 CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 353 }
mbed_official 52:a51c77007319 354
mbed_official 52:a51c77007319 355 /* Filter FIFO assignment */
mbed_official 52:a51c77007319 356 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
mbed_official 52:a51c77007319 357 {
mbed_official 52:a51c77007319 358 /* FIFO 0 assignation for the filter */
mbed_official 52:a51c77007319 359 CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 360 }
mbed_official 52:a51c77007319 361
mbed_official 52:a51c77007319 362 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
mbed_official 52:a51c77007319 363 {
mbed_official 52:a51c77007319 364 /* FIFO 1 assignation for the filter */
mbed_official 52:a51c77007319 365 CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
mbed_official 52:a51c77007319 366 }
mbed_official 52:a51c77007319 367
mbed_official 52:a51c77007319 368 /* Filter activation */
mbed_official 52:a51c77007319 369 if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
mbed_official 52:a51c77007319 370 {
mbed_official 52:a51c77007319 371 CAN1->FA1R |= filter_number_bit_pos;
mbed_official 52:a51c77007319 372 }
mbed_official 52:a51c77007319 373
mbed_official 52:a51c77007319 374 /* Leave the initialisation mode for the filter */
mbed_official 52:a51c77007319 375 CAN1->FMR &= ~FMR_FINIT;
mbed_official 52:a51c77007319 376 }
mbed_official 52:a51c77007319 377
mbed_official 52:a51c77007319 378 /**
mbed_official 52:a51c77007319 379 * @brief Fills each CAN_InitStruct member with its default value.
mbed_official 52:a51c77007319 380 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which
mbed_official 52:a51c77007319 381 * will be initialized.
mbed_official 52:a51c77007319 382 * @retval None.
mbed_official 52:a51c77007319 383 */
mbed_official 52:a51c77007319 384 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
mbed_official 52:a51c77007319 385 {
mbed_official 52:a51c77007319 386 /* Reset CAN init structure parameters values */
mbed_official 52:a51c77007319 387
mbed_official 52:a51c77007319 388 /* Initialize the time triggered communication mode */
mbed_official 52:a51c77007319 389 CAN_InitStruct->CAN_TTCM = DISABLE;
mbed_official 52:a51c77007319 390
mbed_official 52:a51c77007319 391 /* Initialize the automatic bus-off management */
mbed_official 52:a51c77007319 392 CAN_InitStruct->CAN_ABOM = DISABLE;
mbed_official 52:a51c77007319 393
mbed_official 52:a51c77007319 394 /* Initialize the automatic wake-up mode */
mbed_official 52:a51c77007319 395 CAN_InitStruct->CAN_AWUM = DISABLE;
mbed_official 52:a51c77007319 396
mbed_official 52:a51c77007319 397 /* Initialize the no automatic retransmission */
mbed_official 52:a51c77007319 398 CAN_InitStruct->CAN_NART = DISABLE;
mbed_official 52:a51c77007319 399
mbed_official 52:a51c77007319 400 /* Initialize the receive FIFO locked mode */
mbed_official 52:a51c77007319 401 CAN_InitStruct->CAN_RFLM = DISABLE;
mbed_official 52:a51c77007319 402
mbed_official 52:a51c77007319 403 /* Initialize the transmit FIFO priority */
mbed_official 52:a51c77007319 404 CAN_InitStruct->CAN_TXFP = DISABLE;
mbed_official 52:a51c77007319 405
mbed_official 52:a51c77007319 406 /* Initialize the CAN_Mode member */
mbed_official 52:a51c77007319 407 CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
mbed_official 52:a51c77007319 408
mbed_official 52:a51c77007319 409 /* Initialize the CAN_SJW member */
mbed_official 52:a51c77007319 410 CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
mbed_official 52:a51c77007319 411
mbed_official 52:a51c77007319 412 /* Initialize the CAN_BS1 member */
mbed_official 52:a51c77007319 413 CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
mbed_official 52:a51c77007319 414
mbed_official 52:a51c77007319 415 /* Initialize the CAN_BS2 member */
mbed_official 52:a51c77007319 416 CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
mbed_official 52:a51c77007319 417
mbed_official 52:a51c77007319 418 /* Initialize the CAN_Prescaler member */
mbed_official 52:a51c77007319 419 CAN_InitStruct->CAN_Prescaler = 1;
mbed_official 52:a51c77007319 420 }
mbed_official 52:a51c77007319 421
mbed_official 52:a51c77007319 422 /**
mbed_official 52:a51c77007319 423 * @brief Select the start bank filter for slave CAN.
mbed_official 52:a51c77007319 424 * @note This function applies only to STM32 Connectivity line devices.
mbed_official 52:a51c77007319 425 * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
mbed_official 52:a51c77007319 426 * @retval None.
mbed_official 52:a51c77007319 427 */
mbed_official 52:a51c77007319 428 void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
mbed_official 52:a51c77007319 429 {
mbed_official 52:a51c77007319 430 /* Check the parameters */
mbed_official 52:a51c77007319 431 assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
mbed_official 52:a51c77007319 432
mbed_official 52:a51c77007319 433 /* Enter Initialisation mode for the filter */
mbed_official 52:a51c77007319 434 CAN1->FMR |= FMR_FINIT;
mbed_official 52:a51c77007319 435
mbed_official 52:a51c77007319 436 /* Select the start slave bank */
mbed_official 52:a51c77007319 437 CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
mbed_official 52:a51c77007319 438 CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
mbed_official 52:a51c77007319 439
mbed_official 52:a51c77007319 440 /* Leave Initialisation mode for the filter */
mbed_official 52:a51c77007319 441 CAN1->FMR &= ~FMR_FINIT;
mbed_official 52:a51c77007319 442 }
mbed_official 52:a51c77007319 443
mbed_official 52:a51c77007319 444 /**
mbed_official 52:a51c77007319 445 * @brief Enables or disables the DBG Freeze for CAN.
mbed_official 52:a51c77007319 446 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 447 * @param NewState: new state of the CAN peripheral. This parameter can
mbed_official 52:a51c77007319 448 * be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 449 * @retval None.
mbed_official 52:a51c77007319 450 */
mbed_official 52:a51c77007319 451 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
mbed_official 52:a51c77007319 452 {
mbed_official 52:a51c77007319 453 /* Check the parameters */
mbed_official 52:a51c77007319 454 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 455 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 456
mbed_official 52:a51c77007319 457 if (NewState != DISABLE)
mbed_official 52:a51c77007319 458 {
mbed_official 52:a51c77007319 459 /* Enable Debug Freeze */
mbed_official 52:a51c77007319 460 CANx->MCR |= MCR_DBF;
mbed_official 52:a51c77007319 461 }
mbed_official 52:a51c77007319 462 else
mbed_official 52:a51c77007319 463 {
mbed_official 52:a51c77007319 464 /* Disable Debug Freeze */
mbed_official 52:a51c77007319 465 CANx->MCR &= ~MCR_DBF;
mbed_official 52:a51c77007319 466 }
mbed_official 52:a51c77007319 467 }
mbed_official 52:a51c77007319 468
mbed_official 52:a51c77007319 469
mbed_official 52:a51c77007319 470 /**
mbed_official 52:a51c77007319 471 * @brief Enables or disabes the CAN Time TriggerOperation communication mode.
mbed_official 52:a51c77007319 472 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 473 * @param NewState : Mode new state , can be one of @ref FunctionalState.
mbed_official 52:a51c77007319 474 * @note when enabled, Time stamp (TIME[15:0]) value is sent in the last
mbed_official 52:a51c77007319 475 * two data bytes of the 8-byte message: TIME[7:0] in data byte 6
mbed_official 52:a51c77007319 476 * and TIME[15:8] in data byte 7
mbed_official 52:a51c77007319 477 * @note DLC must be programmed as 8 in order Time Stamp (2 bytes) to be
mbed_official 52:a51c77007319 478 * sent over the CAN bus.
mbed_official 52:a51c77007319 479 * @retval None
mbed_official 52:a51c77007319 480 */
mbed_official 52:a51c77007319 481 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
mbed_official 52:a51c77007319 482 {
mbed_official 52:a51c77007319 483 /* Check the parameters */
mbed_official 52:a51c77007319 484 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 485 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 486 if (NewState != DISABLE)
mbed_official 52:a51c77007319 487 {
mbed_official 52:a51c77007319 488 /* Enable the TTCM mode */
mbed_official 52:a51c77007319 489 CANx->MCR |= CAN_MCR_TTCM;
mbed_official 52:a51c77007319 490
mbed_official 52:a51c77007319 491 /* Set TGT bits */
mbed_official 52:a51c77007319 492 CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
mbed_official 52:a51c77007319 493 CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
mbed_official 52:a51c77007319 494 CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
mbed_official 52:a51c77007319 495 }
mbed_official 52:a51c77007319 496 else
mbed_official 52:a51c77007319 497 {
mbed_official 52:a51c77007319 498 /* Disable the TTCM mode */
mbed_official 52:a51c77007319 499 CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
mbed_official 52:a51c77007319 500
mbed_official 52:a51c77007319 501 /* Reset TGT bits */
mbed_official 52:a51c77007319 502 CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
mbed_official 52:a51c77007319 503 CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
mbed_official 52:a51c77007319 504 CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
mbed_official 52:a51c77007319 505 }
mbed_official 52:a51c77007319 506 }
mbed_official 52:a51c77007319 507 /**
mbed_official 52:a51c77007319 508 * @brief Initiates the transmission of a message.
mbed_official 52:a51c77007319 509 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 510 * @param TxMessage: pointer to a structure which contains CAN Id, CAN
mbed_official 52:a51c77007319 511 * DLC and CAN data.
mbed_official 52:a51c77007319 512 * @retval The number of the mailbox that is used for transmission
mbed_official 52:a51c77007319 513 * or CAN_TxStatus_NoMailBox if there is no empty mailbox.
mbed_official 52:a51c77007319 514 */
mbed_official 52:a51c77007319 515 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
mbed_official 52:a51c77007319 516 {
mbed_official 52:a51c77007319 517 uint8_t transmit_mailbox = 0;
mbed_official 52:a51c77007319 518 /* Check the parameters */
mbed_official 52:a51c77007319 519 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 520 assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
mbed_official 52:a51c77007319 521 assert_param(IS_CAN_RTR(TxMessage->RTR));
mbed_official 52:a51c77007319 522 assert_param(IS_CAN_DLC(TxMessage->DLC));
mbed_official 52:a51c77007319 523
mbed_official 52:a51c77007319 524 /* Select one empty transmit mailbox */
mbed_official 52:a51c77007319 525 if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
mbed_official 52:a51c77007319 526 {
mbed_official 52:a51c77007319 527 transmit_mailbox = 0;
mbed_official 52:a51c77007319 528 }
mbed_official 52:a51c77007319 529 else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
mbed_official 52:a51c77007319 530 {
mbed_official 52:a51c77007319 531 transmit_mailbox = 1;
mbed_official 52:a51c77007319 532 }
mbed_official 52:a51c77007319 533 else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
mbed_official 52:a51c77007319 534 {
mbed_official 52:a51c77007319 535 transmit_mailbox = 2;
mbed_official 52:a51c77007319 536 }
mbed_official 52:a51c77007319 537 else
mbed_official 52:a51c77007319 538 {
mbed_official 52:a51c77007319 539 transmit_mailbox = CAN_TxStatus_NoMailBox;
mbed_official 52:a51c77007319 540 }
mbed_official 52:a51c77007319 541
mbed_official 52:a51c77007319 542 if (transmit_mailbox != CAN_TxStatus_NoMailBox)
mbed_official 52:a51c77007319 543 {
mbed_official 52:a51c77007319 544 /* Set up the Id */
mbed_official 52:a51c77007319 545 CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
mbed_official 52:a51c77007319 546 if (TxMessage->IDE == CAN_Id_Standard)
mbed_official 52:a51c77007319 547 {
mbed_official 52:a51c77007319 548 assert_param(IS_CAN_STDID(TxMessage->StdId));
mbed_official 52:a51c77007319 549 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
mbed_official 52:a51c77007319 550 TxMessage->RTR);
mbed_official 52:a51c77007319 551 }
mbed_official 52:a51c77007319 552 else
mbed_official 52:a51c77007319 553 {
mbed_official 52:a51c77007319 554 assert_param(IS_CAN_EXTID(TxMessage->ExtId));
mbed_official 52:a51c77007319 555 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
mbed_official 52:a51c77007319 556 TxMessage->IDE | \
mbed_official 52:a51c77007319 557 TxMessage->RTR);
mbed_official 52:a51c77007319 558 }
mbed_official 52:a51c77007319 559
mbed_official 52:a51c77007319 560 /* Set up the DLC */
mbed_official 52:a51c77007319 561 TxMessage->DLC &= (uint8_t)0x0000000F;
mbed_official 52:a51c77007319 562 CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
mbed_official 52:a51c77007319 563 CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
mbed_official 52:a51c77007319 564
mbed_official 52:a51c77007319 565 /* Set up the data field */
mbed_official 52:a51c77007319 566 CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
mbed_official 52:a51c77007319 567 ((uint32_t)TxMessage->Data[2] << 16) |
mbed_official 52:a51c77007319 568 ((uint32_t)TxMessage->Data[1] << 8) |
mbed_official 52:a51c77007319 569 ((uint32_t)TxMessage->Data[0]));
mbed_official 52:a51c77007319 570 CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
mbed_official 52:a51c77007319 571 ((uint32_t)TxMessage->Data[6] << 16) |
mbed_official 52:a51c77007319 572 ((uint32_t)TxMessage->Data[5] << 8) |
mbed_official 52:a51c77007319 573 ((uint32_t)TxMessage->Data[4]));
mbed_official 52:a51c77007319 574 /* Request transmission */
mbed_official 52:a51c77007319 575 CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
mbed_official 52:a51c77007319 576 }
mbed_official 52:a51c77007319 577 return transmit_mailbox;
mbed_official 52:a51c77007319 578 }
mbed_official 52:a51c77007319 579
mbed_official 52:a51c77007319 580 /**
mbed_official 52:a51c77007319 581 * @brief Checks the transmission of a message.
mbed_official 52:a51c77007319 582 * @param CANx: where x can be 1 or 2 to to select the
mbed_official 52:a51c77007319 583 * CAN peripheral.
mbed_official 52:a51c77007319 584 * @param TransmitMailbox: the number of the mailbox that is used for
mbed_official 52:a51c77007319 585 * transmission.
mbed_official 52:a51c77007319 586 * @retval CAN_TxStatus_Ok if the CAN driver transmits the message, CAN_TxStatus_Failed
mbed_official 52:a51c77007319 587 * in an other case.
mbed_official 52:a51c77007319 588 */
mbed_official 52:a51c77007319 589 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
mbed_official 52:a51c77007319 590 {
mbed_official 52:a51c77007319 591 uint32_t state = 0;
mbed_official 52:a51c77007319 592
mbed_official 52:a51c77007319 593 /* Check the parameters */
mbed_official 52:a51c77007319 594 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 595 assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
mbed_official 52:a51c77007319 596
mbed_official 52:a51c77007319 597 switch (TransmitMailbox)
mbed_official 52:a51c77007319 598 {
mbed_official 52:a51c77007319 599 case (CAN_TXMAILBOX_0):
mbed_official 52:a51c77007319 600 state = CANx->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
mbed_official 52:a51c77007319 601 break;
mbed_official 52:a51c77007319 602 case (CAN_TXMAILBOX_1):
mbed_official 52:a51c77007319 603 state = CANx->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
mbed_official 52:a51c77007319 604 break;
mbed_official 52:a51c77007319 605 case (CAN_TXMAILBOX_2):
mbed_official 52:a51c77007319 606 state = CANx->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
mbed_official 52:a51c77007319 607 break;
mbed_official 52:a51c77007319 608 default:
mbed_official 52:a51c77007319 609 state = CAN_TxStatus_Failed;
mbed_official 52:a51c77007319 610 break;
mbed_official 52:a51c77007319 611 }
mbed_official 52:a51c77007319 612 switch (state)
mbed_official 52:a51c77007319 613 {
mbed_official 52:a51c77007319 614 /* transmit pending */
mbed_official 52:a51c77007319 615 case (0x0): state = CAN_TxStatus_Pending;
mbed_official 52:a51c77007319 616 break;
mbed_official 52:a51c77007319 617 /* transmit failed */
mbed_official 52:a51c77007319 618 case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
mbed_official 52:a51c77007319 619 break;
mbed_official 52:a51c77007319 620 case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
mbed_official 52:a51c77007319 621 break;
mbed_official 52:a51c77007319 622 case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
mbed_official 52:a51c77007319 623 break;
mbed_official 52:a51c77007319 624 /* transmit succeeded */
mbed_official 52:a51c77007319 625 case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
mbed_official 52:a51c77007319 626 break;
mbed_official 52:a51c77007319 627 case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
mbed_official 52:a51c77007319 628 break;
mbed_official 52:a51c77007319 629 case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
mbed_official 52:a51c77007319 630 break;
mbed_official 52:a51c77007319 631 default: state = CAN_TxStatus_Failed;
mbed_official 52:a51c77007319 632 break;
mbed_official 52:a51c77007319 633 }
mbed_official 52:a51c77007319 634 return (uint8_t) state;
mbed_official 52:a51c77007319 635 }
mbed_official 52:a51c77007319 636
mbed_official 52:a51c77007319 637 /**
mbed_official 52:a51c77007319 638 * @brief Cancels a transmit request.
mbed_official 52:a51c77007319 639 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 640 * @param Mailbox: Mailbox number.
mbed_official 52:a51c77007319 641 * @retval None.
mbed_official 52:a51c77007319 642 */
mbed_official 52:a51c77007319 643 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
mbed_official 52:a51c77007319 644 {
mbed_official 52:a51c77007319 645 /* Check the parameters */
mbed_official 52:a51c77007319 646 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 647 assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
mbed_official 52:a51c77007319 648 /* abort transmission */
mbed_official 52:a51c77007319 649 switch (Mailbox)
mbed_official 52:a51c77007319 650 {
mbed_official 52:a51c77007319 651 case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
mbed_official 52:a51c77007319 652 break;
mbed_official 52:a51c77007319 653 case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
mbed_official 52:a51c77007319 654 break;
mbed_official 52:a51c77007319 655 case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
mbed_official 52:a51c77007319 656 break;
mbed_official 52:a51c77007319 657 default:
mbed_official 52:a51c77007319 658 break;
mbed_official 52:a51c77007319 659 }
mbed_official 52:a51c77007319 660 }
mbed_official 52:a51c77007319 661
mbed_official 52:a51c77007319 662
mbed_official 52:a51c77007319 663 /**
mbed_official 52:a51c77007319 664 * @brief Receives a message.
mbed_official 52:a51c77007319 665 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 666 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
mbed_official 52:a51c77007319 667 * @param RxMessage: pointer to a structure receive message which contains
mbed_official 52:a51c77007319 668 * CAN Id, CAN DLC, CAN datas and FMI number.
mbed_official 52:a51c77007319 669 * @retval None.
mbed_official 52:a51c77007319 670 */
mbed_official 52:a51c77007319 671 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
mbed_official 52:a51c77007319 672 {
mbed_official 52:a51c77007319 673 /* Check the parameters */
mbed_official 52:a51c77007319 674 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 675 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 52:a51c77007319 676 /* Get the Id */
mbed_official 52:a51c77007319 677 RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
mbed_official 52:a51c77007319 678 if (RxMessage->IDE == CAN_Id_Standard)
mbed_official 52:a51c77007319 679 {
mbed_official 52:a51c77007319 680 RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
mbed_official 52:a51c77007319 681 }
mbed_official 52:a51c77007319 682 else
mbed_official 52:a51c77007319 683 {
mbed_official 52:a51c77007319 684 RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
mbed_official 52:a51c77007319 685 }
mbed_official 52:a51c77007319 686
mbed_official 52:a51c77007319 687 RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
mbed_official 52:a51c77007319 688 /* Get the DLC */
mbed_official 52:a51c77007319 689 RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
mbed_official 52:a51c77007319 690 /* Get the FMI */
mbed_official 52:a51c77007319 691 RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
mbed_official 52:a51c77007319 692 /* Get the data field */
mbed_official 52:a51c77007319 693 RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
mbed_official 52:a51c77007319 694 RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
mbed_official 52:a51c77007319 695 RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
mbed_official 52:a51c77007319 696 RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
mbed_official 52:a51c77007319 697 RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
mbed_official 52:a51c77007319 698 RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
mbed_official 52:a51c77007319 699 RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
mbed_official 52:a51c77007319 700 RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
mbed_official 52:a51c77007319 701 /* Release the FIFO */
mbed_official 52:a51c77007319 702 /* Release FIFO0 */
mbed_official 52:a51c77007319 703 if (FIFONumber == CAN_FIFO0)
mbed_official 52:a51c77007319 704 {
mbed_official 52:a51c77007319 705 CANx->RF0R |= CAN_RF0R_RFOM0;
mbed_official 52:a51c77007319 706 }
mbed_official 52:a51c77007319 707 /* Release FIFO1 */
mbed_official 52:a51c77007319 708 else /* FIFONumber == CAN_FIFO1 */
mbed_official 52:a51c77007319 709 {
mbed_official 52:a51c77007319 710 CANx->RF1R |= CAN_RF1R_RFOM1;
mbed_official 52:a51c77007319 711 }
mbed_official 52:a51c77007319 712 }
mbed_official 52:a51c77007319 713
mbed_official 52:a51c77007319 714 /**
mbed_official 52:a51c77007319 715 * @brief Releases the specified FIFO.
mbed_official 52:a51c77007319 716 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 717 * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
mbed_official 52:a51c77007319 718 * @retval None.
mbed_official 52:a51c77007319 719 */
mbed_official 52:a51c77007319 720 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
mbed_official 52:a51c77007319 721 {
mbed_official 52:a51c77007319 722 /* Check the parameters */
mbed_official 52:a51c77007319 723 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 724 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 52:a51c77007319 725 /* Release FIFO0 */
mbed_official 52:a51c77007319 726 if (FIFONumber == CAN_FIFO0)
mbed_official 52:a51c77007319 727 {
mbed_official 52:a51c77007319 728 CANx->RF0R |= CAN_RF0R_RFOM0;
mbed_official 52:a51c77007319 729 }
mbed_official 52:a51c77007319 730 /* Release FIFO1 */
mbed_official 52:a51c77007319 731 else /* FIFONumber == CAN_FIFO1 */
mbed_official 52:a51c77007319 732 {
mbed_official 52:a51c77007319 733 CANx->RF1R |= CAN_RF1R_RFOM1;
mbed_official 52:a51c77007319 734 }
mbed_official 52:a51c77007319 735 }
mbed_official 52:a51c77007319 736
mbed_official 52:a51c77007319 737 /**
mbed_official 52:a51c77007319 738 * @brief Returns the number of pending messages.
mbed_official 52:a51c77007319 739 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 740 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
mbed_official 52:a51c77007319 741 * @retval NbMessage : which is the number of pending message.
mbed_official 52:a51c77007319 742 */
mbed_official 52:a51c77007319 743 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
mbed_official 52:a51c77007319 744 {
mbed_official 52:a51c77007319 745 uint8_t message_pending=0;
mbed_official 52:a51c77007319 746 /* Check the parameters */
mbed_official 52:a51c77007319 747 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 748 assert_param(IS_CAN_FIFO(FIFONumber));
mbed_official 52:a51c77007319 749 if (FIFONumber == CAN_FIFO0)
mbed_official 52:a51c77007319 750 {
mbed_official 52:a51c77007319 751 message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
mbed_official 52:a51c77007319 752 }
mbed_official 52:a51c77007319 753 else if (FIFONumber == CAN_FIFO1)
mbed_official 52:a51c77007319 754 {
mbed_official 52:a51c77007319 755 message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
mbed_official 52:a51c77007319 756 }
mbed_official 52:a51c77007319 757 else
mbed_official 52:a51c77007319 758 {
mbed_official 52:a51c77007319 759 message_pending = 0;
mbed_official 52:a51c77007319 760 }
mbed_official 52:a51c77007319 761 return message_pending;
mbed_official 52:a51c77007319 762 }
mbed_official 52:a51c77007319 763
mbed_official 52:a51c77007319 764
mbed_official 52:a51c77007319 765 /**
mbed_official 52:a51c77007319 766 * @brief Select the CAN Operation mode.
mbed_official 52:a51c77007319 767 * @param CAN_OperatingMode : CAN Operating Mode. This parameter can be one
mbed_official 52:a51c77007319 768 * of @ref CAN_OperatingMode_TypeDef enumeration.
mbed_official 52:a51c77007319 769 * @retval status of the requested mode which can be
mbed_official 52:a51c77007319 770 * - CAN_ModeStatus_Failed CAN failed entering the specific mode
mbed_official 52:a51c77007319 771 * - CAN_ModeStatus_Success CAN Succeed entering the specific mode
mbed_official 52:a51c77007319 772
mbed_official 52:a51c77007319 773 */
mbed_official 52:a51c77007319 774 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
mbed_official 52:a51c77007319 775 {
mbed_official 52:a51c77007319 776 uint8_t status = CAN_ModeStatus_Failed;
mbed_official 52:a51c77007319 777
mbed_official 52:a51c77007319 778 /* Timeout for INAK or also for SLAK bits*/
mbed_official 52:a51c77007319 779 uint32_t timeout = INAK_TIMEOUT;
mbed_official 52:a51c77007319 780
mbed_official 52:a51c77007319 781 /* Check the parameters */
mbed_official 52:a51c77007319 782 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 783 assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
mbed_official 52:a51c77007319 784
mbed_official 52:a51c77007319 785 if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
mbed_official 52:a51c77007319 786 {
mbed_official 52:a51c77007319 787 /* Request initialisation */
mbed_official 52:a51c77007319 788 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
mbed_official 52:a51c77007319 789
mbed_official 52:a51c77007319 790 /* Wait the acknowledge */
mbed_official 52:a51c77007319 791 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
mbed_official 52:a51c77007319 792 {
mbed_official 52:a51c77007319 793 timeout--;
mbed_official 52:a51c77007319 794 }
mbed_official 52:a51c77007319 795 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
mbed_official 52:a51c77007319 796 {
mbed_official 52:a51c77007319 797 status = CAN_ModeStatus_Failed;
mbed_official 52:a51c77007319 798 }
mbed_official 52:a51c77007319 799 else
mbed_official 52:a51c77007319 800 {
mbed_official 52:a51c77007319 801 status = CAN_ModeStatus_Success;
mbed_official 52:a51c77007319 802 }
mbed_official 52:a51c77007319 803 }
mbed_official 52:a51c77007319 804 else if (CAN_OperatingMode == CAN_OperatingMode_Normal)
mbed_official 52:a51c77007319 805 {
mbed_official 52:a51c77007319 806 /* Request leave initialisation and sleep mode and enter Normal mode */
mbed_official 52:a51c77007319 807 CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
mbed_official 52:a51c77007319 808
mbed_official 52:a51c77007319 809 /* Wait the acknowledge */
mbed_official 52:a51c77007319 810 while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
mbed_official 52:a51c77007319 811 {
mbed_official 52:a51c77007319 812 timeout--;
mbed_official 52:a51c77007319 813 }
mbed_official 52:a51c77007319 814 if ((CANx->MSR & CAN_MODE_MASK) != 0)
mbed_official 52:a51c77007319 815 {
mbed_official 52:a51c77007319 816 status = CAN_ModeStatus_Failed;
mbed_official 52:a51c77007319 817 }
mbed_official 52:a51c77007319 818 else
mbed_official 52:a51c77007319 819 {
mbed_official 52:a51c77007319 820 status = CAN_ModeStatus_Success;
mbed_official 52:a51c77007319 821 }
mbed_official 52:a51c77007319 822 }
mbed_official 52:a51c77007319 823 else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
mbed_official 52:a51c77007319 824 {
mbed_official 52:a51c77007319 825 /* Request Sleep mode */
mbed_official 52:a51c77007319 826 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
mbed_official 52:a51c77007319 827
mbed_official 52:a51c77007319 828 /* Wait the acknowledge */
mbed_official 52:a51c77007319 829 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
mbed_official 52:a51c77007319 830 {
mbed_official 52:a51c77007319 831 timeout--;
mbed_official 52:a51c77007319 832 }
mbed_official 52:a51c77007319 833 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
mbed_official 52:a51c77007319 834 {
mbed_official 52:a51c77007319 835 status = CAN_ModeStatus_Failed;
mbed_official 52:a51c77007319 836 }
mbed_official 52:a51c77007319 837 else
mbed_official 52:a51c77007319 838 {
mbed_official 52:a51c77007319 839 status = CAN_ModeStatus_Success;
mbed_official 52:a51c77007319 840 }
mbed_official 52:a51c77007319 841 }
mbed_official 52:a51c77007319 842 else
mbed_official 52:a51c77007319 843 {
mbed_official 52:a51c77007319 844 status = CAN_ModeStatus_Failed;
mbed_official 52:a51c77007319 845 }
mbed_official 52:a51c77007319 846
mbed_official 52:a51c77007319 847 return (uint8_t) status;
mbed_official 52:a51c77007319 848 }
mbed_official 52:a51c77007319 849
mbed_official 52:a51c77007319 850 /**
mbed_official 52:a51c77007319 851 * @brief Enters the low power mode.
mbed_official 52:a51c77007319 852 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 853 * @retval status: CAN_Sleep_Ok if sleep entered, CAN_Sleep_Failed in an
mbed_official 52:a51c77007319 854 * other case.
mbed_official 52:a51c77007319 855 */
mbed_official 52:a51c77007319 856 uint8_t CAN_Sleep(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 857 {
mbed_official 52:a51c77007319 858 uint8_t sleepstatus = CAN_Sleep_Failed;
mbed_official 52:a51c77007319 859
mbed_official 52:a51c77007319 860 /* Check the parameters */
mbed_official 52:a51c77007319 861 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 862
mbed_official 52:a51c77007319 863 /* Request Sleep mode */
mbed_official 52:a51c77007319 864 CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
mbed_official 52:a51c77007319 865
mbed_official 52:a51c77007319 866 /* Sleep mode status */
mbed_official 52:a51c77007319 867 if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
mbed_official 52:a51c77007319 868 {
mbed_official 52:a51c77007319 869 /* Sleep mode not entered */
mbed_official 52:a51c77007319 870 sleepstatus = CAN_Sleep_Ok;
mbed_official 52:a51c77007319 871 }
mbed_official 52:a51c77007319 872 /* return sleep mode status */
mbed_official 52:a51c77007319 873 return (uint8_t)sleepstatus;
mbed_official 52:a51c77007319 874 }
mbed_official 52:a51c77007319 875
mbed_official 52:a51c77007319 876 /**
mbed_official 52:a51c77007319 877 * @brief Wakes the CAN up.
mbed_official 52:a51c77007319 878 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 879 * @retval status: CAN_WakeUp_Ok if sleep mode left, CAN_WakeUp_Failed in an
mbed_official 52:a51c77007319 880 * other case.
mbed_official 52:a51c77007319 881 */
mbed_official 52:a51c77007319 882 uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 883 {
mbed_official 52:a51c77007319 884 uint32_t wait_slak = SLAK_TIMEOUT;
mbed_official 52:a51c77007319 885 uint8_t wakeupstatus = CAN_WakeUp_Failed;
mbed_official 52:a51c77007319 886
mbed_official 52:a51c77007319 887 /* Check the parameters */
mbed_official 52:a51c77007319 888 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 889
mbed_official 52:a51c77007319 890 /* Wake up request */
mbed_official 52:a51c77007319 891 CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
mbed_official 52:a51c77007319 892
mbed_official 52:a51c77007319 893 /* Sleep mode status */
mbed_official 52:a51c77007319 894 while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
mbed_official 52:a51c77007319 895 {
mbed_official 52:a51c77007319 896 wait_slak--;
mbed_official 52:a51c77007319 897 }
mbed_official 52:a51c77007319 898 if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
mbed_official 52:a51c77007319 899 {
mbed_official 52:a51c77007319 900 /* wake up done : Sleep mode exited */
mbed_official 52:a51c77007319 901 wakeupstatus = CAN_WakeUp_Ok;
mbed_official 52:a51c77007319 902 }
mbed_official 52:a51c77007319 903 /* return wakeup status */
mbed_official 52:a51c77007319 904 return (uint8_t)wakeupstatus;
mbed_official 52:a51c77007319 905 }
mbed_official 52:a51c77007319 906
mbed_official 52:a51c77007319 907
mbed_official 52:a51c77007319 908 /**
mbed_official 52:a51c77007319 909 * @brief Returns the CANx's last error code (LEC).
mbed_official 52:a51c77007319 910 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 911 * @retval CAN_ErrorCode: specifies the Error code :
mbed_official 52:a51c77007319 912 * - CAN_ERRORCODE_NoErr No Error
mbed_official 52:a51c77007319 913 * - CAN_ERRORCODE_StuffErr Stuff Error
mbed_official 52:a51c77007319 914 * - CAN_ERRORCODE_FormErr Form Error
mbed_official 52:a51c77007319 915 * - CAN_ERRORCODE_ACKErr Acknowledgment Error
mbed_official 52:a51c77007319 916 * - CAN_ERRORCODE_BitRecessiveErr Bit Recessive Error
mbed_official 52:a51c77007319 917 * - CAN_ERRORCODE_BitDominantErr Bit Dominant Error
mbed_official 52:a51c77007319 918 * - CAN_ERRORCODE_CRCErr CRC Error
mbed_official 52:a51c77007319 919 * - CAN_ERRORCODE_SoftwareSetErr Software Set Error
mbed_official 52:a51c77007319 920 */
mbed_official 52:a51c77007319 921
mbed_official 52:a51c77007319 922 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 923 {
mbed_official 52:a51c77007319 924 uint8_t errorcode=0;
mbed_official 52:a51c77007319 925
mbed_official 52:a51c77007319 926 /* Check the parameters */
mbed_official 52:a51c77007319 927 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 928
mbed_official 52:a51c77007319 929 /* Get the error code*/
mbed_official 52:a51c77007319 930 errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
mbed_official 52:a51c77007319 931
mbed_official 52:a51c77007319 932 /* Return the error code*/
mbed_official 52:a51c77007319 933 return errorcode;
mbed_official 52:a51c77007319 934 }
mbed_official 52:a51c77007319 935 /**
mbed_official 52:a51c77007319 936 * @brief Returns the CANx Receive Error Counter (REC).
mbed_official 52:a51c77007319 937 * @note In case of an error during reception, this counter is incremented
mbed_official 52:a51c77007319 938 * by 1 or by 8 depending on the error condition as defined by the CAN
mbed_official 52:a51c77007319 939 * standard. After every successful reception, the counter is
mbed_official 52:a51c77007319 940 * decremented by 1 or reset to 120 if its value was higher than 128.
mbed_official 52:a51c77007319 941 * When the counter value exceeds 127, the CAN controller enters the
mbed_official 52:a51c77007319 942 * error passive state.
mbed_official 52:a51c77007319 943 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 944 * @retval CAN Receive Error Counter.
mbed_official 52:a51c77007319 945 */
mbed_official 52:a51c77007319 946 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 947 {
mbed_official 52:a51c77007319 948 uint8_t counter=0;
mbed_official 52:a51c77007319 949
mbed_official 52:a51c77007319 950 /* Check the parameters */
mbed_official 52:a51c77007319 951 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 952
mbed_official 52:a51c77007319 953 /* Get the Receive Error Counter*/
mbed_official 52:a51c77007319 954 counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
mbed_official 52:a51c77007319 955
mbed_official 52:a51c77007319 956 /* Return the Receive Error Counter*/
mbed_official 52:a51c77007319 957 return counter;
mbed_official 52:a51c77007319 958 }
mbed_official 52:a51c77007319 959
mbed_official 52:a51c77007319 960
mbed_official 52:a51c77007319 961 /**
mbed_official 52:a51c77007319 962 * @brief Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
mbed_official 52:a51c77007319 963 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 964 * @retval LSB of the 9-bit CAN Transmit Error Counter.
mbed_official 52:a51c77007319 965 */
mbed_official 52:a51c77007319 966 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
mbed_official 52:a51c77007319 967 {
mbed_official 52:a51c77007319 968 uint8_t counter=0;
mbed_official 52:a51c77007319 969
mbed_official 52:a51c77007319 970 /* Check the parameters */
mbed_official 52:a51c77007319 971 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 972
mbed_official 52:a51c77007319 973 /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
mbed_official 52:a51c77007319 974 counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
mbed_official 52:a51c77007319 975
mbed_official 52:a51c77007319 976 /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
mbed_official 52:a51c77007319 977 return counter;
mbed_official 52:a51c77007319 978 }
mbed_official 52:a51c77007319 979
mbed_official 52:a51c77007319 980
mbed_official 52:a51c77007319 981 /**
mbed_official 52:a51c77007319 982 * @brief Enables or disables the specified CANx interrupts.
mbed_official 52:a51c77007319 983 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 984 * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
mbed_official 52:a51c77007319 985 * This parameter can be:
mbed_official 52:a51c77007319 986 * - CAN_IT_TME,
mbed_official 52:a51c77007319 987 * - CAN_IT_FMP0,
mbed_official 52:a51c77007319 988 * - CAN_IT_FF0,
mbed_official 52:a51c77007319 989 * - CAN_IT_FOV0,
mbed_official 52:a51c77007319 990 * - CAN_IT_FMP1,
mbed_official 52:a51c77007319 991 * - CAN_IT_FF1,
mbed_official 52:a51c77007319 992 * - CAN_IT_FOV1,
mbed_official 52:a51c77007319 993 * - CAN_IT_EWG,
mbed_official 52:a51c77007319 994 * - CAN_IT_EPV,
mbed_official 52:a51c77007319 995 * - CAN_IT_LEC,
mbed_official 52:a51c77007319 996 * - CAN_IT_ERR,
mbed_official 52:a51c77007319 997 * - CAN_IT_WKU or
mbed_official 52:a51c77007319 998 * - CAN_IT_SLK.
mbed_official 52:a51c77007319 999 * @param NewState: new state of the CAN interrupts.
mbed_official 52:a51c77007319 1000 * This parameter can be: ENABLE or DISABLE.
mbed_official 52:a51c77007319 1001 * @retval None.
mbed_official 52:a51c77007319 1002 */
mbed_official 52:a51c77007319 1003 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
mbed_official 52:a51c77007319 1004 {
mbed_official 52:a51c77007319 1005 /* Check the parameters */
mbed_official 52:a51c77007319 1006 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 1007 assert_param(IS_CAN_IT(CAN_IT));
mbed_official 52:a51c77007319 1008 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 52:a51c77007319 1009
mbed_official 52:a51c77007319 1010 if (NewState != DISABLE)
mbed_official 52:a51c77007319 1011 {
mbed_official 52:a51c77007319 1012 /* Enable the selected CANx interrupt */
mbed_official 52:a51c77007319 1013 CANx->IER |= CAN_IT;
mbed_official 52:a51c77007319 1014 }
mbed_official 52:a51c77007319 1015 else
mbed_official 52:a51c77007319 1016 {
mbed_official 52:a51c77007319 1017 /* Disable the selected CANx interrupt */
mbed_official 52:a51c77007319 1018 CANx->IER &= ~CAN_IT;
mbed_official 52:a51c77007319 1019 }
mbed_official 52:a51c77007319 1020 }
mbed_official 52:a51c77007319 1021 /**
mbed_official 52:a51c77007319 1022 * @brief Checks whether the specified CAN flag is set or not.
mbed_official 52:a51c77007319 1023 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 1024 * @param CAN_FLAG: specifies the flag to check.
mbed_official 52:a51c77007319 1025 * This parameter can be one of the following flags:
mbed_official 52:a51c77007319 1026 * - CAN_FLAG_EWG
mbed_official 52:a51c77007319 1027 * - CAN_FLAG_EPV
mbed_official 52:a51c77007319 1028 * - CAN_FLAG_BOF
mbed_official 52:a51c77007319 1029 * - CAN_FLAG_RQCP0
mbed_official 52:a51c77007319 1030 * - CAN_FLAG_RQCP1
mbed_official 52:a51c77007319 1031 * - CAN_FLAG_RQCP2
mbed_official 52:a51c77007319 1032 * - CAN_FLAG_FMP1
mbed_official 52:a51c77007319 1033 * - CAN_FLAG_FF1
mbed_official 52:a51c77007319 1034 * - CAN_FLAG_FOV1
mbed_official 52:a51c77007319 1035 * - CAN_FLAG_FMP0
mbed_official 52:a51c77007319 1036 * - CAN_FLAG_FF0
mbed_official 52:a51c77007319 1037 * - CAN_FLAG_FOV0
mbed_official 52:a51c77007319 1038 * - CAN_FLAG_WKU
mbed_official 52:a51c77007319 1039 * - CAN_FLAG_SLAK
mbed_official 52:a51c77007319 1040 * - CAN_FLAG_LEC
mbed_official 52:a51c77007319 1041 * @retval The new state of CAN_FLAG (SET or RESET).
mbed_official 52:a51c77007319 1042 */
mbed_official 52:a51c77007319 1043 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
mbed_official 52:a51c77007319 1044 {
mbed_official 52:a51c77007319 1045 FlagStatus bitstatus = RESET;
mbed_official 52:a51c77007319 1046
mbed_official 52:a51c77007319 1047 /* Check the parameters */
mbed_official 52:a51c77007319 1048 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 1049 assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
mbed_official 52:a51c77007319 1050
mbed_official 52:a51c77007319 1051
mbed_official 52:a51c77007319 1052 if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1053 {
mbed_official 52:a51c77007319 1054 /* Check the status of the specified CAN flag */
mbed_official 52:a51c77007319 1055 if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1056 {
mbed_official 52:a51c77007319 1057 /* CAN_FLAG is set */
mbed_official 52:a51c77007319 1058 bitstatus = SET;
mbed_official 52:a51c77007319 1059 }
mbed_official 52:a51c77007319 1060 else
mbed_official 52:a51c77007319 1061 {
mbed_official 52:a51c77007319 1062 /* CAN_FLAG is reset */
mbed_official 52:a51c77007319 1063 bitstatus = RESET;
mbed_official 52:a51c77007319 1064 }
mbed_official 52:a51c77007319 1065 }
mbed_official 52:a51c77007319 1066 else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1067 {
mbed_official 52:a51c77007319 1068 /* Check the status of the specified CAN flag */
mbed_official 52:a51c77007319 1069 if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1070 {
mbed_official 52:a51c77007319 1071 /* CAN_FLAG is set */
mbed_official 52:a51c77007319 1072 bitstatus = SET;
mbed_official 52:a51c77007319 1073 }
mbed_official 52:a51c77007319 1074 else
mbed_official 52:a51c77007319 1075 {
mbed_official 52:a51c77007319 1076 /* CAN_FLAG is reset */
mbed_official 52:a51c77007319 1077 bitstatus = RESET;
mbed_official 52:a51c77007319 1078 }
mbed_official 52:a51c77007319 1079 }
mbed_official 52:a51c77007319 1080 else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1081 {
mbed_official 52:a51c77007319 1082 /* Check the status of the specified CAN flag */
mbed_official 52:a51c77007319 1083 if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1084 {
mbed_official 52:a51c77007319 1085 /* CAN_FLAG is set */
mbed_official 52:a51c77007319 1086 bitstatus = SET;
mbed_official 52:a51c77007319 1087 }
mbed_official 52:a51c77007319 1088 else
mbed_official 52:a51c77007319 1089 {
mbed_official 52:a51c77007319 1090 /* CAN_FLAG is reset */
mbed_official 52:a51c77007319 1091 bitstatus = RESET;
mbed_official 52:a51c77007319 1092 }
mbed_official 52:a51c77007319 1093 }
mbed_official 52:a51c77007319 1094 else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1095 {
mbed_official 52:a51c77007319 1096 /* Check the status of the specified CAN flag */
mbed_official 52:a51c77007319 1097 if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1098 {
mbed_official 52:a51c77007319 1099 /* CAN_FLAG is set */
mbed_official 52:a51c77007319 1100 bitstatus = SET;
mbed_official 52:a51c77007319 1101 }
mbed_official 52:a51c77007319 1102 else
mbed_official 52:a51c77007319 1103 {
mbed_official 52:a51c77007319 1104 /* CAN_FLAG is reset */
mbed_official 52:a51c77007319 1105 bitstatus = RESET;
mbed_official 52:a51c77007319 1106 }
mbed_official 52:a51c77007319 1107 }
mbed_official 52:a51c77007319 1108 else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
mbed_official 52:a51c77007319 1109 {
mbed_official 52:a51c77007319 1110 /* Check the status of the specified CAN flag */
mbed_official 52:a51c77007319 1111 if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1112 {
mbed_official 52:a51c77007319 1113 /* CAN_FLAG is set */
mbed_official 52:a51c77007319 1114 bitstatus = SET;
mbed_official 52:a51c77007319 1115 }
mbed_official 52:a51c77007319 1116 else
mbed_official 52:a51c77007319 1117 {
mbed_official 52:a51c77007319 1118 /* CAN_FLAG is reset */
mbed_official 52:a51c77007319 1119 bitstatus = RESET;
mbed_official 52:a51c77007319 1120 }
mbed_official 52:a51c77007319 1121 }
mbed_official 52:a51c77007319 1122 /* Return the CAN_FLAG status */
mbed_official 52:a51c77007319 1123 return bitstatus;
mbed_official 52:a51c77007319 1124 }
mbed_official 52:a51c77007319 1125
mbed_official 52:a51c77007319 1126 /**
mbed_official 52:a51c77007319 1127 * @brief Clears the CAN's pending flags.
mbed_official 52:a51c77007319 1128 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 1129 * @param CAN_FLAG: specifies the flag to clear.
mbed_official 52:a51c77007319 1130 * This parameter can be one of the following flags:
mbed_official 52:a51c77007319 1131 * - CAN_FLAG_RQCP0
mbed_official 52:a51c77007319 1132 * - CAN_FLAG_RQCP1
mbed_official 52:a51c77007319 1133 * - CAN_FLAG_RQCP2
mbed_official 52:a51c77007319 1134 * - CAN_FLAG_FF1
mbed_official 52:a51c77007319 1135 * - CAN_FLAG_FOV1
mbed_official 52:a51c77007319 1136 * - CAN_FLAG_FF0
mbed_official 52:a51c77007319 1137 * - CAN_FLAG_FOV0
mbed_official 52:a51c77007319 1138 * - CAN_FLAG_WKU
mbed_official 52:a51c77007319 1139 * - CAN_FLAG_SLAK
mbed_official 52:a51c77007319 1140 * - CAN_FLAG_LEC
mbed_official 52:a51c77007319 1141 * @retval None.
mbed_official 52:a51c77007319 1142 */
mbed_official 52:a51c77007319 1143 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
mbed_official 52:a51c77007319 1144 {
mbed_official 52:a51c77007319 1145 uint32_t flagtmp=0;
mbed_official 52:a51c77007319 1146 /* Check the parameters */
mbed_official 52:a51c77007319 1147 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 1148 assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
mbed_official 52:a51c77007319 1149
mbed_official 52:a51c77007319 1150 if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
mbed_official 52:a51c77007319 1151 {
mbed_official 52:a51c77007319 1152 /* Clear the selected CAN flags */
mbed_official 52:a51c77007319 1153 CANx->ESR = (uint32_t)RESET;
mbed_official 52:a51c77007319 1154 }
mbed_official 52:a51c77007319 1155 else /* MSR or TSR or RF0R or RF1R */
mbed_official 52:a51c77007319 1156 {
mbed_official 52:a51c77007319 1157 flagtmp = CAN_FLAG & 0x000FFFFF;
mbed_official 52:a51c77007319 1158
mbed_official 52:a51c77007319 1159 if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
mbed_official 52:a51c77007319 1160 {
mbed_official 52:a51c77007319 1161 /* Receive Flags */
mbed_official 52:a51c77007319 1162 CANx->RF0R = (uint32_t)(flagtmp);
mbed_official 52:a51c77007319 1163 }
mbed_official 52:a51c77007319 1164 else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
mbed_official 52:a51c77007319 1165 {
mbed_official 52:a51c77007319 1166 /* Receive Flags */
mbed_official 52:a51c77007319 1167 CANx->RF1R = (uint32_t)(flagtmp);
mbed_official 52:a51c77007319 1168 }
mbed_official 52:a51c77007319 1169 else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
mbed_official 52:a51c77007319 1170 {
mbed_official 52:a51c77007319 1171 /* Transmit Flags */
mbed_official 52:a51c77007319 1172 CANx->TSR = (uint32_t)(flagtmp);
mbed_official 52:a51c77007319 1173 }
mbed_official 52:a51c77007319 1174 else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
mbed_official 52:a51c77007319 1175 {
mbed_official 52:a51c77007319 1176 /* Operating mode Flags */
mbed_official 52:a51c77007319 1177 CANx->MSR = (uint32_t)(flagtmp);
mbed_official 52:a51c77007319 1178 }
mbed_official 52:a51c77007319 1179 }
mbed_official 52:a51c77007319 1180 }
mbed_official 52:a51c77007319 1181
mbed_official 52:a51c77007319 1182 /**
mbed_official 52:a51c77007319 1183 * @brief Checks whether the specified CANx interrupt has occurred or not.
mbed_official 52:a51c77007319 1184 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 1185 * @param CAN_IT: specifies the CAN interrupt source to check.
mbed_official 52:a51c77007319 1186 * This parameter can be one of the following flags:
mbed_official 52:a51c77007319 1187 * - CAN_IT_TME
mbed_official 52:a51c77007319 1188 * - CAN_IT_FMP0
mbed_official 52:a51c77007319 1189 * - CAN_IT_FF0
mbed_official 52:a51c77007319 1190 * - CAN_IT_FOV0
mbed_official 52:a51c77007319 1191 * - CAN_IT_FMP1
mbed_official 52:a51c77007319 1192 * - CAN_IT_FF1
mbed_official 52:a51c77007319 1193 * - CAN_IT_FOV1
mbed_official 52:a51c77007319 1194 * - CAN_IT_WKU
mbed_official 52:a51c77007319 1195 * - CAN_IT_SLK
mbed_official 52:a51c77007319 1196 * - CAN_IT_EWG
mbed_official 52:a51c77007319 1197 * - CAN_IT_EPV
mbed_official 52:a51c77007319 1198 * - CAN_IT_BOF
mbed_official 52:a51c77007319 1199 * - CAN_IT_LEC
mbed_official 52:a51c77007319 1200 * - CAN_IT_ERR
mbed_official 52:a51c77007319 1201 * @retval The current state of CAN_IT (SET or RESET).
mbed_official 52:a51c77007319 1202 */
mbed_official 52:a51c77007319 1203 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
mbed_official 52:a51c77007319 1204 {
mbed_official 52:a51c77007319 1205 ITStatus itstatus = RESET;
mbed_official 52:a51c77007319 1206 /* Check the parameters */
mbed_official 52:a51c77007319 1207 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 1208 assert_param(IS_CAN_IT(CAN_IT));
mbed_official 52:a51c77007319 1209
mbed_official 52:a51c77007319 1210 /* check the enable interrupt bit */
mbed_official 52:a51c77007319 1211 if((CANx->IER & CAN_IT) != RESET)
mbed_official 52:a51c77007319 1212 {
mbed_official 52:a51c77007319 1213 /* in case the Interrupt is enabled, .... */
mbed_official 52:a51c77007319 1214 switch (CAN_IT)
mbed_official 52:a51c77007319 1215 {
mbed_official 52:a51c77007319 1216 case CAN_IT_TME:
mbed_official 52:a51c77007319 1217 /* Check CAN_TSR_RQCPx bits */
mbed_official 52:a51c77007319 1218 itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);
mbed_official 52:a51c77007319 1219 break;
mbed_official 52:a51c77007319 1220 case CAN_IT_FMP0:
mbed_official 52:a51c77007319 1221 /* Check CAN_RF0R_FMP0 bit */
mbed_official 52:a51c77007319 1222 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);
mbed_official 52:a51c77007319 1223 break;
mbed_official 52:a51c77007319 1224 case CAN_IT_FF0:
mbed_official 52:a51c77007319 1225 /* Check CAN_RF0R_FULL0 bit */
mbed_official 52:a51c77007319 1226 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);
mbed_official 52:a51c77007319 1227 break;
mbed_official 52:a51c77007319 1228 case CAN_IT_FOV0:
mbed_official 52:a51c77007319 1229 /* Check CAN_RF0R_FOVR0 bit */
mbed_official 52:a51c77007319 1230 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);
mbed_official 52:a51c77007319 1231 break;
mbed_official 52:a51c77007319 1232 case CAN_IT_FMP1:
mbed_official 52:a51c77007319 1233 /* Check CAN_RF1R_FMP1 bit */
mbed_official 52:a51c77007319 1234 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);
mbed_official 52:a51c77007319 1235 break;
mbed_official 52:a51c77007319 1236 case CAN_IT_FF1:
mbed_official 52:a51c77007319 1237 /* Check CAN_RF1R_FULL1 bit */
mbed_official 52:a51c77007319 1238 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);
mbed_official 52:a51c77007319 1239 break;
mbed_official 52:a51c77007319 1240 case CAN_IT_FOV1:
mbed_official 52:a51c77007319 1241 /* Check CAN_RF1R_FOVR1 bit */
mbed_official 52:a51c77007319 1242 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);
mbed_official 52:a51c77007319 1243 break;
mbed_official 52:a51c77007319 1244 case CAN_IT_WKU:
mbed_official 52:a51c77007319 1245 /* Check CAN_MSR_WKUI bit */
mbed_official 52:a51c77007319 1246 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);
mbed_official 52:a51c77007319 1247 break;
mbed_official 52:a51c77007319 1248 case CAN_IT_SLK:
mbed_official 52:a51c77007319 1249 /* Check CAN_MSR_SLAKI bit */
mbed_official 52:a51c77007319 1250 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);
mbed_official 52:a51c77007319 1251 break;
mbed_official 52:a51c77007319 1252 case CAN_IT_EWG:
mbed_official 52:a51c77007319 1253 /* Check CAN_ESR_EWGF bit */
mbed_official 52:a51c77007319 1254 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);
mbed_official 52:a51c77007319 1255 break;
mbed_official 52:a51c77007319 1256 case CAN_IT_EPV:
mbed_official 52:a51c77007319 1257 /* Check CAN_ESR_EPVF bit */
mbed_official 52:a51c77007319 1258 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);
mbed_official 52:a51c77007319 1259 break;
mbed_official 52:a51c77007319 1260 case CAN_IT_BOF:
mbed_official 52:a51c77007319 1261 /* Check CAN_ESR_BOFF bit */
mbed_official 52:a51c77007319 1262 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);
mbed_official 52:a51c77007319 1263 break;
mbed_official 52:a51c77007319 1264 case CAN_IT_LEC:
mbed_official 52:a51c77007319 1265 /* Check CAN_ESR_LEC bit */
mbed_official 52:a51c77007319 1266 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);
mbed_official 52:a51c77007319 1267 break;
mbed_official 52:a51c77007319 1268 case CAN_IT_ERR:
mbed_official 52:a51c77007319 1269 /* Check CAN_MSR_ERRI bit */
mbed_official 52:a51c77007319 1270 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI);
mbed_official 52:a51c77007319 1271 break;
mbed_official 52:a51c77007319 1272 default :
mbed_official 52:a51c77007319 1273 /* in case of error, return RESET */
mbed_official 52:a51c77007319 1274 itstatus = RESET;
mbed_official 52:a51c77007319 1275 break;
mbed_official 52:a51c77007319 1276 }
mbed_official 52:a51c77007319 1277 }
mbed_official 52:a51c77007319 1278 else
mbed_official 52:a51c77007319 1279 {
mbed_official 52:a51c77007319 1280 /* in case the Interrupt is not enabled, return RESET */
mbed_official 52:a51c77007319 1281 itstatus = RESET;
mbed_official 52:a51c77007319 1282 }
mbed_official 52:a51c77007319 1283
mbed_official 52:a51c77007319 1284 /* Return the CAN_IT status */
mbed_official 52:a51c77007319 1285 return itstatus;
mbed_official 52:a51c77007319 1286 }
mbed_official 52:a51c77007319 1287
mbed_official 52:a51c77007319 1288 /**
mbed_official 52:a51c77007319 1289 * @brief Clears the CANx's interrupt pending bits.
mbed_official 52:a51c77007319 1290 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
mbed_official 52:a51c77007319 1291 * @param CAN_IT: specifies the interrupt pending bit to clear.
mbed_official 52:a51c77007319 1292 * - CAN_IT_TME
mbed_official 52:a51c77007319 1293 * - CAN_IT_FF0
mbed_official 52:a51c77007319 1294 * - CAN_IT_FOV0
mbed_official 52:a51c77007319 1295 * - CAN_IT_FF1
mbed_official 52:a51c77007319 1296 * - CAN_IT_FOV1
mbed_official 52:a51c77007319 1297 * - CAN_IT_WKU
mbed_official 52:a51c77007319 1298 * - CAN_IT_SLK
mbed_official 52:a51c77007319 1299 * - CAN_IT_EWG
mbed_official 52:a51c77007319 1300 * - CAN_IT_EPV
mbed_official 52:a51c77007319 1301 * - CAN_IT_BOF
mbed_official 52:a51c77007319 1302 * - CAN_IT_LEC
mbed_official 52:a51c77007319 1303 * - CAN_IT_ERR
mbed_official 52:a51c77007319 1304 * @retval None.
mbed_official 52:a51c77007319 1305 */
mbed_official 52:a51c77007319 1306 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
mbed_official 52:a51c77007319 1307 {
mbed_official 52:a51c77007319 1308 /* Check the parameters */
mbed_official 52:a51c77007319 1309 assert_param(IS_CAN_ALL_PERIPH(CANx));
mbed_official 52:a51c77007319 1310 assert_param(IS_CAN_CLEAR_IT(CAN_IT));
mbed_official 52:a51c77007319 1311
mbed_official 52:a51c77007319 1312 switch (CAN_IT)
mbed_official 52:a51c77007319 1313 {
mbed_official 52:a51c77007319 1314 case CAN_IT_TME:
mbed_official 52:a51c77007319 1315 /* Clear CAN_TSR_RQCPx (rc_w1)*/
mbed_official 52:a51c77007319 1316 CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;
mbed_official 52:a51c77007319 1317 break;
mbed_official 52:a51c77007319 1318 case CAN_IT_FF0:
mbed_official 52:a51c77007319 1319 /* Clear CAN_RF0R_FULL0 (rc_w1)*/
mbed_official 52:a51c77007319 1320 CANx->RF0R = CAN_RF0R_FULL0;
mbed_official 52:a51c77007319 1321 break;
mbed_official 52:a51c77007319 1322 case CAN_IT_FOV0:
mbed_official 52:a51c77007319 1323 /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
mbed_official 52:a51c77007319 1324 CANx->RF0R = CAN_RF0R_FOVR0;
mbed_official 52:a51c77007319 1325 break;
mbed_official 52:a51c77007319 1326 case CAN_IT_FF1:
mbed_official 52:a51c77007319 1327 /* Clear CAN_RF1R_FULL1 (rc_w1)*/
mbed_official 52:a51c77007319 1328 CANx->RF1R = CAN_RF1R_FULL1;
mbed_official 52:a51c77007319 1329 break;
mbed_official 52:a51c77007319 1330 case CAN_IT_FOV1:
mbed_official 52:a51c77007319 1331 /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
mbed_official 52:a51c77007319 1332 CANx->RF1R = CAN_RF1R_FOVR1;
mbed_official 52:a51c77007319 1333 break;
mbed_official 52:a51c77007319 1334 case CAN_IT_WKU:
mbed_official 52:a51c77007319 1335 /* Clear CAN_MSR_WKUI (rc_w1)*/
mbed_official 52:a51c77007319 1336 CANx->MSR = CAN_MSR_WKUI;
mbed_official 52:a51c77007319 1337 break;
mbed_official 52:a51c77007319 1338 case CAN_IT_SLK:
mbed_official 52:a51c77007319 1339 /* Clear CAN_MSR_SLAKI (rc_w1)*/
mbed_official 52:a51c77007319 1340 CANx->MSR = CAN_MSR_SLAKI;
mbed_official 52:a51c77007319 1341 break;
mbed_official 52:a51c77007319 1342 case CAN_IT_EWG:
mbed_official 52:a51c77007319 1343 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 52:a51c77007319 1344 CANx->MSR = CAN_MSR_ERRI;
mbed_official 52:a51c77007319 1345 /* Note : the corresponding Flag is cleared by hardware depending
mbed_official 52:a51c77007319 1346 of the CAN Bus status*/
mbed_official 52:a51c77007319 1347 break;
mbed_official 52:a51c77007319 1348 case CAN_IT_EPV:
mbed_official 52:a51c77007319 1349 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 52:a51c77007319 1350 CANx->MSR = CAN_MSR_ERRI;
mbed_official 52:a51c77007319 1351 /* Note : the corresponding Flag is cleared by hardware depending
mbed_official 52:a51c77007319 1352 of the CAN Bus status*/
mbed_official 52:a51c77007319 1353 break;
mbed_official 52:a51c77007319 1354 case CAN_IT_BOF:
mbed_official 52:a51c77007319 1355 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 52:a51c77007319 1356 CANx->MSR = CAN_MSR_ERRI;
mbed_official 52:a51c77007319 1357 /* Note : the corresponding Flag is cleared by hardware depending
mbed_official 52:a51c77007319 1358 of the CAN Bus status*/
mbed_official 52:a51c77007319 1359 break;
mbed_official 52:a51c77007319 1360 case CAN_IT_LEC:
mbed_official 52:a51c77007319 1361 /* Clear LEC bits */
mbed_official 52:a51c77007319 1362 CANx->ESR = RESET;
mbed_official 52:a51c77007319 1363 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 52:a51c77007319 1364 CANx->MSR = CAN_MSR_ERRI;
mbed_official 52:a51c77007319 1365 break;
mbed_official 52:a51c77007319 1366 case CAN_IT_ERR:
mbed_official 52:a51c77007319 1367 /*Clear LEC bits */
mbed_official 52:a51c77007319 1368 CANx->ESR = RESET;
mbed_official 52:a51c77007319 1369 /* Clear CAN_MSR_ERRI (rc_w1) */
mbed_official 52:a51c77007319 1370 CANx->MSR = CAN_MSR_ERRI;
mbed_official 52:a51c77007319 1371 /* Note : BOFF, EPVF and EWGF Flags are cleared by hardware depending
mbed_official 52:a51c77007319 1372 of the CAN Bus status*/
mbed_official 52:a51c77007319 1373 break;
mbed_official 52:a51c77007319 1374 default :
mbed_official 52:a51c77007319 1375 break;
mbed_official 52:a51c77007319 1376 }
mbed_official 52:a51c77007319 1377 }
mbed_official 52:a51c77007319 1378
mbed_official 52:a51c77007319 1379 /**
mbed_official 52:a51c77007319 1380 * @brief Checks whether the CAN interrupt has occurred or not.
mbed_official 52:a51c77007319 1381 * @param CAN_Reg: specifies the CAN interrupt register to check.
mbed_official 52:a51c77007319 1382 * @param It_Bit: specifies the interrupt source bit to check.
mbed_official 52:a51c77007319 1383 * @retval The new state of the CAN Interrupt (SET or RESET).
mbed_official 52:a51c77007319 1384 */
mbed_official 52:a51c77007319 1385 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
mbed_official 52:a51c77007319 1386 {
mbed_official 52:a51c77007319 1387 ITStatus pendingbitstatus = RESET;
mbed_official 52:a51c77007319 1388
mbed_official 52:a51c77007319 1389 if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
mbed_official 52:a51c77007319 1390 {
mbed_official 52:a51c77007319 1391 /* CAN_IT is set */
mbed_official 52:a51c77007319 1392 pendingbitstatus = SET;
mbed_official 52:a51c77007319 1393 }
mbed_official 52:a51c77007319 1394 else
mbed_official 52:a51c77007319 1395 {
mbed_official 52:a51c77007319 1396 /* CAN_IT is reset */
mbed_official 52:a51c77007319 1397 pendingbitstatus = RESET;
mbed_official 52:a51c77007319 1398 }
mbed_official 52:a51c77007319 1399 return pendingbitstatus;
mbed_official 52:a51c77007319 1400 }
mbed_official 52:a51c77007319 1401
mbed_official 52:a51c77007319 1402
mbed_official 52:a51c77007319 1403 /**
mbed_official 52:a51c77007319 1404 * @}
mbed_official 52:a51c77007319 1405 */
mbed_official 52:a51c77007319 1406
mbed_official 52:a51c77007319 1407 /**
mbed_official 52:a51c77007319 1408 * @}
mbed_official 52:a51c77007319 1409 */
mbed_official 52:a51c77007319 1410
mbed_official 52:a51c77007319 1411 /**
mbed_official 52:a51c77007319 1412 * @}
mbed_official 52:a51c77007319 1413 */
mbed_official 52:a51c77007319 1414
mbed_official 52:a51c77007319 1415 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/