Super Vision / Mbed 2 deprecated sv_usb_firmware

Dependencies:   MODSERIAL USBDevice_for_Rev_C_HW mbed

Fork of mbed_sv_firmware_with_init by Bob Recny

main.cpp

Committer:
bob_tpc
Date:
2015-01-20
Revision:
3:e8cc286f9b2e
Parent:
2:efaf8aee55df
Child:
4:13e3e375c0d3

File content as of revision 3:e8cc286f9b2e:

#include "mbed.h"
#include "USBSerial.h"
#include "MODSERIAL.h"
#include "InterruptIn.h"

// Constants
#define LEDON       0               // Low active for LEDs - turns LED on
#define LEDOFF      1               // Low active for LEDs - turns LED off
#define TRUE        1
#define FALSE       0


// Error return values
#define ERR_NONE                0   // Success
#define ERR_CDC_BAD_CMD         1   // First byte of PC to USB board needs to be 0xBB, 0xCC, 0xDD or 0xEE;
#define ERR_CDC_NO_TX_ENDMARK   2   // message for no endmark on message to PC
#define ERR_UART_NOT_WRITEABLE  3   // UART has no buffer space
#define ERR_UART_NO_TX_ENDMARK  4   // message for UART has no 0x7E end-mark
#define ERR_UART_NO_RX_ENDMARK  5   // message received from UART has no end-mark


// I2C addresses
#define PROX        (0x29 << 1)     // default I2C address of VL6180X, shift into upper 7 bits
#define EEPROM      (0xA0)          // default I2C address of EEPROM, already shifted

// UART-RFID baud rate
#define RFIDBAUD    115200          // RFID-FE board default rate = 115.2Kbps

// Peripherals
USBSerial cdc;                      // CDC Class USB<>Serial adapter.  Needs custom INF, but uses existing Windows drivers.
MODSERIAL uart(PTA2, PTA1);         // UART port connected to RFID-FE board
I2C i2c(PTB1, PTB0);                // I2C port connected to VL6180X and EEPROM - note addresses above)

// GPIO signals
DigitalOut led_err(PTC1);           // Red LED shows error condition (active low)
DigitalOut led_com(PTC2);           // Yellow LED shows communication activity (active low)
DigitalOut rfid_int(PTD4);          // RFID FE power control (active high)
DigitalOut rfid_isp(PTD5);          // RFID FE In-System Programming (active high)
DigitalOut rfid_rst(PTD6);          // RFID FE Reset (active high)
DigitalOut rfid_pwr(PTE30);         // RFID power switch on USB board (active high for prototype 1, low for all others)
DigitalIn rfid_hot(PTE0);           // RFID over-current detection on USB board power switch (active low)
InterruptIn prox_int(PTD7);         // Proximity sensor interrupt (active low)

// buffers & variables
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];
uint8_t i2c_buffer_rx[32];          // buffers for I2C devices - Proximity sensor and EEPROM
uint8_t i2c_buffer_tx[32];
int i, j;                           // index variables
int status = 0x00;                  // return value

int prox_irq(void)
{
    return 0;
}

int init_periph(void)
{
    // Set up peripherals
    // RFID
    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
        
    // Prox
    i2c.frequency(400000);                                          // I2C speed = 400Kbps
    prox_int.mode(PullUp);                                          // pull up proximity sensor interrupt at MCU
    
    
    return 0;
}

int rfid_msg(void)
{
    bool end_mark = FALSE;
    int i;
    uint8_t crcCount = sizeof(uart_buffer_tx);                      // use tx buffer size to start
    
    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_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_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
    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
                
        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
        }
        if (crcCount-- == 0)                                        // end of message
        {
            if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK;  
            break;
        }
    }
    return ERR_NONE;
}


int prox_msg()
{
 bool end_mark = FALSE;
    int i;
    uint8_t crcCount = sizeof(i2c_buffer_tx);                      // use tx buffer size to start
    
    i2c.txBufferFlush();                                           // clear out UART buffers
    i2c.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_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_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
    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
                
        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
        }
        if (crcCount-- == 0)                                        // end of message
        {
            if (end_mark == FALSE) return ERR_UART_NO_RX_ENDMARK;  
            break;
        }
    }
    return ERR_NONE;
    return ERR_NONE;    
}

int gpio_msg()
{
    return ERR_NONE;
}

int eeprom_msg()                                                    // eeprom to be implemented along with next hardware prototype
{
    return ERR_NONE;
}

int main() 
{
    // initialize everything
   
    wait(3.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_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
            }
            
            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.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 0xCC:                                                  // Proximity Sensor
           for (i = 0; i < sizeof(cdc_buffer_rx); i++)
            {
                i2c_buffer_tx[i] = cdc_buffer_rx[i + 1];           // copy USB message to buffer for I2C
            }
            
            status = prox_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.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
            eeprom_msg();
            break;
        default:
            return ERR_CDC_BAD_CMD;
    }
}