Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of 161006_BDK_EEPROM by
main.cpp
00001 // firmware for CBEF746 00002 // ======== SPI Slave Pin 00003 // PA5 - SCK 00004 // PA6 - MISO 00005 // PA7 - MOSI 00006 // PA14 - SEL 00007 // ======== I2C Pin for TPM 00008 // PB9 - SDA 00009 // PB8 - SCL 00010 // commit test 00011 00012 #include "mbed.h" 00013 00014 #define DEBUG_SPI 00015 00016 #ifdef DEBUG_SPI 00017 #define PRINTD(arg1,arg2...) printf(arg1,##arg2) 00018 #endif 00019 00020 // ======== SPI Slave Pin define 00021 /* 00022 #define SPI_SCLK PA_5 00023 #define SPI_MISO PA_6 00024 #define SPI_MOSI PA_7 00025 #define SPI_SSEL PA_14 00026 */ 00027 #define SPI_MOSI PA_7 00028 #define SPI_MISO PA_6 00029 #define SPI_SCLK PA_5 00030 #define SPI_SSEL PA_4 00031 00032 #define EEPROM_WREN 0x06 // Write Enable 00033 #define EEPROM_WRDI 0x04 // Write Disable 00034 #define EEPROM_RDSR 0x05 // Read Status Register 00035 #define EEPROM_WRSR 0x01 // Write Status Register 00036 #define EEPROM_READ 0x03 // Read EEPROM Memory 00037 #define EEPROM_WRITE 0x02 // Write EEPROM Memory 00038 #define EEPROM_MULTIREAD 0x07 // Read Multibyte EEPROM Memory 00039 00040 SPISlave spi_slave(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_SSEL); // MOSI, MISO, SCLK(CLK), SSEL(CS)=NC 00041 Serial pc_serial(USBTX, USBRX); 00042 00043 void SPI_SlaveInit() 00044 { 00045 PRINTD("Set the SPI SLAVE format\n"); 00046 spi_slave.format(8,0); // setup 8bit 0 mode 00047 PRINTD("Set the SPI SLAVE frequency\n"); 00048 //spi_slave.frequency(50000000); // default 1MHz 00049 spi_slave.frequency(1000000); 00050 } 00051 00052 void SPI_SlaveWrite() 00053 { 00054 char tx_buffer[255]={0}; 00055 char reply; 00056 char i; 00057 char temp; 00058 char tx_cnt = 0; 00059 int value; 00060 PRINTD("Input Strging="); 00061 while(1) 00062 { 00063 00064 temp=getchar(); 00065 tx_buffer[tx_cnt++]=temp; 00066 if(temp==0x0d) 00067 { 00068 tx_buffer[tx_cnt]=0; 00069 PRINTD("\nData send Finish...\n"); 00070 for(i=0;i<=tx_cnt;++i) 00071 { 00072 PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); 00073 } 00074 PRINTD("\n\n"); 00075 for(i=0;i<=tx_cnt;++i) 00076 { 00077 value=tx_buffer[i]; 00078 PRINTD("write[%d]=%c[%02x]\n",i,value,value); 00079 spi_slave.reply(value); 00080 } 00081 for(i=0;i<tx_cnt;++i) 00082 { 00083 tx_buffer[i]=0; 00084 PRINTD("init_tx_buf[%d]=%c\n",i,tx_buffer[i]); 00085 } 00086 tx_cnt=0; 00087 PRINTD("break\n"); 00088 break; 00089 } 00090 else 00091 { 00092 PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); 00093 } 00094 } 00095 return; 00096 } 00097 00098 void spi_dummy_reply(void) 00099 { 00100 spi_slave.reply( 0xA0 ); 00101 } 00102 00103 #define EEPROM_SIZE 256 00104 char statusReg; 00105 char eepBuf[EEPROM_SIZE]; 00106 00107 void printBuf(char * buf, int length) 00108 { 00109 pc_serial.printf("=======================[EEPROM]========================\n"); 00110 pc_serial.printf(" |"); 00111 for(int iCnt = 0 ; iCnt < 16 ; ++iCnt) 00112 { 00113 pc_serial.printf("%02X|", iCnt); 00114 } 00115 pc_serial.printf("\n"); 00116 00117 00118 for(int iCnt = 0 ; iCnt < length ; ++iCnt) 00119 { 00120 if(0 == ((iCnt) % 16)) 00121 { 00122 pc_serial.printf("[0x%02X] ", iCnt); 00123 } 00124 00125 pc_serial.printf("%02X ", *(buf + iCnt)); 00126 00127 if(0 == ((iCnt+1) % 16)) 00128 { 00129 pc_serial.printf("\n"); 00130 00131 } 00132 00133 } 00134 pc_serial.printf("=======================================================\n"); 00135 } 00136 00137 int main() 00138 { 00139 00140 char spiMulti = 0; 00141 char eepAddr = 0; 00142 char eepData = 0; 00143 00144 char serialTxReadyFlag = 0; 00145 int spiRxTempBuf = 0; 00146 char spiRxBuf[255]; 00147 int spiRxTempCnt = 0; 00148 int spiRxLen = 0; 00149 00150 00151 int spiTxCnt = 0; 00152 char spiTxReadyFlag = 0; 00153 00154 char serialRxBuf[255]; 00155 int serialRxLen = 0; 00156 00157 SPI_SlaveInit(); 00158 00159 /* 00160 NVIC_SetVector( SPI_IRQn , ( uint32_t ) executa_spi_slave_hw ) ; 00161 NVIC_SetPriority( SPI_IRQn , 2 ) ; 00162 NVIC_EnableIRQ( SPI_IRQn ) ; 00163 */ 00164 00165 00166 pc_serial.printf("\n\n========== KSS SPI Slave2 [Start] ==========\n"); 00167 00168 while(1) 00169 { 00170 if(spi_slave.receive()) // wait SPI data input... 00171 { 00172 spiRxTempBuf = spi_slave.read(); 00173 //spi_slave.reply( statusReg++ ); 00174 //continue; 00175 00176 switch(spiRxTempBuf) 00177 { 00178 case EEPROM_WREN: 00179 //spi_dummy_reply(); 00180 pc_serial.printf("[INFO] SPI_MOSI(RX) : WREN\n"); 00181 statusReg = statusReg | (1<<1) ; 00182 pc_serial.printf("[INFO] WREN sucess [0x%02X]\n", statusReg); 00183 break; 00184 00185 case EEPROM_WRDI: 00186 //spi_dummy_reply(); 00187 pc_serial.printf("[INFO] SPI_MOSI(RX) : WRDI\n"); 00188 statusReg = statusReg & (~(1 << 1)) ; 00189 pc_serial.printf("[INFO] WRDI sucess [0x%02X]\n", statusReg); 00190 break; 00191 00192 case EEPROM_RDSR: 00193 spi_slave.reply( statusReg ); 00194 pc_serial.printf("[INFO] SPI_MOSI(RX) : RDSR\n"); 00195 pc_serial.printf("[INFO] SPI_MISO(TX) : RDSR[0x%02X] \n", statusReg); 00196 break; 00197 00198 00199 case EEPROM_WRITE: 00200 // EEPROM Address read.. 00201 while(!spi_slave.receive()); 00202 eepAddr = spi_slave.read(); 00203 00204 // EEPROM Data read.. 00205 while(!spi_slave.receive()); 00206 eepData = spi_slave.read(); 00207 00208 pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData); 00209 00210 if(statusReg & 0x02) 00211 { 00212 statusReg |= 0x01; 00213 eepBuf[eepAddr] = eepData; 00214 printBuf(eepBuf, EEPROM_SIZE); 00215 statusReg &= (~0x01); 00216 00217 } 00218 else 00219 { 00220 pc_serial.printf("\nwrite command is disabled\n"); 00221 } 00222 break; 00223 00224 00225 case EEPROM_READ: 00226 // EEPROM Address read.. 00227 while(!spi_slave.receive()); 00228 eepAddr = spi_slave.read(); 00229 00230 spi_slave.reply( eepBuf[eepAddr] ); 00231 00232 pc_serial.printf("\n[DEBUG] Addr[0x%02X]\n", eepAddr); 00233 00234 00235 00236 00237 break; 00238 00239 00240 case EEPROM_MULTIREAD: 00241 // EEPROM_MULTIREAD.. 00242 while(!spi_slave.receive()); 00243 eepAddr = spi_slave.read(); 00244 00245 //printf("[DEBUG] [M to S] eepAddr=0x%02x\n",eepAddr); 00246 for(int i=0;i<5;i++) 00247 { 00248 spi_slave.reply( eepBuf[eepAddr++] ); 00249 00250 //++eepAddr; 00251 //printf("[0x%02x] ",spiMulti); 00252 } 00253 00254 printf("[DEBUG] [M to S] eepAddr=0x%02x\n",eepAddr++); 00255 break; 00256 00257 00258 default: 00259 //spi_dummy_reply(); 00260 } 00261 00262 //pc_serial.printf("------------------------ end SPI Communication\n"); 00263 00264 } 00265 } 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 /* 00277 spiRxBuf[spiRxTempCnt++] = spiRxTempBuf; 00278 00279 if(0x00 == spiRxTempBuf) 00280 { 00281 serialTxReadyFlag = 1; 00282 //pc_serial.printf("SPI Recive Byte [%d]\n", spiRxTempCnt-1); 00283 spiRxLen = strlen(spiRxBuf); 00284 spiRxTempCnt = 0; 00285 } 00286 00287 } 00288 if(1 == serialTxReadyFlag) 00289 { 00290 serialTxReadyFlag = 0; 00291 pc_serial.printf("SPI_RX Data : len=[%d] %s\n", spiRxLen, spiRxBuf); 00292 } 00293 00294 */ 00295 00296 00297 00298 #if 0 00299 /* TODO "serial -> slave ----(SPI)-----> " */ 00300 00301 if(0 != pc_serial.readable()) // wait serial input.. 00302 { 00303 pc_serial.scanf("%s", serialRxBuf); 00304 serialRxLen = strlen(serialRxBuf); 00305 pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); 00306 spiTxReadyFlag = 1; 00307 } 00308 if(1 == spiTxReadyFlag) 00309 { 00310 PRINTD("spiTxReadyFlag=%d\n",spiTxReadyFlag); 00311 // SPI Send Start 00312 00313 00314 for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) 00315 { 00316 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); 00317 spi_slave.reply(serialRxBuf[spiTxCnt]); 00318 } 00319 00320 for(spiTxCnt = 0 ; spiTxCnt < 1 ; ++spiTxCnt) 00321 { 00322 //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); 00323 00324 spi_slave.reply(serialRxBuf[spiTxCnt]); 00325 PRINTD("reply=%c\n",serialRxBuf[spiTxCnt]); 00326 } 00327 00328 00329 spiTxReadyFlag = 0; 00330 PRINTD("spiTxReadyFlag =0\n"); 00331 } 00332 #endif 00333 00334 00335 00336 00337 00338 #if 0 // 161005_BDK_slave backup start 00339 int i; 00340 char valueFromMaster; 00341 char rx_buffer[255]={0}; 00342 char rx_cnt = -1; 00343 PRINTD("\n=========SLAVE=========\n"); 00344 SPI_SlaveInit(); 00345 00346 while(1) 00347 { 00348 00349 if(spi_slave.receive()) 00350 { 00351 PRINTD("----1\n"); 00352 if(pc.readable()) 00353 { 00354 SPI_SlaveWrite(); 00355 } 00356 PRINTD("----2\n"); 00357 valueFromMaster = spi_slave.read(); 00358 PRINTD("----3\n"); 00359 //PRINTD("valueFromMaster="); 00360 //PRINTD("[%c]%x\n",valueFromMaster,valueFromMaster); 00361 rx_buffer[++rx_cnt]=valueFromMaster; 00362 if(valueFromMaster==0) 00363 { 00364 PRINTD("rx_string="); 00365 for(i=0;i<rx_cnt;i++) 00366 { 00367 PRINTD("%c",rx_buffer[i]); 00368 } 00369 PRINTD("\n"); 00370 for(i=0;i<=rx_cnt;i++) 00371 { 00372 rx_buffer[i]=0; 00373 PRINTD("Init_rx_buf[%d]=%c\n",i,rx_buffer[i]); 00374 } 00375 rx_cnt=-1; 00376 00377 }//valueFromMaster if 00378 00379 }// spi_slave.receive() if 00380 /* 00381 00382 */ 00383 }//end of while 00384 #endif // 161005_BDK_slave backup end 00385 }//end of main
Generated on Thu Jul 14 2022 05:12:27 by
1.7.2
