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@4:13e3e375c0d3, 2015-01-20 (annotated)
- Committer:
- bob_tpc
- Date:
- Tue Jan 20 22:26:26 2015 +0000
- Revision:
- 4:13e3e375c0d3
- Parent:
- 3:e8cc286f9b2e
- Child:
- 5:e77529f7ede3
I2C EEPROM and Proximity commands added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bob_tpc | 0:8604e9cc07f2 | 1 | #include "mbed.h" |
bob_tpc | 0:8604e9cc07f2 | 2 | #include "USBSerial.h" |
bob_tpc | 0:8604e9cc07f2 | 3 | #include "MODSERIAL.h" |
bob_tpc | 0:8604e9cc07f2 | 4 | #include "InterruptIn.h" |
bob_tpc | 0:8604e9cc07f2 | 5 | |
bob_tpc | 0:8604e9cc07f2 | 6 | // Constants |
bob_tpc | 0:8604e9cc07f2 | 7 | #define LEDON 0 // Low active for LEDs - turns LED on |
bob_tpc | 0:8604e9cc07f2 | 8 | #define LEDOFF 1 // Low active for LEDs - turns LED off |
bob_tpc | 0:8604e9cc07f2 | 9 | #define TRUE 1 |
bob_tpc | 0:8604e9cc07f2 | 10 | #define FALSE 0 |
bob_tpc | 0:8604e9cc07f2 | 11 | |
bob_tpc | 0:8604e9cc07f2 | 12 | |
bob_tpc | 0:8604e9cc07f2 | 13 | // Error return values |
bob_tpc | 0:8604e9cc07f2 | 14 | #define ERR_NONE 0 // Success |
bob_tpc | 1:bd988d267998 | 15 | #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 | 16 | #define ERR_CDC_NO_TX_ENDMARK 2 // message for no endmark on message to PC |
bob_tpc | 0:8604e9cc07f2 | 17 | #define ERR_UART_NOT_WRITEABLE 3 // UART has no buffer space |
bob_tpc | 1:bd988d267998 | 18 | #define ERR_UART_NO_TX_ENDMARK 4 // message for UART has no 0x7E end-mark |
bob_tpc | 0:8604e9cc07f2 | 19 | #define ERR_UART_NO_RX_ENDMARK 5 // message received from UART has no end-mark |
bob_tpc | 4:13e3e375c0d3 | 20 | #define ERR_I2C_NOT_WRITEABLE 6 // UART has no buffer space |
bob_tpc | 4:13e3e375c0d3 | 21 | #define ERR_I2C_NO_TX_ENDMARK 7 // message for UART has no 0x7E end-mark |
bob_tpc | 4:13e3e375c0d3 | 22 | #define ERR_I2C_NO_RX_ENDMARK 8 // message received from UART has no end-mark |
bob_tpc | 4:13e3e375c0d3 | 23 | #define ERR_NOT_IMPLEMENTED 255 // method has not yet been implemented |
bob_tpc | 0:8604e9cc07f2 | 24 | |
bob_tpc | 0:8604e9cc07f2 | 25 | |
bob_tpc | 0:8604e9cc07f2 | 26 | // I2C addresses |
bob_tpc | 0:8604e9cc07f2 | 27 | #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits |
bob_tpc | 0:8604e9cc07f2 | 28 | #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted |
bob_tpc | 0:8604e9cc07f2 | 29 | |
bob_tpc | 0:8604e9cc07f2 | 30 | // UART-RFID baud rate |
bob_tpc | 0:8604e9cc07f2 | 31 | #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps |
bob_tpc | 0:8604e9cc07f2 | 32 | |
bob_tpc | 0:8604e9cc07f2 | 33 | // Peripherals |
bob_tpc | 0:8604e9cc07f2 | 34 | USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers. |
bob_tpc | 0:8604e9cc07f2 | 35 | MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board |
bob_tpc | 0:8604e9cc07f2 | 36 | I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above) |
bob_tpc | 0:8604e9cc07f2 | 37 | |
bob_tpc | 0:8604e9cc07f2 | 38 | // GPIO signals |
bob_tpc | 0:8604e9cc07f2 | 39 | DigitalOut led_err(PTC1); // Red LED shows error condition (active low) |
bob_tpc | 0:8604e9cc07f2 | 40 | DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low) |
bob_tpc | 0:8604e9cc07f2 | 41 | DigitalOut rfid_int(PTD4); // RFID FE power control (active high) |
bob_tpc | 0:8604e9cc07f2 | 42 | DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high) |
bob_tpc | 0:8604e9cc07f2 | 43 | DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high) |
bob_tpc | 0:8604e9cc07f2 | 44 | DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 0:8604e9cc07f2 | 45 | DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low) |
bob_tpc | 0:8604e9cc07f2 | 46 | InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low) |
bob_tpc | 0:8604e9cc07f2 | 47 | |
bob_tpc | 0:8604e9cc07f2 | 48 | // buffers & variables |
bob_tpc | 0:8604e9cc07f2 | 49 | uint8_t cdc_buffer_rx[32]; // buffers for cdc (USB-Serial port on PC) |
bob_tpc | 0:8604e9cc07f2 | 50 | uint8_t cdc_buffer_tx[32]; |
bob_tpc | 0:8604e9cc07f2 | 51 | uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board) |
bob_tpc | 0:8604e9cc07f2 | 52 | uint8_t uart_buffer_tx[32]; |
bob_tpc | 4:13e3e375c0d3 | 53 | char i2c_buffer[264]; // 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 | 54 | int i, j; // index variables |
bob_tpc | 0:8604e9cc07f2 | 55 | int status = 0x00; // return value |
bob_tpc | 0:8604e9cc07f2 | 56 | |
bob_tpc | 0:8604e9cc07f2 | 57 | int prox_irq(void) |
bob_tpc | 0:8604e9cc07f2 | 58 | { |
bob_tpc | 0:8604e9cc07f2 | 59 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 60 | } |
bob_tpc | 0:8604e9cc07f2 | 61 | |
bob_tpc | 0:8604e9cc07f2 | 62 | int init_periph(void) |
bob_tpc | 0:8604e9cc07f2 | 63 | { |
bob_tpc | 0:8604e9cc07f2 | 64 | // Set up peripherals |
bob_tpc | 0:8604e9cc07f2 | 65 | // RFID |
bob_tpc | 0:8604e9cc07f2 | 66 | uart.baud(RFIDBAUD); // RFID-FE baud rate |
bob_tpc | 0:8604e9cc07f2 | 67 | |
bob_tpc | 0:8604e9cc07f2 | 68 | rfid_int = 0; // RFID FE power control (active high) |
bob_tpc | 0:8604e9cc07f2 | 69 | rfid_isp = 0; // RFID FE In-System Programming (active high) |
bob_tpc | 0:8604e9cc07f2 | 70 | rfid_rst = 1; // RFID FE Reset (active high) |
bob_tpc | 0:8604e9cc07f2 | 71 | rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 2:efaf8aee55df | 72 | wait(0.25); // wait 250ms before... |
bob_tpc | 0:8604e9cc07f2 | 73 | rfid_rst = 0; // ... taking RFID out of reset |
bob_tpc | 0:8604e9cc07f2 | 74 | |
bob_tpc | 0:8604e9cc07f2 | 75 | // Prox |
bob_tpc | 3:e8cc286f9b2e | 76 | i2c.frequency(400000); // I2C speed = 400Kbps |
bob_tpc | 2:efaf8aee55df | 77 | prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU |
bob_tpc | 2:efaf8aee55df | 78 | |
bob_tpc | 2:efaf8aee55df | 79 | |
bob_tpc | 0:8604e9cc07f2 | 80 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 81 | } |
bob_tpc | 0:8604e9cc07f2 | 82 | |
bob_tpc | 4:13e3e375c0d3 | 83 | /* |
bob_tpc | 4:13e3e375c0d3 | 84 | RFID messages are as defined in the RFID-FE manual. |
bob_tpc | 4:13e3e375c0d3 | 85 | */ |
bob_tpc | 0:8604e9cc07f2 | 86 | int rfid_msg(void) |
bob_tpc | 0:8604e9cc07f2 | 87 | { |
bob_tpc | 0:8604e9cc07f2 | 88 | bool end_mark = FALSE; |
bob_tpc | 0:8604e9cc07f2 | 89 | int i; |
bob_tpc | 0:8604e9cc07f2 | 90 | uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 91 | |
bob_tpc | 0:8604e9cc07f2 | 92 | uart.txBufferFlush(); // clear out UART buffers |
bob_tpc | 0:8604e9cc07f2 | 93 | uart.rxBufferFlush(); |
bob_tpc | 0:8604e9cc07f2 | 94 | |
bob_tpc | 0:8604e9cc07f2 | 95 | for (int i = 0; i < sizeof(uart_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 96 | { |
bob_tpc | 0:8604e9cc07f2 | 97 | if (!uart.writeable()) return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error |
bob_tpc | 0:8604e9cc07f2 | 98 | uart.putc(uart_buffer_tx[i]); // send uart message |
bob_tpc | 0:8604e9cc07f2 | 99 | |
bob_tpc | 0:8604e9cc07f2 | 100 | if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 101 | { |
bob_tpc | 0:8604e9cc07f2 | 102 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 0:8604e9cc07f2 | 103 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 104 | } |
bob_tpc | 0:8604e9cc07f2 | 105 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 106 | { |
bob_tpc | 0:8604e9cc07f2 | 107 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 108 | break; |
bob_tpc | 0:8604e9cc07f2 | 109 | } |
bob_tpc | 0:8604e9cc07f2 | 110 | } |
bob_tpc | 0:8604e9cc07f2 | 111 | |
bob_tpc | 0:8604e9cc07f2 | 112 | end_mark = FALSE; |
bob_tpc | 3:e8cc286f9b2e | 113 | //wait(0.5); // debug |
bob_tpc | 3:e8cc286f9b2e | 114 | while(!uart.readable()); // wait for data from rfid |
bob_tpc | 0:8604e9cc07f2 | 115 | crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 116 | for (i = 0; i < sizeof(uart_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 117 | { |
bob_tpc | 0:8604e9cc07f2 | 118 | uart_buffer_rx[i] = uart.getc(); // read a character |
bob_tpc | 0:8604e9cc07f2 | 119 | // cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug |
bob_tpc | 0:8604e9cc07f2 | 120 | |
bob_tpc | 0:8604e9cc07f2 | 121 | if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message |
bob_tpc | 0:8604e9cc07f2 | 122 | { |
bob_tpc | 0:8604e9cc07f2 | 123 | crcCount = 2; // two more bytes for crc |
bob_tpc | 0:8604e9cc07f2 | 124 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 125 | } |
bob_tpc | 0:8604e9cc07f2 | 126 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 127 | { |
bob_tpc | 0:8604e9cc07f2 | 128 | if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK; |
bob_tpc | 0:8604e9cc07f2 | 129 | break; |
bob_tpc | 0:8604e9cc07f2 | 130 | } |
bob_tpc | 0:8604e9cc07f2 | 131 | } |
bob_tpc | 0:8604e9cc07f2 | 132 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 133 | } |
bob_tpc | 0:8604e9cc07f2 | 134 | |
bob_tpc | 0:8604e9cc07f2 | 135 | |
bob_tpc | 4:13e3e375c0d3 | 136 | /* |
bob_tpc | 4:13e3e375c0d3 | 137 | I2C-prox messages = 0xCC, r/w, number of data bytes, index (2 bytes), data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID |
bob_tpc | 4:13e3e375c0d3 | 138 | |
bob_tpc | 4:13e3e375c0d3 | 139 | Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr(). Location address increments for each byte. |
bob_tpc | 4:13e3e375c0d3 | 140 | */ |
bob_tpc | 4:13e3e375c0d3 | 141 | |
bob_tpc | 4:13e3e375c0d3 | 142 | int prox_msg_wr() // write proximity I2C register |
bob_tpc | 0:8604e9cc07f2 | 143 | { |
bob_tpc | 4:13e3e375c0d3 | 144 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 145 | 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 | 4:13e3e375c0d3 | 146 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 147 | } |
bob_tpc | 3:e8cc286f9b2e | 148 | |
bob_tpc | 4:13e3e375c0d3 | 149 | int prox_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 150 | { |
bob_tpc | 4:13e3e375c0d3 | 151 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 152 | 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 | 4:13e3e375c0d3 | 153 | 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 | 4:13e3e375c0d3 | 154 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 155 | } |
bob_tpc | 4:13e3e375c0d3 | 156 | |
bob_tpc | 4:13e3e375c0d3 | 157 | |
bob_tpc | 4:13e3e375c0d3 | 158 | /* |
bob_tpc | 4:13e3e375c0d3 | 159 | I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID |
bob_tpc | 4:13e3e375c0d3 | 160 | |
bob_tpc | 4:13e3e375c0d3 | 161 | Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte. |
bob_tpc | 4:13e3e375c0d3 | 162 | 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 | 4:13e3e375c0d3 | 163 | */ |
bob_tpc | 4:13e3e375c0d3 | 164 | |
bob_tpc | 4:13e3e375c0d3 | 165 | int eeprom_msg_wr() // write proximity I2C register |
bob_tpc | 4:13e3e375c0d3 | 166 | { |
bob_tpc | 4:13e3e375c0d3 | 167 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 168 | i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0); // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end. |
bob_tpc | 4:13e3e375c0d3 | 169 | while (!i2c.write(EEPROM || i2c_buffer[3])); // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write) |
bob_tpc | 4:13e3e375c0d3 | 170 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 171 | } |
bob_tpc | 3:e8cc286f9b2e | 172 | |
bob_tpc | 4:13e3e375c0d3 | 173 | int eeprom_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 174 | { |
bob_tpc | 4:13e3e375c0d3 | 175 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 176 | i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], 1, 1); // I2C Address & block select, pointer to buffer (just the index), index, number of bytes (for address + data), no stop at end. |
bob_tpc | 4:13e3e375c0d3 | 177 | i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0); // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end. |
bob_tpc | 4:13e3e375c0d3 | 178 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 0:8604e9cc07f2 | 179 | } |
bob_tpc | 0:8604e9cc07f2 | 180 | |
bob_tpc | 0:8604e9cc07f2 | 181 | int gpio_msg() |
bob_tpc | 0:8604e9cc07f2 | 182 | { |
bob_tpc | 0:8604e9cc07f2 | 183 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 184 | } |
bob_tpc | 0:8604e9cc07f2 | 185 | |
bob_tpc | 0:8604e9cc07f2 | 186 | int main() |
bob_tpc | 0:8604e9cc07f2 | 187 | { |
bob_tpc | 0:8604e9cc07f2 | 188 | // initialize everything |
bob_tpc | 0:8604e9cc07f2 | 189 | |
bob_tpc | 3:e8cc286f9b2e | 190 | wait(3.0); // debug - gives some time to start terminal program and open COM port |
bob_tpc | 3:e8cc286f9b2e | 191 | |
bob_tpc | 0:8604e9cc07f2 | 192 | init_periph(); |
bob_tpc | 0:8604e9cc07f2 | 193 | |
bob_tpc | 3:e8cc286f9b2e | 194 | //cdc.printf("Starting...\n\r"); // debug |
bob_tpc | 0:8604e9cc07f2 | 195 | |
bob_tpc | 2:efaf8aee55df | 196 | while(!cdc.readable()); // spin here until a message comes in from the host PC |
bob_tpc | 0:8604e9cc07f2 | 197 | bool end_mark = FALSE; |
bob_tpc | 2:efaf8aee55df | 198 | uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start |
bob_tpc | 3:e8cc286f9b2e | 199 | //cdc.printf("\n\rCDC Input: "); // debug |
bob_tpc | 0:8604e9cc07f2 | 200 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 201 | { |
bob_tpc | 2:efaf8aee55df | 202 | cdc_buffer_rx[i] = cdc.getc(); // read data from USB side |
bob_tpc | 0:8604e9cc07f2 | 203 | |
bob_tpc | 2:efaf8aee55df | 204 | //cdc.printf("%X, ",cdc_buffer_rx[i]); // debug |
bob_tpc | 0:8604e9cc07f2 | 205 | |
bob_tpc | 2:efaf8aee55df | 206 | if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 207 | { |
bob_tpc | 2:efaf8aee55df | 208 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 2:efaf8aee55df | 209 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 210 | } |
bob_tpc | 2:efaf8aee55df | 211 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 212 | { |
bob_tpc | 2:efaf8aee55df | 213 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 214 | break; |
bob_tpc | 0:8604e9cc07f2 | 215 | } |
bob_tpc | 0:8604e9cc07f2 | 216 | } |
bob_tpc | 0:8604e9cc07f2 | 217 | |
bob_tpc | 0:8604e9cc07f2 | 218 | switch(cdc_buffer_rx[0]) |
bob_tpc | 0:8604e9cc07f2 | 219 | { |
bob_tpc | 2:efaf8aee55df | 220 | case 0xBB: // RFID-FE |
bob_tpc | 0:8604e9cc07f2 | 221 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 222 | { |
bob_tpc | 2:efaf8aee55df | 223 | uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID |
bob_tpc | 0:8604e9cc07f2 | 224 | } |
bob_tpc | 0:8604e9cc07f2 | 225 | |
bob_tpc | 2:efaf8aee55df | 226 | status = rfid_msg(); // send buffer to RFID and get response according to RFID board |
bob_tpc | 0:8604e9cc07f2 | 227 | |
bob_tpc | 0:8604e9cc07f2 | 228 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 229 | { |
bob_tpc | 2:efaf8aee55df | 230 | cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer |
bob_tpc | 0:8604e9cc07f2 | 231 | } |
bob_tpc | 0:8604e9cc07f2 | 232 | |
bob_tpc | 2:efaf8aee55df | 233 | //cdc.printf("\n\rRFID Response: "); // debug |
bob_tpc | 0:8604e9cc07f2 | 234 | |
bob_tpc | 0:8604e9cc07f2 | 235 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 236 | { |
bob_tpc | 4:13e3e375c0d3 | 237 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 0:8604e9cc07f2 | 238 | |
bob_tpc | 2:efaf8aee55df | 239 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 240 | { |
bob_tpc | 2:efaf8aee55df | 241 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 2:efaf8aee55df | 242 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 243 | } |
bob_tpc | 2:efaf8aee55df | 244 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 245 | { |
bob_tpc | 0:8604e9cc07f2 | 246 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 247 | break; |
bob_tpc | 0:8604e9cc07f2 | 248 | } |
bob_tpc | 0:8604e9cc07f2 | 249 | } |
bob_tpc | 0:8604e9cc07f2 | 250 | break; |
bob_tpc | 4:13e3e375c0d3 | 251 | case 0xCC: |
bob_tpc | 4:13e3e375c0d3 | 252 | //I2C-prox messages = 0xCC, r/w, number of data bytes, index, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID // Proximity Sensor |
bob_tpc | 4:13e3e375c0d3 | 253 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 3:e8cc286f9b2e | 254 | { |
bob_tpc | 4:13e3e375c0d3 | 255 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 3:e8cc286f9b2e | 256 | } |
bob_tpc | 3:e8cc286f9b2e | 257 | |
bob_tpc | 4:13e3e375c0d3 | 258 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 4:13e3e375c0d3 | 259 | status = prox_msg_rd(); // read the requested data |
bob_tpc | 4:13e3e375c0d3 | 260 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 4:13e3e375c0d3 | 261 | status = prox_msg_wr(); // send buffer to proximity sensor and get response |
bob_tpc | 3:e8cc286f9b2e | 262 | |
bob_tpc | 3:e8cc286f9b2e | 263 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 264 | { |
bob_tpc | 4:13e3e375c0d3 | 265 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 3:e8cc286f9b2e | 266 | } |
bob_tpc | 3:e8cc286f9b2e | 267 | |
bob_tpc | 3:e8cc286f9b2e | 268 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 269 | { |
bob_tpc | 4:13e3e375c0d3 | 270 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 3:e8cc286f9b2e | 271 | |
bob_tpc | 3:e8cc286f9b2e | 272 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 3:e8cc286f9b2e | 273 | { |
bob_tpc | 3:e8cc286f9b2e | 274 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 3:e8cc286f9b2e | 275 | end_mark = TRUE; // end mark was reached |
bob_tpc | 3:e8cc286f9b2e | 276 | } |
bob_tpc | 3:e8cc286f9b2e | 277 | if (crcCount-- == 0) // end of message |
bob_tpc | 3:e8cc286f9b2e | 278 | { |
bob_tpc | 3:e8cc286f9b2e | 279 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 3:e8cc286f9b2e | 280 | break; |
bob_tpc | 3:e8cc286f9b2e | 281 | } |
bob_tpc | 3:e8cc286f9b2e | 282 | } |
bob_tpc | 0:8604e9cc07f2 | 283 | break; |
bob_tpc | 2:efaf8aee55df | 284 | case 0xDD: // GPIO (LEDs and RFID-FE control |
bob_tpc | 0:8604e9cc07f2 | 285 | gpio_msg(); |
bob_tpc | 0:8604e9cc07f2 | 286 | break; |
bob_tpc | 2:efaf8aee55df | 287 | case 0xEE: // Read/write EEPROM |
bob_tpc | 4:13e3e375c0d3 | 288 | /* |
bob_tpc | 4:13e3e375c0d3 | 289 | I2C-EEPROM messages = 0xEE, r/w, number of data bytes, block, address, data bytes, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID |
bob_tpc | 4:13e3e375c0d3 | 290 | |
bob_tpc | 4:13e3e375c0d3 | 291 | Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte. |
bob_tpc | 4:13e3e375c0d3 | 292 | 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 | 4:13e3e375c0d3 | 293 | */ |
bob_tpc | 4:13e3e375c0d3 | 294 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 4:13e3e375c0d3 | 295 | { |
bob_tpc | 4:13e3e375c0d3 | 296 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 4:13e3e375c0d3 | 297 | } |
bob_tpc | 4:13e3e375c0d3 | 298 | |
bob_tpc | 4:13e3e375c0d3 | 299 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 4:13e3e375c0d3 | 300 | status = eeprom_msg_rd(); // read the requested data |
bob_tpc | 4:13e3e375c0d3 | 301 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 4:13e3e375c0d3 | 302 | status = eeprom_msg_wr(); // write the eeprom location |
bob_tpc | 4:13e3e375c0d3 | 303 | |
bob_tpc | 4:13e3e375c0d3 | 304 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 4:13e3e375c0d3 | 305 | { |
bob_tpc | 4:13e3e375c0d3 | 306 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 4:13e3e375c0d3 | 307 | } |
bob_tpc | 4:13e3e375c0d3 | 308 | |
bob_tpc | 4:13e3e375c0d3 | 309 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 4:13e3e375c0d3 | 310 | { |
bob_tpc | 4:13e3e375c0d3 | 311 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 4:13e3e375c0d3 | 312 | |
bob_tpc | 4:13e3e375c0d3 | 313 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 4:13e3e375c0d3 | 314 | { |
bob_tpc | 4:13e3e375c0d3 | 315 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 4:13e3e375c0d3 | 316 | end_mark = TRUE; // end mark was reached |
bob_tpc | 4:13e3e375c0d3 | 317 | } |
bob_tpc | 4:13e3e375c0d3 | 318 | if (crcCount-- == 0) // end of message |
bob_tpc | 4:13e3e375c0d3 | 319 | { |
bob_tpc | 4:13e3e375c0d3 | 320 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 4:13e3e375c0d3 | 321 | break; |
bob_tpc | 4:13e3e375c0d3 | 322 | } |
bob_tpc | 4:13e3e375c0d3 | 323 | } break; |
bob_tpc | 0:8604e9cc07f2 | 324 | default: |
bob_tpc | 0:8604e9cc07f2 | 325 | return ERR_CDC_BAD_CMD; |
bob_tpc | 0:8604e9cc07f2 | 326 | } |
bob_tpc | 0:8604e9cc07f2 | 327 | } |