Skip to content

Commit

Permalink
Reworked ThreadUART macros to work more logically.
Browse files Browse the repository at this point in the history
  • Loading branch information
aclowmclaughlin committed Nov 23, 2024
1 parent d7c6de8 commit f4ef833
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
7 changes: 7 additions & 0 deletions include/core/rtos/Threadx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
#include <core/rtos/Initializable.hpp>
#include <cstdint>

/**
* Converts seconds to rtos ticks
*/
#define S_TO_TICKS(n) ((uint32_t) (n) *TX_TIMER_TICKS_PER_SECOND)

/**
* Converts milliseconds to rtos ticks
*/
#define MS_TO_TICKS(n) ((uint32_t) (n) *TX_TIMER_TICKS_PER_SECOND / 1000u)

namespace core::rtos {
Expand Down
34 changes: 20 additions & 14 deletions include/core/rtos/tsio/ThreadUART.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
#include <core/rtos/Threadx.hpp>
#include <string>

// All defines wrapped in ifndefs so we can set them externally.

#ifndef THREADUART_QUEUE_MESSAGE_SIZE
//Defines the size for a ThreadUART Queue message (in 4 byte words).
//This CANNOT be bigger than 16.
//Wrapped in an ifdef so this can be defined externally
#ifdef THREADUART_QUEUE_MESSAGE_SIZE
#if THREADUART_QUEUE_MESSAGE_SIZE > 16
#error THREADUART_QUEUE_MESSAGE_SIZE must be less than 16.
#endif
#else
#define THREADUART_QUEUE_MESSAGE_SIZE 16
#endif // THREADUART_QUEUE_MESSAGE_SIZE

//Defines the number of message in the ThreadUART Queue.
//Wrapped in an ifndef so this can be defined externally
#ifndef THREADUART_QUEUE_NUM_MESSAGES
#define THREADUART_QUEUE_NUM_MESSAGES 32
#endif // THREADUART_QUEUE_NUM_MESSAGES

#ifndef THREADUART_DEFAULT_STACK_SIZE
#define THREADUART_DEFAULT_STACK_SIZE 1024
#endif // THREADUART_DEFAULT_STACK_SIZE
#define THREADUART_DEFAULT_STACK_SIZE 1024

#ifndef THREADUART_DEFAULT_PRIORITY_LEVEL
#define THREADUART_DEFAULT_PRIORITY_LEVEL 1u
#endif // THREADUART_DEFAULT_PRIORITY_LEVEL
#define THREADUART_DEFAULT_PRIORITY_LEVEL 1u

#ifndef THREADUART_DEFAULT_PREEMPT_THRESHOLD
#define THREADUART_DEFAULT_PREEMPT_THRESHOLD 0u
#endif // THREADUART_DEFAULT_PREEMPT_THRESHOLD
#define THREADUART_DEFAULT_PREEMPT_THRESHOLD 0u

#ifndef THREADUART_DEFAULT_TIME_SLICE
#define THREADUART_DEFAULT_TIME_SLICE MS_TO_TICKS(500)
#endif // THREADUART_DEFAULT_TIME_SLICE
#define THREADUART_DEFAULT_TIME_SLICE MS_TO_TICKS(500)

namespace io = core::io;
namespace core::rtos::tsio {
Expand Down Expand Up @@ -85,6 +85,12 @@ class ThreadUART : public Initializable, public io::UART {

// Inherited UART methods

/**
* Add the given formatted string to the ThreadUART Queue to eventually be sent out over UART.
* Only the first 256 characters of the result string of the formatting will be sent
*
* @param[in] format The format string to print out
*/
void printf(const char* format, ...) override;

void setBaudrate(uint32_t baudrate) override;
Expand Down
33 changes: 17 additions & 16 deletions src/core/rtos/tsio/ThreadUART.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ void ThreadUART::printf(const char* format, ...) {
void ThreadUART::puts(const char* s) {
writeMutex.get(TX_WAIT_FOREVER);
// split longer messages into 64 (63 + null-termination) bit chunks.
char temp[64];
char temp[THREADUART_QUEUE_MESSAGE_SIZE];
uint32_t len = strlen(s);
for (uint32_t i = 0; i < len; i += 63) {
memccpy(temp, s + i, '\0', 63);
temp[63] = '\0'; // set the last bit to the null terminator (should already be that but just in case)
for (uint32_t i = 0; i < len; i += THREADUART_QUEUE_MESSAGE_SIZE-1) {
memccpy(temp, s + i, '\0', THREADUART_QUEUE_MESSAGE_SIZE-1);
temp[THREADUART_QUEUE_MESSAGE_SIZE-1] = '\0'; // set the last bit to the null terminator
// (should already be that but just in case)
queue.send(temp, TXW_WAIT_FOREVER);
}
writeMutex.put();
}

void ThreadUART::putc(char c) {
writeMutex.get(TX_WAIT_FOREVER);
char temp[64];
char temp[THREADUART_QUEUE_MESSAGE_SIZE];
temp[0] = c;
temp[1] = '\0';
queue.send(temp, TXW_WAIT_FOREVER);
Expand All @@ -84,23 +85,23 @@ void ThreadUART::putc(char c) {

void ThreadUART::writeBytes(uint8_t* bytes, size_t size) {
writeMutex.get(TX_WAIT_FOREVER);
// split longer messages into 64 bit chunks.
// we send 63 bytes plus a null-terminating character, since UART is expecting a string
char temp[64];
// split longer messages into THREADUART_QUEUE_MESSAGE_SIZE byte chunks.
// we send THREADUART_QUEUE_MESSAGE_SIZE-1 bytes plus a null-terminating character, since UART is expecting a string
char temp[THREADUART_QUEUE_MESSAGE_SIZE];
size_t i = 0;
// send all the chunks except the last message (which might be less than 64 bytes)
if (size > 63) {
size_t max = size - 63;
for (i = 0; i < max; i += 63) {
memcpy(temp, bytes + i, 63);
temp[63] = '\0';
// send all the chunks except the last message (which might be less than THREADUART_QUEUE_MESSAGE_SIZE bytes)
if (size > THREADUART_QUEUE_MESSAGE_SIZE-1) {
size_t max = size - THREADUART_QUEUE_MESSAGE_SIZE-1;
for (i = 0; i < max; i += THREADUART_QUEUE_MESSAGE_SIZE-1) {
memcpy(temp, bytes + i, THREADUART_QUEUE_MESSAGE_SIZE-1);
temp[THREADUART_QUEUE_MESSAGE_SIZE-1] = '\0';
queue.send(temp, TXW_WAIT_FOREVER);
}
}
// send the last amount of bytes
size_t remaining_bytes = size - i;
if (remaining_bytes > 0) {
memset(temp, 0, 64);
memset(temp, 0, THREADUART_QUEUE_MESSAGE_SIZE);
// copy last message into temp
memcpy(temp, bytes + i, remaining_bytes);
queue.send(temp, TXW_WAIT_FOREVER);
Expand All @@ -113,7 +114,7 @@ void ThreadUART::write(uint8_t byte) {
}

void ThreadUART::sendFirstQueueMessage() {
char buffer[64]; // Buffer array to hold the message
char buffer[THREADUART_QUEUE_MESSAGE_SIZE]; // Buffer array to hold the message
queue.receive(buffer, TXW_WAIT_FOREVER); // Receives the message and assigns it to the buffer variable
copyUART.writeBytes((uint8_t*) (buffer), strlen(buffer));
}
Expand Down

0 comments on commit f4ef833

Please sign in to comment.