flipper_hal.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Flipper devices inc.
  3. GPIO and HAL implementations
  4. */
  5. #include "main.h"
  6. #include "flipper_hal.h"
  7. void app_gpio_init(GpioPin gpio, GpioMode mode) {
  8. if(gpio.pin != 0) {
  9. GPIO_InitTypeDef GPIO_InitStruct;
  10. GPIO_InitStruct.Pin = gpio.pin;
  11. GPIO_InitStruct.Pull = GPIO_NOPULL;
  12. switch(mode) {
  13. case GpioModeInput:
  14. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  15. break;
  16. case GpioModeOutput:
  17. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  18. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  19. break;
  20. case GpioModeOpenDrain:
  21. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  22. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  23. break;
  24. }
  25. HAL_GPIO_Init(gpio.port, &GPIO_InitStruct);
  26. }
  27. }
  28. void delay_us_init_DWT(void) {
  29. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  30. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  31. DWT->CYCCNT = 0U;
  32. }
  33. void delay_us(float time) {
  34. uint32_t start = DWT->CYCCNT;
  35. uint32_t time_ticks = time * (SystemCoreClock / 1000000);
  36. while((DWT->CYCCNT - start) < time_ticks) {
  37. };
  38. }
  39. void pwm_set(float value, float freq, TIM_HandleTypeDef* tim, uint32_t channel) {
  40. tim->Init.CounterMode = TIM_COUNTERMODE_UP;
  41. tim->Init.Period = (uint32_t)((SystemCoreClock / (tim->Init.Prescaler + 1)) / freq);
  42. tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  43. tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  44. HAL_TIM_PWM_Init(tim);
  45. TIM_OC_InitTypeDef sConfigOC;
  46. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  47. sConfigOC.Pulse = (uint16_t)(tim->Init.Period * value);
  48. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  49. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  50. HAL_TIM_PWM_ConfigChannel(tim, &sConfigOC, channel);
  51. HAL_TIM_PWM_Start(tim, channel);
  52. }
  53. void pwm_stop(TIM_HandleTypeDef* tim, uint32_t channel) {
  54. HAL_TIM_PWM_Stop(tim, channel);
  55. }