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:
Fri Jan 23 01:32:07 2015 +0000
Revision:
10:55e35536d493
Parent:
9:046247707ffb
Child:
11:984631a6e373
Complete source with comments, bit definitions, etc.

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