diff --git a/package/STM32F1/STM32F1_ADC.c b/package/STM32F1/STM32F1_ADC.c new file mode 100644 index 000000000..0fa81c775 --- /dev/null +++ b/package/STM32F1/STM32F1_ADC.c @@ -0,0 +1,147 @@ +#include +#include "BaseObj.h" +#include "STM32F1_common.h" +#include "dataStrs.h" + +ADC_HandleTypeDef pika_hadc1 = {0}; + +uint16_t Get_Adc(ADC_HandleTypeDef* hadc, uint32_t ch) { + ADC_ChannelConfTypeDef ADC_ChanConf; + ADC_ChanConf.Channel = ch; + ADC_ChanConf.Rank = ADC_REGULAR_RANK_1; +#ifdef STM32F103xB + ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; +#endif + HAL_ADC_ConfigChannel(hadc, &ADC_ChanConf); + HAL_ADC_Start(hadc); + HAL_ADC_PollForConversion(hadc, 10); + return (uint16_t)HAL_ADC_GetValue(hadc); +} + +void STM32F1_ADC_platformEnable(PikaObj* self) { + char *pin = obj_getStr(self, "pin"); + if (!strIsStartWith(pin, "PA")) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] not match adc pin."); + return; + } + +/* MSP Init */ + +#ifdef STM32F103xB + __HAL_RCC_ADC1_CLK_ENABLE(); +#endif + if (0 != enableClk(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.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct); + + /* init ADC */ + pika_hadc1.Instance = ADC1; + pika_hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + pika_hadc1.Init.ContinuousConvMode = DISABLE; + pika_hadc1.Init.DiscontinuousConvMode = DISABLE; + pika_hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + pika_hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + pika_hadc1.Init.NbrOfConversion = 1; + + HAL_StatusTypeDef state = HAL_ADC_Init(&pika_hadc1); + if (state != HAL_OK) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] adc init faild."); + return; + } + + /* Run the ADC calibration */ + if (HAL_ADCEx_Calibration_Start(&pika_hadc1) != HAL_OK) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] adc calibratie faild."); + return; + } +} + +uint32_t getChannel(char* pin) { + Args* buffs = New_strBuff(); + uint32_t channel = 0; + + pin = strsCopy(buffs, pin + 2); + if (strEqu(pin, "0")) { + channel = ADC_CHANNEL_0; + goto exit; + } + if (strEqu(pin, "1")) { + channel = ADC_CHANNEL_1; + goto exit; + } + if (strEqu(pin, "2")) { + channel = ADC_CHANNEL_2; + goto exit; + } + if (strEqu(pin, "3")) { + channel = ADC_CHANNEL_3; + goto exit; + } + if (strEqu(pin, "4")) { + channel = ADC_CHANNEL_4; + goto exit; + } + if (strEqu(pin, "5")) { + channel = ADC_CHANNEL_5; + goto exit; + } + if (strEqu(pin, "6")) { + channel = ADC_CHANNEL_6; + goto exit; + } + if (strEqu(pin, "7")) { + channel = ADC_CHANNEL_7; + goto exit; + } + if (strEqu(pin, "8")) { + channel = ADC_CHANNEL_8; + goto exit; + } + if (strEqu(pin, "9")) { + channel = ADC_CHANNEL_9; + goto exit; + } + if (strEqu(pin, "10")) { + channel = ADC_CHANNEL_10; + goto exit; + } + if (strEqu(pin, "11")) { + channel = ADC_CHANNEL_11; + goto exit; + } + if (strEqu(pin, "12")) { + channel = ADC_CHANNEL_12; + goto exit; + } + if (strEqu(pin, "13")) { + channel = ADC_CHANNEL_13; + goto exit; + } + if (strEqu(pin, "14")) { + channel = ADC_CHANNEL_14; + goto exit; + } + if (strEqu(pin, "15")) { + channel = ADC_CHANNEL_15; + goto exit; + } +exit: + args_deinit(buffs); + return channel; +} + +void STM32F1_ADC_platformRead(PikaObj* self) { + char *pin = obj_getStr(self, "pin"); + obj_setFloat(self, "val", 3.3f * Get_Adc(&pika_hadc1, getChannel(pin)) / 4096.0f); +} \ No newline at end of file diff --git a/package/STM32F1/STM32F1_IIC.c b/package/STM32F1/STM32F1_IIC.c new file mode 100644 index 000000000..5cf02e349 --- /dev/null +++ b/package/STM32F1/STM32F1_IIC.c @@ -0,0 +1,256 @@ +#include +#include "BaseObj.h" +#include "STM32F1_common.h" +#include "dataStrs.h" + +typedef struct pika_IIC_info_t { + GPIO_TypeDef* SCL_GPIO; + GPIO_TypeDef* SDA_GPIO; + + uint32_t SCL_GPIO_Pin; + uint32_t SDA_GPIO_Pin; + uint8_t deviceAddr; + + uint8_t readBuff[32]; +} pika_IIC_info; + +#define delay_rate 1 + +void SDA_OUT(pika_IIC_info* iic) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + /*Configure GPIO*/ + GPIO_InitStruct.Pin = iic->SDA_GPIO_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(iic->SDA_GPIO, &GPIO_InitStruct); +} + +void SDA_IN(pika_IIC_info* iic) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + /*Configure GPIO*/ + GPIO_InitStruct.Pin = iic->SDA_GPIO_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(iic->SDA_GPIO, &GPIO_InitStruct); +} + +void IIC_SDA_high(pika_IIC_info* iic) { + HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, GPIO_PIN_SET); +} + +void IIC_SDA_low(pika_IIC_info* iic) { + HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, GPIO_PIN_RESET); +} + +void IIC_SCL_high(pika_IIC_info* iic) { + HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_SET); +} + +void IIC_SCL_low(pika_IIC_info* iic) { + HAL_GPIO_WritePin(iic->SCL_GPIO, iic->SCL_GPIO_Pin, GPIO_PIN_RESET); +} + +uint8_t READ_SDA(pika_IIC_info* iic) { + return HAL_GPIO_ReadPin(iic->SDA_GPIO, iic->SDA_GPIO_Pin); +} + +void WRITE_SDA(pika_IIC_info* iic, uint8_t data) { + HAL_GPIO_WritePin(iic->SDA_GPIO, iic->SDA_GPIO_Pin, data); +} + +void IIC_Start(pika_IIC_info* iic) { + SDA_OUT(iic); + IIC_SDA_high(iic); + IIC_SCL_high(iic); + delay_us(delay_rate * 4); + IIC_SDA_low(iic); + delay_us(delay_rate * 4); + IIC_SCL_low(iic); +} + +void IIC_Stop(pika_IIC_info* iic) { + SDA_OUT(iic); + IIC_SCL_low(iic); + IIC_SDA_low(iic); + delay_us(delay_rate * 4); + IIC_SCL_high(iic); + IIC_SDA_high(iic); + delay_us(delay_rate * 4); +} + +uint8_t IIC_Wait_Ack(pika_IIC_info* iic) { + uint8_t ucErrTime = 0; + SDA_IN(iic); + + IIC_SDA_high(iic); + delay_us(delay_rate * 1); + IIC_SCL_high(iic); + delay_us(delay_rate * 1); + while (READ_SDA(iic)) { + ucErrTime++; + if (ucErrTime > 250) { + IIC_Stop(iic); + return 1; + } + } + IIC_SCL_low(iic); + return 0; +} + +void IIC_Ack(pika_IIC_info* iic) { + IIC_SCL_low(iic); + SDA_OUT(iic); + + IIC_SDA_low(iic); + delay_us(delay_rate * 2); + IIC_SCL_high(iic); + delay_us(delay_rate * 2); + IIC_SCL_low(iic); +} + +void IIC_NAck(pika_IIC_info* iic) { + IIC_SCL_low(iic); + SDA_OUT(iic); + + IIC_SDA_high(iic); + delay_us(delay_rate * 2); + IIC_SCL_high(iic); + delay_us(delay_rate * 2); + IIC_SCL_low(iic); +} + +void IIC_Send_Byte(pika_IIC_info* iic, uint8_t txd) { + uint8_t t; + SDA_OUT(iic); + + IIC_SCL_low(iic); + for (t = 0; t < 8; t++) { + WRITE_SDA(iic, (txd & 0x80) >> 7); + txd <<= 1; + delay_us(delay_rate * 2); + IIC_SCL_high(iic); + delay_us(delay_rate * 2); + IIC_SCL_low(iic); + delay_us(delay_rate * 2); + } +} + +uint8_t IIC_Read_Byte(pika_IIC_info* iic, unsigned char ack) { + unsigned char i, receive = 0; + SDA_IN(iic); + + for (i = 0; i < 8; i++) { + IIC_SCL_low(iic); + delay_us(delay_rate * 2); + IIC_SCL_high(iic); + receive <<= 1; + if (READ_SDA(iic)) + receive++; + delay_us(delay_rate * 1); + } + if (!ack) + IIC_NAck(iic); + else + IIC_Ack(iic); + return receive; +} + +uint8_t MPU_Write_Len(pika_IIC_info* iic, + uint8_t addr, + uint8_t reg, + uint8_t len, + uint8_t* buf) { + uint8_t i; + IIC_Start(iic); + IIC_Send_Byte(iic, (addr << 1) | 0); + if (IIC_Wait_Ack(iic)) { + IIC_Stop(iic); + return 1; + } + IIC_Send_Byte(iic, reg); + IIC_Wait_Ack(iic); + for (i = 0; i < len; i++) { + IIC_Send_Byte(iic, buf[i]); + if (IIC_Wait_Ack(iic)) { + IIC_Stop(iic); + return 1; + } + } + IIC_Stop(iic); + return 0; +} + +uint8_t MPU_Read_Len(pika_IIC_info* iic, + uint8_t addr, + uint8_t reg, + uint8_t len, + uint8_t* buf) { + IIC_Start(iic); + IIC_Send_Byte(iic, (addr << 1) | 0); + if (IIC_Wait_Ack(iic)) { + IIC_Stop(iic); + return 1; + } + IIC_Send_Byte(iic, reg); + IIC_Wait_Ack(iic); + IIC_Start(iic); + IIC_Send_Byte(iic, (addr << 1) | 1); + IIC_Wait_Ack(iic); + while (len) { + if (len == 1) + *buf = IIC_Read_Byte(iic, 0); + else + *buf = IIC_Read_Byte(iic, 1); + len--; + buf++; + } + IIC_Stop(iic); + return 0; +} + +void STM32F1_IIC_platformEnable(PikaObj* self) { + obj_run(self, "SCL.init()"); + obj_run(self, "SDA.init()"); + obj_run(self, "SCL.setPin(SCLpin)"); + obj_run(self, "SDA.setPin(SDApin)"); + obj_run(self, "SCL.setMode('out')"); + obj_run(self, "SDA.setMode('out')"); + obj_run(self, "SCL.enable()"); + obj_run(self, "SDA.enable()"); + obj_run(self, "SCL.low()"); + obj_run(self, "SDA.low()"); + char* SCLpin = obj_getStr(self, "SCLpin"); + char* SDApin = obj_getStr(self, "SDApin"); + uint8_t deviceAddr = obj_getInt(self, "deviceAddr"); + + pika_IIC_info* iic = obj_getPtr(self, "iic"); + if (NULL == iic) { + 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->deviceAddr = deviceAddr; + SDA_OUT(iic); +} + +void STM32F1_IIC_platformRead(PikaObj* self) { + int addr = obj_getInt(self, "addr"); + int length = obj_getInt(self, "length"); + pika_IIC_info* iic = obj_getPtr(self, "iic"); + + MPU_Read_Len(iic, iic->deviceAddr, addr, length, (uint8_t*)iic->readBuff); + obj_setStr(self, "readData", (char*)iic->readBuff); +} + +void STM32F1_IIC_platformWrite(PikaObj* self) { + int addr = obj_getInt(self, "addr"); + char* data = obj_getStr(self, "data"); + pika_IIC_info* iic = obj_getPtr(self, "iic"); + + MPU_Write_Len(iic, iic->deviceAddr, addr, strGetSize(data), (uint8_t*)data); +} \ No newline at end of file diff --git a/package/STM32F1/STM32F1_PWM.c b/package/STM32F1/STM32F1_PWM.c new file mode 100644 index 000000000..8d4bddf7f --- /dev/null +++ b/package/STM32F1/STM32F1_PWM.c @@ -0,0 +1,323 @@ +#include +#include "BaseObj.h" +#include "STM32F1_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) { +#ifdef TIM1_EXIST + if (strEqu("PA8", pin) || strEqu("PA9", pin) || strEqu("PA10", pin) || + strEqu("PA11", pin)) { + return TIM1; + } +#endif +#ifdef TIM2_EXIST + if (strEqu("PA0", pin) || strEqu("PA1", pin) || strEqu("PA2", pin) || + strEqu("PA3", pin)) { + return TIM2; + } +#endif +#ifdef TIM3_EXIST + if (strEqu("PA6", pin) || strEqu("PA7", pin) || strEqu("PB0", pin) || + strEqu("PB1", pin)) { + return TIM3; + } +#endif +#ifdef TIM4_EXIST + if (strEqu("PB6", pin) || strEqu("PB7", pin) || strEqu("PB8", pin) || + strEqu("PB9", pin)) { + return TIM4; + } +#endif +#ifdef TIM14_EXIST + if (strEqu("PA4", pin)) { + return TIM14; + } +#endif +#ifdef TIM16_EXIST + if (strEqu("PD0", pin)) { + return TIM16; + } +#endif +#ifdef TIM17_EXIST_EXIST + if (strEqu("PD1", pin)) { + return TIM17; + } +#endif + return NULL; +} + +static void PWM_TimClockEnable(TIM_TypeDef* timInstance) { +#ifdef TIM1_EXIST + if (TIM1 == timInstance) { + __HAL_RCC_TIM1_CLK_ENABLE(); + return; + } +#endif +#ifdef TIM2_EXIST + if (TIM2 == timInstance) { + __HAL_RCC_TIM2_CLK_ENABLE(); + return; + } +#endif +#ifdef TIM3_EXIST + if (TIM3 == timInstance) { + __HAL_RCC_TIM3_CLK_ENABLE(); + return; + } +#endif +#ifdef TIM4_EXIST + if (TIM4 == timInstance) { + __HAL_RCC_TIM4_CLK_ENABLE(); + return; + } +#endif +#ifdef TIM14_EXIST + if (TIM14 == timInstance) { + __HAL_RCC_TIM14_CLK_ENABLE(); + 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) { + /* this Pin do not match any PWM generator */ + return 1; + } + enableClk(pin); + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = getGpioPin(pin); + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(getGpioPort(pin), &GPIO_InitStruct); + PWM_TimClockEnable(timInstance); + return 0; +} + +uint32_t getTimChennel(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; + } + if (strEqu("PA9", pin) || strEqu("PA1", pin) || strEqu("PB7", pin) || + strEqu("PA7", pin)) { + return TIM_CHANNEL_2; + } + if (strEqu("PA10", pin) || strEqu("PA2", pin) || strEqu("PB8", pin) || + strEqu("PB0", pin)) { + return TIM_CHANNEL_3; + } + if (strEqu("PA11", pin) || strEqu("PA3", pin) || strEqu("PB9", pin) || + strEqu("PB1", pin)) { + return TIM_CHANNEL_4; + } + /* Chennel not match */ + return 99999; +} + +void STM32F1_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}; + + if (0 != PWM_MspInit(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; + } + + pika_tim->Instance = getTimInstance(pin); +#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); + 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); + return; + } + if (HAL_TIM_PWM_Init(pika_tim) != HAL_OK) { + obj_setSysOut(self, "[error]: init PWM faild."); + obj_setErrorCode(self, 1); + return; + } + if (HAL_TIM_OC_Init(pika_tim) != HAL_OK) { + obj_setSysOut(self, "[error]: init PWM faild."); + obj_setErrorCode(self, 1); + return; + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + 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 (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 STM32F1_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) { + 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)); + } +} + +void STM32F1_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) { + 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); + float duty = obj_getFloat(self, "duty"); + __HAL_TIM_SET_COMPARE(pika_tim, getTimChennel(pin), + (uint32_t)(pika_tim->Init.Period * duty)); + } +} \ No newline at end of file diff --git a/package/STM32F1/STM32F1_UART.c b/package/STM32F1/STM32F1_UART.c new file mode 100644 index 000000000..ab0a34d17 --- /dev/null +++ b/package/STM32F1/STM32F1_UART.c @@ -0,0 +1,365 @@ +#include +#include "BaseObj.h" +#include "STM32F1_common.h" +#include "dataStrs.h" + +extern PikaObj* pikaMain; + +#ifdef UART1_EXIST +pika_uart_t pika_uart1; +#endif +#ifdef UART2_EXIST +pika_uart_t pika_uart2; +#endif +#ifdef UART3_EXIST +pika_uart_t pika_uart3; +#endif +#ifdef UART4_EXIST +pika_uart_t pika_uart4; +#endif + +UART_HandleTypeDef huart1; + +/* support prinf */ +int fputc(int ch, FILE* f) { + HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 0xffff); + return ch; +} + +/* support scanf */ +int fgetc(FILE* f) { + uint8_t ch = 0; + HAL_UART_Receive(&huart1, &ch, 1, 0xffff); + return ch; +} + +static pika_uart_t* getPikaUart(uint8_t id) { + if (1 == id) { + return &pika_uart1; + } + if (2 == id) { + return &pika_uart2; + } +#ifdef UART3_EXIST + if (3 == id) { + return &pika_uart3; + } +#endif +#ifdef UART4_EXIST + if (4 == id) { + return &pika_uart4; + } +#endif + return NULL; +} + +static void setUartObj(uint8_t id, PikaObj* obj) { + pika_uart_t* pika_uart = getPikaUart(id); + pika_uart->obj = obj; +} + +static PikaObj* getUartObj(uint8_t id) { + pika_uart_t* pika_uart = getPikaUart(id); + if (NULL == pika_uart) { + return NULL; + } + return pika_uart->obj; +} + +static USART_TypeDef* getUartInstance(uint8_t id) { +#ifdef UART1_EXIST + if (1 == id) { + return USART1; + } +#endif +#ifdef UART2_EXIST + if (2 == id) { + return USART2; + } +#endif +#ifdef UART3_EXIST + if (3 == id) { + return USART3; + } +#endif +#ifdef UART4_EXIST + if (4 == id) { + return USART4; + } +#endif + return NULL; +} + +static uint8_t getUartId(UART_HandleTypeDef* huart) { +#ifdef UART1_EXIST + if (huart == &pika_uart1.huart) { + return 1; + } +#endif +#ifdef UART2_EXIST + if (huart == &pika_uart2.huart) { + return 2; + } +#endif +#ifdef UART3_EXIST + if (huart == &pika_uart3.huart) { + return 3; + } +#endif +#ifdef UART4_EXIST + if (huart == &pika_uart4.huart) { + return 4; + } +#endif + return 0; +} + +static UART_HandleTypeDef* getUartHandle(uint8_t id) { + pika_uart_t* pika_uart = getPikaUart(id); + if (NULL == pika_uart) { + return NULL; + } + return &(pika_uart->huart); +} + +static char* getUartRxBuff(uint8_t id) { + pika_uart_t* pika_uart = getPikaUart(id); + if (NULL == pika_uart) { + return NULL; + } + return pika_uart->rxBuff; +} + +static uint8_t USART_UART_Init(uint32_t baudRate, uint8_t id) { + uint8_t errCode = 0; + UART_HandleTypeDef* huart = getUartHandle(id); + huart->Instance = getUartInstance(id); + if (NULL == huart->Instance) { + errCode = 5; + goto exit; + } + huart->Init.BaudRate = baudRate; + huart->Init.WordLength = UART_WORDLENGTH_8B; + huart->Init.StopBits = UART_STOPBITS_1; + huart->Init.Parity = UART_PARITY_NONE; + huart->Init.Mode = UART_MODE_TX_RX; + huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart->Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(huart) != HAL_OK) { + errCode = 1; + goto exit; + } +exit: + return errCode; +} + +static void UART_MspInit(UART_HandleTypeDef* uartHandle) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; +#ifdef UART1_EXIST + if (uartHandle->Instance == USART1) { + /* USART1 clock enable */ + __HAL_RCC_USART1_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); +/**USART1 GPIO Configuration +PA9 ------> USART1_TX +PA10 ------> USART1_RX +*/ +#ifdef STM32F103xB + GPIO_InitStruct.Pin = GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#endif + /* USART1 interrupt Init */ + HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART1_IRQn); + } +#endif +#ifdef UART2_EXIST + if (uartHandle->Instance == USART2) { + /* USART2 clock enable */ + __HAL_RCC_USART2_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); +/**USART2 GPIO Configuration +PA2 ------> USART2_TX +PA3 ------> USART2_RX +*/ + +#ifdef STM32F103xB + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#endif + /* USART2 interrupt Init */ + HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); + } +#endif +#ifdef UART3_EXIST + if (uartHandle->Instance == USART3) { + /* USART3 clock enable */ + __HAL_RCC_USART3_CLK_ENABLE(); + + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); +/**USART3 GPIO Configuration +PA5 ------> USART3_TX +PB0 ------> USART3_RX +*/ + +#ifdef STM32F103xB + /**USART3 GPIO Configuration + PB10 ------> USART3_TX + PB11 ------> USART3_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); +#endif + + /* USART3 interrupt Init */ +#ifdef STM32F103xB + HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(USART3_IRQn); +#endif + } +#endif + +} + +/* Msp handle interrupt */ +#ifdef UART1_EXIST +void USART1_IRQHandler(void) { + HAL_UART_IRQHandler(&pika_uart1.huart); +} +#endif +#ifdef UART2_EXIST +void USART2_IRQHandler(void) { + HAL_UART_IRQHandler(&pika_uart2.huart); +} +#endif +#ifdef UART3_EXIST +#ifdef STM32F103xB +void USART3_IRQHandler(void) { + HAL_UART_IRQHandler(&pika_uart3.huart); +} +#endif +#endif + +#if (defined UART3_EXIST) && (defined UART4_EXIST) +#if defined STM32G070xx +void USART3_4_IRQHandler(void) { + HAL_UART_IRQHandler(&pika_uart3.huart); + HAL_UART_IRQHandler(&pika_uart4.huart); +} +#endif +#endif + +void STM32F1_UART_platformEnable(PikaObj* self) { + int id = obj_getInt(self, "id"); + int baudRate = obj_getInt(self, "baudRate"); + setUartObj(id, self); + /* uart 1 is inited by hardward */ + if (1 == id) { + return; + } + UART_HandleTypeDef* huart = getUartHandle(id); + huart->Instance = getUartInstance(id); + UART_MspInit(huart); + int errCode = USART_UART_Init(baudRate, id); + if (0 != errCode) { + obj_setErrorCode(self, 1); + obj_setSysOut(self, "[error] uart init faild."); + return; + } + HAL_UART_Receive_IT(getUartHandle(id), (uint8_t*)getUartRxBuff(id), 1); +} + +void STM32F1_UART_platformRead(PikaObj* self) { + int id = obj_getInt(self, "id"); + int length = obj_getInt(self, "length"); + Args* buffs = New_strBuff(); + char* readBuff = NULL; + pika_uart_t* pika_uart = getPikaUart(id); + if (length >= pika_uart->rxBuffOffset) { + /* not enough str */ + length = pika_uart->rxBuffOffset; + } + readBuff = args_getBuff(buffs, length); + memcpy(readBuff, pika_uart->rxBuff, length); + obj_setStr(self, "readBuff", readBuff); + readBuff = obj_getStr(self, "readBuff"); + + /* update rxBuff */ + memcpy(pika_uart->rxBuff, pika_uart->rxBuff + length, + pika_uart->rxBuffOffset - length); + pika_uart->rxBuffOffset -= length; + pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0; + + UART_Start_Receive_IT( + &pika_uart->huart, + (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1); +exit: + args_deinit(buffs); + obj_setStr(self,"readData", readBuff); +} + +void STM32F1_UART_platformWrite(PikaObj* self) { + char *data = obj_getStr(self, "data"); + int id = obj_getInt(self, "id"); + HAL_UART_Transmit(getUartHandle(id), (uint8_t*)data, strGetSize(data), 100); +} + +void STM32F1_UART_clearRxBuff(pika_uart_t* pika_uart) { + pika_uart->rxBuffOffset = 0; + pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0; + UART_Start_Receive_IT( + &pika_uart->huart, + (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1); +} + +char pikaShell[RX_BUFF_LENGTH] = {0}; +uint8_t pikaShellRxOk = 0; + +/* Recive Interrupt Handler */ +void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) { + uint8_t id = getUartId(huart); + pika_uart_t* pika_uart = getPikaUart(id); + char inputChar = pika_uart->rxBuff[pika_uart->rxBuffOffset]; + + if ((id == 1) && ('\n' == inputChar)) { + } + /* avoid recive buff overflow */ + if (pika_uart->rxBuffOffset + 2 > RX_BUFF_LENGTH) { + memmove(pika_uart->rxBuff, pika_uart->rxBuff + 1, RX_BUFF_LENGTH); + UART_Start_Receive_IT( + huart, (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1); + return; + } + + /* recive next char */ + pika_uart->rxBuffOffset++; + pika_uart->rxBuff[pika_uart->rxBuffOffset] = 0; + UART_Start_Receive_IT( + huart, (uint8_t*)(pika_uart->rxBuff + pika_uart->rxBuffOffset), 1); +} +