![](/media/cache/profiles/a7921ea22e084f4c3f660b30749325c3.jpg.50x50_q85.jpg)
mille-feuille board base MCU program
main.cpp@0:9dc31a0e43ac, 23 months ago (annotated)
- Committer:
- Info
- Date:
- Tue Sep 06 04:42:04 2022 +0000
- Revision:
- 0:9dc31a0e43ac
open
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Info | 0:9dc31a0e43ac | 1 | #include "mbed.h" |
Info | 0:9dc31a0e43ac | 2 | #include "EEPROM24LC01.h" |
Info | 0:9dc31a0e43ac | 3 | |
Info | 0:9dc31a0e43ac | 4 | //#define ROMtest 1 |
Info | 0:9dc31a0e43ac | 5 | //#define debug 1 |
Info | 0:9dc31a0e43ac | 6 | //#define debug2 1 |
Info | 0:9dc31a0e43ac | 7 | //#define test 1 |
Info | 0:9dc31a0e43ac | 8 | |
Info | 0:9dc31a0e43ac | 9 | #define def_myAddress 0x80000000 |
Info | 0:9dc31a0e43ac | 10 | //#define SPIfreq 2000000 |
Info | 0:9dc31a0e43ac | 11 | ////#define BaseSPIfreq 4170000 |
Info | 0:9dc31a0e43ac | 12 | #define BaseSPIfreq 17000000 |
Info | 0:9dc31a0e43ac | 13 | ////#define BaseSPIfreq 8000000 |
Info | 0:9dc31a0e43ac | 14 | |
Info | 0:9dc31a0e43ac | 15 | #define MAX_INOUT 16 |
Info | 0:9dc31a0e43ac | 16 | #define MAX_BANK 20 |
Info | 0:9dc31a0e43ac | 17 | |
Info | 0:9dc31a0e43ac | 18 | #define NOTHOLD 0 |
Info | 0:9dc31a0e43ac | 19 | #define HOLD_10 1 |
Info | 0:9dc31a0e43ac | 20 | #define HOLD_01 2 |
Info | 0:9dc31a0e43ac | 21 | #define ALL_HOLD 3 |
Info | 0:9dc31a0e43ac | 22 | |
Info | 0:9dc31a0e43ac | 23 | #ifdef debug2 |
Info | 0:9dc31a0e43ac | 24 | //Serial pc(USBTX, USBRX); |
Info | 0:9dc31a0e43ac | 25 | Serial pc(P0_4, P0_0); |
Info | 0:9dc31a0e43ac | 26 | #endif |
Info | 0:9dc31a0e43ac | 27 | |
Info | 0:9dc31a0e43ac | 28 | I2C i2c(I2C_SDA,I2C_SCL); // sda, scl |
Info | 0:9dc31a0e43ac | 29 | EEPROM24LC01 eeprom(&i2c, 0x50); |
Info | 0:9dc31a0e43ac | 30 | |
Info | 0:9dc31a0e43ac | 31 | DigitalOut addr0(P0_8); |
Info | 0:9dc31a0e43ac | 32 | DigitalOut addr1(P0_13); |
Info | 0:9dc31a0e43ac | 33 | DigitalOut addr2(P0_9); |
Info | 0:9dc31a0e43ac | 34 | DigitalOut addr3(P0_27); |
Info | 0:9dc31a0e43ac | 35 | DigitalOut addr4(P0_14); |
Info | 0:9dc31a0e43ac | 36 | |
Info | 0:9dc31a0e43ac | 37 | DigitalOut WData0(P0_21); |
Info | 0:9dc31a0e43ac | 38 | //DigitalOut WData1(P0_7);///ERRROR |
Info | 0:9dc31a0e43ac | 39 | DigitalOut WData1(P0_17);//change |
Info | 0:9dc31a0e43ac | 40 | DigitalOut WData2(P0_22); |
Info | 0:9dc31a0e43ac | 41 | DigitalOut WData3(P0_20); |
Info | 0:9dc31a0e43ac | 42 | DigitalOut WData4(P0_19); |
Info | 0:9dc31a0e43ac | 43 | |
Info | 0:9dc31a0e43ac | 44 | //DigitalOut WLatch(P0_18);//ERROR |
Info | 0:9dc31a0e43ac | 45 | DigitalOut WLatch(P0_23);//change |
Info | 0:9dc31a0e43ac | 46 | |
Info | 0:9dc31a0e43ac | 47 | /* |
Info | 0:9dc31a0e43ac | 48 | DigitalIn RData0(P0_14); |
Info | 0:9dc31a0e43ac | 49 | //DigitalIn RData1(P0_23); |
Info | 0:9dc31a0e43ac | 50 | DigitalIn RData1(P0_18);//change |
Info | 0:9dc31a0e43ac | 51 | DigitalIn RData2(P0_28); |
Info | 0:9dc31a0e43ac | 52 | DigitalIn RData3(P0_11); |
Info | 0:9dc31a0e43ac | 53 | DigitalIn RData4(P0_9); |
Info | 0:9dc31a0e43ac | 54 | |
Info | 0:9dc31a0e43ac | 55 | DigitalOut RLatch(P0_6); |
Info | 0:9dc31a0e43ac | 56 | */ |
Info | 0:9dc31a0e43ac | 57 | |
Info | 0:9dc31a0e43ac | 58 | DigitalOut myled(P0_16,0); |
Info | 0:9dc31a0e43ac | 59 | SPISlave device(P0_26, P0_25, P0_24, P0_15); // mosi, miso, sclk, ssel |
Info | 0:9dc31a0e43ac | 60 | DigitalOut MRESET(P0_1); |
Info | 0:9dc31a0e43ac | 61 | |
Info | 0:9dc31a0e43ac | 62 | DigitalInOut myIO1(P0_6); |
Info | 0:9dc31a0e43ac | 63 | DigitalInOut myIO2(P0_28); |
Info | 0:9dc31a0e43ac | 64 | |
Info | 0:9dc31a0e43ac | 65 | #ifdef test |
Info | 0:9dc31a0e43ac | 66 | DigitalIn iAddress1(P0_15); |
Info | 0:9dc31a0e43ac | 67 | #endif |
Info | 0:9dc31a0e43ac | 68 | |
Info | 0:9dc31a0e43ac | 69 | uint8_t address[4]; |
Info | 0:9dc31a0e43ac | 70 | uint8_t exData[3]; |
Info | 0:9dc31a0e43ac | 71 | uint8_t gateData; |
Info | 0:9dc31a0e43ac | 72 | uint32_t mod_myAddress; |
Info | 0:9dc31a0e43ac | 73 | |
Info | 0:9dc31a0e43ac | 74 | |
Info | 0:9dc31a0e43ac | 75 | |
Info | 0:9dc31a0e43ac | 76 | #ifdef ROMtest |
Info | 0:9dc31a0e43ac | 77 | void EEPROMtest(void){ |
Info | 0:9dc31a0e43ac | 78 | char bufData[10]; |
Info | 0:9dc31a0e43ac | 79 | |
Info | 0:9dc31a0e43ac | 80 | printf("write test\n\r"); |
Info | 0:9dc31a0e43ac | 81 | |
Info | 0:9dc31a0e43ac | 82 | bufData[0]=0xC0;//address1 |
Info | 0:9dc31a0e43ac | 83 | bufData[1]=0x00;//address2 |
Info | 0:9dc31a0e43ac | 84 | bufData[2]=0x00;//address3 |
Info | 0:9dc31a0e43ac | 85 | bufData[3]=0x01;//address4 |
Info | 0:9dc31a0e43ac | 86 | bufData[4]=0x04;//extra data |
Info | 0:9dc31a0e43ac | 87 | bufData[5]=0x05;//extra data |
Info | 0:9dc31a0e43ac | 88 | bufData[6]=0x06;//extra data |
Info | 0:9dc31a0e43ac | 89 | bufData[7]=0x07;//gate open pattern number |
Info | 0:9dc31a0e43ac | 90 | int res = eeprom.byte8_write( bufData); |
Info | 0:9dc31a0e43ac | 91 | //printf("write res = %d\r\n",res); |
Info | 0:9dc31a0e43ac | 92 | |
Info | 0:9dc31a0e43ac | 93 | bufData[0]=0xAA; |
Info | 0:9dc31a0e43ac | 94 | |
Info | 0:9dc31a0e43ac | 95 | printf("end\n\r"); |
Info | 0:9dc31a0e43ac | 96 | |
Info | 0:9dc31a0e43ac | 97 | |
Info | 0:9dc31a0e43ac | 98 | bufData[0]=0; |
Info | 0:9dc31a0e43ac | 99 | bufData[1]=0; |
Info | 0:9dc31a0e43ac | 100 | bufData[2]=0; |
Info | 0:9dc31a0e43ac | 101 | bufData[3]=0; |
Info | 0:9dc31a0e43ac | 102 | bufData[4]=0; |
Info | 0:9dc31a0e43ac | 103 | bufData[5]=0; |
Info | 0:9dc31a0e43ac | 104 | bufData[6]=0; |
Info | 0:9dc31a0e43ac | 105 | bufData[7]=0; |
Info | 0:9dc31a0e43ac | 106 | |
Info | 0:9dc31a0e43ac | 107 | |
Info | 0:9dc31a0e43ac | 108 | eeprom.readAll( bufData, 8 ); |
Info | 0:9dc31a0e43ac | 109 | |
Info | 0:9dc31a0e43ac | 110 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:9dc31a0e43ac | 111 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:9dc31a0e43ac | 112 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:9dc31a0e43ac | 113 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:9dc31a0e43ac | 114 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:9dc31a0e43ac | 115 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:9dc31a0e43ac | 116 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:9dc31a0e43ac | 117 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:9dc31a0e43ac | 118 | |
Info | 0:9dc31a0e43ac | 119 | printf("\n\r Read again! \n\r\n\r"); |
Info | 0:9dc31a0e43ac | 120 | |
Info | 0:9dc31a0e43ac | 121 | |
Info | 0:9dc31a0e43ac | 122 | eeprom.readAll( bufData, 8 ); |
Info | 0:9dc31a0e43ac | 123 | |
Info | 0:9dc31a0e43ac | 124 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:9dc31a0e43ac | 125 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:9dc31a0e43ac | 126 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:9dc31a0e43ac | 127 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:9dc31a0e43ac | 128 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:9dc31a0e43ac | 129 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:9dc31a0e43ac | 130 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:9dc31a0e43ac | 131 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:9dc31a0e43ac | 132 | |
Info | 0:9dc31a0e43ac | 133 | printf("\nend\n\r"); |
Info | 0:9dc31a0e43ac | 134 | |
Info | 0:9dc31a0e43ac | 135 | |
Info | 0:9dc31a0e43ac | 136 | while(1); |
Info | 0:9dc31a0e43ac | 137 | } |
Info | 0:9dc31a0e43ac | 138 | #endif |
Info | 0:9dc31a0e43ac | 139 | |
Info | 0:9dc31a0e43ac | 140 | void ReadDataFromEEPROM(void){ |
Info | 0:9dc31a0e43ac | 141 | int i,res; |
Info | 0:9dc31a0e43ac | 142 | char bufData[8]; |
Info | 0:9dc31a0e43ac | 143 | //char buf; |
Info | 0:9dc31a0e43ac | 144 | //char buf1,buf2; |
Info | 0:9dc31a0e43ac | 145 | |
Info | 0:9dc31a0e43ac | 146 | res = eeprom.readAll( bufData, 8 ); |
Info | 0:9dc31a0e43ac | 147 | if(res == 0){ |
Info | 0:9dc31a0e43ac | 148 | #ifdef debug |
Info | 0:9dc31a0e43ac | 149 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:9dc31a0e43ac | 150 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:9dc31a0e43ac | 151 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:9dc31a0e43ac | 152 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:9dc31a0e43ac | 153 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:9dc31a0e43ac | 154 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:9dc31a0e43ac | 155 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:9dc31a0e43ac | 156 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:9dc31a0e43ac | 157 | #endif |
Info | 0:9dc31a0e43ac | 158 | |
Info | 0:9dc31a0e43ac | 159 | for(i=0;i<4;++i){ |
Info | 0:9dc31a0e43ac | 160 | address[i] = bufData[i]; |
Info | 0:9dc31a0e43ac | 161 | mod_myAddress = (mod_myAddress<<8) + bufData[i]; |
Info | 0:9dc31a0e43ac | 162 | #ifdef debug |
Info | 0:9dc31a0e43ac | 163 | printf("address%d : %02x\n\r",i,address[i]); |
Info | 0:9dc31a0e43ac | 164 | #endif |
Info | 0:9dc31a0e43ac | 165 | } |
Info | 0:9dc31a0e43ac | 166 | #ifdef debug |
Info | 0:9dc31a0e43ac | 167 | printf("mod_myAddress %08x\n\r",mod_myAddress); |
Info | 0:9dc31a0e43ac | 168 | #endif |
Info | 0:9dc31a0e43ac | 169 | |
Info | 0:9dc31a0e43ac | 170 | for(i=4;i<7;++i){ |
Info | 0:9dc31a0e43ac | 171 | exData[i-4] = bufData[i]; |
Info | 0:9dc31a0e43ac | 172 | #ifdef debug |
Info | 0:9dc31a0e43ac | 173 | printf("exData%d : %02x\n\r",i-4,exData[i-4]); |
Info | 0:9dc31a0e43ac | 174 | #endif |
Info | 0:9dc31a0e43ac | 175 | } |
Info | 0:9dc31a0e43ac | 176 | |
Info | 0:9dc31a0e43ac | 177 | gateData = bufData[7]; |
Info | 0:9dc31a0e43ac | 178 | #ifdef debug |
Info | 0:9dc31a0e43ac | 179 | printf("gateData : %02x\n\r",gateData); |
Info | 0:9dc31a0e43ac | 180 | #endif |
Info | 0:9dc31a0e43ac | 181 | |
Info | 0:9dc31a0e43ac | 182 | return; |
Info | 0:9dc31a0e43ac | 183 | |
Info | 0:9dc31a0e43ac | 184 | }else if(res == 1){ |
Info | 0:9dc31a0e43ac | 185 | for(i=0;i<4;++i){ |
Info | 0:9dc31a0e43ac | 186 | address[i] = 0; |
Info | 0:9dc31a0e43ac | 187 | } |
Info | 0:9dc31a0e43ac | 188 | return; |
Info | 0:9dc31a0e43ac | 189 | } |
Info | 0:9dc31a0e43ac | 190 | } |
Info | 0:9dc31a0e43ac | 191 | |
Info | 0:9dc31a0e43ac | 192 | |
Info | 0:9dc31a0e43ac | 193 | |
Info | 0:9dc31a0e43ac | 194 | |
Info | 0:9dc31a0e43ac | 195 | #ifdef test |
Info | 0:9dc31a0e43ac | 196 | void IoTest(void){ |
Info | 0:9dc31a0e43ac | 197 | int inData[2]; |
Info | 0:9dc31a0e43ac | 198 | int inAddr1 = 0; |
Info | 0:9dc31a0e43ac | 199 | |
Info | 0:9dc31a0e43ac | 200 | myIO1.input(); |
Info | 0:9dc31a0e43ac | 201 | myIO2.input(); |
Info | 0:9dc31a0e43ac | 202 | |
Info | 0:9dc31a0e43ac | 203 | printf("IoTest\r\n"); |
Info | 0:9dc31a0e43ac | 204 | |
Info | 0:9dc31a0e43ac | 205 | while(1){ |
Info | 0:9dc31a0e43ac | 206 | |
Info | 0:9dc31a0e43ac | 207 | inAddr1 = iAddress1; |
Info | 0:9dc31a0e43ac | 208 | if(inAddr1 == 0){ |
Info | 0:9dc31a0e43ac | 209 | printf("myIOs input\r\n"); |
Info | 0:9dc31a0e43ac | 210 | myIO1.input(); |
Info | 0:9dc31a0e43ac | 211 | myIO2.input(); |
Info | 0:9dc31a0e43ac | 212 | myIO1.mode(PullNone); |
Info | 0:9dc31a0e43ac | 213 | myIO2.mode(PullNone); |
Info | 0:9dc31a0e43ac | 214 | inData[0] = myIO1; |
Info | 0:9dc31a0e43ac | 215 | inData[1] = myIO2; |
Info | 0:9dc31a0e43ac | 216 | printf("inData[0] = %d\r\n",inData[0]); |
Info | 0:9dc31a0e43ac | 217 | printf("inData[1] = %d\r\n",inData[1]); |
Info | 0:9dc31a0e43ac | 218 | }else{ |
Info | 0:9dc31a0e43ac | 219 | printf("myIOs output\r\n"); |
Info | 0:9dc31a0e43ac | 220 | myIO1.output(); |
Info | 0:9dc31a0e43ac | 221 | myIO2.output(); |
Info | 0:9dc31a0e43ac | 222 | myIO1.mode(PullUp); |
Info | 0:9dc31a0e43ac | 223 | myIO2.mode(PullUp); |
Info | 0:9dc31a0e43ac | 224 | myIO1 = 1; |
Info | 0:9dc31a0e43ac | 225 | myIO2 = 1; |
Info | 0:9dc31a0e43ac | 226 | } |
Info | 0:9dc31a0e43ac | 227 | |
Info | 0:9dc31a0e43ac | 228 | |
Info | 0:9dc31a0e43ac | 229 | |
Info | 0:9dc31a0e43ac | 230 | wait(1); |
Info | 0:9dc31a0e43ac | 231 | |
Info | 0:9dc31a0e43ac | 232 | if(inAddr1==1){ |
Info | 0:9dc31a0e43ac | 233 | myIO1 = 0; |
Info | 0:9dc31a0e43ac | 234 | myIO2 = 0; |
Info | 0:9dc31a0e43ac | 235 | } |
Info | 0:9dc31a0e43ac | 236 | |
Info | 0:9dc31a0e43ac | 237 | |
Info | 0:9dc31a0e43ac | 238 | wait(1); |
Info | 0:9dc31a0e43ac | 239 | |
Info | 0:9dc31a0e43ac | 240 | } |
Info | 0:9dc31a0e43ac | 241 | |
Info | 0:9dc31a0e43ac | 242 | } |
Info | 0:9dc31a0e43ac | 243 | |
Info | 0:9dc31a0e43ac | 244 | #endif |
Info | 0:9dc31a0e43ac | 245 | |
Info | 0:9dc31a0e43ac | 246 | |
Info | 0:9dc31a0e43ac | 247 | void holdIOs(int select, int readData){ |
Info | 0:9dc31a0e43ac | 248 | |
Info | 0:9dc31a0e43ac | 249 | if(select == 1){ |
Info | 0:9dc31a0e43ac | 250 | myIO1.mode(PullDown); |
Info | 0:9dc31a0e43ac | 251 | //hold |
Info | 0:9dc31a0e43ac | 252 | myIO1.output(); |
Info | 0:9dc31a0e43ac | 253 | myIO1.mode(PullUp); |
Info | 0:9dc31a0e43ac | 254 | myIO1 = readData; |
Info | 0:9dc31a0e43ac | 255 | |
Info | 0:9dc31a0e43ac | 256 | }else if(select == 2){ |
Info | 0:9dc31a0e43ac | 257 | myIO2.mode(PullDown); |
Info | 0:9dc31a0e43ac | 258 | //hold |
Info | 0:9dc31a0e43ac | 259 | myIO2.output(); |
Info | 0:9dc31a0e43ac | 260 | myIO2.mode(PullUp); |
Info | 0:9dc31a0e43ac | 261 | myIO2 = readData; |
Info | 0:9dc31a0e43ac | 262 | |
Info | 0:9dc31a0e43ac | 263 | } |
Info | 0:9dc31a0e43ac | 264 | |
Info | 0:9dc31a0e43ac | 265 | } |
Info | 0:9dc31a0e43ac | 266 | |
Info | 0:9dc31a0e43ac | 267 | void holdeMode(int data){ |
Info | 0:9dc31a0e43ac | 268 | int inData[2]; |
Info | 0:9dc31a0e43ac | 269 | |
Info | 0:9dc31a0e43ac | 270 | myIO1.input(); |
Info | 0:9dc31a0e43ac | 271 | myIO2.input(); |
Info | 0:9dc31a0e43ac | 272 | //read |
Info | 0:9dc31a0e43ac | 273 | inData[0] = myIO1; |
Info | 0:9dc31a0e43ac | 274 | inData[1] = myIO2; |
Info | 0:9dc31a0e43ac | 275 | //printf("inData[0]=%d\r\n",inData[0]); |
Info | 0:9dc31a0e43ac | 276 | //printf("inData[1]=%d\r\n",inData[1]); |
Info | 0:9dc31a0e43ac | 277 | |
Info | 0:9dc31a0e43ac | 278 | if(data == NOTHOLD){ |
Info | 0:9dc31a0e43ac | 279 | //printf("NOTHOLD\r\n"); |
Info | 0:9dc31a0e43ac | 280 | myIO1.mode(PullNone); |
Info | 0:9dc31a0e43ac | 281 | myIO2.mode(PullNone); |
Info | 0:9dc31a0e43ac | 282 | |
Info | 0:9dc31a0e43ac | 283 | myled = 0; |
Info | 0:9dc31a0e43ac | 284 | |
Info | 0:9dc31a0e43ac | 285 | }else if(data == HOLD_10){ |
Info | 0:9dc31a0e43ac | 286 | //printf("HOLD_10\r\n"); |
Info | 0:9dc31a0e43ac | 287 | holdIOs(1,inData[0]); |
Info | 0:9dc31a0e43ac | 288 | |
Info | 0:9dc31a0e43ac | 289 | myled = 1; |
Info | 0:9dc31a0e43ac | 290 | |
Info | 0:9dc31a0e43ac | 291 | }else if(data == HOLD_01){ |
Info | 0:9dc31a0e43ac | 292 | //printf("HOLD_01\r\n"); |
Info | 0:9dc31a0e43ac | 293 | holdIOs(2,inData[1]); |
Info | 0:9dc31a0e43ac | 294 | |
Info | 0:9dc31a0e43ac | 295 | myled = 1; |
Info | 0:9dc31a0e43ac | 296 | |
Info | 0:9dc31a0e43ac | 297 | }else if(data == ALL_HOLD){ |
Info | 0:9dc31a0e43ac | 298 | //printf("ALL_HOLD\r\n"); |
Info | 0:9dc31a0e43ac | 299 | holdIOs(1,inData[0]); |
Info | 0:9dc31a0e43ac | 300 | holdIOs(2,inData[1]); |
Info | 0:9dc31a0e43ac | 301 | |
Info | 0:9dc31a0e43ac | 302 | myled = 1; |
Info | 0:9dc31a0e43ac | 303 | |
Info | 0:9dc31a0e43ac | 304 | } |
Info | 0:9dc31a0e43ac | 305 | } |
Info | 0:9dc31a0e43ac | 306 | |
Info | 0:9dc31a0e43ac | 307 | |
Info | 0:9dc31a0e43ac | 308 | |
Info | 0:9dc31a0e43ac | 309 | |
Info | 0:9dc31a0e43ac | 310 | |
Info | 0:9dc31a0e43ac | 311 | void writeReg(uint16_t addr, uint16_t writeData){ |
Info | 0:9dc31a0e43ac | 312 | |
Info | 0:9dc31a0e43ac | 313 | addr0 = (addr & 0x0001); |
Info | 0:9dc31a0e43ac | 314 | addr1 = ((addr >> 1)&0x001); |
Info | 0:9dc31a0e43ac | 315 | addr2 = ((addr >> 2)&0x001); |
Info | 0:9dc31a0e43ac | 316 | addr3 = ((addr >> 3)&0x001); |
Info | 0:9dc31a0e43ac | 317 | addr4 = ((addr >> 4)&0x001); |
Info | 0:9dc31a0e43ac | 318 | |
Info | 0:9dc31a0e43ac | 319 | WData0 = (writeData & 0x0001); |
Info | 0:9dc31a0e43ac | 320 | WData1 = ((writeData >> 1)&0x001); |
Info | 0:9dc31a0e43ac | 321 | WData2 = ((writeData >> 2)&0x001); |
Info | 0:9dc31a0e43ac | 322 | WData3 = ((writeData >> 3)&0x001); |
Info | 0:9dc31a0e43ac | 323 | WData4 = ((writeData >> 4)&0x001); |
Info | 0:9dc31a0e43ac | 324 | |
Info | 0:9dc31a0e43ac | 325 | WLatch = 0; |
Info | 0:9dc31a0e43ac | 326 | wait_us(0.1); |
Info | 0:9dc31a0e43ac | 327 | WLatch = 1; |
Info | 0:9dc31a0e43ac | 328 | |
Info | 0:9dc31a0e43ac | 329 | //pc.printf("addr = %x\r\n",addr); |
Info | 0:9dc31a0e43ac | 330 | |
Info | 0:9dc31a0e43ac | 331 | //pc.printf("writeData = %x\r\n",writeData); |
Info | 0:9dc31a0e43ac | 332 | |
Info | 0:9dc31a0e43ac | 333 | //pc.printf("\r\nwrite Data addr1\r\n"); |
Info | 0:9dc31a0e43ac | 334 | |
Info | 0:9dc31a0e43ac | 335 | } |
Info | 0:9dc31a0e43ac | 336 | |
Info | 0:9dc31a0e43ac | 337 | |
Info | 0:9dc31a0e43ac | 338 | /* |
Info | 0:9dc31a0e43ac | 339 | uint16_t readReg(uint16_t addr){ |
Info | 0:9dc31a0e43ac | 340 | |
Info | 0:9dc31a0e43ac | 341 | uint16_t myReadData = 0; |
Info | 0:9dc31a0e43ac | 342 | |
Info | 0:9dc31a0e43ac | 343 | addr0 = (addr & 0x0001); |
Info | 0:9dc31a0e43ac | 344 | addr1 = ((addr >> 1)&0x001); |
Info | 0:9dc31a0e43ac | 345 | addr2 = ((addr >> 2)&0x001); |
Info | 0:9dc31a0e43ac | 346 | addr3 = ((addr >> 3)&0x001); |
Info | 0:9dc31a0e43ac | 347 | addr4 = ((addr >> 4)&0x001); |
Info | 0:9dc31a0e43ac | 348 | |
Info | 0:9dc31a0e43ac | 349 | RLatch = 0; |
Info | 0:9dc31a0e43ac | 350 | wait_ms(0.01); |
Info | 0:9dc31a0e43ac | 351 | RLatch = 1; |
Info | 0:9dc31a0e43ac | 352 | wait_ms(0.01); |
Info | 0:9dc31a0e43ac | 353 | |
Info | 0:9dc31a0e43ac | 354 | myReadData += RData0; |
Info | 0:9dc31a0e43ac | 355 | myReadData += (RData1 << 1); |
Info | 0:9dc31a0e43ac | 356 | myReadData += (RData2 << 2); |
Info | 0:9dc31a0e43ac | 357 | myReadData += (RData3 << 3); |
Info | 0:9dc31a0e43ac | 358 | myReadData += (RData4 << 4); |
Info | 0:9dc31a0e43ac | 359 | |
Info | 0:9dc31a0e43ac | 360 | //pc.printf("read Data addr1 = 5'b %d\r\n",myReadData); |
Info | 0:9dc31a0e43ac | 361 | |
Info | 0:9dc31a0e43ac | 362 | return myReadData; |
Info | 0:9dc31a0e43ac | 363 | } |
Info | 0:9dc31a0e43ac | 364 | */ |
Info | 0:9dc31a0e43ac | 365 | |
Info | 0:9dc31a0e43ac | 366 | |
Info | 0:9dc31a0e43ac | 367 | void resetAll(void){ |
Info | 0:9dc31a0e43ac | 368 | int i; |
Info | 0:9dc31a0e43ac | 369 | for(i=0;i<=15;++i){ |
Info | 0:9dc31a0e43ac | 370 | writeReg(i,0); |
Info | 0:9dc31a0e43ac | 371 | } |
Info | 0:9dc31a0e43ac | 372 | } |
Info | 0:9dc31a0e43ac | 373 | |
Info | 0:9dc31a0e43ac | 374 | |
Info | 0:9dc31a0e43ac | 375 | int main() { |
Info | 0:9dc31a0e43ac | 376 | uint8_t flg_mode = 0; |
Info | 0:9dc31a0e43ac | 377 | uint16_t SpiRdata = 0; |
Info | 0:9dc31a0e43ac | 378 | //uint16_t SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 379 | uint32_t SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 380 | uint16_t SpiIoNo = 0; |
Info | 0:9dc31a0e43ac | 381 | uint16_t SpiWrite = 0; |
Info | 0:9dc31a0e43ac | 382 | uint8_t resetCounter = 0; |
Info | 0:9dc31a0e43ac | 383 | uint8_t flg_ResetMode = 0; |
Info | 0:9dc31a0e43ac | 384 | uint8_t nextSend = 0; |
Info | 0:9dc31a0e43ac | 385 | |
Info | 0:9dc31a0e43ac | 386 | address[0] = 0x80; |
Info | 0:9dc31a0e43ac | 387 | address[1] = 0x00; |
Info | 0:9dc31a0e43ac | 388 | address[2] = 0x00; |
Info | 0:9dc31a0e43ac | 389 | address[3] = 0x00; |
Info | 0:9dc31a0e43ac | 390 | |
Info | 0:9dc31a0e43ac | 391 | //ReadDataFromEEPROM(); |
Info | 0:9dc31a0e43ac | 392 | |
Info | 0:9dc31a0e43ac | 393 | myIO1.input(); |
Info | 0:9dc31a0e43ac | 394 | myIO2.input(); |
Info | 0:9dc31a0e43ac | 395 | myIO1.mode(PullNone); |
Info | 0:9dc31a0e43ac | 396 | myIO2.mode(PullNone); |
Info | 0:9dc31a0e43ac | 397 | |
Info | 0:9dc31a0e43ac | 398 | myled = 1; |
Info | 0:9dc31a0e43ac | 399 | wait(0.1); |
Info | 0:9dc31a0e43ac | 400 | myled = 0; |
Info | 0:9dc31a0e43ac | 401 | wait(0.1); |
Info | 0:9dc31a0e43ac | 402 | myled = 1; |
Info | 0:9dc31a0e43ac | 403 | wait(0.1); |
Info | 0:9dc31a0e43ac | 404 | myled = 0; |
Info | 0:9dc31a0e43ac | 405 | wait(0.1); |
Info | 0:9dc31a0e43ac | 406 | |
Info | 0:9dc31a0e43ac | 407 | #ifdef test |
Info | 0:9dc31a0e43ac | 408 | while(1){ |
Info | 0:9dc31a0e43ac | 409 | holdeMode(0); |
Info | 0:9dc31a0e43ac | 410 | wait(1); |
Info | 0:9dc31a0e43ac | 411 | holdeMode(1); |
Info | 0:9dc31a0e43ac | 412 | wait(1); |
Info | 0:9dc31a0e43ac | 413 | //holdeMode(2); |
Info | 0:9dc31a0e43ac | 414 | //wait(1); |
Info | 0:9dc31a0e43ac | 415 | //holdeMode(3); |
Info | 0:9dc31a0e43ac | 416 | //wait(1); |
Info | 0:9dc31a0e43ac | 417 | } |
Info | 0:9dc31a0e43ac | 418 | |
Info | 0:9dc31a0e43ac | 419 | while(1){ |
Info | 0:9dc31a0e43ac | 420 | IoTest(); |
Info | 0:9dc31a0e43ac | 421 | } |
Info | 0:9dc31a0e43ac | 422 | #endif |
Info | 0:9dc31a0e43ac | 423 | |
Info | 0:9dc31a0e43ac | 424 | #ifdef ROMtest |
Info | 0:9dc31a0e43ac | 425 | EEPROMtest(); |
Info | 0:9dc31a0e43ac | 426 | while(1){ |
Info | 0:9dc31a0e43ac | 427 | ; |
Info | 0:9dc31a0e43ac | 428 | } |
Info | 0:9dc31a0e43ac | 429 | #endif |
Info | 0:9dc31a0e43ac | 430 | /* |
Info | 0:9dc31a0e43ac | 431 | writeReg(0x01,0x01); |
Info | 0:9dc31a0e43ac | 432 | writeReg(0x02,0x02); |
Info | 0:9dc31a0e43ac | 433 | writeReg(0x03,0x03); |
Info | 0:9dc31a0e43ac | 434 | writeReg(0x04,0x04); |
Info | 0:9dc31a0e43ac | 435 | // writeReg(0x07,0x07);//error |
Info | 0:9dc31a0e43ac | 436 | |
Info | 0:9dc31a0e43ac | 437 | while(1){ |
Info | 0:9dc31a0e43ac | 438 | myled = 1; |
Info | 0:9dc31a0e43ac | 439 | wait(0.1); |
Info | 0:9dc31a0e43ac | 440 | myled = 0; |
Info | 0:9dc31a0e43ac | 441 | wait(0.1); |
Info | 0:9dc31a0e43ac | 442 | wait(1); |
Info | 0:9dc31a0e43ac | 443 | } |
Info | 0:9dc31a0e43ac | 444 | */ |
Info | 0:9dc31a0e43ac | 445 | |
Info | 0:9dc31a0e43ac | 446 | MRESET = 0; |
Info | 0:9dc31a0e43ac | 447 | wait_ms(100); |
Info | 0:9dc31a0e43ac | 448 | MRESET = 1; |
Info | 0:9dc31a0e43ac | 449 | |
Info | 0:9dc31a0e43ac | 450 | device.format(8,0); |
Info | 0:9dc31a0e43ac | 451 | device.frequency(BaseSPIfreq); |
Info | 0:9dc31a0e43ac | 452 | |
Info | 0:9dc31a0e43ac | 453 | device.reply(0x00); // Prime SPI with first reply |
Info | 0:9dc31a0e43ac | 454 | myled = 0; |
Info | 0:9dc31a0e43ac | 455 | |
Info | 0:9dc31a0e43ac | 456 | while(1) { |
Info | 0:9dc31a0e43ac | 457 | flg_ResetMode = 0; |
Info | 0:9dc31a0e43ac | 458 | if(device.receive()) { |
Info | 0:9dc31a0e43ac | 459 | SpiRdata = device.read(); |
Info | 0:9dc31a0e43ac | 460 | device.reply(nextSend); // Make this the next reply |
Info | 0:9dc31a0e43ac | 461 | |
Info | 0:9dc31a0e43ac | 462 | |
Info | 0:9dc31a0e43ac | 463 | #ifdef debug |
Info | 0:9dc31a0e43ac | 464 | printf("device.receive() SpiRdata = %d\r\n",SpiRdata); |
Info | 0:9dc31a0e43ac | 465 | printf("flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 466 | #endif |
Info | 0:9dc31a0e43ac | 467 | //myled = 1; |
Info | 0:9dc31a0e43ac | 468 | if(SpiRdata == 0x00){ |
Info | 0:9dc31a0e43ac | 469 | nextSend = 0x00; |
Info | 0:9dc31a0e43ac | 470 | ++resetCounter; |
Info | 0:9dc31a0e43ac | 471 | if(resetCounter >= 6){ |
Info | 0:9dc31a0e43ac | 472 | //if((SpiRdata == 0x00)&&(flg_mode != 1)){//reset |
Info | 0:9dc31a0e43ac | 473 | flg_mode = 0; |
Info | 0:9dc31a0e43ac | 474 | SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 475 | SpiIoNo = 0; |
Info | 0:9dc31a0e43ac | 476 | SpiWrite = 0; |
Info | 0:9dc31a0e43ac | 477 | resetCounter = 0; |
Info | 0:9dc31a0e43ac | 478 | flg_ResetMode = 1; |
Info | 0:9dc31a0e43ac | 479 | |
Info | 0:9dc31a0e43ac | 480 | //resetAll(); |
Info | 0:9dc31a0e43ac | 481 | myled = 1; |
Info | 0:9dc31a0e43ac | 482 | wait(0.1); |
Info | 0:9dc31a0e43ac | 483 | myled = 0; |
Info | 0:9dc31a0e43ac | 484 | wait(0.1); |
Info | 0:9dc31a0e43ac | 485 | myled = 1; |
Info | 0:9dc31a0e43ac | 486 | wait(0.1); |
Info | 0:9dc31a0e43ac | 487 | myled = 0; |
Info | 0:9dc31a0e43ac | 488 | wait(0.1); |
Info | 0:9dc31a0e43ac | 489 | #ifdef debug |
Info | 0:9dc31a0e43ac | 490 | printf("reset\r\n"); |
Info | 0:9dc31a0e43ac | 491 | #endif |
Info | 0:9dc31a0e43ac | 492 | } |
Info | 0:9dc31a0e43ac | 493 | |
Info | 0:9dc31a0e43ac | 494 | }else{ |
Info | 0:9dc31a0e43ac | 495 | resetCounter = 0; |
Info | 0:9dc31a0e43ac | 496 | } |
Info | 0:9dc31a0e43ac | 497 | |
Info | 0:9dc31a0e43ac | 498 | //}else{//select mode |
Info | 0:9dc31a0e43ac | 499 | |
Info | 0:9dc31a0e43ac | 500 | if((flg_mode == 0) && (flg_ResetMode == 0)) {//read address data |
Info | 0:9dc31a0e43ac | 501 | SpiAddr = SpiRdata; |
Info | 0:9dc31a0e43ac | 502 | nextSend = address[0]; |
Info | 0:9dc31a0e43ac | 503 | flg_mode = 1; |
Info | 0:9dc31a0e43ac | 504 | |
Info | 0:9dc31a0e43ac | 505 | #ifdef debug |
Info | 0:9dc31a0e43ac | 506 | printf("0 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 507 | #endif |
Info | 0:9dc31a0e43ac | 508 | }else if(flg_mode == 1) {//read address data |
Info | 0:9dc31a0e43ac | 509 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:9dc31a0e43ac | 510 | nextSend = address[1]; |
Info | 0:9dc31a0e43ac | 511 | flg_mode = 11; |
Info | 0:9dc31a0e43ac | 512 | #ifdef debug |
Info | 0:9dc31a0e43ac | 513 | printf("1 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 514 | #endif |
Info | 0:9dc31a0e43ac | 515 | }else if(flg_mode == 11) {//read address data |
Info | 0:9dc31a0e43ac | 516 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:9dc31a0e43ac | 517 | nextSend = address[2]; |
Info | 0:9dc31a0e43ac | 518 | flg_mode = 12; |
Info | 0:9dc31a0e43ac | 519 | #ifdef debug |
Info | 0:9dc31a0e43ac | 520 | printf("11 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 521 | #endif |
Info | 0:9dc31a0e43ac | 522 | }else if(flg_mode == 12) {//read address data |
Info | 0:9dc31a0e43ac | 523 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:9dc31a0e43ac | 524 | nextSend = address[3]; |
Info | 0:9dc31a0e43ac | 525 | flg_mode = 2; |
Info | 0:9dc31a0e43ac | 526 | #ifdef debug |
Info | 0:9dc31a0e43ac | 527 | printf("12 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 528 | #endif |
Info | 0:9dc31a0e43ac | 529 | }else if(flg_mode == 2) {//read address data |
Info | 0:9dc31a0e43ac | 530 | SpiIoNo = SpiRdata; |
Info | 0:9dc31a0e43ac | 531 | nextSend = 0x00; |
Info | 0:9dc31a0e43ac | 532 | flg_mode = 3; |
Info | 0:9dc31a0e43ac | 533 | #ifdef debug |
Info | 0:9dc31a0e43ac | 534 | printf("2 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 535 | #endif |
Info | 0:9dc31a0e43ac | 536 | }else if(flg_mode == 3) {//read fpga write data |
Info | 0:9dc31a0e43ac | 537 | SpiWrite = SpiRdata; |
Info | 0:9dc31a0e43ac | 538 | flg_mode = 0; |
Info | 0:9dc31a0e43ac | 539 | #ifdef debug2 |
Info | 0:9dc31a0e43ac | 540 | pc.printf("3 flg_mode = %d\r\n",flg_mode); |
Info | 0:9dc31a0e43ac | 541 | pc.printf("SpiAddr = %d\r\n",SpiAddr); |
Info | 0:9dc31a0e43ac | 542 | pc.printf("SpiAddr = %08x\r\n",SpiAddr); |
Info | 0:9dc31a0e43ac | 543 | pc.printf("SpiIoNo = %d\r\n",SpiIoNo); |
Info | 0:9dc31a0e43ac | 544 | pc.printf("SpiWrite = %d\r\n",SpiWrite); |
Info | 0:9dc31a0e43ac | 545 | #endif |
Info | 0:9dc31a0e43ac | 546 | |
Info | 0:9dc31a0e43ac | 547 | //Select mode |
Info | 0:9dc31a0e43ac | 548 | if(SpiAddr == def_myAddress){ |
Info | 0:9dc31a0e43ac | 549 | #ifdef debug |
Info | 0:9dc31a0e43ac | 550 | printf("Select mode\r\n"); |
Info | 0:9dc31a0e43ac | 551 | #endif |
Info | 0:9dc31a0e43ac | 552 | //printf("Select mode;SpiAddr = 0x%X, SpiIoNo = 0x%X, SpiWrite = 0x%X\r\n",SpiAddr,SpiIoNo,SpiWrite); |
Info | 0:9dc31a0e43ac | 553 | |
Info | 0:9dc31a0e43ac | 554 | if(SpiWrite == 0xee){//read from eeprom address |
Info | 0:9dc31a0e43ac | 555 | ReadDataFromEEPROM(); |
Info | 0:9dc31a0e43ac | 556 | }else if(SpiWrite == 0xcc){//hold mode |
Info | 0:9dc31a0e43ac | 557 | holdeMode(SpiIoNo); |
Info | 0:9dc31a0e43ac | 558 | }else{//set register |
Info | 0:9dc31a0e43ac | 559 | writeReg(SpiIoNo,SpiWrite); |
Info | 0:9dc31a0e43ac | 560 | } |
Info | 0:9dc31a0e43ac | 561 | //writeMode1(SpiIoNo); |
Info | 0:9dc31a0e43ac | 562 | //writeMode2(SpiWrite); |
Info | 0:9dc31a0e43ac | 563 | myled = 1; |
Info | 0:9dc31a0e43ac | 564 | |
Info | 0:9dc31a0e43ac | 565 | /*while(1) { |
Info | 0:9dc31a0e43ac | 566 | for(int i=0;i<SpiIoNo;++i){ |
Info | 0:9dc31a0e43ac | 567 | myled = 1; |
Info | 0:9dc31a0e43ac | 568 | wait(0.2); |
Info | 0:9dc31a0e43ac | 569 | myled = 0; |
Info | 0:9dc31a0e43ac | 570 | wait(0.2); |
Info | 0:9dc31a0e43ac | 571 | } |
Info | 0:9dc31a0e43ac | 572 | myled = 1; |
Info | 0:9dc31a0e43ac | 573 | wait(1); |
Info | 0:9dc31a0e43ac | 574 | myled = 0; |
Info | 0:9dc31a0e43ac | 575 | wait(1); |
Info | 0:9dc31a0e43ac | 576 | }*/ |
Info | 0:9dc31a0e43ac | 577 | |
Info | 0:9dc31a0e43ac | 578 | flg_mode = 0; |
Info | 0:9dc31a0e43ac | 579 | SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 580 | SpiIoNo = 0; |
Info | 0:9dc31a0e43ac | 581 | SpiWrite = 0; |
Info | 0:9dc31a0e43ac | 582 | |
Info | 0:9dc31a0e43ac | 583 | //}else if(SpiAddr == mod_myAddress){ |
Info | 0:9dc31a0e43ac | 584 | // writeReg(SpiIoNo,SpiWrite); |
Info | 0:9dc31a0e43ac | 585 | // myled = 1; |
Info | 0:9dc31a0e43ac | 586 | // flg_mode = 0; |
Info | 0:9dc31a0e43ac | 587 | // SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 588 | // SpiIoNo = 0; |
Info | 0:9dc31a0e43ac | 589 | // SpiWrite = 0; |
Info | 0:9dc31a0e43ac | 590 | |
Info | 0:9dc31a0e43ac | 591 | |
Info | 0:9dc31a0e43ac | 592 | }else{//not this address |
Info | 0:9dc31a0e43ac | 593 | //flug reset |
Info | 0:9dc31a0e43ac | 594 | flg_mode = 0; |
Info | 0:9dc31a0e43ac | 595 | SpiAddr = 0; |
Info | 0:9dc31a0e43ac | 596 | SpiIoNo = 0; |
Info | 0:9dc31a0e43ac | 597 | SpiWrite = 0; |
Info | 0:9dc31a0e43ac | 598 | myled = 0; |
Info | 0:9dc31a0e43ac | 599 | #ifdef debug |
Info | 0:9dc31a0e43ac | 600 | printf("Not me\r\n"); |
Info | 0:9dc31a0e43ac | 601 | #endif |
Info | 0:9dc31a0e43ac | 602 | |
Info | 0:9dc31a0e43ac | 603 | } |
Info | 0:9dc31a0e43ac | 604 | } |
Info | 0:9dc31a0e43ac | 605 | |
Info | 0:9dc31a0e43ac | 606 | } |
Info | 0:9dc31a0e43ac | 607 | } |
Info | 0:9dc31a0e43ac | 608 | } |