161006_BDK_SPIslave
Dependencies: mbed
Fork of Nucleo_SPIslave_F303K8_KSS by
main.cpp@4:cc97d7a33efc, 2016-10-06 (annotated)
- Committer:
- bcup
- Date:
- Thu Oct 06 00:11:39 2016 +0000
- Revision:
- 4:cc97d7a33efc
- Parent:
- 3:4bb95aa46ebc
161006_BDK_SPIslave
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 | |
gandol2 | 3:4bb95aa46ebc | 9 | |
gandol2 | 3:4bb95aa46ebc | 10 | #define SPI_MOSI PA_7 |
gandol2 | 3:4bb95aa46ebc | 11 | #define SPI_MISO PA_6 |
gandol2 | 3:4bb95aa46ebc | 12 | #define SPI_SCLK PA_5 |
gandol2 | 3:4bb95aa46ebc | 13 | #define SPI_SSEL PA_4 |
gandol2 | 3:4bb95aa46ebc | 14 | |
gandol2 | 3:4bb95aa46ebc | 15 | |
gandol2 | 3:4bb95aa46ebc | 16 | SPISlave spi_slave(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_SSEL); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC |
gandol2 | 3:4bb95aa46ebc | 17 | Serial pc_serial(USBTX, USBRX); |
bcup | 0:24e90e3ca3f4 | 18 | |
bcup | 2:5311ad7c83e6 | 19 | void SPI_SlaveInit() |
bcup | 0:24e90e3ca3f4 | 20 | { |
bcup | 2:5311ad7c83e6 | 21 | PRINTD("Set the SPI SLAVE format\n"); |
gandol2 | 3:4bb95aa46ebc | 22 | spi_slave.format(8,0); // setup 8bit 0 mode |
bcup | 2:5311ad7c83e6 | 23 | PRINTD("Set the SPI SLAVE frequency\n"); |
gandol2 | 3:4bb95aa46ebc | 24 | spi_slave.frequency(); // default 1MHz |
bcup | 0:24e90e3ca3f4 | 25 | } |
bcup | 0:24e90e3ca3f4 | 26 | |
bcup | 2:5311ad7c83e6 | 27 | void SPI_SlaveWrite() |
bcup | 0:24e90e3ca3f4 | 28 | { |
bcup | 2:5311ad7c83e6 | 29 | char tx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 30 | char reply; |
bcup | 2:5311ad7c83e6 | 31 | char i; |
bcup | 1:3a338e553a54 | 32 | char temp; |
bcup | 2:5311ad7c83e6 | 33 | char tx_cnt = 0; |
bcup | 2:5311ad7c83e6 | 34 | int value; |
bcup | 2:5311ad7c83e6 | 35 | PRINTD("Input Strging="); |
bcup | 0:24e90e3ca3f4 | 36 | while(1) |
bcup | 0:24e90e3ca3f4 | 37 | { |
bcup | 0:24e90e3ca3f4 | 38 | |
bcup | 0:24e90e3ca3f4 | 39 | temp=getchar(); |
bcup | 2:5311ad7c83e6 | 40 | tx_buffer[tx_cnt++]=temp; |
bcup | 0:24e90e3ca3f4 | 41 | if(temp==0x0d) |
bcup | 0:24e90e3ca3f4 | 42 | { |
bcup | 0:24e90e3ca3f4 | 43 | tx_buffer[tx_cnt]=0; |
bcup | 0:24e90e3ca3f4 | 44 | PRINTD("\nData send Finish...\n"); |
bcup | 0:24e90e3ca3f4 | 45 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 46 | { |
bcup | 0:24e90e3ca3f4 | 47 | PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 48 | } |
bcup | 0:24e90e3ca3f4 | 49 | PRINTD("\n\n"); |
bcup | 0:24e90e3ca3f4 | 50 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 51 | { |
bcup | 0:24e90e3ca3f4 | 52 | value=tx_buffer[i]; |
bcup | 2:5311ad7c83e6 | 53 | PRINTD("write[%d]=%c[%02x]\n",i,value,value); |
gandol2 | 3:4bb95aa46ebc | 54 | spi_slave.reply(value); |
bcup | 0:24e90e3ca3f4 | 55 | } |
bcup | 2:5311ad7c83e6 | 56 | for(i=0;i<tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 57 | { |
bcup | 0:24e90e3ca3f4 | 58 | tx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 59 | PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 60 | } |
bcup | 2:5311ad7c83e6 | 61 | tx_cnt=0; |
bcup | 2:5311ad7c83e6 | 62 | PRINTD("break\n"); |
bcup | 2:5311ad7c83e6 | 63 | break; |
bcup | 0:24e90e3ca3f4 | 64 | } |
bcup | 0:24e90e3ca3f4 | 65 | else |
bcup | 0:24e90e3ca3f4 | 66 | { |
bcup | 0:24e90e3ca3f4 | 67 | PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); |
bcup | 0:24e90e3ca3f4 | 68 | } |
bcup | 0:24e90e3ca3f4 | 69 | } |
bcup | 2:5311ad7c83e6 | 70 | return; |
bcup | 0:24e90e3ca3f4 | 71 | } |
bcup | 0:24e90e3ca3f4 | 72 | |
bcup | 0:24e90e3ca3f4 | 73 | int main() |
bcup | 0:24e90e3ca3f4 | 74 | { |
gandol2 | 3:4bb95aa46ebc | 75 | char serialTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 76 | int spiRxTempBuf = 0; |
gandol2 | 3:4bb95aa46ebc | 77 | char spiRxBuf[255]; |
gandol2 | 3:4bb95aa46ebc | 78 | int spiRxTempCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 79 | int spiRxLen = 0; |
gandol2 | 3:4bb95aa46ebc | 80 | |
gandol2 | 3:4bb95aa46ebc | 81 | |
gandol2 | 3:4bb95aa46ebc | 82 | int spiTxCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 83 | char spiTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 84 | |
gandol2 | 3:4bb95aa46ebc | 85 | char serialRxBuf[255]; |
gandol2 | 3:4bb95aa46ebc | 86 | int serialRxLen = 0; |
gandol2 | 3:4bb95aa46ebc | 87 | |
gandol2 | 3:4bb95aa46ebc | 88 | SPI_SlaveInit(); |
gandol2 | 3:4bb95aa46ebc | 89 | spi_slave.reply(0x00); // Prime SPI with first reply |
gandol2 | 3:4bb95aa46ebc | 90 | |
gandol2 | 3:4bb95aa46ebc | 91 | |
gandol2 | 3:4bb95aa46ebc | 92 | pc_serial.printf("\n\n========== KSS SPI Slave [Start] ==========\n"); |
gandol2 | 3:4bb95aa46ebc | 93 | |
gandol2 | 3:4bb95aa46ebc | 94 | while(1) |
gandol2 | 3:4bb95aa46ebc | 95 | { |
gandol2 | 3:4bb95aa46ebc | 96 | if(spi_slave.receive()) // wait SPI data input... |
gandol2 | 3:4bb95aa46ebc | 97 | { |
gandol2 | 3:4bb95aa46ebc | 98 | spiRxTempBuf = spi_slave.read(); |
gandol2 | 3:4bb95aa46ebc | 99 | spiRxBuf[spiRxTempCnt++] = spiRxTempBuf; |
gandol2 | 3:4bb95aa46ebc | 100 | |
gandol2 | 3:4bb95aa46ebc | 101 | if(0x00 == spiRxTempBuf) |
gandol2 | 3:4bb95aa46ebc | 102 | { |
gandol2 | 3:4bb95aa46ebc | 103 | serialTxReadyFlag = 1; |
gandol2 | 3:4bb95aa46ebc | 104 | //pc_serial.printf("SPI Recive Byte [%d]\n", spiRxTempCnt-1); |
gandol2 | 3:4bb95aa46ebc | 105 | spiRxLen = strlen(spiRxBuf); |
gandol2 | 3:4bb95aa46ebc | 106 | spiRxTempCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 107 | } |
gandol2 | 3:4bb95aa46ebc | 108 | } |
gandol2 | 3:4bb95aa46ebc | 109 | if(1 == serialTxReadyFlag) |
gandol2 | 3:4bb95aa46ebc | 110 | { |
gandol2 | 3:4bb95aa46ebc | 111 | serialTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 112 | pc_serial.printf("len=[%d] %s\n", spiRxLen, spiRxBuf); |
gandol2 | 3:4bb95aa46ebc | 113 | } |
gandol2 | 3:4bb95aa46ebc | 114 | |
gandol2 | 3:4bb95aa46ebc | 115 | |
gandol2 | 3:4bb95aa46ebc | 116 | |
gandol2 | 3:4bb95aa46ebc | 117 | |
bcup | 4:cc97d7a33efc | 118 | /* TODO "serial -> slave ----(SPI)-----> " */ |
gandol2 | 3:4bb95aa46ebc | 119 | |
gandol2 | 3:4bb95aa46ebc | 120 | if(0 != pc_serial.readable()) // wait serial input.. |
gandol2 | 3:4bb95aa46ebc | 121 | { |
gandol2 | 3:4bb95aa46ebc | 122 | pc_serial.scanf("%s", serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 123 | serialRxLen = strlen(serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 124 | pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 125 | spiTxReadyFlag = 1; |
gandol2 | 3:4bb95aa46ebc | 126 | } |
gandol2 | 3:4bb95aa46ebc | 127 | if(1 == spiTxReadyFlag) |
gandol2 | 3:4bb95aa46ebc | 128 | { |
bcup | 4:cc97d7a33efc | 129 | PRINTD("spiTxReadyFlag=%d\n",spiTxReadyFlag); |
gandol2 | 3:4bb95aa46ebc | 130 | // SPI Send Start |
gandol2 | 3:4bb95aa46ebc | 131 | |
bcup | 4:cc97d7a33efc | 132 | #if 0 |
gandol2 | 3:4bb95aa46ebc | 133 | for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) |
gandol2 | 3:4bb95aa46ebc | 134 | { |
gandol2 | 3:4bb95aa46ebc | 135 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
gandol2 | 3:4bb95aa46ebc | 136 | spi_slave.reply(serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 137 | } |
bcup | 4:cc97d7a33efc | 138 | #endif |
bcup | 4:cc97d7a33efc | 139 | for(spiTxCnt = 0 ; spiTxCnt < 1 ; ++spiTxCnt) |
bcup | 4:cc97d7a33efc | 140 | { |
bcup | 4:cc97d7a33efc | 141 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 142 | |
bcup | 4:cc97d7a33efc | 143 | spi_slave.reply(serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 144 | PRINTD("reply=%c\n",serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 145 | } |
gandol2 | 3:4bb95aa46ebc | 146 | |
gandol2 | 3:4bb95aa46ebc | 147 | |
gandol2 | 3:4bb95aa46ebc | 148 | spiTxReadyFlag = 0; |
bcup | 4:cc97d7a33efc | 149 | PRINTD("spiTxReadyFlag =0\n"); |
gandol2 | 3:4bb95aa46ebc | 150 | } |
gandol2 | 3:4bb95aa46ebc | 151 | |
bcup | 4:cc97d7a33efc | 152 | |
gandol2 | 3:4bb95aa46ebc | 153 | |
gandol2 | 3:4bb95aa46ebc | 154 | |
gandol2 | 3:4bb95aa46ebc | 155 | |
gandol2 | 3:4bb95aa46ebc | 156 | |
gandol2 | 3:4bb95aa46ebc | 157 | |
gandol2 | 3:4bb95aa46ebc | 158 | |
gandol2 | 3:4bb95aa46ebc | 159 | |
gandol2 | 3:4bb95aa46ebc | 160 | } |
gandol2 | 3:4bb95aa46ebc | 161 | |
gandol2 | 3:4bb95aa46ebc | 162 | |
gandol2 | 3:4bb95aa46ebc | 163 | |
gandol2 | 3:4bb95aa46ebc | 164 | |
gandol2 | 3:4bb95aa46ebc | 165 | |
gandol2 | 3:4bb95aa46ebc | 166 | |
gandol2 | 3:4bb95aa46ebc | 167 | |
gandol2 | 3:4bb95aa46ebc | 168 | |
gandol2 | 3:4bb95aa46ebc | 169 | |
gandol2 | 3:4bb95aa46ebc | 170 | #if 0 // 161005_BDK_slave backup start |
bcup | 2:5311ad7c83e6 | 171 | int i; |
bcup | 2:5311ad7c83e6 | 172 | char valueFromMaster; |
bcup | 2:5311ad7c83e6 | 173 | char rx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 174 | char rx_cnt = -1; |
bcup | 2:5311ad7c83e6 | 175 | PRINTD("\n=========SLAVE=========\n"); |
bcup | 2:5311ad7c83e6 | 176 | SPI_SlaveInit(); |
bcup | 2:5311ad7c83e6 | 177 | |
bcup | 0:24e90e3ca3f4 | 178 | while(1) |
bcup | 0:24e90e3ca3f4 | 179 | { |
bcup | 2:5311ad7c83e6 | 180 | |
gandol2 | 3:4bb95aa46ebc | 181 | if(spi_slave.receive()) |
bcup | 2:5311ad7c83e6 | 182 | { |
bcup | 2:5311ad7c83e6 | 183 | PRINTD("----1\n"); |
bcup | 2:5311ad7c83e6 | 184 | if(pc.readable()) |
bcup | 2:5311ad7c83e6 | 185 | { |
bcup | 2:5311ad7c83e6 | 186 | SPI_SlaveWrite(); |
bcup | 2:5311ad7c83e6 | 187 | } |
bcup | 2:5311ad7c83e6 | 188 | PRINTD("----2\n"); |
gandol2 | 3:4bb95aa46ebc | 189 | valueFromMaster = spi_slave.read(); |
bcup | 2:5311ad7c83e6 | 190 | PRINTD("----3\n"); |
bcup | 2:5311ad7c83e6 | 191 | //PRINTD("valueFromMaster="); |
bcup | 2:5311ad7c83e6 | 192 | //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); |
bcup | 2:5311ad7c83e6 | 193 | rx_buffer[++rx_cnt]=valueFromMaster; |
bcup | 2:5311ad7c83e6 | 194 | if(valueFromMaster==0) |
bcup | 2:5311ad7c83e6 | 195 | { |
bcup | 2:5311ad7c83e6 | 196 | PRINTD("rx_string="); |
bcup | 2:5311ad7c83e6 | 197 | for(i=0;i<rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 198 | { |
bcup | 2:5311ad7c83e6 | 199 | PRINTD("%c",rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 200 | } |
bcup | 2:5311ad7c83e6 | 201 | PRINTD("\n"); |
bcup | 2:5311ad7c83e6 | 202 | for(i=0;i<=rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 203 | { |
bcup | 2:5311ad7c83e6 | 204 | rx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 205 | PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 206 | } |
bcup | 2:5311ad7c83e6 | 207 | rx_cnt=-1; |
bcup | 2:5311ad7c83e6 | 208 | |
bcup | 2:5311ad7c83e6 | 209 | }//valueFromMaster if |
bcup | 2:5311ad7c83e6 | 210 | |
gandol2 | 3:4bb95aa46ebc | 211 | }// spi_slave.receive() if |
bcup | 2:5311ad7c83e6 | 212 | /* |
bcup | 2:5311ad7c83e6 | 213 | |
bcup | 2:5311ad7c83e6 | 214 | */ |
bcup | 2:5311ad7c83e6 | 215 | }//end of while |
gandol2 | 3:4bb95aa46ebc | 216 | #endif // 161005_BDK_slave backup end |
bcup | 2:5311ad7c83e6 | 217 | }//end of main |