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@1:bd988d267998, 2015-01-19 (annotated)
- Committer:
- bob_tpc
- Date:
- Mon Jan 19 23:27:45 2015 +0000
- Revision:
- 1:bd988d267998
- Parent:
- 0:8604e9cc07f2
- Child:
- 2:efaf8aee55df
Updated comments
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 | 0:8604e9cc07f2 | 20 | |
bob_tpc | 0:8604e9cc07f2 | 21 | |
bob_tpc | 0:8604e9cc07f2 | 22 | // I2C addresses |
bob_tpc | 0:8604e9cc07f2 | 23 | #define PROX (0x29 << 1) // default I2C address of VL6180X, shift into upper 7 bits |
bob_tpc | 0:8604e9cc07f2 | 24 | #define EEPROM (0xA0) // default I2C address of EEPROM, already shifted |
bob_tpc | 0:8604e9cc07f2 | 25 | |
bob_tpc | 0:8604e9cc07f2 | 26 | // UART-RFID baud rate |
bob_tpc | 0:8604e9cc07f2 | 27 | #define RFIDBAUD 115200 // RFID-FE board default rate = 115.2Kbps |
bob_tpc | 0:8604e9cc07f2 | 28 | |
bob_tpc | 0:8604e9cc07f2 | 29 | // Peripherals |
bob_tpc | 0:8604e9cc07f2 | 30 | USBSerial cdc; // CDC Class USB<>Serial adapter. Needs custom INF, but uses existing Windows drivers. |
bob_tpc | 0:8604e9cc07f2 | 31 | MODSERIAL uart(PTA2, PTA1); // UART port connected to RFID-FE board |
bob_tpc | 0:8604e9cc07f2 | 32 | I2C i2c(PTB1, PTB0); // I2C port connected to VL6180X and EEPROM - note addresses above) |
bob_tpc | 0:8604e9cc07f2 | 33 | |
bob_tpc | 0:8604e9cc07f2 | 34 | // GPIO signals |
bob_tpc | 0:8604e9cc07f2 | 35 | DigitalOut led_err(PTC1); // Red LED shows error condition (active low) |
bob_tpc | 0:8604e9cc07f2 | 36 | DigitalOut led_com(PTC2); // Yellow LED shows communication activity (active low) |
bob_tpc | 0:8604e9cc07f2 | 37 | DigitalOut rfid_int(PTD4); // RFID FE power control (active high) |
bob_tpc | 0:8604e9cc07f2 | 38 | DigitalOut rfid_isp(PTD5); // RFID FE In-System Programming (active high) |
bob_tpc | 0:8604e9cc07f2 | 39 | DigitalOut rfid_rst(PTD6); // RFID FE Reset (active high) |
bob_tpc | 0:8604e9cc07f2 | 40 | DigitalOut rfid_pwr(PTE30); // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 0:8604e9cc07f2 | 41 | DigitalIn rfid_hot(PTE0); // RFID over-current detection on USB board power switch (active low) |
bob_tpc | 0:8604e9cc07f2 | 42 | InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low) |
bob_tpc | 0:8604e9cc07f2 | 43 | |
bob_tpc | 0:8604e9cc07f2 | 44 | // buffers & variables |
bob_tpc | 0:8604e9cc07f2 | 45 | uint8_t cdc_buffer_rx[32]; // buffers for cdc (USB-Serial port on PC) |
bob_tpc | 0:8604e9cc07f2 | 46 | uint8_t cdc_buffer_tx[32]; |
bob_tpc | 0:8604e9cc07f2 | 47 | uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board) |
bob_tpc | 0:8604e9cc07f2 | 48 | uint8_t uart_buffer_tx[32]; |
bob_tpc | 0:8604e9cc07f2 | 49 | int i, j; // index variables |
bob_tpc | 0:8604e9cc07f2 | 50 | int status = 0x00; // return value |
bob_tpc | 0:8604e9cc07f2 | 51 | |
bob_tpc | 0:8604e9cc07f2 | 52 | int prox_irq(void) |
bob_tpc | 0:8604e9cc07f2 | 53 | { |
bob_tpc | 0:8604e9cc07f2 | 54 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 55 | } |
bob_tpc | 0:8604e9cc07f2 | 56 | |
bob_tpc | 0:8604e9cc07f2 | 57 | int init_periph(void) |
bob_tpc | 0:8604e9cc07f2 | 58 | { |
bob_tpc | 0:8604e9cc07f2 | 59 | // Set up peripherals |
bob_tpc | 0:8604e9cc07f2 | 60 | // RFID |
bob_tpc | 0:8604e9cc07f2 | 61 | uart.baud(RFIDBAUD); // RFID-FE baud rate |
bob_tpc | 0:8604e9cc07f2 | 62 | |
bob_tpc | 0:8604e9cc07f2 | 63 | rfid_int = 0; // RFID FE power control (active high) |
bob_tpc | 0:8604e9cc07f2 | 64 | rfid_isp = 0; // RFID FE In-System Programming (active high) |
bob_tpc | 0:8604e9cc07f2 | 65 | rfid_rst = 1; // RFID FE Reset (active high) |
bob_tpc | 0:8604e9cc07f2 | 66 | rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others) |
bob_tpc | 0:8604e9cc07f2 | 67 | wait(0.25); // wait 1/4 second before... |
bob_tpc | 0:8604e9cc07f2 | 68 | rfid_rst = 0; // ... taking RFID out of reset |
bob_tpc | 0:8604e9cc07f2 | 69 | |
bob_tpc | 0:8604e9cc07f2 | 70 | // Prox |
bob_tpc | 0:8604e9cc07f2 | 71 | return 0; |
bob_tpc | 0:8604e9cc07f2 | 72 | } |
bob_tpc | 0:8604e9cc07f2 | 73 | |
bob_tpc | 0:8604e9cc07f2 | 74 | int rfid_msg(void) |
bob_tpc | 0:8604e9cc07f2 | 75 | { |
bob_tpc | 0:8604e9cc07f2 | 76 | bool end_mark = FALSE; |
bob_tpc | 0:8604e9cc07f2 | 77 | int i; |
bob_tpc | 0:8604e9cc07f2 | 78 | uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 79 | |
bob_tpc | 0:8604e9cc07f2 | 80 | uart.txBufferFlush(); // clear out UART buffers |
bob_tpc | 0:8604e9cc07f2 | 81 | uart.rxBufferFlush(); |
bob_tpc | 0:8604e9cc07f2 | 82 | |
bob_tpc | 0:8604e9cc07f2 | 83 | for (int i = 0; i < sizeof(uart_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 84 | { |
bob_tpc | 0:8604e9cc07f2 | 85 | if (!uart.writeable()) return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error |
bob_tpc | 0:8604e9cc07f2 | 86 | uart.putc(uart_buffer_tx[i]); // send uart message |
bob_tpc | 0:8604e9cc07f2 | 87 | |
bob_tpc | 0:8604e9cc07f2 | 88 | if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 89 | { |
bob_tpc | 0:8604e9cc07f2 | 90 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 0:8604e9cc07f2 | 91 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 92 | } |
bob_tpc | 0:8604e9cc07f2 | 93 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 94 | { |
bob_tpc | 0:8604e9cc07f2 | 95 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 96 | break; |
bob_tpc | 0:8604e9cc07f2 | 97 | } |
bob_tpc | 0:8604e9cc07f2 | 98 | } |
bob_tpc | 0:8604e9cc07f2 | 99 | |
bob_tpc | 0:8604e9cc07f2 | 100 | end_mark = FALSE; |
bob_tpc | 0:8604e9cc07f2 | 101 | wait(0.5); |
bob_tpc | 0:8604e9cc07f2 | 102 | while(!uart.readable()) printf("."); // wait for data from rfid |
bob_tpc | 0:8604e9cc07f2 | 103 | crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 104 | for (i = 0; i < sizeof(uart_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 105 | { |
bob_tpc | 0:8604e9cc07f2 | 106 | uart_buffer_rx[i] = uart.getc(); // read a character |
bob_tpc | 0:8604e9cc07f2 | 107 | // cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug |
bob_tpc | 0:8604e9cc07f2 | 108 | |
bob_tpc | 0:8604e9cc07f2 | 109 | if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message |
bob_tpc | 0:8604e9cc07f2 | 110 | { |
bob_tpc | 0:8604e9cc07f2 | 111 | crcCount = 2; // two more bytes for crc |
bob_tpc | 0:8604e9cc07f2 | 112 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 113 | } |
bob_tpc | 0:8604e9cc07f2 | 114 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 115 | { |
bob_tpc | 0:8604e9cc07f2 | 116 | if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK; |
bob_tpc | 0:8604e9cc07f2 | 117 | break; |
bob_tpc | 0:8604e9cc07f2 | 118 | } |
bob_tpc | 0:8604e9cc07f2 | 119 | } |
bob_tpc | 0:8604e9cc07f2 | 120 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 121 | } |
bob_tpc | 0:8604e9cc07f2 | 122 | |
bob_tpc | 0:8604e9cc07f2 | 123 | |
bob_tpc | 0:8604e9cc07f2 | 124 | int prox_msg() |
bob_tpc | 0:8604e9cc07f2 | 125 | { |
bob_tpc | 0:8604e9cc07f2 | 126 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 127 | } |
bob_tpc | 0:8604e9cc07f2 | 128 | |
bob_tpc | 0:8604e9cc07f2 | 129 | int gpio_msg() |
bob_tpc | 0:8604e9cc07f2 | 130 | { |
bob_tpc | 0:8604e9cc07f2 | 131 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 132 | } |
bob_tpc | 0:8604e9cc07f2 | 133 | |
bob_tpc | 1:bd988d267998 | 134 | int eeprom_msg() // eeprom to be implemented along with next hardware prototype |
bob_tpc | 0:8604e9cc07f2 | 135 | { |
bob_tpc | 0:8604e9cc07f2 | 136 | return ERR_NONE; |
bob_tpc | 0:8604e9cc07f2 | 137 | } |
bob_tpc | 0:8604e9cc07f2 | 138 | |
bob_tpc | 0:8604e9cc07f2 | 139 | int main() |
bob_tpc | 0:8604e9cc07f2 | 140 | { |
bob_tpc | 0:8604e9cc07f2 | 141 | // initialize everything |
bob_tpc | 0:8604e9cc07f2 | 142 | |
bob_tpc | 0:8604e9cc07f2 | 143 | wait(3.0); |
bob_tpc | 0:8604e9cc07f2 | 144 | init_periph(); |
bob_tpc | 0:8604e9cc07f2 | 145 | |
bob_tpc | 0:8604e9cc07f2 | 146 | cdc.printf("Starting...\n\r"); |
bob_tpc | 0:8604e9cc07f2 | 147 | |
bob_tpc | 0:8604e9cc07f2 | 148 | //while(!cdc.readable()); // spin here until a message comes in from the host PC |
bob_tpc | 0:8604e9cc07f2 | 149 | bool end_mark = FALSE; |
bob_tpc | 0:8604e9cc07f2 | 150 | uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start |
bob_tpc | 0:8604e9cc07f2 | 151 | cdc.printf("\n\rCDC Input: "); |
bob_tpc | 0:8604e9cc07f2 | 152 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 153 | { |
bob_tpc | 0:8604e9cc07f2 | 154 | //cdc_buffer_rx[i] = cdc.getc(); // |
bob_tpc | 0:8604e9cc07f2 | 155 | |
bob_tpc | 0:8604e9cc07f2 | 156 | cdc.printf("%X, ",cdc_buffer_rx[i]); |
bob_tpc | 0:8604e9cc07f2 | 157 | |
bob_tpc | 0:8604e9cc07f2 | 158 | if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 159 | { |
bob_tpc | 0:8604e9cc07f2 | 160 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 0:8604e9cc07f2 | 161 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 162 | } |
bob_tpc | 0:8604e9cc07f2 | 163 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 164 | { |
bob_tpc | 0:8604e9cc07f2 | 165 | if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 166 | break; |
bob_tpc | 0:8604e9cc07f2 | 167 | } |
bob_tpc | 0:8604e9cc07f2 | 168 | } |
bob_tpc | 0:8604e9cc07f2 | 169 | |
bob_tpc | 0:8604e9cc07f2 | 170 | switch(cdc_buffer_rx[0]) |
bob_tpc | 0:8604e9cc07f2 | 171 | { |
bob_tpc | 0:8604e9cc07f2 | 172 | case 0xBB: // RFID-FE |
bob_tpc | 0:8604e9cc07f2 | 173 | for (i = 0; i < sizeof(cdc_buffer_rx); i++) |
bob_tpc | 0:8604e9cc07f2 | 174 | { |
bob_tpc | 0:8604e9cc07f2 | 175 | uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID |
bob_tpc | 0:8604e9cc07f2 | 176 | } |
bob_tpc | 0:8604e9cc07f2 | 177 | |
bob_tpc | 0:8604e9cc07f2 | 178 | status = rfid_msg(); // send buffer to RFID and get response according to RFID board |
bob_tpc | 0:8604e9cc07f2 | 179 | |
bob_tpc | 0:8604e9cc07f2 | 180 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 181 | { |
bob_tpc | 0:8604e9cc07f2 | 182 | cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer |
bob_tpc | 0:8604e9cc07f2 | 183 | } |
bob_tpc | 0:8604e9cc07f2 | 184 | |
bob_tpc | 0:8604e9cc07f2 | 185 | cdc.printf("\n\rRFID Response: "); |
bob_tpc | 0:8604e9cc07f2 | 186 | |
bob_tpc | 0:8604e9cc07f2 | 187 | for (i = 0; i < sizeof(cdc_buffer_tx); i++) |
bob_tpc | 0:8604e9cc07f2 | 188 | { |
bob_tpc | 0:8604e9cc07f2 | 189 | cdc.printf("%x, ", cdc_buffer_tx[i]); // send message back to PC |
bob_tpc | 0:8604e9cc07f2 | 190 | |
bob_tpc | 0:8604e9cc07f2 | 191 | if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message |
bob_tpc | 0:8604e9cc07f2 | 192 | { |
bob_tpc | 0:8604e9cc07f2 | 193 | crcCount = 2; // two more bytes for CRC |
bob_tpc | 0:8604e9cc07f2 | 194 | end_mark = TRUE; // end mark was reached |
bob_tpc | 0:8604e9cc07f2 | 195 | } |
bob_tpc | 0:8604e9cc07f2 | 196 | if (crcCount-- == 0) // end of message |
bob_tpc | 0:8604e9cc07f2 | 197 | { |
bob_tpc | 0:8604e9cc07f2 | 198 | if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected |
bob_tpc | 0:8604e9cc07f2 | 199 | break; |
bob_tpc | 0:8604e9cc07f2 | 200 | } |
bob_tpc | 0:8604e9cc07f2 | 201 | } |
bob_tpc | 0:8604e9cc07f2 | 202 | break; |
bob_tpc | 0:8604e9cc07f2 | 203 | case 0xCC: // Proximity Sensor |
bob_tpc | 0:8604e9cc07f2 | 204 | prox_msg(); |
bob_tpc | 0:8604e9cc07f2 | 205 | break; |
bob_tpc | 0:8604e9cc07f2 | 206 | case 0xDD: // GPIO (LEDs and RFID-FE control |
bob_tpc | 0:8604e9cc07f2 | 207 | gpio_msg(); |
bob_tpc | 0:8604e9cc07f2 | 208 | break; |
bob_tpc | 0:8604e9cc07f2 | 209 | case 0xEE: // Read/write EEPROM |
bob_tpc | 0:8604e9cc07f2 | 210 | eeprom_msg(); |
bob_tpc | 0:8604e9cc07f2 | 211 | break; |
bob_tpc | 0:8604e9cc07f2 | 212 | default: |
bob_tpc | 0:8604e9cc07f2 | 213 | return ERR_CDC_BAD_CMD; |
bob_tpc | 0:8604e9cc07f2 | 214 | } |
bob_tpc | 0:8604e9cc07f2 | 215 | } |