X2D lib first commit

Committer:
sev2000
Date:
Sun Nov 17 15:00:15 2019 +0000
Revision:
9:7825828972d4
Parent:
8:e134863cf7c3
Child:
10:65fd918fc224
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sev2000 0:9ef8edfe21bc 1 #include "mbed.h"
sev2000 0:9ef8edfe21bc 2 #include "X2D.h"
sev2000 0:9ef8edfe21bc 3
sev2000 9:7825828972d4 4 #define BUF_SIZE 1001
sev2000 7:eaa973daaf58 5 #define BIT_SIZE 160 //16Bytes + 0xFF + 0x00 + 3 bits + (n * 0 bit inserted by the X2D protocole to cut 0xFF
sev2000 0:9ef8edfe21bc 6
sev2000 7:eaa973daaf58 7 int processData(info_t *message);
sev2000 0:9ef8edfe21bc 8
sev2000 0:9ef8edfe21bc 9 DigitalOut Reg_Data(PC_1);
sev2000 0:9ef8edfe21bc 10 DigitalOut RxTx(PB_0);
sev2000 0:9ef8edfe21bc 11 DigitalOut Tx(PA_9);
sev2000 0:9ef8edfe21bc 12 DigitalOut UART(PA_0, 1);
sev2000 0:9ef8edfe21bc 13
sev2000 0:9ef8edfe21bc 14 DigitalIn CD(PC_0);
sev2000 0:9ef8edfe21bc 15 //DigitalIn Rx(PB_7);
sev2000 0:9ef8edfe21bc 16 DigitalIn BU(PA_4);
sev2000 0:9ef8edfe21bc 17 DigitalIn CLR(PA_1);
sev2000 0:9ef8edfe21bc 18 DigitalIn RSTO(PC_2);
sev2000 0:9ef8edfe21bc 19
sev2000 4:844c00dd0366 20 InterruptIn Rx(PB_7, PullUp);
sev2000 0:9ef8edfe21bc 21
sev2000 0:9ef8edfe21bc 22 pulse_t timeDiff;
sev2000 0:9ef8edfe21bc 23 CircularBuffer<pulse_t, BUF_SIZE> PulseWidth;
sev2000 7:eaa973daaf58 24 bool dataBits[BIT_SIZE]={0};
sev2000 0:9ef8edfe21bc 25
sev2000 0:9ef8edfe21bc 26
sev2000 0:9ef8edfe21bc 27 long startedAt=0;
sev2000 0:9ef8edfe21bc 28 long endedAt=0;
sev2000 0:9ef8edfe21bc 29
sev2000 0:9ef8edfe21bc 30 Timer xTime;
sev2000 0:9ef8edfe21bc 31
sev2000 0:9ef8edfe21bc 32 void getPulseF(void)
sev2000 0:9ef8edfe21bc 33 {
sev2000 0:9ef8edfe21bc 34 endedAt = xTime.read_us(); // set timer end for last pin
sev2000 0:9ef8edfe21bc 35 timeDiff.v = endedAt - startedAt;
sev2000 0:9ef8edfe21bc 36 timeDiff.pin = 1;
sev2000 0:9ef8edfe21bc 37 PulseWidth.push(timeDiff);
sev2000 0:9ef8edfe21bc 38 startedAt= endedAt; // set timer start for this pin
sev2000 0:9ef8edfe21bc 39 }
sev2000 0:9ef8edfe21bc 40
sev2000 0:9ef8edfe21bc 41 void getPulseR(void)
sev2000 0:9ef8edfe21bc 42 {
sev2000 0:9ef8edfe21bc 43 endedAt = xTime.read_us(); // set timer end for last pin
sev2000 0:9ef8edfe21bc 44 timeDiff.v = endedAt - startedAt;
sev2000 0:9ef8edfe21bc 45 timeDiff.pin = 0;
sev2000 0:9ef8edfe21bc 46 PulseWidth.push(timeDiff);
sev2000 0:9ef8edfe21bc 47 startedAt= endedAt; // set timer start for this pin
sev2000 0:9ef8edfe21bc 48 }
sev2000 0:9ef8edfe21bc 49
sev2000 4:844c00dd0366 50 void wait_posedge(void)
sev2000 4:844c00dd0366 51 {
sev2000 4:844c00dd0366 52 if(CLR != 0)
sev2000 4:844c00dd0366 53 {
sev2000 4:844c00dd0366 54 while(CLR != 0) ;
sev2000 4:844c00dd0366 55 }
sev2000 4:844c00dd0366 56 else ;
sev2000 4:844c00dd0366 57 while(CLR == 0) ;
sev2000 4:844c00dd0366 58 }
sev2000 4:844c00dd0366 59
sev2000 4:844c00dd0366 60 void wait_negedge(void)
sev2000 4:844c00dd0366 61 {
sev2000 4:844c00dd0366 62 if(CLR == 0)
sev2000 4:844c00dd0366 63 {
sev2000 4:844c00dd0366 64 while(CLR == 0) ;
sev2000 4:844c00dd0366 65 }
sev2000 4:844c00dd0366 66 else ;
sev2000 4:844c00dd0366 67 while(CLR != 0) ;
sev2000 4:844c00dd0366 68 }
sev2000 4:844c00dd0366 69
sev2000 4:844c00dd0366 70 unsigned long read_ctrl_reg(void)
sev2000 4:844c00dd0366 71 {
sev2000 4:844c00dd0366 72 unsigned long return_value = 0;
sev2000 4:844c00dd0366 73 unsigned long curr_bit = 0 ;
sev2000 4:844c00dd0366 74 int i ;
sev2000 4:844c00dd0366 75
sev2000 4:844c00dd0366 76 RxTx = 1;
sev2000 4:844c00dd0366 77 //Reg_Data = 0;
sev2000 4:844c00dd0366 78 wait_posedge();
sev2000 4:844c00dd0366 79 //wait_ms(10);
sev2000 4:844c00dd0366 80 Reg_Data = 1;
sev2000 4:844c00dd0366 81 RxTx = 1;
sev2000 4:844c00dd0366 82
sev2000 4:844c00dd0366 83 for(i=0;i<=23;i++)
sev2000 4:844c00dd0366 84 {
sev2000 4:844c00dd0366 85 //DBG("i= %d", i);
sev2000 4:844c00dd0366 86 wait_posedge();
sev2000 4:844c00dd0366 87 curr_bit = Rx ;
sev2000 4:844c00dd0366 88 return_value |= ((curr_bit) << (23-i)) ;
sev2000 4:844c00dd0366 89 }
sev2000 4:844c00dd0366 90 //wait_negedge();
sev2000 4:844c00dd0366 91
sev2000 4:844c00dd0366 92 Reg_Data = 0; //clr pin reg_data
sev2000 4:844c00dd0366 93
sev2000 4:844c00dd0366 94 wait(1) ;
sev2000 4:844c00dd0366 95 return return_value ;
sev2000 4:844c00dd0366 96 }
sev2000 4:844c00dd0366 97
sev2000 4:844c00dd0366 98 void write_ctrl_reg(unsigned long write_data)
sev2000 4:844c00dd0366 99 {
sev2000 4:844c00dd0366 100 int i ;
sev2000 4:844c00dd0366 101
sev2000 5:dcbebd42186a 102 Reg_Data = 1; //set pin reg_data to Register mode
sev2000 5:dcbebd42186a 103 RxTx = 0; //set pin rxtx to Tx mode
sev2000 6:e373216c80bf 104 wait_posedge(); //find posedge for Tcr
sev2000 4:844c00dd0366 105
sev2000 4:844c00dd0366 106 for(i=0;i<=23;i++) //low code effciency may result in wrong writing
sev2000 4:844c00dd0366 107 {
sev2000 4:844c00dd0366 108 if(((write_data >> (23-i)) & 0x1) == 0)
sev2000 4:844c00dd0366 109 {
sev2000 4:844c00dd0366 110 Tx = 0;
sev2000 4:844c00dd0366 111 }
sev2000 4:844c00dd0366 112 else
sev2000 4:844c00dd0366 113 {
sev2000 4:844c00dd0366 114 Tx = 1;
sev2000 4:844c00dd0366 115 }
sev2000 4:844c00dd0366 116 wait_posedge();
sev2000 4:844c00dd0366 117 }
sev2000 5:dcbebd42186a 118 RxTx = 1; //set pin rxtx to Rx
sev2000 4:844c00dd0366 119 Reg_Data = 0; //set pin reg_data
sev2000 4:844c00dd0366 120 Tx = 0;
sev2000 4:844c00dd0366 121 wait(0.1);
sev2000 4:844c00dd0366 122 }
sev2000 4:844c00dd0366 123
sev2000 1:265edb6bdd52 124 void SendBit(bool value)
sev2000 1:265edb6bdd52 125 {
sev2000 5:dcbebd42186a 126 // DBG("%d ", value);
sev2000 1:265edb6bdd52 127 Tx = !Tx;
sev2000 1:265edb6bdd52 128 if(value)
sev2000 1:265edb6bdd52 129 {
sev2000 1:265edb6bdd52 130 wait_us(800);
sev2000 1:265edb6bdd52 131 Tx = !Tx;
sev2000 1:265edb6bdd52 132 wait_us(800);
sev2000 1:265edb6bdd52 133 }
sev2000 1:265edb6bdd52 134 else
sev2000 1:265edb6bdd52 135 {
sev2000 1:265edb6bdd52 136 wait_us(1600);
sev2000 1:265edb6bdd52 137 }
sev2000 1:265edb6bdd52 138 }
sev2000 1:265edb6bdd52 139
sev2000 1:265edb6bdd52 140 void SendByte(char value)
sev2000 1:265edb6bdd52 141 {
sev2000 1:265edb6bdd52 142 int i=0;
sev2000 1:265edb6bdd52 143 bool bit;
sev2000 1:265edb6bdd52 144 static char cnt=0;
sev2000 1:265edb6bdd52 145
sev2000 1:265edb6bdd52 146 pc.printf("%0.2X ", value);
sev2000 1:265edb6bdd52 147 for(i=0; i<8; i++)
sev2000 1:265edb6bdd52 148 {
sev2000 1:265edb6bdd52 149 bit = (bool)(0x01 & (value>>i)); // LSB first
sev2000 1:265edb6bdd52 150 SendBit(bit);
sev2000 1:265edb6bdd52 151 if(bit)
sev2000 1:265edb6bdd52 152 cnt++;
sev2000 1:265edb6bdd52 153 else
sev2000 1:265edb6bdd52 154 cnt=0;
sev2000 1:265edb6bdd52 155 if(cnt == 5)
sev2000 7:eaa973daaf58 156 {
sev2000 1:265edb6bdd52 157 SendBit(0);
sev2000 7:eaa973daaf58 158 cnt=0;
sev2000 7:eaa973daaf58 159 }
sev2000 1:265edb6bdd52 160 }
sev2000 1:265edb6bdd52 161 }
sev2000 1:265edb6bdd52 162
sev2000 1:265edb6bdd52 163 void SendPreamble(void)
sev2000 1:265edb6bdd52 164 {
sev2000 1:265edb6bdd52 165 int i;
sev2000 1:265edb6bdd52 166
sev2000 7:eaa973daaf58 167 //pc.printf("\r\n");
sev2000 1:265edb6bdd52 168 Tx=1;
sev2000 1:265edb6bdd52 169 for(i=0; i<17; i++)
sev2000 1:265edb6bdd52 170 SendBit(0);
sev2000 1:265edb6bdd52 171 for(i=0; i<6; i++)
sev2000 1:265edb6bdd52 172 SendBit(1);
sev2000 1:265edb6bdd52 173 SendBit(0);
sev2000 1:265edb6bdd52 174
sev2000 1:265edb6bdd52 175 }
sev2000 1:265edb6bdd52 176
sev2000 1:265edb6bdd52 177 void SendPostamble(void)
sev2000 1:265edb6bdd52 178 {
sev2000 1:265edb6bdd52 179 int i;
sev2000 1:265edb6bdd52 180
sev2000 1:265edb6bdd52 181 for(i=0; i<8; i++) // Send 0xFF
sev2000 1:265edb6bdd52 182 SendBit(1);
sev2000 1:265edb6bdd52 183 for(i=0; i<8; i++)
sev2000 1:265edb6bdd52 184 SendBit(0);
sev2000 1:265edb6bdd52 185
sev2000 1:265edb6bdd52 186 }
sev2000 1:265edb6bdd52 187
sev2000 1:265edb6bdd52 188 void SendFrame(char *data, int length)
sev2000 1:265edb6bdd52 189 {
sev2000 1:265edb6bdd52 190
sev2000 1:265edb6bdd52 191 int i, chksum=0;
sev2000 1:265edb6bdd52 192
sev2000 1:265edb6bdd52 193 RxTx = 0; //set pin rxtx to Tx
sev2000 7:eaa973daaf58 194 Tx=0;
sev2000 7:eaa973daaf58 195 // wait_us(500);
sev2000 7:eaa973daaf58 196 //wait_ms(1);
sev2000 1:265edb6bdd52 197
sev2000 1:265edb6bdd52 198 SendPreamble();
sev2000 1:265edb6bdd52 199
sev2000 1:265edb6bdd52 200 for(i=0; i<length; i++)
sev2000 1:265edb6bdd52 201 {
sev2000 1:265edb6bdd52 202 SendByte(data[i]);
sev2000 1:265edb6bdd52 203 chksum += data[i];
sev2000 1:265edb6bdd52 204 }
sev2000 1:265edb6bdd52 205 chksum = ~chksum +1;
sev2000 1:265edb6bdd52 206
sev2000 1:265edb6bdd52 207 SendByte((char)(chksum>>8));
sev2000 1:265edb6bdd52 208 SendByte((char)chksum);
sev2000 1:265edb6bdd52 209 SendPostamble();
sev2000 1:265edb6bdd52 210
sev2000 7:eaa973daaf58 211 // Tx=1;
sev2000 7:eaa973daaf58 212
sev2000 1:265edb6bdd52 213 RxTx = 1; //set pin rxtx to Rx
sev2000 7:eaa973daaf58 214 pc.printf("\r\n");
sev2000 1:265edb6bdd52 215 }
sev2000 1:265edb6bdd52 216
sev2000 7:eaa973daaf58 217 void SendCmd(int prog, int zone)
sev2000 1:265edb6bdd52 218 {
sev2000 1:265edb6bdd52 219 char tab[8]={0xF1,0xE6,0x01,0x10,0x01,0x00,0x03,0x64}; // Zone1 Sun
sev2000 1:265edb6bdd52 220 char maison[2]={0xF1,0xE6};
sev2000 7:eaa973daaf58 221 char source = 0x02;
sev2000 1:265edb6bdd52 222 char dest = 0;
sev2000 7:eaa973daaf58 223 char trans = 0x02;
sev2000 7:eaa973daaf58 224 // control 1 char
sev2000 7:eaa973daaf58 225 // information 0-8 char
sev2000 7:eaa973daaf58 226
sev2000 1:265edb6bdd52 227 int i;
sev2000 1:265edb6bdd52 228
sev2000 1:265edb6bdd52 229 dest = 0x10 + zone-1;
sev2000 7:eaa973daaf58 230 tab[2] = source;
sev2000 1:265edb6bdd52 231 tab[3] = dest;
sev2000 7:eaa973daaf58 232 tab[4] = trans;
sev2000 1:265edb6bdd52 233 tab[6] = prog;
sev2000 1:265edb6bdd52 234
sev2000 1:265edb6bdd52 235 for(i=0; i<3; i++)
sev2000 1:265edb6bdd52 236 {
sev2000 1:265edb6bdd52 237 tab[5]=i; //message count
sev2000 1:265edb6bdd52 238 SendFrame(tab, 8);
sev2000 1:265edb6bdd52 239 wait_ms(30);
sev2000 1:265edb6bdd52 240 }
sev2000 1:265edb6bdd52 241 }
sev2000 1:265edb6bdd52 242
sev2000 7:eaa973daaf58 243 void SendCmda(char *tab, int lenght)
sev2000 7:eaa973daaf58 244 {
sev2000 7:eaa973daaf58 245 char buf[16];
sev2000 7:eaa973daaf58 246 int i;
sev2000 7:eaa973daaf58 247
sev2000 7:eaa973daaf58 248 /* dest = 0x10 + zone-1;
sev2000 7:eaa973daaf58 249 tab[3] = dest;
sev2000 7:eaa973daaf58 250 tab[6] = prog;
sev2000 7:eaa973daaf58 251 */
sev2000 7:eaa973daaf58 252 memcpy(buf, tab, lenght);
sev2000 7:eaa973daaf58 253 for(i=0; i<3; i++)
sev2000 7:eaa973daaf58 254 {
sev2000 7:eaa973daaf58 255 buf[5] = tab[5] + i; //message count
sev2000 7:eaa973daaf58 256 SendFrame(buf, lenght);
sev2000 7:eaa973daaf58 257 wait_ms(30);
sev2000 7:eaa973daaf58 258 }
sev2000 7:eaa973daaf58 259 }
sev2000 7:eaa973daaf58 260
sev2000 7:eaa973daaf58 261
sev2000 7:eaa973daaf58 262 int detectPreamble(info_t *message)
sev2000 0:9ef8edfe21bc 263 {
sev2000 0:9ef8edfe21bc 264 pulse_t pulse;
sev2000 0:9ef8edfe21bc 265 int cnt = 0;
sev2000 0:9ef8edfe21bc 266 char tmp[32]={0};
sev2000 7:eaa973daaf58 267 char timing[8*BUF_SIZE]={0};
sev2000 0:9ef8edfe21bc 268 char state=0;
sev2000 0:9ef8edfe21bc 269 char s=0, l=0, bit_ptr=0;
sev2000 0:9ef8edfe21bc 270
sev2000 0:9ef8edfe21bc 271 while (!PulseWidth.empty())
sev2000 0:9ef8edfe21bc 272 {
sev2000 0:9ef8edfe21bc 273 PulseWidth.pop(pulse);
sev2000 0:9ef8edfe21bc 274 sprintf(tmp, "%d, %ld|", pulse.pin, pulse.v);
sev2000 7:eaa973daaf58 275
sev2000 7:eaa973daaf58 276 //strcat(timing, tmp);
sev2000 7:eaa973daaf58 277 //pc.printf("%s ", tmp);
sev2000 0:9ef8edfe21bc 278
sev2000 0:9ef8edfe21bc 279 if ((pulse.v > 700) && (pulse.v < 1000))
sev2000 0:9ef8edfe21bc 280 { // short off detected
sev2000 0:9ef8edfe21bc 281 s++;
sev2000 0:9ef8edfe21bc 282 l=0;
sev2000 0:9ef8edfe21bc 283 }
sev2000 0:9ef8edfe21bc 284 else if ((pulse.v > 1500) && (pulse.v < 1800))
sev2000 0:9ef8edfe21bc 285 { // long off detected
sev2000 0:9ef8edfe21bc 286 l++;
sev2000 0:9ef8edfe21bc 287 s=0;
sev2000 0:9ef8edfe21bc 288 }
sev2000 0:9ef8edfe21bc 289 else
sev2000 0:9ef8edfe21bc 290 {
sev2000 0:9ef8edfe21bc 291 l=0;
sev2000 0:9ef8edfe21bc 292 s=0;
sev2000 0:9ef8edfe21bc 293 bit_ptr=0;
sev2000 0:9ef8edfe21bc 294 state=0;
sev2000 0:9ef8edfe21bc 295 }
sev2000 0:9ef8edfe21bc 296 switch(state)
sev2000 0:9ef8edfe21bc 297 {
sev2000 0:9ef8edfe21bc 298 case 0: // Detect preamble
sev2000 0:9ef8edfe21bc 299 if(s >= 12) // out of 12
sev2000 0:9ef8edfe21bc 300 state=1;
sev2000 0:9ef8edfe21bc 301 //pc.printf("%d ", s);
sev2000 0:9ef8edfe21bc 302 break;
sev2000 0:9ef8edfe21bc 303 case 1: // wait start bit (first long)
sev2000 0:9ef8edfe21bc 304 //pc.printf("OK2");
sev2000 0:9ef8edfe21bc 305 s=0;
sev2000 0:9ef8edfe21bc 306 if (l==1)
sev2000 0:9ef8edfe21bc 307 {
sev2000 0:9ef8edfe21bc 308 state = 2;
sev2000 0:9ef8edfe21bc 309 //bit_ptr++; inculde start bit in payload
sev2000 0:9ef8edfe21bc 310 }
sev2000 0:9ef8edfe21bc 311 l=0;
sev2000 0:9ef8edfe21bc 312 break;
sev2000 0:9ef8edfe21bc 313 case 2:
sev2000 0:9ef8edfe21bc 314 //pc.printf(" %d", pulse.v);
sev2000 0:9ef8edfe21bc 315
sev2000 0:9ef8edfe21bc 316 if (s == 2)
sev2000 0:9ef8edfe21bc 317 {
sev2000 0:9ef8edfe21bc 318 dataBits[bit_ptr] = 1;
sev2000 0:9ef8edfe21bc 319 l=0;
sev2000 0:9ef8edfe21bc 320 s=0;
sev2000 0:9ef8edfe21bc 321 bit_ptr++;
sev2000 0:9ef8edfe21bc 322 }
sev2000 0:9ef8edfe21bc 323 if (l == 1 && s==0)
sev2000 0:9ef8edfe21bc 324 {
sev2000 0:9ef8edfe21bc 325 dataBits[bit_ptr] = 0;
sev2000 0:9ef8edfe21bc 326 l=0;
sev2000 0:9ef8edfe21bc 327 s=0;
sev2000 0:9ef8edfe21bc 328 bit_ptr++;
sev2000 0:9ef8edfe21bc 329 }
sev2000 0:9ef8edfe21bc 330 if(bit_ptr > BIT_SIZE)
sev2000 0:9ef8edfe21bc 331 {
sev2000 0:9ef8edfe21bc 332 state=0;
sev2000 0:9ef8edfe21bc 333 bit_ptr=0;
sev2000 7:eaa973daaf58 334 pc.printf("Frame too long : dropped\r\n");
sev2000 0:9ef8edfe21bc 335 }
sev2000 0:9ef8edfe21bc 336
sev2000 0:9ef8edfe21bc 337 break;
sev2000 0:9ef8edfe21bc 338 }
sev2000 0:9ef8edfe21bc 339
sev2000 0:9ef8edfe21bc 340 if(pulse.v > 30000 && cnt>0) // End of frame
sev2000 0:9ef8edfe21bc 341 {
sev2000 7:eaa973daaf58 342 processData(message);
sev2000 0:9ef8edfe21bc 343 //timing[0]=0;
sev2000 0:9ef8edfe21bc 344 state=0;
sev2000 0:9ef8edfe21bc 345 bit_ptr=0;
sev2000 0:9ef8edfe21bc 346 //PulseWidth.reset();
sev2000 0:9ef8edfe21bc 347 // WARN(" Waiting...");
sev2000 0:9ef8edfe21bc 348 }
sev2000 0:9ef8edfe21bc 349
sev2000 0:9ef8edfe21bc 350 cnt++;
sev2000 0:9ef8edfe21bc 351 }
sev2000 0:9ef8edfe21bc 352 if (cnt>0) // if buffer wasn't empty
sev2000 0:9ef8edfe21bc 353 {
sev2000 7:eaa973daaf58 354 processData(message);
sev2000 0:9ef8edfe21bc 355 //pc.printf("%s\r\n", timing);
sev2000 0:9ef8edfe21bc 356 }
sev2000 0:9ef8edfe21bc 357
sev2000 0:9ef8edfe21bc 358 return(0);
sev2000 0:9ef8edfe21bc 359 }
sev2000 0:9ef8edfe21bc 360
sev2000 7:eaa973daaf58 361 int processData(info_t *message)
sev2000 0:9ef8edfe21bc 362 {
sev2000 0:9ef8edfe21bc 363 int x=0;
sev2000 0:9ef8edfe21bc 364 int i = 0;
sev2000 0:9ef8edfe21bc 365 int j= 0;
sev2000 0:9ef8edfe21bc 366 char nibble[18]={0}, cnt=0;
sev2000 7:eaa973daaf58 367 int chksum=0, ETX_pos=16;
sev2000 0:9ef8edfe21bc 368
sev2000 7:eaa973daaf58 369 for (i=0; i<ETX_pos; i++)
sev2000 0:9ef8edfe21bc 370 {
sev2000 0:9ef8edfe21bc 371 for (j=0;j<8;j++)
sev2000 0:9ef8edfe21bc 372 {
sev2000 0:9ef8edfe21bc 373 if ( dataBits[x])
sev2000 0:9ef8edfe21bc 374 {
sev2000 0:9ef8edfe21bc 375 nibble[i] |= 1<<j;
sev2000 0:9ef8edfe21bc 376 cnt++;
sev2000 0:9ef8edfe21bc 377 }
sev2000 0:9ef8edfe21bc 378 else
sev2000 0:9ef8edfe21bc 379 {
sev2000 0:9ef8edfe21bc 380 if (cnt == 5)
sev2000 0:9ef8edfe21bc 381 j--;
sev2000 0:9ef8edfe21bc 382 cnt=0;
sev2000 0:9ef8edfe21bc 383 }
sev2000 0:9ef8edfe21bc 384 dataBits[x] =0; // clean variable
sev2000 0:9ef8edfe21bc 385 x++;
sev2000 0:9ef8edfe21bc 386 }
sev2000 0:9ef8edfe21bc 387 if (cnt >= 8) // End of Frame detection
sev2000 7:eaa973daaf58 388 ETX_pos=i;
sev2000 0:9ef8edfe21bc 389 }
sev2000 0:9ef8edfe21bc 390
sev2000 0:9ef8edfe21bc 391
sev2000 7:eaa973daaf58 392 for (i=0; i<ETX_pos-2; i++) // Calculate Checksum
sev2000 0:9ef8edfe21bc 393 chksum += nibble[i];
sev2000 0:9ef8edfe21bc 394 chksum = ~chksum +1;
sev2000 0:9ef8edfe21bc 395
sev2000 0:9ef8edfe21bc 396 #ifdef __DEBUG__
sev2000 7:eaa973daaf58 397 for (i=0; i<ETX_pos; i++)
sev2000 0:9ef8edfe21bc 398 pc.printf("%0.2X ",nibble[i]);
sev2000 7:eaa973daaf58 399 if ( (char)(chksum>>8) != nibble[ETX_pos-2] || (char)chksum != nibble[ETX_pos-1] )
sev2000 0:9ef8edfe21bc 400 pc.printf(" CRC Error");
sev2000 0:9ef8edfe21bc 401 pc.printf("\r\n");
sev2000 0:9ef8edfe21bc 402 #endif
sev2000 7:eaa973daaf58 403
sev2000 7:eaa973daaf58 404 if((nibble[5]&0xF0) == 0 && nibble[2]==1 && nibble[4]!=nibble[2])
sev2000 7:eaa973daaf58 405 {
sev2000 7:eaa973daaf58 406 message->zone=nibble[3]-0x10 +1;
sev2000 7:eaa973daaf58 407 message->mode=(h_mode)nibble[6];
sev2000 7:eaa973daaf58 408 }
sev2000 7:eaa973daaf58 409
sev2000 0:9ef8edfe21bc 410 return 0;
sev2000 0:9ef8edfe21bc 411 }
sev2000 0:9ef8edfe21bc 412
sev2000 0:9ef8edfe21bc 413
sev2000 0:9ef8edfe21bc 414 void Init_X2D()
sev2000 0:9ef8edfe21bc 415 {
sev2000 6:e373216c80bf 416 unsigned long Ctrl_Reg = 0;
sev2000 5:dcbebd42186a 417
sev2000 4:844c00dd0366 418 Rx.fall(&getPulseF);
sev2000 4:844c00dd0366 419 Rx.rise(&getPulseR);
sev2000 0:9ef8edfe21bc 420 UART = 1;
sev2000 5:dcbebd42186a 421
sev2000 5:dcbebd42186a 422 write_ctrl_reg(0x01E22F); // 1200 baud, deviation =1, Asynchrone
sev2000 5:dcbebd42186a 423 Ctrl_Reg = read_ctrl_reg();
sev2000 5:dcbebd42186a 424 DBG("Ctrl_reg = %X", Ctrl_Reg);
sev2000 5:dcbebd42186a 425
sev2000 0:9ef8edfe21bc 426 RxTx = 1; //set pin rxtx to Rx
sev2000 0:9ef8edfe21bc 427 Reg_Data = 0; //set pin reg_data
sev2000 0:9ef8edfe21bc 428 Tx = 0;
sev2000 0:9ef8edfe21bc 429
sev2000 0:9ef8edfe21bc 430 xTime.start();
sev2000 0:9ef8edfe21bc 431 xTime.reset();
sev2000 0:9ef8edfe21bc 432 startedAt = xTime.read_us(); // set initial timer end
sev2000 0:9ef8edfe21bc 433
sev2000 0:9ef8edfe21bc 434 //thread.start(getData);
sev2000 0:9ef8edfe21bc 435 }