diff --git a/.vscode/settings.json b/.vscode/settings.json index 2f51b86..36e17e8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,6 +38,9 @@ "freertos.h": "c", "tcp.h": "c", "cmsis_os.h": "c", - "freertosconfig.h": "c" + "freertosconfig.h": "c", + "communication_protocol.h": "c", + "user_gpio.h": "c", + "user_lib.h": "c" } } \ No newline at end of file diff --git a/Core/Inc/gpio.h b/Core/Inc/gpio.h index 6d9fc61..adda4ed 100644 --- a/Core/Inc/gpio.h +++ b/Core/Inc/gpio.h @@ -22,17 +22,18 @@ #define __GPIO_H__ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* Includes ------------------------------------------------------------------*/ #include "main.h" -/* USER CODE BEGIN Includes */ + /* USER CODE BEGIN Includes */ -/* USER CODE END Includes */ + /* USER CODE END Includes */ -/* USER CODE BEGIN Private defines */ + /* USER CODE BEGIN Private defines */ typedef enum { DI_1, @@ -46,17 +47,16 @@ extern "C" { DO_EN, DI_DO_MAX, } gpio_e; -/* USER CODE END Private defines */ + /* USER CODE END Private defines */ -void MX_GPIO_Init(void); + void MX_GPIO_Init(void); -/* USER CODE BEGIN Prototypes */ - extern void gpio_do_test(gpio_e gpio_num, GPIO_PinState state); - extern GPIO_PinState gpio_di_test(gpio_e gpio_num); -/* USER CODE END Prototypes */ + /* USER CODE BEGIN Prototypes */ + extern void gpio_do_test(uint8_t gpio_num, GPIO_PinState state); + extern GPIO_PinState gpio_di_test(uint8_t gpio_num); + /* USER CODE END Prototypes */ #ifdef __cplusplus } #endif #endif /*__ GPIO_H__ */ - diff --git a/Core/Inc/main.h b/Core/Inc/main.h index e3a84c6..eb4bd98 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -37,29 +37,29 @@ extern "C" #include "tcpserverc.h" #include "leds.h" #include - /* USER CODE END Includes */ +/* USER CODE END Includes */ - /* Exported types ------------------------------------------------------------*/ - /* USER CODE BEGIN ET */ - extern uint8_t tcp_echo_flags_hart1; - extern uint8_t tcp_echo_flags_hart2; - extern uint8_t tcp_echo_flags_ble1; - extern uint8_t tcp_echo_flags_ble2; - extern uint8_t tcp_echo_flags_control; +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ +extern uint8_t tcp_echo_flags_hart1; +extern uint8_t tcp_echo_flags_hart2; +extern uint8_t tcp_echo_flags_ble1; +extern uint8_t tcp_echo_flags_ble2; +extern uint8_t tcp_echo_flags_control; #define ARRAY_LEN(arr) (sizeof(arr)) / (sizeof(arr[0])) - typedef struct - { - uint16_t rx_num; - uint8_t rx_data[512]; - uint8_t rx_data_temp[512]; - uint8_t tx_data[512]; - } uart_t; +typedef struct +{ + uint16_t rx_num; + uint8_t rx_data[512]; + uint8_t rx_data_temp[512]; + uint8_t tx_data[512]; +} uart_t; - extern uart_t lcd_uart4; - extern uart_t ble2_uart3; - extern uart_t ble1_uart6; - extern uart_t hart1_uart5; - extern uart_t hart2_uart2; +extern uart_t lcd_uart4; +extern uart_t ble2_uart3; +extern uart_t ble1_uart6; +extern uart_t hart1_uart5; +extern uart_t hart2_uart2; #define DEST_IP_ADDR0 192 #define DEST_IP_ADDR1 168 #define DEST_IP_ADDR2 1 diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 20059c6..e2e598b 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -28,6 +28,7 @@ #include "dac161s997.h" #include "ad7124.h" #include "usart.h" +#include "communication_protocol.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -56,7 +57,7 @@ osThreadId adc_taskHandle; /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ - +extern float current_buff[2]; /* USER CODE END FunctionPrototypes */ void start_tcp_task(void const *argument); @@ -199,8 +200,9 @@ void start_dac_task(void const *argument) /* Infinite loop */ for (;;) { - dac161s997_output(DAC161S997_1, 12.0f); - dac161s997_output(DAC161S997_2, 12.0f); + dac161s997_output(DAC161S997_1, current_buff[0]); + dac161s997_output(DAC161S997_2, current_buff[1]); + vTaskDelay(100); } /* USER CODE END start_dac_task */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index b7907a3..57c3a2e 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -33,12 +33,12 @@ /* USER CODE END 1 */ /** Configure pins as - * Analog - * Input - * Output - * EVENT_OUT - * EXTI -*/ + * Analog + * Input + * Output + * EVENT_OUT + * EXTI + */ void MX_GPIO_Init(void) { @@ -56,15 +56,13 @@ void MX_GPIO_Init(void) HAL_GPIO_WritePin(ETH_RESET_GPIO_Port, ETH_RESET_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, LED3_R_Pin|LED3_G_Pin|DAC1_CS_Pin|DAC2_CS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOB, LED3_R_Pin | LED3_G_Pin | DAC1_CS_Pin | DAC2_CS_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOE, LED3_Y_Pin|LED2_R_Pin|LED2_G_Pin|LED2_Y_Pin - |ADC_CS_Pin|AD7124_SYNC_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOE, LED3_Y_Pin | LED2_R_Pin | LED2_G_Pin | LED2_Y_Pin | ADC_CS_Pin | AD7124_SYNC_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOD, DO_CH4_Pin|DO_CH3_Pin|DO_CH1_Pin|DO_CH2_Pin - |DO_EN_Pin|HART1_RTS_Pin|HART2_RTS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOD, DO_CH4_Pin | DO_CH3_Pin | DO_CH1_Pin | DO_CH2_Pin | DO_EN_Pin | HART1_RTS_Pin | HART2_RTS_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(HART1_RST_GPIO_Port, HART1_RST_Pin, GPIO_PIN_SET); @@ -80,35 +78,35 @@ void MX_GPIO_Init(void) HAL_GPIO_Init(ETH_RESET_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PBPin PBPin */ - GPIO_InitStruct.Pin = LED3_R_Pin|LED3_G_Pin; + GPIO_InitStruct.Pin = LED3_R_Pin | LED3_G_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : PEPin PEPin PEPin PEPin */ - GPIO_InitStruct.Pin = LED3_Y_Pin|LED2_R_Pin|LED2_G_Pin|LED2_Y_Pin; + GPIO_InitStruct.Pin = LED3_Y_Pin | LED2_R_Pin | LED2_G_Pin | LED2_Y_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /*Configure GPIO pins : PDPin PDPin PDPin PDPin */ - GPIO_InitStruct.Pin = DO_CH4_Pin|DO_CH3_Pin|DO_CH1_Pin|DO_CH2_Pin; + GPIO_InitStruct.Pin = DO_CH4_Pin | DO_CH3_Pin | DO_CH1_Pin | DO_CH2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*Configure GPIO pins : PDPin PDPin PDPin PDPin */ - GPIO_InitStruct.Pin = DO_EN_Pin|HART1_RTS_Pin|HART2_RTS_Pin|HART2_RST_Pin; + GPIO_InitStruct.Pin = DO_EN_Pin | HART1_RTS_Pin | HART2_RTS_Pin | HART2_RST_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*Configure GPIO pins : PCPin PCPin */ - GPIO_InitStruct.Pin = DI_CH1_Pin|DI_CH2_Pin; + GPIO_InitStruct.Pin = DI_CH1_Pin | DI_CH2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); @@ -121,19 +119,19 @@ void MX_GPIO_Init(void) HAL_GPIO_Init(HART1_RST_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PAPin PAPin */ - GPIO_InitStruct.Pin = DI_CH3_Pin|DI_CH4_Pin; + GPIO_InitStruct.Pin = DI_CH3_Pin | DI_CH4_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PDPin PDPin */ - GPIO_InitStruct.Pin = HART1_OCD_Pin|HART2_OCD_Pin; + GPIO_InitStruct.Pin = HART1_OCD_Pin | HART2_OCD_Pin; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*Configure GPIO pins : PBPin PBPin */ - GPIO_InitStruct.Pin = DAC1_CS_Pin|DAC2_CS_Pin; + GPIO_InitStruct.Pin = DAC1_CS_Pin | DAC2_CS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; @@ -159,11 +157,10 @@ void MX_GPIO_Init(void) HAL_NVIC_SetPriority(EXTI3_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI3_IRQn); - } /* USER CODE BEGIN 2 */ -void gpio_do_test(gpio_e gpio_num, GPIO_PinState state) +void gpio_do_test(uint8_t gpio_num, GPIO_PinState state) { HAL_GPIO_WritePin(DO_EN_GPIO_Port, DO_EN_Pin, GPIO_PIN_RESET); switch (gpio_num) @@ -185,7 +182,7 @@ void gpio_do_test(gpio_e gpio_num, GPIO_PinState state) } } -GPIO_PinState gpio_di_test(gpio_e gpio_num) +GPIO_PinState gpio_di_test(uint8_t gpio_num) { GPIO_PinState state; switch (gpio_num) diff --git a/MDK-ARM/TEST2.uvoptx b/MDK-ARM/TEST2.uvoptx index e07726f..2eb454a 100644 --- a/MDK-ARM/TEST2.uvoptx +++ b/MDK-ARM/TEST2.uvoptx @@ -148,7 +148,24 @@ -UB -O2254 -SF1800 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(2BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F407VGTx$CMSIS\Flash\STM32F4xx_1024.FLM) - + + + 0 + 0 + 4 + 1 +
0
+ 0 + 0 + 0 + 0 + 0 + 0 + ../User/application/inc/communication_protocol.h + + +
+
0 @@ -213,7 +230,7 @@ 12 1 - ad7124_analog,0x10 + ad7124_analog,0x0A 13 @@ -501,7 +518,7 @@ Drivers/STM32F4xx_HAL_Driver - 1 + 0 0 0 0 @@ -1773,6 +1790,30 @@ 0 0 + + 10 + 118 + 1 + 1 + 0 + 0 + ..\User\application\src\communication_protocol.c + communication_protocol.c + 0 + 0 + + + 10 + 119 + 1 + 0 + 0 + 0 + ..\User\application\src\user_lib.c + user_lib.c + 0 + 0 + @@ -1783,7 +1824,7 @@ 0 11 - 118 + 120 1 0 0 @@ -1795,7 +1836,7 @@ 11 - 119 + 121 1 0 0 @@ -1807,7 +1848,7 @@ 11 - 120 + 122 1 0 0 @@ -1821,13 +1862,13 @@ User/system - 0 + 1 0 0 0 12 - 121 + 123 1 0 0 @@ -1837,6 +1878,18 @@ 0 0 + + 12 + 124 + 1 + 0 + 0 + 0 + ..\User\system\user_gpio.c + user_gpio.c + 0 + 0 + @@ -1847,7 +1900,7 @@ 0 13 - 122 + 125 1 0 0 @@ -1859,7 +1912,7 @@ 13 - 123 + 126 1 0 0 @@ -1871,7 +1924,7 @@ 13 - 124 + 127 1 0 0 @@ -1883,7 +1936,7 @@ 13 - 125 + 128 1 0 0 @@ -1895,7 +1948,7 @@ 13 - 126 + 129 1 0 0 @@ -1907,7 +1960,7 @@ 13 - 127 + 130 1 0 0 @@ -1919,7 +1972,7 @@ 13 - 128 + 131 1 0 0 @@ -1931,7 +1984,7 @@ 13 - 129 + 132 1 0 0 @@ -1943,7 +1996,7 @@ 13 - 130 + 133 1 0 0 @@ -1955,7 +2008,7 @@ 13 - 131 + 134 1 0 0 diff --git a/MDK-ARM/TEST2.uvprojx b/MDK-ARM/TEST2.uvprojx index 7eeb951..bfca159 100644 --- a/MDK-ARM/TEST2.uvprojx +++ b/MDK-ARM/TEST2.uvprojx @@ -1523,6 +1523,16 @@ 1 ..\User\application\src\tcpserverc.c + + communication_protocol.c + 1 + ..\User\application\src\communication_protocol.c + + + user_lib.c + 1 + ..\User\application\src\user_lib.c + @@ -1553,6 +1563,11 @@ 1 ..\User\system\user_spi.c + + user_gpio.c + 1 + ..\User\system\user_gpio.c + diff --git a/MDK-ARM/TEST2/TEST2.build_log.htm b/MDK-ARM/TEST2/TEST2.build_log.htm deleted file mode 100644 index 31a5b4e..0000000 --- a/MDK-ARM/TEST2/TEST2.build_log.htm +++ /dev/null @@ -1,190 +0,0 @@ - - -
-

