Anyone tried to call those functions from a task, but I get a thread crashing… I can call them in main() without any issues. Any ideas and code examples are appreciated.
uart_poll_out() and uart_poll_in () -- calling from a thread?
Depends on which kind of thread. If you’re doing it from one you setup using K_THREAD_DEFINE
make sure you have enough stack space. The fault report will typically indicate a stack overflow which should be your first clue.
Also be careful calling functions, especially system ones, from timer callbacks and GPIO pin interrupt callbacks.
- Edited
Thank you Jared! I tried to increase the stack size, but I will get a crash. The call is made from a msg_controller().
static void **msg_controller** (const struct device *local_dev_tty2)
{
//tty2 is not used (remove)
struct socketcan_frame from_can_tx;
struct socketcan_frame from_can_rx;
while (true ) {
k_sleep(K_SECONDS(1));
...
uart_poll_out(local_dev_tty2, 'A'); // <<------- causes a BUS FAULT
k_sleep(K_SECONDS(1));
...
k_msgq_get(&incoming_msgq, &from_can_rx, K_NO_WAIT); <---- this call does not cause a crash
...
k_sleep(K_SECONDS(1));
}
}
The thread was created this way:
msg_ctrl_tid = k_thread_create(&msg_ctrl_data, msg_ctrl_stack,
K_THREAD_STACK_SIZEOF(msg_ctrl_stack)+10,
(k_thread_entry_t)**msg_controller,** NULL,
NULL, NULL, PRIORITY, 0, K_NO_WAIT); // tty2 is for UART to get
00:00:04.007,000] <err> os: ***** BUS FAULT *****
[00:00:04.007,000] <err> os: Precise data bus error
[00:00:04.007,000] <err> os: BFAR Address: 0x2feecd12
[00:00:04.008,000] <err> os: r0/a1: 0x00000000 r1/a2: 0x6000c071 r2/a3: 0x00000000
[00:00:04.008,000] <err> os: r3/a4: 0x2feecc96 r12/ip: 0x0000c350 r14/lr: 0x6000c027
[00:00:04.008,000] <err> os: xpsr: 0x61000000
[00:00:04.008,000] <err> os: Faulting instruction address (r15/pc): 0x600078d2
[00:00:04.008,000] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:04.008,000] <err> os: Current thread: 0x80000300 (unknown)
[00:00:04.009,000] <err> os: Halting system
I’m not sure if you’re passing the correct UART device (if at all). Is it initialized and setup somewhere?
The three NULL values in the create
functions are pointers to any arbitrary data/devices that you’d like that thread to use. And typically thread functions have 3 void arguments not one device. (you may be looking at the usage of SYS_INIT which does provide a device as an argument)
jaredwolff You are absolutely right. I have created a thread with the function uart_thread() which was untested. The parameter should have been tty, not &tty. Again, thank you for your help and support. Your blog and your videos are priceless.
msg_uart_tid = k_thread_create(&msg_uart_data, msg_uart_stack,
K_THREAD_STACK_SIZEOF(msg_uart_stack),
(k_thread_entry_t)uart_thread, **&tty2**,
NULL, NULL, PRIORITY, 0, K_NO_WAIT);