tim_delay.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "at32f403a_407.h"
  2. #include "tim_delay.h"
  3. #include "mux.h"
  4. static bool tx_enable = false;
  5. //
  6. void mb_helper_tim_init(uint32_t baudrate)
  7. {
  8. float foo;
  9. foo = 1.0/((float)baudrate / 11.0);
  10. foo *= 2000.0; // время в мс (длительность в два символа)
  11. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  12. crm_clocks_freq_get(&crm_clocks_freq_struct);
  13. nvic_irq_disable(TMR6_GLOBAL_IRQn);
  14. crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
  15. tmr_base_init(TMR6, (uint32_t)(foo * 1000) - 1, (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
  16. tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
  17. NVIC_ClearPendingIRQ(TMR6_GLOBAL_IRQn);
  18. nvic_irq_enable(TMR6_GLOBAL_IRQn, 5, 0);
  19. }
  20. //
  21. void mb_helper_tim_enable(void)
  22. {
  23. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  24. tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
  25. tmr_counter_value_set(TMR6, 0);
  26. tmr_counter_enable(TMR6, TRUE);
  27. }
  28. //
  29. void mb_helper_tim_disable(void)
  30. {
  31. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  32. tmr_interrupt_enable(TMR6, TMR_OVF_INT, FALSE);
  33. tmr_counter_value_set(TMR6, 0);
  34. tmr_counter_enable(TMR6, FALSE);
  35. }
  36. //
  37. void mb_helper_set_tx_state(bool state)
  38. {
  39. tx_enable = state;
  40. }
  41. //
  42. void TMR6_GLOBAL_IRQHandler(void)
  43. {
  44. if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
  45. {
  46. tmr_flag_clear(TMR6, TMR_OVF_FLAG);
  47. if (tx_enable) {
  48. usart_interrupt_enable(USART3, USART_TDBE_INT, TRUE);
  49. leds[TX_R].state = LED_ON;
  50. }
  51. else {
  52. gpio_bits_reset(GPIOD, GPIO_PINS_10);
  53. leds[TX_R].state = LED_OFF;
  54. }
  55. mb_helper_tim_disable();
  56. }
  57. }