Fabien Lepoutre
/
Prog_Projet
Projet3i4
ComSPI.cpp@2:e253b64effa0, 2012-04-24 (annotated)
- Committer:
- fabienlepoutre
- Date:
- Tue Apr 24 15:01:11 2012 +0000
- Revision:
- 2:e253b64effa0
- Parent:
- 1:bb98d7d1e25f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabienlepoutre | 0:f0c9c47e4edd | 1 | #include "ComSPI.h" |
fabienlepoutre | 0:f0c9c47e4edd | 2 | SPI SPI_Master(p11, p12, p13); //mosi, miso, sck |
fabienlepoutre | 0:f0c9c47e4edd | 3 | DigitalOut cs(p14); //Selection Slave |
fabienlepoutre | 0:f0c9c47e4edd | 4 | SPISlave SPI_Slave(p5, p6, p7, p8); //mosi, miso, sck, ssel |
fabienlepoutre | 1:bb98d7d1e25f | 5 | volatile int attente; |
fabienlepoutre | 0:f0c9c47e4edd | 6 | |
fabienlepoutre | 0:f0c9c47e4edd | 7 | |
fabienlepoutre | 0:f0c9c47e4edd | 8 | void v_InitSPI(){ |
fabienlepoutre | 0:f0c9c47e4edd | 9 | SPI_Master.format(8,0); |
fabienlepoutre | 0:f0c9c47e4edd | 10 | SPI_Master.frequency(1000000); |
fabienlepoutre | 0:f0c9c47e4edd | 11 | SPI_Slave.format(8,0); |
fabienlepoutre | 1:bb98d7d1e25f | 12 | SPI_Slave.frequency(1000000); |
fabienlepoutre | 0:f0c9c47e4edd | 13 | } |
fabienlepoutre | 0:f0c9c47e4edd | 14 | |
fabienlepoutre | 0:f0c9c47e4edd | 15 | |
fabienlepoutre | 0:f0c9c47e4edd | 16 | void v_TestComSPI(){ |
fabienlepoutre | 1:bb98d7d1e25f | 17 | attente=0; |
fabienlepoutre | 0:f0c9c47e4edd | 18 | v_InitSPI(); |
fabienlepoutre | 0:f0c9c47e4edd | 19 | Thread tSlave(v_threadSPISlave); |
fabienlepoutre | 0:f0c9c47e4edd | 20 | Thread tMaster(v_threadSPIMaster); |
fabienlepoutre | 1:bb98d7d1e25f | 21 | while(attente==0); |
fabienlepoutre | 0:f0c9c47e4edd | 22 | } |
fabienlepoutre | 0:f0c9c47e4edd | 23 | |
fabienlepoutre | 0:f0c9c47e4edd | 24 | void v_threadSPIMaster(void const* argument){ |
fabienlepoutre | 0:f0c9c47e4edd | 25 | int answer, msgsend=1; |
fabienlepoutre | 0:f0c9c47e4edd | 26 | cs=0; |
fabienlepoutre | 0:f0c9c47e4edd | 27 | answer = SPI_Master.write(msgsend); |
fabienlepoutre | 0:f0c9c47e4edd | 28 | cs=1; |
fabienlepoutre | 0:f0c9c47e4edd | 29 | wait_ms(100); |
fabienlepoutre | 0:f0c9c47e4edd | 30 | cs=0; |
fabienlepoutre | 0:f0c9c47e4edd | 31 | answer = SPI_Master.write(0x00); |
fabienlepoutre | 0:f0c9c47e4edd | 32 | cs=1; |
fabienlepoutre | 0:f0c9c47e4edd | 33 | |
fabienlepoutre | 0:f0c9c47e4edd | 34 | printf("Master -> Valeur envoye :%d Valeur recu : %d \r\n", msgsend, answer); |
fabienlepoutre | 0:f0c9c47e4edd | 35 | if(answer == (msgsend+5)){ //Normaly slave returns msgsend + 5 |
fabienlepoutre | 0:f0c9c47e4edd | 36 | cs=0; |
fabienlepoutre | 0:f0c9c47e4edd | 37 | answer=SPI_Master.write(TERMINATE); //send a Terminate value |
fabienlepoutre | 0:f0c9c47e4edd | 38 | cs=1; |
fabienlepoutre | 1:bb98d7d1e25f | 39 | if(answer==0) printf("Communication SPI OK \r\n)"); //Normaly Slave returns 0 : end of transmission |
fabienlepoutre | 1:bb98d7d1e25f | 40 | else printf("Probleme de communication SPI1 \r\n)"); |
fabienlepoutre | 0:f0c9c47e4edd | 41 | } |
fabienlepoutre | 1:bb98d7d1e25f | 42 | else{ |
fabienlepoutre | 1:bb98d7d1e25f | 43 | printf("Probleme de communication SPI2 \r\n"); |
fabienlepoutre | 1:bb98d7d1e25f | 44 | } |
fabienlepoutre | 1:bb98d7d1e25f | 45 | attente++; |
fabienlepoutre | 0:f0c9c47e4edd | 46 | } |
fabienlepoutre | 0:f0c9c47e4edd | 47 | |
fabienlepoutre | 0:f0c9c47e4edd | 48 | void v_threadSPISlave(void const* argument){ |
fabienlepoutre | 0:f0c9c47e4edd | 49 | int rec, rec2; |
fabienlepoutre | 0:f0c9c47e4edd | 50 | bool end=false; |
fabienlepoutre | 0:f0c9c47e4edd | 51 | while(!end){ |
fabienlepoutre | 0:f0c9c47e4edd | 52 | if(SPI_Slave.receive()) { |
fabienlepoutre | 0:f0c9c47e4edd | 53 | rec = SPI_Slave.read(); |
fabienlepoutre | 0:f0c9c47e4edd | 54 | rec2=rec+5; // Read byte from master |
fabienlepoutre | 0:f0c9c47e4edd | 55 | printf("Slave -> Valeur recue :%d\r\n", rec); |
fabienlepoutre | 0:f0c9c47e4edd | 56 | if(rec!=0){ // discard the dummy buffer write |
fabienlepoutre | 0:f0c9c47e4edd | 57 | if(rec == TERMINATE) { //if terminate signal : stop kill thread |
fabienlepoutre | 0:f0c9c47e4edd | 58 | printf("Signal Termine \r\n"); |
fabienlepoutre | 0:f0c9c47e4edd | 59 | end = true; |
fabienlepoutre | 0:f0c9c47e4edd | 60 | SPI_Slave.reply(0); |
fabienlepoutre | 0:f0c9c47e4edd | 61 | } |
fabienlepoutre | 0:f0c9c47e4edd | 62 | |
fabienlepoutre | 0:f0c9c47e4edd | 63 | else{ |
fabienlepoutre | 0:f0c9c47e4edd | 64 | SPI_Slave.reply(rec2); // Make this the next reply (add 5); |
fabienlepoutre | 0:f0c9c47e4edd | 65 | printf("Slave -> Valeur renvoyee : %d \r\n",rec2); |
fabienlepoutre | 0:f0c9c47e4edd | 66 | } |
fabienlepoutre | 1:bb98d7d1e25f | 67 | } |
fabienlepoutre | 0:f0c9c47e4edd | 68 | } |
fabienlepoutre | 0:f0c9c47e4edd | 69 | } |
fabienlepoutre | 0:f0c9c47e4edd | 70 | } |
fabienlepoutre | 0:f0c9c47e4edd | 71 |