1
0
Fork 0
mirror of https://github.com/RobotechLille/cdf2018-principal synced 2024-11-23 16:46:04 +01:00
cdf2018-principal/arduino/AC.c

107 lines
2.3 KiB
C
Raw Normal View History

2018-02-14 18:07:10 +01:00
#include "AC.h"
void registerRxHandlerAC(unsigned char code, rxHandler handler)
2018-02-14 18:07:10 +01:00
{
rxHandlersAC[code] = handler;
2018-02-14 18:07:10 +01:00
}
2018-02-14 18:07:10 +01:00
ISR(USART0_UDRE_vect)
{
// When a transmit is ready to be done again
UDR0 = *toSendAC;
toSendAC++;
toSendSizeAC--;
if (toSendSizeAC <= 0) {
UCSR0B &= ~(1 << UDRIE0);
TaskHandle_t holder = xSemaphoreGetMutexHolder(sSendAC);
if (holder != NULL) {
vTaskNotifyGiveFromISR(holder, NULL);
}
}
}
2018-02-14 18:07:10 +01:00
void sendAC(unsigned char code, void* data, size_t size)
{
xSemaphoreTake(sSendAC, portMAX_DELAY);
toSendAC = &code;
toSendSizeAC = sizeof(code);
UCSR0B |= (1 << UDRIE0);
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
if (size > 0) {
toSendAC = data;
toSendSizeAC = size;
UCSR0B |= (1 << UDRIE0);
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
}
xSemaphoreGive(sSendAC);
}
2018-02-14 18:07:10 +01:00
ISR(USART0_RX_vect)
{
// When a character is received
vTaskNotifyGiveFromISR(tReaderAC, NULL);
}
2018-02-14 18:07:10 +01:00
unsigned char readByteAC()
{
while (!bit_is_set(UCSR0A, RXC0)) {
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
}
unsigned char c = UDR0;
return c;
}
2018-02-14 18:07:10 +01:00
void readAC(void* data, size_t size)
{
unsigned char* p = data;
for (int i = 0; i < size; i++) {
*p = readByteAC();
p++;
}
}
void TaskReaderAC(void* pvParameters)
{
(void)pvParameters;
unsigned char code;
for (;;) {
2018-02-14 18:07:10 +01:00
code = readByteAC();
rxHandler handler = rxHandlersAC[code];
2018-02-14 18:07:10 +01:00
if (handler != NULL) {
handler();
} else {
struct A2CD_ERRs err = { ERR_UNKNOWN_CODE };
2018-02-14 18:07:10 +01:00
sendAC(A2CD_ERR, &err, sizeof(err));
}
}
}
void onC2AD_PING()
{
sendAC(C2AD_PING, NULL, 0);
}
2018-02-14 18:07:10 +01:00
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 << TXEN0 | 1 << RXEN0);
/* Set 8 bits character and 1 stop bit */
UCSR0C = (1 << UCSZ01 | 1 << UCSZ00);
2018-02-14 18:07:10 +01:00
for (int i = 0; i < 256; i++) {
rxHandlersAC[i] = NULL;
2018-02-14 18:07:10 +01:00
}
sSendAC = xSemaphoreCreateMutex();
xTaskCreate(TaskReaderAC, "TaskReaderAC", 128, NULL, 2, &tReaderAC);
registerRxHandlerAC(C2AD_PING, onC2AD_PING);
}