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-06
Revision:
3:af892e4bf53e
Parent:
2:201618ffa295
Child:
4:7da0cd1fcb8a

File content as of revision 3:af892e4bf53e:

#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       45
#define NUMBER_BYTES_PER_MSG        6
#define NUMBER_BYTES_TO_READ        NUMBER_BYTES_PER_MSG + 2
#define NUMBER_SLAVE_BOARDS         3
#define FIRST_NODE_ID_SLAVE_1       1
#define FIRST_NODE_ID_SLAVE_2       FIRST_NODE_ID_SLAVE_1 + NUMBER_MAX_EPOS2_PER_SLAVE
#define FIRST_NODE_ID_SLAVE_3       FIRST_NODE_ID_SLAVE_2 + NUMBER_MAX_EPOS2_PER_SLAVE
#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 OPEN_ARROW          0x3C //< = 60
#define CLOSE_ARROW         0x3E //< = 62
#define DUMMY_BYTE          0x00
#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.

SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs[NUMBER_SLAVE_BOARDS] = {(p8), (p9), (p10)}; //Slave Mbed number 1 //chip select
DigitalOut sync_slave[NUMBER_SLAVE_BOARDS] = {(p25), (p24), (p23)}; //test to sync the slave

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_MAX_EPOS2_PER_SLAVE][NUMBER_BYTES_TO_READ]; //buffer ready to be sent over SPI to different slaves
uint8_t readBufferSPI[NUMBER_SLAVE_BOARDS][NUMBER_MAX_EPOS2_PER_SLAVE][NUMBER_BYTES_TO_READ]; //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;
 
char rByte = 0x00;
char writeChecksum[NUMBER_SLAVE_BOARDS]; //one checksum per board
char readChecksum[NUMBER_SLAVE_BOARDS];

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;
}

void calculateSPIChecksum()
{
    for(int k=0; k<NUMBER_SLAVE_BOARDS; k++)
    {
        int sum = 0; 
        
        for(int i=0; i<NUMBER_MAX_EPOS2_PER_SLAVE; i++)
        {
            for(int j=0; j<NUMBER_BYTES_TO_READ; j++)
            {
                writeChecksum[k] += writeBufferSPI[k][i][j];
            }
        }
    
        writeChecksum[k] = (char)(~sum);
    }
}

// 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;      
    rByte = serial->rxGetLastChar();
    
    if(!fiveArrowsFound)
    {
        if(nbArrows < NUMBER_OF_ARROWS)
        {
            if(rByte == CLOSE_ARROW)
            {   
                nbArrows++;  
            }
           
            if((nbArrows > 0) && (rByte != CLOSE_ARROW))
            {
                nbArrows = 0; //reset in case the previous arrows was data. 
            }
            
            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;      
    }   
    
    logicPin = 0;
}
 
int main() 
{
    //Deselect all mbed slaves
    for(int k=0; k<NUMBER_SLAVE_BOARDS; k++)
    {
        cs[k] = 1;
        sync_slave[k] = 0;
    }
        
    ros.baud(460800); //460800 works //921600 don't
    pc.baud(115200);
    
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8, 0); //spi.format(8,3);
    spi.frequency(1000000); //32000000
    
    //init the SPI arrays
    for(int i=0; i<NUMBER_SLAVE_BOARDS; i++)
    { 
        for(int j=0; j<NUMBER_MAX_EPOS2_PER_SLAVE; j++)
        { 
            for(int k=0; k<NUMBER_BYTES_TO_READ; k++)
            {
                writeBufferSPI[i][j][k] = 0x00;
                readBufferSPI[i][j][k] = 0x00;
            }
        }
        
        writeChecksum[i] = 0x00;
    }
    
    //init alloc
    readBufferSerial = (char*)malloc(NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG*sizeof(char*));
    
    //uint8_t my_val;
    
    ros.attach(&rxCallback, MODSERIAL::RxIrq);
    
    pc.printf("*** Start Master Main ***\n\r");
    
    logicPin = 0;
 
    // Wait here until we detect a valid message in the serial RX buffer.
    while(1)
    {
        if(msgValid) //pass it to the SPI bus
        {   
            //init the SPI arrays
            for(int i=0; i<NUMBER_SLAVE_BOARDS; i++)
            { 
                for(int j=0; j<NUMBER_MAX_EPOS2_PER_SLAVE; j++)
                { 
                    for(int k=0; k<NUMBER_BYTES_TO_READ; k++)
                    {
                        writeBufferSPI[i][j][k] = 0x00; //mode 0 for null command
                        readBufferSPI[i][j][k] = 0x00;
                    }
                }
                
                writeChecksum[i] = 0x00;
            }
            
            //init nb cmds per slave
            for(int i=0; i<NUMBER_SLAVE_BOARDS; i++)
            {
                numberCmds[i] = 0;
            }
            
            
            //sort messages for each slave
            for(int i=0; i<NUMBER_MSG_PER_PACKET*NUMBER_BYTES_PER_MSG; i+=NUMBER_BYTES_PER_MSG)
            {
                uint8_t nodeID = readBufferSerial[i];
                
                if(nodeID>=FIRST_NODE_ID_SLAVE_1 && nodeID<FIRST_NODE_ID_SLAVE_2) //slave 1
                {           
                    for(int j=0; j<NUMBER_BYTES_PER_MSG; j++)
                    {
                        writeBufferSPI[0][i/NUMBER_BYTES_PER_MSG][j] = readBufferSerial[i+j];                        
                    }
                                                                                
                    numberCmds[0]++;                
                }
                else if(nodeID>=FIRST_NODE_ID_SLAVE_2 && nodeID<FIRST_NODE_ID_SLAVE_3) //slave 2
                {
                    for(int j=0; j<NUMBER_BYTES_PER_MSG; j++)
                    {
                        writeBufferSPI[1][i/NUMBER_BYTES_PER_MSG][j] = readBufferSerial[i+j];
                    }
                    numberCmds[1]++;
                }
                else if(nodeID>=FIRST_NODE_ID_SLAVE_3) //slave 3
                {
                    for(int j=0; j<NUMBER_BYTES_PER_MSG; j++)
                    {
                        writeBufferSPI[2][i/NUMBER_BYTES_PER_MSG][j] = readBufferSerial[i+j];
                    }
                    numberCmds[2]++;
                }     
            }      
            
            //add dummy bytes
            for(int k=0; k<NUMBER_SLAVE_BOARDS; k++)
            {
                for(int i=0; i<NUMBER_MAX_EPOS2_PER_SLAVE; i++)
                {   
                    for(int j=NUMBER_BYTES_PER_MSG; j<NUMBER_BYTES_TO_READ; j++)
                    {
                        writeBufferSPI[k][i][j] = DUMMY_BYTE;                        
                    }
                }
            }
            
            //now all individual SPI buffers for slaves have been created
            //compute checksum for each slave and update the variable, it'll be sent later at the end of SPI writting
            calculateSPIChecksum(); //this update the writeChecksum[k]
            
            //pc.printf("nbCmd %d %d %d\n", numberCmds[0], numberCmds[1], numberCmds[2]);   
            //pc.printf("1st Cmd %02X %02X %02X %02X %02X %02X %02X\n", writeBufferSPI[0][0][0], writeBufferSPI[0][0][1], writeBufferSPI[0][0][2], writeBufferSPI[0][0][3], writeBufferSPI[0][0][4], writeBufferSPI[0][0][5], writeBufferSPI[0][0][6]);  
                    
            //new commands has been grabbed and are ready to be sent to slaves            
            for(int k=0; k<NUMBER_SLAVE_BOARDS; k++)  //NUMBER_SLAVE_BOARDS for each slave
            {
                sync_slave[k] = 1;
                wait_us(10); //pause so the slave can see it's been selected //TODO array with the other slaves
                sync_slave[k] = 0;
                            
                cs[k] = 0;
                //while (!(LPC_SSP1->SR & TNF));            
                spi.write(OPEN_ARROW);
                //while (!(LPC_SSP1->SR & TFE));
                wait_us(5);
                cs[k] = 1;          
                wait_us(10); //wait_us(10);
                
                cs[k] = 0;
                //while (!(LPC_SSP1->SR & TNF));  
                spi.write(OPEN_ARROW);
               // while (!(LPC_SSP1->SR & TFE));
                wait_us(5);
                cs[k] = 1;
                wait_us(10);
                
                cs[k] = 0;
                //while (!(LPC_SSP1->SR & TNF));  
                spi.write(OPEN_ARROW);
                //while (!(LPC_SSP1->SR & TFE)); //MAYBE REMOVE COMMENT ?
                wait_us(5);
                cs[k] = 1;
                wait_us(10);
                
                for(int i=0; i<NUMBER_MAX_EPOS2_PER_SLAVE; i++) 
                {
                    for(int j=0; j<NUMBER_BYTES_PER_MSG; j++) 
                    {
                        cs[k] = 0;
                        readBufferSPI[k][i][j] = (char)(spi.write(writeBufferSPI[k][i][j]));
                        wait_us(5);
                        cs[k] = 1;
                        wait_us(10);
                    }
                }
                
                //finally write the command checksum and read the data checksum at the same time
                cs[k] = 0; 
                readChecksum[k] = (char)(spi.write(writeChecksum[k]));
                wait_us(5);
                cs[k] = 1;
                wait_us(10);                       
            }
            
            logicPin = 0;
            wait_us(10);
            logicPin = 1;
            wait_us(10);
            //print the array :
            /*
            for(int i=0; i<2; i++)
            { 
                pc.printf("%02X %02X %02X %02X %02X %02X %02X\n\r", readBufferSPI[0][i][0], readBufferSPI[0][i][1], readBufferSPI[0][i][2], readBufferSPI[0][i][3], readBufferSPI[0][i][4], readBufferSPI[0][i][5], readBufferSPI[0][i][6]);
            }
            */
 /*           int i=0;
            pc.printf("%02X %02X %02X %02X %02X %02X %02X\n", readBufferSPI[0][i][0], readBufferSPI[0][i][1], readBufferSPI[0][i][2], readBufferSPI[0][i][3], readBufferSPI[0][i][4], readBufferSPI[0][i][5], readBufferSPI[0][i][6]);
            i=14;
            pc.printf("%02X %02X %02X %02X %02X %02X %02X\n\r", readBufferSPI[0][i][0], readBufferSPI[0][i][1], readBufferSPI[0][i][2], readBufferSPI[0][i][3], readBufferSPI[0][i][4], readBufferSPI[0][i][5], readBufferSPI[0][i][6]);
            
            //pc.printf("\n\r");    
            logicPin = 0;     
            
            //build the motorDataSet_msg          
            for(int i=0; i<13; i++)
            {            
                motorDataSet_msg.motorData[i].encPosition = 100*i;
                motorDataSet_msg.motorData[i].potiPosition = 10*i;   
                motorDataSet_msg.motorData[i].current = -10*i;
                motorDataSet_msg.motorData[i].force = 2*i; 
            }
     */       
            //TODO write the data msg on serial Tx
            logicPin = 0;
            
            msgValid = false; //toggle flag for next message
            
        }
        
        wait_us(10);
    }       
}