mirror of
https://github.com/odriverobotics/ODrive.git
synced 2026-08-18 18:04:12 +08:00
Add stack_size vars, reduce stack usage
This commit is contained in:
@@ -11,5 +11,7 @@ extern osSemaphoreId sem_can;
|
||||
|
||||
extern osThreadId defaultTaskHandle;
|
||||
extern osThreadId usb_irq_thread;
|
||||
extern const uint32_t stack_size_usb_irq_thread;
|
||||
extern const uint32_t stack_size_default_task;
|
||||
|
||||
#endif /* __FREERTOS_H */
|
||||
@@ -89,12 +89,14 @@ osSemaphoreId sem_usb_tx;
|
||||
osSemaphoreId sem_can;
|
||||
|
||||
osThreadId usb_irq_thread;
|
||||
const uint32_t stack_size_usb_irq_thread = 1024; // Bytes
|
||||
|
||||
// Place FreeRTOS heap in core coupled memory for better performance
|
||||
__attribute__((section(".ccmram")))
|
||||
uint8_t ucHeap[configTOTAL_HEAP_SIZE];
|
||||
/* USER CODE END Variables */
|
||||
osThreadId defaultTaskHandle;
|
||||
const uint32_t stack_size_default_task = 1024; // Bytes
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN FunctionPrototypes */
|
||||
@@ -150,7 +152,7 @@ void usb_deferred_interrupt_thread(void * ctx) {
|
||||
|
||||
void init_deferred_interrupts(void) {
|
||||
// Start USB interrupt handler thread
|
||||
osThreadDef(task_usb_pump, usb_deferred_interrupt_thread, osPriorityAboveNormal, 0, 512);
|
||||
osThreadDef(task_usb_pump, usb_deferred_interrupt_thread, osPriorityAboveNormal, 0, stack_size_usb_irq_thread / sizeof(StackType_t));
|
||||
usb_irq_thread = osThreadCreate(osThread(task_usb_pump), NULL);
|
||||
}
|
||||
|
||||
@@ -206,7 +208,7 @@ void MX_FREERTOS_Init(void) {
|
||||
|
||||
/* Create the thread(s) */
|
||||
/* definition and creation of defaultTask */
|
||||
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 256);
|
||||
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, stack_size_default_task / sizeof(StackType_t));
|
||||
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
|
||||
@@ -86,8 +86,8 @@ static void run_state_machine_loop_wrapper(void* ctx) {
|
||||
|
||||
// @brief Starts run_state_machine_loop in a new thread
|
||||
void Axis::start_thread() {
|
||||
osThreadDef(thread_def, run_state_machine_loop_wrapper, hw_config_.thread_priority, 0, 4 * 512);
|
||||
thread_id_ = osThreadCreate(osThread(thread_def), this);
|
||||
osThreadDef(thread_def, run_state_machine_loop_wrapper, hw_config_.thread_priority, 0, stack_size_ / sizeof(StackType_t));
|
||||
thread_id_ = osThreadCreate(osThread(thread_def), this);
|
||||
thread_id_valid_ = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -234,6 +234,7 @@ public:
|
||||
Endstop& max_endstop_;
|
||||
|
||||
osThreadId thread_id_;
|
||||
const uint32_t stack_size_ = 1024; // Bytes
|
||||
volatile bool thread_id_valid_ = false;
|
||||
|
||||
// variables exposed on protocol
|
||||
|
||||
@@ -757,7 +757,7 @@ static void analog_polling_thread(void *)
|
||||
}
|
||||
|
||||
void start_analog_thread() {
|
||||
osThreadDef(thread_def, analog_polling_thread, osPriorityLow, 0, 128);
|
||||
osThreadDef(thread_def, analog_polling_thread, osPriorityLow, 0, 512 / sizeof(StackType_t));
|
||||
osThreadCreate(osThread(thread_def), NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -193,6 +193,17 @@ void vApplicationIdleHook(void) {
|
||||
system_stats_.min_stack_space_uart = uxTaskGetStackHighWaterMark(uart_thread) * sizeof(StackType_t);
|
||||
system_stats_.min_stack_space_usb_irq = uxTaskGetStackHighWaterMark(usb_irq_thread) * sizeof(StackType_t);
|
||||
system_stats_.min_stack_space_startup = uxTaskGetStackHighWaterMark(defaultTaskHandle) * sizeof(StackType_t);
|
||||
system_stats_.min_stack_space_can = uxTaskGetStackHighWaterMark(odCAN->thread_id_) * sizeof(StackType_t);
|
||||
|
||||
// Actual usage, in bytes, so we don't have to math
|
||||
system_stats_.stack_usage_axis0 = axes[0]->stack_size_ - system_stats_.min_stack_space_axis0;
|
||||
system_stats_.stack_usage_axis1 = axes[1]->stack_size_ - system_stats_.min_stack_space_axis1;
|
||||
system_stats_.stack_usage_comms = stack_size_comm_thread - system_stats_.min_stack_space_comms;
|
||||
system_stats_.stack_usage_usb = stack_size_usb_thread - system_stats_.min_stack_space_usb;
|
||||
system_stats_.stack_usage_uart = stack_size_uart_thread - system_stats_.min_stack_space_uart;
|
||||
system_stats_.stack_usage_usb_irq = stack_size_usb_irq_thread - system_stats_.min_stack_space_usb_irq;
|
||||
system_stats_.stack_usage_startup = stack_size_default_task - system_stats_.min_stack_space_startup;
|
||||
system_stats_.stack_usage_can = odCAN->stack_size_ - system_stats_.min_stack_space_can;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,15 @@ typedef struct {
|
||||
uint32_t min_stack_space_usb_irq;
|
||||
uint32_t min_stack_space_startup;
|
||||
uint32_t min_stack_space_can;
|
||||
|
||||
uint32_t stack_usage_axis0;
|
||||
uint32_t stack_usage_axis1;
|
||||
uint32_t stack_usage_comms;
|
||||
uint32_t stack_usage_usb;
|
||||
uint32_t stack_usage_uart;
|
||||
uint32_t stack_usage_usb_irq;
|
||||
uint32_t stack_usage_startup;
|
||||
uint32_t stack_usage_can;
|
||||
} SystemStats_t;
|
||||
extern SystemStats_t system_stats_;
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ const uint8_t fw_version_revision = FW_VERSION_REVISION;
|
||||
const uint8_t fw_version_unreleased = FW_VERSION_UNRELEASED; // 0 for official releases, 1 otherwise
|
||||
|
||||
osThreadId comm_thread;
|
||||
const uint32_t stack_size_comm_thread = 2048; // Bytes
|
||||
volatile bool endpoint_list_valid = false;
|
||||
|
||||
static uint32_t test_property = 0;
|
||||
@@ -84,7 +85,7 @@ void init_communication(void) {
|
||||
printf("hi!\r\n");
|
||||
|
||||
// Start command handling thread
|
||||
osThreadDef(task_cmd_parse, communication_task, osPriorityNormal, 0, 8000 /* in 32-bit words */); // TODO: fix stack issues
|
||||
osThreadDef(task_cmd_parse, communication_task, osPriorityNormal, 0, stack_size_comm_thread / sizeof(StackType_t));
|
||||
comm_thread = osThreadCreate(osThread(task_cmd_parse), NULL);
|
||||
|
||||
while (!endpoint_list_valid)
|
||||
@@ -135,6 +136,14 @@ static inline auto make_obj_tree() {
|
||||
make_protocol_ro_property("min_stack_space_can", &system_stats_.min_stack_space_can),
|
||||
make_protocol_ro_property("min_stack_space_usb_irq", &system_stats_.min_stack_space_usb_irq),
|
||||
make_protocol_ro_property("min_stack_space_startup", &system_stats_.min_stack_space_startup),
|
||||
make_protocol_ro_property("stack_usage_axis0", &system_stats_.stack_usage_axis0),
|
||||
make_protocol_ro_property("stack_usage_axis1", &system_stats_.stack_usage_axis1),
|
||||
make_protocol_ro_property("stack_usage_comms", &system_stats_.stack_usage_comms),
|
||||
make_protocol_ro_property("stack_usage_usb", &system_stats_.stack_usage_usb),
|
||||
make_protocol_ro_property("stack_usage_uart", &system_stats_.stack_usage_uart),
|
||||
make_protocol_ro_property("stack_usage_usb_irq", &system_stats_.stack_usage_usb_irq),
|
||||
make_protocol_ro_property("stack_usage_startup", &system_stats_.stack_usage_startup),
|
||||
make_protocol_ro_property("stack_usage_can", &system_stats_.stack_usage_can),
|
||||
make_protocol_object("usb",
|
||||
make_protocol_ro_property("rx_cnt", &usb_stats_.rx_cnt),
|
||||
make_protocol_ro_property("tx_cnt", &usb_stats_.tx_cnt),
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
#include <cmsis_os.h>
|
||||
|
||||
extern osThreadId comm_thread;
|
||||
extern const uint32_t stack_size_comm_thread;
|
||||
|
||||
extern const uint8_t hw_version_major;
|
||||
extern const uint8_t hw_version_minor;
|
||||
|
||||
@@ -78,7 +78,7 @@ bool ODriveCAN::start_can_server() {
|
||||
if (status == HAL_OK)
|
||||
status = HAL_CAN_ActivateNotification(handle_, CAN_IT_RX_FIFO0_MSG_PENDING);
|
||||
|
||||
osThreadDef(can_server_thread_def, can_server_thread_wrapper, osPriorityNormal, 0, 512);
|
||||
osThreadDef(can_server_thread_def, can_server_thread_wrapper, osPriorityNormal, 0, stack_size_ / sizeof(StackType_t));
|
||||
thread_id_ = osThreadCreate(osThread(can_server_thread_def), this);
|
||||
thread_id_valid_ = true;
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ class ODriveCAN {
|
||||
|
||||
// Thread Relevant Data
|
||||
osThreadId thread_id_;
|
||||
const uint32_t stack_size_ = 1024; // Bytes
|
||||
Error_t error_ = ERROR_NONE;
|
||||
|
||||
volatile bool thread_id_valid_ = false;
|
||||
|
||||
@@ -22,6 +22,7 @@ static uint32_t dma_last_rcv_idx;
|
||||
// static thread_local uint32_t deadline_ms = 0;
|
||||
|
||||
osThreadId uart_thread;
|
||||
const uint32_t stack_size_uart_thread = 2048; // Bytes
|
||||
|
||||
|
||||
class UART4Sender : public StreamSink {
|
||||
@@ -98,7 +99,7 @@ void start_uart_server() {
|
||||
dma_last_rcv_idx = UART_RX_BUFFER_SIZE - huart4.hdmarx->Instance->NDTR;
|
||||
|
||||
// Start UART communication thread
|
||||
osThreadDef(uart_server_thread_def, uart_server_thread, osPriorityNormal, 0, 1024 /* the ascii protocol needs considerable stack space */);
|
||||
osThreadDef(uart_server_thread_def, uart_server_thread, osPriorityNormal, 0, stack_size_uart_thread / sizeof(StackType_t) /* the ascii protocol needs considerable stack space */);
|
||||
uart_thread = osThreadCreate(osThread(uart_server_thread_def), NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ extern "C" {
|
||||
#include <cmsis_os.h>
|
||||
|
||||
extern osThreadId uart_thread;
|
||||
extern const uint32_t stack_size_uart_thread;
|
||||
|
||||
void start_uart_server(void);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <odrive_main.h>
|
||||
|
||||
osThreadId usb_thread;
|
||||
const uint32_t stack_size_usb_thread = 2048; // Bytes
|
||||
USBStats_t usb_stats_ = {0};
|
||||
|
||||
class USBSender : public PacketSink {
|
||||
@@ -177,6 +178,6 @@ void usb_rx_process_packet(uint8_t *buf, uint32_t len, uint8_t endpoint_pair) {
|
||||
|
||||
void start_usb_server() {
|
||||
// Start USB communication thread
|
||||
osThreadDef(usb_server_thread_def, usb_server_thread, osPriorityNormal, 0, 1024);
|
||||
osThreadDef(usb_server_thread_def, usb_server_thread, osPriorityNormal, 0, stack_size_usb_thread / sizeof(StackType_t));
|
||||
usb_thread = osThreadCreate(osThread(usb_server_thread_def), NULL);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
|
||||
extern osThreadId usb_thread;
|
||||
extern const uint32_t stack_size_usb_thread;
|
||||
|
||||
typedef struct {
|
||||
uint32_t rx_cnt;
|
||||
|
||||
Reference in New Issue
Block a user