Compare commits

...

1 Commits

Author SHA1 Message Date
Julian Marmier ba94373f3e Update param freq 2024-08-01 20:56:09 +02:00
4 changed files with 150 additions and 113 deletions

View File

@ -21,19 +21,19 @@
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "app_azure_rtos.h" #include "app_azure_rtos.h"
#include "stm32h7xx.h" #include "stm32h7xx.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "app.h" #include "app.h"
#include "app_touchgfx.h"
#include "leds.h" #include "leds.h"
#include "main.h" #include "main.h"
#include "ui.h"
#include "vehicle.h"
#include "app_touchgfx.h"
#include "tx_api.h" #include "tx_api.h"
#include "tx_port.h" #include "tx_port.h"
#include "ui.h"
#include "vehicle.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -56,10 +56,11 @@
#if (USE_STATIC_ALLOCATION == 1) #if (USE_STATIC_ALLOCATION == 1)
/* USER CODE BEGIN TX_Pool_Buffer */ /* USER CODE BEGIN TX_Pool_Buffer */
/* USER CODE END TX_Pool_Buffer */ /* USER CODE END TX_Pool_Buffer */
#if defined ( __ICCARM__ ) #if defined(__ICCARM__)
#pragma data_alignment=4 #pragma data_alignment = 4
#endif #endif
__ALIGN_BEGIN static UCHAR tx_byte_pool_buffer[TX_APP_MEM_POOL_SIZE] __ALIGN_END; __ALIGN_BEGIN static UCHAR
tx_byte_pool_buffer[TX_APP_MEM_POOL_SIZE] __ALIGN_END;
static TX_BYTE_POOL tx_app_byte_pool; static TX_BYTE_POOL tx_app_byte_pool;
#endif #endif
@ -69,6 +70,7 @@ TX_THREAD app_thread;
TX_THREAD ui_thread; TX_THREAD ui_thread;
TX_THREAD vehicle_thread; TX_THREAD vehicle_thread;
TX_THREAD led_thread; TX_THREAD led_thread;
TX_THREAD params_thread;
TX_QUEUE gui_button_queue; TX_QUEUE gui_button_queue;
TX_EVENT_FLAGS_GROUP gui_update_events; TX_EVENT_FLAGS_GROUP gui_update_events;
/* USER CODE END PV */ /* USER CODE END PV */
@ -79,12 +81,11 @@ TX_EVENT_FLAGS_GROUP gui_update_events;
/* USER CODE END PFP */ /* USER CODE END PFP */
/** /**
* @brief Define the initial system. * @brief Define the initial system.
* @param first_unused_memory : Pointer to the first unused memory * @param first_unused_memory : Pointer to the first unused memory
* @retval None * @retval None
*/ */
VOID tx_application_define(VOID *first_unused_memory) VOID tx_application_define(VOID *first_unused_memory) {
{
/* USER CODE BEGIN tx_application_define_1*/ /* USER CODE BEGIN tx_application_define_1*/
/* USER CODE END tx_application_define_1 */ /* USER CODE END tx_application_define_1 */
@ -92,22 +93,20 @@ VOID tx_application_define(VOID *first_unused_memory)
UINT status = TX_SUCCESS; UINT status = TX_SUCCESS;
VOID *memory_ptr; VOID *memory_ptr;
if (tx_byte_pool_create(&tx_app_byte_pool, "Tx App memory pool", tx_byte_pool_buffer, TX_APP_MEM_POOL_SIZE) != TX_SUCCESS) if (tx_byte_pool_create(&tx_app_byte_pool, "Tx App memory pool",
{ tx_byte_pool_buffer,
TX_APP_MEM_POOL_SIZE) != TX_SUCCESS) {
/* USER CODE BEGIN TX_Byte_Pool_Error */ /* USER CODE BEGIN TX_Byte_Pool_Error */
/* USER CODE END TX_Byte_Pool_Error */ /* USER CODE END TX_Byte_Pool_Error */
} } else {
else
{
/* USER CODE BEGIN TX_Byte_Pool_Success */ /* USER CODE BEGIN TX_Byte_Pool_Success */
/* USER CODE END TX_Byte_Pool_Success */ /* USER CODE END TX_Byte_Pool_Success */
memory_ptr = (VOID *)&tx_app_byte_pool; memory_ptr = (VOID *)&tx_app_byte_pool;
status = App_ThreadX_Init(memory_ptr); status = App_ThreadX_Init(memory_ptr);
if (status != TX_SUCCESS) if (status != TX_SUCCESS) {
{
/* USER CODE BEGIN App_ThreadX_Init_Error */ /* USER CODE BEGIN App_ThreadX_Init_Error */
while (1) { while (1) {
} }
@ -117,21 +116,22 @@ VOID tx_application_define(VOID *first_unused_memory)
/* USER CODE BEGIN App_ThreadX_Init_Success */ /* USER CODE BEGIN App_ThreadX_Init_Success */
/* USER CODE END App_ThreadX_Init_Success */ /* USER CODE END App_ThreadX_Init_Success */
} }
#else #else
/* /*
* Using dynamic memory allocation requires to apply some changes to the linker file. * Using dynamic memory allocation requires to apply some changes to the
* ThreadX needs to pass a pointer to the first free memory location in RAM to the tx_application_define() function, linker file.
* ThreadX needs to pass a pointer to the first free memory location in RAM to
the tx_application_define() function,
* using the "first_unused_memory" argument. * using the "first_unused_memory" argument.
* This require changes in the linker files to expose this memory location. * This require changes in the linker files to expose this memory location.
* For EWARM add the following section into the .icf file: * For EWARM add the following section into the .icf file:
place in RAM_region { last section FREE_MEM }; place in RAM_region { last section FREE_MEM };
* For MDK-ARM * For MDK-ARM
- either define the RW_IRAM1 region in the ".sct" file - either define the RW_IRAM1 region in the ".sct" file
- or modify the line below in "tx_initialize_low_level.S to match the memory region being used - or modify the line below in "tx_initialize_low_level.S to match the
LDR r1, =|Image$$RW_IRAM1$$ZI$$Limit| memory region being used LDR r1, =|Image$$RW_IRAM1$$ZI$$Limit|
* For STM32CubeIDE add the following section into the .ld file: * For STM32CubeIDE add the following section into the .ld file:
._threadx_heap : ._threadx_heap :
@ -141,13 +141,17 @@ VOID tx_application_define(VOID *first_unused_memory)
. = . + 64K; . = . + 64K;
. = ALIGN(8); . = ALIGN(8);
} >RAM_D1 AT> RAM_D1 } >RAM_D1 AT> RAM_D1
* The simplest way to provide memory for ThreadX is to define a new section, see ._threadx_heap above. * The simplest way to provide memory for ThreadX is to define a new
section, see ._threadx_heap above.
* In the example above the ThreadX heap size is set to 64KBytes. * In the example above the ThreadX heap size is set to 64KBytes.
* The ._threadx_heap must be located between the .bss and the ._user_heap_stack sections in the linker script. * The ._threadx_heap must be located between the .bss and the
* Caution: Make sure that ThreadX does not need more than the provided heap memory (64KBytes in this example). ._user_heap_stack sections in the linker script.
* Caution: Make sure that ThreadX does not need more than the provided
heap memory (64KBytes in this example).
* Read more in STM32CubeIDE User Guide, chapter: "Linker script". * Read more in STM32CubeIDE User Guide, chapter: "Linker script".
* The "tx_initialize_low_level.S" should be also modified to enable the "USE_DYNAMIC_MEMORY_ALLOCATION" flag. * The "tx_initialize_low_level.S" should be also modified to enable the
"USE_DYNAMIC_MEMORY_ALLOCATION" flag.
*/ */
/* USER CODE BEGIN DYNAMIC_MEM_ALLOC */ /* USER CODE BEGIN DYNAMIC_MEM_ALLOC */
@ -208,6 +212,15 @@ VOID tx_application_define(VOID *first_unused_memory)
Error_Handler(); Error_Handler();
} }
void *params_thread_stack = mem;
mem += THREAD_STACK_SIZE;
if (tx_thread_create(&params_thread, "Params Thread", params_thread_entry, 0,
params_thread_stack, THREAD_STACK_SIZE,
THREAD_PRIO_PARAMS, THREAD_PRIO_UI, TX_NO_TIME_SLICE,
TX_AUTO_START) != TX_SUCCESS) {
Error_Handler();
}
if (MX_TouchGFX_Init(mem) != TX_SUCCESS) { if (MX_TouchGFX_Init(mem) != TX_SUCCESS) {
Error_Handler(); Error_Handler();
} }
@ -215,5 +228,4 @@ VOID tx_application_define(VOID *first_unused_memory)
mem += 4096; mem += 4096;
/* USER CODE END DYNAMIC_MEM_ALLOC */ /* USER CODE END DYNAMIC_MEM_ALLOC */
#endif #endif
} }

