Skip to content

Commit

Permalink
DMA TX
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Sep 26, 2023
1 parent 7d638de commit 3ef4d63
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 275 deletions.
64 changes: 15 additions & 49 deletions TFT/src/User/API/RRFSendCmd.c
Original file line number Diff line number Diff line change
@@ -1,58 +1,24 @@
#include "RRFSendCmd.h"
#include "Serial.h"
#include <stdio.h>
#include "includes.h"

static uint8_t n_sent = 0;
static uint32_t line_number = 0;
static uint8_t checksum = 0;

void sendCharAndChecksum(const char c)
void rrfSendCmd(const char * cmd_ptr)
{
checksum ^= c;
Serial_PutChar(SERIAL_PORT, c);
n_sent++;
}
char rrfCmd[CMD_MAX_SIZE];
char * rrfCmd_ptr = rrfCmd;
uint8_t checksum = 0;

void sendChar(const char c)
{
if (c == '\n')
{
if (n_sent != 0)
{
Serial_PutChar(SERIAL_PORT, '*');
char digit0 = checksum % 10 + '0';
checksum /= 10;
char digit1 = checksum % 10 + '0';
checksum /= 10;
if (checksum != 0)
{
Serial_PutChar(SERIAL_PORT, checksum + '0');
}
Serial_PutChar(SERIAL_PORT, digit1);
Serial_PutChar(SERIAL_PORT, digit0);
}
Serial_PutChar(SERIAL_PORT, c);
n_sent = 0;
}
else
{
if (n_sent == 0)
{
char number[11];
checksum = 0;
sendCharAndChecksum('N');
snprintf(number, 11, "%lu", line_number++);
rrfSendCmd(number);
sendCharAndChecksum(' ');
}
sendCharAndChecksum(c);
}
}
sprintf(rrfCmd, "N%lu %s", line_number++, cmd_ptr);

void rrfSendCmd(const char* cmd_ptr)
{
while (*cmd_ptr != 0)
// calculate checksum
while (*rrfCmd_ptr != '\n')
{
sendChar(*cmd_ptr++);
checksum ^= *rrfCmd_ptr++;
}

// add checksum and finalize formatting the RRF command
sprintf(rrfCmd_ptr, "*%u\n", checksum);

// send the command to the serial port
Serial_Put(SERIAL_PORT, rrfCmd);
}
33 changes: 19 additions & 14 deletions TFT/src/User/API/SerialConnection.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#include "SerialConnection.h"
#include "includes.h"

#define SERIAL_PORT_QUEUE_SIZE NOBEYOND(512, RAM_SIZE * 64, 4096)
#define SERIAL_PORT_2_QUEUE_SIZE 512
#define SERIAL_PORT_3_QUEUE_SIZE 512
#define SERIAL_PORT_4_QUEUE_SIZE 512
#define SERIAL_PORT_RX_QUEUE_SIZE NOBEYOND(512, RAM_SIZE * 64, 4096)
#define SERIAL_PORT_2_RX_QUEUE_SIZE 512
#define SERIAL_PORT_3_RX_QUEUE_SIZE 512
#define SERIAL_PORT_4_RX_QUEUE_SIZE 512

#define SERIAL_PORT_TX_QUEUE_SIZE CMD_MAX_SIZE
#define SERIAL_PORT_2_TX_QUEUE_SIZE CMD_MAX_SIZE
#define SERIAL_PORT_3_TX_QUEUE_SIZE CMD_MAX_SIZE
#define SERIAL_PORT_4_TX_QUEUE_SIZE CMD_MAX_SIZE

const SERIAL_PORT_INFO serialPort[SERIAL_PORT_COUNT] = {
{SERIAL_PORT, SERIAL_PORT_QUEUE_SIZE, "", "1 - Printer"},
{SERIAL_PORT , SERIAL_PORT_RX_QUEUE_SIZE , SERIAL_PORT_TX_QUEUE_SIZE , "" , "1 - Printer"},
#ifdef SERIAL_PORT_2
{SERIAL_PORT_2, SERIAL_PORT_2_QUEUE_SIZE, "2", "2 - WIFI"},
{SERIAL_PORT_2, SERIAL_PORT_2_RX_QUEUE_SIZE, SERIAL_PORT_2_TX_QUEUE_SIZE, "2", "2 - WiFi"},
#endif
#ifdef SERIAL_PORT_3
{SERIAL_PORT_3, SERIAL_PORT_3_QUEUE_SIZE, "3", "3 - UART3"},
{SERIAL_PORT_3, SERIAL_PORT_3_RX_QUEUE_SIZE, SERIAL_PORT_3_TX_QUEUE_SIZE, "3", "3 - UART3"},
#endif
#ifdef SERIAL_PORT_4
{SERIAL_PORT_4, SERIAL_PORT_4_QUEUE_SIZE, "4", "4 - UART4"}
{SERIAL_PORT_4, SERIAL_PORT_4_RX_QUEUE_SIZE, SERIAL_PORT_4_TX_QUEUE_SIZE, "4", "4 - UART4"}
#endif
};

