├── .gitignore
├── Project
├── .mxproject
├── Core
│ ├── Inc
│ │ ├── main.h
│ │ ├── stm32f1xx_hal_conf.h
│ │ └── stm32f1xx_it.h
│ └── Src
│ │ ├── main.c
│ │ ├── stm32f1xx_hal_msp.c
│ │ ├── stm32f1xx_it.c
│ │ └── system_stm32f1xx.c
├── Drivers
│ ├── CMSIS
│ │ ├── Device
│ │ │ └── ST
│ │ │ │ └── STM32F1xx
│ │ │ │ └── Include
│ │ │ │ ├── stm32f103xe.h
│ │ │ │ ├── stm32f1xx.h
│ │ │ │ └── system_stm32f1xx.h
│ │ └── Include
│ │ │ ├── cmsis_armcc.h
│ │ │ ├── cmsis_armclang.h
│ │ │ ├── cmsis_compiler.h
│ │ │ ├── cmsis_gcc.h
│ │ │ ├── cmsis_iccarm.h
│ │ │ ├── cmsis_version.h
│ │ │ ├── core_armv8mbl.h
│ │ │ ├── core_armv8mml.h
│ │ │ ├── core_cm0.h
│ │ │ ├── core_cm0plus.h
│ │ │ ├── core_cm1.h
│ │ │ ├── core_cm23.h
│ │ │ ├── core_cm3.h
│ │ │ ├── core_cm33.h
│ │ │ ├── core_cm4.h
│ │ │ ├── core_cm7.h
│ │ │ ├── core_sc000.h
│ │ │ ├── core_sc300.h
│ │ │ ├── mpu_armv7.h
│ │ │ ├── mpu_armv8.h
│ │ │ └── tz_context.h
│ └── STM32F1xx_HAL_Driver
│ │ ├── Inc
│ │ ├── Legacy
│ │ │ └── stm32_hal_legacy.h
│ │ ├── stm32f1xx_hal.h
│ │ ├── stm32f1xx_hal_cortex.h
│ │ ├── stm32f1xx_hal_def.h
│ │ ├── stm32f1xx_hal_dma.h
│ │ ├── stm32f1xx_hal_dma_ex.h
│ │ ├── stm32f1xx_hal_exti.h
│ │ ├── stm32f1xx_hal_flash.h
│ │ ├── stm32f1xx_hal_flash_ex.h
│ │ ├── stm32f1xx_hal_gpio.h
│ │ ├── stm32f1xx_hal_gpio_ex.h
│ │ ├── stm32f1xx_hal_iwdg.h
│ │ ├── stm32f1xx_hal_pwr.h
│ │ ├── stm32f1xx_hal_rcc.h
│ │ ├── stm32f1xx_hal_rcc_ex.h
│ │ ├── stm32f1xx_hal_spi.h
│ │ ├── stm32f1xx_hal_tim.h
│ │ ├── stm32f1xx_hal_tim_ex.h
│ │ └── stm32f1xx_hal_uart.h
│ │ └── Src
│ │ ├── stm32f1xx_hal.c
│ │ ├── stm32f1xx_hal_cortex.c
│ │ ├── stm32f1xx_hal_dma.c
│ │ ├── stm32f1xx_hal_exti.c
│ │ ├── stm32f1xx_hal_flash.c
│ │ ├── stm32f1xx_hal_flash_ex.c
│ │ ├── stm32f1xx_hal_gpio.c
│ │ ├── stm32f1xx_hal_gpio_ex.c
│ │ ├── stm32f1xx_hal_iwdg.c
│ │ ├── stm32f1xx_hal_pwr.c
│ │ ├── stm32f1xx_hal_rcc.c
│ │ ├── stm32f1xx_hal_rcc_ex.c
│ │ ├── stm32f1xx_hal_spi.c
│ │ ├── stm32f1xx_hal_tim.c
│ │ ├── stm32f1xx_hal_tim_ex.c
│ │ └── stm32f1xx_hal_uart.c
├── MDK-ARM
│ ├── EventRecorderStub.scvd
│ ├── Project.uvoptx
│ ├── Project.uvprojx
│ ├── RTE
│ │ ├── RTOS
│ │ │ ├── board.c
│ │ │ └── rtconfig.h
│ │ └── _Project
│ │ │ └── RTE_Components.h
│ ├── startup_stm32f103xe.lst
│ └── startup_stm32f103xe.s
├── Project.ioc
├── modules
│ ├── at
│ │ ├── include
│ │ │ ├── at.h
│ │ │ └── at_log.h
│ │ └── src
│ │ │ ├── at_cli.c
│ │ │ ├── at_client.c
│ │ │ └── at_utils.c
│ ├── console
│ │ └── console.c
│ ├── init_module
│ │ ├── init_module.c
│ │ └── init_module.h
│ ├── key
│ │ └── key.c
│ ├── led
│ │ ├── led.c
│ │ └── led.h
│ ├── main_hook
│ │ ├── main_hook.c
│ │ └── main_hook.h
│ ├── oled
│ │ ├── oled.c
│ │ └── oled.h
│ ├── ring
│ │ ├── ringblk_buf.c
│ │ ├── ringblk_buf.h
│ │ ├── ringbuffer.c
│ │ └── ringbuffer.h
│ ├── rtu_master
│ │ └── rtu_master.c
│ ├── runtime
│ │ └── runtime.c
│ ├── ulog
│ │ ├── SConscript
│ │ ├── backend
│ │ │ └── console_be.c
│ │ ├── syslog
│ │ │ ├── syslog.c
│ │ │ └── syslog.h
│ │ ├── ulog.c
│ │ ├── ulog.h
│ │ └── ulog_def.h
│ ├── usr_device
│ │ ├── usr_device.c
│ │ └── usr_device.h
│ └── wifi
│ │ ├── wifi.c
│ │ ├── wifi.h
│ │ └── wifi_tcp_slave.c
├── packages
│ ├── agile_button
│ │ ├── .gitignore
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── SConscript
│ │ ├── inc
│ │ │ └── agile_button.h
│ │ └── src
│ │ │ ├── SConscript
│ │ │ └── agile_button.c
│ ├── agile_led
│ │ ├── .gitignore
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── SConscript
│ │ ├── inc
│ │ │ └── agile_led.h
│ │ └── src
│ │ │ ├── SConscript
│ │ │ └── agile_led.c
│ └── agile_modbus
│ │ ├── .gitignore
│ │ ├── SConscript
│ │ ├── inc
│ │ ├── agile_modbus.h
│ │ ├── agile_modbus_rtu.h
│ │ └── agile_modbus_tcp.h
│ │ └── src
│ │ ├── SConscript
│ │ ├── agile_modbus.c
│ │ ├── agile_modbus_rtu.c
│ │ └── agile_modbus_tcp.c
└── usr-drivers
│ ├── gpio
│ ├── drv_gpio.c
│ └── drv_gpio.h
│ ├── usart
│ ├── config
│ │ ├── drv_usart_config.c
│ │ └── drv_usart_config.h
│ ├── drv_usart.c
│ └── drv_usart.h
│ └── wdt
│ └── drv_wdt.c
├── README.md
├── docs
├── DMA分配.md
└── 手册
│ ├── 0.96寸OLED规格书.pdf
│ ├── DS5792.pdf
│ ├── ESP-01S规格书v1.0.pdf
│ ├── ESP-01系列模组用户手册v1.5.pdf
│ ├── ESP8266-AT指令使用示例v1.3.pdf
│ ├── ESP8266-AT指令集v3.0.1.pdf
│ ├── ESP8266系列入门教程v2.0.pdf
│ ├── RM0008.pdf
│ ├── 中景园电子0.96OLED显示屏_驱动芯片手册.pdf
│ └── 中景园电子0.96寸OLED使用文档新手必看V2.0.pdf
└── 原理图
└── (新版)MINI-STM32--原理图.pdf
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | *.map
3 | *.dblite
4 | *.elf
5 | *.bin
6 | *.hex
7 | *.axf
8 | *.exe
9 | *.pdb
10 | *.idb
11 | *.ilk
12 | *.old
13 | build
14 | Debug
15 | documentation/html
16 | *~
17 | *.o
18 | *.obj
19 | *.out
20 | *.bak
21 | *.dep
22 | *.lib
23 | *.i
24 | *.d
25 | .DS_Stor*
26 | .config 3
27 | .config 4
28 | .config 5
29 | Midea-X1
30 | *.uimg
31 | GPATH
32 | GRTAGS
33 | GTAGS
34 | .vscode
35 | JLinkLog.txt
36 | JLinkSettings.ini
37 | DebugConfig/
38 | settings/
39 | *.uvguix*
40 | cconfig.h
41 | si
--------------------------------------------------------------------------------
/Project/.mxproject:
--------------------------------------------------------------------------------
1 | [PreviousLibFiles]
2 | LibFiles=Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_iwdg.h;Drivers/STM32F1xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_def.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_gpio.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_gpio_ex.h;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_dma_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_dma.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_cortex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_flash.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_flash_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_exti.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_spi.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_uart.h;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_iwdg.h;Drivers/STM32F1xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_def.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_rcc_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_gpio.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_gpio_ex.h;Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_dma_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_dma.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_cortex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_pwr.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_flash.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_flash_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_exti.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_spi.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim_ex.h;Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_uart.h;Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h;Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h;Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h;Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/tz_context.h;
3 |
4 | [PreviousUsedKeilFiles]
5 | SourceFiles=..\Core\Src\main.c;..\Core\Src\stm32f1xx_it.c;..\Core\Src\stm32f1xx_hal_msp.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c;..\Core\Src/system_stm32f1xx.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_iwdg.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_spi.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c;..\Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c;..\Core\Src/system_stm32f1xx.c;..\Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c;;
6 | HeaderPath=..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F1xx\Include;..\Drivers\CMSIS\Include;..\Core\Inc;
7 | CDefines=USE_HAL_DRIVER;STM32F103xE;USE_HAL_DRIVER;USE_HAL_DRIVER;
8 |
9 | [PreviousGenFiles]
10 | AdvancedFolderStructure=true
11 | HeaderFileListSize=3
12 | HeaderFiles#0=D:/stm32/STM32_MINI/Project/Core/Inc/stm32f1xx_it.h
13 | HeaderFiles#1=D:/stm32/STM32_MINI/Project/Core/Inc/stm32f1xx_hal_conf.h
14 | HeaderFiles#2=D:/stm32/STM32_MINI/Project/Core/Inc/main.h
15 | HeaderFolderListSize=1
16 | HeaderPath#0=D:/stm32/STM32_MINI/Project/Core/Inc
17 | HeaderFiles=;
18 | SourceFileListSize=3
19 | SourceFiles#0=D:/stm32/STM32_MINI/Project/Core/Src/stm32f1xx_it.c
20 | SourceFiles#1=D:/stm32/STM32_MINI/Project/Core/Src/stm32f1xx_hal_msp.c
21 | SourceFiles#2=D:/stm32/STM32_MINI/Project/Core/Src/main.c
22 | SourceFolderListSize=1
23 | SourcePath#0=D:/stm32/STM32_MINI/Project/Core/Src
24 | SourceFiles=;
25 |
26 |
--------------------------------------------------------------------------------
/Project/Core/Inc/main.h:
--------------------------------------------------------------------------------
1 | /* USER CODE BEGIN Header */
2 | /**
3 | ******************************************************************************
4 | * @file : main.h
5 | * @brief : Header for main.c file.
6 | * This file contains the common defines of the application.
7 | ******************************************************************************
8 | * @attention
9 | *
10 | *
© Copyright (c) 2020 STMicroelectronics.
11 | * All rights reserved.
12 | *
13 | * This software component is licensed by ST under BSD 3-Clause license,
14 | * the "License"; You may not use this file except in compliance with the
15 | * License. You may obtain a copy of the License at:
16 | * opensource.org/licenses/BSD-3-Clause
17 | *
18 | ******************************************************************************
19 | */
20 | /* USER CODE END Header */
21 |
22 | /* Define to prevent recursive inclusion -------------------------------------*/
23 | #ifndef __MAIN_H
24 | #define __MAIN_H
25 |
26 | #ifdef __cplusplus
27 | extern "C" {
28 | #endif
29 |
30 | /* Includes ------------------------------------------------------------------*/
31 | #include "stm32f1xx_hal.h"
32 |
33 | /* Private includes ----------------------------------------------------------*/
34 | /* USER CODE BEGIN Includes */
35 |
36 | /* USER CODE END Includes */
37 |
38 | /* Exported types ------------------------------------------------------------*/
39 | /* USER CODE BEGIN ET */
40 |
41 | /* USER CODE END ET */
42 |
43 | /* Exported constants --------------------------------------------------------*/
44 | /* USER CODE BEGIN EC */
45 |
46 | /* USER CODE END EC */
47 |
48 | /* Exported macro ------------------------------------------------------------*/
49 | /* USER CODE BEGIN EM */
50 |
51 | /* USER CODE END EM */
52 |
53 | /* Exported functions prototypes ---------------------------------------------*/
54 | void Error_Handler(void);
55 |
56 | /* USER CODE BEGIN EFP */
57 |
58 | /* USER CODE END EFP */
59 |
60 | /* Private defines -----------------------------------------------------------*/
61 | #define WK_UP_Pin GPIO_PIN_0
62 | #define WK_UP_GPIO_Port GPIOA
63 | #define WIFI_Power_Pin GPIO_PIN_0
64 | #define WIFI_Power_GPIO_Port GPIOB
65 | #define WIFI_RST_Pin GPIO_PIN_1
66 | #define WIFI_RST_GPIO_Port GPIOB
67 | #define OLED_RES_Pin GPIO_PIN_6
68 | #define OLED_RES_GPIO_Port GPIOC
69 | #define OLED_DC_Pin GPIO_PIN_7
70 | #define OLED_DC_GPIO_Port GPIOC
71 | #define OLED_CS_Pin GPIO_PIN_8
72 | #define OLED_CS_GPIO_Port GPIOC
73 | #define D0_Pin GPIO_PIN_8
74 | #define D0_GPIO_Port GPIOA
75 | #define D1_Pin GPIO_PIN_2
76 | #define D1_GPIO_Port GPIOD
77 | /* USER CODE BEGIN Private defines */
78 |
79 | /* USER CODE END Private defines */
80 |
81 | #ifdef __cplusplus
82 | }
83 | #endif
84 |
85 | #endif /* __MAIN_H */
86 |
87 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
88 |
--------------------------------------------------------------------------------
/Project/Core/Inc/stm32f1xx_it.h:
--------------------------------------------------------------------------------
1 | /* USER CODE BEGIN Header */
2 | /**
3 | ******************************************************************************
4 | * @file stm32f1xx_it.h
5 | * @brief This file contains the headers of the interrupt handlers.
6 | ******************************************************************************
7 | * @attention
8 | *
9 | * © Copyright (c) 2020 STMicroelectronics.
10 | * All rights reserved.
11 | *
12 | * This software component is licensed by ST under BSD 3-Clause license,
13 | * the "License"; You may not use this file except in compliance with the
14 | * License. You may obtain a copy of the License at:
15 | * opensource.org/licenses/BSD-3-Clause
16 | *
17 | ******************************************************************************
18 | */
19 | /* USER CODE END Header */
20 |
21 | /* Define to prevent recursive inclusion -------------------------------------*/
22 | #ifndef __STM32F1xx_IT_H
23 | #define __STM32F1xx_IT_H
24 |
25 | #ifdef __cplusplus
26 | extern "C" {
27 | #endif
28 |
29 | /* Private includes ----------------------------------------------------------*/
30 | /* USER CODE BEGIN Includes */
31 |
32 | /* USER CODE END Includes */
33 |
34 | /* Exported types ------------------------------------------------------------*/
35 | /* USER CODE BEGIN ET */
36 |
37 | /* USER CODE END ET */
38 |
39 | /* Exported constants --------------------------------------------------------*/
40 | /* USER CODE BEGIN EC */
41 |
42 | /* USER CODE END EC */
43 |
44 | /* Exported macro ------------------------------------------------------------*/
45 | /* USER CODE BEGIN EM */
46 |
47 | /* USER CODE END EM */
48 |
49 | /* Exported functions prototypes ---------------------------------------------*/
50 | void NMI_Handler(void);
51 | void MemManage_Handler(void);
52 | void BusFault_Handler(void);
53 | void UsageFault_Handler(void);
54 | void SVC_Handler(void);
55 | void DebugMon_Handler(void);
56 | /* USER CODE BEGIN EFP */
57 |
58 | /* USER CODE END EFP */
59 |
60 | #ifdef __cplusplus
61 | }
62 | #endif
63 |
64 | #endif /* __STM32F1xx_IT_H */
65 |
66 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
67 |
--------------------------------------------------------------------------------
/Project/Core/Src/main.c:
--------------------------------------------------------------------------------
1 | /* USER CODE BEGIN Header */
2 | /**
3 | ******************************************************************************
4 | * @file : main.c
5 | * @brief : Main program body
6 | ******************************************************************************
7 | * @attention
8 | *
9 | * © Copyright (c) 2020 STMicroelectronics.
10 | * All rights reserved.
11 | *
12 | * This software component is licensed by ST under BSD 3-Clause license,
13 | * the "License"; You may not use this file except in compliance with the
14 | * License. You may obtain a copy of the License at:
15 | * opensource.org/licenses/BSD-3-Clause
16 | *
17 | ******************************************************************************
18 | */
19 | /* USER CODE END Header */
20 | /* Includes ------------------------------------------------------------------*/
21 | #include "main.h"
22 |
23 | /* Private includes ----------------------------------------------------------*/
24 | /* USER CODE BEGIN Includes */
25 | #include "init_module.h"
26 | #include "main_hook.h"
27 | /* USER CODE END Includes */
28 |
29 | /* Private typedef -----------------------------------------------------------*/
30 | /* USER CODE BEGIN PTD */
31 |
32 | /* USER CODE END PTD */
33 |
34 | /* Private define ------------------------------------------------------------*/
35 | /* USER CODE BEGIN PD */
36 | /* USER CODE END PD */
37 |
38 | /* Private macro -------------------------------------------------------------*/
39 | /* USER CODE BEGIN PM */
40 |
41 | /* USER CODE END PM */
42 |
43 | /* Private variables ---------------------------------------------------------*/
44 |
45 | /* USER CODE BEGIN PV */
46 |
47 | /* USER CODE END PV */
48 |
49 | /* Private function prototypes -----------------------------------------------*/
50 | void SystemClock_Config(void);
51 | /* USER CODE BEGIN PFP */
52 |
53 | /* USER CODE END PFP */
54 |
55 | /* Private user code ---------------------------------------------------------*/
56 | /* USER CODE BEGIN 0 */
57 |
58 | int main(void)
59 | {
60 | init_module_init();
61 |
62 | while(1)
63 | {
64 | rt_thread_mdelay(100);
65 | main_hook_process();
66 | }
67 | }
68 |
69 | /* USER CODE END 0 */
70 |
71 | /**
72 | * @brief The application entry point.
73 | * @retval int
74 | */
75 |
76 | /**
77 | * @brief System Clock Configuration
78 | * @retval None
79 | */
80 | void SystemClock_Config(void)
81 | {
82 | RCC_OscInitTypeDef RCC_OscInitStruct = {0};
83 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
84 |
85 | /** Initializes the RCC Oscillators according to the specified parameters
86 | * in the RCC_OscInitTypeDef structure.
87 | */
88 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
89 | RCC_OscInitStruct.HSEState = RCC_HSE_ON;
90 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
91 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
92 | RCC_OscInitStruct.LSIState = RCC_LSI_ON;
93 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
94 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
95 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
96 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
97 | {
98 | Error_Handler();
99 | }
100 | /** Initializes the CPU, AHB and APB buses clocks
101 | */
102 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
103 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
104 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
105 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
106 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
107 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
108 |
109 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
110 | {
111 | Error_Handler();
112 | }
113 | }
114 |
115 | /* USER CODE BEGIN 4 */
116 |
117 | /* USER CODE END 4 */
118 |
119 | /**
120 | * @brief This function is executed in case of error occurrence.
121 | * @retval None
122 | */
123 | void Error_Handler(void)
124 | {
125 | /* USER CODE BEGIN Error_Handler_Debug */
126 | /* User can add his own implementation to report the HAL error return state */
127 | __disable_irq();
128 | while (1)
129 | {
130 | }
131 | /* USER CODE END Error_Handler_Debug */
132 | }
133 |
134 | #ifdef USE_FULL_ASSERT
135 | /**
136 | * @brief Reports the name of the source file and the source line number
137 | * where the assert_param error has occurred.
138 | * @param file: pointer to the source file name
139 | * @param line: assert_param error line source number
140 | * @retval None
141 | */
142 | void assert_failed(uint8_t *file, uint32_t line)
143 | {
144 | /* USER CODE BEGIN 6 */
145 | /* User can add his own implementation to report the file name and line number,
146 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
147 | /* USER CODE END 6 */
148 | }
149 | #endif /* USE_FULL_ASSERT */
150 |
151 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
152 |
--------------------------------------------------------------------------------
/Project/Core/Src/stm32f1xx_it.c:
--------------------------------------------------------------------------------
1 | /* USER CODE BEGIN Header */
2 | /**
3 | ******************************************************************************
4 | * @file stm32f1xx_it.c
5 | * @brief Interrupt Service Routines.
6 | ******************************************************************************
7 | * @attention
8 | *
9 | * © Copyright (c) 2020 STMicroelectronics.
10 | * All rights reserved.
11 | *
12 | * This software component is licensed by ST under BSD 3-Clause license,
13 | * the "License"; You may not use this file except in compliance with the
14 | * License. You may obtain a copy of the License at:
15 | * opensource.org/licenses/BSD-3-Clause
16 | *
17 | ******************************************************************************
18 | */
19 | /* USER CODE END Header */
20 |
21 | /* Includes ------------------------------------------------------------------*/
22 | #include "main.h"
23 | #include "stm32f1xx_it.h"
24 | /* Private includes ----------------------------------------------------------*/
25 | /* USER CODE BEGIN Includes */
26 | /* USER CODE END Includes */
27 |
28 | /* Private typedef -----------------------------------------------------------*/
29 | /* USER CODE BEGIN TD */
30 |
31 | /* USER CODE END TD */
32 |
33 | /* Private define ------------------------------------------------------------*/
34 | /* USER CODE BEGIN PD */
35 |
36 | /* USER CODE END PD */
37 |
38 | /* Private macro -------------------------------------------------------------*/
39 | /* USER CODE BEGIN PM */
40 |
41 | /* USER CODE END PM */
42 |
43 | /* Private variables ---------------------------------------------------------*/
44 | /* USER CODE BEGIN PV */
45 |
46 | /* USER CODE END PV */
47 |
48 | /* Private function prototypes -----------------------------------------------*/
49 | /* USER CODE BEGIN PFP */
50 |
51 | /* USER CODE END PFP */
52 |
53 | /* Private user code ---------------------------------------------------------*/
54 | /* USER CODE BEGIN 0 */
55 |
56 | /* USER CODE END 0 */
57 |
58 | /* External variables --------------------------------------------------------*/
59 |
60 | /* USER CODE BEGIN EV */
61 |
62 | /* USER CODE END EV */
63 |
64 | /******************************************************************************/
65 | /* Cortex-M3 Processor Interruption and Exception Handlers */
66 | /******************************************************************************/
67 | /**
68 | * @brief This function handles Non maskable interrupt.
69 | */
70 | void NMI_Handler(void)
71 | {
72 | /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
73 |
74 | /* USER CODE END NonMaskableInt_IRQn 0 */
75 | /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
76 | while (1)
77 | {
78 | }
79 | /* USER CODE END NonMaskableInt_IRQn 1 */
80 | }
81 |
82 | /**
83 | * @brief This function handles Memory management fault.
84 | */
85 | void MemManage_Handler(void)
86 | {
87 | /* USER CODE BEGIN MemoryManagement_IRQn 0 */
88 |
89 | /* USER CODE END MemoryManagement_IRQn 0 */
90 | while (1)
91 | {
92 | /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
93 | /* USER CODE END W1_MemoryManagement_IRQn 0 */
94 | }
95 | }
96 |
97 | /**
98 | * @brief This function handles Prefetch fault, memory access fault.
99 | */
100 | void BusFault_Handler(void)
101 | {
102 | /* USER CODE BEGIN BusFault_IRQn 0 */
103 |
104 | /* USER CODE END BusFault_IRQn 0 */
105 | while (1)
106 | {
107 | /* USER CODE BEGIN W1_BusFault_IRQn 0 */
108 | /* USER CODE END W1_BusFault_IRQn 0 */
109 | }
110 | }
111 |
112 | /**
113 | * @brief This function handles Undefined instruction or illegal state.
114 | */
115 | void UsageFault_Handler(void)
116 | {
117 | /* USER CODE BEGIN UsageFault_IRQn 0 */
118 |
119 | /* USER CODE END UsageFault_IRQn 0 */
120 | while (1)
121 | {
122 | /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
123 | /* USER CODE END W1_UsageFault_IRQn 0 */
124 | }
125 | }
126 |
127 | /**
128 | * @brief This function handles System service call via SWI instruction.
129 | */
130 | void SVC_Handler(void)
131 | {
132 | /* USER CODE BEGIN SVCall_IRQn 0 */
133 |
134 | /* USER CODE END SVCall_IRQn 0 */
135 | /* USER CODE BEGIN SVCall_IRQn 1 */
136 |
137 | /* USER CODE END SVCall_IRQn 1 */
138 | }
139 |
140 | /**
141 | * @brief This function handles Debug monitor.
142 | */
143 | void DebugMon_Handler(void)
144 | {
145 | /* USER CODE BEGIN DebugMonitor_IRQn 0 */
146 |
147 | /* USER CODE END DebugMonitor_IRQn 0 */
148 | /* USER CODE BEGIN DebugMonitor_IRQn 1 */
149 |
150 | /* USER CODE END DebugMonitor_IRQn 1 */
151 | }
152 |
153 | /******************************************************************************/
154 | /* STM32F1xx Peripheral Interrupt Handlers */
155 | /* Add here the Interrupt Handlers for the used peripherals. */
156 | /* For the available peripheral interrupt handler names, */
157 | /* please refer to the startup file (startup_stm32f1xx.s). */
158 | /******************************************************************************/
159 |
160 | /* USER CODE BEGIN 1 */
161 |
162 | /* USER CODE END 1 */
163 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
164 |
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/Project/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/Project/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file system_stm32f10x.h
4 | * @author MCD Application Team
5 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File.
6 | ******************************************************************************
7 | * @attention
8 | *
9 | * © Copyright (c) 2017 STMicroelectronics.
10 | * All rights reserved.
11 | *
12 | * This software component is licensed by ST under BSD 3-Clause license,
13 | * the "License"; You may not use this file except in compliance with the
14 | * License. You may obtain a copy of the License at:
15 | * opensource.org/licenses/BSD-3-Clause
16 | *
17 | ******************************************************************************
18 | */
19 |
20 | /** @addtogroup CMSIS
21 | * @{
22 | */
23 |
24 | /** @addtogroup stm32f10x_system
25 | * @{
26 | */
27 |
28 | /**
29 | * @brief Define to prevent recursive inclusion
30 | */
31 | #ifndef __SYSTEM_STM32F10X_H
32 | #define __SYSTEM_STM32F10X_H
33 |
34 | #ifdef __cplusplus
35 | extern "C" {
36 | #endif
37 |
38 | /** @addtogroup STM32F10x_System_Includes
39 | * @{
40 | */
41 |
42 | /**
43 | * @}
44 | */
45 |
46 |
47 | /** @addtogroup STM32F10x_System_Exported_types
48 | * @{
49 | */
50 |
51 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */
52 | extern const uint8_t AHBPrescTable[16U]; /*!< AHB prescalers table values */
53 | extern const uint8_t APBPrescTable[8U]; /*!< APB prescalers table values */
54 |
55 | /**
56 | * @}
57 | */
58 |
59 | /** @addtogroup STM32F10x_System_Exported_Constants
60 | * @{
61 | */
62 |
63 | /**
64 | * @}
65 | */
66 |
67 | /** @addtogroup STM32F10x_System_Exported_Macros
68 | * @{
69 | */
70 |
71 | /**
72 | * @}
73 | */
74 |
75 | /** @addtogroup STM32F10x_System_Exported_Functions
76 | * @{
77 | */
78 |
79 | extern void SystemInit(void);
80 | extern void SystemCoreClockUpdate(void);
81 | /**
82 | * @}
83 | */
84 |
85 | #ifdef __cplusplus
86 | }
87 | #endif
88 |
89 | #endif /*__SYSTEM_STM32F10X_H */
90 |
91 | /**
92 | * @}
93 | */
94 |
95 | /**
96 | * @}
97 | */
98 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
99 |
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Include/cmsis_compiler.h:
--------------------------------------------------------------------------------
1 | /**************************************************************************//**
2 | * @file cmsis_compiler.h
3 | * @brief CMSIS compiler generic header file
4 | * @version V5.0.4
5 | * @date 10. January 2018
6 | ******************************************************************************/
7 | /*
8 | * Copyright (c) 2009-2018 Arm Limited. All rights reserved.
9 | *
10 | * SPDX-License-Identifier: Apache-2.0
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the License); you may
13 | * not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | */
24 |
25 | #ifndef __CMSIS_COMPILER_H
26 | #define __CMSIS_COMPILER_H
27 |
28 | #include
29 |
30 | /*
31 | * Arm Compiler 4/5
32 | */
33 | #if defined ( __CC_ARM )
34 | #include "cmsis_armcc.h"
35 |
36 |
37 | /*
38 | * Arm Compiler 6 (armclang)
39 | */
40 | #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
41 | #include "cmsis_armclang.h"
42 |
43 |
44 | /*
45 | * GNU Compiler
46 | */
47 | #elif defined ( __GNUC__ )
48 | #include "cmsis_gcc.h"
49 |
50 |
51 | /*
52 | * IAR Compiler
53 | */
54 | #elif defined ( __ICCARM__ )
55 | #include
56 |
57 |
58 | /*
59 | * TI Arm Compiler
60 | */
61 | #elif defined ( __TI_ARM__ )
62 | #include
63 |
64 | #ifndef __ASM
65 | #define __ASM __asm
66 | #endif
67 | #ifndef __INLINE
68 | #define __INLINE inline
69 | #endif
70 | #ifndef __STATIC_INLINE
71 | #define __STATIC_INLINE static inline
72 | #endif
73 | #ifndef __STATIC_FORCEINLINE
74 | #define __STATIC_FORCEINLINE __STATIC_INLINE
75 | #endif
76 | #ifndef __NO_RETURN
77 | #define __NO_RETURN __attribute__((noreturn))
78 | #endif
79 | #ifndef __USED
80 | #define __USED __attribute__((used))
81 | #endif
82 | #ifndef __WEAK
83 | #define __WEAK __attribute__((weak))
84 | #endif
85 | #ifndef __PACKED
86 | #define __PACKED __attribute__((packed))
87 | #endif
88 | #ifndef __PACKED_STRUCT
89 | #define __PACKED_STRUCT struct __attribute__((packed))
90 | #endif
91 | #ifndef __PACKED_UNION
92 | #define __PACKED_UNION union __attribute__((packed))
93 | #endif
94 | #ifndef __UNALIGNED_UINT32 /* deprecated */
95 | struct __attribute__((packed)) T_UINT32 { uint32_t v; };
96 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
97 | #endif
98 | #ifndef __UNALIGNED_UINT16_WRITE
99 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
100 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val))
101 | #endif
102 | #ifndef __UNALIGNED_UINT16_READ
103 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
104 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
105 | #endif
106 | #ifndef __UNALIGNED_UINT32_WRITE
107 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
108 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
109 | #endif
110 | #ifndef __UNALIGNED_UINT32_READ
111 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
112 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
113 | #endif
114 | #ifndef __ALIGNED
115 | #define __ALIGNED(x) __attribute__((aligned(x)))
116 | #endif
117 | #ifndef __RESTRICT
118 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
119 | #define __RESTRICT
120 | #endif
121 |
122 |
123 | /*
124 | * TASKING Compiler
125 | */
126 | #elif defined ( __TASKING__ )
127 | /*
128 | * The CMSIS functions have been implemented as intrinsics in the compiler.
129 | * Please use "carm -?i" to get an up to date list of all intrinsics,
130 | * Including the CMSIS ones.
131 | */
132 |
133 | #ifndef __ASM
134 | #define __ASM __asm
135 | #endif
136 | #ifndef __INLINE
137 | #define __INLINE inline
138 | #endif
139 | #ifndef __STATIC_INLINE
140 | #define __STATIC_INLINE static inline
141 | #endif
142 | #ifndef __STATIC_FORCEINLINE
143 | #define __STATIC_FORCEINLINE __STATIC_INLINE
144 | #endif
145 | #ifndef __NO_RETURN
146 | #define __NO_RETURN __attribute__((noreturn))
147 | #endif
148 | #ifndef __USED
149 | #define __USED __attribute__((used))
150 | #endif
151 | #ifndef __WEAK
152 | #define __WEAK __attribute__((weak))
153 | #endif
154 | #ifndef __PACKED
155 | #define __PACKED __packed__
156 | #endif
157 | #ifndef __PACKED_STRUCT
158 | #define __PACKED_STRUCT struct __packed__
159 | #endif
160 | #ifndef __PACKED_UNION
161 | #define __PACKED_UNION union __packed__
162 | #endif
163 | #ifndef __UNALIGNED_UINT32 /* deprecated */
164 | struct __packed__ T_UINT32 { uint32_t v; };
165 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
166 | #endif
167 | #ifndef __UNALIGNED_UINT16_WRITE
168 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
169 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
170 | #endif
171 | #ifndef __UNALIGNED_UINT16_READ
172 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
173 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
174 | #endif
175 | #ifndef __UNALIGNED_UINT32_WRITE
176 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
177 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
178 | #endif
179 | #ifndef __UNALIGNED_UINT32_READ
180 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
181 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
182 | #endif
183 | #ifndef __ALIGNED
184 | #define __ALIGNED(x) __align(x)
185 | #endif
186 | #ifndef __RESTRICT
187 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
188 | #define __RESTRICT
189 | #endif
190 |
191 |
192 | /*
193 | * COSMIC Compiler
194 | */
195 | #elif defined ( __CSMC__ )
196 | #include
197 |
198 | #ifndef __ASM
199 | #define __ASM _asm
200 | #endif
201 | #ifndef __INLINE
202 | #define __INLINE inline
203 | #endif
204 | #ifndef __STATIC_INLINE
205 | #define __STATIC_INLINE static inline
206 | #endif
207 | #ifndef __STATIC_FORCEINLINE
208 | #define __STATIC_FORCEINLINE __STATIC_INLINE
209 | #endif
210 | #ifndef __NO_RETURN
211 | // NO RETURN is automatically detected hence no warning here
212 | #define __NO_RETURN
213 | #endif
214 | #ifndef __USED
215 | #warning No compiler specific solution for __USED. __USED is ignored.
216 | #define __USED
217 | #endif
218 | #ifndef __WEAK
219 | #define __WEAK __weak
220 | #endif
221 | #ifndef __PACKED
222 | #define __PACKED @packed
223 | #endif
224 | #ifndef __PACKED_STRUCT
225 | #define __PACKED_STRUCT @packed struct
226 | #endif
227 | #ifndef __PACKED_UNION
228 | #define __PACKED_UNION @packed union
229 | #endif
230 | #ifndef __UNALIGNED_UINT32 /* deprecated */
231 | @packed struct T_UINT32 { uint32_t v; };
232 | #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
233 | #endif
234 | #ifndef __UNALIGNED_UINT16_WRITE
235 | __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
236 | #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
237 | #endif
238 | #ifndef __UNALIGNED_UINT16_READ
239 | __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
240 | #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
241 | #endif
242 | #ifndef __UNALIGNED_UINT32_WRITE
243 | __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
244 | #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
245 | #endif
246 | #ifndef __UNALIGNED_UINT32_READ
247 | __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
248 | #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
249 | #endif
250 | #ifndef __ALIGNED
251 | #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
252 | #define __ALIGNED(x)
253 | #endif
254 | #ifndef __RESTRICT
255 | #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
256 | #define __RESTRICT
257 | #endif
258 |
259 |
260 | #else
261 | #error Unknown compiler.
262 | #endif
263 |
264 |
265 | #endif /* __CMSIS_COMPILER_H */
266 |
267 |
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Include/cmsis_version.h:
--------------------------------------------------------------------------------
1 | /**************************************************************************//**
2 | * @file cmsis_version.h
3 | * @brief CMSIS Core(M) Version definitions
4 | * @version V5.0.2
5 | * @date 19. April 2017
6 | ******************************************************************************/
7 | /*
8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
9 | *
10 | * SPDX-License-Identifier: Apache-2.0
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the License); you may
13 | * not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | */
24 |
25 | #if defined ( __ICCARM__ )
26 | #pragma system_include /* treat file as system include file for MISRA check */
27 | #elif defined (__clang__)
28 | #pragma clang system_header /* treat file as system include file */
29 | #endif
30 |
31 | #ifndef __CMSIS_VERSION_H
32 | #define __CMSIS_VERSION_H
33 |
34 | /* CMSIS Version definitions */
35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */
36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */
37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \
38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */
39 | #endif
40 |
--------------------------------------------------------------------------------
/Project/Drivers/CMSIS/Include/tz_context.h:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | * @file tz_context.h
3 | * @brief Context Management for Armv8-M TrustZone
4 | * @version V1.0.1
5 | * @date 10. January 2018
6 | ******************************************************************************/
7 | /*
8 | * Copyright (c) 2017-2018 Arm Limited. All rights reserved.
9 | *
10 | * SPDX-License-Identifier: Apache-2.0
11 | *
12 | * Licensed under the Apache License, Version 2.0 (the License); you may
13 | * not use this file except in compliance with the License.
14 | * You may obtain a copy of the License at
15 | *
16 | * www.apache.org/licenses/LICENSE-2.0
17 | *
18 | * Unless required by applicable law or agreed to in writing, software
19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 | * See the License for the specific language governing permissions and
22 | * limitations under the License.
23 | */
24 |
25 | #if defined ( __ICCARM__ )
26 | #pragma system_include /* treat file as system include file for MISRA check */
27 | #elif defined (__clang__)
28 | #pragma clang system_header /* treat file as system include file */
29 | #endif
30 |
31 | #ifndef TZ_CONTEXT_H
32 | #define TZ_CONTEXT_H
33 |
34 | #include
35 |
36 | #ifndef TZ_MODULEID_T
37 | #define TZ_MODULEID_T
38 | /// \details Data type that identifies secure software modules called by a process.
39 | typedef uint32_t TZ_ModuleId_t;
40 | #endif
41 |
42 | /// \details TZ Memory ID identifies an allocated memory slot.
43 | typedef uint32_t TZ_MemoryId_t;
44 |
45 | /// Initialize secure context memory system
46 | /// \return execution status (1: success, 0: error)
47 | uint32_t TZ_InitContextSystem_S (void);
48 |
49 | /// Allocate context memory for calling secure software modules in TrustZone
50 | /// \param[in] module identifies software modules called from non-secure mode
51 | /// \return value != 0 id TrustZone memory slot identifier
52 | /// \return value 0 no memory available or internal error
53 | TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module);
54 |
55 | /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S
56 | /// \param[in] id TrustZone memory slot identifier
57 | /// \return execution status (1: success, 0: error)
58 | uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id);
59 |
60 | /// Load secure context (called on RTOS thread context switch)
61 | /// \param[in] id TrustZone memory slot identifier
62 | /// \return execution status (1: success, 0: error)
63 | uint32_t TZ_LoadContext_S (TZ_MemoryId_t id);
64 |
65 | /// Store secure context (called on RTOS thread context switch)
66 | /// \param[in] id TrustZone memory slot identifier
67 | /// \return execution status (1: success, 0: error)
68 | uint32_t TZ_StoreContext_S (TZ_MemoryId_t id);
69 |
70 | #endif // TZ_CONTEXT_H
71 |
--------------------------------------------------------------------------------
/Project/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_def.h:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file stm32f1xx_hal_def.h
4 | * @author MCD Application Team
5 | * @brief This file contains HAL common defines, enumeration, macros and
6 | * structures definitions.
7 | ******************************************************************************
8 | * @attention
9 | *
10 | * © Copyright (c) 2017 STMicroelectronics.
11 | * All rights reserved.
12 | *
13 | * This software component is licensed by ST under BSD 3-Clause license,
14 | * the "License"; You may not use this file except in compliance with the
15 | * License. You may obtain a copy of the License at:
16 | * opensource.org/licenses/BSD-3-Clause
17 | *
18 | ******************************************************************************
19 | */
20 |
21 | /* Define to prevent recursive inclusion -------------------------------------*/
22 | #ifndef __STM32F1xx_HAL_DEF
23 | #define __STM32F1xx_HAL_DEF
24 |
25 | #ifdef __cplusplus
26 | extern "C" {
27 | #endif
28 |
29 | /* Includes ------------------------------------------------------------------*/
30 | #include "stm32f1xx.h"
31 | #include "Legacy/stm32_hal_legacy.h"
32 | #include
33 |
34 | /* Exported types ------------------------------------------------------------*/
35 |
36 | /**
37 | * @brief HAL Status structures definition
38 | */
39 | typedef enum
40 | {
41 | HAL_OK = 0x00U,
42 | HAL_ERROR = 0x01U,
43 | HAL_BUSY = 0x02U,
44 | HAL_TIMEOUT = 0x03U
45 | } HAL_StatusTypeDef;
46 |
47 | /**
48 | * @brief HAL Lock structures definition
49 | */
50 | typedef enum
51 | {
52 | HAL_UNLOCKED = 0x00U,
53 | HAL_LOCKED = 0x01U
54 | } HAL_LockTypeDef;
55 |
56 | /* Exported macro ------------------------------------------------------------*/
57 | #define HAL_MAX_DELAY 0xFFFFFFFFU
58 |
59 | #define HAL_IS_BIT_SET(REG, BIT) (((REG) & (BIT)) != 0U)
60 | #define HAL_IS_BIT_CLR(REG, BIT) (((REG) & (BIT)) == 0U)
61 |
62 | #define __HAL_LINKDMA(__HANDLE__, __PPP_DMA_FIELD__, __DMA_HANDLE__) \
63 | do{ \
64 | (__HANDLE__)->__PPP_DMA_FIELD__ = &(__DMA_HANDLE__); \
65 | (__DMA_HANDLE__).Parent = (__HANDLE__); \
66 | } while(0U)
67 |
68 | #define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */
69 |
70 | /** @brief Reset the Handle's State field.
71 | * @param __HANDLE__ specifies the Peripheral Handle.
72 | * @note This macro can be used for the following purpose:
73 | * - When the Handle is declared as local variable; before passing it as parameter
74 | * to HAL_PPP_Init() for the first time, it is mandatory to use this macro
75 | * to set to 0 the Handle's "State" field.
76 | * Otherwise, "State" field may have any random value and the first time the function
77 | * HAL_PPP_Init() is called, the low level hardware initialization will be missed
78 | * (i.e. HAL_PPP_MspInit() will not be executed).
79 | * - When there is a need to reconfigure the low level hardware: instead of calling
80 | * HAL_PPP_DeInit() then HAL_PPP_Init(), user can make a call to this macro then HAL_PPP_Init().
81 | * In this later function, when the Handle's "State" field is set to 0, it will execute the function
82 | * HAL_PPP_MspInit() which will reconfigure the low level hardware.
83 | * @retval None
84 | */
85 | #define __HAL_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = 0U)
86 |
87 | #if (USE_RTOS == 1U)
88 | /* Reserved for future use */
89 | #error "USE_RTOS should be 0 in the current HAL release"
90 | #else
91 | #define __HAL_LOCK(__HANDLE__) \
92 | do{ \
93 | if((__HANDLE__)->Lock == HAL_LOCKED) \
94 | { \
95 | return HAL_BUSY; \
96 | } \
97 | else \
98 | { \
99 | (__HANDLE__)->Lock = HAL_LOCKED; \
100 | } \
101 | }while (0U)
102 |
103 | #define __HAL_UNLOCK(__HANDLE__) \
104 | do{ \
105 | (__HANDLE__)->Lock = HAL_UNLOCKED; \
106 | }while (0U)
107 | #endif /* USE_RTOS */
108 |
109 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */
110 | #ifndef __weak
111 | #define __weak __attribute__((weak))
112 | #endif
113 | #ifndef __packed
114 | #define __packed __attribute__((packed))
115 | #endif
116 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */
117 | #ifndef __weak
118 | #define __weak __attribute__((weak))
119 | #endif /* __weak */
120 | #ifndef __packed
121 | #define __packed __attribute__((__packed__))
122 | #endif /* __packed */
123 | #endif /* __GNUC__ */
124 |
125 |
126 | /* Macro to get variable aligned on 4-bytes, for __ICCARM__ the directive "#pragma data_alignment=4" must be used instead */
127 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */
128 | #ifndef __ALIGN_BEGIN
129 | #define __ALIGN_BEGIN
130 | #endif
131 | #ifndef __ALIGN_END
132 | #define __ALIGN_END __attribute__ ((aligned (4)))
133 | #endif
134 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */
135 | #ifndef __ALIGN_END
136 | #define __ALIGN_END __attribute__ ((aligned (4)))
137 | #endif /* __ALIGN_END */
138 | #ifndef __ALIGN_BEGIN
139 | #define __ALIGN_BEGIN
140 | #endif /* __ALIGN_BEGIN */
141 | #else
142 | #ifndef __ALIGN_END
143 | #define __ALIGN_END
144 | #endif /* __ALIGN_END */
145 | #ifndef __ALIGN_BEGIN
146 | #if defined (__CC_ARM) /* ARM Compiler V5*/
147 | #define __ALIGN_BEGIN __align(4)
148 | #elif defined (__ICCARM__) /* IAR Compiler */
149 | #define __ALIGN_BEGIN
150 | #endif /* __CC_ARM */
151 | #endif /* __ALIGN_BEGIN */
152 | #endif /* __GNUC__ */
153 |
154 |
155 | /**
156 | * @brief __RAM_FUNC definition
157 | */
158 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
159 | /* ARM Compiler V4/V5 and V6
160 | --------------------------
161 | RAM functions are defined using the toolchain options.
162 | Functions that are executed in RAM should reside in a separate source module.
163 | Using the 'Options for File' dialog you can simply change the 'Code / Const'
164 | area of a module to a memory space in physical RAM.
165 | Available memory areas are declared in the 'Target' tab of the 'Options for Target'
166 | dialog.
167 | */
168 | #define __RAM_FUNC
169 |
170 | #elif defined ( __ICCARM__ )
171 | /* ICCARM Compiler
172 | ---------------
173 | RAM functions are defined using a specific toolchain keyword "__ramfunc".
174 | */
175 | #define __RAM_FUNC __ramfunc
176 |
177 | #elif defined ( __GNUC__ )
178 | /* GNU Compiler
179 | ------------
180 | RAM functions are defined using a specific toolchain attribute
181 | "__attribute__((section(".RamFunc")))".
182 | */
183 | #define __RAM_FUNC __attribute__((section(".RamFunc")))
184 |
185 | #endif
186 |
187 | /**
188 | * @brief __NOINLINE definition
189 | */
190 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined ( __GNUC__ )
191 | /* ARM V4/V5 and V6 & GNU Compiler
192 | -------------------------------
193 | */
194 | #define __NOINLINE __attribute__ ( (noinline) )
195 |
196 | #elif defined ( __ICCARM__ )
197 | /* ICCARM Compiler
198 | ---------------
199 | */
200 | #define __NOINLINE _Pragma("optimize = no_inline")
201 |
202 | #endif
203 |
204 | #ifdef __cplusplus
205 | }
206 | #endif
207 |
208 | #endif /* ___STM32F1xx_HAL_DEF */
209 |
210 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
211 |
--------------------------------------------------------------------------------
/Project/Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_iwdg.h:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file stm32f1xx_hal_iwdg.h
4 | * @author MCD Application Team
5 | * @brief Header file of IWDG HAL module.
6 | ******************************************************************************
7 | * @attention
8 | *
9 | * © Copyright (c) 2016 STMicroelectronics.
10 | * All rights reserved.
11 | *
12 | * This software component is licensed by ST under BSD 3-Clause license,
13 | * the "License"; You may not use this file except in compliance with the
14 | * License. You may obtain a copy of the License at:
15 | * opensource.org/licenses/BSD-3-Clause
16 | *
17 | ******************************************************************************
18 | */
19 |
20 | /* Define to prevent recursive inclusion -------------------------------------*/
21 | #ifndef STM32F1xx_HAL_IWDG_H
22 | #define STM32F1xx_HAL_IWDG_H
23 |
24 | #ifdef __cplusplus
25 | extern "C" {
26 | #endif
27 |
28 | /* Includes ------------------------------------------------------------------*/
29 | #include "stm32f1xx_hal_def.h"
30 |
31 | /** @addtogroup STM32F1xx_HAL_Driver
32 | * @{
33 | */
34 |
35 | /** @defgroup IWDG IWDG
36 | * @{
37 | */
38 |
39 | /* Exported types ------------------------------------------------------------*/
40 | /** @defgroup IWDG_Exported_Types IWDG Exported Types
41 | * @{
42 | */
43 |
44 | /**
45 | * @brief IWDG Init structure definition
46 | */
47 | typedef struct
48 | {
49 | uint32_t Prescaler; /*!< Select the prescaler of the IWDG.
50 | This parameter can be a value of @ref IWDG_Prescaler */
51 |
52 | uint32_t Reload; /*!< Specifies the IWDG down-counter reload value.
53 | This parameter must be a number between Min_Data = 0 and Max_Data = 0x0FFF */
54 |
55 | } IWDG_InitTypeDef;
56 |
57 | /**
58 | * @brief IWDG Handle Structure definition
59 | */
60 | typedef struct
61 | {
62 | IWDG_TypeDef *Instance; /*!< Register base address */
63 |
64 | IWDG_InitTypeDef Init; /*!< IWDG required parameters */
65 | } IWDG_HandleTypeDef;
66 |
67 |
68 | /**
69 | * @}
70 | */
71 |
72 | /* Exported constants --------------------------------------------------------*/
73 | /** @defgroup IWDG_Exported_Constants IWDG Exported Constants
74 | * @{
75 | */
76 |
77 | /** @defgroup IWDG_Prescaler IWDG Prescaler
78 | * @{
79 | */
80 | #define IWDG_PRESCALER_4 0x00000000U /*!< IWDG prescaler set to 4 */
81 | #define IWDG_PRESCALER_8 IWDG_PR_PR_0 /*!< IWDG prescaler set to 8 */
82 | #define IWDG_PRESCALER_16 IWDG_PR_PR_1 /*!< IWDG prescaler set to 16 */
83 | #define IWDG_PRESCALER_32 (IWDG_PR_PR_1 | IWDG_PR_PR_0) /*!< IWDG prescaler set to 32 */
84 | #define IWDG_PRESCALER_64 IWDG_PR_PR_2 /*!< IWDG prescaler set to 64 */
85 | #define IWDG_PRESCALER_128 (IWDG_PR_PR_2 | IWDG_PR_PR_0) /*!< IWDG prescaler set to 128 */
86 | #define IWDG_PRESCALER_256 (IWDG_PR_PR_2 | IWDG_PR_PR_1) /*!< IWDG prescaler set to 256 */
87 |
88 | /**
89 | * @}
90 | */
91 |
92 |
93 |
94 | /**
95 | * @}
96 | */
97 |
98 | /* Exported macros -----------------------------------------------------------*/
99 | /** @defgroup IWDG_Exported_Macros IWDG Exported Macros
100 | * @{
101 | */
102 |
103 | /**
104 | * @brief Enable the IWDG peripheral.
105 | * @param __HANDLE__ IWDG handle
106 | * @retval None
107 | */
108 | #define __HAL_IWDG_START(__HANDLE__) WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_ENABLE)
109 |
110 | /**
111 | * @brief Reload IWDG counter with value defined in the reload register
112 | * (write access to IWDG_PR and IWDG_RLR registers disabled).
113 | * @param __HANDLE__ IWDG handle
114 | * @retval None
115 | */
116 | #define __HAL_IWDG_RELOAD_COUNTER(__HANDLE__) WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_RELOAD)
117 |
118 | /**
119 | * @}
120 | */
121 |
122 | /* Exported functions --------------------------------------------------------*/
123 | /** @defgroup IWDG_Exported_Functions IWDG Exported Functions
124 | * @{
125 | */
126 |
127 | /** @defgroup IWDG_Exported_Functions_Group1 Initialization and Start functions
128 | * @{
129 | */
130 | /* Initialization/Start functions ********************************************/
131 | HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg);
132 | /**
133 | * @}
134 | */
135 |
136 | /** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
137 | * @{
138 | */
139 | /* I/O operation functions ****************************************************/
140 | HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg);
141 | /**
142 | * @}
143 | */
144 |
145 | /**
146 | * @}
147 | */
148 |
149 | /* Private constants ---------------------------------------------------------*/
150 | /** @defgroup IWDG_Private_Constants IWDG Private Constants
151 | * @{
152 | */
153 |
154 | /**
155 | * @brief IWDG Key Register BitMask
156 | */
157 | #define IWDG_KEY_RELOAD 0x0000AAAAU /*!< IWDG Reload Counter Enable */
158 | #define IWDG_KEY_ENABLE 0x0000CCCCU /*!< IWDG Peripheral Enable */
159 | #define IWDG_KEY_WRITE_ACCESS_ENABLE 0x00005555U /*!< IWDG KR Write Access Enable */
160 | #define IWDG_KEY_WRITE_ACCESS_DISABLE 0x00000000U /*!< IWDG KR Write Access Disable */
161 |
162 | /**
163 | * @}
164 | */
165 |
166 | /* Private macros ------------------------------------------------------------*/
167 | /** @defgroup IWDG_Private_Macros IWDG Private Macros
168 | * @{
169 | */
170 |
171 | /**
172 | * @brief Enable write access to IWDG_PR and IWDG_RLR registers.
173 | * @param __HANDLE__ IWDG handle
174 | * @retval None
175 | */
176 | #define IWDG_ENABLE_WRITE_ACCESS(__HANDLE__) WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_WRITE_ACCESS_ENABLE)
177 |
178 | /**
179 | * @brief Disable write access to IWDG_PR and IWDG_RLR registers.
180 | * @param __HANDLE__ IWDG handle
181 | * @retval None
182 | */
183 | #define IWDG_DISABLE_WRITE_ACCESS(__HANDLE__) WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_WRITE_ACCESS_DISABLE)
184 |
185 | /**
186 | * @brief Check IWDG prescaler value.
187 | * @param __PRESCALER__ IWDG prescaler value
188 | * @retval None
189 | */
190 | #define IS_IWDG_PRESCALER(__PRESCALER__) (((__PRESCALER__) == IWDG_PRESCALER_4) || \
191 | ((__PRESCALER__) == IWDG_PRESCALER_8) || \
192 | ((__PRESCALER__) == IWDG_PRESCALER_16) || \
193 | ((__PRESCALER__) == IWDG_PRESCALER_32) || \
194 | ((__PRESCALER__) == IWDG_PRESCALER_64) || \
195 | ((__PRESCALER__) == IWDG_PRESCALER_128)|| \
196 | ((__PRESCALER__) == IWDG_PRESCALER_256))
197 |
198 | /**
199 | * @brief Check IWDG reload value.
200 | * @param __RELOAD__ IWDG reload value
201 | * @retval None
202 | */
203 | #define IS_IWDG_RELOAD(__RELOAD__) ((__RELOAD__) <= IWDG_RLR_RL)
204 |
205 |
206 |
207 | /**
208 | * @}
209 | */
210 |
211 | /**
212 | * @}
213 | */
214 |
215 | /**
216 | * @}
217 | */
218 |
219 |
220 | #ifdef __cplusplus
221 | }
222 | #endif
223 |
224 | #endif /* STM32F1xx_HAL_IWDG_H */
225 |
226 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
227 |
--------------------------------------------------------------------------------
/Project/Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file stm32f1xx_hal_gpio_ex.c
4 | * @author MCD Application Team
5 | * @brief GPIO Extension HAL module driver.
6 | * This file provides firmware functions to manage the following
7 | * functionalities of the General Purpose Input/Output (GPIO) extension peripheral.
8 | * + Extended features functions
9 | *
10 | @verbatim
11 | ==============================================================================
12 | ##### GPIO Peripheral extension features #####
13 | ==============================================================================
14 | [..] GPIO module on STM32F1 family, manage also the AFIO register:
15 | (+) Possibility to use the EVENTOUT Cortex feature
16 |
17 | ##### How to use this driver #####
18 | ==============================================================================
19 | [..] This driver provides functions to use EVENTOUT Cortex feature
20 | (#) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout()
21 | (#) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout()
22 | (#) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout()
23 |
24 | @endverbatim
25 | ******************************************************************************
26 | * @attention
27 | *
28 | * © Copyright (c) 2016 STMicroelectronics.
29 | * All rights reserved.
30 | *
31 | * This software component is licensed by ST under BSD 3-Clause license,
32 | * the "License"; You may not use this file except in compliance with the
33 | * License. You may obtain a copy of the License at:
34 | * opensource.org/licenses/BSD-3-Clause
35 | *
36 | ******************************************************************************
37 | */
38 |
39 | /* Includes ------------------------------------------------------------------*/
40 | #include "stm32f1xx_hal.h"
41 |
42 | /** @addtogroup STM32F1xx_HAL_Driver
43 | * @{
44 | */
45 |
46 | /** @defgroup GPIOEx GPIOEx
47 | * @brief GPIO HAL module driver
48 | * @{
49 | */
50 |
51 | #ifdef HAL_GPIO_MODULE_ENABLED
52 |
53 | /** @defgroup GPIOEx_Exported_Functions GPIOEx Exported Functions
54 | * @{
55 | */
56 |
57 | /** @defgroup GPIOEx_Exported_Functions_Group1 Extended features functions
58 | * @brief Extended features functions
59 | *
60 | @verbatim
61 | ==============================================================================
62 | ##### Extended features functions #####
63 | ==============================================================================
64 | [..] This section provides functions allowing to:
65 | (+) Configure EVENTOUT Cortex feature using the function HAL_GPIOEx_ConfigEventout()
66 | (+) Activate EVENTOUT Cortex feature using the HAL_GPIOEx_EnableEventout()
67 | (+) Deactivate EVENTOUT Cortex feature using the HAL_GPIOEx_DisableEventout()
68 |
69 | @endverbatim
70 | * @{
71 | */
72 |
73 | /**
74 | * @brief Configures the port and pin on which the EVENTOUT Cortex signal will be connected.
75 | * @param GPIO_PortSource Select the port used to output the Cortex EVENTOUT signal.
76 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PORT.
77 | * @param GPIO_PinSource Select the pin used to output the Cortex EVENTOUT signal.
78 | * This parameter can be a value of @ref GPIOEx_EVENTOUT_PIN.
79 | * @retval None
80 | */
81 | void HAL_GPIOEx_ConfigEventout(uint32_t GPIO_PortSource, uint32_t GPIO_PinSource)
82 | {
83 | /* Verify the parameters */
84 | assert_param(IS_AFIO_EVENTOUT_PORT(GPIO_PortSource));
85 | assert_param(IS_AFIO_EVENTOUT_PIN(GPIO_PinSource));
86 |
87 | /* Apply the new configuration */
88 | MODIFY_REG(AFIO->EVCR, (AFIO_EVCR_PORT) | (AFIO_EVCR_PIN), (GPIO_PortSource) | (GPIO_PinSource));
89 | }
90 |
91 | /**
92 | * @brief Enables the Event Output.
93 | * @retval None
94 | */
95 | void HAL_GPIOEx_EnableEventout(void)
96 | {
97 | SET_BIT(AFIO->EVCR, AFIO_EVCR_EVOE);
98 | }
99 |
100 | /**
101 | * @brief Disables the Event Output.
102 | * @retval None
103 | */
104 | void HAL_GPIOEx_DisableEventout(void)
105 | {
106 | CLEAR_BIT(AFIO->EVCR, AFIO_EVCR_EVOE);
107 | }
108 |
109 | /**
110 | * @}
111 | */
112 |
113 | /**
114 | * @}
115 | */
116 |
117 | #endif /* HAL_GPIO_MODULE_ENABLED */
118 |
119 | /**
120 | * @}
121 | */
122 |
123 | /**
124 | * @}
125 | */
126 |
127 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
128 |
--------------------------------------------------------------------------------
/Project/MDK-ARM/EventRecorderStub.scvd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Project/MDK-ARM/RTE/RTOS/board.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2019, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2017-07-24 Tanek the first version
9 | * 2018-11-12 Ernest Chen modify copyright
10 | */
11 |
12 | #include
13 | #include
14 | #include
15 | #include "stm32f1xx_hal.h"
16 |
17 | // Updates the variable SystemCoreClock and must be called
18 | // whenever the core clock is changed during program execution.
19 | extern void SystemCoreClockUpdate(void);
20 |
21 | // Holds the system core clock, which is the system clock
22 | // frequency supplied to the SysTick timer and the processor
23 | // core clock.
24 | extern uint32_t SystemCoreClock;
25 |
26 | /* SysTick configuration */
27 | static void rt_hw_systick_init(void)
28 | {
29 | HAL_SYSTICK_Config(SystemCoreClock / RT_TICK_PER_SECOND);
30 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
31 | }
32 |
33 | #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
34 | #define STM32_SRAM_START (0x20000000)
35 | #define STM32_SRAM_END (STM32_SRAM_START + 48 * 1024) // 结束地址 = 0x20000000(基址) + 48K(RAM大小)
36 |
37 | #if defined(__CC_ARM) || defined(__CLANG_ARM)
38 | extern int Image$$RW_IRAM1$$ZI$$Limit; // RW_IRAM1,需与链接脚本中运行时域名相对应
39 | #define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
40 | #endif
41 |
42 | #define HEAP_END STM32_SRAM_END
43 | #endif
44 |
45 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
46 | {
47 | return HAL_OK;
48 | }
49 |
50 | extern void SystemClock_Config(void);
51 | extern int console_init(void);
52 |
53 | /**
54 | * This function will initial your board.
55 | */
56 | void rt_hw_board_init()
57 | {
58 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
59 | HAL_Init();
60 |
61 | /* Configure the system clock */
62 | SystemClock_Config();
63 |
64 | /* System Clock Update */
65 | SystemCoreClockUpdate();
66 |
67 | /* System Tick Configuration */
68 | rt_hw_systick_init();
69 |
70 | /* Call components board initial (use INIT_BOARD_EXPORT()) */
71 | #ifdef RT_USING_COMPONENTS_INIT
72 | rt_components_board_init();
73 | #endif
74 |
75 | #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
76 | rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
77 | #endif
78 |
79 | console_init();
80 | }
81 |
82 | void SysTick_Handler(void)
83 | {
84 | /* enter interrupt */
85 | rt_interrupt_enter();
86 |
87 | HAL_IncTick();
88 | rt_tick_increase();
89 |
90 | /* leave interrupt */
91 | rt_interrupt_leave();
92 | }
93 |
--------------------------------------------------------------------------------
/Project/MDK-ARM/RTE/RTOS/rtconfig.h:
--------------------------------------------------------------------------------
1 | /* RT-Thread config file */
2 |
3 | #ifndef __RTTHREAD_CFG_H__
4 | #define __RTTHREAD_CFG_H__
5 |
6 | #if defined(__CC_ARM) || defined(__CLANG_ARM)
7 | #include "RTE_Components.h"
8 |
9 | #if defined(RTE_USING_FINSH)
10 | #define RT_USING_FINSH
11 | #endif //RTE_USING_FINSH
12 |
13 | #endif //(__CC_ARM) || (__CLANG_ARM)
14 |
15 | // <<< Use Configuration Wizard in Context Menu >>>
16 | // Basic Configuration
17 | // Maximal level of thread priority <8-256>
18 | // Default: 32
19 | #define RT_THREAD_PRIORITY_MAX 8
20 | // OS tick per second
21 | // Default: 1000 (1ms)
22 | #define RT_TICK_PER_SECOND 1000
23 | // Alignment size for CPU architecture data access
24 | // Default: 4
25 | #define RT_ALIGN_SIZE 4
26 | // the max length of object name<2-16>
27 | // Default: 8
28 | #define RT_NAME_MAX 8
29 | // Using RT-Thread components initialization
30 | // Using RT-Thread components initialization
31 | #define RT_USING_COMPONENTS_INIT
32 | //
33 |
34 | #define RT_USING_USER_MAIN
35 |
36 | // the stack size of main thread<1-4086>
37 | // Default: 512
38 | #define RT_MAIN_THREAD_STACK_SIZE 1024
39 |
40 | //
41 |
42 | // Debug Configuration
43 | // enable kernel debug configuration
44 | // Default: enable kernel debug configuration
45 | #define RT_DEBUG
46 | //
47 | // enable kernel debug color
48 | // Default: enable kernel debug color
49 | #define RT_DEBUG_COLOR
50 | //
51 | // enable components initialization debug configuration<0-1>
52 | // Default: 0
53 | #define RT_DEBUG_INIT 0
54 | // thread stack over flow detect
55 | // Diable Thread stack over flow detect
56 | //#define RT_USING_OVERFLOW_CHECK
57 | //
58 | //
59 |
60 | // Hook Configuration
61 | // using hook
62 | // using hook
63 | //#define RT_USING_HOOK
64 | //
65 | // using idle hook
66 | // using idle hook
67 | #define RT_USING_IDLE_HOOK
68 | //
69 | //
70 |
71 | // Software timers Configuration
72 | // Enables user timers
73 | #define RT_USING_TIMER_SOFT 0
74 | #if RT_USING_TIMER_SOFT == 0
75 | #undef RT_USING_TIMER_SOFT
76 | #endif
77 | // The priority level of timer thread <0-31>
78 | // Default: 4
79 | #define RT_TIMER_THREAD_PRIO 4
80 | // The stack size of timer thread <0-8192>
81 | // Default: 512
82 | #define RT_TIMER_THREAD_STACK_SIZE 512
83 | //
84 |
85 | // IPC(Inter-process communication) Configuration
86 | // Using Semaphore
87 | // Using Semaphore
88 | #define RT_USING_SEMAPHORE
89 | //
90 | // Using Mutex
91 | // Using Mutex
92 | #define RT_USING_MUTEX
93 | //
94 | // Using Event
95 | // Using Event
96 | #define RT_USING_EVENT
97 | //
98 | // Using MailBox
99 | // Using MailBox
100 | #define RT_USING_MAILBOX
101 | //
102 | // Using Message Queue
103 | // Using Message Queue
104 | //#define RT_USING_MESSAGEQUEUE
105 | //
106 | //
107 |
108 | // Memory Management Configuration
109 | // Dynamic Heap Management
110 | // Dynamic Heap Management
111 | //#define RT_USING_HEAP
112 | //
113 | // using small memory
114 | // using small memory
115 | //#define RT_USING_SMALL_MEM
116 | //
117 | // using tiny size of memory
118 | // using tiny size of memory
119 | //#define RT_USING_TINY_SIZE
120 | //
121 | //
122 |
123 | // Console Configuration
124 | // Using console
125 | // Using console
126 | #define RT_USING_CONSOLE
127 | //
128 | // the buffer size of console <1-1024>
129 | // the buffer size of console
130 | // Default: 128 (128Byte)
131 | #define RT_CONSOLEBUF_SIZE 512
132 | // the device name for console
133 | // the device name for console
134 | #define RT_CONSOLE_DEVICE_NAME "usart1"
135 | //
136 |
137 | #if defined(RT_USING_FINSH)
138 | #define FINSH_USING_MSH
139 | #define FINSH_USING_MSH_ONLY
140 | // Finsh Configuration
141 | // the priority of finsh thread <1-7>
142 | // the priority of finsh thread
143 | // Default: 6
144 | #define __FINSH_THREAD_PRIORITY 4
145 | #define FINSH_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
146 | // the stack of finsh thread <1-4096>
147 | // the stack of finsh thread
148 | // Default: 4096 (4096Byte)
149 | #define FINSH_THREAD_STACK_SIZE 1024
150 | // the history lines of finsh thread <1-32>
151 | // the history lines of finsh thread
152 | // Default: 5
153 | #define FINSH_HISTORY_LINES 5
154 |
155 | #define FINSH_USING_SYMTAB
156 | #define FINSH_USING_HISTORY
157 | //
158 | #endif
159 |
160 | // AT commands Configuration
161 | // Enables AT commands
162 | #define RT_USING_AT 1
163 | #if RT_USING_AT == 0
164 | #undef RT_USING_AT
165 | #endif
166 | #ifdef RT_USING_AT
167 | // enable debug log output
168 | // enable debug log output
169 | #define AT_DEBUG
170 | //
171 | // enable AT commands client
172 | // enable AT commands client
173 | #define AT_USING_CLIENT
174 | //
175 | #ifdef AT_USING_CLIENT
176 | // the maximum number of supported clients
177 | // Default: 1
178 | #define AT_CLIENT_NUM_MAX 1
179 | // enable CLI(Command-Line Interface) for AT commands
180 | // enable CLI(Command-Line Interface) for AT commands
181 | #define AT_USING_CLI
182 | //
183 | // enable print RAW format AT command communication data
184 | // enable print RAW format AT command communication data
185 | //#define AT_PRINT_RAW_CMD
186 | //
187 | // the maximum length of AT Commands buffer
188 | // Default: 128
189 | #define AT_CMD_MAX_LEN 128
190 | #endif
191 | #endif
192 | //
193 |
194 | // ULOG Configuration
195 | // Enables ULOG
196 | #define RT_USING_ULOG 1
197 | #if RT_USING_ULOG == 0
198 | #undef RT_USING_ULOG
199 | #endif
200 | #ifdef RT_USING_ULOG
201 | // Enable syslog format log and API.
202 | // Enable syslog format log and API.
203 | //#define ULOG_USING_SYSLOG
204 | //
205 | // Normal mode static output log level
206 | // Default: Debug
207 | // <0=> Assert
208 | // <3=> Error
209 | // <4=> Warning
210 | // <6=> Information
211 | // <7=> Debug
212 | #define ULOG_OUTPUT_LVL_NORMAL_MODE 7
213 | // Syslog mode static output log level
214 | // Default: DEBUG
215 | // <0=> EMERG
216 | // <1=> ALERT
217 | // <2=> CRIT
218 | // <3=> ERR
219 | // <4=> WARNING
220 | // <5=> NOTICE
221 | // <6=> INFO
222 | // <7=> DEBUG
223 | #define ULOG_OUTPUT_LVL_SYSLOG_MODE 7
224 |
225 | #ifndef ULOG_USING_SYSLOG
226 | #define ULOG_OUTPUT_LVL ULOG_OUTPUT_LVL_NORMAL_MODE
227 | #else
228 | #define ULOG_OUTPUT_LVL ULOG_OUTPUT_LVL_SYSLOG_MODE
229 | #endif
230 |
231 | // Enable assert check.
232 | // Enable assert check.
233 | //#define ULOG_ASSERT_ENABLE
234 | //
235 | // The log's rbb buffer size
236 | // Default: 4096
237 | #define ULOG_RBB_BUFSZ 4096
238 | // The log's max width.
239 | // Default: 128
240 | #define ULOG_LINE_BUF_SIZE 256
241 | // log format
242 | // Enable float number support. It will using more thread stack.
243 | // Enable float number support. It will using more thread stack.
244 | //#define ULOG_OUTPUT_FLOAT
245 | //
246 | // Enable color log.
247 | // Enable color log.
248 | #define ULOG_USING_COLOR
249 | //
250 | // Enable time information.
251 | // Enable time information.
252 | #define ULOG_OUTPUT_TIME
253 | //
254 | // Enable timestamp format for time. Depends on ULOG_OUTPUT_TIME
255 | // Enable timestamp format for time. Depends on ULOG_OUTPUT_TIME
256 | //#define ULOG_TIME_USING_TIMESTAMP
257 | //
258 | // Enable level information.
259 | // Enable level information.
260 | #define ULOG_OUTPUT_LEVEL
261 | //
262 | // Enable tag information.
263 | // Enable tag information.
264 | #define ULOG_OUTPUT_TAG
265 | //
266 | // Enable thread information.
267 | // Enable thread information.
268 | //#define ULOG_OUTPUT_THREAD_NAME
269 | //
270 | //
271 | // Enable console backend.
272 | // Enable console backend.
273 | #define ULOG_BACKEND_USING_CONSOLE
274 | //
275 | // Enable runtime log filter.
276 | // Enable runtime log filter.
277 | #define ULOG_USING_FILTER
278 | //
279 | // The filter's max num
280 | // Default: 10
281 | #define ULOG_FILTER_NUM 10
282 |
283 | #ifdef ULOG_USING_COLOR
284 | #ifdef ULOG_USING_SYSLOG
285 | #error "It's not available on syslog mode."
286 | #endif
287 | #endif
288 |
289 | #ifdef ULOG_TIME_USING_TIMESTAMP
290 | #ifndef ULOG_OUTPUT_TIME
291 | #error "Please select ULOG_OUTPUT_TIME"
292 | #endif
293 | #endif
294 |
295 | #ifdef ULOG_USING_SYSLOG
296 | #if !defined (ULOG_OUTPUT_TIME) || !defined (ULOG_USING_FILTER)
297 | #error "Please select ULOG_OUTPUT_TIME and ULOG_USING_FILTER"
298 | #endif
299 | #endif
300 |
301 | #endif
302 | //
303 |
304 | // WIFI Configuration
305 | // the client device name for wifi
306 | // the client device name for wifi
307 | #define WIFI_CLIENT_DEVICE_NAME "usart3"
308 | // the power pin of wifi
309 | // the power pin of wifi
310 | #define WIFI_POWER_PIN 5
311 | // the reset pin of wifi
312 | // the reset pin of wifi
313 | #define WIFI_RESET_PIN 6
314 | // the recv buf len of wifi
315 | // the recv buf len of wifi
316 | #define WIFI_RECV_BUFF_LEN 1024
317 | // the listen port of wifi
318 | // the listen port of wifi
319 | #define WIFI_LISTEN_PORT 502
320 | // the client timeout(/s) of wifi
321 | // the client timeout(/s) of wifi
322 | #define WIFI_CLIENT_TIMEOUT 10
323 | //
324 |
325 | // <<< end of configuration section >>>
326 |
327 | #endif
328 |
--------------------------------------------------------------------------------
/Project/MDK-ARM/RTE/_Project/RTE_Components.h:
--------------------------------------------------------------------------------
1 |
2 | /*
3 | * Auto generated Run-Time-Environment Configuration File
4 | * *** Do not modify ! ***
5 | *
6 | * Project: 'Project'
7 | * Target: 'Project'
8 | */
9 |
10 | #ifndef RTE_COMPONENTS_H
11 | #define RTE_COMPONENTS_H
12 |
13 |
14 | /*
15 | * Define the Device Header File:
16 | */
17 | #define CMSIS_device_header "stm32f10x.h"
18 |
19 | /* RealThread.RT-Thread::RTOS:shell:3.1.3 */
20 | #define RTE_USING_FINSH
21 |
22 |
23 | #endif /* RTE_COMPONENTS_H */
24 |
--------------------------------------------------------------------------------
/Project/modules/at/include/at.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-03-30 chenyong first version
9 | * 2018-08-17 chenyong multiple client support
10 | */
11 |
12 | #ifndef __AT_H__
13 | #define __AT_H__
14 |
15 | #include
16 | #include "usr_device.h"
17 |
18 | #ifdef __cplusplus
19 | extern "C" {
20 | #endif
21 |
22 | #define AT_SW_VERSION "1.3.1"
23 |
24 | #define AT_CMD_NAME_LEN 16
25 | #define AT_END_MARK_LEN 4
26 |
27 | #ifndef AT_CMD_MAX_LEN
28 | #define AT_CMD_MAX_LEN 128
29 | #endif
30 |
31 | /* the server AT commands new line sign */
32 | #if defined(AT_CMD_END_MARK_CRLF)
33 | #define AT_CMD_END_MARK "\r\n"
34 | #elif defined(AT_CMD_END_MARK_CR)
35 | #define AT_CMD_END_MARK "\r"
36 | #elif defined(AT_CMD_END_MARK_LF)
37 | #define AT_CMD_END_MARK "\n"
38 | #endif
39 |
40 | #ifndef AT_SERVER_RECV_BUFF_LEN
41 | #define AT_SERVER_RECV_BUFF_LEN 256
42 | #endif
43 |
44 | #ifndef AT_SERVER_DEVICE
45 | #define AT_SERVER_DEVICE "uart2"
46 | #endif
47 |
48 | /* the maximum number of supported AT clients */
49 | #ifndef AT_CLIENT_NUM_MAX
50 | #define AT_CLIENT_NUM_MAX 1
51 | #endif
52 |
53 | #define AT_CMD_EXPORT(_name_, _args_expr_, _test_, _query_, _setup_, _exec_) \
54 | RT_USED static const struct at_cmd __at_cmd_##_test_##_query_##_setup_##_exec_ SECTION("RtAtCmdTab") = \
55 | { \
56 | _name_, \
57 | _args_expr_, \
58 | _test_, \
59 | _query_, \
60 | _setup_, \
61 | _exec_, \
62 | };
63 |
64 | enum at_status
65 | {
66 | AT_STATUS_UNINITIALIZED = 0,
67 | AT_STATUS_INITIALIZED,
68 | AT_STATUS_CLI,
69 | };
70 | typedef enum at_status at_status_t;
71 |
72 | #ifdef AT_USING_SERVER
73 | enum at_result
74 | {
75 | AT_RESULT_OK = 0, /* AT result is no error */
76 | AT_RESULT_FAILE = -1, /* AT result have a generic error */
77 | AT_RESULT_NULL = -2, /* AT result not need return */
78 | AT_RESULT_CMD_ERR = -3, /* AT command format error or No way to execute */
79 | AT_RESULT_CHECK_FAILE = -4, /* AT command expression format is error */
80 | AT_RESULT_PARSE_FAILE = -5, /* AT command arguments parse is error */
81 | };
82 | typedef enum at_result at_result_t;
83 |
84 | struct at_cmd
85 | {
86 | char name[AT_CMD_NAME_LEN];
87 | char *args_expr;
88 | at_result_t (*test)(void);
89 | at_result_t (*query)(void);
90 | at_result_t (*setup)(const char *args);
91 | at_result_t (*exec)(void);
92 | };
93 | typedef struct at_cmd *at_cmd_t;
94 |
95 | struct at_server
96 | {
97 | usr_device_t dev;
98 |
99 | at_status_t status;
100 | rt_err_t (*get_char)(struct at_server *server, char *ch, rt_int32_t timeout);
101 | rt_bool_t echo_mode;
102 |
103 | char recv_buffer[AT_SERVER_RECV_BUFF_LEN];
104 | rt_size_t cur_recv_len;
105 | rt_sem_t rx_notice;
106 | char end_mark[AT_END_MARK_LEN];
107 |
108 | rt_thread_t parser;
109 | void (*parser_entry)(struct at_server *server);
110 | };
111 | typedef struct at_server *at_server_t;
112 | #endif /* AT_USING_SERVER */
113 |
114 | #ifdef AT_USING_CLIENT
115 | enum at_resp_status
116 | {
117 | AT_RESP_OK = 0, /* AT response end is OK */
118 | AT_RESP_ERROR = -1, /* AT response end is ERROR */
119 | AT_RESP_TIMEOUT = -2, /* AT response is timeout */
120 | AT_RESP_BUFF_FULL= -3, /* AT response buffer is full */
121 | };
122 | typedef enum at_resp_status at_resp_status_t;
123 |
124 | struct at_response
125 | {
126 | /* response buffer */
127 | char *buf;
128 | /* the maximum response buffer size, it set by `at_create_resp()` function */
129 | rt_size_t buf_size;
130 | /* the length of current response buffer */
131 | rt_size_t buf_len;
132 | /* the number of setting response lines, it set by `at_create_resp()` function
133 | * == 0: the response data will auto return when received 'OK' or 'ERROR'
134 | * != 0: the response data will return when received setting lines number data */
135 | rt_size_t line_num;
136 | /* the count of received response lines */
137 | rt_size_t line_counts;
138 | /* the maximum response time */
139 | rt_int32_t timeout;
140 | };
141 |
142 | typedef struct at_response *at_response_t;
143 |
144 | struct at_client;
145 |
146 | /* URC(Unsolicited Result Code) object, such as: 'RING', 'READY' request by AT server */
147 | struct at_urc
148 | {
149 | const char *cmd_prefix;
150 | const char *cmd_suffix;
151 | void (*func)(struct at_client *client, const char *data, rt_size_t size);
152 | };
153 | typedef struct at_urc *at_urc_t;
154 |
155 | struct at_urc_table
156 | {
157 | rt_size_t urc_size;
158 | const struct at_urc *urc;
159 | };
160 | typedef struct at_urc *at_urc_table_t;
161 |
162 | struct at_client
163 | {
164 | usr_device_t dev;
165 |
166 | at_status_t status;
167 | char end_sign;
168 |
169 | /* the current received one line data buffer */
170 | char *recv_line_buf;
171 | /* The length of the currently received one line data */
172 | rt_size_t recv_line_len;
173 | /* The maximum supported receive data length */
174 | rt_size_t recv_bufsz;
175 | struct rt_semaphore rx_notice;
176 |
177 | at_response_t resp;
178 | struct rt_semaphore resp_notice;
179 | at_resp_status_t resp_status;
180 |
181 | struct at_urc_table urc_table;
182 | };
183 | typedef struct at_client *at_client_t;
184 | #endif /* AT_USING_CLIENT */
185 |
186 | #ifdef AT_USING_SERVER
187 | /* AT server initialize and start */
188 | int at_server_init(void);
189 |
190 | /* AT server send command execute result to AT device */
191 | void at_server_printf(const char *format, ...);
192 | void at_server_printfln(const char *format, ...);
193 | void at_server_print_result(at_result_t result);
194 | rt_size_t at_server_send(at_server_t server, const char *buf, rt_size_t size);
195 | rt_size_t at_server_recv(at_server_t server, char *buf, rt_size_t size, rt_int32_t timeout);
196 |
197 | /* AT server request arguments parse */
198 | int at_req_parse_args(const char *req_args, const char *req_expr, ...);
199 | #endif /* AT_USING_SERVER */
200 |
201 | #ifdef AT_USING_CLIENT
202 |
203 | /* AT client initialize and start*/
204 | at_client_t at_client_init(usr_device_t dev, char *recv_line_buf, rt_size_t recv_bufsz);
205 | void client_parser(at_client_t client);
206 |
207 | /* ========================== multiple AT client function ============================ */
208 |
209 | /* get AT client object */
210 | at_client_t at_client_get(const char *dev_name);
211 | at_client_t at_client_get_first(void);
212 |
213 | /* AT client wait for connection to external devices. */
214 | int at_client_obj_wait_connect(at_client_t client, at_response_t resp, rt_uint32_t timeout);
215 |
216 | /* AT client send or receive data */
217 | rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size);
218 | rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout);
219 |
220 | /* set AT client a line end sign */
221 | void at_obj_set_end_sign(at_client_t client, char ch);
222 |
223 | /* Set URC(Unsolicited Result Code) table */
224 | int at_obj_set_urc_table(at_client_t client, const struct at_urc * table, rt_size_t size);
225 |
226 | /* AT client send commands to AT server and waiter response */
227 | int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...);
228 |
229 | /* AT response object create and delete */
230 | int at_resp_init(at_response_t resp, char *buf, rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout);
231 | int at_resp_set_info(at_response_t resp, rt_size_t line_num, rt_int32_t timeout);
232 |
233 | /* AT response line buffer get and parse response buffer arguments */
234 | const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line);
235 | const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword);
236 | int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...);
237 | int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...);
238 |
239 | /* ========================== single AT client function ============================ */
240 |
241 | /**
242 | * NOTE: These functions can be used directly when there is only one AT client.
243 | * If there are multiple AT Client in the program, these functions can operate on the first initialized AT client.
244 | */
245 |
246 | #define at_exec_cmd(resp, ...) at_obj_exec_cmd(at_client_get_first(), resp, __VA_ARGS__)
247 | #define at_client_wait_connect(resp, timeout) at_client_obj_wait_connect(at_client_get_first(), resp, timeout)
248 | #define at_client_send(buf, size) at_client_obj_send(at_client_get_first(), buf, size)
249 | #define at_client_recv(buf, size, timeout) at_client_obj_recv(at_client_get_first(), buf, size, timeout)
250 | #define at_set_end_sign(ch) at_obj_set_end_sign(at_client_get_first(), ch)
251 | #define at_set_urc_table(urc_table, table_sz) at_obj_set_urc_table(at_client_get_first(), urc_table, table_sz)
252 |
253 | #endif /* AT_USING_CLIENT */
254 |
255 | /* ========================== User port function ============================ */
256 |
257 | #ifdef AT_USING_SERVER
258 | /* AT server device reset */
259 | void at_port_reset(void);
260 |
261 | /* AT server device factory reset */
262 | void at_port_factory_reset(void);
263 | #endif
264 |
265 | #ifdef __cplusplus
266 | }
267 | #endif
268 |
269 | #endif /* __AT_H__ */
270 |
--------------------------------------------------------------------------------
/Project/modules/at/include/at_log.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-10-12 armink first version
9 | */
10 |
11 | /*
12 | * NOTE: DO NOT include this file on the header file.
13 | */
14 |
15 | #ifndef LOG_TAG
16 | #define DBG_TAG "at"
17 | #else
18 | #define DBG_TAG LOG_TAG
19 | #endif /* LOG_TAG */
20 |
21 | #ifdef AT_DEBUG
22 | #define DBG_LVL DBG_LOG
23 | #else
24 | #define DBG_LVL DBG_INFO
25 | #endif /* AT_DEBUG */
26 |
27 | #include
28 |
--------------------------------------------------------------------------------
/Project/modules/at/src/at_utils.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-04-14 chenyong first version
9 | */
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | static char send_buf[AT_CMD_MAX_LEN];
16 | static rt_size_t last_cmd_len = 0;
17 |
18 | /**
19 | * dump hex format data to console device
20 | *
21 | * @param name name for hex object, it will show on log header
22 | * @param buf hex buffer
23 | * @param size buffer size
24 | */
25 | void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size)
26 | {
27 | #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
28 | #define WIDTH_SIZE 32
29 |
30 | rt_size_t i, j;
31 |
32 | for (i = 0; i < size; i += WIDTH_SIZE)
33 | {
34 | rt_kprintf("[D/AT] %s: %04X-%04X: ", name, i, i + WIDTH_SIZE);
35 | for (j = 0; j < WIDTH_SIZE; j++)
36 | {
37 | if (i + j < size)
38 | {
39 | rt_kprintf("%02X ", buf[i + j]);
40 | }
41 | else
42 | {
43 | rt_kprintf(" ");
44 | }
45 | if ((j + 1) % 8 == 0)
46 | {
47 | rt_kprintf(" ");
48 | }
49 | }
50 | rt_kprintf(" ");
51 | for (j = 0; j < WIDTH_SIZE; j++)
52 | {
53 | if (i + j < size)
54 | {
55 | rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
56 | }
57 | }
58 | rt_kprintf("\n");
59 | }
60 | }
61 |
62 | const char *at_get_last_cmd(rt_size_t *cmd_size)
63 | {
64 | *cmd_size = last_cmd_len;
65 | return send_buf;
66 | }
67 |
68 | rt_size_t at_vprintf(usr_device_t dev, const char *format, va_list args)
69 | {
70 | last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args);
71 |
72 | #ifdef AT_PRINT_RAW_CMD
73 | at_print_raw_cmd("sendline", send_buf, last_cmd_len);
74 | #endif
75 |
76 | return usr_device_write(dev, 0, send_buf, last_cmd_len);
77 | }
78 |
79 | rt_size_t at_vprintfln(usr_device_t dev, const char *format, va_list args)
80 | {
81 | rt_size_t len;
82 |
83 | len = at_vprintf(dev, format, args);
84 |
85 | usr_device_write(dev, 0, "\r\n", 2);
86 |
87 | return len + 2;
88 | }
89 |
--------------------------------------------------------------------------------
/Project/modules/console/console.c:
--------------------------------------------------------------------------------
1 | #include "drv_usart.h"
2 |
3 | ALIGN(RT_ALIGN_SIZE)
4 | /* 串口 */
5 | static rt_uint8_t init_ok = 0;
6 | static usr_device_t dev = RT_NULL;
7 | static rt_uint8_t usart_send_buf[2048];
8 | static rt_uint8_t usart_read_buf[16];
9 | static struct rt_semaphore rx_sem;
10 |
11 | static rt_err_t rx_indicate(usr_device_t dev, rt_size_t size)
12 | {
13 | rt_sem_release(&rx_sem);
14 |
15 | return RT_EOK;
16 | }
17 |
18 | void rt_hw_console_output(const char *str)
19 | {
20 | if(!init_ok)
21 | return;
22 |
23 | usr_device_write(dev, 0, str, rt_strlen(str));
24 | }
25 |
26 | char rt_hw_console_getchar(void)
27 | {
28 | while(!init_ok)
29 | rt_thread_mdelay(100);
30 |
31 | char ch = 0;
32 |
33 | while(usr_device_read(dev, 0, &ch, 1) == 0)
34 | {
35 | rt_sem_take(&rx_sem, rt_tick_from_millisecond(5000));
36 | rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
37 | }
38 |
39 | return ch;
40 | }
41 |
42 | static void main_hook_cb(void)
43 | {
44 | if(!init_ok)
45 | return;
46 |
47 | if(dev->error)
48 | usr_device_init(dev);
49 | }
50 |
51 | #include "main_hook.h"
52 |
53 | static struct main_hook_module console_main_hook_module = {0};
54 |
55 | int console_init(void)
56 | {
57 | dev = usr_device_find(RT_CONSOLE_DEVICE_NAME);
58 | if(dev == RT_NULL)
59 | return -RT_ERROR;
60 |
61 | rt_sem_init(&rx_sem, "con_rx", 0, RT_IPC_FLAG_FIFO);
62 | usr_device_set_rx_indicate(dev, rx_indicate);
63 |
64 | struct usr_device_usart_buffer buffer;
65 | buffer.send_buf = usart_send_buf;
66 | buffer.send_bufsz = sizeof(usart_send_buf);
67 | buffer.read_buf = usart_read_buf;
68 | buffer.read_bufsz = sizeof(usart_read_buf);
69 | usr_device_control(dev, USR_DEVICE_USART_CMD_SET_BUFFER, &buffer);
70 | struct usr_device_usart_parameter parameter;
71 | parameter.baudrate = 115200;
72 | parameter.parity = UART_PARITY_NONE;
73 | parameter.wlen = UART_WORDLENGTH_8B;
74 | parameter.stblen = UART_STOPBITS_1;
75 | usr_device_control(dev, USR_DEVICE_USART_CMD_SET_PARAMETER, ¶meter);
76 | usr_device_init(dev);
77 |
78 | console_main_hook_module.hook = main_hook_cb;
79 | main_hook_module_register(&console_main_hook_module);
80 |
81 | init_ok = 1;
82 |
83 | return RT_EOK;
84 | }
85 |
--------------------------------------------------------------------------------
/Project/modules/init_module/init_module.c:
--------------------------------------------------------------------------------
1 | #include "init_module.h"
2 | #include
3 |
4 | static rt_slist_t init_module_prev_header = RT_SLIST_OBJECT_INIT(init_module_prev_header);
5 | static rt_slist_t init_module_device_header = RT_SLIST_OBJECT_INIT(init_module_device_header);
6 | static rt_slist_t init_module_component_header = RT_SLIST_OBJECT_INIT(init_module_component_header);
7 | static rt_slist_t init_module_env_header = RT_SLIST_OBJECT_INIT(init_module_env_header);
8 | static rt_slist_t init_module_app_header = RT_SLIST_OBJECT_INIT(init_module_app_header);
9 |
10 | static rt_slist_t init_module_header = RT_SLIST_OBJECT_INIT(init_module_header);
11 |
12 | static void init_module_register(rt_slist_t *l, struct init_module *module)
13 | {
14 | rt_base_t level;
15 |
16 | rt_slist_init(&(module->slist));
17 |
18 | level = rt_hw_interrupt_disable();
19 |
20 | rt_slist_append(l, &(module->slist));
21 |
22 | rt_hw_interrupt_enable(level);
23 | }
24 |
25 | void init_module_prev_register(struct init_module *module)
26 | {
27 | init_module_register(&init_module_prev_header, module);
28 | }
29 | RTM_EXPORT(init_module_prev_register);
30 |
31 | void init_module_device_register(struct init_module *module)
32 | {
33 | init_module_register(&init_module_device_header, module);
34 | }
35 | RTM_EXPORT(init_module_device_register);
36 |
37 | void init_module_component_register(struct init_module *module)
38 | {
39 | init_module_register(&init_module_component_header, module);
40 | }
41 | RTM_EXPORT(init_module_component_register);
42 |
43 | void init_module_env_register(struct init_module *module)
44 | {
45 | init_module_register(&init_module_env_header, module);
46 | }
47 | RTM_EXPORT(init_module_env_register);
48 |
49 | void init_module_app_register(struct init_module *module)
50 | {
51 | init_module_register(&init_module_app_header, module);
52 | }
53 | RTM_EXPORT(init_module_app_register);
54 |
55 | int init_module_init(void)
56 | {
57 | rt_slist_tail(&init_module_header)->next = init_module_prev_header.next;
58 | rt_slist_tail(&init_module_header)->next = init_module_device_header.next;
59 | rt_slist_tail(&init_module_header)->next = init_module_component_header.next;
60 | rt_slist_tail(&init_module_header)->next = init_module_env_header.next;
61 | rt_slist_tail(&init_module_header)->next = init_module_app_header.next;
62 |
63 | rt_slist_t *node;
64 | rt_slist_for_each(node, &init_module_header)
65 | {
66 | struct init_module *module = rt_slist_entry(node, struct init_module, slist);
67 | module->init();
68 | }
69 |
70 | return RT_EOK;
71 | }
72 |
--------------------------------------------------------------------------------
/Project/modules/init_module/init_module.h:
--------------------------------------------------------------------------------
1 | #ifndef __INIT_MODULE_H
2 | #define __INIT_MODULE_H
3 | #include
4 |
5 | struct init_module
6 | {
7 | int (*init)(void);
8 | rt_slist_t slist;
9 | };
10 |
11 |
12 | void init_module_prev_register(struct init_module *module);
13 | void init_module_device_register(struct init_module *module);
14 | void init_module_component_register(struct init_module *module);
15 | void init_module_env_register(struct init_module *module);
16 | void init_module_app_register(struct init_module *module);
17 |
18 | int init_module_init(void);
19 |
20 | #endif
21 |
--------------------------------------------------------------------------------
/Project/modules/key/key.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "wifi.h"
3 |
4 | #define KEY_PIN 2
5 |
6 | ALIGN(RT_ALIGN_SIZE)
7 | static struct agile_btn key;
8 | static struct usr_device_wifi *wifi_dev = RT_NULL;
9 |
10 | static void btn_click_event_cb(agile_btn_t *btn)
11 | {
12 | if((btn->hold_time >= 100) && (btn->hold_time <= 1000))
13 | {
14 | usr_device_control(&(wifi_dev->parent), USR_DEVICE_WIFI_CMD_SMART, (void *)(!wifi_dev->smart_flag));
15 | }
16 | }
17 |
18 | static int key_init(void)
19 | {
20 | wifi_dev = (struct usr_device_wifi *)usr_device_find("wifi");
21 | RT_ASSERT(wifi_dev);
22 |
23 | agile_btn_init(&key, KEY_PIN, PIN_HIGH, PIN_MODE_INPUT_PULLDOWN);
24 | RT_ASSERT(agile_btn_set_event_cb(&key, BTN_CLICK_EVENT, btn_click_event_cb) == RT_EOK);
25 | RT_ASSERT(agile_btn_start(&key) == RT_EOK);
26 |
27 | return RT_EOK;
28 | }
29 |
30 | #include "init_module.h"
31 |
32 | static struct init_module key_init_module = {0};
33 |
34 | static int key_init_module_register(void)
35 | {
36 | key_init_module.init = key_init;
37 | init_module_app_register(&key_init_module);
38 |
39 | return RT_EOK;
40 | }
41 | INIT_DEVICE_EXPORT(key_init_module_register);
42 |
--------------------------------------------------------------------------------
/Project/modules/led/led.c:
--------------------------------------------------------------------------------
1 | #include "led.h"
2 | #include
3 |
4 | #define D0_LED_PIN 0
5 | #define D1_LED_PIN 1
6 |
7 | #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
8 |
9 | ALIGN(RT_ALIGN_SIZE)
10 | static struct agile_led d0_led;
11 | static struct agile_led d1_led;
12 | static struct rt_mailbox mb;
13 | static rt_uint8_t mb_pool[400];
14 | static rt_uint8_t led_stack[256];
15 | static struct rt_thread led_thread;
16 | static const uint32_t light_mode_50ms[] = {50, 50};
17 | static const uint32_t light_mode_100ms[] = {100, 100};
18 | static const uint32_t light_mode_1000ms[] = {1000, 1000};
19 |
20 | static void led_msg(void *parameter)
21 | {
22 | rt_ubase_t cmd = LED_CMD_NULL;
23 | rt_uint8_t wifi_connected = 0;
24 |
25 | while(1)
26 | {
27 | rt_err_t rc = rt_mb_recv(&mb, &cmd, RT_WAITING_FOREVER);
28 | if(rc != RT_EOK)
29 | continue;
30 |
31 | switch(cmd)
32 | {
33 | case LED_CMD_WIFI_RESET:
34 | {
35 | RT_ASSERT(agile_led_stop(&d0_led) == RT_EOK);
36 | agile_led_off(&d0_led);
37 | wifi_connected = 0;
38 | }
39 | break;
40 |
41 | case LED_CMD_WIFI_START_SMART:
42 | {
43 | RT_ASSERT(agile_led_stop(&d0_led) == RT_EOK);
44 | agile_led_set_light_mode(&d0_led, light_mode_100ms, ITEM_NUM(light_mode_100ms), -1);
45 | RT_ASSERT(agile_led_start(&d0_led) == RT_EOK);
46 | wifi_connected = 0;
47 | }
48 | break;
49 |
50 | case LED_CMD_WIFI_CONNECTED:
51 | {
52 | RT_ASSERT(agile_led_stop(&d0_led) == RT_EOK);
53 | agile_led_on(&d0_led);
54 | wifi_connected = 1;
55 | }
56 | break;
57 |
58 | case LED_CMD_WIFI_DISCONNECT:
59 | {
60 | RT_ASSERT(agile_led_stop(&d0_led) == RT_EOK);
61 | agile_led_set_light_mode(&d0_led, light_mode_1000ms, ITEM_NUM(light_mode_1000ms), -1);
62 | RT_ASSERT(agile_led_start(&d0_led) == RT_EOK);
63 | wifi_connected = 0;
64 | }
65 | break;
66 |
67 | case LED_CMD_WIFI_DATA:
68 | {
69 | if(wifi_connected)
70 | {
71 | RT_ASSERT(agile_led_stop(&d1_led) == RT_EOK);
72 | RT_ASSERT(agile_led_start(&d1_led) == RT_EOK);
73 | }
74 | }
75 | break;
76 |
77 | default:
78 | break;
79 | }
80 | }
81 | }
82 |
83 | int led_control(enum led_cmd_t cmd)
84 | {
85 | rt_ubase_t value = cmd;
86 | rt_mb_send(&mb, value);
87 | return RT_EOK;
88 | }
89 |
90 | static int led_init(void)
91 | {
92 | agile_led_init(&d0_led, D0_LED_PIN, PIN_LOW, light_mode_1000ms, ITEM_NUM(light_mode_1000ms), -1);
93 | agile_led_init(&d1_led, D1_LED_PIN, PIN_LOW, light_mode_50ms, ITEM_NUM(light_mode_50ms), 2);
94 |
95 | rt_thread_mdelay(100);
96 | agile_led_on(&d0_led);
97 | agile_led_on(&d1_led);
98 | rt_thread_mdelay(200);
99 | agile_led_off(&d0_led);
100 | agile_led_off(&d1_led);
101 |
102 | rt_mb_init(&mb, "led", &mb_pool[0], sizeof(mb_pool) / 4, RT_IPC_FLAG_FIFO);
103 |
104 | rt_thread_init(&led_thread,
105 | "led_msg",
106 | led_msg,
107 | RT_NULL,
108 | &led_stack[0],
109 | sizeof(led_stack),
110 | 0,
111 | 100);
112 | rt_thread_startup(&led_thread);
113 |
114 | return RT_EOK;
115 | }
116 |
117 | #include "init_module.h"
118 |
119 | static struct init_module led_init_module = {0};
120 |
121 | static int led_init_module_register(void)
122 | {
123 | led_init_module.init = led_init;
124 | init_module_prev_register(&led_init_module);
125 |
126 | return RT_EOK;
127 | }
128 | INIT_PREV_EXPORT(led_init_module_register);
129 |
--------------------------------------------------------------------------------
/Project/modules/led/led.h:
--------------------------------------------------------------------------------
1 | #ifndef __LED_H
2 | #define __LED_H
3 | #include
4 |
5 | enum led_cmd_t
6 | {
7 | LED_CMD_NULL = 0,
8 | LED_CMD_WIFI_RESET,
9 | LED_CMD_WIFI_START_SMART,
10 | LED_CMD_WIFI_CONNECTED,
11 | LED_CMD_WIFI_DISCONNECT,
12 | LED_CMD_WIFI_DATA
13 | };
14 |
15 | int led_control(enum led_cmd_t cmd);
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/Project/modules/main_hook/main_hook.c:
--------------------------------------------------------------------------------
1 | #include "main_hook.h"
2 | #include
3 |
4 | static rt_slist_t main_hook_module_header = RT_SLIST_OBJECT_INIT(main_hook_module_header);
5 |
6 | void main_hook_module_register(struct main_hook_module *module)
7 | {
8 | rt_base_t level;
9 |
10 | rt_slist_init(&(module->slist));
11 |
12 | level = rt_hw_interrupt_disable();
13 |
14 | rt_slist_append(&main_hook_module_header, &(module->slist));
15 |
16 | rt_hw_interrupt_enable(level);
17 | }
18 |
19 | void main_hook_process(void)
20 | {
21 | rt_slist_t *node;
22 | rt_slist_for_each(node, &main_hook_module_header)
23 | {
24 | struct main_hook_module *module = rt_slist_entry(node, struct main_hook_module, slist);
25 | if(module->hook)
26 | module->hook();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Project/modules/main_hook/main_hook.h:
--------------------------------------------------------------------------------
1 | #ifndef __MAIN_HOOK_H
2 | #define __MAIN_HOOK_H
3 | #include
4 |
5 | struct main_hook_module
6 | {
7 | void(*hook)(void);
8 | rt_slist_t slist;
9 | };
10 |
11 | void main_hook_module_register(struct main_hook_module *module);
12 | void main_hook_process(void);
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/Project/modules/oled/oled.c:
--------------------------------------------------------------------------------
1 | #include "oled.h"
2 | #include "drv_gpio.h"
3 | #include "stm32f1xx_hal.h"
4 | #include
5 |
6 | #define DBG_ENABLE
7 | #define DBG_COLOR
8 | #define DBG_SECTION_NAME "oled"
9 | #define DBG_LEVEL DBG_INFO
10 | #include
11 |
12 | #define OLED_RES_PIN 7
13 | #define OLED_DC_PIN 8
14 | #define OLED_CS_PIN 9
15 |
16 | SPI_HandleTypeDef hspi3;
17 | DMA_HandleTypeDef hdma_spi3_tx;
18 |
19 | ALIGN(RT_ALIGN_SIZE)
20 | static rt_uint8_t oled_gram[8][128] = {0};
21 | static struct usr_device_oled oled_device = {0};
22 |
23 | static void _oled_hw_init(void)
24 | {
25 | static rt_uint8_t init_ok = 0;
26 |
27 | rt_base_t level = rt_hw_interrupt_disable();
28 |
29 | if(init_ok)
30 | HAL_SPI_DeInit(&hspi3);
31 |
32 | drv_pin_mode(OLED_RES_PIN, PIN_MODE_OUTPUT);
33 | drv_pin_write(OLED_RES_PIN, PIN_HIGH);
34 |
35 | drv_pin_mode(OLED_DC_PIN, PIN_MODE_OUTPUT);
36 | drv_pin_write(OLED_DC_PIN, PIN_HIGH);
37 |
38 | drv_pin_mode(OLED_CS_PIN, PIN_MODE_OUTPUT);
39 | drv_pin_write(OLED_CS_PIN, PIN_HIGH);
40 |
41 | hspi3.Instance = SPI3;
42 | hspi3.Init.Mode = SPI_MODE_MASTER;
43 | hspi3.Init.Direction = SPI_DIRECTION_2LINES;
44 | hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
45 | hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
46 | hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
47 | hspi3.Init.NSS = SPI_NSS_SOFT;
48 | hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
49 | hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
50 | hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
51 | hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
52 | hspi3.Init.CRCPolynomial = 10;
53 | HAL_SPI_Init(&hspi3);
54 | HAL_SPI_Abort(&hspi3);
55 |
56 | init_ok = 1;
57 |
58 | rt_hw_interrupt_enable(level);
59 | }
60 |
61 | static void _oled_write_cmd(rt_uint8_t cmd)
62 | {
63 | rt_sem_take(&(oled_device.sem_lock), RT_WAITING_FOREVER);
64 |
65 | drv_pin_write(OLED_DC_PIN, PIN_LOW);
66 | drv_pin_write(OLED_CS_PIN, PIN_LOW);
67 |
68 | HAL_SPI_Abort(&hspi3);
69 | HAL_SPI_Transmit(&hspi3, &cmd, 1, 1000);
70 |
71 | drv_pin_write(OLED_CS_PIN, PIN_HIGH);
72 | drv_pin_write(OLED_DC_PIN, PIN_HIGH);
73 |
74 | rt_sem_release(&(oled_device.sem_lock));
75 | }
76 |
77 | static void _oled_refresh_gram(void)
78 | {
79 | rt_sem_take(&(oled_device.sem_lock), RT_WAITING_FOREVER);
80 |
81 | drv_pin_write(OLED_DC_PIN, PIN_HIGH);
82 | drv_pin_write(OLED_CS_PIN, PIN_LOW);
83 |
84 | HAL_SPI_Abort(&hspi3);
85 | HAL_SPI_Transmit_DMA(&hspi3, oled_gram[0], sizeof(oled_gram));
86 | }
87 |
88 | static rt_err_t _oled_init(usr_device_t dev)
89 | {
90 | rt_sem_init(&(oled_device.sem_lock), "oled", 1, RT_IPC_FLAG_FIFO);
91 |
92 | _oled_hw_init();
93 |
94 | drv_pin_write(OLED_RES_PIN, PIN_HIGH);
95 | rt_thread_mdelay(100);
96 | drv_pin_write(OLED_RES_PIN, PIN_LOW);
97 | rt_thread_mdelay(200);
98 | drv_pin_write(OLED_RES_PIN, PIN_HIGH);
99 | rt_thread_mdelay(100);
100 |
101 | _oled_write_cmd(0xAE);//--turn off oled panel
102 | _oled_write_cmd(0x00);//---set low column address
103 | _oled_write_cmd(0x10);//---set high column address
104 | _oled_write_cmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
105 | _oled_write_cmd(0x81);//--set contrast control register
106 | _oled_write_cmd(0xCF); // Set SEG Output Current Brightness
107 | _oled_write_cmd(0xA1);//--Set SEG/Column Mapping
108 | _oled_write_cmd(0xC8);//Set COM/Row Scan Direction
109 | _oled_write_cmd(0xA6);//--set normal display
110 | _oled_write_cmd(0xA8);//--set multiplex ratio(1 to 64)
111 | _oled_write_cmd(0x3f);//--1/64 duty
112 | _oled_write_cmd(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
113 | _oled_write_cmd(0x00);//-not offset
114 | _oled_write_cmd(0xd5);//--set display clock divide ratio/oscillator frequency
115 | _oled_write_cmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
116 | _oled_write_cmd(0xD9);//--set pre-charge period
117 | _oled_write_cmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
118 | _oled_write_cmd(0xDA);//--set com pins hardware configuration
119 | _oled_write_cmd(0x12);
120 | _oled_write_cmd(0xDB);//--set vcomh
121 | _oled_write_cmd(0x40);//Set VCOM Deselect Level
122 | _oled_write_cmd(0x20);//-Set Horizontal addressing mode (0x00/0x01/0x02)
123 | _oled_write_cmd(0x00);//
124 | _oled_write_cmd(0x8D);//--set Charge Pump enable/disable
125 | _oled_write_cmd(0x14);//--set(0x10) disable
126 | _oled_write_cmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
127 | _oled_write_cmd(0xA6);// Disable Inverse Display On (0xa6/a7)
128 | _oled_write_cmd(0xAF);//--turn on oled panel
129 |
130 | _oled_refresh_gram();
131 |
132 | return RT_EOK;
133 | }
134 |
135 | static rt_err_t _oled_control(usr_device_t dev, int cmd, void *args)
136 | {
137 | rt_err_t result = -RT_ERROR;
138 |
139 | switch(cmd)
140 | {
141 | case USR_DEVICE_OLED_CMD_RECT_UPDATE:
142 | {
143 | _oled_refresh_gram();
144 | result = RT_EOK;
145 | }
146 | break;
147 |
148 | default:
149 | break;
150 | }
151 |
152 | return result;
153 | }
154 |
155 |
156 | static int _hw_oled_init(void)
157 | {
158 | rt_memset(&oled_device, 0, sizeof(struct usr_device_oled));
159 |
160 | oled_device.parent.init = _oled_init;
161 | oled_device.parent.read = RT_NULL;
162 | oled_device.parent.write = RT_NULL;
163 | oled_device.parent.control = _oled_control;
164 |
165 | usr_device_register(&(oled_device.parent), "oled");
166 |
167 | return RT_EOK;
168 | }
169 | INIT_BOARD_EXPORT(_hw_oled_init);
170 |
171 | void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
172 | {
173 | drv_pin_write(OLED_CS_PIN, PIN_HIGH);
174 |
175 | rt_sem_release(&(oled_device.sem_lock));
176 |
177 | if(oled_device.parent.tx_complete)
178 | oled_device.parent.tx_complete(&(oled_device.parent), RT_NULL);
179 | }
180 |
181 | void DMA2_Channel2_IRQHandler(void)
182 | {
183 | /* enter interrupt */
184 | rt_interrupt_enter();
185 |
186 | HAL_DMA_IRQHandler(&hdma_spi3_tx);
187 |
188 | /* leave interrupt */
189 | rt_interrupt_leave();
190 | }
191 |
--------------------------------------------------------------------------------
/Project/modules/oled/oled.h:
--------------------------------------------------------------------------------
1 | #ifndef __OLED_H
2 | #define __OLED_H
3 | #include
4 | #include "usr_device.h"
5 |
6 | #define USR_DEVICE_OLED_CMD_RECT_UPDATE 0x01
7 |
8 | struct usr_device_oled
9 | {
10 | struct usr_device parent;
11 | struct rt_semaphore sem_lock;
12 | };
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/Project/modules/ring/ringblk_buf.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-08-25 armink the first version
9 | */
10 |
11 | #ifndef _RINGBLK_BUF_H_
12 | #define _RINGBLK_BUF_H_
13 |
14 | /*
15 | * Introduction:
16 | * The rbb is the ring buffer which is composed with many blocks. It is different from the ring buffer.
17 | * The ring buffer is only composed with chars. The rbb put and get supported zero copies. So the rbb
18 | * is very suitable for put block and get block by a certain order. Such as DMA block transmit,
19 | * communicate frame send/recv, and so on.
20 | */
21 |
22 | #ifdef __cplusplus
23 | extern "C" {
24 | #endif
25 |
26 | #include
27 |
28 | enum rt_rbb_status
29 | {
30 | /* unused status when first initialize or after blk_free() */
31 | RT_RBB_BLK_UNUSED,
32 | /* initialized status after blk_alloc() */
33 | RT_RBB_BLK_INITED,
34 | /* put status after blk_put() */
35 | RT_RBB_BLK_PUT,
36 | /* get status after blk_get() */
37 | RT_RBB_BLK_GET,
38 | };
39 | typedef enum rt_rbb_status rt_rbb_status_t;
40 |
41 | /**
42 | * the block of rbb
43 | */
44 | struct rt_rbb_blk
45 | {
46 | rt_rbb_status_t status :8;
47 | /* less then 2^24 */
48 | rt_size_t size :24;
49 | rt_uint8_t *buf;
50 | rt_slist_t list;
51 | };
52 | typedef struct rt_rbb_blk *rt_rbb_blk_t;
53 |
54 | /**
55 | * Rbb block queue: the blocks (from block1->buf to blockn->buf) memory which on this queue is continuous.
56 | */
57 | struct rt_rbb_blk_queue
58 | {
59 | rt_rbb_blk_t blocks;
60 | rt_size_t blk_num;
61 | };
62 | typedef struct rt_rbb_blk_queue *rt_rbb_blk_queue_t;
63 |
64 | /**
65 | * ring block buffer
66 | */
67 | struct rt_rbb
68 | {
69 | rt_uint8_t *buf;
70 | rt_size_t buf_size;
71 | /* all of blocks */
72 | rt_rbb_blk_t blk_set;
73 | rt_size_t blk_max_num;
74 | /* saved the initialized and put status blocks */
75 | rt_slist_t blk_list;
76 | };
77 | typedef struct rt_rbb *rt_rbb_t;
78 |
79 | /* rbb (ring block buffer) API */
80 | void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num);
81 | rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb);
82 |
83 | #ifdef RT_USING_HEAP
84 | rt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num);
85 | void rt_rbb_destroy(rt_rbb_t rbb);
86 | #endif
87 |
88 | /* rbb block API */
89 | rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size);
90 | void rt_rbb_blk_put(rt_rbb_blk_t block);
91 | rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb);
92 | rt_size_t rt_rbb_blk_size(rt_rbb_blk_t block);
93 | rt_uint8_t *rt_rbb_blk_buf(rt_rbb_blk_t block);
94 | void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block);
95 |
96 | /* rbb block queue API */
97 | rt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue);
98 | rt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue);
99 | rt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue);
100 | void rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue);
101 | rt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb);
102 |
103 |
104 | #ifdef __cplusplus
105 | }
106 | #endif
107 |
108 | #endif /* _RINGBLK_BUF_H_ */
109 |
--------------------------------------------------------------------------------
/Project/modules/ring/ringbuffer.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | */
9 | #ifndef RINGBUFFER_H__
10 | #define RINGBUFFER_H__
11 |
12 | #ifdef __cplusplus
13 | extern "C" {
14 | #endif
15 |
16 | #include
17 |
18 | /* ring buffer */
19 | struct rt_ringbuffer
20 | {
21 | rt_uint8_t *buffer_ptr;
22 | /* use the msb of the {read,write}_index as mirror bit. You can see this as
23 | * if the buffer adds a virtual mirror and the pointers point either to the
24 | * normal or to the mirrored buffer. If the write_index has the same value
25 | * with the read_index, but in a different mirror, the buffer is full.
26 | * While if the write_index and the read_index are the same and within the
27 | * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
28 | *
29 | * mirror = 0 mirror = 1
30 | * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
31 | * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
32 | * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
33 | * read_idx-^ write_idx-^
34 | *
35 | * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
36 | * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
37 | * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
38 | * read_idx-^ ^-write_idx
39 | *
40 | * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
41 | * But it should be enough for most of the cases.
42 | *
43 | * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
44 | rt_uint16_t read_mirror : 1;
45 | rt_uint16_t read_index : 15;
46 | rt_uint16_t write_mirror : 1;
47 | rt_uint16_t write_index : 15;
48 | /* as we use msb of index as mirror bit, the size should be signed and
49 | * could only be positive. */
50 | rt_int16_t buffer_size;
51 | };
52 |
53 | enum rt_ringbuffer_state
54 | {
55 | RT_RINGBUFFER_EMPTY,
56 | RT_RINGBUFFER_FULL,
57 | /* half full is neither full nor empty */
58 | RT_RINGBUFFER_HALFFULL,
59 | };
60 |
61 | /**
62 | * RingBuffer for DeviceDriver
63 | *
64 | * Please note that the ring buffer implementation of RT-Thread
65 | * has no thread wait or resume feature.
66 | */
67 | void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
68 | void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
69 | rt_size_t rt_ringbuffer_put_update(struct rt_ringbuffer *rb, rt_uint16_t length);
70 | void rt_ringbuffer_put_raw(struct rt_ringbuffer *rb, rt_uint16_t write_index, const rt_uint8_t *ptr, rt_uint16_t length);
71 | rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
72 | rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
73 | rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
74 | rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
75 | rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
76 | rt_size_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, rt_uint8_t **ptr, rt_uint16_t length);
77 | rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
78 | rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
79 |
80 | #ifdef RT_USING_HEAP
81 | struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
82 | void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
83 | #endif
84 |
85 | rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
86 | {
87 | RT_ASSERT(rb != RT_NULL);
88 | return rb->buffer_size;
89 | }
90 |
91 | /** return the size of empty space in rb */
92 | #define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
93 |
94 |
95 | #ifdef __cplusplus
96 | }
97 | #endif
98 |
99 | #endif
100 |
--------------------------------------------------------------------------------
/Project/modules/rtu_master/rtu_master.c:
--------------------------------------------------------------------------------
1 | #include "drv_usart.h"
2 | #include "agile_modbus.h"
3 |
4 | #define DBG_ENABLE
5 | #define DBG_COLOR
6 | #define DBG_SECTION_NAME "rtu_master"
7 | #define DBG_LEVEL DBG_LOG
8 | #include
9 |
10 | #define DEVICE_NAME "usart2"
11 |
12 | ALIGN(RT_ALIGN_SIZE)
13 | /* 串口 */
14 | static usr_device_t dev = RT_NULL;
15 | static rt_uint8_t usart_send_buf[2048];
16 | static rt_uint8_t usart_read_buf[256];
17 | static struct rt_semaphore rx_sem;
18 |
19 | /* modbus */
20 | static rt_uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH];
21 | static rt_uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH];
22 | static rt_uint32_t send_count = 0;
23 | static rt_uint32_t success_count = 0;
24 |
25 | /* 线程 */
26 | static rt_uint8_t _thread_stack[512];
27 | static struct rt_thread _thread;
28 |
29 |
30 | static int get_rtu_master_info(void)
31 | {
32 | LOG_I("send_cnt:%u, success_cnt:%u", send_count, success_count);
33 |
34 | return RT_EOK;
35 | }
36 | MSH_CMD_EXPORT(get_rtu_master_info, get rtu master info);
37 |
38 | static int _usart_receive(rt_uint8_t *read_buf, int read_bufsz, int timeout)
39 | {
40 | rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
41 |
42 | int len = 0;
43 | rt_uint8_t flag = 0;
44 |
45 | while(1)
46 | {
47 | int rc = usr_device_read(dev, 0, read_buf + len, read_bufsz);
48 | if(rc > 0)
49 | {
50 | len += rc;
51 | read_bufsz -= rc;
52 | if(read_bufsz <= 0)
53 | break;
54 |
55 | flag = 1;
56 | }
57 | else
58 | {
59 | if(flag)
60 | timeout = 20;
61 |
62 | if(rt_sem_take(&rx_sem, timeout) != RT_EOK)
63 | break;
64 | }
65 | }
66 |
67 | return len;
68 | }
69 |
70 | static int _usart_pass(rt_uint8_t *send_buf, int send_len, rt_uint8_t *read_buf, int read_bufsz, int timeout)
71 | {
72 | if((send_buf == RT_NULL) || (send_len <= 0) || (read_buf == RT_NULL) || (read_bufsz <= 0) || (timeout <= 0))
73 | return -RT_ERROR;
74 |
75 | usr_device_write(dev, 0, send_buf, send_len);
76 | int read_len = _usart_receive(read_buf, read_bufsz, timeout);
77 |
78 | return read_len;
79 | }
80 |
81 | static void rtu_master_entry(void *parameter)
82 | {
83 | agile_modbus_rtu_t ctx;
84 | agile_modbus_rtu_init(&ctx, ctx_send_buf, sizeof(ctx_send_buf), ctx_read_buf, sizeof(ctx_read_buf));
85 | agile_modbus_set_slave(&(ctx._ctx), 1);
86 |
87 | rt_uint16_t hold_register[100];
88 | while(1)
89 | {
90 | rt_thread_mdelay(10);
91 |
92 | send_count++;
93 | int send_len = agile_modbus_serialize_read_registers(&(ctx._ctx), 0, 100);
94 | int read_len = _usart_pass(ctx._ctx.send_buf, send_len, ctx._ctx.read_buf, ctx._ctx.read_bufsz, 1000);
95 | int rc = agile_modbus_deserialize_read_registers(&(ctx._ctx), read_len, hold_register);
96 |
97 | if(rc == 100)
98 | {
99 | success_count++;
100 | }
101 | }
102 | }
103 |
104 | static rt_err_t rx_indicate(usr_device_t dev, rt_size_t size)
105 | {
106 | rt_sem_release(&rx_sem);
107 |
108 | return RT_EOK;
109 | }
110 |
111 |
112 | static int rtu_master_init(void)
113 | {
114 | dev = usr_device_find(DEVICE_NAME);
115 | if(dev == RT_NULL)
116 | return -RT_ERROR;
117 |
118 | rt_sem_init(&rx_sem, "rms_r", 0, RT_IPC_FLAG_FIFO);
119 | usr_device_set_rx_indicate(dev, rx_indicate);
120 |
121 |
122 | struct usr_device_usart_buffer buffer;
123 | buffer.send_buf = usart_send_buf;
124 | buffer.send_bufsz = sizeof(usart_send_buf);
125 | buffer.read_buf = usart_read_buf;
126 | buffer.read_bufsz = sizeof(usart_read_buf);
127 | usr_device_control(dev, USR_DEVICE_USART_CMD_SET_BUFFER, &buffer);
128 | usr_device_init(dev);
129 |
130 | rt_thread_init(&_thread,
131 | "rtu_master",
132 | rtu_master_entry,
133 | RT_NULL,
134 | &_thread_stack[0],
135 | sizeof(_thread_stack),
136 | 3,
137 | 100);
138 | rt_thread_startup(&_thread);
139 |
140 | return RT_EOK;
141 | }
142 |
143 | #include "init_module.h"
144 |
145 | static struct init_module rtu_master_init_module = {0};
146 |
147 | static int rtu_master_init_module_register(void)
148 | {
149 | rtu_master_init_module.init = rtu_master_init;
150 | init_module_app_register(&rtu_master_init_module);
151 |
152 | return RT_EOK;
153 | }
154 | INIT_PREV_EXPORT(rtu_master_init_module_register);
155 |
--------------------------------------------------------------------------------
/Project/modules/runtime/runtime.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #define DBG_ENABLE
6 | #define DBG_COLOR
7 | #define DBG_SECTION_NAME "runtime"
8 | #define DBG_LEVEL DBG_LOG
9 | #include
10 |
11 | static rt_uint32_t run_time = 0;
12 |
13 | time_t time(time_t *t)
14 | {
15 | if (t != RT_NULL)
16 | {
17 | *t = run_time;
18 | }
19 |
20 | return run_time;
21 | }
22 |
23 | static void main_hook_cb(void)
24 | {
25 | static rt_tick_t prev_tick = 0;
26 | static rt_tick_t redress = 0;
27 |
28 | rt_tick_t now_tick = rt_tick_get();
29 | rt_tick_t diff_tick = 0;
30 |
31 | if(now_tick >= prev_tick)
32 | diff_tick = now_tick - prev_tick;
33 | else
34 | diff_tick = RT_TICK_MAX - prev_tick + now_tick;
35 | diff_tick += redress;
36 |
37 | rt_uint32_t sec_part = diff_tick / RT_TICK_PER_SECOND;
38 | if(sec_part > 0)
39 | {
40 | prev_tick = now_tick;
41 | redress = diff_tick % RT_TICK_PER_SECOND;
42 | run_time += sec_part;
43 | }
44 | }
45 |
46 | static int get_runtime(void)
47 | {
48 | LOG_I("running [%u]/s", run_time);
49 |
50 | return RT_EOK;
51 | }
52 | MSH_CMD_EXPORT(get_runtime, get runtime);
53 |
54 | #include "main_hook.h"
55 |
56 | static struct main_hook_module runtime_main_hook_module = {0};
57 |
58 | static int runtime_init(void)
59 | {
60 | runtime_main_hook_module.hook = main_hook_cb;
61 | main_hook_module_register(&runtime_main_hook_module);
62 |
63 | return RT_EOK;
64 | }
65 | INIT_PREV_EXPORT(runtime_init);
66 |
--------------------------------------------------------------------------------
/Project/modules/ulog/SConscript:
--------------------------------------------------------------------------------
1 | from building import *
2 |
3 | cwd = GetCurrentDir()
4 | src = Glob('*.c')
5 | path = [cwd]
6 |
7 | if GetDepend('ULOG_BACKEND_USING_CONSOLE'):
8 | src += ['backend/console_be.c']
9 |
10 | if GetDepend('ULOG_USING_SYSLOG'):
11 | path += [cwd + '/syslog']
12 | src += Glob('syslog/*.c')
13 |
14 | group = DefineGroup('Utilities', src, depend = ['RT_USING_ULOG'], CPPPATH = path)
15 |
16 | Return('group')
17 |
--------------------------------------------------------------------------------
/Project/modules/ulog/backend/console_be.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-09-04 armink the first version
9 | */
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | #ifdef ULOG_BACKEND_USING_CONSOLE
16 |
17 | #if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 384
18 | #error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
19 | #endif
20 |
21 | static struct ulog_backend console = {0};
22 |
23 | void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
24 | char *log, size_t len)
25 | {
26 | #ifdef RT_USING_DEVICE
27 | rt_device_t dev = rt_console_get_device();
28 |
29 | if (dev == RT_NULL)
30 | {
31 | rt_hw_console_output(log);
32 | }
33 | else
34 | {
35 | rt_uint16_t old_flag = dev->open_flag;
36 |
37 | dev->open_flag |= RT_DEVICE_FLAG_STREAM;
38 | rt_device_write(dev, 0, log, len);
39 | dev->open_flag = old_flag;
40 | }
41 | #else
42 | if(len > ULOG_LINE_BUF_SIZE)
43 | len = ULOG_LINE_BUF_SIZE;
44 | log[len] = '\0';
45 | rt_hw_console_output(log);
46 | #endif
47 |
48 | }
49 |
50 | int ulog_console_backend_init(void)
51 | {
52 | console.output = ulog_console_backend_output;
53 |
54 | ulog_backend_register(&console, "console", RT_TRUE);
55 |
56 | return 0;
57 | }
58 | INIT_DEVICE_EXPORT(ulog_console_backend_init);
59 |
60 | #endif /* ULOG_BACKEND_USING_CONSOLE */
61 |
--------------------------------------------------------------------------------
/Project/modules/ulog/syslog/syslog.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-09-07 armink the first version
9 | */
10 |
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include "syslog.h"
17 |
18 | #ifdef ULOG_OUTPUT_FLOAT
19 | #include
20 | #endif
21 |
22 | /*
23 | * reference:
24 | * http://pubs.opengroup.org/onlinepubs/7908799/xsh/syslog.h.html
25 | * https://www.gnu.org/software/libc/manual/html_node/Submitting-Syslog-Messages.html
26 | * http://man7.org/linux/man-pages/man3/syslog.3.html
27 | */
28 |
29 | #ifdef ULOG_USING_SYSLOG
30 |
31 | #include
32 |
33 | #ifndef ULOG_SYSLOG_IDENT_MAX_LEN
34 | #define ULOG_SYSLOG_IDENT_MAX_LEN ULOG_FILTER_TAG_MAX_LEN
35 | #endif
36 |
37 | static char local_ident[ULOG_SYSLOG_IDENT_MAX_LEN + 1];
38 | static int local_facility = LOG_USER;
39 | static int local_option = LOG_USER;
40 | static rt_bool_t is_open = RT_FALSE;
41 |
42 | /**
43 | * open connection to syslog
44 | *
45 | * @param ident is an arbitrary identification string which future syslog invocations will prefix to each message.
46 | * @param option is not using on ulog.
47 | * @param facility is the default facility code for this connection.
48 | */
49 | void openlog(const char *ident, int option, int facility)
50 | {
51 | rt_base_t level;
52 |
53 | level = rt_hw_interrupt_disable();
54 |
55 | rt_memset(local_ident, 0, sizeof(local_ident));
56 | if (ident)
57 | {
58 | rt_strncpy(local_ident, ident, ULOG_SYSLOG_IDENT_MAX_LEN);
59 | }
60 | else
61 | {
62 | rt_strncpy(local_ident, "rtt", ULOG_SYSLOG_IDENT_MAX_LEN);
63 | }
64 |
65 | local_option = option;
66 |
67 | if (facility)
68 | {
69 | local_facility = facility;
70 | }
71 | else
72 | {
73 | /* default facility is LOG_USER */
74 | local_facility = LOG_USER;
75 | }
76 | /* output all level log */
77 | setlogmask(LOG_UPTO(LOG_DEBUG));
78 |
79 | is_open = RT_TRUE;
80 |
81 | rt_hw_interrupt_enable(level);
82 |
83 | }
84 |
85 | /**
86 | * This is functionally identical to syslog.
87 | *
88 | * @param priority log priority, can be generated by the macro LOG_MAKEPRI
89 | * @param format log format
90 | * @param args log arguments
91 | */
92 | void vsyslog(int priority, const char *format, va_list args)
93 | {
94 | if (LOG_FAC(priority) == 0)
95 | {
96 | /* using local facility */
97 | priority |= local_facility;
98 | }
99 |
100 | ulog_voutput(priority, local_ident, RT_TRUE, format, args);
101 | }
102 |
103 | /**
104 | * generates a log message
105 | *
106 | * @param priority log priority, can be generated by the macro LOG_MAKEPRI
107 | * @param format log format, like printf()
108 | */
109 | void syslog(int priority, const char *format, ...)
110 | {
111 | va_list args;
112 |
113 | if (!is_open)
114 | {
115 | openlog(0, 0, 0);
116 | }
117 | /* args point to the first variable parameter */
118 | va_start(args, format);
119 |
120 | vsyslog(priority, format, args);
121 |
122 | va_end(args);
123 | }
124 |
125 | /**
126 | * close the syslog
127 | */
128 | void closelog(void)
129 | {
130 | is_open = RT_FALSE;
131 | }
132 |
133 | /**
134 | * set log priority mask
135 | *
136 | * @param mask The log priority mask which generate by macro LOG_MASK and LOG_UPTO.
137 | *
138 | * @return This function returns the previous log priority mask.
139 | */
140 | int setlogmask(int mask)
141 | {
142 | static int old_mask = 0;
143 | int return_mask = old_mask;
144 |
145 | ulog_tag_lvl_filter_set(local_ident, mask);
146 |
147 | old_mask = mask;
148 |
149 | return return_mask;
150 | }
151 |
152 | static const char *get_month_str(uint8_t month)
153 | {
154 | switch(month)
155 | {
156 | case 1: return "Jan";
157 | case 2: return "Feb";
158 | case 3: return "Mar";
159 | case 4: return "Apr";
160 | case 5: return "May";
161 | case 6: return "June";
162 | case 7: return "July";
163 | case 8: return "Aug";
164 | case 9: return "Sept";
165 | case 10: return "Oct";
166 | case 11: return "Nov";
167 | case 12: return "Dec";
168 | default: return "Unknown";
169 | }
170 | }
171 |
172 | RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, rt_bool_t newline, const char *format, va_list args)
173 | {
174 | extern size_t ulog_strcpy(size_t cur_len, char *dst, const char *src);
175 |
176 | rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
177 | int fmt_result;
178 |
179 | RT_ASSERT(log_buf);
180 | RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG);
181 | RT_ASSERT(tag);
182 | RT_ASSERT(format);
183 |
184 | /* add time and priority (level) info */
185 | {
186 | time_t now = time(RT_NULL);
187 | struct tm tm_info;
188 |
189 | localtime_r(&now, &tm_info);
190 |
191 | #ifdef ULOG_OUTPUT_LEVEL
192 | rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "<%d>%s%3d %02d:%02d:%02d", level,
193 | get_month_str(tm_info.tm_mon + 1), tm_info.tm_mday, tm_info.tm_hour, tm_info.tm_min, tm_info.tm_sec);
194 | #else
195 | rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%s%3d %02d:%02d:%02d",
196 | get_month_str(tm_info.tm_mon + 1), tm_info.tm_mday, tm_info.tm_hour, tm_info.tm_min, tm_info.tm_sec);
197 | #endif /* ULOG_OUTPUT_LEVEL */
198 |
199 | log_len += rt_strlen(log_buf + log_len);
200 | }
201 |
202 | #ifdef ULOG_OUTPUT_TAG
203 | /* add identification (tag) info */
204 | {
205 | log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
206 | log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
207 | }
208 | #endif /* ULOG_OUTPUT_TAG */
209 |
210 | #ifdef ULOG_OUTPUT_THREAD_NAME
211 | /* add thread info */
212 | {
213 | log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
214 | /* is not in interrupt context */
215 | if (rt_interrupt_get_nest() == 0)
216 | {
217 | rt_size_t name_len = rt_strnlen(rt_thread_self()->name, RT_NAME_MAX);
218 |
219 | rt_strncpy(log_buf + log_len, rt_thread_self()->name, name_len);
220 | log_len += name_len;
221 | }
222 | else
223 | {
224 | log_len += ulog_strcpy(log_len, log_buf + log_len, "ISR");
225 | }
226 | }
227 | #endif /* ULOG_OUTPUT_THREAD_NAME */
228 |
229 | log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
230 |
231 | #ifdef ULOG_OUTPUT_FLOAT
232 | fmt_result = vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
233 | #else
234 | fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
235 | #endif /* ULOG_OUTPUT_FLOAT */
236 |
237 | /* calculate log length */
238 | if ((log_len + fmt_result <= ULOG_LINE_BUF_SIZE) && (fmt_result > -1))
239 | {
240 | log_len += fmt_result;
241 | }
242 | else
243 | {
244 | /* using max length */
245 | log_len = ULOG_LINE_BUF_SIZE;
246 | }
247 |
248 | /* overflow check and reserve some space for newline sign */
249 | if (log_len + newline_len > ULOG_LINE_BUF_SIZE)
250 | {
251 | /* using max length */
252 | log_len = ULOG_LINE_BUF_SIZE;
253 | /* reserve some space for newline sign */
254 | log_len -= newline_len;
255 | }
256 |
257 | /* package newline sign */
258 | if (newline)
259 | {
260 | log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
261 | }
262 |
263 | return log_len;
264 | }
265 |
266 | #endif /* ULOG_USING_SYSLOG */
267 |
--------------------------------------------------------------------------------
/Project/modules/ulog/syslog/syslog.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-09-07 armink the first version
9 | */
10 |
11 | #ifndef _SYSLOG_H_
12 | #define _SYSLOG_H_
13 |
14 | #ifdef __cplusplus
15 | extern "C" {
16 | #endif
17 |
18 | /*
19 | * priorities/facilities are encoded into a single 32-bit quantity, where the
20 | * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
21 | * (0-big number). Both the priorities and the facilities map roughly
22 | * one-to-one to strings in the syslogd(8) source code. This mapping is
23 | * included in this file.
24 | *
25 | * priorities (these are ordered)
26 | */
27 | #define LOG_EMERG 0 /* system is unusable */
28 | #define LOG_ALERT 1 /* action must be taken immediately */
29 | #define LOG_CRIT 2 /* critical conditions */
30 | #define LOG_ERR 3 /* error conditions */
31 | #define LOG_WARNING 4 /* warning conditions */
32 | #define LOG_NOTICE 5 /* normal but significant condition */
33 | #define LOG_INFO 6 /* informational */
34 | #define LOG_DEBUG 7 /* debug-level messages */
35 |
36 | #define LOG_PRIMASK 0x07
37 |
38 | #define LOG_PRI(p) ((p) & LOG_PRIMASK)
39 | #define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
40 |
41 | /* facility codes */
42 | #define LOG_KERN (0<<3) /* kernel messages */
43 | #define LOG_USER (1<<3) /* random user-level messages */
44 | #define LOG_MAIL (2<<3) /* mail system */
45 | #define LOG_DAEMON (3<<3) /* system daemons */
46 | #define LOG_AUTH (4<<3) /* security/authorization messages */
47 | #define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
48 | #define LOG_LPR (6<<3) /* line printer subsystem */
49 | #define LOG_NEWS (7<<3) /* network news subsystem */
50 | #define LOG_UUCP (8<<3) /* UUCP subsystem */
51 | #define LOG_CRON (9<<3) /* clock daemon */
52 | #define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
53 |
54 | /* other codes through 15 reserved for system use */
55 | #define LOG_LOCAL0 (16<<3) /* reserved for local use */
56 | #define LOG_LOCAL1 (17<<3) /* reserved for local use */
57 | #define LOG_LOCAL2 (18<<3) /* reserved for local use */
58 | #define LOG_LOCAL3 (19<<3) /* reserved for local use */
59 | #define LOG_LOCAL4 (20<<3) /* reserved for local use */
60 | #define LOG_LOCAL5 (21<<3) /* reserved for local use */
61 | #define LOG_LOCAL6 (22<<3) /* reserved for local use */
62 | #define LOG_LOCAL7 (23<<3) /* reserved for local use */
63 |
64 | #define LOG_NFACILITIES 24 /* current number of facilities */
65 | #define LOG_FACMASK 0x03f8 /* mask to extract facility part */
66 | /* facility of pri */
67 | #define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3)
68 |
69 | /*
70 | * arguments to setlogmask.
71 | */
72 | #define LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
73 | #define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1) /* all priorities through pri */
74 |
75 | /*
76 | * Option flags for openlog.
77 | *
78 | * LOG_ODELAY no longer does anything.
79 | * LOG_NDELAY is the inverse of what it used to be.
80 | */
81 | #define LOG_PID 0x01 /* log the pid with each message */
82 | #define LOG_CONS 0x02 /* log on the console if errors in sending */
83 | #define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */
84 | #define LOG_NDELAY 0x08 /* don't delay open */
85 | #define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */
86 | #define LOG_PERROR 0x20 /* log to stderr as well */
87 |
88 | #include
89 |
90 | void closelog(void);
91 | void openlog(const char *ident, int option, int facility);
92 | int setlogmask(int mask);
93 | void syslog(int priority, const char *format, ...);
94 | void vsyslog(int priority, const char *format, va_list args);
95 |
96 | #ifdef __cplusplus
97 | }
98 | #endif
99 |
100 | #endif /* _SYSLOG_H_ */
101 |
--------------------------------------------------------------------------------
/Project/modules/ulog/ulog.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-08-25 armink the first version
9 | */
10 |
11 | #ifndef _ULOG_H_
12 | #define _ULOG_H_
13 |
14 | #include
15 | #include "ulog_def.h"
16 |
17 | #ifdef __cplusplus
18 | extern "C" {
19 | #endif
20 |
21 | #define ULOG_VERSION_STR "0.1.1"
22 |
23 | /*
24 | * ulog init and deint
25 | */
26 | int ulog_init(void);
27 |
28 | /*
29 | * output different level log by LOG_X API
30 | *
31 | * NOTE: The `LOG_TAG` and `LOG_LVL` must be defined before including the when you want to use LOG_X API.
32 | *
33 | * #define LOG_TAG "example"
34 | * #define LOG_LVL LOG_LVL_DBG
35 | * #include
36 | *
37 | * Then you can using LOG_X API to output log
38 | *
39 | * LOG_D("this is a debug log!");
40 | * LOG_E("this is a error log!");
41 | */
42 | #define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__)
43 | #define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__)
44 | #define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__)
45 | #define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__)
46 | #define LOG_RAW(...) ulog_raw(__VA_ARGS__)
47 | #define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size)
48 |
49 | /*
50 | * backend register and unregister
51 | */
52 | rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color);
53 |
54 | #ifdef ULOG_USING_FILTER
55 | /*
56 | * log filter setting
57 | */
58 | int ulog_tag_lvl_filter_set(const char *tag, rt_uint32_t level);
59 | rt_uint32_t ulog_tag_lvl_filter_get(const char *tag);
60 | void ulog_global_filter_lvl_set(rt_uint32_t level);
61 | rt_uint32_t ulog_global_filter_lvl_get(void);
62 | void ulog_global_filter_tag_set(const char *tag);
63 | const char *ulog_global_filter_tag_get(void);
64 | void ulog_global_filter_kw_set(const char *keyword);
65 | const char *ulog_global_filter_kw_get(void);
66 | #endif /* ULOG_USING_FILTER */
67 |
68 | /*
69 | * flush all backends's log
70 | */
71 | void ulog_flush(void);
72 |
73 | /*
74 | * dump the hex format data to log
75 | */
76 | void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t size);
77 |
78 | /*
79 | * Another log output API. This API is more difficult to use than LOG_X API.
80 | */
81 | void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
82 | void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...);
83 | void ulog_raw(const char *format, ...);
84 |
85 | #ifdef __cplusplus
86 | }
87 | #endif
88 |
89 | #endif /* _ULOG_H_ */
90 |
--------------------------------------------------------------------------------
/Project/modules/ulog/ulog_def.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-2018, RT-Thread Development Team
3 | *
4 | * SPDX-License-Identifier: Apache-2.0
5 | *
6 | * Change Logs:
7 | * Date Author Notes
8 | * 2018-08-25 armink the first version
9 | */
10 |
11 | #ifndef _ULOG_DEF_H_
12 | #define _ULOG_DEF_H_
13 |
14 | #ifdef __cplusplus
15 | extern "C" {
16 | #endif
17 |
18 | /* logger level, the number is compatible for syslog */
19 | #define LOG_LVL_ASSERT 0
20 | #define LOG_LVL_ERROR 3
21 | #define LOG_LVL_WARNING 4
22 | #define LOG_LVL_INFO 6
23 | #define LOG_LVL_DBG 7
24 |
25 | /* the output silent level and all level for filter setting */
26 | #ifndef ULOG_USING_SYSLOG
27 | #define LOG_FILTER_LVL_SILENT 0
28 | #define LOG_FILTER_LVL_ALL 7
29 | #else
30 | #define LOG_FILTER_LVL_SILENT 1
31 | #define LOG_FILTER_LVL_ALL 255
32 | #endif /* ULOG_USING_SYSLOG */
33 |
34 | /* compatible for rtdbg */
35 | #undef LOG_D
36 | #undef LOG_I
37 | #undef LOG_W
38 | #undef LOG_E
39 | #undef LOG_RAW
40 | #undef DBG_ERROR
41 | #undef DBG_WARNING
42 | #undef DBG_INFO
43 | #undef DBG_LOG
44 | #undef dbg_log
45 | #define DBG_ERROR LOG_LVL_ERROR
46 | #define DBG_WARNING LOG_LVL_WARNING
47 | #define DBG_INFO LOG_LVL_INFO
48 | #define DBG_LOG LOG_LVL_DBG
49 | #define dbg_log(level, ...) \
50 | if ((level) <= LOG_LVL) \
51 | { \
52 | ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
53 | }
54 |
55 | #if !defined(LOG_TAG)
56 | /* compatible for rtdbg */
57 | #if defined(DBG_TAG)
58 | #define LOG_TAG DBG_TAG
59 | #elif defined(DBG_SECTION_NAME)
60 | #define LOG_TAG DBG_SECTION_NAME
61 | #else
62 | #define LOG_TAG "NO_TAG"
63 | #endif
64 | #endif /* !defined(LOG_TAG) */
65 |
66 | #if !defined(LOG_LVL)
67 | /* compatible for rtdbg */
68 | #if defined(DBG_LVL)
69 | #define LOG_LVL DBG_LVL
70 | #elif defined(DBG_LEVEL)
71 | #define LOG_LVL DBG_LEVEL
72 | #else
73 | #define LOG_LVL LOG_LVL_DBG
74 | #endif
75 | #endif /* !defined(LOG_LVL) */
76 |
77 | #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
78 | #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
79 | #else
80 | #define ulog_d(TAG, ...)
81 | #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
82 |
83 | #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
84 | #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
85 | #else
86 | #define ulog_i(TAG, ...)
87 | #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
88 |
89 | #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
90 | #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
91 | #else
92 | #define ulog_w(TAG, ...)
93 | #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
94 |
95 | #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
96 | #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
97 | #else
98 | #define ulog_e(TAG, ...)
99 | #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
100 |
101 | #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
102 | #define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
103 | #else
104 | #define ulog_hex(TAG, width, buf, size)
105 | #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
106 |
107 | /* assert for developer. */
108 | #ifdef ULOG_ASSERT_ENABLE
109 | #define ULOG_ASSERT(EXPR) \
110 | if (!(EXPR)) \
111 | { \
112 | ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
113 | ulog_flush(); \
114 | while (1); \
115 | }
116 | #else
117 | #define ULOG_ASSERT(EXPR)
118 | #endif
119 |
120 | /* ASSERT API definition */
121 | #if !defined(ASSERT)
122 | #define ASSERT ULOG_ASSERT
123 | #endif
124 |
125 | /* compatible for elog */
126 | #undef assert
127 | #undef log_e
128 | #undef log_w
129 | #undef log_i
130 | #undef log_d
131 | #undef log_v
132 | #undef ELOG_LVL_ASSERT
133 | #undef ELOG_LVL_ERROR
134 | #undef ELOG_LVL_WARN
135 | #undef ELOG_LVL_INFO
136 | #undef ELOG_LVL_DEBUG
137 | #undef ELOG_LVL_VERBOSE
138 | #define assert ASSERT
139 | #define log_e LOG_E
140 | #define log_w LOG_W
141 | #define log_i LOG_I
142 | #define log_d LOG_D
143 | #define log_v LOG_D
144 | #define log_raw LOG_RAW
145 | #define log_hex LOG_HEX
146 | #define ELOG_LVL_ASSERT LOG_LVL_ASSERT
147 | #define ELOG_LVL_ERROR LOG_LVL_ERROR
148 | #define ELOG_LVL_WARN LOG_LVL_WARNING
149 | #define ELOG_LVL_INFO LOG_LVL_INFO
150 | #define ELOG_LVL_DEBUG LOG_LVL_DBG
151 | #define ELOG_LVL_VERBOSE LOG_LVL_DBG
152 |
153 | /* setting static output log level */
154 | #ifndef ULOG_OUTPUT_LVL
155 | #define ULOG_OUTPUT_LVL LOG_LVL_DBG
156 | #endif
157 |
158 | /* buffer size for log rbb*/
159 | #ifndef ULOG_RBB_BUFSZ
160 | #define ULOG_RBB_BUFSZ 4096
161 | #endif
162 |
163 | /* buffer size for every line's log */
164 | #ifndef ULOG_LINE_BUF_SIZE
165 | #define ULOG_LINE_BUF_SIZE 128
166 | #endif
167 |
168 | /* output filter's tag max length */
169 | #ifndef ULOG_FILTER_TAG_MAX_LEN
170 | #define ULOG_FILTER_TAG_MAX_LEN 23
171 | #endif
172 |
173 | /* output filter's keyword max length */
174 | #ifndef ULOG_FILTER_KW_MAX_LEN
175 | #define ULOG_FILTER_KW_MAX_LEN 15
176 | #endif
177 |
178 | /* output filter's max num */
179 | #ifndef ULOG_FILTER_NUM
180 | #define ULOG_FILTER_NUM 10
181 | #endif
182 |
183 | #ifndef ULOG_NEWLINE_SIGN
184 | #define ULOG_NEWLINE_SIGN "\r\n"
185 | #endif
186 |
187 | /* tag's level filter */
188 | struct ulog_tag_lvl_filter
189 | {
190 | rt_uint8_t used;
191 | char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
192 | rt_uint32_t level;
193 | };
194 | typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
195 |
196 | struct ulog_backend
197 | {
198 | char name[RT_NAME_MAX];
199 | rt_bool_t support_color;
200 | void (*init) (struct ulog_backend *backend);
201 | void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, char *log, size_t len);
202 | void (*flush) (struct ulog_backend *backend);
203 | void (*deinit)(struct ulog_backend *backend);
204 | rt_slist_t list;
205 | };
206 | typedef struct ulog_backend *ulog_backend_t;
207 |
208 | #ifdef __cplusplus
209 | }
210 | #endif
211 |
212 | #endif /* _ULOG_DEF_H_ */
213 |
--------------------------------------------------------------------------------
/Project/modules/usr_device/usr_device.c:
--------------------------------------------------------------------------------
1 | #include "usr_device.h"
2 | #include
3 |
4 | #define device_init (dev->init)
5 | #define device_read (dev->read)
6 | #define device_write (dev->write)
7 | #define device_control (dev->control)
8 |
9 | static rt_slist_t usr_device_header = RT_SLIST_OBJECT_INIT(usr_device_header);
10 |
11 | usr_device_t usr_device_find(const char *name)
12 | {
13 | rt_slist_t *node;
14 |
15 | rt_base_t level = rt_hw_interrupt_disable();
16 |
17 | rt_slist_for_each(node, &usr_device_header)
18 | {
19 | usr_device_t dev = rt_slist_entry(node, struct usr_device, slist);
20 | if(rt_strncmp(dev->name, name, RT_NAME_MAX) == 0)
21 | {
22 | rt_hw_interrupt_enable(level);
23 |
24 | return dev;
25 | }
26 | }
27 |
28 | rt_hw_interrupt_enable(level);
29 |
30 | return RT_NULL;
31 | }
32 |
33 | rt_err_t usr_device_register(usr_device_t dev, const char *name)
34 | {
35 | if(usr_device_find(name) != RT_NULL)
36 | return -RT_ERROR;
37 |
38 | rt_strncpy(dev->name, name, RT_NAME_MAX);
39 |
40 | rt_slist_init(&(dev->slist));
41 |
42 | rt_base_t level = rt_hw_interrupt_disable();
43 |
44 | rt_slist_append(&usr_device_header, &(dev->slist));
45 |
46 | rt_hw_interrupt_enable(level);
47 |
48 | return RT_EOK;
49 | }
50 |
51 | rt_err_t usr_device_init(usr_device_t dev)
52 | {
53 | rt_err_t result = RT_EOK;
54 |
55 | RT_ASSERT(dev != RT_NULL);
56 |
57 | if(device_init != RT_NULL)
58 | result = device_init(dev);
59 |
60 | return result;
61 | }
62 |
63 | rt_size_t usr_device_read(usr_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
64 | {
65 | RT_ASSERT(dev != RT_NULL);
66 |
67 | if(device_read != RT_NULL)
68 | return device_read(dev, pos, buffer, size);
69 |
70 | return 0;
71 | }
72 |
73 | rt_size_t usr_device_write(usr_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
74 | {
75 | RT_ASSERT(dev != RT_NULL);
76 |
77 | if(device_write != RT_NULL)
78 | return device_write(dev, pos, buffer, size);
79 |
80 | return 0;
81 | }
82 |
83 | rt_err_t usr_device_control(usr_device_t dev, int cmd, void *args)
84 | {
85 | RT_ASSERT(dev != RT_NULL);
86 |
87 | if(device_control != RT_NULL)
88 | return device_control(dev, cmd, args);
89 |
90 | return -RT_ERROR;
91 | }
92 |
93 | rt_err_t usr_device_set_rx_indicate(usr_device_t dev, rt_err_t (*rx_indicate)(usr_device_t dev, rt_size_t size))
94 | {
95 | RT_ASSERT(dev != RT_NULL);
96 |
97 | rt_base_t level = rt_hw_interrupt_disable();
98 |
99 | dev->rx_indicate = rx_indicate;
100 |
101 | rt_hw_interrupt_enable(level);
102 |
103 | return RT_EOK;
104 | }
105 |
106 | rt_err_t usr_device_set_tx_complete(usr_device_t dev, rt_err_t (*tx_complete)(usr_device_t dev, void *buffer))
107 | {
108 | RT_ASSERT(dev != RT_NULL);
109 |
110 | rt_base_t level = rt_hw_interrupt_disable();
111 |
112 | dev->tx_complete = tx_complete;
113 |
114 | rt_hw_interrupt_enable(level);
115 |
116 | return RT_EOK;
117 | }
118 |
119 | static int list_usr_device(void)
120 | {
121 | rt_kprintf("-------------------------\r\n");
122 |
123 | rt_slist_t *node;
124 | rt_slist_for_each(node, &usr_device_header)
125 | {
126 | usr_device_t dev = rt_slist_entry(node, struct usr_device, slist);
127 | rt_kprintf("device: %-*.*s\r\n", RT_NAME_MAX, RT_NAME_MAX, dev->name);
128 | }
129 |
130 | return RT_EOK;
131 | }
132 | MSH_CMD_EXPORT(list_usr_device, list usr device);
133 |
--------------------------------------------------------------------------------
/Project/modules/usr_device/usr_device.h:
--------------------------------------------------------------------------------
1 | #ifndef __USR_DEVICE_H
2 | #define __USR_DEVICE_H
3 | #include
4 |
5 | typedef struct usr_device *usr_device_t;
6 |
7 | struct usr_device
8 | {
9 | char name[RT_NAME_MAX];
10 |
11 | rt_uint8_t error;
12 |
13 | /* device call back */
14 | rt_err_t (*rx_indicate)(usr_device_t dev, rt_size_t size);
15 | rt_err_t (*tx_complete)(usr_device_t dev, void *buffer);
16 |
17 | /* common device interface */
18 | rt_err_t (*init)(usr_device_t dev);
19 | rt_size_t (*read)(usr_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
20 | rt_size_t (*write)(usr_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
21 | rt_err_t (*control)(usr_device_t dev, int cmd, void *args);
22 |
23 | rt_slist_t slist;
24 | };
25 |
26 |
27 | usr_device_t usr_device_find(const char *name);
28 | rt_err_t usr_device_register(usr_device_t dev, const char *name);
29 | rt_err_t usr_device_init(usr_device_t dev);
30 | rt_size_t usr_device_read(usr_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
31 | rt_size_t usr_device_write(usr_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
32 | rt_err_t usr_device_control(usr_device_t dev, int cmd, void *args);
33 | rt_err_t usr_device_set_rx_indicate(usr_device_t dev, rt_err_t (*rx_indicate)(usr_device_t dev, rt_size_t size));
34 | rt_err_t usr_device_set_tx_complete(usr_device_t dev, rt_err_t (*tx_complete)(usr_device_t dev, void *buffer));
35 |
36 | #endif
37 |
--------------------------------------------------------------------------------
/Project/modules/wifi/wifi.h:
--------------------------------------------------------------------------------
1 | #ifndef __WIFI_H
2 | #define __WIFI_H
3 | #include
4 | #include "ringblk_buf.h"
5 | #include "usr_device.h"
6 | #include "at.h"
7 |
8 | #define WIFI_SERVER_MAX_CONN 5
9 | #define WIFI_CLIENT_RBB_BUFSZ 512
10 | #define WIFI_CLIENT_RBB_BLKNUM 10
11 |
12 | #define USR_DEVICE_WIFI_CMD_SMART 0x01
13 |
14 | typedef enum
15 | {
16 | WIFI_STATE_RESET = 0,
17 | WIFI_STATE_POWER_ON,
18 | WIFI_STATE_PARA_INIT,
19 | WIFI_STATE_NET_INIT,
20 | WIFI_STATE_NET_PROCESS
21 | }wifi_state_t;
22 |
23 | typedef enum
24 | {
25 | WIFI_SESSION_STATE_CLOSED = 0,
26 | WIFI_SESSION_STATE_CLOSING,
27 | WIFI_SESSION_STATE_CONNECTED,
28 | }wifi_session_state;
29 |
30 | struct wifi_session
31 | {
32 | int link_id;
33 | rt_tick_t timeout;
34 | rt_uint8_t recv_rbb_buf[WIFI_CLIENT_RBB_BUFSZ];
35 | struct rt_rbb_blk recv_rbb_blk[WIFI_CLIENT_RBB_BLKNUM];
36 | struct rt_rbb recv_rbb;
37 | wifi_session_state state;
38 | };
39 |
40 | struct usr_device_wifi
41 | {
42 | struct usr_device parent;
43 |
44 | rt_uint8_t init_ok;
45 | at_client_t client;
46 | rt_uint8_t smart_flag;
47 | struct rt_event evt;
48 | wifi_state_t wifi_state;
49 | struct wifi_session sessions[WIFI_SERVER_MAX_CONN];
50 |
51 | rt_uint16_t error_cnt;
52 | rt_tick_t wifi_timeout;
53 |
54 | char ssid[100];
55 | int rssi;
56 | char ip[16];
57 | char gateway[16];
58 | char netmask[16];
59 | };
60 |
61 | int wifi_session_send(struct wifi_session *session, const rt_uint8_t *buf, int buf_len);
62 |
63 | #endif
64 |
--------------------------------------------------------------------------------
/Project/modules/wifi/wifi_tcp_slave.c:
--------------------------------------------------------------------------------
1 | #include "wifi.h"
2 | #include "agile_modbus.h"
3 |
4 | #define SLAVE_ADDR 1
5 |
6 | static rt_uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH];
7 | static rt_uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH];
8 | static rt_uint16_t hold_registers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
9 |
10 | static int _modbus_slave_process(agile_modbus_t *ctx, int msg_length)
11 | {
12 | if(agile_modbus_receive_judge(ctx, msg_length) < 0)
13 | return -RT_ERROR;
14 |
15 | int offset;
16 | int slave;
17 | int function;
18 | uint16_t address;
19 | int rsp_length = 0;
20 | agile_modbus_sft_t sft;
21 |
22 | offset = ctx->backend->header_length;
23 | slave = ctx->read_buf[offset - 1];
24 | function = ctx->read_buf[offset];
25 | address = (ctx->read_buf[offset + 1] << 8) + ctx->read_buf[offset + 2];
26 |
27 | sft.slave = slave;
28 | sft.function = function;
29 | sft.t_id = ctx->backend->prepare_response_tid(ctx->read_buf, &msg_length);
30 |
31 | if(slave != SLAVE_ADDR)
32 | return 0;
33 |
34 | switch(function)
35 | {
36 | case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS:
37 | case AGILE_MODBUS_FC_READ_INPUT_REGISTERS:
38 | {
39 | int nb = (ctx->read_buf[offset + 3] << 8) + ctx->read_buf[offset + 4];
40 | if (nb < 1 || AGILE_MODBUS_MAX_READ_REGISTERS < nb)
41 | return -1;
42 | if ((address + nb) > 0x10000)
43 | return -1;
44 |
45 | rsp_length = ctx->backend->build_response_basis(&sft, ctx->send_buf);
46 | ctx->send_buf[rsp_length++] = nb << 1;
47 |
48 | for (int i = address; i < address + nb; i++)
49 | {
50 | uint16_t data = 0;
51 |
52 | if(i < 10)
53 | data = hold_registers[i];
54 |
55 | ctx->send_buf[rsp_length++] = data >> 8;
56 | ctx->send_buf[rsp_length++] = data & 0xFF;
57 | }
58 |
59 | rsp_length = ctx->backend->send_msg_pre(ctx->send_buf, rsp_length);
60 | }
61 | break;
62 |
63 | default:
64 | break;
65 | }
66 |
67 | return rsp_length;
68 | }
69 |
70 | int wifi_session_process(struct wifi_session *session, rt_uint8_t *recv_buf, int recv_len)
71 | {
72 | if((recv_len <= 0) || (recv_len > sizeof(ctx_read_buf)))
73 | return -RT_ERROR;
74 |
75 | agile_modbus_tcp_t ctx_tcp;
76 | agile_modbus_tcp_init(&ctx_tcp, ctx_send_buf, sizeof(ctx_send_buf), ctx_read_buf, sizeof(ctx_read_buf));
77 | rt_memcpy(ctx_tcp._ctx.read_buf, recv_buf, recv_len);
78 |
79 | int rsp_len = _modbus_slave_process(&(ctx_tcp._ctx), recv_len);
80 | if(rsp_len < 0)
81 | return -RT_ERROR;
82 |
83 | if(rsp_len > 0)
84 | {
85 | int send_len = wifi_session_send(session, ctx_tcp._ctx.send_buf, rsp_len);
86 |
87 | if(send_len != rsp_len)
88 | return -RT_ERROR;
89 | }
90 |
91 | return RT_EOK;
92 | }
93 |
--------------------------------------------------------------------------------
/Project/packages/agile_button/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Object files
5 | *.o
6 | *.ko
7 | *.obj
8 | *.elf
9 |
10 | # Linker output
11 | *.ilk
12 | *.map
13 | *.exp
14 |
15 | # Precompiled Headers
16 | *.gch
17 | *.pch
18 |
19 | # Libraries
20 | *.lib
21 | *.a
22 | *.la
23 | *.lo
24 |
25 | # Shared objects (inc. Windows DLLs)
26 | *.dll
27 | *.so
28 | *.so.*
29 | *.dylib
30 |
31 | # Executables
32 | *.exe
33 | *.out
34 | *.app
35 | *.i*86
36 | *.x86_64
37 | *.hex
38 |
39 | # Debug files
40 | *.dSYM/
41 | *.su
42 | *.idb
43 | *.pdb
44 | .vscode
45 |
46 | # Kernel Module Compile Results
47 | *.mod*
48 | *.cmd
49 | .tmp_versions/
50 | modules.order
51 | Module.symvers
52 | Mkfile.old
53 | dkms.conf
54 |
--------------------------------------------------------------------------------
/Project/packages/agile_button/README.md:
--------------------------------------------------------------------------------
1 | # agile_button
2 |
3 | ## 1、介绍
4 |
5 | agile_button是基于RT-Thread实现的button软件包,提供button操作的API。
6 |
7 | ### 1.1 特性
8 |
9 | 1. 代码简洁易懂,充分使用RT-Thread提供的API
10 | 2. 详细注释
11 | 3. 线程安全
12 | 4. 断言保护
13 | 5. API操作简单
14 |
15 | ### 1.2 目录结构
16 |
17 | | 名称 | 说明 |
18 | | ---- | ---- |
19 | | examples | 例子目录 |
20 | | inc | 头文件目录 |
21 | | src | 源代码目录 |
22 |
23 | ### 1.3 许可证
24 |
25 | agile_button package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。
26 |
27 | ### 1.4 依赖
28 |
29 | - RT-Thread 3.0+
30 | - RT-Thread 4.0+
31 |
32 | ## 2、如何打开 agile_button
33 |
34 | 使用 agile_button package 需要在 RT-Thread 的包管理器中选择它,具体路径如下:
35 |
36 | ```
37 | RT-Thread online packages
38 | peripheral libraries and drivers --->
39 | [*] agile_button: A agile button package
40 | ```
41 |
42 | 然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
43 |
44 | ## 3、使用 agile_button
45 |
46 | 在打开 agile_button package 后,当进行 bsp 编译时,它会被加入到 bsp 工程中进行编译。
47 |
48 | ### 3.1、API说明
49 |
50 | 1. 创建按键对象
51 |
52 | ```C
53 | agile_btn_t *agile_btn_create(rt_base_t pin, rt_base_t active_logic, rt_base_t pin_mode);
54 | ```
55 |
56 | |参数|注释|
57 | |----|----|
58 | |pin|按键引脚|
59 | |active_logic|有效电平(PIN_HIGH/PIN_LOW)|
60 | |pin_mode|引脚模式|
61 |
62 | |返回|注释|
63 | |----|----|
64 | |!=RT_NULL|agile_btn对象指针|
65 | |RT_NULL|异常|
66 |
67 | 2. 删除按键对象
68 |
69 | ```C
70 | int agile_btn_delete(agile_btn_t *btn);
71 | ```
72 |
73 | |参数|注释|
74 | |----|----|
75 | |btn|按键对象|
76 |
77 | |返回|注释|
78 | |----|----|
79 | |RT_EOK|成功|
80 |
81 | 3. 启动按键
82 |
83 | ```C
84 | int agile_btn_start(agile_btn_t *btn);
85 | ```
86 |
87 | |参数|注释|
88 | |----|----|
89 | |btn|按键对象|
90 |
91 | |返回|注释|
92 | |----|----|
93 | |RT_EOK|成功|
94 | |!=RT_OK|异常|
95 |
96 | 4. 停止按键
97 |
98 | ```C
99 | int agile_btn_stop(agile_btn_t *btn);
100 | ```
101 |
102 | |参数|注释|
103 | |----|----|
104 | |btn|按键对象|
105 |
106 | |返回|注释|
107 | |----|----|
108 | |RT_EOK|成功|
109 |
110 | 5. 设置按键消抖时间
111 |
112 | ```C
113 | int agile_btn_set_elimination_time(agile_btn_t *btn, uint8_t elimination_time);
114 | ```
115 |
116 | |参数|注释|
117 | |----|----|
118 | |btn|按键对象|
119 | |elimination_time|消抖时间(单位ms)|
120 |
121 | |返回|注释|
122 | |----|----|
123 | |RT_EOK|成功|
124 |
125 | 6. 设置按键按下后BTN_HOLD_EVENT事件回调函数的周期
126 |
127 | ```C
128 | int agile_btn_set_hold_cycle_time(agile_btn_t *btn, uint32_t hold_cycle_time);
129 | ```
130 |
131 | |参数|注释|
132 | |----|----|
133 | |btn|按键对象|
134 | |hold_cycle_time|周期时间(单位ms)|
135 |
136 | |返回|注释|
137 | |----|----|
138 | |RT_EOK|成功|
139 |
140 | 7. 设置按键事件回调函数
141 |
142 | ```C
143 | int agile_btn_set_event_cb(agile_btn_t *btn, enum agile_btn_event event, void (*event_cb)(agile_btn_t *btn));
144 | ```
145 |
146 | |参数|注释|
147 | |----|----|
148 | |btn|按键对象|
149 | |event|事件|
150 | |event_cb|回调函数|
151 |
152 | |返回|注释|
153 | |----|----|
154 | |RT_EOK|成功|
155 | |!=RT_OK|异常|
156 |
157 | ### 3.2、示例
158 |
159 | 使用示例在 [examples](./examples) 下。
160 |
161 | ## 4、注意事项
162 |
163 | 1. 调用 `agile_btn_create` API创建完按键对象后,调用其他API确保按键对象创建成功,否则被断言。
164 | 2. 两次按下间隔在500ms内记为连续按下, `repeact_cnt` 变量自加,否则为0。应用层可以根据该变量得到连续按下多少次,执行相应的动作。
165 | 3. 消抖时间设置可以使用 `agile_btn_set_elimination_time` 该API设置,默认为15ms;BTN_HOLD_EVENT事件回调函数的周期可以使用 `agile_btn_set_hold_cycle_time` 该API设置,默认为1s。
166 | 4. 通过 `hold_time` 变量可以知道按下持续时间,应用层可以根据其做相应的操作
167 |
168 | ## 5、联系方式 & 感谢
169 |
170 | * 维护:马龙伟
171 | * 主页:
172 | * 邮箱:<2544047213@qq.com>
--------------------------------------------------------------------------------
/Project/packages/agile_button/SConscript:
--------------------------------------------------------------------------------
1 | # RT-Thread building script for bridge
2 |
3 | import os
4 | from building import *
5 |
6 | cwd = GetCurrentDir()
7 | objs = []
8 | list = os.listdir(cwd)
9 |
10 | if GetDepend('PKG_USING_AGILE_BUTTON'):
11 | for d in list:
12 | path = os.path.join(cwd, d)
13 | if os.path.isfile(os.path.join(path, 'SConscript')):
14 | objs = objs + SConscript(os.path.join(d, 'SConscript'))
15 |
16 | Return('objs')
17 |
--------------------------------------------------------------------------------
/Project/packages/agile_button/inc/agile_button.h:
--------------------------------------------------------------------------------
1 | #ifndef __PKG_AGILE_BUTTON_H
2 | #define __PKG_AGILE_BUTTON_H
3 | #include
4 | #include "drv_gpio.h"
5 | #include
6 |
7 | #ifdef __cplusplus
8 | extern "C" {
9 | #endif
10 |
11 | enum agile_btn_event
12 | {
13 | BTN_PRESS_DOWN_EVENT = 0,
14 | BTN_HOLD_EVENT,
15 | BTN_PRESS_UP_EVENT,
16 | BTN_CLICK_EVENT,
17 | BTN_EVENT_SUM
18 | };
19 |
20 | enum agile_btn_state
21 | {
22 | BTN_STATE_NONE_PRESS = 0,
23 | BTN_STATE_CHECK_PRESS,
24 | BTN_STATE_PRESS_DOWN,
25 | BTN_STATE_PRESS_HOLD,
26 | BTN_STATE_PRESS_UP,
27 | };
28 |
29 | // agile_button 结构体
30 | typedef struct agile_btn agile_btn_t;
31 |
32 | struct agile_btn
33 | {
34 | uint8_t active; // 激活标志
35 | uint8_t repeat_cnt; // 按键重按计数
36 | uint8_t elimination_time; // 按键消抖时间(单位ms,默认15ms)
37 | enum agile_btn_event event; // 按键对象事件
38 | enum agile_btn_state state; // 按键对象状态
39 | uint32_t hold_time; // 按键按下持续时间(单位ms)
40 | uint32_t prev_hold_time; // 缓存hold_time变量
41 | uint32_t hold_cycle_time; // 按键按下后持续调用回调函数的周期(单位ms,默认1s)
42 | rt_base_t pin; // 按键引脚
43 | rt_base_t active_logic; // 有效电平(PIN_HIGH/PIN_LOW)
44 | rt_tick_t tick_timeout; // 超时时间
45 | void (*event_cb[BTN_EVENT_SUM])(agile_btn_t *btn); // 按键对象事件回调函数
46 | void (*hook)(agile_btn_t *btn); // 按键钩子回调函数
47 | rt_slist_t slist; // 单向链表节点
48 | };
49 |
50 | // 初始化按键对象
51 | int agile_btn_init(agile_btn_t *btn, rt_base_t pin, rt_base_t active_logic, rt_base_t pin_mode);
52 | // 启动按键
53 | int agile_btn_start(agile_btn_t *btn);
54 | // 停止按键
55 | int agile_btn_stop(agile_btn_t *btn);
56 | // 设置按键消抖时间
57 | int agile_btn_set_elimination_time(agile_btn_t *btn, uint8_t elimination_time);
58 | // 设置按键按下后BTN_HOLD_EVENT事件回调函数的周期
59 | int agile_btn_set_hold_cycle_time(agile_btn_t *btn, uint32_t hold_cycle_time);
60 | // 设置按键事件回调函数
61 | int agile_btn_set_event_cb(agile_btn_t *btn, enum agile_btn_event event, void (*event_cb)(agile_btn_t *btn));
62 | // 设置按键钩子回调函数
63 | int agile_btn_set_hook(agile_btn_t *btn, void (*hook)(agile_btn_t *btn));
64 |
65 | #ifdef __cplusplus
66 | }
67 | #endif
68 |
69 | #endif
70 |
--------------------------------------------------------------------------------
/Project/packages/agile_button/src/SConscript:
--------------------------------------------------------------------------------
1 | from building import *
2 |
3 | cwd = GetCurrentDir()
4 | src = Glob('*.c') + Glob('*.cpp')
5 | CPPPATH = [cwd + '/../inc']
6 |
7 | group = DefineGroup('agile_button', src, depend = ['PKG_USING_AGILE_BUTTON'], CPPPATH = CPPPATH)
8 |
9 | Return('group')
10 |
--------------------------------------------------------------------------------
/Project/packages/agile_button/src/agile_button.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #define DBG_ENABLE
4 | #define DBG_COLOR
5 | #define DBG_SECTION_NAME "agile_button"
6 | #ifdef PKG_AGILE_BUTTON_DEBUG
7 | #define DBG_LEVEL DBG_LOG
8 | #else
9 | #define DBG_LEVEL DBG_INFO
10 | #endif
11 | #include
12 |
13 | // agile_button 线程堆栈大小
14 | #ifndef PKG_AGILE_BUTTON_THREAD_STACK_SIZE
15 | #define PKG_AGILE_BUTTON_THREAD_STACK_SIZE 512
16 | #endif
17 |
18 | // agile_button 线程优先级
19 | #ifndef PKG_AGILE_BUTTON_THREAD_PRIORITY
20 | #define PKG_AGILE_BUTTON_THREAD_PRIORITY RT_THREAD_PRIORITY_MAX - 2
21 | #endif
22 |
23 | // 按键消抖默认时间 15ms
24 | #define AGILE_BUTTON_ELIMINATION_TIME_DEFAULT 15
25 |
26 | // 两次按键按下间隔超过500ms清零重复计数
27 | #define AGILE_BUTTON_TWO_INTERVAL_TIME_DEFAULT 500
28 |
29 | // 按键按下后持续调用回调函数的周期
30 | #define AGILE_BUTTON_HOLD_CYCLE_TIME_DEFAULT 1000
31 |
32 | // 获取按键引脚电平状态
33 | #define AGILE_BUTTON_PIN_STATE(btn) drv_pin_read(btn->pin)
34 |
35 | // 调用按键事件的回调函数
36 | #define AGILE_BUTTON_EVENT_CB(btn, event) do { \
37 | RT_ASSERT(btn); \
38 | if(btn->event_cb[event]) { \
39 | btn->event_cb[event](btn); \
40 | } \
41 | } while(0)
42 |
43 | ALIGN(RT_ALIGN_SIZE)
44 | // agile_button 线程
45 | static rt_uint8_t agile_button_stack[PKG_AGILE_BUTTON_THREAD_STACK_SIZE];
46 | static struct rt_thread agile_button_thread;
47 | // agile_button 单向链表
48 | static rt_slist_t agile_btn_list = RT_SLIST_OBJECT_INIT(agile_btn_list);
49 | // agile_button 互斥锁
50 | static struct rt_mutex lock_mtx;
51 | // agile_button 初始化完成标志
52 | static uint8_t is_initialized = 0;
53 |
54 |
55 | int agile_btn_init(agile_btn_t *btn, rt_base_t pin, rt_base_t active_logic, rt_base_t pin_mode)
56 | {
57 | if (!is_initialized)
58 | {
59 | LOG_E("Agile button haven't initialized!");
60 | return -RT_ERROR;
61 | }
62 |
63 | rt_memset(btn, 0, sizeof(agile_btn_t));
64 | btn->active = 0;
65 | btn->repeat_cnt = 0;
66 | btn->elimination_time = AGILE_BUTTON_ELIMINATION_TIME_DEFAULT;
67 | btn->event = BTN_EVENT_SUM;
68 | btn->state = BTN_STATE_NONE_PRESS;
69 | btn->hold_time = 0;
70 | btn->prev_hold_time = 0;
71 | btn->hold_cycle_time = AGILE_BUTTON_HOLD_CYCLE_TIME_DEFAULT;
72 | btn->pin = pin;
73 | btn->active_logic = active_logic;
74 | btn->tick_timeout = rt_tick_get();
75 | rt_slist_init(&(btn->slist));
76 |
77 | drv_pin_mode(pin, pin_mode);
78 |
79 | return RT_EOK;
80 | }
81 |
82 | int agile_btn_start(agile_btn_t *btn)
83 | {
84 | RT_ASSERT(btn);
85 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
86 | if(btn->active)
87 | {
88 | rt_mutex_release(&lock_mtx);
89 | return -RT_ERROR;
90 | }
91 | btn->repeat_cnt = 0;
92 | btn->event = BTN_EVENT_SUM;
93 | btn->state = BTN_STATE_NONE_PRESS;
94 | btn->hold_time = 0;
95 | btn->prev_hold_time = 0;
96 | btn->tick_timeout = rt_tick_get();
97 | rt_slist_append(&(agile_btn_list), &(btn->slist));
98 | btn->active = 1;
99 | rt_mutex_release(&lock_mtx);
100 | return RT_EOK;
101 | }
102 |
103 | int agile_btn_stop(agile_btn_t *btn)
104 | {
105 | RT_ASSERT(btn);
106 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
107 | if(!btn->active)
108 | {
109 | rt_mutex_release(&lock_mtx);
110 | return RT_EOK;
111 | }
112 | rt_slist_remove(&(agile_btn_list), &(btn->slist));
113 | btn->slist.next = RT_NULL;
114 | btn->active = 0;
115 | rt_mutex_release(&lock_mtx);
116 | return RT_EOK;
117 | }
118 |
119 | int agile_btn_set_elimination_time(agile_btn_t *btn, uint8_t elimination_time)
120 | {
121 | RT_ASSERT(btn);
122 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
123 | btn->elimination_time = elimination_time;
124 | rt_mutex_release(&lock_mtx);
125 | return RT_EOK;
126 | }
127 |
128 | int agile_btn_set_hold_cycle_time(agile_btn_t *btn, uint32_t hold_cycle_time)
129 | {
130 | RT_ASSERT(btn);
131 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
132 | btn->hold_cycle_time = hold_cycle_time;
133 | rt_mutex_release(&lock_mtx);
134 | return RT_EOK;
135 | }
136 |
137 | int agile_btn_set_event_cb(agile_btn_t *btn, enum agile_btn_event event, void (*event_cb)(agile_btn_t *btn))
138 | {
139 | RT_ASSERT(btn);
140 | if(event >= BTN_EVENT_SUM)
141 | return -RT_ERROR;
142 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
143 | btn->event_cb[event] = event_cb;
144 | rt_mutex_release(&lock_mtx);
145 | return RT_EOK;
146 | }
147 |
148 | int agile_btn_set_hook(agile_btn_t *btn, void (*hook)(agile_btn_t *btn))
149 | {
150 | RT_ASSERT(btn);
151 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
152 | btn->hook = hook;
153 | rt_mutex_release(&lock_mtx);
154 | return RT_EOK;
155 | }
156 |
157 | static void agile_btn_cal_hold_time(agile_btn_t *btn)
158 | {
159 | RT_ASSERT(btn);
160 | if(rt_tick_get() < btn->tick_timeout)
161 | {
162 | btn->hold_time = RT_TICK_MAX - btn->tick_timeout + rt_tick_get();
163 | }
164 | else
165 | {
166 | btn->hold_time = rt_tick_get() - btn->tick_timeout;
167 | }
168 | btn->hold_time = btn->hold_time * (1000 / RT_TICK_PER_SECOND);
169 | }
170 |
171 | static void btn_process(void *parameter)
172 | {
173 | rt_slist_t *node;
174 | while (1)
175 | {
176 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
177 | rt_slist_for_each(node, &(agile_btn_list))
178 | {
179 | agile_btn_t *btn = rt_slist_entry(node, agile_btn_t, slist);
180 | if(btn->hook)
181 | btn->hook(btn);
182 |
183 | switch (btn->state)
184 | {
185 | case BTN_STATE_NONE_PRESS:
186 | {
187 | if (AGILE_BUTTON_PIN_STATE(btn) == btn->active_logic)
188 | {
189 | btn->tick_timeout = rt_tick_get() + rt_tick_from_millisecond(btn->elimination_time);
190 | btn->state = BTN_STATE_CHECK_PRESS;
191 | }
192 | else
193 | {
194 | // 2次按下中间间隔过大,清零重按计数
195 | if (btn->repeat_cnt)
196 | {
197 | if ((rt_tick_get() - btn->tick_timeout) < (RT_TICK_MAX / 2))
198 | {
199 | btn->repeat_cnt = 0;
200 | }
201 | }
202 | }
203 | }
204 | break;
205 | case BTN_STATE_CHECK_PRESS:
206 | {
207 | if (AGILE_BUTTON_PIN_STATE(btn) == btn->active_logic)
208 | {
209 | if ((rt_tick_get() - btn->tick_timeout) < (RT_TICK_MAX / 2))
210 | {
211 | btn->state = BTN_STATE_PRESS_DOWN;
212 | }
213 | }
214 | else
215 | {
216 | btn->state = BTN_STATE_NONE_PRESS;
217 | }
218 | }
219 | break;
220 | case BTN_STATE_PRESS_DOWN:
221 | {
222 | btn->hold_time = 0;
223 | btn->prev_hold_time = 0;
224 | btn->repeat_cnt++;
225 | btn->event = BTN_PRESS_DOWN_EVENT;
226 | AGILE_BUTTON_EVENT_CB(btn, btn->event);
227 |
228 | btn->tick_timeout = rt_tick_get();
229 | btn->state = BTN_STATE_PRESS_HOLD;
230 | }
231 | break;
232 | case BTN_STATE_PRESS_HOLD:
233 | {
234 | if (AGILE_BUTTON_PIN_STATE(btn) == btn->active_logic)
235 | {
236 | agile_btn_cal_hold_time(btn);
237 | if (btn->hold_time - btn->prev_hold_time >= btn->hold_cycle_time)
238 | {
239 | btn->event = BTN_HOLD_EVENT;
240 | AGILE_BUTTON_EVENT_CB(btn, btn->event);
241 | btn->prev_hold_time = btn->hold_time;
242 | }
243 | }
244 | else
245 | {
246 | btn->state = BTN_STATE_PRESS_UP;
247 | }
248 | }
249 | break;
250 | case BTN_STATE_PRESS_UP:
251 | {
252 | btn->event = BTN_PRESS_UP_EVENT;
253 | AGILE_BUTTON_EVENT_CB(btn, btn->event);
254 | btn->event = BTN_CLICK_EVENT;
255 | AGILE_BUTTON_EVENT_CB(btn, btn->event);
256 |
257 | btn->tick_timeout = rt_tick_get() + rt_tick_from_millisecond(AGILE_BUTTON_TWO_INTERVAL_TIME_DEFAULT);
258 | btn->state = BTN_STATE_NONE_PRESS;
259 | }
260 | break;
261 | default:
262 | break;
263 | }
264 | }
265 | rt_mutex_release(&lock_mtx);
266 | rt_thread_mdelay(5);
267 | }
268 | }
269 |
270 |
271 | static int agile_btn_env_init(void)
272 | {
273 | rt_mutex_init(&lock_mtx, "btn_mtx", RT_IPC_FLAG_FIFO);
274 |
275 | rt_thread_init(&agile_button_thread,
276 | "agile_btn",
277 | btn_process,
278 | RT_NULL,
279 | &agile_button_stack[0],
280 | sizeof(agile_button_stack),
281 | PKG_AGILE_BUTTON_THREAD_PRIORITY,
282 | 100);
283 | rt_thread_startup(&agile_button_thread);
284 |
285 | is_initialized = 1;
286 | return RT_EOK;
287 | }
288 | INIT_APP_EXPORT(agile_btn_env_init);
289 |
--------------------------------------------------------------------------------
/Project/packages/agile_led/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Object files
5 | *.o
6 | *.ko
7 | *.obj
8 | *.elf
9 |
10 | # Linker output
11 | *.ilk
12 | *.map
13 | *.exp
14 |
15 | # Precompiled Headers
16 | *.gch
17 | *.pch
18 |
19 | # Libraries
20 | *.lib
21 | *.a
22 | *.la
23 | *.lo
24 |
25 | # Shared objects (inc. Windows DLLs)
26 | *.dll
27 | *.so
28 | *.so.*
29 | *.dylib
30 |
31 | # Executables
32 | *.exe
33 | *.out
34 | *.app
35 | *.i*86
36 | *.x86_64
37 | *.hex
38 |
39 | # Debug files
40 | *.dSYM/
41 | *.su
42 | *.idb
43 | *.pdb
44 | .vscode
45 |
46 | # Kernel Module Compile Results
47 | *.mod*
48 | *.cmd
49 | .tmp_versions/
50 | modules.order
51 | Module.symvers
52 | Mkfile.old
53 | dkms.conf
54 |
--------------------------------------------------------------------------------
/Project/packages/agile_led/README.md:
--------------------------------------------------------------------------------
1 | # agile_led
2 |
3 | ## 1、介绍
4 |
5 | agile_led是基于RT-Thread实现的led软件包,提供led操作的API。
6 |
7 | ### 1.1 特性
8 |
9 | 1. 代码简洁易懂,充分使用RT-Thread提供的API
10 | 2. 详细注释
11 | 3. 线程安全
12 | 4. 断言保护
13 | 5. API操作简单
14 |
15 | ### 1.2 目录结构
16 |
17 | | 名称 | 说明 |
18 | | ---- | ---- |
19 | | examples | 例子目录 |
20 | | inc | 头文件目录 |
21 | | src | 源代码目录 |
22 |
23 | ### 1.3 许可证
24 |
25 | agile_led package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。
26 |
27 | ### 1.4 依赖
28 |
29 | - RT-Thread 3.0+
30 | - RT-Thread 4.0+
31 |
32 | ## 2、如何打开 agile_led
33 |
34 | 使用 agile_led package 需要在 RT-Thread 的包管理器中选择它,具体路径如下:
35 |
36 | ```
37 | RT-Thread online packages
38 | peripheral libraries and drivers --->
39 | [*] agile_led: A agile led package
40 | ```
41 |
42 | 然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
43 |
44 | ## 3、使用 agile_led
45 |
46 | 在打开 agile_led package 后,当进行 bsp 编译时,它会被加入到 bsp 工程中进行编译。
47 |
48 | ### 3.1、API说明
49 |
50 | 1. 创建led对象
51 |
52 | ```C
53 | agile_led_t *agile_led_create(rt_base_t pin, rt_base_t active_logic, const char *light_mode, int32_t loop_cnt);
54 | ```
55 |
56 | |参数|注释|
57 | |----|----|
58 | |pin|控制led的引脚|
59 | |active_logic|led有效电平(PIN_HIGH/PIN_LOW)|
60 | |light_mode|闪烁模式字符串|
61 | |loop_cnt|循环次数(负数为永久循环)|
62 |
63 | |返回|注释|
64 | |----|----|
65 | |!=RT_NULL|agile_led对象指针|
66 | |RT_NULL|异常|
67 |
68 | 2. 删除led对象
69 |
70 | ```C
71 | int agile_led_delete(agile_led_t *led);
72 | ```
73 |
74 | |参数|注释|
75 | |----|----|
76 | |led|led对象指针|
77 |
78 | |返回|注释|
79 | |----|----|
80 | |RT_EOK|成功|
81 |
82 | 3. 启动led对象,根据设置的模式执行动作
83 |
84 | ```C
85 | int agile_led_start(agile_led_t *led);
86 | ```
87 |
88 | |参数|注释|
89 | |----|----|
90 | |led|led对象指针|
91 |
92 | |返回|注释|
93 | |----|----|
94 | |RT_EOK|成功|
95 | |!=RT_OK|异常|
96 |
97 | 4. 停止led对象
98 |
99 | ```C
100 | int agile_led_stop(agile_led_t *led);
101 | ```
102 |
103 | |参数|注释|
104 | |----|----|
105 | |led|led对象指针|
106 |
107 | |返回|注释|
108 | |----|----|
109 | |RT_EOK|成功|
110 |
111 | 5. 设置led对象的模式
112 |
113 | ```C
114 | int agile_led_set_light_mode(agile_led_t *led, const char *light_mode, int32_t loop_cnt);
115 | ```
116 |
117 | |参数|注释|
118 | |----|----|
119 | |led|led对象指针|
120 | |light_mode|闪烁模式字符串|
121 | |loop_cnt|循环次数(负数为永久循环)|
122 |
123 | |返回|注释|
124 | |----|----|
125 | |RT_EOK|成功|
126 | |!=RT_EOK|异常|
127 |
128 | 6. 设置led对象操作完成的回调函数
129 |
130 | ```C
131 | int agile_led_set_compelete_callback(agile_led_t *led, void (*compelete)(agile_led_t *led));
132 | ```
133 |
134 | |参数|注释|
135 | |----|----|
136 | |led|led对象指针|
137 | |compelete|操作完成回调函数|
138 |
139 | |返回|注释|
140 | |----|----|
141 | |RT_EOK|成功|
142 |
143 | 7. led对象电平翻转
144 |
145 | ```C
146 | void agile_led_toggle(agile_led_t *led);
147 | ```
148 |
149 | |参数|注释|
150 | |----|----|
151 | |led|led对象指针|
152 |
153 | 8. led对象亮
154 |
155 | ```C
156 | void agile_led_on(agile_led_t *led);
157 | ```
158 |
159 | |参数|注释|
160 | |----|----|
161 | |led|led对象指针|
162 |
163 | 9. led对象灭
164 |
165 | ```C
166 | void agile_led_off(agile_led_t *led);
167 | ```
168 |
169 | |参数|注释|
170 | |----|----|
171 | |led|led对象指针|
172 |
173 | ### 3.2、示例
174 |
175 | 使用示例在 [examples](./examples) 下。
176 |
177 | ## 4、注意事项
178 |
179 | 1. 调用 `agile_led_create` API创建完led对象后,调用其他API确保led对象创建成功,否则被断言。
180 | 2. 调用 `agile_led_create` 和 `agile_led_set_light_mode` API时,参数 `light_mode` 可以为RT_NULL。
181 | 3. `light_mode` 确保时字符串形式,如 `"100,50,10,60"` 或 `"100,50,10,60,"` ,只支持正整数,按照亮灭亮灭...规律。
182 |
183 | ## 5、联系方式 & 感谢
184 |
185 | * 维护:马龙伟
186 | * 主页:
187 | * 邮箱:<2544047213@qq.com>
--------------------------------------------------------------------------------
/Project/packages/agile_led/SConscript:
--------------------------------------------------------------------------------
1 | # RT-Thread building script for bridge
2 |
3 | import os
4 | from building import *
5 |
6 | cwd = GetCurrentDir()
7 | objs = []
8 | list = os.listdir(cwd)
9 |
10 | if GetDepend('PKG_USING_AGILE_LED'):
11 | for d in list:
12 | path = os.path.join(cwd, d)
13 | if os.path.isfile(os.path.join(path, 'SConscript')):
14 | objs = objs + SConscript(os.path.join(d, 'SConscript'))
15 |
16 | Return('objs')
17 |
--------------------------------------------------------------------------------
/Project/packages/agile_led/inc/agile_led.h:
--------------------------------------------------------------------------------
1 | #ifndef __PKG_AGILE_LED_H
2 | #define __PKG_AGILE_LED_H
3 | #include
4 | #include "drv_gpio.h"
5 | #include
6 |
7 | #ifdef __cplusplus
8 | extern "C" {
9 | #endif
10 |
11 | // agile_led 结构体
12 | typedef struct agile_led agile_led_t;
13 |
14 | struct agile_led
15 | {
16 | uint8_t active; // 激活标志
17 | rt_base_t pin; // 控制引脚
18 | rt_base_t active_logic; // 有效电平(PIN_HIGH/PIN_LOW)
19 | const uint32_t *light_arr; // 闪烁数组
20 | uint32_t arr_num; // 数组元素数目
21 | uint32_t arr_index; // 数组索引
22 | int32_t loop_init; // 循环次数
23 | int32_t loop_cnt; // 循环次数计数
24 | rt_tick_t tick_timeout; // 超时时间
25 | void (*compelete)(agile_led_t *led); // 操作完成回调函数
26 | rt_slist_t slist; // 单向链表节点
27 | };
28 |
29 | // 初始化led对象
30 | int agile_led_init(agile_led_t *led, rt_base_t pin, rt_base_t active_logic, const uint32_t *light_array, int array_size, int32_t loop_cnt);
31 | // 启动led对象,根据设置的模式执行动作
32 | int agile_led_start(agile_led_t *led);
33 | // 停止led对象
34 | int agile_led_stop(agile_led_t *led);
35 | // 设置led对象的模式
36 | int agile_led_set_light_mode(agile_led_t *led, const uint32_t *light_array, int array_size, int32_t loop_cnt);
37 | // 设置led对象操作完成的回调函数
38 | int agile_led_set_compelete_callback(agile_led_t *led, void (*compelete)(agile_led_t *led));
39 | // led对象电平翻转
40 | void agile_led_toggle(agile_led_t *led);
41 | // led对象亮
42 | void agile_led_on(agile_led_t *led);
43 | // led对象灭
44 | void agile_led_off(agile_led_t *led);
45 |
46 | #ifdef __cplusplus
47 | }
48 | #endif
49 |
50 | #endif
51 |
--------------------------------------------------------------------------------
/Project/packages/agile_led/src/SConscript:
--------------------------------------------------------------------------------
1 | from building import *
2 |
3 | cwd = GetCurrentDir()
4 | src = Glob('*.c') + Glob('*.cpp')
5 | CPPPATH = [cwd + '/../inc']
6 |
7 | group = DefineGroup('agile_led', src, depend = ['PKG_USING_AGILE_LED'], CPPPATH = CPPPATH)
8 |
9 | Return('group')
10 |
--------------------------------------------------------------------------------
/Project/packages/agile_led/src/agile_led.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #define DBG_ENABLE
6 | #define DBG_COLOR
7 | #define DBG_SECTION_NAME "agile_led"
8 | #ifdef PKG_AGILE_LED_DEBUG
9 | #define DBG_LEVEL DBG_LOG
10 | #else
11 | #define DBG_LEVEL DBG_INFO
12 | #endif
13 | #include
14 |
15 | // agile_led 线程堆栈大小
16 | #ifndef PKG_AGILE_LED_THREAD_STACK_SIZE
17 | #define PKG_AGILE_LED_THREAD_STACK_SIZE 256
18 | #endif
19 |
20 | // agile_led 线程优先级
21 | #ifndef PKG_AGILE_LED_THREAD_PRIORITY
22 | #define PKG_AGILE_LED_THREAD_PRIORITY RT_THREAD_PRIORITY_MAX - 2
23 | #endif
24 |
25 | ALIGN(RT_ALIGN_SIZE)
26 | // agile_led 线程
27 | static rt_uint8_t agile_led_stack[PKG_AGILE_LED_THREAD_STACK_SIZE];
28 | static struct rt_thread agile_led_thread;
29 | // agile_led 单向链表
30 | static rt_slist_t agile_led_list = RT_SLIST_OBJECT_INIT(agile_led_list);
31 | // agile_led 互斥锁
32 | static struct rt_mutex lock_mtx;
33 | // agile_led 初始化完成标志
34 | static uint8_t is_initialized = 0;
35 |
36 |
37 | static void agile_led_default_compelete_callback(agile_led_t *led)
38 | {
39 | RT_ASSERT(led);
40 | LOG_D("led pin:%d compeleted.", led->pin);
41 | }
42 |
43 | int agile_led_init(agile_led_t *led, rt_base_t pin, rt_base_t active_logic, const uint32_t *light_array, int array_size, int32_t loop_cnt)
44 | {
45 | if (!is_initialized)
46 | {
47 | LOG_E("Agile led haven't initialized!");
48 | return -RT_ERROR;
49 | }
50 |
51 | rt_memset(led, 0, sizeof(agile_led_t));
52 | led->active = 0;
53 | led->pin = pin;
54 | led->active_logic = active_logic;
55 | led->light_arr = light_array;
56 | led->arr_num = array_size;
57 | led->arr_index = 0;
58 | led->loop_init = loop_cnt;
59 | led->loop_cnt = led->loop_init;
60 | led->tick_timeout = rt_tick_get();
61 | led->compelete = agile_led_default_compelete_callback;
62 | rt_slist_init(&(led->slist));
63 |
64 | drv_pin_mode(pin, PIN_MODE_OUTPUT);
65 | drv_pin_write(pin, !active_logic);
66 |
67 | return RT_EOK;
68 | }
69 |
70 | int agile_led_start(agile_led_t *led)
71 | {
72 | RT_ASSERT(led);
73 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
74 | if(led->active)
75 | {
76 | rt_mutex_release(&lock_mtx);
77 | return -RT_ERROR;
78 | }
79 | if((led->light_arr == RT_NULL) || (led->arr_num == 0))
80 | {
81 | rt_mutex_release(&lock_mtx);
82 | return -RT_ERROR;
83 | }
84 | led->arr_index = 0;
85 | led->loop_cnt = led->loop_init;
86 | led->tick_timeout = rt_tick_get();
87 | rt_slist_append(&(agile_led_list), &(led->slist));
88 | led->active = 1;
89 | rt_mutex_release(&lock_mtx);
90 | return RT_EOK;
91 | }
92 |
93 | int agile_led_stop(agile_led_t *led)
94 | {
95 | RT_ASSERT(led);
96 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
97 | if(!led->active)
98 | {
99 | rt_mutex_release(&lock_mtx);
100 | return RT_EOK;
101 | }
102 | rt_slist_remove(&(agile_led_list), &(led->slist));
103 | led->slist.next = RT_NULL;
104 | led->active = 0;
105 | rt_mutex_release(&lock_mtx);
106 | return RT_EOK;
107 | }
108 |
109 | int agile_led_set_light_mode(agile_led_t *led, const uint32_t *light_array, int array_size, int32_t loop_cnt)
110 | {
111 | RT_ASSERT(led);
112 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
113 | led->light_arr = light_array;
114 | led->arr_num = array_size;
115 | led->arr_index = 0;
116 | led->loop_init = loop_cnt;
117 | led->loop_cnt = led->loop_init;
118 | led->tick_timeout = rt_tick_get();
119 | rt_mutex_release(&lock_mtx);
120 | return RT_EOK;
121 | }
122 |
123 | int agile_led_set_compelete_callback(agile_led_t *led, void (*compelete)(agile_led_t *led))
124 | {
125 | RT_ASSERT(led);
126 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
127 | led->compelete = compelete;
128 | rt_mutex_release(&lock_mtx);
129 | return RT_EOK;
130 | }
131 |
132 | void agile_led_toggle(agile_led_t *led)
133 | {
134 | RT_ASSERT(led);
135 | drv_pin_write(led->pin, !drv_pin_read(led->pin));
136 | }
137 |
138 | void agile_led_on(agile_led_t *led)
139 | {
140 | RT_ASSERT(led);
141 | drv_pin_write(led->pin, led->active_logic);
142 | }
143 |
144 | void agile_led_off(agile_led_t *led)
145 | {
146 | RT_ASSERT(led);
147 | drv_pin_write(led->pin, !led->active_logic);
148 | }
149 |
150 | static void led_process(void *parameter)
151 | {
152 | rt_slist_t *node;
153 | while (1)
154 | {
155 | rt_mutex_take(&lock_mtx, RT_WAITING_FOREVER);
156 | rt_slist_for_each(node, &(agile_led_list))
157 | {
158 | agile_led_t *led = rt_slist_entry(node, agile_led_t, slist);
159 | if(led->loop_cnt == 0)
160 | {
161 | agile_led_stop(led);
162 | if(led->compelete)
163 | {
164 | led->compelete(led);
165 | }
166 | node = &agile_led_list;
167 | continue;
168 | }
169 | __repeat:
170 | if((rt_tick_get() - led->tick_timeout) < (RT_TICK_MAX / 2))
171 | {
172 | if(led->arr_index < led->arr_num)
173 | {
174 | if (led->light_arr[led->arr_index] == 0)
175 | {
176 | led->arr_index++;
177 | goto __repeat;
178 | }
179 | if(led->arr_index % 2)
180 | {
181 | agile_led_off(led);
182 | }
183 | else
184 | {
185 | agile_led_on(led);
186 | }
187 | led->tick_timeout = rt_tick_get() + rt_tick_from_millisecond(led->light_arr[led->arr_index]);
188 | led->arr_index++;
189 | }
190 | else
191 | {
192 | led->arr_index = 0;
193 | if(led->loop_cnt > 0)
194 | led->loop_cnt--;
195 | }
196 | }
197 | }
198 | rt_mutex_release(&lock_mtx);
199 | rt_thread_mdelay(5);
200 | }
201 | }
202 |
203 |
204 | static int agile_led_env_init(void)
205 | {
206 | rt_mutex_init(&lock_mtx, "led_mtx", RT_IPC_FLAG_FIFO);
207 |
208 | rt_thread_init(&agile_led_thread,
209 | "agile_led",
210 | led_process,
211 | RT_NULL,
212 | &agile_led_stack[0],
213 | sizeof(agile_led_stack),
214 | PKG_AGILE_LED_THREAD_PRIORITY,
215 | 100);
216 | rt_thread_startup(&agile_led_thread);
217 |
218 | is_initialized = 1;
219 | return RT_EOK;
220 | }
221 | INIT_APP_EXPORT(agile_led_env_init);
222 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Object files
5 | *.o
6 | *.ko
7 | *.obj
8 | *.elf
9 |
10 | # Linker output
11 | *.ilk
12 | *.map
13 | *.exp
14 |
15 | # Precompiled Headers
16 | *.gch
17 | *.pch
18 |
19 | # Libraries
20 | *.lib
21 | *.a
22 | *.la
23 | *.lo
24 |
25 | # Shared objects (inc. Windows DLLs)
26 | *.dll
27 | *.so
28 | *.so.*
29 | *.dylib
30 |
31 | # Executables
32 | *.exe
33 | *.out
34 | *.app
35 | *.i*86
36 | *.x86_64
37 | *.hex
38 |
39 | # Debug files
40 | *.dSYM/
41 | *.su
42 | *.idb
43 | *.pdb
44 | .vscode
45 |
46 | # Kernel Module Compile Results
47 | *.mod*
48 | *.cmd
49 | .tmp_versions/
50 | modules.order
51 | Module.symvers
52 | Mkfile.old
53 | dkms.conf
54 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/SConscript:
--------------------------------------------------------------------------------
1 | # RT-Thread building script for bridge
2 |
3 | import os
4 | from building import *
5 |
6 | cwd = GetCurrentDir()
7 | objs = []
8 | list = os.listdir(cwd)
9 |
10 | if GetDepend('PKG_USING_AGILE_MODBUS'):
11 | for d in list:
12 | path = os.path.join(cwd, d)
13 | if os.path.isfile(os.path.join(path, 'SConscript')):
14 | objs = objs + SConscript(os.path.join(d, 'SConscript'))
15 |
16 | Return('objs')
17 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/inc/agile_modbus.h:
--------------------------------------------------------------------------------
1 | #ifndef __PKG_AGILE_MODBUS_H
2 | #define __PKG_AGILE_MODBUS_H
3 | #include
4 |
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 | /* Modbus function codes */
10 | #define AGILE_MODBUS_FC_READ_COILS 0x01
11 | #define AGILE_MODBUS_FC_READ_DISCRETE_INPUTS 0x02
12 | #define AGILE_MODBUS_FC_READ_HOLDING_REGISTERS 0x03
13 | #define AGILE_MODBUS_FC_READ_INPUT_REGISTERS 0x04
14 | #define AGILE_MODBUS_FC_WRITE_SINGLE_COIL 0x05
15 | #define AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER 0x06
16 | #define AGILE_MODBUS_FC_READ_EXCEPTION_STATUS 0x07
17 | #define AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS 0x0F
18 | #define AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS 0x10
19 | #define AGILE_MODBUS_FC_REPORT_SLAVE_ID 0x11
20 | #define AGILE_MODBUS_FC_MASK_WRITE_REGISTER 0x16
21 | #define AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
22 |
23 | #define AGILE_MODBUS_BROADCAST_ADDRESS 0
24 |
25 | /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
26 | * Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
27 | * (chapter 6 section 11 page 29)
28 | * Quantity of Coils to write (2 bytes): 1 to 1968 (0x7B0)
29 | */
30 | #define AGILE_MODBUS_MAX_READ_BITS 2000
31 | #define AGILE_MODBUS_MAX_WRITE_BITS 1968
32 |
33 | /* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 3 page 15)
34 | * Quantity of Registers to read (2 bytes): 1 to 125 (0x7D)
35 | * (chapter 6 section 12 page 31)
36 | * Quantity of Registers to write (2 bytes) 1 to 123 (0x7B)
37 | * (chapter 6 section 17 page 38)
38 | * Quantity of Registers to write in R/W registers (2 bytes) 1 to 121 (0x79)
39 | */
40 | #define AGILE_MODBUS_MAX_READ_REGISTERS 125
41 | #define AGILE_MODBUS_MAX_WRITE_REGISTERS 123
42 | #define AGILE_MODBUS_MAX_WR_WRITE_REGISTERS 121
43 | #define AGILE_MODBUS_MAX_WR_READ_REGISTERS 125
44 |
45 | /* The size of the MODBUS PDU is limited by the size constraint inherited from
46 | * the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256
47 | * bytes). Therefore, MODBUS PDU for serial line communication = 256 - Server
48 | * address (1 byte) - CRC (2 bytes) = 253 bytes.
49 | */
50 | #define AGILE_MODBUS_MAX_PDU_LENGTH 253
51 |
52 | /* Consequently:
53 | * - RTU MODBUS ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256
54 | * bytes.
55 | * - TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
56 | * so the maximum of both backend in 260 bytes. This size can used to allocate
57 | * an array of bytes to store responses and it will be compatible with the two
58 | * backends.
59 | */
60 | #define AGILE_MODBUS_MAX_ADU_LENGTH 260
61 |
62 |
63 | typedef enum
64 | {
65 | AGILE_MODBUS_BACKEND_TYPE_RTU = 0,
66 | AGILE_MODBUS_BACKEND_TYPE_TCP
67 | } agile_modbus_backend_type_t;
68 |
69 | /*
70 | * ---------- Request Indication ----------
71 | * | Client | ---------------------->| Server |
72 | * ---------- Confirmation Response ----------
73 | */
74 | typedef enum
75 | {
76 | /* Request message on the server side */
77 | AGILE_MODBUS_MSG_INDICATION,
78 | /* Request message on the client side */
79 | AGILE_MODBUS_MSG_CONFIRMATION
80 | } agile_modbus_msg_type_t;
81 |
82 | typedef struct agile_modbus_sft
83 | {
84 | int slave;
85 | int function;
86 | int t_id;
87 | } agile_modbus_sft_t;
88 |
89 | typedef struct agile_modbus agile_modbus_t;
90 |
91 | typedef struct agile_modbus_backend
92 | {
93 | uint32_t backend_type;
94 | uint32_t header_length;
95 | uint32_t checksum_length;
96 | uint32_t max_adu_length;
97 | int (*set_slave) (agile_modbus_t *ctx, int slave);
98 | int (*build_request_basis) (agile_modbus_t *ctx, int function, int addr,
99 | int nb, uint8_t *req);
100 | int (*build_response_basis) (agile_modbus_sft_t *sft, uint8_t *rsp);
101 | int (*prepare_response_tid) (const uint8_t *req, int *req_length);
102 | int (*send_msg_pre) (uint8_t *req, int req_length);
103 | int (*check_integrity) (agile_modbus_t *ctx, uint8_t *msg, const int msg_length);
104 | int (*pre_check_confirmation) (agile_modbus_t *ctx, const uint8_t *req,
105 | const uint8_t *rsp, int rsp_length);
106 | } agile_modbus_backend_t;
107 |
108 | struct agile_modbus
109 | {
110 | int slave;
111 | uint8_t *send_buf;
112 | int send_bufsz;
113 | uint8_t *read_buf;
114 | int read_bufsz;
115 | const agile_modbus_backend_t *backend;
116 | void *backend_data;
117 | };
118 |
119 | void agile_modbus_common_init(agile_modbus_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
120 | int agile_modbus_set_slave(agile_modbus_t *ctx, int slave);
121 |
122 | int agile_modbus_serialize_read_bits(agile_modbus_t *ctx, int addr, int nb);
123 | int agile_modbus_deserialize_read_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
124 | int agile_modbus_serialize_read_input_bits(agile_modbus_t *ctx, int addr, int nb);
125 | int agile_modbus_deserialize_read_input_bits(agile_modbus_t *ctx, int msg_length, uint8_t *dest);
126 | int agile_modbus_serialize_read_registers(agile_modbus_t *ctx, int addr, int nb);
127 | int agile_modbus_deserialize_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
128 | int agile_modbus_serialize_read_input_registers(agile_modbus_t *ctx, int addr, int nb);
129 | int agile_modbus_deserialize_read_input_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
130 | int agile_modbus_serialize_write_bit(agile_modbus_t *ctx, int addr, int status);
131 | int agile_modbus_deserialize_write_bit(agile_modbus_t *ctx, int msg_length);
132 | int agile_modbus_serialize_write_register(agile_modbus_t *ctx, int addr, const uint16_t value);
133 | int agile_modbus_deserialize_write_register(agile_modbus_t *ctx, int msg_length);
134 | int agile_modbus_serialize_write_bits(agile_modbus_t *ctx, int addr, int nb, const uint8_t *src);
135 | int agile_modbus_deserialize_write_bits(agile_modbus_t *ctx, int msg_length);
136 | int agile_modbus_serialize_write_registers(agile_modbus_t *ctx, int addr, int nb, const uint16_t *src);
137 | int agile_modbus_deserialize_write_registers(agile_modbus_t *ctx, int msg_length);
138 | int agile_modbus_serialize_mask_write_register(agile_modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask);
139 | int agile_modbus_deserialize_mask_write_register(agile_modbus_t *ctx, int msg_length);
140 | int agile_modbus_serialize_write_and_read_registers(agile_modbus_t *ctx,
141 | int write_addr, int write_nb,
142 | const uint16_t *src,
143 | int read_addr, int read_nb);
144 | int agile_modbus_deserialize_write_and_read_registers(agile_modbus_t *ctx, int msg_length, uint16_t *dest);
145 | int agile_modbus_serialize_report_slave_id(agile_modbus_t *ctx);
146 | int agile_modbus_deserialize_report_slave_id(agile_modbus_t *ctx, int msg_length, int max_dest, uint8_t *dest);
147 | int agile_modbus_serialize_raw_request(agile_modbus_t *ctx, const uint8_t *raw_req, int raw_req_length);
148 | int agile_modbus_deserialize_raw_response(agile_modbus_t *ctx, int msg_length);
149 | int agile_modbus_receive_judge(agile_modbus_t *ctx, int msg_length);
150 |
151 | #include "agile_modbus_rtu.h"
152 | #include "agile_modbus_tcp.h"
153 |
154 | #ifdef __cplusplus
155 | }
156 | #endif
157 |
158 | #endif
159 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/inc/agile_modbus_rtu.h:
--------------------------------------------------------------------------------
1 | #ifndef __PKG_AGILE_MODBUS_RTU_H
2 | #define __PKG_AGILE_MODBUS_RTU_H
3 | #include
4 |
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 | #define AGILE_MODBUS_RTU_HEADER_LENGTH 1
10 | #define AGILE_MODBUS_RTU_PRESET_REQ_LENGTH 6
11 | #define AGILE_MODBUS_RTU_PRESET_RSP_LENGTH 2
12 |
13 | #define AGILE_MODBUS_RTU_CHECKSUM_LENGTH 2
14 |
15 | /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
16 | * RS232 / RS485 ADU = 253 bytes + slave (1 byte) + CRC (2 bytes) = 256 bytes
17 | */
18 | #define AGILE_MODBUS_RTU_MAX_ADU_LENGTH 256
19 |
20 |
21 | typedef struct agile_modbus_rtu
22 | {
23 | agile_modbus_t _ctx;
24 | } agile_modbus_rtu_t;
25 |
26 | int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
27 |
28 | #ifdef __cplusplus
29 | }
30 | #endif
31 |
32 | #endif
33 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/inc/agile_modbus_tcp.h:
--------------------------------------------------------------------------------
1 | #ifndef __PKG_AGILE_MODBUS_TCP_H
2 | #define __PKG_AGILE_MODBUS_TCP_H
3 | #include
4 |
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 | #define AGILE_MODBUS_TCP_HEADER_LENGTH 7
10 | #define AGILE_MODBUS_TCP_PRESET_REQ_LENGTH 12
11 | #define AGILE_MODBUS_TCP_PRESET_RSP_LENGTH 8
12 |
13 | #define AGILE_MODBUS_TCP_CHECKSUM_LENGTH 0
14 |
15 | /* Modbus_Application_Protocol_V1_1b.pdf Chapter 4 Section 1 Page 5
16 | * TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes
17 | */
18 | #define AGILE_MODBUS_TCP_MAX_ADU_LENGTH 260
19 |
20 | typedef struct agile_modbus_tcp
21 | {
22 | agile_modbus_t _ctx;
23 | /* Extract from MODBUS Messaging on TCP/IP Implementation Guide V1.0b
24 | (page 23/46):
25 | The transaction identifier is used to associate the future response
26 | with the request. This identifier is unique on each TCP connection. */
27 | uint16_t t_id;
28 | } agile_modbus_tcp_t;
29 |
30 | int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz);
31 |
32 | #ifdef __cplusplus
33 | }
34 | #endif
35 |
36 | #endif
37 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/src/SConscript:
--------------------------------------------------------------------------------
1 | from building import *
2 |
3 | cwd = GetCurrentDir()
4 | src = Glob('*.c') + Glob('*.cpp')
5 | CPPPATH = [cwd + '/../inc']
6 |
7 | group = DefineGroup('agile_modbus', src, depend = ['PKG_USING_AGILE_MODBUS'], CPPPATH = CPPPATH)
8 |
9 | Return('group')
10 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/src/agile_modbus_rtu.c:
--------------------------------------------------------------------------------
1 | #include "agile_modbus.h"
2 | #include "agile_modbus_rtu.h"
3 |
4 | /* Table of CRC values for high-order byte */
5 | static const uint8_t table_crc_hi[] =
6 | {
7 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
8 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
9 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
10 | 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
11 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
12 | 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
13 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
14 | 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
15 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
16 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
17 | 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
18 | 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
19 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
20 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
21 | 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
22 | 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
23 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
24 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
25 | 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
26 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
27 | 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
28 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
29 | 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
30 | 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
31 | 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
32 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
33 | };
34 |
35 | /* Table of CRC values for low-order byte */
36 | static const uint8_t table_crc_lo[] =
37 | {
38 | 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
39 | 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
40 | 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
41 | 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
42 | 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
43 | 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
44 | 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
45 | 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
46 | 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
47 | 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
48 | 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
49 | 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
50 | 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
51 | 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
52 | 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
53 | 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
54 | 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
55 | 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
56 | 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
57 | 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
58 | 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
59 | 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
60 | 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
61 | 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
62 | 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
63 | 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
64 | };
65 |
66 | static uint16_t agile_modbus_crc16(uint8_t *buffer, uint16_t buffer_length)
67 | {
68 | uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
69 | uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
70 | unsigned int i; /* will index into CRC lookup */
71 |
72 | /* pass through message buffer */
73 | while (buffer_length--)
74 | {
75 | i = crc_hi ^ *buffer++; /* calculate the CRC */
76 | crc_hi = crc_lo ^ table_crc_hi[i];
77 | crc_lo = table_crc_lo[i];
78 | }
79 |
80 | return (crc_hi << 8 | crc_lo);
81 | }
82 |
83 | /* Define the slave ID of the remote device to talk in master mode or set the
84 | * internal slave ID in slave mode */
85 | static int agile_modbus_rtu_set_slave(agile_modbus_t *ctx, int slave)
86 | {
87 | ctx->slave = slave;
88 | return 0;
89 | }
90 |
91 | /* Builds a RTU request header */
92 | static int agile_modbus_rtu_build_request_basis(agile_modbus_t *ctx, int function,
93 | int addr, int nb,
94 | uint8_t *req)
95 | {
96 | req[0] = ctx->slave;
97 | req[1] = function;
98 | req[2] = addr >> 8;
99 | req[3] = addr & 0x00ff;
100 | req[4] = nb >> 8;
101 | req[5] = nb & 0x00ff;
102 |
103 | return AGILE_MODBUS_RTU_PRESET_REQ_LENGTH;
104 | }
105 |
106 | /* Builds a RTU response header */
107 | static int agile_modbus_rtu_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
108 | {
109 | /* In this case, the slave is certainly valid because a check is already
110 | * done in _modbus_rtu_listen */
111 | rsp[0] = sft->slave;
112 | rsp[1] = sft->function;
113 |
114 | return AGILE_MODBUS_RTU_PRESET_RSP_LENGTH;
115 | }
116 |
117 | static int agile_modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
118 | {
119 | (*req_length) -= AGILE_MODBUS_RTU_CHECKSUM_LENGTH;
120 | /* No TID */
121 | return 0;
122 | }
123 |
124 | static int agile_modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
125 | {
126 | uint16_t crc = agile_modbus_crc16(req, req_length);
127 | req[req_length++] = crc >> 8;
128 | req[req_length++] = crc & 0x00FF;
129 |
130 | return req_length;
131 | }
132 |
133 | /* The check_crc16 function shall return 0 is the message is ignored and the
134 | message length if the CRC is valid. Otherwise it shall return -1 and set
135 | errno to EMBADCRC. */
136 | static int agile_modbus_rtu_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
137 | {
138 | uint16_t crc_calculated;
139 | uint16_t crc_received;
140 |
141 | crc_calculated = agile_modbus_crc16(msg, msg_length - 2);
142 | crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
143 |
144 | /* Check CRC of msg */
145 | if (crc_calculated == crc_received)
146 | return msg_length;
147 |
148 | return -1;
149 | }
150 |
151 | static int agile_modbus_rtu_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
152 | const uint8_t *rsp, int rsp_length)
153 | {
154 | /* Check responding slave is the slave we requested (except for broacast
155 | * request) */
156 | if (req[0] != rsp[0] && req[0] != AGILE_MODBUS_BROADCAST_ADDRESS)
157 | return -1;
158 |
159 | return 0;
160 | }
161 |
162 | static const agile_modbus_backend_t agile_modbus_rtu_backend =
163 | {
164 | AGILE_MODBUS_BACKEND_TYPE_RTU,
165 | AGILE_MODBUS_RTU_HEADER_LENGTH,
166 | AGILE_MODBUS_RTU_CHECKSUM_LENGTH,
167 | AGILE_MODBUS_RTU_MAX_ADU_LENGTH,
168 | agile_modbus_rtu_set_slave,
169 | agile_modbus_rtu_build_request_basis,
170 | agile_modbus_rtu_build_response_basis,
171 | agile_modbus_rtu_prepare_response_tid,
172 | agile_modbus_rtu_send_msg_pre,
173 | agile_modbus_rtu_check_integrity,
174 | agile_modbus_rtu_pre_check_confirmation
175 | };
176 |
177 | int agile_modbus_rtu_init(agile_modbus_rtu_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
178 | {
179 | agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
180 | ctx->_ctx.backend = &agile_modbus_rtu_backend;
181 | ctx->_ctx.backend_data = ctx;
182 |
183 | return 0;
184 | }
185 |
--------------------------------------------------------------------------------
/Project/packages/agile_modbus/src/agile_modbus_tcp.c:
--------------------------------------------------------------------------------
1 | #include "agile_modbus.h"
2 | #include "agile_modbus_tcp.h"
3 |
4 | static int agile_modbus_tcp_set_slave(agile_modbus_t *ctx, int slave)
5 | {
6 | ctx->slave = slave;
7 | return 0;
8 | }
9 |
10 | /* Builds a TCP request header */
11 | static int agile_modbus_tcp_build_request_basis(agile_modbus_t *ctx, int function,
12 | int addr, int nb,
13 | uint8_t *req)
14 | {
15 | agile_modbus_tcp_t *ctx_tcp = ctx->backend_data;
16 |
17 | /* Increase transaction ID */
18 | if (ctx_tcp->t_id < UINT16_MAX)
19 | ctx_tcp->t_id++;
20 | else
21 | ctx_tcp->t_id = 0;
22 | req[0] = ctx_tcp->t_id >> 8;
23 | req[1] = ctx_tcp->t_id & 0x00ff;
24 |
25 | /* Protocol Modbus */
26 | req[2] = 0;
27 | req[3] = 0;
28 |
29 | /* Length will be defined later by set_req_length_tcp at offsets 4
30 | and 5 */
31 |
32 | req[6] = ctx->slave;
33 | req[7] = function;
34 | req[8] = addr >> 8;
35 | req[9] = addr & 0x00ff;
36 | req[10] = nb >> 8;
37 | req[11] = nb & 0x00ff;
38 |
39 | return AGILE_MODBUS_TCP_PRESET_REQ_LENGTH;
40 | }
41 |
42 | /* Builds a TCP response header */
43 | static int agile_modbus_tcp_build_response_basis(agile_modbus_sft_t *sft, uint8_t *rsp)
44 | {
45 | /* Extract from MODBUS Messaging on TCP/IP Implementation
46 | Guide V1.0b (page 23/46):
47 | The transaction identifier is used to associate the future
48 | response with the request. */
49 | rsp[0] = sft->t_id >> 8;
50 | rsp[1] = sft->t_id & 0x00ff;
51 |
52 | /* Protocol Modbus */
53 | rsp[2] = 0;
54 | rsp[3] = 0;
55 |
56 | /* Length will be set later by send_msg (4 and 5) */
57 |
58 | /* The slave ID is copied from the indication */
59 | rsp[6] = sft->slave;
60 | rsp[7] = sft->function;
61 |
62 | return AGILE_MODBUS_TCP_PRESET_RSP_LENGTH;
63 | }
64 |
65 | static int agile_modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
66 | {
67 | return (req[0] << 8) + req[1];
68 | }
69 |
70 | static int agile_modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
71 | {
72 | /* Substract the header length to the message length */
73 | int mbap_length = req_length - 6;
74 |
75 | req[4] = mbap_length >> 8;
76 | req[5] = mbap_length & 0x00FF;
77 |
78 | return req_length;
79 | }
80 |
81 | static int agile_modbus_tcp_check_integrity(agile_modbus_t *ctx, uint8_t *msg, const int msg_length)
82 | {
83 | return msg_length;
84 | }
85 |
86 | static int agile_modbus_tcp_pre_check_confirmation(agile_modbus_t *ctx, const uint8_t *req,
87 | const uint8_t *rsp, int rsp_length)
88 | {
89 | /* Check transaction ID */
90 | if (req[0] != rsp[0] || req[1] != rsp[1])
91 | return -1;
92 |
93 | /* Check protocol ID */
94 | if (rsp[2] != 0x0 && rsp[3] != 0x0)
95 | return -1;
96 |
97 | return 0;
98 | }
99 |
100 | static const agile_modbus_backend_t agile_modbus_tcp_backend =
101 | {
102 | AGILE_MODBUS_BACKEND_TYPE_TCP,
103 | AGILE_MODBUS_TCP_HEADER_LENGTH,
104 | AGILE_MODBUS_TCP_CHECKSUM_LENGTH,
105 | AGILE_MODBUS_TCP_MAX_ADU_LENGTH,
106 | agile_modbus_tcp_set_slave,
107 | agile_modbus_tcp_build_request_basis,
108 | agile_modbus_tcp_build_response_basis,
109 | agile_modbus_tcp_prepare_response_tid,
110 | agile_modbus_tcp_send_msg_pre,
111 | agile_modbus_tcp_check_integrity,
112 | agile_modbus_tcp_pre_check_confirmation
113 | };
114 |
115 | int agile_modbus_tcp_init(agile_modbus_tcp_t *ctx, uint8_t *send_buf, int send_bufsz, uint8_t *read_buf, int read_bufsz)
116 | {
117 | agile_modbus_common_init(&(ctx->_ctx), send_buf, send_bufsz, read_buf, read_bufsz);
118 | ctx->_ctx.backend = &agile_modbus_tcp_backend;
119 | ctx->_ctx.backend_data = ctx;
120 |
121 | ctx->t_id = 0;
122 |
123 | return 0;
124 | }
125 |
--------------------------------------------------------------------------------
/Project/usr-drivers/gpio/drv_gpio.c:
--------------------------------------------------------------------------------
1 | #include "drv_gpio.h"
2 |
3 | #define RCC_GPIOA 0
4 | #define RCC_GPIOB 1
5 | #define RCC_GPIOC 2
6 | #define RCC_GPIOD 3
7 |
8 | struct pin_index
9 | {
10 | int index;
11 | uint8_t rcc_gpiox;
12 | GPIO_TypeDef *GPIOx;
13 | uint32_t pin;
14 | };
15 |
16 | static const struct pin_index pins[] =
17 | {
18 | {0, RCC_GPIOA, GPIOA, GPIO_PIN_8}, //D0
19 | {1, RCC_GPIOD, GPIOD, GPIO_PIN_2}, //D1
20 | {2, RCC_GPIOA, GPIOA, GPIO_PIN_0}, //WK_UP
21 | {5, RCC_GPIOB, GPIOB, GPIO_PIN_0}, //WIFI Power
22 | {6, RCC_GPIOB, GPIOB, GPIO_PIN_1}, //WIFI RST
23 | {7, RCC_GPIOC, GPIOC, GPIO_PIN_6}, //OLED RES
24 | {8, RCC_GPIOC, GPIOC, GPIO_PIN_7}, //OLED DC
25 | {9, RCC_GPIOC, GPIOC, GPIO_PIN_8} //OLED CS
26 | };
27 |
28 | #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
29 |
30 | static const struct pin_index *get_pin(uint8_t pin)
31 | {
32 | int num = ITEM_NUM(pins);
33 | for (int i = 0; i < num; i++)
34 | {
35 | if(pins[i].index == pin)
36 | return &pins[i];
37 | }
38 |
39 | return RT_NULL;
40 | };
41 |
42 | void drv_pin_mode(rt_base_t pin, rt_base_t mode)
43 | {
44 | const struct pin_index *index = get_pin(pin);
45 | if(index == RT_NULL)
46 | return;
47 |
48 | switch(index->rcc_gpiox)
49 | {
50 | case RCC_GPIOA:
51 | __HAL_RCC_GPIOA_CLK_ENABLE();
52 | break;
53 |
54 | case RCC_GPIOB:
55 | __HAL_RCC_GPIOB_CLK_ENABLE();
56 | break;
57 |
58 | case RCC_GPIOC:
59 | __HAL_RCC_GPIOC_CLK_ENABLE();
60 | break;
61 |
62 | case RCC_GPIOD:
63 | __HAL_RCC_GPIOD_CLK_ENABLE();
64 | break;
65 |
66 | default:
67 | return;
68 | }
69 |
70 | GPIO_InitTypeDef GPIO_InitStruct = {0};
71 | GPIO_InitStruct.Pin = index->pin;
72 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
73 | GPIO_InitStruct.Pull = GPIO_NOPULL;
74 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
75 |
76 | switch(mode)
77 | {
78 | case PIN_MODE_OUTPUT:
79 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
80 | GPIO_InitStruct.Pull = GPIO_NOPULL;
81 | break;
82 |
83 | case PIN_MODE_INPUT:
84 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
85 | GPIO_InitStruct.Pull = GPIO_NOPULL;
86 | break;
87 |
88 | case PIN_MODE_INPUT_PULLUP:
89 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
90 | GPIO_InitStruct.Pull = GPIO_PULLUP;
91 | break;
92 |
93 | case PIN_MODE_INPUT_PULLDOWN:
94 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
95 | GPIO_InitStruct.Pull = GPIO_PULLDOWN;
96 | break;
97 |
98 | case PIN_MODE_OUTPUT_OD:
99 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
100 | GPIO_InitStruct.Pull = GPIO_NOPULL;
101 | break;
102 |
103 | default:
104 | return;
105 | }
106 |
107 | HAL_GPIO_Init(index->GPIOx, &GPIO_InitStruct);
108 | }
109 |
110 | void drv_pin_write(rt_base_t pin, rt_base_t value)
111 | {
112 | const struct pin_index *index = get_pin(pin);
113 | if(index == RT_NULL)
114 | return;
115 |
116 | HAL_GPIO_WritePin(index->GPIOx, index->pin, (GPIO_PinState)(value ? 1 : 0));
117 | }
118 |
119 | int drv_pin_read(rt_base_t pin)
120 | {
121 | int value = PIN_LOW;
122 | const struct pin_index *index = get_pin(pin);
123 | if(index == RT_NULL)
124 | return value;
125 |
126 | value = HAL_GPIO_ReadPin(index->GPIOx, index->pin);
127 |
128 | return value;
129 | }
130 |
131 | static rt_size_t _pin_read(usr_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
132 | {
133 | if(pos < 0)
134 | return 0;
135 |
136 | int *status = (int *)buffer;
137 | if((status == RT_NULL) || (size != sizeof(int)))
138 | return 0;
139 |
140 | *status = drv_pin_read(pos);
141 | return size;
142 | }
143 |
144 | static rt_size_t _pin_write(usr_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
145 | {
146 | if(pos < 0)
147 | return 0;
148 |
149 | int *status = (int *)buffer;
150 | if((status == RT_NULL) || (size != sizeof(int)))
151 | return 0;
152 |
153 | drv_pin_write(pos, *status);
154 | return size;
155 | }
156 |
157 | static rt_err_t _pin_control(usr_device_t dev, int cmd, void *args)
158 | {
159 | struct usr_device_pin_mode *mode = (struct usr_device_pin_mode *)args;
160 | if(mode == RT_NULL)
161 | return -RT_ERROR;
162 |
163 | drv_pin_mode(mode->pin, mode->mode);
164 |
165 | return RT_EOK;
166 | }
167 |
168 | static struct usr_device_pin _hw_pin = {0};
169 |
170 | static int drv_hw_pin_init(void)
171 | {
172 | rt_memset(&_hw_pin, 0, sizeof(struct usr_device_pin));
173 |
174 | _hw_pin.parent.init = RT_NULL;
175 | _hw_pin.parent.read = _pin_read;
176 | _hw_pin.parent.write = _pin_write;
177 | _hw_pin.parent.control = _pin_control;
178 |
179 | usr_device_register(&(_hw_pin.parent), "pin");
180 |
181 | return RT_EOK;
182 | }
183 | INIT_BOARD_EXPORT(drv_hw_pin_init);
184 |
--------------------------------------------------------------------------------
/Project/usr-drivers/gpio/drv_gpio.h:
--------------------------------------------------------------------------------
1 | #ifndef __DRV_GPIO_H
2 | #define __DRV_GPIO_H
3 | #include
4 | #include "stm32f1xx_hal.h"
5 | #include "usr_device.h"
6 |
7 | #define PIN_LOW 0x00
8 | #define PIN_HIGH 0x01
9 |
10 | #define PIN_MODE_OUTPUT 0x00
11 | #define PIN_MODE_INPUT 0x01
12 | #define PIN_MODE_INPUT_PULLUP 0x02
13 | #define PIN_MODE_INPUT_PULLDOWN 0x03
14 | #define PIN_MODE_OUTPUT_OD 0x04
15 |
16 | struct usr_device_pin
17 | {
18 | struct usr_device parent;
19 | };
20 |
21 | struct usr_device_pin_mode
22 | {
23 | rt_base_t pin;
24 | rt_base_t mode;
25 | };
26 |
27 | void drv_pin_mode(rt_base_t pin, rt_base_t mode);
28 | void drv_pin_write(rt_base_t pin, rt_base_t value);
29 | int drv_pin_read(rt_base_t pin);
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/Project/usr-drivers/usart/config/drv_usart_config.c:
--------------------------------------------------------------------------------
1 | #include "stm32f1xx_hal.h"
2 |
3 | UART_HandleTypeDef huart1;
4 | DMA_HandleTypeDef hdma_usart1_tx;
5 | DMA_HandleTypeDef hdma_usart1_rx;
6 |
7 | UART_HandleTypeDef huart2;
8 | DMA_HandleTypeDef hdma_usart2_tx;
9 | DMA_HandleTypeDef hdma_usart2_rx;
10 |
11 | UART_HandleTypeDef huart3;
12 | DMA_HandleTypeDef hdma_usart3_tx;
13 | DMA_HandleTypeDef hdma_usart3_rx;
14 |
--------------------------------------------------------------------------------
/Project/usr-drivers/usart/config/drv_usart_config.h:
--------------------------------------------------------------------------------
1 | #ifndef __DRV_USART_CONFIG_H
2 | #define __DRV_USART_CONFIG_H
3 |
4 | /* usart1 config */
5 | extern UART_HandleTypeDef huart1;
6 | extern DMA_HandleTypeDef hdma_usart1_tx;
7 | extern DMA_HandleTypeDef hdma_usart1_rx;
8 | #define USART1_CONFIG \
9 | { \
10 | .name = "usart1", \
11 | .handle = &huart1, \
12 | .instance = USART1, \
13 | .dma_tx = &hdma_usart1_tx, \
14 | .dma_rx = &hdma_usart1_rx, \
15 | .rs485_control_pin = -1, \
16 | .rs485_send_logic = -1 \
17 | }
18 |
19 | /* usart2 config */
20 | extern UART_HandleTypeDef huart2;
21 | extern DMA_HandleTypeDef hdma_usart2_tx;
22 | extern DMA_HandleTypeDef hdma_usart2_rx;
23 | #define USART2_CONFIG \
24 | { \
25 | .name = "usart2", \
26 | .handle = &huart2, \
27 | .instance = USART2, \
28 | .dma_tx = &hdma_usart2_tx, \
29 | .dma_rx = &hdma_usart2_rx, \
30 | .rs485_control_pin = -1, \
31 | .rs485_send_logic = -1 \
32 | }
33 |
34 | /* usart3 config */
35 | extern UART_HandleTypeDef huart3;
36 | extern DMA_HandleTypeDef hdma_usart3_tx;
37 | extern DMA_HandleTypeDef hdma_usart3_rx;
38 | #define USART3_CONFIG \
39 | { \
40 | .name = "usart3", \
41 | .handle = &huart3, \
42 | .instance = USART3, \
43 | .dma_tx = &hdma_usart3_tx, \
44 | .dma_rx = &hdma_usart3_rx, \
45 | .rs485_control_pin = -1, \
46 | .rs485_send_logic = -1 \
47 | }
48 |
49 | #endif
50 |
--------------------------------------------------------------------------------
/Project/usr-drivers/usart/drv_usart.h:
--------------------------------------------------------------------------------
1 | #ifndef __DRV_USART_H
2 | #define __DRV_USART_H
3 | #include
4 | #include "ringbuffer.h"
5 | #include "stm32f1xx_hal.h"
6 | #include "usr_device.h"
7 |
8 | #define USR_DEVICE_USART_TX_ACTIVATED_TIMEOUT 15
9 |
10 | #define USR_DEVICE_USART_PARAMETER_DEFAULT \
11 | { \
12 | 9600, \
13 | UART_PARITY_NONE, \
14 | UART_WORDLENGTH_8B, \
15 | UART_STOPBITS_1 \
16 | }
17 |
18 | #define USR_DEVICE_USART_CMD_SET_PARAMETER 0x01
19 | #define USR_DEVICE_USART_CMD_SET_BUFFER 0x02
20 | #define USR_DEVICE_USART_CMD_FLUSH 0X03
21 |
22 | #define USR_DEVICE_USART_ERROR_TX_TIMEOUT 0x01
23 | #define USR_DEVICE_USART_ERROR_TX_RB_SAVE 0x02
24 | #define USR_DEVICE_USART_ERROR_RX_RB_FULL 0x04
25 | #define USR_DEVICE_USART_ERROR_OTHER 0x08
26 |
27 | struct usr_device_usart_config
28 | {
29 | const char *name;
30 | UART_HandleTypeDef *handle;
31 | USART_TypeDef *instance;
32 | DMA_HandleTypeDef *dma_tx;
33 | DMA_HandleTypeDef *dma_rx;
34 | int rs485_control_pin;
35 | int rs485_send_logic;
36 | };
37 |
38 | struct usr_device_usart_parameter
39 | {
40 | rt_uint32_t baudrate;
41 | rt_uint32_t parity;
42 | rt_uint32_t wlen;
43 | rt_uint32_t stblen;
44 | };
45 |
46 | struct usr_device_usart_buffer
47 | {
48 | rt_uint8_t *send_buf;
49 | int send_bufsz;
50 | rt_uint8_t *read_buf;
51 | int read_bufsz;
52 | };
53 |
54 | struct usr_device_usart
55 | {
56 | struct usr_device parent;
57 | rt_uint8_t init_ok;
58 | struct usr_device_usart_buffer buffer;
59 | struct rt_ringbuffer tx_rb;
60 | rt_uint16_t need_send;
61 | struct rt_ringbuffer rx_rb;
62 | rt_uint16_t rx_index;
63 | rt_uint8_t tx_activated;
64 | rt_tick_t tx_activated_timeout;
65 | struct usr_device_usart_parameter parameter;
66 | const struct usr_device_usart_config *config;
67 | rt_slist_t slist;
68 | };
69 |
70 | #endif
71 |
--------------------------------------------------------------------------------
/Project/usr-drivers/wdt/drv_wdt.c:
--------------------------------------------------------------------------------
1 | #include "stm32f1xx_hal.h"
2 | #include
3 |
4 | static IWDG_HandleTypeDef hiwdg;
5 |
6 | static void idle_hook_cb(void)
7 | {
8 | #define FEED_WDT_CYCLE 1
9 |
10 | static rt_tick_t feed_timeout = 0;
11 |
12 | if((rt_tick_get() - feed_timeout) < (RT_TICK_MAX / 2))
13 | {
14 | HAL_IWDG_Refresh(&hiwdg);
15 | feed_timeout = rt_tick_get() + rt_tick_from_millisecond(FEED_WDT_CYCLE * 1000);
16 | }
17 | }
18 |
19 | static int drv_wdt_init(void)
20 | {
21 | hiwdg.Instance = IWDG;
22 | hiwdg.Init.Prescaler = IWDG_PRESCALER_128;
23 | hiwdg.Init.Reload = 4000; // 4000 * 3.2ms
24 |
25 | if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
26 | {
27 | rt_thread_mdelay(100);
28 | HAL_NVIC_SystemReset();
29 | }
30 |
31 | rt_thread_idle_sethook(idle_hook_cb);
32 |
33 | return RT_EOK;
34 | }
35 | INIT_PREV_EXPORT(drv_wdt_init);
36 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 使用说明
2 |
3 | ## 注意事项
4 |
5 | 1. 需要安装 RT-Thread Nano 3.1.3 keil 包
6 | 2. 安装 STM32F1 keil pack
7 |
--------------------------------------------------------------------------------
/docs/DMA分配.md:
--------------------------------------------------------------------------------
1 | # DMA分配
2 |
3 | |外设|发送DMA|接收DMA|
4 | |---|---|---|
5 | |USART1|DMA1 CH4|DMA1 CH5|
6 | |USART2|DMA1 CH7|DMA1 CH6|
7 | |USART3|DMA1 CH2|DMA1 CH3|
8 | |SPI3|DMA2 CH2|---|
9 |
--------------------------------------------------------------------------------
/docs/手册/0.96寸OLED规格书.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/0.96寸OLED规格书.pdf
--------------------------------------------------------------------------------
/docs/手册/DS5792.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/DS5792.pdf
--------------------------------------------------------------------------------
/docs/手册/ESP-01S规格书v1.0.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/ESP-01S规格书v1.0.pdf
--------------------------------------------------------------------------------
/docs/手册/ESP-01系列模组用户手册v1.5.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/ESP-01系列模组用户手册v1.5.pdf
--------------------------------------------------------------------------------
/docs/手册/ESP8266-AT指令使用示例v1.3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/ESP8266-AT指令使用示例v1.3.pdf
--------------------------------------------------------------------------------
/docs/手册/ESP8266-AT指令集v3.0.1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/ESP8266-AT指令集v3.0.1.pdf
--------------------------------------------------------------------------------
/docs/手册/ESP8266系列入门教程v2.0.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/ESP8266系列入门教程v2.0.pdf
--------------------------------------------------------------------------------
/docs/手册/RM0008.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/RM0008.pdf
--------------------------------------------------------------------------------
/docs/手册/中景园电子0.96OLED显示屏_驱动芯片手册.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/中景园电子0.96OLED显示屏_驱动芯片手册.pdf
--------------------------------------------------------------------------------
/docs/手册/中景园电子0.96寸OLED使用文档新手必看V2.0.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/docs/手册/中景园电子0.96寸OLED使用文档新手必看V2.0.pdf
--------------------------------------------------------------------------------
/原理图/(新版)MINI-STM32--原理图.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loogg/agile_modbus_demo/f2f651669f5caa41f68c7c1427c5293c2dacc959/原理图/(新版)MINI-STM32--原理图.pdf
--------------------------------------------------------------------------------