bsp_ctrl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "bsp_ctrl.h"
  2. #include "stdint.h"
  3. #include "string.h"
  4. static uint8_t dma_send[512] = {0};
  5. static uint8_t dma_recive[2][512] = {0};
  6. static usart_t ctrl_uart={0};
  7. static int usart0_recive(uint8_t *recive);
  8. static int usart0_send(uint8_t *send,uint16_t len);
  9. usart_t* get_ctrl()
  10. {
  11. return &ctrl_uart;
  12. }
  13. static void ctrl_gpio(void)
  14. {
  15. rcu_periph_clock_enable(RCU_GPIOA);
  16. gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
  17. gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
  18. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
  19. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  20. gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
  21. gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
  22. }
  23. static void ctrl_usart(uint32_t baund)
  24. {
  25. rcu_periph_clock_enable(RCU_USART0);
  26. usart_deinit(USART0);
  27. usart_baudrate_set(USART0, baund);
  28. usart_word_length_set(USART0, USART_WL_8BIT);
  29. usart_stop_bit_set(USART0, USART_STB_1BIT);
  30. usart_parity_config(USART0, USART_PM_NONE);
  31. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  32. usart_receive_config(USART0,USART_RECEIVE_ENABLE);
  33. usart_interrupt_enable(USART0,USART_INT_IDLE);
  34. usart_interrupt_flag_clear(USART0,USART_INT_FLAG_IDLE);
  35. nvic_irq_enable(USART0_IRQn, 5, 2);
  36. usart_enable(USART0);
  37. }
  38. static void dma_config(void)
  39. {
  40. rcu_periph_clock_enable(RCU_DMA1);
  41. dma_single_data_parameter_struct dma_init_struct;
  42. dma_single_data_para_struct_init(&dma_init_struct);
  43. dma_deinit(DMA1, DMA_CH7);
  44. dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
  45. dma_init_struct.memory0_addr = (uint32_t)(&dma_send);
  46. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  47. dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
  48. dma_init_struct.number = (uint32_t)(512);
  49. dma_init_struct.periph_addr = ((uint32_t)&USART_DATA(USART0));
  50. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  51. dma_init_struct.priority = DMA_PRIORITY_LOW;
  52. dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
  53. dma_deinit(DMA1, DMA_CH2);
  54. dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
  55. dma_init_struct.memory0_addr = (uint32_t)(&dma_recive[0][0]);
  56. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  57. dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
  58. dma_init_struct.number = (512);
  59. dma_init_struct.periph_addr = ((uint32_t)&USART_DATA(USART0));
  60. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  61. dma_init_struct.priority = DMA_PRIORITY_LOW;
  62. dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
  63. dma_circulation_disable(DMA1, DMA_CH2);
  64. dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
  65. usart_dma_receive_config(USART0,USART_RECEIVE_DMA_ENABLE);
  66. dma_circulation_disable(DMA1, DMA_CH7);
  67. dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);
  68. usart_dma_transmit_config(USART0,USART_TRANSMIT_DMA_ENABLE);
  69. // dma_flag_clear(DMA0,DMA_CH5,DMA_FLAG_FTF);
  70. // dma_flag_clear(DMA0,DMA_CH6,DMA_FLAG_FTF);
  71. //dma_channel_enable(DMA0, DMA_CH6);
  72. dma_channel_enable(DMA1, DMA_CH2);
  73. }
  74. void bsp_ctrl_init(void)
  75. {
  76. ctrl_gpio();
  77. ctrl_usart(38400);
  78. dma_config();
  79. ctrl_uart.send = usart0_send;
  80. ctrl_uart.recive = usart0_recive;
  81. }
  82. void USART0_IRQHandler(void)
  83. {
  84. uint16_t len= 0;
  85. if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
  86. USART_STAT0(USART0);
  87. USART_DATA(USART0);
  88. dma_channel_disable(DMA1, DMA_CH2);
  89. ctrl_uart.recive_len = 512 - dma_transfer_number_get(DMA1, DMA_CH2);
  90. ctrl_uart.recive_complete = SET;
  91. ctrl_uart.curent_recive_id = (ctrl_uart.curent_recive_id == 0) ? 1 : 0;
  92. dma_memory_address_config(DMA1, DMA_CH2,DMA_MEMORY_0,(uint32_t)(&dma_recive[ctrl_uart.curent_recive_id][0]));
  93. dma_transfer_number_config(DMA1, DMA_CH2, 512);
  94. //usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);
  95. dma_flag_clear(DMA1,DMA_CH2,DMA_FLAG_FTF);
  96. dma_channel_enable(DMA1, DMA_CH2);
  97. }
  98. }
  99. static int usart0_send(uint8_t *send,uint16_t len)
  100. {
  101. dma_channel_disable(DMA1,DMA_CH7);
  102. memcpy(dma_send,send,len);
  103. dma_transfer_number_config(DMA1, DMA_CH7,len);
  104. dma_channel_enable(DMA1,DMA_CH7);
  105. while(!dma_flag_get(DMA1, DMA_CH7, DMA_FLAG_FTF)){
  106. vTaskDelay(1);
  107. }
  108. dma_flag_clear(DMA1, DMA_CH7, DMA_FLAG_FTF);
  109. ctrl_uart.recive_complete = RESET;
  110. return 0;
  111. }
  112. static int usart0_recive(uint8_t *recive)
  113. {
  114. uint8_t id = ctrl_uart.curent_recive_id == 1 ? 0 : 1;
  115. memcpy(recive,&dma_recive[id][0],ctrl_uart.recive_len);
  116. return ctrl_uart.recive_len;
  117. }