WL55JC_AS923/Core/Src/tim.c

468 lines
12 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
#include "main.h"
#include "stdio.h"
#include "ev1527.h"
/* USER CODE BEGIN 0 */
extern TIM_HandleTypeDef htim1, htim2;
// DMA_HandleTypeDef hdma_tim1_ch1; //PA8 WS2812B
//DMA_HandleTypeDef hdma_tim1_ch2; //PA9 WS2812B
// TIM_HandleTypeDef htim1;
/* USER CODE BEGIN PV */
#if 0
extern volatile uint32_t capture_Buf[3]; // counter
extern volatile uint8_t capture_Cnt; // state
extern volatile uint32_t high_time, low_time; // high level duration, low level duration
/* Captured Value */
extern uint32_t uwIC2Value;
/* Duty Cycle Value */
extern uint32_t uwDutyCycle;
/* Frequency Value */
extern uint32_t uwFrequency;
#endif
uint8_t bit=0;
#if 0
/* Captured Values */
extern uint32_t uwIC2Value1;
extern uint32_t uwIC2Value2;
extern uint32_t uwDiffCapture;
/* Capture index */
extern uint16_t uhCaptureIndex;
/* Frequency Value */
extern uint32_t uwFrequency;
#endif
/* USER CODE END PV */
/* USER CODE END 0 */
#if 0
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
//TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = TIM1_PRESCALER_VALUE;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 9*TIM1_PERIOD_VALUE; // TIM1_PERIOD_VALUE; // 0xffff; // TIM1_PERIOD_VALUE;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
#if 1
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
#endif
#if 1
if (HAL_TIM_IC_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
#endif
#if 0
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
#endif
#if 1
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
#endif
//sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
// sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
{
printf("\r\n tim1 ch1 falling config error \r\n");
Error_Handler();
}
#if 1
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
printf("\r\n tim1 ch2 rising config error \r\n");
Error_Handler();
}
#endif
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
#endif
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = PRESCALER_VALUE;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = PERIOD_VALUE;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
#if 1
#define EV1527_H4 328 // NARROW PULSE WIDTH us, varies on different remote control
#define EV1527_H4_MIN (EV1527_H4 * 90 / 100)
#define EV1527_H4_MAX (EV1527_H4 * 110 / 100)
#define EV1527_H12 1100 // 998 // WIDE PULSE WIDTH us, varies on different remote control
#define EV1527_H12_MIN (EV1527_H12 * 90 / 100)
#define EV1527_H12_MAX (EV1527_H12 * 110 / 100)
#define EV1527_L4 EV1527_H4 // WIDE PULSE WIDTH us
#define EV1527_L4_MIN (EV1527_L4 * 90 / 100)
#define EV1527_L4_MAX (EV1527_L4 * 110 / 100)
#define EV1527_L12 EV1527_H12 // WIDE PULSE WIDTH us
#define EV1527_L12_MIN (EV1527_L12 * 90 / 100)
#define EV1527_L12_MAX (EV1527_L12 * 110 / 100)
#define EV1527_SYN_L 11981-328 // SYNC PULSE LOW WIDTH us, HIGH TIME=EV1527_H4
#define EV1527_SYN_MIN (EV1527_L12 * 90 / 100)
#define EV1527_SYN_MAX (EV1527_L12 * 110 / 100)
static uint32_t highCnt =0, lowCnt =0; //pulse high and low duration
static uint32_t syn =0, code =0, pulseCnt =0; // sync, parsed code, bit cnt used in decoding process
void EV1527Reset(void)
{
highCnt = 0;
lowCnt = 0;
syn = 0;
code = 0;
pulseCnt= 0;
}
void EV1527Decode(uint32_t v)
{
code <<= 1;
if (v)
{
code |= 1;
}
pulseCnt ++;
//printf("c: %ld, p= %ld \r\n", v, pulseCnt);
if(pulseCnt == 24)
{
// 1. same valid code received more than 1 times
// 2. always same code all the time, send out/up once
// 3. some long press application, say tune up light, +++ send up/out
// 4. send up/out via message queue
printf("Addr: %ld, code= %ld \r\n", code >> 4, code&0x000000FF);
EV1527Reset();
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
#if 0
if (TIM1 == htim->Instance)
{
printf(".");
} else
if (TIM2 == htim->Instance)
#endif
{
RF_Signal_Decode();
}
}
#endif
#if 0
/* TIM1 init function */
void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 48 -1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xffff -1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;//TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; //TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
#if 1
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
#endif
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspInit 0 */
/* USER CODE END TIM1_MspInit 0 */
/* TIM1 clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* TIM1 DMA Init */
/* TIM1_CH1 Init */
hdma_tim1_ch1.Instance = DMA1_Channel1;
hdma_tim1_ch1.Init.Request = DMA_REQUEST_TIM1_CH1;
hdma_tim1_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim1_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim1_ch1.Init.MemInc = DMA_MINC_ENABLE;
hdma_tim1_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tim1_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; //DMA_MDATAALIGN_HALFWORD; //DMA_MDATAALIGN_HALFWORD;
hdma_tim1_ch1.Init.Mode = DMA_CIRCULAR;
hdma_tim1_ch1.Init.Priority = DMA_PRIORITY_HIGH; //was HIGH
if (HAL_DMA_Init(&hdma_tim1_ch1) != HAL_OK)
{
Error_Handler();
}
#ifdef STM32WL55xx
if (HAL_DMA_ConfigChannelAttributes(&hdma_tim1_ch1, DMA_CHANNEL_NPRIV) != HAL_OK)
{
Error_Handler();
}
#endif
__HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_CC1],hdma_tim1_ch1);
/* USER CODE BEGIN TIM1_MspInit 1 */
/* USER CODE END TIM1_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspPostInit 0 */
/* USER CODE END TIM1_MspPostInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM1 GPIO Configuration
PA8 ------> TIM1_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP; //GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM1_MspPostInit 1 */
/* USER CODE END TIM1_MspPostInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspDeInit 0 */
/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* TIM1 DMA DeInit */
HAL_DMA_DeInit(tim_baseHandle->hdma[TIM_DMA_ID_CC1]);
/* USER CODE BEGIN TIM1_MspDeInit 1 */
/* USER CODE END TIM1_MspDeInit 1 */
}
}
#endif
/* 获取高电平的时间 */
uint8_t Get_Pulse_Time(void)
{
uint8_t time = 0;
while( RF_Receive_Data_In() )
{
time ++;
HAL_Delay_Us(1);
if(time == 250)
return time; // 超时溢出
}
return time;
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */