|
|
@ -3,55 +3,155 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#include "iflytop_microcontroller/fancontroler/stm32/fan.h" |
|
|
|
#include "iwdg.h" |
|
|
|
#include "main.h" |
|
|
|
#include "port.h" |
|
|
|
// |
|
|
|
#include "iflytop_microcontroller/fancontroler/stm32/fan.h" |
|
|
|
#include "iflytop_microcontroller/zkey/std/zkey.h" |
|
|
|
#include "iflytop_microcontroller/zmodbus/std/modbus_processer.h" |
|
|
|
#include "iflytop_microcontroller/zmodbus/std/zmodbus_common.h" |
|
|
|
|
|
|
|
#define interrupt_reception_send_buf_max 32 |
|
|
|
#define interrupt_reception_send_string "UART_TEST\r\n" |
|
|
|
#define TAG "main" |
|
|
|
|
|
|
|
bool interrupt_trigger_flag; |
|
|
|
#define interrupt_reception_send_buf_max 32 |
|
|
|
#define interrupt_reception_send_string "MODBUS_UART\r\n" |
|
|
|
|
|
|
|
/*********************************************************************************************************************** |
|
|
|
* ====================================================全局状态===================================================== * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
static uint64_t s_key_press_state; |
|
|
|
static fan_t s_fan[1] = { |
|
|
|
FAN_INIT(NULL, 0, false, &FAN1_PWM_TIMER, FAN1_PWM_TIMER_CHANNEL, 1000, false, FAN1_FB_GPIO_PORT, FAN1_FB_PIN, |
|
|
|
false), |
|
|
|
}; |
|
|
|
|
|
|
|
static void uart_test_interrupt_reception() { |
|
|
|
uint8_t send_buffer[interrupt_reception_send_buf_max] = interrupt_reception_send_string; |
|
|
|
uint16_t send_string_size = strlen(interrupt_reception_send_string); |
|
|
|
if (send_string_size >= interrupt_reception_send_buf_max) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (interrupt_trigger_flag) { |
|
|
|
HAL_UART_Transmit(&huart2, &send_buffer[0], send_string_size, 100); |
|
|
|
interrupt_trigger_flag = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @brief 处理从串口接收到的数据 |
|
|
|
*/ |
|
|
|
void port_mock_on_uart_rx(uart_t* uart) { |
|
|
|
// 处理指令串口接收到的数据 |
|
|
|
if (uart->uarthandler == &UART_TEST) { |
|
|
|
interrupt_trigger_flag = true; |
|
|
|
if (uart->uarthandler == &MODBUS_UART) { |
|
|
|
modbus_processer_push_data(uart->rxbuf); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*********************************************************************************************************************** |
|
|
|
* ============================================ORDER_MODBUS_SLAVE============================================= * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
static uint8_t modbus_rx_buf[kmodbus_processer_rx_buf_size]; |
|
|
|
static uint8_t modbus_tx_buf[kmodbus_processer_tx_buf_size]; |
|
|
|
void process_10(uint16_t startreg, uint32_t regoff, uint16_t rxvalue); |
|
|
|
void process_03(uint16_t startreg, uint32_t regoff); |
|
|
|
void order_uart485_tx(uint8_t* rx, uint16_t len) { HAL_UART_Transmit(&MODBUS_UART, rx, len, 0xffffffff); } |
|
|
|
void modbux_process_rx(modbus_processer_context_t* context); |
|
|
|
static void delay_us(uint32_t us) { sys_delay_us(&US_TIMER, us); } |
|
|
|
modbus_processer_t s_modbus_processer = { |
|
|
|
.modbus_device_id = MODBUS_DEVICE_ID, |
|
|
|
.modbus_processer_rx_buf = modbus_rx_buf, |
|
|
|
.modbus_processer_rx_buf_size = sizeof(modbus_rx_buf), |
|
|
|
.modbus_processer_tx_buf = modbus_tx_buf, |
|
|
|
.modbus_processer_tx_buf_size = sizeof(modbus_tx_buf), |
|
|
|
/*************************************/ |
|
|
|
.modbus_baundrate_one_packet_delay_us = kmodbus_baundrate_one_packet_delay_us, |
|
|
|
.port_enter_critical = sys_critical_enter, |
|
|
|
.port_exit_critical = sys_critical_exit, |
|
|
|
.port_delay_us = delay_us, |
|
|
|
.tx = order_uart485_tx, |
|
|
|
/*************************************/ |
|
|
|
.process_rx = modbux_process_rx, |
|
|
|
}; |
|
|
|
|
|
|
|
#if 0 |
|
|
|
寄存器: |
|
|
|
Addr | 功能 |
|
|
|
------------|------------------------------- |
|
|
|
0 | 主版本号 |
|
|
|
1 | 次版本号 |
|
|
|
8(16Byte) | 继电器0 ->继电器15 掩码 |
|
|
|
9(16Byte) | 继电器16->继电器31 掩码 |
|
|
|
10(16Byte) | 继电器0 ->继电器15 继电器状态 |
|
|
|
11(16Byte) | 继电器16->继电器31 继电器状态 |
|
|
|
12(16Byte) | 所有限位输入状态 |
|
|
|
15(16Byte) | 按键状态 KeyX|按下,长按,松开事件|(Key0|0,1,2|,Key1|3,4,5|,Key2|6,7,8|,Key3|9,10,11|,Key4|12,13,14|) |
|
|
|
#endif |
|
|
|
void modbus_process_0_15(modbus_processer_context_t* context) { |
|
|
|
ModbusFunctionCode fcode = modbus_get_function_code(context); |
|
|
|
if (fcode == ModbusOrder10) { |
|
|
|
/** |
|
|
|
* @brief 基本GPIO写 |
|
|
|
*/ |
|
|
|
if (modbus_if_register_exists_10(context, 8) && modbus_if_register_exists_10(context, 9) && |
|
|
|
modbus_if_register_exists_10(context, 10) && modbus_if_register_exists_10(context, 11)) { |
|
|
|
uint32_t mask = modbus_get_reg_10(context, 8) | (modbus_get_reg_10(context, 9) << 16); |
|
|
|
uint32_t value = modbus_get_reg_10(context, 10) | (modbus_get_reg_10(context, 11) << 16); |
|
|
|
for (size_t i = 0; i < 32; i++) { |
|
|
|
if (mask & (0x0001 << i)) port_electric_relay_set_state(i, !!(value & (0x0001 << i))); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (fcode == ModbusOrder03) { |
|
|
|
if (modbus_if_register_exists_03(context, 0)) { |
|
|
|
modbus_set_tx_reg_03(context, 0, VERSION_MAIN_ID); |
|
|
|
} |
|
|
|
if (modbus_if_register_exists_03(context, 1)) { |
|
|
|
modbus_set_tx_reg_03(context, 1, VERSION_SUB_ID); |
|
|
|
} |
|
|
|
if (modbus_if_register_exists_03(context, 10)) { |
|
|
|
uint32_t value = 0; |
|
|
|
for (size_t i = 0; i < 16; i++) { |
|
|
|
if (port_electric_relay_get_state(i)) value |= (0x0001 << i); |
|
|
|
} |
|
|
|
modbus_set_tx_reg_03(context, 10, value); |
|
|
|
} |
|
|
|
if (modbus_if_register_exists_03(context, 11)) { |
|
|
|
uint32_t value = 0; |
|
|
|
for (size_t i = 16; i < 32; i++) { |
|
|
|
if (port_electric_relay_get_state(i)) value |= (0x0001 << i); |
|
|
|
} |
|
|
|
modbus_set_tx_reg_03(context, 11, value); |
|
|
|
} |
|
|
|
|
|
|
|
if (modbus_if_register_exists_03(context, 12)) { |
|
|
|
uint16_t value = 0; |
|
|
|
for (size_t i = 0; i < 16; i++) { |
|
|
|
if (port_get_gpio_int(i)) { |
|
|
|
value |= (0x0001 << i); |
|
|
|
} |
|
|
|
} |
|
|
|
modbus_set_tx_reg_03(context, 12, value); |
|
|
|
} |
|
|
|
|
|
|
|
if (modbus_if_register_exists_03(context, 15)) { |
|
|
|
modbus_set_tx_reg_03(context, 15, s_key_press_state & 0xffff); |
|
|
|
s_key_press_state = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void modbux_process_rx(modbus_processer_context_t* context) { |
|
|
|
ModbusFunctionCode fcode = modbus_get_function_code(context); |
|
|
|
modbus_process_0_15(context); |
|
|
|
if (fcode == ModbusOrder10) { |
|
|
|
modbus_send_10(context, Modbus_OK); |
|
|
|
} else if (fcode == ModbusOrder03) { |
|
|
|
modbus_send_03(context, Modbus_OK); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void user_main() { |
|
|
|
ZLOGI(TAG, "VERSION :%d.%d", VERSION_MAIN_ID, VERSION_SUB_ID); |
|
|
|
// 开启所有中断接收 |
|
|
|
port_uart_start_all_uart_receive(); |
|
|
|
modbus_processer_init(&s_modbus_processer); |
|
|
|
|
|
|
|
//使用了风扇的代码控制PWM(占空比) |
|
|
|
// 使用了风扇的代码控制PWM(占空比) |
|
|
|
fan_set_power(&s_fan[0], 50); |
|
|
|
while (1) { |
|
|
|
// debug灯 |
|
|
|
port_do_debug_light_state(); |
|
|
|
|
|
|
|
// 串口测试中断处理 |
|
|
|
uart_test_interrupt_reception(); |
|
|
|
// 指令485数据处理 |
|
|
|
modbus_processer_try_process_data(); |
|
|
|
|
|
|
|
// 刷新看门狗 |
|
|
|
HAL_IWDG_Refresh(&hiwdg); |
|
|
|