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 Mar 02 17:00:37 2015 +0000
Revision:
18:d128c7020292
Parent:
17:1e704604e1ac
Child:
19:d3bef4fbab69
Move to new FW approach

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bob_tpc 10:55e35536d493 1 /**
bob_tpc 10:55e35536d493 2 * @file main.cpp
bob_tpc 10:55e35536d493 3 * @date January 2015
bob_tpc 10:55e35536d493 4 * @brief Freescale KL25Z firmware for USB-RFID adapter
bob_tpc 10:55e35536d493 5 *
bob_tpc 10:55e35536d493 6 * This firmware provided communication from a PC's USB host port to the following peripherals:
bob_tpc 10:55e35536d493 7 * RFID-FE over UART - SuperVision RFID reader module
bob_tpc 10:55e35536d493 8 * VL6180X over I2C - ST Microelectronics proximity and ambient light sensor
bob_tpc 10:55e35536d493 9 * GPIO - two LEDs and several control signals for the RFID module
bob_tpc 10:55e35536d493 10 * EEPROM - over I2C - Generic 24LC16B (untested since first revision prototype hardware does not have an EEPROM)
bob_tpc 10:55e35536d493 11 */
bob_tpc 10:55e35536d493 12
bob_tpc 10:55e35536d493 13
bob_tpc 0:8604e9cc07f2 14 #include "mbed.h"
bob_tpc 0:8604e9cc07f2 15 #include "USBSerial.h"
bob_tpc 18:d128c7020292 16 //#include "MODSERIAL.h"
bob_tpc 0:8604e9cc07f2 17
bob_tpc 0:8604e9cc07f2 18 // Constants
bob_tpc 15:713c26178a7d 19 #define LEDON 0 // Low active for LEDs - turns LED on
bob_tpc 15:713c26178a7d 20 #define LEDOFF 1 // Low active for LEDs - turns LED off
bob_tpc 0:8604e9cc07f2 21 #define TRUE 1
bob_tpc 0:8604e9cc07f2 22 #define FALSE 0
bob_tpc 0:8604e9cc07f2 23
bob_tpc 0:8604e9cc07f2 24 // Error return values
bob_tpc 15:713c26178a7d 25 #define ERR_NONE 0x00 // Success
bob_tpc 15:713c26178a7d 26 #define ERR_CDC_BAD_CMD 0x01 // First byte of PC to USB board needs to be 0xBB, 0xCC, 0xDD or 0xEE;
bob_tpc 15:713c26178a7d 27 #define ERR_CDC_NO_TX_ENDMARK 0xA1 // message for no endmark on message to PC
bob_tpc 15:713c26178a7d 28 #define ERR_UART_NOT_WRITEABLE 0xB1 // UART has no buffer space
bob_tpc 15:713c26178a7d 29 #define ERR_UART_NOT_READABLE 0xB2 // UART has no buffer space
bob_tpc 15:713c26178a7d 30 #define ERR_UART_NO_TX_ENDMARK 0xB3 // message for UART has no 0x7E end-mark
bob_tpc 15:713c26178a7d 31 #define ERR_UART_NO_RX_ENDMARK 0xB4 // message received from UART has no end-mark
bob_tpc 15:713c26178a7d 32 #define ERR_I2C_NOT_WRITEABLE 0xC1 // UART has no buffer space
bob_tpc 15:713c26178a7d 33 #define ERR_I2C_NO_TX_ENDMARK 0xC2 // message for UART has no 0x7E end-mark
bob_tpc 15:713c26178a7d 34 #define ERR_I2C_NO_RX_ENDMARK 0xC3 // message received from UART has no end-mark
bob_tpc 15:713c26178a7d 35 #define ERR_NOT_IMPLEMENTED 0xFF // method has not yet been implemented
bob_tpc 0:8604e9cc07f2 36
bob_tpc 10:55e35536d493 37 // I2C addresses and parameters
bob_tpc 15:713c26178a7d 38 #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits
bob_tpc 15:713c26178a7d 39 #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted
bob_tpc 15:713c26178a7d 40 #define I2CRATE 400000 // I2C speed
bob_tpc 0:8604e9cc07f2 41
bob_tpc 0:8604e9cc07f2 42 // UART-RFID baud rate
bob_tpc 15:713c26178a7d 43 #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps
bob_tpc 16:b6230e4d0ed8 44 #define BUFFERSIZE 128 // default buffer sizes
bob_tpc 15:713c26178a7d 45 #define RFIDLENLOW 5 // RFID message length location (length is 2 bytes)
bob_tpc 0:8604e9cc07f2 46
bob_tpc 0:8604e9cc07f2 47 // Peripherals
bob_tpc 15:713c26178a7d 48 USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers.
bob_tpc 18:d128c7020292 49 //MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board
bob_tpc 18:d128c7020292 50 Serial uart(PTA2, PTA1); // UART port connected to RFID-FE board
bob_tpc 15:713c26178a7d 51 I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above)
bob_tpc 0:8604e9cc07f2 52
bob_tpc 0:8604e9cc07f2 53 // GPIO signals
bob_tpc 15:713c26178a7d 54 DigitalOut led_err(PTC1); // Red LED shows error condition (active low)
bob_tpc 15:713c26178a7d 55 DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low)
bob_tpc 15:713c26178a7d 56 DigitalOut rfid_int(PTD4); // RFID FE power control (active high)
bob_tpc 15:713c26178a7d 57 DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high)
bob_tpc 15:713c26178a7d 58 DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high)
bob_tpc 15:713c26178a7d 59 DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others)
bob_tpc 15:713c26178a7d 60 DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low)
bob_tpc 15:713c26178a7d 61 InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low)
bob_tpc 0:8604e9cc07f2 62
bob_tpc 0:8604e9cc07f2 63 // buffers & variables
bob_tpc 15:713c26178a7d 64 uint8_t gpio_values = 0x00; // register to read GPIO values
bob_tpc 15:713c26178a7d 65 uint8_t cdc_buffer_rx[BUFFERSIZE]; // buffers for cdc (USB-Serial port on PC)
bob_tpc 15:713c26178a7d 66 uint8_t cdc_buffer_tx[BUFFERSIZE];
bob_tpc 15:713c26178a7d 67 uint8_t uart_buffer_rx[BUFFERSIZE]; // buffers for uart (RFID-FE board)
bob_tpc 15:713c26178a7d 68 uint8_t uart_buffer_tx[BUFFERSIZE];
bob_tpc 15:713c26178a7d 69 uint8_t gpio_buffer[BUFFERSIZE]; // buffer for GPIO messages
bob_tpc 15:713c26178a7d 70 char i2c_buffer[BUFFERSIZE]; // buffer for I2C devices - Proximity sensor and EEPROM - up to 256 bytes data payload for EEPROM, up to 4 for proximity
bob_tpc 15:713c26178a7d 71 int i, j; // index variables
bob_tpc 15:713c26178a7d 72 int status = 0x00; // return value
bob_tpc 15:713c26178a7d 73 uint8_t led_com_state = LEDOFF; // initial LED state
bob_tpc 15:713c26178a7d 74 uint8_t prox_irq_state = 0; // interrupt state passed from service routine
bob_tpc 15:713c26178a7d 75 uint8_t usb_irq_state = 0; // interrupt state passed from service routine
bob_tpc 9:046247707ffb 76
bob_tpc 10:55e35536d493 77
bob_tpc 10:55e35536d493 78 /**
bob_tpc 10:55e35536d493 79 * @name prox_irq
bob_tpc 10:55e35536d493 80 * @brief Sets interrupt variable for use in the main loop.
bob_tpc 10:55e35536d493 81 * The interrupt is triggered by the VL6180X GPIO1 (IRQ output)
bob_tpc 10:55e35536d493 82 *
bob_tpc 10:55e35536d493 83 * @param [in] none
bob_tpc 10:55e35536d493 84 * @param [out] prox_irq_state = 1 indicates an interrupt occured.
bob_tpc 10:55e35536d493 85 */
bob_tpc 9:046247707ffb 86 void prox_irq(void)
bob_tpc 0:8604e9cc07f2 87 {
bob_tpc 9:046247707ffb 88 prox_irq_state = 1;
bob_tpc 0:8604e9cc07f2 89 }
bob_tpc 0:8604e9cc07f2 90
bob_tpc 15:713c26178a7d 91 void usb_irq(void)
bob_tpc 15:713c26178a7d 92 {
bob_tpc 15:713c26178a7d 93 usb_irq_state = 1;
bob_tpc 15:713c26178a7d 94 }
bob_tpc 15:713c26178a7d 95
bob_tpc 10:55e35536d493 96 /**
bob_tpc 10:55e35536d493 97 * @name init_periph
bob_tpc 10:55e35536d493 98 * @brief Initializes the KL25Z peripheal interfaces
bob_tpc 10:55e35536d493 99 * KL25Z interfaces:
bob_tpc 10:55e35536d493 100 * UART - connects to SuperVision RFID-FE module
bob_tpc 10:55e35536d493 101 * I2C - connects to the ST VL6180X proximity/ambient light sensor device and a 24LC16B EEPROM (future)
bob_tpc 10:55e35536d493 102 * GPIO - includes two LEDs and signals to control RFID reader.
bob_tpc 10:55e35536d493 103 *
bob_tpc 10:55e35536d493 104 * @param [in] none
bob_tpc 10:55e35536d493 105 * @param [out] none
bob_tpc 10:55e35536d493 106 *
bob_tpc 10:55e35536d493 107 * @retval ERR_NONE No error
bob_tpc 10:55e35536d493 108 */
bob_tpc 10:55e35536d493 109 int init_periph(void)
bob_tpc 0:8604e9cc07f2 110 {
bob_tpc 0:8604e9cc07f2 111 // Set up peripherals
bob_tpc 0:8604e9cc07f2 112 // RFID
bob_tpc 15:713c26178a7d 113 uart.baud(RFIDBAUD); // RFID-FE baud rate
bob_tpc 15:713c26178a7d 114
bob_tpc 15:713c26178a7d 115 cdc.attach(&usb_irq);
bob_tpc 15:713c26178a7d 116
bob_tpc 15:713c26178a7d 117 rfid_int = 0; // RFID FE power control (active high)
bob_tpc 15:713c26178a7d 118 rfid_isp = 0; // RFID FE In-System Programming (active high)
bob_tpc 15:713c26178a7d 119 rfid_rst = 1; // RFID FE Reset (active high)
bob_tpc 17:1e704604e1ac 120 // rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1)
bob_tpc 17:1e704604e1ac 121 rfid_pwr = 0; // RFID power switch on USB board (active low for all others)
bob_tpc 18:d128c7020292 122 wait(0.35); // wait 250ms before...
bob_tpc 15:713c26178a7d 123 rfid_rst = 0; // ... taking RFID out of reset
bob_tpc 18:d128c7020292 124 wait(0.35);
bob_tpc 0:8604e9cc07f2 125
bob_tpc 17:1e704604e1ac 126 while(!uart.readable()) // wait for RESET message from RFID
bob_tpc 15:713c26178a7d 127 {
bob_tpc 15:713c26178a7d 128 led_err.write(LEDON); // flash LED until it arrives
bob_tpc 15:713c26178a7d 129 wait(0.1);
bob_tpc 15:713c26178a7d 130 led_err.write(LEDOFF);
bob_tpc 15:713c26178a7d 131 wait(0.1);
bob_tpc 15:713c26178a7d 132 }
bob_tpc 15:713c26178a7d 133
bob_tpc 18:d128c7020292 134 //uart.txBufferFlush(); // clear out UART buffers
bob_tpc 18:d128c7020292 135 //uart.rxBufferFlush();
bob_tpc 15:713c26178a7d 136
bob_tpc 15:713c26178a7d 137 led_err.write(LEDOFF);
bob_tpc 0:8604e9cc07f2 138
bob_tpc 5:e77529f7ede3 139 // Prox & EEPROM
bob_tpc 15:713c26178a7d 140 i2c.frequency(I2CRATE); // I2C speed = 400Kbps
bob_tpc 15:713c26178a7d 141 prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU
bob_tpc 15:713c26178a7d 142 prox_int.fall(&prox_irq); // VL6180X interrupt is low active
bob_tpc 15:713c26178a7d 143 prox_int.enable_irq(); // Enable proximity interrupt inputs
bob_tpc 5:e77529f7ede3 144
bob_tpc 15:713c26178a7d 145 // LEDs // Cycle through the LEDs.
bob_tpc 5:e77529f7ede3 146 led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 147 led_com.write(LEDON);
bob_tpc 5:e77529f7ede3 148 wait(0.5);
bob_tpc 5:e77529f7ede3 149 led_err.write(LEDOFF);
bob_tpc 5:e77529f7ede3 150 wait(0.5);
bob_tpc 5:e77529f7ede3 151 led_com.write(LEDOFF);
bob_tpc 5:e77529f7ede3 152
bob_tpc 10:55e35536d493 153 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 154 }
bob_tpc 0:8604e9cc07f2 155
bob_tpc 10:55e35536d493 156 /**
bob_tpc 15:713c26178a7d 157 * @name rfid_wr
bob_tpc 15:713c26178a7d 158 * @brief Forwards command to RFID reader
bob_tpc 15:713c26178a7d 159 *
bob_tpc 15:713c26178a7d 160 * RFID reader is connected to the KL25Z UART interface. The host PC will have a USB CDC class COM port device driver.
bob_tpc 15:713c26178a7d 161 * The host PC sends the RFID command over the COM port. Messages destined for the RFID reader (0xBB leading byte) are
bob_tpc 15:713c26178a7d 162 * forwarded as-is to the RFID reader. The reader then responds in kind. All RFID commands are described in the
bob_tpc 15:713c26178a7d 163 * RFID-FE module manual.
bob_tpc 15:713c26178a7d 164 *
bob_tpc 15:713c26178a7d 165 * @param [out] uart_buffer_tx - messages to the RFID reader
bob_tpc 15:713c26178a7d 166 *
bob_tpc 15:713c26178a7d 167 * @retval ERR_NONE No error
bob_tpc 15:713c26178a7d 168 * @retval ERR_UART_NOT_WRITEABLE UART has no buffer space
bob_tpc 15:713c26178a7d 169 * @retval ERR_UART_NO_TX_ENDMARK message for UART has no 0x7E end-mark
bob_tpc 15:713c26178a7d 170 * @example
bob_tpc 15:713c26178a7d 171 * BB 00 03 00 01 02 7E 2E C9 = read
bob_tpc 15:713c26178a7d 172 */
bob_tpc 15:713c26178a7d 173 int rfid_wr(void)
bob_tpc 15:713c26178a7d 174 {
bob_tpc 15:713c26178a7d 175 int em_pos = 0;
bob_tpc 15:713c26178a7d 176
bob_tpc 15:713c26178a7d 177 for (i = 0; i < sizeof(uart_buffer_tx); i++)
bob_tpc 15:713c26178a7d 178 {
bob_tpc 15:713c26178a7d 179 if (uart_buffer_tx[i] == 0x7E) em_pos = (i + 1); // allows 0x7E to appear in the data payload - uses last one for end-mark
bob_tpc 15:713c26178a7d 180 }
bob_tpc 15:713c26178a7d 181
bob_tpc 15:713c26178a7d 182 if (em_pos == 0)
bob_tpc 15:713c26178a7d 183 {
bob_tpc 15:713c26178a7d 184 led_err.write(LEDON); // end mark never reached
bob_tpc 15:713c26178a7d 185 return ERR_UART_NO_TX_ENDMARK;
bob_tpc 15:713c26178a7d 186 }
bob_tpc 15:713c26178a7d 187
bob_tpc 15:713c26178a7d 188 if (!uart.writeable())
bob_tpc 15:713c26178a7d 189 {
bob_tpc 15:713c26178a7d 190 led_err.write(LEDON);
bob_tpc 15:713c26178a7d 191 return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error
bob_tpc 15:713c26178a7d 192 }
bob_tpc 15:713c26178a7d 193
bob_tpc 15:713c26178a7d 194 for (i = 0; i < (em_pos + 2); i++)
bob_tpc 15:713c26178a7d 195 {
bob_tpc 15:713c26178a7d 196 uart.putc(uart_buffer_tx[i]); // send uart message
bob_tpc 15:713c26178a7d 197 }
bob_tpc 15:713c26178a7d 198
bob_tpc 15:713c26178a7d 199 return ERR_NONE;
bob_tpc 15:713c26178a7d 200 }
bob_tpc 15:713c26178a7d 201
bob_tpc 15:713c26178a7d 202 /**
bob_tpc 15:713c26178a7d 203 * @name rfid_rd
bob_tpc 15:713c26178a7d 204 * @brief Sends RFID response to CDC
bob_tpc 10:55e35536d493 205 *
bob_tpc 10:55e35536d493 206 * RFID reader is connected to the KL25Z UART interface. The host PC will have a USB CDC class COM port device driver.
bob_tpc 10:55e35536d493 207 * The host PC sends the RFID command over the COM port. Messages destined for the RFID reader (0xBB leading byte) are
bob_tpc 10:55e35536d493 208 * forwarded as-is to the RFID reader. The reader then responds in kind. All RFID commands are described in the
bob_tpc 10:55e35536d493 209 * RFID-FE module manual.
bob_tpc 10:55e35536d493 210
bob_tpc 10:55e35536d493 211 * @param [in] uart_buffer_rx - messages from the RFID reader
bob_tpc 10:55e35536d493 212 *
bob_tpc 10:55e35536d493 213 * @retval ERR_NONE No error
bob_tpc 10:55e35536d493 214 * @example
bob_tpc 10:55e35536d493 215 * BB 00 03 00 01 02 7E 2E C9 = read
bob_tpc 4:13e3e375c0d3 216 */
bob_tpc 15:713c26178a7d 217 int rfid_rd(void)
bob_tpc 0:8604e9cc07f2 218 {
bob_tpc 15:713c26178a7d 219 int rfid_len = 0;
bob_tpc 17:1e704604e1ac 220
bob_tpc 15:713c26178a7d 221 led_com.write(LEDON);
bob_tpc 15:713c26178a7d 222 for (i = 0; i < (RFIDLENLOW); i++) // Get first part of message to find out total count
bob_tpc 0:8604e9cc07f2 223 {
bob_tpc 15:713c26178a7d 224 uart_buffer_rx[i] = uart.getc(); // get a byte from rfid
bob_tpc 0:8604e9cc07f2 225 }
bob_tpc 15:713c26178a7d 226 rfid_len = ((uart_buffer_rx[i-2]<<8) + (uart_buffer_rx[i-1])); // location of message length for RFID
bob_tpc 15:713c26178a7d 227
bob_tpc 15:713c26178a7d 228 for (i = RFIDLENLOW; i < (RFIDLENLOW + rfid_len + 3); i++)
bob_tpc 0:8604e9cc07f2 229 {
bob_tpc 15:713c26178a7d 230 uart_buffer_rx[i] = uart.getc(); // get a byte from rfid
bob_tpc 0:8604e9cc07f2 231 }
bob_tpc 15:713c26178a7d 232
bob_tpc 15:713c26178a7d 233 led_com.write(LEDOFF);
bob_tpc 0:8604e9cc07f2 234 return ERR_NONE;
bob_tpc 0:8604e9cc07f2 235 }
bob_tpc 0:8604e9cc07f2 236
bob_tpc 10:55e35536d493 237 /**
bob_tpc 10:55e35536d493 238 * @name prox_msg_wr
bob_tpc 10:55e35536d493 239 * @brief Forwards command to VL6180X sensor
bob_tpc 10:55e35536d493 240 *
bob_tpc 10:55e35536d493 241 * Proximity/ALS reader is connected to the KL25Z I2C interface.
bob_tpc 10:55e35536d493 242 * The host PC sends the sensor command over the COM port. Messages destined for the proximity/ALS sensor (0xCC leading byte) are
bob_tpc 10:55e35536d493 243 * forwarded to the proximity/ALS sensor after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes).
bob_tpc 10:55e35536d493 244 * The sensor then responds in kind. Firmware re-attaches the leading 0xCC and trailing bytes before sending the response over the
bob_tpc 10:55e35536d493 245 * CDC port to the host PC.
bob_tpc 10:55e35536d493 246 *
bob_tpc 11:984631a6e373 247 * I2C-prox messages:
bob_tpc 11:984631a6e373 248 * - 0xCC (byte) leading value = 0xCC
bob_tpc 11:984631a6e373 249 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 250 * - number of data bytes(byte) 0 to 32 (size of declared buffers)
bob_tpc 11:984631a6e373 251 * - index (2 bytes) 12-bit VL6801X register offset, high byte first
bob_tpc 11:984631a6e373 252 * - data (n bytes) number of data bytes noted above
bob_tpc 11:984631a6e373 253 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 254 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 255 *
bob_tpc 10:55e35536d493 256 * Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr(). Location address increments for each byte.
bob_tpc 10:55e35536d493 257 * VL6180X registers are defined in the sensor datasheet.
bob_tpc 10:55e35536d493 258 *
bob_tpc 10:55e35536d493 259 * @param [in] i2c_buffer - messages to and from the VL6180X and EEPROM
bob_tpc 10:55e35536d493 260 *
bob_tpc 10:55e35536d493 261 * @retval 0 No error
bob_tpc 10:55e35536d493 262 * @retval 1 I2C bus has NAK'd / failure
bob_tpc 10:55e35536d493 263 *
bob_tpc 10:55e35536d493 264 * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above
bob_tpc 10:55e35536d493 265 *
bob_tpc 4:13e3e375c0d3 266 */
bob_tpc 15:713c26178a7d 267 int prox_msg_wr() // write proximity I2C register
bob_tpc 0:8604e9cc07f2 268 {
bob_tpc 4:13e3e375c0d3 269 int i2c_err;
bob_tpc 4:13e3e375c0d3 270 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 15:713c26178a7d 271 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 272 }
bob_tpc 3:e8cc286f9b2e 273
bob_tpc 10:55e35536d493 274 /**
bob_tpc 10:55e35536d493 275 * @name prox_msg_rd
bob_tpc 10:55e35536d493 276 * @brief retrieves response from VL6180X sensor
bob_tpc 10:55e35536d493 277 *
bob_tpc 10:55e35536d493 278 * Proximity/ALS reader is connected to the KL25Z I2C interface.
bob_tpc 10:55e35536d493 279 * The host PC sends the sensor command over the COM port. Messages destined for the proximity/ALS sensor (0xCC leading byte) are
bob_tpc 10:55e35536d493 280 * forwarded to the proximity/ALS sensor after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes).
bob_tpc 10:55e35536d493 281 * The sensor then responds in kind. Firmware re-attaches the leading 0xCC and trailing bytes before sending the response over the
bob_tpc 10:55e35536d493 282 * CDC port to the host PC.
bob_tpc 10:55e35536d493 283 *
bob_tpc 11:984631a6e373 284 * I2C-prox messages:
bob_tpc 11:984631a6e373 285 * - 0xCC (byte) leading value = 0xCC
bob_tpc 11:984631a6e373 286 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 287 * - number of data bytes(byte) 0 to 32 (size of declared buffers)
bob_tpc 11:984631a6e373 288 * - index (2 bytes) 12-bit VL6801X register offset, high byte first
bob_tpc 11:984631a6e373 289 * - data (n bytes) number of data bytes noted above
bob_tpc 11:984631a6e373 290 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 291 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 292 *
bob_tpc 10:55e35536d493 293 * Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr(). Location address increments for each byte.
bob_tpc 10:55e35536d493 294 * VL6180X registers are defined in the sensor datasheet.
bob_tpc 10:55e35536d493 295 *
bob_tpc 10:55e35536d493 296 * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above
bob_tpc 10:55e35536d493 297 *
bob_tpc 10:55e35536d493 298 * @retval 0 No error
bob_tpc 10:55e35536d493 299 * @retval 1 I2C bus has NAK'd / failure
bob_tpc 10:55e35536d493 300 *
bob_tpc 10:55e35536d493 301 *
bob_tpc 10:55e35536d493 302 */
bob_tpc 4:13e3e375c0d3 303 int prox_msg_rd()
bob_tpc 4:13e3e375c0d3 304 {
bob_tpc 4:13e3e375c0d3 305 int i2c_err;
bob_tpc 15:713c26178a7d 306 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 15:713c26178a7d 307 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 15:713c26178a7d 308 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 309 }
bob_tpc 4:13e3e375c0d3 310
bob_tpc 10:55e35536d493 311 /**
bob_tpc 10:55e35536d493 312 * @name gpio_rd
bob_tpc 10:55e35536d493 313 * @brief retrieves instantaneous value of GPIO pins
bob_tpc 10:55e35536d493 314 *
bob_tpc 10:55e35536d493 315 * GPIO signals are defined directly off of the KL25Z.
bob_tpc 10:55e35536d493 316 * The host PC sends the GPIO command over the COM port. With a 0xDD leading byte in the message, the state of the GPIO signals are read and returned.
bob_tpc 10:55e35536d493 317 * This allows a read-modify-write GPIO sequence.
bob_tpc 10:55e35536d493 318 *
bob_tpc 11:984631a6e373 319 * GPIO messages:
bob_tpc 11:984631a6e373 320 * - 0xDD (byte) leading value = 0xDD
bob_tpc 11:984631a6e373 321 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 322 * - data (byte) see below
bob_tpc 11:984631a6e373 323 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 324 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 325 *
bob_tpc 11:984631a6e373 326 * GPIO data bits:
bob_tpc 13:a390c4798a0d 327 * - 0 LED - Error 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 328 * - 1 LED - Comm state 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 329 * - 2 RFID interrupt input 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 330 * - 3 RFID in-system-prog 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 331 * - 4 RFID reset 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 332 * - 5 RFID power enable for first prototype, 0 = off, 1 = on / for production, 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 333 * - 6 RFID over-current 0 = overcurrent detected, 1 = OK
bob_tpc 13:a390c4798a0d 334 * - 7 Proximity interrupt 0 = interrupt, 1 = idle (This pin may not return anything meaningful here. The interrupt is edge triggered).
bob_tpc 10:55e35536d493 335 *
bob_tpc 10:55e35536d493 336 * @param [in/out] gpio_buffer - GPIO states
bob_tpc 10:55e35536d493 337 *
bob_tpc 10:55e35536d493 338 * @retval 0 No error
bob_tpc 10:55e35536d493 339 *
bob_tpc 10:55e35536d493 340 */
bob_tpc 9:046247707ffb 341
bob_tpc 5:e77529f7ede3 342 int gpio_rd()
bob_tpc 5:e77529f7ede3 343 {
bob_tpc 15:713c26178a7d 344 gpio_buffer[2] = ( led_err.read() & 0x01); // read all of the GPIO pins and store in a single byte
bob_tpc 15:713c26178a7d 345 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 346 gpio_buffer[2] |= ((rfid_int.read() << 2) & 0x04);
bob_tpc 9:046247707ffb 347 gpio_buffer[2] |= ((rfid_isp.read() << 3) & 0x08);
bob_tpc 9:046247707ffb 348 gpio_buffer[2] |= ((rfid_rst.read() << 4) & 0x10);
bob_tpc 9:046247707ffb 349 gpio_buffer[2] |= ((rfid_pwr.read() << 5) & 0x20);
bob_tpc 10:55e35536d493 350 gpio_buffer[2] |= ((rfid_hot.read() << 6) & 0x40);
bob_tpc 10:55e35536d493 351 gpio_buffer[2] |= ((prox_int.read() << 7) & 0x80);
bob_tpc 5:e77529f7ede3 352 return ERR_NONE;
bob_tpc 5:e77529f7ede3 353 }
bob_tpc 4:13e3e375c0d3 354
bob_tpc 10:55e35536d493 355
bob_tpc 10:55e35536d493 356 /**
bob_tpc 10:55e35536d493 357 * @name gpio_wr
bob_tpc 10:55e35536d493 358 * @brief sets value of GPIO pins
bob_tpc 10:55e35536d493 359 *
bob_tpc 10:55e35536d493 360 * GPIO signals are defined directly off of the KL25Z.
bob_tpc 10:55e35536d493 361 * The host PC sends the GPIO command over the COM port. With a 0xDD leading byte in the message, the state of the GPIO signals are read and returned.
bob_tpc 10:55e35536d493 362 * This allows a read-modify-write GPIO sequence.
bob_tpc 10:55e35536d493 363 *
bob_tpc 11:984631a6e373 364 * GPIO messages:
bob_tpc 11:984631a6e373 365 * - 0xDD (byte) leading value = 0xDD
bob_tpc 11:984631a6e373 366 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 367 * - data (byte) see below
bob_tpc 11:984631a6e373 368 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 369 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 370 *
bob_tpc 11:984631a6e373 371 * GPIO data bits:
bob_tpc 13:a390c4798a0d 372 * - 0 LED - Error 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 373 * - 1 LED - Comm state 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 374 * - 2 RFID interrupt input 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 375 * - 3 RFID in-system-prog 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 376 * - 4 RFID reset 0 = off, 1 = on (inverted in h/w)
bob_tpc 13:a390c4798a0d 377 * - 5 RFID power enable for first prototype, 0 = off, 1 = on / for production, 0 = on, 1 = off
bob_tpc 13:a390c4798a0d 378 * - 6 RFID over-current 0 = overcurrent detected, 1 = OK
bob_tpc 13:a390c4798a0d 379 * - 7 Proximity interrupt 0 = interrupt, 1 = idle (This pin may not return anything meaningful here. The interrupt is edge triggered).
bob_tpc 10:55e35536d493 380 *
bob_tpc 10:55e35536d493 381 * @param [in/out] gpio_buffer - GPIO states
bob_tpc 10:55e35536d493 382 *
bob_tpc 10:55e35536d493 383 * @retval 0 No error
bob_tpc 10:55e35536d493 384 *
bob_tpc 10:55e35536d493 385 */
bob_tpc 5:e77529f7ede3 386 int gpio_wr()
bob_tpc 5:e77529f7ede3 387 {
bob_tpc 9:046247707ffb 388 if ((gpio_buffer[2] & 0x02) == 0x00)
bob_tpc 9:046247707ffb 389 {
bob_tpc 9:046247707ffb 390 led_com_state = LEDON;
bob_tpc 9:046247707ffb 391 }
bob_tpc 9:046247707ffb 392 else
bob_tpc 9:046247707ffb 393 {
bob_tpc 9:046247707ffb 394 led_com_state = LEDOFF;
bob_tpc 9:046247707ffb 395 }
bob_tpc 9:046247707ffb 396 led_err.write(gpio_buffer[2] & 0x01);
bob_tpc 9:046247707ffb 397 led_com.write(led_com_state);
bob_tpc 9:046247707ffb 398 rfid_int.write(gpio_buffer[2] & 0x04);
bob_tpc 9:046247707ffb 399 rfid_isp.write(gpio_buffer[2] & 0x08);
bob_tpc 9:046247707ffb 400 rfid_rst.write(gpio_buffer[2] & 0x10);
bob_tpc 9:046247707ffb 401 rfid_pwr.write(gpio_buffer[2] & 0x20);
bob_tpc 5:e77529f7ede3 402 return ERR_NONE;
bob_tpc 5:e77529f7ede3 403 }
bob_tpc 5:e77529f7ede3 404
bob_tpc 5:e77529f7ede3 405
bob_tpc 10:55e35536d493 406 /**
bob_tpc 10:55e35536d493 407 * @name eeprom_msg_wr
bob_tpc 10:55e35536d493 408 * @brief writes data to the I2C EEPROM
bob_tpc 10:55e35536d493 409 * @note *** UNTESTED with first prototype ***
bob_tpc 10:55e35536d493 410 *
bob_tpc 10:55e35536d493 411 * The EEPROM is connected to the KL25Z I2C interface.
bob_tpc 10:55e35536d493 412 * The host PC sends the sensor command over the COM port. Messages destined for the EERPOM (0xEE leading byte) are
bob_tpc 10:55e35536d493 413 * forwarded to the EEPROM after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes).
bob_tpc 10:55e35536d493 414 * Firmware re-attaches the leading 0xFF and trailing bytes before sending the response over the
bob_tpc 10:55e35536d493 415 * CDC port to the host PC.
bob_tpc 10:55e35536d493 416 *
bob_tpc 11:984631a6e373 417 * I2C-EEPROM messages:
bob_tpc 11:984631a6e373 418 * - 0xEE (byte) leading value = 0xEE
bob_tpc 11:984631a6e373 419 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 420 * - number of data bytes(byte) 0 to 32 (size of declared buffers)
bob_tpc 11:984631a6e373 421 * - block (byte) lower 3 bits are logically OR'd with the I2C address
bob_tpc 11:984631a6e373 422 * - address (byte) memory location within block
bob_tpc 11:984631a6e373 423 * - data (n bytes) number of data bytes noted above
bob_tpc 11:984631a6e373 424 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 425 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 426 *
bob_tpc 10:55e35536d493 427 * Multiple memory locations can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte.
bob_tpc 10:55e35536d493 428 * Read/Write sequences are defined in the 24LC16B datasheet.
bob_tpc 10:55e35536d493 429 *
bob_tpc 10:55e35536d493 430 * 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 10:55e35536d493 431 *
bob_tpc 10:55e35536d493 432 * @param [in] i2c_buffer - messages to and from the VL6180X and EEPROM
bob_tpc 10:55e35536d493 433 *
bob_tpc 10:55e35536d493 434 * @retval 0 No error
bob_tpc 10:55e35536d493 435 * @retval 1 I2C bus has NAK'd / failure
bob_tpc 10:55e35536d493 436 *
bob_tpc 10:55e35536d493 437 * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above
bob_tpc 10:55e35536d493 438 *
bob_tpc 4:13e3e375c0d3 439 */
bob_tpc 4:13e3e375c0d3 440
bob_tpc 15:713c26178a7d 441 int eeprom_msg_wr() // write proximity I2C register
bob_tpc 4:13e3e375c0d3 442 {
bob_tpc 4:13e3e375c0d3 443 int i2c_err;
bob_tpc 17:1e704604e1ac 444 i2c_err = i2c.write((EEPROM | (i2c_buffer[3] << 1 )), &i2c_buffer[4], i2c_buffer[2] + 1, 0);
bob_tpc 15:713c26178a7d 445 // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end.
bob_tpc 17:1e704604e1ac 446 while ( i2c.write(EEPROM | (i2c_buffer[3] << 1 ))); // wait until write is done (EEPROM will ACK = 0 for single byte i2c.write)
bob_tpc 15:713c26178a7d 447 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 4:13e3e375c0d3 448 }
bob_tpc 3:e8cc286f9b2e 449
bob_tpc 10:55e35536d493 450 /**
bob_tpc 10:55e35536d493 451 * @name eeprom_msg_rd
bob_tpc 10:55e35536d493 452 * @brief read data from the I2C EEPROM
bob_tpc 10:55e35536d493 453 * @note *** UNTESTED with first prototype ***
bob_tpc 10:55e35536d493 454 *
bob_tpc 10:55e35536d493 455 * The EEPROM is connected to the KL25Z I2C interface.
bob_tpc 10:55e35536d493 456 * The host PC sends the sensor command over the COM port. Messages destined for the EERPOM (0xEE leading byte) are
bob_tpc 10:55e35536d493 457 * forwarded to the EEPROM after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes).
bob_tpc 10:55e35536d493 458 * Firmware re-attaches the leading 0xFF and trailing bytes before sending the response over the
bob_tpc 10:55e35536d493 459 * CDC port to the host PC.
bob_tpc 10:55e35536d493 460 *
bob_tpc 11:984631a6e373 461 * I2C-EEPROM messages:
bob_tpc 11:984631a6e373 462 * - 0xEE (byte) leading value = 0xEE
bob_tpc 11:984631a6e373 463 * - r/w# (byte) 0 = write, 1 = read
bob_tpc 11:984631a6e373 464 * - number of data bytes(byte) 0 to 32 (size of declared buffers)
bob_tpc 11:984631a6e373 465 * - block (byte) lower 3 bits are logically OR'd with the I2C address
bob_tpc 11:984631a6e373 466 * - address (byte) memory location within block
bob_tpc 11:984631a6e373 467 * - data (n bytes) number of data bytes noted above
bob_tpc 11:984631a6e373 468 * - end_mark (byte) 0x7E
bob_tpc 11:984631a6e373 469 * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes
bob_tpc 10:55e35536d493 470 *
bob_tpc 10:55e35536d493 471 * Multiple memory locations can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte.
bob_tpc 10:55e35536d493 472 * Read/Write sequences are defined in the 24LC16B datasheet.
bob_tpc 10:55e35536d493 473 *
bob_tpc 10:55e35536d493 474 * 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 10:55e35536d493 475 *
bob_tpc 10:55e35536d493 476 * @param [in/out] i2c_buffer - messages to and from the VL6180X and EEPROM
bob_tpc 10:55e35536d493 477 *
bob_tpc 10:55e35536d493 478 * @retval [none]0 No error
bob_tpc 10:55e35536d493 479 * @retval 1 I2C bus has NAK'd / failure
bob_tpc 10:55e35536d493 480 *
bob_tpc 10:55e35536d493 481 * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above
bob_tpc 10:55e35536d493 482 *
bob_tpc 10:55e35536d493 483 */
bob_tpc 10:55e35536d493 484 int eeprom_msg_rd()
bob_tpc 4:13e3e375c0d3 485 {
bob_tpc 4:13e3e375c0d3 486 int i2c_err;
bob_tpc 17:1e704604e1ac 487 i2c_err = i2c.write((EEPROM | (i2c_buffer[3] << 1)), &i2c_buffer[4], 1, 1);
bob_tpc 15:713c26178a7d 488 // I2C Address & block select, pointer to buffer (just the index), index, number of bytes (for address + data), no stop at end.
bob_tpc 17:1e704604e1ac 489 i2c_err |= i2c.read((EEPROM | (i2c_buffer[3] << 1)), &i2c_buffer[5], i2c_buffer[2], 0);
bob_tpc 15:713c26178a7d 490 // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end.
bob_tpc 15:713c26178a7d 491 return i2c_err; // 0 = ACK received, 1 = NAK/failure
bob_tpc 0:8604e9cc07f2 492 }
bob_tpc 0:8604e9cc07f2 493
bob_tpc 10:55e35536d493 494 /**
bob_tpc 10:55e35536d493 495 * @name main
bob_tpc 10:55e35536d493 496 * @brief main firmware loop
bob_tpc 10:55e35536d493 497 *
bob_tpc 10:55e35536d493 498 * @returns [none]
bob_tpc 10:55e35536d493 499 */
bob_tpc 10:55e35536d493 500 int main(void)
bob_tpc 0:8604e9cc07f2 501 {
bob_tpc 15:713c26178a7d 502 int em_pos = 0; // end of message count - allows multiple 0x7e in message.
bob_tpc 9:046247707ffb 503
bob_tpc 15:713c26178a7d 504 //wait(5.0); // gives a chance to connect the COM port - this can be removed for production
bob_tpc 15:713c26178a7d 505
bob_tpc 15:713c26178a7d 506 init_periph(); // initialize everything
bob_tpc 0:8604e9cc07f2 507
bob_tpc 5:e77529f7ede3 508 while(1)
bob_tpc 0:8604e9cc07f2 509 {
bob_tpc 15:713c26178a7d 510 led_com.write(led_com_state); // turn off communication LED unless it was specifically turned on by GPIO command
bob_tpc 15:713c26178a7d 511 if (prox_irq_state == 1) // process the proximity interrupt
bob_tpc 9:046247707ffb 512 {
bob_tpc 15:713c26178a7d 513 prox_irq_state = 0;
bob_tpc 15:713c26178a7d 514 cdc_buffer_tx[0] = 0xFF; // send message to PC
bob_tpc 15:713c26178a7d 515 cdc_buffer_tx[1] = 0x7E;
bob_tpc 15:713c26178a7d 516 cdc_buffer_tx[2] = 0x0F;
bob_tpc 15:713c26178a7d 517 cdc_buffer_tx[3] = 0xF0;
bob_tpc 15:713c26178a7d 518 cdc.writeBlock(cdc_buffer_tx, 4);
bob_tpc 15:713c26178a7d 519 }
bob_tpc 15:713c26178a7d 520
bob_tpc 15:713c26178a7d 521 if (uart.readable()) // message availalbe from rfid
bob_tpc 18:d128c7020292 522 // while (uart.readable()) // message availalbe from rfid
bob_tpc 15:713c26178a7d 523 {
bob_tpc 15:713c26178a7d 524 rfid_rd();
bob_tpc 15:713c26178a7d 525 em_pos = 0;
bob_tpc 15:713c26178a7d 526 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 9:046247707ffb 527 {
bob_tpc 15:713c26178a7d 528 cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer
bob_tpc 15:713c26178a7d 529 if (cdc_buffer_tx[i] == 0x7E)
bob_tpc 15:713c26178a7d 530 {
bob_tpc 15:713c26178a7d 531 em_pos = (i + 1); // allows 0x7E to appear in the data payload - uses last one for end-mark
bob_tpc 15:713c26178a7d 532 }
bob_tpc 9:046247707ffb 533 }
bob_tpc 15:713c26178a7d 534 if (em_pos == 0)
bob_tpc 5:e77529f7ede3 535 {
bob_tpc 15:713c26178a7d 536 led_err.write(LEDON); // end mark never reached
bob_tpc 5:e77529f7ede3 537 break;
bob_tpc 5:e77529f7ede3 538 }
bob_tpc 15:713c26178a7d 539 cdc.writeBlock(cdc_buffer_tx, (em_pos + 2));
bob_tpc 15:713c26178a7d 540 }
bob_tpc 15:713c26178a7d 541 if (usb_irq_state == 1) // message available from PC
bob_tpc 5:e77529f7ede3 542 {
bob_tpc 15:713c26178a7d 543 led_com.write(LEDON); // Message received - turn on LED
bob_tpc 15:713c26178a7d 544 usb_irq_state = 0;
bob_tpc 15:713c26178a7d 545
bob_tpc 15:713c26178a7d 546 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 15:713c26178a7d 547 {
bob_tpc 15:713c26178a7d 548 if (cdc.readable()) cdc_buffer_rx[i] = cdc._getc(); // read data from USB side
bob_tpc 15:713c26178a7d 549 }
bob_tpc 15:713c26178a7d 550 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 15:713c26178a7d 551 {
bob_tpc 15:713c26178a7d 552 if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message
bob_tpc 5:e77529f7ede3 553 {
bob_tpc 15:713c26178a7d 554 em_pos = (i + 1);
bob_tpc 5:e77529f7ede3 555 }
bob_tpc 15:713c26178a7d 556 }
bob_tpc 15:713c26178a7d 557 if (em_pos == 0) // end mark never reached
bob_tpc 15:713c26178a7d 558 {
bob_tpc 15:713c26178a7d 559 led_err.write(LEDON);
bob_tpc 15:713c26178a7d 560 break;
bob_tpc 15:713c26178a7d 561 }
bob_tpc 15:713c26178a7d 562
bob_tpc 15:713c26178a7d 563 switch(cdc_buffer_rx[0]) // check first byte for "destination"
bob_tpc 15:713c26178a7d 564 {
bob_tpc 15:713c26178a7d 565 case 0xBB: // RFID-FE
bob_tpc 15:713c26178a7d 566 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 15:713c26178a7d 567 {
bob_tpc 15:713c26178a7d 568 uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID
bob_tpc 15:713c26178a7d 569 }
bob_tpc 15:713c26178a7d 570
bob_tpc 15:713c26178a7d 571 status = rfid_wr(); // send buffer to RFID and get response according to RFID board
bob_tpc 15:713c26178a7d 572 break;
bob_tpc 5:e77529f7ede3 573
bob_tpc 15:713c26178a7d 574 case 0xCC: // Proximity Sensor
bob_tpc 15:713c26178a7d 575 led_com.write(LEDON);
bob_tpc 15:713c26178a7d 576 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 5:e77529f7ede3 577 {
bob_tpc 15:713c26178a7d 578 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 5:e77529f7ede3 579 }
bob_tpc 15:713c26178a7d 580
bob_tpc 15:713c26178a7d 581 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 15:713c26178a7d 582 status = prox_msg_rd(); // read the requested data
bob_tpc 15:713c26178a7d 583 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 15:713c26178a7d 584 status = prox_msg_wr(); // send buffer to proximity sensor and get response
bob_tpc 15:713c26178a7d 585
bob_tpc 17:1e704604e1ac 586 if (status) led_err.write(LEDON);
bob_tpc 15:713c26178a7d 587
bob_tpc 15:713c26178a7d 588 em_pos = 0;
bob_tpc 15:713c26178a7d 589 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 590 {
bob_tpc 15:713c26178a7d 591 cdc_buffer_tx[i] = i2c_buffer[i]; // copy RFID response back to USB buffer
bob_tpc 15:713c26178a7d 592 if (cdc_buffer_tx[i] == 0x7E)
bob_tpc 15:713c26178a7d 593 {
bob_tpc 15:713c26178a7d 594 em_pos = (i + 1); // allows 0x7E to appear in the data payload - uses last one for end-mark
bob_tpc 15:713c26178a7d 595 }
bob_tpc 15:713c26178a7d 596 }
bob_tpc 15:713c26178a7d 597 if (em_pos == 0)
bob_tpc 15:713c26178a7d 598 {
bob_tpc 15:713c26178a7d 599 led_err.write(LEDON); // end mark never reached
bob_tpc 5:e77529f7ede3 600 break;
bob_tpc 5:e77529f7ede3 601 }
bob_tpc 15:713c26178a7d 602
bob_tpc 15:713c26178a7d 603 cdc.writeBlock(cdc_buffer_tx, (em_pos + 2));
bob_tpc 15:713c26178a7d 604 led_com.write(LEDOFF);
bob_tpc 15:713c26178a7d 605 break;
bob_tpc 5:e77529f7ede3 606
bob_tpc 15:713c26178a7d 607 case 0xDD: // GPIO (LEDs and RFID-FE control)
bob_tpc 15:713c26178a7d 608
bob_tpc 15:713c26178a7d 609 led_com.write(LEDON);
bob_tpc 15:713c26178a7d 610 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 15:713c26178a7d 611 {
bob_tpc 15:713c26178a7d 612 gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 15:713c26178a7d 613 }
bob_tpc 15:713c26178a7d 614
bob_tpc 15:713c26178a7d 615 if (gpio_buffer[1] == 1) // GPIO read = 1
bob_tpc 15:713c26178a7d 616 status = gpio_rd(); // read the requested data
bob_tpc 15:713c26178a7d 617 else if (gpio_buffer[1] == 0) // GPIO write = 0
bob_tpc 15:713c26178a7d 618 status = gpio_wr(); // send GPIO pin data
bob_tpc 3:e8cc286f9b2e 619
bob_tpc 15:713c26178a7d 620 em_pos = 0;
bob_tpc 15:713c26178a7d 621 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 622 {
bob_tpc 15:713c26178a7d 623 cdc_buffer_tx[i] = gpio_buffer[i]; // copy RFID response back to USB buffer
bob_tpc 15:713c26178a7d 624 if (cdc_buffer_tx[i] == 0x7E)
bob_tpc 15:713c26178a7d 625 {
bob_tpc 15:713c26178a7d 626 em_pos = (i + 1); // allows 0x7E to appear in the data payload - uses last one for end-mark
bob_tpc 15:713c26178a7d 627 }
bob_tpc 5:e77529f7ede3 628 }
bob_tpc 15:713c26178a7d 629 if (em_pos == 0)
bob_tpc 5:e77529f7ede3 630 {
bob_tpc 15:713c26178a7d 631 led_err.write(LEDON); // end mark never reached
bob_tpc 5:e77529f7ede3 632 break;
bob_tpc 5:e77529f7ede3 633 }
bob_tpc 15:713c26178a7d 634
bob_tpc 15:713c26178a7d 635 cdc.writeBlock(cdc_buffer_tx, (em_pos + 2));
bob_tpc 15:713c26178a7d 636 led_com.write(LEDOFF);
bob_tpc 15:713c26178a7d 637
bob_tpc 15:713c26178a7d 638 break;
bob_tpc 15:713c26178a7d 639
bob_tpc 15:713c26178a7d 640 case 0xEE: // Read/write EEPROM
bob_tpc 15:713c26178a7d 641 led_com.write(LEDON);
bob_tpc 15:713c26178a7d 642 for (i = 0; i < sizeof(cdc_buffer_rx); i++)
bob_tpc 15:713c26178a7d 643 {
bob_tpc 15:713c26178a7d 644 i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C
bob_tpc 15:713c26178a7d 645 }
bob_tpc 15:713c26178a7d 646
bob_tpc 15:713c26178a7d 647 if (i2c_buffer[1] == 1) // I2C read = 1
bob_tpc 15:713c26178a7d 648 status = eeprom_msg_rd(); // read the requested data
bob_tpc 15:713c26178a7d 649 else if (i2c_buffer[1] == 0) // I2C write = 0
bob_tpc 15:713c26178a7d 650 status = eeprom_msg_wr(); // send buffer to proximity sensor and get response
bob_tpc 15:713c26178a7d 651
bob_tpc 17:1e704604e1ac 652 if (status) led_err.write(LEDON);
bob_tpc 5:e77529f7ede3 653
bob_tpc 15:713c26178a7d 654 em_pos = 0;
bob_tpc 15:713c26178a7d 655 for (i = 0; i < sizeof(cdc_buffer_tx); i++)
bob_tpc 5:e77529f7ede3 656 {
bob_tpc 15:713c26178a7d 657 cdc_buffer_tx[i] = i2c_buffer[i]; // copy RFID response back to USB buffer
bob_tpc 15:713c26178a7d 658 if (cdc_buffer_tx[i] == 0x7E)
bob_tpc 15:713c26178a7d 659 {
bob_tpc 15:713c26178a7d 660 em_pos = (i + 1); // allows 0x7E to appear in the data payload - uses last one for end-mark
bob_tpc 15:713c26178a7d 661 }
bob_tpc 5:e77529f7ede3 662 }
bob_tpc 15:713c26178a7d 663 if (em_pos == 0)
bob_tpc 5:e77529f7ede3 664 {
bob_tpc 15:713c26178a7d 665 led_err.write(LEDON); // end mark never reached
bob_tpc 5:e77529f7ede3 666 break;
bob_tpc 5:e77529f7ede3 667 }
bob_tpc 15:713c26178a7d 668
bob_tpc 15:713c26178a7d 669 cdc.writeBlock(cdc_buffer_tx, (em_pos + 2));
bob_tpc 15:713c26178a7d 670 led_com.write(LEDOFF);
bob_tpc 15:713c26178a7d 671 break;
bob_tpc 17:1e704604e1ac 672
bob_tpc 15:713c26178a7d 673 default:
bob_tpc 15:713c26178a7d 674 led_err.write(LEDON);
bob_tpc 15:713c26178a7d 675 while(1);
bob_tpc 15:713c26178a7d 676 }
bob_tpc 5:e77529f7ede3 677 }
bob_tpc 15:713c26178a7d 678 led_com.write(LEDOFF);
bob_tpc 0:8604e9cc07f2 679 }
bob_tpc 0:8604e9cc07f2 680 }
bob_tpc 10:55e35536d493 681 //EOF