Skip to content

Commit 614b04d

Browse files
committed
add lots of stuff from the tempdeck-gen3 setup code
This sets up tim7 as our hal_tick source which is needed for the freertos side to work also add a vtaskdelay in the motor task so it doesn't get caught forever
1 parent 0b18e56 commit 614b04d

File tree

7 files changed

+524
-315
lines changed

7 files changed

+524
-315
lines changed

stm32-modules/flex-stacker/firmware/motor_control/freertos_motor_task.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ auto run(tasks::FirmwareTasks::QueueAggregator* aggregator) -> void {
3030

3131
// auto policy = motor_policy::MotorPolicy();
3232
while (true) {
33+
vTaskDelay(1000);
3334
// _top_task.run_once(policy);
3435
}
3536
}

stm32-modules/flex-stacker/firmware/system/FreeRTOSConfig.h

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/*
2-
* FreeRTOS Kernel V10.3.1
3-
* Portion Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights
4-
* Reserved. Portion Copyright (C) 2019 StMicroelectronics, Inc. All Rights
5-
* Reserved.
2+
* FreeRTOS Kernel V10.0.1
3+
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
64
*
75
* Permission is hereby granted, free of charge, to any person obtaining a copy
86
* of this software and associated documentation files (the "Software"), to deal
@@ -32,7 +30,7 @@
3230
#define FREERTOS_CONFIG_H
3331

3432
/*-----------------------------------------------------------
35-
* Application specific definitions.
33+
* this is a template configuration files
3634
*
3735
* These definitions should be adjusted for your particular hardware and
3836
* application requirements.
@@ -43,40 +41,54 @@
4341
* See http://www.freertos.org/a00110.html
4442
*----------------------------------------------------------*/
4543

46-
/* Ensure definitions are only used by the compiler, and not by the assembler.
47-
*/
44+
/* Ensure stdint is only used by the compiler, and not the assembler. */
4845
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
4946
#include <stdint.h>
5047
extern uint32_t SystemCoreClock;
5148
#endif
52-
#define configENABLE_FPU 0
53-
#define configENABLE_MPU 0
5449

50+
/* CMSIS-RTOSv2 defines 56 levels of priorities. To be able to use them
51+
* all and avoid application misbehavior,
52+
* configUSE_PORT_OPTIMISED_TASK_SELECTION must be set to 0 and
53+
* configMAX_PRIORITIES to 56
54+
*
55+
*/
56+
/* #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0*/
57+
/* #define configMAX_PRIORITIES ( 56 ) */
5558
#define configUSE_PREEMPTION 1
56-
#define configSUPPORT_STATIC_ALLOCATION 1
57-
#define configSUPPORT_DYNAMIC_ALLOCATION 1
5859
#define configUSE_IDLE_HOOK 0
5960
#define configUSE_TICK_HOOK 0
61+
#define configMAX_PRIORITIES (7)
62+
#define configSUPPORT_STATIC_ALLOCATION 1
6063
#define configCPU_CLOCK_HZ (SystemCoreClock)
6164
#define configTICK_RATE_HZ ((TickType_t)1000)
62-
#define configMAX_PRIORITIES (7)
6365
#define configMINIMAL_STACK_SIZE ((uint16_t)128)
64-
#define configTOTAL_HEAP_SIZE ((size_t)3072)
66+
#define configTOTAL_HEAP_SIZE ((size_t)(15 * 1024))
6567
#define configMAX_TASK_NAME_LEN (16)
68+
#define configUSE_TRACE_FACILITY 1
6669
#define configUSE_16_BIT_TICKS 0
70+
#define configIDLE_SHOULD_YIELD 1
6771
#define configUSE_MUTEXES 1
6872
#define configQUEUE_REGISTRY_SIZE 8
69-
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
70-
/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
71-
/* Defaults to size_t for backward compatibility, but can be changed
72-
if lengths will always be less than the number of bytes in a size_t. */
73-
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
74-
/* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */
73+
#define configCHECK_FOR_STACK_OVERFLOW 0
74+
#define configUSE_RECURSIVE_MUTEXES 1
75+
#define configUSE_MALLOC_FAILED_HOOK 0
76+
#define configUSE_APPLICATION_TASK_TAG 0
77+
#define configUSE_COUNTING_SEMAPHORES 1
78+
#define configGENERATE_RUN_TIME_STATS 0
79+
#define configUSE_TASK_NOTIFICATIONS 1
80+
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 8
7581

