Super Vision / Mbed 2 deprecated sv_usb_firmware

Dependencies:   MODSERIAL USBDevice_for_Rev_C_HW mbed

Fork of mbed_sv_firmware_with_init by Bob Recny

Committer:
bob_tpc
Date:
Mon Jan 19 23:48:21 2015 +0000
Revision:
2:efaf8aee55df
Parent:
1:bd988d267998
Child:
3:e8cc286f9b2e
Commented out debug messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bob_tpc 0:8604e9cc07f2 1 #include "mbed.h"
bob_tpc 0:8604e9cc07f2 2 #include "USBSerial.h"
bob_tpc 0:8604e9cc07f2 3 #include "MODSERIAL.h"
bob_tpc 0:8604e9cc07f2 4 #include "InterruptIn.h"
bob_tpc 0:8604e9cc07f2 5
bob_tpc 0:8604e9cc07f2 6 // Constants
bob_tpc 0:8604e9cc07f2 7 #define LEDON 0 // Low active for LEDs - turns LED on
bob_tpc 0:8604e9cc07f2 8 #define LEDOFF 1 // Low active for LEDs - turns LED off
bob_tpc 0:8604e9cc07f2 9 #define TRUE 1
bob_tpc 0:8604e9cc07f2 10 #define FALSE 0
bob_tpc 0:8604e9cc07f2 11
bob_tpc 0:8604e9cc07f2 12
bob_tpc 0:8604e9cc07f2 13 // Error return values
bob_tpc 0:8604e9cc07f2 14 #define ERR_NONE 0 // Success
bob_tpc 1:bd988d267998 15 #define ERR_CDC_BAD_CMD 1 // First byte of PC to USB board needs to be 0xBB, 0xCC, 0xDD or 0xEE;
bob_tpc 1:bd988d267998 16 #define ERR_CDC_NO_TX_ENDMARK 2 // message for no endmark on message to PC
bob_tpc 0:8604e9cc07f2 17 #define ERR_UART_NOT_WRITEABLE 3 // UART has no buffer space
bob_tpc 1:bd988d267998 18 #define ERR_UART_NO_TX_ENDMARK 4 // message for UART has no 0x7E end-mark
bob_tpc 0:8604e9cc07f2 19 #define ERR_UART_NO_RX_ENDMARK 5 // message received from UART has no end-mark
bob_tpc 0:8604e9cc07f2 20
bob_tpc 0:8604e9cc07f2 21
bob_tpc 0:8604e9cc07f2 22 // I2C addresses
bob_tpc 0:8604e9cc07f2 23 #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits
bob_tpc 0:8604e9cc07f2 24 #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted
bob_tpc 0:8604e9cc07f2 25
bob_tpc 0:8604e9cc07f2 26 // UART-RFID baud rate
bob_tpc 0:8604e9cc07f2 27 #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps
bob_tpc 0:8604e9cc07f2 28
bob_tpc 0:8604e9cc07f2 29 // Peripherals
bob_tpc 0:8604e9cc07f2 30 USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers.
bob_tpc 0:8604e9cc07f2 31 MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board
bob_tpc 0:8604e9cc07f2 32 I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above)
bob_tpc 0:8604e9cc07f2 33
bob_tpc 0:8604e9cc07f2 34 // GPIO signals
bob_tpc 0:8604e9cc07f2 35 DigitalOut led_err(PTC1); // Red LED shows error condition (active low)
bob_tpc 0:8604e9cc07f2 36 DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low)
bob_tpc 0:8604e9cc07f2 37 DigitalOut rfid_int(PTD4); // RFID FE power control (active high)
bob_tpc 0:8604e9cc07f2 38 DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high)
bob_tpc 0:8604e9cc07f2 39 DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high)
bob_tpc 0:8604e9cc07f2 40 DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 0:8604e9cc07f2 41 DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low)
bob_tpc 0:8604e9cc07f2 42 InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low)
bob_tpc 0:8604e9cc07f2 43
bob_tpc 0:8604e9cc07f2 44 // buffers & variables
bob_tpc 0:8604e9cc07f2 45 uint8_t cdc_buffer_rx[32]; // buffers for cdc (USB-Serial port on PC)
bob_tpc 0:8604e9cc07f2 46 uint8_t cdc_buffer_tx[32];
bob_tpc 0:8604e9cc07f2 47 uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board)
bob_tpc 0:8604e9cc07f2 48 uint8_t uart_buffer_tx[32];
bob_tpc 0:8604e9cc07f2 49 int i, j; // index variables
bob_tpc 0:8604e9cc07f2 50 int status = 0x00; // return value
bob_tpc 0:8604e9cc07f2 51
bob_tpc 0:8604e9cc07f2 52 int prox_irq(void)
bob_tpc 0:8604e9cc07f2 53 {
bob_tpc 0:8604e9cc07f2 54 return 0;
bob_tpc 0:8604e9cc07f2 55 }
bob_tpc 0:8604e9cc07f2 56
bob_tpc 0:8604e9cc07f2 57 int init_periph(void)
bob_tpc 0:8604e9cc07f2 58 {
bob_tpc 0:8604e9cc07f2 59 // Set up peripherals
bob_tpc 0:8604e9cc07f2 60 // RFID
bob_tpc 0:8604e9cc07f2 61 uart.baud(RFIDBAUD); // RFID-FE baud rate
bob_tpc 0:8604e9cc07f2 62
bob_tpc 0:8604e9cc07f2 63 rfid_int = 0; // RFID FE power control (active high)
bob_tpc 0:8604e9cc07f2 64 rfid_isp = 0; // RFID FE In-System Programming (active high)
bob_tpc 0:8604e9cc07f2 65 rfid_rst = 1; // RFID FE Reset (active high)
bob_tpc 0:8604e9cc07f2 66 rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 2:efaf8aee55df 67 wait(0.25); // wait 250ms before...
bob_tpc 0:8604e9cc07f2 68 rfid_rst = 0; // ... taking RFID out of reset
bob_tpc 0:8604e9cc07f2 69
bob_tpc 0:8604e9cc07f2 70 // Prox
bob_tpc 2:efaf8aee55df 71 i2c.frequency(400000);
bob_tpc 2:efaf8aee55df 72 prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU
bob_tpc 2:efaf8aee55df 73
bob_tpc 2:efaf8aee55df 74
bob_tpc 0:8604e9cc07f2 75 return 0;
bob_tpc 0:8604e9cc07f2 76 }
bob_tpc 0:8604e9cc07f2 77
bob_tpc 0:8604e9cc07f2 78 int rfid_msg(void)
bob_tpc 0:8604e9cc07f2 79 {
bob_tpc 0:8604e9cc07f2 80 bool end_mark = FALSE;
bob_tpc 0:8604e9cc07f2 81 int i;
bob_tpc 0:8604e9cc07f2 82 uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start
bob_tpc 0:8604e9cc07f2 83
bob_tpc 0:8604e9cc07f2 84 uart.txBufferFlush(); // clear out UART buffers
bob_tpc 0:8604e9cc07f2 85 uart.rxBufferFlush();
bob_tpc 0:8604e9cc07f2 86
bob_tpc 0:8604e9cc07f2 87 for (int i = 0; i < sizeof(uart_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 88 {
bob_tpc 0:8604e9cc07f2 89 if (!uart.writeable()) return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error
bob_tpc 0:8604e9cc07f2 90 uart.putc(uart_buffer_tx[i]); // send uart message
bob_tpc 0:8604e9cc07f2 91
bob_tpc 0:8604e9cc07f2 92 if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 0:8604e9cc07f2 93 {
bob_tpc 0:8604e9cc07f2 94 crcCount = 2; // two more bytes for CRC
bob_tpc 0:8604e9cc07f2 95 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 96 }
bob_tpc 0:8604e9cc07f2 97 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 98 {
bob_tpc 0:8604e9cc07f2 99 if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 0:8604e9cc07f2 100 break;
bob_tpc 0:8604e9cc07f2 101 }
bob_tpc 0:8604e9cc07f2 102 }
bob_tpc 0:8604e9cc07f2 103
bob_tpc 0:8604e9cc07f2 104 end_mark = FALSE;
bob_tpc 0:8604e9cc07f2 105 wait(0.5);
bob_tpc 0:8604e9cc07f2 106 while(!uart.readable()) printf("."); // wait for data from rfid
bob_tpc 0:8604e9cc07f2 107 crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start
bob_tpc 0:8604e9cc07f2 108 for (i = 0; i < sizeof(uart_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 109 {
bob_tpc 0:8604e9cc07f2 110 uart_buffer_rx[i] = uart.getc(); // read a character
bob_tpc 0:8604e9cc07f2 111 // cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug
bob_tpc 0:8604e9cc07f2 112
bob_tpc 0:8604e9cc07f2 113 if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message
bob_tpc 0:8604e9cc07f2 114 {
bob_tpc 0:8604e9cc07f2 115 crcCount = 2; // two more bytes for crc
bob_tpc 0:8604e9cc07f2 116 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 117 }
bob_tpc 0:8604e9cc07f2 118 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 119 {
bob_tpc 0:8604e9cc07f2 120 if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK;
bob_tpc 0:8604e9cc07f2 121 break;
bob_tpc 0:8604e9cc07f2 122 }
bob_tpc 0:8604e9cc07f2 123 }
bob_tpc 0:8604e9cc07f2 124 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 125 }
bob_tpc 0:8604e9cc07f2 126
bob_tpc 0:8604e9cc07f2 127
bob_tpc 0:8604e9cc07f2 128 int prox_msg()
bob_tpc 0:8604e9cc07f2 129 {
bob_tpc 0:8604e9cc07f2 130 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 131 }
bob_tpc 0:8604e9cc07f2 132
bob_tpc 0:8604e9cc07f2 133 int gpio_msg()
bob_tpc 0:8604e9cc07f2 134 {
bob_tpc 0:8604e9cc07f2 135 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 136 }
bob_tpc 0:8604e9cc07f2 137
bob_tpc 1:bd988d267998 138 int eeprom_msg() // eeprom to be implemented along with next hardware prototype
bob_tpc 0:8604e9cc07f2 139 {
bob_tpc 0:8604e9cc07f2 140 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 141 }
bob_tpc 0:8604e9cc07f2 142
bob_tpc 0:8604e9cc07f2 143 int main()
bob_tpc 0:8604e9cc07f2 144 {
bob_tpc 0:8604e9cc07f2 145 // initialize everything
bob_tpc 0:8604e9cc07f2 146
bob_tpc 0:8604e9cc07f2 147 wait(3.0);
bob_tpc 0:8604e9cc07f2 148 init_periph();
bob_tpc 0:8604e9cc07f2 149
bob_tpc 0:8604e9cc07f2 150 cdc.printf("Starting...\n\r");
bob_tpc 0:8604e9cc07f2 151
bob_tpc 2:efaf8aee55df 152 while(!cdc.readable()); // spin here until a message comes in from the host PC
bob_tpc 0:8604e9cc07f2 153 bool end_mark = FALSE;
bob_tpc 2:efaf8aee55df 154 uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start
bob_tpc 0:8604e9cc07f2 155 cdc.printf("\n\rCDC Input: ");
bob_tpc 0:8604e9cc07f2 156 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 157 {
bob_tpc 2:efaf8aee55df 158 cdc_buffer_rx[i] = cdc.getc(); // read data from USB side
bob_tpc 0:8604e9cc07f2 159
bob_tpc 2:efaf8aee55df 160 //cdc.printf("%X, ",cdc_buffer_rx[i]); // debug
bob_tpc 0:8604e9cc07f2 161
bob_tpc 2:efaf8aee55df 162 if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 0:8604e9cc07f2 163 {
bob_tpc 2:efaf8aee55df 164 crcCount = 2; // two more bytes for CRC
bob_tpc 2:efaf8aee55df 165 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 166 }
bob_tpc 2:efaf8aee55df 167 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 168 {
bob_tpc 2:efaf8aee55df 169 if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 0:8604e9cc07f2 170 break;
bob_tpc 0:8604e9cc07f2 171 }
bob_tpc 0:8604e9cc07f2 172 }
bob_tpc 0:8604e9cc07f2 173
bob_tpc 0:8604e9cc07f2 174 switch(cdc_buffer_rx[0])
bob_tpc 0:8604e9cc07f2 175 {
bob_tpc 2:efaf8aee55df 176 case 0xBB: // RFID-FE
bob_tpc 0:8604e9cc07f2 177 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 178 {
bob_tpc 2:efaf8aee55df 179 uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID
bob_tpc 0:8604e9cc07f2 180 }
bob_tpc 0:8604e9cc07f2 181
bob_tpc 2:efaf8aee55df 182 status = rfid_msg(); // send buffer to RFID and get response according to RFID board
bob_tpc 0:8604e9cc07f2 183
bob_tpc 0:8604e9cc07f2 184 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 185 {
bob_tpc 2:efaf8aee55df 186 cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer
bob_tpc 0:8604e9cc07f2 187 }
bob_tpc 0:8604e9cc07f2 188
bob_tpc 2:efaf8aee55df 189 //cdc.printf("\n\rRFID Response: "); // debug
bob_tpc 0:8604e9cc07f2 190
bob_tpc 0:8604e9cc07f2 191 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 192 {
bob_tpc 2:efaf8aee55df 193 //cdc.printf("%x, ", cdc_buffer_tx[i]); // debug send message back to PC
bob_tpc 0:8604e9cc07f2 194
bob_tpc 2:efaf8aee55df 195 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 0:8604e9cc07f2 196 {
bob_tpc 2:efaf8aee55df 197 crcCount = 2; // two more bytes for CRC
bob_tpc 2:efaf8aee55df 198 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 199 }
bob_tpc 2:efaf8aee55df 200 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 201 {
bob_tpc 0:8604e9cc07f2 202 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 0:8604e9cc07f2 203 break;
bob_tpc 0:8604e9cc07f2 204 }
bob_tpc 0:8604e9cc07f2 205 }
bob_tpc 0:8604e9cc07f2 206 break;
bob_tpc 2:efaf8aee55df 207 case 0xCC: // Proximity Sensor
bob_tpc 0:8604e9cc07f2 208 prox_msg();
bob_tpc 0:8604e9cc07f2 209 break;
bob_tpc 2:efaf8aee55df 210 case 0xDD: // GPIO (LEDs and RFID-FE control
bob_tpc 0:8604e9cc07f2 211 gpio_msg();
bob_tpc 0:8604e9cc07f2 212 break;
bob_tpc 2:efaf8aee55df 213 case 0xEE: // Read/write EEPROM
bob_tpc 0:8604e9cc07f2 214 eeprom_msg();
bob_tpc 0:8604e9cc07f2 215 break;
bob_tpc 0:8604e9cc07f2 216 default:
bob_tpc 0:8604e9cc07f2 217 return ERR_CDC_BAD_CMD;
bob_tpc 0:8604e9cc07f2 218 }
bob_tpc 0:8604e9cc07f2 219 }