2020-08-07 14:33:22 +00:00
|
|
|
/*
|
|
|
|
Flipper devices inc.
|
|
|
|
|
|
|
|
GPIO and HAL implementations
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
2020-08-17 09:45:53 +00:00
|
|
|
#include "flipper_hal.h"
|
2020-08-07 14:33:22 +00:00
|
|
|
|
|
|
|
void app_gpio_init(GpioPin gpio, GpioMode mode) {
|
|
|
|
if(gpio.pin != 0) {
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct;
|
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = gpio.pin;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
|
|
|
|
switch(mode) {
|
2020-09-29 23:18:30 +00:00
|
|
|
case GpioModeInput:
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
2020-08-07 14:33:22 +00:00
|
|
|
break;
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
case GpioModeOutput:
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
|
2020-08-07 14:33:22 +00:00
|
|
|
break;
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
case GpioModeOpenDrain:
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
|
2020-08-07 14:33:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
HAL_GPIO_Init(gpio.port, &GPIO_InitStruct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
void delay_us_init_DWT(void) {
|
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
|
|
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
|
|
|
|
DWT->CYCCNT = 0U;
|
|
|
|
}
|
2020-08-07 14:33:22 +00:00
|
|
|
|
2020-10-23 09:39:11 +00:00
|
|
|
void delay_us(float time) {
|
|
|
|
uint32_t start = DWT->CYCCNT;
|
|
|
|
uint32_t time_ticks = time * (SystemCoreClock / 1000000);
|
|
|
|
while((DWT->CYCCNT - start) < time_ticks) {
|
|
|
|
};
|
2020-08-07 14:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) {
|
|
|
|
tim->Init.CounterMode = TIM_COUNTERMODE_UP;
|
2020-10-23 09:39:11 +00:00
|
|
|
tim->Init.Period = (uint32_t)((SystemCoreClock / (tim->Init.Prescaler + 1)) / freq);
|
2020-08-07 14:33:22 +00:00
|
|
|
tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
|
|
HAL_TIM_PWM_Init(tim);
|
|
|
|
|
|
|
|
TIM_OC_InitTypeDef sConfigOC;
|
2020-09-29 23:18:30 +00:00
|
|
|
|
2020-08-07 14:33:22 +00:00
|
|
|
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
2020-10-23 09:39:11 +00:00
|
|
|
sConfigOC.Pulse = (uint16_t)(tim->Init.Period * value);
|
2020-08-07 14:33:22 +00:00
|
|
|
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
|
|
|
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
|
|
|
HAL_TIM_PWM_ConfigChannel(tim, &sConfigOC, channel);
|
|
|
|
HAL_TIM_PWM_Start(tim, channel);
|
2020-10-23 09:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
|
|
|
|
HAL_TIM_PWM_Stop(tim, channel);
|
2020-08-07 14:33:22 +00:00
|
|
|
}
|