CEBF746 Module.. PC <--> This Module (SPI Master) <-->
Dependencies: cube_CEBF746 cube_crc_16 mbed
Diff: main.cpp
- Revision:
- 6:a98f9d63f8a2
- Parent:
- 5:589ab465ed00
- Child:
- 7:efe7a78d7724
diff -r 589ab465ed00 -r a98f9d63f8a2 main.cpp --- a/main.cpp Fri Oct 14 04:41:27 2016 +0000 +++ b/main.cpp Wed Oct 19 08:52:35 2016 +0000 @@ -6,18 +6,14 @@ // PA14 - SEL #include "mbed.h" +#include "lib_CEBF746.h" +#include "lib_crc.h" #define DEBUG_SPI #ifdef DEBUG_SPI #define PRINTD(arg1,arg2...) printf(arg1,##arg2) #endif -// ======== SPI Master Pin define -#define SPI_SCLK PA_5 -#define SPI_MISO PA_6 -#define SPI_MOSI PA_7 -#define SPI_SSEL PA_4 - // ======== SPI Func define #define EEPROM_WREN 0x06 // Write Enable #define EEPROM_WRDI 0x04 // Write Disable @@ -31,28 +27,20 @@ #define SPI_CONFIG_FREQUENCY 1000000 // 1Mhz //#define SPI_CONFIG_FREQUENCY 50000000 // 50Mhz -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)); -} +Serial pc_serial(USBTX, USBRX); -enum cubeMenu +typedef enum { - 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_READ_STATUS_REG = 1, + MENU_SPI_SELF_TEST_WRITE = 2, + MENU_SPI_SELF_TEST_READ = 3, + MENU_SPI_WRITE_ENC_DATA = 4, + MENU_SPI_READ_ENC_DATA = 5, + MENU_EXIT_Program = 9 -}; +}cebf746_command; /** @brief print menu * @@ -60,53 +48,24 @@ void print_menu(void) { 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); + pc_serial.printf("-----------[CEBF746 Test MENU]----------\n"); +// CEBF746 func ### + + + pc_serial.printf("%d) Read Status Reg\n", MENU_READ_STATUS_REG); + pc_serial.printf("%d) Self Test(Write)\n", MENU_SPI_SELF_TEST_WRITE); + pc_serial.printf("%d) Self Test(Read)\n", MENU_SPI_SELF_TEST_READ); + pc_serial.printf("%d) Encrypt Data Write(16Byte)\n", MENU_SPI_WRITE_ENC_DATA); + pc_serial.printf("%d) Encrypted Data Read(16Byte)\n", MENU_SPI_READ_ENC_DATA); // 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("----------------------------------------\n"); pc_serial.printf("input menu : "); } -/** @brief spi master start - * - */ -void spi_start(void) -{ - spi_master.lock(); - cs = 0; - wait_us(0); -} - -/** @brief spi master end - * - */ -void spi_end(void) -{ - wait_us(10); - cs = 1; - spi_master.unlock(); -} - -/** @brief spi master dummy write - * - */ -void spi_dummy_write(void) -{ - spi_start(); - spi_master.write(0x00); - spi_end(); -} - /** @brief convert ASCII to hex * @@ -149,29 +108,32 @@ return ret; } + + + + + + /** @brief main function ################################################################### * */ int main() { + uint16_t tempCrc; + char ret = 0; + char retCode; char inputMenuNum = 0; - char statusReg = 0x00; - char test_data[10] = { 0 }; - - char eepAddrBuf[2] = { 0 }; - char eepAddr = 0; + spiDataStr sendData; + spiDataStr reciveData; + + uint8_t spiMOSIBuf[255]; + uint8_t spiMISOBuf[255]; + - char eepDataBuf[2] = { 0 }; - char eepData = 0; - int eepReadData = 0; - int maxInputCnt = 0; + + cebf746_use_init(); - int ret; - - SPI_InitMaster(); - spi_dummy_write(); - - pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n"); + pc_serial.printf("\n\n========== SPI Master for CEBF746 [Start] ==========\n"); while (1) { @@ -191,201 +153,131 @@ continue; } - spi_start(); + + + + + memset(spiMISOBuf, 0, 255); + memset(spiMOSIBuf, 0, 255); + retCode = 0xFF; switch (inputMenuNum) { - case MENU_EEPROM_Write_Enable: // Write Enable =================================[OK] - pc_serial.printf("[DEBUG] Write Enable progress..\n"); - spi_master.write(EEPROM_WREN); + + case MENU_READ_STATUS_REG: // Write Enable =================================[OK] + pc_serial.printf("[DEBUG] Read Status Reg..\n"); + ret = 0; + ret = cebf746_packet_read(FUNC_READ_STATUS, 0); + printf("[INFO] status reg [%02X]\n", ret); 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_SPI_SELF_TEST_WRITE: // 161017_KSS_Test OK + char spiSelfTestBuf[CEB_BUF_SIZE] = WRITE_SELF_TEST_DATA; + + sendData.size = sizeof(spiSelfTestBuf); + sendData.buf = (uint8_t*)spiSelfTestBuf; + sendData.crc16 = 0; - 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); + ret = cebf746_packet_write(FUNC_WRITE_SELF_TEST, &sendData); +/* + cebf746_print_packet(&sendData); + + pc_serial.printf("FUNC_WRITE_SELF_TEST.."); + if(0 == ret) + { + pc_serial.printf("[SUCESS] ret = %02X\n", ret); + } + else + { + pc_serial.printf("[FAIL] ******** ret = %02X ******** \n", ret); + } +*/ break; - - case MENU_EEPROM_Write_Memory: // Write Memory ================================= - pc_serial.printf("[DEBUG] Write Memory progress..\n"); - - // EEPROM Address input - - maxInputCnt = 3; - while (maxInputCnt--) + + case MENU_SPI_SELF_TEST_READ: // 161017_KSS_Test OK + char selfTempRxBuf[CEB_BUF_SIZE]={0,}; + + reciveData.size = CEB_BUF_SIZE; + reciveData.buf = (uint8_t*)selfTempRxBuf; + sendData.crc16 = 0; + + ret = cebf746_packet_read(FUNC_READ_SELF_TEST, &reciveData); + + break; + + + case MENU_SPI_WRITE_ENC_DATA: + uint8_t encTempBuf[CEB_BUF_SIZE] = {0, }; + + pc_serial.printf("input Encrypt String (16 character) : "); + for(int iCnt = 0 ; iCnt < 16 ; ++iCnt) + { + encTempBuf[iCnt] = pc_serial.getc(); + pc_serial.printf("%c", encTempBuf[iCnt]); + } + + sendData.size = sizeof(encTempBuf); + sendData.buf = (uint8_t*)encTempBuf; + sendData.crc16 = 0; + + ret = cebf746_packet_write(FUNC_WRITE_ENC_DATA, &sendData); + + break; + + + case MENU_SPI_READ_ENC_DATA: + int encryptWaitCnt = 10; + while(1) { - pc_serial.printf("input Address (hex format : 00~FF) : "); - for (int iCnt = 0; iCnt < 2; ++iCnt) + ret = cebf746_packet_read(FUNC_READ_STATUS, 0); + if(ret & 0x02) // Encrypting.. { - eepAddrBuf[iCnt] = pc_serial.getc(); - pc_serial.printf("%c", eepAddrBuf[iCnt]); + --encryptWaitCnt; } - - ret = converHex(&eepAddr, eepAddrBuf, 2); - - if (0 == ret) + else { - //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) + + if(0 == encryptWaitCnt) + { + printf("[INFO] The CEBF746 is running\n"); + continue; + } + else { - // 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; - } - } + /* 161019_암호화된 데이터 읽어와야함. 슬레이브도 구현 해야함 */ } - + + + + /* + uint8_t encTempBuf[CEB_BUF_SIZE] = {0, }; + + pc_serial.printf("input Encrypt String (16 character) : "); + for(int iCnt = 0 ; iCnt < 16 ; ++iCnt) + { + encTempBuf[iCnt] = pc_serial.getc(); + pc_serial.printf("%c", encTempBuf[iCnt]); + } + + sendData.size = sizeof(encTempBuf); + sendData.buf = (uint8_t*)encTempBuf; + sendData.crc16 = 0; + + ret = cebf746_packet_write(FUNC_WRITE_ENC_DATA, &sendData); + */ 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) - { - 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 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); - - 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; } - spi_end(); + }