]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/main.c
873f4c6d8d2365cf1b18e452dfc6dec40ee534ba
[lincan.git] / embedded / app / usbcan / main.c
1 /**************************************************************************/
2 /* File: main.c - setup and main loop of USB<->CAN converter              */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2011 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2008 Jan Kriz email:johen@post.cz                        */
7 /*                                                                        */
8 /* LinCAN is free software; you can redistribute it and/or modify it      */
9 /* under terms of the GNU General Public License as published by the      */
10 /* Free Software Foundation; either version 2, or (at your option) any    */
11 /* later version.  LinCAN is distributed in the hope that it will be      */
12 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
13 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
14 /* General Public License for more details. You should have received a    */
15 /* copy of the GNU General Public License along with LinCAN; see file     */
16 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
17 /* Cambridge, MA 02139, USA.                                              */
18 /*                                                                        */
19 /* To allow use of LinCAN in the compact embedded systems firmware        */
20 /* and RT-executives (RTEMS for example), main authors agree with next    */
21 /* special exception:                                                     */
22 /*                                                                        */
23 /* Including LinCAN header files in a file, instantiating LinCAN generics */
24 /* or templates, or linking other files with LinCAN objects to produce    */
25 /* an application image/executable, does not by itself cause the          */
26 /* resulting application image/executable to be covered by                */
27 /* the GNU General Public License.                                        */
28 /* This exception does not however invalidate any other reasons           */
29 /* why the executable file might be covered by the GNU Public License.    */
30 /* Publication of enhanced or derived LinCAN files is required although.  */
31 /**************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <cpu_def.h>
36 #include <system_def.h>
37 #include <lt_timer.h>
38 #include <local_config.h>
39 #include <usb/usbdebug.h>
40 #include <usb/usb.h>
41 #include <usb/lpc.h>
42 #include <usb/usb_srq.h>
43 #include <mem_loc.h>
44 #include <keyval_id.h>
45 #include <hal_machperiph.h>
46 #include <keyval_loc.h>
47 #include <lpciap.h>
48 #include <lpciap_kvpb.h>
49
50 #include <endian.h>
51 #if __BYTE_ORDER == __BIG_ENDIAN
52   #include <byteswap.h>
53 #endif
54
55 #include "./can/can.h"
56 #include "./can/sja1000p.h"
57 #include "./can/main.h"
58
59 // #include "./can/can_sysdep.h"
60 #include "./can/modparms.h"
61 #include "./can/devcommon.h"
62
63 //#include "./can/ul_usb1.h"
64
65 #include "./can/setup.h"
66
67 #include "./usb/usb_defs.h"
68 #include "./usb/usb_vend.h"
69
70 extern int can_lmc1_register(struct hwspecops_t *hwspecops);
71 extern int ul_usb1_register(struct hwspecops_t *hwspecops);
72
73 #define MASK_EP1RX  0x01
74 #define MASK_EP1TX  0x02
75
76 #define CAN_OP_MASK 0x80
77 #define CAN_OP_READ 0x80
78 #define CAN_OP_WRITE 0x00
79
80 #ifdef USB_MAX_PACKET
81         #undef USB_MAX_PACKET
82         #define USB_MAX_PACKET 16
83 #endif
84 /***********************************************************************
85  * Note:
86  * Comparing to LinCAN, there is no need to sleep for processes
87  * because the degree of filling of fifo from the client side
88  * is solved in main cycle (no new messages are accepted when full)
89  * and on the server side by speed of USB interface. FIFO in edge
90  * from SJA chip to USB interface should never be filled.
91  ***********************************************************************/
92
93 /***********************************************************************
94  * Note:
95  * Code is wittingly complex in order to ease future changes in hardware
96  * configuration and to make it as much similar as the code of LinCAN
97  ***********************************************************************/
98
99 LT_TIMER_DEC(lt_10msec)
100 LT_TIMER_IMP(lt_10msec)
101 LT_TIMER_DEC(lt_100msec)
102 LT_TIMER_IMP(lt_100msec)
103 LT_TIMER_DEC(lt_2sec)
104 LT_TIMER_IMP(lt_2sec)
105
106 typedef void (*FNC)(); //function ptr
107
108 /***********************************************************************
109  * global variables
110  ***********************************************************************/
111
112 usb_device_t usb_device;
113
114 usb_ep_t eps[NUM_ENDPOINTS];
115 unsigned char ep1_rx_buff[USB_MAX_PACKET];
116 unsigned char ep1_tx_buff[USB_MAX_PACKET];
117 uint8_t timer_str,timer_rx_off,timer_tx_off,timer_configured;
118 volatile uint8_t bootloader_run;
119
120 uint8_t vendor_ret;
121
122 int processlocal;
123
124 int baudrate[MAX_TOT_CHIPS];
125 struct canhardware_t canhardware;
126 struct canhardware_t *hardware_p=&canhardware;
127 struct canchip_t *chips_p[MAX_TOT_CHIPS];
128 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
129
130 struct canuser_t *canuser;
131
132 extern int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
133 extern int register_chip_struct(struct canchip_t *chip, int minorbase);
134 extern int register_obj_struct(struct msgobj_t *obj, int minorbase);
135
136 /***********************************************************************
137  * IF SOMETHING BAD HAPPENED
138  ***********************************************************************/
139
140 int sys_err(){
141   
142   unsigned char i=0;
143
144   while(1) {
145     if (lt_100msec_expired(100)) {
146       i++;
147       if (i&1) {
148         SET_OUT_PIN(LED_PORT,LED_ERR);
149       } else {
150         CLR_OUT_PIN(LED_PORT,LED_ERR);
151       }
152      #ifdef WATCHDOG_ENABLED
153       watchdog_feed();
154      #endif /* WATCHDOG_ENABLED */
155     }
156   }
157 }
158
159 /***********************************************************************
160  * Routine for visible LED blinking (on USB transmission)
161  ***********************************************************************/
162
163 void timer_10ms(void)
164 {
165   if (timer_tx_off!=0) timer_tx_off--;
166   else SET_OUT_PIN(LED_PORT,LED1_BIT);
167   if (timer_rx_off!=0) timer_rx_off--;
168   else SET_OUT_PIN(LED_PORT,LED2_BIT);
169
170 /*  if (timer_configured!=0) timer_configured--;
171   else {
172     timer_configured=20;
173                 CLR_OUT_PIN(LED_PORT,LED1_BIT);
174                 CLR_OUT_PIN(LED_PORT,LED2_BIT);
175                 timer_rx_off=timer_tx_off=5;
176   }*/
177 }
178
179 /***********************************************************************
180  * Main routine
181  ***********************************************************************/
182
183 int main(void)
184 {
185         struct candevice_t *candev;
186         struct canchip_t *chip=NULL;
187         struct msgobj_t *obj;
188         struct canque_ends_t *qends;
189         struct canque_edge_t *edge,*qedge;
190         struct canque_slot_t *slot;
191         struct canmsg_t canmsg;
192         can_spin_irqflags_t iflags;
193
194         int chipnr,bd;
195         int i,size,m=0;
196
197         CANMSG("Starting USBCAN module firmware...\n");
198
199 //      volatile int i=0;
200         bootloader_run=0;
201         /***********************************/
202
203         lt_10msec_init();
204         lt_100msec_init();
205         lt_2sec_init();
206
207         // DEBUG
208         //SET_OUT_PIN(LED_PORT,LED_ERR);
209         //CLR_OUT_PIN(LED_PORT,LED_GP);
210
211         if (USB_MAX_PACKET<16){
212                 CANMSG("Maximum packet size less than 16B (is %dB)\n",USB_MAX_PACKET);
213                 sys_err();
214         }
215
216
217         // ***********************************************************************
218         // * CAN device initialization - device side (adapted from LinCAN setup.c)
219         // ***********************************************************************
220
221 //      useless with lpc17xx (defined in can_lpcbusemu.c)
222 //      can_init();
223
224
225         DEBUGMSG("Initiating CAN device initialization\n");
226         baudrate[0]=1000;
227
228         canqueue_kern_initialize();
229
230         hardware_p->nr_boards=1;
231
232         candev=(struct candevice_t *)malloc(sizeof(struct candevice_t));
233         if (!candev){
234                 CANMSG("No space left in memory\n");
235                 sys_err();
236         }
237         memset(candev, 0, sizeof(struct candevice_t));
238
239         hardware_p->candevice[0]=candev;
240         candev->candev_idx=0;
241         candev->io_addr=0;
242         candev->dev_base_addr=0;
243
244         candev->hwspecops=(struct hwspecops_t *)malloc(sizeof(struct hwspecops_t));
245         if (!candev->hwspecops){
246                 CANMSG("No space left in memory\n");
247                 sys_err();
248         }
249         memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
250
251         
252
253 #ifdef CONFIG_OC_LINCAN_CARD_ul_usb1
254         ul_usb1_register(candev->hwspecops);
255 #endif
256 #ifdef CONFIG_OC_LINCAN_CARD_can_lmc1
257         can_lmc1_register(candev->hwspecops);
258 #endif
259
260         bd=baudrate[0];
261         if (candev->hwspecops->init_hw_data(candev)){
262                 CANMSG("HW data could not be initialized\n");
263                 sys_err();
264         }
265
266         // Alocate and initialize the chip structures
267         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
268 //              if(chipnr<irqnum)
269 //                      irqsig=irq[*irq_param_idx_p+chipnr];
270                 if (init_chip_struct(candev, chipnr, 0, bd*1000)){
271                         CANMSG("Chip structure could not be initialized\n");
272                         sys_err();
273                 }
274         }
275
276
277
278         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
279                 struct canchip_t *chip=candev->chip[chipnr];
280                 int objnr;
281
282                 register_chip_struct(chip, m);
283
284                 for (objnr=0; objnr<chip->max_objects; objnr++) {
285                         register_obj_struct(chip->msgobj[objnr], m);
286                         if(m>=0) m++;
287                 }
288         }
289
290
291         if (candev->hwspecops->request_io(candev))
292                 sys_err();
293         candev->flags|=CANDEV_IO_RESERVED;
294
295
296         if (candev->hwspecops->reset(candev))
297                 sys_err();
298
299
300         for(chipnr=0; chipnr<candev->nr_all_chips; chipnr++) {
301                 if((chip=candev->chip[chipnr])==NULL)
302                         continue;
303
304                 if(chip->chipspecops->attach_to_chip(chip)<0) {
305 //                      CANMSG("Initial attach to the chip HW failed\n");
306                         sys_err();
307                 }
308
309                 chip->flags |= CHIP_ATTACHED;
310
311                 // used with lpc17xx:
312                 if(can_chip_setup_irq(chip)<0) {
313                         CANMSG("Error to setup chip IRQ\n");
314                         sys_err();
315                 }
316
317         }
318
319         if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
320                 if (candev->hwspecops->program_irq(candev)){
321 //                      CANMSG("Error to program board interrupt\n");
322                         sys_err();
323                 }
324
325         //***********************************************************************
326         // * CAN device initialization - client side (adapted from LinCAN open.c)
327         // ***********************************************************************
328
329
330         chip=candev->chip[0];
331         obj=chip->msgobj[0];
332         atomic_inc(&obj->obj_used);
333         can_msgobj_set_fl(obj,OPENED);
334
335         if (chip->flags & CHIP_CONFIGURED)
336                 DEBUGMSG("Device is already configured.\n");
337         else {
338                 if (chip->chipspecops->chip_config(chip))
339                         CANMSG("Error configuring chip.\n");
340                 else
341                         chip->flags |= CHIP_CONFIGURED;
342
343                 if (chip->chipspecops->pre_read_config(chip,obj)<0)
344                         CANMSG("Error initializing chip for receiving\n");
345
346         } // End of chip configuration
347
348
349         canuser = (struct canuser_t *)malloc(sizeof(struct canuser_t));
350         if(canuser == NULL) sys_err();
351         canuser->flags=0;
352 //      canuser->userinfo.fileinfo.file = file;
353         canuser->msgobj = obj;
354 //      canuser->magic = CAN_USER_MAGIC;
355 //      file->private_data = canuser;
356
357         qends = (struct canque_ends_t *)malloc(sizeof(struct canque_ends_t));
358         if(qends == NULL) sys_err();
359         canqueue_ends_init_kern(qends);
360         canuser->qends = qends;
361
362         //required to synchronize with RT-Linux context
363         can_spin_lock_irqsave(&canuser_manipulation_lock, iflags);
364         list_add(&canuser->peers, &obj->obj_users);
365         can_spin_unlock_irqrestore(&canuser_manipulation_lock, iflags);
366
367         if(canqueue_connect_edge(edge=canque_new_edge_kern(MAX_BUF_LENGTH),
368                 canuser->qends, obj->qends)<0) sys_err();
369
370         if(canqueue_connect_edge(canuser->rx_edge0=canque_new_edge_kern(MAX_BUF_LENGTH),
371                 obj->qends, canuser->qends)<0) sys_err();
372         //FIXME: more generic model should be used there
373         canque_edge_decref(canuser->rx_edge0);
374         canque_edge_decref(edge);
375
376
377
378         /***********************************************************************
379          * USB Init
380          ***********************************************************************/
381
382         memset( &usb_device, 0, sizeof( usb_device));
383         usb_device.id = 1;
384         usb_device.devdes_table = &usb_devdes_table;
385         usb_device.init = usb_lpc_init;
386         usb_debug_set_level(DEBUG_LEVEL_NONE);
387         usb_device.cntep = NUM_ENDPOINTS;
388         usb_device.ep = eps;
389
390         eps[0].max_packet_size = USB_MAX_PACKET;
391         eps[1].max_packet_size = USB_MAX_PACKET;
392         eps[0].epnum = 0x01;
393         eps[1].epnum = 0x81;
394         eps[0].event_mask = 0x04;
395         eps[1].event_mask = 0x08;
396         eps[0].udev = &usb_device;
397         eps[1].udev = &usb_device;
398
399         usb_device.vendor_fnc=usbcan_vendor;
400
401         usb_init(&usb_device);
402         usb_connect(&usb_device);
403         usb_device.ep_events |= MASK_EP1TX;
404
405         /***********************************************************************
406          * Start
407          ***********************************************************************/
408
409         timer_rx_off=timer_tx_off=timer_str=timer_configured=0;
410
411
412         printf("Main loop\n");
413
414         while (1) {
415
416                 usb_check_events(&usb_device);
417                 usb_control_response(&usb_device);
418
419 #ifdef CONFIG_OC_LINCAN_CARD_ul_usb1
420                 /* polled IRQ mode for ul_usb1 board*/
421                 if (!(IO0PIN&P0_SJA1000_INT_PIN)) /* INT PIN is inverted */
422                         chip->chipspecops->irq_handler(0,chip);
423 #endif
424
425                 if (usb_device.ep_events & MASK_EP1RX) {  //EP1RX - data waiting to receive
426                         
427                         if (canque_get_inslot(qends, &qedge, &slot, 0)>=0){ //Free slot obtained
428
429                                 size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,16);
430                                 if (size==16){
431                                         uint16_t msgflags;
432                                         uint32_t msgid;
433                                         canmsg.cob=0;
434                                         canmsg.length=*(uint8_t *)(ep1_rx_buff+1);
435                                         if (canmsg.length > CAN_MSG_LENGTH)
436                                                 canmsg.length=CAN_MSG_LENGTH;
437                                         msgflags=*(uint16_t *)(ep1_rx_buff+2);
438                                         msgid=*(uint32_t *)(ep1_rx_buff+4);
439                                         #if __BYTE_ORDER == __BIG_ENDIAN
440                                                 msgflags  = bswap_16( msgflags);
441                                                 msgid  = bswap_32( msgid);
442                                         #endif
443                                         canmsg.flags=msgflags;
444                                         canmsg.id=msgid;
445
446                                         for (i=0;i<canmsg.length;i++){
447                                                 canmsg.data[i]=*(unsigned char*)(ep1_rx_buff+8+i);
448                                         }
449                                         for (;i<CAN_MSG_LENGTH;i++){
450                                                 canmsg.data[i]=0;
451                                         }
452                                         /* Automatic selection of extended format if ID>2047 */
453                                         if (canmsg.id & ~0x7ffl & MSG_ID_MASK ) canmsg.flags |= MSG_EXT;
454                                         /* has been dependent on "extended" option */
455                         
456                                         slot->msg=canmsg;
457                                         canque_put_inslot(qends, qedge, slot);
458
459                                 }
460                                 else
461                                         canque_abort_inslot(qends,qedge,slot);
462                                         
463
464                                 timer_rx_off=50;        //rosviceni diody pri prijmu
465                                 CLR_OUT_PIN(LED_PORT,LED2_BIT);
466                                 usb_device.ep_events &= ~MASK_EP1RX;
467                         
468                                         
469                         }
470                         
471
472 /*                      if (size==2){
473                                 uint8_t val;
474                                 if ((data[0]&CAN_OP_MASK)==CAN_OP_READ){ // Get data from CAN device and return to caller
475                                         val = can_read(data[0] & ~CAN_OP_MASK);
476                                         *(data+1)=val;
477                                         usb_udev_write_endpoint(&eps[1],(unsigned char *)data,size);
478                                         timer_rx_off=50;        //rosviceni diody pri prijmu
479                                         CLR_OUT_PIN(LED_PORT,LED2_BIT);
480                                         usb_can_send=0;
481                                 }
482                                 if ((data[0]&CAN_OP_MASK)==CAN_OP_WRITE){ // Save data to CAN device
483                                         can_write(data[1], data[0] & ~CAN_OP_MASK);
484                                         timer_tx_off=50;                //rozsviceni diod pri vysilani
485                                         CLR_OUT_PIN(LED_PORT,LED1_BIT);
486                                 }
487                         }*/
488                 }
489
490                 
491                 if(usb_device.ep_events & MASK_EP1TX){ //EP1TX - data transmitted
492                         if(canque_test_outslot(qends, &qedge, &slot)>=0){
493                                 DEBUGMSG("CAN message ready to send over usb\n");
494                                 uint16_t msgflags;
495                                 uint32_t msgid;
496
497                                 *(uint8_t *)(ep1_tx_buff)=0;
498                                 *(uint8_t *)(ep1_tx_buff+1)=slot->msg.length;
499
500                                 msgflags=slot->msg.flags;
501                                 msgid=slot->msg.id;
502                                 #if __BYTE_ORDER == __BIG_ENDIAN
503                                         msgflags  = bswap_16( msgflags);
504                                         msgid  = bswap_32( msgid);
505                                 #endif
506
507                                 *(uint16_t *)(ep1_tx_buff+2)=msgflags;
508                                 *(uint32_t *)(ep1_tx_buff+4)=msgid;
509                                 for (i=0;i<slot->msg.length;i++){
510                                         *(uint8_t *)(ep1_tx_buff+8+i)=slot->msg.data[i];
511                                 }
512                                 for (;i<CAN_MSG_LENGTH;i++){
513                                         *(uint8_t *)(ep1_tx_buff+8+i)=0;
514                                 }
515                                 usb_udev_write_endpoint(&eps[1],ep1_tx_buff,16);
516
517                                 canque_free_outslot(qends, qedge, slot);
518                                 timer_tx_off=50;                //rozsviceni diod pri vysilani
519                                 CLR_OUT_PIN(LED_PORT,LED1_BIT);
520                                 usb_device.ep_events &= ~MASK_EP1TX;
521
522                         }
523                         
524
525                 }
526                 
527
528                 //if (usb_can_send && )
529
530 #ifdef WATCHDOG_ENABLED
531                 watchdog_feed();
532 #endif /* WATCHDOG_ENABLED */
533
534                 /* 10ms timer */
535                 if (lt_10msec_expired(10))
536                         timer_10ms();
537         }
538
539         SET_OUT_PIN(LED_PORT,LED_GP);
540         SET_OUT_PIN(LED_PORT,LED_ERR);
541
542         /* unreachable code */
543 #ifdef SDCC
544         vec_jmp(0x0);  /* need to call a function from misc to correct linking */
545 #endif
546         return 0;
547 }