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
Diff: main.cpp
- Revision:
- 5:e77529f7ede3
- Parent:
- 4:13e3e375c0d3
- Child:
- 6:2941452a0e6d
diff -r 13e3e375c0d3 -r e77529f7ede3 main.cpp --- a/main.cpp Tue Jan 20 22:26:26 2015 +0000 +++ b/main.cpp Wed Jan 21 01:10:59 2015 +0000 @@ -46,11 +46,14 @@ InterruptIn prox_int(PTD7); // Proximity sensor interrupt (active low) // buffers & variables +uint8_t gpio_values = 0x00; // register to read GPIO values + uint8_t cdc_buffer_rx[32]; // buffers for cdc (USB-Serial port on PC) uint8_t cdc_buffer_tx[32]; uint8_t uart_buffer_rx[32]; // buffers for uart (RFID-FE board) uint8_t uart_buffer_tx[32]; -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 +uint8_t gpio_buffer[32]; // buffer for GPIO messages +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 int i, j; // index variables int status = 0x00; // return value @@ -63,18 +66,28 @@ { // Set up peripherals // RFID - uart.baud(RFIDBAUD); // RFID-FE baud rate + uart.baud(RFIDBAUD); // RFID-FE baud rate - rfid_int = 0; // RFID FE power control (active high) - rfid_isp = 0; // RFID FE In-System Programming (active high) - rfid_rst = 1; // RFID FE Reset (active high) - rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others) - wait(0.25); // wait 250ms before... - rfid_rst = 0; // ... taking RFID out of reset + rfid_int = 0; // RFID FE power control (active high) + rfid_isp = 0; // RFID FE In-System Programming (active high) + rfid_rst = 1; // RFID FE Reset (active high) + rfid_pwr = 1; // RFID power switch on USB board (active high for prototype 1, low for all others) + wait(0.25); // wait 250ms before... + rfid_rst = 0; // ... taking RFID out of reset - // Prox - i2c.frequency(400000); // I2C speed = 400Kbps - prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU + // Prox & EEPROM + i2c.frequency(400000); // I2C speed = 400Kbps + prox_int.mode(PullUp); // pull up proximity sensor interrupt at MCU + + // LEDs // Cycle through the LEDs. + led_err.write(LEDON); + led_com.write(LEDON); + wait(0.5); + led_err.write(LEDOFF); + wait(0.5); + led_com.write(LEDOFF); + + return 0; @@ -87,45 +100,57 @@ { bool end_mark = FALSE; int i; - uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start + uint8_t crcCount = sizeof(uart_buffer_tx); // use tx buffer size to start - uart.txBufferFlush(); // clear out UART buffers + uart.txBufferFlush(); // clear out UART buffers uart.rxBufferFlush(); for (int i = 0; i < sizeof(uart_buffer_tx); i++) { - if (!uart.writeable()) return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error - uart.putc(uart_buffer_tx[i]); // send uart message + if (!uart.writeable()) + { + led_err.write(LEDON); + return ERR_UART_NOT_WRITEABLE; // if no space in uart, return error + } + uart.putc(uart_buffer_tx[i]); // send uart message - if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + if (uart_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message { - crcCount = 2; // two more bytes for CRC - end_mark = TRUE; // end mark was reached + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached } - if (crcCount-- == 0) // end of message + if (crcCount-- == 0) // end of message { - if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected + if (end_mark == FALSE) + { + led_err.write(LEDON); + return ERR_UART_NO_TX_ENDMARK; // no end mark detected + } break; } } end_mark = FALSE; - //wait(0.5); // debug - while(!uart.readable()); // wait for data from rfid - crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start + //wait(0.5); // debug + while(!uart.readable()); // wait for data from rfid + crcCount = sizeof(uart_buffer_rx); // use rx buffer size to start for (i = 0; i < sizeof(uart_buffer_rx); i++) { - uart_buffer_rx[i] = uart.getc(); // read a character -// cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug + uart_buffer_rx[i] = uart.getc(); // read a character +// cdc.printf("%d, 0x%X\n\r", i, uart_buffer_rx[i]); // debug - if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message + if (uart_buffer_rx[i] == 0x7E) // check for rfid end mark in inbound message { - crcCount = 2; // two more bytes for crc - end_mark = TRUE; // end mark was reached + crcCount = 2; // two more bytes for crc + end_mark = TRUE; // end mark was reached } - if (crcCount-- == 0) // end of message + if (crcCount-- == 0) // end of message { - if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK; + if (end_mark == FALSE) + { + led_err.write(LEDON); + return ERR_UART_NO_RX_ENDMARK; + } break; } } @@ -139,22 +164,48 @@ Multiple registers can be read or written with single prox_msg_rd() or prox_msg_wr(). Location address increments for each byte. */ -int prox_msg_wr() // write proximity I2C register +int prox_msg_wr() // write proximity I2C register { int i2c_err; 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. - return i2c_err; // 0 = ACK received, 1 = NAK/failure + return i2c_err; // 0 = ACK received, 1 = NAK/failure } int prox_msg_rd() { int i2c_err; - 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. - 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. - return i2c_err; // 0 = ACK received, 1 = NAK/failure + 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. + 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. + return i2c_err; // 0 = ACK received, 1 = NAK/failure } +// GPIO messages = 0xDD, r/w, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID +int gpio_rd() +{ + gpio_buffer[2] = (led_err.read() && 0x01); // read all of the GPIO pins and store in a single byte + gpio_buffer[2] |= ((led_com.read() << 1) && 0x02); + gpio_buffer[2] |= ((rfid_int.read() << 2) && 0x04); + gpio_buffer[2] |= ((rfid_isp.read() << 3) && 0x08); + gpio_buffer[2] |= ((rfid_rst.read() << 4) && 0x10); + gpio_buffer[2] |= ((rfid_pwr.read() << 5) && 0x20); + gpio_buffer[2] |= ((rfid_hot.read() << 6) && 0x40); + gpio_buffer[2] |= ((prox_int.read() << 7) && 0x80); + + return ERR_NONE; +} +int gpio_wr() +{ + led_err.write(gpio_buffer[2] && 0x01); // any bit set will write a 0 + led_com.write(gpio_buffer[2] && 0x02); // any bit set will write a 0 + rfid_int.write(gpio_buffer[2] && 0x04); // any bit set will write a 0 + rfid_isp.write(gpio_buffer[2] && 0x05); // any bit set will write a 0 + rfid_rst.write(gpio_buffer[2] && 0x10); // any bit set will write a 0 + rfid_pwr.write(gpio_buffer[2] && 0x20); // any bit set will write a 0 + return ERR_NONE; +} + + /* 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 @@ -162,166 +213,205 @@ 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. */ -int eeprom_msg_wr() // write proximity I2C register +int eeprom_msg_wr() // write proximity I2C register { int i2c_err; - 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. - while (!i2c.write(EEPROM || i2c_buffer[3])); // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write) - return i2c_err; // 0 = ACK received, 1 = NAK/failure + 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. + while (!i2c.write(EEPROM || i2c_buffer[3])); // wait until write is done (EEPROM will ACK = 1 for single byte i2c.write) + return i2c_err; // 0 = ACK received, 1 = NAK/failure } int eeprom_msg_rd() { int i2c_err; - 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. - 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. - return i2c_err; // 0 = ACK received, 1 = NAK/failure + 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. + 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. + return i2c_err; // 0 = ACK received, 1 = NAK/failure } -int gpio_msg() -{ - return ERR_NONE; -} + int main() { // initialize everything - wait(3.0); // debug - gives some time to start terminal program and open COM port + wait(2.0); // debug - gives some time to start terminal program and open COM port init_periph(); - //cdc.printf("Starting...\n\r"); // debug - - while(!cdc.readable()); // spin here until a message comes in from the host PC - bool end_mark = FALSE; - uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start - //cdc.printf("\n\rCDC Input: "); // debug - for (i = 0; i < sizeof(cdc_buffer_rx); i++) + cdc.printf("Starting...\n\r"); // debug + + while(1) { - cdc_buffer_rx[i] = cdc.getc(); // read data from USB side - - //cdc.printf("%X, ",cdc_buffer_rx[i]); // debug - - if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message + led_com.write(LEDOFF); // turn off communication LED + while(!cdc.readable()); // spin here until a message comes in from the host PC + led_com.write(LEDON); // Message received - turn on LED + bool end_mark = FALSE; + uint8_t crcCount = sizeof(cdc_buffer_rx); // use tx buffer size to start + //cdc.printf("\n\rCDC Input: "); // debug + for (i = 0; i < sizeof(cdc_buffer_rx); i++) { - crcCount = 2; // two more bytes for CRC - end_mark = TRUE; // end mark was reached - } - if (crcCount-- == 0) // end of message - { - if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected - break; + cdc_buffer_rx[i] = cdc.getc(); // read data from USB side + + //cdc.printf("%X, ",cdc_buffer_rx[i]); // debug + + if (cdc_buffer_rx[i] == 0x7E) // check for rfid end mark in outbound message + { + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached + } + if (crcCount-- == 0) // end of message + { + if (end_mark == FALSE) return ERR_UART_NO_TX_ENDMARK; // no end mark detected + break; + } } - } - switch(cdc_buffer_rx[0]) - { - case 0xBB: // RFID-FE - for (i = 0; i < sizeof(cdc_buffer_rx); i++) - { - uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID - } + switch(cdc_buffer_rx[0]) + { + case 0xBB: // RFID-FE + for (i = 0; i < sizeof(cdc_buffer_rx); i++) + { + uart_buffer_tx[i] = cdc_buffer_rx[i]; // copy USB message to UART for RFID + } + + status = rfid_msg(); // send buffer to RFID and get response according to RFID board - status = rfid_msg(); // send buffer to RFID and get response according to RFID board + for (i = 0; i < sizeof(cdc_buffer_tx); i++) + { + cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer + } + + //cdc.printf("\n\rRFID Response: "); // debug - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc_buffer_tx[i] = uart_buffer_rx[i]; // copy RFID response back to USB buffer - } + for (i = 0; i < sizeof(cdc_buffer_tx); i++) + { + cdc.putc(cdc_buffer_tx[i]); // send message back to PC + + if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + { + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached + } + if (crcCount-- == 0) // end of message + { + if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected + break; + } + } + break; - //cdc.printf("\n\rRFID Response: "); // debug - - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc.putc(cdc_buffer_tx[i]); // send message back to PC - - if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + case 0xCC: // Proximity Sensor + //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 + for (i = 0; i < sizeof(cdc_buffer_rx); i++) { - crcCount = 2; // two more bytes for CRC - end_mark = TRUE; // end mark was reached + i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C } - if (crcCount-- == 0) // end of message + + if (i2c_buffer[1] == 1) // I2C read = 1 + status = prox_msg_rd(); // read the requested data + else if (i2c_buffer[1] == 0) // I2C write = 0 + status = prox_msg_wr(); // send buffer to proximity sensor and get response + + for (i = 0; i < sizeof(cdc_buffer_tx); i++) { - if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected - break; + cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer } - } - break; - case 0xCC: - //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 - for (i = 0; i < sizeof(cdc_buffer_rx); i++) - { - i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C - } - if (i2c_buffer[1] == 1) // I2C read = 1 - status = prox_msg_rd(); // read the requested data - else if (i2c_buffer[1] == 0) // I2C write = 0 - status = prox_msg_wr(); // send buffer to proximity sensor and get response + for (i = 0; i < sizeof(cdc_buffer_tx); i++) + { + cdc.putc(cdc_buffer_tx[i]); // send message back to PC + + if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + { + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached + } + if (crcCount-- == 0) // end of message + { + if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected + break; + } + } + break; - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer - } + case 0xDD: // GPIO (LEDs and RFID-FE control) + //GPIO messages = 0xDD, r/w, value, 0x7E, 0xXX, 0xXX - last two are fillers where CRC goes for RFID + for (i = 0; i < sizeof(cdc_buffer_rx); i++) + { + gpio_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C + } - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc.putc(cdc_buffer_tx[i]); // send message back to PC - - if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + if (gpio_buffer[1] == 1) // I2C read = 1 + status = gpio_rd(); // read the requested data + else if (gpio_buffer[1] == 0) // I2C write = 0 + status = gpio_wr(); // send buffer to proximity sensor and get response + + for (i = 0; i < sizeof(cdc_buffer_tx); i++) { - crcCount = 2; // two more bytes for CRC - end_mark = TRUE; // end mark was reached + cdc_buffer_tx[i] = gpio_buffer[i]; // copy prox response back to USB buffer } - if (crcCount-- == 0) // end of message + + for (i = 0; i < sizeof(cdc_buffer_tx); i++) { - if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected - break; + cdc.putc(cdc_buffer_tx[i]); // send message back to PC + + if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + { + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached + } + if (crcCount-- == 0) // end of message + { + if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected + break; + } } - } - break; - case 0xDD: // GPIO (LEDs and RFID-FE control - gpio_msg(); - break; - case 0xEE: // Read/write EEPROM + break; + + case 0xEE: // Read/write EEPROM /* 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 Multiple registers can be read or written with single eeprom_msg_rd() or eeprom_msg_wr(). Location address increments for each byte. 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. */ - for (i = 0; i < sizeof(cdc_buffer_rx); i++) - { - i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C - } + for (i = 0; i < sizeof(cdc_buffer_rx); i++) + { + i2c_buffer[i] = cdc_buffer_rx[i]; // copy USB message to buffer for I2C + } - if (i2c_buffer[1] == 1) // I2C read = 1 - status = eeprom_msg_rd(); // read the requested data - else if (i2c_buffer[1] == 0) // I2C write = 0 - status = eeprom_msg_wr(); // write the eeprom location + if (i2c_buffer[1] == 1) // I2C read = 1 + status = gpio_rd(); // read the gpio pins + else if (i2c_buffer[1] == 0) // I2C write = 0 + status = gpio_wr(); // write gpio pins - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer - } + for (i = 0; i < sizeof(cdc_buffer_tx); i++) + { + cdc_buffer_tx[i] = i2c_buffer[i]; // copy prox response back to USB buffer + } - for (i = 0; i < sizeof(cdc_buffer_tx); i++) - { - cdc.putc(cdc_buffer_tx[i]); // send message back to PC - - if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + for (i = 0; i < sizeof(cdc_buffer_tx); i++) { - crcCount = 2; // two more bytes for CRC - end_mark = TRUE; // end mark was reached - } - if (crcCount-- == 0) // end of message - { - if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected - break; - } - } break; - default: - return ERR_CDC_BAD_CMD; + cdc.putc(cdc_buffer_tx[i]); // send message back to PC + + if (cdc_buffer_tx[i] == 0x7E) // check for rfid end mark in outbound message + { + crcCount = 2; // two more bytes for CRC + end_mark = TRUE; // end mark was reached + } + if (crcCount-- == 0) // end of message + { + if (end_mark == FALSE) return ERR_CDC_NO_TX_ENDMARK; // no end mark detected + break; + } + } + break; + default: + return ERR_CDC_BAD_CMD; + } } }