Skip to content

Adds a new example: onReceiveExample.ino #9415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ SemaphoreHandle_t uart_buffer_Mutex = NULL;
// task created when onReceive() is used
void UART0_RX_CB() {
// take the mutex, waits forever until loop() finishes its processing
if (xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
if (uart_buffer_Mutex != NULL && xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
uint32_t now = millis(); // tracks timeout
while ((millis() - now) < communicationTimeout_ms) {
if (UART0.available()) {
Expand All @@ -88,7 +88,10 @@ void UART0_RX_CB() {
void setup() {
// creates a mutex object to control access to uart_buffer
uart_buffer_Mutex = xSemaphoreCreateMutex();

if(uart_buffer_Mutex == NULL) {
log_e("Error creating Mutex. Sketch will fail.");
delay(1000);
}
UART0.begin(115200);
UART0.onReceive(UART0_RX_CB); // sets the callback function
UART0.println("Send data to UART0 in order to activate the RX callback");
Expand All @@ -98,7 +101,7 @@ uint32_t counter = 0;
void loop() {
if (uart_buffer.length() > 0) {
// signals that the onReceive function shall not change uart_buffer while processing
if (xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
if (uart_buffer_Mutex != NULL && xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
// process the received data from UART0 - example, just print it beside a counter
UART0.print("[");
UART0.print(counter++);
Expand Down