Vision Build Log

-

Tool Versions:

-IDE-Version: Vision V5.36.0.0 -Copyright (C) 2021 ARM Ltd and ARM Germany GmbH. All rights reserved. -License Information: aaa Administrator, aaa, LIC=6XJT4-F8J98-8YUVV-P833R-DBAKX-Y8EU6 - -Tool Versions: -Toolchain: MDK-ARM Professional Version: 5.36.0.0 -Toolchain Path: C:\Keil_v5\ARM\ARMCC\Bin -C Compiler: Armcc.exe V5.06 update 7 (build 960) -Assembler: Armasm.exe V5.06 update 7 (build 960) -Linker/Locator: ArmLink.exe V5.06 update 7 (build 960) -Library Manager: ArmAr.exe V5.06 update 7 (build 960) -Hex Converter: FromElf.exe V5.06 update 7 (build 960) -CPU DLL: SARMCM3.DLL V5.36.0.0 -Dialog DLL: DCM.DLL V1.17.3.0 -Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V3.0.9.0 -Dialog DLL: TCM.DLL V1.53.0.0 - -

Project:

-D:\WORK\TEST1\TEST2\MDK-ARM\TEST2.uvprojx -Project File Date: 02/10/2025 - -

Output:

-*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' -Build target 'TEST2' -assembling startup_stm32f407xx.s... -compiling lan8742.c... -compiling main.c... -compiling stm32f4xx_it.c... -compiling spi.c... -compiling stm32f4xx_hal_msp.c... -compiling stm32f4xx_hal_timebase_tim.c... -compiling dma.c... -compiling freertos.c... -compiling gpio.c... -compiling usart.c... -compiling tim.c... -compiling stm32f4xx_hal_rcc.c... -compiling lwip.c... -compiling ethernetif.c... -compiling stm32f4xx_hal_rcc_ex.c... -compiling stm32f4xx_hal_flash.c... -compiling stm32f4xx_hal_flash_ramfunc.c... -compiling stm32f4xx_hal_flash_ex.c... -compiling stm32f4xx_hal_gpio.c... -compiling stm32f4xx_hal_pwr.c... -compiling stm32f4xx_hal_dma.c... -compiling stm32f4xx_hal_dma_ex.c... -compiling stm32f4xx_hal_pwr_ex.c... -compiling stm32f4xx_hal.c... -compiling stm32f4xx_hal_cortex.c... -compiling stm32f4xx_hal_exti.c... -compiling stm32f4xx_hal_spi.c... -compiling stm32f4xx_hal_eth.c... -compiling stm32f4xx_hal_tim_ex.c... -compiling stm32f4xx_hal_tim.c... -compiling stm32f4xx_hal_uart.c... -compiling system_stm32f4xx.c... -compiling ccp.c... -compiling chap_ms.c... -compiling auth.c... -compiling chap-md5.c... -compiling chap-new.c... -compiling demand.c... -compiling eui64.c... -compiling eap.c... -compiling fsm.c... -compiling ipcp.c... -compiling ipv6cp.c... -compiling lcp.c... -compiling magic.c... -compiling mppe.c... -compiling ppp.c... -compiling multilink.c... -compiling pppapi.c... -compiling pppoe.c... -compiling pppol2tp.c... -compiling pppcrypt.c... -compiling pppos.c... -compiling upap.c... -compiling utils.c... -compiling vj.c... -compiling bridgeif.c... -compiling ethernet.c... -compiling bridgeif_fdb.c... -compiling lowpan6.c... -compiling lowpan6_ble.c... -compiling lowpan6_common.c... -compiling slipif.c... -compiling zepif.c... -compiling ecp.c... -compiling err.c... -compiling api_lib.c... -compiling netdb.c... -compiling api_msg.c... -compiling if_api.c... -compiling netifapi.c... -compiling altcp.c... -compiling netbuf.c... -compiling tcpip.c... -compiling altcp_alloc.c... -compiling sockets.c... -compiling altcp_tcp.c... -compiling def.c... -compiling dns.c... -compiling init.c... -compiling ip.c... -compiling inet_chksum.c... -compiling memp.c... -compiling mem.c... -compiling netif.c... -compiling raw.c... -compiling stats.c... -compiling sys.c... -compiling pbuf.c... -compiling tcp.c... -compiling tcp_in.c... -compiling timeouts.c... -compiling tcp_out.c... -compiling udp.c... -compiling autoip.c... -compiling dhcp.c... -compiling igmp.c... -compiling etharp.c... -compiling icmp.c... -compiling ip4.c... -compiling ip4_addr.c... -compiling dhcp6.c... -compiling ip4_frag.c... -compiling ethip6.c... -compiling icmp6.c... -compiling inet6.c... -compiling ip6.c... -compiling croutine.c... -compiling ip6_addr.c... -compiling ip6_frag.c... -compiling mld6.c... -compiling nd6.c... -compiling sys_arch.c... -compiling leds.c... -compiling event_groups.c... -compiling tcpclient.c... -compiling tcpserverc.c... -compiling list.c... -compiling dac161s997.c... -compiling mqtt.c... -compiling stream_buffer.c... -compiling queue.c... -compiling ht1200m.c... -compiling user_spi.c... -compiling ad7124.c... -compiling timers.c... -compiling tasks.c... -compiling heap_4.c... -compiling cmsis_os.c... -compiling port.c... -linking... -Program Size: Code=85896 RO-data=1388 RW-data=1508 ZI-data=61092 -"TEST2\TEST2.axf" - 0 Error(s), 0 Warning(s). - -

