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