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:
Wed Jan 21 01:10:59 2015 +0000
Revision:
5:e77529f7ede3
Parent:
4:13e3e375c0d3
Child:
6:2941452a0e6d
Added GPIO functions

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 4:13e3e375c0d3 20 #define ERR_I2C_NOT_WRITEABLE 6 // UART has no buffer space
bob_tpc 4:13e3e375c0d3 21 #define ERR_I2C_NO_TX_ENDMARK 7 // message for UART has no 0x7E end-mark
bob_tpc 4:13e3e375c0d3 22 #define ERR_I2C_NO_RX_ENDMARK 8 // message received from UART has no end-mark
bob_tpc 4:13e3e375c0d3 23 #define ERR_NOT_IMPLEMENTED 255 // method has not yet been implemented
bob_tpc 0:8604e9cc07f2 24
bob_tpc 0:8604e9cc07f2 25
bob_tpc 0:8604e9cc07f2 26 // I2C addresses
bob_tpc 0:8604e9cc07f2 27 #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits
bob_tpc 0:8604e9cc07f2 28 #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted
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 0:8604e9cc07f2 51 uint8_t cdc_buffer_rx[32]; // 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 0:8604e9cc07f2 60 int prox_irq(void)
bob_tpc 0:8604e9cc07f2 61 {
bob_tpc 0:8604e9cc07f2 62 return 0;
bob_tpc 0:8604e9cc07f2 63 }
bob_tpc 0:8604e9cc07f2 64
bob_tpc 0:8604e9cc07f2 65 int init_periph(void)
bob_tpc 0:8604e9cc07f2 66 {
bob_tpc 0:8604e9cc07f2 67 // Set up peripherals
bob_tpc 0:8604e9cc07f2 68 // RFID
bob_tpc 5:e77529f7ede3 69 uart.baud(RFIDBAUD); // RFID-FE baud rate
bob_tpc 0:8604e9cc07f2 70
bob_tpc 5:e77529f7ede3 71 rfid_int = 0; // RFID FE power control (active high)
bob_tpc 5:e77529f7ede3 72 rfid_isp = 0; // RFID FE In-System Programming (active high)
bob_tpc 5:e77529f7ede3 73 rfid_rst = 1; // RFID FE Reset (active high)
bob_tpc 5:e77529f7ede3 74 rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 5:e77529f7ede3 75 wait(0.25); // wait 250ms before...
bob_tpc 5:e77529f7ede3 76 rfid_rst = 0; // ... taking RFID out of reset
bob_tpc 0:8604e9cc07f2 77
bob_tpc 5:e77529f7ede3 78 // Prox & EEPROM
bob_tpc 5:e77529f7ede3 79 i2c.frequency(400000); // I2C speed = 400Kbps
bob_tpc 5:e77529f7ede3 80 prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU
bob_tpc 5:e77529f7ede3 81
bob_tpc 5:e77529f7ede3 82 // LEDs // Cycle through the LEDs.
bob_tpc 5:e77529f7ede3 83 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 84 led_com.write(LEDON);
bob_tpc 5:e77529f7ede3 85 wait(0.5);
bob_tpc 5:e77529f7ede3 86 led_err.write(LEDOFF);
bob_tpc 5:e77529f7ede3 87 wait(0.5);
bob_tpc 5:e77529f7ede3 88 led_com.write(LEDOFF);
bob_tpc 5:e77529f7ede3 89
bob_tpc 5:e77529f7ede3 90
bob_tpc 2:efaf8aee55df 91
bob_tpc 2:efaf8aee55df 92
bob_tpc 0:8604e9cc07f2 93 return 0;
bob_tpc 0:8604e9cc07f2 94 }
bob_tpc 0:8604e9cc07f2 95
bob_tpc 4:13e3e375c0d3 96 /*
bob_tpc 4:13e3e375c0d3 97 RFID messages are as defined in the RFID-FE manual.
bob_tpc 4:13e3e375c0d3 98 */
bob_tpc 0:8604e9cc07f2 99 int rfid_msg(void)
bob_tpc 0:8604e9cc07f2 100 {
bob_tpc 0:8604e9cc07f2 101 bool end_mark = FALSE;
bob_tpc 0:8604e9cc07f2 102 int i;
bob_tpc 5:e77529f7ede3 103 uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start
bob_tpc 0:8604e9cc07f2 104
bob_tpc 5:e77529f7ede3 105 uart.txBufferFlush(); // clear out UART buffers
bob_tpc 0:8604e9cc07f2 106 uart.rxBufferFlush();
bob_tpc 0:8604e9cc07f2 107
bob_tpc 0:8604e9cc07f2 108 for (int i = 0; i < sizeof(uart_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 109 {
bob_tpc 5:e77529f7ede3 110 if (!uart.writeable())
bob_tpc 5:e77529f7ede3 111 {
bob_tpc 5:e77529f7ede3 112 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 113 return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error
bob_tpc 5:e77529f7ede3 114 }
bob_tpc 5:e77529f7ede3 115 uart.putc(uart_buffer_tx[i]); // send uart message
bob_tpc 0:8604e9cc07f2 116
bob_tpc 5:e77529f7ede3 117 if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 0:8604e9cc07f2 118 {
bob_tpc 5:e77529f7ede3 119 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 120 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 121 }
bob_tpc 5:e77529f7ede3 122 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 123 {
bob_tpc 5:e77529f7ede3 124 if (end_mark == FALSE)
bob_tpc 5:e77529f7ede3 125 {
bob_tpc 5:e77529f7ede3 126 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 127 return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 128 }
bob_tpc 0:8604e9cc07f2 129 break;
bob_tpc 0:8604e9cc07f2 130 }
bob_tpc 0:8604e9cc07f2 131 }
bob_tpc 0:8604e9cc07f2 132
bob_tpc 0:8604e9cc07f2 133 end_mark = FALSE;
bob_tpc 5:e77529f7ede3 134 //wait(0.5); // debug
bob_tpc 5:e77529f7ede3 135 while(!uart.readable()); // wait for data from rfid
bob_tpc 5:e77529f7ede3 136 crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start
bob_tpc 0:8604e9cc07f2 137 for (i = 0; i < sizeof(uart_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 138 {
bob_tpc 5:e77529f7ede3 139 uart_buffer_rx[i] = uart.getc(); // read a character
bob_tpc 5:e77529f7ede3 140 // cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug
bob_tpc 0:8604e9cc07f2 141
bob_tpc 5:e77529f7ede3 142 if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message
bob_tpc 0:8604e9cc07f2 143 {
bob_tpc 5:e77529f7ede3 144 crcCount = 2; // two more bytes for crc
bob_tpc 5:e77529f7ede3 145 end_mark = TRUE; // end mark was reached
bob_tpc 0:8604e9cc07f2 146 }
bob_tpc 5:e77529f7ede3 147 if (crcCount-- == 0) // end of message
bob_tpc 0:8604e9cc07f2 148 {
bob_tpc 5:e77529f7ede3 149 if (end_mark == FALSE)
bob_tpc 5:e77529f7ede3 150 {
bob_tpc 5:e77529f7ede3 151 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 152 return ERR_UART_NO_RX_ENDMARK;
bob_tpc 5:e77529f7ede3 153 }
bob_tpc 0:8604e9cc07f2 154 break;
bob_tpc 0:8604e9cc07f2 155 }
bob_tpc 0:8604e9cc07f2 156 }
bob_tpc 0:8604e9cc07f2 157 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 158 }
bob_tpc 0:8604e9cc07f2 159
bob_tpc 0:8604e9cc07f2 160
bob_tpc 4:13e3e375c0d3 161 /*
bob_tpc 4:13e3e375c0d3 162 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 163
bob_tpc 4:13e3e375c0d3 164 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 165 */
bob_tpc 4:13e3e375c0d3 166
bob_tpc 5:e77529f7ede3 167 int prox_msg_wr() // write proximity I2C register
bob_tpc 0:8604e9cc07f2 168 {
bob_tpc 4:13e3e375c0d3 169 int i2c_err;
bob_tpc 4:13e3e375c0d3 170 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 171 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 172 }
bob_tpc 3:e8cc286f9b2e 173
bob_tpc 4:13e3e375c0d3 174 int prox_msg_rd()
bob_tpc 4:13e3e375c0d3 175 {
bob_tpc 4:13e3e375c0d3 176 int i2c_err;
bob_tpc 5:e77529f7ede3 177 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 178 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 179 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 180 }
bob_tpc 4:13e3e375c0d3 181
bob_tpc 5:e77529f7ede3 182 // GPIO messages = 0xDD, r/w, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 5:e77529f7ede3 183 int gpio_rd()
bob_tpc 5:e77529f7ede3 184 {
bob_tpc 5:e77529f7ede3 185 gpio_buffer[2] = (led_err.read() && 0x01); // read all of the GPIO pins and store in a single byte
bob_tpc 5:e77529f7ede3 186 gpio_buffer[2] |= ((led_com.read() << 1) && 0x02);
bob_tpc 5:e77529f7ede3 187 gpio_buffer[2] |= ((rfid_int.read() << 2) && 0x04);
bob_tpc 5:e77529f7ede3 188 gpio_buffer[2] |= ((rfid_isp.read() << 3) && 0x08);
bob_tpc 5:e77529f7ede3 189 gpio_buffer[2] |= ((rfid_rst.read() << 4) && 0x10);
bob_tpc 5:e77529f7ede3 190 gpio_buffer[2] |= ((rfid_pwr.read() << 5) && 0x20);
bob_tpc 5:e77529f7ede3 191 gpio_buffer[2] |= ((rfid_hot.read() << 6) && 0x40);
bob_tpc 5:e77529f7ede3 192 gpio_buffer[2] |= ((prox_int.read() << 7) && 0x80);
bob_tpc 5:e77529f7ede3 193
bob_tpc 5:e77529f7ede3 194 return ERR_NONE;
bob_tpc 5:e77529f7ede3 195 }
bob_tpc 4:13e3e375c0d3 196
bob_tpc 5:e77529f7ede3 197 int gpio_wr()
bob_tpc 5:e77529f7ede3 198 {
bob_tpc 5:e77529f7ede3 199 led_err.write(gpio_buffer[2] && 0x01); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 200 led_com.write(gpio_buffer[2] && 0x02); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 201 rfid_int.write(gpio_buffer[2] && 0x04); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 202 rfid_isp.write(gpio_buffer[2] && 0x05); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 203 rfid_rst.write(gpio_buffer[2] && 0x10); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 204 rfid_pwr.write(gpio_buffer[2] && 0x20); // any bit set will write a 0
bob_tpc 5:e77529f7ede3 205 return ERR_NONE;
bob_tpc 5:e77529f7ede3 206 }
bob_tpc 5:e77529f7ede3 207
bob_tpc 5:e77529f7ede3 208
bob_tpc 4:13e3e375c0d3 209 /*
bob_tpc 4:13e3e375c0d3 210 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 211
bob_tpc 4:13e3e375c0d3 212 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 213 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 214 */
bob_tpc 4:13e3e375c0d3 215
bob_tpc 5:e77529f7ede3 216 int eeprom_msg_wr() // write proximity I2C register
bob_tpc 4:13e3e375c0d3 217 {
bob_tpc 4:13e3e375c0d3 218 int i2c_err;
bob_tpc 5:e77529f7ede3 219 i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0);
bob_tpc 5:e77529f7ede3 220 // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end.
bob_tpc 5:e77529f7ede3 221 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 222 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 223 }
bob_tpc 3:e8cc286f9b2e 224
bob_tpc 4:13e3e375c0d3 225 int eeprom_msg_rd()
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], 1, 1);
bob_tpc 5:e77529f7ede3 229 // 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 230 i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0);
bob_tpc 5:e77529f7ede3 231 // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end.
bob_tpc 5:e77529f7ede3 232 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 0:8604e9cc07f2 233 }
bob_tpc 0:8604e9cc07f2 234
bob_tpc 5:e77529f7ede3 235
bob_tpc 0:8604e9cc07f2 236
bob_tpc 0:8604e9cc07f2 237 int main()
bob_tpc 0:8604e9cc07f2 238 {
bob_tpc 0:8604e9cc07f2 239 // initialize everything
bob_tpc 0:8604e9cc07f2 240
bob_tpc 5:e77529f7ede3 241 wait(2.0); // debug - gives some time to start terminal program and open COM port
bob_tpc 3:e8cc286f9b2e 242
bob_tpc 0:8604e9cc07f2 243 init_periph();
bob_tpc 0:8604e9cc07f2 244
bob_tpc 5:e77529f7ede3 245 cdc.printf("Starting...\n\r"); // debug
bob_tpc 5:e77529f7ede3 246
bob_tpc 5:e77529f7ede3 247 while(1)
bob_tpc 0:8604e9cc07f2 248 {
bob_tpc 5:e77529f7ede3 249 led_com.write(LEDOFF); // turn off communication LED
bob_tpc 5:e77529f7ede3 250 while(!cdc.readable()); // spin here until a message comes in from the host PC
bob_tpc 5:e77529f7ede3 251 led_com.write(LEDON); // Message received - turn on LED
bob_tpc 5:e77529f7ede3 252 bool end_mark = FALSE;
bob_tpc 5:e77529f7ede3 253 uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start
bob_tpc 5:e77529f7ede3 254 //cdc.printf("\n\rCDC Input: "); // debug
bob_tpc 5:e77529f7ede3 255 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 256 {
bob_tpc 5:e77529f7ede3 257 cdc_buffer_rx[i] = cdc.getc(); // read data from USB side
bob_tpc 5:e77529f7ede3 258
bob_tpc 5:e77529f7ede3 259 //cdc.printf("%X, ",cdc_buffer_rx[i]); // debug
bob_tpc 5:e77529f7ede3 260
bob_tpc 5:e77529f7ede3 261 if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 262 {
bob_tpc 5:e77529f7ede3 263 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 264 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 265 }
bob_tpc 5:e77529f7ede3 266 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 267 {
bob_tpc 5:e77529f7ede3 268 if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 269 break;
bob_tpc 5:e77529f7ede3 270 }
bob_tpc 0:8604e9cc07f2 271 }
bob_tpc 0:8604e9cc07f2 272
bob_tpc 5:e77529f7ede3 273 switch(cdc_buffer_rx[0])
bob_tpc 5:e77529f7ede3 274 {
bob_tpc 5:e77529f7ede3 275 case 0xBB: // RFID-FE
bob_tpc 5:e77529f7ede3 276 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 277 {
bob_tpc 5:e77529f7ede3 278 uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID
bob_tpc 5:e77529f7ede3 279 }
bob_tpc 5:e77529f7ede3 280
bob_tpc 5:e77529f7ede3 281 status = rfid_msg(); // send buffer to RFID and get response according to RFID board
bob_tpc 0:8604e9cc07f2 282
bob_tpc 5:e77529f7ede3 283 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 284 {
bob_tpc 5:e77529f7ede3 285 cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer
bob_tpc 5:e77529f7ede3 286 }
bob_tpc 5:e77529f7ede3 287
bob_tpc 5:e77529f7ede3 288 //cdc.printf("\n\rRFID Response: "); // debug
bob_tpc 0:8604e9cc07f2 289
bob_tpc 5:e77529f7ede3 290 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 291 {
bob_tpc 5:e77529f7ede3 292 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 293
bob_tpc 5:e77529f7ede3 294 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 295 {
bob_tpc 5:e77529f7ede3 296 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 297 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 298 }
bob_tpc 5:e77529f7ede3 299 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 300 {
bob_tpc 5:e77529f7ede3 301 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 302 break;
bob_tpc 5:e77529f7ede3 303 }
bob_tpc 5:e77529f7ede3 304 }
bob_tpc 5:e77529f7ede3 305 break;
bob_tpc 0:8604e9cc07f2 306
bob_tpc 5:e77529f7ede3 307 case 0xCC: // Proximity Sensor
bob_tpc 5:e77529f7ede3 308 //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 309 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 0:8604e9cc07f2 310 {
bob_tpc 5:e77529f7ede3 311 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 0:8604e9cc07f2 312 }
bob_tpc 5:e77529f7ede3 313
bob_tpc 5:e77529f7ede3 314 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 5:e77529f7ede3 315 status = prox_msg_rd(); // read the requested data
bob_tpc 5:e77529f7ede3 316 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 5:e77529f7ede3 317 status = prox_msg_wr(); // send buffer to proximity sensor and get response
bob_tpc 5:e77529f7ede3 318
bob_tpc 5:e77529f7ede3 319 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 0:8604e9cc07f2 320 {
bob_tpc 5:e77529f7ede3 321 cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer
bob_tpc 0:8604e9cc07f2 322 }
bob_tpc 3:e8cc286f9b2e 323
bob_tpc 5:e77529f7ede3 324 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 325 {
bob_tpc 5:e77529f7ede3 326 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 327
bob_tpc 5:e77529f7ede3 328 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 329 {
bob_tpc 5:e77529f7ede3 330 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 331 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 332 }
bob_tpc 5:e77529f7ede3 333 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 334 {
bob_tpc 5:e77529f7ede3 335 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 336 break;
bob_tpc 5:e77529f7ede3 337 }
bob_tpc 5:e77529f7ede3 338 }
bob_tpc 5:e77529f7ede3 339 break;
bob_tpc 3:e8cc286f9b2e 340
bob_tpc 5:e77529f7ede3 341 case 0xDD: // GPIO (LEDs and RFID-FE control)
bob_tpc 5:e77529f7ede3 342 //GPIO messages = 0xDD, r/w, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID
bob_tpc 5:e77529f7ede3 343 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 344 {
bob_tpc 5:e77529f7ede3 345 gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 5:e77529f7ede3 346 }
bob_tpc 3:e8cc286f9b2e 347
bob_tpc 5:e77529f7ede3 348 if (gpio_buffer[1] == 1) // I2C read = 1
bob_tpc 5:e77529f7ede3 349 status = gpio_rd(); // read the requested data
bob_tpc 5:e77529f7ede3 350 else if (gpio_buffer[1] == 0) // I2C write = 0
bob_tpc 5:e77529f7ede3 351 status = gpio_wr(); // send buffer to proximity sensor and get response
bob_tpc 5:e77529f7ede3 352
bob_tpc 5:e77529f7ede3 353 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 3:e8cc286f9b2e 354 {
bob_tpc 5:e77529f7ede3 355 cdc_buffer_tx[i] = gpio_buffer[i]; // copy prox response back to USB buffer
bob_tpc 3:e8cc286f9b2e 356 }
bob_tpc 5:e77529f7ede3 357
bob_tpc 5:e77529f7ede3 358 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 3:e8cc286f9b2e 359 {
bob_tpc 5:e77529f7ede3 360 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 361
bob_tpc 5:e77529f7ede3 362 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 363 {
bob_tpc 5:e77529f7ede3 364 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 365 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 366 }
bob_tpc 5:e77529f7ede3 367 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 368 {
bob_tpc 5:e77529f7ede3 369 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 370 break;
bob_tpc 5:e77529f7ede3 371 }
bob_tpc 3:e8cc286f9b2e 372 }
bob_tpc 5:e77529f7ede3 373 break;
bob_tpc 5:e77529f7ede3 374
bob_tpc 5:e77529f7ede3 375 case 0xEE: // Read/write EEPROM
bob_tpc 4:13e3e375c0d3 376 /*
bob_tpc 4:13e3e375c0d3 377 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 378
bob_tpc 4:13e3e375c0d3 379 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 380 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 381 */
bob_tpc 5:e77529f7ede3 382 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 383 {
bob_tpc 5:e77529f7ede3 384 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 5:e77529f7ede3 385 }
bob_tpc 4:13e3e375c0d3 386
bob_tpc 5:e77529f7ede3 387 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 5:e77529f7ede3 388 status = gpio_rd(); // read the gpio pins
bob_tpc 5:e77529f7ede3 389 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 5:e77529f7ede3 390 status = gpio_wr(); // write gpio pins
bob_tpc 4:13e3e375c0d3 391
bob_tpc 5:e77529f7ede3 392 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 393 {
bob_tpc 5:e77529f7ede3 394 cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer
bob_tpc 5:e77529f7ede3 395 }
bob_tpc 4:13e3e375c0d3 396
bob_tpc 5:e77529f7ede3 397 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 4:13e3e375c0d3 398 {
bob_tpc 5:e77529f7ede3 399 cdc.putc(cdc_buffer_tx[i]); // send message back to PC
bob_tpc 5:e77529f7ede3 400
bob_tpc 5:e77529f7ede3 401 if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 402 {
bob_tpc 5:e77529f7ede3 403 crcCount = 2; // two more bytes for CRC
bob_tpc 5:e77529f7ede3 404 end_mark = TRUE; // end mark was reached
bob_tpc 5:e77529f7ede3 405 }
bob_tpc 5:e77529f7ede3 406 if (crcCount-- == 0) // end of message
bob_tpc 5:e77529f7ede3 407 {
bob_tpc 5:e77529f7ede3 408 if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected
bob_tpc 5:e77529f7ede3 409 break;
bob_tpc 5:e77529f7ede3 410 }
bob_tpc 5:e77529f7ede3 411 }
bob_tpc 5:e77529f7ede3 412 break;
bob_tpc 5:e77529f7ede3 413 default:
bob_tpc 5:e77529f7ede3 414 return ERR_CDC_BAD_CMD;
bob_tpc 5:e77529f7ede3 415 }
bob_tpc 0:8604e9cc07f2 416 }
bob_tpc 0:8604e9cc07f2 417 }