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