View File

@ -27,7 +27,6 @@ extern "C" {
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
#include "app_azure_rtos_config.h" #include "app_azure_rtos_config.h"
#include "app_threadx.h" #include "app_threadx.h"
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
@ -44,6 +43,7 @@ extern "C" {
#define THREAD_STACK_SIZE 4096 #define THREAD_STACK_SIZE 4096
#define THREAD_PRIO_APP 10 #define THREAD_PRIO_APP 10
#define THREAD_PRIO_PARAMS 8
#define THREAD_PRIO_UI 6 #define THREAD_PRIO_UI 6
#define THREAD_PRIO_VEHICLE 7 #define THREAD_PRIO_VEHICLE 7
#define THREAD_PRIO_LED 11 #define THREAD_PRIO_LED 11

View File

@ -7,18 +7,20 @@ extern "C" {
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <tx_port.h>
#include "util.h" #include "util.h"
CountedEnum(ParamType, size_t, PF_PLIM, PF_TLIM, PF_SLIM, PF_TVEC, PF_PG, PF_REKU); CountedEnum(ParamType, size_t, PF_PLIM, PF_TLIM, PF_SLIM, PF_TVEC, PF_PG,
PF_REKU);
typedef struct { typedef struct {
unsigned plim; //< Power limit unsigned plim; //< Power limit
unsigned tlim; //< Torque limit unsigned tlim; //< Torque limit
unsigned slim; //< Speed limit unsigned slim; //< Speed limit
unsigned tvec; //< Torque vectoring unsigned tvec; //< Torque vectoring
unsigned pg; //< Power ground unsigned pg; //< Power ground
unsigned reku; //< Rekuperation unsigned reku; //< Rekuperation
} Params; } Params;
extern Params params; extern Params params;
@ -28,8 +30,13 @@ void params_inc(ParamType param);
void params_dec(ParamType param); void params_dec(ParamType param);
void params_broadcast(ParamType param); void params_broadcast(ParamType param);
/** FT24 only
* Broadcasts the current registered params to the CAN bus every second
*/
void params_thread_entry(ULONG _);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // INC_PARAMS_H #endif // INC_PARAMS_H

View File

@ -1,19 +1,29 @@
#include "params.h" #include "params.h"
#include <tx_api.h>
#include "can-halal.h" #include "can-halal.h"
#include "leds.h"
#include "vehicle.h" #include "vehicle.h"
/** /**
* Decrements the given value if it is above the minimum allowed value * Decrements the given value if it is above the minimum allowed value
*/ */
// TODO these functions take into account that the parameters are unsigned, it's definitely better to have them // TODO these functions take into account that the parameters are unsigned, it's
// signed but would need to be tested with the autobox // definitely better to have them signed but would need to be tested with the
#define DEC_IF_ABOVE(param_val, min_val, decr_amt) ((param_val) = (((int)(param_val) - (int)(decr_amt)) > (int)(min_val)) ? ((param_val) - (decr_amt)) : (min_val)) // autobox
#define INC_IF_BELOW(param_val, max_val, incr_amt) ((param_val) = (((param_val) + (incr_amt)) < (max_val)) ? ((param_val) + (incr_amt)) : (max_val)) #define DEC_IF_ABOVE(param_val, min_val, decr_amt) \
((param_val) = (((int)(param_val) - (int)(decr_amt)) > (int)(min_val)) \
? ((param_val) - (decr_amt)) \
: (min_val))
#define INC_IF_BELOW(param_val, max_val, incr_amt) \
((param_val) = (((param_val) + (incr_amt)) < (max_val)) \
? ((param_val) + (incr_amt)) \
: (max_val))
Params params = {0}; Params params = {0};
void params_init() void params_init() {
{
// Default values // Default values
params.plim = 20; params.plim = 20;
params.tlim = 1400; params.tlim = 1400;
@ -23,81 +33,89 @@ void params_init()
params.reku = 0; params.reku = 0;
} }
void params_inc(ParamType param) void params_inc(ParamType param) {
{ switch (param) {
switch (param) case PF_PLIM:
{ INC_IF_BELOW(params.plim, 80, 1);
case PF_PLIM: break;
INC_IF_BELOW(params.plim, 80, 1); case PF_TLIM:
break; INC_IF_BELOW(params.tlim, 1500, 100);
case PF_TLIM: break;
INC_IF_BELOW(params.tlim, 1500, 100); case PF_SLIM:
break; INC_IF_BELOW(params.slim, 100, 1);
case PF_SLIM: break;
INC_IF_BELOW(params.slim, 100, 1); case PF_TVEC:
break; INC_IF_BELOW(params.tvec, 100, 1);
case PF_TVEC: break;
INC_IF_BELOW(params.tvec, 100, 1); case PF_PG:
break; INC_IF_BELOW(params.pg, 100, 1);
case PF_PG: break;
INC_IF_BELOW(params.pg, 100, 1); case PF_REKU:
break; INC_IF_BELOW(params.reku, 100, 1);
case PF_REKU: break;
INC_IF_BELOW(params.reku, 100, 1);
break;
} }
} }
void params_dec(ParamType param) void params_dec(ParamType param) {
{ switch (param) {
switch (param) case PF_PLIM:
{ DEC_IF_ABOVE(params.plim, 0, 1);
case PF_PLIM: break;
DEC_IF_ABOVE(params.plim, 0, 1); case PF_TLIM:
break; DEC_IF_ABOVE(params.tlim, 0, 100);
case PF_TLIM: break;
DEC_IF_ABOVE(params.tlim, 0, 100); case PF_SLIM:
break; DEC_IF_ABOVE(params.slim, 0, 1);
case PF_SLIM: break;
DEC_IF_ABOVE(params.slim, 0, 1); case PF_TVEC:
break; DEC_IF_ABOVE(params.tvec, 0, 1);
case PF_TVEC: break;
DEC_IF_ABOVE(params.tvec, 0, 1); case PF_PG:
break; DEC_IF_ABOVE(params.pg, 0, 1);
case PF_PG: break;
DEC_IF_ABOVE(params.pg, 0, 1); case PF_REKU:
break; DEC_IF_ABOVE(params.reku, 0, 1);
case PF_REKU: break;
DEC_IF_ABOVE(params.reku, 0, 1);
break;
} }
} }
void params_broadcast(ParamType param) void params_broadcast(ParamType param) {
{
int32_t value; int32_t value;
switch (param) switch (param) {
{ case PF_PLIM:
case PF_PLIM: value = params.plim;
value = params.plim; break;
break; case PF_TLIM:
case PF_TLIM: value = params.tlim;
value = params.tlim; break;
break; case PF_SLIM:
case PF_SLIM: value = params.slim;
value = params.slim; break;
break; case PF_TVEC:
case PF_TVEC: value = params.tvec;
value = params.tvec; break;
break; case PF_PG:
case PF_PG: value = params.pg;
value = params.pg; break;
break; case PF_REKU:
case PF_REKU: value = params.reku;
value = params.reku; break;
break; default:
default: return;
return;
} }
vehicle_broadcast_param(param, value); vehicle_broadcast_param(param, value);
} }
void params_thread_entry(ULONG _) {
tx_thread_sleep(TX_TIMER_TICKS_PER_SECOND);
while (1) {
for (size_t i = 0; i < ParamType_COUNT; i++) {
led_set(1, 0, 255, 0);
params_broadcast(i);
}
// Wait one second
tx_thread_sleep(TX_TIMER_TICKS_PER_SECOND);
}
}