3 Commits

Author SHA1 Message Date
ba94373f3e Update param freq 2024-08-01 20:56:09 +02:00
25da5cde32 Fix dumb config typo 2024-07-24 09:35:22 +02:00
10aa474124 Fix DRS 2024-07-23 15:13:22 +02:00
7 changed files with 295 additions and 255 deletions

View File

@ -21,19 +21,19 @@
/* Includes ------------------------------------------------------------------*/
#include "app_azure_rtos.h"
#include "stm32h7xx.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app.h"
#include "app_touchgfx.h"
#include "leds.h"
#include "main.h"
#include "ui.h"
#include "vehicle.h"
#include "app_touchgfx.h"
#include "tx_api.h"
#include "tx_port.h"
#include "ui.h"
#include "vehicle.h"
/* USER CODE END Includes */
@ -59,7 +59,8 @@
#if defined(__ICCARM__)
#pragma data_alignment = 4
#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;
#endif
@ -69,6 +70,7 @@ TX_THREAD app_thread;
TX_THREAD ui_thread;
TX_THREAD vehicle_thread;
TX_THREAD led_thread;
TX_THREAD params_thread;
TX_QUEUE gui_button_queue;
TX_EVENT_FLAGS_GROUP gui_update_events;
/* USER CODE END PV */
@ -83,8 +85,7 @@ TX_EVENT_FLAGS_GROUP gui_update_events;
* @param first_unused_memory : Pointer to the first unused memory
* @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 END tx_application_define_1 */
@ -92,22 +93,20 @@ VOID tx_application_define(VOID *first_unused_memory)
UINT status = TX_SUCCESS;
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 END TX_Byte_Pool_Error */
}
else
{
} else {
/* USER CODE BEGIN TX_Byte_Pool_Success */
/* USER CODE END TX_Byte_Pool_Success */
memory_ptr = (VOID *)&tx_app_byte_pool;
status = App_ThreadX_Init(memory_ptr);
if (status != TX_SUCCESS)
{
if (status != TX_SUCCESS) {
/* USER CODE BEGIN App_ThreadX_Init_Error */
while (1) {
}
@ -117,21 +116,22 @@ VOID tx_application_define(VOID *first_unused_memory)
/* USER CODE BEGIN App_ThreadX_Init_Success */
/* USER CODE END App_ThreadX_Init_Success */
}
#else
/*
* Using dynamic memory allocation requires to apply some changes to the linker file.
* ThreadX needs to pass a pointer to the first free memory location in RAM to the tx_application_define() function,
* Using dynamic memory allocation requires to apply some changes to the
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.
* This require changes in the linker files to expose this memory location.
* For EWARM add the following section into the .icf file:
place in RAM_region { last section FREE_MEM };
* For MDK-ARM
- 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
LDR r1, =|Image$$RW_IRAM1$$ZI$$Limit|
- or modify the line below in "tx_initialize_low_level.S to match the
memory region being used LDR r1, =|Image$$RW_IRAM1$$ZI$$Limit|
* For STM32CubeIDE add the following section into the .ld file:
._threadx_heap :
@ -141,13 +141,17 @@ VOID tx_application_define(VOID *first_unused_memory)
. = . + 64K;
. = ALIGN(8);
} >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.
* The ._threadx_heap must be located between the .bss and the ._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).
* The ._threadx_heap must be located between the .bss and the
._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".
* 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 */
@ -208,6 +212,15 @@ VOID tx_application_define(VOID *first_unused_memory)
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) {
Error_Handler();
}
@ -215,5 +228,4 @@ VOID tx_application_define(VOID *first_unused_memory)
mem += 4096;
/* USER CODE END DYNAMIC_MEM_ALLOC */
#endif
}

View File

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

View File

@ -7,10 +7,12 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>
#include <tx_port.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 {
unsigned plim; //< Power limit
@ -28,6 +30,11 @@ void params_inc(ParamType param);
void params_dec(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
}
#endif

View File

