PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
UARTRX_Demo.cpp

Write "Hello world!" out via UART protocol and receive an echo

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(UARTRX_Demo)
create_simple_executable(${PROJECT_NAME} UARTRX_Demo.cpp)
// Create the test string - useful when testing with a terminal
static const char TEST_STRING[] = "Hello, world!\n";
static const uint32_t BAUD_RATE = 115200;
static const Port::Mask TX_PIN = Port::Mask::P12;
static const Port::Mask RX_PIN = Port::Mask::P13;
static const UART::Parity PARITY = UART::Parity::NO_PARITY;
static void error (const PropWare::ErrorCode err);
class Listener : public Runnable {
public:
template<size_t N>
Listener (const uint32_t (&stack)[N])
: Runnable(stack) {
}
void run () {
PropWare::ErrorCode err;
int32_t receivedLength;
this->init();
pwSyncOut.printf("Ready to receive!\n");
while (1) {
receivedLength = sizeof(this->m_buffer);
if ((err = this->m_listener.fgets(this->m_buffer, &receivedLength)))
error(err);
pwSyncOut.printf("Data (%d chars): \"%s\"\n", receivedLength, this->m_buffer);
}
}
void init () {
this->m_listener.set_rx_mask(RX_PIN);
this->m_listener.set_baud_rate(BAUD_RATE);
this->m_listener.set_parity(PARITY);
// A very short wait to ensure the main cog has finished printing its "I'm ready" statement before we start
// printing ours
waitcnt(20 * MILLISECOND + CNT);
}
private:
UARTRX m_listener;
char m_buffer[sizeof(TEST_STRING)];
};
int main () {
uint32_t threadStack[256];
Listener listener(threadStack);
UARTTX speaker(TX_PIN);
// Start our new cog and initialize the speaking UART
speaker.set_baud_rate(BAUD_RATE);
speaker.set_parity(PARITY);
pwSyncOut.printf("New cog ID: %d. Ready to send!!!\n", Runnable::invoke(listener));
while (1) {
waitcnt(200 * MILLISECOND + CNT);
speaker.puts(TEST_STRING);
}
}
void error (const PropWare::ErrorCode err) {
SimplePort debugLEDs(Port::P16, 8, Pin::Dir::OUT);
pwSyncOut.printf("Unknown error: %u\n", err);
while (1) {
debugLEDs.write((uint32_t) err);
waitcnt(100 * MILLISECOND);
debugLEDs.write(0);
waitcnt(100 * MILLISECOND);
}
}
runnable.h
pwSyncOut
const PropWare::SynchronousPrinter pwSyncOut
Global and shared instance for easy printing to the terminal (thread safe)
PropWare::UART
Abstract base class for all unbuffered UART devices.
Definition: uart.h:86
uartrx.h
Listener::run
void run()
Invoked in the new cog, this method should be the root of the business logic.
Definition: UARTRX_Demo.cpp:57
synchronousprinter.h
uarttx.h
PropWare::UARTRX
Receive routines for basic UART communication.
Definition: uartrx.h:41
PropWare::UARTTX
Definition: uarttx.h:38
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::SynchronousPrinter::printf
void printf(const char fmt[]) const
Definition: synchronousprinter.h:139
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
simpleport.h
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
PropWare::Runnable
Helper class for creating easy parallel applications.
Definition: runnable.h:75
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
PropWare::SimplePort
The PropWare::SimplePort is the recommended way to use data ports on the Propeller....
Definition: simpleport.h:36
Listener
Definition: UARTRX_Demo.cpp:50
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33