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 09 18:55:19 2015 +0000
Revision:
19:d3bef4fbab69
Parent:
18:d128c7020292
Child:
22:5707b236cbdb
Included second GPIO byte - see documentation link for bit assignments.

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