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.

215 lines
7.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "zboard.h"
  7. #include "zport.h"
  8. //
  9. #include "../copyZ_zstm32uart_irq_rx_service/zstm32uart_irq_rx_service.h"
  10. #include "gpio.h"
  11. #include "iwdg.h"
  12. #include "main.h"
  13. #include "modbus_processer.h"
  14. #include "tim.h"
  15. #include "usart.h"
  16. #include "zmodbus_slave.h"
  17. static uint8_t modbus_rx_buf[kmodbus_processer_rx_buf_size];
  18. static uint8_t modbus_tx_buf[kmodbus_processer_tx_buf_size];
  19. //当触发闪光时候,自动触发拍照信号
  20. uint16_t g_auto_trigger_shut_when_flash = 1;
  21. // state
  22. uint32_t g_light_flicker_trigger_ticket;
  23. bool g_flicker_on = false;
  24. /***********************************************************************************************************************
  25. * ========================================================GLOBAL======================================================== *
  26. ***********************************************************************************************************************/
  27. typedef enum {
  28. klight_control_mode_always_close = 0,
  29. klight_control_mode_always_open,
  30. klight_control_mode_flicker,
  31. klight_control_mode_control_direct,
  32. klight_control_mode_max,
  33. } light_control_mode_t;
  34. // config
  35. uint16_t g_light_brightness = 0;
  36. uint16_t g_light_control_mode = klight_control_mode_always_close;
  37. uint16_t g_light_flicker_duration = 0;
  38. void update_light_state() {
  39. if (g_light_control_mode == klight_control_mode_always_close) {
  40. port_set_led_brightness(0);
  41. } else if (g_light_control_mode == klight_control_mode_always_open) {
  42. port_set_led_brightness(g_light_brightness);
  43. }
  44. }
  45. void trigger_one_time_flicker(uint32_t brightness, uint16_t duration_ms) {
  46. //
  47. g_light_flicker_trigger_ticket = HAL_GetTick();
  48. g_light_flicker_duration = duration_ms;
  49. g_flicker_on = true;
  50. port_set_led_brightness(brightness);
  51. if (g_auto_trigger_shut_when_flash > 0) {
  52. if (port_extra_gpio_get_mode(0) == kegm_camera_trigger_signal) {
  53. port_extra_gpio_set_level(0, true);
  54. }
  55. if (port_extra_gpio_get_mode(1) == kegm_camera_trigger_signal) {
  56. port_extra_gpio_set_level(1, true);
  57. }
  58. }
  59. }
  60. void order_uart485_tx(uint8_t* rx, uint16_t len) { HAL_UART_Transmit(&ORDER_485_UART, rx, len, 0xffffffff); }
  61. /***********************************************************************************************************************
  62. * =======================================================UART======================================================== *
  63. ***********************************************************************************************************************/
  64. static zstm32uart_t s_uarts[] = {
  65. {
  66. .huart = &ORDER_485_UART,
  67. } //
  68. };
  69. void modbux_process_rx(modbus_processer_context_t* context) {
  70. // 10 and 03
  71. // 0->15 | 闪光灯控制
  72. // 0(16Byte) | 闪光灯亮度(0->65536)
  73. // 1(16Byte) | 闪光灯亮度(0->100)
  74. // 2(16Byte) | mode(0:长关模式,1:常亮模式,2:触发模式)
  75. // 3-4(32Byte) | 触发一次闪光,reg3亮度(0->65535),reg4持续时间ms
  76. // 7(16Byte) | 触发模式下,是否驱动GPIO,输出拍照信号 (0:输出,1:不输出)
  77. // ------------|-------------------------------
  78. // 16->31 | 扩展GPIO1控制
  79. // 16(16Byte) | 扩展GPIO功能(0:失能,1:触发模式输出)
  80. // 17(16Byte) | 是否反向
  81. // ------------|-------------------------------
  82. // 32->47 | 扩展GPIO2控制
  83. // 32(16Byte) | 扩展GPIO功能(0:失能,1:触发模式输出)
  84. // 33(16Byte) | 是否反向
  85. ModbusFunctionCode fcode = modbus_get_function_code(context);
  86. if (fcode == ModbusOrder10) {
  87. //写
  88. // REG0
  89. if (modbus_if_register_exists_10(context, 0)) {
  90. uint16_t brightness = modbus_get_reg_10(context, 0);
  91. g_light_brightness = brightness;
  92. LOGD("set light_brightness %d", brightness);
  93. update_light_state();
  94. }
  95. // REG1
  96. if (modbus_if_register_exists_10(context, 1)) {
  97. uint16_t brightness_100 = modbus_get_reg_10(context, 1);
  98. brightness_100 = brightness_100 > 100 ? 100 : brightness_100;
  99. uint16_t brightness = 65535 * (brightness_100 / 100.0);
  100. g_light_brightness = brightness;
  101. LOGD("set light_brightness %d%%(%d)", brightness_100, brightness);
  102. update_light_state();
  103. }
  104. // REG2
  105. if (modbus_if_register_exists_10(context, 2)) {
  106. if (g_light_control_mode < klight_control_mode_max) {
  107. g_light_control_mode = modbus_get_reg_10(context, 2);
  108. LOGD("set light_control_mode %d", g_light_control_mode);
  109. update_light_state();
  110. } else {
  111. LOGE("set light_control_mode fail,parameter error");
  112. }
  113. }
  114. // REG3,4
  115. if (modbus_if_register_exists_10(context, 3) && modbus_if_register_exists_03(context, 4)) {
  116. //触发一次闪光
  117. if (g_light_control_mode == klight_control_mode_flicker) {
  118. uint32_t brightness = modbus_get_reg_10(context, 3);
  119. uint16_t duration_ms = modbus_get_reg_10(context, 4);
  120. LOGD("trigger flicker %d %dms", brightness, duration_ms);
  121. trigger_one_time_flicker(brightness, duration_ms);
  122. } else {
  123. LOGE("trigger flicker fail,mode != klight_control_mode_flicker");
  124. }
  125. }
  126. // REG5
  127. if (modbus_if_register_exists_10(context, 5)) {
  128. uint16_t setvalue = modbus_get_reg_10(context, 5);
  129. LOGD("port_set_led_brightness1 %d%%", setvalue);
  130. port_set_led_brightness1(65535 * (setvalue / 100.0));
  131. }
  132. // REG6
  133. if (modbus_if_register_exists_10(context, 6)) {
  134. uint16_t setvalue = modbus_get_reg_10(context, 6);
  135. LOGD("port_set_led_brightness2 %d%%", setvalue);
  136. port_set_led_brightness2(65535 * (setvalue / 100.0));
  137. }
  138. modbus_send_10(context, Modbus_OK);
  139. } else if (fcode == ModbusOrder03) {
  140. //读
  141. if (modbus_if_register_exists_03(context, 0)) {
  142. modbus_set_tx_reg_03(context, 0, g_light_brightness);
  143. }
  144. if (modbus_if_register_exists_03(context, 1)) {
  145. modbus_set_tx_reg_03(context, 1, g_light_brightness / 65535.0 * 100);
  146. }
  147. if (modbus_if_register_exists_03(context, 2)) {
  148. modbus_set_tx_reg_03(context, 2, g_light_control_mode);
  149. }
  150. modbus_send_03(context, Modbus_OK);
  151. }
  152. }
  153. modbus_processer_t s_modbus_processer = {
  154. .modbus_device_id = MODBUS_DEVICE_ID,
  155. .modbus_processer_rx_buf = modbus_rx_buf,
  156. .modbus_processer_rx_buf_size = sizeof(modbus_rx_buf),
  157. .modbus_processer_tx_buf = modbus_tx_buf,
  158. .modbus_processer_tx_buf_size = sizeof(modbus_tx_buf),
  159. /*************************************/
  160. .modbus_baundrate_one_packet_delay_us = kmodbus_baundrate_one_packet_delay_us,
  161. .port_enter_critical = port_enter_critical,
  162. .port_exit_critical = port_exit_critical,
  163. .port_delay_us = port_delay_us,
  164. .tx = order_uart485_tx,
  165. /*************************************/
  166. .process_rx = modbux_process_rx,
  167. };
  168. /**
  169. * @brief
  170. */
  171. void port_do_debug_light_state() {
  172. static uint32_t lastprocess = 0;
  173. if (port_haspassedms(lastprocess) > 300) {
  174. lastprocess = HAL_GetTick();
  175. HAL_GPIO_TogglePin(DEBUG_LIGHT_PORT, DEBUG_LIGHT_PIN);
  176. }
  177. }
  178. /**
  179. * @brief
  180. */
  181. void HOOK_ZUART_RxCpltCallback(UART_HandleTypeDef* huart, uint8_t rxdata) {
  182. if (huart == &ORDER_485_UART) {
  183. //处理接收到485指令
  184. modbus_processer_push_data(rxdata);
  185. }
  186. }
  187. void user_main() {
  188. uart_service_start_all_uart_rx(s_uarts, ARRARY_SIZE(s_uarts));
  189. modbus_processer_init(&s_modbus_processer);
  190. while (true) {
  191. port_do_debug_light_state();
  192. modbus_processer_try_process_data();
  193. }
  194. }