161006_BDK_SPImaster
Dependencies: mbed
Fork of Nucleo_SPImaster_F042K6_KSS by
main.cpp
- Committer:
- gandol2
- Date:
- 2016-10-06
- Revision:
- 6:50376de8d756
- Parent:
- 5:47353da6bca4
File content as of revision 6:50376de8d756:
#include "mbed.h" #include <SPI.h> #define DEBUG_SPI #ifdef DEBUG_SPI #define PRINTD(arg1,arg2...) printf(arg1,##arg2) #endif #define SPI_MOSI PA_7 #define SPI_MISO PA_6 #define SPI_SCLK PA_5 #define SPI_SSEL PA_4 SPI spi_master(SPI_MOSI,SPI_MISO,SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK) DigitalOut cs(SPI_SSEL); Serial pc_serial(USBTX, USBRX); void SPI_INIT() { pc_serial.printf("Set SPI init..\n"); pc_serial.printf("Set SPI format..\n"); spi_master.format(8,0); pc_serial.printf("Set frequency to default..\n"); spi_master.frequency(1000000); // default 1MHz } /* void SPI_Write() { char temp; int i,value; char response; char tx_cnt = -1; char rx_cnt = -1; char tx_buffer[255]={0}; char rx_buffer[255]={0}; PRINTD("\n==========MASTER==========\n"); PRINTD("DATA SEND START...\n"); PRINTD("Lock SPI BUS..\n"); while(1) { temp=getchar(); tx_buffer[++tx_cnt]=temp; if(temp==0x0d) { tx_buffer[tx_cnt]=0; PRINTD("\nData send Finish...\n"); for(i=0;i<=tx_cnt;++i) { PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); } PRINTD("\n\n"); spi_master.lock(); for(i=0;i<=tx_cnt;++i) { value=tx_buffer[i]; PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value); cs=0; response=spi_master.write(value); cs=-1; PRINTD("[M]receive=%c[%x]\n",response,response); rx_buffer[++rx_cnt]=response; } spi_master.unlock(); for(i=0;i<255;++i) { tx_buffer[i]=0; } for(i=0;i<=tx_cnt;i++) { PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]); } tx_cnt=-1; } else { PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); } } } */ #define EEPROM_WREN 0x06 // Write Enable #define EEPROM_WRDI 0x04 // Write Disable #define EEPROM_RDSR 0x05 // Read Status Register #define EEPROM_WRSR 0x01 // Write Status Register #define EEPROM_READ 0x03 // Read EEPROM Memory #define EEPROM_WRITE 0x02 // Write EEPROM Memory enum cubeMenu { EEPROM_Write_Enable = 1 , EEPROM_Write_Disable, EEPROM_Read_Status, EEPROM_Write_Status, EEPROM_Write_Memory, EEPROM_Read_Memory, TEST_Multi_Byte_Read, EXIT_Program = 9 }; void print_menu(void) { pc_serial.printf("\n\n\n\n\n\n\n"); pc_serial.printf("-----------[MENU]----------\n"); pc_serial.printf("%d) Write Enable\n", cubeMenu.EEPROM_Write_Enable); pc_serial.printf("%d) Write Disable\n", cubeMenu.EEPROM_Write_Disable); pc_serial.printf("%d) Read Status Register\n", cubeMenu.EEPROM_Read_Status); pc_serial.printf("%d) Write Status Register\n",cubeMenu.EEPROM_Write_Status); pc_serial.printf("%d) Write Memory\n", cubeMenu.EEPROM_Write_Memory); pc_serial.printf("%d) Read Memory\n", cubeMenu.EEPROM_Read_Memory); pc_serial.printf("%d) multi byte read test\n", cubeMenu.TEST_Multi_Byte_Read); pc_serial.printf("%d) Exit program\n", cubeMenu.EXIT_Program); pc_serial.printf("---------------------------\n"); pc_serial.printf("input menu : "); } void spi_start(void) { spi_master.lock(); cs = 0; wait_us(0); } void spi_end(void) { wait_us(10); cs = 1; spi_master.unlock(); } void spi_dummy_write(void) { char dummy_read; spi_start(); dummy_read = spi_master.write( 0x00 ); spi_end(); } /* @convertedData : converted Data @sourceBuf : ASCII data array @lenght : sourceBuf length @return : 0 is valid hex format, other is error */ int converHex(char * convertedData, char * sourceBuf, int length) { int ret = 0; char tempBuf[length]; *convertedData = 0; for(int iCnt = 0 ; iCnt < length ; iCnt++) { if( ('0' <= *(sourceBuf+iCnt)) && ('9' >= *(sourceBuf+iCnt)) ) { tempBuf[iCnt] = (*(sourceBuf+iCnt) - 48); } else if( ('A' <= *(sourceBuf+iCnt)) && ('F' >= *(sourceBuf+iCnt)) ) { tempBuf[iCnt] = (*(sourceBuf+iCnt) - 55); } else if( ('a' <= *(sourceBuf+iCnt)) && ('f' >= *(sourceBuf+iCnt)) ) { tempBuf[iCnt] = (*(sourceBuf+iCnt) - 87); } else // error { ret = -1; return ret; } } //pc_serial.printf("[TEST] tempBuf[0] = 0x%02X\n", tempBuf[0]); //pc_serial.printf("[TEST] tempBuf[1] = 0x%02X\n", tempBuf[1]); *convertedData = ((tempBuf[0] & 0x0F) << 4) | (tempBuf[1] & 0x0F); //pc_serial.printf("[TEST] convertedData = 0x%02X\n", *convertedData); return ret; } // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main(); int main() { char dummyRead; char inputMenuNum = 0; char statusReg = 0x00; int spiTxCnt = 0; int spiRxCnt = 0; char spiTxReadyFlag = 0; int res; int spiRxTempBuf = 0; char serialRxBuf[255]; char spiRxBuf[255]; int serialRxLen = 0; char eepAddrBuf[2] = {0}; char eepAddr = 0; char eepDataBuf[2] = {0}; char eepData = 0; int eepReadData = 0; int maxInputCnt = 0; int ret; SPI_INIT(); spi_dummy_write(); pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n"); while(1) { print_menu(); inputMenuNum = pc_serial.getc(); pc_serial.printf("[DEBUG] input menu number = %c\n", inputMenuNum); if( ('0' < inputMenuNum) && ('9' >= inputMenuNum) ) { inputMenuNum = inputMenuNum - 48; } else { pc_serial.printf("[INFO] correct input menu number.. please retry..\n"); wait(1); continue; } spi_start(); switch(inputMenuNum) { case 1: // Write Enable =================================[OK] pc_serial.printf("[DEBUG] Write Enable progress..\n"); dummyRead = spi_master.write(EEPROM_WREN); break; case 2: // Write Disable =================================[OK] pc_serial.printf("[DEBUG] Write Disable progress..\n"); dummyRead = spi_master.write(EEPROM_WRDI); break; case 3: // Read Status Register =================================[OK] pc_serial.printf("[DEBUG] Read Status Register progress..\n"); spi_master.write(EEPROM_RDSR); statusReg = spi_master.write(EEPROM_RDSR); // TODO : print statusReg data to serial pc_serial.printf("[INFO] Status Register Data : 0x%02X\n", statusReg); break; case 4: // Write Status Register ================================= pc_serial.printf("[DEBUG] Write Status Register progress..\n"); // TODO : input data for serial spi_master.write(EEPROM_WRSR); spi_master.write(statusReg); break; case 5: // Write Memory ================================= pc_serial.printf("[DEBUG] Write Memory progress..\n"); // EEPROM Address input maxInputCnt = 3; while(maxInputCnt--) { pc_serial.printf("input Address (hex format : 00~FF) : "); for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) { eepAddrBuf[iCnt] = pc_serial.getc(); pc_serial.printf("%c", eepAddrBuf[iCnt]); } ret = converHex(&eepAddr , eepAddrBuf, 2); if(0 == ret) { //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); break; } else // error { pc_serial.printf("\n[INFO] hex formet error!\n"); continue; } } if(0 == ret) { // EEPROM data input maxInputCnt = 3; while(maxInputCnt--) { pc_serial.printf("\ninput Data (hex format : 00~FF) : "); for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) { eepDataBuf[iCnt] = pc_serial.getc(); pc_serial.printf("%c", eepDataBuf[iCnt]); } int ret = converHex(&eepData , eepDataBuf, 2); if(0 == ret) { pc_serial.printf("\n[INFO] SPI Write Memory.... Start! \n"); //pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData); spi_master.write(EEPROM_RDSR); if(! (spi_master.write(EEPROM_RDSR) & 0x01) ) { spi_master.write(EEPROM_WRITE); spi_master.write(eepAddr); spi_master.write(eepData); pc_serial.printf("\n[INFO] SPI Write success \n"); } else { pc_serial.printf("\n[INFO] SPI Write fail.. device is busy! \n"); } break; } else // error { pc_serial.printf("\n[INFO] hex formet error!\n"); continue; } } } break; case 6: // Read Memory ================================= pc_serial.printf("[DEBUG] Read Memory progress..\n"); // EEPROM Address input maxInputCnt = 3; while(maxInputCnt--) { pc_serial.printf("input Address (hex format : 00~FF) : "); for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) { eepAddrBuf[iCnt] = pc_serial.getc(); pc_serial.printf("%c", eepAddrBuf[iCnt]); } ret = converHex(&eepAddr , eepAddrBuf, 2); if(0 == ret) { //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); spi_master.write(EEPROM_READ); spi_master.write(eepAddr); eepReadData = spi_master.write(0); pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepReadData); break; } else // error { pc_serial.printf("\n[INFO] hex formet error!\n"); continue; } } break; case 9: spi_end(); pc_serial.printf("exit program... thank you\n"); //return 0; default: } spi_end(); } while(1) { if(0 != pc_serial.readable()) // wait serial input.. { pc_serial.scanf("%s", serialRxBuf); serialRxLen = strlen(serialRxBuf); pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); spiTxReadyFlag = 1; } if(1 == spiTxReadyFlag) { // SPI Send Start spi_master.lock(); cs = 0; for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) { //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); spi_master.write(serialRxBuf[spiTxCnt]); //res=spi_master.write(serialRxBuf[spiTxCnt]); //PRINTD("res=%c 0x[%02x]\n",res,res); //spiRxBuf[spiRxCnt++]=res; } cs = 1; spi_master.unlock(); spiTxReadyFlag = 0; } /* TODO " ----(SPI)-----> master --> serial " */ #if 0 if(res!=0) { PRINTD("res=%d\n",res); PRINTD("res=%s\n",spiRxBuf); } #endif } #if 0 // 161005_BDK_backup2 start int send_data; SPI_INIT(); while(1) { SPI_Write(); } #endif // 161005_BDK_backup2 end return 0; }