7682
/* Co-routine definitions. */
7783
#define configUSE_CO_ROUTINES 0
7884
#define configMAX_CO_ROUTINE_PRIORITIES (2)
7985

86+
/* Software timer definitions. */
87+
#define configUSE_TIMERS 1
88+
#define configTIMER_TASK_PRIORITY (2)
89+
#define configTIMER_QUEUE_LENGTH 10
90+
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)
91+
8092
/* Set the following definitions to 1 to include the API function, or zero
8193
to exclude the API function. */
8294
#define INCLUDE_vTaskPrioritySet 1
@@ -88,23 +100,48 @@ to exclude the API function. */
88100
#define INCLUDE_vTaskDelay 1
89101
#define INCLUDE_xTaskGetSchedulerState 1
90102

103+
/*------------- CMSIS-RTOS V2 specific defines -----------*/
104+
/* When using CMSIS-RTOSv2 set configSUPPORT_STATIC_ALLOCATION to 1
105+
* is mandatory to avoid compile errors.
106+
* CMSIS-RTOS V2 implmentation requires the following defines
107+
*
108+
#define configSUPPORT_STATIC_ALLOCATION 1 <-- cmsis_os threads are
109+
created using xTaskCreateStatic() API #define configMAX_PRIORITIES (56) <--
110+
Priority range in CMSIS-RTOS V2 is [0 .. 56] #define
111+
configUSE_PORT_OPTIMISED_TASK_SELECTION 0 <-- when set to 1,
112+
configMAX_PRIORITIES can't be more than 32 which is not suitable for the new
113+
CMSIS-RTOS v2 priority range
114+
*/
115+
116+
/* the CMSIS-RTOS V2 FreeRTOS wrapper is dependent on the heap implementation
117+
used
118+
* by the application thus the correct define need to be enabled from the list
119+
* below
120+
*
121+
//define USE_FreeRTOS_HEAP_1
122+
//define USE_FreeRTOS_HEAP_2
123+
//define USE_FreeRTOS_HEAP_3
124+
//define USE_FreeRTOS_HEAP_4
125+
//define USE_FreeRTOS_HEAP_5
126+
*/
127+
91128
/* Cortex-M specific definitions. */
92129
#ifdef __NVIC_PRIO_BITS
93130
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
94131
#define configPRIO_BITS __NVIC_PRIO_BITS
95132
#else
96-
#define configPRIO_BITS 4
133+
#define configPRIO_BITS 4 /* 15 priority levels */
97134
#endif
98135

99136
/* The lowest interrupt priority that can be used in a call to a "set priority"
100137
function. */
101-
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15
138+
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf
102139

103140
/* The highest interrupt priority that can be used by any interrupt service
104141
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
105142
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
106143
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
107-
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
144+
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 4
108145

109146
/* Interrupt priorities used by the kernel port layer itself. These are generic
110147
to all Cortex-M ports, and do not rely on any particular library functions. */
@@ -117,23 +154,29 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
117154

118155
/* Normal assert() semantics without relying on the provision of an assert.h
119156
header file. */
157+
#ifdef ASSERTIONS_ENABLED
120158

121159
#define configASSERT(x) \
122160
if ((x) == 0) { \
123161
taskDISABLE_INTERRUPTS(); \
124162
for (;;) \
125163
; \
126164
}
165+
#else
166+
167+
#define configASSERT(x) (void)(x)
168+
169+
#endif
127170

128171
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
129-
standard names. */
172+
standard names. */
130173
#define vPortSVCHandler SVC_Handler
131174
#define xPortPendSVHandler PendSV_Handler
132175

