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