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:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
52:a51c77007319
Child:
84:f54042cbc282
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

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