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