From 078b4ad2d9fa82ce36cb4ef3421ca852ea031e78 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Mon, 12 Feb 2018 13:51:51 +0100 Subject: [PATCH] =?UTF-8?q?Communication=20A=E2=86=94C=20:=20Base=20cot?= =?UTF-8?q?=C3=A9=20Arduino?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arduino/ACsignals.h | 37 ++++++++++++++++++++ arduino/FreeRTOSConfig.h | 5 +++ arduino/Makefile | 3 ++ arduino/principal.c | 21 +++++++----- arduino/serial.c | 74 ++++++++++++++++++++++++++++++++++++++++ arduino/serial.h | 20 +++++++++++ 6 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 arduino/ACsignals.h create mode 100644 arduino/serial.c create mode 100644 arduino/serial.h diff --git a/arduino/ACsignals.h b/arduino/ACsignals.h new file mode 100644 index 0000000..0b95384 --- /dev/null +++ b/arduino/ACsignals.h @@ -0,0 +1,37 @@ +// Définition des signaux échagés entre l'Arduino et le chef +// + +#ifndef __ACSIGNALS_H_ +#define __ACSIGNALS_H_ + +#define AC_BAUDRATE 9600UL + +// D: Direct +// Sens naturel : Envoi de donnée +// Sens inverse : Accusé de réception + +// I: Indirect +// Sens inverse : Demande de donnée +// Sens naturel : Envoi de donnée + +// T: Trigger +// Sens inverse : Paramètrage du trigger +// Sens naturel : Envoi de trigger + +// Chef → Arduino + +// Pour le debug +#define C2AD_PING 0 +// Arrête tous les actionneurs +#define C2AD_STOP 1 +// Donne une destination +#define C2AD_GOTO 2 +// Donne une rotation +#define C2AD_ROTATE 3 + +// Arduino → Chef + +// Envoie la position actuelle +#define A2CI_POS + +#endif diff --git a/arduino/FreeRTOSConfig.h b/arduino/FreeRTOSConfig.h index 37757be..89c586a 100644 --- a/arduino/FreeRTOSConfig.h +++ b/arduino/FreeRTOSConfig.h @@ -80,6 +80,11 @@ * See http://www.freertos.org/a00110.html. *----------------------------------------------------------*/ +// GF: added +#define configUSE_MUTEXES 1 +#define INCLUDE_xQueueGetMutexHolder 1 +// -- done + // JF: added #define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY 3 diff --git a/arduino/Makefile b/arduino/Makefile index 73097b9..ac76a72 100644 --- a/arduino/Makefile +++ b/arduino/Makefile @@ -29,9 +29,11 @@ FORMAT = ihex # Target file name (without extension). TARGET = principal +OBJS = serial # Custom NAME = $(TARGET).c +OBJSC = $(addsuffix .c,$(OBJS)) # List C source files here. (C dependencies are automatically generated.) INSTALL_DIR=./FreeRTOSv9.0.0/FreeRTOS @@ -41,6 +43,7 @@ PORT_DIR = . SRC = \ $(NAME) \ +$(OBJSC) \ $(SOURCE_DIR)/tasks.c \ $(SOURCE_DIR)/queue.c \ $(SOURCE_DIR)/list.c \ diff --git a/arduino/principal.c b/arduino/principal.c index cab6aed..48bf23e 100644 --- a/arduino/principal.c +++ b/arduino/principal.c @@ -1,25 +1,30 @@ +#include +#include #include #include -#include -void TaskBlink(void *pvParameters) -{ +#include "serial.h" + +unsigned char speed = 200; + +void TaskBlink(void *pvParameters) { (void) pvParameters; TickType_t xLastWakeTime; - const TickType_t xFrequency = 200 / portTICK_PERIOD_MS; + TickType_t xFrequency = speed / portTICK_PERIOD_MS; DDRB = 0xFF; xLastWakeTime = xTaskGetTickCount(); - for (;;) - { + for (;;) { PORTB = PINB ^ 0xFF; vTaskDelayUntil(&xLastWakeTime, xFrequency); } } -int main(void) -{ +int main(void) { + configureAC(); + sei(); + xTaskCreate(TaskBlink, "Blink", 128, NULL, 2, NULL); vTaskStartScheduler(); return 0; diff --git a/arduino/serial.c b/arduino/serial.c new file mode 100644 index 0000000..386b5d4 --- /dev/null +++ b/arduino/serial.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +#include "serial.h" +#include "ACsignals.h" + +ISR(USART0_UDRE_vect) { + // When a transmit is ready to be done again + TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC); + if (holder != NULL) { + vTaskResume(holder); + } +} + +void sendByteAC(unsigned char data) { + while (!bit_is_set(UCSR0A, UDRE0)) { + vTaskSuspend(xSemaphoreGetMutexHolder(sSendAC)); + } + UDR0 = data; +} + +void sendAC(unsigned char code, void* data, size_t size) { + xSemaphoreTake(sSendAC, 0); + sendByteAC(code); + unsigned char* p = data; + for (int i = 0; i < size; i++) { + sendByteAC(*p++); + } + xSemaphoreGive(sSendAC); +} + +ISR(USART0_RX_vect) { + // When a character is received + vTaskResume(tReaderAC); +} + + +unsigned char readAC() { + while (!bit_is_set(UCSR0A, RXC0)) { + vTaskSuspend(tReaderAC); + } + return UDR0; +} + +void TaskReaderAC(void *pvParameters) { + (void) pvParameters; + unsigned char code; + for (;;) { + code = readAC(); + + char* sending = "Bonjour ! Comment va ?"; + sendAC('@', sending, 22); + } +} + +void configureAC() { + /* Set baud rate */ + UBRR0 = AC_PRESCALER; + + /* Set off UART baud doubler */ + UCSR0A &= ~(1 << U2X0); + + /* Enable transmitter & receiver with interrupts */ + UCSR0B = (1 << RXCIE0 | 1 << UDRIE0 | 1 << TXEN0 | 1 << RXEN0); + + /* Set 8 bits character and 1 stop bit */ + UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); + + sSendAC = xSemaphoreCreateMutex(); + xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC); +} + diff --git a/arduino/serial.h b/arduino/serial.h new file mode 100644 index 0000000..970b9aa --- /dev/null +++ b/arduino/serial.h @@ -0,0 +1,20 @@ +#ifndef __SERIAL_H_ +#define __SERIAL_H_ + +#include +#include +#include + +#define CPU_FREQ 16000000UL +#define AC_PRESCALER CPU_FREQ / (AC_BAUDRATE << 4) - 1 + +TaskHandle_t tReaderAC; +SemaphoreHandle_t sSendAC; + +void sendByteAC(unsigned char data); +void sendAC(unsigned char code, void* data, size_t size); +unsigned char readAC(); +void TaskReaderAC(void *pvParameters); +void configureAC(); + +#endif