d28cc13db1c352192bc7532b4b42728f9f9e54c3
[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
21 #define MASK_EP1RX  0x01
22 #define MASK_EP1TX  0x02
23
24 #ifdef USB_MAX_PACKET
25         #undef USB_MAX_PACKET
26         #define USB_MAX_PACKET 8
27 #endif
28
29 #define CAN_OP_MASK 0x80
30 #define CAN_OP_READ 0x80
31 #define CAN_OP_WRITE 0x00
32
33
34 LT_TIMER_DEC(lt_10msec)
35 LT_TIMER_IMP(lt_10msec)
36 LT_TIMER_DEC(lt_100msec)
37 LT_TIMER_IMP(lt_100msec)
38 LT_TIMER_DEC(lt_2sec)
39 LT_TIMER_IMP(lt_2sec)
40
41 typedef void (*FNC)(); //function ptr
42
43 /***********************************/
44 // global variables
45
46 usb_device_t usb_device;
47
48 usb_ep_t eps[2];
49 unsigned char ep1_rx_buff[USB_MAX_PACKET];
50 unsigned char ep1_tx_buff[USB_MAX_PACKET];
51 uint8_t timer_str,timer_rx_off,timer_tx_off,timer_configured,usb_can_send;
52 volatile uint8_t bootloader_run;
53
54 int processlocal;
55
56 /**
57         SOMETHING BAD HAPPENED
58 */
59 int sys_err(){
60   unsigned char i=0;
61
62   while(1) {
63     if (lt_100msec_expired(100)) {
64       i++;
65       if (i&1) {
66         SET_OUT_PIN(LED_PORT,LED_ERR);
67       } else {
68         CLR_OUT_PIN(LED_PORT,LED_ERR);
69       }
70      #ifdef WATCHDOG_ENABLED
71       watchdog_feed();
72      #endif /* WATCHDOG_ENABLED */
73     }
74   }
75 }
76
77 /**
78         Routine for visible LED blinking
79 */
80 void timer_10ms(void)
81 {
82   if (timer_tx_off!=0) timer_tx_off--;
83   else SET_OUT_PIN(LED_PORT,LED1_BIT);
84   if (timer_rx_off!=0) timer_rx_off--;
85   else SET_OUT_PIN(LED_PORT,LED2_BIT);
86
87 /*  if (timer_configured!=0) timer_configured--;
88   else {
89     timer_configured=20;
90                 CLR_OUT_PIN(LED_PORT,LED1_BIT);
91                 CLR_OUT_PIN(LED_PORT,LED2_BIT);
92                 timer_rx_off=timer_tx_off=5;
93   }*/
94 }
95
96 /***********************************/
97 int main(void)
98 {
99 //      volatile int i=0;
100         bootloader_run=0;
101         /***********************************/
102         lt_10msec_init();
103         lt_100msec_init();
104         lt_2sec_init();
105
106         SET_OUT_PIN(LED_PORT,LED_ERR);
107         CLR_OUT_PIN(LED_PORT,LED_GP);
108
109   //********************
110   // USB init
111   memset( &usb_device, 0, sizeof( usb_device));
112   usb_device.id = 1;
113   usb_device.init = usb_lpc_init;
114   usb_debug_set_level(DEBUG_LEVEL_NONE);
115         usb_device.cntep = 3;
116         usb_device.ep = eps;
117
118         eps[0].max_packet_size = USB_MAX_PACKET;
119         eps[1].max_packet_size = USB_MAX_PACKET;
120         eps[0].epnum = 0x01;
121         eps[1].epnum = 0x81;
122         eps[0].event_mask = 0x04;
123         eps[1].event_mask = 0x08;
124         eps[0].udev = &usb_device;
125         eps[1].udev = &usb_device;
126
127 //  usb_device.vendor_fnc=usb_loader;
128
129   usb_init(&usb_device);
130   usb_connect(&usb_device);
131
132         can_init();
133         usb_can_send=1;
134
135         /********************/
136         // start
137   timer_rx_off=timer_tx_off=timer_str=timer_configured=0;
138         while (1) {
139
140                 usb_check_events(&usb_device);
141                 usb_control_response(&usb_device);
142
143                 if ((usb_device.ep_events & MASK_EP1RX)&&(usb_can_send)) {  //EP1RX
144                         int size;
145                         uint8_t *data;
146                         uint8_t val;
147                         size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
148                         data=(uint8_t *)ep1_rx_buff;
149                         if (size==2){
150                                 if (((*data)&CAN_OP_MASK)==CAN_OP_READ){ // Get data from CAN device and return to caller
151                                         can_read((*data) & 0x7F,&val);
152                                         *(data+1)=val;
153                                         usb_udev_write_endpoint(&eps[1],(unsigned char *)data,size);
154                                         timer_rx_off=50;        //rosviceni diody pri prijmu
155                                         CLR_OUT_PIN(LED_PORT,LED2_BIT);
156                                         usb_can_send=0;
157                                 }
158                                 if (((*data)&CAN_OP_MASK)==CAN_OP_WRITE){ // Save data to CAN device
159                                         can_write((*data)&(~CAN_OP_MASK),data+1);
160                                         timer_tx_off=50;                //rozsviceni diod pri vysilani
161                                         CLR_OUT_PIN(LED_PORT,LED1_BIT);
162                                 }
163                         }
164                         usb_device.ep_events &= ~MASK_EP1RX;
165                 }
166
167                 if(usb_device.ep_events & MASK_EP1TX){
168                         usb_can_send=1;
169                         usb_device.ep_events &= ~MASK_EP1TX;
170                 }
171
172 #ifdef WATCHDOG_ENABLED
173                 watchdog_feed();
174 #endif /* WATCHDOG_ENABLED */
175
176                 /* 10ms timer */
177                 if (lt_10msec_expired(10))
178                         timer_10ms();
179         }
180
181         SET_OUT_PIN(LED_PORT,LED_GP);
182         SET_OUT_PIN(LED_PORT,LED_ERR);
183
184         /* unreachable code */
185 #ifdef SDCC
186         vec_jmp(0x0);  /* need to call a function from misc to correct linking */
187 #endif
188         return 0;
189 }