]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_usb.c
RoCoN: reserve USB product ID for RoCoN from PiKRON acquired/bought pool.
[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   if(ret)
46     return ret;
47   return usb_common_loader(udev);
48 }
49
50 int appl_usb_u2u_poll(void)
51 {
52   int active=0;
53
54   if (usb_device.ep_events & MASK_EP1RX) {  //EP1RX
55     int size;
56     size=usb_udev_read_endpoint(&eps[0],ep1_rx_buff,USB_MAX_PACKET);
57     ul_dcnv_send(&ep1_dcnv_state,ep1_rx_buff,size);
58     usb_device.ep_events &= ~MASK_EP1RX;
59     //timer_tx_off=5;           //rozsviceni diod pri vysilani 
60     //CLR_OUT_PIN(LED_PORT,LED1_BIT);
61     active=1;
62   }
63
64   if(usb_device.ep_events & MASK_EP1TX){
65     ep1_dcnv_state.rx_wait4host=0;
66     usb_device.ep_events &= ~MASK_EP1TX;
67   }
68
69   if (!ep1_dcnv_state.rx_wait4host) { // EP1TX
70     int size=ul_dcnv_rec(&ep1_dcnv_state, ep1_tx_buff, USB_MAX_PACKET);
71     if (size){
72       usb_udev_write_endpoint(&eps[1],ep1_tx_buff,size);
73       ep1_dcnv_state.rx_wait4host=1;
74     } else {
75       if (ul_dcnv_rec_start(&ep1_dcnv_state, ep1_tx_buff,8)==8) { //HEADER
76         usb_udev_write_endpoint(&eps[1],ep1_tx_buff,8);
77         //timer_rx_off=5;        //rosviceni diody pri prijmu 
78         //CLR_OUT_PIN(LED_PORT,LED2_BIT);
79         ep1_dcnv_state.rx_wait4host=1;
80         active=1;
81       }
82     }
83   }
84   return active;
85 }
86
87 #endif /*CONFIG_LIB_U2U_V2*/
88
89 int usb_app_fill_serial_number(uint32_t ul_sn)
90 {
91   char *p=usb_devdes_serial_number;
92   int len_max=sizeof(usb_devdes_serial_number);
93   char c;
94
95   while((len_max-=2)>=2) {
96     p+=2;
97     c=((ul_sn>>(32-4))&0xf)+'0';
98     ul_sn<<=4;
99     if(c>'9') c+='A'-'9'-1;
100     *p=c;
101   }
102   return 0;
103 }
104
105 int usb_app_init(void)
106 {
107   memset( &usb_device, 0, sizeof( usb_device));
108   usb_device.id = 1;
109   usb_device.devdes_table = &usb_devdes_table;
110   usb_device.init = usb_lpc_init;
111   usb_debug_set_level(DEBUG_LEVEL_NONE);
112   usb_device.cntep = NUM_ENDPOINTS;
113   usb_device.ep = eps;
114
115  #ifndef CONFIG_LIB_U2U_V2
116   usb_device.vendor_fnc=usb_common_loader;
117  #else /*CONFIG_LIB_U2U_V2*/
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=appl_usb_vendor;
128
129   ul_dcnv_init_by_name(&ep1_dcnv_state,NULL,1);
130   ep1_dcnv_state.idstr=ul_dcnv_idstr;
131  #endif /*CONFIG_LIB_U2U_V2*/
132
133   usb_init(&usb_device);
134   usb_connect(&usb_device);
135   return 0;
136 }
137
138 int usb_app_poll(void)
139 {
140   int active = usb_loadder_active;
141   static char last_configuration = 0;
142   usb_loadder_active = 0;
143
144   usb_check_events(&usb_device);
145   usb_control_response(&usb_device);
146
147   if(usb_device.configuration != last_configuration) {
148     last_configuration = usb_device.configuration;
149   }
150
151  #ifdef CONFIG_LIB_U2U_V2
152   if (ul_dcnv_is_open(&ep1_dcnv_state)) {
153    if (usb_device.configuration == 0)
154      ul_dcnv_close(&ep1_dcnv_state);
155    else
156      if(appl_usb_u2u_poll()>0)
157        active=1;
158   }
159  #endif /*CONFIG_LIB_U2U_V2*/
160
161   return active;
162 }
163
164 int usb_app_stop(void)
165 {
166   usb_disconnect(&usb_device);
167   return 0;
168 }