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:
Thu Jan 22 23:04:42 2015 +0000
Revision:
9:046247707ffb
Parent:
8:3313aa7f9082
Child:
10:55e35536d493
Functionally complete.  Still needs descriptions for each routine.

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
bob_tpc 0:8604e9cc07f2 5 // Constants
bob_tpc 0:8604e9cc07f2 6 #define LEDON 0 // Low active for LEDs - turns LED on
bob_tpc 0:8604e9cc07f2 7 #define LEDOFF 1 // Low active for LEDs - turns LED off
bob_tpc 0:8604e9cc07f2 8 #define TRUE 1
bob_tpc 0:8604e9cc07f2 9 #define FALSE 0
bob_tpc 0:8604e9cc07f2 10
bob_tpc 0:8604e9cc07f2 11
bob_tpc 0:8604e9cc07f2 12 // Error return values
bob_tpc 0:8604e9cc07f2 13 #define ERR_NONE 0 // Success
bob_tpc 1:bd988d267998 14 #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 15 #define ERR_CDC_NO_TX_ENDMARK 2 // message for no endmark on message to PC
bob_tpc 0:8604e9cc07f2 16 #define ERR_UART_NOT_WRITEABLE 3 // UART has no buffer space
bob_tpc 1:bd988d267998 17 #define ERR_UART_NO_TX_ENDMARK 4 // message for UART has no 0x7E end-mark
bob_tpc 0:8604e9cc07f2 18 #define ERR_UART_NO_RX_ENDMARK 5 // message received from UART has no end-mark
bob_tpc 4:13e3e375c0d3 19 #define ERR_I2C_NOT_WRITEABLE 6 // UART has no buffer space
bob_tpc 4:13e3e375c0d3 20 #define ERR_I2C_NO_TX_ENDMARK 7 // message for UART has no 0x7E end-mark
bob_tpc 4:13e3e375c0d3 21 #define ERR_I2C_NO_RX_ENDMARK 8 // message received from UART has no end-mark
bob_tpc 4:13e3e375c0d3 22 #define ERR_NOT_IMPLEMENTED 255 // method has not yet been implemented
bob_tpc 0:8604e9cc07f2 23
bob_tpc 0:8604e9cc07f2 24
bob_tpc 0:8604e9cc07f2 25 // I2C addresses
bob_tpc 0:8604e9cc07f2 26 #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits
bob_tpc 0:8604e9cc07f2 27 #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted
bob_tpc 6:2941452a0e6d 28 #define I2CRATE 400000 // I2C speed
bob_tpc 0:8604e9cc07f2 29
bob_tpc 0:8604e9cc07f2 30 // UART-RFID baud rate
bob_tpc 0:8604e9cc07f2 31 #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps
bob_tpc 0:8604e9cc07f2 32
bob_tpc 0:8604e9cc07f2 33 // Peripherals
bob_tpc 0:8604e9cc07f2 34 USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers.
bob_tpc 0:8604e9cc07f2 35 MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board
bob_tpc 0:8604e9cc07f2 36 I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above)
bob_tpc 0:8604e9cc07f2 37
bob_tpc 0:8604e9cc07f2 38 // GPIO signals
bob_tpc 0:8604e9cc07f2 39 DigitalOut led_err(PTC1); // Red LED shows error condition (active low)
bob_tpc 0:8604e9cc07f2 40 DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low)
bob_tpc 0:8604e9cc07f2 41 DigitalOut rfid_int(PTD4); // RFID FE power control (active high)
bob_tpc 0:8604e9cc07f2 42 DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high)
bob_tpc 0:8604e9cc07f2 43 DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high)
bob_tpc 0:8604e9cc07f2 44 DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 0:8604e9cc07f2 45 DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low)
bob_tpc 0:8604e9cc07f2 46 InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low)
bob_tpc 0:8604e9cc07f2 47
bob_tpc 0:8604e9cc07f2 48 // buffers & variables
bob_tpc 5:e77529f7ede3 49 uint8_t gpio_values = 0x00; // register to read GPIO values
bob_tpc 5:e77529f7ede3 50
bob_tpc 6:2941452a0e6d 51 uint8_t cdc_buffer_rx[32] = {0xBB, 0x00, 0x03, 0x00, 0x01, 0x02, 0x7E, 0x2E, 0xC9}; // buffers for cdc (USB-Serial port on PC)
bob_tpc 0:8604e9cc07f2 52 uint8_t cdc_buffer_tx[32];
bob_tpc 0:8604e9cc07f2 53 uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board)
bob_tpc 0:8604e9cc07f2 54 uint8_t uart_buffer_tx[32];
bob_tpc 5:e77529f7ede3 55 uint8_t gpio_buffer[32]; // buffer for GPIO messages
bob_tpc 5:e77529f7ede3 56 char i2c_buffer[32]; // buffer for I2C devices - Proximity sensor and EEPROM - up to 256 bytes data payload for EEPROM, up to 4 for proximity
bob_tpc 0:8604e9cc07f2 57 int i, j; // index variables
bob_tpc 0:8604e9cc07f2 58 int status = 0x00; // return value
bob_tpc 0:8604e9cc07f2 59
bob_tpc 9:046247707ffb 60 uint8_t led_com_state = LEDOFF;
bob_tpc 9:046247707ffb 61 uint8_t prox_irq_state = 0; // interrupt state passed from service routine
bob_tpc 9:046247707ffb 62
bob_tpc 9:046247707ffb 63 void prox_irq(void)
bob_tpc 0:8604e9cc07f2 64 {
bob_tpc 9:046247707ffb 65 prox_irq_state = 1;
bob_tpc 0:8604e9cc07f2 66 }
bob_tpc 0:8604e9cc07f2 67
bob_tpc 0:8604e9cc07f2 68 int init_periph(void)
bob_tpc 0:8604e9cc07f2 69 {
bob_tpc 0:8604e9cc07f2 70 // Set up peripherals
bob_tpc 0:8604e9cc07f2 71 // RFID
bob_tpc 5:e77529f7ede3 72 uart.baud(RFIDBAUD); // RFID-FE baud rate
bob_tpc 0:8604e9cc07f2 73
bob_tpc 5:e77529f7ede3 74 rfid_int = 0; // RFID FE power control (active high)
bob_tpc 5:e77529f7ede3 75 rfid_isp = 0; // RFID FE In-System Programming (active high)
bob_tpc 5:e77529f7ede3 76 rfid_rst = 1; // RFID FE Reset (active high)
bob_tpc 5:e77529f7ede3 77 rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 5:e77529f7ede3 78 wait(0.25); // wait 250ms before...
bob_tpc 5:e77529f7ede3 79 rfid_rst = 0; // ... taking RFID out of reset
bob_tpc 0:8604e9cc07f2 80
bob_tpc 5:e77529f7ede3 81 // Prox & EEPROM
bob_tpc 6:2941452a0e6d 82 i2c.frequency(I2CRATE); // I2C speed = 400Kbps
bob_tpc 5:e77529f7ede3 83 prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU
bob_tpc 9:046247707ffb 84 prox_int.fall(&prox_irq);
bob_tpc 9:046247707ffb 85 prox_int.enable_irq();
bob_tpc 9:046247707ffb 86 cdc.printf("Interrupt active");
bob_tpc 5:e77529f7ede3 87
bob_tpc 5:e77529f7ede3 88 // LEDs // Cycle through the LEDs.
bob_tpc 5:e77529f7ede3 89 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 90 led_com.write(LEDON);
bob_tpc 5:e77529f7ede3 91 wait(0.5);
bob_tpc 5:e77529f7ede3 92 led_err.write(LEDOFF);
bob_tpc 5:e77529f7ede3 93 wait(0.5);
bob_tpc 5:e77529f7ede3 94 led_com.write(LEDOFF);
bob_tpc 5:e77529f7ede3 95
bob_tpc 0:8604e9cc07f2 96 return 0;
bob_tpc 0:8604e9cc07f2 97 }
bob_tpc 0:8604e9cc07f2 98
bob_tpc 4:13e3e375c0d3 99 /*
bob_tpc 4:13e3e375c0d3 100 RFID messages are as defined in the RFID-FE manual.
bob_tpc 4:13e3e375c0d3 101 */
bob_tpc 0:8604e9cc07f2 102 int rfid_msg(void)
bob_tpc 0:8604e9cc07f2 103 {
bob_tpc 0:8604e9cc07f2 104 bool end_mark = FALSE;
bob_tpc 0:8604e9cc07f2 105 int i;
bob_tpc 5:e77529f7ede3 106 uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start
bob_tpc 0:8604e9cc07f2 107
bob_tpc 5:e77529f7ede3 108 uart.txBufferFlush(); // clear out UART buffers
bob_tpc 0:8604e9cc07f2 109 uart.rxBufferFlush();
bob_tpc 0:8604e9cc07f2 110
bob_tpc 0:8604e9cc07f2 111 for (int i = 0; i < sizeof(uart_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 112 {
bob_tpc 5:e77529f7ede3 113 if (!uart.writeable())
bob_tpc 5:e77529f7ede3 114 {
bob_tpc 5:e77529f7ede3 115 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 116 return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error
bob_tpc 5:e77529f7ede3 117 }
bob_tpc 6:2941452a0e6d 118
bob_tpc 5:e77529f7ede3 119 uart.putc(uart_buffer_tx[i]); // send uart message
bob_tpc 0:8604e9cc07f2 120
bob_tpc 5:e77529f7ede3 121 if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 0:8604e9cc07f2 122 {
bob_tpc 5:e77529f7ede3 123 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 124 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 125 }
bob_tpc 5:e77529f7ede3 126 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 127 {
bob_tpc 5:e77529f7ede3 128 if (end_mark == FALSE)
bob_tpc 5:e77529f7ede3 129 {
bob_tpc 5:e77529f7ede3 130 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 131 return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 132 }
bob_tpc 0:8604e9cc07f2 133 break;
bob_tpc 0:8604e9cc07f2 134 }
bob_tpc 0:8604e9cc07f2 135 }
bob_tpc 0:8604e9cc07f2 136
bob_tpc 0:8604e9cc07f2 137 end_mark = FALSE;
bob_tpc 5:e77529f7ede3 138 while(!uart.readable()); // wait for data from rfid
bob_tpc 5:e77529f7ede3 139 crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start
bob_tpc 0:8604e9cc07f2 140 for (i = 0; i < sizeof(uart_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 141 {
bob_tpc 5:e77529f7ede3 142 uart_buffer_rx[i] = uart.getc(); // read a character
bob_tpc 0:8604e9cc07f2 143
bob_tpc 5:e77529f7ede3 144 if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message
bob_tpc 0:8604e9cc07f2 145 {
bob_tpc 5:e77529f7ede3 146 crcCount = 2; // two more bytes for crc
bob_tpc 5:e77529f7ede3 147 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 148 }
bob_tpc 5:e77529f7ede3 149 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 150 {
bob_tpc 5:e77529f7ede3 151 if (end_mark == FALSE)
bob_tpc 5:e77529f7ede3 152 {
bob_tpc 5:e77529f7ede3 153 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 154 return ERR_UART_NO_RX_ENDMARK;
bob_tpc 5:e77529f7ede3 155 }
bob_tpc 0:8604e9cc07f2 156 break;
bob_tpc 0:8604e9cc07f2 157 }
bob_tpc 0:8604e9cc07f2 158 }
bob_tpc 0:8604e9cc07f2 159 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 160 }
bob_tpc 0:8604e9cc07f2 161
bob_tpc 0:8604e9cc07f2 162
bob_tpc 4:13e3e375c0d3 163 /*
bob_tpc 8:3313aa7f9082 164 I2C-prox messages = 0xCC, r/w#, number of data bytes, index (2 bytes), data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 4:13e3e375c0d3 165
bob_tpc 4:13e3e375c0d3 166 Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr(). Location address increments for each byte.
bob_tpc 4:13e3e375c0d3 167 */
bob_tpc 4:13e3e375c0d3 168
bob_tpc 5:e77529f7ede3 169 int prox_msg_wr() // write proximity I2C register
bob_tpc 0:8604e9cc07f2 170 {
bob_tpc 4:13e3e375c0d3 171 int i2c_err;
bob_tpc 4:13e3e375c0d3 172 i2c_err = i2c.write(PROX, &i2c_buffer[3], i2c_buffer[2] + 2, 0);// I2C Address, pointer to buffer, number of bytes (for index + data), stop at end.
bob_tpc 5:e77529f7ede3 173 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 174 }
bob_tpc 3:e8cc286f9b2e 175
bob_tpc 4:13e3e375c0d3 176 int prox_msg_rd()
bob_tpc 4:13e3e375c0d3 177 {
bob_tpc 4:13e3e375c0d3 178 int i2c_err;
bob_tpc 5:e77529f7ede3 179 i2c_err = i2c.write(PROX, &i2c_buffer[3], 2, 1); // I2C Address, pointer to buffer (just the index), index, number of bytes (2 for index), no stop at end.
bob_tpc 5:e77529f7ede3 180 i2c_err |= i2c.read(PROX, &i2c_buffer[5], i2c_buffer[2], 0); // I2C Address, pointer to buffer (just the data), number of data bytes, stop at end.
bob_tpc 5:e77529f7ede3 181 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 182 }
bob_tpc 4:13e3e375c0d3 183
bob_tpc 9:046247707ffb 184
bob_tpc 8:3313aa7f9082 185 // GPIO messages = 0xDD, r/w#, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 5:e77529f7ede3 186 int gpio_rd()
bob_tpc 5:e77529f7ede3 187 {
bob_tpc 9:046247707ffb 188 gpio_buffer[2] = ( led_err.read() & 0x01); // read all of the GPIO pins and store in a single byte
bob_tpc 9:046247707ffb 189 gpio_buffer[2] |= ((led_com_state << 1) & 0x02); // use of led_com_state allows the led to be ON during this call, but send back the pre-call state.
bob_tpc 9:046247707ffb 190 gpio_buffer[2] |= ((rfid_int.read() << 2) & 0x04);
bob_tpc 9:046247707ffb 191 gpio_buffer[2] |= ((rfid_isp.read() << 3) & 0x08);
bob_tpc 9:046247707ffb 192 gpio_buffer[2] |= ((rfid_rst.read() << 4) & 0x10);
bob_tpc 9:046247707ffb 193 gpio_buffer[2] |= ((rfid_pwr.read() << 5) & 0x20);
bob_tpc 5:e77529f7ede3 194
bob_tpc 5:e77529f7ede3 195 return ERR_NONE;
bob_tpc 5:e77529f7ede3 196 }
bob_tpc 4:13e3e375c0d3 197
bob_tpc 5:e77529f7ede3 198 int gpio_wr()
bob_tpc 5:e77529f7ede3 199 {
bob_tpc 9:046247707ffb 200 if ((gpio_buffer[2] & 0x02) == 0x00)
bob_tpc 9:046247707ffb 201 {
bob_tpc 9:046247707ffb 202 led_com_state = LEDON;
bob_tpc 9:046247707ffb 203 }
bob_tpc 9:046247707ffb 204 else
bob_tpc 9:046247707ffb 205 {
bob_tpc 9:046247707ffb 206 led_com_state = LEDOFF;
bob_tpc 9:046247707ffb 207 }
bob_tpc 9:046247707ffb 208 led_err.write(gpio_buffer[2] & 0x01);
bob_tpc 9:046247707ffb 209 led_com.write(led_com_state);
bob_tpc 9:046247707ffb 210 rfid_int.write(gpio_buffer[2] & 0x04);
bob_tpc 9:046247707ffb 211 rfid_isp.write(gpio_buffer[2] & 0x08);
bob_tpc 9:046247707ffb 212 rfid_rst.write(gpio_buffer[2] & 0x10);
bob_tpc 9:046247707ffb 213 rfid_pwr.write(gpio_buffer[2] & 0x20);
bob_tpc 5:e77529f7ede3 214 return ERR_NONE;
bob_tpc 5:e77529f7ede3 215 }
bob_tpc 5:e77529f7ede3 216
bob_tpc 5:e77529f7ede3 217
bob_tpc 4:13e3e375c0d3 218 /*
bob_tpc 4:13e3e375c0d3 219 I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 4:13e3e375c0d3 220
bob_tpc 4:13e3e375c0d3 221 Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte.
bob_tpc 4:13e3e375c0d3 222 This practically the the same as the proximity calls, except the index/location is only one byte and the block select is part of the I2C address byte.
bob_tpc 4:13e3e375c0d3 223 */
bob_tpc 4:13e3e375c0d3 224
bob_tpc 5:e77529f7ede3 225 int eeprom_msg_wr() // write proximity I2C register
bob_tpc 4:13e3e375c0d3 226 {
bob_tpc 4:13e3e375c0d3 227 int i2c_err;
bob_tpc 5:e77529f7ede3 228 i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0);
bob_tpc 5:e77529f7ede3 229 // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end.
bob_tpc 5:e77529f7ede3 230 while (!i2c.write(EEPROM || i2c_buffer[3])); // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write)
bob_tpc 5:e77529f7ede3 231 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 232 }
bob_tpc 3:e8cc286f9b2e 233
bob_tpc 4:13e3e375c0d3 234 int eeprom_msg_rd()
bob_tpc 4:13e3e375c0d3 235 {
bob_tpc 4:13e3e375c0d3 236 int i2c_err;
bob_tpc 5:e77529f7ede3 237 i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], 1, 1);
bob_tpc 5:e77529f7ede3 238 // I2C Address & block select, pointer to buffer (just the index), index, number of bytes (for address + data), no stop at end.
bob_tpc 5:e77529f7ede3 239 i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0);
bob_tpc 5:e77529f7ede3 240 // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end.
bob_tpc 5:e77529f7ede3 241 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 0:8604e9cc07f2 242 }
bob_tpc 0:8604e9cc07f2 243
bob_tpc 5:e77529f7ede3 244
bob_tpc 0:8604e9cc07f2 245
bob_tpc 0:8604e9cc07f2 246 int main()
bob_tpc 0:8604e9cc07f2 247 {
bob_tpc 0:8604e9cc07f2 248 // initialize everything
bob_tpc 6:2941452a0e6d 249
bob_tpc 9:046247707ffb 250 wait(5.0); // gives a chance to connect the COM port - this can be removed for production
bob_tpc 9:046247707ffb 251
bob_tpc 0:8604e9cc07f2 252 init_periph();
bob_tpc 0:8604e9cc07f2 253
bob_tpc 5:e77529f7ede3 254 while(1)
bob_tpc 0:8604e9cc07f2 255 {
bob_tpc 9:046247707ffb 256 led_com.write(led_com_state); // turn off communication LED unless it was specifically turned on by GPIO command
bob_tpc 9:046247707ffb 257 while(!cdc.readable())
bob_tpc 9:046247707ffb 258 {
bob_tpc 9:046247707ffb 259 if(prox_irq_state == 1)
bob_tpc 9:046247707ffb 260 {
bob_tpc 9:046247707ffb 261 prox_irq_state = 0;
bob_tpc 9:046247707ffb 262 cdc.putc(0xFF);
bob_tpc 9:046247707ffb 263 cdc.putc(0x7E);
bob_tpc 9:046247707ffb 264 cdc.putc(0x0F);
bob_tpc 9:046247707ffb 265 cdc.putc(0xF0);
bob_tpc 9:046247707ffb 266 }
bob_tpc 9:046247707ffb 267 } // spin here until a message comes in from the host PC
bob_tpc 5:e77529f7ede3 268 led_com.write(LEDON); // Message received - turn on LED
bob_tpc 5:e77529f7ede3 269 bool end_mark = FALSE;
bob_tpc 5:e77529f7ede3 270 uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start
bob_tpc 5:e77529f7ede3 271 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 272 {
bob_tpc 5:e77529f7ede3 273 cdc_buffer_rx[i] = cdc.getc(); // read data from USB side
bob_tpc 5:e77529f7ede3 274
bob_tpc 5:e77529f7ede3 275 if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 276 {
bob_tpc 5:e77529f7ede3 277 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 278 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 279 }
bob_tpc 5:e77529f7ede3 280 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 281 {
bob_tpc 5:e77529f7ede3 282 if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 283 break;
bob_tpc 5:e77529f7ede3 284 }
bob_tpc 0:8604e9cc07f2 285 }
bob_tpc 0:8604e9cc07f2 286
bob_tpc 5:e77529f7ede3 287 switch(cdc_buffer_rx[0])
bob_tpc 5:e77529f7ede3 288 {
bob_tpc 5:e77529f7ede3 289 case 0xBB: // RFID-FE
bob_tpc 5:e77529f7ede3 290 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 291 {
bob_tpc 5:e77529f7ede3 292 uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID
bob_tpc 5:e77529f7ede3 293 }
bob_tpc 5:e77529f7ede3 294
bob_tpc 5:e77529f7ede3 295 status = rfid_msg(); // send buffer to RFID and get response according to RFID board
bob_tpc 0:8604e9cc07f2 296
bob_tpc 5:e77529f7ede3 297 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 298 {
bob_tpc 5:e77529f7ede3 299 cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer
bob_tpc 5:e77529f7ede3 300 }
bob_tpc 5:e77529f7ede3 301
bob_tpc 5:e77529f7ede3 302 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 303 {
bob_tpc 5:e77529f7ede3 304 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 305
bob_tpc 5:e77529f7ede3 306 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 307 {
bob_tpc 5:e77529f7ede3 308 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 309 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 310 }
bob_tpc 5:e77529f7ede3 311 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 312 {
bob_tpc 5:e77529f7ede3 313 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 314 break;
bob_tpc 5:e77529f7ede3 315 }
bob_tpc 5:e77529f7ede3 316 }
bob_tpc 5:e77529f7ede3 317 break;
bob_tpc 0:8604e9cc07f2 318
bob_tpc 5:e77529f7ede3 319 case 0xCC: // Proximity Sensor
bob_tpc 5:e77529f7ede3 320 //I2C-prox messages = 0xCC, r/w, number of data bytes, index, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 5:e77529f7ede3 321 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 322 {
bob_tpc 5:e77529f7ede3 323 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 0:8604e9cc07f2 324 }
bob_tpc 5:e77529f7ede3 325
bob_tpc 5:e77529f7ede3 326 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 5:e77529f7ede3 327 status = prox_msg_rd(); // read the requested data
bob_tpc 5:e77529f7ede3 328 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 5:e77529f7ede3 329 status = prox_msg_wr(); // send buffer to proximity sensor and get response
bob_tpc 5:e77529f7ede3 330
bob_tpc 5:e77529f7ede3 331 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 332 {
bob_tpc 5:e77529f7ede3 333 cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer
bob_tpc 0:8604e9cc07f2 334 }
bob_tpc 3:e8cc286f9b2e 335
bob_tpc 5:e77529f7ede3 336 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 337 {
bob_tpc 5:e77529f7ede3 338 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 339
bob_tpc 5:e77529f7ede3 340 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 341 {
bob_tpc 5:e77529f7ede3 342 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 343 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 344 }
bob_tpc 5:e77529f7ede3 345 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 346 {
bob_tpc 5:e77529f7ede3 347 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 348 break;
bob_tpc 5:e77529f7ede3 349 }
bob_tpc 5:e77529f7ede3 350 }
bob_tpc 5:e77529f7ede3 351 break;
bob_tpc 3:e8cc286f9b2e 352
bob_tpc 5:e77529f7ede3 353 case 0xDD: // GPIO (LEDs and RFID-FE control)
bob_tpc 6:2941452a0e6d 354 //GPIO messages = 0xDD, r/w#, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 5:e77529f7ede3 355 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 356 {
bob_tpc 5:e77529f7ede3 357 gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 5:e77529f7ede3 358 }
bob_tpc 3:e8cc286f9b2e 359
bob_tpc 6:2941452a0e6d 360 if (gpio_buffer[1] == 1) // GPIO read = 1
bob_tpc 5:e77529f7ede3 361 status = gpio_rd(); // read the requested data
bob_tpc 6:2941452a0e6d 362 else if (gpio_buffer[1] == 0) // GPIO write = 0
bob_tpc 6:2941452a0e6d 363 status = gpio_wr(); // send GPIO pin data
bob_tpc 5:e77529f7ede3 364
bob_tpc 5:e77529f7ede3 365 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 3:e8cc286f9b2e 366 {
bob_tpc 6:2941452a0e6d 367 cdc_buffer_tx[i] = gpio_buffer[i]; // copy GPIO response back to USB buffer
bob_tpc 3:e8cc286f9b2e 368 }
bob_tpc 5:e77529f7ede3 369
bob_tpc 5:e77529f7ede3 370 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 3:e8cc286f9b2e 371 {
bob_tpc 5:e77529f7ede3 372 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 373
bob_tpc 5:e77529f7ede3 374 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 375 {
bob_tpc 5:e77529f7ede3 376 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 377 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 378 }
bob_tpc 5:e77529f7ede3 379 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 380 {
bob_tpc 5:e77529f7ede3 381 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 382 break;
bob_tpc 5:e77529f7ede3 383 }
bob_tpc 3:e8cc286f9b2e 384 }
bob_tpc 5:e77529f7ede3 385 break;
bob_tpc 5:e77529f7ede3 386
bob_tpc 5:e77529f7ede3 387 case 0xEE: // Read/write EEPROM
bob_tpc 4:13e3e375c0d3 388 /*
bob_tpc 4:13e3e375c0d3 389 I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 4:13e3e375c0d3 390
bob_tpc 4:13e3e375c0d3 391 Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte.
bob_tpc 4:13e3e375c0d3 392 This practically the the same as the proximity calls, except the index/location is only one byte and the block select is part of the I2C address byte.
bob_tpc 4:13e3e375c0d3 393 */
bob_tpc 5:e77529f7ede3 394 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 395 {
bob_tpc 5:e77529f7ede3 396 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 5:e77529f7ede3 397 }
bob_tpc 4:13e3e375c0d3 398
bob_tpc 5:e77529f7ede3 399 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 5:e77529f7ede3 400 status = gpio_rd(); // read the gpio pins
bob_tpc 5:e77529f7ede3 401 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 5:e77529f7ede3 402 status = gpio_wr(); // write gpio pins
bob_tpc 4:13e3e375c0d3 403
bob_tpc 5:e77529f7ede3 404 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 405 {
bob_tpc 5:e77529f7ede3 406 cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer
bob_tpc 5:e77529f7ede3 407 }
bob_tpc 4:13e3e375c0d3 408
bob_tpc 5:e77529f7ede3 409 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 4:13e3e375c0d3 410 {
bob_tpc 5:e77529f7ede3 411 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 412
bob_tpc 5:e77529f7ede3 413 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 414 {
bob_tpc 5:e77529f7ede3 415 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 416 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 417 }
bob_tpc 5:e77529f7ede3 418 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 419 {
bob_tpc 5:e77529f7ede3 420 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 421 break;
bob_tpc 5:e77529f7ede3 422 }
bob_tpc 5:e77529f7ede3 423 }
bob_tpc 5:e77529f7ede3 424 break;
bob_tpc 5:e77529f7ede3 425 default:
bob_tpc 5:e77529f7ede3 426 return ERR_CDC_BAD_CMD;
bob_tpc 5:e77529f7ede3 427 }
bob_tpc 0:8604e9cc07f2 428 }
bob_tpc 0:8604e9cc07f2 429 }