app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Committer:
ericbisson
Date:
Tue Mar 07 19:56:37 2017 +0000
Revision:
26:6ffbf3161f6f
Parent:
25:d0c348c3c85d
commit apr?s validation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ericbisson 15:7c2e70c36b98 1 #include "CRC16.h"
ericbisson 7:5501dbea5650 2 #include "mbed.h"
ericbisson 0:c637467eeb8f 3 #include "rtos.h"
ericbisson 25:d0c348c3c85d 4
ericbisson 8:5b87b1f9d91f 5 Thread ThreadLecture;
ericbisson 24:2034362db66f 6 Thread ThreadWrite;
ericbisson 15:7c2e70c36b98 7 const int PREAMBULE = 0b01010101;
ericbisson 15:7c2e70c36b98 8 const int START = 0b01111110;
ericbisson 15:7c2e70c36b98 9 const int END = 0b01111110;
ericbisson 14:bd909277eb13 10 Serial pc(USBTX, USBRX);
ericbisson 24:2034362db66f 11 DigitalOut out(p13);
ericbisson 24:2034362db66f 12 DigitalOut myled1(LED1);
ericbisson 24:2034362db66f 13 DigitalOut myled2(LED2);
ericbisson 25:d0c348c3c85d 14 InterruptIn interrupt_p30(p30);
ericbisson 25:d0c348c3c85d 15 CRC16 myCRC;
ericbisson 25:d0c348c3c85d 16 Timer t;
ericbisson 25:d0c348c3c85d 17 int periode = 0;
ericbisson 25:d0c348c3c85d 18 char sync = 0;
ericbisson 2:c6465d4e82d2 19
JoeyDionne 12:a55f77a0e188 20 bool transmit = false; //faux pour le debut de demi-periode d'envoie d'un bit, vrai pour la 2ème demi-periode
JoeyDionne 12:a55f77a0e188 21 char trame_sent[80]; //tableau de la trame à envoyer
JoeyDionne 12:a55f77a0e188 22 uint8_t byte_sent_pos, //position de l'octet dans le tableau d'envoi
JoeyDionne 12:a55f77a0e188 23 trame_length; //longueur de la trame complete
ericbisson 20:ac8682cf923b 24
JoeyDionne 23:9c4e4898b741 25 signed char bit_sent; //position du bit de l'octet en cours d'envoi (du MSB au LSB) 7..0
JoeyDionne 23:9c4e4898b741 26 bool bTimer1 = false;
JoeyDionne 11:b27d1a83f688 27
ericbisson 26:6ffbf3161f6f 28 bool bitSent[320];
ericbisson 26:6ffbf3161f6f 29 int length = 0;
ericbisson 26:6ffbf3161f6f 30 bool IsFinish = false;
ericbisson 26:6ffbf3161f6f 31
JoeyDionne 23:9c4e4898b741 32 //encodage manchester
JoeyDionne 23:9c4e4898b741 33 bool codeManchester(bool bit, bool clock)
JoeyDionne 23:9c4e4898b741 34 {
JoeyDionne 23:9c4e4898b741 35 return (bit == clock);
JoeyDionne 23:9c4e4898b741 36 }
JoeyDionne 23:9c4e4898b741 37
JoeyDionne 23:9c4e4898b741 38 //Envoie de données bit par bit
JoeyDionne 23:9c4e4898b741 39 void send_data()
ericbisson 25:d0c348c3c85d 40 {
ericbisson 26:6ffbf3161f6f 41 bitSent[length] = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), bTimer1);
ericbisson 26:6ffbf3161f6f 42 out = bitSent[length];
ericbisson 26:6ffbf3161f6f 43 length++;
JoeyDionne 23:9c4e4898b741 44 if(bTimer1)
JoeyDionne 23:9c4e4898b741 45 {
JoeyDionne 23:9c4e4898b741 46 bit_sent--;
JoeyDionne 23:9c4e4898b741 47 }
JoeyDionne 23:9c4e4898b741 48 bTimer1 = !bTimer1; //varier entre la 1ere et 2eme partie de demi-periode
JoeyDionne 23:9c4e4898b741 49 if (bit_sent < 0) //Si l'octet a ete envoye
JoeyDionne 23:9c4e4898b741 50 {
JoeyDionne 23:9c4e4898b741 51 bit_sent = 7; //remettre la position initiale pour le prochain octet
JoeyDionne 23:9c4e4898b741 52 byte_sent_pos++; //incrementer l'octet
JoeyDionne 23:9c4e4898b741 53 if (byte_sent_pos >= trame_length) //Si la trame a ete envoyee
JoeyDionne 23:9c4e4898b741 54 {
JoeyDionne 23:9c4e4898b741 55 byte_sent_pos = 0;
JoeyDionne 23:9c4e4898b741 56 bTimer1 = false;
ericbisson 26:6ffbf3161f6f 57 IsFinish = true;
JoeyDionne 23:9c4e4898b741 58 LPC_TIM1->TCR = 0; // disable Timer
JoeyDionne 23:9c4e4898b741 59 }
JoeyDionne 23:9c4e4898b741 60 }
JoeyDionne 23:9c4e4898b741 61 }
JoeyDionne 23:9c4e4898b741 62
ericbisson 25:d0c348c3c85d 63 char messageBuffer[80];
ericbisson 25:d0c348c3c85d 64 char messageLength = 0;
ericbisson 25:d0c348c3c85d 65 char lastpos = 0;
ericbisson 25:d0c348c3c85d 66 void show_message()
ericbisson 25:d0c348c3c85d 67 {
ericbisson 25:d0c348c3c85d 68 while (true)
ericbisson 25:d0c348c3c85d 69 {
ericbisson 25:d0c348c3c85d 70 ThreadLecture.signal_wait(1);
ericbisson 26:6ffbf3161f6f 71 pc.printf("Message Recu :\n\r");
ericbisson 25:d0c348c3c85d 72 for (int i = lastpos; i < messageLength; i++)
ericbisson 25:d0c348c3c85d 73 {
ericbisson 25:d0c348c3c85d 74 pc.printf("%x",messageBuffer[i]);
ericbisson 25:d0c348c3c85d 75
ericbisson 25:d0c348c3c85d 76 }
ericbisson 26:6ffbf3161f6f 77 pc.printf("\n\rFin Message\n\r");
ericbisson 25:d0c348c3c85d 78 lastpos = messageLength;
ericbisson 25:d0c348c3c85d 79 }
ericbisson 25:d0c348c3c85d 80 }
ericbisson 25:d0c348c3c85d 81
ericbisson 25:d0c348c3c85d 82 char byte = 0;
ericbisson 25:d0c348c3c85d 83 char shift = 0;
ericbisson 25:d0c348c3c85d 84 char totalsize = 7;
ericbisson 25:d0c348c3c85d 85 void resetMessage()
ericbisson 25:d0c348c3c85d 86 {
ericbisson 25:d0c348c3c85d 87 byte = 0;
ericbisson 25:d0c348c3c85d 88 shift = 0;
ericbisson 25:d0c348c3c85d 89 totalsize = 7;
ericbisson 25:d0c348c3c85d 90 messageLength = 0;
ericbisson 25:d0c348c3c85d 91 lastpos = 0;
ericbisson 25:d0c348c3c85d 92 }
ericbisson 25:d0c348c3c85d 93
ericbisson 25:d0c348c3c85d 94 void read(bool bit)
ericbisson 25:d0c348c3c85d 95 {
ericbisson 25:d0c348c3c85d 96 int time = t.read_us();
ericbisson 25:d0c348c3c85d 97 if (sync == 1)
ericbisson 25:d0c348c3c85d 98 {
ericbisson 25:d0c348c3c85d 99 periode = time;
ericbisson 25:d0c348c3c85d 100 }
ericbisson 25:d0c348c3c85d 101 sync++;
ericbisson 25:d0c348c3c85d 102
ericbisson 25:d0c348c3c85d 103 if (sync > 1 && (time*1.5 >= periode))
ericbisson 25:d0c348c3c85d 104 {
ericbisson 25:d0c348c3c85d 105 messageBuffer[messageLength] = (messageBuffer[messageLength] << 1) + bit;
ericbisson 25:d0c348c3c85d 106 }
ericbisson 25:d0c348c3c85d 107
ericbisson 25:d0c348c3c85d 108 shift++;
ericbisson 25:d0c348c3c85d 109 if (shift == 8)
ericbisson 25:d0c348c3c85d 110 {
ericbisson 25:d0c348c3c85d 111 shift = 0;
ericbisson 25:d0c348c3c85d 112
ericbisson 25:d0c348c3c85d 113 messageLength++;
ericbisson 25:d0c348c3c85d 114
ericbisson 25:d0c348c3c85d 115 // debug
ericbisson 25:d0c348c3c85d 116 ThreadLecture.signal_set(1);
ericbisson 25:d0c348c3c85d 117
ericbisson 25:d0c348c3c85d 118 // Validations de base
ericbisson 25:d0c348c3c85d 119 if (messageLength == 2 && messageBuffer[1] != START)
ericbisson 25:d0c348c3c85d 120 {
ericbisson 25:d0c348c3c85d 121 resetMessage();
ericbisson 25:d0c348c3c85d 122 }
ericbisson 25:d0c348c3c85d 123
ericbisson 25:d0c348c3c85d 124 if (messageLength == 4)
ericbisson 25:d0c348c3c85d 125 {
ericbisson 25:d0c348c3c85d 126 totalsize = 7 + messageBuffer[3];
ericbisson 25:d0c348c3c85d 127 }
ericbisson 25:d0c348c3c85d 128
ericbisson 25:d0c348c3c85d 129 // fin
ericbisson 25:d0c348c3c85d 130 if (totalsize == messageLength)
ericbisson 25:d0c348c3c85d 131 {
ericbisson 25:d0c348c3c85d 132 // Calcul du CRC
ericbisson 25:d0c348c3c85d 133 unsigned short currentCRC = messageBuffer[totalsize-2] + messageBuffer[totalsize-3]<<8;
ericbisson 25:d0c348c3c85d 134
ericbisson 25:d0c348c3c85d 135 if (currentCRC == myCRC.calculateCRC16(messageBuffer+4, totalsize-7) && messageBuffer[totalsize-1] == END)
ericbisson 25:d0c348c3c85d 136 {
ericbisson 25:d0c348c3c85d 137 // Affiche à l'écran le message valide
ericbisson 25:d0c348c3c85d 138 ThreadLecture.signal_set(1);
ericbisson 25:d0c348c3c85d 139 }
ericbisson 25:d0c348c3c85d 140 resetMessage();
ericbisson 25:d0c348c3c85d 141 }
ericbisson 25:d0c348c3c85d 142 }
ericbisson 25:d0c348c3c85d 143
ericbisson 25:d0c348c3c85d 144 // reset le timer pour mesurer la période
ericbisson 25:d0c348c3c85d 145 t.reset();
ericbisson 25:d0c348c3c85d 146 }
ericbisson 25:d0c348c3c85d 147
ericbisson 25:d0c348c3c85d 148 void rise()
ericbisson 25:d0c348c3c85d 149 {
ericbisson 25:d0c348c3c85d 150 read(0);
ericbisson 25:d0c348c3c85d 151 }
ericbisson 25:d0c348c3c85d 152
ericbisson 25:d0c348c3c85d 153 void fall()
ericbisson 25:d0c348c3c85d 154 {
ericbisson 25:d0c348c3c85d 155 read(1);
ericbisson 25:d0c348c3c85d 156 }
ericbisson 25:d0c348c3c85d 157
ericbisson 14:bd909277eb13 158 extern "C" void TIMER1_IRQHandler()
ericbisson 14:bd909277eb13 159 {
ericbisson 24:2034362db66f 160 LPC_TIM1->TC = 0;
ericbisson 24:2034362db66f 161
ericbisson 24:2034362db66f 162 myled1 = !myled1;
ericbisson 14:bd909277eb13 163 if ((LPC_TIM1->IR & 0x01) == 0x01)
ericbisson 14:bd909277eb13 164 {
JoeyDionne 22:7244c3391bac 165 send_data();
ericbisson 14:bd909277eb13 166 LPC_TIM1->IR |= 1 << 0; // clear
ericbisson 14:bd909277eb13 167 }
ericbisson 1:b3ae0d9f02ad 168 }
ericbisson 1:b3ae0d9f02ad 169
JoeyDionne 13:a436ba0b78e8 170 //création de la trame
ericbisson 20:ac8682cf923b 171 void create_trame(char message[],unsigned char taille)
JoeyDionne 12:a55f77a0e188 172 {
ericbisson 20:ac8682cf923b 173 unsigned short resultCRC = myCRC.calculateCRC16(message,taille);
JoeyDionne 12:a55f77a0e188 174
ericbisson 20:ac8682cf923b 175 trame_sent[0] = PREAMBULE; //Preambule
ericbisson 20:ac8682cf923b 176 trame_sent[1] = START; //Start
JoeyDionne 12:a55f77a0e188 177 trame_sent[2] = 0x00; //Type + Flag mis a 0x00
ericbisson 20:ac8682cf923b 178 trame_sent[3] = taille; //Longueur du message (Max 33 caractères)
JoeyDionne 12:a55f77a0e188 179
JoeyDionne 13:a436ba0b78e8 180 //message
ericbisson 26:6ffbf3161f6f 181 printf("\n\rMessage a envoyer :");
ericbisson 26:6ffbf3161f6f 182 printf(message);
ericbisson 26:6ffbf3161f6f 183 for (int i=0;i<taille;i++)
JoeyDionne 13:a436ba0b78e8 184 {
JoeyDionne 13:a436ba0b78e8 185 trame_sent[taille + 4] = message[i];
ericbisson 26:6ffbf3161f6f 186
JoeyDionne 13:a436ba0b78e8 187 }
ericbisson 26:6ffbf3161f6f 188 printf(" : Fin Message\n\r");
ericbisson 26:6ffbf3161f6f 189 printf("Result CRC : %x", resultCRC);
JoeyDionne 13:a436ba0b78e8 190
JoeyDionne 13:a436ba0b78e8 191 //CRC16
JoeyDionne 13:a436ba0b78e8 192 trame_sent[taille + 4] = (resultCRC >> 8) & 0xFF;
JoeyDionne 13:a436ba0b78e8 193 trame_sent[taille + 5] = resultCRC & 0xFF;
JoeyDionne 13:a436ba0b78e8 194
ericbisson 20:ac8682cf923b 195 trame_sent[taille + 6] = END; //End
JoeyDionne 13:a436ba0b78e8 196 trame_length = taille + 7; //Longueur de la trame
ericbisson 26:6ffbf3161f6f 197
JoeyDionne 13:a436ba0b78e8 198 }
JoeyDionne 13:a436ba0b78e8 199
JoeyDionne 13:a436ba0b78e8 200 //obtention du texte
JoeyDionne 13:a436ba0b78e8 201 void get_text()
JoeyDionne 13:a436ba0b78e8 202 {
ericbisson 24:2034362db66f 203 while (true)
ericbisson 24:2034362db66f 204 {
ericbisson 26:6ffbf3161f6f 205
ericbisson 26:6ffbf3161f6f 206 IsFinish = false;
ericbisson 26:6ffbf3161f6f 207 length = 0;
ericbisson 26:6ffbf3161f6f 208
ericbisson 24:2034362db66f 209 char text[4] = {'a','l','l','o'};
ericbisson 24:2034362db66f 210 unsigned char count = 4;
ericbisson 24:2034362db66f 211
ericbisson 24:2034362db66f 212 create_trame(text,count);
ericbisson 24:2034362db66f 213 byte_sent_pos = 0;
ericbisson 24:2034362db66f 214 bit_sent = 7;
ericbisson 24:2034362db66f 215 bTimer1 = false;
ericbisson 24:2034362db66f 216 LPC_TIM1->TCR = 1; // enable and reset Timer
ericbisson 24:2034362db66f 217
ericbisson 25:d0c348c3c85d 218 wait_ms(5000);
ericbisson 24:2034362db66f 219 }
ericbisson 24:2034362db66f 220 /*pc.printf("\n\rYour text : ");
ericbisson 20:ac8682cf923b 221 unsigned char count = 0;
JoeyDionne 12:a55f77a0e188 222 char c = 0x00;
JoeyDionne 12:a55f77a0e188 223 char text[73];
ericbisson 20:ac8682cf923b 224 while(c != '\r' && count < 73) {
JoeyDionne 12:a55f77a0e188 225 c = pc.getc();
JoeyDionne 12:a55f77a0e188 226 text[count] = c;
JoeyDionne 12:a55f77a0e188 227 pc.putc(c);
JoeyDionne 12:a55f77a0e188 228 count++;
JoeyDionne 12:a55f77a0e188 229 }
ericbisson 24:2034362db66f 230 pc.printf("get_text();\n");*/
JoeyDionne 12:a55f77a0e188 231 }
JoeyDionne 12:a55f77a0e188 232
ericbisson 24:2034362db66f 233 void initTimers()
ericbisson 24:2034362db66f 234 {
ericbisson 24:2034362db66f 235 //Timer 1 (match)
ericbisson 24:2034362db66f 236 LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1
ericbisson 24:2034362db66f 237 LPC_SC->PCONP |= (1 << 2); // timer1 power on
ericbisson 24:2034362db66f 238 LPC_TIM1->MCR = 3; // interrupt and reset control
ericbisson 24:2034362db66f 239 LPC_TIM1->TC = 0; // clear timer counter
ericbisson 24:2034362db66f 240 LPC_TIM1->PC = 0; // clear prescale counter
ericbisson 24:2034362db66f 241 LPC_TIM1->PR = 0; // clear prescale register
ericbisson 25:d0c348c3c85d 242 LPC_TIM1->MR0 = (960000); // 10 ms
ericbisson 24:2034362db66f 243 LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match
ericbisson 24:2034362db66f 244 NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt
ericbisson 24:2034362db66f 245 LPC_TIM1->TCR = 0; // disable Timer
ericbisson 24:2034362db66f 246 }
ericbisson 24:2034362db66f 247
ericbisson 2:c6465d4e82d2 248 int main() {
ericbisson 14:bd909277eb13 249 LPC_PINCON->PINSEL0 |= (3 << 8); // pin30
ericbisson 24:2034362db66f 250 LPC_PINCON->PINMODE1 |= 3;
ericbisson 14:bd909277eb13 251 initTimers();
ericbisson 25:d0c348c3c85d 252 ThreadLecture.start(show_message);
ericbisson 24:2034362db66f 253 ThreadWrite.start(get_text);
ericbisson 25:d0c348c3c85d 254 interrupt_p30.fall(fall);
ericbisson 25:d0c348c3c85d 255 interrupt_p30.rise(rise);
ericbisson 25:d0c348c3c85d 256 t.start();
ericbisson 0:c637467eeb8f 257
JoeyDionne 22:7244c3391bac 258 while(true)
JoeyDionne 22:7244c3391bac 259 {
ericbisson 26:6ffbf3161f6f 260 if (IsFinish == true)
ericbisson 26:6ffbf3161f6f 261 {
ericbisson 26:6ffbf3161f6f 262 pc.printf("\n\rBits Envoyes : ");
ericbisson 26:6ffbf3161f6f 263 for (int i=0;i<length;i++)
ericbisson 26:6ffbf3161f6f 264 {
ericbisson 26:6ffbf3161f6f 265 pc.printf("%d:",bitSent[i]);
ericbisson 26:6ffbf3161f6f 266 if(i%16 == 15)
ericbisson 26:6ffbf3161f6f 267 {
ericbisson 26:6ffbf3161f6f 268 pc.printf("\n\r");
ericbisson 26:6ffbf3161f6f 269 }
ericbisson 26:6ffbf3161f6f 270 }
ericbisson 26:6ffbf3161f6f 271 pc.printf("Fin Bits Envoyes\n\r");
ericbisson 26:6ffbf3161f6f 272 IsFinish = false;
ericbisson 26:6ffbf3161f6f 273 }
ericbisson 0:c637467eeb8f 274 }
ericbisson 7:5501dbea5650 275 };