Skip to content

Commit

Permalink
DMA TX
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Sep 24, 2023
1 parent cd69550 commit 832e527
Show file tree
Hide file tree
Showing 11 changed files with 536 additions and 285 deletions.
2 changes: 1 addition & 1 deletion TFT/src/User/API/Printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void loopBreakToCondition(CONDITION_CALLBACK condCallback)
// from that command. Than another "M108" will be sent to unlock a next possible blocking command.
// This way enough "M108" will be sent to unlock all possible blocking command(s) (ongoing or enqueued) but not too much and
// not too fast one after another to overload/overrun the serial communication
TASK_LOOP_WHILE(condCallback(), if (Serial_Available(SERIAL_PORT) != 0) sendEmergencyCmd("M108\n"));
TASK_LOOP_WHILE(condCallback(), if (Serial_Available(SERIAL_PORT)) sendEmergencyCmd("M108\n"));

// remove any enqueued command that could come from a supplementary serial port or TFT media
// (if printing from remote host or TFT media) during the loop above
Expand Down
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);
}
38 changes: 20 additions & 18 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 @@ -28,7 +33,7 @@ static inline void Serial_InitPrimary(void)
infoHost.status = HOST_STATUS_IDLE;
setReminderMsg(LABEL_UNCONNECTED, SYS_STATUS_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 @@ -55,7 +60,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 @@ -66,7 +71,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 @@ -136,21 +141,18 @@ void Serial_Forward(SERIAL_PORT_INDEX portIndex, const char * msg)
}
}

uint16_t Serial_Available(SERIAL_PORT_INDEX portIndex)
bool Serial_Available(SERIAL_PORT_INDEX portIndex)
{
if (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1))
return 0;

return (dmaL1Data[portIndex].cacheSize + dmaL1Data[portIndex].wIndex - dmaL1Data[portIndex].rIndex) % dmaL1Data[portIndex].cacheSize;
return (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1) && dmaL1DataRX[portIndex].wIndex != 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
9 changes: 3 additions & 6 deletions 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 Expand Up @@ -62,11 +63,7 @@ void Serial_DeInit(SERIAL_PORT_INDEX portIndex);
// - specific port index: specific serial port
void Serial_Forward(SERIAL_PORT_INDEX portIndex, const char * msg);

// retrieve the number of bytes available on the provided serial port:
// - portIndex: index of serial port
//
// - return value: number of bytes available on serial port
uint16_t Serial_Available(SERIAL_PORT_INDEX portIndex);
bool Serial_Available(SERIAL_PORT_INDEX portIndex);

// retrieve a message from the provided serial port, if any:
// - portIndex: index of serial port where data are read from
Expand Down
Loading

0 comments on commit 832e527

Please sign in to comment.