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@6:2941452a0e6d, 2015-01-22 (annotated)
- Committer:
- bob_tpc
- Date:
- Thu Jan 22 01:42:02 2015 +0000
- Revision:
- 6:2941452a0e6d
- Parent:
- 5:e77529f7ede3
- Child:
- 8:3313aa7f9082
Proximity r/w OK. Still working on GPIO (0xDD).
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 | 6:2941452a0e6d | 29 | #define I2CRATE 400000 // I2C speed |
bob_tpc | 0:8604e9cc07f2 | 30 | |
bob_tpc | 0:8604e9cc07f2 | 31 | // UART-RFID baud rate |
bob_tpc | 0:8604e9cc07f2 | 32 | #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps |
bob_tpc | 0:8604e9cc07f2 | 33 | |
bob_tpc | 0:8604e9cc07f2 | 34 | // Peripherals |
bob_tpc | 0:8604e9cc07f2 | 35 | USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers. |
bob_tpc | 0:8604e9cc07f2 | 36 | MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board |
bob_tpc | 0:8604e9cc07f2 | 37 | I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above) |
bob_tpc | 0:8604e9cc07f2 | 38 | |
bob_tpc | 0:8604e9cc07f2 | 39 | // GPIO signals |
bob_tpc | 0:8604e9cc07f2 | 40 | DigitalOut led_err(PTC1); // Red LED shows error condition (active low) |
bob_tpc | 0:8604e9cc07f2 | 41 | DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low) |
bob_tpc | 0:8604e9cc07f2 | 42 | DigitalOut rfid_int(PTD4); // RFID FE power control (active high) |
bob_tpc | 0:8604e9cc07f2 | 43 | DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high) |
bob_tpc | 0:8604e9cc07f2 | 44 | DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high) |
bob_tpc | 0:8604e9cc07f2 | 45 | DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 0:8604e9cc07f2 | 46 | DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low) |
bob_tpc | 0:8604e9cc07f2 | 47 | InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low) |
bob_tpc | 0:8604e9cc07f2 | 48 | |
bob_tpc | 0:8604e9cc07f2 | 49 | // buffers & variables |
bob_tpc | 5:e77529f7ede3 | 50 | uint8_t gpio_values = 0x00; // register to read GPIO values |
bob_tpc | 5:e77529f7ede3 | 51 | |
bob_tpc | 6:2941452a0e6d | 52 | uint8_t cdc_buffer_rx[32] = {0xBB, 0x00, 0x03, 0x00, 0x01, 0x02, 0x7E, 0x2E, 0xC9}; // buffers for cdc (USB-Serial port on PC) |
bob_tpc | 0:8604e9cc07f2 | 53 | uint8_t cdc_buffer_tx[32]; |
bob_tpc | 0:8604e9cc07f2 | 54 | uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board) |
bob_tpc | 0:8604e9cc07f2 | 55 | uint8_t uart_buffer_tx[32]; |
bob_tpc | 5:e77529f7ede3 | 56 | uint8_t gpio_buffer[32]; // buffer for GPIO messages |
bob_tpc | 5:e77529f7ede3 | 57 | 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 | 58 | int i, j; // index variables |
bob_tpc | 0:8604e9cc07f2 | 59 | int status = 0x00; // return value |
bob_tpc | 0:8604e9cc07f2 | 60 | |
bob_tpc | 0:8604e9cc07f2 | 61 | int prox_irq(void) |
bob_tpc | 0:8604e9cc07f2 | 62 | { |
bob_tpc | 0:8604e9cc07f2 | 63 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 64 | } |
bob_tpc | 0:8604e9cc07f2 | 65 | |
bob_tpc | 0:8604e9cc07f2 | 66 | int init_periph(void) |
bob_tpc | 0:8604e9cc07f2 | 67 | { |
bob_tpc | 0:8604e9cc07f2 | 68 | // Set up peripherals |
bob_tpc | 0:8604e9cc07f2 | 69 | // RFID |
bob_tpc | 5:e77529f7ede3 | 70 | uart.baud(RFIDBAUD); // RFID-FE baud rate |
bob_tpc | 0:8604e9cc07f2 | 71 | |
bob_tpc | 5:e77529f7ede3 | 72 | rfid_int = 0; // RFID FE power control (active high) |
bob_tpc | 5:e77529f7ede3 | 73 | rfid_isp = 0; // RFID FE In-System Programming (active high) |
bob_tpc | 5:e77529f7ede3 | 74 | rfid_rst = 1; // RFID FE Reset (active high) |
bob_tpc | 5:e77529f7ede3 | 75 | rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 5:e77529f7ede3 | 76 | wait(0.25); // wait 250ms before... |
bob_tpc | 5:e77529f7ede3 | 77 | rfid_rst = 0; // ... taking RFID out of reset |
bob_tpc | 0:8604e9cc07f2 | 78 | |
bob_tpc | 5:e77529f7ede3 | 79 | // Prox & EEPROM |
bob_tpc | 6:2941452a0e6d | 80 | i2c.frequency(I2CRATE); // I2C speed = 400Kbps |
bob_tpc | 5:e77529f7ede3 | 81 | prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU |
bob_tpc | 5:e77529f7ede3 | 82 | |
bob_tpc | 5:e77529f7ede3 | 83 | // LEDs // Cycle through the LEDs. |
bob_tpc | 5:e77529f7ede3 | 84 | led_err.write(LEDON); |
bob_tpc | 5:e77529f7ede3 | 85 | led_com.write(LEDON); |
bob_tpc | 5:e77529f7ede3 | 86 | wait(0.5); |
bob_tpc | 5:e77529f7ede3 | 87 | led_err.write(LEDOFF); |
bob_tpc | 5:e77529f7ede3 | 88 | wait(0.5); |
bob_tpc | 5:e77529f7ede3 | 89 | led_com.write(LEDOFF); |
bob_tpc | 5:e77529f7ede3 | 90 | |
bob_tpc | 0:8604e9cc07f2 | 91 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 92 | } |
bob_tpc | 0:8604e9cc07f2 | 93 | |
bob_tpc | 4:13e3e375c0d3 | 94 | /* |
bob_tpc | 4:13e3e375c0d3 | 95 | RFID messages are as defined in the RFID-FE manual. |
bob_tpc | 4:13e3e375c0d3 | 96 | */ |
bob_tpc | 0:8604e9cc07f2 | 97 | int rfid_msg(void) |
bob_tpc | 0:8604e9cc07f2 | 98 | { |
bob_tpc | 0:8604e9cc07f2 | 99 | bool end_mark = FALSE; |
bob_tpc | 0:8604e9cc07f2 | 100 | int i; |
bob_tpc | 5:e77529f7ede3 | 101 | uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 102 | |
bob_tpc | 5:e77529f7ede3 | 103 | uart.txBufferFlush(); // clear out UART buffers |
bob_tpc | 0:8604e9cc07f2 | 104 | uart.rxBufferFlush(); |
bob_tpc | 0:8604e9cc07f2 | 105 | |
bob_tpc | 0:8604e9cc07f2 | 106 | for (int i = 0; i < sizeof(uart_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 107 | { |
bob_tpc | 5:e77529f7ede3 | 108 | if (!uart.writeable()) |
bob_tpc | 5:e77529f7ede3 | 109 | { |
bob_tpc | 5:e77529f7ede3 | 110 | led_err.write(LEDON); |
bob_tpc | 5:e77529f7ede3 | 111 | return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error |
bob_tpc | 5:e77529f7ede3 | 112 | } |
bob_tpc | 6:2941452a0e6d | 113 | |
bob_tpc | 5:e77529f7ede3 | 114 | uart.putc(uart_buffer_tx[i]); // send uart message |
bob_tpc | 0:8604e9cc07f2 | 115 | |
bob_tpc | 5:e77529f7ede3 | 116 | if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 117 | { |
bob_tpc | 5:e77529f7ede3 | 118 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 119 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 120 | } |
bob_tpc | 5:e77529f7ede3 | 121 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 122 | { |
bob_tpc | 5:e77529f7ede3 | 123 | if (end_mark == FALSE) |
bob_tpc | 5:e77529f7ede3 | 124 | { |
bob_tpc | 5:e77529f7ede3 | 125 | led_err.write(LEDON); |
bob_tpc | 5:e77529f7ede3 | 126 | return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 127 | } |
bob_tpc | 0:8604e9cc07f2 | 128 | break; |
bob_tpc | 0:8604e9cc07f2 | 129 | } |
bob_tpc | 0:8604e9cc07f2 | 130 | } |
bob_tpc | 0:8604e9cc07f2 | 131 | |
bob_tpc | 0:8604e9cc07f2 | 132 | end_mark = FALSE; |
bob_tpc | 5:e77529f7ede3 | 133 | while(!uart.readable()); // wait for data from rfid |
bob_tpc | 5:e77529f7ede3 | 134 | crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 135 | for (i = 0; i < sizeof(uart_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 136 | { |
bob_tpc | 5:e77529f7ede3 | 137 | uart_buffer_rx[i] = uart.getc(); // read a character |
bob_tpc | 0:8604e9cc07f2 | 138 | |
bob_tpc | 5:e77529f7ede3 | 139 | if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message |
bob_tpc | 0:8604e9cc07f2 | 140 | { |
bob_tpc | 5:e77529f7ede3 | 141 | crcCount = 2; // two more bytes for crc |
bob_tpc | 5:e77529f7ede3 | 142 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 143 | } |
bob_tpc | 5:e77529f7ede3 | 144 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 145 | { |
bob_tpc | 5:e77529f7ede3 | 146 | if (end_mark == FALSE) |
bob_tpc | 5:e77529f7ede3 | 147 | { |
bob_tpc | 5:e77529f7ede3 | 148 | led_err.write(LEDON); |
bob_tpc | 5:e77529f7ede3 | 149 | return ERR_UART_NO_RX_ENDMARK; |
bob_tpc | 5:e77529f7ede3 | 150 | } |
bob_tpc | 0:8604e9cc07f2 | 151 | break; |
bob_tpc | 0:8604e9cc07f2 | 152 | } |
bob_tpc | 0:8604e9cc07f2 | 153 | } |
bob_tpc | 0:8604e9cc07f2 | 154 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 155 | } |
bob_tpc | 0:8604e9cc07f2 | 156 | |
bob_tpc | 0:8604e9cc07f2 | 157 | |
bob_tpc | 4:13e3e375c0d3 | 158 | /* |
bob_tpc | 4:13e3e375c0d3 | 159 | 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 | 160 | |
bob_tpc | 4:13e3e375c0d3 | 161 | 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 | 162 | */ |
bob_tpc | 4:13e3e375c0d3 | 163 | |
bob_tpc | 5:e77529f7ede3 | 164 | int prox_msg_wr() // write proximity I2C register |
bob_tpc | 0:8604e9cc07f2 | 165 | { |
bob_tpc | 4:13e3e375c0d3 | 166 | int i2c_err; |
bob_tpc | 4:13e3e375c0d3 | 167 | 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 | 168 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 169 | } |
bob_tpc | 3:e8cc286f9b2e | 170 | |
bob_tpc | 4:13e3e375c0d3 | 171 | int prox_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 172 | { |
bob_tpc | 4:13e3e375c0d3 | 173 | int i2c_err; |
bob_tpc | 5:e77529f7ede3 | 174 | 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 | 175 | 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 | 176 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 177 | } |
bob_tpc | 4:13e3e375c0d3 | 178 | |
bob_tpc | 5:e77529f7ede3 | 179 | // GPIO messages = 0xDD, r/w, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID |
bob_tpc | 5:e77529f7ede3 | 180 | int gpio_rd() |
bob_tpc | 5:e77529f7ede3 | 181 | { |
bob_tpc | 6:2941452a0e6d | 182 | led_err.write(LEDON); |
bob_tpc | 6:2941452a0e6d | 183 | led_com.write(LEDON); |
bob_tpc | 6:2941452a0e6d | 184 | rfid_int.write(0); |
bob_tpc | 6:2941452a0e6d | 185 | rfid_isp.write(0); |
bob_tpc | 6:2941452a0e6d | 186 | cdc.putc( led_err.read());// && 0x01); // read all of the GPIO pins and store in a single byte |
bob_tpc | 6:2941452a0e6d | 187 | cdc.putc( led_com.read());// << 1) && 0x02); |
bob_tpc | 6:2941452a0e6d | 188 | cdc.putc( rfid_int.read());// << 2) && 0x04); |
bob_tpc | 6:2941452a0e6d | 189 | cdc.putc( rfid_isp.read());// << 3) && 0x08); |
bob_tpc | 6:2941452a0e6d | 190 | |
bob_tpc | 5:e77529f7ede3 | 191 | |
bob_tpc | 5:e77529f7ede3 | 192 | return ERR_NONE; |
bob_tpc | 5:e77529f7ede3 | 193 | } |
bob_tpc | 4:13e3e375c0d3 | 194 | |
bob_tpc | 5:e77529f7ede3 | 195 | int gpio_wr() |
bob_tpc | 5:e77529f7ede3 | 196 | { |
bob_tpc | 6:2941452a0e6d | 197 | cdc.printf("debug"); |
bob_tpc | 6:2941452a0e6d | 198 | cdc.putc(gpio_buffer[2] && 0x01); |
bob_tpc | 6:2941452a0e6d | 199 | cdc.putc(gpio_buffer[2] && 0x02); |
bob_tpc | 6:2941452a0e6d | 200 | cdc.putc(gpio_buffer[2] && 0x04); |
bob_tpc | 6:2941452a0e6d | 201 | cdc.putc(gpio_buffer[2] && 0x08); |
bob_tpc | 6:2941452a0e6d | 202 | cdc.putc(gpio_buffer[2] && 0x10); |
bob_tpc | 6:2941452a0e6d | 203 | cdc.putc(gpio_buffer[2] && 0x20); |
bob_tpc | 5:e77529f7ede3 | 204 | led_err.write(gpio_buffer[2] && 0x01); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 205 | led_com.write(gpio_buffer[2] && 0x02); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 206 | rfid_int.write(gpio_buffer[2] && 0x04); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 207 | rfid_isp.write(gpio_buffer[2] && 0x05); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 208 | rfid_rst.write(gpio_buffer[2] && 0x10); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 209 | rfid_pwr.write(gpio_buffer[2] && 0x20); // any bit set will write a 0 |
bob_tpc | 5:e77529f7ede3 | 210 | return ERR_NONE; |
bob_tpc | 5:e77529f7ede3 | 211 | } |
bob_tpc | 5:e77529f7ede3 | 212 | |
bob_tpc | 5:e77529f7ede3 | 213 | |
bob_tpc | 4:13e3e375c0d3 | 214 | /* |
bob_tpc | 4:13e3e375c0d3 | 215 | 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 | 216 | |
bob_tpc | 4:13e3e375c0d3 | 217 | 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 | 218 | 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 | 219 | */ |
bob_tpc | 4:13e3e375c0d3 | 220 | |
bob_tpc | 5:e77529f7ede3 | 221 | int eeprom_msg_wr() // write proximity I2C register |
bob_tpc | 4:13e3e375c0d3 | 222 | { |
bob_tpc | 4:13e3e375c0d3 | 223 | int i2c_err; |
bob_tpc | 5:e77529f7ede3 | 224 | i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], i2c_buffer[2] + 1, 0); |
bob_tpc | 5:e77529f7ede3 | 225 | // I2C Address & block select, pointer to buffer, number of bytes (for address + data), stop at end. |
bob_tpc | 5:e77529f7ede3 | 226 | while (!i2c.write(EEPROM || i2c_buffer[3])); // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write) |
bob_tpc | 5:e77529f7ede3 | 227 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 4:13e3e375c0d3 | 228 | } |
bob_tpc | 3:e8cc286f9b2e | 229 | |
bob_tpc | 4:13e3e375c0d3 | 230 | int eeprom_msg_rd() |
bob_tpc | 4:13e3e375c0d3 | 231 | { |
bob_tpc | 4:13e3e375c0d3 | 232 | int i2c_err; |
bob_tpc | 5:e77529f7ede3 | 233 | i2c_err = i2c.write((EEPROM || i2c_buffer[3]), &i2c_buffer[4], 1, 1); |
bob_tpc | 5:e77529f7ede3 | 234 | // 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 | 235 | i2c_err |= i2c.read((EEPROM || i2c_buffer[3]), &i2c_buffer[5], i2c_buffer[2], 0); |
bob_tpc | 5:e77529f7ede3 | 236 | // I2C Address & block select, pointer to buffer (just the data), number of data bytes, stop at end. |
bob_tpc | 5:e77529f7ede3 | 237 | return i2c_err; // 0 = ACK received, 1 = NAK/failure |
bob_tpc | 0:8604e9cc07f2 | 238 | } |
bob_tpc | 0:8604e9cc07f2 | 239 | |
bob_tpc | 5:e77529f7ede3 | 240 | |
bob_tpc | 0:8604e9cc07f2 | 241 | |
bob_tpc | 0:8604e9cc07f2 | 242 | int main() |
bob_tpc | 0:8604e9cc07f2 | 243 | { |
bob_tpc | 0:8604e9cc07f2 | 244 | // initialize everything |
bob_tpc | 6:2941452a0e6d | 245 | |
bob_tpc | 6:2941452a0e6d | 246 | wait(2.0); |
bob_tpc | 0:8604e9cc07f2 | 247 | |
bob_tpc | 0:8604e9cc07f2 | 248 | init_periph(); |
bob_tpc | 0:8604e9cc07f2 | 249 | |
bob_tpc | 6:2941452a0e6d | 250 | |
bob_tpc | 5:e77529f7ede3 | 251 | while(1) |
bob_tpc | 0:8604e9cc07f2 | 252 | { |
bob_tpc | 5:e77529f7ede3 | 253 | led_com.write(LEDOFF); // turn off communication LED |
bob_tpc | 5:e77529f7ede3 | 254 | while(!cdc.readable()); // spin here until a message comes in from the host PC |
bob_tpc | 5:e77529f7ede3 | 255 | led_com.write(LEDON); // Message received - turn on LED |
bob_tpc | 5:e77529f7ede3 | 256 | bool end_mark = FALSE; |
bob_tpc | 5:e77529f7ede3 | 257 | uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start |
bob_tpc | 5:e77529f7ede3 | 258 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 259 | { |
bob_tpc | 5:e77529f7ede3 | 260 | cdc_buffer_rx[i] = cdc.getc(); // read data from USB side |
bob_tpc | 5:e77529f7ede3 | 261 | |
bob_tpc | 5:e77529f7ede3 | 262 | if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 263 | { |
bob_tpc | 5:e77529f7ede3 | 264 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 265 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 266 | } |
bob_tpc | 5:e77529f7ede3 | 267 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 268 | { |
bob_tpc | 5:e77529f7ede3 | 269 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 270 | break; |
bob_tpc | 5:e77529f7ede3 | 271 | } |
bob_tpc | 0:8604e9cc07f2 | 272 | } |
bob_tpc | 0:8604e9cc07f2 | 273 | |
bob_tpc | 5:e77529f7ede3 | 274 | switch(cdc_buffer_rx[0]) |
bob_tpc | 5:e77529f7ede3 | 275 | { |
bob_tpc | 5:e77529f7ede3 | 276 | case 0xBB: // RFID-FE |
bob_tpc | 5:e77529f7ede3 | 277 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 278 | { |
bob_tpc | 5:e77529f7ede3 | 279 | uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID |
bob_tpc | 5:e77529f7ede3 | 280 | } |
bob_tpc | 5:e77529f7ede3 | 281 | |
bob_tpc | 5:e77529f7ede3 | 282 | status = rfid_msg(); // send buffer to RFID and get response according to RFID board |
bob_tpc | 0:8604e9cc07f2 | 283 | |
bob_tpc | 5:e77529f7ede3 | 284 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 285 | { |
bob_tpc | 5:e77529f7ede3 | 286 | cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer |
bob_tpc | 5:e77529f7ede3 | 287 | } |
bob_tpc | 5:e77529f7ede3 | 288 | |
bob_tpc | 5:e77529f7ede3 | 289 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 290 | { |
bob_tpc | 5:e77529f7ede3 | 291 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 292 | |
bob_tpc | 5:e77529f7ede3 | 293 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 294 | { |
bob_tpc | 5:e77529f7ede3 | 295 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 296 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 297 | } |
bob_tpc | 5:e77529f7ede3 | 298 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 299 | { |
bob_tpc | 5:e77529f7ede3 | 300 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 301 | break; |
bob_tpc | 5:e77529f7ede3 | 302 | } |
bob_tpc | 5:e77529f7ede3 | 303 | } |
bob_tpc | 5:e77529f7ede3 | 304 | break; |
bob_tpc | 0:8604e9cc07f2 | 305 | |
bob_tpc | 5:e77529f7ede3 | 306 | case 0xCC: // Proximity Sensor |
bob_tpc | 5:e77529f7ede3 | 307 | //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 |
bob_tpc | 5:e77529f7ede3 | 308 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 309 | { |
bob_tpc | 5:e77529f7ede3 | 310 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 0:8604e9cc07f2 | 311 | } |
bob_tpc | 5:e77529f7ede3 | 312 | |
bob_tpc | 5:e77529f7ede3 | 313 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 5:e77529f7ede3 | 314 | status = prox_msg_rd(); // read the requested data |
bob_tpc | 5:e77529f7ede3 | 315 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 5:e77529f7ede3 | 316 | status = prox_msg_wr(); // send buffer to proximity sensor and get response |
bob_tpc | 5:e77529f7ede3 | 317 | |
bob_tpc | 5:e77529f7ede3 | 318 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 319 | { |
bob_tpc | 5:e77529f7ede3 | 320 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 0:8604e9cc07f2 | 321 | } |
bob_tpc | 3:e8cc286f9b2e | 322 | |
bob_tpc | 5:e77529f7ede3 | 323 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 324 | { |
bob_tpc | 5:e77529f7ede3 | 325 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 326 | |
bob_tpc | 5:e77529f7ede3 | 327 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 328 | { |
bob_tpc | 5:e77529f7ede3 | 329 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 330 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 331 | } |
bob_tpc | 5:e77529f7ede3 | 332 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 333 | { |
bob_tpc | 5:e77529f7ede3 | 334 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 335 | break; |
bob_tpc | 5:e77529f7ede3 | 336 | } |
bob_tpc | 5:e77529f7ede3 | 337 | } |
bob_tpc | 5:e77529f7ede3 | 338 | break; |
bob_tpc | 3:e8cc286f9b2e | 339 | |
bob_tpc | 5:e77529f7ede3 | 340 | case 0xDD: // GPIO (LEDs and RFID-FE control) |
bob_tpc | 6:2941452a0e6d | 341 | //GPIO messages = 0xDD, r/w#, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID |
bob_tpc | 5:e77529f7ede3 | 342 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 343 | { |
bob_tpc | 5:e77529f7ede3 | 344 | gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 5:e77529f7ede3 | 345 | } |
bob_tpc | 3:e8cc286f9b2e | 346 | |
bob_tpc | 6:2941452a0e6d | 347 | if (gpio_buffer[1] == 1) // GPIO read = 1 |
bob_tpc | 5:e77529f7ede3 | 348 | status = gpio_rd(); // read the requested data |
bob_tpc | 6:2941452a0e6d | 349 | else if (gpio_buffer[1] == 0) // GPIO write = 0 |
bob_tpc | 6:2941452a0e6d | 350 | status = gpio_wr(); // send GPIO pin data |
bob_tpc | 5:e77529f7ede3 | 351 | |
bob_tpc | 5:e77529f7ede3 | 352 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 353 | { |
bob_tpc | 6:2941452a0e6d | 354 | cdc_buffer_tx[i] = gpio_buffer[i]; // copy GPIO response back to USB buffer |
bob_tpc | 3:e8cc286f9b2e | 355 | } |
bob_tpc | 5:e77529f7ede3 | 356 | |
bob_tpc | 5:e77529f7ede3 | 357 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 3:e8cc286f9b2e | 358 | { |
bob_tpc | 5:e77529f7ede3 | 359 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 360 | |
bob_tpc | 5:e77529f7ede3 | 361 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 362 | { |
bob_tpc | 5:e77529f7ede3 | 363 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 364 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 365 | } |
bob_tpc | 5:e77529f7ede3 | 366 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 367 | { |
bob_tpc | 5:e77529f7ede3 | 368 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 369 | break; |
bob_tpc | 5:e77529f7ede3 | 370 | } |
bob_tpc | 3:e8cc286f9b2e | 371 | } |
bob_tpc | 5:e77529f7ede3 | 372 | break; |
bob_tpc | 5:e77529f7ede3 | 373 | |
bob_tpc | 5:e77529f7ede3 | 374 | case 0xEE: // Read/write EEPROM |
bob_tpc | 4:13e3e375c0d3 | 375 | /* |
bob_tpc | 4:13e3e375c0d3 | 376 | 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 | 377 | |
bob_tpc | 4:13e3e375c0d3 | 378 | 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 | 379 | 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 | 380 | */ |
bob_tpc | 5:e77529f7ede3 | 381 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 5:e77529f7ede3 | 382 | { |
bob_tpc | 5:e77529f7ede3 | 383 | i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C |
bob_tpc | 5:e77529f7ede3 | 384 | } |
bob_tpc | 4:13e3e375c0d3 | 385 | |
bob_tpc | 5:e77529f7ede3 | 386 | if (i2c_buffer[1] == 1) // I2C read = 1 |
bob_tpc | 5:e77529f7ede3 | 387 | status = gpio_rd(); // read the gpio pins |
bob_tpc | 5:e77529f7ede3 | 388 | else if (i2c_buffer[1] == 0) // I2C write = 0 |
bob_tpc | 5:e77529f7ede3 | 389 | status = gpio_wr(); // write gpio pins |
bob_tpc | 4:13e3e375c0d3 | 390 | |
bob_tpc | 5:e77529f7ede3 | 391 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 5:e77529f7ede3 | 392 | { |
bob_tpc | 5:e77529f7ede3 | 393 | cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer |
bob_tpc | 5:e77529f7ede3 | 394 | } |
bob_tpc | 4:13e3e375c0d3 | 395 | |
bob_tpc | 5:e77529f7ede3 | 396 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 4:13e3e375c0d3 | 397 | { |
bob_tpc | 5:e77529f7ede3 | 398 | cdc.putc(cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 5:e77529f7ede3 | 399 | |
bob_tpc | 5:e77529f7ede3 | 400 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 5:e77529f7ede3 | 401 | { |
bob_tpc | 5:e77529f7ede3 | 402 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 5:e77529f7ede3 | 403 | end_mark = TRUE; // end mark was reached |
bob_tpc | 5:e77529f7ede3 | 404 | } |
bob_tpc | 5:e77529f7ede3 | 405 | if (crcCount-- == 0) // end of message |
bob_tpc | 5:e77529f7ede3 | 406 | { |
bob_tpc | 5:e77529f7ede3 | 407 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 5:e77529f7ede3 | 408 | break; |
bob_tpc | 5:e77529f7ede3 | 409 | } |
bob_tpc | 5:e77529f7ede3 | 410 | } |
bob_tpc | 5:e77529f7ede3 | 411 | break; |
bob_tpc | 5:e77529f7ede3 | 412 | default: |
bob_tpc | 5:e77529f7ede3 | 413 | return ERR_CDC_BAD_CMD; |
bob_tpc | 5:e77529f7ede3 | 414 | } |
bob_tpc | 0:8604e9cc07f2 | 415 | } |
bob_tpc | 0:8604e9cc07f2 | 416 | } |