![](/media/cache/profiles/a7921ea22e084f4c3f660b30749325c3.jpg.50x50_q85.jpg)
mille-feuille module MCU program
main.cpp@0:98ed24be8501, 23 months ago (annotated)
- Committer:
- Info
- Date:
- Tue Sep 06 04:45:08 2022 +0000
- Revision:
- 0:98ed24be8501
open
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Info | 0:98ed24be8501 | 1 | #include "mbed.h" |
Info | 0:98ed24be8501 | 2 | #include "EEPROM24LC01.h" |
Info | 0:98ed24be8501 | 3 | |
Info | 0:98ed24be8501 | 4 | //#define def_myAddress 0x8001 |
Info | 0:98ed24be8501 | 5 | |
Info | 0:98ed24be8501 | 6 | //Bus switch |
Info | 0:98ed24be8501 | 7 | //TC7MBL3125 nOE |
Info | 0:98ed24be8501 | 8 | //#define GateClose 1 |
Info | 0:98ed24be8501 | 9 | //#define GateOpen 0 |
Info | 0:98ed24be8501 | 10 | |
Info | 0:98ed24be8501 | 11 | //TC7MBL3126 OE |
Info | 0:98ed24be8501 | 12 | #define GateClose 0 |
Info | 0:98ed24be8501 | 13 | #define GateOpen 1 |
Info | 0:98ed24be8501 | 14 | |
Info | 0:98ed24be8501 | 15 | |
Info | 0:98ed24be8501 | 16 | #define OffFET 0 |
Info | 0:98ed24be8501 | 17 | #define OnFET 1 |
Info | 0:98ed24be8501 | 18 | #define HOLD 0xff |
Info | 0:98ed24be8501 | 19 | #define ALL_HOLD 0xff |
Info | 0:98ed24be8501 | 20 | #define NOTHOLD 0 |
Info | 0:98ed24be8501 | 21 | |
Info | 0:98ed24be8501 | 22 | //#define test 1 |
Info | 0:98ed24be8501 | 23 | //#define ROMtest 1 |
Info | 0:98ed24be8501 | 24 | //#define debug 1 |
Info | 0:98ed24be8501 | 25 | |
Info | 0:98ed24be8501 | 26 | //#define SPIfreq 2000000 |
Info | 0:98ed24be8501 | 27 | #define SPIfreq 17000000 |
Info | 0:98ed24be8501 | 28 | |
Info | 0:98ed24be8501 | 29 | I2C i2c(I2C_SDA,I2C_SCL); // sda, scl |
Info | 0:98ed24be8501 | 30 | EEPROM24LC01 eeprom(&i2c, 0x50); |
Info | 0:98ed24be8501 | 31 | |
Info | 0:98ed24be8501 | 32 | //DigitalOut myGate(P0_21,0); |
Info | 0:98ed24be8501 | 33 | DigitalOut Gate1_1(P0_14,1); |
Info | 0:98ed24be8501 | 34 | DigitalOut Gate1_2(P0_23,1); |
Info | 0:98ed24be8501 | 35 | DigitalOut Gate1_3(P0_21,1); |
Info | 0:98ed24be8501 | 36 | DigitalOut Gate1_4(P0_22,1); |
Info | 0:98ed24be8501 | 37 | DigitalOut Gate2_MISO(P0_20,1); |
Info | 0:98ed24be8501 | 38 | DigitalOut Gate2_MOSI(P0_19,1); |
Info | 0:98ed24be8501 | 39 | DigitalOut Gate2_SCK(P0_17,1); |
Info | 0:98ed24be8501 | 40 | DigitalOut Gate2_4(P0_13,1); |
Info | 0:98ed24be8501 | 41 | |
Info | 0:98ed24be8501 | 42 | DigitalInOut myIO1(P0_1);//ng |
Info | 0:98ed24be8501 | 43 | DigitalInOut myIO2(P0_28); |
Info | 0:98ed24be8501 | 44 | DigitalInOut myIO3(P0_27); |
Info | 0:98ed24be8501 | 45 | DigitalInOut myIO4(P0_26); |
Info | 0:98ed24be8501 | 46 | |
Info | 0:98ed24be8501 | 47 | DigitalOut myFET(P0_6,0); |
Info | 0:98ed24be8501 | 48 | |
Info | 0:98ed24be8501 | 49 | DigitalIn iAddress1(P0_8); |
Info | 0:98ed24be8501 | 50 | DigitalIn iAddress2(P0_9); |
Info | 0:98ed24be8501 | 51 | |
Info | 0:98ed24be8501 | 52 | DigitalOut myled(LED2,0); |
Info | 0:98ed24be8501 | 53 | |
Info | 0:98ed24be8501 | 54 | #ifdef debug |
Info | 0:98ed24be8501 | 55 | Serial pc(USBTX, USBRX); // tx, rx |
Info | 0:98ed24be8501 | 56 | #endif |
Info | 0:98ed24be8501 | 57 | |
Info | 0:98ed24be8501 | 58 | //SPISlave device(P0_26, P0_25, P0_24, P0_15); // mosi, miso, sclk, ssel |
Info | 0:98ed24be8501 | 59 | //SPISlave device(P0_25, P0_26, P0_24, P0_15); // mosi, miso, sclk, ssel |
Info | 0:98ed24be8501 | 60 | SPISlave device(P0_25, P0_18, P0_24, P0_15); // mosi, miso, sclk, ssel |
Info | 0:98ed24be8501 | 61 | |
Info | 0:98ed24be8501 | 62 | uint8_t address[4]; |
Info | 0:98ed24be8501 | 63 | uint8_t exData[3]; |
Info | 0:98ed24be8501 | 64 | uint8_t gateData; |
Info | 0:98ed24be8501 | 65 | uint32_t def_myAddress; |
Info | 0:98ed24be8501 | 66 | uint32_t def_searchAddress; |
Info | 0:98ed24be8501 | 67 | |
Info | 0:98ed24be8501 | 68 | void closeAllGate(void); |
Info | 0:98ed24be8501 | 69 | |
Info | 0:98ed24be8501 | 70 | #ifdef test |
Info | 0:98ed24be8501 | 71 | void spiTest(void){ |
Info | 0:98ed24be8501 | 72 | device.reply(0x00); // Prime SPI with first reply |
Info | 0:98ed24be8501 | 73 | printf("SPI slave test\r\n"); |
Info | 0:98ed24be8501 | 74 | while(1) { |
Info | 0:98ed24be8501 | 75 | if(device.receive()) { |
Info | 0:98ed24be8501 | 76 | int v = device.read(); // Read byte from master |
Info | 0:98ed24be8501 | 77 | printf("Recieve = %d\r\n",v); |
Info | 0:98ed24be8501 | 78 | //v = (v + 1) % 0x100; // Add one to it, modulo 256 |
Info | 0:98ed24be8501 | 79 | device.reply(v+1); // Make this the next reply |
Info | 0:98ed24be8501 | 80 | printf("reply = %d\r\n",v); |
Info | 0:98ed24be8501 | 81 | } |
Info | 0:98ed24be8501 | 82 | } |
Info | 0:98ed24be8501 | 83 | } |
Info | 0:98ed24be8501 | 84 | |
Info | 0:98ed24be8501 | 85 | void IoTest(void){ |
Info | 0:98ed24be8501 | 86 | int inData[4]; |
Info | 0:98ed24be8501 | 87 | int inAddr1,inAddr2; |
Info | 0:98ed24be8501 | 88 | |
Info | 0:98ed24be8501 | 89 | myIO1.input(); |
Info | 0:98ed24be8501 | 90 | myIO2.input(); |
Info | 0:98ed24be8501 | 91 | myIO3.input(); |
Info | 0:98ed24be8501 | 92 | myIO4.input(); |
Info | 0:98ed24be8501 | 93 | |
Info | 0:98ed24be8501 | 94 | printf("IoTest\r\n"); |
Info | 0:98ed24be8501 | 95 | |
Info | 0:98ed24be8501 | 96 | while(1){ |
Info | 0:98ed24be8501 | 97 | |
Info | 0:98ed24be8501 | 98 | inAddr1 = iAddress1; |
Info | 0:98ed24be8501 | 99 | inAddr2 = iAddress2; |
Info | 0:98ed24be8501 | 100 | if(inAddr1 == 0){ |
Info | 0:98ed24be8501 | 101 | myIO1.input(); |
Info | 0:98ed24be8501 | 102 | myIO2.input(); |
Info | 0:98ed24be8501 | 103 | myIO3.input(); |
Info | 0:98ed24be8501 | 104 | myIO4.input(); |
Info | 0:98ed24be8501 | 105 | myIO1.mode(PullNone); |
Info | 0:98ed24be8501 | 106 | myIO2.mode(PullNone); |
Info | 0:98ed24be8501 | 107 | myIO3.mode(PullNone); |
Info | 0:98ed24be8501 | 108 | myIO4.mode(PullNone); |
Info | 0:98ed24be8501 | 109 | inData[0] = myIO1; |
Info | 0:98ed24be8501 | 110 | inData[1] = myIO2; |
Info | 0:98ed24be8501 | 111 | inData[2] = myIO3; |
Info | 0:98ed24be8501 | 112 | inData[3] = myIO4; |
Info | 0:98ed24be8501 | 113 | printf("inData[0] = %d\r\n",inData[0]); |
Info | 0:98ed24be8501 | 114 | printf("inData[1] = %d\r\n",inData[1]); |
Info | 0:98ed24be8501 | 115 | printf("inData[2] = %d\r\n",inData[2]); |
Info | 0:98ed24be8501 | 116 | printf("inData[3] = %d\r\n\r\n",inData[3]); |
Info | 0:98ed24be8501 | 117 | }else{ |
Info | 0:98ed24be8501 | 118 | printf("myIOs output\r\n"); |
Info | 0:98ed24be8501 | 119 | myIO1.output(); |
Info | 0:98ed24be8501 | 120 | myIO2.output(); |
Info | 0:98ed24be8501 | 121 | myIO3.output(); |
Info | 0:98ed24be8501 | 122 | myIO4.output(); |
Info | 0:98ed24be8501 | 123 | myIO1.mode(PullUp); |
Info | 0:98ed24be8501 | 124 | myIO2.mode(PullUp); |
Info | 0:98ed24be8501 | 125 | myIO3.mode(PullUp); |
Info | 0:98ed24be8501 | 126 | myIO4.mode(PullUp); |
Info | 0:98ed24be8501 | 127 | myIO1 = 1; |
Info | 0:98ed24be8501 | 128 | myIO2 = 1; |
Info | 0:98ed24be8501 | 129 | myIO3 = 1; |
Info | 0:98ed24be8501 | 130 | myIO4 = 1; |
Info | 0:98ed24be8501 | 131 | } |
Info | 0:98ed24be8501 | 132 | printf("inAddr1 = %d\r\n",inAddr1); |
Info | 0:98ed24be8501 | 133 | printf("inAddr2 = %d\r\n",inAddr2); |
Info | 0:98ed24be8501 | 134 | |
Info | 0:98ed24be8501 | 135 | |
Info | 0:98ed24be8501 | 136 | Gate1_1 = 1; |
Info | 0:98ed24be8501 | 137 | Gate1_2 = 1; |
Info | 0:98ed24be8501 | 138 | Gate1_3 = 1; |
Info | 0:98ed24be8501 | 139 | Gate1_4 = 1; |
Info | 0:98ed24be8501 | 140 | Gate2_MISO = 1; |
Info | 0:98ed24be8501 | 141 | Gate2_MOSI = 1; |
Info | 0:98ed24be8501 | 142 | Gate2_SCK = 1; |
Info | 0:98ed24be8501 | 143 | //Gate2_4 = 1; |
Info | 0:98ed24be8501 | 144 | myFET = 1; |
Info | 0:98ed24be8501 | 145 | myled = 1; |
Info | 0:98ed24be8501 | 146 | |
Info | 0:98ed24be8501 | 147 | |
Info | 0:98ed24be8501 | 148 | wait(1); |
Info | 0:98ed24be8501 | 149 | |
Info | 0:98ed24be8501 | 150 | if(inAddr1==1){ |
Info | 0:98ed24be8501 | 151 | myIO1 = 0; |
Info | 0:98ed24be8501 | 152 | myIO2 = 0; |
Info | 0:98ed24be8501 | 153 | myIO3 = 0; |
Info | 0:98ed24be8501 | 154 | myIO4 = 0; |
Info | 0:98ed24be8501 | 155 | } |
Info | 0:98ed24be8501 | 156 | |
Info | 0:98ed24be8501 | 157 | Gate1_1 = 0; |
Info | 0:98ed24be8501 | 158 | Gate1_2 = 0; |
Info | 0:98ed24be8501 | 159 | Gate1_3 = 0; |
Info | 0:98ed24be8501 | 160 | Gate1_4 = 0; |
Info | 0:98ed24be8501 | 161 | Gate2_MISO = 0; |
Info | 0:98ed24be8501 | 162 | Gate2_MOSI = 0; |
Info | 0:98ed24be8501 | 163 | Gate2_SCK = 0; |
Info | 0:98ed24be8501 | 164 | //Gate2_4 = 0; |
Info | 0:98ed24be8501 | 165 | myFET = 0; |
Info | 0:98ed24be8501 | 166 | myled = 0; |
Info | 0:98ed24be8501 | 167 | |
Info | 0:98ed24be8501 | 168 | wait(1); |
Info | 0:98ed24be8501 | 169 | |
Info | 0:98ed24be8501 | 170 | } |
Info | 0:98ed24be8501 | 171 | |
Info | 0:98ed24be8501 | 172 | } |
Info | 0:98ed24be8501 | 173 | |
Info | 0:98ed24be8501 | 174 | #endif |
Info | 0:98ed24be8501 | 175 | |
Info | 0:98ed24be8501 | 176 | |
Info | 0:98ed24be8501 | 177 | #ifdef ROMtest |
Info | 0:98ed24be8501 | 178 | void EEPROMtest(void){ |
Info | 0:98ed24be8501 | 179 | char bufData[10]; |
Info | 0:98ed24be8501 | 180 | |
Info | 0:98ed24be8501 | 181 | printf("write test\n\r"); |
Info | 0:98ed24be8501 | 182 | |
Info | 0:98ed24be8501 | 183 | bufData[0]=0x80;//address1 |
Info | 0:98ed24be8501 | 184 | bufData[1]=0x00;//address2 |
Info | 0:98ed24be8501 | 185 | bufData[2]=0x00;//address3 |
Info | 0:98ed24be8501 | 186 | bufData[3]=0x0D;//address4 |
Info | 0:98ed24be8501 | 187 | bufData[4]=0x04;//extra data |
Info | 0:98ed24be8501 | 188 | bufData[5]=0x05;//extra data |
Info | 0:98ed24be8501 | 189 | bufData[6]=0x06;//extra data |
Info | 0:98ed24be8501 | 190 | bufData[7]=0x07;//gate open pattern number |
Info | 0:98ed24be8501 | 191 | eeprom.byte8_write( bufData); |
Info | 0:98ed24be8501 | 192 | |
Info | 0:98ed24be8501 | 193 | bufData[0]=0xAA; |
Info | 0:98ed24be8501 | 194 | |
Info | 0:98ed24be8501 | 195 | printf("end\n\r"); |
Info | 0:98ed24be8501 | 196 | |
Info | 0:98ed24be8501 | 197 | |
Info | 0:98ed24be8501 | 198 | bufData[0]=0; |
Info | 0:98ed24be8501 | 199 | bufData[1]=0; |
Info | 0:98ed24be8501 | 200 | bufData[2]=0; |
Info | 0:98ed24be8501 | 201 | bufData[3]=0; |
Info | 0:98ed24be8501 | 202 | bufData[4]=0; |
Info | 0:98ed24be8501 | 203 | bufData[5]=0; |
Info | 0:98ed24be8501 | 204 | bufData[6]=0; |
Info | 0:98ed24be8501 | 205 | bufData[7]=0; |
Info | 0:98ed24be8501 | 206 | |
Info | 0:98ed24be8501 | 207 | |
Info | 0:98ed24be8501 | 208 | eeprom.readAll( bufData, 8 ); |
Info | 0:98ed24be8501 | 209 | |
Info | 0:98ed24be8501 | 210 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:98ed24be8501 | 211 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:98ed24be8501 | 212 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:98ed24be8501 | 213 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:98ed24be8501 | 214 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:98ed24be8501 | 215 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:98ed24be8501 | 216 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:98ed24be8501 | 217 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:98ed24be8501 | 218 | |
Info | 0:98ed24be8501 | 219 | printf("\n\r Read again! \n\r\n\r"); |
Info | 0:98ed24be8501 | 220 | |
Info | 0:98ed24be8501 | 221 | |
Info | 0:98ed24be8501 | 222 | eeprom.readAll( bufData, 8 ); |
Info | 0:98ed24be8501 | 223 | |
Info | 0:98ed24be8501 | 224 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:98ed24be8501 | 225 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:98ed24be8501 | 226 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:98ed24be8501 | 227 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:98ed24be8501 | 228 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:98ed24be8501 | 229 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:98ed24be8501 | 230 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:98ed24be8501 | 231 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:98ed24be8501 | 232 | |
Info | 0:98ed24be8501 | 233 | printf("\nend\n\r"); |
Info | 0:98ed24be8501 | 234 | |
Info | 0:98ed24be8501 | 235 | |
Info | 0:98ed24be8501 | 236 | while(1); |
Info | 0:98ed24be8501 | 237 | } |
Info | 0:98ed24be8501 | 238 | #endif |
Info | 0:98ed24be8501 | 239 | |
Info | 0:98ed24be8501 | 240 | void ReadDataFromEEPROM(void){ |
Info | 0:98ed24be8501 | 241 | int i; |
Info | 0:98ed24be8501 | 242 | char bufData[8]; |
Info | 0:98ed24be8501 | 243 | char buf; |
Info | 0:98ed24be8501 | 244 | char buf1,buf2; |
Info | 0:98ed24be8501 | 245 | |
Info | 0:98ed24be8501 | 246 | eeprom.readAll( bufData, 8 ); |
Info | 0:98ed24be8501 | 247 | #ifdef debug |
Info | 0:98ed24be8501 | 248 | printf("0:%02x\n\r",bufData[0]); |
Info | 0:98ed24be8501 | 249 | printf("1:%02x\n\r",bufData[1]); |
Info | 0:98ed24be8501 | 250 | printf("2:%02x\n\r",bufData[2]); |
Info | 0:98ed24be8501 | 251 | printf("3:%02x\n\r",bufData[3]); |
Info | 0:98ed24be8501 | 252 | printf("4:%02x\n\r",bufData[4]); |
Info | 0:98ed24be8501 | 253 | printf("5:%02x\n\r",bufData[5]); |
Info | 0:98ed24be8501 | 254 | printf("6:%02x\n\r",bufData[6]); |
Info | 0:98ed24be8501 | 255 | printf("7:%02x\n\r",bufData[7]); |
Info | 0:98ed24be8501 | 256 | #endif |
Info | 0:98ed24be8501 | 257 | |
Info | 0:98ed24be8501 | 258 | for(i=0;i<4;++i){ |
Info | 0:98ed24be8501 | 259 | address[i] = bufData[i]; |
Info | 0:98ed24be8501 | 260 | def_myAddress = (def_myAddress<<8) + bufData[i]; |
Info | 0:98ed24be8501 | 261 | #ifdef debug |
Info | 0:98ed24be8501 | 262 | printf("address%d : %02x\n\r",i,address[i]); |
Info | 0:98ed24be8501 | 263 | #endif |
Info | 0:98ed24be8501 | 264 | } |
Info | 0:98ed24be8501 | 265 | #ifdef debug |
Info | 0:98ed24be8501 | 266 | printf("def_myAddress %08x\n\r",def_myAddress); |
Info | 0:98ed24be8501 | 267 | #endif |
Info | 0:98ed24be8501 | 268 | |
Info | 0:98ed24be8501 | 269 | buf = 0; |
Info | 0:98ed24be8501 | 270 | buf1 = iAddress1.read(); |
Info | 0:98ed24be8501 | 271 | if(buf1==0){ |
Info | 0:98ed24be8501 | 272 | buf1 = 1; |
Info | 0:98ed24be8501 | 273 | }else if(buf1==1){ |
Info | 0:98ed24be8501 | 274 | buf1 = 0; |
Info | 0:98ed24be8501 | 275 | } |
Info | 0:98ed24be8501 | 276 | buf = buf1; |
Info | 0:98ed24be8501 | 277 | |
Info | 0:98ed24be8501 | 278 | // buf = iAddress1.read(); |
Info | 0:98ed24be8501 | 279 | |
Info | 0:98ed24be8501 | 280 | |
Info | 0:98ed24be8501 | 281 | buf2 = iAddress2.read(); |
Info | 0:98ed24be8501 | 282 | if(buf2==0){ |
Info | 0:98ed24be8501 | 283 | buf2 = 1; |
Info | 0:98ed24be8501 | 284 | }else if(buf2==1){ |
Info | 0:98ed24be8501 | 285 | buf2 = 0; |
Info | 0:98ed24be8501 | 286 | } |
Info | 0:98ed24be8501 | 287 | |
Info | 0:98ed24be8501 | 288 | buf = (buf<<1) + buf2; |
Info | 0:98ed24be8501 | 289 | |
Info | 0:98ed24be8501 | 290 | // buf = (buf<<1) + iAddress2.read(); |
Info | 0:98ed24be8501 | 291 | |
Info | 0:98ed24be8501 | 292 | def_myAddress += buf; |
Info | 0:98ed24be8501 | 293 | |
Info | 0:98ed24be8501 | 294 | #ifdef debug |
Info | 0:98ed24be8501 | 295 | printf("def_myAddress %08x\n\r",def_myAddress); |
Info | 0:98ed24be8501 | 296 | #endif |
Info | 0:98ed24be8501 | 297 | |
Info | 0:98ed24be8501 | 298 | for(i=4;i<7;++i){ |
Info | 0:98ed24be8501 | 299 | exData[i-4] = bufData[i]; |
Info | 0:98ed24be8501 | 300 | #ifdef debug |
Info | 0:98ed24be8501 | 301 | printf("exData%d : %02x\n\r",i-4,exData[i-4]); |
Info | 0:98ed24be8501 | 302 | #endif |
Info | 0:98ed24be8501 | 303 | } |
Info | 0:98ed24be8501 | 304 | |
Info | 0:98ed24be8501 | 305 | gateData = bufData[7]; |
Info | 0:98ed24be8501 | 306 | #ifdef debug |
Info | 0:98ed24be8501 | 307 | printf("gateData : %02x\n\r",gateData); |
Info | 0:98ed24be8501 | 308 | #endif |
Info | 0:98ed24be8501 | 309 | |
Info | 0:98ed24be8501 | 310 | def_searchAddress = (def_myAddress & 0x3FFFFFFF) + 0x40000000; |
Info | 0:98ed24be8501 | 311 | |
Info | 0:98ed24be8501 | 312 | |
Info | 0:98ed24be8501 | 313 | } |
Info | 0:98ed24be8501 | 314 | |
Info | 0:98ed24be8501 | 315 | /* |
Info | 0:98ed24be8501 | 316 | void holdMode(int data){ |
Info | 0:98ed24be8501 | 317 | int inData[4]; |
Info | 0:98ed24be8501 | 318 | |
Info | 0:98ed24be8501 | 319 | myIO1.input(); |
Info | 0:98ed24be8501 | 320 | myIO2.input(); |
Info | 0:98ed24be8501 | 321 | myIO3.input(); |
Info | 0:98ed24be8501 | 322 | myIO4.input(); |
Info | 0:98ed24be8501 | 323 | |
Info | 0:98ed24be8501 | 324 | if(data == NOTHOLD){ |
Info | 0:98ed24be8501 | 325 | myIO1.mode(PullNone); |
Info | 0:98ed24be8501 | 326 | myIO2.mode(PullNone); |
Info | 0:98ed24be8501 | 327 | myIO3.mode(PullNone); |
Info | 0:98ed24be8501 | 328 | myIO4.mode(PullNone); |
Info | 0:98ed24be8501 | 329 | |
Info | 0:98ed24be8501 | 330 | myled = 0; |
Info | 0:98ed24be8501 | 331 | |
Info | 0:98ed24be8501 | 332 | }else if(data == HOLD){ |
Info | 0:98ed24be8501 | 333 | |
Info | 0:98ed24be8501 | 334 | myIO1.mode(PullDown); |
Info | 0:98ed24be8501 | 335 | myIO2.mode(PullDown); |
Info | 0:98ed24be8501 | 336 | myIO3.mode(PullDown); |
Info | 0:98ed24be8501 | 337 | myIO4.mode(PullDown); |
Info | 0:98ed24be8501 | 338 | |
Info | 0:98ed24be8501 | 339 | //read |
Info | 0:98ed24be8501 | 340 | inData[0] = myIO1; |
Info | 0:98ed24be8501 | 341 | inData[1] = myIO2; |
Info | 0:98ed24be8501 | 342 | inData[2] = myIO3; |
Info | 0:98ed24be8501 | 343 | inData[3] = myIO4; |
Info | 0:98ed24be8501 | 344 | //printf("inData[1]=%d\r\n",inData[1]); |
Info | 0:98ed24be8501 | 345 | |
Info | 0:98ed24be8501 | 346 | //closeAllGate(); |
Info | 0:98ed24be8501 | 347 | |
Info | 0:98ed24be8501 | 348 | //hold |
Info | 0:98ed24be8501 | 349 | myIO1.output(); |
Info | 0:98ed24be8501 | 350 | myIO2.output(); |
Info | 0:98ed24be8501 | 351 | myIO3.output(); |
Info | 0:98ed24be8501 | 352 | myIO4.output(); |
Info | 0:98ed24be8501 | 353 | |
Info | 0:98ed24be8501 | 354 | myIO1.mode(PullUp); |
Info | 0:98ed24be8501 | 355 | myIO2.mode(PullUp); |
Info | 0:98ed24be8501 | 356 | myIO3.mode(PullUp); |
Info | 0:98ed24be8501 | 357 | myIO4.mode(PullUp); |
Info | 0:98ed24be8501 | 358 | |
Info | 0:98ed24be8501 | 359 | myIO1 = inData[0]; |
Info | 0:98ed24be8501 | 360 | myIO2 = inData[1]; |
Info | 0:98ed24be8501 | 361 | myIO3 = inData[2]; |
Info | 0:98ed24be8501 | 362 | myIO4 = inData[3]; |
Info | 0:98ed24be8501 | 363 | |
Info | 0:98ed24be8501 | 364 | myled = 1; |
Info | 0:98ed24be8501 | 365 | |
Info | 0:98ed24be8501 | 366 | } |
Info | 0:98ed24be8501 | 367 | } |
Info | 0:98ed24be8501 | 368 | */ |
Info | 0:98ed24be8501 | 369 | |
Info | 0:98ed24be8501 | 370 | void holdIOs(int select, int readData){ |
Info | 0:98ed24be8501 | 371 | |
Info | 0:98ed24be8501 | 372 | if(select == 1){ |
Info | 0:98ed24be8501 | 373 | myIO1.mode(PullDown); |
Info | 0:98ed24be8501 | 374 | //hold |
Info | 0:98ed24be8501 | 375 | myIO1.output(); |
Info | 0:98ed24be8501 | 376 | myIO1.mode(PullUp); |
Info | 0:98ed24be8501 | 377 | myIO1 = readData; |
Info | 0:98ed24be8501 | 378 | |
Info | 0:98ed24be8501 | 379 | }else if(select == 2){ |
Info | 0:98ed24be8501 | 380 | myIO2.mode(PullDown); |
Info | 0:98ed24be8501 | 381 | //hold |
Info | 0:98ed24be8501 | 382 | myIO2.output(); |
Info | 0:98ed24be8501 | 383 | myIO2.mode(PullUp); |
Info | 0:98ed24be8501 | 384 | myIO2 = readData; |
Info | 0:98ed24be8501 | 385 | |
Info | 0:98ed24be8501 | 386 | }else if(select == 3){ |
Info | 0:98ed24be8501 | 387 | myIO3.mode(PullDown); |
Info | 0:98ed24be8501 | 388 | //hold |
Info | 0:98ed24be8501 | 389 | myIO3.output(); |
Info | 0:98ed24be8501 | 390 | myIO3.mode(PullUp); |
Info | 0:98ed24be8501 | 391 | myIO3 = readData; |
Info | 0:98ed24be8501 | 392 | |
Info | 0:98ed24be8501 | 393 | }else if(select == 4){ |
Info | 0:98ed24be8501 | 394 | myIO4.mode(PullDown); |
Info | 0:98ed24be8501 | 395 | //hold |
Info | 0:98ed24be8501 | 396 | myIO4.output(); |
Info | 0:98ed24be8501 | 397 | myIO4.mode(PullUp); |
Info | 0:98ed24be8501 | 398 | myIO4 = readData; |
Info | 0:98ed24be8501 | 399 | |
Info | 0:98ed24be8501 | 400 | } |
Info | 0:98ed24be8501 | 401 | |
Info | 0:98ed24be8501 | 402 | } |
Info | 0:98ed24be8501 | 403 | |
Info | 0:98ed24be8501 | 404 | void holdMode(uint16_t data){ |
Info | 0:98ed24be8501 | 405 | int inData[4]; |
Info | 0:98ed24be8501 | 406 | uint8_t data8,dataBuf; |
Info | 0:98ed24be8501 | 407 | data8 = data; |
Info | 0:98ed24be8501 | 408 | //printf("data = %d, data8 = %d\r\n",data,data8); |
Info | 0:98ed24be8501 | 409 | |
Info | 0:98ed24be8501 | 410 | myIO1.input(); |
Info | 0:98ed24be8501 | 411 | myIO2.input(); |
Info | 0:98ed24be8501 | 412 | myIO3.input(); |
Info | 0:98ed24be8501 | 413 | myIO4.input(); |
Info | 0:98ed24be8501 | 414 | //read |
Info | 0:98ed24be8501 | 415 | inData[0] = myIO1; |
Info | 0:98ed24be8501 | 416 | inData[1] = myIO2; |
Info | 0:98ed24be8501 | 417 | inData[2] = myIO3; |
Info | 0:98ed24be8501 | 418 | inData[3] = myIO4; |
Info | 0:98ed24be8501 | 419 | //printf("inData[0]=%d\r\n",inData[0]); |
Info | 0:98ed24be8501 | 420 | //printf("inData[1]=%d\r\n",inData[1]); |
Info | 0:98ed24be8501 | 421 | |
Info | 0:98ed24be8501 | 422 | if(data == NOTHOLD){ |
Info | 0:98ed24be8501 | 423 | //printf("NOTHOLD\r\n"); |
Info | 0:98ed24be8501 | 424 | myIO1.mode(PullNone); |
Info | 0:98ed24be8501 | 425 | myIO2.mode(PullNone); |
Info | 0:98ed24be8501 | 426 | myIO3.mode(PullNone); |
Info | 0:98ed24be8501 | 427 | myIO4.mode(PullNone); |
Info | 0:98ed24be8501 | 428 | |
Info | 0:98ed24be8501 | 429 | myled = 0; |
Info | 0:98ed24be8501 | 430 | |
Info | 0:98ed24be8501 | 431 | }else if(data == ALL_HOLD){ |
Info | 0:98ed24be8501 | 432 | //printf("ALL_HOLD\r\n"); |
Info | 0:98ed24be8501 | 433 | holdIOs(1,inData[0]); |
Info | 0:98ed24be8501 | 434 | holdIOs(2,inData[1]); |
Info | 0:98ed24be8501 | 435 | holdIOs(3,inData[2]); |
Info | 0:98ed24be8501 | 436 | holdIOs(4,inData[3]); |
Info | 0:98ed24be8501 | 437 | |
Info | 0:98ed24be8501 | 438 | myled = 1; |
Info | 0:98ed24be8501 | 439 | }else{ |
Info | 0:98ed24be8501 | 440 | dataBuf = data8 & 0b00000001; |
Info | 0:98ed24be8501 | 441 | if(dataBuf == 1){ |
Info | 0:98ed24be8501 | 442 | //printf("data 1\r\n"); |
Info | 0:98ed24be8501 | 443 | holdIOs(1,inData[0]); |
Info | 0:98ed24be8501 | 444 | } |
Info | 0:98ed24be8501 | 445 | |
Info | 0:98ed24be8501 | 446 | dataBuf = (data8>>1) & 0b00000001; |
Info | 0:98ed24be8501 | 447 | if(dataBuf == 1){ |
Info | 0:98ed24be8501 | 448 | //printf("data 2\r\n"); |
Info | 0:98ed24be8501 | 449 | holdIOs(2,inData[1]); |
Info | 0:98ed24be8501 | 450 | } |
Info | 0:98ed24be8501 | 451 | |
Info | 0:98ed24be8501 | 452 | dataBuf = (data8>>2) & 0b00000001; |
Info | 0:98ed24be8501 | 453 | if(dataBuf == 1){ |
Info | 0:98ed24be8501 | 454 | //printf("data 3\r\n"); |
Info | 0:98ed24be8501 | 455 | holdIOs(3,inData[2]); |
Info | 0:98ed24be8501 | 456 | } |
Info | 0:98ed24be8501 | 457 | |
Info | 0:98ed24be8501 | 458 | dataBuf = (data8>>3) & 0b00000001; |
Info | 0:98ed24be8501 | 459 | if(dataBuf == 1){ |
Info | 0:98ed24be8501 | 460 | //printf("data 4\r\n"); |
Info | 0:98ed24be8501 | 461 | holdIOs(4,inData[3]); |
Info | 0:98ed24be8501 | 462 | } |
Info | 0:98ed24be8501 | 463 | } |
Info | 0:98ed24be8501 | 464 | } |
Info | 0:98ed24be8501 | 465 | |
Info | 0:98ed24be8501 | 466 | |
Info | 0:98ed24be8501 | 467 | |
Info | 0:98ed24be8501 | 468 | |
Info | 0:98ed24be8501 | 469 | |
Info | 0:98ed24be8501 | 470 | void closeAllGate(void){ |
Info | 0:98ed24be8501 | 471 | |
Info | 0:98ed24be8501 | 472 | Gate1_1 = GateClose; |
Info | 0:98ed24be8501 | 473 | Gate1_2 = GateClose; |
Info | 0:98ed24be8501 | 474 | Gate1_3 = GateClose; |
Info | 0:98ed24be8501 | 475 | Gate1_4 = GateClose; |
Info | 0:98ed24be8501 | 476 | Gate2_MISO = GateClose; |
Info | 0:98ed24be8501 | 477 | Gate2_MOSI = GateClose; |
Info | 0:98ed24be8501 | 478 | Gate2_SCK = GateClose; |
Info | 0:98ed24be8501 | 479 | Gate2_4 = GateClose; |
Info | 0:98ed24be8501 | 480 | myFET = OffFET; |
Info | 0:98ed24be8501 | 481 | } |
Info | 0:98ed24be8501 | 482 | |
Info | 0:98ed24be8501 | 483 | void writeMode1(uint16_t mode1, uint16_t mode2){ |
Info | 0:98ed24be8501 | 484 | |
Info | 0:98ed24be8501 | 485 | switch(mode1){ |
Info | 0:98ed24be8501 | 486 | case 0x01: |
Info | 0:98ed24be8501 | 487 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 488 | //close all gate |
Info | 0:98ed24be8501 | 489 | myFET = OffFET; |
Info | 0:98ed24be8501 | 490 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 491 | closeAllGate(); |
Info | 0:98ed24be8501 | 492 | myled = 0; |
Info | 0:98ed24be8501 | 493 | #ifdef debug |
Info | 0:98ed24be8501 | 494 | printf("close gate\r\n"); |
Info | 0:98ed24be8501 | 495 | #endif |
Info | 0:98ed24be8501 | 496 | break; |
Info | 0:98ed24be8501 | 497 | |
Info | 0:98ed24be8501 | 498 | case 0x02: |
Info | 0:98ed24be8501 | 499 | //open gate normal IOs |
Info | 0:98ed24be8501 | 500 | closeAllGate(); |
Info | 0:98ed24be8501 | 501 | myFET = OffFET; |
Info | 0:98ed24be8501 | 502 | //myGate = GateOpen; |
Info | 0:98ed24be8501 | 503 | Gate1_1 = GateOpen; |
Info | 0:98ed24be8501 | 504 | Gate1_2 = GateOpen; |
Info | 0:98ed24be8501 | 505 | Gate1_3 = GateOpen; |
Info | 0:98ed24be8501 | 506 | Gate1_4 = GateOpen; |
Info | 0:98ed24be8501 | 507 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 508 | myled = 1; |
Info | 0:98ed24be8501 | 509 | #ifdef debug |
Info | 0:98ed24be8501 | 510 | printf("open gate\r\n"); |
Info | 0:98ed24be8501 | 511 | #endif |
Info | 0:98ed24be8501 | 512 | break; |
Info | 0:98ed24be8501 | 513 | |
Info | 0:98ed24be8501 | 514 | case 0x03: |
Info | 0:98ed24be8501 | 515 | //Detect mode ON |
Info | 0:98ed24be8501 | 516 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 517 | closeAllGate(); |
Info | 0:98ed24be8501 | 518 | wait_ms(0.01); |
Info | 0:98ed24be8501 | 519 | myFET = OnFET; |
Info | 0:98ed24be8501 | 520 | myled = 1; |
Info | 0:98ed24be8501 | 521 | #ifdef debug |
Info | 0:98ed24be8501 | 522 | printf("Detect mode ON\r\n"); |
Info | 0:98ed24be8501 | 523 | #endif |
Info | 0:98ed24be8501 | 524 | break; |
Info | 0:98ed24be8501 | 525 | |
Info | 0:98ed24be8501 | 526 | case 0x04: |
Info | 0:98ed24be8501 | 527 | //Detect mode OFF |
Info | 0:98ed24be8501 | 528 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 529 | closeAllGate(); |
Info | 0:98ed24be8501 | 530 | wait_ms(0.01); |
Info | 0:98ed24be8501 | 531 | myFET = OffFET; |
Info | 0:98ed24be8501 | 532 | myled = 0; |
Info | 0:98ed24be8501 | 533 | #ifdef debug |
Info | 0:98ed24be8501 | 534 | printf("Detect mode OFF\r\n"); |
Info | 0:98ed24be8501 | 535 | #endif |
Info | 0:98ed24be8501 | 536 | break; |
Info | 0:98ed24be8501 | 537 | |
Info | 0:98ed24be8501 | 538 | case 0x05://LED on |
Info | 0:98ed24be8501 | 539 | myled = 1; |
Info | 0:98ed24be8501 | 540 | #ifdef debug |
Info | 0:98ed24be8501 | 541 | printf("LED ON\r\n"); |
Info | 0:98ed24be8501 | 542 | #endif |
Info | 0:98ed24be8501 | 543 | break; |
Info | 0:98ed24be8501 | 544 | |
Info | 0:98ed24be8501 | 545 | case 0x06://LED off |
Info | 0:98ed24be8501 | 546 | myled = 0; |
Info | 0:98ed24be8501 | 547 | #ifdef debug |
Info | 0:98ed24be8501 | 548 | printf("LED OFF\r\n"); |
Info | 0:98ed24be8501 | 549 | #endif |
Info | 0:98ed24be8501 | 550 | break; |
Info | 0:98ed24be8501 | 551 | |
Info | 0:98ed24be8501 | 552 | case 0x07://SPI gate : MOSI MISO SCK CS |
Info | 0:98ed24be8501 | 553 | closeAllGate(); |
Info | 0:98ed24be8501 | 554 | Gate2_MOSI = GateOpen; |
Info | 0:98ed24be8501 | 555 | Gate2_MISO = GateOpen; |
Info | 0:98ed24be8501 | 556 | Gate2_SCK = GateOpen; |
Info | 0:98ed24be8501 | 557 | Gate1_4 = GateOpen; |
Info | 0:98ed24be8501 | 558 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 559 | myled = 1; |
Info | 0:98ed24be8501 | 560 | break; |
Info | 0:98ed24be8501 | 561 | |
Info | 0:98ed24be8501 | 562 | case 0x08://One way SPI gate : MOSI SCK CS EXtra IO |
Info | 0:98ed24be8501 | 563 | closeAllGate(); |
Info | 0:98ed24be8501 | 564 | Gate2_MOSI = GateOpen; |
Info | 0:98ed24be8501 | 565 | Gate1_2 = GateOpen; |
Info | 0:98ed24be8501 | 566 | Gate2_SCK = GateOpen; |
Info | 0:98ed24be8501 | 567 | Gate1_4 = GateOpen; |
Info | 0:98ed24be8501 | 568 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 569 | myled = 1; |
Info | 0:98ed24be8501 | 570 | |
Info | 0:98ed24be8501 | 571 | break; |
Info | 0:98ed24be8501 | 572 | |
Info | 0:98ed24be8501 | 573 | case 0x09://SCK as a clock : SCK Extra IOs |
Info | 0:98ed24be8501 | 574 | closeAllGate(); |
Info | 0:98ed24be8501 | 575 | Gate1_1 = GateOpen; |
Info | 0:98ed24be8501 | 576 | Gate1_2 = GateOpen; |
Info | 0:98ed24be8501 | 577 | Gate2_SCK = GateOpen; |
Info | 0:98ed24be8501 | 578 | Gate1_4 = GateOpen; |
Info | 0:98ed24be8501 | 579 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 580 | myled = 1; |
Info | 0:98ed24be8501 | 581 | |
Info | 0:98ed24be8501 | 582 | break; |
Info | 0:98ed24be8501 | 583 | |
Info | 0:98ed24be8501 | 584 | case 0x0A://SPI gate : MISO SCK CS |
Info | 0:98ed24be8501 | 585 | closeAllGate(); |
Info | 0:98ed24be8501 | 586 | Gate1_1 = GateOpen; |
Info | 0:98ed24be8501 | 587 | Gate2_MISO = GateOpen; |
Info | 0:98ed24be8501 | 588 | Gate2_SCK = GateOpen; |
Info | 0:98ed24be8501 | 589 | Gate1_4 = GateOpen; |
Info | 0:98ed24be8501 | 590 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 591 | myled = 1; |
Info | 0:98ed24be8501 | 592 | |
Info | 0:98ed24be8501 | 593 | break; |
Info | 0:98ed24be8501 | 594 | |
Info | 0:98ed24be8501 | 595 | case 0x0B://hold mode ON |
Info | 0:98ed24be8501 | 596 | //holdMode(HOLD); |
Info | 0:98ed24be8501 | 597 | holdMode(mode2); |
Info | 0:98ed24be8501 | 598 | closeAllGate(); |
Info | 0:98ed24be8501 | 599 | break; |
Info | 0:98ed24be8501 | 600 | |
Info | 0:98ed24be8501 | 601 | case 0x0C://hold mode OFF |
Info | 0:98ed24be8501 | 602 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 603 | //closeAllGate(); |
Info | 0:98ed24be8501 | 604 | break; |
Info | 0:98ed24be8501 | 605 | |
Info | 0:98ed24be8501 | 606 | default: |
Info | 0:98ed24be8501 | 607 | myFET = OffFET; |
Info | 0:98ed24be8501 | 608 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 609 | closeAllGate(); |
Info | 0:98ed24be8501 | 610 | holdMode(NOTHOLD); |
Info | 0:98ed24be8501 | 611 | #ifdef debug |
Info | 0:98ed24be8501 | 612 | printf("default\r\n"); |
Info | 0:98ed24be8501 | 613 | #endif |
Info | 0:98ed24be8501 | 614 | myled = 0; |
Info | 0:98ed24be8501 | 615 | break; |
Info | 0:98ed24be8501 | 616 | } |
Info | 0:98ed24be8501 | 617 | } |
Info | 0:98ed24be8501 | 618 | |
Info | 0:98ed24be8501 | 619 | |
Info | 0:98ed24be8501 | 620 | void writeMode2(uint16_t mode2){ |
Info | 0:98ed24be8501 | 621 | //not yet |
Info | 0:98ed24be8501 | 622 | |
Info | 0:98ed24be8501 | 623 | } |
Info | 0:98ed24be8501 | 624 | |
Info | 0:98ed24be8501 | 625 | void resetAll(void){ |
Info | 0:98ed24be8501 | 626 | myFET = OffFET; |
Info | 0:98ed24be8501 | 627 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 628 | closeAllGate(); |
Info | 0:98ed24be8501 | 629 | myled = 0; |
Info | 0:98ed24be8501 | 630 | } |
Info | 0:98ed24be8501 | 631 | |
Info | 0:98ed24be8501 | 632 | int main() { |
Info | 0:98ed24be8501 | 633 | uint8_t flg_mode = 0; |
Info | 0:98ed24be8501 | 634 | uint16_t SpiRdata = 0; |
Info | 0:98ed24be8501 | 635 | //uint16_t SpiAddr = 0; |
Info | 0:98ed24be8501 | 636 | uint32_t SpiAddr = 0; |
Info | 0:98ed24be8501 | 637 | uint16_t SpiIoNo = 0; |
Info | 0:98ed24be8501 | 638 | uint16_t SpiWrite = 0; |
Info | 0:98ed24be8501 | 639 | uint8_t resetCounter = 0; |
Info | 0:98ed24be8501 | 640 | uint8_t flg_ResetMode = 0; |
Info | 0:98ed24be8501 | 641 | |
Info | 0:98ed24be8501 | 642 | #ifdef ROMtest |
Info | 0:98ed24be8501 | 643 | EEPROMtest(); |
Info | 0:98ed24be8501 | 644 | while(1){ |
Info | 0:98ed24be8501 | 645 | ; |
Info | 0:98ed24be8501 | 646 | } |
Info | 0:98ed24be8501 | 647 | #endif |
Info | 0:98ed24be8501 | 648 | |
Info | 0:98ed24be8501 | 649 | #ifdef test |
Info | 0:98ed24be8501 | 650 | while(1){ |
Info | 0:98ed24be8501 | 651 | holdMode(0b00001001); |
Info | 0:98ed24be8501 | 652 | wait(1); |
Info | 0:98ed24be8501 | 653 | holdMode(0b00000110); |
Info | 0:98ed24be8501 | 654 | wait(1); |
Info | 0:98ed24be8501 | 655 | holdMode(0b00000101); |
Info | 0:98ed24be8501 | 656 | wait(1); |
Info | 0:98ed24be8501 | 657 | holdMode(0b00001010); |
Info | 0:98ed24be8501 | 658 | wait(1); |
Info | 0:98ed24be8501 | 659 | } |
Info | 0:98ed24be8501 | 660 | spiTest(); |
Info | 0:98ed24be8501 | 661 | IoTest(); |
Info | 0:98ed24be8501 | 662 | #endif |
Info | 0:98ed24be8501 | 663 | |
Info | 0:98ed24be8501 | 664 | myIO1.input(); |
Info | 0:98ed24be8501 | 665 | myIO2.input(); |
Info | 0:98ed24be8501 | 666 | myIO3.input(); |
Info | 0:98ed24be8501 | 667 | myIO4.input(); |
Info | 0:98ed24be8501 | 668 | myIO1.mode(PullNone); |
Info | 0:98ed24be8501 | 669 | myIO2.mode(PullNone); |
Info | 0:98ed24be8501 | 670 | myIO3.mode(PullNone); |
Info | 0:98ed24be8501 | 671 | myIO4.mode(PullNone); |
Info | 0:98ed24be8501 | 672 | |
Info | 0:98ed24be8501 | 673 | myled = 1; |
Info | 0:98ed24be8501 | 674 | wait(0.1); |
Info | 0:98ed24be8501 | 675 | myled = 0; |
Info | 0:98ed24be8501 | 676 | wait(0.1); |
Info | 0:98ed24be8501 | 677 | myled = 1; |
Info | 0:98ed24be8501 | 678 | wait(0.1); |
Info | 0:98ed24be8501 | 679 | myled = 0; |
Info | 0:98ed24be8501 | 680 | wait(0.1); |
Info | 0:98ed24be8501 | 681 | |
Info | 0:98ed24be8501 | 682 | //myUart.printf("test OK /r/n"); |
Info | 0:98ed24be8501 | 683 | //EEPROMtest(); |
Info | 0:98ed24be8501 | 684 | |
Info | 0:98ed24be8501 | 685 | ReadDataFromEEPROM(); |
Info | 0:98ed24be8501 | 686 | |
Info | 0:98ed24be8501 | 687 | //def_myAddress=0x80000001; |
Info | 0:98ed24be8501 | 688 | //while(1); |
Info | 0:98ed24be8501 | 689 | |
Info | 0:98ed24be8501 | 690 | myFET = OffFET; |
Info | 0:98ed24be8501 | 691 | //myGate = GateClose; |
Info | 0:98ed24be8501 | 692 | closeAllGate(); |
Info | 0:98ed24be8501 | 693 | |
Info | 0:98ed24be8501 | 694 | myled = 0; |
Info | 0:98ed24be8501 | 695 | device.format(8,0); |
Info | 0:98ed24be8501 | 696 | device.frequency(SPIfreq); |
Info | 0:98ed24be8501 | 697 | |
Info | 0:98ed24be8501 | 698 | device.reply(0x00); // Prime SPI with first reply |
Info | 0:98ed24be8501 | 699 | |
Info | 0:98ed24be8501 | 700 | /* |
Info | 0:98ed24be8501 | 701 | holdMode(HOLD); |
Info | 0:98ed24be8501 | 702 | while(1){ |
Info | 0:98ed24be8501 | 703 | ; |
Info | 0:98ed24be8501 | 704 | } |
Info | 0:98ed24be8501 | 705 | */ |
Info | 0:98ed24be8501 | 706 | |
Info | 0:98ed24be8501 | 707 | while(1) { |
Info | 0:98ed24be8501 | 708 | flg_ResetMode = 0; |
Info | 0:98ed24be8501 | 709 | if(device.receive()) { |
Info | 0:98ed24be8501 | 710 | SpiRdata = device.read(); |
Info | 0:98ed24be8501 | 711 | #ifdef debug |
Info | 0:98ed24be8501 | 712 | printf("device.receive() SpiRdata = %d\r\n",SpiRdata); |
Info | 0:98ed24be8501 | 713 | printf("flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 714 | #endif |
Info | 0:98ed24be8501 | 715 | //myled = 1; |
Info | 0:98ed24be8501 | 716 | if(SpiRdata == 0x00){ |
Info | 0:98ed24be8501 | 717 | ++resetCounter; |
Info | 0:98ed24be8501 | 718 | if(resetCounter >= 6){ |
Info | 0:98ed24be8501 | 719 | //if((SpiRdata == 0x00)&&(flg_mode != 1)){//reset |
Info | 0:98ed24be8501 | 720 | flg_mode = 0; |
Info | 0:98ed24be8501 | 721 | SpiAddr = 0; |
Info | 0:98ed24be8501 | 722 | SpiIoNo = 0; |
Info | 0:98ed24be8501 | 723 | SpiWrite = 0; |
Info | 0:98ed24be8501 | 724 | resetCounter = 0; |
Info | 0:98ed24be8501 | 725 | flg_ResetMode = 1; |
Info | 0:98ed24be8501 | 726 | |
Info | 0:98ed24be8501 | 727 | //resetAll(); |
Info | 0:98ed24be8501 | 728 | myled = 1; |
Info | 0:98ed24be8501 | 729 | wait(0.1); |
Info | 0:98ed24be8501 | 730 | myled = 0; |
Info | 0:98ed24be8501 | 731 | wait(0.1); |
Info | 0:98ed24be8501 | 732 | myled = 1; |
Info | 0:98ed24be8501 | 733 | wait(0.1); |
Info | 0:98ed24be8501 | 734 | myled = 0; |
Info | 0:98ed24be8501 | 735 | wait(0.1); |
Info | 0:98ed24be8501 | 736 | #ifdef debug |
Info | 0:98ed24be8501 | 737 | printf("reset\r\n"); |
Info | 0:98ed24be8501 | 738 | #endif |
Info | 0:98ed24be8501 | 739 | } |
Info | 0:98ed24be8501 | 740 | |
Info | 0:98ed24be8501 | 741 | }else{ |
Info | 0:98ed24be8501 | 742 | resetCounter = 0; |
Info | 0:98ed24be8501 | 743 | } |
Info | 0:98ed24be8501 | 744 | |
Info | 0:98ed24be8501 | 745 | //}else{//select mode |
Info | 0:98ed24be8501 | 746 | |
Info | 0:98ed24be8501 | 747 | if((flg_mode == 0) && (flg_ResetMode == 0)) {//read address data |
Info | 0:98ed24be8501 | 748 | SpiAddr = SpiRdata; |
Info | 0:98ed24be8501 | 749 | flg_mode = 1; |
Info | 0:98ed24be8501 | 750 | #ifdef debug |
Info | 0:98ed24be8501 | 751 | printf("0 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 752 | #endif |
Info | 0:98ed24be8501 | 753 | }else if(flg_mode == 1) {//read address data |
Info | 0:98ed24be8501 | 754 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:98ed24be8501 | 755 | flg_mode = 11; |
Info | 0:98ed24be8501 | 756 | #ifdef debug |
Info | 0:98ed24be8501 | 757 | printf("1 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 758 | #endif |
Info | 0:98ed24be8501 | 759 | }else if(flg_mode == 11) {//read address data |
Info | 0:98ed24be8501 | 760 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:98ed24be8501 | 761 | flg_mode = 12; |
Info | 0:98ed24be8501 | 762 | #ifdef debug |
Info | 0:98ed24be8501 | 763 | printf("11 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 764 | #endif |
Info | 0:98ed24be8501 | 765 | }else if(flg_mode == 12) {//read address data |
Info | 0:98ed24be8501 | 766 | SpiAddr = (SpiAddr<<8) + SpiRdata; |
Info | 0:98ed24be8501 | 767 | flg_mode = 2; |
Info | 0:98ed24be8501 | 768 | #ifdef debug |
Info | 0:98ed24be8501 | 769 | printf("12 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 770 | #endif |
Info | 0:98ed24be8501 | 771 | }else if(flg_mode == 2) {//read address data |
Info | 0:98ed24be8501 | 772 | SpiIoNo = SpiRdata; |
Info | 0:98ed24be8501 | 773 | flg_mode = 3; |
Info | 0:98ed24be8501 | 774 | #ifdef debug |
Info | 0:98ed24be8501 | 775 | printf("2 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 776 | #endif |
Info | 0:98ed24be8501 | 777 | }else if(flg_mode == 3) {//read fpga write data |
Info | 0:98ed24be8501 | 778 | SpiWrite = SpiRdata; |
Info | 0:98ed24be8501 | 779 | flg_mode = 0; |
Info | 0:98ed24be8501 | 780 | #ifdef debug |
Info | 0:98ed24be8501 | 781 | printf("3 flg_mode = %d\r\n",flg_mode); |
Info | 0:98ed24be8501 | 782 | printf("SpiAddr = %d\r\n",SpiAddr); |
Info | 0:98ed24be8501 | 783 | printf("SpiAddr = %08x\r\n",SpiAddr); |
Info | 0:98ed24be8501 | 784 | printf("SpiIoNo = %d\r\n",SpiIoNo); |
Info | 0:98ed24be8501 | 785 | printf("SpiWrite = %d\r\n",SpiWrite); |
Info | 0:98ed24be8501 | 786 | #endif |
Info | 0:98ed24be8501 | 787 | |
Info | 0:98ed24be8501 | 788 | //Select mode |
Info | 0:98ed24be8501 | 789 | if(SpiAddr == def_myAddress){ |
Info | 0:98ed24be8501 | 790 | #ifdef debug |
Info | 0:98ed24be8501 | 791 | printf("Select mode\r\n"); |
Info | 0:98ed24be8501 | 792 | #endif |
Info | 0:98ed24be8501 | 793 | writeMode1(SpiIoNo,SpiWrite); |
Info | 0:98ed24be8501 | 794 | //writeMode2(SpiWrite); |
Info | 0:98ed24be8501 | 795 | //myled = 1; |
Info | 0:98ed24be8501 | 796 | |
Info | 0:98ed24be8501 | 797 | /*while(1) { |
Info | 0:98ed24be8501 | 798 | for(int i=0;i<SpiIoNo;++i){ |
Info | 0:98ed24be8501 | 799 | myled = 1; |
Info | 0:98ed24be8501 | 800 | wait(0.2); |
Info | 0:98ed24be8501 | 801 | myled = 0; |
Info | 0:98ed24be8501 | 802 | wait(0.2); |
Info | 0:98ed24be8501 | 803 | } |
Info | 0:98ed24be8501 | 804 | myled = 1; |
Info | 0:98ed24be8501 | 805 | wait(1); |
Info | 0:98ed24be8501 | 806 | myled = 0; |
Info | 0:98ed24be8501 | 807 | wait(1); |
Info | 0:98ed24be8501 | 808 | }*/ |
Info | 0:98ed24be8501 | 809 | |
Info | 0:98ed24be8501 | 810 | flg_mode = 0; |
Info | 0:98ed24be8501 | 811 | SpiAddr = 0; |
Info | 0:98ed24be8501 | 812 | SpiIoNo = 0; |
Info | 0:98ed24be8501 | 813 | SpiWrite = 0; |
Info | 0:98ed24be8501 | 814 | |
Info | 0:98ed24be8501 | 815 | }else if((SpiAddr < 0x80000000)&&(SpiAddr <= def_searchAddress)){ |
Info | 0:98ed24be8501 | 816 | writeMode1(SpiIoNo,SpiWrite); |
Info | 0:98ed24be8501 | 817 | //writeMode2(SpiWrite); |
Info | 0:98ed24be8501 | 818 | }else{//not this address |
Info | 0:98ed24be8501 | 819 | //flug reset |
Info | 0:98ed24be8501 | 820 | flg_mode = 0; |
Info | 0:98ed24be8501 | 821 | SpiAddr = 0; |
Info | 0:98ed24be8501 | 822 | SpiIoNo = 0; |
Info | 0:98ed24be8501 | 823 | SpiWrite = 0; |
Info | 0:98ed24be8501 | 824 | myled = 0; |
Info | 0:98ed24be8501 | 825 | #ifdef debug |
Info | 0:98ed24be8501 | 826 | printf("Not me\r\n"); |
Info | 0:98ed24be8501 | 827 | #endif |
Info | 0:98ed24be8501 | 828 | |
Info | 0:98ed24be8501 | 829 | } |
Info | 0:98ed24be8501 | 830 | } |
Info | 0:98ed24be8501 | 831 | |
Info | 0:98ed24be8501 | 832 | } |
Info | 0:98ed24be8501 | 833 | } |
Info | 0:98ed24be8501 | 834 | } |