161007_BDK_EEPROM
Dependencies: mbed
Fork of 161006_BDK_SPImaster by
main.cpp@7:d2710f5d6f66, 2016-10-07 (annotated)
- Committer:
- bcup
- Date:
- Fri Oct 07 08:16:15 2016 +0000
- Revision:
- 7:d2710f5d6f66
- Parent:
- 6:50376de8d756
- Child:
- 8:2599e5a13e05
161007_BDK_EEPROM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcup | 0:24e90e3ca3f4 | 1 | #include "mbed.h" |
gandol2 | 6:50376de8d756 | 2 | #include <SPI.h> |
bcup | 0:24e90e3ca3f4 | 3 | |
bcup | 0:24e90e3ca3f4 | 4 | #define DEBUG_SPI |
bcup | 0:24e90e3ca3f4 | 5 | |
bcup | 0:24e90e3ca3f4 | 6 | #ifdef DEBUG_SPI |
bcup | 0:24e90e3ca3f4 | 7 | #define PRINTD(arg1,arg2...) printf(arg1,##arg2) |
bcup | 0:24e90e3ca3f4 | 8 | #endif |
bcup | 0:24e90e3ca3f4 | 9 | |
gandol2 | 4:a9fa91152c17 | 10 | |
gandol2 | 4:a9fa91152c17 | 11 | #define SPI_MOSI PA_7 |
gandol2 | 4:a9fa91152c17 | 12 | #define SPI_MISO PA_6 |
gandol2 | 4:a9fa91152c17 | 13 | #define SPI_SCLK PA_5 |
gandol2 | 4:a9fa91152c17 | 14 | #define SPI_SSEL PA_4 |
gandol2 | 4:a9fa91152c17 | 15 | |
gandol2 | 4:a9fa91152c17 | 16 | SPI spi_master(SPI_MOSI,SPI_MISO,SPI_SCLK); // MOSI, MISO, SCLK(CLK,SCK) |
gandol2 | 4:a9fa91152c17 | 17 | DigitalOut cs(SPI_SSEL); |
gandol2 | 4:a9fa91152c17 | 18 | |
gandol2 | 4:a9fa91152c17 | 19 | Serial pc_serial(USBTX, USBRX); |
gandol2 | 4:a9fa91152c17 | 20 | |
bcup | 0:24e90e3ca3f4 | 21 | |
bcup | 3:c6f8271b2d4e | 22 | void SPI_INIT() |
bcup | 0:24e90e3ca3f4 | 23 | { |
gandol2 | 6:50376de8d756 | 24 | pc_serial.printf("Set SPI init..\n"); |
gandol2 | 6:50376de8d756 | 25 | pc_serial.printf("Set SPI format..\n"); |
gandol2 | 4:a9fa91152c17 | 26 | spi_master.format(8,0); |
gandol2 | 6:50376de8d756 | 27 | pc_serial.printf("Set frequency to default..\n"); |
bcup | 7:d2710f5d6f66 | 28 | //spi_master.frequency(50000000); // default 1MHz |
bcup | 7:d2710f5d6f66 | 29 | spi_master.frequency(1000000); |
bcup | 0:24e90e3ca3f4 | 30 | } |
gandol2 | 4:a9fa91152c17 | 31 | /* |
bcup | 3:c6f8271b2d4e | 32 | void SPI_Write() |
bcup | 0:24e90e3ca3f4 | 33 | { |
bcup | 3:c6f8271b2d4e | 34 | char temp; |
bcup | 3:c6f8271b2d4e | 35 | int i,value; |
bcup | 3:c6f8271b2d4e | 36 | char response; |
bcup | 3:c6f8271b2d4e | 37 | char tx_cnt = -1; |
bcup | 3:c6f8271b2d4e | 38 | char rx_cnt = -1; |
bcup | 2:5311ad7c83e6 | 39 | char tx_buffer[255]={0}; |
bcup | 3:c6f8271b2d4e | 40 | char rx_buffer[255]={0}; |
bcup | 3:c6f8271b2d4e | 41 | PRINTD("\n==========MASTER==========\n"); |
bcup | 3:c6f8271b2d4e | 42 | PRINTD("DATA SEND START...\n"); |
bcup | 3:c6f8271b2d4e | 43 | PRINTD("Lock SPI BUS..\n"); |
bcup | 3:c6f8271b2d4e | 44 | |
bcup | 0:24e90e3ca3f4 | 45 | while(1) |
bcup | 0:24e90e3ca3f4 | 46 | { |
bcup | 0:24e90e3ca3f4 | 47 | |
bcup | 0:24e90e3ca3f4 | 48 | temp=getchar(); |
bcup | 3:c6f8271b2d4e | 49 | tx_buffer[++tx_cnt]=temp; |
bcup | 0:24e90e3ca3f4 | 50 | if(temp==0x0d) |
bcup | 0:24e90e3ca3f4 | 51 | { |
bcup | 0:24e90e3ca3f4 | 52 | tx_buffer[tx_cnt]=0; |
bcup | 0:24e90e3ca3f4 | 53 | PRINTD("\nData send Finish...\n"); |
bcup | 0:24e90e3ca3f4 | 54 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 55 | { |
bcup | 0:24e90e3ca3f4 | 56 | PRINTD("%c[%02x]",tx_buffer[i],tx_buffer[i]); |
bcup | 0:24e90e3ca3f4 | 57 | } |
bcup | 0:24e90e3ca3f4 | 58 | PRINTD("\n\n"); |
gandol2 | 4:a9fa91152c17 | 59 | spi_master.lock(); |
bcup | 0:24e90e3ca3f4 | 60 | for(i=0;i<=tx_cnt;++i) |
bcup | 0:24e90e3ca3f4 | 61 | { |
bcup | 0:24e90e3ca3f4 | 62 | value=tx_buffer[i]; |
bcup | 3:c6f8271b2d4e | 63 | PRINTD("[M]write[%d]=%c[%02x]\n",i,value,value); |
bcup | 3:c6f8271b2d4e | 64 | cs=0; |
gandol2 | 4:a9fa91152c17 | 65 | response=spi_master.write(value); |
bcup | 3:c6f8271b2d4e | 66 | cs=-1; |
bcup | 3:c6f8271b2d4e | 67 | PRINTD("[M]receive=%c[%x]\n",response,response); |
bcup | 3:c6f8271b2d4e | 68 | rx_buffer[++rx_cnt]=response; |
bcup | 0:24e90e3ca3f4 | 69 | } |
gandol2 | 4:a9fa91152c17 | 70 | spi_master.unlock(); |
bcup | 3:c6f8271b2d4e | 71 | for(i=0;i<255;++i) |
bcup | 0:24e90e3ca3f4 | 72 | { |
bcup | 0:24e90e3ca3f4 | 73 | tx_buffer[i]=0; |
bcup | 0:24e90e3ca3f4 | 74 | } |
bcup | 3:c6f8271b2d4e | 75 | for(i=0;i<=tx_cnt;i++) |
bcup | 3:c6f8271b2d4e | 76 | { |
bcup | 3:c6f8271b2d4e | 77 | PRINTD("init_tx_buffer[%d]=%c\n",i,tx_buffer[i]); |
bcup | 3:c6f8271b2d4e | 78 | } |
bcup | 3:c6f8271b2d4e | 79 | tx_cnt=-1; |
bcup | 0:24e90e3ca3f4 | 80 | } |
bcup | 0:24e90e3ca3f4 | 81 | else |
bcup | 0:24e90e3ca3f4 | 82 | { |
bcup | 0:24e90e3ca3f4 | 83 | PRINTD("%c[%02x]",tx_buffer[tx_cnt],tx_buffer[tx_cnt]); |
bcup | 0:24e90e3ca3f4 | 84 | } |
bcup | 0:24e90e3ca3f4 | 85 | } |
bcup | 0:24e90e3ca3f4 | 86 | } |
gandol2 | 4:a9fa91152c17 | 87 | */ |
bcup | 0:24e90e3ca3f4 | 88 | |
bcup | 7:d2710f5d6f66 | 89 | #define EEPROM_WREN 0x06 // Write Enable |
bcup | 7:d2710f5d6f66 | 90 | #define EEPROM_WRDI 0x04 // Write Disable |
bcup | 7:d2710f5d6f66 | 91 | #define EEPROM_RDSR 0x05 // Read Status Register |
bcup | 7:d2710f5d6f66 | 92 | #define EEPROM_WRSR 0x01 // Write Status Register |
bcup | 7:d2710f5d6f66 | 93 | #define EEPROM_READ 0x03 // Read EEPROM Memory |
bcup | 7:d2710f5d6f66 | 94 | #define EEPROM_WRITE 0x02 // Write EEPROM Memory |
bcup | 7:d2710f5d6f66 | 95 | #define EEPROM_MULTIREAD 0x07 // Write EEPROM Memory |
gandol2 | 6:50376de8d756 | 96 | |
gandol2 | 6:50376de8d756 | 97 | |
gandol2 | 6:50376de8d756 | 98 | enum cubeMenu { EEPROM_Write_Enable = 1 , |
gandol2 | 6:50376de8d756 | 99 | EEPROM_Write_Disable, |
gandol2 | 6:50376de8d756 | 100 | EEPROM_Read_Status, |
gandol2 | 6:50376de8d756 | 101 | EEPROM_Write_Status, |
gandol2 | 6:50376de8d756 | 102 | EEPROM_Write_Memory, |
gandol2 | 6:50376de8d756 | 103 | EEPROM_Read_Memory, |
gandol2 | 6:50376de8d756 | 104 | TEST_Multi_Byte_Read, |
gandol2 | 6:50376de8d756 | 105 | EXIT_Program = 9 |
gandol2 | 6:50376de8d756 | 106 | }; |
gandol2 | 6:50376de8d756 | 107 | |
gandol2 | 6:50376de8d756 | 108 | void print_menu(void) |
gandol2 | 6:50376de8d756 | 109 | { |
gandol2 | 6:50376de8d756 | 110 | pc_serial.printf("\n\n\n\n\n\n\n"); |
gandol2 | 6:50376de8d756 | 111 | pc_serial.printf("-----------[MENU]----------\n"); |
bcup | 7:d2710f5d6f66 | 112 | pc_serial.printf("1) Write Enable\n");//, cubeMenu.EEPROM_Write_Enable); |
bcup | 7:d2710f5d6f66 | 113 | pc_serial.printf("2) Write Disable\n");//, cubeMenu.EEPROM_Write_Disable); |
bcup | 7:d2710f5d6f66 | 114 | pc_serial.printf("3) Read Status Register\n");//, cubeMenu.EEPROM_Read_Status); |
bcup | 7:d2710f5d6f66 | 115 | pc_serial.printf("4) Write Status Register\n");//,cubeMenu.EEPROM_Write_Status); |
bcup | 7:d2710f5d6f66 | 116 | pc_serial.printf("5) Write Memory\n");//, cubeMenu.EEPROM_Write_Memory); |
bcup | 7:d2710f5d6f66 | 117 | pc_serial.printf("6) Read Memory\n");//, cubeMenu.EEPROM_Read_Memory); |
bcup | 7:d2710f5d6f66 | 118 | pc_serial.printf("7) multi byte read\n");//, cubeMenu.TEST_Multi_Byte_Read); |
bcup | 7:d2710f5d6f66 | 119 | pc_serial.printf("9) Exit program\n");//, cubeMenu.EXIT_Program); |
gandol2 | 6:50376de8d756 | 120 | pc_serial.printf("---------------------------\n"); |
gandol2 | 6:50376de8d756 | 121 | pc_serial.printf("input menu : "); |
gandol2 | 6:50376de8d756 | 122 | } |
gandol2 | 6:50376de8d756 | 123 | |
gandol2 | 6:50376de8d756 | 124 | void spi_start(void) |
gandol2 | 6:50376de8d756 | 125 | { |
gandol2 | 6:50376de8d756 | 126 | spi_master.lock(); |
gandol2 | 6:50376de8d756 | 127 | cs = 0; |
gandol2 | 6:50376de8d756 | 128 | wait_us(0); |
gandol2 | 6:50376de8d756 | 129 | } |
gandol2 | 6:50376de8d756 | 130 | void spi_end(void) |
gandol2 | 6:50376de8d756 | 131 | { |
gandol2 | 6:50376de8d756 | 132 | wait_us(10); |
gandol2 | 6:50376de8d756 | 133 | cs = 1; |
gandol2 | 6:50376de8d756 | 134 | spi_master.unlock(); |
gandol2 | 6:50376de8d756 | 135 | } |
gandol2 | 6:50376de8d756 | 136 | void spi_dummy_write(void) |
gandol2 | 6:50376de8d756 | 137 | { |
gandol2 | 6:50376de8d756 | 138 | char dummy_read; |
gandol2 | 6:50376de8d756 | 139 | spi_start(); |
gandol2 | 6:50376de8d756 | 140 | dummy_read = spi_master.write( 0x00 ); |
gandol2 | 6:50376de8d756 | 141 | spi_end(); |
gandol2 | 6:50376de8d756 | 142 | } |
gandol2 | 6:50376de8d756 | 143 | |
gandol2 | 6:50376de8d756 | 144 | /* |
gandol2 | 6:50376de8d756 | 145 | @convertedData : converted Data |
gandol2 | 6:50376de8d756 | 146 | @sourceBuf : ASCII data array |
gandol2 | 6:50376de8d756 | 147 | @lenght : sourceBuf length |
gandol2 | 6:50376de8d756 | 148 | |
gandol2 | 6:50376de8d756 | 149 | @return : 0 is valid hex format, other is error |
gandol2 | 6:50376de8d756 | 150 | */ |
gandol2 | 6:50376de8d756 | 151 | int converHex(char * convertedData, char * sourceBuf, int length) |
gandol2 | 6:50376de8d756 | 152 | { |
gandol2 | 6:50376de8d756 | 153 | int ret = 0; |
gandol2 | 6:50376de8d756 | 154 | char tempBuf[length]; |
gandol2 | 6:50376de8d756 | 155 | *convertedData = 0; |
gandol2 | 6:50376de8d756 | 156 | |
gandol2 | 6:50376de8d756 | 157 | |
gandol2 | 6:50376de8d756 | 158 | for(int iCnt = 0 ; iCnt < length ; iCnt++) |
gandol2 | 6:50376de8d756 | 159 | { |
gandol2 | 6:50376de8d756 | 160 | if( ('0' <= *(sourceBuf+iCnt)) && ('9' >= *(sourceBuf+iCnt)) ) |
gandol2 | 6:50376de8d756 | 161 | { |
gandol2 | 6:50376de8d756 | 162 | tempBuf[iCnt] = (*(sourceBuf+iCnt) - 48); |
gandol2 | 6:50376de8d756 | 163 | } |
gandol2 | 6:50376de8d756 | 164 | else if( ('A' <= *(sourceBuf+iCnt)) && ('F' >= *(sourceBuf+iCnt)) ) |
gandol2 | 6:50376de8d756 | 165 | { |
gandol2 | 6:50376de8d756 | 166 | tempBuf[iCnt] = (*(sourceBuf+iCnt) - 55); |
gandol2 | 6:50376de8d756 | 167 | } |
gandol2 | 6:50376de8d756 | 168 | else if( ('a' <= *(sourceBuf+iCnt)) && ('f' >= *(sourceBuf+iCnt)) ) |
gandol2 | 6:50376de8d756 | 169 | { |
gandol2 | 6:50376de8d756 | 170 | tempBuf[iCnt] = (*(sourceBuf+iCnt) - 87); |
gandol2 | 6:50376de8d756 | 171 | } |
gandol2 | 6:50376de8d756 | 172 | else // error |
gandol2 | 6:50376de8d756 | 173 | { |
gandol2 | 6:50376de8d756 | 174 | ret = -1; |
gandol2 | 6:50376de8d756 | 175 | return ret; |
gandol2 | 6:50376de8d756 | 176 | } |
gandol2 | 6:50376de8d756 | 177 | } |
gandol2 | 6:50376de8d756 | 178 | |
gandol2 | 6:50376de8d756 | 179 | //pc_serial.printf("[TEST] tempBuf[0] = 0x%02X\n", tempBuf[0]); |
gandol2 | 6:50376de8d756 | 180 | //pc_serial.printf("[TEST] tempBuf[1] = 0x%02X\n", tempBuf[1]); |
gandol2 | 6:50376de8d756 | 181 | *convertedData = ((tempBuf[0] & 0x0F) << 4) | (tempBuf[1] & 0x0F); |
gandol2 | 6:50376de8d756 | 182 | //pc_serial.printf("[TEST] convertedData = 0x%02X\n", *convertedData); |
gandol2 | 6:50376de8d756 | 183 | return ret; |
gandol2 | 6:50376de8d756 | 184 | } |
gandol2 | 4:a9fa91152c17 | 185 | |
gandol2 | 4:a9fa91152c17 | 186 | // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ main(); |
bcup | 0:24e90e3ca3f4 | 187 | int main() |
bcup | 0:24e90e3ca3f4 | 188 | { |
gandol2 | 6:50376de8d756 | 189 | char dummyRead; |
gandol2 | 6:50376de8d756 | 190 | char inputMenuNum = 0; |
gandol2 | 6:50376de8d756 | 191 | char statusReg = 0x00; |
bcup | 7:d2710f5d6f66 | 192 | char test_data[10]={0}; |
gandol2 | 6:50376de8d756 | 193 | |
bcup | 5:47353da6bca4 | 194 | int spiTxCnt = 0; |
bcup | 5:47353da6bca4 | 195 | int spiRxCnt = 0; |
gandol2 | 4:a9fa91152c17 | 196 | char spiTxReadyFlag = 0; |
gandol2 | 6:50376de8d756 | 197 | |
gandol2 | 6:50376de8d756 | 198 | |
bcup | 5:47353da6bca4 | 199 | int res; |
gandol2 | 6:50376de8d756 | 200 | |
gandol2 | 6:50376de8d756 | 201 | |
bcup | 5:47353da6bca4 | 202 | int spiRxTempBuf = 0; |
bcup | 5:47353da6bca4 | 203 | char serialRxBuf[255]; |
bcup | 5:47353da6bca4 | 204 | char spiRxBuf[255]; |
bcup | 5:47353da6bca4 | 205 | int serialRxLen = 0; |
gandol2 | 4:a9fa91152c17 | 206 | |
gandol2 | 6:50376de8d756 | 207 | char eepAddrBuf[2] = {0}; |
gandol2 | 6:50376de8d756 | 208 | char eepAddr = 0; |
gandol2 | 4:a9fa91152c17 | 209 | |
gandol2 | 6:50376de8d756 | 210 | char eepDataBuf[2] = {0}; |
gandol2 | 6:50376de8d756 | 211 | char eepData = 0; |
gandol2 | 6:50376de8d756 | 212 | int eepReadData = 0; |
gandol2 | 6:50376de8d756 | 213 | int maxInputCnt = 0; |
gandol2 | 6:50376de8d756 | 214 | |
gandol2 | 6:50376de8d756 | 215 | |
gandol2 | 6:50376de8d756 | 216 | |
gandol2 | 6:50376de8d756 | 217 | int ret; |
gandol2 | 6:50376de8d756 | 218 | |
gandol2 | 6:50376de8d756 | 219 | SPI_INIT(); |
gandol2 | 6:50376de8d756 | 220 | spi_dummy_write(); |
gandol2 | 6:50376de8d756 | 221 | |
gandol2 | 6:50376de8d756 | 222 | |
gandol2 | 6:50376de8d756 | 223 | |
gandol2 | 6:50376de8d756 | 224 | |
gandol2 | 6:50376de8d756 | 225 | pc_serial.printf("\n\n========== SPI Master for EEPROM [Start] ==========\n"); |
gandol2 | 6:50376de8d756 | 226 | |
gandol2 | 6:50376de8d756 | 227 | |
gandol2 | 6:50376de8d756 | 228 | while(1) |
gandol2 | 6:50376de8d756 | 229 | { |
gandol2 | 6:50376de8d756 | 230 | print_menu(); |
gandol2 | 6:50376de8d756 | 231 | inputMenuNum = pc_serial.getc(); |
gandol2 | 6:50376de8d756 | 232 | |
gandol2 | 6:50376de8d756 | 233 | pc_serial.printf("[DEBUG] input menu number = %c\n", inputMenuNum); |
gandol2 | 6:50376de8d756 | 234 | |
gandol2 | 6:50376de8d756 | 235 | if( ('0' < inputMenuNum) && ('9' >= inputMenuNum) ) |
gandol2 | 6:50376de8d756 | 236 | { |
gandol2 | 6:50376de8d756 | 237 | inputMenuNum = inputMenuNum - 48; |
gandol2 | 6:50376de8d756 | 238 | } |
gandol2 | 6:50376de8d756 | 239 | else |
gandol2 | 6:50376de8d756 | 240 | { |
gandol2 | 6:50376de8d756 | 241 | pc_serial.printf("[INFO] correct input menu number.. please retry..\n"); |
gandol2 | 6:50376de8d756 | 242 | wait(1); |
gandol2 | 6:50376de8d756 | 243 | continue; |
gandol2 | 6:50376de8d756 | 244 | } |
gandol2 | 6:50376de8d756 | 245 | |
gandol2 | 6:50376de8d756 | 246 | |
gandol2 | 6:50376de8d756 | 247 | |
gandol2 | 6:50376de8d756 | 248 | spi_start(); |
gandol2 | 6:50376de8d756 | 249 | switch(inputMenuNum) |
gandol2 | 6:50376de8d756 | 250 | { |
gandol2 | 6:50376de8d756 | 251 | case 1: // Write Enable =================================[OK] |
gandol2 | 6:50376de8d756 | 252 | pc_serial.printf("[DEBUG] Write Enable progress..\n"); |
gandol2 | 6:50376de8d756 | 253 | dummyRead = spi_master.write(EEPROM_WREN); |
gandol2 | 6:50376de8d756 | 254 | break; |
gandol2 | 6:50376de8d756 | 255 | |
gandol2 | 6:50376de8d756 | 256 | |
gandol2 | 6:50376de8d756 | 257 | |
gandol2 | 6:50376de8d756 | 258 | case 2: // Write Disable =================================[OK] |
gandol2 | 6:50376de8d756 | 259 | pc_serial.printf("[DEBUG] Write Disable progress..\n"); |
gandol2 | 6:50376de8d756 | 260 | dummyRead = spi_master.write(EEPROM_WRDI); |
gandol2 | 6:50376de8d756 | 261 | break; |
gandol2 | 6:50376de8d756 | 262 | |
gandol2 | 6:50376de8d756 | 263 | |
gandol2 | 6:50376de8d756 | 264 | |
gandol2 | 6:50376de8d756 | 265 | case 3: // Read Status Register =================================[OK] |
gandol2 | 6:50376de8d756 | 266 | pc_serial.printf("[DEBUG] Read Status Register progress..\n"); |
gandol2 | 6:50376de8d756 | 267 | spi_master.write(EEPROM_RDSR); |
gandol2 | 6:50376de8d756 | 268 | statusReg = spi_master.write(EEPROM_RDSR); |
gandol2 | 6:50376de8d756 | 269 | |
gandol2 | 6:50376de8d756 | 270 | // TODO : print statusReg data to serial |
gandol2 | 6:50376de8d756 | 271 | pc_serial.printf("[INFO] Status Register Data : 0x%02X\n", statusReg); |
gandol2 | 6:50376de8d756 | 272 | |
gandol2 | 6:50376de8d756 | 273 | break; |
gandol2 | 6:50376de8d756 | 274 | |
gandol2 | 6:50376de8d756 | 275 | |
gandol2 | 6:50376de8d756 | 276 | |
gandol2 | 6:50376de8d756 | 277 | |
gandol2 | 6:50376de8d756 | 278 | |
gandol2 | 6:50376de8d756 | 279 | case 4: // Write Status Register ================================= |
gandol2 | 6:50376de8d756 | 280 | pc_serial.printf("[DEBUG] Write Status Register progress..\n"); |
gandol2 | 6:50376de8d756 | 281 | // TODO : input data for serial |
gandol2 | 6:50376de8d756 | 282 | |
gandol2 | 6:50376de8d756 | 283 | spi_master.write(EEPROM_WRSR); |
gandol2 | 6:50376de8d756 | 284 | spi_master.write(statusReg); |
gandol2 | 6:50376de8d756 | 285 | break; |
gandol2 | 6:50376de8d756 | 286 | |
gandol2 | 6:50376de8d756 | 287 | |
gandol2 | 6:50376de8d756 | 288 | |
gandol2 | 6:50376de8d756 | 289 | |
gandol2 | 6:50376de8d756 | 290 | |
gandol2 | 6:50376de8d756 | 291 | case 5: // Write Memory ================================= |
gandol2 | 6:50376de8d756 | 292 | pc_serial.printf("[DEBUG] Write Memory progress..\n"); |
gandol2 | 6:50376de8d756 | 293 | |
gandol2 | 6:50376de8d756 | 294 | |
gandol2 | 6:50376de8d756 | 295 | |
gandol2 | 6:50376de8d756 | 296 | |
gandol2 | 6:50376de8d756 | 297 | // EEPROM Address input |
gandol2 | 6:50376de8d756 | 298 | |
gandol2 | 6:50376de8d756 | 299 | maxInputCnt = 3; |
gandol2 | 6:50376de8d756 | 300 | while(maxInputCnt--) |
gandol2 | 6:50376de8d756 | 301 | { |
gandol2 | 6:50376de8d756 | 302 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
gandol2 | 6:50376de8d756 | 303 | for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) |
gandol2 | 6:50376de8d756 | 304 | { |
gandol2 | 6:50376de8d756 | 305 | eepAddrBuf[iCnt] = pc_serial.getc(); |
gandol2 | 6:50376de8d756 | 306 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
gandol2 | 6:50376de8d756 | 307 | } |
gandol2 | 6:50376de8d756 | 308 | |
gandol2 | 6:50376de8d756 | 309 | ret = converHex(&eepAddr , eepAddrBuf, 2); |
gandol2 | 6:50376de8d756 | 310 | |
gandol2 | 6:50376de8d756 | 311 | if(0 == ret) |
gandol2 | 6:50376de8d756 | 312 | { |
gandol2 | 6:50376de8d756 | 313 | //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); |
gandol2 | 6:50376de8d756 | 314 | break; |
gandol2 | 6:50376de8d756 | 315 | } |
gandol2 | 6:50376de8d756 | 316 | else // error |
gandol2 | 6:50376de8d756 | 317 | { |
gandol2 | 6:50376de8d756 | 318 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 6:50376de8d756 | 319 | continue; |
gandol2 | 6:50376de8d756 | 320 | } |
gandol2 | 6:50376de8d756 | 321 | } |
gandol2 | 6:50376de8d756 | 322 | |
gandol2 | 6:50376de8d756 | 323 | |
gandol2 | 6:50376de8d756 | 324 | if(0 == ret) |
gandol2 | 6:50376de8d756 | 325 | { |
gandol2 | 6:50376de8d756 | 326 | // EEPROM data input |
gandol2 | 6:50376de8d756 | 327 | maxInputCnt = 3; |
gandol2 | 6:50376de8d756 | 328 | while(maxInputCnt--) |
gandol2 | 6:50376de8d756 | 329 | { |
gandol2 | 6:50376de8d756 | 330 | pc_serial.printf("\ninput Data (hex format : 00~FF) : "); |
gandol2 | 6:50376de8d756 | 331 | for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) |
gandol2 | 6:50376de8d756 | 332 | { |
gandol2 | 6:50376de8d756 | 333 | eepDataBuf[iCnt] = pc_serial.getc(); |
gandol2 | 6:50376de8d756 | 334 | pc_serial.printf("%c", eepDataBuf[iCnt]); |
gandol2 | 6:50376de8d756 | 335 | } |
gandol2 | 6:50376de8d756 | 336 | |
gandol2 | 6:50376de8d756 | 337 | int ret = converHex(&eepData , eepDataBuf, 2); |
gandol2 | 6:50376de8d756 | 338 | if(0 == ret) |
gandol2 | 6:50376de8d756 | 339 | { |
gandol2 | 6:50376de8d756 | 340 | |
gandol2 | 6:50376de8d756 | 341 | pc_serial.printf("\n[INFO] SPI Write Memory.... Start! \n"); |
gandol2 | 6:50376de8d756 | 342 | //pc_serial.printf("\n[DEBUG] Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepData); |
gandol2 | 6:50376de8d756 | 343 | |
gandol2 | 6:50376de8d756 | 344 | spi_master.write(EEPROM_RDSR); |
gandol2 | 6:50376de8d756 | 345 | if(! (spi_master.write(EEPROM_RDSR) & 0x01) ) |
gandol2 | 6:50376de8d756 | 346 | { |
gandol2 | 6:50376de8d756 | 347 | spi_master.write(EEPROM_WRITE); |
gandol2 | 6:50376de8d756 | 348 | spi_master.write(eepAddr); |
gandol2 | 6:50376de8d756 | 349 | spi_master.write(eepData); |
gandol2 | 6:50376de8d756 | 350 | pc_serial.printf("\n[INFO] SPI Write success \n"); |
gandol2 | 6:50376de8d756 | 351 | } |
gandol2 | 6:50376de8d756 | 352 | else |
gandol2 | 6:50376de8d756 | 353 | { |
gandol2 | 6:50376de8d756 | 354 | pc_serial.printf("\n[INFO] SPI Write fail.. device is busy! \n"); |
gandol2 | 6:50376de8d756 | 355 | } |
gandol2 | 6:50376de8d756 | 356 | |
gandol2 | 6:50376de8d756 | 357 | |
gandol2 | 6:50376de8d756 | 358 | break; |
gandol2 | 6:50376de8d756 | 359 | } |
gandol2 | 6:50376de8d756 | 360 | else // error |
gandol2 | 6:50376de8d756 | 361 | { |
gandol2 | 6:50376de8d756 | 362 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 6:50376de8d756 | 363 | continue; |
gandol2 | 6:50376de8d756 | 364 | } |
gandol2 | 6:50376de8d756 | 365 | } |
gandol2 | 6:50376de8d756 | 366 | } |
gandol2 | 6:50376de8d756 | 367 | |
gandol2 | 6:50376de8d756 | 368 | |
gandol2 | 6:50376de8d756 | 369 | break; |
gandol2 | 6:50376de8d756 | 370 | |
gandol2 | 6:50376de8d756 | 371 | |
gandol2 | 6:50376de8d756 | 372 | |
gandol2 | 6:50376de8d756 | 373 | |
gandol2 | 6:50376de8d756 | 374 | |
gandol2 | 6:50376de8d756 | 375 | case 6: // Read Memory ================================= |
gandol2 | 6:50376de8d756 | 376 | pc_serial.printf("[DEBUG] Read Memory progress..\n"); |
gandol2 | 6:50376de8d756 | 377 | |
gandol2 | 6:50376de8d756 | 378 | // EEPROM Address input |
gandol2 | 6:50376de8d756 | 379 | |
gandol2 | 6:50376de8d756 | 380 | maxInputCnt = 3; |
gandol2 | 6:50376de8d756 | 381 | while(maxInputCnt--) |
gandol2 | 6:50376de8d756 | 382 | { |
gandol2 | 6:50376de8d756 | 383 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
gandol2 | 6:50376de8d756 | 384 | for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) |
gandol2 | 6:50376de8d756 | 385 | { |
gandol2 | 6:50376de8d756 | 386 | eepAddrBuf[iCnt] = pc_serial.getc(); |
gandol2 | 6:50376de8d756 | 387 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
gandol2 | 6:50376de8d756 | 388 | } |
gandol2 | 6:50376de8d756 | 389 | |
gandol2 | 6:50376de8d756 | 390 | ret = converHex(&eepAddr , eepAddrBuf, 2); |
gandol2 | 6:50376de8d756 | 391 | |
gandol2 | 6:50376de8d756 | 392 | if(0 == ret) |
gandol2 | 6:50376de8d756 | 393 | { |
gandol2 | 6:50376de8d756 | 394 | //pc_serial.printf("\n[DEBUG] before convert hex [0x%02x] \n", eepAddr); |
gandol2 | 6:50376de8d756 | 395 | spi_master.write(EEPROM_READ); |
gandol2 | 6:50376de8d756 | 396 | spi_master.write(eepAddr); |
gandol2 | 6:50376de8d756 | 397 | |
gandol2 | 6:50376de8d756 | 398 | |
gandol2 | 6:50376de8d756 | 399 | eepReadData = spi_master.write(0); |
gandol2 | 6:50376de8d756 | 400 | |
gandol2 | 6:50376de8d756 | 401 | |
gandol2 | 6:50376de8d756 | 402 | pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X] Data[0x%02X] \n", eepAddr, eepReadData); |
gandol2 | 6:50376de8d756 | 403 | break; |
gandol2 | 6:50376de8d756 | 404 | } |
gandol2 | 6:50376de8d756 | 405 | else // error |
gandol2 | 6:50376de8d756 | 406 | { |
gandol2 | 6:50376de8d756 | 407 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
gandol2 | 6:50376de8d756 | 408 | continue; |
gandol2 | 6:50376de8d756 | 409 | } |
gandol2 | 6:50376de8d756 | 410 | } |
gandol2 | 6:50376de8d756 | 411 | |
gandol2 | 6:50376de8d756 | 412 | |
gandol2 | 6:50376de8d756 | 413 | |
gandol2 | 6:50376de8d756 | 414 | break; |
gandol2 | 6:50376de8d756 | 415 | |
gandol2 | 6:50376de8d756 | 416 | |
bcup | 7:d2710f5d6f66 | 417 | case 7: |
bcup | 7:d2710f5d6f66 | 418 | pc_serial.printf("\n[DEBUG] TEST_Multi_Byte_Read\n"); |
bcup | 7:d2710f5d6f66 | 419 | |
bcup | 7:d2710f5d6f66 | 420 | maxInputCnt = 3; |
bcup | 7:d2710f5d6f66 | 421 | while(maxInputCnt--) |
bcup | 7:d2710f5d6f66 | 422 | { |
bcup | 7:d2710f5d6f66 | 423 | pc_serial.printf("input Address (hex format : 00~FF) : "); |
bcup | 7:d2710f5d6f66 | 424 | for(int iCnt = 0 ; iCnt < 2 ; ++iCnt) |
bcup | 7:d2710f5d6f66 | 425 | { |
bcup | 7:d2710f5d6f66 | 426 | eepAddrBuf[iCnt] = pc_serial.getc(); |
bcup | 7:d2710f5d6f66 | 427 | pc_serial.printf("%c", eepAddrBuf[iCnt]); |
bcup | 7:d2710f5d6f66 | 428 | } |
bcup | 7:d2710f5d6f66 | 429 | |
bcup | 7:d2710f5d6f66 | 430 | ret = converHex(&eepAddr , eepAddrBuf, 2); |
bcup | 7:d2710f5d6f66 | 431 | |
bcup | 7:d2710f5d6f66 | 432 | if(0 == ret) |
bcup | 7:d2710f5d6f66 | 433 | { |
bcup | 7:d2710f5d6f66 | 434 | spi_master.write(EEPROM_MULTIREAD); |
bcup | 7:d2710f5d6f66 | 435 | spi_master.write(eepAddr); |
bcup | 7:d2710f5d6f66 | 436 | |
bcup | 7:d2710f5d6f66 | 437 | |
bcup | 7:d2710f5d6f66 | 438 | for(int i=0;i<10;i++) |
bcup | 7:d2710f5d6f66 | 439 | { |
bcup | 7:d2710f5d6f66 | 440 | //test_data[i] = spi_master.write(0); |
bcup | 7:d2710f5d6f66 | 441 | //spi_master.write(++eepAddr); |
bcup | 7:d2710f5d6f66 | 442 | test_data[i] = spi_master.write(0); |
bcup | 7:d2710f5d6f66 | 443 | |
bcup | 7:d2710f5d6f66 | 444 | } |
bcup | 7:d2710f5d6f66 | 445 | |
bcup | 7:d2710f5d6f66 | 446 | for(int i=0;i<10;i++) |
bcup | 7:d2710f5d6f66 | 447 | { |
bcup | 7:d2710f5d6f66 | 448 | printf("\n[DEBUG] Read EEPROM Data[0x%02X]\n",test_data[i]); |
bcup | 7:d2710f5d6f66 | 449 | } |
bcup | 7:d2710f5d6f66 | 450 | |
bcup | 7:d2710f5d6f66 | 451 | //pc_serial.printf("\n[DEBUG] Read EEPROM Addr[0x%02X]\n", eepAddr); |
bcup | 7:d2710f5d6f66 | 452 | break; |
bcup | 7:d2710f5d6f66 | 453 | } |
bcup | 7:d2710f5d6f66 | 454 | else // error |
bcup | 7:d2710f5d6f66 | 455 | { |
bcup | 7:d2710f5d6f66 | 456 | pc_serial.printf("\n[INFO] hex formet error!\n"); |
bcup | 7:d2710f5d6f66 | 457 | continue; |
bcup | 7:d2710f5d6f66 | 458 | } |
bcup | 7:d2710f5d6f66 | 459 | } |
bcup | 7:d2710f5d6f66 | 460 | |
bcup | 7:d2710f5d6f66 | 461 | |
bcup | 7:d2710f5d6f66 | 462 | break; |
bcup | 7:d2710f5d6f66 | 463 | |
bcup | 7:d2710f5d6f66 | 464 | |
gandol2 | 6:50376de8d756 | 465 | |
gandol2 | 6:50376de8d756 | 466 | |
gandol2 | 6:50376de8d756 | 467 | |
gandol2 | 6:50376de8d756 | 468 | case 9: |
gandol2 | 6:50376de8d756 | 469 | spi_end(); |
gandol2 | 6:50376de8d756 | 470 | pc_serial.printf("exit program... thank you\n"); |
gandol2 | 6:50376de8d756 | 471 | //return 0; |
gandol2 | 6:50376de8d756 | 472 | default: |
gandol2 | 6:50376de8d756 | 473 | } |
gandol2 | 6:50376de8d756 | 474 | spi_end(); |
gandol2 | 6:50376de8d756 | 475 | |
gandol2 | 6:50376de8d756 | 476 | } |
gandol2 | 6:50376de8d756 | 477 | |
gandol2 | 6:50376de8d756 | 478 | |
gandol2 | 6:50376de8d756 | 479 | |
gandol2 | 6:50376de8d756 | 480 | |
gandol2 | 6:50376de8d756 | 481 | |
gandol2 | 6:50376de8d756 | 482 | |
gandol2 | 6:50376de8d756 | 483 | |
gandol2 | 6:50376de8d756 | 484 | |
gandol2 | 6:50376de8d756 | 485 | |
gandol2 | 6:50376de8d756 | 486 | |
gandol2 | 6:50376de8d756 | 487 | |
gandol2 | 6:50376de8d756 | 488 | |
gandol2 | 6:50376de8d756 | 489 | |
gandol2 | 6:50376de8d756 | 490 | |
gandol2 | 6:50376de8d756 | 491 | |
gandol2 | 6:50376de8d756 | 492 | |
gandol2 | 6:50376de8d756 | 493 | |
gandol2 | 6:50376de8d756 | 494 | |
gandol2 | 6:50376de8d756 | 495 | |
gandol2 | 6:50376de8d756 | 496 | |
gandol2 | 6:50376de8d756 | 497 | |
gandol2 | 6:50376de8d756 | 498 | |
gandol2 | 6:50376de8d756 | 499 | |
gandol2 | 6:50376de8d756 | 500 | |
gandol2 | 6:50376de8d756 | 501 | |
gandol2 | 6:50376de8d756 | 502 | |
gandol2 | 6:50376de8d756 | 503 | |
gandol2 | 4:a9fa91152c17 | 504 | |
gandol2 | 4:a9fa91152c17 | 505 | |
gandol2 | 4:a9fa91152c17 | 506 | while(1) |
gandol2 | 4:a9fa91152c17 | 507 | { |
gandol2 | 4:a9fa91152c17 | 508 | if(0 != pc_serial.readable()) // wait serial input.. |
gandol2 | 4:a9fa91152c17 | 509 | { |
gandol2 | 4:a9fa91152c17 | 510 | pc_serial.scanf("%s", serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 511 | serialRxLen = strlen(serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 512 | pc_serial.printf("len=[%d] %s\n", serialRxLen, serialRxBuf); |
gandol2 | 4:a9fa91152c17 | 513 | spiTxReadyFlag = 1; |
gandol2 | 4:a9fa91152c17 | 514 | } |
gandol2 | 4:a9fa91152c17 | 515 | if(1 == spiTxReadyFlag) |
gandol2 | 4:a9fa91152c17 | 516 | { |
gandol2 | 4:a9fa91152c17 | 517 | // SPI Send Start |
gandol2 | 4:a9fa91152c17 | 518 | spi_master.lock(); |
gandol2 | 4:a9fa91152c17 | 519 | cs = 0; |
gandol2 | 4:a9fa91152c17 | 520 | for(spiTxCnt = 0 ; spiTxCnt < serialRxLen + 1 ; ++spiTxCnt) |
gandol2 | 4:a9fa91152c17 | 521 | { |
gandol2 | 4:a9fa91152c17 | 522 | //printf("send Cnt[%d] [0x%02X]\n", spiTxCnt, serialRxBuf[spiTxCnt]); |
gandol2 | 6:50376de8d756 | 523 | spi_master.write(serialRxBuf[spiTxCnt]); |
gandol2 | 6:50376de8d756 | 524 | //res=spi_master.write(serialRxBuf[spiTxCnt]); |
gandol2 | 6:50376de8d756 | 525 | //PRINTD("res=%c 0x[%02x]\n",res,res); |
gandol2 | 6:50376de8d756 | 526 | //spiRxBuf[spiRxCnt++]=res; |
gandol2 | 4:a9fa91152c17 | 527 | } |
gandol2 | 4:a9fa91152c17 | 528 | cs = 1; |
gandol2 | 4:a9fa91152c17 | 529 | spi_master.unlock(); |
gandol2 | 4:a9fa91152c17 | 530 | spiTxReadyFlag = 0; |
gandol2 | 4:a9fa91152c17 | 531 | } |
gandol2 | 4:a9fa91152c17 | 532 | |
gandol2 | 4:a9fa91152c17 | 533 | |
gandol2 | 4:a9fa91152c17 | 534 | |
bcup | 5:47353da6bca4 | 535 | /* TODO " ----(SPI)-----> master --> serial " */ |
bcup | 5:47353da6bca4 | 536 | |
gandol2 | 4:a9fa91152c17 | 537 | |
bcup | 5:47353da6bca4 | 538 | #if 0 |
bcup | 5:47353da6bca4 | 539 | if(res!=0) |
bcup | 5:47353da6bca4 | 540 | { |
bcup | 5:47353da6bca4 | 541 | PRINTD("res=%d\n",res); |
bcup | 5:47353da6bca4 | 542 | PRINTD("res=%s\n",spiRxBuf); |
bcup | 5:47353da6bca4 | 543 | } |
bcup | 5:47353da6bca4 | 544 | #endif |
bcup | 5:47353da6bca4 | 545 | |
bcup | 5:47353da6bca4 | 546 | |
gandol2 | 4:a9fa91152c17 | 547 | } |
gandol2 | 4:a9fa91152c17 | 548 | |
gandol2 | 4:a9fa91152c17 | 549 | |
gandol2 | 4:a9fa91152c17 | 550 | |
gandol2 | 4:a9fa91152c17 | 551 | |
gandol2 | 4:a9fa91152c17 | 552 | |
gandol2 | 4:a9fa91152c17 | 553 | |
gandol2 | 4:a9fa91152c17 | 554 | |
gandol2 | 4:a9fa91152c17 | 555 | |
gandol2 | 4:a9fa91152c17 | 556 | |
gandol2 | 4:a9fa91152c17 | 557 | |
gandol2 | 4:a9fa91152c17 | 558 | |
gandol2 | 4:a9fa91152c17 | 559 | |
gandol2 | 4:a9fa91152c17 | 560 | |
gandol2 | 4:a9fa91152c17 | 561 | |
gandol2 | 4:a9fa91152c17 | 562 | |
gandol2 | 4:a9fa91152c17 | 563 | |
gandol2 | 4:a9fa91152c17 | 564 | |
gandol2 | 4:a9fa91152c17 | 565 | |
gandol2 | 4:a9fa91152c17 | 566 | |
gandol2 | 4:a9fa91152c17 | 567 | |
gandol2 | 4:a9fa91152c17 | 568 | |
gandol2 | 4:a9fa91152c17 | 569 | |
gandol2 | 4:a9fa91152c17 | 570 | #if 0 // 161005_BDK_backup2 start |
bcup | 3:c6f8271b2d4e | 571 | int send_data; |
gandol2 | 4:a9fa91152c17 | 572 | SPI_INIT(); |
bcup | 0:24e90e3ca3f4 | 573 | while(1) |
bcup | 0:24e90e3ca3f4 | 574 | { |
bcup | 3:c6f8271b2d4e | 575 | SPI_Write(); |
bcup | 3:c6f8271b2d4e | 576 | } |
gandol2 | 4:a9fa91152c17 | 577 | #endif // 161005_BDK_backup2 end |
gandol2 | 4:a9fa91152c17 | 578 | return 0; |
bcup | 3:c6f8271b2d4e | 579 | } |