uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_can.h
Go to the documentation of this file.
1 /***********************************************************************/
21 /* Peripheral group ----------------------------------------------------------- */
27 #ifndef LPC17XX_CAN_H_
28 #define LPC17XX_CAN_H_
29 
30 /* Includes ------------------------------------------------------------------- */
31 #include "LPC17xx.h"
32 #include "lpc_types.h"
33 
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38 
39 /* Public Types --------------------------------------------------------------- */
43 #define MSG_ENABLE ((uint8_t)(0))
44 #define MSG_DISABLE ((uint8_t)(1))
45 #define CAN1_CTRL ((uint8_t)(0))
46 #define CAN2_CTRL ((uint8_t)(1))
47 #define PARAM_FULLCAN_IC(n) ((n==FULLCAN_IC0)||(n==FULLCAN_IC1))
48 #define ID_11 1
49 #define MAX_HW_FULLCAN_OBJ 64
50 #define MAX_SW_FULLCAN_OBJ 32
51 
56 /* Private Macros ------------------------------------------------------------- */
61 /* --------------------- BIT DEFINITIONS -------------------------------------- */
62 /*********************************************************************/
66 #define CAN_MOD_RM ((uint32_t)(1))
67 
68 #define CAN_MOD_LOM ((uint32_t)(1<<1))
69 
70 #define CAN_MOD_STM ((uint32_t)(1<<2))
71 
72 #define CAN_MOD_TPM ((uint32_t)(1<<3))
73 
74 #define CAN_MOD_SM ((uint32_t)(1<<4))
75 
76 #define CAN_MOD_RPM ((uint32_t)(1<<5))
77 
78 #define CAN_MOD_TM ((uint32_t)(1<<7))
79 
80 /*********************************************************************/
84 #define CAN_CMR_TR ((uint32_t)(1))
85 
86 #define CAN_CMR_AT ((uint32_t)(1<<1))
87 
88 #define CAN_CMR_RRB ((uint32_t)(1<<2))
89 
90 #define CAN_CMR_CDO ((uint32_t)(1<<3))
91 
92 #define CAN_CMR_SRR ((uint32_t)(1<<4))
93 
94 #define CAN_CMR_STB1 ((uint32_t)(1<<5))
95 
96 #define CAN_CMR_STB2 ((uint32_t)(1<<6))
97 
98 #define CAN_CMR_STB3 ((uint32_t)(1<<7))
99 
100 /*********************************************************************/
104 #define CAN_GSR_RBS ((uint32_t)(1))
105 
106 #define CAN_GSR_DOS ((uint32_t)(1<<1))
107 
108 #define CAN_GSR_TBS ((uint32_t)(1<<2))
109 
110 #define CAN_GSR_TCS ((uint32_t)(1<<3))
111 
112 #define CAN_GSR_RS ((uint32_t)(1<<4))
113 
114 #define CAN_GSR_TS ((uint32_t)(1<<5))
115 
116 #define CAN_GSR_ES ((uint32_t)(1<<6))
117 
118 #define CAN_GSR_BS ((uint32_t)(1<<7))
119 
120 #define CAN_GSR_RXERR(n) ((uint32_t)((n&0xFF)<<16))
121 
122 #define CAN_GSR_TXERR(n) ((uint32_t)(n&0xFF)<<24))
123 
124 /*********************************************************************/
128 #define CAN_ICR_RI ((uint32_t)(1))
129 
130 #define CAN_ICR_TI1 ((uint32_t)(1<<1))
131 
132 #define CAN_ICR_EI ((uint32_t)(1<<2))
133 
134 #define CAN_ICR_DOI ((uint32_t)(1<<3))
135 
136 #define CAN_ICR_WUI ((uint32_t)(1<<4))
137 
138 #define CAN_ICR_EPI ((uint32_t)(1<<5))
139 
140 #define CAN_ICR_ALI ((uint32_t)(1<<6))
141 
142 #define CAN_ICR_BEI ((uint32_t)(1<<7))
143 
144 #define CAN_ICR_IDI ((uint32_t)(1<<8))
145 
146 #define CAN_ICR_TI2 ((uint32_t)(1<<9))
147 
148 #define CAN_ICR_TI3 ((uint32_t)(1<<10))
149 
150 #define CAN_ICR_ERRBIT(n) ((uint32_t)((n&0x1F)<<16))
151 
152 #define CAN_ICR_ERRDIR ((uint32_t)(1<<21))
153 
154 #define CAN_ICR_ERRC(n) ((uint32_t)((n&0x3)<<22))
155 
156 #define CAN_ICR_ALCBIT(n) ((uint32_t)((n&0xFF)<<24))
157 
158 /*********************************************************************/
162 #define CAN_IER_RIE ((uint32_t)(1))
163 
164 #define CAN_IER_TIE1 ((uint32_t)(1<<1))
165 
166 #define CAN_IER_EIE ((uint32_t)(1<<2))
167 
168 #define CAN_IER_DOIE ((uint32_t)(1<<3))
169 
170 #define CAN_IER_WUIE ((uint32_t)(1<<4))
171 
172 #define CAN_IER_EPIE ((uint32_t)(1<<5))
173 
174 #define CAN_IER_ALIE ((uint32_t)(1<<6))
175 
176 #define CAN_IER_BEIE ((uint32_t)(1<<7))
177 
178 #define CAN_IER_IDIE ((uint32_t)(1<<8))
179 
180 #define CAN_IER_TIE2 ((uint32_t)(1<<9))
181 
182 #define CAN_IER_TIE3 ((uint32_t)(1<<10))
183 
184 /*********************************************************************/
188 #define CAN_BTR_BRP(n) ((uint32_t)(n&0x3FF))
189 
190 #define CAN_BTR_SJM(n) ((uint32_t)((n&0x3)<<14))
191 
192 #define CAN_BTR_TESG1(n) ((uint32_t)(n&0xF)<<16))
193 
194 #define CAN_BTR_TESG2(n) ((uint32_t)(n&0xF)<<20))
195 
196 #define CAN_BTR_SAM(n) ((uint32_t)(1<<23))
197 
198 /*********************************************************************/
202 #define CAN_EWL_EWL(n) ((uint32_t)(n&0xFF))
203 
204 /*********************************************************************/
208 #define CAN_SR_RBS ((uint32_t)(1))
209 
210 #define CAN_SR_DOS ((uint32_t)(1<<1))
211 
212 #define CAN_SR_TBS1 ((uint32_t)(1<<2))
213 
214 #define CAN_SR_TCS1 ((uint32_t)(1<<3))
215 
216 #define CAN_SR_RS ((uint32_t)(1<<4))
217 
218 #define CAN_SR_TS1 ((uint32_t)(1<<5))
219 
220 #define CAN_SR_ES ((uint32_t)(1<<6))
221 
222 #define CAN_SR_BS ((uint32_t)(1<<7))
223 
224 #define CAN_SR_TBS2 ((uint32_t)(1<<10))
225 
226 #define CAN_SR_TCS2 ((uint32_t)(1<<11))
227 
228 #define CAN_SR_TS2 ((uint32_t)(1<<13))
229 
230 #define CAN_SR_TBS3 ((uint32_t)(1<<18))
231 
232 #define CAN_SR_TCS3 ((uint32_t)(1<<19))
233 
234 #define CAN_SR_TS3 ((uint32_t)(1<<21))
235 
236 /*********************************************************************/
240 #define CAN_RFS_ID_INDEX(n) ((uint32_t)(n&0x3FF))
241 
242 #define CAN_RFS_BP ((uint32_t)(1<<10))
243 
244 #define CAN_RFS_DLC(n) ((uint32_t)((n&0xF)<<16)
245 
246 #define CAN_RFS_RTR ((uint32_t)(1<<30))
247 
248 #define CAN_RFS_FF ((uint32_t)(1<<31))
249 
250 /*********************************************************************/
254 #define CAN_RID_ID_11(n) ((uint32_t)(n&0x7FF))
255 
256 #define CAN_RID_ID_29(n) ((uint32_t)(n&0x1FFFFFFF))
257 
258 /*********************************************************************/
262 #define CAN_RDA_DATA1(n) ((uint32_t)(n&0xFF))
263 
264 #define CAN_RDA_DATA2(n) ((uint32_t)((n&0xFF)<<8))
265 
266 #define CAN_RDA_DATA3(n) ((uint32_t)((n&0xFF)<<16))
267 
268 #define CAN_RDA_DATA4(n) ((uint32_t)((n&0xFF)<<24))
269 
270 /*********************************************************************/
274 #define CAN_RDB_DATA5(n) ((uint32_t)(n&0xFF))
275 
276 #define CAN_RDB_DATA6(n) ((uint32_t)((n&0xFF)<<8))
277 
278 #define CAN_RDB_DATA7(n) ((uint32_t)((n&0xFF)<<16))
279 
280 #define CAN_RDB_DATA8(n) ((uint32_t)((n&0xFF)<<24))
281 
282 /*********************************************************************/
286 #define CAN_TFI_PRIO(n) ((uint32_t)(n&0xFF))
287 
288 #define CAN_TFI_DLC(n) ((uint32_t)((n&0xF)<<16))
289 
290 #define CAN_TFI_RTR ((uint32_t)(1<<30))
291 
292 #define CAN_TFI_FF ((uint32_t)(1<<31))
293 
294 /*********************************************************************/
298 #define CAN_TID_ID11(n) ((uint32_t)(n&0x7FF))
299 
300 #define CAN_TID_ID29(n) ((uint32_t)(n&0x1FFFFFFF))
301 
302 /*********************************************************************/
306 #define CAN_TDA_DATA1(n) ((uint32_t)(n&0xFF))
307 
308 #define CAN_TDA_DATA2(n) ((uint32_t)((n&0xFF)<<8))
309 
310 #define CAN_TDA_DATA3(n) ((uint32_t)((n&0xFF)<<16))
311 
312 #define CAN_TDA_DATA4(n) ((uint32_t)((n&0xFF)<<24))
313 
314 /*********************************************************************/
318 #define CAN_TDA_DATA5(n) ((uint32_t)(n&0xFF))
319 
320 #define CAN_TDA_DATA6(n) ((uint32_t)((n&0xFF)<<8))
321 
322 #define CAN_TDA_DATA7(n) ((uint32_t)((n&0xFF)<<16))
323 
324 #define CAN_TDA_DATA8(n) ((uint32_t)((n&0xFF)<<24))
325 
326 /*********************************************************************/
330 #define CAN1SLEEPCLR ((uint32_t)(1<<1))
331 
332 #define CAN2SLEEPCLR ((uint32_t)(1<<2))
333 
334 /*********************************************************************/
338 #define CAN_WAKEFLAGES_CAN1WAKE ((uint32_t)(1<<1))
339 
340 #define CAN_WAKEFLAGES_CAN2WAKE ((uint32_t)(1<<2))
341 
342 /*********************************************************************/
346 #define CAN_TSR_TS1 ((uint32_t)(1))
347 
348 #define CAN_TSR_TS2 ((uint32_t)(1<<1))
349 
350 #define CAN_TSR_TBS1 ((uint32_t)(1<<8))
351 
352 #define CAN_TSR_TBS2 ((uint32_t)(1<<9))
353 
354 #define CAN_TSR_TCS1 ((uint32_t)(1<<16))
355 
356 #define CAN_TSR_TCS2 ((uint32_t)(1<<17))
357 
358 /*********************************************************************/
362 #define CAN_RSR_RS1 ((uint32_t)(1))
363 
364 #define CAN_RSR_RS2 ((uint32_t)(1<<1))
365 
366 #define CAN_RSR_RB1 ((uint32_t)(1<<8))
367 
368 #define CAN_RSR_RB2 ((uint32_t)(1<<9))
369 
370 #define CAN_RSR_DOS1 ((uint32_t)(1<<16))
371 
372 #define CAN_RSR_DOS2 ((uint32_t)(1<<17))
373 
374 /*********************************************************************/
378 #define CAN_MSR_E1 ((uint32_t)(1))
379 
380 #define CAN_MSR_E2 ((uint32_t)(1<<1))
381 
382 #define CAN_MSR_BS1 ((uint32_t)(1<<8))
383 
384 #define CAN_MSR_BS2 ((uint32_t)(1<<9))
385 
386 /*********************************************************************/
390 #define CAN_AFMR_AccOff ((uint32_t)(1))
391 
392 #define CAN_AFMR_AccBP ((uint32_t)(1<<1))
393 
394 #define CAN_AFMR_eFCAN ((uint32_t)(1<<2))
395 
396 /*********************************************************************/
400 #define CAN_STT_sa(n) ((uint32_t)((n&1FF)<<2))
401 
402 /*********************************************************************/
406 #define CAN_SFF_GRP_sa(n) ((uint32_t)((n&3FF)<<2))
407 
408 /*********************************************************************/
412 #define CAN_EFF_sa(n) ((uint32_t)((n&1FF)<<2))
413 
414 /*********************************************************************/
418 #define CAN_Eff_GRP_sa(n) ((uint32_t)((n&3FF)<<2))
419 
420 /*********************************************************************/
424 #define CAN_EndofTable(n) ((uint32_t)((n&3FF)<<2))
425 
426 /*********************************************************************/
430 #define CAN_LUTerrAd(n) ((uint32_t)((n&1FF)<<2))
431 
432 /*********************************************************************/
436 #define CAN_LUTerr ((uint32_t)(1))
437 
438 /*********************************************************************/
442 #define CAN_FCANIE ((uint32_t)(1))
443 
444 /*********************************************************************/
448 #define CAN_FCANIC0_IntPnd(n) ((uint32_t)(1<<n))
449 
450 /*********************************************************************/
454 #define CAN_FCANIC1_IntPnd(n) ((uint32_t)(1<<(n-32)))
455 
456 
457 /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
459 #define PARAM_CANx(x) ((((uint32_t*)x)==((uint32_t *)LPC_CAN1)) \
460 ||(((uint32_t*)x)==((uint32_t *)LPC_CAN2)))
461 
462 /* Macro to determine if it is valid CANAF or not*/
463 #define PARAM_CANAFx(x) (((uint32_t*)x)== ((uint32_t*)LPC_CANAF))
464 
465 /* Macro to determine if it is valid CANAF RAM or not*/
466 #define PARAM_CANAFRAMx(x) (((uint32_t*)x)== (uint32_t*)LPC_CANAF_RAM)
467 
468 /* Macro to determine if it is valid CANCR or not*/
469 #define PARAM_CANCRx(x) (((uint32_t*)x)==((uint32_t*)LPC_CANCR))
470 
472 #define PARAM_I2S_DATA(data) ((data>=0)&&(data <= 0xFFFFFFFF))
473 
475 #define PRAM_I2S_FREQ(freq) ((freq>=16000)&&(freq <= 96000))
476 
478 #define PARAM_ID_11(n) ((n>>11)==0) /*-- 11 bit --*/
479 #define PARAM_ID_29(n) ((n>>29)==0) /*-- 29 bit --*/
480 
482 #define PARAM_DLC(n) ((n>>4)==0) /*-- 4 bit --*/
483 
484 #define PARAM_ID_FORMAT(n) ((n==STD_ID_FORMAT)||(n==EXT_ID_FORMAT))
485 
487 #define PARAM_GRP_ID(x, y) ((x<=y))
488 
490 #define PARAM_FRAME_TYPE(n) ((n==DATA_FRAME)||(n==REMOTE_FRAME))
491 
493 #define PARAM_CTRL_STS_TYPE(n) ((n==CANCTRL_GLOBAL_STS)||(n==CANCTRL_INT_CAP) \
494 ||(n==CANCTRL_ERR_WRN)||(n==CANCTRL_STS))
495 
497 #define PARAM_CR_STS_TYPE(n) ((n==CANCR_TX_STS)||(n==CANCR_RX_STS) \
498 ||(n==CANCR_MS))
499 
500 #define PARAM_AFMODE_TYPE(n) ((n==CAN_Normal)||(n==CAN_AccOff) \
501 ||(n==CAN_AccBP)||(n==CAN_eFCAN))
502 
504 #define PARAM_MODE_TYPE(n) ((n==CAN_OPERATING_MODE)||(n==CAN_RESET_MODE) \
505 ||(n==CAN_LISTENONLY_MODE)||(n==CAN_SELFTEST_MODE) \
506 ||(n==CAN_TXPRIORITY_MODE)||(n==CAN_SLEEP_MODE) \
507 ||(n==CAN_RXPOLARITY_MODE)||(n==CAN_TEST_MODE))
508 
510 #define PARAM_CTRL(n) ((n==CAN1_CTRL)|(n==CAN2_CTRL))
511 
513 #define PARAM_MSG_DISABLE(n) ((n==MSG_ENABLE)|(n==MSG_DISABLE))
514 
516 #define PARAM_INT_EN_TYPE(n) ((n==CANINT_RIE)||(n==CANINT_TIE1) \
517 ||(n==CANINT_EIE)||(n==CANINT_DOIE) \
518 ||(n==CANINT_WUIE)||(n==CANINT_EPIE) \
519 ||(n==CANINT_ALIE)||(n==CANINT_BEIE) \
520 ||(n==CANINT_IDIE)||(n==CANINT_TIE2) \
521 ||(n==CANINT_TIE3)||(n==CANINT_FCE))
522 
524 #define PARAM_AFLUT_ENTRY_TYPE(n) ((n==FULLCAN_ENTRY)||(n==EXPLICIT_STANDARD_ENTRY)\
525 ||(n==GROUP_STANDARD_ENTRY)||(n==EXPLICIT_EXTEND_ENTRY) \
526 ||(n==GROUP_EXTEND_ENTRY))
527 
529 #define PARAM_POSITION(n) ((n>=0)&&(n<512))
530 
535 /* Public Types --------------------------------------------------------------- */
541 /***********************************************************************
542  * CAN device configuration commands (IOCTL commands and arguments)
543  **********************************************************************/
547 typedef enum {
551 
555 typedef enum {
562 
566 typedef enum {
570 
574 typedef enum {
580 
584 typedef enum {
589 
593 typedef enum{
597 
601 typedef enum {
615 
619 typedef enum {
625 
629 typedef enum {
638 } CAN_MODE_Type;
639 
643 typedef enum {
644  CAN_OK = 1,
651 } CAN_ERROR;
652 
656 typedef struct {
657  uint8_t RD;
665  uint8_t TD;
674 
678 typedef struct {
679  uint32_t id;
683  uint8_t dataA[4];
684  uint8_t dataB[4];
685  uint8_t len;
689  uint8_t format;
693  uint8_t type;
698 } CAN_MSG_Type;
699 
703 typedef struct {
704  uint8_t controller;
708  uint8_t disable;
712  uint16_t id_11;
713 } FullCAN_Entry;
714 
718 typedef struct {
719  uint8_t controller;
723  uint8_t disable;
727  uint16_t id_11;
728 } SFF_Entry;
729 
733 typedef struct {
734  uint8_t controller1;
738  uint8_t disable1;
742  uint16_t lowerID;
743  uint8_t controller2;
747  uint8_t disable2;
751  uint16_t upperID;
754 } SFF_GPR_Entry;
755 
759 typedef struct {
760  uint8_t controller;
764  uint32_t ID_29;
765 } EFF_Entry;
766 
767 
771 typedef struct {
772  uint8_t controller1;
776  uint8_t controller2;
780  uint32_t lowerEID;
781  uint32_t upperEID;
782 } EFF_GPR_Entry;
783 
784 
788 typedef struct {
790  uint8_t FC_NumEntry;
792  uint8_t SFF_NumEntry;
796  uint8_t EFF_NumEntry;
799 } AF_SectionDef;
800 
806 /* Public Functions ----------------------------------------------------------- */
811 /* Init/DeInit CAN peripheral -----------*/
812 void CAN_Init(LPC_CAN_TypeDef *CANx, uint32_t baudrate);
813 void CAN_DeInit(LPC_CAN_TypeDef *CANx);
814 
815 /* CAN messages functions ---------------*/
819 
820 /* CAN configure functions ---------------*/
822  FunctionalState NewState);
823 void CAN_SetAFMode(LPC_CANAF_TypeDef* CANAFx, CAN_AFMODE_Type AFmode);
824 void CAN_SetCommand(LPC_CAN_TypeDef* CANx, uint32_t CMRType);
825 
826 /* AFLUT functions ---------------------- */
830  CAN_ID_FORMAT_Type format);
831 CAN_ERROR CAN_LoadGroupEntry(LPC_CAN_TypeDef* CANx, uint32_t lowerID,
832  uint32_t upperID, CAN_ID_FORMAT_Type format);
833 CAN_ERROR CAN_RemoveEntry(AFLUT_ENTRY_Type EntryType, uint16_t position);
834 
835 /* CAN interrupt functions -----------------*/
836 void CAN_IRQCmd(LPC_CAN_TypeDef* CANx, CAN_INT_EN_Type arg, FunctionalState NewState);
837 uint32_t CAN_IntGetStatus(LPC_CAN_TypeDef* CANx);
838 
839 /* CAN get status functions ----------------*/
843 uint32_t CAN_GetCRStatus(LPC_CANCR_TypeDef* CANCRx, CAN_CR_STS_Type arg);
844 
850 #ifdef __cplusplus
851 }
852 #endif
853 
854 #endif /* LPC17XX_CAN_H_ */
855 
860 /* --------------------------------- End Of File ------------------------------ */