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