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-03-05
Revision:
1:95d85c81bb11
Parent:
0:369222671f3c
Child:
2:201618ffa295

File content as of revision 1:95d85c81bb11:

#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

#define CLOSE_ARROW         0x3E //< = 62
#define NUMBER_OF_ARROWS    5

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 fiveArrowsFound = false;
bool msgValid = false;

int move(char *s, int nbBytes) //custom move function (cp from MODESERIAL without the end character)
{
    int counter = 0;
    char c;
    while(ros.readable()) {
        c = ros.getc();        
        *(s++) = c;
        counter++;
        if (counter == nbBytes) break;
    }
    return counter;
}

bool verifyChecksum(char* data, int length, char checksum)
{      
    for(int i=0; i<length; i++)
    {
        checksum += data[i];
    }        
    
    checksum++; //add 1 to obtain 0x00
        
    if(checksum == 0x00) return true;
    else return 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();
    //pc.printf("0x%02X ", rByte);
    
    if(!fiveArrowsFound)
    {
        if(nbArrows < NUMBER_OF_ARROWS)
        {
            if(rByte == CLOSE_ARROW)
            {   
                nbArrows++;  
                //pc.printf("nbArrows %d\n\r", nbArrows);
            }
            //else 
            if((nbArrows > 0) && (rByte != CLOSE_ARROW))
            {
                nbArrows = 0; //reset in case the previous arrows was data. 
                //pc.printf("reset\n\r");
            }
            
            if(nbArrows == NUMBER_OF_ARROWS) 
            {
                fiveArrowsFound = true;
            } 
        }
    }
    else //fiveArrowsFound, so rByte is the checksum
    {
        move(readBufferSerial, NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG);
        //pc.printf("r cs 0x%02X\n", rByte);
        //pc.printf("move %02X %02X %02X %02X %02X %02X %02X \n", readBufferSerial[0], readBufferSerial[1], readBufferSerial[2], readBufferSerial[3], readBufferSerial[4], readBufferSerial[5], readBufferSerial[6]);
        
        msgValid = verifyChecksum(readBufferSerial, NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG, rByte);
               
        if(msgValid) pc.printf("msgValid\n\r");
        
        //reset
        serial->rxBufferFlush();  
        nbArrows = 0;
        fiveArrowsFound = false;      
    }   
/*        
    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(1)
    {
        if(msgValid)
        {
            //pass it to the SPI bus
        }
    }
        
    
    // When we get here the RX buffer now contains a sentence.
    // ...
 
}