CEBF746 Module.. PC <--> This Module (SPI Master) <-->
Dependencies: cube_CEBF746 cube_crc_16 mbed
main.cpp@5:589ab465ed00, 2016-10-14 (annotated)
- Committer:
- gandol2
- Date:
- Fri Oct 14 04:41:27 2016 +0000
- Revision:
- 5:589ab465ed00
- Parent:
- 0:9a61123385e9
- Child:
- 6:a98f9d63f8a2
backup eeprom code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gandol2 | 0:9a61123385e9 | 1 | // firmware for NUCLEO-F746ZG |
gandol2 | 0:9a61123385e9 | 2 | // ======== SPI Master Pin |
gandol2 | 0:9a61123385e9 | 3 | // PA5 - SCK |
gandol2 | 0:9a61123385e9 | 4 | // PA6 - MISO |
gandol2 | 0:9a61123385e9 | 5 | // PA7 - MOSI |
gandol2 | 0:9a61123385e9 | 6 | // PA14 - SEL |
gandol2 | 0:9a61123385e9 | 7 | |
gandol2 | 0:9a61123385e9 | 8 | #include "mbed.h" |
gandol2 | 0:9a61123385e9 | 9 | |
gandol2 | 0:9a61123385e9 | 10 | #define DEBUG_SPI |
gandol2 | 0:9a61123385e9 | 11 | #ifdef DEBUG_SPI |
gandol2 | 0:9a61123385e9 | 12 | #define PRINTD(arg1,arg2...) printf(arg1,##arg2) |
gandol2 | 0:9a61123385e9 | 13 | #endif |
gandol2 | 0:9a61123385e9 | 14 | |
gandol2 | 0:9a61123385e9 | 15 | // ======== SPI Master Pin define |
gandol2 | 0:9a61123385e9 | 16 | #define SPI_SCLK PA_5 |
gandol2 | 0:9a61123385e9 | 17 | #define SPI_MISO PA_6 |
gandol2 | 0:9a61123385e9 | 18 | #define SPI_MOSI PA_7 |
gandol2 | 0:9a61123385e9 | 19 | #define SPI_SSEL PA_4 |
gandol2 | 0:9a61123385e9 | 20 | |
gandol2 | 5:589ab465ed00 | 21 | // ======== SPI Func define |
gandol2 | 0:9a61123385e9 | 22 | #define EEPROM_WREN 0x06 // Write Enable |
gandol2 | 0:9a61123385e9 | 23 | #define EEPROM_WRDI 0x04 // Write Disable |
gandol2 | 0:9a61123385e9 | 24 | #define EEPROM_RDSR 0x05 // Read Status Register |
gandol2 | 0:9a61123385e9 | 25 | #define EEPROM_WRSR 0x01 // Write Status Register |
gandol2 | 0:9a61123385e9 | 26 | #define EEPROM_READ 0x03 // Read EEPROM Memory |
gandol2 | 0:9a61123385e9 | 27 | #define EEPROM_WRITE 0x02 // Write EEPROM Memory |
gandol2 | 0:9a61123385e9 | 28 | #define EEPROM_MULTIREAD 0x07 // Write EEPROM Memory |
gandol2 | 0:9a61123385e9 | 29 | |
gandol2 | 5:589ab465ed00 | 30 | #define SPI_CONFIG_BIT 8 |
gandol2 | 5:589ab465ed00 | 31 | #define SPI_CONFIG_FREQUENCY 1000000 // 1Mhz |
gandol2 | 5:589ab465ed00 | 32 | //#define SPI_CONFIG_FREQUENCY 50000000 // 50Mhz |
gandol2 | 0:9a61123385e9 | 33 | |
gandol2 | 5:589ab465ed00 | 34 | Serial pc_serial(USBTX, USBRX); |
gandol2 | 5:589ab465ed00 | 35 | SPI spi_master(SPI_MOSI, SPI_MISO, SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK) |
gandol2 | 5:589ab465ed00 | 36 | DigitalOut cs(SPI_SSEL); |
gandol2 | 5:589ab465ed00 | 37 | |
gandol2 | 5:589ab465ed00 | 38 | void SPI_InitMaster() |
gandol2 | 5:589ab465ed00 | 39 | { |
gandol2 | 5:589ab465ed00 | 40 | spi_master.format(SPI_CONFIG_BIT, 0); |
gandol2 | 5:589ab465ed00 | 41 | spi_master.frequency(SPI_CONFIG_FREQUENCY); |
gandol2 | 5:589ab465ed00 | 42 | PRINTD("SPI Init.. Packet bit size=[%d] Frequency=[%d]Mhz\n", SPI_CONFIG_BIT, (SPI_CONFIG_FREQUENCY/1000000)); |
gandol2 | 5:589ab465ed00 | 43 | } |
gandol2 | 0:9a61123385e9 | 44 | |
gandol2 | 5:589ab465ed00 | 45 | enum cubeMenu |
gandol2 | 5:589ab465ed00 | 46 | { |
gandol2 | 5:589ab465ed00 | 47 | MENU_EEPROM_Write_Enable = 1, |
gandol2 | 5:589ab465ed00 | 48 | MENU_EEPROM_Write_Disable, |
gandol2 | 5:589ab465ed00 | 49 | MENU_EEPROM_Read_Status, |
gandol2 | 5:589ab465ed00 | 50 | MENU_EEPROM_Write_Status, |
gandol2 | 5:589ab465ed00 | 51 | MENU_EEPROM_Write_Memory, |
gandol2 | 5:589ab465ed00 | 52 | MENU_EEPROM_Read_Memory, |
gandol2 | 5:589ab465ed00 | 53 | MENU_TEST_Multi_Byte_Read, |
gandol2 | 5:589ab465ed00 | 54 | MENU_EXIT_Program = 9 |
gandol2 | 5:589ab465ed00 | 55 | }; |
gandol2 | 5:589ab465ed00 | 56 | |
gandol2 | 5:589ab465ed00 | 57 | /** @brief print menu |
gandol2 | 5:589ab465ed00 | 58 | * |
gandol2 | 5:589ab465ed00 | 59 | */ |
gandol2 | 0:9a61123385e9 | 60 | void print_menu(void) |
gandol2 | 0:9a61123385e9 | 61 | { |
gandol2 | 5:589ab465ed00 | 62 | pc_serial.printf("\n\n\n"); |
gandol2 | 5:589ab465ed00 | 63 | pc_serial.printf("-----------[MENU]----------\n"); |
gandol2 | 5:589ab465ed00 | 64 | // EEPROM func ### |
gandol2 | 5:589ab465ed00 | 65 | pc_serial.printf("%d) Write Enable\n", MENU_EEPROM_Write_Enable); |
gandol2 | 5:589ab465ed00 | 66 | pc_serial.printf("%d) Write Disable\n", MENU_EEPROM_Write_Disable); |
gandol2 | 5:589ab465ed00 | 67 | pc_serial.printf("%d) Read Status Register\n", MENU_EEPROM_Read_Status); |
gandol2 | 5:589ab465ed00 | 68 | pc_serial.printf("%d) Write Status Register\n", MENU_EEPROM_Write_Status); |
gandol2 | 5:589ab465ed00 | 69 | pc_serial.printf("%d) Write Memory\n", MENU_EEPROM_Write_Memory); |
gandol2 | 5:589ab465ed00 | 70 | pc_serial.printf("%d) Read Memory\n", MENU_EEPROM_Read_Memory); |
gandol2 | 5:589ab465ed00 | 71 | // cubebite test func ### |
gandol2 | 5:589ab465ed00 | 72 | pc_serial.printf("%d) multi byte read\n", MENU_TEST_Multi_Byte_Read); |
gandol2 | 5:589ab465ed00 | 73 | |
gandol2 | 5:589ab465ed00 | 74 | // exit func ### |
gandol2 | 5:589ab465ed00 | 75 | pc_serial.printf("%d) Exit program\n", MENU_EXIT_Program); |
gandol2 | 5:589ab465ed00 | 76 | pc_serial.printf("---------------------------\n"); |
gandol2 | 5:589ab465ed00 | 77 | pc_serial.printf("input menu : "); |
gandol2 | 0:9a61123385e9 | 78 | } |
gandol2 | 0:9a61123385e9 | 79 | |
gandol2 | 5:589ab465ed00 | 80 | /** @brief spi master start |
gandol2 | 5:589ab465ed00 | 81 | * |
gandol2 | 5:589ab465ed00 | 82 | */ |
gandol2 | 0:9a61123385e9 | 83 | void spi_start(void) |
gandol2 | 0:9a61123385e9 | 84 | { |
gandol2 | 5:589ab465ed00 | 85 | spi_master.lock(); |
gandol2 | 5:589ab465ed00 | 86 | cs = 0; |
gandol2 | 0:9a61123385e9 | 87 | wait_us(0); |
gandol2 | 0:9a61123385e9 | 88 | } |
gandol2 | 5:589ab465ed00 | 89 | |
gandol2 | 5:589ab465ed00 | 90 | /** @brief spi master end |
gandol2 | 5:589ab465ed00 | 91 | * |
gandol2 | 5:589ab465ed00 | 92 | */ |
gandol2 | 0:9a61123385e9 | 93 | void spi_end(void) |
gandol2 | 0:9a61123385e9 | 94 | { |
gandol2 | 0:9a61123385e9 | 95 | wait_us(10); |
gandol2 | 5:589ab465ed00 | 96 | cs = 1; |
gandol2 | 0:9a61123385e9 | 97 | spi_master.unlock(); |
gandol2 | 0:9a61123385e9 | 98 | } |
gandol2 | 5:589ab465ed00 | 99 | |
gandol2 | 5:589ab465ed00 | 100 | /** @brief spi master dummy write |
gandol2 | 5:589ab465ed00 | 101 | * |
gandol2 | 5:589ab465ed00 | 102 | */ |
gandol2 | 0:9a61123385e9 | 103 | void spi_dummy_write(void) |
gandol2 | 0:9a61123385e9 | 104 | { |
gandol2 | 0:9a61123385e9 | 105 | spi_start(); |
gandol2 | 5:589ab465ed00 | 106 | spi_master.write(0x00); |
gandol2 | 0:9a61123385e9 | 107 | spi_end(); |
gandol2 | 0:9a61123385e9 | 108 | } |
gandol2 | 0:9a61123385e9 | 109 | |
gandol2 | 5:589ab465ed00 | 110 | |
gandol2 | 5:589ab465ed00 | 111 | /** @brief convert ASCII to hex |
gandol2 | 5:589ab465ed00 | 112 | * |
gandol2 | 5:589ab465ed00 | 113 | * @param convertedData converted Data |
gandol2 | 5:589ab465ed00 | 114 | * @param sourceBuf ASCII data array |
gandol2 | 5:589ab465ed00 | 115 | * @param lenght sourceBuf length |
gandol2 | 5:589ab465ed00 | 116 | * @return 0 is valid hex format, other is error |
gandol2 | 5:589ab465ed00 | 117 | */ |
gandol2 | 0:9a61123385e9 | 118 | int converHex(char * convertedData, char * sourceBuf, int length) |
gandol2 | 0:9a61123385e9 | 119 | { |
gandol2 | 0:9a61123385e9 | 120 | int ret = 0; |
gandol2 | 0:9a61123385e9 | 121 | char tempBuf[length]; |
gandol2 | 0:9a61123385e9 | 122 | *convertedData = 0; |
gandol2 | 5:589ab465ed00 | 123 | |
gandol2 | 5:589ab465ed00 | 124 | for (int iCnt = 0; iCnt < length; iCnt++) |
gandol2 | 0:9a61123385e9 | 125 | { |
gandol2 | 5:589ab465ed00 | 126 | if (('0' <= *(sourceBuf + iCnt)) && ('9' >= *(sourceBuf + iCnt))) |
gandol2 | 0:9a61123385e9 | 127 | { |
gandol2 | 5:589ab465ed00 | 128 | tempBuf[iCnt] = (*(sourceBuf + iCnt) - 48); |
gandol2 | 0:9a61123385e9 | 129 | } |
gandol2 | 5:589ab465ed00 | 130 | else if (('A' <= *(sourceBuf + iCnt)) && ('F' >= *(sourceBuf + iCnt))) |
gandol2 | 0:9a61123385e9 | 131 | { |
gandol2 | 5:589ab465ed00 | 132 | tempBuf[iCnt] = (*(sourceBuf + iCnt) - 55); |
gandol2 | 0:9a61123385e9 | 133 | } |
gandol2 | 5:589ab465ed00 | 134 | else if (('a' <= *(sourceBuf + iCnt)) && ('f' >= *(sourceBuf + iCnt))) |
gandol2 | 5:589ab465ed00 | 135 | { |
gandol2 | 5:589ab465ed00 | 136 | tempBuf[iCnt] = (*(sourceBuf + iCnt) - 87); |
gandol2 | 0:9a61123385e9 | 137 | } |
gandol2 | 0:9a61123385e9 | 138 | else // error |
gandol2 | 0:9a61123385e9 | 139 | { |
gandol2 | 0:9a61123385e9 | 140 | ret = -1; |
gandol2 | 5:589ab465ed00 | 141 | return ret; |
gandol2 | 5:589ab465ed00 | 142 | } |
gandol2 | 0:9a61123385e9 | 143 | } |
gandol2 | 5:589ab465ed00 | 144 | |
gandol2 | 0:9a61123385e9 | 145 | //pc_serial.printf("[TEST] tempBuf[0] = 0x%02X\n", tempBuf[0]); |
gandol2 | 0:9a61123385e9 | 146 | //pc_serial.printf("[TEST] tempBuf[1] = 0x%02X\n", tempBuf[1]); |
gandol2 | 0:9a61123385e9 | 147 | *convertedData = ((tempBuf[0] & 0x0F) << 4) | (tempBuf[1] & 0x0F); |
gandol2 | 0:9a61123385e9 | 148 | //pc_serial.printf("[TEST] convertedData = 0x%02X\n", *convertedData); |
gandol2 | 0:9a61123385e9 | 149 | return ret; |
gandol2 | 0:9a61123385e9 | 150 | } |
gandol2 | 0:9a61123385e9 | 151 | |
gandol2 | 5:589ab465ed00 | 152 | /** @brief main function ################################################################### |
gandol2 | 5:589ab465ed00 | 153 | * |
gandol2 | 5:589ab465ed00 | 154 | */ |
gandol2 | 0:9a61123385e9 | 155 | int main() |
gandol2 | 0:9a61123385e9 | 156 | { |
gandol2 | 5:589ab465ed00 | 157 | char inputMenuNum = 0; |
gandol2 | 5:589ab465ed00 | 158 | char statusReg = 0x00; |
gandol2 | 5:589ab465ed00 | 159 | char test_data[10] = { 0 }; |
gandol2 | 5:589ab465ed00 | 160 | |
gandol2 | 5:589ab465ed00 | 161 | char eepAddrBuf[2] = { 0 }; |
gandol2 | 5:589ab465ed00 | 162 | char eepAddr = 0; |
gandol2 | 5:589ab465ed00 | 163 | |
gandol2 | 5:589ab465ed00 | 164 | char eepDataBuf[2] = { 0 }; |
gandol2 | 5:589ab465ed00 | 165 | char eepData = 0; |
gandol2 | 5:589ab465ed00 | 166 | int eepReadData = 0; |
gandol2 | 5:589ab465ed00 | 167 | int maxInputCnt = 0; |
gandol2 | 5:589ab465ed00 | 168 | |
gandol2 | 5:589ab465ed00 | 169 | int ret; |
gandol2 | 5:589ab465ed00 | 170 | |
gandol2 | 5:589ab465ed00 | 171 | SPI_InitMaster(); |
gandol2 | 5:589ab465ed00 | 172 | spi_dummy_write(); |
gandol2 | 5:589ab465ed00 | 173 | |
gandol2 | 5:589ab465ed00 | 174 | pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n"); |
gandol2 | 5:589ab465ed00 | 175 | |
gandol2 | 5:589ab465ed00 | 176 | while (1) |
gandol2 | 5:589ab465ed00 | 177 | { |
gandol2 | 5:589ab465ed00 | 178 | print_menu(); |
gandol2 | 5:589ab465ed00 | 179 | inputMenuNum = pc_serial.getc(); |
gandol2 | 5:589ab465ed00 | 180 | |
gandol2 | 5:589ab465ed00 | 181 | pc_serial.printf("[DEBUG] input menu number = %c\n", inputMenuNum); |
gandol2 | 5:589ab465ed00 | 182 | |
gandol2 | 5:589ab465ed00 | 183 | if (('0' < inputMenuNum) && ('9' >= inputMenuNum)) |
gandol2 | 5:589ab465ed00 | 184 | { |
gandol2 | 0:9a61123385e9 | 185 | inputMenuNum = inputMenuNum - 48; |
gandol2 | 5:589ab465ed00 | 186 | } |
gandol2 | 5:589ab465ed00 | 187 | else |
gandol2 | 5:589ab465ed00 | 188 | { |
gandol2 | 5:589ab465ed00 | 189 | pc_serial.printf("[INFO] correct input menu number.. please retry..\n"); |
gandol2 | 5:589ab465ed00 | 190 | wait(1); |
gandol2 | 5:589ab465ed00 | 191 | continue; |
gandol2 | 5:589ab465ed00 | 192 | } |
gandol2 | 5:589ab465ed00 | 193 | |
gandol2 | 5:589ab465ed00 | 194 | spi_start(); |
gandol2 | 5:589ab465ed00 | 195 | switch (inputMenuNum) |
gandol2 | 5:589ab465ed00 | 196 | { |
gandol2 | 5:589ab465ed00 | 197 | case MENU_EEPROM_Write_Enable: // Write Enable =================================[OK] |
gandol2 | 5:589ab465ed00 | 198 | pc_serial.printf("[DEBUG] Write Enable progress..\n"); |
gandol2 | 5:589ab465ed00 | 199 | spi_master.write(EEPROM_WREN); |
gandol2 | 5:589ab465ed00 | 200 | break; |
gandol2 | 5:589ab465ed00 | 201 | |
gandol2 | 5:589ab465ed00 | 202 | case MENU_EEPROM_Write_Disable: // Write Disable =================================[OK] |
gandol2 | 5:589ab465ed00 | 203 | pc_serial.printf("[DEBUG] Write Disable progress..\n"); |
gandol2 | 5:589ab465ed00 | 204 | spi_master.write(EEPROM_WRDI); |
gandol2 | 5:589ab465ed00 | 205 | break; |
gandol2 | 5:589ab465ed00 | 206 | |
gandol2 | 5:589ab465ed00 | 207 | case MENU_EEPROM_Read_Status: // Read Status Register =================================[OK] |
gandol2 | 5:589ab465ed00 | 208 | pc_serial.printf("[DEBUG] Read Status Register progress..\n"); |
gandol2 | 5:589ab465ed00 | 209 | spi_master.write(EEPROM_RDSR); |
gandol2 | 5:589ab465ed00 | 210 | statusReg = spi_master.write(EEPROM_RDSR); |
gandol2 | 5:589ab465ed00 | 211 | |
gandol2 | 5:589ab465ed00 | 212 | // TODO : print statusReg data to serial |
gandol2 | 5:589ab465ed00 | 213 | pc_serial.printf("[INFO] Status Register Data : 0x%02X\n", statusReg); |
gandol2 | 5:589ab465ed00 | 214 | |
gandol2 | 5:589ab465ed00 | 215 | break; |
gandol2 | 5:589ab465ed00 | 216 | |
gandol2 | 5:589ab465ed00 | 217 | case MENU_EEPROM_Write_Status: // Write Status Register ================================= |
gandol2 | 5:589ab465ed00 | 218 | pc_serial.printf("[DEBUG] Write Status Register progress..\n"); |
gandol2 | 5:589ab465ed00 | 219 | // TODO : input data for serial |
gandol2 | 5:589ab465ed00 | 220 | |
gandol2 | 5:589ab465ed00 | 221 | spi_master.write(EEPROM_WRSR); |
gandol2 | 5:589ab465ed00 | 222 | spi_master.write(statusReg); |
gandol2 | 5:589ab465ed00 | 223 | break; |
gandol2 | 5:589ab465ed00 | 224 | |
gandol2 | 5:589ab465ed00 | 225 | case MENU_EEPROM_Write_Memory: // Write Memory ================================= |
gandol2 | 5:589ab465ed00 | 226 | pc_serial.printf("[DEBUG] Write Memory progress..\n"); |
gandol2 | 5:589ab465ed00 | 227 | |
gandol2 | 5:589ab465ed00 | 228 | // EEPROM Address input |
gandol2 | 5:589ab465ed00 | 229 | |
gandol2 | 5:589ab465ed00 | 230 | maxInputCnt = 3; |
gandol2 | 5:589ab465ed00 | 231 | while (maxInputCnt--) |
gandol2 | 5:589ab465ed00 | 232 | { |
gandol2 | 5:589ab465ed00 | 233 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
gandol2 | 5:589ab465ed00 | 234 | for (int iCnt = 0; iCnt < 2; ++iCnt) |
gandol2 | 5:589ab465ed00 | 235 | { |
gandol2 | 5:589ab465ed00 | 236 | eepAddrBuf[iCnt] = pc_serial.getc(); |
gandol2 | 5:589ab465ed00 | 237 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
gandol2 | 5:589ab465ed00 | 238 | } |
gandol2 | 5:589ab465ed00 | 239 | |
gandol2 | 5:589ab465ed00 | 240 | ret = converHex(&eepAddr, eepAddrBuf, 2); |
gandol2 | 5:589ab465ed00 | 241 | |
gandol2 | 5:589ab465ed00 | 242 | if (0 == ret) |
gandol2 | 5:589ab465ed00 | 243 | { |
gandol2 | 5:589ab465ed00 | 244 | //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); |
gandol2 | 5:589ab465ed00 | 245 | break; |
gandol2 | 5:589ab465ed00 | 246 | } |
gandol2 | 5:589ab465ed00 | 247 | else // error |
gandol2 | 5:589ab465ed00 | 248 | { |
gandol2 | 5:589ab465ed00 | 249 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 5:589ab465ed00 | 250 | continue; |
gandol2 | 5:589ab465ed00 | 251 | } |
gandol2 | 5:589ab465ed00 | 252 | } |
gandol2 | 5:589ab465ed00 | 253 | |
gandol2 | 5:589ab465ed00 | 254 | if (0 == ret) |
gandol2 | 5:589ab465ed00 | 255 | { |
gandol2 | 5:589ab465ed00 | 256 | // EEPROM data input |
gandol2 | 0:9a61123385e9 | 257 | maxInputCnt = 3; |
gandol2 | 5:589ab465ed00 | 258 | while (maxInputCnt--) |
gandol2 | 0:9a61123385e9 | 259 | { |
gandol2 | 5:589ab465ed00 | 260 | pc_serial.printf("\ninput Data (hex format : 00~FF) : "); |
gandol2 | 5:589ab465ed00 | 261 | for (int iCnt = 0; iCnt < 2; ++iCnt) |
gandol2 | 5:589ab465ed00 | 262 | { |
gandol2 | 5:589ab465ed00 | 263 | eepDataBuf[iCnt] = pc_serial.getc(); |
gandol2 | 5:589ab465ed00 | 264 | pc_serial.printf("%c", eepDataBuf[iCnt]); |
gandol2 | 0:9a61123385e9 | 265 | } |
gandol2 | 5:589ab465ed00 | 266 | |
gandol2 | 5:589ab465ed00 | 267 | int ret = converHex(&eepData, eepDataBuf, 2); |
gandol2 | 5:589ab465ed00 | 268 | if (0 == ret) |
gandol2 | 0:9a61123385e9 | 269 | { |
gandol2 | 5:589ab465ed00 | 270 | |
gandol2 | 5:589ab465ed00 | 271 | pc_serial.printf("\n[INFO] SPI Write Memory.... Start! \n"); |
gandol2 | 5:589ab465ed00 | 272 | //pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData); |
gandol2 | 5:589ab465ed00 | 273 | |
gandol2 | 5:589ab465ed00 | 274 | spi_master.write(EEPROM_RDSR); |
gandol2 | 5:589ab465ed00 | 275 | if (!(spi_master.write(EEPROM_RDSR) & 0x01)) |
gandol2 | 5:589ab465ed00 | 276 | { |
gandol2 | 5:589ab465ed00 | 277 | spi_master.write(EEPROM_WRITE); |
gandol2 | 5:589ab465ed00 | 278 | spi_master.write(eepAddr); |
gandol2 | 5:589ab465ed00 | 279 | spi_master.write(eepData); |
gandol2 | 5:589ab465ed00 | 280 | pc_serial.printf("\n[INFO] SPI Write success \n"); |
gandol2 | 5:589ab465ed00 | 281 | } |
gandol2 | 5:589ab465ed00 | 282 | else |
gandol2 | 5:589ab465ed00 | 283 | { |
gandol2 | 5:589ab465ed00 | 284 | pc_serial.printf("\n[INFO] SPI Write fail.. device is busy! \n"); |
gandol2 | 5:589ab465ed00 | 285 | } |
gandol2 | 5:589ab465ed00 | 286 | |
gandol2 | 0:9a61123385e9 | 287 | break; |
gandol2 | 0:9a61123385e9 | 288 | } |
gandol2 | 0:9a61123385e9 | 289 | else // error |
gandol2 | 0:9a61123385e9 | 290 | { |
gandol2 | 0:9a61123385e9 | 291 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 0:9a61123385e9 | 292 | continue; |
gandol2 | 0:9a61123385e9 | 293 | } |
gandol2 | 0:9a61123385e9 | 294 | } |
gandol2 | 5:589ab465ed00 | 295 | } |
gandol2 | 5:589ab465ed00 | 296 | |
gandol2 | 5:589ab465ed00 | 297 | break; |
gandol2 | 5:589ab465ed00 | 298 | |
gandol2 | 5:589ab465ed00 | 299 | case MENU_EEPROM_Read_Memory: // Read Memory ================================= |
gandol2 | 5:589ab465ed00 | 300 | pc_serial.printf("[DEBUG] Read Memory progress..\n"); |
gandol2 | 5:589ab465ed00 | 301 | |
gandol2 | 5:589ab465ed00 | 302 | // EEPROM Address input |
gandol2 | 5:589ab465ed00 | 303 | |
gandol2 | 5:589ab465ed00 | 304 | maxInputCnt = 3; |
gandol2 | 5:589ab465ed00 | 305 | while (maxInputCnt--) |
gandol2 | 5:589ab465ed00 | 306 | { |
gandol2 | 5:589ab465ed00 | 307 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
gandol2 | 5:589ab465ed00 | 308 | for (int iCnt = 0; iCnt < 2; ++iCnt) |
gandol2 | 0:9a61123385e9 | 309 | { |
gandol2 | 5:589ab465ed00 | 310 | eepAddrBuf[iCnt] = pc_serial.getc(); |
gandol2 | 5:589ab465ed00 | 311 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
gandol2 | 0:9a61123385e9 | 312 | } |
gandol2 | 5:589ab465ed00 | 313 | |
gandol2 | 5:589ab465ed00 | 314 | ret = converHex(&eepAddr, eepAddrBuf, 2); |
gandol2 | 5:589ab465ed00 | 315 | |
gandol2 | 5:589ab465ed00 | 316 | if (0 == ret) |
gandol2 | 0:9a61123385e9 | 317 | { |
gandol2 | 5:589ab465ed00 | 318 | //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); |
gandol2 | 5:589ab465ed00 | 319 | spi_master.write(EEPROM_READ); |
gandol2 | 5:589ab465ed00 | 320 | spi_master.write(eepAddr); |
gandol2 | 5:589ab465ed00 | 321 | |
gandol2 | 5:589ab465ed00 | 322 | eepReadData = spi_master.write(0); |
gandol2 | 5:589ab465ed00 | 323 | |
gandol2 | 5:589ab465ed00 | 324 | pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepReadData); |
gandol2 | 5:589ab465ed00 | 325 | break; |
gandol2 | 5:589ab465ed00 | 326 | } |
gandol2 | 5:589ab465ed00 | 327 | else // error |
gandol2 | 5:589ab465ed00 | 328 | { |
gandol2 | 5:589ab465ed00 | 329 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 5:589ab465ed00 | 330 | continue; |
gandol2 | 0:9a61123385e9 | 331 | } |
gandol2 | 5:589ab465ed00 | 332 | } |
gandol2 | 5:589ab465ed00 | 333 | |
gandol2 | 5:589ab465ed00 | 334 | break; |
gandol2 | 5:589ab465ed00 | 335 | |
gandol2 | 5:589ab465ed00 | 336 | case MENU_TEST_Multi_Byte_Read: |
gandol2 | 5:589ab465ed00 | 337 | pc_serial.printf("\n[DEBUG] TEST_Multi_Byte_Read\n"); |
gandol2 | 5:589ab465ed00 | 338 | |
gandol2 | 5:589ab465ed00 | 339 | maxInputCnt = 3; |
gandol2 | 5:589ab465ed00 | 340 | while (maxInputCnt--) |
gandol2 | 5:589ab465ed00 | 341 | { |
gandol2 | 5:589ab465ed00 | 342 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
gandol2 | 5:589ab465ed00 | 343 | for (int iCnt = 0; iCnt < 2; ++iCnt) |
gandol2 | 5:589ab465ed00 | 344 | { |
gandol2 | 5:589ab465ed00 | 345 | eepAddrBuf[iCnt] = pc_serial.getc(); |
gandol2 | 5:589ab465ed00 | 346 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
gandol2 | 5:589ab465ed00 | 347 | } |
gandol2 | 5:589ab465ed00 | 348 | |
gandol2 | 5:589ab465ed00 | 349 | ret = converHex(&eepAddr, eepAddrBuf, 2); |
gandol2 | 5:589ab465ed00 | 350 | |
gandol2 | 5:589ab465ed00 | 351 | if (0 == ret) |
gandol2 | 5:589ab465ed00 | 352 | { |
gandol2 | 5:589ab465ed00 | 353 | spi_master.write(EEPROM_MULTIREAD); |
gandol2 | 5:589ab465ed00 | 354 | spi_master.write(eepAddr); |
gandol2 | 0:9a61123385e9 | 355 | |
gandol2 | 5:589ab465ed00 | 356 | for (int i = 0; i < 5; i++) |
gandol2 | 5:589ab465ed00 | 357 | { |
gandol2 | 5:589ab465ed00 | 358 | wait_ms(10); |
gandol2 | 5:589ab465ed00 | 359 | //test_data[i] = spi_master.write(0); |
gandol2 | 5:589ab465ed00 | 360 | //spi_master.write(++eepAddr); |
gandol2 | 5:589ab465ed00 | 361 | test_data[i] = spi_master.write(0); // dummy write |
gandol2 | 5:589ab465ed00 | 362 | |
gandol2 | 5:589ab465ed00 | 363 | } |
gandol2 | 5:589ab465ed00 | 364 | |
gandol2 | 5:589ab465ed00 | 365 | for (int i = 0; i < 5; i++) |
gandol2 | 5:589ab465ed00 | 366 | { |
gandol2 | 5:589ab465ed00 | 367 | printf("\n[DEBUG] Read EEPROM Data[0x%02X]\n", test_data[i]); |
gandol2 | 5:589ab465ed00 | 368 | } |
gandol2 | 5:589ab465ed00 | 369 | |
gandol2 | 5:589ab465ed00 | 370 | //pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X]\n", eepAddr); |
gandol2 | 5:589ab465ed00 | 371 | break; |
gandol2 | 5:589ab465ed00 | 372 | } |
gandol2 | 5:589ab465ed00 | 373 | else // error |
gandol2 | 5:589ab465ed00 | 374 | { |
gandol2 | 5:589ab465ed00 | 375 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 5:589ab465ed00 | 376 | continue; |
gandol2 | 5:589ab465ed00 | 377 | } |
gandol2 | 5:589ab465ed00 | 378 | } |
gandol2 | 5:589ab465ed00 | 379 | |
gandol2 | 5:589ab465ed00 | 380 | break; |
gandol2 | 5:589ab465ed00 | 381 | |
gandol2 | 5:589ab465ed00 | 382 | case MENU_EXIT_Program: |
gandol2 | 5:589ab465ed00 | 383 | spi_end(); |
gandol2 | 5:589ab465ed00 | 384 | pc_serial.printf("exit program... thank you\n"); |
gandol2 | 5:589ab465ed00 | 385 | //return 0; |
gandol2 | 5:589ab465ed00 | 386 | |
gandol2 | 0:9a61123385e9 | 387 | } |
gandol2 | 5:589ab465ed00 | 388 | spi_end(); |
gandol2 | 5:589ab465ed00 | 389 | |
gandol2 | 0:9a61123385e9 | 390 | } |
gandol2 | 5:589ab465ed00 | 391 | |
gandol2 | 0:9a61123385e9 | 392 | return 0; |
gandol2 | 0:9a61123385e9 | 393 | } |