![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
main.cpp@15:7c2e70c36b98, 2017-03-07 (annotated)
- Committer:
- ericbisson
- Date:
- Tue Mar 07 04:54:45 2017 +0000
- Revision:
- 15:7c2e70c36b98
- Parent:
- 13:a436ba0b78e8
- Parent:
- 14:bd909277eb13
- Child:
- 16:f2661759714c
merged
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 | 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 | bit_sent, //position du bit de l'octet en cours d'envoi (du MSB au LSB) 7..0 |
JoeyDionne | 12:a55f77a0e188 | 18 | trame_length; //longueur de la trame complete |
JoeyDionne | 11:b27d1a83f688 | 19 | |
ericbisson | 14:bd909277eb13 | 20 | void initTimers() |
ericbisson | 14:bd909277eb13 | 21 | { |
ericbisson | 14:bd909277eb13 | 22 | //Timer 1 (match) |
ericbisson | 14:bd909277eb13 | 23 | LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 |
ericbisson | 14:bd909277eb13 | 24 | LPC_SC->PCONP |= (1 << 2); // timer1 power on |
ericbisson | 15:7c2e70c36b98 | 25 | LPC_TIM1->MR0 = 960000; // 100 ms |
ericbisson | 14:bd909277eb13 | 26 | LPC_TIM1->MCR = 3; // interrupt and reset control |
ericbisson | 14:bd909277eb13 | 27 | LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match |
ericbisson | 14:bd909277eb13 | 28 | NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt |
ericbisson | 14:bd909277eb13 | 29 | LPC_TIM1->TCR = 1; // enable Timer |
ericbisson | 14:bd909277eb13 | 30 | |
ericbisson | 14:bd909277eb13 | 31 | //Timer 2 (cap) |
ericbisson | 14:bd909277eb13 | 32 | LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 |
ericbisson | 14:bd909277eb13 | 33 | LPC_SC->PCONP |= (1 << 22); // timer2 power on |
ericbisson | 14:bd909277eb13 | 34 | LPC_TIM2->TC = 0; // clear timer counter |
ericbisson | 14:bd909277eb13 | 35 | LPC_TIM2->PC = 0; // clear prescale counter |
ericbisson | 14:bd909277eb13 | 36 | LPC_TIM2->PR = 0; // clear prescale register |
ericbisson | 14:bd909277eb13 | 37 | LPC_TIM2->TCR |= (1 << 1); // reset timer |
ericbisson | 14:bd909277eb13 | 38 | LPC_TIM2->TCR &= ~(1 << 1); // release reset |
ericbisson | 14:bd909277eb13 | 39 | LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register |
ericbisson | 14:bd909277eb13 | 40 | LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture |
ericbisson | 14:bd909277eb13 | 41 | NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt |
ericbisson | 14:bd909277eb13 | 42 | LPC_TIM2->TCR = 1; // start Timer |
ericbisson | 14:bd909277eb13 | 43 | } |
JoeyDionne | 11:b27d1a83f688 | 44 | |
ericbisson | 14:bd909277eb13 | 45 | bool bTimer1 = false; |
ericbisson | 14:bd909277eb13 | 46 | extern "C" void TIMER1_IRQHandler() |
ericbisson | 14:bd909277eb13 | 47 | { |
ericbisson | 14:bd909277eb13 | 48 | if ((LPC_TIM1->IR & 0x01) == 0x01) |
ericbisson | 14:bd909277eb13 | 49 | { |
ericbisson | 14:bd909277eb13 | 50 | bTimer1 = !bTimer1; |
ericbisson | 14:bd909277eb13 | 51 | |
ericbisson | 14:bd909277eb13 | 52 | LPC_TIM1->IR |= 1 << 0; // clear |
ericbisson | 14:bd909277eb13 | 53 | } |
ericbisson | 1:b3ae0d9f02ad | 54 | } |
ericbisson | 1:b3ae0d9f02ad | 55 | |
ericbisson | 14:bd909277eb13 | 56 | int sumClocks = 0; |
ericbisson | 14:bd909277eb13 | 57 | int PeriodLength = 0; |
ericbisson | 14:bd909277eb13 | 58 | bool bReceivedFirstBit = false; |
ericbisson | 14:bd909277eb13 | 59 | extern "C" void TIMER2_IRQHandler() |
ericbisson | 7:5501dbea5650 | 60 | { |
ericbisson | 14:bd909277eb13 | 61 | if (PeriodLength > 0) |
ericbisson | 7:5501dbea5650 | 62 | { |
ericbisson | 15:7c2e70c36b98 | 63 | if (LPC_TIM2->CR0 >= PeriodLength*1.5 || (sumClocks + LPC_TIM2->CR0) >= PeriodLength*1.5) |
ericbisson | 8:5b87b1f9d91f | 64 | { |
ericbisson | 14:bd909277eb13 | 65 | sumClocks = 0; |
ericbisson | 14:bd909277eb13 | 66 | |
ericbisson | 14:bd909277eb13 | 67 | ThreadLecture.signal_set(1); |
ericbisson | 14:bd909277eb13 | 68 | } |
ericbisson | 14:bd909277eb13 | 69 | else |
ericbisson | 14:bd909277eb13 | 70 | { |
ericbisson | 14:bd909277eb13 | 71 | sumClocks += LPC_TIM2->CR0; |
ericbisson | 8:5b87b1f9d91f | 72 | } |
ericbisson | 1:b3ae0d9f02ad | 73 | } |
ericbisson | 14:bd909277eb13 | 74 | else if (bReceivedFirstBit) |
ericbisson | 14:bd909277eb13 | 75 | { |
ericbisson | 14:bd909277eb13 | 76 | PeriodLength = LPC_TIM2->CR0 / 2; |
ericbisson | 14:bd909277eb13 | 77 | ThreadLecture.signal_set(1); |
ericbisson | 14:bd909277eb13 | 78 | } |
ericbisson | 14:bd909277eb13 | 79 | else |
ericbisson | 14:bd909277eb13 | 80 | { |
ericbisson | 14:bd909277eb13 | 81 | bReceivedFirstBit = true; |
ericbisson | 14:bd909277eb13 | 82 | ThreadLecture.signal_set(1); |
ericbisson | 14:bd909277eb13 | 83 | } |
ericbisson | 15:7c2e70c36b98 | 84 | |
ericbisson | 14:bd909277eb13 | 85 | LPC_TIM2->TC = 0; |
ericbisson | 14:bd909277eb13 | 86 | LPC_TIM2->IR |= 0xFFFFFFFF; // clear |
ericbisson | 14:bd909277eb13 | 87 | } |
ericbisson | 4:87e9b434bb4d | 88 | |
JoeyDionne | 13:a436ba0b78e8 | 89 | bool codeManchester(bool bit, bool clock) |
JoeyDionne | 13:a436ba0b78e8 | 90 | { |
JoeyDionne | 13:a436ba0b78e8 | 91 | return (bit == clock); |
JoeyDionne | 13:a436ba0b78e8 | 92 | } |
JoeyDionne | 13:a436ba0b78e8 | 93 | |
JoeyDionne | 12:a55f77a0e188 | 94 | void send_data() |
JoeyDionne | 12:a55f77a0e188 | 95 | { |
ericbisson | 15:7c2e70c36b98 | 96 | out = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), bTimer1); // Encodage Manchester |
ericbisson | 15:7c2e70c36b98 | 97 | if(bTimer1) |
JoeyDionne | 13:a436ba0b78e8 | 98 | { |
JoeyDionne | 12:a55f77a0e188 | 99 | bit_sent--; |
JoeyDionne | 12:a55f77a0e188 | 100 | } |
ericbisson | 15:7c2e70c36b98 | 101 | bTimer1 = !bTimer1; //varier entre la 1ere et 2eme partie de demi-periode |
JoeyDionne | 12:a55f77a0e188 | 102 | if (bit_sent < 0) { //Si l'octet a ete envoye |
JoeyDionne | 12:a55f77a0e188 | 103 | bit_sent = 7; //remettre la position initiale pour le prochain octet |
JoeyDionne | 12:a55f77a0e188 | 104 | byte_sent_pos++; //incrementer l'octet |
JoeyDionne | 13:a436ba0b78e8 | 105 | if (byte_sent_pos >= trame_length) { //Si la trame a ete envoyee |
JoeyDionne | 12:a55f77a0e188 | 106 | byte_sent_pos = 0; |
ericbisson | 15:7c2e70c36b98 | 107 | bTimer1 = false; |
JoeyDionne | 12:a55f77a0e188 | 108 | } |
JoeyDionne | 12:a55f77a0e188 | 109 | } |
JoeyDionne | 12:a55f77a0e188 | 110 | } |
JoeyDionne | 12:a55f77a0e188 | 111 | |
JoeyDionne | 13:a436ba0b78e8 | 112 | //création de la trame |
JoeyDionne | 13:a436ba0b78e8 | 113 | void create_trame(char message[],int taille) |
JoeyDionne | 12:a55f77a0e188 | 114 | { |
JoeyDionne | 12:a55f77a0e188 | 115 | CRC16 *myCRC = new CRC16(); |
JoeyDionne | 13:a436ba0b78e8 | 116 | unsigned short resultCRC = myCRC->calculateCRC16(message,taille); |
JoeyDionne | 12:a55f77a0e188 | 117 | |
JoeyDionne | 12:a55f77a0e188 | 118 | trame_sent[0] = 0x55; //Preambule |
JoeyDionne | 12:a55f77a0e188 | 119 | trame_sent[1] = 0x7E; //Start |
JoeyDionne | 12:a55f77a0e188 | 120 | trame_sent[2] = 0x00; //Type + Flag mis a 0x00 |
JoeyDionne | 13:a436ba0b78e8 | 121 | trame_sent[3] = (char)taille; //Longueur du message (Max 33 caractères) |
JoeyDionne | 12:a55f77a0e188 | 122 | |
JoeyDionne | 13:a436ba0b78e8 | 123 | //message |
JoeyDionne | 13:a436ba0b78e8 | 124 | for (int i=0;i<taille;i++) |
JoeyDionne | 13:a436ba0b78e8 | 125 | { |
JoeyDionne | 13:a436ba0b78e8 | 126 | trame_sent[taille + 4] = message[i]; |
JoeyDionne | 13:a436ba0b78e8 | 127 | } |
JoeyDionne | 13:a436ba0b78e8 | 128 | |
JoeyDionne | 13:a436ba0b78e8 | 129 | //CRC16 |
JoeyDionne | 13:a436ba0b78e8 | 130 | trame_sent[taille + 4] = (resultCRC >> 8) & 0xFF; |
JoeyDionne | 13:a436ba0b78e8 | 131 | trame_sent[taille + 5] = resultCRC & 0xFF; |
JoeyDionne | 13:a436ba0b78e8 | 132 | |
JoeyDionne | 13:a436ba0b78e8 | 133 | trame_sent[taille + 6] = 0x7E; //End |
JoeyDionne | 13:a436ba0b78e8 | 134 | trame_length = taille + 7; //Longueur de la trame |
JoeyDionne | 13:a436ba0b78e8 | 135 | } |
JoeyDionne | 13:a436ba0b78e8 | 136 | |
JoeyDionne | 13:a436ba0b78e8 | 137 | //obtention du texte |
JoeyDionne | 13:a436ba0b78e8 | 138 | void get_text() |
JoeyDionne | 13:a436ba0b78e8 | 139 | { |
JoeyDionne | 12:a55f77a0e188 | 140 | pc.printf("\n\rYour text : "); |
JoeyDionne | 12:a55f77a0e188 | 141 | int count = 0; |
JoeyDionne | 12:a55f77a0e188 | 142 | char c = 0x00; |
JoeyDionne | 12:a55f77a0e188 | 143 | char text[73]; |
JoeyDionne | 13:a436ba0b78e8 | 144 | while(c != 0x0D && count < 73) { //Tant que c'est different de '\r' et pour un maximum de 73 caractères |
JoeyDionne | 12:a55f77a0e188 | 145 | c = pc.getc(); |
JoeyDionne | 12:a55f77a0e188 | 146 | text[count] = c; |
JoeyDionne | 12:a55f77a0e188 | 147 | pc.putc(c); |
JoeyDionne | 12:a55f77a0e188 | 148 | count++; |
JoeyDionne | 12:a55f77a0e188 | 149 | } |
JoeyDionne | 12:a55f77a0e188 | 150 | |
JoeyDionne | 13:a436ba0b78e8 | 151 | create_trame(text,count); |
JoeyDionne | 12:a55f77a0e188 | 152 | } |
JoeyDionne | 12:a55f77a0e188 | 153 | |
JoeyDionne | 12:a55f77a0e188 | 154 | void write() |
JoeyDionne | 12:a55f77a0e188 | 155 | { |
JoeyDionne | 12:a55f77a0e188 | 156 | byte_sent_pos = 0; |
JoeyDionne | 12:a55f77a0e188 | 157 | bit_sent = 7; |
ericbisson | 15:7c2e70c36b98 | 158 | bTimer1 = false; |
JoeyDionne | 13:a436ba0b78e8 | 159 | //TODO start interrupt timer 1 |
JoeyDionne | 13:a436ba0b78e8 | 160 | //LPC_RIT->MCR = 1; //Permettre les interruption du MAT |
JoeyDionne | 13:a436ba0b78e8 | 161 | //LPC_RIT->MR0 = LPC_RIT->TC + HALF_PERIOD; //Faire une interruption a la prochaine demi-periode |
JoeyDionne | 12:a55f77a0e188 | 162 | } |
JoeyDionne | 12:a55f77a0e188 | 163 | |
ericbisson | 15:7c2e70c36b98 | 164 | void read() |
ericbisson | 15:7c2e70c36b98 | 165 | { |
ericbisson | 15:7c2e70c36b98 | 166 | char byte; |
ericbisson | 15:7c2e70c36b98 | 167 | vector<char> bytes; |
ericbisson | 15:7c2e70c36b98 | 168 | char shift = 0; |
ericbisson | 15:7c2e70c36b98 | 169 | while (true) |
ericbisson | 15:7c2e70c36b98 | 170 | { |
ericbisson | 15:7c2e70c36b98 | 171 | ThreadLecture.signal_wait(1); |
ericbisson | 15:7c2e70c36b98 | 172 | |
ericbisson | 15:7c2e70c36b98 | 173 | byte = (byte << 1) + in; |
ericbisson | 15:7c2e70c36b98 | 174 | |
ericbisson | 15:7c2e70c36b98 | 175 | shift++; |
ericbisson | 15:7c2e70c36b98 | 176 | if (shift == 8) |
ericbisson | 15:7c2e70c36b98 | 177 | { |
ericbisson | 15:7c2e70c36b98 | 178 | bytes.push_back(byte); |
ericbisson | 15:7c2e70c36b98 | 179 | shift = 0; |
ericbisson | 15:7c2e70c36b98 | 180 | } |
ericbisson | 15:7c2e70c36b98 | 181 | } |
ericbisson | 15:7c2e70c36b98 | 182 | } |
ericbisson | 15:7c2e70c36b98 | 183 | |
ericbisson | 2:c6465d4e82d2 | 184 | int main() { |
ericbisson | 14:bd909277eb13 | 185 | LPC_PINCON->PINSEL0 |= (3 << 8); // pin30 |
ericbisson | 14:bd909277eb13 | 186 | initTimers(); |
ericbisson | 8:5b87b1f9d91f | 187 | ThreadLecture.start(read); |
ericbisson | 0:c637467eeb8f | 188 | |
ericbisson | 3:3ecbcc05bc85 | 189 | while(true) { |
ericbisson | 0:c637467eeb8f | 190 | } |
ericbisson | 7:5501dbea5650 | 191 | }; |