Code for the mbed NXP LPC1768. To be used on The Robot Studio Master Boards. License : Simplified BSD.

Dependencies:   MODSERIAL mbed

main.cpp

Committer:
rrknight
Date:
2013-02-28
Revision:
0:369222671f3c
Child:
1:95d85c81bb11

File content as of revision 0:369222671f3c:

#include"mbed.h"

#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512
#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024
#include "MODSERIAL.h"

//Define 
#define NUMBER_MAX_EPOS2_PER_SLAVE  15
#define NUMBER_MSG_PER_PACKET       15
#define NUMBER_BYTES_PER_MSG        7
#define NUMBER_SLAVE_BOARDS         3
#define EPOS2_OK                    0
#define EPOS2_ERROR                 -1
#define LOOP_PERIOD_TIME            20000 //20 ms

//SPI RxTx FIFO bits
#define TNF 0x02
#define TFE 0x01
#define RNE 0x04

MODSERIAL ros(p28, p27, 1024, 512); // tx, rx
Serial pc(USBTX, USBRX); //terminal for debug
DigitalOut ledchain[] = {(LED1), (LED2), (LED3), (LED4)}; //used for debugging
DigitalOut logicPin(p26); //to record with Logic analyser on an event, pin high.

char* readBufferSerial; //[NUMBER_MSG_PER_PACKET][NUMBER_BYTES_PER_MSG]; //buffer of packets read by the master (written by the ros node on pc side)
uint8_t writeBufferSPI[NUMBER_SLAVE_BOARDS][NUMBER_MSG_PER_PACKET][NUMBER_BYTES_PER_MSG]; //buffer ready to be sent over SPI to different slaves
uint8_t readBufferSPI[NUMBER_SLAVE_BOARDS][NUMBER_MSG_PER_PACKET][NUMBER_BYTES_PER_MSG]; //buffer read by the master on SPI bus 

Timer timer;
uint64_t begin, end;
uint8_t numberCmds[NUMBER_SLAVE_BOARDS];

bool newCmd_detected = false;
uint8_t nbArrows = 0;

uint8_t i=0;
uint8_t j=0;
 
char rByte = 0x00;
bool threeArrowsFound = false;

// Called everytime a new character goes into
// the RX buffer. Test that character for '/'
// Note, rxGetLastChar() gets the last char that
// we received but it does NOT remove it from
// the RX buffer.
void rxCallback(MODSERIAL_IRQ_INFO *q) 
{
    logicPin = 1;
    
    MODSERIAL *serial = q->serial;
    
    if ( serial->rxGetLastChar() == '/') 
    {
        //pc.printf("new1\n");
        //newline_detected = true;
        serial->move(readBufferSerial, NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG, '\n');
        serial->rxBufferFlush();
        //pc.printf("new\n");
        //for(int n=0; n<1; n++)
        //{
        //pc.printf("new2\n\r");
        //pc.printf("0x%02X\n", s[0]);
            //pc.printf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n", s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
        //}
    }
    /*
    rByte = serial->rxGetLastChar();
    
    if(nbArrows < 3)
    {
        if(rByte == 0x3C) 
        {   
            nbArrows++;  
        }
        else 
        {
            nbArrows = 0; 
        }
        
        if(nbArrows == 3) 
        {
            threeArrowsFound = true;
        } 
    }
    else
    {               
        readBufferSerial[i][j] = rByte;
        //pc.printf("i=%d j=%d\n", i,j);
        
        j++;
        if(j >= NUMBER_BYTES_PER_MSG)
        {
            j = 0;
            i++; //next cmd
            
            if(i >= NUMBER_MSG_PER_PACKET)
            {
                nbArrows = 0;
                i = 0;
                j = 0;
                serial->rxBufferFlush();
                //serial->txBufferFlush();
                
                for(int n=0; n<1; n++)
                {
                    pc.printf("0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n", readBufferSerial[n][0], readBufferSerial[n][1], readBufferSerial[n][2], readBufferSerial[n][3], readBufferSerial[n][4], readBufferSerial[n][5], readBufferSerial[n][6]);
                }
            }   
        }        
    }
    */
    logicPin = 0;
}
 
int main() 
{
    ros.baud(460800); //460800 works //921600 don't
    pc.baud(115200);
    
    i=0;
    j=0;
    
    //init alloc
    readBufferSerial = (char*)malloc(NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG*sizeof(char*));
    
    ros.attach(&rxCallback, MODSERIAL::RxIrq);
    
    pc.printf("*** Start Master Main ***\n\r");
    
    logicPin = 0;
 
    // Wait here until we detect the \n going into the buffer.
    while(!newCmd_detected);    
    
    // When we get here the RX buffer now contains a sentence.
    // ...
 
}