Trying to play with Thread.
I have a F303K8 that send by Serial
a char
that is at 0 or 1.
On the other side I have a F411RE that receive the Serial to get the 1 or 0 with interrupt. It display it at the screen.
This part of the code works well.
Now I want to introduce thread to have my interrupt function that fullfill a queue with 1 or 0 and a second thread that get informations in the queue to know if the led have to be ON (1) of OFF (0).
But it does not works. The led never change of state but I can display at my screen that I receive 1 or 0.
It worked well when I used a standard function with a basic loop that increment a i
and I sent i % 2
from a thread to another, but over Serial and in Interruption function it seems to not works.
Here is the code of my receiver and led blinker:
#include "mbed.h"
#include "rtos.h"
typedef struct frame_s {
int std_led;
} frame_t;
DigitalOut std_led(LED1);
DigitalOut red_led(D9);
DigitalOut yellow_led(D12);
MemoryPool<frame_t, 16> mpool;
Queue<frame_t, 16> queue;
Serial uart(D10, D3);
Serial pc(USBTX, USBRX);
void getDatas(void const *args) {
yellow_led = 1;
while (42) {
// yellow_led = !yellow_led;
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
frame_t *frame = (frame_t *)evt.value.p;
std_led = frame->std_led;
mpool.free(frame);
}
}
}
void uartRxInterruption(void) {
char c = uart.getc();
red_led = !red_led;
frame_t *frame = mpool.alloc();
frame->std_led = (int)c;
queue.put(frame);
pc.putc(c + 48);
}
int main(void) {
Thread data_generator(getDatas);
uart.attach(&uartRxInterruption);
uart.baud(9600);
pc.baud(115200);
return 0;
}
and here is my Serial
generator: (I’m not sure you need that but just in case)
#include "mbed.h"
Serial uart(D13, D12);
int main(void) {
int i = 0;
while(42) {
i += 1;
uart.putc(i % 2);
wait_ms(500);
}
return 0;
}
And here is a code that works great with a fake generation signal (a function that loop based on a i % 2
)
#include "mbed.h"
#include "rtos.h"
typedef struct frame_s {
int std_led;
} frame_t;
DigitalOut std_led(LED1);
DigitalOut red_led(D9);
DigitalOut yellow_led(D12);
MemoryPool<frame_t, 16> mpool;
Queue<frame_t, 16> queue;
Serial uart(D10, D3);
void getDatas(void const *args) {
yellow_led = 1;
while (42) {
yellow_led = !yellow_led;
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
frame_t *frame = (frame_t *)evt.value.p;
std_led = frame->std_led;
mpool.free(frame);
}
}
}
void uartRxInterruption(void const *args) {
}
int main(void) {
Thread data_generator(getDatas);
// Thread uart_rx(uartRxInterruption);
// uart.attach(&uartRxInterruption);
uart.baud(9600);
int i = 0;
char c;
while(true) {
// char c = uart.getc();
i += 1;
c = i % 2;
red_led = !red_led;
frame_t *frame = mpool.alloc();
frame->std_led = (int)c;
queue.put(frame);
wait(0.5);
}
return 0;
}
And the same code, but with the generation code in another thread, it does not works anymore…
#include "mbed.h"
#include "rtos.h"
typedef struct frame_s {
int std_led;
} frame_t;
DigitalOut std_led(LED1);
DigitalOut red_led(D9);
DigitalOut yellow_led(D12);
MemoryPool<frame_t, 16> mpool;
Queue<frame_t, 16> queue;
Serial uart(D10, D3);
void getDatas(void const *args) {
yellow_led = 1;
while (42) {
yellow_led = !yellow_led;
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
frame_t *frame = (frame_t *)evt.value.p;
std_led = frame->std_led;
mpool.free(frame);
}
}
}
void uartRxInterruption(void const *args) {
int i = 0;
char c;
while(true) {
// char c = uart.getc();
i += 1;
c = i % 2;
red_led = !red_led;
frame_t *frame = mpool.alloc();
frame->std_led = (int)c;
queue.put(frame);
wait(0.5);
}
}
int main(void) {
Thread data_generator(getDatas);
Thread fake_uart_rx(uartRxInterruption);
// uart.attach(&uartRxInterruption);
uart.baud(9600);
return 0;
}
Maybe I missunderstand how to play with thread but I can’t see what’s wrong.