mirror of
https://github.com/RobotechLille/cdf2018-principal
synced 2024-11-24 17:16:04 +01:00
Communication A↔C : Base coté Arduino
This commit is contained in:
parent
e0cb79d1cc
commit
078b4ad2d9
37
arduino/ACsignals.h
Normal file
37
arduino/ACsignals.h
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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 \
|
||||
|
|
|
@ -1,25 +1,30 @@
|
|||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
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;
|
||||
|
|
74
arduino/serial.c
Normal file
74
arduino/serial.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <queue.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
20
arduino/serial.h
Normal file
20
arduino/serial.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef __SERIAL_H_
|
||||
#define __SERIAL_H_
|
||||
|
||||
#include <semphr.h>
|
||||
#include <stdlib.h>
|
||||
#include <task.h>
|
||||
|
||||
#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
|
Loading…
Reference in a new issue