add intercom queue

This commit is contained in:
Martijn Scheepers
2021-12-03 15:18:29 +01:00
parent b95ed7def4
commit 28c9190288
9 changed files with 241 additions and 63 deletions

View File

@@ -1,38 +1,162 @@
/*
* Intercom.c
*
* Created: 3-12-2021 13:26:59
* Author: MS
*/
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
//#include <avr/pgmspace.h>
//#include <util/atomic.h>
#include <string.h>
#include <stddef.h>
* Intercom.c
*
* Created: 3-12-2021 13:26:59
* Author: MS
*/
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
//#include <util/atomic.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "Global.h"
#include "usart.h"
#include "GPIO.h"
#include "Eeprom.h"
#include "AnswerMessages.h"
#include "Global.h"
#include "usart.h"
#include "Intercom.h"
QueueHandle_t intercomRxQueue = NULL;
QueueHandle_t intercomTxQueue = NULL;
// ---------------- Messages Queue ---------------------------
void addToIntercomTxQueue(const char *str)
{
if( intercomTxQueue != NULL )
{
if( xQueueSend( intercomTxQueue, (void *)str, (TickType_t) 10 ) != pdPASS )
{
#ifdef INTERCOMDEBUG
printf_P(PSTR("intercomTxQueue post failed - %s\r\n"), str);
#endif //INTERCOMDEBUG
}
}
}
void addToIntercomRxQueue(const char *str)
{
if (intercomRxQueue != NULL)
{
if (xQueueSend(intercomRxQueue, str, 0 ) == pdFALSE)
{
#ifdef INTERCOMDEBUG
printf_P(PSTR("intercomRxQueue send failed\r\n"));
#endif //INTERCOMDEBUG
}
}
}
void getFromIntercomRxQueue(char *buf)
{
if( intercomRxQueue != NULL )
{
xQueueReceive(intercomRxQueue, buf, portMAX_DELAY);
}
}
//Data format for messages with fixed length
//<STX>RRPPTTXXXXYYYY$$SS<ETX>
//Data format for messages with variable length
//<STX>RRPPTT$$D1...DnSS<ETX>
//<STX> (Hex 02) for synchronisation
//RR System number of the receiver
//PP Task number
//TT System number of the initiator
//XXXX Parameter 1
//YYYY Parameter 2
//D1...Dn Data, variable length (max. 240 bytes, 2 digits are 1 byte)
//$$ Type of message
//SS Check sum
//<ETX> (Hex 03) End of message
void vIntercomTaskFunction(void *pvParameters)
{
(void)pvParameters; //Silence warning
char txData[100];
char rxData[100];
int rxIndex = 0;
uint16_t rxTimeout = 0;
for(;;)
{
if(intercomTxQueue != NULL)
{
if(xQueueReceive(intercomTxQueue, txData, 0) == pdPASS)
{
usart2_putstr(txData);
//usart3_putstr(txData);
//vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
taskYIELD();
if (IntercomDataInReceiveBuffer())
{
uint8_t cChar = IntercomReceiveByte();
#ifdef INTERCOMDEBUG
printf_P(PSTR("intercom byte : %c - index : %u\r\n"), cChar, rxIndex);
#endif //INTERCOMDEBUG
rxData[rxIndex] = cChar;
rxIndex++;
if (cChar == 0x02)
{
//STX start of message
memset(rxData, '\0', sizeof(rxData));
rxIndex = 0;
}
if (cChar == 0x03)
{
//ETX end of message
#ifdef INTERCOMDEBUG
printf_P(PSTR("add to intercom queue : %s\r\n"), rxData);
#endif //INTERCOMDEBUG
addToIntercomRxQueue(rxData);
memset(rxData, '\0', sizeof(rxData));
rxIndex = 0;
}
//if (rxIndex > 12)
//{
//memset(rxData, '\0', sizeof(rxData));
//rxIndex = 0;
//}
rxTimeout = 0;
}
rxTimeout++;
if (rxTimeout > 256)
{
memset(rxData, '\0', sizeof(rxData));
rxIndex = 0;
}
taskYIELD();
}
vTaskDelete(NULL);
}
void createIntercomBuffers()
{
intercomRxQueue = xQueueCreate(INTERCOMRXQUEUESIZE, sizeof(char[13]));
if( intercomRxQueue == NULL )
{
printf_P(PSTR("intercomRxQueue creation failed\r\n"));
}
void vIntercomTaskFunction(void *pvParameters)
{
(void)pvParameters; //Silence warning
for( ;; )
{
//readInputs();
//vTaskDelay(100 / portTICK_PERIOD_MS);
taskYIELD();
}
vTaskDelete( NULL );
}
intercomTxQueue = xQueueCreate(INTERCOMTXQUEUESIZE, sizeof(char[13]));
if( intercomRxQueue == NULL )
{
printf_P(PSTR("intercomTxQueue creation failed\r\n"));
}
}

View File

@@ -9,8 +9,13 @@
#ifndef INTERCOM_H_
#define INTERCOM_H_
#define INTERCOMDEBUG
#define INTERCOMRXQUEUESIZE 16
#define INTERCOMTXQUEUESIZE 16
void vIntercomTaskFunction(void *pvParameters);
void createIntercomBuffers();
#endif /* INTERCOM_H_ */

View File

@@ -35,9 +35,9 @@ void addToRCMTxQueue(const char *str)
{
if( xQueueSend( rcmTxQueue, (void *)str, (TickType_t) 10 ) != pdPASS )
{
#ifdef USARTDEBUG
#ifdef RCMDEBUG
printf_P(PSTR("rcmTxQueue post failed - %s\r\n"), str);
#endif //USARTDEBUG
#endif //RCMDEBUG
}
}
}
@@ -48,9 +48,9 @@ void addToRCMRxQueue(const char *str)
{
if (xQueueSend(rcmRxQueue, str, 0 ) == pdFALSE)
{
#ifdef USARTDEBUG
#ifdef RCMDEBUG
printf_P(PSTR("rcmRxQueue send failed\r\n"));
#endif //USARTDEBUG
#endif //RCMDEBUG
}
}
}
@@ -89,17 +89,17 @@ void vRCMTask(void *pvParameters)
{
uint8_t cChar = RCMReceiveByte();
#ifdef USARTDEBUG
#ifdef RCMDEBUG
printf_P(PSTR("RCM byte : %c - index : %u\r\n"), cChar, rxIndex);
#endif //USARTDEBUG
#endif //RCMDEBUG
rxData[rxIndex] = cChar;
rxIndex++;
if ((cChar == '$' && rxIndex == 8) || (cChar == '&' && rxIndex == 12))
{
#ifdef USARTDEBUG
printf_P(PSTR("add to queue : %s\r\n"), rxData);
#endif //USARTDEBUG
#ifdef RCMDEBUG
printf_P(PSTR("add to RCM queue : %s\r\n"), rxData);
#endif //RCMDEBUG
addToRCMRxQueue(rxData);
memset(rxData, '\0', sizeof(rxData));

View File

@@ -9,11 +9,10 @@
#ifndef RCM_H_
#define RCM_H_
//#define RCMDEBUG
#define RCMRXQUEUESIZE 16
#define RCMTXQUEUESIZE 16
#define SERIALPORTQUEUESIZE 24
void addToRCMTxQueue(const char *str);
void addToRCMRxQueue(const char *str);

View File

@@ -51,6 +51,23 @@ void ucsQCodeFromNetwork(char *data){
LEDOFF(LED_BUSY);
}
// K2Z4€01$
void ucsZCodeFromNetwork(char *data){
#ifdef UCSCOMMANDSDEBUG
printf_P(PSTR("Zone code data = %s\r\n"), data);
#endif // UCSCOMMANDSDEBUG
LEDON(LED_BUSY);
char zone = data[3];
printf_P(PSTR("Zone = %c\r\n"), zone);
LEDOFF(LED_BUSY);
}
// C0..m00$
// 01234567
void ucsCommandShort(char *data){
@@ -257,6 +274,10 @@ void vUCSTaskFunction(void *pvParameters)
{
ucsQCodeFromNetwork(rcmBuffer);
}
else if (rcmBuffer[2] == 'Z')
{
ucsZCodeFromNetwork(rcmBuffer);
}
else
{
ucsCommandShort(rcmBuffer);

View File

@@ -9,7 +9,7 @@
#ifndef UCSCOMMANDS_H_
#define UCSCOMMANDS_H_
//#define UCSCOMMANDSDEBUG
#define UCSCOMMANDSDEBUG
void vUCSTaskFunction(void *pvParameters);

View File

@@ -98,7 +98,7 @@ int main(void)
{
//wdt_disable();
wdt_enable(WDTO_8S);
cli(); // switch interrupts off while messing with their settings
cli(); // switch interrupts off while messing with their settings
stdout = &serialConsole;
stderr = &serialConsole;
@@ -132,8 +132,9 @@ int main(void)
answerOutputs();
xTaskCreate( vUCSTaskFunction, "UCS", configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY), &ucsTaskHandle);
//initUsarts();
//xTaskCreate( vIntercomTaskFunction, "intercom", configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY), &intercomTaskHandle);
initUsarts();
createIntercomBuffers();
xTaskCreate( vIntercomTaskFunction, "intercom", configMINIMAL_STACK_SIZE*6, NULL, (tskIDLE_PRIORITY), &intercomTaskHandle);
wdt_enable(WDTO_1S);
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG

View File

@@ -171,7 +171,6 @@ ISR(USART1_RX_vect) //RCM
USART1_RxBuf[tmphead] = UDR1;
LEDOFF(LED_BUSY);
}
unsigned char RCMReceiveByte(void)
{
unsigned char tmptail;
@@ -179,26 +178,43 @@ unsigned char RCMReceiveByte(void)
USART1_RxTail = tmptail; /* store new index */
return USART1_RxBuf[tmptail]; /* return data */
}
bool RCMDataInReceiveBuffer(void)
{
return (USART1_RxHead != USART1_RxTail);
}
//-------- USART 2 intercom ---------------
static unsigned char USART2_RxBuf[UART2_RX_BUFFER_SIZE];
static volatile unsigned char USART2_RxHead;
static volatile unsigned char USART2_RxTail;
ISR(USART2_RX_vect) //USART 2
{
LEDON(LED_BUSY);
//If error bit set (frame error, data over run, parity), return nothing
//if (! (UCSR1A & ((1<<FE1)|(1<<DOR1)|(1<<UPE1)))){
uint8_t cChar = UDR2;
(void)cChar;
//}
LEDON(LED_BUSY);
uint8_t tmphead = (USART2_RxHead + 1) & UART2_RX_BUFFER_MASK;
USART2_RxHead = tmphead; /* store new index */
#ifdef USARTDEBUG
if (tmphead == USART2_RxTail)
{
printf_P(PSTR("USART2 Receive buffer overflow\r\n"));
}
#endif //USARTDEBUG
USART2_RxBuf[tmphead] = UDR2;
LEDOFF(LED_BUSY);
}
unsigned char IntercomReceiveByte(void)
{
unsigned char tmptail;
tmptail = ( USART2_RxTail + 1 ) & UART2_RX_BUFFER_MASK;/* calculate buffer index */
USART2_RxTail = tmptail; /* store new index */
return USART2_RxBuf[tmptail]; /* return data */
}
bool IntercomDataInReceiveBuffer(void)
{
return (USART2_RxHead != USART2_RxTail);
}
//-------- USART 3 intercom ---------------
//-------- USART 3 ---------------
ISR(USART3_RX_vect) //USART 3
{
LEDON(LED_BUSY);

View File

@@ -1,9 +1,9 @@
/*
* usart.h
*
* Created: 23-7-2021
* Author: MS
*/
* usart.h
*
* Created: 23-7-2021
* Author: MS
*/
#ifndef USART_H_
@@ -31,7 +31,7 @@
#define UART1_RX_BUFFER_SIZE 32
#define UART1_RX_BUFFER_MASK ( UART1_RX_BUFFER_SIZE - 1 )
#if ( UART1_RX_BUFFER_SIZE & UART1_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#error USART 1 RX buffer size is not a power of 2
#endif
@@ -39,6 +39,12 @@
#define USART2_BAUDRATE 19200 //0.2% error
#define USART2_BAUD_PRESCALE (((F_CPU / (USART2_BAUDRATE * 16UL))) - 1)
#define UART2_RX_BUFFER_SIZE 64
#define UART2_RX_BUFFER_MASK ( UART2_RX_BUFFER_SIZE - 1 )
#if ( UART2_RX_BUFFER_SIZE & UART2_RX_BUFFER_MASK )
#error USART 2 RX buffer size is not a power of 2
#endif
//USART 3
#define USART3_BAUDRATE 19200 //0.2% error
#define USART3_BAUD_PRESCALE (((F_CPU / (USART3_BAUDRATE * 16UL))) - 1)
@@ -46,11 +52,17 @@
int usart0_putchar_printf(char var, FILE *stream);
int usart1_putchar_printf(char var, FILE *stream);
void usart1_putstr(const char *str);
void usart2_putstr(const char *str);
void usart3_putstr(const char *str);
unsigned char RCMReceiveByte(void);
bool RCMDataInReceiveBuffer(void);
unsigned char IntercomReceiveByte(void);
bool IntercomDataInReceiveBuffer(void);
void initDebugUsart();
void initRCMUsart();
void initUsarts();