]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/main.c
Device side of canqueue, compilation successful
[lincan.git] / embedded / app / usbcan / main.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <cpu_def.h>
4 #include <system_def.h>
5 #include <lt_timer.h>
6 #include <local_config.h>
7 #include <usb/usbdebug.h>
8 #include <usb/usb.h>
9 #include <usb/lpc.h>
10 #include <usb/usb_srq.h>
11 #include <mem_loc.h>
12 #include <keyval_id.h>
13 #include <hal_machperiph.h>
14 #include <keyval_loc.h>
15 #include <lpciap.h>
16 #include <lpciap_kvpb.h>
17
18 #include "./can/can.h"
19 #include "./can/sja1000p.h"
20 #include "./can/main.h"
21
22 #include "./can/can_sysdep.h"
23 #include "./can/modparms.h"
24 #include "./can/devcommon.h"
25
26 #include "./can/ul_usb1.h"
27 //#include "./can/setup.h"
28
29 #define MASK_EP1RX  0x01
30 #define MASK_EP1TX  0x02
31
32 #ifdef USB_MAX_PACKET
33         #undef USB_MAX_PACKET
34         #define USB_MAX_PACKET 8
35 #endif
36
37 #define CAN_OP_MASK 0x80
38 #define CAN_OP_READ 0x80
39 #define CAN_OP_WRITE 0x00
40
41
42 LT_TIMER_DEC(lt_10msec)
43 LT_TIMER_IMP(lt_10msec)
44 LT_TIMER_DEC(lt_100msec)
45 LT_TIMER_IMP(lt_100msec)
46 LT_TIMER_DEC(lt_2sec)
47 LT_TIMER_IMP(lt_2sec)
48
49 typedef void (*FNC)(); //function ptr
50
51 /***********************************/
52 // global variables
53
54 usb_device_t usb_device;
55
56 usb_ep_t eps[2];
57 unsigned char ep1_rx_buff[USB_MAX_PACKET];
58 unsigned char ep1_tx_buff[USB_MAX_PACKET];
59 uint8_t timer_str,timer_rx_off,timer_tx_off,timer_configured,usb_can_send;
60 volatile uint8_t bootloader_run;
61
62 int processlocal;
63
64 int baudrate[MAX_TOT_CHIPS];
65 struct canhardware_t canhardware;
66 struct canhardware_t *hardware_p=&canhardware;
67 struct canchip_t *chips_p[MAX_TOT_CHIPS];
68 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
69
70
71 /**
72         SOMETHING BAD HAPPENED
73 */
74 int sys_err(){
75   unsigned char i=0;
76
77   while(1) {
78     if (lt_100msec_expired(100)) {
79       i++;
80       if (i&1) {
81         SET_OUT_PIN(LED_PORT,LED_ERR);
82       } else {
83         CLR_OUT_PIN(LED_PORT,LED_ERR);
84       }
85      #ifdef WATCHDOG_ENABLED
86       watchdog_feed();
87      #endif /* WATCHDOG_ENABLED */
88     }
89   }
90 }
91
92 /**
93         Routine for visible LED blinking
94 */
95 void timer_10ms(void)
96 {
97   if (timer_tx_off!=0) timer_tx_off--;
98   else SET_OUT_PIN(LED_PORT,LED1_BIT);
99   if (timer_rx_off!=0) timer_rx_off--;
100   else SET_OUT_PIN(LED_PORT,LED2_BIT);
101
102 /*  if (timer_configured!=0) timer_configured--;
103   else {
104     timer_configured=20;
105                 CLR_OUT_PIN(LED_PORT,LED1_BIT);
106                 CLR_OUT_PIN(LED_PORT,LED2_BIT);
107                 timer_rx_off=timer_tx_off=5;
108   }*/
109 }
110
111 /***********************************/
112 int main(void)
113 {
114         struct candevice_t *candev;
115         struct canchip_t *chip;
116         int chipnr,bd;
117
118 //      volatile int i=0;
119         bootloader_run=0;
120         /***********************************/
121         lt_10msec_init();
122         lt_100msec_init();
123         lt_2sec_init();
124
125         SET_OUT_PIN(LED_PORT,LED_ERR);
126         CLR_OUT_PIN(LED_PORT,LED_GP);
127
128         /// *************************
129         /// CAN device initialization
130
131         baudrate[0]=1000;
132
133         canqueue_kern_initialize();
134
135         hardware_p->nr_boards=1;
136
137         candev=(struct candevice_t *)malloc(sizeof(struct candevice_t));
138                 if (candev==NULL) sys_err();
139                 memset(candev, 0, sizeof(struct candevice_t));
140
141         hardware_p->candevice[0]=candev;
142         candev->candev_idx=0;
143         candev->io_addr=0;
144         candev->dev_base_addr=0;
145
146         candev->hwspecops=(struct hwspecops_t *)malloc(sizeof(struct hwspecops_t));
147                 if (candev->hwspecops==NULL) sys_err();
148                 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
149
150         ul_usb1_register(candev->hwspecops);
151
152         bd=baudrate[0];
153         if (candev->hwspecops->init_hw_data(candev)) sys_err();
154         /* Alocate and initialize the chip structures */
155         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
156 /*              if(chipnr<irqnum)
157                         irqsig=irq[*irq_param_idx_p+chipnr];*/
158                 if (init_chip_struct(candev, chipnr, 0, bd*1000)) sys_err();
159         }
160         if (candev->hwspecops->request_io(candev))
161                 sys_err();
162         candev->flags|=CANDEV_IO_RESERVED;
163         if (candev->hwspecops->reset(candev))
164                 sys_err();
165         for(chipnr=0; chipnr<candev->nr_all_chips; chipnr++) {
166                 if((chip=candev->chip[chipnr])==NULL)
167                         continue;
168
169                 if(chip->chipspecops->attach_to_chip(chip)<0) {
170 //                      CANMSG("Initial attach to the chip HW failed\n");
171                         sys_err();
172                 }
173
174                 chip->flags |= CHIP_ATTACHED;
175
176 // Interrupts from chip are served in main cycle
177 /*              if(can_chip_setup_irq(chip)<0) {
178 //                      CANMSG("Error to setup chip IRQ\n");
179                         sys_err();
180                 }*/
181         }
182
183         if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
184                 if (candev->hwspecops->program_irq(candev)){
185 //                      CANMSG("Error to program board interrupt\n");
186                         sys_err();
187                 }
188
189
190
191         /// ***************************************
192         /// CAN device initialization - client side
193
194         ///*********
195         /// USB init
196
197         memset( &usb_device, 0, sizeof( usb_device));
198         usb_device.id = 1;
199         usb_device.init = usb_lpc_init;
200         usb_debug_set_level(DEBUG_LEVEL_NONE);
201         usb_device.cntep = 3;
202         usb_device.ep = eps;
203
204         eps[0].max_packet_size = USB_MAX_PACKET;
205         eps[1].max_packet_size = USB_MAX_PACKET;
206         eps[0].epnum = 0x01;
207         eps[1].epnum = 0x81;
208         eps[0].event_mask = 0x04;
209         eps[1].event_mask = 0x08;
210         eps[0].udev = &usb_device;
211         eps[1].udev = &usb_device;
212
213 //  usb_device.vendor_fnc=usb_loader;
214
215         usb_init(&usb_device);
216         usb_connect(&usb_device);
217
218         can_init();
219         usb_can_send=1;
220
221         /********************/
222         // start
223   timer_rx_off=timer_tx_off=timer_str=timer_configured=0;
224         while (1) {
225
226                 usb_check_events(&usb_device);
227                 usb_control_response(&usb_device);
228
229                 if ((usb_device.ep_events & MASK_EP1RX)&&(usb_can_send)) {  //EP1RX
230                         int size;
231                         uint8_t *data;
232                         uint8_t val;
233                         size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
234                         data=(uint8_t *)ep1_rx_buff;
235                         if (size==2){
236                                 if (((*data)&CAN_OP_MASK)==CAN_OP_READ){ // Get data from CAN device and return to caller
237                                         can_read((*data) & 0x7F,&val);
238                                         *(data+1)=val;
239                                         usb_udev_write_endpoint(&eps[1],(unsigned char *)data,size);
240                                         timer_rx_off=50;        //rosviceni diody pri prijmu
241                                         CLR_OUT_PIN(LED_PORT,LED2_BIT);
242                                         usb_can_send=0;
243                                 }
244                                 if (((*data)&CAN_OP_MASK)==CAN_OP_WRITE){ // Save data to CAN device
245                                         can_write((*data)&(~CAN_OP_MASK),data+1);
246                                         timer_tx_off=50;                //rozsviceni diod pri vysilani
247                                         CLR_OUT_PIN(LED_PORT,LED1_BIT);
248                                 }
249                         }
250                         usb_device.ep_events &= ~MASK_EP1RX;
251                 }
252
253                 if(usb_device.ep_events & MASK_EP1TX){
254                         usb_can_send=1;
255                         usb_device.ep_events &= ~MASK_EP1TX;
256                 }
257
258 #ifdef WATCHDOG_ENABLED
259                 watchdog_feed();
260 #endif /* WATCHDOG_ENABLED */
261
262                 /* 10ms timer */
263                 if (lt_10msec_expired(10))
264                         timer_10ms();
265         }
266
267         SET_OUT_PIN(LED_PORT,LED_GP);
268         SET_OUT_PIN(LED_PORT,LED_ERR);
269
270         /* unreachable code */
271 #ifdef SDCC
272         vec_jmp(0x0);  /* need to call a function from misc to correct linking */
273 #endif
274         return 0;
275 }