1 | /*****************************************************************************
|
2 | * can.c: CAN module API file for NXP LPC17xx Family Microprocessors
|
3 | *
|
4 | * Copyright(C) 2009, NXP Semiconductor
|
5 | * All rights reserved.
|
6 | *
|
7 | * History
|
8 | * 2009.05.27 ver 1.00 Prelimnary version, first Release
|
9 | * 13.10.2010 ver 1.1 Änderung bezüglich CAN2
|
10 | *****************************************************************************/
|
11 | #include "lpc17xx.h"
|
12 | #include "type.h"
|
13 | #include "can.h"
|
14 | #include "mfa_display.h"
|
15 |
|
16 | /* Receive Queue: one queue for each CAN port */
|
17 | extern CAN_MSG MsgBuf_RX1;
|
18 | extern volatile uint32_t CAN1RxDone;
|
19 | extern volatile uint32_t kmph;
|
20 | extern volatile uint32_t data[12];
|
21 |
|
22 | volatile uint32_t CANStatus;
|
23 | uint32_t CAN1RxCount = 0;
|
24 | uint32_t CAN1ErrCount = 0;
|
25 |
|
26 |
|
27 | /******************************************************************************
|
28 | ** Function name: CAN_ISR_Rx1
|
29 | **
|
30 | ** Descriptions: CAN Rx1 interrupt handler
|
31 | **
|
32 | ** parameters: None
|
33 | ** Returned value: None
|
34 | **
|
35 | ******************************************************************************/
|
36 | void CAN_ISR_Rx1( void )
|
37 | {
|
38 | uint32_t * pDest;
|
39 |
|
40 |
|
41 | pDest = (uint32_t *)&MsgBuf_RX1;
|
42 | *pDest = LPC_CAN1->RFS;
|
43 |
|
44 | pDest++;
|
45 | *pDest = LPC_CAN1->RID;
|
46 |
|
47 |
|
48 | pDest++;
|
49 | *pDest = LPC_CAN1->RDA;
|
50 |
|
51 |
|
52 | pDest++;
|
53 | *pDest = LPC_CAN1->RDB;
|
54 |
|
55 | LPC_CAN1->CMR = 0x04; /* release receive buffer */
|
56 | /*CAN1RxDone = TRUE;
|
57 | return;
|
58 | }
|
59 |
|
60 | /*****************************************************************************
|
61 | ** Function name: CAN_Handler
|
62 | **
|
63 | ** Descriptions: CAN interrupt handler
|
64 | **
|
65 | ** parameters: None
|
66 | ** Returned value: None
|
67 | **
|
68 | *****************************************************************************/
|
69 | void CAN_IRQHandler(void)
|
70 | {
|
71 | CAN_ISR_Rx1();
|
72 | return;
|
73 | }
|
74 |
|
75 | /******************************************************************************
|
76 | ** Function name: CAN_Init
|
77 | **
|
78 | ** Descriptions: Initialize CAN, install CAN interrupt handler
|
79 | **
|
80 | ** parameters: bitrate
|
81 | ** Returned value: true or false, false if initialization failed.
|
82 | **
|
83 | ******************************************************************************/
|
84 | uint32_t CAN_Init( uint32_t can_btr )
|
85 | { CAN1RxDone = FALSE;
|
86 |
|
87 | LPC_SC->PCONP |= ((1<<13)|(1<<14)); /* Enable CAN1 and CAN2 clock */
|
88 |
|
89 | LPC_PINCON->PINSEL0 &= ~0x0000000F; /* CAN1 is p0.0 and p0.1 */
|
90 | LPC_PINCON->PINSEL0 |= 0x00000005;
|
91 |
|
92 | LPC_CAN1->MOD = LPC_CAN2->MOD = 1; /* Reset CAN */
|
93 | LPC_CAN1->IER = LPC_CAN2->IER = 0; /* Disable Receive Interrupt */
|
94 | LPC_CAN1->GSR = LPC_CAN2->GSR = 0; /* Reset error counter when CANxMOD is in reset */
|
95 |
|
96 | LPC_CAN1->BTR = LPC_CAN2->BTR = can_btr;
|
97 | LPC_CAN1->MOD = LPC_CAN2->MOD = 0x0; /* CAN in normal operation mode */
|
98 |
|
99 | NVIC_EnableIRQ(CAN_IRQn);
|
100 |
|
101 | LPC_CAN1->IER = LPC_CAN2->IER = 0x01; /* Enable receive interrupts */
|
102 | return( TRUE );
|
103 | }
|
104 |
|
105 | /******************************************************************************
|
106 | ** Function name: CAN_SetACCF_Lookup
|
107 | **
|
108 | ** Descriptions: unlock IDs
|
109 | **
|
110 | ** parameters: none
|
111 | ** Returned value: none
|
112 | **
|
113 | ******************************************************************************/
|
114 | void CAN_SetACCF_Lookup( void )
|
115 | {
|
116 | uint16_t Id11_lo = 0x23;
|
117 | uint16_t Id11_hi = 0x45;
|
118 | uint32_t Id29_lo = 0x18DAF100;
|
119 | uint32_t Id29_hi = 0x18DAF1FF;
|
120 |
|
121 |
|
122 | uint32_t adr = 0;
|
123 |
|
124 | LPC_CANAF->AFMR = ACCF_OFF; // Acceptance Filter OFF
|
125 |
|
126 | LPC_CANAF->SFF_sa = adr;
|
127 | *((volatile uint32_t *)(LPC_CANAF_RAM_BASE + adr)) = (Id11_lo << 16) | Id11_hi;
|
128 | adr += 4;
|
129 | LPC_CANAF->SFF_GRP_sa = adr;
|
130 | LPC_CANAF->EFF_sa = adr;
|
131 | LPC_CANAF->EFF_GRP_sa = adr;
|
132 | LPC_CANAF->ENDofTable = adr;
|
133 |
|
134 | return;
|
135 | }
|
136 |
|
137 | /******************************************************************************
|
138 | ** Function name: CAN_SetACCF
|
139 | **
|
140 | ** Descriptions: Set acceptance filter and SRAM associated with
|
141 | **
|
142 | ** parameters: ACMF mode
|
143 | ** Returned value: None
|
144 | **
|
145 | **
|
146 | ******************************************************************************/
|
147 | void CAN_SetACCF( uint32_t ACCFMode )
|
148 | {
|
149 | switch ( ACCFMode )
|
150 | {
|
151 | case ACCF_OFF: /* Filter off*/
|
152 | LPC_CANAF->AFMR = ACCFMode;
|
153 | LPC_CAN1->MOD = LPC_CAN2->MOD = 1; // Reset CAN
|
154 | LPC_CAN1->IER = LPC_CAN2->IER = 0; // Disable Receive Interrupt
|
155 | LPC_CAN1->GSR = LPC_CAN2->GSR = 0; // Reset error counter when CANxMOD is in reset
|
156 | break;
|
157 |
|
158 | case ACCF_BYPASS: /* Bypass Mode --> all messages are accepted and stored */
|
159 | LPC_CANAF->AFMR = ACCFMode;
|
160 | break;
|
161 |
|
162 | case ACCF_ON: /* hardware accepting filtering */
|
163 | case ACCF_FULLCAN: /* hardware accepting filtering */
|
164 | LPC_CANAF->AFMR = ACCF_OFF;
|
165 | CAN_SetACCF_Lookup();
|
166 | LPC_CANAF->AFMR = ACCFMode;
|
167 | break;
|
168 |
|
169 | default:
|
170 | break;
|
171 | }
|
172 | return;
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | /*****************************************************************************
|
179 | ** Function name: main
|
180 | **
|
181 | ** Descriptions: main routine for CAN module test
|
182 | **
|
183 | ** parameters: None
|
184 | ** Returned value: int
|
185 | **
|
186 | *****************************************************************************/
|
187 | int main( void )
|
188 |
|
189 | {
|
190 | SystemInit(); // Funktionsaufruf Systeminitialisierung
|
191 | lcd_init();
|
192 | INOUT_Def();
|
193 |
|
194 | /* Please note, the bit timing is based on the setting of the
|
195 | PCLK, if different PCLK is used, please read can.h carefully
|
196 | and set your CAN bit timing accordingly. */
|
197 |
|
198 | CAN_Init(BITRATE500K15MHZ); // Bitrate = 500kbaud bei 15MHz
|
199 |
|
200 |
|
201 | MsgBuf_TX1.Frame = 0x00080000; // 11-bit, no RTR, DLC is 8 bytes
|
202 | MsgBuf_TX1.MsgID = 0x2ee; // Explicit Standard ID
|
203 | MsgBuf_TX1.DataA = data[0];
|
204 | MsgBuf_TX1.DataB = data[1];
|
205 |
|
206 | MsgBuf_RX1.Frame = 0x0;
|
207 | MsgBuf_RX1.MsgID = 0x0;
|
208 | MsgBuf_RX1.DataA = 0x0;
|
209 | MsgBuf_RX1.DataB = 0x0;
|
210 |
|
211 | CAN_SetACCF( ACCF_ON ); // Filtermodus festlegen
|
212 |
|
213 | while ( 1 )
|
214 | {
|
215 |
|
216 | /* Transmit initial message on CAN 1 */
|
217 | while ( !(LPC_CAN1->GSR & (1 << 3)) )
|
218 | MsgBuf_TX1.DataA = data[0];
|
219 | MsgBuf_TX1.DataB = data[1];
|
220 | if ( CAN1_SendMessage( &MsgBuf_TX1 ) == FALSE )
|
221 | {
|
222 | continue;
|
223 | }
|
224 | lcd_draw_string(font_vw_16px, NORMAL_STYLE,"m/h",79,50,255);
|
225 | kmph=(((data[1]>>14))&0b1111111111)*0.32;
|
226 | if(kmph)
|
227 | {
|
228 | mph = kmph/(1.609);
|
229 | char feld[12];
|
230 | sprintf(feld,"%1u",mph);
|
231 | lcd_draw_string(font_vw_16px, NORMAL_STYLE,feld,50,50,255);
|
232 | }
|
233 | else
|
234 | {
|
235 | lcd_draw_string(font_vw_16px, NORMAL_STYLE,"-.-",50,50,255);
|
236 | }
|
237 | /* please note: FULLCAN identifier will NOT be received as it's not set
|
238 | in the acceptance filter. */
|
239 | if ( CAN1RxDone == TRUE )
|
240 | {
|
241 | CAN1RxDone = FALSE;
|
242 | MsgBuf_RX1.Frame = 0x0; // reset buffer
|
243 | MsgBuf_RX1.MsgID = 0x0;
|
244 | MsgBuf_RX1.DataA = 0x0;
|
245 | MsgBuf_RX1.DataB = 0x0;
|
246 | } /* Message on CAN 2 received */
|
247 |
|
248 | /*Schalter = ((data[4]>>19)&TASTER_MASK); // Tasterauswertung
|
249 | if(Schalter == 0x3)
|
250 | {
|
251 | LPC_GPIO2->FIOPIN &= ~(0x00000040);
|
252 | }*/
|
253 |
|
254 | if((MsgBuf_RX1.MsgID = 0x45)) // falls Nachricht mit entsprechenden Identifier da
|
255 | {
|
256 |
|
257 | LPC_GPIO2->FIOPIN &= ~(0x00000001); // setze LEDs
|
258 | }
|
259 |
|
260 | }
|
261 |
|
262 | }
|