| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
#include "usrp_common.h" |
|---|
| 22 |
#include "usrp_commands.h" |
|---|
| 23 |
#include "fpga.h" |
|---|
| 24 |
#include "usrp_gpif_inline.h" |
|---|
| 25 |
#include "timer.h" |
|---|
| 26 |
#include "i2c.h" |
|---|
| 27 |
#include "isr.h" |
|---|
| 28 |
#include "usb_common.h" |
|---|
| 29 |
#include "fx2utils.h" |
|---|
| 30 |
#include "usrp_globals.h" |
|---|
| 31 |
#include "usrp_i2c_addr.h" |
|---|
| 32 |
#include <string.h> |
|---|
| 33 |
#include "spi.h" |
|---|
| 34 |
#include "eeprom_io.h" |
|---|
| 35 |
#include "usb_descriptors.h" |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
#define HW_REV_OFFSET 5 |
|---|
| 41 |
#define SERIAL_NO_OFFSET 248 |
|---|
| 42 |
#define SERIAL_NO_LEN 8 |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
#define bRequestType SETUPDAT[0] |
|---|
| 46 |
#define bRequest SETUPDAT[1] |
|---|
| 47 |
#define wValueL SETUPDAT[2] |
|---|
| 48 |
#define wValueH SETUPDAT[3] |
|---|
| 49 |
#define wIndexL SETUPDAT[4] |
|---|
| 50 |
#define wIndexH SETUPDAT[5] |
|---|
| 51 |
#define wLengthL SETUPDAT[6] |
|---|
| 52 |
#define wLengthH SETUPDAT[7] |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
unsigned char g_tx_enable = 0; |
|---|
| 56 |
unsigned char g_rx_enable = 0; |
|---|
| 57 |
unsigned char g_rx_overrun = 0; |
|---|
| 58 |
unsigned char g_tx_underrun = 0; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
#define USRP_HASH_SIZE 16 |
|---|
| 65 |
xdata at USRP_HASH_SLOT_1_ADDR unsigned char hash1[USRP_HASH_SIZE]; |
|---|
| 66 |
|
|---|
| 67 |
static void |
|---|
| 68 |
get_ep0_data (void) |
|---|
| 69 |
{ |
|---|
| 70 |
EP0BCL = 0; |
|---|
| 71 |
|
|---|
| 72 |
while (EP0CS & bmEPBUSY) |
|---|
| 73 |
; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
unsigned char |
|---|
| 81 |
app_vendor_cmd (void) |
|---|
| 82 |
{ |
|---|
| 83 |
if (bRequestType == VRT_VENDOR_IN){ |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
switch (bRequest){ |
|---|
| 90 |
|
|---|
| 91 |
case VRQ_GET_STATUS: |
|---|
| 92 |
switch (wIndexL){ |
|---|
| 93 |
|
|---|
| 94 |
case GS_TX_UNDERRUN: |
|---|
| 95 |
EP0BUF[0] = g_tx_underrun; |
|---|
| 96 |
g_tx_underrun = 0; |
|---|
| 97 |
EP0BCH = 0; |
|---|
| 98 |
EP0BCL = 1; |
|---|
| 99 |
break; |
|---|
| 100 |
|
|---|
| 101 |
case GS_RX_OVERRUN: |
|---|
| 102 |
EP0BUF[0] = g_rx_overrun; |
|---|
| 103 |
g_rx_overrun = 0; |
|---|
| 104 |
EP0BCH = 0; |
|---|
| 105 |
EP0BCL = 1; |
|---|
| 106 |
break; |
|---|
| 107 |
|
|---|
| 108 |
default: |
|---|
| 109 |
return 0; |
|---|
| 110 |
} |
|---|
| 111 |
break; |
|---|
| 112 |
|
|---|
| 113 |
case VRQ_I2C_READ: |
|---|
| 114 |
if (!i2c_read (wValueL, EP0BUF, wLengthL)) |
|---|
| 115 |
return 0; |
|---|
| 116 |
|
|---|
| 117 |
EP0BCH = 0; |
|---|
| 118 |
EP0BCL = wLengthL; |
|---|
| 119 |
break; |
|---|
| 120 |
|
|---|
| 121 |
case VRQ_SPI_READ: |
|---|
| 122 |
if (!spi_read (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, wLengthL)) |
|---|
| 123 |
return 0; |
|---|
| 124 |
|
|---|
| 125 |
EP0BCH = 0; |
|---|
| 126 |
EP0BCL = wLengthL; |
|---|
| 127 |
break; |
|---|
| 128 |
|
|---|
| 129 |
default: |
|---|
| 130 |
return 0; |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
else if (bRequestType == VRT_VENDOR_OUT){ |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
switch (bRequest){ |
|---|
| 141 |
|
|---|
| 142 |
case VRQ_SET_LED: |
|---|
| 143 |
switch (wIndexL){ |
|---|
| 144 |
case 0: |
|---|
| 145 |
set_led_0 (wValueL); |
|---|
| 146 |
break; |
|---|
| 147 |
|
|---|
| 148 |
case 1: |
|---|
| 149 |
set_led_1 (wValueL); |
|---|
| 150 |
break; |
|---|
| 151 |
|
|---|
| 152 |
default: |
|---|
| 153 |
return 0; |
|---|
| 154 |
} |
|---|
| 155 |
break; |
|---|
| 156 |
|
|---|
| 157 |
case VRQ_FPGA_LOAD: |
|---|
| 158 |
switch (wIndexL){ |
|---|
| 159 |
case FL_BEGIN: |
|---|
| 160 |
return fpga_load_begin (); |
|---|
| 161 |
|
|---|
| 162 |
case FL_XFER: |
|---|
| 163 |
get_ep0_data (); |
|---|
| 164 |
return fpga_load_xfer (EP0BUF, EP0BCL); |
|---|
| 165 |
|
|---|
| 166 |
case FL_END: |
|---|
| 167 |
return fpga_load_end (); |
|---|
| 168 |
|
|---|
| 169 |
default: |
|---|
| 170 |
return 0; |
|---|
| 171 |
} |
|---|
| 172 |
break; |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
case VRQ_FPGA_SET_RESET: |
|---|
| 176 |
fpga_set_reset (wValueL); |
|---|
| 177 |
break; |
|---|
| 178 |
|
|---|
| 179 |
case VRQ_FPGA_SET_TX_ENABLE: |
|---|
| 180 |
fpga_set_tx_enable (wValueL); |
|---|
| 181 |
break; |
|---|
| 182 |
|
|---|
| 183 |
case VRQ_FPGA_SET_RX_ENABLE: |
|---|
| 184 |
fpga_set_rx_enable (wValueL); |
|---|
| 185 |
break; |
|---|
| 186 |
|
|---|
| 187 |
case VRQ_FPGA_SET_TX_RESET: |
|---|
| 188 |
fpga_set_tx_reset (wValueL); |
|---|
| 189 |
break; |
|---|
| 190 |
|
|---|
| 191 |
case VRQ_FPGA_SET_RX_RESET: |
|---|
| 192 |
fpga_set_rx_reset (wValueL); |
|---|
| 193 |
break; |
|---|
| 194 |
|
|---|
| 195 |
case VRQ_I2C_WRITE: |
|---|
| 196 |
get_ep0_data (); |
|---|
| 197 |
if (!i2c_write (wValueL, EP0BUF, EP0BCL)) |
|---|
| 198 |
return 0; |
|---|
| 199 |
break; |
|---|
| 200 |
|
|---|
| 201 |
case VRQ_SPI_WRITE: |
|---|
| 202 |
get_ep0_data (); |
|---|
| 203 |
if (!spi_write (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, EP0BCL)) |
|---|
| 204 |
return 0; |
|---|
| 205 |
break; |
|---|
| 206 |
|
|---|
| 207 |
default: |
|---|
| 208 |
return 0; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
} |
|---|
| 212 |
else |
|---|
| 213 |
return 0; |
|---|
| 214 |
|
|---|
| 215 |
return 1; |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
static void |
|---|
| 221 |
main_loop (void) |
|---|
| 222 |
{ |
|---|
| 223 |
setup_flowstate_common (); |
|---|
| 224 |
|
|---|
| 225 |
while (1){ |
|---|
| 226 |
|
|---|
| 227 |
if (usb_setup_packet_avail ()) |
|---|
| 228 |
usb_handle_setup_packet (); |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
if (GPIFTRIG & bmGPIF_IDLE){ |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
if (UC_BOARD_HAS_FPGA && (USRP_PA & (bmPA_TX_UNDERRUN | bmPA_RX_OVERRUN))){ |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
if (USRP_PA & bmPA_TX_UNDERRUN) |
|---|
| 241 |
g_tx_underrun = 1; |
|---|
| 242 |
|
|---|
| 243 |
if (USRP_PA & bmPA_RX_OVERRUN) |
|---|
| 244 |
g_rx_overrun = 1; |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
fpga_clear_flags (); |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
if (g_tx_enable && !(EP24FIFOFLGS & 0x02)){ |
|---|
| 254 |
|
|---|
| 255 |
if (fpga_has_room_for_packet ()){ |
|---|
| 256 |
|
|---|
| 257 |
GPIFTCB1 = 0x01; SYNCDELAY; |
|---|
| 258 |
GPIFTCB0 = 0x00; SYNCDELAY; |
|---|
| 259 |
|
|---|
| 260 |
setup_flowstate_write (); |
|---|
| 261 |
|
|---|
| 262 |
SYNCDELAY; |
|---|
| 263 |
GPIFTRIG = bmGPIF_EP2_START | bmGPIF_WRITE; |
|---|
| 264 |
SYNCDELAY; |
|---|
| 265 |
|
|---|
| 266 |
while (!(GPIFTRIG & bmGPIF_IDLE)){ |
|---|
| 267 |
|
|---|
| 268 |
} |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
if (g_rx_enable && !(EP6CS & bmEPFULL)){ |
|---|
| 276 |
|
|---|
| 277 |
if (fpga_has_packet_avail ()){ |
|---|
| 278 |
|
|---|
| 279 |
GPIFTCB1 = 0x01; SYNCDELAY; |
|---|
| 280 |
GPIFTCB0 = 0x00; SYNCDELAY; |
|---|
| 281 |
|
|---|
| 282 |
setup_flowstate_read (); |
|---|
| 283 |
|
|---|
| 284 |
SYNCDELAY; |
|---|
| 285 |
GPIFTRIG = bmGPIF_EP6_START | bmGPIF_READ; |
|---|
| 286 |
SYNCDELAY; |
|---|
| 287 |
|
|---|
| 288 |
while (!(GPIFTRIG & bmGPIF_IDLE)){ |
|---|
| 289 |
|
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
SYNCDELAY; |
|---|
| 293 |
INPKTEND = 6; |
|---|
| 294 |
} |
|---|
| 295 |
} |
|---|
| 296 |
} |
|---|
| 297 |
} |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
void |
|---|
| 307 |
isr_tick (void) interrupt |
|---|
| 308 |
{ |
|---|
| 309 |
static unsigned char count = 1; |
|---|
| 310 |
|
|---|
| 311 |
if (--count == 0){ |
|---|
| 312 |
count = 50; |
|---|
| 313 |
USRP_LED_REG ^= bmLED0; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
clear_timer_irq (); |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
void |
|---|
| 324 |
patch_usb_descriptors(void) |
|---|
| 325 |
{ |
|---|
| 326 |
static xdata unsigned char hw_rev; |
|---|
| 327 |
static xdata unsigned char serial_no[8]; |
|---|
| 328 |
unsigned char i; |
|---|
| 329 |
|
|---|
| 330 |
eeprom_read(I2C_ADDR_BOOT, HW_REV_OFFSET, &hw_rev, 1); |
|---|
| 331 |
usb_desc_hw_rev_binary_patch_location_0[0] = hw_rev; |
|---|
| 332 |
usb_desc_hw_rev_binary_patch_location_1[0] = hw_rev; |
|---|
| 333 |
usb_desc_hw_rev_ascii_patch_location_0[0] = hw_rev + '0'; |
|---|
| 334 |
|
|---|
| 335 |
eeprom_read(I2C_ADDR_BOOT, SERIAL_NO_OFFSET, serial_no, SERIAL_NO_LEN); |
|---|
| 336 |
|
|---|
| 337 |
for (i = 0; i < SERIAL_NO_LEN; i++){ |
|---|
| 338 |
unsigned char ch = serial_no[i]; |
|---|
| 339 |
if (ch == 0xff) |
|---|
| 340 |
ch = '0'; |
|---|
| 341 |
usb_desc_serial_number_ascii[i << 1] = ch; |
|---|
| 342 |
} |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
void |
|---|
| 346 |
main (void) |
|---|
| 347 |
{ |
|---|
| 348 |
#if 0 |
|---|
| 349 |
g_rx_enable = 0; |
|---|
| 350 |
g_tx_enable = 0; |
|---|
| 351 |
g_rx_overrun = 0; |
|---|
| 352 |
g_tx_underrun = 0; |
|---|
| 353 |
#endif |
|---|
| 354 |
|
|---|
| 355 |
memset (hash1, 0, USRP_HASH_SIZE); |
|---|
| 356 |
|
|---|
| 357 |
init_usrp (); |
|---|
| 358 |
init_gpif (); |
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
IFCONFIG |= bmGSTATE; |
|---|
| 362 |
|
|---|
| 363 |
set_led_0 (0); |
|---|
| 364 |
set_led_1 (0); |
|---|
| 365 |
|
|---|
| 366 |
EA = 0; |
|---|
| 367 |
|
|---|
| 368 |
patch_usb_descriptors(); |
|---|
| 369 |
|
|---|
| 370 |
setup_autovectors (); |
|---|
| 371 |
usb_install_handlers (); |
|---|
| 372 |
hook_timer_tick ((unsigned short) isr_tick); |
|---|
| 373 |
|
|---|
| 374 |
EIEX4 = 1; |
|---|
| 375 |
EA = 1; |
|---|
| 376 |
|
|---|
| 377 |
fx2_renumerate (); |
|---|
| 378 |
|
|---|
| 379 |
main_loop (); |
|---|
| 380 |
} |
|---|