161006_BDK_SPIslave
Dependencies: mbed
Fork of Nucleo_SPIslave_F303K8_KSS by
main.cpp@0:24e90e3ca3f4, 2016-10-05 (annotated)
- Committer:
- bcup
- Date:
- Wed Oct 05 07:02:51 2016 +0000
- Revision:
- 0:24e90e3ca3f4
- Child:
- 1:3a338e553a54
161005_K303K8
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 | 0:24e90e3ca3f4 | 9 | SPISlave spislave(PA_7, PA_6, PA_5, PA_4); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC |
bcup | 0:24e90e3ca3f4 | 10 | Serial pc(USBTX, USBRX); |
bcup | 0:24e90e3ca3f4 | 11 | |
bcup | 0:24e90e3ca3f4 | 12 | void SPI_SlaveInit() |
bcup | 0:24e90e3ca3f4 | 13 | { |
bcup | 0:24e90e3ca3f4 | 14 | PRINTD("Set the SPI SLAVE format\n"); |
bcup | 0:24e90e3ca3f4 | 15 | spislave.format(8,0); // setup 8bit 0 mode |
bcup | 0:24e90e3ca3f4 | 16 | PRINTD("Set the SPI SLAVE frequency\n"); |
bcup | 0:24e90e3ca3f4 | 17 | spislave.frequency(); // default 1MHz |
bcup | 0:24e90e3ca3f4 | 18 | } |
bcup | 0:24e90e3ca3f4 | 19 | |
bcup | 0:24e90e3ca3f4 | 20 | void SPI_SlaveWrite() |
bcup | 0:24e90e3ca3f4 | 21 | { |
bcup | 0:24e90e3ca3f4 | 22 | char tx_buffer[255]={0}; |
bcup | 0:24e90e3ca3f4 | 23 | char reply; |
bcup | 0:24e90e3ca3f4 | 24 | char i; |
bcup | 0:24e90e3ca3f4 | 25 | char temp; |
bcup | 0:24e90e3ca3f4 | 26 | char tx_cnt = 0; |
bcup | 0:24e90e3ca3f4 | 27 | int value; |
bcup | 0:24e90e3ca3f4 | 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 | 0:24e90e3ca3f4 | 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 | 0:24e90e3ca3f4 | 46 | PRINTD("write[%d]=%c[%02x]\n",i,value,value); |
bcup | 0:24e90e3ca3f4 | 47 | spislave.reply(value); |
bcup | 0:24e90e3ca3f4 | 48 | } |
bcup | 0:24e90e3ca3f4 | 49 | for(i=0;i<tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 50 | { |
bcup | 0:24e90e3ca3f4 | 51 | tx_buffer[i]=0; |
bcup | 0:24e90e3ca3f4 | 52 | PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 53 | } |
bcup | 0:24e90e3ca3f4 | 54 | tx_cnt=0; |
bcup | 0:24e90e3ca3f4 | 55 | PRINTD("break\n"); |
bcup | 0:24e90e3ca3f4 | 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 | 0:24e90e3ca3f4 | 63 | return; |
bcup | 0:24e90e3ca3f4 | 64 | } |
bcup | 0:24e90e3ca3f4 | 65 | |
bcup | 0:24e90e3ca3f4 | 66 | int main() |
bcup | 0:24e90e3ca3f4 | 67 | { |
bcup | 0:24e90e3ca3f4 | 68 | int i; |
bcup | 0:24e90e3ca3f4 | 69 | char valueFromMaster; |
bcup | 0:24e90e3ca3f4 | 70 | char rx_buffer[255]={0}; |
bcup | 0:24e90e3ca3f4 | 71 | char rx_cnt = -1; |
bcup | 0:24e90e3ca3f4 | 72 | PRINTD("\n=========SLAVE=========\n"); |
bcup | 0:24e90e3ca3f4 | 73 | SPI_SlaveInit(); |
bcup | 0:24e90e3ca3f4 | 74 | |
bcup | 0:24e90e3ca3f4 | 75 | while(1) |
bcup | 0:24e90e3ca3f4 | 76 | { |
bcup | 0:24e90e3ca3f4 | 77 | |
bcup | 0:24e90e3ca3f4 | 78 | if(spislave.receive()) |
bcup | 0:24e90e3ca3f4 | 79 | { |
bcup | 0:24e90e3ca3f4 | 80 | PRINTD("----1\n"); |
bcup | 0:24e90e3ca3f4 | 81 | if(pc.readable()) |
bcup | 0:24e90e3ca3f4 | 82 | { |
bcup | 0:24e90e3ca3f4 | 83 | SPI_SlaveWrite(); |
bcup | 0:24e90e3ca3f4 | 84 | } |
bcup | 0:24e90e3ca3f4 | 85 | PRINTD("----2\n"); |
bcup | 0:24e90e3ca3f4 | 86 | valueFromMaster = spislave.read(); |
bcup | 0:24e90e3ca3f4 | 87 | PRINTD("----3\n"); |
bcup | 0:24e90e3ca3f4 | 88 | //PRINTD("valueFromMaster="); |
bcup | 0:24e90e3ca3f4 | 89 | //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); |
bcup | 0:24e90e3ca3f4 | 90 | rx_buffer[++rx_cnt]=valueFromMaster; |
bcup | 0:24e90e3ca3f4 | 91 | if(valueFromMaster==0) |
bcup | 0:24e90e3ca3f4 | 92 | { |
bcup | 0:24e90e3ca3f4 | 93 | PRINTD("rx_string="); |
bcup | 0:24e90e3ca3f4 | 94 | for(i=0;i<rx_cnt;i++) |
bcup | 0:24e90e3ca3f4 | 95 | { |
bcup | 0:24e90e3ca3f4 | 96 | PRINTD("%c",rx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 97 | } |
bcup | 0:24e90e3ca3f4 | 98 | PRINTD("\n"); |
bcup | 0:24e90e3ca3f4 | 99 | for(i=0;i<=rx_cnt;i++) |
bcup | 0:24e90e3ca3f4 | 100 | { |
bcup | 0:24e90e3ca3f4 | 101 | rx_buffer[i]=0; |
bcup | 0:24e90e3ca3f4 | 102 | PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 103 | } |
bcup | 0:24e90e3ca3f4 | 104 | rx_cnt=-1; |
bcup | 0:24e90e3ca3f4 | 105 | |
bcup | 0:24e90e3ca3f4 | 106 | }//valueFromMaster if |
bcup | 0:24e90e3ca3f4 | 107 | |
bcup | 0:24e90e3ca3f4 | 108 | }// spislave.receive() if |
bcup | 0:24e90e3ca3f4 | 109 | /* |
bcup | 0:24e90e3ca3f4 | 110 | |
bcup | 0:24e90e3ca3f4 | 111 | */ |
bcup | 0:24e90e3ca3f4 | 112 | }//end of while |
bcup | 0:24e90e3ca3f4 | 113 | }//end of main |