mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
swich PWM lib to LL for g030
This commit is contained in:
parent
7bfce729cb
commit
947934eb4e
@ -24,17 +24,17 @@ void STM32_ADC_platformEnable(PikaObj* self) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (0 != enableClk(pin)) {
|
||||
if (0 != GPIO_enable_clock(pin)) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
return;
|
||||
}
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = getGpioPin(pin);
|
||||
GPIO_InitStruct.Pin = GPIO_get_pin(pin);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
|
||||
/* init ADC */
|
||||
LL_ADC_InitTypeDef ADC_InitStruct = {0};
|
||||
|
@ -9,14 +9,14 @@ void STM32_GPIO_platformDisable(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
char* mode = obj_getStr(self, "mode");
|
||||
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -30,20 +30,20 @@ void STM32_GPIO_platformEnable(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
char* mode = obj_getStr(self, "mode");
|
||||
|
||||
if (0 != enableClk(pin)) {
|
||||
if (0 != GPIO_enable_clock(pin)) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
return;
|
||||
}
|
||||
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -73,14 +73,14 @@ void STM32_GPIO_platformEnable(PikaObj* self) {
|
||||
}
|
||||
void STM32_GPIO_platformLow(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -90,14 +90,14 @@ void STM32_GPIO_platformLow(PikaObj* self) {
|
||||
}
|
||||
void STM32_GPIO_platformHigh(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -108,20 +108,20 @@ void STM32_GPIO_platformHigh(PikaObj* self) {
|
||||
void STM32_GPIO_platformSetMode(PikaObj* self) {
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
char *mode = obj_getStr(self, "mode");
|
||||
if (0 != enableClk(pin)) {
|
||||
if (0 != GPIO_enable_clock(pin)) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
return;
|
||||
}
|
||||
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
@ -144,12 +144,12 @@ void STM32_GPIO_platformSetMode(PikaObj* self) {
|
||||
|
||||
void STM32_GPIO_platformRead(PikaObj *self){
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
@ -158,12 +158,12 @@ void STM32_GPIO_platformRead(PikaObj *self){
|
||||
}
|
||||
|
||||
int STM32_lowLevel_readPin(PikaObj *self, char * pin){
|
||||
GPIO_TypeDef* gpioPort = getGpioPort(pin);
|
||||
GPIO_TypeDef* gpioPort = GPIO_get_Group(pin);
|
||||
if (NULL == gpioPort) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio port.");
|
||||
}
|
||||
uint16_t gpioPin = getGpioPin(pin);
|
||||
uint16_t gpioPin = GPIO_get_pin(pin);
|
||||
if (0 == gpioPin) {
|
||||
obj_setErrorCode(self, 1);
|
||||
obj_setSysOut(self, "[error] not match gpio pin.");
|
||||
|
@ -232,10 +232,10 @@ void STM32_IIC_platformEnable(PikaObj* self) {
|
||||
iic = pikaMalloc(sizeof(pika_IIC_info));
|
||||
obj_setPtr(self, "iic", iic);
|
||||
}
|
||||
iic->SDA_GPIO = getGpioPort(SDApin);
|
||||
iic->SDA_GPIO_Pin = getGpioPin(SDApin);
|
||||
iic->SCL_GPIO = getGpioPort(SCLpin);
|
||||
iic->SCL_GPIO_Pin = getGpioPin(SCLpin);
|
||||
iic->SDA_GPIO = GPIO_get_Group(SDApin);
|
||||
iic->SDA_GPIO_Pin = GPIO_get_pin(SDApin);
|
||||
iic->SCL_GPIO = GPIO_get_Group(SCLpin);
|
||||
iic->SCL_GPIO_Pin = GPIO_get_pin(SCLpin);
|
||||
iic->deviceAddr = deviceAddr;
|
||||
SDA_OUT(iic);
|
||||
}
|
||||
|
@ -4,72 +4,7 @@
|
||||
#include "STM32_common.h"
|
||||
#include "dataStrs.h"
|
||||
|
||||
#ifdef TIM1_EXIST
|
||||
TIM_HandleTypeDef pika_tim1;
|
||||
#endif
|
||||
#ifdef TIM2_EXIST
|
||||
TIM_HandleTypeDef pika_tim2;
|
||||
#endif
|
||||
#ifdef TIM3_EXIST
|
||||
TIM_HandleTypeDef pika_tim3;
|
||||
#endif
|
||||
#ifdef TIM4_EXIST
|
||||
TIM_HandleTypeDef pika_tim4;
|
||||
#endif
|
||||
#ifdef TIM14_EXIST
|
||||
TIM_HandleTypeDef pika_tim14;
|
||||
#endif
|
||||
#ifdef TIM16_EXIST
|
||||
TIM_HandleTypeDef pika_tim16;
|
||||
#endif
|
||||
#ifdef TIM17_EXIST
|
||||
TIM_HandleTypeDef pika_tim17;
|
||||
#endif
|
||||
|
||||
static TIM_HandleTypeDef* getTimHandle(char* pin) {
|
||||
#ifdef TIM1_EXIST
|
||||
if (strEqu("PA8", pin) || strEqu("PA9", pin) || strEqu("PA10", pin) ||
|
||||
strEqu("PA11", pin)) {
|
||||
return &pika_tim1;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM2_EXIST
|
||||
if (strEqu("PA0", pin) || strEqu("PA1", pin) || strEqu("PA2", pin) ||
|
||||
strEqu("PA3", pin)) {
|
||||
return &pika_tim2;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM3_EXIST
|
||||
if (strEqu("PA6", pin) || strEqu("PA7", pin) || strEqu("PB0", pin) ||
|
||||
strEqu("PB1", pin)) {
|
||||
return &pika_tim3;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM4_EXIST
|
||||
if (strEqu("PB6", pin) || strEqu("PB7", pin) || strEqu("PB8", pin) ||
|
||||
strEqu("PB9", pin)) {
|
||||
return &pika_tim3;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM14_EXIST
|
||||
if (strEqu("PA4", pin)) {
|
||||
return &pika_tim14;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM16_EXIST
|
||||
if (strEqu("PD0", pin)) {
|
||||
return &pika_tim16;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM17_EXIST
|
||||
if (strEqu("PD1", pin)) {
|
||||
return &pika_tim17;
|
||||
}
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static TIM_TypeDef* getTimInstance(char* pin) {
|
||||
static TIM_TypeDef* PWM_get_TIM_instance(char* pin) {
|
||||
#ifdef TIM1_EXIST
|
||||
if (strEqu("PA8", pin) || strEqu("PA9", pin) || strEqu("PA10", pin) ||
|
||||
strEqu("PA11", pin)) {
|
||||
@ -112,119 +47,82 @@ static TIM_TypeDef* getTimInstance(char* pin) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#if (defined STM32G030xx) || (defined STM32G070xx)
|
||||
static uint32_t getGPIO_AlternateForTim(TIM_TypeDef* timInstance) {
|
||||
#ifdef TIM1_EXIST
|
||||
if (TIM1 == timInstance) {
|
||||
static uint32_t TIM_get_GPIO_alternate(TIM_TypeDef* TIMx) {
|
||||
if (TIM1 == TIMx) {
|
||||
return GPIO_AF2_TIM1;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM3_EXIST
|
||||
if (TIM3 == timInstance) {
|
||||
if (TIM3 == TIMx) {
|
||||
return GPIO_AF1_TIM3;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM14_EXIST
|
||||
if (TIM14 == timInstance) {
|
||||
if (TIM14 == TIMx) {
|
||||
return GPIO_AF4_TIM14;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM16_EXIST
|
||||
if (TIM16 == timInstance) {
|
||||
if (TIM16 == TIMx) {
|
||||
return GPIO_AF2_TIM16;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM17_EXIST
|
||||
if (TIM17 == timInstance) {
|
||||
if (TIM17 == TIMx) {
|
||||
return GPIO_AF2_TIM17;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void PWM_TimClockEnable(TIM_TypeDef* timInstance) {
|
||||
#ifdef TIM1_EXIST
|
||||
if (TIM1 == timInstance) {
|
||||
__HAL_RCC_TIM1_CLK_ENABLE();
|
||||
static void TIM_clock_enable(TIM_TypeDef* TIMx) {
|
||||
if (TIM1 == TIMx) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM2_EXIST
|
||||
if (TIM2 == timInstance) {
|
||||
__HAL_RCC_TIM2_CLK_ENABLE();
|
||||
if (TIM3 == TIMx) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM3_EXIST
|
||||
if (TIM3 == timInstance) {
|
||||
__HAL_RCC_TIM3_CLK_ENABLE();
|
||||
if (TIM14 == TIMx) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM14);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM4_EXIST
|
||||
if (TIM4 == timInstance) {
|
||||
__HAL_RCC_TIM4_CLK_ENABLE();
|
||||
if (TIM16 == TIMx) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM16);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM14_EXIST
|
||||
if (TIM14 == timInstance) {
|
||||
__HAL_RCC_TIM14_CLK_ENABLE();
|
||||
if (TIM17 == TIMx) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM17);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM16_EXIST
|
||||
if (TIM16 == timInstance) {
|
||||
__HAL_RCC_TIM16_CLK_ENABLE();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef TIM17_EXIST
|
||||
if (TIM17 == timInstance) {
|
||||
__HAL_RCC_TIM17_CLK_ENABLE();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t PWM_MspInit(char* pin) {
|
||||
TIM_TypeDef* timInstance = getTimInstance(pin);
|
||||
if (NULL == timInstance) {
|
||||
uint8_t PWM_GPIO_init(char* pin) {
|
||||
TIM_TypeDef* TIMx = PWM_get_TIM_instance(pin);
|
||||
if (NULL == TIMx) {
|
||||
/* this Pin do not match any PWM generator */
|
||||
return 1;
|
||||
}
|
||||
enableClk(pin);
|
||||
GPIO_enable_clock(pin);
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = getGpioPin(pin);
|
||||
GPIO_InitStruct.Pin = GPIO_get_pin(pin);
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
#if (defined STM32G030xx) || (defined STM32G070xx)
|
||||
GPIO_InitStruct.Alternate = getGPIO_AlternateForTim(timInstance);
|
||||
#endif
|
||||
HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct);
|
||||
PWM_TimClockEnable(timInstance);
|
||||
GPIO_InitStruct.Alternate = TIM_get_GPIO_alternate(TIMx);
|
||||
HAL_GPIO_Init(GPIO_get_Group(pin), &GPIO_InitStruct);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t getTimChennel(char* pin) {
|
||||
uint32_t PWM_get_LL_TIM_channel(char* pin) {
|
||||
if (strEqu("PA8", pin) || strEqu("PA0", pin) || strEqu("PA6", pin) ||
|
||||
strEqu("PB6", pin) || strEqu("PA4", pin) || strEqu("PD0", pin) ||
|
||||
strEqu("PD1", pin)) {
|
||||
return TIM_CHANNEL_1;
|
||||
return LL_TIM_CHANNEL_CH1;
|
||||
}
|
||||
if (strEqu("PA9", pin) || strEqu("PA1", pin) || strEqu("PB7", pin) ||
|
||||
strEqu("PA7", pin)) {
|
||||
return TIM_CHANNEL_2;
|
||||
return LL_TIM_CHANNEL_CH2;
|
||||
}
|
||||
if (strEqu("PA10", pin) || strEqu("PA2", pin) || strEqu("PB8", pin) ||
|
||||
strEqu("PB0", pin)) {
|
||||
return TIM_CHANNEL_3;
|
||||
return LL_TIM_CHANNEL_CH3;
|
||||
}
|
||||
if (strEqu("PA11", pin) || strEqu("PA3", pin) || strEqu("PB9", pin) ||
|
||||
strEqu("PB1", pin)) {
|
||||
return TIM_CHANNEL_4;
|
||||
return LL_TIM_CHANNEL_CH4;
|
||||
}
|
||||
/* Chennel not match */
|
||||
return 99999;
|
||||
@ -234,140 +132,109 @@ void STM32_PWM_platformEnable(PikaObj* self) {
|
||||
float duty = obj_getFloat(self, "duty");
|
||||
int freq = obj_getInt(self, "freq");
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
TIM_OC_InitTypeDef sConfigOC = {0};
|
||||
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
|
||||
TIM_TypeDef* TIMx = PWM_get_TIM_instance(pin);
|
||||
|
||||
if (0 != PWM_MspInit(pin)) {
|
||||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||||
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
||||
LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
|
||||
uint32_t LL_TIM_channel = PWM_get_LL_TIM_channel(pin);
|
||||
/* Peripheral clock enable */
|
||||
TIM_clock_enable(TIMx);
|
||||
|
||||
TIM_InitStruct.Prescaler = 64 - 1;
|
||||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||||
TIM_InitStruct.Autoreload =
|
||||
(uint32_t)((float)(1000 * 1000) / (float)freq) - 1;
|
||||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||||
TIM_InitStruct.RepetitionCounter = 0;
|
||||
LL_TIM_Init(TIMx, &TIM_InitStruct);
|
||||
LL_TIM_DisableARRPreload(TIMx);
|
||||
LL_TIM_OC_EnablePreload(TIMx, LL_TIM_channel);
|
||||
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
||||
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
||||
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
||||
/* calculate pulse by duty and freq */
|
||||
TIM_OC_InitStruct.CompareValue = TIM_InitStruct.Autoreload * duty;
|
||||
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
||||
TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
|
||||
TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
|
||||
TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
|
||||
LL_TIM_OC_Init(TIMx, LL_TIM_channel, &TIM_OC_InitStruct);
|
||||
LL_TIM_OC_DisableFast(TIMx, LL_TIM_channel);
|
||||
LL_TIM_SetTriggerOutput(TIMx, LL_TIM_TRGO_RESET);
|
||||
LL_TIM_SetTriggerOutput2(TIMx, LL_TIM_TRGO2_RESET);
|
||||
LL_TIM_DisableMasterSlaveMode(TIMx);
|
||||
TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
|
||||
TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
|
||||
TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
|
||||
TIM_BDTRInitStruct.DeadTime = 0;
|
||||
TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
|
||||
TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
|
||||
TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
|
||||
TIM_BDTRInitStruct.BreakAFMode = LL_TIM_BREAK_AFMODE_INPUT;
|
||||
TIM_BDTRInitStruct.Break2State = LL_TIM_BREAK2_DISABLE;
|
||||
TIM_BDTRInitStruct.Break2Polarity = LL_TIM_BREAK2_POLARITY_HIGH;
|
||||
TIM_BDTRInitStruct.Break2Filter = LL_TIM_BREAK2_FILTER_FDIV1;
|
||||
TIM_BDTRInitStruct.Break2AFMode = LL_TIM_BREAK_AFMODE_INPUT;
|
||||
TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
|
||||
LL_TIM_BDTR_Init(TIMx, &TIM_BDTRInitStruct);
|
||||
/* init gpio */
|
||||
if (0 != PWM_GPIO_init(pin)) {
|
||||
obj_setSysOut(self, "[error]: init PWM port faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
|
||||
if (NULL == pika_tim) {
|
||||
obj_setSysOut(self, "[error]: can not found PWM hardware.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
/* start */
|
||||
LL_TIM_CC_EnableChannel(TIMx, LL_TIM_channel);
|
||||
LL_TIM_EnableCounter(TIMx);
|
||||
LL_TIM_EnableAllOutputs(TIMx);
|
||||
}
|
||||
|
||||
pika_tim->Instance = getTimInstance(pin);
|
||||
#if (defined STM32G030xx) || (defined STM32G070xx)
|
||||
pika_tim->Init.Prescaler = 64 - 1;
|
||||
#endif
|
||||
#if (defined STM32F103xB)
|
||||
pika_tim->Init.Prescaler = 72 - 1;
|
||||
#endif
|
||||
pika_tim->Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
/* calculate period */
|
||||
pika_tim->Init.Period = (uint32_t)((float)(1000 * 1000) / (float)freq) - 1;
|
||||
pika_tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
pika_tim->Init.RepetitionCounter = 0;
|
||||
pika_tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
||||
|
||||
if (HAL_TIM_Base_Init(pika_tim) != HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
void PWM_set_duty(TIM_TypeDef* TIMx, uint32_t LL_TIM_channel, float duty) {
|
||||
if (LL_TIM_CHANNEL_CH1 == LL_TIM_channel) {
|
||||
LL_TIM_OC_SetCompareCH1(TIMx, LL_TIM_GetAutoReload(TIMx) * duty);
|
||||
return;
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(pika_tim, &sClockSourceConfig) != HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
if (LL_TIM_CHANNEL_CH2 == LL_TIM_channel) {
|
||||
LL_TIM_OC_SetCompareCH2(TIMx, LL_TIM_GetAutoReload(TIMx) * duty);
|
||||
return;
|
||||
}
|
||||
if (HAL_TIM_PWM_Init(pika_tim) != HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
if (LL_TIM_CHANNEL_CH3 == LL_TIM_channel) {
|
||||
LL_TIM_OC_SetCompareCH3(TIMx, LL_TIM_GetAutoReload(TIMx) * duty);
|
||||
return;
|
||||
}
|
||||
if (HAL_TIM_OC_Init(pika_tim) != HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
if (LL_TIM_CHANNEL_CH4 == LL_TIM_channel) {
|
||||
LL_TIM_OC_SetCompareCH4(TIMx, LL_TIM_GetAutoReload(TIMx) * duty);
|
||||
return;
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
#if (defined STM32G030xx) || (defined STM32G070xx)
|
||||
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
|
||||
#endif
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(pika_tim, &sMasterConfig) !=
|
||||
HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
/* calculate pulse by duty and freq */
|
||||
sConfigOC.Pulse = (uint32_t)(pika_tim->Init.Period * duty);
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
|
||||
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
||||
if (HAL_TIM_PWM_ConfigChannel(pika_tim, &sConfigOC, getTimChennel(pin)) !=
|
||||
HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
#if (defined STM32G030xx) || (defined STM32G070xx)
|
||||
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;
|
||||
#endif
|
||||
if (HAL_TIMEx_ConfigBreakDeadTime(pika_tim, &sBreakDeadTimeConfig) !=
|
||||
HAL_OK) {
|
||||
obj_setSysOut(self, "[error]: init PWM faild.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
HAL_TIM_PWM_Start(pika_tim, getTimChennel(pin));
|
||||
}
|
||||
|
||||
void STM32_PWM_platformSetDuty(PikaObj* self) {
|
||||
float duty = obj_getFloat(self, "duty");
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
|
||||
if (NULL == pika_tim) {
|
||||
uint32_t LL_TIM_channel = PWM_get_LL_TIM_channel(pin);
|
||||
TIM_TypeDef* TIMx = PWM_get_TIM_instance(pin);
|
||||
if (NULL == TIMx) {
|
||||
obj_setSysOut(self, "[error]: can not found PWM hardware.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
/* update duty in run time */
|
||||
if (NULL != pika_tim->Instance) {
|
||||
__HAL_TIM_SET_COMPARE(pika_tim, getTimChennel(pin),
|
||||
(uint32_t)(pika_tim->Init.Period * duty));
|
||||
}
|
||||
PWM_set_duty(TIMx, LL_TIM_channel, duty);
|
||||
}
|
||||
|
||||
void STM32_PWM_platformSetFrequency(PikaObj* self) {
|
||||
int freq = obj_getInt(self, "freq");
|
||||
char* pin = obj_getStr(self, "pin");
|
||||
TIM_HandleTypeDef* pika_tim = getTimHandle(pin);
|
||||
if (NULL == pika_tim) {
|
||||
TIM_TypeDef* TIMx = PWM_get_TIM_instance(pin);
|
||||
if (NULL == TIMx) {
|
||||
obj_setSysOut(self, "[error]: can not found PWM hardware.");
|
||||
obj_setErrorCode(self, 1);
|
||||
return;
|
||||
}
|
||||
/* update frequency in run time */
|
||||
if (NULL != pika_tim->Instance) {
|
||||
__HAL_TIM_SET_AUTORELOAD(
|
||||
pika_tim, (uint32_t)((float)(1000 * 1000) / (float)freq) - 1);
|
||||
LL_TIM_SetAutoReload(TIMx,
|
||||
(uint32_t)((float)(1000 * 1000) / (float)freq) - 1);
|
||||
float duty = obj_getFloat(self, "duty");
|
||||
__HAL_TIM_SET_COMPARE(pika_tim, getTimChennel(pin),
|
||||
(uint32_t)(pika_tim->Init.Period * duty));
|
||||
}
|
||||
uint32_t LL_TIM_channel = PWM_get_LL_TIM_channel(pin);
|
||||
PWM_set_duty(TIMx, LL_TIM_channel, duty);
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ void delay_us(uint32_t udelay) {
|
||||
}
|
||||
}
|
||||
|
||||
GPIO_TypeDef* getGpioPort(char* pin) {
|
||||
GPIO_TypeDef* GPIO_get_Group(char* pin) {
|
||||
if (strIsStartWith(pin, "PA")) {
|
||||
return GPIOA;
|
||||
}
|
||||
@ -62,7 +62,7 @@ GPIO_TypeDef* getGpioPort(char* pin) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint16_t getGpioPin(char* pin) {
|
||||
uint16_t GPIO_get_pin(char* pin) {
|
||||
Args* buffs = New_strBuff();
|
||||
uint16_t gpioPin = 0;
|
||||
|
||||
@ -147,7 +147,7 @@ uint32_t getPinMode(char* mode) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t enableClk(char* pin) {
|
||||
uint8_t GPIO_enable_clock(char* pin) {
|
||||
if (strIsStartWith(pin, "PA")) {
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
return 0;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#ifndef __STM32__COMMON__H
|
||||
#define __STM32__COMMON__H
|
||||
#include "PikaObj.h"
|
||||
#include "main.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* support std lib for stm32 */
|
||||
@ -79,10 +80,10 @@ typedef struct {
|
||||
PikaObj* obj;
|
||||
} pika_uart_t;
|
||||
|
||||
GPIO_TypeDef* getGpioPort(char* pin);
|
||||
uint16_t getGpioPin(char* pin);
|
||||
GPIO_TypeDef* GPIO_get_Group(char* pin);
|
||||
uint16_t GPIO_get_pin(char* pin);
|
||||
uint32_t getPinMode(char* mode);
|
||||
uint8_t enableClk(char* pin);
|
||||
uint8_t GPIO_enable_clock(char* pin);
|
||||
void delay_us(uint32_t delay);
|
||||
void delay_unit(uint32_t delay);
|
||||
void STM32_UART_clearRxBuff(pika_uart_t* pika_uart);
|
||||
|
Loading…
x
Reference in New Issue
Block a user