Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Oct 19, 2021
1 parent cf142bc commit 04cf85c
Show file tree
Hide file tree
Showing 10 changed files with 1,146 additions and 1,007 deletions.
2 changes: 1 addition & 1 deletion src/eez/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ int main(int argc, char **argv) {
MX_SPI5_Init();
// MX_TIM3_Init();
MX_TIM6_Init();
//MX_TIM8_Init();
MX_TIM8_Init();

MX_TIM7_Init();
HAL_TIM_Base_Start_IT(&htim7);
Expand Down
31 changes: 30 additions & 1 deletion src/eez/modules/mcu/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <eez/modules/mcu/encoder.h>

#if defined(EEZ_PLATFORM_STM32)
#include <tim.h>
#include <eez/modules/mcu/button.h>
#endif

Expand Down Expand Up @@ -84,7 +85,30 @@ void write(int counter, bool clicked) {
#endif

#if defined(EEZ_PLATFORM_STM32)
bool g_debounceTimerStarted = false;
volatile uint8_t g_pinState;

#define READ_ENC_PIN_STATE (HAL_GPIO_ReadPin(ENC_B_GPIO_Port, ENC_B_Pin) << 1) | HAL_GPIO_ReadPin(ENC_A_GPIO_Port, ENC_A_Pin)

void onPinInterrupt() {
if (!g_debounceTimerStarted) {
g_pinState = READ_ENC_PIN_STATE;
TIM8->ARR = 500;
HAL_TIM_Base_Start_IT(&htim8);
g_debounceTimerStarted = true;
}
}

void onDebounceTimer() {
HAL_TIM_Base_Stop_IT(&htim8);
g_debounceTimerStarted = false;

uint8_t pinState = READ_ENC_PIN_STATE;
if (pinState != g_pinState) {
// debounce
return;
}

// https://github.com/buxtronix/arduino/blob/master/libraries/Rotary/Rotary.cpp
// static const uint8_t DIR_NONE = 0x0; // No complete step yet.
static const uint8_t DIR_CW = 0x10; // Clockwise step.
Expand Down Expand Up @@ -145,7 +169,6 @@ void onPinInterrupt() {

static volatile uint8_t g_rotationState = R_START;

uint8_t pinState = (HAL_GPIO_ReadPin(ENC_B_GPIO_Port, ENC_B_Pin) << 1) | HAL_GPIO_ReadPin(ENC_A_GPIO_Port, ENC_A_Pin);
g_rotationState = g_ttable[g_rotationState & 0xf][pinState];

uint8_t dir = g_rotationState & 0x30;
Expand Down Expand Up @@ -243,3 +266,9 @@ static int getAcceleratedCounter(int increment) {
} // namespace eez

#endif

#if defined(EEZ_PLATFORM_STM32)
extern "C" void Encoder_OnDebounceTimer() {
eez::mcu::encoder::onDebounceTimer();
}
#endif
16 changes: 12 additions & 4 deletions src/third_party/stm32_cubeide/.cproject

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/third_party/stm32_cubeide/.mxproject

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/third_party/stm32_cubeide/Core/Inc/stm32f7xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void ADC_IRQHandler(void);
void EXTI9_5_IRQHandler(void);
void TIM1_UP_TIM10_IRQHandler(void);
void EXTI15_10_IRQHandler(void);
void TIM8_UP_TIM13_IRQHandler(void);
void SDMMC1_IRQHandler(void);
void UART4_IRQHandler(void);
void TIM7_IRQHandler(void);
Expand Down
2 changes: 2 additions & 0 deletions src/third_party/stm32_cubeide/Core/Inc/tim.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim7;
extern TIM_HandleTypeDef htim8;

/* USER CODE BEGIN Private defines */
extern TIM_HandleTypeDef htim12;
Expand All @@ -44,6 +45,7 @@ void MX_TIM3_Init(void);
void MX_TIM4_Init(void);
void MX_TIM6_Init(void);
void MX_TIM7_Init(void);
void MX_TIM8_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

Expand Down
4 changes: 4 additions & 0 deletions src/third_party/stm32_cubeide/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ void SystemClock_Config_R2B4(void)
}

void PSU_IncTick();
void Encoder_OnDebounceTimer();
/* USER CODE END 4 */

/**
Expand All @@ -271,6 +272,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
else if (htim->Instance == TIM7) {
PSU_IncTick();
}
else if (htim->Instance == TIM8) {
Encoder_OnDebounceTimer();
}
/* USER CODE END Callback 1 */
}

Expand Down
15 changes: 15 additions & 0 deletions src/third_party/stm32_cubeide/Core/Src/stm32f7xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extern DMA_HandleTypeDef hdma_spi4_tx;
extern DMA_HandleTypeDef hdma_spi5_rx;
extern DMA_HandleTypeDef hdma_spi5_tx;
extern TIM_HandleTypeDef htim7;
extern TIM_HandleTypeDef htim8;
extern DMA_HandleTypeDef hdma_uart4_rx;
extern UART_HandleTypeDef huart4;
extern TIM_HandleTypeDef htim10;
Expand Down Expand Up @@ -305,6 +306,20 @@ void EXTI15_10_IRQHandler(void)
/* USER CODE END EXTI15_10_IRQn 1 */
}

/**
* @brief This function handles TIM8 update interrupt and TIM13 global interrupt.
*/
void TIM8_UP_TIM13_IRQHandler(void)
{
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 0 */

/* USER CODE END TIM8_UP_TIM13_IRQn 0 */
HAL_TIM_IRQHandler(&htim8);
/* USER CODE BEGIN TIM8_UP_TIM13_IRQn 1 */

/* USER CODE END TIM8_UP_TIM13_IRQn 1 */
}

/**
* @brief This function handles SDMMC1 global interrupt.
*/
Expand Down
72 changes: 72 additions & 0 deletions src/third_party/stm32_cubeide/Core/Src/tim.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim7;
TIM_HandleTypeDef htim8;

/* TIM3 init function */
void MX_TIM3_Init(void)
Expand Down Expand Up @@ -191,6 +192,48 @@ void MX_TIM7_Init(void)
/* USER CODE END TIM7_Init 2 */

}
/* TIM8 init function */
void MX_TIM8_Init(void)
{

/* USER CODE BEGIN TIM8_Init 0 */

/* USER CODE END TIM8_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};

/* USER CODE BEGIN TIM8_Init 1 */

/* USER CODE END TIM8_Init 1 */
htim8.Instance = TIM8;
htim8.Init.Prescaler = 108;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 1000;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM8_Init 2 */

/* USER CODE END TIM8_Init 2 */

}

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{
Expand Down Expand Up @@ -250,6 +293,21 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)

/* USER CODE END TIM7_MspInit 1 */
}
else if(tim_baseHandle->Instance==TIM8)
{
/* USER CODE BEGIN TIM8_MspInit 0 */

/* USER CODE END TIM8_MspInit 0 */
/* TIM8 clock enable */
__HAL_RCC_TIM8_CLK_ENABLE();

/* TIM8 interrupt Init */
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);
/* USER CODE BEGIN TIM8_MspInit 1 */

/* USER CODE END TIM8_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
Expand Down Expand Up @@ -366,6 +424,20 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)

/* USER CODE END TIM7_MspDeInit 1 */
}
else if(tim_baseHandle->Instance==TIM8)
{
/* USER CODE BEGIN TIM8_MspDeInit 0 */

/* USER CODE END TIM8_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM8_CLK_DISABLE();

/* TIM8 interrupt Deinit */
HAL_NVIC_DisableIRQ(TIM8_UP_TIM13_IRQn);
/* USER CODE BEGIN TIM8_MspDeInit 1 */

/* USER CODE END TIM8_MspDeInit 1 */
}
}

/* USER CODE BEGIN 1 */
Expand Down
Loading

0 comments on commit 04cf85c

Please sign in to comment.