161006_BDK_SPIslave
Dependencies: mbed
Fork of Nucleo_SPIslave_F303K8_KSS by
Diff: main.cpp
- Revision:
- 1:3a338e553a54
- Parent:
- 0:24e90e3ca3f4
- Child:
- 2:5311ad7c83e6
--- a/main.cpp Wed Oct 05 07:02:51 2016 +0000 +++ b/main.cpp Wed Oct 05 07:04:31 2016 +0000 @@ -6,31 +6,36 @@ #define PRINTD(arg1,arg2...) printf(arg1,##arg2) #endif -SPISlave spislave(PA_7, PA_6, PA_5, PA_4); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC -Serial pc(USBTX, USBRX); +SPI spi(PA_7,PA_6,PA_5); // MOSI, MISO, SCLK(CLK,SCK) +DigitalOut cs(PA_4); -void SPI_SlaveInit() +void SPI_INIT() { - PRINTD("Set the SPI SLAVE format\n"); - spislave.format(8,0); // setup 8bit 0 mode - PRINTD("Set the SPI SLAVE frequency\n"); - spislave.frequency(); // default 1MHz + PRINTD("Set SPI init..\n"); + PRINTD("Set SPI format..\n"); + spi.format(8,0); + PRINTD("Set frequency to default..\n"); + spi.frequency(1000000); // default 1MHz } -void SPI_SlaveWrite() +void SPI_Write() { + char temp; + int i,value; + char response; + char tx_cnt = -1; + char rx_cnt = -1; char tx_buffer[255]={0}; - char reply; - char i; - char temp; - char tx_cnt = 0; - int value; - PRINTD("Input Strging="); + char rx_buffer[255]={0}; + PRINTD("\n==========MASTER==========\n"); + PRINTD("DATA SEND START...\n"); + PRINTD("Lock SPI BUS..\n"); + while(1) { temp=getchar(); - tx_buffer[tx_cnt++]=temp; + tx_buffer[++tx_cnt]=temp; if(temp==0x0d) { tx_buffer[tx_cnt]=0; @@ -40,74 +45,42 @@ PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); } PRINTD("\n\n"); + spi.lock(); for(i=0;i<=tx_cnt;++i) { value=tx_buffer[i]; - PRINTD("write[%d]=%c[%02x]\n",i,value,value); - spislave.reply(value); + PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value); + cs=0; + response=spi.write(value); + cs=-1; + PRINTD("[M]receive=%c[%x]\n",response,response); + rx_buffer[++rx_cnt]=response; } - for(i=0;i<tx_cnt;++i) + spi.unlock(); + for(i=0;i<255;++i) { tx_buffer[i]=0; - PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); } - tx_cnt=0; - PRINTD("break\n"); - break; + for(i=0;i<=tx_cnt;i++) + { + PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]); + } + tx_cnt=-1; } else { PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); } } - return; } int main() { - int i; - char valueFromMaster; - char rx_buffer[255]={0}; - char rx_cnt = -1; - PRINTD("\n=========SLAVE=========\n"); - SPI_SlaveInit(); - + int send_data; + SPI_INIT(); + while(1) { - - if(spislave.receive()) - { - PRINTD("----1\n"); - if(pc.readable()) - { - SPI_SlaveWrite(); - } - PRINTD("----2\n"); - valueFromMaster = spislave.read(); - PRINTD("----3\n"); - //PRINTD("valueFromMaster="); - //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); - rx_buffer[++rx_cnt]=valueFromMaster; - if(valueFromMaster==0) - { - PRINTD("rx_string="); - for(i=0;i<rx_cnt;i++) - { - PRINTD("%c",rx_buffer[i]); - } - PRINTD("\n"); - for(i=0;i<=rx_cnt;i++) - { - rx_buffer[i]=0; - PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); - } - rx_cnt=-1; - - }//valueFromMaster if - - }// spislave.receive() if - /* - - */ - }//end of while -}//end of main + SPI_Write(); + } +}