You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
6.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "board.h"
  7. #include "port.h"
  8. //
  9. #include "gpio.h"
  10. #include "main.h"
  11. #include "tim.h"
  12. #include "usart.h"
  13. //
  14. #include "stm32f429xx.h"
  15. #include "stm32f4xx_hal_tim.h"
  16. #include "stm32f4xx_hal_usart.h"
  17. /**
  18. * @brief IO状态
  19. */
  20. /**
  21. * @brief
  22. */
  23. #define LOG_RELEASE(fmt, ...) printf("" fmt, ##__VA_ARGS__);
  24. #define LOGI(fmt, ...) printf("" fmt, ##__VA_ARGS__);
  25. #define LOGD(fmt, ...) printf("D:" fmt, ##__VA_ARGS__);
  26. #define LOGE(fmt, ...) printf("E:" fmt, ##__VA_ARGS__);
  27. #if 0
  28. #define LOGI(fmt, ...) printf("" fmt, ##__VA_ARGS__);
  29. #define LOGD(fmt, ...) printf("D:" fmt, ##__VA_ARGS__);
  30. #define LOGE(fmt, ...) printf("E:" fmt, ##__VA_ARGS__);
  31. #else
  32. #define LOGI(fmt, ...) printf("" fmt, ##__VA_ARGS__);
  33. #define LOGD(fmt, ...)
  34. #define LOGE(fmt, ...)
  35. #endif
  36. static uint8_t s_order_rxbuf;
  37. static uint8_t s_rxbuf[50];
  38. static uint8_t s_rxbuf_has_received;
  39. static bool s_rxbuf_is_ready;
  40. //连续输出PWM
  41. static bool g_continue_output_mode;
  42. static uint32_t g_pulse_num = 0;
  43. static bool g_internal_sync_mode;
  44. void set_mode_in_internal_pwm_mode() {
  45. // LOG_RELEASE("内部信号同步模式\n");
  46. EXT_OEn_SET(true);
  47. PWM_OEn_SET(false);
  48. g_internal_sync_mode = true;
  49. }
  50. void set_mode_in_external_pwm_mode() {
  51. EXT_OEn_SET(false);
  52. PWM_OEn_SET(true);
  53. // LOG_RELEASE("外部信号同步模式\n");
  54. g_internal_sync_mode = false;
  55. }
  56. /**
  57. * @brief
  58. *
  59. * @param husart
  60. */
  61. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  62. if (&huart1 == huart) {
  63. /**
  64. * @brief
  65. */
  66. if (!s_rxbuf_is_ready) {
  67. if (s_order_rxbuf == 'S') {
  68. s_rxbuf[s_rxbuf_has_received] = s_order_rxbuf;
  69. s_rxbuf_has_received++;
  70. } else if (s_rxbuf_has_received != 0) {
  71. s_rxbuf[s_rxbuf_has_received] = s_order_rxbuf;
  72. s_rxbuf_has_received++;
  73. }
  74. if (s_rxbuf_has_received == 20) {
  75. s_rxbuf_is_ready = true;
  76. }
  77. }
  78. // HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  79. }
  80. }
  81. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
  82. if (&ORDER_UART == huart) {
  83. // HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  84. }
  85. }
  86. void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart) {
  87. if (&ORDER_UART == huart) {
  88. // HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  89. }
  90. }
  91. void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart) {
  92. if (&ORDER_UART == huart) {
  93. // HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  94. }
  95. }
  96. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
  97. if (htim->Instance == TIM3) {
  98. /**
  99. * @brief
  100. */
  101. #if 1
  102. if (g_continue_output_mode) return;
  103. if (g_pulse_num == 0) {
  104. HAL_TIM_PWM_Stop_IT(&htim3, TIM_CHANNEL_1);
  105. } else {
  106. g_pulse_num--;
  107. }
  108. #endif
  109. }
  110. }
  111. /**
  112. * @brief PWM的周期
  113. *
  114. * @param target_pluse_width_us
  115. * @param targetfreq
  116. * @param arr
  117. * @return uint16_t COMPARE寄存器的数值
  118. */
  119. uint16_t get_tim_compare_value(int32_t target_pluse_width_us,
  120. int32_t targetfreq, uint16_t arr) {
  121. uint16_t comparevalue = 0;
  122. float periodus = 1.0 / targetfreq * 1000 * 1000;
  123. float duty_radio = target_pluse_width_us / periodus;
  124. if (duty_radio >= 1) duty_radio = 1;
  125. comparevalue = duty_radio * (arr + 1);
  126. if (comparevalue < 2) {
  127. return 1;
  128. }
  129. comparevalue = comparevalue - 1;
  130. return comparevalue;
  131. }
  132. /**
  133. * @brief PWM
  134. *
  135. * @param freq
  136. * @param pluse_width
  137. * @param pluse_num
  138. */
  139. void trigger_pwm(int32_t freq, float duty, int32_t pluse_num) {
  140. //无论怎样先停止PWM输
  141. HAL_TIM_PWM_Stop_IT(&htim3, TIM_CHANNEL_1);
  142. if (freq == 0) {
  143. LOGE("E:trigger pwm fail,freq == 0\n");
  144. return;
  145. }
  146. // 确定预分频参数,重载值
  147. float psc_x_arr = 90 * 1000 /*系统时钟*/ * 1000 / freq;
  148. uint32_t psc = 0;
  149. uint32_t arr = 65534;
  150. for (; arr > 2; arr--) {
  151. psc = psc_x_arr / arr;
  152. if (psc >= 1) {
  153. uint32_t tmparr = psc_x_arr / psc;
  154. if (tmparr >= 65534) continue;
  155. break;
  156. }
  157. }
  158. if (psc == 0) return;
  159. if (arr <= 3) return; //定时器一周期的分辨率太小了
  160. arr = psc_x_arr / psc;
  161. arr = arr - 1;
  162. psc = psc - 1;
  163. __HAL_TIM_SET_AUTORELOAD(&htim3, arr);
  164. __HAL_TIM_SET_PRESCALER(&htim3, psc);
  165. // 确定PWM通道占空比
  166. uint16_t comparevalue = duty * arr;
  167. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, comparevalue);
  168. if (pluse_num == 0) {
  169. g_continue_output_mode = true;
  170. } else {
  171. g_pulse_num = pluse_num - 1;
  172. g_continue_output_mode = false;
  173. }
  174. __HAL_TIM_SET_COUNTER(&htim3, 0);
  175. LOGD("a[%d] p[%d]c[%d]\r\n", arr, psc, comparevalue);
  176. HAL_TIM_PWM_Start_IT(&htim3, TIM_CHANNEL_1);
  177. }
  178. void process_rxbuf() {
  179. // clang-format off
  180. /**
  181. *
  182. * @brief
  183. *
  184. * S0000112345601234567
  185. * S 0 0 0 0 1 1 2 3 4 5 6 0 1 2 3 4 5 6 7
  186. * [S] [2][3][4][5][6] [7][8][9][10][11][12][13] [14][15][16][17][18][19][20]
  187. * HZ 0.1us
  188. *
  189. *
  190. */
  191. // clang-format on
  192. LOGD("rx:%s\r\n", s_rxbuf);
  193. char freq_c[6] = {0};
  194. char pluse_width_c[8] = {0};
  195. char pluse_num_c[8] = {0};
  196. int32_t freq = 0, pluse_width = 0, pluse_num = 0;
  197. memcpy(freq_c, &s_rxbuf[1], 5);
  198. memcpy(pluse_width_c, &s_rxbuf[6], 7);
  199. memcpy(pluse_num_c, &s_rxbuf[13], 7);
  200. freq = atoi(freq_c);
  201. pluse_width = atoi(pluse_width_c);
  202. pluse_num = atoi(pluse_num_c);
  203. //使能内部,失能外部
  204. set_mode_in_internal_pwm_mode();
  205. /**
  206. * @brief pwm
  207. */
  208. // LOG_RELEASE("触发PWM 频率:%dHZ,脉冲宽度:%dus,脉冲数量:%d\n", freq,
  209. // pluse_width, pluse_num);
  210. float duty = pluse_width / 10.0 / (1 * 1000 * 1000.0 / freq);
  211. LOGI("f[%d]hz \r\n", freq);
  212. LOGI("w[%d]us \r\n", pluse_width/10);
  213. trigger_pwm(freq, duty, pluse_num);
  214. }
  215. void user_main() {
  216. // LOG_RELEASE("wavebox %s\r\n", VERSION);
  217. // LOG_RELEASE("%s\r\n", "www.zwtime.com");
  218. HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  219. set_mode_in_external_pwm_mode();
  220. while (true) {
  221. /**
  222. * @brief
  223. */
  224. #if 1
  225. if (s_rxbuf_is_ready) {
  226. process_rxbuf();
  227. s_rxbuf_is_ready = false;
  228. s_rxbuf_has_received = 0;
  229. }
  230. #endif
  231. if (ORDER_UART.RxState == HAL_UART_STATE_READY) {
  232. HAL_UART_Receive_IT(&ORDER_UART, &s_order_rxbuf, 1);
  233. }
  234. /**
  235. * @brief
  236. */
  237. {
  238. static uint32_t lastprocess = 0;
  239. if (port_haspassedms(lastprocess) > 300) {
  240. lastprocess = HAL_GetTick();
  241. HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN);
  242. }
  243. }
  244. /**/
  245. }
  246. }