161007_BDK_EEPROM(M)
Dependencies: mbed
Fork of 161006_BDK_SPIslave3 by
main.cpp@3:4bb95aa46ebc, 2016-10-05 (annotated)
- Committer:
- gandol2
- Date:
- Wed Oct 05 08:59:53 2016 +0000
- Revision:
- 3:4bb95aa46ebc
- Parent:
- 2:5311ad7c83e6
- Child:
- 4:cc97d7a33efc
161005_F0426K_SPISlave_KSS
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 | |
gandol2 | 3:4bb95aa46ebc | 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 | { |
gandol2 | 3:4bb95aa46ebc | 129 | // SPI Send Start |
gandol2 | 3:4bb95aa46ebc | 130 | |
gandol2 | 3:4bb95aa46ebc | 131 | |
gandol2 | 3:4bb95aa46ebc | 132 | for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) |
gandol2 | 3:4bb95aa46ebc | 133 | { |
gandol2 | 3:4bb95aa46ebc | 134 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
gandol2 | 3:4bb95aa46ebc | 135 | spi_slave.reply(serialRxBuf[spiTxCnt]); |
gandol2 | 3:4bb95aa46ebc | 136 | } |
gandol2 | 3:4bb95aa46ebc | 137 | |
gandol2 | 3:4bb95aa46ebc | 138 | |
gandol2 | 3:4bb95aa46ebc | 139 | spiTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 140 | } |
gandol2 | 3:4bb95aa46ebc | 141 | |
gandol2 | 3:4bb95aa46ebc | 142 | */ |
gandol2 | 3:4bb95aa46ebc | 143 | |
gandol2 | 3:4bb95aa46ebc | 144 | |
gandol2 | 3:4bb95aa46ebc | 145 | |
gandol2 | 3:4bb95aa46ebc | 146 | |
gandol2 | 3:4bb95aa46ebc | 147 | |
gandol2 | 3:4bb95aa46ebc | 148 | |
gandol2 | 3:4bb95aa46ebc | 149 | |
gandol2 | 3:4bb95aa46ebc | 150 | |
gandol2 | 3:4bb95aa46ebc | 151 | } |
gandol2 | 3:4bb95aa46ebc | 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 | #if 0 // 161005_BDK_slave backup start |
bcup | 2:5311ad7c83e6 | 162 | int i; |
bcup | 2:5311ad7c83e6 | 163 | char valueFromMaster; |
bcup | 2:5311ad7c83e6 | 164 | char rx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 165 | char rx_cnt = -1; |
bcup | 2:5311ad7c83e6 | 166 | PRINTD("\n=========SLAVE=========\n"); |
bcup | 2:5311ad7c83e6 | 167 | SPI_SlaveInit(); |
bcup | 2:5311ad7c83e6 | 168 | |
bcup | 0:24e90e3ca3f4 | 169 | while(1) |
bcup | 0:24e90e3ca3f4 | 170 | { |
bcup | 2:5311ad7c83e6 | 171 | |
gandol2 | 3:4bb95aa46ebc | 172 | if(spi_slave.receive()) |
bcup | 2:5311ad7c83e6 | 173 | { |
bcup | 2:5311ad7c83e6 | 174 | PRINTD("----1\n"); |
bcup | 2:5311ad7c83e6 | 175 | if(pc.readable()) |
bcup | 2:5311ad7c83e6 | 176 | { |
bcup | 2:5311ad7c83e6 | 177 | SPI_SlaveWrite(); |
bcup | 2:5311ad7c83e6 | 178 | } |
bcup | 2:5311ad7c83e6 | 179 | PRINTD("----2\n"); |
gandol2 | 3:4bb95aa46ebc | 180 | valueFromMaster = spi_slave.read(); |
bcup | 2:5311ad7c83e6 | 181 | PRINTD("----3\n"); |
bcup | 2:5311ad7c83e6 | 182 | //PRINTD("valueFromMaster="); |
bcup | 2:5311ad7c83e6 | 183 | //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); |
bcup | 2:5311ad7c83e6 | 184 | rx_buffer[++rx_cnt]=valueFromMaster; |
bcup | 2:5311ad7c83e6 | 185 | if(valueFromMaster==0) |
bcup | 2:5311ad7c83e6 | 186 | { |
bcup | 2:5311ad7c83e6 | 187 | PRINTD("rx_string="); |
bcup | 2:5311ad7c83e6 | 188 | for(i=0;i<rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 189 | { |
bcup | 2:5311ad7c83e6 | 190 | PRINTD("%c",rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 191 | } |
bcup | 2:5311ad7c83e6 | 192 | PRINTD("\n"); |
bcup | 2:5311ad7c83e6 | 193 | for(i=0;i<=rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 194 | { |
bcup | 2:5311ad7c83e6 | 195 | rx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 196 | PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 197 | } |
bcup | 2:5311ad7c83e6 | 198 | rx_cnt=-1; |
bcup | 2:5311ad7c83e6 | 199 | |
bcup | 2:5311ad7c83e6 | 200 | }//valueFromMaster if |
bcup | 2:5311ad7c83e6 | 201 | |
gandol2 | 3:4bb95aa46ebc | 202 | }// spi_slave.receive() if |
bcup | 2:5311ad7c83e6 | 203 | /* |
bcup | 2:5311ad7c83e6 | 204 | |
bcup | 2:5311ad7c83e6 | 205 | */ |
bcup | 2:5311ad7c83e6 | 206 | }//end of while |
gandol2 | 3:4bb95aa46ebc | 207 | #endif // 161005_BDK_slave backup end |
bcup | 2:5311ad7c83e6 | 208 | }//end of main |