Expand All @@ -26,7 +31,7 @@ static inline void Serial_InitPrimary(void)
{
InfoHost_Init(false); // initialize infoHost when disconnected

Serial_Config(serialPort[PORT_1].port, serialPort[PORT_1].cacheSize, baudrateValues[infoSettings.serial_port[PORT_1]]);
Serial_Config(serialPort[PORT_1].port, serialPort[PORT_1].cacheSizeRX, serialPort[PORT_1].cacheSizeTX, baudrateValues[infoSettings.serial_port[PORT_1]]);
}

static inline void Serial_DeInitPrimary(void)
Expand All @@ -53,7 +58,7 @@ void Serial_Init(SERIAL_PORT_INDEX portIndex)
// Disable the serial port when it is not in use and/or not connected to a device (floating) to
// avoid to receive and process wrong data due to possible electromagnetic interference (EMI).
if (infoSettings.serial_port[portIndex] > 0) // if serial port is enabled
Serial_Config(serialPort[portIndex].port, serialPort[portIndex].cacheSize,
Serial_Config(serialPort[portIndex].port, serialPort[portIndex].cacheSizeRX, serialPort[portIndex].cacheSizeTX,
baudrateValues[infoSettings.serial_port[portIndex]]);
}
}
Expand All @@ -64,7 +69,7 @@ void Serial_Init(SERIAL_PORT_INDEX portIndex)
{
if (infoSettings.serial_port[portIndex] > 0) // if serial port is enabled
{
Serial_Config(serialPort[portIndex].port, serialPort[portIndex].cacheSize,
Serial_Config(serialPort[portIndex].port, serialPort[portIndex].cacheSizeRX, serialPort[portIndex].cacheSizeTX,
baudrateValues[infoSettings.serial_port[portIndex]]);
}
}
Expand Down Expand Up @@ -138,16 +143,16 @@ uint16_t Serial_GetReadingIndex(SERIAL_PORT_INDEX portIndex)
if (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1))
return 0;

return dmaL1Data[portIndex].rIndex;
return dmaL1DataRX[portIndex].rIndex;
}

uint16_t Serial_Get(SERIAL_PORT_INDEX portIndex, char * buf, uint16_t bufSize)
{
// if port index is out of range or no data to read from L1 cache
if (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1) || dmaL1Data[portIndex].flag == dmaL1Data[portIndex].wIndex)
if (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1) || dmaL1DataRX[portIndex].flag == dmaL1DataRX[portIndex].wIndex)
return 0;

DMA_CIRCULAR_BUFFER * dmaL1Data_ptr = &dmaL1Data[portIndex];
DMA_CIRCULAR_BUFFER * dmaL1Data_ptr = &dmaL1DataRX[portIndex];

// make a static access to dynamically changed (by L1 cache's interrupt handler) variables/attributes
uint16_t wIndex = dmaL1Data_ptr->wIndex;
Expand Down
3 changes: 2 additions & 1 deletion TFT/src/User/API/SerialConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ typedef enum
typedef struct
{
uint8_t port; // physical port (e.g. _USART1) related to serial port (e.g. 0 for SERIAL_PORT, 1 for SERIAL_PORT_2 etc...)
uint16_t cacheSize; // queue size for sending/receiving data to/from the port
uint16_t cacheSizeRX; // buffer size for receiving data from the serial port
uint16_t cacheSizeTX; // buffer size for sending data to the serial port
const char * const id; // serial port ID (e.g. "" for SERIAL_PORT, "2" for SERIAL_PORT_2 etc...)
const char * const desc; // serial port description (e.g. "1 - Printer" for SERIAL_PORT, "2 - WIFI" for SERIAL_PORT_2 etc...)
} SERIAL_PORT_INFO; // serial port info
Expand Down
Loading

0 comments on commit 3ef4d63

Please sign in to comment.