![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
main.cpp@25:d0c348c3c85d, 2017-03-07 (annotated)
- Committer:
- ericbisson
- Date:
- Tue Mar 07 17:25:16 2017 +0000
- Revision:
- 25:d0c348c3c85d
- Parent:
- 24:2034362db66f
- Child:
- 26:6ffbf3161f6f
pour remise
Who changed what in which revision?
User | Revision | Line number | New 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 | |
JoeyDionne | 23:9c4e4898b741 | 28 | //encodage manchester |
JoeyDionne | 23:9c4e4898b741 | 29 | bool codeManchester(bool bit, bool clock) |
JoeyDionne | 23:9c4e4898b741 | 30 | { |
JoeyDionne | 23:9c4e4898b741 | 31 | return (bit == clock); |
JoeyDionne | 23:9c4e4898b741 | 32 | } |
JoeyDionne | 23:9c4e4898b741 | 33 | |
JoeyDionne | 23:9c4e4898b741 | 34 | //Envoie de données bit par bit |
JoeyDionne | 23:9c4e4898b741 | 35 | void send_data() |
ericbisson | 25:d0c348c3c85d | 36 | { |
JoeyDionne | 23:9c4e4898b741 | 37 | out = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), bTimer1); |
JoeyDionne | 23:9c4e4898b741 | 38 | if(bTimer1) |
JoeyDionne | 23:9c4e4898b741 | 39 | { |
JoeyDionne | 23:9c4e4898b741 | 40 | bit_sent--; |
JoeyDionne | 23:9c4e4898b741 | 41 | } |
JoeyDionne | 23:9c4e4898b741 | 42 | bTimer1 = !bTimer1; //varier entre la 1ere et 2eme partie de demi-periode |
JoeyDionne | 23:9c4e4898b741 | 43 | if (bit_sent < 0) //Si l'octet a ete envoye |
JoeyDionne | 23:9c4e4898b741 | 44 | { |
JoeyDionne | 23:9c4e4898b741 | 45 | bit_sent = 7; //remettre la position initiale pour le prochain octet |
JoeyDionne | 23:9c4e4898b741 | 46 | byte_sent_pos++; //incrementer l'octet |
JoeyDionne | 23:9c4e4898b741 | 47 | if (byte_sent_pos >= trame_length) //Si la trame a ete envoyee |
JoeyDionne | 23:9c4e4898b741 | 48 | { |
JoeyDionne | 23:9c4e4898b741 | 49 | byte_sent_pos = 0; |
JoeyDionne | 23:9c4e4898b741 | 50 | bTimer1 = false; |
JoeyDionne | 23:9c4e4898b741 | 51 | LPC_TIM1->TCR = 0; // disable Timer |
JoeyDionne | 23:9c4e4898b741 | 52 | } |
JoeyDionne | 23:9c4e4898b741 | 53 | } |
JoeyDionne | 23:9c4e4898b741 | 54 | } |
JoeyDionne | 23:9c4e4898b741 | 55 | |
ericbisson | 25:d0c348c3c85d | 56 | char messageBuffer[80]; |
ericbisson | 25:d0c348c3c85d | 57 | char messageLength = 0; |
ericbisson | 25:d0c348c3c85d | 58 | char lastpos = 0; |
ericbisson | 25:d0c348c3c85d | 59 | void show_message() |
ericbisson | 25:d0c348c3c85d | 60 | { |
ericbisson | 25:d0c348c3c85d | 61 | while (true) |
ericbisson | 25:d0c348c3c85d | 62 | { |
ericbisson | 25:d0c348c3c85d | 63 | ThreadLecture.signal_wait(1); |
ericbisson | 25:d0c348c3c85d | 64 | for (int i = lastpos; i < messageLength; i++) |
ericbisson | 25:d0c348c3c85d | 65 | { |
ericbisson | 25:d0c348c3c85d | 66 | pc.printf("%x",messageBuffer[i]); |
ericbisson | 25:d0c348c3c85d | 67 | |
ericbisson | 25:d0c348c3c85d | 68 | if ((i % 11) == 0 && i > 0) |
ericbisson | 25:d0c348c3c85d | 69 | { |
ericbisson | 25:d0c348c3c85d | 70 | pc.printf("\n"); |
ericbisson | 25:d0c348c3c85d | 71 | } |
ericbisson | 25:d0c348c3c85d | 72 | } |
ericbisson | 25:d0c348c3c85d | 73 | lastpos = messageLength; |
ericbisson | 25:d0c348c3c85d | 74 | } |
ericbisson | 25:d0c348c3c85d | 75 | } |
ericbisson | 25:d0c348c3c85d | 76 | |
ericbisson | 25:d0c348c3c85d | 77 | char byte = 0; |
ericbisson | 25:d0c348c3c85d | 78 | char shift = 0; |
ericbisson | 25:d0c348c3c85d | 79 | char totalsize = 7; |
ericbisson | 25:d0c348c3c85d | 80 | void resetMessage() |
ericbisson | 25:d0c348c3c85d | 81 | { |
ericbisson | 25:d0c348c3c85d | 82 | byte = 0; |
ericbisson | 25:d0c348c3c85d | 83 | shift = 0; |
ericbisson | 25:d0c348c3c85d | 84 | totalsize = 7; |
ericbisson | 25:d0c348c3c85d | 85 | messageLength = 0; |
ericbisson | 25:d0c348c3c85d | 86 | lastpos = 0; |
ericbisson | 25:d0c348c3c85d | 87 | } |
ericbisson | 25:d0c348c3c85d | 88 | |
ericbisson | 25:d0c348c3c85d | 89 | void read(bool bit) |
ericbisson | 25:d0c348c3c85d | 90 | { |
ericbisson | 25:d0c348c3c85d | 91 | int time = t.read_us(); |
ericbisson | 25:d0c348c3c85d | 92 | if (sync == 1) |
ericbisson | 25:d0c348c3c85d | 93 | { |
ericbisson | 25:d0c348c3c85d | 94 | periode = time; |
ericbisson | 25:d0c348c3c85d | 95 | } |
ericbisson | 25:d0c348c3c85d | 96 | sync++; |
ericbisson | 25:d0c348c3c85d | 97 | |
ericbisson | 25:d0c348c3c85d | 98 | if (sync > 1 && (time*1.5 >= periode)) |
ericbisson | 25:d0c348c3c85d | 99 | { |
ericbisson | 25:d0c348c3c85d | 100 | messageBuffer[messageLength] = (messageBuffer[messageLength] << 1) + bit; |
ericbisson | 25:d0c348c3c85d | 101 | } |
ericbisson | 25:d0c348c3c85d | 102 | |
ericbisson | 25:d0c348c3c85d | 103 | shift++; |
ericbisson | 25:d0c348c3c85d | 104 | if (shift == 8) |
ericbisson | 25:d0c348c3c85d | 105 | { |
ericbisson | 25:d0c348c3c85d | 106 | shift = 0; |
ericbisson | 25:d0c348c3c85d | 107 | |
ericbisson | 25:d0c348c3c85d | 108 | messageLength++; |
ericbisson | 25:d0c348c3c85d | 109 | |
ericbisson | 25:d0c348c3c85d | 110 | // debug |
ericbisson | 25:d0c348c3c85d | 111 | ThreadLecture.signal_set(1); |
ericbisson | 25:d0c348c3c85d | 112 | |
ericbisson | 25:d0c348c3c85d | 113 | // Validations de base |
ericbisson | 25:d0c348c3c85d | 114 | if (messageLength == 2 && messageBuffer[1] != START) |
ericbisson | 25:d0c348c3c85d | 115 | { |
ericbisson | 25:d0c348c3c85d | 116 | resetMessage(); |
ericbisson | 25:d0c348c3c85d | 117 | } |
ericbisson | 25:d0c348c3c85d | 118 | |
ericbisson | 25:d0c348c3c85d | 119 | if (messageLength == 4) |
ericbisson | 25:d0c348c3c85d | 120 | { |
ericbisson | 25:d0c348c3c85d | 121 | totalsize = 7 + messageBuffer[3]; |
ericbisson | 25:d0c348c3c85d | 122 | } |
ericbisson | 25:d0c348c3c85d | 123 | |
ericbisson | 25:d0c348c3c85d | 124 | // fin |
ericbisson | 25:d0c348c3c85d | 125 | if (totalsize == messageLength) |
ericbisson | 25:d0c348c3c85d | 126 | { |
ericbisson | 25:d0c348c3c85d | 127 | // Calcul du CRC |
ericbisson | 25:d0c348c3c85d | 128 | unsigned short currentCRC = messageBuffer[totalsize-2] + messageBuffer[totalsize-3]<<8; |
ericbisson | 25:d0c348c3c85d | 129 | |
ericbisson | 25:d0c348c3c85d | 130 | if (currentCRC == myCRC.calculateCRC16(messageBuffer+4, totalsize-7) && messageBuffer[totalsize-1] == END) |
ericbisson | 25:d0c348c3c85d | 131 | { |
ericbisson | 25:d0c348c3c85d | 132 | // Affiche à l'écran le message valide |
ericbisson | 25:d0c348c3c85d | 133 | ThreadLecture.signal_set(1); |
ericbisson | 25:d0c348c3c85d | 134 | } |
ericbisson | 25:d0c348c3c85d | 135 | resetMessage(); |
ericbisson | 25:d0c348c3c85d | 136 | } |
ericbisson | 25:d0c348c3c85d | 137 | } |
ericbisson | 25:d0c348c3c85d | 138 | |
ericbisson | 25:d0c348c3c85d | 139 | // reset le timer pour mesurer la période |
ericbisson | 25:d0c348c3c85d | 140 | t.reset(); |
ericbisson | 25:d0c348c3c85d | 141 | } |
ericbisson | 25:d0c348c3c85d | 142 | |
ericbisson | 25:d0c348c3c85d | 143 | void rise() |
ericbisson | 25:d0c348c3c85d | 144 | { |
ericbisson | 25:d0c348c3c85d | 145 | read(0); |
ericbisson | 25:d0c348c3c85d | 146 | } |
ericbisson | 25:d0c348c3c85d | 147 | |
ericbisson | 25:d0c348c3c85d | 148 | void fall() |
ericbisson | 25:d0c348c3c85d | 149 | { |
ericbisson | 25:d0c348c3c85d | 150 | read(1); |
ericbisson | 25:d0c348c3c85d | 151 | } |
ericbisson | 25:d0c348c3c85d | 152 | |
ericbisson | 14:bd909277eb13 | 153 | extern "C" void TIMER1_IRQHandler() |
ericbisson | 14:bd909277eb13 | 154 | { |
ericbisson | 24:2034362db66f | 155 | LPC_TIM1->TC = 0; |
ericbisson | 24:2034362db66f | 156 | |
ericbisson | 24:2034362db66f | 157 | myled1 = !myled1; |
ericbisson | 14:bd909277eb13 | 158 | if ((LPC_TIM1->IR & 0x01) == 0x01) |
ericbisson | 14:bd909277eb13 | 159 | { |
JoeyDionne | 22:7244c3391bac | 160 | send_data(); |
ericbisson | 14:bd909277eb13 | 161 | LPC_TIM1->IR |= 1 << 0; // clear |
ericbisson | 14:bd909277eb13 | 162 | } |
ericbisson | 1:b3ae0d9f02ad | 163 | } |
ericbisson | 1:b3ae0d9f02ad | 164 | |
JoeyDionne | 13:a436ba0b78e8 | 165 | //création de la trame |
ericbisson | 20:ac8682cf923b | 166 | void create_trame(char message[],unsigned char taille) |
JoeyDionne | 12:a55f77a0e188 | 167 | { |
ericbisson | 20:ac8682cf923b | 168 | unsigned short resultCRC = myCRC.calculateCRC16(message,taille); |
JoeyDionne | 12:a55f77a0e188 | 169 | |
ericbisson | 20:ac8682cf923b | 170 | trame_sent[0] = PREAMBULE; //Preambule |
ericbisson | 20:ac8682cf923b | 171 | trame_sent[1] = START; //Start |
JoeyDionne | 12:a55f77a0e188 | 172 | trame_sent[2] = 0x00; //Type + Flag mis a 0x00 |
ericbisson | 20:ac8682cf923b | 173 | trame_sent[3] = taille; //Longueur du message (Max 33 caractères) |
JoeyDionne | 12:a55f77a0e188 | 174 | |
JoeyDionne | 13:a436ba0b78e8 | 175 | //message |
ericbisson | 20:ac8682cf923b | 176 | for (char i=0;i<taille;i++) |
JoeyDionne | 13:a436ba0b78e8 | 177 | { |
JoeyDionne | 13:a436ba0b78e8 | 178 | trame_sent[taille + 4] = message[i]; |
JoeyDionne | 13:a436ba0b78e8 | 179 | } |
JoeyDionne | 13:a436ba0b78e8 | 180 | |
JoeyDionne | 13:a436ba0b78e8 | 181 | //CRC16 |
JoeyDionne | 13:a436ba0b78e8 | 182 | trame_sent[taille + 4] = (resultCRC >> 8) & 0xFF; |
JoeyDionne | 13:a436ba0b78e8 | 183 | trame_sent[taille + 5] = resultCRC & 0xFF; |
JoeyDionne | 13:a436ba0b78e8 | 184 | |
ericbisson | 20:ac8682cf923b | 185 | trame_sent[taille + 6] = END; //End |
JoeyDionne | 13:a436ba0b78e8 | 186 | trame_length = taille + 7; //Longueur de la trame |
JoeyDionne | 13:a436ba0b78e8 | 187 | } |
JoeyDionne | 13:a436ba0b78e8 | 188 | |
JoeyDionne | 13:a436ba0b78e8 | 189 | //obtention du texte |
JoeyDionne | 13:a436ba0b78e8 | 190 | void get_text() |
JoeyDionne | 13:a436ba0b78e8 | 191 | { |
ericbisson | 24:2034362db66f | 192 | while (true) |
ericbisson | 24:2034362db66f | 193 | { |
ericbisson | 24:2034362db66f | 194 | char text[4] = {'a','l','l','o'}; |
ericbisson | 24:2034362db66f | 195 | unsigned char count = 4; |
ericbisson | 24:2034362db66f | 196 | |
ericbisson | 24:2034362db66f | 197 | create_trame(text,count); |
ericbisson | 24:2034362db66f | 198 | byte_sent_pos = 0; |
ericbisson | 24:2034362db66f | 199 | bit_sent = 7; |
ericbisson | 24:2034362db66f | 200 | bTimer1 = false; |
ericbisson | 24:2034362db66f | 201 | LPC_TIM1->TCR = 1; // enable and reset Timer |
ericbisson | 24:2034362db66f | 202 | |
ericbisson | 25:d0c348c3c85d | 203 | wait_ms(5000); |
ericbisson | 24:2034362db66f | 204 | } |
ericbisson | 24:2034362db66f | 205 | /*pc.printf("\n\rYour text : "); |
ericbisson | 20:ac8682cf923b | 206 | unsigned char count = 0; |
JoeyDionne | 12:a55f77a0e188 | 207 | char c = 0x00; |
JoeyDionne | 12:a55f77a0e188 | 208 | char text[73]; |
ericbisson | 20:ac8682cf923b | 209 | while(c != '\r' && count < 73) { |
JoeyDionne | 12:a55f77a0e188 | 210 | c = pc.getc(); |
JoeyDionne | 12:a55f77a0e188 | 211 | text[count] = c; |
JoeyDionne | 12:a55f77a0e188 | 212 | pc.putc(c); |
JoeyDionne | 12:a55f77a0e188 | 213 | count++; |
JoeyDionne | 12:a55f77a0e188 | 214 | } |
ericbisson | 24:2034362db66f | 215 | pc.printf("get_text();\n");*/ |
JoeyDionne | 12:a55f77a0e188 | 216 | } |
JoeyDionne | 12:a55f77a0e188 | 217 | |
ericbisson | 24:2034362db66f | 218 | void initTimers() |
ericbisson | 24:2034362db66f | 219 | { |
ericbisson | 24:2034362db66f | 220 | //Timer 1 (match) |
ericbisson | 24:2034362db66f | 221 | LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 |
ericbisson | 24:2034362db66f | 222 | LPC_SC->PCONP |= (1 << 2); // timer1 power on |
ericbisson | 24:2034362db66f | 223 | LPC_TIM1->MCR = 3; // interrupt and reset control |
ericbisson | 24:2034362db66f | 224 | LPC_TIM1->TC = 0; // clear timer counter |
ericbisson | 24:2034362db66f | 225 | LPC_TIM1->PC = 0; // clear prescale counter |
ericbisson | 24:2034362db66f | 226 | LPC_TIM1->PR = 0; // clear prescale register |
ericbisson | 25:d0c348c3c85d | 227 | LPC_TIM1->MR0 = (960000); // 10 ms |
ericbisson | 24:2034362db66f | 228 | LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match |
ericbisson | 24:2034362db66f | 229 | NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt |
ericbisson | 24:2034362db66f | 230 | LPC_TIM1->TCR = 0; // disable Timer |
ericbisson | 24:2034362db66f | 231 | } |
ericbisson | 24:2034362db66f | 232 | |
ericbisson | 2:c6465d4e82d2 | 233 | int main() { |
ericbisson | 14:bd909277eb13 | 234 | LPC_PINCON->PINSEL0 |= (3 << 8); // pin30 |
ericbisson | 24:2034362db66f | 235 | LPC_PINCON->PINMODE1 |= 3; |
ericbisson | 14:bd909277eb13 | 236 | initTimers(); |
ericbisson | 25:d0c348c3c85d | 237 | ThreadLecture.start(show_message); |
ericbisson | 24:2034362db66f | 238 | ThreadWrite.start(get_text); |
ericbisson | 25:d0c348c3c85d | 239 | interrupt_p30.fall(fall); |
ericbisson | 25:d0c348c3c85d | 240 | interrupt_p30.rise(rise); |
ericbisson | 25:d0c348c3c85d | 241 | t.start(); |
ericbisson | 0:c637467eeb8f | 242 | |
JoeyDionne | 22:7244c3391bac | 243 | while(true) |
JoeyDionne | 22:7244c3391bac | 244 | { |
ericbisson | 0:c637467eeb8f | 245 | } |
ericbisson | 7:5501dbea5650 | 246 | }; |