From f85beb93ea738ae0e857e266a3f4fd6d9c2b3bdd Mon Sep 17 00:00:00 2001 From: YunHorn Technology Date: Tue, 25 Feb 2025 20:48:59 +0800 Subject: [PATCH] park here for RC --- Core/Inc/tim.h | 51 ++++ Core/Src/tim.c | 671 ++++++++++++++++++++++++++++++++++++++++++++ Core/Src/user_tim.c | 9 + 3 files changed, 731 insertions(+) create mode 100644 Core/Inc/tim.h create mode 100644 Core/Src/tim.c create mode 100644 Core/Src/user_tim.c diff --git a/Core/Inc/tim.h b/Core/Inc/tim.h new file mode 100644 index 0000000..efd1057 --- /dev/null +++ b/Core/Inc/tim.h @@ -0,0 +1,51 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file tim.h + * @brief This file contains all the function prototypes for + * the tim.c file + ****************************************************************************** + * @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 */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __TIM_H__ +#define __TIM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ +extern TIM_HandleTypeDef htim1; +/* USER CODE END Private defines */ + + +/* USER CODE BEGIN Prototypes */ +void MX_TIM1_Init(void); +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIM_H__ */ + diff --git a/Core/Src/tim.c b/Core/Src/tim.c new file mode 100644 index 0000000..7e86e12 --- /dev/null +++ b/Core/Src/tim.c @@ -0,0 +1,671 @@ +/* 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; + +// DMA_HandleTypeDef hdma_tim1_ch1; //PA8 WS2812B + +//DMA_HandleTypeDef hdma_tim1_ch2; //PA9 WS2812B + +// TIM_HandleTypeDef htim1; + +/* USER CODE BEGIN PV */ +#if 1 + +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 1 +/* 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 */ + +/** + * @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 = 0xffff; // 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 0 + if (HAL_TIM_Base_Init(&htim1) != HAL_OK) + { + Error_Handler(); + } +#endif + if (HAL_TIM_IC_Init(&htim1) != HAL_OK) + { + Error_Handler(); + } +#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 */ + +} + +#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_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + static uint32_t uwICValue; + static uint32_t last_uwICValue; + uint32_t uwDiffCapture; + static uint32_t highCnt=0, lowCnt=0; + static uint8_t sync=0; + + //printf(" cc "); + // RF_Signal_Decode(); + + //if (TIM1 == htim->Instance) + { + //if ((htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)) + { + if (HAL_GPIO_ReadPin(RF_Receive_GPIO_Port,RF_Receive_GPIO_Pin)== GPIO_PIN_RESET) + { // Falling edge + //printf(">"); + highCnt = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2); // get current value + } + else if ((HAL_GPIO_ReadPin(RF_Receive_GPIO_Port,RF_Receive_GPIO_Pin) == GPIO_PIN_SET)) + { // Rising edge + lowCnt = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2); // get current value + if (syn == 1) + { // sync then decode + if ((lowCnt > EV1527_L4_MIN) && (highCnt < EV1527_L4_MAX)) + { + + if ((lowCnt > EV1527_H12_MIN) && (highCnt < EV1527_H12_MAX)) + { // short pulse, there must be a valid long pulse, data=1, otherwise error + EV1527Decode(1); + } + else + { + EV1527Reset(); // error handling + } + } + } + else if ((lowCnt > EV1527_L12_MIN) && (lowCnt < EV1527_L12_MAX)) + { + if ((highCnt > EV1527_H4_MIN) && (highCnt < EV1527_H4_MAX)) + { // long pulse, there must be a valid short pulse, data =0, otherwise error + EV1527Decode(0); + } + else + { + EV1527Reset(); // error handling + } + } + } + else + { + if ((lowCnt > EV1527_SYN_MIN) && (lowCnt < EV1527_SYN_MAX)) + { // sync feature Low level + if ((highCnt > EV1527_H4_MIN) && (highCnt < EV1527_H4_MAX)) + { + // and there must be a valid sync high pulse, sync starting (may also be error code) + syn = 1; + pulseCnt = 0; + code = 0; + //printf("sync: %d \r\n", lowCnt); + } + } + } + } + } +} + +#endif + +/** + * @brief Input Capture callback in non blocking mode + * @param htim : TIM IC handle + * @retval None + */ +#if 0 +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + static uint32_t uwICValue; + static uint32_t last_uwICValue; + uint32_t uwDiffCapture; + // printf(" cc "); + // RF_Signal_Decode(); + + if (TIM1 == htim->Instance) + { + // if ((htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)||(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)) + // if ((htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)) + { + switch(capture_Cnt) + { + case 1: + capture_Buf[0] = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2); // get current value + //printf("B0:%ld ", capture_Buf[0]); + //printf("/"); + TIM_SET_CAPTUREPOLARITY(&htim1, TIM_CHANNEL_2, TIM_ICPOLARITY_FALLING); // set falling edge capture + capture_Cnt ++; + break; + case 2: + capture_Buf[1] = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2); // get current value + //printf("B1:%ld ", capture_Buf[1]); + //printf("_"); + //HAL_TIM_IC_Stop_IT(&htim1, TIM_CHANNEL_2); // stop capture or _HAL_TIM_DISABLE(&htim1); + TIM_SET_CAPTUREPOLARITY(&htim1, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING); // set falling edge capture + capture_Cnt ++; + case 3: + capture_Buf[2] = HAL_TIM_ReadCapturedValue(&htim1, TIM_CHANNEL_2); // get current value + //printf("B2:%ld ", capture_Buf[2]); + printf(")"); + HAL_TIM_IC_Stop_IT(&htim1, TIM_CHANNEL_2); // stop capture or _HAL_TIM_DISABLE(&htim1); + //TIM_SET_CAPTUREPOLARITY(&htim1, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING); // set falling edge capture + // capture_Cnt ++; + break; + + + } + // RF_Signal_Decode(); + } + } + +} +#endif + +#if 0 +/* USER CODE BEGIN 4 */ +/** + * @brief Input capture callback in non blocking mode + * @param htim : htim handle + * @retval None + */ +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) + { + if(uhCaptureIndex == 0) + { + /* Get the 1st Input Capture value */ + uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + uhCaptureIndex = 1; + printf("Capture_state=%d \r\n",uhCaptureIndex); + } + else if(uhCaptureIndex == 1) + { + /* Get the 2nd Input Capture value */ + uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + + /* Capture computation */ + if (uwIC2Value2 > uwIC2Value1) + { + uwDiffCapture = (uwIC2Value2 - uwIC2Value1); + } + else if (uwIC2Value2 < uwIC2Value1) + { + /* 0xFFFF is max TIM1_CCRx value */ + uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2) + 1; + } + else + { + /* If capture values are equal, we have reached the limit of frequency + measures */ + Error_Handler(); + } +#if 1 + /* Frequency computation: for this example TIMx (TIM1) is clocked by + APB2Clk */ + uwFrequency = HAL_RCC_GetPCLK2Freq() / uwDiffCapture; + + printf("diff=%ld uwF=%ld Hz\r\n", uwDiffCapture, uwFrequency); + // uhCaptureIndex = 0; +#endif + if ((uwDiffCapture > 100) && (uwDiffCapture < 300)) + { + uhCaptureIndex = 2; + printf("Capture_state=%d \r\n",uhCaptureIndex); + } + } else if (uhCaptureIndex ==2) + { + /* Get the 2nd Input Capture value */ + uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + uhCaptureIndex = 3; + } else if (uhCaptureIndex ==3) + { + uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + + /* Capture computation */ + if (uwIC2Value2 > uwIC2Value1) + { + uwDiffCapture = (uwIC2Value2 - uwIC2Value1); + } + else if (uwIC2Value2 < uwIC2Value1) + { + /* 0xFFFF is max TIM1_CCRx value */ + uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2) + 1; + } + else + { + /* If capture values are equal, we have reached the limit of frequency + measures */ + Error_Handler(); + } + + if ((uwDiffCapture > 300) && (uwDiffCapture < 500)) + { + bit0_high =1; + } else if ((uwDiffCapture > 500) && (uwDiffCapture < 1100)) + { + bit0_low =1; + } + + uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + uhCaptureIndex = 2; + + } + } +} +#endif + +#if 0 +/** + * @brief Input Capture callback in non blocking mode + * @param htim : TIM IC handle + * @retval None + */ +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) + { + /* Get the Input Capture value */ + uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + + if (uwIC2Value != 0) + { + /* Duty cycle computation */ + // uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value; + uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2) - uwIC2Value) * 100) / uwIC2Value; + if ( uwDutyCycle > 70) + { + bit = 1; + } else if (uwDutyCycle < 30) + { + bit = 0; + } + printf("%2d ", bit); + /* uwFrequency computation + TIM1 counter clock = (System Clock) */ + // uwFrequency = ( HAL_RCC_GetSysClockFreq() ) / uwIC2Value; + // printf("f=%ld duty=%ld \r\n", uwFrequency, uwDutyCycle); + } + else + { + uwDutyCycle = 0; + uwFrequency = 0; + } + } +} + +#endif + +#if 0 +/** + * @brief Input Capture callback in non blocking mode + * @param htim : TIM IC handle + * @retval None + */ +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) + { + /* Get the Input Capture value */ + uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); + + if (uwIC2Value != 0) + { + /* Duty cycle computation */ + uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value; + + /* uwFrequency computation + TIM1 counter clock = (System Clock) */ + uwFrequency = ( HAL_RCC_GetSysClockFreq() ) / uwIC2Value; + printf("f=%ld duty=%ld \r\n", uwFrequency, uwDutyCycle); + } + else + { + uwDutyCycle = 0; + uwFrequency = 0; + } + } +} + +#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 + + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Core/Src/user_tim.c b/Core/Src/user_tim.c new file mode 100644 index 0000000..5bd00a8 --- /dev/null +++ b/Core/Src/user_tim.c @@ -0,0 +1,9 @@ + +#include "stm32wlxx.h" +#include "timer.h" +#include "delay.h" + +void TIM6_NVIC_Init() +{ + NVIC_InitStructure.NVIC_InitStructre; +} \ No newline at end of file