Software Packages used:

- -Package Vendor: ARM - http://www.keil.com/pack/ARM.CMSIS.5.8.0.pack - ARM.CMSIS.5.8.0 - CMSIS (Common Microcontroller Software Interface Standard) - * Component: CORE Version: 5.5.0 - -Package Vendor: Keil - http://www.keil.com/pack/Keil.STM32F4xx_DFP.2.12.0.pack - Keil.STM32F4xx_DFP.2.12.0 - STMicroelectronics STM32F4 Series Device Support, Drivers and Examples - -

Collection of Component include folders:

- .\RTE\_TEST2 - C:\Users\Administrator\AppData\Local\Arm\Packs\ARM\CMSIS\5.8.0\CMSIS\Core\Include - C:\Users\Administrator\AppData\Local\Arm\Packs\Keil\STM32F4xx_DFP\2.12.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include - -

Collection of Component Files used:

- - * Component: ARM::CMSIS:CORE:5.5.0 - Include file: CMSIS\Core\Include\tz_context.h -Build Time Elapsed: 00:00:13 -
- - diff --git a/User/application/inc/communication_protocol.h b/User/application/inc/communication_protocol.h new file mode 100644 index 0000000..0a5bc6c --- /dev/null +++ b/User/application/inc/communication_protocol.h @@ -0,0 +1,56 @@ +#ifndef _COMMUNICATION_PROTOCOL_H_ +#define _COMMUNICATION_PROTOCOL_H_ + +#include "user_lib.h" + +#define COM_ERROR_CODE_SIZE 7 // 通信错误返回帧长度 +#define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度 +#define FRAME_HEAD 0xAA // 帧头 +#define FRAME_TAIL 0x3C // 帧尾 + +typedef enum +{ + COM_OK = 0, + FRAMING_ERROR, + CHECK_ERROR, + COMMAND_ERROR, + DEVICE_ERROR, +} communication_error_e; + +typedef struct +{ + uint8_t start_addr; + uint8_t num; + uint8_t data[16]; +} communication_do_t; + +typedef struct +{ + uint8_t start_addr; + uint8_t num; +} communication_di_t; + +typedef struct +{ + uint8_t channel; + float32_u data; +} communication_ao_t; + +typedef struct +{ + uint8_t channel; +} communication_ai_t; + +typedef union +{ + uint8_t data[50]; + communication_do_t do_data; + communication_di_t di_data; + communication_ao_t ao_data; + communication_ai_t ai_data; +} communication_data_u; + +void communication_exception(uint8_t *tx_data, const uint8_t *const rx_data, communication_error_e error_code); +void communication_get_ai(communication_ai_t *ai_data, uint8_t *tx_data, const uint8_t *const rx_data); +void communication_set_ao(communication_ao_t *ao_data); +#endif diff --git a/User/application/inc/user_lib.h b/User/application/inc/user_lib.h new file mode 100644 index 0000000..7461a21 --- /dev/null +++ b/User/application/inc/user_lib.h @@ -0,0 +1,41 @@ +#ifndef _USER_LIB_H_ +#define _USER_LIB_H_ + +#include + +typedef union +{ + float f; + int32_t c; +} float32_u; // 浮点数共用体 + +// uint32小端转大端 +#define S2B_UINT32(a) \ + (((uint32_t)(a) & 0xFF000000) >> 24) + (((uint32_t)(a) & 0x00FF0000) >> 8) + (((uint32_t)(a) & 0x0000FF00) << 8) + (((uint32_t)(a) & 0x000000FF) << 24) + +// uint32大端转小端 +#define B2S_UINT32(a) S2B_UINT32(a) + +// uint16小端转大端 +#define S2B_UINT16(a) ((((uint16_t)(a) & 0xFF00) >> 8) + (((uint16_t)(a) & 0x00FF) << 8)) + +// uint16大端转小端 +#define B2S_UINT16(a) S2B_UINT16(a) + +#define BUILD_UINT16(loByte, hiByte) \ + ((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8))) + +// float32小端转大端 +static inline float S2B_FLOAT32(float fv) +{ + float32_u _f; + _f.f = fv; + _f.c = S2B_UINT32(_f.c); + return _f.f; +} + +// float32大端转小端 +#define B2S_FLOAT32(a) S2B_FLOAT32(a) + +uint8_t xor_compute(const uint8_t *const data, uint16_t length); +#endif diff --git a/User/application/src/communication_protocol.c b/User/application/src/communication_protocol.c new file mode 100644 index 0000000..838067f --- /dev/null +++ b/User/application/src/communication_protocol.c @@ -0,0 +1,49 @@ +#include "communication_protocol.h" +#include "user_lib.h" +#include "ad7124.h" + +extern float current_buff[2]; +extern ad7124_analog_t ad7124_analog[AD7124_CHANNEL_EN_MAX]; +/** + * @brief 处理通信异常 + * + * 当通信发生异常时,此函数将构造一个包含错误信息的TCP数据包,并填充相应的数据。 + * + * @param tx_data 发送的数据指针 + * @param rx_data 接收的数据指针 + * @param error_code 通信错误代码 + */ +void communication_exception(uint8_t *tx_data, const uint8_t *const rx_data, communication_error_e error_code) +{ + uint8_t tx_data_len = COM_ERROR_CODE_SIZE; + tx_data[0] = FRAME_HEAD; // 帧头 + tx_data[1] = error_code; // 状态 + tx_data[2] = rx_data[2]; // 设备号 + tx_data[3] = rx_data[3]; // 命令号 + tx_data[4] = 0x00; // 数据长度 + tx_data[5] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验 + tx_data[6] = FRAME_TAIL; // 帧尾 +} + +void communication_get_ai(communication_ai_t *ai_data, uint8_t *tx_data, const uint8_t *const rx_data) +{ + float32_u analog_data; + uint8_t tx_data_len = COM_AI_DATA_SIZE; + analog_data.f = S2B_FLOAT32(ad7124_analog[ai_data->channel].current); + tx_data[0] = FRAME_HEAD; // 帧头 + tx_data[1] = COM_OK; // 状态 + tx_data[2] = rx_data[2]; // 设备号 + tx_data[3] = rx_data[3]; // 命令号 + tx_data[4] = 4; // 数据长度 + tx_data[5] = analog_data.c; // 数据 + tx_data[6] = analog_data.c >> 8; // 数据 + tx_data[7] = analog_data.c >> 16; // 数据 + tx_data[8] = analog_data.c >> 24; // 数据 + tx_data[9] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验 + tx_data[10] = FRAME_TAIL; // 帧尾 +} + +void communication_set_ao(communication_ao_t *ao_data) +{ + current_buff[ao_data->channel] = B2S_FLOAT32(ao_data->data.f); +} diff --git a/User/application/src/tcpserverc.c b/User/application/src/tcpserverc.c index 5c9af76..b79fb50 100644 --- a/User/application/src/tcpserverc.c +++ b/User/application/src/tcpserverc.c @@ -11,11 +11,21 @@ #include "usart.h" #include "main.h" #include "ht1200m.h" +#include "user_lib.h" +#include "communication_protocol.h" +#include "user_gpio.h" + struct tcp_pcb *server_pcb_hart1 = NULL; struct tcp_pcb *server_pcb_hart2 = NULL; struct tcp_pcb *server_pcb_ble1 = NULL; struct tcp_pcb *server_pcb_ble2 = NULL; struct tcp_pcb *server_pcb_control = NULL; + +communication_di_t *user_communication_di = NULL; +communication_do_t *user_communication_do = NULL; +communication_ai_t *user_communication_ai = NULL; +communication_ao_t *user_communication_ao = NULL; + extern uint8_t tcp_echo_flags_hart1; extern uint8_t tcp_echo_flags_hart2; extern uint8_t tcp_echo_flags_ble1; @@ -117,25 +127,81 @@ static err_t tcpecho_recv_ble2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, } static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) -{ +{ uint8_t tcp_rx_data[128] = {0}; // 接受数据缓存区 + uint8_t tcp_tx_data[128] = {0}; // 发送数据缓存区 + uint8_t rx_data_len = 0; + uint8_t tx_data_len = 0; + communication_data_u communication_data; if (p != NULL) { /* 更新窗口*/ - tcp_echo_flags_control = 1; + tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 server_pcb_control = tpcb; // 直接赋值 memcpy(tcp_rx_data, (int *)p->payload, p->tot_len); + rx_data_len = p->tot_len; /*1. 对接收的数据做异或校验、帧头帧尾判断,校验失败返回信息,校验通过继续下一步、校验数据从帧头后面到校验位结束*/ - /*2. 判断所要执行的操作 读或写指令*/ - /*3. 对要发送的数据进行校验,组包,返回数据*/ + if (tcp_rx_data[0] != 0xAA || tcp_rx_data[rx_data_len - 1] != 0x3C) // 帧头帧尾判断 + { + tx_data_len = COM_ERROR_CODE_SIZE; + communication_exception(tcp_tx_data, tcp_rx_data, FRAMING_ERROR); + tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); + } + else + { + if (tcp_rx_data[rx_data_len - 2] != xor_compute(tcp_rx_data + 1, rx_data_len - 3)) // 异或校验 + { + tx_data_len = COM_ERROR_CODE_SIZE; + communication_exception(tcp_tx_data, tcp_rx_data, CHECK_ERROR); + tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); + } + memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]); + if (tcp_rx_data[3] == 0x00) // 读模拟量指令 + { + /*读操作,从寄存器读取数据,组包返回*/ + tx_data_len = COM_AI_DATA_SIZE; + user_communication_ai = &communication_data.ai_data; + communication_get_ai(user_communication_ai, tcp_tx_data, tcp_rx_data); + tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); + } + else if (tcp_rx_data[3] == 0x01) // 写模拟量指令 + { + /*写操作,将数据写入寄存器,组包返回*/ + tcp_echo_flags_control = 1; + user_communication_ao = &communication_data.ao_data; + communication_set_ao(user_communication_ao); + tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); + } + else if (tcp_rx_data[3] == 0x02) // 读数字量指令 + { + } + else if (tcp_rx_data[3] == 0x03) // 写数字量指令 + { + /*写操作,将数据写入寄存器,组包返回*/ + user_communication_do = &communication_data.do_data; + user_write_gpio(user_communication_do); + tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); + } + else + { + // 返回命令号错误 + tx_data_len = COM_ERROR_CODE_SIZE; + communication_exception(tcp_tx_data, tcp_rx_data, COMMAND_ERROR); + tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); + } - pbuf_free(p); + /*2. 判断所要执行的操作 读或写指令*/ + /*3. 对要发送的数据进行校验,组包,返回数据*/ + + pbuf_free(p); + } } else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空 { return tcp_close(tpcb); } + return ERR_OK; } static err_t tcpecho_accept_hart1(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的 diff --git a/User/application/src/user_lib.c b/User/application/src/user_lib.c new file mode 100644 index 0000000..eab9582 --- /dev/null +++ b/User/application/src/user_lib.c @@ -0,0 +1,22 @@ +#include "user_lib.h" + +/** + * 计算并返回指定数据区域异或的值 + * + * @param data: 待计算的数据区首地址 + * @param length: 待计算的数据区长度 + * + * @return 异或计算的结果 + */ +uint8_t xor_compute(const uint8_t *const data, uint16_t length) +{ + uint16_t i; + const uint8_t *ptr = data; + uint8_t xor = 0; + for (i = 0; i < length; i++) + { + xor ^= *ptr; + ptr++; + } + return xor; +} diff --git a/User/driver/ad7124.c b/User/driver/ad7124.c index 5f3fd88..452123d 100644 --- a/User/driver/ad7124.c +++ b/User/driver/ad7124.c @@ -39,7 +39,7 @@ static ad7124_st_reg_t ad7124_regs[AD7124_REG_NO] = { {AD7124_CONFIG_5, 0x0860, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Config_5 */ {AD7124_CONFIG_6, 0x0860, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Config_6 */ {AD7124_CONFIG_7, 0x0860, AD7124_SIZE_2, AD7124_RW}, /* AD7124_Config_7 */ - {AD7124_FILTER_0, 0x960000, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Filter_0 */ + {AD7124_FILTER_0, 0x060080, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Filter_0 */ {AD7124_FILTER_1, 0x060180, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Filter_1 */ {AD7124_FILTER_2, 0x060180, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Filter_2 */ {AD7124_FILTER_3, 0x060180, AD7124_SIZE_3, AD7124_RW}, /* AD7124_Filter_3 */ diff --git a/User/system/user_gpio.c b/User/system/user_gpio.c new file mode 100644 index 0000000..2e09505 --- /dev/null +++ b/User/system/user_gpio.c @@ -0,0 +1,29 @@ +#include "user_gpio.h" + +void user_write_gpio(communication_do_t *do_data) +{ + uint8_t i = 0; + uint8_t start_addr = do_data->start_addr; // 写输出的起始地址 + uint8_t length = do_data->num; // 写输出的数量 + for (i = 0; i < length; i++) + { + if (do_data->data[i] == 0x01) + { + gpio_do_test(DO_1 + start_addr + i, GPIO_PIN_SET); + } + else + { + gpio_do_test(DO_1 + start_addr + i, GPIO_PIN_RESET); + } + } +} +void user_read_gpio(uint8_t *const tx_data, uint8_t *const rx_data, uint16_t length) +{ + uint8_t i = 0; + uint8_t start_addr = rx_data[0]; // 读输入的起始地址 + uint8_t leng = rx_data[1]; // 读输入的数量 + for (i = 0; i < leng; i++) + { + tx_data[i] = gpio_di_test(DI_1 + start_addr + i); + } +} diff --git a/User/system/user_gpio.h b/User/system/user_gpio.h new file mode 100644 index 0000000..ffc3fa3 --- /dev/null +++ b/User/system/user_gpio.h @@ -0,0 +1,10 @@ +#ifndef __USER_GPIO_H__ +#define __USER_GPIO_H__ +#include "gpio.h" +#include "main.h" +#include "communication_protocol.h" + +void user_write_gpio(communication_do_t *do_data); +void user_read_gpio(uint8_t *const tx_data, uint8_t *const rx_data, uint16_t length); + +#endif