CEBF746 Module.. PC <--> This Module (SPI Master) <-->
Dependencies: cube_CEBF746 cube_crc_16 mbed
Diff: main.cpp
- Revision:
- 5:589ab465ed00
- Parent:
- 0:9a61123385e9
- Child:
- 6:a98f9d63f8a2
--- a/main.cpp Wed Oct 12 00:48:11 2016 +0000 +++ b/main.cpp Fri Oct 14 04:41:27 2016 +0000 @@ -5,12 +5,9 @@ // PA7 - MOSI // PA14 - SEL - #include "mbed.h" -#include <SPI.h> #define DEBUG_SPI - #ifdef DEBUG_SPI #define PRINTD(arg1,arg2...) printf(arg1,##arg2) #endif @@ -21,81 +18,7 @@ #define SPI_MOSI PA_7 #define SPI_SSEL PA_4 -#define MASTER - -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(50000000); // default 1MHz - spi_master.frequency(1000000); -} -/* -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]); - } - } -} -*/ - +// ======== SPI Func define #define EEPROM_WREN 0x06 // Write Enable #define EEPROM_WRDI 0x04 // Write Disable #define EEPROM_RDSR 0x05 // Read Status Register @@ -104,88 +27,121 @@ #define EEPROM_WRITE 0x02 // Write EEPROM Memory #define EEPROM_MULTIREAD 0x07 // Write EEPROM Memory +#define SPI_CONFIG_BIT 8 +#define SPI_CONFIG_FREQUENCY 1000000 // 1Mhz +//#define SPI_CONFIG_FREQUENCY 50000000 // 50Mhz -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 - }; +Serial pc_serial(USBTX, USBRX); +SPI spi_master(SPI_MOSI, SPI_MISO, SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK) +DigitalOut cs(SPI_SSEL); + +void SPI_InitMaster() +{ + spi_master.format(SPI_CONFIG_BIT, 0); + spi_master.frequency(SPI_CONFIG_FREQUENCY); + PRINTD("SPI Init.. Packet bit size=[%d] Frequency=[%d]Mhz\n", SPI_CONFIG_BIT, (SPI_CONFIG_FREQUENCY/1000000)); +} +enum cubeMenu +{ + MENU_EEPROM_Write_Enable = 1, + MENU_EEPROM_Write_Disable, + MENU_EEPROM_Read_Status, + MENU_EEPROM_Write_Status, + MENU_EEPROM_Write_Memory, + MENU_EEPROM_Read_Memory, + MENU_TEST_Multi_Byte_Read, + MENU_EXIT_Program = 9 +}; + +/** @brief print menu + * + */ void print_menu(void) { - pc_serial.printf("\n\n\n\n\n\n\n"); - pc_serial.printf("-----------[MENU]----------\n"); - pc_serial.printf("1) Write Enable\n");//, cubeMenu.EEPROM_Write_Enable); - pc_serial.printf("2) Write Disable\n");//, cubeMenu.EEPROM_Write_Disable); - pc_serial.printf("3) Read Status Register\n");//, cubeMenu.EEPROM_Read_Status); - pc_serial.printf("4) Write Status Register\n");//,cubeMenu.EEPROM_Write_Status); - pc_serial.printf("5) Write Memory\n");//, cubeMenu.EEPROM_Write_Memory); - pc_serial.printf("6) Read Memory\n");//, cubeMenu.EEPROM_Read_Memory); - pc_serial.printf("7) multi byte read\n");//, cubeMenu.TEST_Multi_Byte_Read); - pc_serial.printf("9) Exit program\n");//, cubeMenu.EXIT_Program); - pc_serial.printf("---------------------------\n"); - pc_serial.printf("input menu : "); + pc_serial.printf("\n\n\n"); + pc_serial.printf("-----------[MENU]----------\n"); +// EEPROM func ### + pc_serial.printf("%d) Write Enable\n", MENU_EEPROM_Write_Enable); + pc_serial.printf("%d) Write Disable\n", MENU_EEPROM_Write_Disable); + pc_serial.printf("%d) Read Status Register\n", MENU_EEPROM_Read_Status); + pc_serial.printf("%d) Write Status Register\n", MENU_EEPROM_Write_Status); + pc_serial.printf("%d) Write Memory\n", MENU_EEPROM_Write_Memory); + pc_serial.printf("%d) Read Memory\n", MENU_EEPROM_Read_Memory); +// cubebite test func ### + pc_serial.printf("%d) multi byte read\n", MENU_TEST_Multi_Byte_Read); + +// exit func ### + pc_serial.printf("%d) Exit program\n", MENU_EXIT_Program); + pc_serial.printf("---------------------------\n"); + pc_serial.printf("input menu : "); } +/** @brief spi master start + * + */ void spi_start(void) { - spi_master.lock(); - cs = 0; + spi_master.lock(); + cs = 0; wait_us(0); } + +/** @brief spi master end + * + */ void spi_end(void) { wait_us(10); - cs = 1; + cs = 1; spi_master.unlock(); } + +/** @brief spi master dummy write + * + */ void spi_dummy_write(void) { - char dummy_read; spi_start(); - dummy_read = spi_master.write( 0x00 ); + 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 -*/ + +/** @brief convert ASCII to hex + * + * @param convertedData converted Data + * @param sourceBuf ASCII data array + * @param 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++) + + for (int iCnt = 0; iCnt < length; iCnt++) { - if( ('0' <= *(sourceBuf+iCnt)) && ('9' >= *(sourceBuf+iCnt)) ) + if (('0' <= *(sourceBuf + iCnt)) && ('9' >= *(sourceBuf + iCnt))) { - tempBuf[iCnt] = (*(sourceBuf+iCnt) - 48); + tempBuf[iCnt] = (*(sourceBuf + iCnt) - 48); } - else if( ('A' <= *(sourceBuf+iCnt)) && ('F' >= *(sourceBuf+iCnt)) ) + else if (('A' <= *(sourceBuf + iCnt)) && ('F' >= *(sourceBuf + iCnt))) { - tempBuf[iCnt] = (*(sourceBuf+iCnt) - 55); + tempBuf[iCnt] = (*(sourceBuf + iCnt) - 55); } - else if( ('a' <= *(sourceBuf+iCnt)) && ('f' >= *(sourceBuf+iCnt)) ) - { - tempBuf[iCnt] = (*(sourceBuf+iCnt) - 87); + else if (('a' <= *(sourceBuf + iCnt)) && ('f' >= *(sourceBuf + iCnt))) + { + tempBuf[iCnt] = (*(sourceBuf + iCnt) - 87); } else // error { ret = -1; - return ret; - } + 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); @@ -193,398 +149,245 @@ return ret; } -// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main(); +/** @brief main function ################################################################### + * + */ int main() { - char dummyRead; - char inputMenuNum = 0; - char statusReg = 0x00; - char test_data[10]={0}; - - 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) ) - { + char inputMenuNum = 0; + char statusReg = 0x00; + char test_data[10] = { 0 }; + + char eepAddrBuf[2] = { 0 }; + char eepAddr = 0; + + char eepDataBuf[2] = { 0 }; + char eepData = 0; + int eepReadData = 0; + int maxInputCnt = 0; + + int ret; + + SPI_InitMaster(); + 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 - + } + else + { + pc_serial.printf("[INFO] correct input menu number.. please retry..\n"); + wait(1); + continue; + } + + spi_start(); + switch (inputMenuNum) + { + case MENU_EEPROM_Write_Enable: // Write Enable =================================[OK] + pc_serial.printf("[DEBUG] Write Enable progress..\n"); + spi_master.write(EEPROM_WREN); + break; + + case MENU_EEPROM_Write_Disable: // Write Disable =================================[OK] + pc_serial.printf("[DEBUG] Write Disable progress..\n"); + spi_master.write(EEPROM_WRDI); + break; + + case MENU_EEPROM_Read_Status: // 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 MENU_EEPROM_Write_Status: // 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 MENU_EEPROM_Write_Memory: // 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--) + 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]); + 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]); } - - ret = converHex(&eepAddr , eepAddrBuf, 2); - - if(0 == ret) + + int ret = converHex(&eepData, eepDataBuf, 2); + if (0 == ret) { - //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); + + 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; - } - } - - - 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--) + } + + break; + + case MENU_EEPROM_Read_Memory: // 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) { - 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; - } + eepAddrBuf[iCnt] = pc_serial.getc(); + pc_serial.printf("%c", eepAddrBuf[iCnt]); } - - - - break; - - - case 7: - pc_serial.printf("\n[DEBUG] TEST_Multi_Byte_Read\n"); - - maxInputCnt = 3; - while(maxInputCnt--) + + ret = converHex(&eepAddr, eepAddrBuf, 2); + + if (0 == ret) { - 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) - { - spi_master.write(EEPROM_MULTIREAD); - spi_master.write(eepAddr); - - - for(int i=0;i<5;i++) - { - wait_ms(10); - //test_data[i] = spi_master.write(0); - //spi_master.write(++eepAddr); - test_data[i] = spi_master.write(0); // dummy write - - } - - for(int i=0;i<5;i++) - { - printf("\n[DEBUG] Read EEPROM Data[0x%02X]\n",test_data[i]); - } - - //pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X]\n", eepAddr); - break; - } - else // error - { - pc_serial.printf("\n[INFO] hex formet error!\n"); - continue; - } + //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(); - - } - + } + + break; + + case MENU_TEST_Multi_Byte_Read: + pc_serial.printf("\n[DEBUG] TEST_Multi_Byte_Read\n"); + + 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) + { + spi_master.write(EEPROM_MULTIREAD); + spi_master.write(eepAddr); - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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; + for (int i = 0; i < 5; i++) + { + wait_ms(10); + //test_data[i] = spi_master.write(0); + //spi_master.write(++eepAddr); + test_data[i] = spi_master.write(0); // dummy write + + } + + for (int i = 0; i < 5; i++) + { + printf("\n[DEBUG] Read EEPROM Data[0x%02X]\n", test_data[i]); + } + + //pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X]\n", eepAddr); + break; + } + else // error + { + pc_serial.printf("\n[INFO] hex formet error!\n"); + continue; + } + } + + break; + + case MENU_EXIT_Program: + spi_end(); + pc_serial.printf("exit program... thank you\n"); + //return 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(); + spi_end(); + } -#endif // 161005_BDK_backup2 end + return 0; }