app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
main.cpp
- Committer:
- JoeyDionne
- Date:
- 2017-03-07
- Revision:
- 13:a436ba0b78e8
- Parent:
- 12:a55f77a0e188
- Child:
- 15:7c2e70c36b98
- Child:
- 21:73db1bea19b1
File content as of revision 13:a436ba0b78e8:
#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); } };