test
Dependencies: mbed
Fork of 161006_BDK_SPIslave2 by
main.cpp@2:5311ad7c83e6, 2016-10-05 (annotated)
- Committer:
- bcup
- Date:
- Wed Oct 05 07:09:51 2016 +0000
- Revision:
- 2:5311ad7c83e6
- Parent:
- 1:3a338e553a54
- Child:
- 3:4bb95aa46ebc
fgf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcup | 0:24e90e3ca3f4 | 1 | #include "mbed.h" |
bcup | 0:24e90e3ca3f4 | 2 | |
bcup | 0:24e90e3ca3f4 | 3 | #define DEBUG_SPI |
bcup | 0:24e90e3ca3f4 | 4 | |
bcup | 0:24e90e3ca3f4 | 5 | #ifdef DEBUG_SPI |
bcup | 0:24e90e3ca3f4 | 6 | #define PRINTD(arg1,arg2...) printf(arg1,##arg2) |
bcup | 0:24e90e3ca3f4 | 7 | #endif |
bcup | 0:24e90e3ca3f4 | 8 | |
bcup | 2:5311ad7c83e6 | 9 | SPISlave spislave(PA_7, PA_6, PA_5, PA_4); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC |
bcup | 2:5311ad7c83e6 | 10 | Serial pc(USBTX, USBRX); |
bcup | 0:24e90e3ca3f4 | 11 | |
bcup | 2:5311ad7c83e6 | 12 | void SPI_SlaveInit() |
bcup | 0:24e90e3ca3f4 | 13 | { |
bcup | 2:5311ad7c83e6 | 14 | PRINTD("Set the SPI SLAVE format\n"); |
bcup | 2:5311ad7c83e6 | 15 | spislave.format(8,0); // setup 8bit 0 mode |
bcup | 2:5311ad7c83e6 | 16 | PRINTD("Set the SPI SLAVE frequency\n"); |
bcup | 2:5311ad7c83e6 | 17 | spislave.frequency(); // default 1MHz |
bcup | 0:24e90e3ca3f4 | 18 | } |
bcup | 0:24e90e3ca3f4 | 19 | |
bcup | 2:5311ad7c83e6 | 20 | void SPI_SlaveWrite() |
bcup | 0:24e90e3ca3f4 | 21 | { |
bcup | 2:5311ad7c83e6 | 22 | char tx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 23 | char reply; |
bcup | 2:5311ad7c83e6 | 24 | char i; |
bcup | 1:3a338e553a54 | 25 | char temp; |
bcup | 2:5311ad7c83e6 | 26 | char tx_cnt = 0; |
bcup | 2:5311ad7c83e6 | 27 | int value; |
bcup | 2:5311ad7c83e6 | 28 | PRINTD("Input Strging="); |
bcup | 0:24e90e3ca3f4 | 29 | while(1) |
bcup | 0:24e90e3ca3f4 | 30 | { |
bcup | 0:24e90e3ca3f4 | 31 | |
bcup | 0:24e90e3ca3f4 | 32 | temp=getchar(); |
bcup | 2:5311ad7c83e6 | 33 | tx_buffer[tx_cnt++]=temp; |
bcup | 0:24e90e3ca3f4 | 34 | if(temp==0x0d) |
bcup | 0:24e90e3ca3f4 | 35 | { |
bcup | 0:24e90e3ca3f4 | 36 | tx_buffer[tx_cnt]=0; |
bcup | 0:24e90e3ca3f4 | 37 | PRINTD("\nData send Finish...\n"); |
bcup | 0:24e90e3ca3f4 | 38 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 39 | { |
bcup | 0:24e90e3ca3f4 | 40 | PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 41 | } |
bcup | 0:24e90e3ca3f4 | 42 | PRINTD("\n\n"); |
bcup | 0:24e90e3ca3f4 | 43 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 44 | { |
bcup | 0:24e90e3ca3f4 | 45 | value=tx_buffer[i]; |
bcup | 2:5311ad7c83e6 | 46 | PRINTD("write[%d]=%c[%02x]\n",i,value,value); |
bcup | 2:5311ad7c83e6 | 47 | spislave.reply(value); |
bcup | 0:24e90e3ca3f4 | 48 | } |
bcup | 2:5311ad7c83e6 | 49 | for(i=0;i<tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 50 | { |
bcup | 0:24e90e3ca3f4 | 51 | tx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 52 | PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 53 | } |
bcup | 2:5311ad7c83e6 | 54 | tx_cnt=0; |
bcup | 2:5311ad7c83e6 | 55 | PRINTD("break\n"); |
bcup | 2:5311ad7c83e6 | 56 | break; |
bcup | 0:24e90e3ca3f4 | 57 | } |
bcup | 0:24e90e3ca3f4 | 58 | else |
bcup | 0:24e90e3ca3f4 | 59 | { |
bcup | 0:24e90e3ca3f4 | 60 | PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); |
bcup | 0:24e90e3ca3f4 | 61 | } |
bcup | 0:24e90e3ca3f4 | 62 | } |
bcup | 2:5311ad7c83e6 | 63 | return; |
bcup | 0:24e90e3ca3f4 | 64 | } |
bcup | 0:24e90e3ca3f4 | 65 | |
bcup | 0:24e90e3ca3f4 | 66 | int main() |
bcup | 0:24e90e3ca3f4 | 67 | { |
bcup | 2:5311ad7c83e6 | 68 | int i; |
bcup | 2:5311ad7c83e6 | 69 | char valueFromMaster; |
bcup | 2:5311ad7c83e6 | 70 | char rx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 71 | char rx_cnt = -1; |
bcup | 2:5311ad7c83e6 | 72 | PRINTD("\n=========SLAVE=========\n"); |
bcup | 2:5311ad7c83e6 | 73 | SPI_SlaveInit(); |
bcup | 2:5311ad7c83e6 | 74 | |
bcup | 0:24e90e3ca3f4 | 75 | while(1) |
bcup | 0:24e90e3ca3f4 | 76 | { |
bcup | 2:5311ad7c83e6 | 77 | |
bcup | 2:5311ad7c83e6 | 78 | if(spislave.receive()) |
bcup | 2:5311ad7c83e6 | 79 | { |
bcup | 2:5311ad7c83e6 | 80 | PRINTD("----1\n"); |
bcup | 2:5311ad7c83e6 | 81 | if(pc.readable()) |
bcup | 2:5311ad7c83e6 | 82 | { |
bcup | 2:5311ad7c83e6 | 83 | SPI_SlaveWrite(); |
bcup | 2:5311ad7c83e6 | 84 | } |
bcup | 2:5311ad7c83e6 | 85 | PRINTD("----2\n"); |
bcup | 2:5311ad7c83e6 | 86 | valueFromMaster = spislave.read(); |
bcup | 2:5311ad7c83e6 | 87 | PRINTD("----3\n"); |
bcup | 2:5311ad7c83e6 | 88 | //PRINTD("valueFromMaster="); |
bcup | 2:5311ad7c83e6 | 89 | //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); |
bcup | 2:5311ad7c83e6 | 90 | rx_buffer[++rx_cnt]=valueFromMaster; |
bcup | 2:5311ad7c83e6 | 91 | if(valueFromMaster==0) |
bcup | 2:5311ad7c83e6 | 92 | { |
bcup | 2:5311ad7c83e6 | 93 | PRINTD("rx_string="); |
bcup | 2:5311ad7c83e6 | 94 | for(i=0;i<rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 95 | { |
bcup | 2:5311ad7c83e6 | 96 | PRINTD("%c",rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 97 | } |
bcup | 2:5311ad7c83e6 | 98 | PRINTD("\n"); |
bcup | 2:5311ad7c83e6 | 99 | for(i=0;i<=rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 100 | { |
bcup | 2:5311ad7c83e6 | 101 | rx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 102 | PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 103 | } |
bcup | 2:5311ad7c83e6 | 104 | rx_cnt=-1; |
bcup | 2:5311ad7c83e6 | 105 | |
bcup | 2:5311ad7c83e6 | 106 | }//valueFromMaster if |
bcup | 2:5311ad7c83e6 | 107 | |
bcup | 2:5311ad7c83e6 | 108 | }// spislave.receive() if |
bcup | 2:5311ad7c83e6 | 109 | /* |
bcup | 2:5311ad7c83e6 | 110 | |
bcup | 2:5311ad7c83e6 | 111 | */ |
bcup | 2:5311ad7c83e6 | 112 | }//end of while |
bcup | 2:5311ad7c83e6 | 113 | }//end of main |