@ -1,17 +1,29 @@
#include "params.h"
#include <tx_api.h>
#include "can-halal.h"
#include "leds.h"
#include "vehicle.h"
/**
* Decrements the given value if it is above the minimum allowed value
*/
#define DEC_IF_ABOVE(param_val, min_val, decr_amt) ((param_val) = ((param_val) - (decr_amt) ) > (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))
// TODO these functions take into account that the parameters are unsigned, it's
// definitely better to have them signed but would need to be tested with the
// autobox
#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};
void params_init()
{
void params_init() {
// Default values
params.plim = 20;
params.tlim = 1400;
@ -21,10 +33,8 @@ void params_init()
params.reku = 0;
}
void params_inc(ParamType param)
{
switch (param)
{
void params_inc(ParamType param) {
switch (param) {
case PF_PLIM:
INC_IF_BELOW(params.plim, 80, 1);
break;
@ -46,10 +56,8 @@ void params_inc(ParamType param)
}
}
void params_dec(ParamType param)
{
switch (param)
{
void params_dec(ParamType param) {
switch (param) {
case PF_PLIM:
DEC_IF_ABOVE(params.plim, 0, 1);
break;
@ -71,11 +79,9 @@ void params_dec(ParamType param)
}
}
void params_broadcast(ParamType param)
{
void params_broadcast(ParamType param) {
int32_t value;
switch (param)
{
switch (param) {
case PF_PLIM:
value = params.plim;
break;
@ -99,3 +105,17 @@ void params_broadcast(ParamType param)
}
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);
}
}

View File

@ -13,7 +13,7 @@
#include "leds.h"
#define DRS_BUTTON_IDX (6)
#define DRS_PRESS_WAIT_CYCLES (10)
#define DRS_PRESS_WAIT_CYCLES (1000)
static int drs_press_buf_cycles = 0;
void ui_thread_entry(ULONG _) {

View File

@ -1,16 +1,14 @@
#include "vehicle.h"
#include "main.h"
#include "ui.h"
#include "vehicle_state.h"
#include "can-halal.h"
#include "main.h"
#include "stm32h7xx.h"
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_fdcan.h"
#include "stm32h7xx_hal_gpio.h"
#include "tx_api.h"
#include "ui.h"
#include "vehicle_state.h"
#define CAN_ID_AMS_SLAVE_PANIC 0x9
#define CAN_ID_AMS_STATUS 0xA
@ -98,7 +96,8 @@ void vehicle_broadcast_param(ParamType param, int32_t value) {
void vehicle_broadcast_buttons(GPIO_PinState *button_states) {
uint8_t data = (button_states[0] << 2) | (button_states[1] << 0) |
(button_states[2] << 1) | (button_states[3] << 3);
(button_states[2] << 1) | (button_states[3] << 3) |
(button_states[6] << 4);
ftcan_transmit(CAN_ID_STW_BUTTONS, &data, 1);
}
@ -228,12 +227,14 @@ void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) {
break;
case CAN_ID_ABX_PARAM_CONFIRMED:
vehicle_state.last_param_confirmed = data[0];
tx_event_flags_set(&gui_update_events, GUI_UPDATE_PARAM_CONFIRMED, TX_OR);
tx_event_flags_set(&gui_update_events, GUI_UPDATE_PARAM_CONFIRMED,
TX_OR);
break;
case CAN_ID_SHUNT_CURRENT: {
// The first two bytes of shunt result messages are metadata
const uint8_t *result_ptr = &data[2];
vehicle_state.ts_current = ftcan_unmarshal_signed(&result_ptr, 4) * 1e-3;
vehicle_state.ts_current =
ftcan_unmarshal_signed(&result_ptr, 4) * 1e-3;
break;
}
case CAN_ID_SHUNT_VOLTAGE1: {

View File

@ -336,7 +336,7 @@ static_assert(sizeof(dataFieldDescs) / sizeof(dataFieldDescs[0]) ==
NamedFieldDescription paramFieldDescs[] = {
[PF_PLIM] = {NamedFieldKind::Int, "PLIM", 2, 0, PARAM_FIELD(plim)},
[PF_TLIM] = {NamedFieldKind::Int, "TLIM", 4, 1, PARAM_FIELD(plim)},
[PF_TLIM] = {NamedFieldKind::Int, "TLIM", 4, 1, PARAM_FIELD(tlim)},
[PF_SLIM] = {NamedFieldKind::Int, "SLIM", 2, 2, PARAM_FIELD(slim)},
[PF_TVEC] = {NamedFieldKind::Int, "TVEC", 2, 1, PARAM_FIELD(tvec)},
[PF_PG] = {NamedFieldKind::Int, "PG", 2, 0, PARAM_FIELD(pg)},