]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_usb.c
Some cleanup
[fpga/lx-cpu1/lx-rocon.git] / sw / app / rocon / appl_usb.c
1 #include <types.h>
2 #include <cpu_def.h>
3 #include <system_def.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <endian.h>
7 #include <usb/lpc.h>
8 #include <usb/usb.h>
9 #include <usb/usb_loader.h>
10 #include <usb/usbdebug.h>
11
12 #include "appl_defs.h"
13
14 #ifdef CONFIG_LIB_U2U_V2
15 #define NUM_ENDPOINTS 2
16 #define USB_VENDOR_ID      0x1669
17 #define USB_PRODUCT_ID     0x1023
18 #endif
19
20 #include "usb/usb_defs.h"
21
22 usb_device_t usb_device;
23 usb_ep_t eps[NUM_ENDPOINTS];
24
25 #define MASK_EP1RX  0x01
26 #define MASK_EP1TX  0x02
27
28 extern int usb_common_loader(usb_device_t *udev);
29
30 #ifdef CONFIG_LIB_U2U_V2
31
32 #include "ul_dcnv.h"
33 #include "u2u_vend.h"
34
35 const char ul_dcnv_idstr[] = ".mt u2u_v2int";
36
37 ul_dcnv_state_t ep1_dcnv_state;
38 unsigned char ep1_rx_buff[USB_MAX_PACKET];
39 unsigned char ep1_tx_buff[USB_MAX_PACKET];
40
41 int appl_usb_vendor(usb_device_t *udev)
42 {
43   int ret;
44   ret = usb_u2u_vendor4dcnv(&ep1_dcnv_state, udev);
45
46   if (ret)
47     return ret;
48
49   return usb_common_loader(udev);
50 }
51
52 int appl_usb_u2u_poll(void)
53 {
54   int active = 0;
55
56   if (usb_device.ep_events & MASK_EP1RX)
57     //EP1RX
58   {
59     int size;
60     size = usb_udev_read_endpoint(&eps[0], ep1_rx_buff, USB_MAX_PACKET);
61     ul_dcnv_send(&ep1_dcnv_state, ep1_rx_buff, size);
62     usb_device.ep_events &= ~MASK_EP1RX;
63     //timer_tx_off=5;     //rozsviceni diod pri vysilani
64     //CLR_OUT_PIN(LED_PORT,LED1_BIT);
65     active = 1;
66   }
67
68   if (usb_device.ep_events & MASK_EP1TX)
69   {
70     ep1_dcnv_state.rx_wait4host = 0;
71     usb_device.ep_events &= ~MASK_EP1TX;
72   }
73
74   if (!ep1_dcnv_state.rx_wait4host)
75     // EP1TX
76   {
77     int size = ul_dcnv_rec(&ep1_dcnv_state, ep1_tx_buff, USB_MAX_PACKET);
78
79     if (size)
80     {
81       usb_udev_write_endpoint(&eps[1], ep1_tx_buff, size);
82       ep1_dcnv_state.rx_wait4host = 1;
83     }
84     else
85     {
86       if (ul_dcnv_rec_start(&ep1_dcnv_state, ep1_tx_buff, 8) == 8)
87         //HEADER
88       {
89         usb_udev_write_endpoint(&eps[1], ep1_tx_buff, 8);
90         //timer_rx_off=5;        //rosviceni diody pri prijmu
91         //CLR_OUT_PIN(LED_PORT,LED2_BIT);
92         ep1_dcnv_state.rx_wait4host = 1;
93         active = 1;
94       }
95     }
96   }
97
98   return active;
99 }
100
101 #endif /*CONFIG_LIB_U2U_V2*/
102
103 int usb_app_fill_serial_number(uint32_t ul_sn)
104 {
105   char *p = usb_devdes_serial_number;
106   int len_max = sizeof(usb_devdes_serial_number);
107   char c;
108
109   while ((len_max -= 2) >= 2)
110   {
111     p += 2;
112     c = ((ul_sn >> (32 - 4)) & 0xf) + '0';
113     ul_sn <<= 4;
114
115     if (c > '9')
116       c += 'A' - '9' - 1;
117
118     *p = c;
119   }
120
121   return 0;
122 }
123
124 int usb_app_init(void)
125 {
126   memset(&usb_device, 0, sizeof(usb_device));
127   usb_device.id = 1;
128   usb_device.devdes_table = &usb_devdes_table;
129   usb_device.init = usb_lpc_init;
130   usb_debug_set_level(DEBUG_LEVEL_NONE);
131   usb_device.cntep = NUM_ENDPOINTS;
132   usb_device.ep = eps;
133
134 #ifndef CONFIG_LIB_U2U_V2
135   usb_device.vendor_fnc = usb_common_loader;
136 #else /*CONFIG_LIB_U2U_V2*/
137   eps[0].max_packet_size = USB_MAX_PACKET;
138   eps[1].max_packet_size = USB_MAX_PACKET;
139   eps[0].epnum = 0x01;
140   eps[1].epnum = 0x81;
141   eps[0].event_mask = 0x04;
142   eps[1].event_mask = 0x08;
143   eps[0].udev = &usb_device;
144   eps[1].udev = &usb_device;
145
146   usb_device.vendor_fnc = appl_usb_vendor;
147
148   ul_dcnv_init_by_name(&ep1_dcnv_state, NULL, 1);
149   ep1_dcnv_state.idstr = ul_dcnv_idstr;
150 #endif /*CONFIG_LIB_U2U_V2*/
151
152   usb_init(&usb_device);
153   usb_connect(&usb_device);
154   return 0;
155 }
156
157 int usb_app_poll(void)
158 {
159   int active = usb_loadder_active;
160   static char last_configuration = 0;
161   usb_loadder_active = 0;
162
163   usb_check_events(&usb_device);
164   usb_control_response(&usb_device);
165
166   if (usb_device.configuration != last_configuration)
167   {
168     last_configuration = usb_device.configuration;
169   }
170
171 #ifdef CONFIG_LIB_U2U_V2
172
173   if (ul_dcnv_is_open(&ep1_dcnv_state))
174   {
175     if (usb_device.configuration == 0)
176       ul_dcnv_close(&ep1_dcnv_state);
177     else
178       if (appl_usb_u2u_poll() > 0)
179         active = 1;
180   }
181
182 #endif /*CONFIG_LIB_U2U_V2*/
183
184   return active;
185 }
186
187 int usb_app_stop(void)
188 {
189   usb_disconnect(&usb_device);
190   return 0;
191 }