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