Projet3i4

ComSPI.cpp

Committer:
fabienlepoutre
Date:
2012-04-23
Revision:
0:f0c9c47e4edd
Child:
1:bb98d7d1e25f

File content as of revision 0:f0c9c47e4edd:

#include "ComSPI.h"
SPI SPI_Master(p11, p12, p13);          //mosi, miso, sck
DigitalOut cs(p14);                     //Selection Slave
SPISlave SPI_Slave(p5, p6, p7, p8);     //mosi, miso, sck, ssel



void v_InitSPI(){
    printf("Entree dans Init \n\r");
    SPI_Master.format(8,0);
    SPI_Master.frequency(1000000);    
    SPI_Slave.format(8,0);
    SPI_Slave.frequency(1000000);
        
}


void v_TestComSPI(){
    printf("Entree dans testcom \r\n");
    v_InitSPI();
    Thread tSlave(v_threadSPISlave);
    Thread tMaster(v_threadSPIMaster);
}

void v_threadSPIMaster(void const* argument){    
    printf("Entree dans le thread Master \r \n");
    int answer, msgsend=1;
    
   
    cs=0;
    answer = SPI_Master.write(msgsend);   
    cs=1;  
    cout<<endl<<cs<<endl;
    wait_ms(100);
    cs=0;
    answer = SPI_Master.write(0x00); 
    cs=1;
    
    printf("Master -> Valeur envoye :%d  Valeur recu : %d \r\n", msgsend, answer);
    if(answer == (msgsend+5)){          //Normaly slave returns msgsend + 5
        cs=0;
        answer=SPI_Master.write(TERMINATE);   //send a Terminate value
        cs=1;  
        if(answer==0) printf("Communication OK \r\n)");    //Normaly Slave returns 0 : end of transmission
        else printf("Probleme de communication \r\n)");
    }  
    printf("Sortie du thread Master \r\n");
}

void v_threadSPISlave(void const* argument){
    printf("Entree dans le thread Slave \r\n");
     int rec, rec2;
     bool end=false;
     while(!end){
         if(SPI_Slave.receive()) {
                
                rec = SPI_Slave.read(); 
                rec2=rec+5;                   // Read byte from master
                printf("Slave -> Valeur recue :%d\r\n", rec);                  
                if(rec!=0){                                  // discard the dummy buffer write
                    if(rec == TERMINATE) {                   //if terminate signal : stop kill thread
                        printf("Signal Termine \r\n");
                        end = true;
                        SPI_Slave.reply(0);
                     }
                     
                     else{ 
                        SPI_Slave.reply(rec2);                   // Make this the next reply (add 5);
                        printf("Slave -> Valeur renvoyee : %d \r\n",rec2);
                        }
                }
                    
          }
      }
      printf("Sortie du thread Slave \r\n");


}