X2D lib first commit

Committer:
sev2000
Date:
Sun Mar 01 15:39:39 2020 +0000
Revision:
22:2f87ca62099c
Parent:
21:ffdda83d49e8
Child:
23:87c967bdb0ff
Change debug #define

Who changed what in which revision?

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