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