]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/can/lpc17xx_can.h
d4683e50d79f50c1ac6a14d0a34750a07ffb978f
[lincan.git] / embedded / app / usbcan / can / lpc17xx_can.h
1 #ifndef LPC17XX_CAN_H_
2 #define LPC17XX_CAN_H_
3
4 #include "LPC17xx.h"
5 #include "hal_machperiph.h"
6 #include "cpu_def.h"
7 #include "system_def.h"
8
9 #include "can/can.h"
10 #include "can/canmsg.h"
11
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16
17
18 #define CAN1_REGS_BASE  0x40044000UL
19 #define CAN2_REGS_BASE  0x40048000UL
20 #define CANAF_REGS_BASE 0x4003C000UL
21
22 #define CAN_MOD_o               0x0000
23 #define CAN_CMR_o               0x0004
24 #define CAN_GSR_o               0x0008
25 #define CAN_ICR_o               0x000C
26 #define CAN_IER_o               0x0010
27 #define CAN_BTR_o               0x0014
28 #define CAN_EWL_o               0x0018
29 #define CAN_SR_o                0x001C
30 #define CAN_RFS_o               0x0020
31 #define CAN_RID_o               0x0024
32 #define CAN_RDA_o               0x0028
33 #define CAN_RDB_o               0x002C
34 #define CAN_TFI1_o              0x0030
35 #define CAN_TID1_o              0x0034
36 #define CAN_TDA1_o              0x0038
37 #define CAN_TDB1_o              0x003C
38 #define CAN_TFI2_o              0x0040
39 #define CAN_TID2_o              0x0044
40 #define CAN_TDA2_o              0x0048
41 #define CAN_TDB2_o              0x004C
42 #define CAN_TFI3_o              0x0050
43 #define CAN_TID3_o              0x0054
44 #define CAN_TDA3_o              0x0058
45 #define CAN_TDB3_o              0x005C
46
47
48 #define CANAF_AFMR_o            0x0000
49 #define CANAF_SFF_sa_o          0x0004
50 #define CANAF_SFF_GRP_sa_o      0x0008
51 #define CANAF_EFF_sa_o          0x000C
52 #define CANAF_EFF_GRP_sa_o      0x0010
53 #define CANAF_ENDofTable_o      0x0014
54 #define CANAF_LUTerrAd_o        0x0018
55 #define CANAF_LUTerr_o          0x001C
56
57 //----------------------------------
58
59 #define CAN1MOD                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_MOD_o))
60 #define CAN1CMR                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_CMR_o))
61 #define CAN1GSR                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_GSR_o))
62 #define CAN1ICR                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_ICR_o))
63 #define CAN1IER                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_IER_o))
64 #define CAN1BTR                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_BTR_o))
65 #define CAN1EWL                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_EWL_o))
66 #define CAN1SR                  (*(uint32_t*)(CAN1_REGS_BASE+CAN_SR_o))
67 #define CAN1RFS                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_RFS_o))
68 #define CAN1RID                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_RID_o))
69 #define CAN1RDA                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_RDA_o))
70 #define CAN1RDB                 (*(uint32_t*)(CAN1_REGS_BASE+CAN_RDB_o))
71 #define CAN1TFI1                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TFI1_o))
72 #define CAN1TID1                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TID1_o))
73 #define CAN1TDA1                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDA1_o))
74 #define CAN1TDB1                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDB1_o))
75 #define CAN1TFI2                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TFI2_o))
76 #define CAN1TID2                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TID2_o))
77 #define CAN1TDA2                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDA2_o))
78 #define CAN1TDB2                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDB2_o))
79 #define CAN1TFI3                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TFI3_o))
80 #define CAN1TID3                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TID3_o))
81 #define CAN1TDA3                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDA3_o))
82 #define CAN1TDB3                (*(uint32_t*)(CAN1_REGS_BASE+CAN_TDB3_o))
83
84
85
86 #define CANAF_AFMR                              (*(uint32_t*)(CANAF_REGS_BASE+CANAF_AFMR_o))
87 #define CANAF_SFF_sa                    (*(uint32_t*)(CANAF_REGS_BASE+CANAF_SFF_sa_o))
88 #define CANAF_SFF_GRP_sa                (*(uint32_t*)(CANAF_REGS_BASE+CANAF_SFF_GRP_sa_o))
89 #define CANAF_EFF_sa                    (*(uint32_t*)(CANAF_REGS_BASE+CANAF_EFF_sa_o))
90 #define CANAF_EFF_GRP_sa                (*(uint32_t*)(CANAF_REGS_BASE+CANAF_EFF_GRP_sa_o))
91 #define CANAF_ENDofTable                (*(uint32_t*)(CANAF_REGS_BASE+CANAF_ENDofTable_o))
92 #define CANAF_LUTerrAd                  (*(uint32_t*)(CANAF_REGS_BASE+CANAF_LUTerrAd_o))
93 #define CANAF_LUTerr                    (*(uint32_t*)(CANAF_REGS_BASE+CANAF_LUTerr_o))
94
95 //----------------------------------
96
97 //CAN Global Status Register
98 #define CAN_GSR_BS              (1<<7)
99
100 //CAN Interrupt and Capture Register bits
101 #define CAN_ICR_RI              (1<<0)
102 #define CAN_ICR_TI1             (1<<1)
103 #define CAN_ICR_DOI             (1<<3) 
104 #define CAN_ICR_IDI             (1<<8) 
105
106 //CAN Status Register bits
107 #define CAN_SR_RBS              (1<<0)
108 #define CAN_SR_DOS              (1<<1) 
109 #define CAN_SR_TBS1             (1<<2)          
110
111 //CAN Command Register bits
112 #define CAN_CMR_TR              (1<<0)
113 #define CAN_CMR_AT              (1<<1)
114 #define CAN_CMR_RRB             (1<<2)
115 #define CAN_CMR_CDO             (1<<3)
116 #define CAN_CMR_STB1    (1<<5)
117
118 //CAN Transmit Frame Information register 1 bits
119 #define CAN_TFI1_RTR    (1<<30)
120 #define CAN_TFI1_EXT    (1<<31)
121
122 //CAN Receive Frame Status register
123 #define CAN_RFS_RTR     (1<<30)
124 #define CAN_RFS_EXT     (1<<31)
125
126 //CAN Interrupt Enable Register bits
127 #define CAN_IER_RIE             (1<<0) 
128 #define CAN_IER_TIE1    (1<<1)
129 #define CAN_IER_DOIE    (1<<3) 
130 #define CAN_IER_IDIE    (1<<8) 
131
132
133 //----------------------------------
134
135 #define PCONP SC->PCONP
136 #define PCLKSEL0 SC->PCLKSEL0 
137
138 #define PINSEL0 PINCON->PINSEL0
139 #define PINMODE0 PINCON->PINMODE0
140 #define PINMODE_OD0 PINCON->PINMODE_OD0
141
142 #define PCCAN1 (1<<13) // CAN Controller 1 power/clock control bit. 
143 #define PCCAN2 (1<<14) // CAN Controller 2 power/clock control bit. 
144
145 #define PCLK_CAN1_MASK  ((1<<27)|(1<<26)) 
146 #define PCLK_CAN2_MASK  ((1<<29)|(1<<28)) 
147 #define PCLK_ACF_MASK   ((1<<31)|(1<<30))
148
149 #define CAN1_RX_MASK    ((1<<1)|(1<<0))
150 #define CAN1_TX_MASK    ((1<<3)|(1<<2))
151
152 // CAN1_RX_BIT a CAN1_TX_BIT jiz definovany v system_def.h
153
154
155 void CAN_init(uint32_t baudrate);
156 void CAN_send(canmsg_t* msg);
157 void CAN_recv(canmsg_t* msg);
158 void CAN_IRQHandler(void);
159
160 //----------------------------------
161
162 struct can_lmc1_chip_data
163 {
164         int flags;
165 };
166
167 // board can-lmc1 specific functions:
168 int can_lmc1_register(struct hwspecops_t *hwspecops);
169 int can_lmc1_init_hw_data(struct candevice_t *candev);
170 int can_lmc1_init_chip_data(struct candevice_t *candev, int chipnr);
171 int can_lmc1_init_obj_data(struct canchip_t *chip, int objnr);
172 void can_lmc1_write_register(unsigned data, unsigned long address);
173 unsigned can_lmc1_read_register(unsigned long address);
174 int can_lmc1_request_io(struct candevice_t *candev);
175 int can_lmc1_reset(struct candevice_t *candev);
176
177 // lpc17xx can chip specific functions:
178 int lpc17xx_chip_config(struct canchip_t *chip);
179 int lpc17xx_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
180                                                         struct canmsg_t *msg);
181 int lpc17xx_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
182                                                         struct canmsg_t *msg);
183 int lpc17xx_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj);
184 int lpc17xx_irq_handler(int irq, struct canchip_t *chip);
185 void lpc17xx_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj);
186 int lpc17xx_fill_chipspecops(struct canchip_t *chip);
187 int lpc17xx_register(struct chipspecops_t *chipspecops);
188 int lpc17xx_attach_to_chip(struct canchip_t *chip);
189 int lpc17xx_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj);
190 void lpc17xx_read(struct canchip_t *chip, struct msgobj_t *obj);
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif // LPC17XX_CAN_H_