You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include "chip_helper.hpp"
extern "C" { static uint8_t g_port_exit_critical_count;
void chip_critical_enter(void) { if (g_port_exit_critical_count == 0) { __disable_irq(); } g_port_exit_critical_count++; } void chip_critical_exit(void) { g_port_exit_critical_count--; if (g_port_exit_critical_count == 0) { __enable_irq(); } }
GPIO_TypeDef* chip_get_gpio(Pin_t pin) { int port = pin >> 4;
switch (port) { case 1: #ifdef GPIOA
return GPIOA; #endif
break; case 2: #ifdef GPIOB
return GPIOB; #endif
break; case 3: #ifdef GPIOC
return GPIOC; #endif
break; case 4: #ifdef GPIOD
return GPIOD; #endif
break;
case 5: #ifdef GPIOE
return GPIOE; #endif
break; case 6: #ifdef GPIOF
return GPIOF; #endif
break; case 7: #ifdef GPIOG
return GPIOG; #endif
break; default: break; } return NULL; } uint16_t chip_get_pinoff(Pin_t pin) { uint16_t pinoff = pin & 0x0F; switch (pinoff) { case 0: return GPIO_PIN_0; case 1: return GPIO_PIN_1; case 2: return GPIO_PIN_2; case 3: return GPIO_PIN_3; case 4: return GPIO_PIN_4; case 5: return GPIO_PIN_5; case 6: return GPIO_PIN_6; case 7: return GPIO_PIN_7; case 8: return GPIO_PIN_8; case 9: return GPIO_PIN_9; case 10: return GPIO_PIN_10; case 11: return GPIO_PIN_11; case 12: return GPIO_PIN_12; case 13: return GPIO_PIN_13; case 14: return GPIO_PIN_14; case 15: return GPIO_PIN_15; default: break; }; return 0; }
uint32_t chip_get_timer_clock_sorce_freq(TIM_HandleTypeDef* tim) { uint32_t timClkFreq = 0; uint32_t pclk1Freq = HAL_RCC_GetPCLK1Freq(); uint32_t pclk2Freq = HAL_RCC_GetPCLK2Freq(); uint32_t sysClkFreq = HAL_RCC_GetSysClockFreq();
uint32_t pFLatency; RCC_ClkInitTypeDef clkconfig; HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
bool isAPB2 = false;
#ifdef TIM1
if (tim->Instance == TIM1) isAPB2 = true; #endif
#ifdef TIM8
if (tim->Instance == TIM8) isAPB2 = true; #endif
#ifdef TIM9
if (tim->Instance == TIM9) isAPB2 = true; #endif
#ifdef TIM10
if (tim->Instance == TIM10) isAPB2 = true; #endif
#ifdef TIM11
if (tim->Instance == TIM11) isAPB2 = true; #endif
if (isAPB2) { if (clkconfig.APB2CLKDivider == RCC_HCLK_DIV1) { timClkFreq = HAL_RCC_GetPCLK2Freq(); } else { timClkFreq = 2 * HAL_RCC_GetPCLK2Freq(); } } else { if (clkconfig.APB1CLKDivider == RCC_HCLK_DIV1) { timClkFreq = HAL_RCC_GetPCLK1Freq(); } else { timClkFreq = 2 * HAL_RCC_GetPCLK1Freq(); } } return timClkFreq; } }
bool chip_calculate_prescaler_and_autoreload_by_expect_freq(uint32_t timerInClk, float infreqhz, uint32_t* prescaler, uint32_t* autoreload) { /**
* @brief �����Ĵ�����ֵ */ float psc_x_arr = timerInClk / infreqhz; uint32_t psc = 0; uint32_t arr = 65534; for (; arr > 2; arr--) { psc = psc_x_arr / arr; if (psc >= 1) { uint32_t tmparr = psc_x_arr / psc; if (tmparr >= 65534) continue; break; } } if (psc == 0) return false; if (arr <= 3) return false; // ��ʱ��һ���ڵķֱ���̫С��
arr = psc_x_arr / psc;
int psc_x_arr_real = arr * psc; float realfreq = timerInClk / psc_x_arr_real;
arr = arr - 1; psc = psc - 1; uint16_t comparevalue = 50 / 100.0 * arr;
*prescaler = psc; *autoreload = arr; return true; }
|