161007_BDK_EEPROM(M)
Dependencies: mbed
Fork of 161006_BDK_SPIslave3 by
main.cpp@6:21b3128fbaef, 2016-10-06 (annotated)
- Committer:
- gandol2
- Date:
- Thu Oct 06 08:27:32 2016 +0000
- Branch:
- KSS
- Revision:
- 6:21b3128fbaef
- Parent:
- 5:1ccdbfe5e4bb
- Child:
- 7:45b16ef560a9
161006_implement spi eeprom
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 | 6:21b3128fbaef | 15 | #define EEPROM_WREN 0x06 // Write Enable |
gandol2 | 6:21b3128fbaef | 16 | #define EEPROM_WRDI 0x04 // Write Disable |
gandol2 | 6:21b3128fbaef | 17 | #define EEPROM_RDSR 0x05 // Read Status Register |
gandol2 | 6:21b3128fbaef | 18 | #define EEPROM_WRSR 0x01 // Write Status Register |
gandol2 | 6:21b3128fbaef | 19 | #define EEPROM_READ 0x03 // Read EEPROM Memory |
gandol2 | 6:21b3128fbaef | 20 | #define EEPROM_WRITE 0x02 // Write EEPROM Memory |
gandol2 | 3:4bb95aa46ebc | 21 | |
gandol2 | 3:4bb95aa46ebc | 22 | SPISlave spi_slave(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_SSEL); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC |
gandol2 | 3:4bb95aa46ebc | 23 | Serial pc_serial(USBTX, USBRX); |
bcup | 0:24e90e3ca3f4 | 24 | |
bcup | 2:5311ad7c83e6 | 25 | void SPI_SlaveInit() |
bcup | 0:24e90e3ca3f4 | 26 | { |
bcup | 2:5311ad7c83e6 | 27 | PRINTD("Set the SPI SLAVE format\n"); |
gandol2 | 3:4bb95aa46ebc | 28 | spi_slave.format(8,0); // setup 8bit 0 mode |
bcup | 2:5311ad7c83e6 | 29 | PRINTD("Set the SPI SLAVE frequency\n"); |
gandol2 | 6:21b3128fbaef | 30 | spi_slave.frequency(1000000); // default 1MHz |
bcup | 0:24e90e3ca3f4 | 31 | } |
bcup | 0:24e90e3ca3f4 | 32 | |
bcup | 2:5311ad7c83e6 | 33 | void SPI_SlaveWrite() |
bcup | 0:24e90e3ca3f4 | 34 | { |
bcup | 2:5311ad7c83e6 | 35 | char tx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 36 | char reply; |
bcup | 2:5311ad7c83e6 | 37 | char i; |
bcup | 1:3a338e553a54 | 38 | char temp; |
bcup | 2:5311ad7c83e6 | 39 | char tx_cnt = 0; |
bcup | 2:5311ad7c83e6 | 40 | int value; |
bcup | 2:5311ad7c83e6 | 41 | PRINTD("Input Strging="); |
bcup | 0:24e90e3ca3f4 | 42 | while(1) |
bcup | 0:24e90e3ca3f4 | 43 | { |
bcup | 0:24e90e3ca3f4 | 44 | |
bcup | 0:24e90e3ca3f4 | 45 | temp=getchar(); |
bcup | 2:5311ad7c83e6 | 46 | tx_buffer[tx_cnt++]=temp; |
bcup | 0:24e90e3ca3f4 | 47 | if(temp==0x0d) |
bcup | 0:24e90e3ca3f4 | 48 | { |
bcup | 0:24e90e3ca3f4 | 49 | tx_buffer[tx_cnt]=0; |
bcup | 0:24e90e3ca3f4 | 50 | PRINTD("\nData send Finish...\n"); |
bcup | 0:24e90e3ca3f4 | 51 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 52 | { |
bcup | 0:24e90e3ca3f4 | 53 | PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 54 | } |
bcup | 0:24e90e3ca3f4 | 55 | PRINTD("\n\n"); |
bcup | 0:24e90e3ca3f4 | 56 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 57 | { |
bcup | 0:24e90e3ca3f4 | 58 | value=tx_buffer[i]; |
bcup | 2:5311ad7c83e6 | 59 | PRINTD("write[%d]=%c[%02x]\n",i,value,value); |
gandol2 | 3:4bb95aa46ebc | 60 | spi_slave.reply(value); |
bcup | 0:24e90e3ca3f4 | 61 | } |
bcup | 2:5311ad7c83e6 | 62 | for(i=0;i<tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 63 | { |
bcup | 0:24e90e3ca3f4 | 64 | tx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 65 | PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 66 | } |
bcup | 2:5311ad7c83e6 | 67 | tx_cnt=0; |
bcup | 2:5311ad7c83e6 | 68 | PRINTD("break\n"); |
bcup | 2:5311ad7c83e6 | 69 | break; |
bcup | 0:24e90e3ca3f4 | 70 | } |
bcup | 0:24e90e3ca3f4 | 71 | else |
bcup | 0:24e90e3ca3f4 | 72 | { |
bcup | 0:24e90e3ca3f4 | 73 | PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); |
bcup | 0:24e90e3ca3f4 | 74 | } |
bcup | 0:24e90e3ca3f4 | 75 | } |
bcup | 2:5311ad7c83e6 | 76 | return; |
bcup | 0:24e90e3ca3f4 | 77 | } |
bcup | 0:24e90e3ca3f4 | 78 | |
gandol2 | 6:21b3128fbaef | 79 | void spi_dummy_reply(void) |
gandol2 | 6:21b3128fbaef | 80 | { |
gandol2 | 6:21b3128fbaef | 81 | spi_slave.reply( 0xA0 ); |
gandol2 | 6:21b3128fbaef | 82 | } |
gandol2 | 6:21b3128fbaef | 83 | |
gandol2 | 6:21b3128fbaef | 84 | #define EEPROM_SIZE 256 |
gandol2 | 6:21b3128fbaef | 85 | char statusReg; |
gandol2 | 6:21b3128fbaef | 86 | char eepBuf[EEPROM_SIZE]; |
gandol2 | 6:21b3128fbaef | 87 | |
gandol2 | 6:21b3128fbaef | 88 | void printBuf(char * buf, int length) |
gandol2 | 6:21b3128fbaef | 89 | { |
gandol2 | 6:21b3128fbaef | 90 | pc_serial.printf("=======================[EEPROM]========================\n"); |
gandol2 | 6:21b3128fbaef | 91 | pc_serial.printf(" |"); |
gandol2 | 6:21b3128fbaef | 92 | for(int iCnt = 0 ; iCnt < 16 ; ++iCnt) |
gandol2 | 6:21b3128fbaef | 93 | { |
gandol2 | 6:21b3128fbaef | 94 | pc_serial.printf("%02X|", iCnt); |
gandol2 | 6:21b3128fbaef | 95 | } |
gandol2 | 6:21b3128fbaef | 96 | pc_serial.printf("\n"); |
gandol2 | 6:21b3128fbaef | 97 | |
gandol2 | 6:21b3128fbaef | 98 | |
gandol2 | 6:21b3128fbaef | 99 | for(int iCnt = 0 ; iCnt < length ; ++iCnt) |
gandol2 | 6:21b3128fbaef | 100 | { |
gandol2 | 6:21b3128fbaef | 101 | if(0 == ((iCnt) % 16)) |
gandol2 | 6:21b3128fbaef | 102 | { |
gandol2 | 6:21b3128fbaef | 103 | pc_serial.printf("[0x%02X] ", iCnt); |
gandol2 | 6:21b3128fbaef | 104 | } |
gandol2 | 6:21b3128fbaef | 105 | |
gandol2 | 6:21b3128fbaef | 106 | pc_serial.printf("%02X ", *(buf + iCnt)); |
gandol2 | 6:21b3128fbaef | 107 | |
gandol2 | 6:21b3128fbaef | 108 | if(0 == ((iCnt+1) % 16)) |
gandol2 | 6:21b3128fbaef | 109 | { |
gandol2 | 6:21b3128fbaef | 110 | pc_serial.printf("\n"); |
gandol2 | 6:21b3128fbaef | 111 | |
gandol2 | 6:21b3128fbaef | 112 | } |
gandol2 | 6:21b3128fbaef | 113 | |
gandol2 | 6:21b3128fbaef | 114 | } |
gandol2 | 6:21b3128fbaef | 115 | pc_serial.printf("=======================================================\n"); |
gandol2 | 6:21b3128fbaef | 116 | } |
gandol2 | 6:21b3128fbaef | 117 | |
bcup | 0:24e90e3ca3f4 | 118 | int main() |
bcup | 0:24e90e3ca3f4 | 119 | { |
gandol2 | 6:21b3128fbaef | 120 | |
gandol2 | 6:21b3128fbaef | 121 | |
gandol2 | 6:21b3128fbaef | 122 | char eepAddr = 0; |
gandol2 | 6:21b3128fbaef | 123 | char eepData = 0; |
gandol2 | 6:21b3128fbaef | 124 | |
gandol2 | 3:4bb95aa46ebc | 125 | char serialTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 126 | int spiRxTempBuf = 0; |
gandol2 | 3:4bb95aa46ebc | 127 | char spiRxBuf[255]; |
gandol2 | 3:4bb95aa46ebc | 128 | int spiRxTempCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 129 | int spiRxLen = 0; |
gandol2 | 3:4bb95aa46ebc | 130 | |
gandol2 | 3:4bb95aa46ebc | 131 | |
gandol2 | 3:4bb95aa46ebc | 132 | int spiTxCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 133 | char spiTxReadyFlag = 0; |
gandol2 | 3:4bb95aa46ebc | 134 | |
gandol2 | 3:4bb95aa46ebc | 135 | char serialRxBuf[255]; |
gandol2 | 3:4bb95aa46ebc | 136 | int serialRxLen = 0; |
gandol2 | 3:4bb95aa46ebc | 137 | |
gandol2 | 3:4bb95aa46ebc | 138 | SPI_SlaveInit(); |
gandol2 | 6:21b3128fbaef | 139 | |
gandol2 | 6:21b3128fbaef | 140 | // test code start |
gandol2 | 6:21b3128fbaef | 141 | NVIC_SetVector( SPI_IRQn , ( uint32_t ) executa_spi_slave_hw ) ; |
gandol2 | 6:21b3128fbaef | 142 | NVIC_SetPriority( SPI_IRQn , 2 ) ; |
gandol2 | 6:21b3128fbaef | 143 | NVIC_EnableIRQ( SPI_IRQn ) ; |
gandol2 | 6:21b3128fbaef | 144 | // test code end |
gandol2 | 3:4bb95aa46ebc | 145 | |
gandol2 | 3:4bb95aa46ebc | 146 | |
gandol2 | 5:1ccdbfe5e4bb | 147 | pc_serial.printf("\n\n========== KSS SPI Slave2 [Start] ==========\n"); |
gandol2 | 3:4bb95aa46ebc | 148 | |
gandol2 | 3:4bb95aa46ebc | 149 | while(1) |
gandol2 | 3:4bb95aa46ebc | 150 | { |
gandol2 | 3:4bb95aa46ebc | 151 | if(spi_slave.receive()) // wait SPI data input... |
gandol2 | 3:4bb95aa46ebc | 152 | { |
gandol2 | 3:4bb95aa46ebc | 153 | spiRxTempBuf = spi_slave.read(); |
gandol2 | 6:21b3128fbaef | 154 | //spi_slave.reply( statusReg++ ); |
gandol2 | 6:21b3128fbaef | 155 | //continue; |
gandol2 | 6:21b3128fbaef | 156 | |
gandol2 | 6:21b3128fbaef | 157 | switch(spiRxTempBuf) |
gandol2 | 6:21b3128fbaef | 158 | { |
gandol2 | 6:21b3128fbaef | 159 | case EEPROM_WREN: |
gandol2 | 6:21b3128fbaef | 160 | //spi_dummy_reply(); |
gandol2 | 6:21b3128fbaef | 161 | pc_serial.printf("[INFO] SPI_MOSI(RX) : WREN\n"); |
gandol2 | 6:21b3128fbaef | 162 | statusReg = statusReg | (1<<1) ; |
gandol2 | 6:21b3128fbaef | 163 | pc_serial.printf("[INFO] WREN sucess [0x%02X]\n", statusReg); |
gandol2 | 6:21b3128fbaef | 164 | break; |
gandol2 | 6:21b3128fbaef | 165 | |
gandol2 | 6:21b3128fbaef | 166 | case EEPROM_WRDI: |
gandol2 | 6:21b3128fbaef | 167 | //spi_dummy_reply(); |
gandol2 | 6:21b3128fbaef | 168 | pc_serial.printf("[INFO] SPI_MOSI(RX) : WRDI\n"); |
gandol2 | 6:21b3128fbaef | 169 | statusReg = statusReg & (~(1 << 1)) ; |
gandol2 | 6:21b3128fbaef | 170 | pc_serial.printf("[INFO] WRDI sucess [0x%02X]\n", statusReg); |
gandol2 | 6:21b3128fbaef | 171 | break; |
gandol2 | 6:21b3128fbaef | 172 | |
gandol2 | 6:21b3128fbaef | 173 | case EEPROM_RDSR: |
gandol2 | 6:21b3128fbaef | 174 | spi_slave.reply( statusReg ); |
gandol2 | 6:21b3128fbaef | 175 | pc_serial.printf("[INFO] SPI_MOSI(RX) : RDSR\n"); |
gandol2 | 6:21b3128fbaef | 176 | pc_serial.printf("[INFO] SPI_MISO(TX) : RDSR[0x%02X] \n", statusReg); |
gandol2 | 6:21b3128fbaef | 177 | break; |
gandol2 | 6:21b3128fbaef | 178 | |
gandol2 | 6:21b3128fbaef | 179 | |
gandol2 | 6:21b3128fbaef | 180 | case EEPROM_WRITE: |
gandol2 | 6:21b3128fbaef | 181 | // EEPROM Address read.. |
gandol2 | 6:21b3128fbaef | 182 | while(!spi_slave.receive()); |
gandol2 | 6:21b3128fbaef | 183 | eepAddr = spi_slave.read(); |
gandol2 | 6:21b3128fbaef | 184 | |
gandol2 | 6:21b3128fbaef | 185 | // EEPROM Data read.. |
gandol2 | 6:21b3128fbaef | 186 | while(!spi_slave.receive()); |
gandol2 | 6:21b3128fbaef | 187 | eepData = spi_slave.read(); |
gandol2 | 6:21b3128fbaef | 188 | |
gandol2 | 6:21b3128fbaef | 189 | pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData); |
gandol2 | 6:21b3128fbaef | 190 | |
gandol2 | 6:21b3128fbaef | 191 | if(statusReg & 0x02) |
gandol2 | 6:21b3128fbaef | 192 | { |
gandol2 | 6:21b3128fbaef | 193 | statusReg |= 0x01; |
gandol2 | 6:21b3128fbaef | 194 | eepBuf[eepAddr] = eepData; |
gandol2 | 6:21b3128fbaef | 195 | printBuf(eepBuf, EEPROM_SIZE); |
gandol2 | 6:21b3128fbaef | 196 | statusReg &= (~0x01); |
gandol2 | 6:21b3128fbaef | 197 | |
gandol2 | 6:21b3128fbaef | 198 | } |
gandol2 | 6:21b3128fbaef | 199 | else |
gandol2 | 6:21b3128fbaef | 200 | { |
gandol2 | 6:21b3128fbaef | 201 | pc_serial.printf("\nwrite command is disabled\n"); |
gandol2 | 6:21b3128fbaef | 202 | } |
gandol2 | 6:21b3128fbaef | 203 | break; |
gandol2 | 6:21b3128fbaef | 204 | |
gandol2 | 6:21b3128fbaef | 205 | |
gandol2 | 6:21b3128fbaef | 206 | case EEPROM_READ: |
gandol2 | 6:21b3128fbaef | 207 | // EEPROM Address read.. |
gandol2 | 6:21b3128fbaef | 208 | while(!spi_slave.receive()); |
gandol2 | 6:21b3128fbaef | 209 | eepAddr = spi_slave.read(); |
gandol2 | 6:21b3128fbaef | 210 | |
gandol2 | 6:21b3128fbaef | 211 | spi_slave.reply( eepBuf[eepAddr] ); |
gandol2 | 6:21b3128fbaef | 212 | |
gandol2 | 6:21b3128fbaef | 213 | pc_serial.printf("\n[DEBUG] Addr[0x%02X]\n", eepAddr); |
gandol2 | 6:21b3128fbaef | 214 | |
gandol2 | 6:21b3128fbaef | 215 | |
gandol2 | 6:21b3128fbaef | 216 | |
gandol2 | 6:21b3128fbaef | 217 | |
gandol2 | 6:21b3128fbaef | 218 | break; |
gandol2 | 6:21b3128fbaef | 219 | |
gandol2 | 6:21b3128fbaef | 220 | |
gandol2 | 6:21b3128fbaef | 221 | default: |
gandol2 | 6:21b3128fbaef | 222 | //spi_dummy_reply(); |
gandol2 | 6:21b3128fbaef | 223 | } |
gandol2 | 6:21b3128fbaef | 224 | |
gandol2 | 6:21b3128fbaef | 225 | //pc_serial.printf("------------------------ end SPI Communication\n"); |
gandol2 | 6:21b3128fbaef | 226 | |
gandol2 | 6:21b3128fbaef | 227 | } |
gandol2 | 6:21b3128fbaef | 228 | } |
gandol2 | 6:21b3128fbaef | 229 | |
gandol2 | 6:21b3128fbaef | 230 | |
gandol2 | 6:21b3128fbaef | 231 | |
gandol2 | 6:21b3128fbaef | 232 | |
gandol2 | 6:21b3128fbaef | 233 | |
gandol2 | 6:21b3128fbaef | 234 | |
gandol2 | 6:21b3128fbaef | 235 | |
gandol2 | 6:21b3128fbaef | 236 | |
gandol2 | 6:21b3128fbaef | 237 | |
gandol2 | 6:21b3128fbaef | 238 | |
gandol2 | 6:21b3128fbaef | 239 | /* |
gandol2 | 3:4bb95aa46ebc | 240 | spiRxBuf[spiRxTempCnt++] = spiRxTempBuf; |
gandol2 | 3:4bb95aa46ebc | 241 | |
gandol2 | 3:4bb95aa46ebc | 242 | if(0x00 == spiRxTempBuf) |
gandol2 | 3:4bb95aa46ebc | 243 | { |
gandol2 | 3:4bb95aa46ebc | 244 | serialTxReadyFlag = 1; |
gandol2 | 3:4bb95aa46ebc | 245 | //pc_serial.printf("SPI Recive Byte [%d]\n", spiRxTempCnt-1); |
gandol2 | 3:4bb95aa46ebc | 246 | spiRxLen = strlen(spiRxBuf); |
gandol2 | 3:4bb95aa46ebc | 247 | spiRxTempCnt = 0; |
gandol2 | 3:4bb95aa46ebc | 248 | } |
gandol2 | 6:21b3128fbaef | 249 | |
gandol2 | 3:4bb95aa46ebc | 250 | } |
gandol2 | 3:4bb95aa46ebc | 251 | if(1 == serialTxReadyFlag) |
gandol2 | 3:4bb95aa46ebc | 252 | { |
gandol2 | 3:4bb95aa46ebc | 253 | serialTxReadyFlag = 0; |
gandol2 | 6:21b3128fbaef | 254 | pc_serial.printf("SPI_RX Data : len=[%d] %s\n", spiRxLen, spiRxBuf); |
gandol2 | 3:4bb95aa46ebc | 255 | } |
gandol2 | 3:4bb95aa46ebc | 256 | |
gandol2 | 6:21b3128fbaef | 257 | */ |
gandol2 | 6:21b3128fbaef | 258 | |
gandol2 | 3:4bb95aa46ebc | 259 | |
gandol2 | 3:4bb95aa46ebc | 260 | |
gandol2 | 6:21b3128fbaef | 261 | #if 0 |
bcup | 4:cc97d7a33efc | 262 | /* TODO "serial -> slave ----(SPI)-----> " */ |
gandol2 | 3:4bb95aa46ebc | 263 | |
gandol2 | 3:4bb95aa46ebc | 264 | if(0 != pc_serial.readable()) // wait serial input.. |
gandol2 | 3:4bb95aa46ebc | 265 | { |
gandol2 | 3:4bb95aa46ebc | 266 | pc_serial.scanf("%s", serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 267 | serialRxLen = strlen(serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 268 | pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); |
gandol2 | 3:4bb95aa46ebc | 269 | spiTxReadyFlag = 1; |
gandol2 | 3:4bb95aa46ebc | 270 | } |
gandol2 | 3:4bb95aa46ebc | 271 | if(1 == spiTxReadyFlag) |
gandol2 | 3:4bb95aa46ebc | 272 | { |
bcup | 4:cc97d7a33efc | 273 | PRINTD("spiTxReadyFlag=%d\n",spiTxReadyFlag); |
gandol2 | 3:4bb95aa46ebc | 274 | // SPI Send Start |
gandol2 | 3:4bb95aa46ebc | 275 | |
gandol2 | 6:21b3128fbaef | 276 | |
gandol2 | 3:4bb95aa46ebc | 277 | for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) |
gandol2 | 3:4bb95aa46ebc | 278 | { |
gandol2 | 3:4bb95aa46ebc | 279 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
gandol2 | 3:4bb95aa46ebc | 280 | spi_slave.reply(serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 281 | } |
gandol2 | 6:21b3128fbaef | 282 | |
bcup | 4:cc97d7a33efc | 283 | for(spiTxCnt = 0 ; spiTxCnt < 1 ; ++spiTxCnt) |
bcup | 4:cc97d7a33efc | 284 | { |
bcup | 4:cc97d7a33efc | 285 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 286 | |
bcup | 4:cc97d7a33efc | 287 | spi_slave.reply(serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 288 | PRINTD("reply=%c\n",serialRxBuf[spiTxCnt]); |
bcup | 4:cc97d7a33efc | 289 | } |
gandol2 | 3:4bb95aa46ebc | 290 | |
gandol2 | 3:4bb95aa46ebc | 291 | |
gandol2 | 3:4bb95aa46ebc | 292 | spiTxReadyFlag = 0; |
bcup | 4:cc97d7a33efc | 293 | PRINTD("spiTxReadyFlag =0\n"); |
gandol2 | 3:4bb95aa46ebc | 294 | } |
gandol2 | 6:21b3128fbaef | 295 | #endif |
bcup | 4:cc97d7a33efc | 296 | |
gandol2 | 3:4bb95aa46ebc | 297 | |
gandol2 | 3:4bb95aa46ebc | 298 | |
gandol2 | 3:4bb95aa46ebc | 299 | |
gandol2 | 3:4bb95aa46ebc | 300 | |
gandol2 | 3:4bb95aa46ebc | 301 | #if 0 // 161005_BDK_slave backup start |
bcup | 2:5311ad7c83e6 | 302 | int i; |
bcup | 2:5311ad7c83e6 | 303 | char valueFromMaster; |
bcup | 2:5311ad7c83e6 | 304 | char rx_buffer[255]={0}; |
bcup | 2:5311ad7c83e6 | 305 | char rx_cnt = -1; |
bcup | 2:5311ad7c83e6 | 306 | PRINTD("\n=========SLAVE=========\n"); |
bcup | 2:5311ad7c83e6 | 307 | SPI_SlaveInit(); |
bcup | 2:5311ad7c83e6 | 308 | |
bcup | 0:24e90e3ca3f4 | 309 | while(1) |
bcup | 0:24e90e3ca3f4 | 310 | { |
bcup | 2:5311ad7c83e6 | 311 | |
gandol2 | 3:4bb95aa46ebc | 312 | if(spi_slave.receive()) |
bcup | 2:5311ad7c83e6 | 313 | { |
bcup | 2:5311ad7c83e6 | 314 | PRINTD("----1\n"); |
bcup | 2:5311ad7c83e6 | 315 | if(pc.readable()) |
bcup | 2:5311ad7c83e6 | 316 | { |
bcup | 2:5311ad7c83e6 | 317 | SPI_SlaveWrite(); |
bcup | 2:5311ad7c83e6 | 318 | } |
bcup | 2:5311ad7c83e6 | 319 | PRINTD("----2\n"); |
gandol2 | 3:4bb95aa46ebc | 320 | valueFromMaster = spi_slave.read(); |
bcup | 2:5311ad7c83e6 | 321 | PRINTD("----3\n"); |
bcup | 2:5311ad7c83e6 | 322 | //PRINTD("valueFromMaster="); |
bcup | 2:5311ad7c83e6 | 323 | //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); |
bcup | 2:5311ad7c83e6 | 324 | rx_buffer[++rx_cnt]=valueFromMaster; |
bcup | 2:5311ad7c83e6 | 325 | if(valueFromMaster==0) |
bcup | 2:5311ad7c83e6 | 326 | { |
bcup | 2:5311ad7c83e6 | 327 | PRINTD("rx_string="); |
bcup | 2:5311ad7c83e6 | 328 | for(i=0;i<rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 329 | { |
bcup | 2:5311ad7c83e6 | 330 | PRINTD("%c",rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 331 | } |
bcup | 2:5311ad7c83e6 | 332 | PRINTD("\n"); |
bcup | 2:5311ad7c83e6 | 333 | for(i=0;i<=rx_cnt;i++) |
bcup | 2:5311ad7c83e6 | 334 | { |
bcup | 2:5311ad7c83e6 | 335 | rx_buffer[i]=0; |
bcup | 2:5311ad7c83e6 | 336 | PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); |
bcup | 2:5311ad7c83e6 | 337 | } |
bcup | 2:5311ad7c83e6 | 338 | rx_cnt=-1; |
bcup | 2:5311ad7c83e6 | 339 | |
bcup | 2:5311ad7c83e6 | 340 | }//valueFromMaster if |
bcup | 2:5311ad7c83e6 | 341 | |
gandol2 | 3:4bb95aa46ebc | 342 | }// spi_slave.receive() if |
bcup | 2:5311ad7c83e6 | 343 | /* |
bcup | 2:5311ad7c83e6 | 344 | |
bcup | 2:5311ad7c83e6 | 345 | */ |
bcup | 2:5311ad7c83e6 | 346 | }//end of while |
gandol2 | 3:4bb95aa46ebc | 347 | #endif // 161005_BDK_slave backup end |
bcup | 2:5311ad7c83e6 | 348 | }//end of main |