133-
/* IMPORTANT: This define is commented when used with STM32Cube firmware, when
134-
the timebase source is SysTick, to prevent overwriting SysTick_Handler
135-
defined within STM32Cube HAL */
136-
176+
/* IMPORTANT: FreeRTOS is using the SysTick as internal time base, thus make
177+
sure the system and peripherials are using a different time base (TIM based
178+
for example).
179+
*/
137180
#define xPortSysTickHandler SysTick_Handler
138181

139182
#endif /* FREERTOS_CONFIG_H */
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @file hal_tick.c
3+
* @brief Provides an alternate source for the STM32 HAL Tick timer. This is
4+
* the systick timer by default, but in a FreeRTOS application it is important
5+
* to provide an alternative timer to free up systick for RTOS use.
6+
*
7+
* The actual tick value is incremented in the HAL_TIM_PeriodElapsedCallback.
8+
* See \ref stm32g4xx_it.c for detail.
9+
*/
10+
11+
/* Includes ------------------------------------------------------------------*/
12+
#include "stm32g4xx_hal.h"
13+
#include "stm32g4xx_hal_tim.h"
14+
15+
16+
TIM_HandleTypeDef htim7;
17+
18+
/**
19+
* @brief This function configures the TIM7 as a time base source.
20+
* The time source is configured to have 1ms time base with a dedicated
21+
* Tick interrupt priority.
22+
* @note This function is called automatically at the beginning of program after
23+
* reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
24+
* @param TickPriority: Tick interrupt priority.
25+
* @retval HAL status
26+
*/
27+
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
28+
{
29+
RCC_ClkInitTypeDef clkconfig;
30+
uint32_t uwTimclock = 0;
31+
uint32_t uwPrescalerValue = 0;
32+
uint32_t pFLatency;
33+
HAL_StatusTypeDef status = HAL_OK;
34+
35+
/* Enable TIM7 clock */
36+
__HAL_RCC_TIM7_CLK_ENABLE();
37+
38+
/* Get clock configuration */
39+
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
40+
41+
/* Compute TIM7 clock */
42+
uwTimclock = HAL_RCC_GetPCLK1Freq();
43+
/* Compute the prescaler value to have TIM7 counter clock equal to 1MHz */
44+
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
45+
46+
/* Initialize TIM7 */
47+
htim7.Instance = TIM7;
48+
49+
/* Initialize TIMx peripheral as follow:
50+
+ Period = [(TIM7CLK/1000) - 1]. to have a (1/1000) s time base.
51+
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
52+
+ ClockDivision = 0
53+
+ Counter direction = Up
54+
*/
55+
htim7.State = HAL_TIM_STATE_RESET;
56+
htim7.Init.Period = (1000000U / 1000U) - 1U;
57+
htim7.Init.Prescaler = uwPrescalerValue;
58+
htim7.Init.ClockDivision = 0;
59+
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
60+
61+
status = HAL_TIM_Base_Init(&htim7);
62+
if (status == HAL_OK)
63+
{
64+
/* Start the TIM time Base generation in interrupt mode */
65+
status = HAL_TIM_Base_Start_IT(&htim7);
66+
if (status == HAL_OK)
67+
{
68+
/* Enable the TIM7 global Interrupt */
69+
HAL_NVIC_EnableIRQ(TIM7_IRQn);
70+
/* Configure the SysTick IRQ priority */
71+
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
72+
{
73+
/* Configure the TIM IRQ priority */
74+
HAL_NVIC_SetPriority(TIM7_IRQn, TickPriority, 0U);
75+
uwTickPrio = TickPriority;
76+
}
77+
else
78+
{
79+
status = HAL_ERROR;
80+
}
81+
}
82+
}
83+
/* Return function status */
84+
return status;
85+
}
86+
87+
/**
88+
* @brief Suspend Tick increment.
89+
* @note Disable the tick increment by disabling TIM7 update interrupt.
90+
* @param None
91+
* @retval None
92+
*/
93+
void HAL_SuspendTick(void)
94+
{
95+
/* Disable TIM7 update Interrupt */
96+
__HAL_TIM_DISABLE_IT(&htim7, TIM_IT_UPDATE);
97+
}
98+
99+
/**
100+
* @brief Resume Tick increment.
101+
* @note Enable the tick increment by Enabling TIM7 update interrupt.
102+
* @param None
103+
* @retval None
104+
*/
105+
void HAL_ResumeTick(void)
106+
{
107+
/* Enable TIM7 Update interrupt */
108+
__HAL_TIM_ENABLE_IT(&htim7, TIM_IT_UPDATE);
109+
}
110+
111+
/**
112+
* @brief Period elapsed callback in non blocking mode
113+
* @note This function is called when TIM7 interrupt took place.
114+
* It makes a direct call to HAL_IncTick() to increment a global variable
115+
* "uwTick" used as application time base.
116+
* @param htim : TIM handle
117+
* @retval None
118+
*/
119+
void TIM7_IRQHandler(void)
120+
{
121+
HAL_TIM_IRQHandler(&htim7);
122+
}

