CARME-M4 BSP  V1.5
can.h
Go to the documentation of this file.
1 #ifndef __CAN_H__
2 #define __CAN_H__
3 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif /* __cplusplus */
76 
77 /*----- Header-Files -------------------------------------------------------*/
78 #include <stm32f4xx.h> /* Processor STM32F407IG */
79 #include <carme.h> /* CARME Module */
80 #include <can_sja1000.h> /* CAN Controller SJA1000 defines */
81 
82 /*----- Macros -------------------------------------------------------------*/
83 #define CARME_CAN_nCAN_INT_PORT GPIOI
84 #define CARME_CAN_nCAN_INT_PIN GPIO_Pin_8
85 #define CARME_CAN_nCAN_IRQn_CH EXTI9_5_IRQn
87 /* baudrates */
88 #define CARME_CAN_BAUD_125K 125000
89 #define CARME_CAN_BAUD_250K 250000
90 #define CARME_CAN_BAUD_500K 500000
91 #define CARME_CAN_BAUD_1M 1000000
93 /* error codes */
94 #define CARME_ERROR_CAN CARME_ERROR_CAN_BASE + 0
95 #define CARME_ERROR_CAN_INVALID_BAUDRATE CARME_ERROR_CAN_BASE + 1
96 #define CARME_ERROR_CAN_RXFIFO_EMPTY CARME_ERROR_CAN_BASE + 2
97 #define CARME_ERROR_CAN_INVALID_MODE CARME_ERROR_CAN_BASE + 3
98 #define CARME_ERROR_CAN_INVALID_OPMODE CARME_ERROR_CAN_BASE + 4
99 #define CARME_ERROR_CAN_INVALID_ACCEPTANCE_MODE CARME_ERROR_CAN_BASE + 5
100 #define CARME_ERROR_CAN_ERROR_STATUS CARME_ERROR_CAN_BASE + 6
102 /* interrupt enable flags */
103 #define CARME_CAN_INT_BUSERR (SJA1000_IER_BEIE)
104 #define CARME_CAN_INT_ARBIT_LOST (SJA1000_IER_ALIE)
105 #define CARME_CAN_INT_PASSIVE (SJA1000_IER_EPIE)
106 #define CARME_CAN_INT_WAKEUP (SJA1000_IER_WUIE)
107 #define CARME_CAN_INT_OVERRUN (SJA1000_IER_DOIE)
108 #define CARME_CAN_INT_ERROR (SJA1000_IER_EIE)
109 #define CARME_CAN_INT_TX (SJA1000_IER_TIE)
110 #define CARME_CAN_INT_RX (SJA1000_IER_RIE)
112 /* driver flags */
113 #define CARME_CAN_DF_RESET 0x00
114 #define CARME_CAN_DF_NORMAL 0x01
115 #define CARME_CAN_DF_LISTEN_ONLY 0x02
116 //#define CARME_CAN_DF_SLEEP 0x04
117 
118 /*----- Data types ---------------------------------------------------------*/
133 };
134 
138 typedef struct _CARME_CAN_MESSAGE {
139  uint32_t id;
140  uint8_t ext;
143  uint8_t rtr;
144  uint8_t dlc;
145  uint8_t data[8];
147 
157 };
158 
164  uint8_t acr[4];
165  uint8_t amr[4];
168 
172 typedef void (*IRQ_CALLBACK)();
173 
174 /*----- Function prototypes ------------------------------------------------*/
175 extern void CARME_CAN_Init(uint32_t baud, uint8_t flags);
176 extern void CARME_CAN_InitI(uint32_t baud, uint8_t flags, uint32_t interrupts);
179 
180 extern void CARME_CAN_Interrupt_Handler(void);
182  IRQ_CALLBACK pIRQCallback);
184 
185 extern ERROR_CODES CARME_CAN_SetMode(uint8_t flags);
186 extern ERROR_CODES CARME_CAN_SetBaudrate(uint32_t baud);
191 #ifdef CARME_CAN_DEBUG
192 /* debug functions */
193 extern void CARME_CAN_PrintRegisters();
194 extern void CARME_CAN_GetRegisterString(char* pStr);
195 #endif
196 
197 /*----- Data ---------------------------------------------------------------*/
198 static __IO uint8_t *SJA1000_CMD = (__IO uint8_t *) (FSMC_CAN_BASE);
199 static __IO uint8_t *SJA1000_DATA = (__IO uint8_t *) (FSMC_CAN_BASE + 8);
200 
201 /*----- Implementation -----------------------------------------------------*/
207 static void CARME_CAN_Write_Register(uint8_t registerAddress, uint8_t val) {
208  *SJA1000_CMD = registerAddress; /* write address */
209  *SJA1000_DATA = val; /* write data */
210 }
211 
217 static uint8_t CARME_CAN_Read_Register(uint8_t registerAddress) {
218  *SJA1000_CMD = registerAddress; /* write address */
219  return *SJA1000_DATA; /* read data */
220 }
221 
229 static inline uint8_t CARME_CAN_IsBusOn(void) {
231 }
232 
239 static inline uint8_t CARME_CAN_IsDataOverrun(void) {
241  == SJA1000_SR_DOS;
242 }
243 
251 static inline uint8_t CARME_CAN_IsError(void) {
253  == SJA1000_SR_ES;
254 }
255 
259 static inline void CARME_CAN_ClearDataOverrun(void) {
261 }
262 
266 static inline void CARME_CAN_AbortTransmisssion(void) {
267 
269 }
270 
277 static inline ERROR_CODES CARME_CAN_SetErrorWarningLimit(uint8_t limit) {
279 
282  }
284 
285  return err;
286 }
287 
293 static inline void CARME_CAN_GetErrorWarningLimit(uint8_t* limit) {
295 }
296 
302 static inline void CARME_CAN_GetRxErrCount(uint8_t* count) {
304 }
305 
311 static inline void CARME_CAN_GetTxErrCount(uint8_t* count) {
312 
314 }
315 
321 static inline void CARME_CAN_GetArbitrationLostCapture(uint8_t* alc) {
323 }
324 
330 static inline void CARME_CAN_GetErrorCodeCapture(uint8_t* ecc) {
331 
333 }
334 
335 #ifdef __cplusplus
336 }
337 #endif /* __cplusplus */
338 
344 #endif /* __CAN_H__ */
uint8_t ERROR_CODES
Error variable.
Definition: carme.h:255
Information about the acceptance filter.
Definition: can.h:163
#define SJA1000_RXERR
Definition: can_sja1000.h:99
static ERROR_CODES CARME_CAN_SetErrorWarningLimit(uint8_t limit)
Set the value of the Error Warning Limit Register. The content can only be changed in reset mode...
Definition: can.h:277
#define SJA1000_SR_DOS
Definition: can_sja1000.h:149
void CARME_CAN_Init(uint32_t baud, uint8_t flags)
Initialize GPIOs and the CAN-Controller.
Definition: can.c:166
#define SJA1000_SR_BS
Definition: can_sja1000.h:143
uint8_t data[8]
Definition: can.h:145
static uint8_t CARME_CAN_IsError(void)
Returns 1 if at least one of the error counters has reached or exceeded the value in the Error Warnin...
Definition: can.h:251
static void CARME_CAN_GetArbitrationLostCapture(uint8_t *alc)
Get the value of the TX Error Count Register.
Definition: can.h:321
static uint8_t CARME_CAN_Read_Register(uint8_t registerAddress)
Reads a value from a SJA1000 register.
Definition: can.h:217
struct _CARME_CAN_MESSAGE CARME_CAN_MESSAGE
This struct encapsulates a CAN message.
struct _CARME_CAN_ACCEPTANCE_FILTER CARME_CAN_ACCEPTANCE_FILTER
Information about the acceptance filter.
static void CARME_CAN_GetRxErrCount(uint8_t *count)
Get the value of the RX Error Count Register.
Definition: can.h:302
#define FSMC_CAN_BASE
Definition: carme.h:88
static void CARME_CAN_GetErrorCodeCapture(uint8_t *ecc)
Get the value of the TX Error Count Register.
Definition: can.h:330
uint8_t ext
Definition: can.h:140
#define SJA1000_TXERR
Definition: can_sja1000.h:100
enum CARME_CAN_ACCEPTANCE_FILTER_MODE afm
Definition: can.h:166
ERROR_CODES CARME_CAN_SetAcceptaceFilter(CARME_CAN_ACCEPTANCE_FILTER *af)
ACCEPTANCE FILTER With the help of the acceptance filter the CAN controller is able to allow passing ...
Definition: can.c:657
static uint8_t CARME_CAN_IsBusOn(void)
Returns 1 if the CAN Controller is involved in bus activities The Function checks the BS-bit in the ...
Definition: can.h:229
#define SJA1000_CMR
Definition: can_sja1000.h:86
uint32_t id
Definition: can.h:139
#define SJA1000_ECC
Definition: can_sja1000.h:97
CARME_CAN_IRQ_CALLBACKS
Interrupt sources of the CAN-Controller.
Definition: can.h:122
#define SJA1000_SR
Definition: can_sja1000.h:87
void CARME_CAN_RegisterIRQCallback(enum CARME_CAN_IRQ_CALLBACKS id, IRQ_CALLBACK pIRQCallback)
Registers a callback function. Callback functions are called in Interruptmode. Use this function afte...
Definition: can.c:258
SJA1000 specific declarations.
#define SJA1000_EWL
Definition: can_sja1000.h:98
CARME_CAN_ACCEPTANCE_FILTER_MODE
Acceptance filter modes.
Definition: can.h:152
void CARME_CAN_InitI(uint32_t baud, uint8_t flags, uint32_t interrupts)
Initialize GPIOs and the CAN-Controller.
Definition: can.c:236
#define SJA1000_CMR_AT
Definition: can_sja1000.h:122
static void CARME_CAN_Write_Register(uint8_t registerAddress, uint8_t val)
Write a value in a SJA1000 register.
Definition: can.h:207
static void CARME_CAN_ClearDataOverrun(void)
Clear Data-Overrun-Bit in the status register of the SJA1000.
Definition: can.h:259
#define CARME_NO_ERROR
Definition: carme.h:135
#define SJA1000_ALC
Definition: can_sja1000.h:96
static void CARME_CAN_AbortTransmisssion(void)
Cancel a pending transmission request.
Definition: can.h:266
ERROR_CODES CARME_CAN_Write(CARME_CAN_MESSAGE *txMsg)
Sends a CAN-Messsage over the CAN-Bus.
Definition: can.c:501
static uint8_t CARME_CAN_IsDataOverrun(void)
Returns 1 if an data overrun occurred The Function checks the DOS-bit in the status register of the S...
Definition: can.h:239
#define SJA1000_MOD
Definition: can_sja1000.h:85
void CARME_CAN_UnregisterIRQCallback(enum CARME_CAN_IRQ_CALLBACKS id)
Unregisters a callback function. Callback functions are called in Interruptmode.
Definition: can.c:273
Definition: can.h:155
ERROR_CODES CARME_CAN_GetAcceptaceFilter(CARME_CAN_ACCEPTANCE_FILTER *af)
Get the Acceptance Filter settings.
Definition: can.c:700
uint8_t dlc
Definition: can.h:144
uint8_t rtr
Definition: can.h:143
ERROR_CODES CARME_CAN_Read(CARME_CAN_MESSAGE *rxMsg)
Reads a CAN-Message from the Receive-Buffer. If no message is in the buffer, the function returns CAR...
Definition: can.c:324
ERROR_CODES CARME_CAN_SetBaudrate(uint32_t baud)
Set the baudrate for the CAN-Communication.
Definition: can.c:469
#define SJA1000_MOD_RM
Definition: can_sja1000.h:117
#define SJA1000_CMR_CDO
Definition: can_sja1000.h:120
#define CARME_ERROR_CAN_INVALID_OPMODE
Definition: can.h:98
#define SJA1000_SR_ES
Definition: can_sja1000.h:144
This struct encapsulates a CAN message.
Definition: can.h:138
static void CARME_CAN_GetErrorWarningLimit(uint8_t *limit)
Get the value of the Error Warning Limit Register.
Definition: can.h:293
void(* IRQ_CALLBACK)()
Pointer to a function.
Definition: can.h:172
void CARME_CAN_Interrupt_Handler(void)
This function will be called from the Interrupt-Handler on a CAN-Interrupt. If it was not the EXTI L...
Definition: can.c:391
static void CARME_CAN_GetTxErrCount(uint8_t *count)
Get the value of the TX Error Count Register.
Definition: can.h:311
ERROR_CODES CARME_CAN_SetMode(uint8_t flags)
Change operating mode from the CAN-Controller.
Definition: can.c:292