Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
Revision 16:f2661759714c, committed 2017-03-07
- Comitter:
- ericbisson
- Date:
- Tue Mar 07 05:11:35 2017 +0000
- Parent:
- 15:7c2e70c36b98
- Child:
- 17:2b4b3f3a0489
- Commit message:
- update read
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp.orig | Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Mar 07 04:54:45 2017 +0000
+++ b/main.cpp Tue Mar 07 05:11:35 2017 +0000
@@ -163,20 +163,50 @@
void read()
{
- char byte;
+ char byte = 0;
vector<char> bytes;
char shift = 0;
+ char totalsize = 7;
while (true)
{
ThreadLecture.signal_wait(1);
- byte = (byte << 1) + in;
+ byte = (byte << 1) + !in; // inversion car 2e période
shift++;
if (shift == 8)
{
+ shift = 0;
+
+ // à partir d'ici, je travaille en byte et non bit
bytes.push_back(byte);
- shift = 0;
+
+ // Validations de base
+ if ((bytes.size() == 1 && bytes[0] != PREAMBULE) ||
+ (bytes.size() == 2 && bytes[1] != START))
+ {
+ bytes.clear();
+ }
+ if (bytes.size() == 3)
+ {
+ totalsize = 7 + bytes[2];
+ }
+
+ // fin
+ if (totalsize == bytes.size())
+ {
+ // Calcul du CRC
+ unsigned short currentCRC = bytes[bytes.size()-2] + bytes[bytes.size()-3]<<8;
+
+ vector<char> charge_utile(&bytes[4], &bytes[bytes.size()-4]);
+
+ if (currentCRC == mycrc16.calculateCRC16(&charge_utile[0], charge_utile.size()) && bytes.back() == END)
+ {
+ // Affiche à l'écran le message valide
+ pc.printf(&charge_utile[0], charge_utile.size());
+ }
+ bytes.clear();
+ }
}
}
}
--- a/main.cpp.orig Tue Mar 07 04:54:45 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,169 +0,0 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "bit.h"
-#include "uart.h"
-
-const float HALF_PERIOD = 0.05; // secondes
-
-Serial pc(USBTX, USBRX, 9600);
-DigitalIn in(p14);
-DigitalOut out(p13);
-Thread ThreadLecture;
-
-bool transmit = false; //faux pour le debut de demi-periode d'envoie d'un bit, vrai pour la 2ème demi-periode
-char trame_sent[80]; //tableau de la trame à envoyer
-uint8_t byte_sent_pos, //position de l'octet dans le tableau d'envoi
- bit_sent, //position du bit de l'octet en cours d'envoi (du MSB au LSB) 7..0
- trame_length; //longueur de la trame complete
-//test
-
-bool clockTick = false;
-
-extern "C" void RIT_IRQHandler(void) {
- //clear flag
- LPC_RIT->RICTRL |= bit0; //write 1 to clear bit
-
- //bIsHalfPeriod = !bIsHalfPeriod;
-}
-
-void p14_interrupt()
-{
- // On turn off les interrupts de lecture une fois qu'on a détecter un message
- //uart.attach(NULL, uart.RxIrq);
-
- // On envoie le signal au thread de lecture
- ThreadLecture.signal_set(1);
-};
-
-void read()
-{
- while(true)
- {
- // Attente passive d'un message entrant
- //uart.attach(&p14_interrupt, uart.RxIrq);
- ThreadLecture.signal_wait(1);
-
- // Lis le message. Retourne une exception si il y a une erreur
- //vector<char> message = uart_read(uart);
-
- //if (!message.empty())
- //{
- // Affiche le message à l'écran
- //pc.printf(&message[0], message.size());
- //}
- }
-};
-
-void rit_init()
-{
- //Timer 1 (match)
- LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1
- LPC_SC->PCONP |= (1 << 2); // timer1 power on
- LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 100; // 10 ms
- LPC_TIM1->MCR = 3; // interrupt and reset control
- // Interrupt & reset timer on match
- LPC_TIM1->EMR = (3 << 4);
- NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt
- LPC_TIM1->TCR = 1; // enable Timer
-
- //Timer 2 (cap)
- LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2
- LPC_SC->PCONP |= (1 << 22); // timer2 power on
- LPC_TIM2->TC = 0; // clear timer counter
- LPC_TIM2->PC = 0; // clear prescale counter
- LPC_TIM2->PR = 0; // clear prescale register
- LPC_TIM2->TCR |= (1 << 1); // reset timer
- LPC_TIM2->TCR &= ~(1 << 1); // release reset
- LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register
- LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture on 2.0
- NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt
- LPC_TIM2->TCR = 1; // start Timer
-};
-
-bool codeManchester(bool bit, bool clock)
-{
- return (bit == clock);
-}
-
-void send_data()
-{
- out = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), clockTick); // Encodage Manchester
- if(clockTick)
- {
- bit_sent--;
- }
- clockTick = !clockTick; //varier entre la 1ere et 2eme partie de demi-periode
- if (bit_sent < 0) { //Si l'octet a ete envoye
- bit_sent = 7; //remettre la position initiale pour le prochain octet
- byte_sent_pos++; //incrementer l'octet
- if (byte_sent_pos >= trame_length) { //Si la trame a ete envoyee
- byte_sent_pos = 0;
- clockTick = false;
- }
- }
-}
-
-//création de la trame
-void create_trame(char message[],int taille)
-{
- CRC16 *myCRC = new CRC16();
- unsigned short resultCRC = myCRC->calculateCRC16(message,taille);
-
- trame_sent[0] = 0x55; //Preambule
- trame_sent[1] = 0x7E; //Start
- trame_sent[2] = 0x00; //Type + Flag mis a 0x00
- trame_sent[3] = (char)taille; //Longueur du message (Max 33 caractères)
-
- //message
- for (int i=0;i<taille;i++)
- {
- trame_sent[taille + 4] = message[i];
- }
-
- //CRC16
- trame_sent[taille + 4] = (resultCRC >> 8) & 0xFF;
- trame_sent[taille + 5] = resultCRC & 0xFF;
-
- trame_sent[taille + 6] = 0x7E; //End
- trame_length = taille + 7; //Longueur de la trame
-}
-
-//obtention du texte
-void get_text()
-{
- pc.printf("\n\rYour text : ");
- int count = 0;
- char c = 0x00;
- char text[73];
- while(c != 0x0D && count < 73) { //Tant que c'est different de '\r' et pour un maximum de 73 caractères
- c = pc.getc();
- text[count] = c;
- pc.putc(c);
- count++;
- }
-
- create_trame(text,count);
-}
-
-void write()
-{
- byte_sent_pos = 0;
- bit_sent = 7;
- clockTick = false;
- //TODO start interrupt timer 1
- //LPC_RIT->MCR = 1; //Permettre les interruption du MAT
- //LPC_RIT->MR0 = LPC_RIT->TC + HALF_PERIOD; //Faire une interruption a la prochaine demi-periode
-}
-
-int main() {
- rit_init();
-
- ThreadLecture.start(read);
-
- while(true) {
- get_text();
- write();
- //uart.printf("12345");
- wait_ms(100);
- }
-};
\ No newline at end of file