stm32-modules/flex-stacker/firmware/system/stm32g4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ void assert_failed(uint8_t *file, uint32_t line);
384384
#else
385385
#define assert_param(expr) ((void)0U)
386386
#endif /* USE_FULL_ASSERT */
387-
387+
void Error_Handler(void);
388388
#ifdef __cplusplus
389389
}
390390
#endif

stm32-modules/flex-stacker/firmware/system/stm32g4xx_hal_msp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ void HAL_MspInit(void)
4747
HAL_PWREx_DisableUCPDDeadBattery();
4848
}
4949

50+
void HAL_MspDeInit(void)
51+
{
52+
__HAL_RCC_SYSCFG_CLK_DISABLE();
53+
__HAL_RCC_PWR_CLK_DISABLE();
54+
55+
__HAL_RCC_TIM7_CLK_DISABLE();
56+
__HAL_RCC_USB_CLK_DISABLE();
57+
__HAL_RCC_SYSCFG_CLK_DISABLE();
58+
__HAL_RCC_PWR_CLK_DISABLE();
59+
/* System interrupt init*/
60+
/* PendSV_IRQn interrupt configuration */
61+
HAL_NVIC_DisableIRQ(PendSV_IRQn);
62+
HAL_NVIC_DisableIRQ(RCC_IRQn);
63+
}
5064
/**
5165
* @brief I2C MSP Initialization
5266
* This function configures the hardware resources used in this example
@@ -225,6 +239,11 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
225239
/* Peripheral clock enable */
226240
__HAL_RCC_TIM3_CLK_ENABLE();
227241
}
242+
else if(htim_base->Instance==TIM7)
243+
{
244+
/* Peripheral clock enable */
245+
__HAL_RCC_TIM7_CLK_ENABLE();
246+
}
228247
else if(htim_base->Instance==TIM17)
229248
{
230249
/* Peripheral clock enable */

stm32-modules/flex-stacker/firmware/system/stm32g4xx_it.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,20 @@ void DebugMon_Handler(void)
8686
{
8787
}
8888

89-
///**
90-
// * @brief This function handles System tick timer.
91-
// */
92-
//void SysTick_Handler(void)
93-
//{
94-
// HAL_IncTick();
95-
//#if (INCLUDE_xTaskGetSchedulerState == 1 )
96-
// if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
97-
// {
98-
//#endif /* INCLUDE_xTaskGetSchedulerState */
99-
// xPortSysTickHandler();
100-
//#if (INCLUDE_xTaskGetSchedulerState == 1 )
101-
// }
102-
//#endif /* INCLUDE_xTaskGetSchedulerState */
103-
//}
104-
10589
/******************************************************************************/
10690
/* STM32G4xx Peripheral Interrupt Handlers */
10791
/* Add here the Interrupt Handlers for the used peripherals. */
10892
/* For the available peripheral interrupt handler names, */
10993
/* please refer to the startup file (startup_stm32g4xx.s). */
11094
/******************************************************************************/
11195

96+
11297
/**
113-
* @brief This function handles RCC global interrupt.
98+
* TIM7 = timebase counter
11499
*/
115-
void RCC_IRQHandler(void)
100+
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
116101
{
102+
if(htim->Instance == TIM7) {
103+
HAL_IncTick();
104+
}
117105
}
118-

0 commit comments

Comments
 (0)