Martin Johnson / STM32F3-Discovery

Dependents:   Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more

Committer:
MartinJohnson
Date:
Mon May 09 04:00:25 2016 +0000
Revision:
0:404f5a4f1385
Initial library for STM32F3 discovery board

Who changed what in which revision?

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