RCM to seperate file

This commit is contained in:
Martijn Scheepers
2021-12-03 13:57:17 +01:00
parent 0c7a56fd19
commit b95ed7def4
13 changed files with 382 additions and 169 deletions

View File

@@ -10,19 +10,20 @@
#include "util/delay.h"
#include "Global.h"
#include "usart.h"
#include "RCM.h"
void answerError(){
errorFlag = 5;
char answer[10];
sprintf_P(answer, PSTR("%s-ERROR&"),myId);
addToTxQueue(answer);
addToRCMTxQueue(answer);
}
void answerId(){
char answer[9];
sprintf_P(answer, PSTR("%s..z%c_$"),myId, myType);
addToTxQueue(answer);
addToRCMTxQueue(answer);
}
void answerInputs(){
@@ -31,14 +32,14 @@ void answerInputs(){
val &= ~(1 << 7);
char answer[9];
sprintf_P(answer, PSTR("%s..N%02X$"),myId, val);
addToTxQueue(answer);
addToRCMTxQueue(answer);
}
void answerOutputs(){
uint8_t val = PINJ >> 3 ;
char answer[9];
sprintf_P(answer, PSTR("%s..O%02X$"),myId, val);
addToTxQueue(answer);
addToRCMTxQueue(answer);
}
void answerValidCommand(){

View File

@@ -9,7 +9,7 @@
#define EEPROM_H_
#include <stdbool.h>
//#define EEPROMDEBUG
#define EEPROMDEBUG
typedef struct{
char outputL_H1[9]; //0x0104

View File

@@ -22,6 +22,7 @@
#include "GPIO.h"
#include "Eeprom.h"
#include "AnswerMessages.h"
#include "RCM.h"
void readInputs(){
@@ -35,11 +36,11 @@ void readInputs(){
//Rising edge L->H
if (qCodesInput[i].inputL_H1[0] != '\0')
{
addToTxQueue(qCodesInput[i].inputL_H1);
addToRCMTxQueue(qCodesInput[i].inputL_H1);
}
if (qCodesInput[i].inputL_H2[0] != '\0')
{
addToTxQueue(qCodesInput[i].inputL_H2);
addToRCMTxQueue(qCodesInput[i].inputL_H2);
}
//if (myType == 'U')
//{
@@ -66,11 +67,11 @@ void readInputs(){
//Falling edge H->L
if (qCodesInput[i].inputH_L1[0] != '\0')
{
addToTxQueue(qCodesInput[i].inputH_L1);
addToRCMTxQueue(qCodesInput[i].inputH_L1);
}
if (qCodesInput[i].inputH_L2[0] != '\0')
{
addToTxQueue(qCodesInput[i].inputH_L2);
addToRCMTxQueue(qCodesInput[i].inputH_L2);
}
//if (myType == 'U')
//{

38
UCS_H1401/Intercom.c Normal file
View File

@@ -0,0 +1,38 @@
/*
* 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>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "Global.h"
#include "usart.h"
#include "GPIO.h"
#include "Eeprom.h"
#include "AnswerMessages.h"
void vIntercomTaskFunction(void *pvParameters)
{
(void)pvParameters; //Silence warning
for( ;; )
{
//readInputs();
//vTaskDelay(100 / portTICK_PERIOD_MS);
taskYIELD();
}
vTaskDelete( NULL );
}

16
UCS_H1401/Intercom.h Normal file
View File

@@ -0,0 +1,16 @@
/*
* Intercom.h
*
* Created: 3-12-2021 13:27:11
* Author: MS
*/
#ifndef INTERCOM_H_
#define INTERCOM_H_
void vIntercomTaskFunction(void *pvParameters);
#endif /* INTERCOM_H_ */

140
UCS_H1401/RCM.c Normal file
View File

@@ -0,0 +1,140 @@
/*
* RCM.c
*
* Created: 3-12-2021 13:33:10
* 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"
#include "queue.h"
/* Program headers. */
#include "FreeRTOSConfig.h"
#include "usart.h"
//#include "GPIO.h"
#include "RCM.h"
QueueHandle_t rcmRxQueue = NULL;
QueueHandle_t rcmTxQueue = NULL;
// ---------------- Messages Queue ---------------------------
void addToRCMTxQueue(const char *str)
{
if( rcmTxQueue != NULL )
{
if( xQueueSend( rcmTxQueue, (void *)str, (TickType_t) 10 ) != pdPASS )
{
#ifdef USARTDEBUG
printf_P(PSTR("rcmTxQueue post failed - %s\r\n"), str);
#endif //USARTDEBUG
}
}
}
void addToRCMRxQueue(const char *str)
{
if (rcmRxQueue != NULL)
{
if (xQueueSend(rcmRxQueue, str, 0 ) == pdFALSE)
{
#ifdef USARTDEBUG
printf_P(PSTR("rcmRxQueue send failed\r\n"));
#endif //USARTDEBUG
}
}
}
void getFromRCMRxQueue(char *buf)
{
if( rcmRxQueue != NULL )
{
xQueueReceive(rcmRxQueue, buf, portMAX_DELAY);
}
}
// ---------------- RCM Task ---------------------------
void vRCMTask(void *pvParameters)
{
(void) pvParameters; //Remove warning about unused parameters.
char txData[13];
char rxData[13];
int rxIndex = 0;
uint16_t rxTimeout = 0;
for(;;)
{
if(rcmTxQueue != NULL)
{
if(xQueueReceive(rcmTxQueue, txData, 0) == pdPASS)
{
usart1_putstr(txData);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
taskYIELD();
if (RCMDataInReceiveBuffer())
{
uint8_t cChar = RCMReceiveByte();
#ifdef USARTDEBUG
printf_P(PSTR("RCM byte : %c - index : %u\r\n"), cChar, rxIndex);
#endif //USARTDEBUG
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
addToRCMRxQueue(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 createRCMBuffers()
{
rcmRxQueue = xQueueCreate(RCMRXQUEUESIZE, sizeof(char[13]));
if( rcmRxQueue == NULL )
{
printf_P(PSTR("rcmRxQueue creation failed\r\n"));
}
rcmTxQueue = xQueueCreate(RCMTXQUEUESIZE, sizeof(char[13]));
if( rcmRxQueue == NULL )
{
printf_P(PSTR("rcmTxQueue creation failed\r\n"));
}
}

25
UCS_H1401/RCM.h Normal file
View File

@@ -0,0 +1,25 @@
/*
* RCM.h
*
* Created: 3-12-2021 13:33:20
* Author: MS
*/
#ifndef RCM_H_
#define RCM_H_
#define RCMRXQUEUESIZE 16
#define RCMTXQUEUESIZE 16
#define SERIALPORTQUEUESIZE 24
void addToRCMTxQueue(const char *str);
void addToRCMRxQueue(const char *str);
void getFromRCMRxQueue(char *buf);
void vRCMTask(void *pvParameters);
void createRCMBuffers();
#endif /* RCM_H_ */

View File

@@ -23,6 +23,7 @@
#include "usart.h"
#include "Eeprom.h"
#include "AnswerMessages.h"
#include "RCM.h"
// Q0..A00$
@@ -166,7 +167,6 @@ void ucsCommandShort(char *data){
printf_P(PSTR("Reset\r\n"));
#endif // UCSCOMMANDSDEBUG
taskENTER_CRITICAL(); //Disable global interrupts
//wdt_enable(WDTO_15MS); //15ms in watchdog timer
while(1); //loop
break;
@@ -199,7 +199,7 @@ void ucsCommandLong(char *data){
unsigned long loc = strtoul(&data[7], NULL, 16);
char answer[9];
sprintf_P(answer, PSTR("%s..P%02X$"),myId, readEepromByte(loc));
addToTxQueue(answer);
addToRCMTxQueue(answer);
//_delay_ms(25);
//char answerlong[12];
@@ -220,7 +220,7 @@ void ucsCommandLong(char *data){
char answer[9];
sprintf_P(answer, PSTR("%s..P%02X$"),myId, readEepromByte(loc));
addToTxQueue(answer);
addToRCMTxQueue(answer);
}
}
else{
@@ -245,7 +245,7 @@ void vUCSTaskFunction(void *pvParameters)
for( ;; )
{
getFromRxQueue(rcmBuffer);
getFromRCMRxQueue(rcmBuffer);
#ifdef UCSCOMMANDSDEBUG
printf_P(PSTR("UCS buffer = %s\r\n"), rcmBuffer);

View File

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

View File

@@ -256,9 +256,21 @@
<Compile Include="Inputs.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="Intercom.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="Intercom.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="RCM.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="RCM.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="UCSCommands.c">
<SubType>compile</SubType>
</Compile>

View File

@@ -25,6 +25,8 @@
#include "Inputs.h"
#include "AnswerMessages.h"
#include "UCSCommands.h"
#include "Intercom.h"
#include "RCM.h"
static FILE serialConsole = FDEV_SETUP_STREAM(usart0_putchar_printf,NULL,_FDEV_SETUP_WRITE);
@@ -90,6 +92,7 @@ TaskHandle_t runLedTaskHandle;
TaskHandle_t rcmTaskHandle;
TaskHandle_t inputTaskHandle;
TaskHandle_t ucsTaskHandle;
TaskHandle_t intercomTaskHandle;
int main(void)
{
@@ -129,11 +132,8 @@ int main(void)
answerOutputs();
xTaskCreate( vUCSTaskFunction, "UCS", configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY), &ucsTaskHandle);
//if (myType == 'G')
//{
//initGateControl();
//xTaskCreate( vGateTask, "Gate", configMINIMAL_STACK_SIZE*12, NULL, (tskIDLE_PRIORITY), &gateControlTaskHandle);
//}
//initUsarts();
//xTaskCreate( vIntercomTaskFunction, "intercom", configMINIMAL_STACK_SIZE*4, NULL, (tskIDLE_PRIORITY), &intercomTaskHandle);
wdt_enable(WDTO_1S);
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG

View File

@@ -23,45 +23,6 @@
#include "usart.h"
#include "GPIO.h"
QueueHandle_t rcmRxQueue = NULL;
QueueHandle_t rcmTxQueue = NULL;
// ---------------- Messages Queue ---------------------------
void addToTxQueue(const char *str)
{
if( rcmTxQueue != NULL )
{
if( xQueueSend( rcmTxQueue, (void *)str, (TickType_t) 10 ) != pdPASS )
{
#ifdef USARTDEBUG
printf_P(PSTR("rcmTxQueue post failed - %s\r\n"), str);
#endif //USARTDEBUG
}
}
}
void addToRxQueue(const char *str)
{
if (rcmRxQueue != NULL)
{
if (xQueueSend(rcmRxQueue, str, 0 ) == pdFALSE)
{
#ifdef USARTDEBUG
printf_P(PSTR("rcmRxQueue send failed\r\n"));
#endif //USARTDEBUG
}
}
}
void getFromRxQueue(char *buf)
{
if( rcmRxQueue != NULL )
{
xQueueReceive(rcmRxQueue, buf, portMAX_DELAY);
}
}
// ---------------- USART 0 Serial Port ---------------------------
void usart0_putchar(unsigned char data){
LEDON(LED_BUSY);
@@ -115,11 +76,59 @@ void usart1_putStr_P(const char *data){
usart1_putchar(pgm_read_byte(data++));
}
//**************** Interrupts ******************************************
static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
static volatile unsigned char UART_RxHead;
static volatile unsigned char UART_RxTail;
//------------ USART 2 --------------------------------
void usart2_putchar(unsigned char data){
LEDON(LED_BUSY);
while (!(UCSR2A & (1 << UDRE2)));
UDR2 = data;
LEDOFF(LED_BUSY);
}
int usart2_putchar_printf(char var, FILE *stream){
(void)stream; //Silence warning
if (var == '\n') usart2_putchar('\r');
usart2_putchar(var);
return 0;
}
//String verzenden
void usart2_putstr(const char *str){
while (*str){
usart2_putchar(*str);
str++;
}
}
// PGMSPACE output
void usart2_putStr_P(const char *data){
while (pgm_read_byte(data) != 0x00)
usart2_putchar(pgm_read_byte(data++));
}
//------------ USART 3 --------------------------------
void usart3_putchar(unsigned char data){
LEDON(LED_BUSY);
while (!(UCSR3A & (1 << UDRE3)));
UDR3 = data;
LEDOFF(LED_BUSY);
}
int usart3_putchar_printf(char var, FILE *stream){
(void)stream; //Silence warning
if (var == '\n') usart3_putchar('\r');
usart3_putchar(var);
return 0;
}
//String verzenden
void usart3_putstr(const char *str){
while (*str){
usart3_putchar(*str);
str++;
}
}
// PGMSPACE output
void usart3_putStr_P(const char *data){
while (pgm_read_byte(data) != 0x00)
usart3_putchar(pgm_read_byte(data++));
}
//**************** Interrupts ******************************************
ISR(USART0_RX_vect) //Serial Port
{
LEDON(LED_BUSY);
@@ -131,10 +140,14 @@ ISR(USART0_RX_vect) //Serial Port
LEDOFF(LED_BUSY);
}
//-------- USART 1 RCM ---------------
static unsigned char USART1_RxBuf[UART1_RX_BUFFER_SIZE];
static volatile unsigned char USART1_RxHead;
static volatile unsigned char USART1_RxTail;
ISR(USART1_RX_vect) //RCM
{
LEDON(LED_BUSY);
LEDON(LED_BUSY);
//#ifdef USARTDEBUG //to slow when enabled
//if (! (UCSR0A & (1<<FE0))){
//printf_P(PSTR("USART0 frame error\r\n"));
@@ -147,95 +160,58 @@ ISR(USART1_RX_vect) //RCM
//}
//#endif //USARTDEBUG
uint8_t tmphead = (UART_RxHead + 1) & UART_RX_BUFFER_MASK;
UART_RxHead = tmphead; /* store new index */
uint8_t tmphead = (USART1_RxHead + 1) & UART1_RX_BUFFER_MASK;
USART1_RxHead = tmphead; /* store new index */
#ifdef USARTDEBUG
if (tmphead == UART_RxTail)
if (tmphead == USART1_RxTail)
{
printf_P(PSTR("USART1 Receive buffer overflow\r\n"));
}
#endif //USARTDEBUG
USART1_RxBuf[tmphead] = UDR1;
LEDOFF(LED_BUSY);
}
UART_RxBuf[tmphead] = UDR1;
unsigned char RCMReceiveByte(void)
{
unsigned char tmptail;
tmptail = ( USART1_RxTail + 1 ) & UART1_RX_BUFFER_MASK;/* calculate buffer index */
USART1_RxTail = tmptail; /* store new index */
return USART1_RxBuf[tmptail]; /* return data */
}
bool RCMDataInReceiveBuffer(void)
{
return (USART1_RxHead != USART1_RxTail);
}
//-------- USART 2 intercom ---------------
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;
//}
LEDOFF(LED_BUSY);
}
//-------- USART 3 intercom ---------------
ISR(USART3_RX_vect) //USART 3
{
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 = UDR3;
(void)cChar;
//}
LEDOFF(LED_BUSY);
}
unsigned char ReceiveByte(void)
{
unsigned char tmptail;
tmptail = ( UART_RxTail + 1 ) & UART_RX_BUFFER_MASK;/* calculate buffer index */
UART_RxTail = tmptail; /* store new index */
return UART_RxBuf[tmptail]; /* return data */
}
bool DataInReceiveBuffer(void)
{
return (UART_RxHead != UART_RxTail);
}
//-----------------------------------------------------------------------------------------------------------
void vRCMTask(void *pvParameters)
{
(void) pvParameters; //Remove warning about unused parameters.
char txData[13];
char rxData[13];
int rxIndex = 0;
uint16_t rxTimeout = 0;
for(;;)
{
if(rcmTxQueue != NULL)
{
if(xQueueReceive(rcmTxQueue, txData, 0) == pdPASS)
{
usart1_putstr(txData);
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
taskYIELD();
if (DataInReceiveBuffer())
{
uint8_t cChar = ReceiveByte();
#ifdef USARTDEBUG
printf_P(PSTR("RCM byte : %c - index : %u\r\n"), cChar, rxIndex);
#endif //USARTDEBUG
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
addToRxQueue(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 initDebugUsart()
{
// UART 0
@@ -253,22 +229,21 @@ void initRCMUsart()
UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11); // Use 8-bit character sizes
UCSR1B |= (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1); // Turn on the transmission, reception, and Receive interrupt
UART_RxTail = 0;
UART_RxHead = 0;
USART1_RxTail = 0;
USART1_RxHead = 0;
}
void createRCMBuffers()
void initUsarts()
{
rcmRxQueue = xQueueCreate(RCMRXQUEUESIZE, sizeof(char[13]));
if( rcmRxQueue == NULL )
{
printf_P(PSTR("rcmRxQueue creation failed\r\n"));
}
rcmTxQueue = xQueueCreate(RCMTXQUEUESIZE, sizeof(char[13]));
if( rcmRxQueue == NULL )
{
printf_P(PSTR("rcmTxQueue creation failed\r\n"));
}
}
// UART 2
UBRR2H = (USART2_BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UBRR2L = USART2_BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UCSR2C |= (1 << UCSZ20) | (1 << UCSZ21); // Set frame format to 8 data bits, no parity, 1 stop bit
UCSR2B |= (1 << RXEN2) | (1 << TXEN2) | (1 << RXCIE2); // Turn on the transmission, reception, and Receive interrupt
// UART 3
UBRR3H = (USART3_BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UBRR3L = USART3_BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UCSR3C |= (1 << UCSZ30) | (1 << UCSZ31); // Set frame format to 8 data bits, no parity, 1 stop bit
UCSR3B |= (1 << RXEN3) | (1 << TXEN3) | (1 << RXCIE3); // Turn on the transmission, reception, and Receive interrupt
}

View File

@@ -9,8 +9,9 @@
#ifndef USART_H_
#define USART_H_
//#define USARTDEBUG
#include <stdbool.h>
#define USARTDEBUG
//Serial port
//#define USART0_BAUDRATE 9600 //0.2% error
@@ -27,27 +28,31 @@
#define USART1_BAUDRATE 76800 //0.2% error
#define USART1_BAUD_PRESCALE (((F_CPU / (USART1_BAUDRATE * 16UL))) - 1)
#define RCMRXQUEUESIZE 16
#define RCMTXQUEUESIZE 16
#define SERIALPORTQUEUESIZE 24
#define UART_RX_BUFFER_SIZE 32
#define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 )
#if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK )
#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
#endif
void addToTxQueue(const char *str);
void addToRxQueue(const char *str);
void getFromRxQueue(char *buf);
//USART 2
#define USART2_BAUDRATE 19200 //0.2% error
#define USART2_BAUD_PRESCALE (((F_CPU / (USART2_BAUDRATE * 16UL))) - 1)
//USART 3
#define USART3_BAUDRATE 19200 //0.2% error
#define USART3_BAUD_PRESCALE (((F_CPU / (USART3_BAUDRATE * 16UL))) - 1)
int usart0_putchar_printf(char var, FILE *stream);
int usart1_putchar_printf(char var, FILE *stream);
void usart1_putstr(const char *str);
void vRCMTask(void *pvParameters);
unsigned char RCMReceiveByte(void);
bool RCMDataInReceiveBuffer(void);
void initDebugUsart();
void initRCMUsart();
void createRCMBuffers();
void initUsarts();
#endif /* USART_H_ */