app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Committer:
JoeyDionne
Date:
Tue Mar 07 05:45:26 2017 +0000
Revision:
22:7244c3391bac
Parent:
20:ac8682cf923b
Child:
23:9c4e4898b741
modifications envoi message

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 15:7c2e70c36b98 4 #include <vector>
ericbisson 15:7c2e70c36b98 5 using std::vector;
ericbisson 15:7c2e70c36b98 6 DigitalIn in(p30);
ericbisson 8:5b87b1f9d91f 7 Thread ThreadLecture;
ericbisson 15:7c2e70c36b98 8 const int PREAMBULE = 0b01010101;
ericbisson 15:7c2e70c36b98 9 const int START = 0b01111110;
ericbisson 15:7c2e70c36b98 10 const int END = 0b01111110;
ericbisson 14:bd909277eb13 11 Serial pc(USBTX, USBRX);
ericbisson 14:bd909277eb13 12 DigitalOut out(p8);
ericbisson 2:c6465d4e82d2 13
JoeyDionne 12:a55f77a0e188 14 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 15 char trame_sent[80]; //tableau de la trame à envoyer
JoeyDionne 12:a55f77a0e188 16 uint8_t byte_sent_pos, //position de l'octet dans le tableau d'envoi
JoeyDionne 12:a55f77a0e188 17 trame_length; //longueur de la trame complete
ericbisson 20:ac8682cf923b 18
ericbisson 20:ac8682cf923b 19 signed char bit_sent; //position du bit de l'octet en cours d'envoi (du MSB au LSB) 7..0
JoeyDionne 11:b27d1a83f688 20
ericbisson 14:bd909277eb13 21 void initTimers()
ericbisson 14:bd909277eb13 22 {
ericbisson 14:bd909277eb13 23 //Timer 1 (match)
ericbisson 14:bd909277eb13 24 LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1
ericbisson 14:bd909277eb13 25 LPC_SC->PCONP |= (1 << 2); // timer1 power on
ericbisson 18:73ae643a6803 26 LPC_TIM1->MR0 = 960000; // 10 ms
ericbisson 14:bd909277eb13 27 LPC_TIM1->MCR = 3; // interrupt and reset control
ericbisson 14:bd909277eb13 28 LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match
ericbisson 14:bd909277eb13 29 NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt
ericbisson 14:bd909277eb13 30 LPC_TIM1->TCR = 1; // enable Timer
ericbisson 14:bd909277eb13 31
ericbisson 14:bd909277eb13 32 //Timer 2 (cap)
ericbisson 14:bd909277eb13 33 LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2
ericbisson 14:bd909277eb13 34 LPC_SC->PCONP |= (1 << 22); // timer2 power on
ericbisson 14:bd909277eb13 35 LPC_TIM2->TC = 0; // clear timer counter
ericbisson 14:bd909277eb13 36 LPC_TIM2->PC = 0; // clear prescale counter
ericbisson 14:bd909277eb13 37 LPC_TIM2->PR = 0; // clear prescale register
ericbisson 14:bd909277eb13 38 LPC_TIM2->TCR |= (1 << 1); // reset timer
ericbisson 14:bd909277eb13 39 LPC_TIM2->TCR &= ~(1 << 1); // release reset
ericbisson 14:bd909277eb13 40 LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register
ericbisson 14:bd909277eb13 41 LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture
ericbisson 14:bd909277eb13 42 NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt
ericbisson 14:bd909277eb13 43 LPC_TIM2->TCR = 1; // start Timer
ericbisson 14:bd909277eb13 44 }
JoeyDionne 11:b27d1a83f688 45
ericbisson 14:bd909277eb13 46 bool bTimer1 = false;
ericbisson 14:bd909277eb13 47 extern "C" void TIMER1_IRQHandler()
ericbisson 14:bd909277eb13 48 {
ericbisson 14:bd909277eb13 49 if ((LPC_TIM1->IR & 0x01) == 0x01)
ericbisson 14:bd909277eb13 50 {
JoeyDionne 22:7244c3391bac 51 send_data();
ericbisson 14:bd909277eb13 52
ericbisson 14:bd909277eb13 53 LPC_TIM1->IR |= 1 << 0; // clear
ericbisson 14:bd909277eb13 54 }
ericbisson 1:b3ae0d9f02ad 55 }
ericbisson 1:b3ae0d9f02ad 56
ericbisson 14:bd909277eb13 57 int sumClocks = 0;
ericbisson 14:bd909277eb13 58 int PeriodLength = 0;
ericbisson 14:bd909277eb13 59 bool bReceivedFirstBit = false;
ericbisson 14:bd909277eb13 60 extern "C" void TIMER2_IRQHandler()
ericbisson 7:5501dbea5650 61 {
ericbisson 14:bd909277eb13 62 if (PeriodLength > 0)
ericbisson 7:5501dbea5650 63 {
ericbisson 15:7c2e70c36b98 64 if (LPC_TIM2->CR0 >= PeriodLength*1.5 || (sumClocks + LPC_TIM2->CR0) >= PeriodLength*1.5)
ericbisson 8:5b87b1f9d91f 65 {
ericbisson 14:bd909277eb13 66 sumClocks = 0;
ericbisson 14:bd909277eb13 67
ericbisson 14:bd909277eb13 68 ThreadLecture.signal_set(1);
ericbisson 14:bd909277eb13 69 }
ericbisson 14:bd909277eb13 70 else
ericbisson 14:bd909277eb13 71 {
ericbisson 14:bd909277eb13 72 sumClocks += LPC_TIM2->CR0;
ericbisson 8:5b87b1f9d91f 73 }
ericbisson 1:b3ae0d9f02ad 74 }
ericbisson 14:bd909277eb13 75 else if (bReceivedFirstBit)
ericbisson 14:bd909277eb13 76 {
ericbisson 14:bd909277eb13 77 PeriodLength = LPC_TIM2->CR0 / 2;
ericbisson 14:bd909277eb13 78 ThreadLecture.signal_set(1);
ericbisson 14:bd909277eb13 79 }
ericbisson 14:bd909277eb13 80 else
ericbisson 14:bd909277eb13 81 {
ericbisson 14:bd909277eb13 82 bReceivedFirstBit = true;
ericbisson 14:bd909277eb13 83 ThreadLecture.signal_set(1);
ericbisson 14:bd909277eb13 84 }
ericbisson 15:7c2e70c36b98 85
ericbisson 14:bd909277eb13 86 LPC_TIM2->TC = 0;
ericbisson 14:bd909277eb13 87 LPC_TIM2->IR |= 0xFFFFFFFF; // clear
ericbisson 14:bd909277eb13 88 }
ericbisson 4:87e9b434bb4d 89
JoeyDionne 13:a436ba0b78e8 90 bool codeManchester(bool bit, bool clock)
JoeyDionne 13:a436ba0b78e8 91 {
JoeyDionne 13:a436ba0b78e8 92 return (bit == clock);
JoeyDionne 13:a436ba0b78e8 93 }
JoeyDionne 13:a436ba0b78e8 94
JoeyDionne 12:a55f77a0e188 95 void send_data()
JoeyDionne 12:a55f77a0e188 96 {
ericbisson 15:7c2e70c36b98 97 out = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), bTimer1); // Encodage Manchester
ericbisson 15:7c2e70c36b98 98 if(bTimer1)
JoeyDionne 13:a436ba0b78e8 99 {
JoeyDionne 12:a55f77a0e188 100 bit_sent--;
JoeyDionne 12:a55f77a0e188 101 }
ericbisson 15:7c2e70c36b98 102 bTimer1 = !bTimer1; //varier entre la 1ere et 2eme partie de demi-periode
JoeyDionne 12:a55f77a0e188 103 if (bit_sent < 0) { //Si l'octet a ete envoye
JoeyDionne 12:a55f77a0e188 104 bit_sent = 7; //remettre la position initiale pour le prochain octet
JoeyDionne 12:a55f77a0e188 105 byte_sent_pos++; //incrementer l'octet
JoeyDionne 13:a436ba0b78e8 106 if (byte_sent_pos >= trame_length) { //Si la trame a ete envoyee
JoeyDionne 12:a55f77a0e188 107 byte_sent_pos = 0;
ericbisson 15:7c2e70c36b98 108 bTimer1 = false;
JoeyDionne 12:a55f77a0e188 109 }
JoeyDionne 12:a55f77a0e188 110 }
JoeyDionne 12:a55f77a0e188 111 }
JoeyDionne 12:a55f77a0e188 112
JoeyDionne 13:a436ba0b78e8 113 //création de la trame
ericbisson 20:ac8682cf923b 114 void create_trame(char message[],unsigned char taille)
JoeyDionne 12:a55f77a0e188 115 {
ericbisson 20:ac8682cf923b 116 CRC16 myCRC;
ericbisson 20:ac8682cf923b 117 unsigned short resultCRC = myCRC.calculateCRC16(message,taille);
JoeyDionne 12:a55f77a0e188 118
ericbisson 20:ac8682cf923b 119 trame_sent[0] = PREAMBULE; //Preambule
ericbisson 20:ac8682cf923b 120 trame_sent[1] = START; //Start
JoeyDionne 12:a55f77a0e188 121 trame_sent[2] = 0x00; //Type + Flag mis a 0x00
ericbisson 20:ac8682cf923b 122 trame_sent[3] = taille; //Longueur du message (Max 33 caractères)
JoeyDionne 12:a55f77a0e188 123
JoeyDionne 13:a436ba0b78e8 124 //message
ericbisson 20:ac8682cf923b 125 for (char i=0;i<taille;i++)
JoeyDionne 13:a436ba0b78e8 126 {
JoeyDionne 13:a436ba0b78e8 127 trame_sent[taille + 4] = message[i];
JoeyDionne 13:a436ba0b78e8 128 }
JoeyDionne 13:a436ba0b78e8 129
JoeyDionne 13:a436ba0b78e8 130 //CRC16
JoeyDionne 13:a436ba0b78e8 131 trame_sent[taille + 4] = (resultCRC >> 8) & 0xFF;
JoeyDionne 13:a436ba0b78e8 132 trame_sent[taille + 5] = resultCRC & 0xFF;
JoeyDionne 13:a436ba0b78e8 133
ericbisson 20:ac8682cf923b 134 trame_sent[taille + 6] = END; //End
JoeyDionne 13:a436ba0b78e8 135 trame_length = taille + 7; //Longueur de la trame
JoeyDionne 13:a436ba0b78e8 136 }
JoeyDionne 13:a436ba0b78e8 137
JoeyDionne 22:7244c3391bac 138 void write()
JoeyDionne 22:7244c3391bac 139 {
JoeyDionne 22:7244c3391bac 140 byte_sent_pos = 0;
JoeyDionne 22:7244c3391bac 141 bit_sent = 7;
JoeyDionne 22:7244c3391bac 142 bTimer1 = false;
JoeyDionne 22:7244c3391bac 143 //TODO start interrupt timer 1
JoeyDionne 22:7244c3391bac 144 //LPC_RIT->MCR = 1; //Permettre les interruption du MAT
JoeyDionne 22:7244c3391bac 145 //LPC_RIT->MR0 = LPC_RIT->TC + HALF_PERIOD; //Faire une interruption a la prochaine demi-periode
JoeyDionne 22:7244c3391bac 146 }
JoeyDionne 22:7244c3391bac 147
JoeyDionne 13:a436ba0b78e8 148 //obtention du texte
JoeyDionne 13:a436ba0b78e8 149 void get_text()
JoeyDionne 13:a436ba0b78e8 150 {
JoeyDionne 12:a55f77a0e188 151 pc.printf("\n\rYour text : ");
ericbisson 20:ac8682cf923b 152 unsigned char count = 0;
JoeyDionne 12:a55f77a0e188 153 char c = 0x00;
JoeyDionne 12:a55f77a0e188 154 char text[73];
ericbisson 20:ac8682cf923b 155 while(c != '\r' && count < 73) {
JoeyDionne 12:a55f77a0e188 156 c = pc.getc();
JoeyDionne 12:a55f77a0e188 157 text[count] = c;
JoeyDionne 12:a55f77a0e188 158 pc.putc(c);
JoeyDionne 12:a55f77a0e188 159 count++;
JoeyDionne 12:a55f77a0e188 160 }
JoeyDionne 12:a55f77a0e188 161
JoeyDionne 13:a436ba0b78e8 162 create_trame(text,count);
JoeyDionne 22:7244c3391bac 163 write();
JoeyDionne 12:a55f77a0e188 164 }
JoeyDionne 12:a55f77a0e188 165
ericbisson 15:7c2e70c36b98 166 void read()
ericbisson 15:7c2e70c36b98 167 {
ericbisson 16:f2661759714c 168 char byte = 0;
ericbisson 15:7c2e70c36b98 169 vector<char> bytes;
ericbisson 15:7c2e70c36b98 170 char shift = 0;
ericbisson 16:f2661759714c 171 char totalsize = 7;
ericbisson 17:2b4b3f3a0489 172 CRC16 mycrc16;
ericbisson 15:7c2e70c36b98 173 while (true)
ericbisson 15:7c2e70c36b98 174 {
ericbisson 15:7c2e70c36b98 175 ThreadLecture.signal_wait(1);
ericbisson 15:7c2e70c36b98 176
ericbisson 16:f2661759714c 177 byte = (byte << 1) + !in; // inversion car 2e période
ericbisson 15:7c2e70c36b98 178
ericbisson 15:7c2e70c36b98 179 shift++;
ericbisson 15:7c2e70c36b98 180 if (shift == 8)
ericbisson 15:7c2e70c36b98 181 {
ericbisson 16:f2661759714c 182 shift = 0;
ericbisson 16:f2661759714c 183
ericbisson 16:f2661759714c 184 // à partir d'ici, je travaille en byte et non bit
ericbisson 15:7c2e70c36b98 185 bytes.push_back(byte);
ericbisson 16:f2661759714c 186
ericbisson 16:f2661759714c 187 // Validations de base
ericbisson 16:f2661759714c 188 if ((bytes.size() == 1 && bytes[0] != PREAMBULE) ||
ericbisson 16:f2661759714c 189 (bytes.size() == 2 && bytes[1] != START))
ericbisson 16:f2661759714c 190 {
ericbisson 16:f2661759714c 191 bytes.clear();
ericbisson 20:ac8682cf923b 192
ericbisson 20:ac8682cf923b 193 if (bytes.size() == 1)
ericbisson 20:ac8682cf923b 194 {
ericbisson 20:ac8682cf923b 195 pc.printf("[DEBUG] - Invalid PREAMBULE : %x\n", bytes[0]);
ericbisson 20:ac8682cf923b 196 }
ericbisson 20:ac8682cf923b 197 else
ericbisson 20:ac8682cf923b 198 {
ericbisson 20:ac8682cf923b 199 pc.printf("[DEBUG] - Invalid START : %x\n", bytes[1]);
ericbisson 20:ac8682cf923b 200 }
ericbisson 16:f2661759714c 201 }
ericbisson 19:e13ac9d940ab 202 if (bytes.size() == 4)
ericbisson 16:f2661759714c 203 {
ericbisson 19:e13ac9d940ab 204 totalsize = 7 + bytes[3];
ericbisson 16:f2661759714c 205 }
ericbisson 16:f2661759714c 206
ericbisson 16:f2661759714c 207 // fin
ericbisson 16:f2661759714c 208 if (totalsize == bytes.size())
ericbisson 16:f2661759714c 209 {
ericbisson 16:f2661759714c 210 // Calcul du CRC
ericbisson 16:f2661759714c 211 unsigned short currentCRC = bytes[bytes.size()-2] + bytes[bytes.size()-3]<<8;
ericbisson 16:f2661759714c 212
ericbisson 16:f2661759714c 213 vector<char> charge_utile(&bytes[4], &bytes[bytes.size()-4]);
ericbisson 16:f2661759714c 214
ericbisson 16:f2661759714c 215 if (currentCRC == mycrc16.calculateCRC16(&charge_utile[0], charge_utile.size()) && bytes.back() == END)
ericbisson 16:f2661759714c 216 {
ericbisson 16:f2661759714c 217 // Affiche à l'écran le message valide
ericbisson 16:f2661759714c 218 pc.printf(&charge_utile[0], charge_utile.size());
ericbisson 16:f2661759714c 219 }
ericbisson 20:ac8682cf923b 220 else
ericbisson 20:ac8682cf923b 221 {
ericbisson 20:ac8682cf923b 222 pc.printf("[DEBUG] - Invalid CRC, dumping message\n");
ericbisson 20:ac8682cf923b 223 }
ericbisson 16:f2661759714c 224 bytes.clear();
ericbisson 16:f2661759714c 225 }
ericbisson 15:7c2e70c36b98 226 }
ericbisson 15:7c2e70c36b98 227 }
ericbisson 15:7c2e70c36b98 228 }
ericbisson 15:7c2e70c36b98 229
ericbisson 2:c6465d4e82d2 230 int main() {
ericbisson 14:bd909277eb13 231 LPC_PINCON->PINSEL0 |= (3 << 8); // pin30
ericbisson 14:bd909277eb13 232 initTimers();
ericbisson 8:5b87b1f9d91f 233 ThreadLecture.start(read);
ericbisson 0:c637467eeb8f 234
JoeyDionne 22:7244c3391bac 235 while(true)
JoeyDionne 22:7244c3391bac 236 {
JoeyDionne 22:7244c3391bac 237 get_text();
ericbisson 0:c637467eeb8f 238 }
ericbisson 7:5501dbea5650 239 };