CAN-Demo/README.md
2024-07-05 14:06:49 +02:00

59 lines
1.9 KiB
Markdown

# STM32 CAN Demo
This Project is a simple CAN Demo for STM32F407. It uses sta-core, rtos2-utils and TACOS.
## Explanation
In the `startup.cpp` we register threads in the `onManagerInit()` func, by calling the following:
```c++
// Register Spam Thread to only run in the first state
addThread<demo::CanSpam>({ALL_STATES}, 0x123);
// Register Receive Thread to run in states 0 and 2
addThread<demo::CanReceiver>({0,2}, 0x124);
```
The `CanSpam` thread sends a message every 1 second, while the `CanReceiver` thread receives messages and prints them to the console.
Sending is done by calling the following:
```c++
CanSysMsg msg;
msg.paylod = ...; // Set the payload
msg.header.sid = getCanID(); // Use the ID of the thread
msg.header.format = 0; // Set the format to standard id
sta::tacos::queueCanBusMsg(msg, 0); // Send the message, 0 means no timeout
```
As we can see here every task has a unique ID, which is used to identify the task. Trough this ID (which is set at construction), messages are automagically forwarded to the correct task.
Receiving is done by calling the following:
```c++
CanSysMsg msg;
if (CAN_queue_.get(&msg, osWaitForever))
{
// use msg.payload and msg.header to access the data
}
```
# Setup
The only thing one must change outside of the App repository (and ofc the including of the Libs) is starting the ALPAKA stack in Core/freertos.c:
```c++
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
extern void startALPAKA(void *);
startALPAKA(argument);
for(;;)
{
osDelay(1);
}
/* USER CODE END StartDefaultTask */
}
```
## Notes
We are using one of our own "ASEAG" modules which have a special define in the `config.hpp` to indicate the CAN bus and UART output. To replicate this for other hardware one must define `STA_STM32_USART_HANDLE` and `STA_STM32_CAN_HANDLE` in the `config.hpp` file.