Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MODSERIAL USBDevice_for_Rev_C_HW mbed
Fork of mbed_sv_firmware_with_init by
main.cpp@11:984631a6e373, 2015-01-23 (annotated)
- Committer:
- bob_tpc
- Date:
- Fri Jan 23 01:52:18 2015 +0000
- Revision:
- 11:984631a6e373
- Parent:
- 10:55e35536d493
- Child:
- 12:d298b41bac0d
Updated formatting on descriptions
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:984631a6e373 | 220 | * I2C-prox messages: |
bob_tpc | 11:984631a6e373 | 221 | * - 0xCC (byte) leading value = 0xCC |
bob_tpc | 11:984631a6e373 | 222 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 223 | * - number of data bytes(byte) 0 to 32 (size of declared buffers) |
bob_tpc | 11:984631a6e373 | 224 | * - index (2 bytes) 12-bit VL6801X register offset, high byte first |
bob_tpc | 11:984631a6e373 | 225 | * - data (n bytes) number of data bytes noted above |
bob_tpc | 11:984631a6e373 | 226 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 227 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 228 | * |
bob_tpc | 10:55e35536d493 | 229 | * 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 | 230 | * VL6180X registers are defined in the sensor datasheet. |
bob_tpc | 10:55e35536d493 | 231 | * |
bob_tpc | 10:55e35536d493 | 232 | * @param [in] i2c_buffer - messages to and from the VL6180X and EEPROM |
bob_tpc | 10:55e35536d493 | 233 | * |
bob_tpc | 10:55e35536d493 | 234 | * @retval 0 No error |
bob_tpc | 10:55e35536d493 | 235 | * @retval 1 I2C bus has NAK'd / failure |
bob_tpc | 10:55e35536d493 | 236 | * |
bob_tpc | 10:55e35536d493 | 237 | * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above |
bob_tpc | 10:55e35536d493 | 238 | * |
bob_tpc | 4:13e3e375c0d3 | 239 | */ |
bob_tpc | 5:e77529f7ede3 | 240 | int prox_msg_wr() // write proximity I2C register |
bob_tpc | 0:8604e9cc07f2 | 241 | { |
bob_tpc | 4:13e3e375c0d3 | 242 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 243 | 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 | 244 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 245 | } |
bob_tpc | 3:e8cc286f9b2e | 246 | |
bob_tpc | 10:55e35536d493 | 247 | /** |
bob_tpc | 10:55e35536d493 | 248 | * @name prox_msg_rd |
bob_tpc | 10:55e35536d493 | 249 | * @brief retrieves response from VL6180X sensor |
bob_tpc | 10:55e35536d493 | 250 | * |
bob_tpc | 10:55e35536d493 | 251 | * Proximity/ALS reader is connected to the KL25Z I2C interface. |
bob_tpc | 10:55e35536d493 | 252 | * 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 | 253 | * forwarded to the proximity/ALS sensor after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes). |
bob_tpc | 10:55e35536d493 | 254 | * 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 | 255 | * CDC port to the host PC. |
bob_tpc | 10:55e35536d493 | 256 | * |
bob_tpc | 11:984631a6e373 | 257 | * I2C-prox messages: |
bob_tpc | 11:984631a6e373 | 258 | * - 0xCC (byte) leading value = 0xCC |
bob_tpc | 11:984631a6e373 | 259 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 260 | * - number of data bytes(byte) 0 to 32 (size of declared buffers) |
bob_tpc | 11:984631a6e373 | 261 | * - index (2 bytes) 12-bit VL6801X register offset, high byte first |
bob_tpc | 11:984631a6e373 | 262 | * - data (n bytes) number of data bytes noted above |
bob_tpc | 11:984631a6e373 | 263 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 264 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 265 | * |
bob_tpc | 10:55e35536d493 | 266 | * 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 | 267 | * VL6180X registers are defined in the sensor datasheet. |
bob_tpc | 10:55e35536d493 | 268 | * |
bob_tpc | 10:55e35536d493 | 269 | * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above |
bob_tpc | 10:55e35536d493 | 270 | * |
bob_tpc | 10:55e35536d493 | 271 | * @retval 0 No error |
bob_tpc | 10:55e35536d493 | 272 | * @retval 1 I2C bus has NAK'd / failure |
bob_tpc | 10:55e35536d493 | 273 | * |
bob_tpc | 10:55e35536d493 | 274 | * |
bob_tpc | 10:55e35536d493 | 275 | */ |
bob_tpc | 4:13e3e375c0d3 | 276 | int prox_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 277 | { |
bob_tpc | 4:13e3e375c0d3 | 278 | int i2c_err; |
bob_tpc | 5:e77529f7ede3 | 279 | 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 | 280 | 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 | 281 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 282 | } |
bob_tpc | 4:13e3e375c0d3 | 283 | |
bob_tpc | 10:55e35536d493 | 284 | /** |
bob_tpc | 10:55e35536d493 | 285 | * @name gpio_rd |
bob_tpc | 10:55e35536d493 | 286 | * @brief retrieves instantaneous value of GPIO pins |
bob_tpc | 10:55e35536d493 | 287 | * |
bob_tpc | 10:55e35536d493 | 288 | * GPIO signals are defined directly off of the KL25Z. |
bob_tpc | 10:55e35536d493 | 289 | * 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 | 290 | * This allows a read-modify-write GPIO sequence. |
bob_tpc | 10:55e35536d493 | 291 | * |
bob_tpc | 11:984631a6e373 | 292 | * GPIO messages: |
bob_tpc | 11:984631a6e373 | 293 | * - 0xDD (byte) leading value = 0xDD |
bob_tpc | 11:984631a6e373 | 294 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 295 | * - data (byte) see below |
bob_tpc | 11:984631a6e373 | 296 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 297 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 298 | * |
bob_tpc | 11:984631a6e373 | 299 | * GPIO data bits: |
bob_tpc | 11:984631a6e373 | 300 | * 0. LED - Error 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 301 | * 1. LED - Comm state 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 302 | * 2. RFID interrupt input 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 303 | * 3. RFID in-system-prog 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 304 | * 4. RFID reset 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 305 | * 5. RFID power enable |
bob_tpc | 11:984631a6e373 | 306 | * + for first prototype, 0 = off, 1 = on |
bob_tpc | 11:984631a6e373 | 307 | * + for production, 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 308 | * 6. RFID over-current 0 = overcurrent detected, 1 = OK |
bob_tpc | 11:984631a6e373 | 309 | * 7. Proximity interrupt 0 = interrupt, 1 = idle (This pin may not return anything meaningful here. The interrupt is edge triggered). |
bob_tpc | 10:55e35536d493 | 310 | * |
bob_tpc | 10:55e35536d493 | 311 | * @param [in/out] gpio_buffer - GPIO states |
bob_tpc | 10:55e35536d493 | 312 | * |
bob_tpc | 10:55e35536d493 | 313 | * @retval 0 No error |
bob_tpc | 10:55e35536d493 | 314 | * |
bob_tpc | 10:55e35536d493 | 315 | */ |
bob_tpc | 9:046247707ffb | 316 | |
bob_tpc | 5:e77529f7ede3 | 317 | int gpio_rd() |
bob_tpc | 5:e77529f7ede3 | 318 | { |
bob_tpc | 9:046247707ffb | 319 | gpio_buffer[2] = ( led_err.read() & 0x01); // read all of the GPIO pins and store in a single byte |
bob_tpc | 9:046247707ffb | 320 | 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 | 321 | gpio_buffer[2] |= ((rfid_int.read() << 2) & 0x04); |
bob_tpc | 9:046247707ffb | 322 | gpio_buffer[2] |= ((rfid_isp.read() << 3) & 0x08); |
bob_tpc | 9:046247707ffb | 323 | gpio_buffer[2] |= ((rfid_rst.read() << 4) & 0x10); |
bob_tpc | 9:046247707ffb | 324 | gpio_buffer[2] |= ((rfid_pwr.read() << 5) & 0x20); |
bob_tpc | 10:55e35536d493 | 325 | gpio_buffer[2] |= ((rfid_hot.read() << 6) & 0x40); |
bob_tpc | 10:55e35536d493 | 326 | gpio_buffer[2] |= ((prox_int.read() << 7) & 0x80); |
bob_tpc | 5:e77529f7ede3 | 327 | return ERR_NONE; |
bob_tpc | 5:e77529f7ede3 | 328 | } |
bob_tpc | 4:13e3e375c0d3 | 329 | |
bob_tpc | 10:55e35536d493 | 330 | |
bob_tpc | 10:55e35536d493 | 331 | /** |
bob_tpc | 10:55e35536d493 | 332 | * @name gpio_wr |
bob_tpc | 10:55e35536d493 | 333 | * @brief sets value of GPIO pins |
bob_tpc | 10:55e35536d493 | 334 | * |
bob_tpc | 10:55e35536d493 | 335 | * GPIO signals are defined directly off of the KL25Z. |
bob_tpc | 10:55e35536d493 | 336 | * 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 | 337 | * This allows a read-modify-write GPIO sequence. |
bob_tpc | 10:55e35536d493 | 338 | * |
bob_tpc | 11:984631a6e373 | 339 | * GPIO messages: |
bob_tpc | 11:984631a6e373 | 340 | * - 0xDD (byte) leading value = 0xDD |
bob_tpc | 11:984631a6e373 | 341 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 342 | * - data (byte) see below |
bob_tpc | 11:984631a6e373 | 343 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 344 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 345 | * |
bob_tpc | 11:984631a6e373 | 346 | * GPIO data bits: |
bob_tpc | 11:984631a6e373 | 347 | * 0. LED - Error 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 348 | * 1. LED - Comm state 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 349 | * 2. RFID interrupt input 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 350 | * 3. RFID in-system-prog 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 351 | * 4. RFID reset 0 = off, 1 = on (inverted in h/w) |
bob_tpc | 11:984631a6e373 | 352 | * 5. RFID power enable |
bob_tpc | 11:984631a6e373 | 353 | * + for first prototype, 0 = off, 1 = on |
bob_tpc | 11:984631a6e373 | 354 | * + for production, 0 = on, 1 = off |
bob_tpc | 11:984631a6e373 | 355 | * 6. RFID over-current READ ONLY, ignored here |
bob_tpc | 11:984631a6e373 | 356 | * 7. Proximity interrupt READ ONLY, ignored here |
bob_tpc | 10:55e35536d493 | 357 | * |
bob_tpc | 10:55e35536d493 | 358 | * @param [in/out] gpio_buffer - GPIO states |
bob_tpc | 10:55e35536d493 | 359 | * |
bob_tpc | 10:55e35536d493 | 360 | * @retval 0 No error |
bob_tpc | 10:55e35536d493 | 361 | * |
bob_tpc | 10:55e35536d493 | 362 | */ |
bob_tpc | 5:e77529f7ede3 | 363 | int gpio_wr() |
bob_tpc | 5:e77529f7ede3 | 364 | { |
bob_tpc | 9:046247707ffb | 365 | if ((gpio_buffer[2] & 0x02) == 0x00) |
bob_tpc | 9:046247707ffb | 366 | { |
bob_tpc | 9:046247707ffb | 367 | led_com_state = LEDON; |
bob_tpc | 9:046247707ffb | 368 | } |
bob_tpc | 9:046247707ffb | 369 | else |
bob_tpc | 9:046247707ffb | 370 | { |
bob_tpc | 9:046247707ffb | 371 | led_com_state = LEDOFF; |
bob_tpc | 9:046247707ffb | 372 | } |
bob_tpc | 9:046247707ffb | 373 | led_err.write(gpio_buffer[2] & 0x01); |
bob_tpc | 9:046247707ffb | 374 | led_com.write(led_com_state); |
bob_tpc | 9:046247707ffb | 375 | rfid_int.write(gpio_buffer[2] & 0x04); |
bob_tpc | 9:046247707ffb | 376 | rfid_isp.write(gpio_buffer[2] & 0x08); |
bob_tpc | 9:046247707ffb | 377 | rfid_rst.write(gpio_buffer[2] & 0x10); |
bob_tpc | 9:046247707ffb | 378 | rfid_pwr.write(gpio_buffer[2] & 0x20); |
bob_tpc | 5:e77529f7ede3 | 379 | return ERR_NONE; |
bob_tpc | 5:e77529f7ede3 | 380 | } |
bob_tpc | 5:e77529f7ede3 | 381 | |
bob_tpc | 5:e77529f7ede3 | 382 | |
bob_tpc | 10:55e35536d493 | 383 | /** |
bob_tpc | 10:55e35536d493 | 384 | * @name eeprom_msg_wr |
bob_tpc | 10:55e35536d493 | 385 | * @brief writes data to the I2C EEPROM |
bob_tpc | 10:55e35536d493 | 386 | * @note *** UNTESTED with first prototype *** |
bob_tpc | 10:55e35536d493 | 387 | * |
bob_tpc | 10:55e35536d493 | 388 | * The EEPROM is connected to the KL25Z I2C interface. |
bob_tpc | 10:55e35536d493 | 389 | * The host PC sends the sensor command over the COM port. Messages destined for the EERPOM (0xEE leading byte) are |
bob_tpc | 10:55e35536d493 | 390 | * forwarded to the EEPROM after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes). |
bob_tpc | 10:55e35536d493 | 391 | * Firmware re-attaches the leading 0xFF and trailing bytes before sending the response over the |
bob_tpc | 10:55e35536d493 | 392 | * CDC port to the host PC. |
bob_tpc | 10:55e35536d493 | 393 | * |
bob_tpc | 11:984631a6e373 | 394 | * I2C-EEPROM messages: |
bob_tpc | 11:984631a6e373 | 395 | * - 0xEE (byte) leading value = 0xEE |
bob_tpc | 11:984631a6e373 | 396 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 397 | * - number of data bytes(byte) 0 to 32 (size of declared buffers) |
bob_tpc | 11:984631a6e373 | 398 | * - block (byte) lower 3 bits are logically OR'd with the I2C address |
bob_tpc | 11:984631a6e373 | 399 | * - address (byte) memory location within block |
bob_tpc | 11:984631a6e373 | 400 | * - data (n bytes) number of data bytes noted above |
bob_tpc | 11:984631a6e373 | 401 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 402 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 403 | * |
bob_tpc | 10:55e35536d493 | 404 | * 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 | 405 | * Read/Write sequences are defined in the 24LC16B datasheet. |
bob_tpc | 10:55e35536d493 | 406 | * |
bob_tpc | 10:55e35536d493 | 407 | * 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 | 408 | * |
bob_tpc | 10:55e35536d493 | 409 | * @param [in] i2c_buffer - messages to and from the VL6180X and EEPROM |
bob_tpc | 10:55e35536d493 | 410 | * |
bob_tpc | 10:55e35536d493 | 411 | * @retval 0 No error |
bob_tpc | 10:55e35536d493 | 412 | * @retval 1 I2C bus has NAK'd / failure |
bob_tpc | 10:55e35536d493 | 413 | * |
bob_tpc | 10:55e35536d493 | 414 | * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above |
bob_tpc | 10:55e35536d493 | 415 | * |
bob_tpc | 4:13e3e375c0d3 | 416 | */ |
bob_tpc | 4:13e3e375c0d3 | 417 | |
bob_tpc | 5:e77529f7ede3 | 418 | int eeprom_msg_wr() // write proximity I2C register |
bob_tpc | 4:13e3e375c0d3 | 419 | { |
bob_tpc | 4:13e3e375c0d3 | 420 | int i2c_err; |
bob_tpc | 10:55e35536d493 | 421 | i2c_err = i2c.write((EEPROM | i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0); |
bob_tpc | 5:e77529f7ede3 | 422 | // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end. |
bob_tpc | 10:55e35536d493 | 423 | 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 | 424 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 425 | } |
bob_tpc | 3:e8cc286f9b2e | 426 | |
bob_tpc | 10:55e35536d493 | 427 | /** |
bob_tpc | 10:55e35536d493 | 428 | * @name eeprom_msg_rd |
bob_tpc | 10:55e35536d493 | 429 | * @brief read data from the I2C EEPROM |
bob_tpc | 10:55e35536d493 | 430 | * @note *** UNTESTED with first prototype *** |
bob_tpc | 10:55e35536d493 | 431 | * |
bob_tpc | 10:55e35536d493 | 432 | * The EEPROM is connected to the KL25Z I2C interface. |
bob_tpc | 10:55e35536d493 | 433 | * The host PC sends the sensor command over the COM port. Messages destined for the EERPOM (0xEE leading byte) are |
bob_tpc | 10:55e35536d493 | 434 | * forwarded to the EEPROM after removing the leading byte and trailing bytes (0x7E endmark plus 2 bytes). |
bob_tpc | 10:55e35536d493 | 435 | * Firmware re-attaches the leading 0xFF and trailing bytes before sending the response over the |
bob_tpc | 10:55e35536d493 | 436 | * CDC port to the host PC. |
bob_tpc | 10:55e35536d493 | 437 | * |
bob_tpc | 11:984631a6e373 | 438 | * I2C-EEPROM messages: |
bob_tpc | 11:984631a6e373 | 439 | * - 0xEE (byte) leading value = 0xEE |
bob_tpc | 11:984631a6e373 | 440 | * - r/w# (byte) 0 = write, 1 = read |
bob_tpc | 11:984631a6e373 | 441 | * - number of data bytes(byte) 0 to 32 (size of declared buffers) |
bob_tpc | 11:984631a6e373 | 442 | * - block (byte) lower 3 bits are logically OR'd with the I2C address |
bob_tpc | 11:984631a6e373 | 443 | * - address (byte) memory location within block |
bob_tpc | 11:984631a6e373 | 444 | * - data (n bytes) number of data bytes noted above |
bob_tpc | 11:984631a6e373 | 445 | * - end_mark (byte) 0x7E |
bob_tpc | 11:984631a6e373 | 446 | * - dummy (2 bytes) values are don't-care - fillers for RFID CRC bytes |
bob_tpc | 10:55e35536d493 | 447 | * |
bob_tpc | 10:55e35536d493 | 448 | * 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 | 449 | * Read/Write sequences are defined in the 24LC16B datasheet. |
bob_tpc | 10:55e35536d493 | 450 | * |
bob_tpc | 10:55e35536d493 | 451 | * 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 | 452 | * |
bob_tpc | 10:55e35536d493 | 453 | * @param [in/out] i2c_buffer - messages to and from the VL6180X and EEPROM |
bob_tpc | 10:55e35536d493 | 454 | * |
bob_tpc | 10:55e35536d493 | 455 | * @retval [none]0 No error |
bob_tpc | 10:55e35536d493 | 456 | * @retval 1 I2C bus has NAK'd / failure |
bob_tpc | 10:55e35536d493 | 457 | * |
bob_tpc | 10:55e35536d493 | 458 | * @param [in/out] i2c_buffer - messages to and from the i2c bus - see above |
bob_tpc | 10:55e35536d493 | 459 | * |
bob_tpc | 10:55e35536d493 | 460 | */ |
bob_tpc | 10:55e35536d493 | 461 | int eeprom_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 462 | { |
bob_tpc | 4:13e3e375c0d3 | 463 | int i2c_err; |
bob_tpc | 10:55e35536d493 | 464 | i2c_err = i2c.write((EEPROM | i2c_buffer[3]), &i2c_buffer[4], 1, 1); |
bob_tpc | 5:e77529f7ede3 | 465 | // 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 | 466 | i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0); |
bob_tpc | 5:e77529f7ede3 | 467 | // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end. |
bob_tpc | 5:e77529f7ede3 | 468 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 0:8604e9cc07f2 | 469 | } |
bob_tpc | 0:8604e9cc07f2 | 470 | |
bob_tpc | 5:e77529f7ede3 | 471 | |
bob_tpc | 10:55e35536d493 | 472 | /** |
bob_tpc | 10:55e35536d493 | 473 | * @name main |
bob_tpc | 10:55e35536d493 | 474 | * @brief main firmware loop |
bob_tpc | 10:55e35536d493 | 475 | * |
bob_tpc | 10:55e35536d493 | 476 | * @returns [none] |
bob_tpc | 10:55e35536d493 | 477 | */ |
bob_tpc | 10:55e35536d493 | 478 | int main(void) |
bob_tpc | 0:8604e9cc07f2 | 479 | { |
bob_tpc | 9:046247707ffb | 480 | wait(5.0); // gives a chance to connect the COM port - this can be removed for production |
bob_tpc | 9:046247707ffb | 481 | |
bob_tpc | 10:55e35536d493 | 482 | init_periph(); // initialize everything |
bob_tpc | 0:8604e9cc07f2 | 483 | |
bob_tpc | 5:e77529f7ede3 | 484 | while(1) |
bob_tpc | 0:8604e9cc07f2 | 485 | { |
bob_tpc | 9:046247707ffb | 486 | led_com.write(led_com_state); // turn off communication LED unless it was specifically turned on by GPIO command |
bob_tpc | 10:55e35536d493 | 487 | while(!cdc.readable()) // spin here until a message comes in from the host PC |
bob_tpc | 9:046247707ffb | 488 | { |
bob_tpc | 10:55e35536d493 | 489 | if(prox_irq_state == 1) // process the interrupt if it occurs while waiting for PC message |
bob_tpc | 9:046247707ffb | 490 | { |
bob_tpc | 9:046247707ffb | 491 | prox_irq_state = 0; |
bob_tpc | 9:046247707ffb | 492 | cdc.putc(0xFF); |
bob_tpc | 9:046247707ffb | 493 | cdc.putc(0x7E); |
bob_tpc | 9:046247707ffb | 494 | cdc.putc(0x0F); |
bob_tpc | 9:046247707ffb | 495 | cdc.putc(0xF0); |
bob_tpc | 9:046247707ffb | 496 | } |
bob_tpc | 10:55e35536d493 | 497 | } |
bob_tpc | 5:e77529f7ede3 | 498 | led_com.write(LEDON); // Message received - turn on LED |
bob_tpc | 5:e77529f7ede3 | 499 | bool end_mark = FALSE; |
bob_tpc | 5:e77529f7ede3 | 500 | uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start |
bob_tpc | 5:e77529f7ede3 | 501 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 502 | { |
bob_tpc | 5:e77529f7ede3 | 503 | cdc_buffer_rx[i] = cdc.getc(); // read data from USB side |
bob_tpc | 5:e77529f7ede3 | 504 | |
bob_tpc | 5:e77529f7ede3 | 505 | if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 506 | { |
bob_tpc | 5:e77529f7ede3 | 507 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 508 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 509 | } |
bob_tpc | 5:e77529f7ede3 | 510 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 511 | { |
bob_tpc | 5:e77529f7ede3 | 512 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 513 | break; |
bob_tpc | 5:e77529f7ede3 | 514 | } |
bob_tpc | 0:8604e9cc07f2 | 515 | } |
bob_tpc | 0:8604e9cc07f2 | 516 | |
bob_tpc | 10:55e35536d493 | 517 | switch(cdc_buffer_rx[0]) // check first byte for "destination" |
bob_tpc | 5:e77529f7ede3 | 518 | { |
bob_tpc | 5:e77529f7ede3 | 519 | case 0xBB: // RFID-FE |
bob_tpc | 5:e77529f7ede3 | 520 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 521 | { |
bob_tpc | 5:e77529f7ede3 | 522 | uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID |
bob_tpc | 5:e77529f7ede3 | 523 | } |
bob_tpc | 5:e77529f7ede3 | 524 | |
bob_tpc | 5:e77529f7ede3 | 525 | status = rfid_msg(); // send buffer to RFID and get response according to RFID board |
bob_tpc | 0:8604e9cc07f2 | 526 | |
bob_tpc | 5:e77529f7ede3 | 527 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 528 | { |
bob_tpc | 5:e77529f7ede3 | 529 | cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer |
bob_tpc | 5:e77529f7ede3 | 530 | } |
bob_tpc | 5:e77529f7ede3 | 531 | |
bob_tpc | 5:e77529f7ede3 | 532 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 533 | { |
bob_tpc | 5:e77529f7ede3 | 534 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 535 | |
bob_tpc | 5:e77529f7ede3 | 536 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 537 | { |
bob_tpc | 5:e77529f7ede3 | 538 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 539 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 540 | } |
bob_tpc | 5:e77529f7ede3 | 541 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 542 | { |
bob_tpc | 5:e77529f7ede3 | 543 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 544 | break; |
bob_tpc | 5:e77529f7ede3 | 545 | } |
bob_tpc | 5:e77529f7ede3 | 546 | } |
bob_tpc | 5:e77529f7ede3 | 547 | break; |
bob_tpc | 0:8604e9cc07f2 | 548 | |
bob_tpc | 5:e77529f7ede3 | 549 | case 0xCC: // Proximity Sensor |
bob_tpc | 5:e77529f7ede3 | 550 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 551 | { |
bob_tpc | 5:e77529f7ede3 | 552 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 0:8604e9cc07f2 | 553 | } |
bob_tpc | 5:e77529f7ede3 | 554 | |
bob_tpc | 5:e77529f7ede3 | 555 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 5:e77529f7ede3 | 556 | status = prox_msg_rd(); // read the requested data |
bob_tpc | 5:e77529f7ede3 | 557 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 5:e77529f7ede3 | 558 | status = prox_msg_wr(); // send buffer to proximity sensor and get response |
bob_tpc | 5:e77529f7ede3 | 559 | |
bob_tpc | 5:e77529f7ede3 | 560 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 561 | { |
bob_tpc | 5:e77529f7ede3 | 562 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 0:8604e9cc07f2 | 563 | } |
bob_tpc | 3:e8cc286f9b2e | 564 | |
bob_tpc | 5:e77529f7ede3 | 565 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 566 | { |
bob_tpc | 5:e77529f7ede3 | 567 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 568 | |
bob_tpc | 5:e77529f7ede3 | 569 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 570 | { |
bob_tpc | 5:e77529f7ede3 | 571 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 572 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 573 | } |
bob_tpc | 5:e77529f7ede3 | 574 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 575 | { |
bob_tpc | 5:e77529f7ede3 | 576 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 577 | break; |
bob_tpc | 5:e77529f7ede3 | 578 | } |
bob_tpc | 5:e77529f7ede3 | 579 | } |
bob_tpc | 5:e77529f7ede3 | 580 | break; |
bob_tpc | 3:e8cc286f9b2e | 581 | |
bob_tpc | 5:e77529f7ede3 | 582 | case 0xDD: // GPIO (LEDs and RFID-FE control) |
bob_tpc | 5:e77529f7ede3 | 583 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 584 | { |
bob_tpc | 5:e77529f7ede3 | 585 | gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 5:e77529f7ede3 | 586 | } |
bob_tpc | 3:e8cc286f9b2e | 587 | |
bob_tpc | 6:2941452a0e6d | 588 | if (gpio_buffer[1] == 1) // GPIO read = 1 |
bob_tpc | 5:e77529f7ede3 | 589 | status = gpio_rd(); // read the requested data |
bob_tpc | 6:2941452a0e6d | 590 | else if (gpio_buffer[1] == 0) // GPIO write = 0 |
bob_tpc | 6:2941452a0e6d | 591 | status = gpio_wr(); // send GPIO pin data |
bob_tpc | 5:e77529f7ede3 | 592 | |
bob_tpc | 5:e77529f7ede3 | 593 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 594 | { |
bob_tpc | 6:2941452a0e6d | 595 | cdc_buffer_tx[i] = gpio_buffer[i]; // copy GPIO response back to USB buffer |
bob_tpc | 3:e8cc286f9b2e | 596 | } |
bob_tpc | 5:e77529f7ede3 | 597 | |
bob_tpc | 5:e77529f7ede3 | 598 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 599 | { |
bob_tpc | 5:e77529f7ede3 | 600 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 601 | |
bob_tpc | 5:e77529f7ede3 | 602 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 603 | { |
bob_tpc | 5:e77529f7ede3 | 604 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 605 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 606 | } |
bob_tpc | 5:e77529f7ede3 | 607 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 608 | { |
bob_tpc | 5:e77529f7ede3 | 609 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 610 | break; |
bob_tpc | 5:e77529f7ede3 | 611 | } |
bob_tpc | 3:e8cc286f9b2e | 612 | } |
bob_tpc | 5:e77529f7ede3 | 613 | break; |
bob_tpc | 5:e77529f7ede3 | 614 | |
bob_tpc | 5:e77529f7ede3 | 615 | case 0xEE: // Read/write EEPROM |
bob_tpc | 5:e77529f7ede3 | 616 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 617 | { |
bob_tpc | 5:e77529f7ede3 | 618 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 5:e77529f7ede3 | 619 | } |
bob_tpc | 4:13e3e375c0d3 | 620 | |
bob_tpc | 5:e77529f7ede3 | 621 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 5:e77529f7ede3 | 622 | status = gpio_rd(); // read the gpio pins |
bob_tpc | 5:e77529f7ede3 | 623 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 5:e77529f7ede3 | 624 | status = gpio_wr(); // write gpio pins |
bob_tpc | 4:13e3e375c0d3 | 625 | |
bob_tpc | 5:e77529f7ede3 | 626 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 627 | { |
bob_tpc | 5:e77529f7ede3 | 628 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 5:e77529f7ede3 | 629 | } |
bob_tpc | 4:13e3e375c0d3 | 630 | |
bob_tpc | 5:e77529f7ede3 | 631 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 4:13e3e375c0d3 | 632 | { |
bob_tpc | 5:e77529f7ede3 | 633 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 634 | |
bob_tpc | 5:e77529f7ede3 | 635 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 636 | { |
bob_tpc | 5:e77529f7ede3 | 637 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 638 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 639 | } |
bob_tpc | 5:e77529f7ede3 | 640 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 641 | { |
bob_tpc | 5:e77529f7ede3 | 642 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 643 | break; |
bob_tpc | 5:e77529f7ede3 | 644 | } |
bob_tpc | 5:e77529f7ede3 | 645 | } |
bob_tpc | 5:e77529f7ede3 | 646 | break; |
bob_tpc | 5:e77529f7ede3 | 647 | default: |
bob_tpc | 5:e77529f7ede3 | 648 | return ERR_CDC_BAD_CMD; |
bob_tpc | 5:e77529f7ede3 | 649 | } |
bob_tpc | 0:8604e9cc07f2 | 650 | } |
bob_tpc | 0:8604e9cc07f2 | 651 | } |
bob_tpc | 10:55e35536d493 | 652 | //EOF |