樊春春 3 ヶ月 前
コミット
ad866f6154
100 ファイル変更1735 行追加1599 行削除
  1. 50 49
      .clang-format
  2. 1 1
      .gitignore
  3. 54 26
      CMakeLists.txt
  4. 2 2
      CMakePresets.json
  5. 0 7
      app/app_conf.h
  6. 0 51
      ble/ble_core.h
  7. 0 0
      cmake/gcc-arm-none-eabi.cmake
  8. 40 0
      common/inc/hal_math.h
  9. 1 1
      common/inc/queue.h
  10. 40 0
      common/inc/utils.h
  11. 26 0
      common/src/hal_math.c
  12. 0 49
      dev/dev_can_id.h
  13. 0 11
      dev/dev_conf.h
  14. 0 83
      dev/dev_key.c
  15. 0 30
      dev/dev_key.h
  16. 0 42
      dev/dev_led.c
  17. 0 18
      dev/dev_systick.c
  18. 0 9
      dev/dev_systick.h
  19. 0 50
      dev/dev_uart_table.h
  20. 5 0
      docs/readme.md
  21. 19 13
      drivers/can/can.c
  22. 16 0
      drivers/can/can.h
  23. 1 2
      drivers/flash/flash.c
  24. 32 25
      drivers/flash/flash.h
  25. 16 16
      drivers/gpio/gpio.c
  26. 63 0
      drivers/gpio/gpio.h
  27. 16 16
      drivers/i2c/i2c1.c
  28. 20 0
      drivers/i2c/i2c1.h
  29. 1 2
      drivers/pwm/pwm.c
  30. 62 0
      drivers/pwm/pwm.h
  31. 19 3
      drivers/systick/systick.c
  32. 57 0
      drivers/systick/systick.h
  33. 41 42
      drivers/uart/uart.c
  34. 2 2
      drivers/uart/uart.h
  35. 45 0
      drivers/uart/uart4.c
  36. 47 0
      drivers/uart/uart4.h
  37. 0 16
      hal/hal_can.h
  38. 0 11
      hal/hal_conf.h
  39. 0 27
      hal/hal_gpio.h
  40. 0 20
      hal/hal_i2c1.h
  41. 0 62
      hal/hal_pwm.h
  42. 0 14
      hal/hal_systick.h
  43. 0 136
      hal/hal_uart4.c
  44. 0 64
      hal/hal_uart4.h
  45. 0 179
      hal/platform_queue.c
  46. 0 81
      hal/platform_queue.h
  47. 0 104
      hal/queue.c111
  48. 0 118
      hal/queue.h111
  49. 2 2
      modules/ble/ble_core.c
  50. 50 0
      modules/ble/ble_core.h
  51. 25 36
      modules/can/dev_can.c
  52. 1 2
      modules/can/dev_can.h
  53. 165 0
      modules/can/dev_can_cali.c
  54. 9 19
      modules/can/dev_can_cali.h
  55. 23 1
      modules/can/dev_can_id.c
  56. 52 0
      modules/can/dev_can_id.h
  57. 3 3
      modules/cli/cli.c
  58. 0 0
      modules/cli/cli.h
  59. 0 0
      modules/cli/cli_inter_actor.c
  60. 0 0
      modules/cli/cli_inter_actor.h
  61. 0 0
      modules/cli/cli_inter_preter.c
  62. 9 12
      modules/cli/cli_inter_preter.h
  63. 0 1
      modules/eeprom/app_eeprom.c
  64. 3 1
      modules/eeprom/app_eeprom.h
  65. 5 5
      modules/eeprom/dev_at24cxx.c
  66. 1 1
      modules/eeprom/dev_at24cxx.h
  67. 0 0
      modules/esp8266/esp8266_cmd.c
  68. 13 0
      modules/esp8266/esp8266_cmd.h
  69. 59 0
      modules/esp8266/esp8266_core.c
  70. 50 0
      modules/esp8266/esp8266_core.h
  71. 85 0
      modules/gpio/key.c
  72. 70 0
      modules/gpio/key.h
  73. 42 0
      modules/gpio/led.c
  74. 9 9
      modules/gpio/led.h
  75. 4 5
      modules/iap/iap.c
  76. 45 47
      modules/iap/iap.h
  77. 14 30
      modules/task/app_task.c
  78. 1 1
      modules/task/app_task.h
  79. 0 0
      modules/task/dev_task.c
  80. 0 0
      modules/task/dev_task.h
  81. 89 0
      modules/uart/uart_interface.c
  82. 68 0
      modules/uart/uart_interface.h
  83. 2 3
      modules/uart/uart_table.c
  84. 55 0
      modules/uart/uart_table.h
  85. 7 7
      modules/uart/uart_task.c
  86. 53 0
      modules/uart/uart_task.h
  87. 0 0
      project/STM32F40x.svd
  88. 0 0
      project/STM32F417IG_FLASH.ld
  89. 13 14
      project/main.c
  90. 0 0
      project/startup/gcc/startup_stm32f40_41xxx.s
  91. 0 1
      project/stm32f4xx_conf.h
  92. 17 1
      project/stm32f4xx_it.c
  93. 15 16
      project/stm32f4xx_it.h
  94. 0 0
      project/system_stm32f4xx.c
  95. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h
  96. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h
  97. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Release_Notes.html
  98. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TASKING/cstart_thumb2.asm
  99. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f401xx.s
  100. 0 0
      vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f40_41xxx.s

+ 50 - 49
.clang-format

@@ -1,6 +1,6 @@
 ---
 # 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
-Language:        Cpp
+Language: Cpp
 # BasedOnStyle:  LLVM
 # 访问说明符(public、private等)的偏移
 AccessModifierOffset: -4
@@ -22,7 +22,7 @@ AlignEscapedNewlines: Right
 AllowAllArgumentsOnNextLine: true
 AllowAllConstructorInitializersOnNextLine: true
 AlwaysBreakAfterDefinitionReturnType: None
-AlignOperands:   Align
+AlignOperands: Align
 # 对齐连续的尾随的注释
 AlignTrailingComments: true
 # 允许函数声明的所有参数在放在下一行
@@ -55,21 +55,22 @@ BinPackArguments: true
 BinPackParameters: true
 # 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效
 BraceWrapping:
-  AfterCaseLabel:  false
-  AfterClass:      false
-  AfterControlStatement: Never
-  AfterEnum:       false
-  AfterFunction:   false
-  AfterNamespace:  false
-  AfterObjCDeclaration: false
-  AfterStruct:     false
-  AfterUnion:      false
+  AfterCaseLabel: true
+  # class 定义后面
+  AfterClass: true
+  # 控制语句后面
+  AfterControlStatement: true
+  AfterEnum: true
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: true
+  AfterStruct: true
+  AfterUnion: true
   AfterExternBlock: false
-  BeforeCatch:     false
-  BeforeElse:      false
-  BeforeLambdaBody: false
-  BeforeWhile:     false
-  IndentBraces:    false
+  BeforeCatch: true
+  BeforeElse: true
+  # 缩进大括号
+  IndentBraces: false
   SplitEmptyFunction: true
   SplitEmptyRecord: true
   SplitEmptyNamespace: true
@@ -78,7 +79,7 @@ BreakBeforeBinaryOperators: None
 # 在concept前换行
 BreakBeforeConceptDeclarations: true
 # 在大括号前换行: Attach(始终将大括号附加到周围的上下文)
-BreakBeforeBraces: Allman
+BreakBeforeBraces: Custom #Allman
 BreakBeforeInheritanceComma: false
 # 继承列表样式
 BreakInheritanceList: AfterComma
@@ -89,9 +90,9 @@ BreakConstructorInitializers: BeforeComma
 # 在java字段的注释后换行
 BreakAfterJavaFieldAnnotations: false
 # 每行字符的限制,0表示没有限制
-ColumnLimit:     0
+ColumnLimit: 0
 # 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变
-CommentPragmas:  '^ IWYU pragma:'
+CommentPragmas: "^ IWYU pragma:"
 # 在新行上声明每个命名空间
 CompactNamespaces: false
 # 构造函数的初始化列表的缩进宽度
@@ -105,7 +106,7 @@ DeriveLineEnding: true
 # 继承最常用的指针和引用的对齐方式
 DerivePointerAlignment: false
 # 关闭格式化
-DisableFormat:   false
+DisableFormat: false
 # 删除访问修饰符后的所有空行
 EmptyLineAfterAccessModifier: Never
 # 仅当访问修饰符开始一个新的逻辑块时才添加空行
@@ -122,23 +123,23 @@ ForEachMacros:
 IfMacros:
   - KJ_IF_MAYBE
 # 多个#include块合并在一起并排序为一个
-IncludeBlocks:   Merge
+IncludeBlocks: Merge
 # 可以定义负数优先级从而保证某些#include永远在最前面
 IncludeCategories:
-  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
-    Priority:        2
-    SortPriority:    0
-    CaseSensitive:   false
-  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
-    Priority:        3
-    SortPriority:    0
-    CaseSensitive:   false
-  - Regex:           '.*'
-    Priority:        1
-    SortPriority:    0
-    CaseSensitive:   false
-IncludeIsMainRegex: '(Test)?$'
-IncludeIsMainSourceRegex: ''
+  - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority: 2
+    SortPriority: 0
+    CaseSensitive: false
+  - Regex: '^(<|"(gtest|gmock|isl|json)/)'
+    Priority: 3
+    SortPriority: 0
+    CaseSensitive: false
+  - Regex: ".*"
+    Priority: 1
+    SortPriority: 0
+    CaseSensitive: false
+IncludeIsMainRegex: "(Test)?$"
+IncludeIsMainSourceRegex: ""
 # 缩进访问修饰符
 IndentAccessModifiers: false
 # 缩进case标签
@@ -152,9 +153,9 @@ IndentPPDirectives: None
 # 向后兼容缩进外部块
 IndentExternBlock: AfterExternBlock
 # 缩进模板中的requires子句
-IndentRequires:  false
+IndentRequires: false
 # 缩进宽度
-IndentWidth:     4
+IndentWidth: 4
 # 函数返回类型换行时,缩进函数声明或函数定义的函数名
 IndentWrappedFunctionNames: false
 # 插入尾随逗号
@@ -168,9 +169,9 @@ KeepEmptyLinesAtTheStartOfBlocks: true
 # 相对于 lambda 签名对齐 lambda 主体
 LambdaBodyIndentation: Signature
 # 开始一个块的宏的正则表达式
-MacroBlockBegin: ''
+MacroBlockBegin: ""
 # 结束一个块的宏的正则表达式
-MacroBlockEnd:   ''
+MacroBlockEnd: ""
 # 连续空行的最大数量
 MaxEmptyLinesToKeep: 1
 # 命名空间的缩进
@@ -195,15 +196,15 @@ PenaltyIndentedWhitespace: 0
 # 指针的对齐: Left, Right, Middle
 PointerAlignment: Right
 # 缩进预处理器语句的列数
-PPIndentWidth:   -1
+PPIndentWidth: -1
 # 引用的对齐
 ReferenceAlignment: Pointer
 # 允许重新排版注释
-ReflowComments:  true
+ReflowComments: true
 # 短命名空间跨越的最大展开行数
 ShortNamespaceLines: 1
 # 允许排序#include
-SortIncludes:    true
+SortIncludes: true
 # java静态导入放在非静态导入之前
 SortJavaStaticImport: Before
 # 对using声明排序
@@ -237,7 +238,7 @@ SpaceInEmptyParentheses: false
 # 在尾随的评论前添加的空格数(只适用于//)
 SpacesBeforeTrailingComments: 1
 # 在尖括号的<后和>前添加空格
-SpacesInAngles:  Never
+SpacesInAngles: Never
 # 不在if/for/switch/while条件周围插入空格
 SpacesInConditionalStatement: false
 # 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格
@@ -246,8 +247,8 @@ SpacesInContainerLiterals: true
 SpacesInCStyleCastParentheses: false
 # 行注释开头允许有多少个空格。要禁用最大值,请将其设置为-1,除此之外,最大值优先于最小值
 SpacesInLineCommentPrefix:
-  Minimum:         1
-  Maximum:         -1
+  Minimum: 1
+  Maximum: -1
 # 在圆括号的(后和)前添加空格
 SpacesInParentheses: false
 # 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响
@@ -257,7 +258,7 @@ SpaceBeforeSquareBrackets: false
 # 位域:每边都添加空格
 BitFieldColonSpacing: Both
 # 标准
-Standard:        Latest
+Standard: Latest
 # 在语句前面被忽略的宏定义,就好像它们是一个属性一样
 StatementAttributeLikeMacros:
   - Q_EMIT
@@ -266,11 +267,11 @@ StatementMacros:
   - Q_UNUSED
   - QT_REQUIRE_VERSION
 # tab宽度
-TabWidth:        4
+TabWidth: 4
 # 使用\n换行
-UseCRLF:         false
+UseCRLF: false
 # 使用tab字符:ForIndentation——仅将制表符用于缩进
-UseTab:          Never
+UseTab: Never
 # 对空格敏感的宏定义
 WhitespaceSensitiveMacros:
   - STRINGIZE

+ 1 - 1
.gitignore

@@ -11,6 +11,6 @@
 .idea/
 
 .cache
-project/gcc/build
+build
 compile_commands.json
 **/Thumbs.db

+ 54 - 26
CMakeLists.txt

@@ -27,41 +27,69 @@ set(MCU_PARAMS
 
 # C 源文件
 file(GLOB_RECURSE C_SRCS
-        ${CMAKE_CURRENT_SOURCE_DIR}/user/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/cmsis/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/startup/gcc/startup_stm32f40_41xxx.s
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/packages/multi_button/multi_button.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/packages/j1939/source/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/app/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/ble/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/cli/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/applications/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/common/src/*.c
         ${CMAKE_CURRENT_SOURCE_DIR}/dev/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/hal/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/can/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/task/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/project/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/project/startup/gcc/startup_stm32f40_41xxx.s
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button/multi_button.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source/*.c
         )
 file(GLOB_RECURSE R_SRCS
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_cec.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spdifrx.c
         )
 
 list(REMOVE_ITEM C_SRCS ${R_SRCS})
 
 # C .h文件
 set(INC_C_DIRS
-        ${CMAKE_CURRENT_SOURCE_DIR}/user
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Include
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/CMSIS/Device/ST/STM32F4xx/Include
-        ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/inc
-        ${CMAKE_CURRENT_SOURCE_DIR}/packages/multi_button
-        ${CMAKE_CURRENT_SOURCE_DIR}/packages/j1939/source
-        ${CMAKE_CURRENT_SOURCE_DIR}/app
-        ${CMAKE_CURRENT_SOURCE_DIR}/ble
-        ${CMAKE_CURRENT_SOURCE_DIR}/cli
+        ${CMAKE_CURRENT_SOURCE_DIR}/applications
+        ${CMAKE_CURRENT_SOURCE_DIR}/applications/task
+        ${CMAKE_CURRENT_SOURCE_DIR}/common/inc
         ${CMAKE_CURRENT_SOURCE_DIR}/dev
-        ${CMAKE_CURRENT_SOURCE_DIR}/hal
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/can
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/flash
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/gpio
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/i2c
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/pwm
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/systick
+        ${CMAKE_CURRENT_SOURCE_DIR}/drivers/uart
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/ble
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/can
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/cli
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/eeprom
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/esp8266
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/gpio
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/iap
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/task
+        ${CMAKE_CURRENT_SOURCE_DIR}/modules/uart
+        ${CMAKE_CURRENT_SOURCE_DIR}/project
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Include
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/CMSIS/Device/ST/STM32F4xx/Include
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/ST/STM32F4xx_StdPeriph_Driver/inc
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/multi_button
+        ${CMAKE_CURRENT_SOURCE_DIR}/vendors/j1939/source
         )
 
 set(INC_CXX_DIRS)
@@ -82,7 +110,7 @@ set(LINK_LIBS)
 
 # 链接脚本
 set(LINK_SRC
-        ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/STM32F417IG_FLASH.ld
+        ${CMAKE_CURRENT_SOURCE_DIR}/project/STM32F417IG_FLASH.ld
         )
 
 # 编译器参数

+ 2 - 2
CMakePresets.json

@@ -5,8 +5,8 @@
             "name": "default",
             "hidden": true,
             "generator": "Ninja",
-            "binaryDir": "${sourceDir}/project/gcc/build/${presetName}",
-            "toolchainFile": "${sourceDir}/project/gcc/cmake/gcc-arm-none-eabi.cmake",
+            "binaryDir": "${sourceDir}/build/${presetName}",
+            "toolchainFile": "${sourceDir}/cmake/gcc-arm-none-eabi.cmake",
             "cacheVariables": {
                 "CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
             }

+ 0 - 7
app/app_conf.h

@@ -1,7 +0,0 @@
-#ifndef __APP_CONF_H
-#define __APP_CONF_H
-
-#include "app_eeprom.h"
-#include "app_task.h"
-
-#endif // __APP_CONF_H

+ 0 - 51
ble/ble_core.h

@@ -1,51 +0,0 @@
-#ifndef __BLE_CORE_H
-#define __BLE_CORE_H
-
-#ifdef __cplusplus
-extern 'C'
-{
-#endif
-#ifdef __BLE_CORE_H_GLOBAL
-#define __BLE_CORE_H_EXTERN
-#else
-#define __BLE_CORE_H_EXTERN extern
-#endif
-
-    /************************************************************************************************
-     *                                          Version                                              *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          How to use                                           *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Enable config                                         *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Includes                                             *
-     ************************************************************************************************/
-
-#include "hal_uart.h"
-
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
-#define BT_BUSY_IND_PORT GPIOB
-#define BT_BUSY_IND_PIN  GPIO_Pin_12
-#define BT_CONN_IND_PORT GPIOB
-#define BT_CONN_IND_PIN  GPIO_Pin_13
-#define BT_RST_PORT      GPIOC
-#define BT_RST_PIN       GPIO_Pin_2
-
-    void ble_init(void);
-    void ble_send(void);
-/************************************************************************************************
- *                                          Defines                                              *
- ************************************************************************************************/
-#ifdef __cplusplus
-};
-#endif
-
-#endif // __BLE_CORE_H

+ 0 - 0
project/gcc/cmake/gcc-arm-none-eabi.cmake → cmake/gcc-arm-none-eabi.cmake


+ 40 - 0
common/inc/hal_math.h

@@ -0,0 +1,40 @@
+#ifndef __HAL_MATH_H
+#define __HAL_MATH_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __HAL_MATH_H_GLOBAL
+#define __HAL_MATH_H_EXTERN
+#else
+#define __HAL_MATH_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines *
+ ************************************************************************************************/
+double hal_pow(double x, double N);
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __HAL_MATH_H

+ 1 - 1
hal/queue.h → common/inc/queue.h

@@ -1,6 +1,6 @@
 #ifndef __QUEUE_H
 #define __QUEUE_H
-#include "stdio.h"
+#include "stdint.h"
 
 typedef struct
 {

+ 40 - 0
common/inc/utils.h

@@ -0,0 +1,40 @@
+#ifndef __UTILS_H
+#define __UTILS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __UTILS_H_GLOBAL
+#define __UTILS_H_EXTERN
+#else
+#define __UTILS_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                           *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                         *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#define ARR_SIZE(a) sizeof(a) / sizeof((a)[0])
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __UTILS_H

+ 26 - 0
common/src/hal_math.c

@@ -0,0 +1,26 @@
+#include "hal_math.h"
+
+double pow_branch(double x, long long N)
+{
+    double ans = 1.0;
+    // 贡献的初始值为 x
+    double x_contribute = x;
+    // 在对 N 进行二进制拆分的同时计算答案
+    while (N > 0)
+    {
+        if (N % 2 == 1)
+        {
+            // 如果 N 二进制表示的最低位为 1,那么需要计入贡献
+            ans *= x_contribute;
+        }
+        // 将贡献不断地平方
+        x_contribute *= x_contribute;
+        // 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
+        N = N >> 1;
+    }
+    return ans;
+}
+double hal_pow(double x, double N)
+{
+    return N >= 0 ? pow_branch(x, N) : 1.0 / pow_branch(x, -N);
+}

+ 0 - 49
dev/dev_can_id.h

@@ -1,49 +0,0 @@
-#ifndef __DEV_CAN_ID_H
-#define __DEV_CAN_ID_H
-
-#include <stdint.h>
-#ifdef __cplusplus
-extern 'C'
-{
-#endif
-#ifdef __DEV_CAN_ID_H_GLOBAL
-#define __DEV_CAN_ID_H_EXTERN
-#else
-#define __DEV_CAN_ID_H_EXTERN extern
-#endif
-
-    /************************************************************************************************
-     *                                          Version                                              *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          How to use                                           *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Enable config                                         *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Includes                                             *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
-    extern uint32_t g_ps;
-    extern uint32_t g_can_id;
-    extern uint32_t g_can_ota_id;
-    extern uint8_t  g_can_iap;
-
-    uint32_t get_new_can_id(uint8_t pf);
-    uint32_t set_new_can_id(uint16_t pgn);
-    uint32_t get_ota_id(uint8_t pf);
-/************************************************************************************************
- *                                          Defines                                              *
- ************************************************************************************************/
-#ifdef __cplusplus
-};
-#endif
-
-#endif // __DEV_CAN_ID_H

+ 0 - 11
dev/dev_conf.h

@@ -1,11 +0,0 @@
-#ifndef __DEV_CONF_H
-#define __DEV_CONF_H
-
-#include "dev_at24cxx.h"
-#include "dev_can.h"
-#include "dev_iap.h"
-#include "dev_key.h"
-#include "dev_led.h"
-#include "dev_systick.h"
-#include "dev_task.h"
-#endif // __DEV_CONF_H

+ 0 - 83
dev/dev_key.c

@@ -1,83 +0,0 @@
-#include "dev_key.h"
-
-struct Button *btn_press;
-
-struct Button btn_key_up;
-struct Button btn_key0;
-struct Button btn_key1;
-struct Button btn_key2;
-
-dev_key_t dev_key;
-
-#define KEY_UP_ACTIVE_LEVEL 1
-#define KEY0_ACTIVE_LEVEL   0
-#define KEY1_ACTIVE_LEVEL   0
-#define KEY2_ACTIVE_LEVEL   0
-
-uint8_t dev_key_read_button(uint8_t button_id)
-{
-    // you can share the GPIO read function with multiple Buttons
-    switch (button_id)
-    {
-    case 0:
-        return GPIO_ReadInputDataBit(HAL_GPIO_KEY_UP_PORT, HAL_GPIO_KEY_UP_PIN);
-    case 1:
-        return GPIO_ReadInputDataBit(HAL_GPIO_KEY0_PORT, HAL_GPIO_KEY0_PIN);
-    case 2:
-        return GPIO_ReadInputDataBit(HAL_GPIO_KEY1_PORT, HAL_GPIO_KEY1_PIN);
-    case 3:
-        return GPIO_ReadInputDataBit(HAL_GPIO_KEY2_PORT, HAL_GPIO_KEY2_PIN);
-    default:
-        return 0;
-    }
-}
-
-void dev_key_press_down(void *btn)
-{
-    if (NULL == btn)
-    {
-        dev_key.i = 0;
-    }
-    else
-    {
-        btn_press = (struct Button *)btn;
-        if (btn_press == &btn_key_up)
-        {
-            dev_key.dev_key_state.bits.key_up_state_flag = 1;
-        }
-        if (btn_press == &btn_key0)
-        {
-            dev_key.dev_key_state.bits.key0_state_flag = 1;
-        }
-        if (btn_press == &btn_key1)
-        {
-            dev_key.dev_key_state.bits.key1_state_flag = 1;
-        }
-        if (btn_press == &btn_key2)
-        {
-            dev_key.dev_key_state.bits.key2_state_flag = 1;
-        }
-    }
-}
-
-void dev_key_button_init(void)
-{
-    button_init(&btn_key_up, dev_key_read_button, KEY_UP_ACTIVE_LEVEL, 0);
-    button_init(&btn_key0, dev_key_read_button, KEY0_ACTIVE_LEVEL, 1);
-    button_init(&btn_key1, dev_key_read_button, KEY1_ACTIVE_LEVEL, 2);
-    button_init(&btn_key2, dev_key_read_button, KEY2_ACTIVE_LEVEL, 3);
-
-    button_attach(&btn_key_up, PRESS_DOWN, dev_key_press_down);
-    button_attach(&btn_key0, PRESS_DOWN, dev_key_press_down);
-    button_attach(&btn_key1, PRESS_DOWN, dev_key_press_down);
-    button_attach(&btn_key2, PRESS_DOWN, dev_key_press_down);
-
-    button_start(&btn_key_up);
-    button_start(&btn_key0);
-    button_start(&btn_key1);
-    button_start(&btn_key2);
-}
-void dev_key_button_task()
-{
-    button_ticks();
-}

+ 0 - 30
dev/dev_key.h

@@ -1,30 +0,0 @@
-#ifndef __DEV_KEY_H
-#define __DEV_KEY_H
-
-#include "hal_gpio.h"
-#include "multi_button.h"
-
-typedef union
-{
-    struct
-    {
-        uint8_t key_up_state_flag : 1;
-        uint8_t key0_state_flag   : 1;
-        uint8_t key1_state_flag   : 1;
-        uint8_t key2_state_flag   : 1;
-    } bits;
-    uint8_t key_all_state;
-} dev_key_state_t;
-
-typedef struct
-{
-    dev_key_state_t dev_key_state;
-    uint8_t         i;
-} dev_key_t;
-
-extern dev_key_t dev_key;
-
-void dev_key_button_init(void);
-void dev_key_button_task(void);
-
-#endif // __DEV_KEY_H

+ 0 - 42
dev/dev_led.c

@@ -1,42 +0,0 @@
-#include "dev_led.h"
-#include "hal_conf.h"
-
-dev_led_t dev_led; // 自定义led结构体;
-
-// 驱动led开关
-void dev_led_on_off(uint8_t leds, uint8_t on_off)
-{
-    switch (leds)
-    {
-    case 0:
-    {
-        if (0x00 == on_off)
-        {
-            // HAL_GPIO_LED0(0x00);
-            dev_led.dev_led_state.bits.led0_state_flag = 0;
-        }
-        else
-        {
-            // HAL_GPIO_LED0(0x01);
-            dev_led.dev_led_state.bits.led0_state_flag = 1;
-        }
-        break;
-    }
-    case 1:
-    {
-        if (0x00 == on_off)
-        {
-            HAL_GPIO_LED1(0x00);
-            dev_led.dev_led_state.bits.led1_state_flag = 0;
-        }
-        else
-        {
-            HAL_GPIO_LED1(0x01);
-            dev_led.dev_led_state.bits.led1_state_flag = 1;
-        }
-        break;
-    }
-    default:
-        break;
-    }
-}

+ 0 - 18
dev/dev_systick.c

@@ -1,18 +0,0 @@
-#include "dev_systick.h"
-
-volatile static uint32_t delay;
-volatile uint32_t        sys_tick_u32 = 0;
-static uint32_t          fac_us       = 168;
-
-void dev_systick_decrement(void)
-{
-    if (0u != delay)
-    {
-        delay--;
-    }
-}
-
-void dev_systick_increase(void)
-{
-    sys_tick_u32++;
-}

+ 0 - 9
dev/dev_systick.h

@@ -1,9 +0,0 @@
-#ifndef __DEV_SYSTICK_H
-#define __DEV_SYSTICK_H
-
-#include "stm32f4xx.h"
-
-void dev_systick_decrement(void);
-void dev_systick_increase(void);
-
-#endif // __DEV_SYSTICK_H

+ 0 - 50
dev/dev_uart_table.h

@@ -1,50 +0,0 @@
-#ifndef __DEV──UART──TABLE_H
-#define __DEV──UART──TABLE_H
-
-#include "hal_uart4.h"
-#include <stdint.h>
-#ifdef __cplusplus
-extern 'C'
-{
-#endif
-#ifdef __DEV──UART──TABLE_H_GLOBAL
-#define __DEV──UART──TABLE_H_EXTERN
-#else
-#define __DEV──UART──TABLE_H_EXTERN extern
-#endif
-
-    /************************************************************************************************
-     *                                          Version                                              *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          How to use                                           *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Enable config                                         *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Includes                                             *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
-    typedef struct
-    {
-        uint16_t addr;
-        void (*p_func)(uint8_t memoft, uint8_t *data, uint8_t *read_bytes);
-    } regist_read_type;
-
-    uint8_t get_regist_value(uart_type * p_data, uint8_t mem_start_addr, uint8_t read_len);
-    uint8_t set_regist_value(uart_type * p_data, uint8_t mem_start_addr, uint8_t read_len);
-/************************************************************************************************
- *                                          Defines                                              *
- ************************************************************************************************/
-#ifdef __cplusplus
-};
-#endif
-
-#endif // __DEV──UART──TABLE_H

+ 5 - 0
docs/readme.md

@@ -0,0 +1,5 @@
+通用层(common): 项目中通用的宏定义, 全局共享的变量等.
+应用层(applications): 业务逻辑, 流程框架都在这层.
+模块层(modules): 自己实现的模块化功能代码, 与MCU硬件不相关的组件. 比如串口命令组件, i2c-tools组件等.
+驱动层(drivers): 各个硬件模块驱动代码.
+厂商提供的源码(Vendors): MCU原厂提供的SDK, 第三方模块的源码, 比如FreeRTOS, File System等.

+ 19 - 13
hal/hal_can.c → drivers/can/can.c

@@ -1,4 +1,4 @@
-#include "hal_can.h"
+#include "can.h"
 #include "stm32f4xx.h"
 #include "stm32f4xx_gpio.h"
 #include <string.h>
@@ -18,7 +18,7 @@ pcan_tx  pcan_tx_back = NULL;
 // Fpclk1的时钟在初始化的时候设置为42M,如果设置CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_LoopBack);
 // 则波特率为:42M/((6+7+1)*6)=500Kbps
 
-u8 hal_can1_mode_init(uint8_t tsjw, uint8_t tbs2, uint8_t tbs1, uint16_t brp, uint8_t mode)
+u8 can1_mode_init(uint8_t tsjw, uint8_t tbs2, uint8_t tbs1, uint16_t brp, uint8_t mode)
 {
     GPIO_InitTypeDef      GPIO_InitStructure;
     CAN_InitTypeDef       CAN_InitStructure;
@@ -95,18 +95,18 @@ u8 hal_can1_mode_init(uint8_t tsjw, uint8_t tbs2, uint8_t tbs1, uint16_t brp, ui
  *	形    参: 无
  *	返 回 值: 无
  **********************************************************************************************************/
-void hal_can_init(void)
+void can_init(void)
 {
 #if defined(__CAN_BAUD_500K)
-    hal_can1_mode_init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 6, CAN_Mode_Normal); // CAN1初始化环回模式,波特率500Kbps
+    can1_mode_init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 6, CAN_Mode_Normal); // CAN1初始化环回模式,波特率500Kbps
 #elif defined(__CAN_BAUD_250K)
-    hal_can1_mode_init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 12, CAN_Mode_Normal); // CAN1初始化环回模式,波特率250Kbps
+    can1_mode_init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 12, CAN_Mode_Normal); // CAN1初始化环回模式,波特率250Kbps
 #else
 #error no can baud macro!;
 #endif
 }
 
-uint8_t hal_can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len)
+uint8_t can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len)
 {
     uint8_t flg = 0;
     if (id_type)
@@ -118,22 +118,28 @@ uint8_t hal_can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t le
         tx_message.StdId = id_type; // 标准标识符为0
     }
 
-    tx_message.IDE = id_type;      // 使用扩展标识符
-    tx_message.RTR = CAN_RTR_Data; // 消息类型为数据帧
-    tx_message.DLC = len;          // 发送两帧信息
-
-    memcpy(tx_message.Data, p_data, len);
+    tx_message.IDE = id_type; // 使用扩展标识符
+    tx_message.DLC = len;     // 发送两帧信息
+    if (len == 0)
+    {
+        tx_message.RTR = CAN_RTR_Remote; // 消息类型为远程帧
+    }
+    else
+    {
+        tx_message.RTR = CAN_RTR_Data; // 消息类型为数据帧
+        memcpy(tx_message.Data, p_data, len);
+    }
     flg = CAN_Transmit(CAN1, &tx_message);
 
     return flg;
 }
 
-void hal_can_rx_back_init(pcan_rx p_fun)
+void can_rx_back_init(pcan_rx p_fun)
 {
     pcan_rx_back = p_fun;
 }
 
-void hal_can_tx_back_init(pcan_tx p_fun)
+void can_tx_back_init(pcan_tx p_fun)
 {
     pcan_tx_back = p_fun;
 }

+ 16 - 0
drivers/can/can.h

@@ -0,0 +1,16 @@
+#ifndef __CAN_H
+#define __CAN_H
+
+#include "stm32f4xx.h"
+
+#define __CAN_BAUD_250K
+
+typedef void (*pcan_tx)(void);
+typedef void (*pcan_rx)(CanRxMsg);
+
+void    can_init(); // CAN初始化
+uint8_t can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len);
+void    can_rx_back_init(pcan_rx p_fun);
+void    can_tx_back_init(pcan_tx p_fun);
+
+#endif

+ 1 - 2
hal/hal_flash.c → drivers/flash/flash.c

@@ -2,9 +2,8 @@
  *                                          Include                                              *
  ************************************************************************************************/
 
-#include "hal_flash.h"
+#include "flash.h"
 #include "stm32f4xx.h"
-#include <stdint.h>
 
 /************************************************************************************************
  *                                          Config                                                *

+ 32 - 25
hal/hal_flash.h → drivers/flash/flash.h

@@ -1,35 +1,37 @@
-#ifndef __HAL_FLASH_H
-#define __HAL_FLASH_H
+#ifndef __FLASH_H
+#define __FLASH_H
 
 #ifdef __cplusplus
-extern 'C'
-{
+extern "C" {
 #endif
-#ifdef __HAL_FLASH_H_GLOBAL
-#define __HAL_FLASH_H_EXTERN
+#ifdef __FLASH_H_GLOBAL
+#define __FLASH_H_EXTERN
 #else
-#define __HAL_FLASH_H_EXTERN extern
+#define __FLASH_H_EXTERN extern
 #endif
 
 /************************************************************************************************
- *                                          Version                                              *
+ *                                          Version                                             *
  ************************************************************************************************/
 
 /************************************************************************************************
- *                                          How to use                                           *
+ *                                          How to use                                          *
  ************************************************************************************************/
 
 /************************************************************************************************
- *                                          Enable config                                         *
+ *                                          Enable config                                        *
  ************************************************************************************************/
 
 /************************************************************************************************
- *                                          Includes                                             *
+ *                                          Includes                                            *
  ************************************************************************************************/
-#include "stm32f4xx_flash.h"
+
+#include <stdint.h>
+
 /************************************************************************************************
  *                                          Defines                                              *
  ************************************************************************************************/
+
 #define FMC_PAGE_SIZE     ((uint16_t)0x800)
 #define FMC_PAGE_SIZE_U16 ((uint16_t)0x400)
 #define FMC_FLASH_BASE    ((uint32_t)0x08000000)
@@ -53,22 +55,27 @@ extern 'C'
 #define ADDR_FLASH_SECTOR_10  ((u32)0x080C0000) // 扇区10起始地址,128 Kbytes
 #define ADDR_FLASH_SECTOR_11  ((u32)0x080E0000) // 扇区11起始地址,128 Kbytes
 #define ADDR_FLASH_SECTOR_END ((u32)0x080FFFFF)
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
 
-    void     fmc_clear_flag_star(void);
-    void     fmc_clear_flag_end(void);
-    uint8_t  fmc_read_byte(uint32_t read_addr);
-    uint16_t fmc_read_half_word(uint32_t read_addr);
-    uint8_t  fmc_read_n_half_word(uint32_t read_addr, uint16_t * buff, uint16_t count);
-    uint8_t  fmc_read_one_page(uint32_t read_addr, uint16_t * buff);
-    uint8_t  fmc_erase_pages(uint32_t erase_addr, uint16_t len);
-    uint8_t  fmc_write_n_half_word(uint32_t write_addr, uint16_t * buff, uint16_t len);
-    uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t * pbuff, uint32_t len);
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void     fmc_clear_flag_star(void);
+void     fmc_clear_flag_end(void);
+uint8_t  fmc_read_byte(uint32_t read_addr);
+uint16_t fmc_read_half_word(uint32_t read_addr);
+uint8_t  fmc_read_n_half_word(uint32_t read_addr, uint16_t *buff, uint16_t count);
+uint8_t  fmc_read_one_page(uint32_t read_addr, uint16_t *buff);
+uint8_t  fmc_erase_pages(uint32_t erase_addr, uint16_t len);
+uint8_t  fmc_write_n_half_word(uint32_t write_addr, uint16_t *buff, uint16_t len);
+uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t *pbuff, uint32_t len);
 
 #ifdef __cplusplus
 };
 #endif
 
-#endif // __HAL_FLASH_H
+#endif // __FLASH_H

+ 16 - 16
hal/hal_gpio.c → drivers/gpio/gpio.c

@@ -1,6 +1,6 @@
-#include "hal_gpio.h"
+#include "gpio.h"
 
-void hal_beep_init(void)
+void beep_init(void)
 {
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
 
@@ -9,12 +9,12 @@ void hal_beep_init(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_GPIO_BEEP_PIN;
+    GPIO_StructInit.GPIO_Pin   = GPIO_BEEP_PIN;
 
-    GPIO_Init(HAL_GPIO_BEEP_PORT, &GPIO_StructInit);
+    GPIO_Init(GPIO_BEEP_PORT, &GPIO_StructInit);
 }
 
-void hal_led_init(void)
+void led_init(void)
 {
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
 
@@ -23,12 +23,12 @@ void hal_led_init(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_GPIO_LED1_PIN;
+    GPIO_StructInit.GPIO_Pin   = GPIO_LED1_PIN;
 
-    GPIO_Init(HAL_GPIO_LED1_PORT, &GPIO_StructInit);
+    GPIO_Init(GPIO_LED1_PORT, &GPIO_StructInit);
 }
 
-void hal_key_init(void)
+void key_init(void)
 {
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
@@ -38,18 +38,18 @@ void hal_key_init(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_GPIO_KEY0_PIN | HAL_GPIO_KEY1_PIN | HAL_GPIO_KEY2_PIN;
+    GPIO_StructInit.GPIO_Pin   = GPIO_KEY0_PIN | GPIO_KEY1_PIN | GPIO_KEY2_PIN;
 
-    GPIO_Init(HAL_GPIO_KEY0_PORT, &GPIO_StructInit);
+    GPIO_Init(GPIO_KEY0_PORT, &GPIO_StructInit);
 
     GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_DOWN;
-    GPIO_StructInit.GPIO_Pin  = HAL_GPIO_KEY_UP_PIN;
-    GPIO_Init(HAL_GPIO_KEY_UP_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin  = GPIO_KEY_UP_PIN;
+    GPIO_Init(GPIO_KEY_UP_PORT, &GPIO_StructInit);
 }
 
-void hal_gpio_init(void)
+void gpio_init(void)
 {
-    hal_led_init();
-    hal_key_init();
-    hal_beep_init();
+    led_init();
+    key_init();
+    beep_init();
 }

+ 63 - 0
drivers/gpio/gpio.h

@@ -0,0 +1,63 @@
+#ifndef __GPIO_H
+#define __GPIO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __GPIO_H_GLOBAL
+#define __GPIO_H_EXTERN
+#else
+#define __GPIO_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+#include "stm32f4xx.h"
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define GPIO_BEEP_PORT         GPIOF
+#define GPIO_BEEP_PIN          GPIO_Pin_8
+#define GPIO_LED1_PORT         GPIOF
+#define GPIO_LED1_PIN          GPIO_Pin_10
+#define GPIO_KEY_UP_PORT       GPIOA
+#define GPIO_KEY_UP_PIN        GPIO_Pin_0
+#define GPIO_KEY0_PORT         GPIOE
+#define GPIO_KEY0_PIN          GPIO_Pin_4
+#define GPIO_KEY1_PORT         GPIOE
+#define GPIO_KEY1_PIN          GPIO_Pin_3
+#define GPIO_KEY2_PORT         GPIOE
+#define GPIO_KEY2_PIN          GPIO_Pin_2
+#define GPIO_LED1(on_off)      ((on_off == 0) ? (GPIO_ResetBits(GPIO_LED1_PORT, GPIO_LED1_PIN)) : (GPIO_SetBits(GPIO_LED1_PORT, GPIO_LED1_PIN)))
+#define GPIO_BEEP(beep_status) ((beep_status == 0) ? (GPIO_ResetBits(GPIO_BEEP_PORT, GPIO_BEEP_PIN)) : (GPIO_SetBits(GPIO_BEEP_PORT, GPIO_BEEP_PIN)))
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+void gpio_init(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __GPIO_H

+ 16 - 16
hal/hal_i2c1.c → drivers/i2c/i2c1.c

@@ -1,23 +1,23 @@
-#include "hal_i2c1.h"
+#include "i2c1.h"
 
-void hal_i2c1_gpio_config(void)
+void i2c1_gpio_config(void)
 {
-    RCC_AHB1PeriphClockCmd(HAL_I2C1_SCL_CLK, ENABLE);
-    RCC_AHB1PeriphClockCmd(HAL_I2C1_SDA_CLK, ENABLE);
+    RCC_AHB1PeriphClockCmd(I2C1_SCL_CLK, ENABLE);
+    RCC_AHB1PeriphClockCmd(I2C1_SDA_CLK, ENABLE);
 
     GPIO_InitTypeDef GPIO_StructInit;
     GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
     GPIO_StructInit.GPIO_OType = GPIO_OType_OD;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_StructInit.GPIO_Pin   = HAL_I2C1_SCL_PIN;
-    GPIO_PinAFConfig(HAL_I2C1_SCL_PORT, HAL_I2C1_SCL_SOURCE, GPIO_AF_I2C1);
-    GPIO_Init(HAL_I2C1_SCL_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin   = I2C1_SCL_PIN;
+    GPIO_PinAFConfig(I2C1_SCL_PORT, I2C1_SCL_SOURCE, GPIO_AF_I2C1);
+    GPIO_Init(I2C1_SCL_PORT, &GPIO_StructInit);
 
-    GPIO_PinAFConfig(HAL_I2C1_SDA_PORT, HAL_I2C1_SDA_SOURCE, GPIO_AF_I2C1);
-    GPIO_StructInit.GPIO_Pin = HAL_I2C1_SDA_PIN;
-    GPIO_Init(HAL_I2C1_SDA_PORT, &GPIO_StructInit);
+    GPIO_PinAFConfig(I2C1_SDA_PORT, I2C1_SDA_SOURCE, GPIO_AF_I2C1);
+    GPIO_StructInit.GPIO_Pin = I2C1_SDA_PIN;
+    GPIO_Init(I2C1_SDA_PORT, &GPIO_StructInit);
 }
-void hal_i2c1_config(void)
+void i2c1_config(void)
 {
 
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
@@ -26,18 +26,18 @@ void hal_i2c1_config(void)
     I2C_DeInit(I2C1);
     I2C_InitStructure.I2C_Ack                 = I2C_Ack_Enable;               // I2C应答信号使能
     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // 设置从机地址长度为7位
-    I2C_InitStructure.I2C_ClockSpeed          = HAL_I2C1_SPEED;               // 设置目标周期 400khz
+    I2C_InitStructure.I2C_ClockSpeed          = I2C1_SPEED;                   // 设置目标周期 400khz
     I2C_InitStructure.I2C_DutyCycle           = I2C_DutyCycle_2;              // 设置占空比为2:1
     I2C_InitStructure.I2C_Mode                = I2C_Mode_I2C;                 // 设置为I2C模式
-    I2C_InitStructure.I2C_OwnAddress1         = HAL_I2C1_ADDRESS;             // 设置主机地址
+    I2C_InitStructure.I2C_OwnAddress1         = I2C1_ADDRESS;                 // 设置主机地址
 
     I2C_Init(I2C1, &I2C_InitStructure); // 初始化I2C1结构体
 
     I2C_Cmd(I2C1, ENABLE); // 使能I2C1
 }
 
-void hal_i2c1_init(void)
+void i2c1_init(void)
 {
-    hal_i2c1_gpio_config();
-    hal_i2c1_config();
+    i2c1_gpio_config();
+    i2c1_config();
 }

+ 20 - 0
drivers/i2c/i2c1.h

@@ -0,0 +1,20 @@
+#ifndef __I2C_H
+#define __I2C_H
+
+#include "stm32f4xx.h"
+
+#define I2C1_SCL_PORT   GPIOB
+#define I2C1_SCL_PIN    GPIO_Pin_8
+#define I2C1_SCL_SOURCE GPIO_PinSource8
+#define I2C1_SCL_CLK    RCC_AHB1Periph_GPIOB
+#define I2C1_SDA_PORT   GPIOB
+#define I2C1_SDA_PIN    GPIO_Pin_9
+#define I2C1_SDA_SOURCE GPIO_PinSource9
+#define I2C1_SDA_CLK    RCC_AHB1Periph_GPIOB
+
+#define I2C1_ADDRESS 0xA0
+#define I2C1_SPEED   100000U
+
+void i2c1_init(void);
+
+#endif // __I2C_H

+ 1 - 2
hal/hal_pwm.c → drivers/pwm/pwm.c

@@ -1,5 +1,4 @@
-#include "hal_pwm.h"
-#include "stm32f4xx_tim.h"
+#include "pwm.h"
 
 void pwm_init(pwm_content_t *pwm_content)
 {

+ 62 - 0
drivers/pwm/pwm.h

@@ -0,0 +1,62 @@
+#ifndef __PWM_H
+#define __PWM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __PWM_H_GLOBAL
+#define __PWM_H_EXTERN
+#else
+#define __PWM_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                           *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                         *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                             *
+ ************************************************************************************************/
+
+#include "stm32f4xx.h"
+#include <stdint.h>
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+typedef struct
+{
+    GPIO_TypeDef *pwm_gpio_port;
+    uint32_t      pwm_gpio_pin;
+    uint32_t      pwm_gpio_rcu;
+    uint16_t      pwm_gpio_af;
+    uint8_t       pwm_time_af;
+    TIM_TypeDef  *pwm_timer;
+    uint32_t      pwm_timer_rcu;
+    uint32_t      pwm_timer_ch;
+    uint32_t      pwm_prescaler;
+    uint32_t      pwm_period;
+    uint32_t      pwm_pulse;
+    uint8_t       pwm_status;
+} pwm_content_t;
+
+void pwm_init(pwm_content_t *pwm_content);
+void pwm_mode(pwm_content_t *pwm_content, uint8_t status);
+
+extern pwm_content_t pwm_timer3_content;
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __PWM_H

+ 19 - 3
hal/hal_systick.c → drivers/systick/systick.c

@@ -1,5 +1,5 @@
-#include "hal_systick.h"
-#include <stdint.h>
+#include "systick.h"
+#include "misc.h"
 
 static u8  fac_us = 0; // us延时倍乘数
 static u16 fac_ms = 0; // ms延时倍乘数
@@ -7,7 +7,23 @@ static u16 fac_ms = 0; // ms延时倍乘数
 volatile uint32_t systick_ms = 0;
 volatile uint32_t systick_us = 0;
 
-void hal_systick_init(void)
+volatile static uint32_t delay;
+volatile uint32_t        sys_tick_u32 = 0;
+
+void dev_systick_decrement(void)
+{
+    if (0u != delay)
+    {
+        delay--;
+    }
+}
+
+void dev_systick_increase(void)
+{
+    sys_tick_u32++;
+}
+
+void systick_init(void)
 {
     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
     if (SysTick_Config(SystemCoreClock / 1000))

+ 57 - 0
drivers/systick/systick.h

@@ -0,0 +1,57 @@
+#ifndef __SYSTICK_H
+#define __SYSTICK_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __SYSTICK_H_GLOBAL
+#define __SYSTICK_H_EXTERN
+#else
+#define __SYSTICK_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+#include <stdint.h>
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+#define SYSTICK_EXT_CLOCK_1MS_CNT (SystemCoreClock / 1000)
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void     systick_init(void);
+void     systick_handler(void);
+uint32_t get_systick_ms(void);
+uint32_t get_systick_us(void);
+void     dev_systick_decrement(void);
+void     dev_systick_increase(void);
+void     ms_delay(uint8_t ms_time);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __SYSTICK_H

+ 41 - 42
hal/hal_uart.c → drivers/uart/uart.c

@@ -1,7 +1,6 @@
-#include "hal_uart.h"
-#include "stm32f4xx.h"
-// #include <stdint.h>
-#include <stdint.h>
+#include "uart.h"
+#include "systick.h"
+#include <stdio.h>
 #include <sys/unistd.h>
 
 static uint8_t usart1_tx_buf[256];
@@ -13,26 +12,26 @@ static uint8_t usart2_rx_buf[256];
 static uint8_t usart3_tx_buf[256];
 static uint8_t usart3_rx_buf[256];
 
-#define HAL_UART1_TX_PORT   GPIOA
-#define HAL_UART1_TX_PIN    GPIO_Pin_9
-#define HAL_UART1_TX_SOURCE GPIO_PinSource9
-#define HAL_UART1_RX_PORT   GPIOA
-#define HAL_UART1_RX_PIN    GPIO_Pin_10
-#define HAL_UART1_RX_SOURCE GPIO_PinSource10
-
-#define HAL_UART2_TX_PORT   GPIOA
-#define HAL_UART2_TX_PIN    GPIO_Pin_2
-#define HAL_UART2_TX_SOURCE GPIO_PinSource2
-#define HAL_UART2_RX_PORT   GPIOA
-#define HAL_UART2_RX_PIN    GPIO_Pin_3
-#define HAL_UART2_RX_SOURCE GPIO_PinSource3
-
-#define HAL_UART3_TX_PORT   GPIOB
-#define HAL_UART3_TX_PIN    GPIO_Pin_10
-#define HAL_UART3_TX_SOURCE GPIO_PinSource2
-#define HAL_UART3_RX_PORT   GPIOB
-#define HAL_UART3_RX_PIN    GPIO_Pin_11
-#define HAL_UART3_RX_SOURCE GPIO_PinSource3
+#define UART1_TX_PORT   GPIOA
+#define UART1_TX_PIN    GPIO_Pin_9
+#define UART1_TX_SOURCE GPIO_PinSource9
+#define UART1_RX_PORT   GPIOA
+#define UART1_RX_PIN    GPIO_Pin_10
+#define UART1_RX_SOURCE GPIO_PinSource10
+
+#define UART2_TX_PORT   GPIOA
+#define UART2_TX_PIN    GPIO_Pin_2
+#define UART2_TX_SOURCE GPIO_PinSource2
+#define UART2_RX_PORT   GPIOA
+#define UART2_RX_PIN    GPIO_Pin_3
+#define UART2_RX_SOURCE GPIO_PinSource3
+
+#define UART3_TX_PORT   GPIOB
+#define UART3_TX_PIN    GPIO_Pin_10
+#define UART3_TX_SOURCE GPIO_PinSource2
+#define UART3_RX_PORT   GPIOB
+#define UART3_RX_PIN    GPIO_Pin_11
+#define UART3_RX_SOURCE GPIO_PinSource3
 
 int usart1_snd_tx_cnt = 0;
 
@@ -181,13 +180,13 @@ static void usart1_config(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_UART1_TX_PIN;
-    GPIO_PinAFConfig(HAL_UART1_TX_PORT, HAL_UART1_TX_SOURCE, GPIO_AF_USART1);
-    GPIO_Init(HAL_UART1_TX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin   = UART1_TX_PIN;
+    GPIO_PinAFConfig(UART1_TX_PORT, UART1_TX_SOURCE, GPIO_AF_USART1);
+    GPIO_Init(UART1_TX_PORT, &GPIO_StructInit);
 
-    GPIO_StructInit.GPIO_Pin = HAL_UART1_RX_PIN;
-    GPIO_PinAFConfig(HAL_UART1_RX_PORT, HAL_UART1_RX_SOURCE, GPIO_AF_USART1);
-    GPIO_Init(HAL_UART1_RX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin = UART1_RX_PIN;
+    GPIO_PinAFConfig(UART1_RX_PORT, UART1_RX_SOURCE, GPIO_AF_USART1);
+    GPIO_Init(UART1_RX_PORT, &GPIO_StructInit);
 
     // Usart1 NVIC 配置
     NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn; // 串口1中断通道
@@ -212,13 +211,13 @@ static void usart2_config(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_UART2_TX_PIN;
-    GPIO_PinAFConfig(HAL_UART2_TX_PORT, HAL_UART2_TX_SOURCE, GPIO_AF_USART2);
-    GPIO_Init(HAL_UART2_TX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin   = UART2_TX_PIN;
+    GPIO_PinAFConfig(UART2_TX_PORT, UART2_TX_SOURCE, GPIO_AF_USART2);
+    GPIO_Init(UART2_TX_PORT, &GPIO_StructInit);
 
-    GPIO_StructInit.GPIO_Pin = HAL_UART2_RX_PIN;
-    GPIO_PinAFConfig(HAL_UART2_RX_PORT, HAL_UART2_RX_SOURCE, GPIO_AF_USART2);
-    GPIO_Init(HAL_UART2_RX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin = UART2_RX_PIN;
+    GPIO_PinAFConfig(UART2_RX_PORT, UART2_RX_SOURCE, GPIO_AF_USART2);
+    GPIO_Init(UART2_RX_PORT, &GPIO_StructInit);
 
     // Usart1 NVIC 配置
     NVIC_InitStructure.NVIC_IRQChannel                   = USART2_IRQn; // 串口1中断通道
@@ -243,13 +242,13 @@ static void usart3_config(void)
     GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
     GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = HAL_UART3_TX_PIN;
-    GPIO_PinAFConfig(HAL_UART3_TX_PORT, HAL_UART3_TX_SOURCE, GPIO_AF_USART3);
-    GPIO_Init(HAL_UART3_TX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin   = UART3_TX_PIN;
+    GPIO_PinAFConfig(UART3_TX_PORT, UART3_TX_SOURCE, GPIO_AF_USART3);
+    GPIO_Init(UART3_TX_PORT, &GPIO_StructInit);
 
-    GPIO_StructInit.GPIO_Pin = HAL_UART3_RX_PIN;
-    GPIO_PinAFConfig(HAL_UART3_RX_PORT, HAL_UART3_RX_SOURCE, GPIO_AF_USART3);
-    GPIO_Init(HAL_UART3_RX_PORT, &GPIO_StructInit);
+    GPIO_StructInit.GPIO_Pin = UART3_RX_PIN;
+    GPIO_PinAFConfig(UART3_RX_PORT, UART3_RX_SOURCE, GPIO_AF_USART3);
+    GPIO_Init(UART3_RX_PORT, &GPIO_StructInit);
 
     // Usart1 NVIC 配置
     NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn; // 串口1中断通道

+ 2 - 2
hal/hal_uart.h → drivers/uart/uart.h

@@ -1,7 +1,7 @@
 #ifndef __UART_H
 #define __UART_H
-#include "hal_systick.h"
-#include "stdio.h"
+#include "stdint.h"
+#include "stm32f4xx.h"
 
 #pragma pack(0)
 typedef struct

+ 45 - 0
drivers/uart/uart4.c

@@ -0,0 +1,45 @@
+#include "uart4.h"
+#include "stm32f4xx.h"
+
+void uart4_init(void)
+{
+    GPIO_InitTypeDef GPIO_StructInit;
+    NVIC_InitTypeDef NVIC_InitStructure;
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
+    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
+
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
+    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
+    GPIO_StructInit.GPIO_Pin   = GPIO_Pin_10;
+    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
+    GPIO_Init(GPIOC, &GPIO_StructInit);
+
+    GPIO_StructInit.GPIO_Pin = GPIO_Pin_11;
+    // GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
+    // GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
+    // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
+    // GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
+    GPIO_Init(GPIOC, &GPIO_StructInit);
+
+    NVIC_InitStructure.NVIC_IRQChannel                   = UART4_IRQn; // 串口1中断通道
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;          // 抢占优先级
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;          // 子优先级
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;     // IRQ通道使能
+    NVIC_Init(&NVIC_InitStructure);
+
+    USART_InitTypeDef USART_InitStructure;
+    USART_InitStructure.USART_BaudRate            = 115200;
+    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
+    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
+    USART_InitStructure.USART_Parity              = USART_Parity_No;
+    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
+    USART_Init(UART4, &USART_InitStructure); // 初始化串口
+
+    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
+
+    USART_Cmd(UART4, ENABLE); // 使能串口
+}

+ 47 - 0
drivers/uart/uart4.h

@@ -0,0 +1,47 @@
+#ifndef __UART4_H
+#define __UART4_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __UART4_H_GLOBAL
+#define __UART4_H_EXTERN
+#else
+#define __UART4_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void uart4_init(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __UART4_H

+ 0 - 16
hal/hal_can.h

@@ -1,16 +0,0 @@
-#ifndef __HAL_CAN_H
-#define __HAL_CAN_H
-
-#include "stm32f4xx.h"
-
-#define __CAN_BAUD_250K
-
-typedef void (*pcan_tx)(void);
-typedef void (*pcan_rx)(CanRxMsg);
-
-void    hal_can_init(); // CAN初始化
-uint8_t hal_can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len);
-void    hal_can_rx_back_init(pcan_rx p_fun);
-void    hal_can_tx_back_init(pcan_tx p_fun);
-
-#endif

+ 0 - 11
hal/hal_conf.h

@@ -1,11 +0,0 @@
-#ifndef __HAL_CONF_H
-#define __HAL_CONF_H
-
-#include "hal_can.h"
-#include "hal_flash.h"
-#include "hal_gpio.h"
-#include "hal_i2c1.h"
-#include "hal_pwm.h"
-#include "hal_systick.h"
-#include "hal_uart.h"
-#endif // __APP_TASK_H

+ 0 - 27
hal/hal_gpio.h

@@ -1,27 +0,0 @@
-#ifndef __HAL_GPIO_H
-#define __HAL_GPIO_H
-
-#include "stm32f4xx.h"
-
-#define HAL_GPIO_BEEP_PORT GPIOF
-#define HAL_GPIO_BEEP_PIN  GPIO_Pin_8
-// #define HAL_GPIO_LED0_PORT   GPIOF
-// #define HAL_GPIO_LED0_PIN    GPIO_Pin_9
-#define HAL_GPIO_LED1_PORT   GPIOF
-#define HAL_GPIO_LED1_PIN    GPIO_Pin_10
-#define HAL_GPIO_KEY_UP_PORT GPIOA
-#define HAL_GPIO_KEY_UP_PIN  GPIO_Pin_0
-#define HAL_GPIO_KEY0_PORT   GPIOE
-#define HAL_GPIO_KEY0_PIN    GPIO_Pin_4
-#define HAL_GPIO_KEY1_PORT   GPIOE
-#define HAL_GPIO_KEY1_PIN    GPIO_Pin_3
-#define HAL_GPIO_KEY2_PORT   GPIOE
-#define HAL_GPIO_KEY2_PIN    GPIO_Pin_2
-
-// #define HAL_GPIO_LED0(on_off)      ((on_off == 0) ? (GPIO_ResetBits(HAL_GPIO_LED0_PORT, HAL_GPIO_LED0_PIN)) : (GPIO_SetBits(HAL_GPIO_LED0_PORT, HAL_GPIO_LED0_PIN)))
-#define HAL_GPIO_LED1(on_off)      ((on_off == 0) ? (GPIO_ResetBits(HAL_GPIO_LED1_PORT, HAL_GPIO_LED1_PIN)) : (GPIO_SetBits(HAL_GPIO_LED1_PORT, HAL_GPIO_LED1_PIN)))
-#define HAL_GPIO_BEEP(beep_status) ((beep_status == 0) ? (GPIO_ResetBits(HAL_GPIO_BEEP_PORT, HAL_GPIO_BEEP_PIN)) : (GPIO_SetBits(HAL_GPIO_BEEP_PORT, HAL_GPIO_BEEP_PIN)))
-
-void hal_gpio_init(void);
-
-#endif // __HAL_GPIO_H

+ 0 - 20
hal/hal_i2c1.h

@@ -1,20 +0,0 @@
-#ifndef __HAL_I2C_H
-#define __HAL_I2C_H
-
-#include "stm32f4xx.h"
-
-#define HAL_I2C1_SCL_PORT   GPIOB
-#define HAL_I2C1_SCL_PIN    GPIO_Pin_8
-#define HAL_I2C1_SCL_SOURCE GPIO_PinSource8
-#define HAL_I2C1_SCL_CLK    RCC_AHB1Periph_GPIOB
-#define HAL_I2C1_SDA_PORT   GPIOB
-#define HAL_I2C1_SDA_PIN    GPIO_Pin_9
-#define HAL_I2C1_SDA_SOURCE GPIO_PinSource9
-#define HAL_I2C1_SDA_CLK    RCC_AHB1Periph_GPIOB
-
-#define HAL_I2C1_ADDRESS 0xA0
-#define HAL_I2C1_SPEED   100000U
-
-void hal_i2c1_init(void);
-
-#endif // __HAL_I2C_H

+ 0 - 62
hal/hal_pwm.h

@@ -1,62 +0,0 @@
-#ifndef __HAL_PWM_H
-#define __HAL_PWM_H
-
-#include <stdint.h>
-#ifdef __cplusplus
-extern 'C'
-{
-#endif
-#ifdef __HAL_PWM_H_GLOBAL
-#define __HAL_PWM_H_EXTERN
-#else
-#define __HAL_PWM_H_EXTERN extern
-#endif
-
-    /************************************************************************************************
-     *                                          Version                                              *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          How to use                                           *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Enable config                                         *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Includes                                             *
-     ************************************************************************************************/
-#include "stm32f4xx_gpio.h"
-#include "stm32f4xx_tim.h"
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
-    typedef struct
-    {
-        GPIO_TypeDef *pwm_gpio_port;
-        uint32_t      pwm_gpio_pin;
-        uint32_t      pwm_gpio_rcu;
-        uint16_t      pwm_gpio_af;
-        uint8_t       pwm_time_af;
-        TIM_TypeDef  *pwm_timer;
-        uint32_t      pwm_timer_rcu;
-        uint32_t      pwm_timer_ch;
-        uint32_t      pwm_prescaler;
-        uint32_t      pwm_period;
-        uint32_t      pwm_pulse;
-        uint8_t       pwm_status;
-    } pwm_content_t;
-
-    void pwm_init(pwm_content_t * pwm_content);
-    void pwm_mode(pwm_content_t * pwm_content, uint8_t status);
-
-    extern pwm_content_t pwm_timer3_content;
-/************************************************************************************************
- *                                          Defines                                              *
- ************************************************************************************************/
-#ifdef __cplusplus
-};
-#endif
-
-#endif // __HAL_PWM_H

+ 0 - 14
hal/hal_systick.h

@@ -1,14 +0,0 @@
-#ifndef __HAL_SYSTICK_H
-#define __HAL_SYSTICK_H
-
-#include "stm32f4xx.h"
-
-void     hal_systick_init(void);
-uint32_t get_systick_ms(void);
-
-#define SYSTICK_EXT_CLOCK_1MS_CNT (SystemCoreClock / 1000)
-
-void systick_handler(void);
-
-void ms_delay(uint8_t ms_time);
-#endif // __HAL_SYSTICK_H

+ 0 - 136
hal/hal_uart4.c

@@ -1,136 +0,0 @@
-#include "hal_uart4.h"
-#include "queue.h"
-#include "stm32f4xx.h"
-#include "stm32f4xx_gpio.h"
-#include "stm32f4xx_rcc.h"
-#include "stm32f4xx_usart.h"
-
-uart_type uart_msg = {
-    .rx_count_u8        = 0,
-    .tx_count_u8        = 0,
-    .rx_finished_flg    = 0,
-    .disconnect_flg     = 0,
-    .rx_over_time       = 5,
-    .rx_over_time_count = 0xFF,
-    .disconnect_count   = 0,
-};
-
-void hal_uart4_init(void)
-{
-    GPIO_InitTypeDef GPIO_StructInit;
-    NVIC_InitTypeDef NVIC_InitStructure;
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
-
-    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
-    GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
-    GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_UP;
-    GPIO_StructInit.GPIO_Pin   = GPIO_Pin_10;
-    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
-    GPIO_Init(GPIOC, &GPIO_StructInit);
-
-    GPIO_StructInit.GPIO_Pin = GPIO_Pin_11;
-    // GPIO_StructInit.GPIO_Mode  = GPIO_Mode_AF;
-    // GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
-    // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
-    // GPIO_StructInit.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
-    GPIO_Init(GPIOC, &GPIO_StructInit);
-
-    NVIC_InitStructure.NVIC_IRQChannel                   = UART4_IRQn; // 串口1中断通道
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;          // 抢占优先级
-    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;          // 子优先级
-    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;     // IRQ通道使能
-    NVIC_Init(&NVIC_InitStructure);
-
-    USART_InitTypeDef USART_InitStructure;
-    USART_InitStructure.USART_BaudRate            = 115200;
-    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
-    USART_InitStructure.USART_StopBits            = USART_StopBits_1;
-    USART_InitStructure.USART_Parity              = USART_Parity_No;
-    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
-    USART_Init(UART4, &USART_InitStructure); // 初始化串口
-
-    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
-
-    USART_Cmd(UART4, ENABLE); // 使能串口
-}
-
-void UART4_IRQHandler(void)
-{
-    // 接收中断处理
-    if (RESET != USART_GetITStatus(UART4, USART_IT_RXNE))
-    {
-        USART_ClearITPendingBit(UART4, USART_IT_RXNE);
-
-        uart_msg.rx_over_time_count = 0;
-        uart_msg.disconnect_count   = 0;
-        uart_msg.disconnect_flg     = 0;
-
-        if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN)
-        {
-            uart_msg.rx_count_u8 = 0;
-        }
-
-        uart_msg.rx[uart_msg.rx_count_u8++] = USART_ReceiveData(UART4);
-    }
-
-    // 发送中断处理
-    if (RESET != USART_GetITStatus(UART4, USART_IT_TXE))
-    {
-        USART_ClearITPendingBit(UART4, USART_IT_TXE);
-
-        USART_SendData(UART4, uart_msg.tx[uart_msg.tx_count_u8++]);
-
-        if (uart_msg.tx_count_u8 >= uart_msg.tx_len)
-        {
-            USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
-            USART_ITConfig(UART4, USART_IT_TC, ENABLE);
-        }
-    }
-
-    // 发送完成中断
-    if (RESET != USART_GetITStatus(UART4, USART_IT_TC))
-    {
-        USART_ClearITPendingBit(UART4, USART_IT_TC);
-
-        USART_ITConfig(UART4, USART_IT_TC, DISABLE);
-        USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
-    }
-
-    // 发送错误
-    if (RESET != USART_GetITStatus(UART4, USART_IT_ORE))
-        USART_ClearITPendingBit(UART4, USART_IT_ORE);
-}
-
-void uart4_rx_ticks(void)
-{
-    if (uart_msg.rx_over_time_count != 0xFF)
-    {
-        if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time)
-        {
-            uart_msg.rx_over_time_count = 0xFF;
-            uart_msg.rx_finished_flg    = 1;
-        }
-    }
-
-    if (uart_msg.disconnect_count != 0xFFFF)
-    {
-        if (++uart_msg.disconnect_count >= 2500) // 5000ms
-        {
-            uart_msg.disconnect_count = 0xFFFF;
-            uart_msg.disconnect_flg   = 1;
-        }
-    }
-}
-
-void uart4_start_send(uart_type *p_msg)
-{
-    p_msg->tx_count_u8 = 0;
-
-    USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
-    USART_SendData(UART4, p_msg->tx[p_msg->tx_count_u8++]);
-    USART_ITConfig(UART4, USART_IT_TXE, ENABLE);
-}

+ 0 - 64
hal/hal_uart4.h

@@ -1,64 +0,0 @@
-#ifndef __HAL_UART4_H
-#define __HAL_UART4_H
-
-#include <stdint.h>
-#ifdef __cplusplus
-extern 'C'
-{
-#endif
-#ifdef __HAL_UART4_H_GLOBAL
-#define __HAL_UART4_H_EXTERN
-#else
-#define __HAL_UART4_H_EXTERN extern
-#endif
-
-    /************************************************************************************************
-     *                                          Version                                              *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          How to use                                           *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Enable config                                         *
-     ************************************************************************************************/
-
-    /************************************************************************************************
-     *                                          Includes                                             *
-     ************************************************************************************************/
-#include "stm32f4xx.h"
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
-#define RX_TX_BUF_LEN (50)
-
-    typedef struct
-    {
-        uint8_t  rx[RX_TX_BUF_LEN];
-        uint8_t  tx[RX_TX_BUF_LEN];
-        uint8_t  rx_len;
-        uint8_t  tx_len;
-        uint8_t  rx_count_u8;
-        uint8_t  tx_count_u8;
-        uint8_t  device_type;
-        uint8_t  rx_finished_flg;
-        uint8_t  disconnect_flg;
-        uint8_t  rx_over_time;
-        uint8_t  rx_over_time_count;
-        uint16_t disconnect_count;
-    } uart_type;
-
-    extern uart_type uart_msg;
-
-    void hal_uart4_init(void);
-    void uart4_rx_ticks(void);
-    void uart4_start_send(uart_type * p_msg);
-/************************************************************************************************
- *                                          Defines                                              *
- ************************************************************************************************/
-#ifdef __cplusplus
-};
-#endif
-
-#endif // __HAL_UART4_H

+ 0 - 179
hal/platform_queue.c

@@ -1,179 +0,0 @@
-// #include "platform_queue.h"
-
-// t_queue can_rx;
-// t_queue can_tx;
-
-// void queue_mode(t_queue *this, uint8_t mode)
-// {
-//     this->mode = mode;
-// }
-
-// void queue_empty_one(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM], uint8_t lock)
-// {
-//     *(head + lock)   = buff[lock];
-//     *(tail + lock)   = buff[lock];
-//     this->full[lock] = 0;
-// }
-
-// // 清空队列
-// void queue_empty(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM])
-// {
-//     for (int i = 0; i < QUEUE_BUFFER_SUM; i++)
-//     {
-//         *(head + i)   = this->buff[i];
-//         *(tail + i)   = buff[i];
-//         this->full[i] = 0;
-//     }
-//     this->lock = 0;
-//     this->idle = 0;
-// }
-
-// void qudeue_empty(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM])
-// {
-//     for (int i = 0; i < QUEUE_BUFFER_SUM; i++)
-//     {
-//         *(head + i)   = this->buff[i];
-//         *(tail + i)   = buff[i];
-//         this->full[i] = 0;
-//     }
-//     this->lock = 0;
-//     this->idle = 0;
-// }
-
-// // 队列长度计算
-// void length_queue(t_queue *this, pdu_tag **head, pdu_tag **tail, uint8_t len)
-// {
-//     for (int i = 0; i < QUEUE_BUFFER_SUM; i++)
-//     {
-//         if (*(tail + i) > *(head + i))
-//         {
-//             this->length[i] = *(tail + i) - *(head + i);
-//         }
-//         if (*(tail + i) < *(head + i))
-//         {
-//             this->length[i] = *(tail + i) + len - *(head + i);
-//         }
-//         if (*(tail + i) == *(head + i) && this->full[i])
-//         {
-//             this->length[i] = len;
-//         }
-//         if (*(tail + i) == *(head + i) && !this->full[i])
-//         {
-//             this->length[i] = 0;
-//         }
-//     }
-// }
-
-// // 入队
-// void push_queue(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM], uint8_t len, pdu_tag *data, uint8_t data_len)
-// {
-//     if (this->mode)
-//     {
-//         // 空闲清除读队列
-//         if (this->idle == 0)
-//         {
-//             empty_one_queue((*this), !this->lock);
-//         }
-//         // 非空闲清除写队列
-//         empty_one_queue((*this), this->lock);
-//     }
-
-//     for (int num = 0; num < data_len; num++, data++)
-//     {
-//         if (!this->mode)
-//         {
-//             queue_update((*this));
-//             if ((this->length[0] + this->length[1]) >= 4 && !this->idle)
-//             {
-//                 if (this->length[!this->lock] == 0)
-//                 {
-//                     this->lock              = !this->lock;
-//                     this->full[!this->lock] = 0;
-//                 }
-
-//                 for (int i = 0; i <= ((this->length[this->lock] + this->length[!this->lock]) - 4); i++)
-//                 {
-//                     if (++(*(head + !this->lock)) == buff[!this->lock] + len)
-//                     {
-//                         *(head + !this->lock) = buff[!this->lock];
-//                     }
-//                     if (*(tail + !this->lock) == *(head + !this->lock) && this->full[!this->lock] == 0)
-//                     {
-//                         break;
-//                     }
-//                 }
-//             }
-//         }
-//         // 队列长度更新
-//         queue_update((*this));
-//         /*单纯的数据存储*/
-//         **(tail + this->lock) = *data;
-//         // 队列满
-//         if (this->full[this->lock] == 1)
-//         {
-//             if (++(*(head + this->lock)) == buff[this->lock] + len)
-//             {
-//                 *(head + this->lock) = buff[this->lock];
-//             }
-//             if (++(*(tail + this->lock)) == buff[this->lock] + len)
-//             {
-//                 *(tail + this->lock) = buff[this->lock];
-//             }
-//         }
-//         // 队列未满
-//         else
-//         {
-//             if (++(*(tail + this->lock)) == buff[this->lock] + len)
-//             {
-//                 *(tail + this->lock) = buff[this->lock];
-//             }
-//             if (*(tail + this->lock) == *(head + this->lock))
-//             {
-//                 this->full[this->lock] = 1;
-//             }
-//         }
-//     }
-//     // 队列长度更新
-//     queue_update((*this));
-// }
-
-// // 出队
-// void pull_queue(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM], uint8_t len, pdu_tag *data)
-// {
-//     this->idle = 1;
-//     this->ret  = 0;
-//     // 非空队列
-//     if (!(*(tail + !this->lock) == *(head + !this->lock) && this->full[!this->lock] == 0))
-//     {
-//         *data = **(head + !this->lock);
-//         // 指针指向溢出,指向数组首地址
-//         if (++(*(head + !this->lock)) == buff[!this->lock] + len)
-//         {
-//             *(head + !this->lock) = buff[!this->lock];
-//         }
-//         this->full[!this->lock] = 0;
-//         this->ret               = 1;
-//     }
-//     queue_update((*this));
-//     // 出队时判断当前队列是否为空队列,若为空队列且从队列不为空队列,则跳转,若前面没有获取到数据,重新到跳转队列获取数据
-//     if (this->length[this->lock] != 0 && this->length[!this->lock] == 0)
-//     {
-//         this->lock = !this->lock;
-//         if (this->ret == 0)
-//         {
-//             de_queue((*this), data);
-//         }
-//     }
-//     // 这里结束 队列为空闲态
-//     this->idle = 0;
-// }
-
-// void can_queue_init(void)
-// {
-//     // 提前初始化,否则由于指针问题,HardFault_Handler()
-//     empty_queue(can_rx);
-//     mode_queue(can_rx, TASKQUEUE);
-//     // 提前初始化,否则由于指针问题,HardFault_Handler()
-//     empty_queue(can_tx);
-//     mode_queue(can_tx, TASKQUEUE);
-// }

+ 0 - 81
hal/platform_queue.h

@@ -1,81 +0,0 @@
-// #ifndef __PLATFROM_QUEUE_H
-// #define __PLATFROM_QUEUE_H
-// #include "stdio.h"
-
-// #define MAX_QSIZE (60u) /* 最大队列长度 */
-
-// typedef unsigned long long uint64_t;
-
-// typedef struct
-// {
-//     struct
-//     {
-//         uint8_t ide;
-//         uint8_t rtr;
-//         uint8_t dlc;
-//     } reg;
-//     union
-//     {
-//         uint32_t r;
-//         struct
-//         {
-//             uint8_t sa : 8;
-//             uint8_t ps : 8;
-//             uint8_t pf : 8;
-//             uint8_t dp : 1;
-//             uint8_t r  : 1;
-//             uint8_t p  : 3;
-//         } b;
-//     } id;
-//     union
-//     {
-//         uint8_t  u8_buf[8];
-//         uint16_t u16_buf[4];
-//         uint32_t u32_buf[2];
-//         uint64_t u64_buf;
-//     } data;
-// } pdu_tag, *p_pdu_tag;
-
-// typedef struct
-// {
-//     uint16_t head;
-//     uint16_t tail;
-//     uint16_t count;
-//     pdu_tag  can_message[MAX_QSIZE];
-// } can_queue_tag, *p_can_queue_tag;
-
-// typedef enum
-// {
-//     DATEQUEUE = 0,
-//     TASKQUEUE = 1,
-// } queuemode_e;
-
-// typedef struct
-// {
-// #define QUEUE_BUFFER_SUM 2
-// #define DATA_BUFF_NUM    5
-//     uint8_t  full[QUEUE_BUFFER_SUM];
-//     uint8_t  length[QUEUE_BUFFER_SUM];
-//     uint8_t  ret, lock, idle, mode;
-//     pdu_tag *head[QUEUE_BUFFER_SUM];
-//     pdu_tag *tail[QUEUE_BUFFER_SUM];
-//     pdu_tag  buff[QUEUE_BUFFER_SUM][DATA_BUFF_NUM];
-// } t_queue;
-
-// void queue_mode(t_queue *this, uint8_t mode);
-// void push_queue(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM], uint8_t len, pdu_tag *data, uint8_t data_len);
-// void pull_queue(t_queue *this, pdu_tag **head, pdu_tag **tail, pdu_tag (*buff)[DATA_BUFF_NUM], uint8_t len, pdu_tag *data);
-
-// #define queue_update(this)             length_queue(&this, this.head, this.tail, sizeof((this).buff[0]))
-// #define empty_queue(this)              queue_empty(&this, this.head, this.tail, this.buff)
-// #define empty_one_queue(this, lock)    queue_empty_one(&this, this.head, this.tail, this.buff, (lock))
-// #define en_queue(this, data, data_len) push_queue(&this, this.head, this.tail, this.buff, sizeof((this).buff[0]), (data), (data_len))
-// #define de_queue(this, data)           pull_queue(&this, this.head, this.tail, this.buff, sizeof((this).buff[0]), (data))
-// #define mode_queue(this, mode)         queue_mode(&this, (mode));
-
-// extern t_queue can_tx;
-// extern t_queue can_rx;
-
-// void can_queue_init(void);
-
-// #endif

+ 0 - 104
hal/queue.c111

@@ -1,104 +0,0 @@
-// #include "queue.h"
-
-// ///   front   ...         rear      数据方向 ->
-// /* 队列的顺序存储结构(循环队列) */
-
-// can_queue_tag can_tx_queue;
-// can_queue_tag can_rx_queue;
-
-// /****************************************************
-//  *  函 数 名:init_queue
-//  *  函数功能:初始化队列
-//  *  入口参数:无
-//  *  说    明:
-//  ****************************************************/
-// void queue_init(p_can_queue_tag p_queue)
-// {                                      /* 构造一个空队列Q */
-//     p_queue->head = p_queue->tail = 0; /*空队列*/
-//     p_queue->count                = 0;
-// }
-
-// /****************************************************
-//  *  函 数 名:is_queue_empty
-//  *  函数功能:查询队列是否为空
-//  *  入口参数:Q 队列
-//  *  说    明:空队列,返回SUCCESS;否则返回ERROR
-//  ****************************************************/
-// uint8_t queue_empty(p_can_queue_tag p_queue)
-// { /* 若*/
-//     return p_queue->count == 0;
-// }
-
-// uint8_t queue_full(p_can_queue_tag p_queue)
-// { /* 若*/
-//     return p_queue->count == MAX_QSIZE;
-// }
-
-// /****************************************************
-//  *  函 数 名:queue_length
-//  *  函数功能:初始化长度
-//  *  入口参数:Q 队列
-//  *  说    明:
-//  ****************************************************/
-// uint16_t queue_length(can_queue_tag p_queue)
-// { /* 返回Q的元素个数,即队列的长度 */
-//     return (p_queue.tail - p_queue.head + MAX_QSIZE) % MAX_QSIZE;
-// }
-
-// /****************************************************
-//  *  函 数 名:get_head
-//  *  函数功能:获取对头数据
-//  *  入口参数:Q 队列
-//  *  说    明:
-//  ****************************************************/
-// uint8_t get_head(
-//     can_queue_tag *p_queue,
-//     pdu_tag       *e)
-// {                                       /* 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR*/
-//     if (p_queue->head == p_queue->tail) /* 队列空 */
-//         return Q_ERR;
-//     *e = p_queue->can_message[p_queue->head];
-
-//     p_queue->head = (p_queue->head + 1) % MAX_QSIZE;
-//     return Q_OK;
-// }
-
-// /****************************************************
-//  *  函 数 名:insert_queue
-//  *  函数功能:队列插入数据
-//  *  入口参数:Q 待插入队列    e 待插入数据
-//  *  说    明:
-//  ****************************************************/
-// QUEUE_STATUS en_queue(p_can_queue_tag p_queue,
-//                       pdu_tag         data)
-// {
-//     if (queue_full(p_queue))
-//         return Q_FULL;
-//     p_queue->count++;
-//     p_queue->can_message[p_queue->head] = data;
-//     p_queue->head                       = (p_queue->head + 1) % MAX_QSIZE;
-//     return Q_OK;
-//     /* 插入元素e为Q的新的队尾元素 */
-//     // if ((p_queue->tail + 1) % MAX_QSIZE == p_queue->head) /* 队列满 */
-//     //     return ERROR;
-//     // p_queue->can_message[p_queue->tail] = e;
-//     // p_queue->tail                       = (p_queue->tail + 1) % MAX_QSIZE;
-//     // return SUCCESS;
-// }
-
-// QUEUE_STATUS de_queue(p_can_queue_tag p_queue,
-//                       pdu_tag         data)
-// { /* 插入元素e为Q的新的队尾元素 */
-//     // if ((p_queue->tail + 1) % MAX_QSIZE == p_queue->head) /* 队列满 */
-//     //     return ERROR;
-//     // p_queue->can_message[p_queue->tail] = e;
-//     // p_queue->tail                       = (p_queue->tail + 1) % MAX_QSIZE;
-//     // return SUCCESS;
-//     if (queue_empty(p_queue))
-//         return Q_EMPTY;
-//     data          = p_queue->can_message[p_queue->tail];
-//     p_queue->tail = (p_queue->tail + 1) % MAX_QSIZE;
-//     p_queue->count--;
-
-//     return Q_OK;
-// }

+ 0 - 118
hal/queue.h111

@@ -1,118 +0,0 @@
-#ifndef __QUEUE_H
-#define __QUEUE_H
-#include "stdio.h"
-
-#define MAX_QSIZE (60u) /* 最大队列长度 */
-
-typedef unsigned long long u64;
-
-typedef struct
-{
-    struct
-    {
-        uint8_t ide;
-        uint8_t rtr;
-        uint8_t dlc;
-    } reg;
-    union
-    {
-        uint32_t r;
-        struct
-        {
-            uint8_t sa : 8;
-            uint8_t ps : 8;
-            uint8_t pf : 8;
-            uint8_t dp : 1;
-            uint8_t r  : 1;
-            uint8_t p  : 3;
-        } b;
-    } id;
-    union
-    {
-        uint8_t  u8_buf[8];
-        uint16_t u16_buf[4];
-        uint32_t u32_buf[2];
-        u64      u64_buf;
-    } data;
-} pdu_tag, *p_pdu_tag;
-
-typedef struct
-{
-    uint8_t  buf[8];
-    uint32_t can_id;
-} CanData_TypeDef;
-
-typedef struct
-{
-    uint16_t head;
-    uint16_t tail;
-    uint16_t count;
-    pdu_tag  can_message[MAX_QSIZE];
-} can_queue_tag, *p_can_queue_tag;
-
-typedef enum
-{
-    Q_OK,
-    Q_ERR,
-    Q_FULL,
-    Q_EMPTY,
-} QUEUE_STATUS;
-
-#define QUEUE_ENTRY(type, size) \
-    struct                      \
-    {                           \
-        uint8_t head;           \
-        uint8_t tail;           \
-        uint8_t count;          \
-        type    message[size];  \
-    }
-
-#define QUEUE_INIT(name)                 \
-    {                                    \
-        (name)->head = (name)->tail = 0; \
-        (name)->count               = 0; \
-    }
-
-/* List functions. */
-#define QUEUE_EMPTY(name) ((name)->count == 0)
-#define QUEUE_MAX(name)   ((sizeof((name)->message)) / (sizeof((name)->message[0])))
-#define QUEUE_FULL(name)  ((name)->count == ((sizeof((name)->message)) / (sizeof((name)->message[0]))))
-#define EN_QUEUE(name, field, ret)                                                \
-    do                                                                            \
-    {                                                                             \
-        if (QUEUE_FULL(name))                                                     \
-        {                                                                         \
-            ret = Q_FULL;                                                         \
-        }                                                                         \
-        else                                                                      \
-        {                                                                         \
-            (name)->count++;                                                      \
-            (name)->message[(name)->head] = field;                                \
-            (name)->head                  = ((name)->head + 1) % QUEUE_MAX(name); \
-            ret                           = Q_OK;                                 \
-        }                                                                         \
-    } while (0)
-
-#define DE_QUEUE(name, field, ret)                               \
-    do                                                           \
-    {                                                            \
-        if (QUEUE_EMPTY(name))                                   \
-        {                                                        \
-            ret = Q_EMPTY;                                       \
-        }                                                        \
-        else                                                     \
-        {                                                        \
-            field        = (name)->message[(name)->tail];        \
-            (name)->tail = ((name)->tail + 1) % QUEUE_MAX(name); \
-            (name)->count--;                                     \
-            ret = Q_OK;                                          \
-        }                                                        \
-    } while (0)
-
-void         queue_init(p_can_queue_tag);  // 初始化队列
-uint8_t      queue_empty(p_can_queue_tag); // 查询队列是否为空
-uint8_t      queue_full(p_can_queue_tag);
-uint8_t      get_head(can_queue_tag *p_queue, pdu_tag *data); // 获取对头数据
-QUEUE_STATUS en_queue(p_can_queue_tag, pdu_tag);              // 队列插入数据
-QUEUE_STATUS de_queue(p_can_queue_tag, pdu_tag);
-#endif

+ 2 - 2
ble/ble_core.c → modules/ble/ble_core.c

@@ -2,9 +2,9 @@
  *                                          Include                                              *
  ************************************************************************************************/
 #include "ble_core.h"
-#include "hal_systick.h"
-#include "hal_uart.h"
 #include "stm32f4xx_gpio.h"
+#include "systick.h"
+#include "uart.h"
 #include <stdint.h>
 /************************************************************************************************
  *                                          Config                                                *

+ 50 - 0
modules/ble/ble_core.h

@@ -0,0 +1,50 @@
+#ifndef __BLE_CORE_H
+#define __BLE_CORE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __BLE_CORE_H_GLOBAL
+#define __BLE_CORE_H_EXTERN
+#else
+#define __BLE_CORE_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                           *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                         *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                             *
+ ************************************************************************************************/
+
+#include "uart.h"
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#define BT_BUSY_IND_PORT GPIOB
+#define BT_BUSY_IND_PIN  GPIO_Pin_12
+#define BT_CONN_IND_PORT GPIOB
+#define BT_CONN_IND_PIN  GPIO_Pin_13
+#define BT_RST_PORT      GPIOC
+#define BT_RST_PIN       GPIO_Pin_2
+
+void ble_init(void);
+void ble_send(void);
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __BLE_CORE_H

+ 25 - 36
dev/dev_can.c → modules/can/dev_can.c

@@ -1,10 +1,11 @@
 #include "dev_can.h"
 #include "J1939.H"
+#include "can.h"
 #include "core_cmFunc.h"
+#include "dev_can_cali.h"
 #include "dev_can_id.h"
-#include "dev_iap.h"
-#include "hal_can.h"
-#include "hal_conf.h"
+#include "hal_math.h"
+#include "iap.h"
 #include "queue.h"
 #include <stdint.h>
 #include <stdlib.h>
@@ -18,31 +19,6 @@ uint8_t PGN_00B000H_Ary[20] = {0};
 
 extern void j1939_msg_push_queue(p_pdu_tag rec_msg);
 
-double pow_branch(double x, long long N)
-{
-    double ans = 1.0;
-    // 贡献的初始值为 x
-    double x_contribute = x;
-    // 在对 N 进行二进制拆分的同时计算答案
-    while (N > 0)
-    {
-        if (N % 2 == 1)
-        {
-            // 如果 N 二进制表示的最低位为 1,那么需要计入贡献
-            ans *= x_contribute;
-        }
-        // 将贡献不断地平方
-        x_contribute *= x_contribute;
-        // 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
-        N = N >> 1;
-    }
-    return ans;
-}
-double pow(double x, double N)
-{
-    return N >= 0 ? pow_branch(x, N) : 1.0 / pow_branch(x, -N);
-}
-
 uint8_t recv_can_id = 0;
 
 static uint64_t base_data;
@@ -51,7 +27,7 @@ void data_bit_move(uint8_t start_bit, uint8_t bit_len, uint64_t data)
 {
     uint64_t mask        = 0;
     uint64_t source_data = (uint64_t)data;
-    mask                 = pow(2, bit_len) - 1;
+    mask                 = hal_pow(2, bit_len) - 1;
     base_data |= (mask & source_data) << start_bit;
 }
 
@@ -104,7 +80,7 @@ uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
 static uint8_t can_tx_frame(pdu_tag can_message)
 {
     uint8_t result = CAN_TxStatus_NoMailBox;
-    result         = hal_can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
+    result         = can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
     return result;
 }
 
@@ -203,7 +179,7 @@ void can_rx_callback(CanRxMsg rx_message)
                 data.reg.dlc = rx_message.DLC;
                 memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
 
-                if (data.id.b.pf == g_can_iap)
+                if (data.id.b.pf == CAN_IAP)
                 {
                     en_queue(&can_rx_queue, data, result);
                 }
@@ -258,14 +234,15 @@ void can_rx_update(pdu_tag rec_msg)
     }
 }
 
-#define ota_id 0xDF
 can_rx_tab can_tab[] = {
     0x18011802,
     can_rx_param,
     0x18011801,
     can_rx_ctl,
-    ota_id,
+    CAN_IAP,
     iap_rec_handler,
+    CAN_CALI,
+    can_cali_msg_analysis,
 
 };
 
@@ -697,6 +674,17 @@ void can_tx_0x18190007_message(void)
     push_can_message_to_queue(0x18190005, 8, data);
 }
 
+void can_tx_0x18190009_message(void)
+{
+    uint8_t data[8] = {0};
+    data_bit_move(0, 16, 0xDFFF);
+    data_bit_move(16, 16, 0xF000);
+    data_bit_move(32, 16, 0xE800);
+    data_bit_move(48, 16, 0xE13D);
+    product_array(data);
+    push_can_message_to_queue(0x18190005, 0, data);
+}
+
 void send_message(void)
 {
     static volatile uint8_t times = 0;
@@ -763,16 +751,17 @@ void send_message(void)
 
 void dev_can_network_init(void)
 {
-    hal_can_init();
+    can_init();
     queue_init(&can_rx_queue);
     queue_init(&can_tx_queue);
-    hal_can_rx_back_init(can_rx_callback);
-    hal_can_tx_back_init(can_tx_callback);
+    can_rx_back_init(can_rx_callback);
+    can_tx_back_init(can_tx_callback);
 }
 
 void can_process(void)
 {
     can_rx_process();
     // send_message();
+    // can_tx_0x18190009_message();
     can_start_send();
 }

+ 1 - 2
dev/dev_can.h → modules/can/dev_can.h

@@ -1,8 +1,8 @@
 #ifndef __APP_CAN_H
 #define __APP_CAN_H
 
-#include "hal_conf.h"
 #include "queue.h"
+#include "stm32f4xx_can.h"
 
 #define CAN_PGN_PF 0xFFFF00
 #define CAN_PGN_PS 0xFF00
@@ -20,7 +20,6 @@ typedef struct
 #define ARR_SIZE(a) sizeof(a) / sizeof((a)[0])
 
 // #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
-
 void    can_rx_callback(CanRxMsg rx_message);
 void    can_tx_callback();
 void    can_process(void);

+ 165 - 0
modules/can/dev_can_cali.c

@@ -0,0 +1,165 @@
+#include "dev_can_cali.h"
+#include "dev_can_id.h"
+#include "queue.h"
+#include <stdint.h>
+
+enum
+{
+    cal_ram = 0,
+    cal_eeprom,
+    cal_io,
+};
+
+enum
+{
+    read_cal = 0,
+    write_cal,
+};
+
+enum
+{
+    cali_data_len_32bit = 0,
+    cali_data_len_16bit,
+    cali_data_len_8bit,
+    cali_data_len_1bit,
+    cali_data_len_32bit_float,
+};
+
+typedef union cali_msg
+{
+    uint8_t  u8_data[8];
+    uint64_t u64_data;
+    struct
+    {
+        uint64_t cmd_type  : 4;
+        uint64_t data_len  : 3;
+        uint64_t rw        : 1;
+        uint64_t en        : 1;
+        uint64_t rsed      : 3;
+        uint64_t data_id   : 12;
+        uint64_t data      : 32;
+        uint64_t check_sum : 8;
+    } bit;
+} cali_msg_type;
+
+typedef union
+{
+    float    f_data;
+    uint8_t  u8_data[4];
+    uint32_t u32_data;
+} float_convert;
+
+typedef struct
+{
+    uint8_t  en_cal;  // 自动退出标定;
+    uint8_t *val_cal; // 标定使能位
+    uint32_t count;
+    uint32_t over_time;
+} cali_connect_type;
+
+static cali_connect_type can_cal_mgt[100];
+
+// 标定自动断开设置
+static uint8_t can_cali_conneet_set(uint8_t index, uint32_t over_time, uint8_t *p)
+{
+    if (index < ARR_SIZE(can_cal_mgt))
+    {
+        can_cal_mgt[index].en_cal    = 1;
+        can_cal_mgt[index].val_cal   = p;
+        can_cal_mgt[index].count     = 0;
+        can_cal_mgt[index].over_time = over_time;
+    }
+    return 0;
+}
+
+// 标定自动断开管理
+void can_cal_connect_mgt(void)
+{
+    uint8_t i;
+    for (i = 0; i < ARR_SIZE(can_cal_mgt); i++)
+    {
+        if (can_cal_mgt[i].en_cal)
+        {
+            if (++can_cal_mgt[i].count >= can_cal_mgt[i].over_time)
+            {
+                *can_cal_mgt[i].val_cal = 0; // 超时清除
+                can_cal_mgt[i].en_cal   = 0;
+            }
+        }
+        else
+        {
+            *can_cal_mgt[i].val_cal = 0; // 命令清除
+        }
+    }
+}
+
+uint8_t led_en       = 0;
+uint8_t led_en_value = 0;
+
+void cal_write(pdu_tag rec_msg)
+{
+    pdu_tag       tx_msg;
+    cali_msg_type rx_data;
+    float_convert data;
+    rx_data.u64_data = rec_msg.data.u64_buf;
+
+    switch (rx_data.bit.data_id)
+    {
+    case 1:
+        if ((rx_data.bit.cmd_type == cal_ram) && (rx_data.bit.data_len == cali_data_len_1bit))
+        {
+            if (rx_data.bit.en == 1)
+            {
+                led_en = 1;
+            }
+            else
+            {
+                led_en = 0;
+            }
+            led_en_value = (uint8_t)rx_data.bit.data;
+            can_cali_conneet_set(rx_data.bit.data_id, 2000, &led_en);
+        }
+        break;
+    default:
+        break;
+    }
+}
+
+void cal_read(pdu_tag rec_msg)
+{
+    pdu_tag       tx_msg;
+    cali_msg_type rx_data, tx_data;
+    float_convert data;
+    rx_data.u64_data = rec_msg.data.u64_buf;
+    tx_data.u64_data = rec_msg.data.u64_buf;
+    switch (rx_data.bit.data_id)
+    {
+    case 1:
+        if ((rx_data.bit.cmd_type == cal_ram) && (rx_data.bit.data_len == cali_data_len_1bit))
+        {
+            rx_data.bit.en == led_en;
+            tx_data.bit.data = led_en_value;
+        }
+        break;
+    default:
+        break;
+    }
+
+    tx_msg.data.u64_buf = tx_data.u64_data;
+    push_can_message_to_queue(get_new_can_id(CAN_CALI), 8, tx_msg.data.u8_buf);
+}
+
+void can_cali_msg_analysis(pdu_tag rec_msg)
+{
+    cali_msg_type cal_data;
+    cal_data.u64_data = rec_msg.data.u64_buf;
+
+    if (cal_data.bit.rw == read_cal)
+    {
+        cal_read(rec_msg);
+    }
+    else if (cal_data.bit.rw == write_cal)
+    {
+        cal_write(rec_msg);
+    }
+}

+ 9 - 19
dev/dev_uart4.h → modules/can/dev_can_cali.h

@@ -1,16 +1,13 @@
-#ifndef __DEV_UART4_H
-#define __DEV_UART4_H
+#ifndef __DEV_CAN_CALI_H
+#define __DEV_CAN_CALI_H
 
-#include "hal_uart4.h"
-#include <stdint.h>
 #ifdef __cplusplus
-extern 'C'
-{
+extern "C" {
 #endif
-#ifdef __DEV_UART4_H_GLOBAL
-#define __DEV_UART4_H_EXTERN
+#ifdef __DEV_CAN_CALI_H_GLOBAL
+#define __DEV_CAN_CALI_H_EXTERN
 #else
-#define __DEV_UART4_H_EXTERN extern
+#define __DEV_CAN_CALI_H_EXTERN extern
 #endif
 
 /************************************************************************************************
@@ -28,19 +25,12 @@ extern 'C'
 /************************************************************************************************
  *                                          Includes                                             *
  ************************************************************************************************/
-
+#include "dev_can.h"
 /************************************************************************************************
  *                                          Defines                                              *
  ************************************************************************************************/
-#define UART_ARR_SIZE(a) sizeof(a) / sizeof((a)[0])
-
-    typedef struct
-    {
-        uint8_t cmd;
-        uint8_t (*p_func)(uart_type *data);
-    } com_protocol_type;
 
-    void uart_task(void);
+void can_cali_msg_analysis(pdu_tag rec_msg);
 
 /************************************************************************************************
  *                                          Defines                                              *
@@ -49,4 +39,4 @@ extern 'C'
 };
 #endif
 
-#endif // __DEV_UART4_H
+#endif // __DEV_CAN_CALI_H

+ 23 - 1
dev/dev_can_id.c → modules/can/dev_can_id.c

@@ -2,6 +2,7 @@
  *                                          Include                                              *
  ************************************************************************************************/
 #include "dev_can_id.h"
+#include "dev_can.h"
 #include <stdint.h>
 
 /************************************************************************************************
@@ -27,7 +28,6 @@ union
 uint32_t g_ps         = 0xF4;
 uint32_t g_can_id     = 0x18F011F4;
 uint32_t g_can_ota_id = 0x18DFE1F4;
-uint8_t  g_can_iap    = 0xDF;
 
 uint32_t get_new_can_id(uint8_t pf)
 {
@@ -48,6 +48,28 @@ uint32_t set_new_can_id(uint16_t pgn)
     return new_can_id.r;
 }
 
+uint8_t can_id_set(pdu_tag rec_msg)
+{
+    uint8_t ps = 0;
+    if (rec_msg.data.u8_buf[0] == 0x01)
+    {
+        g_ps                   = rec_msg.data.u8_buf[1];
+        rec_msg.data.u8_buf[1] = 1;
+    }
+    else
+    {
+        rec_msg.data.u8_buf[1] = 0;
+    }
+
+    ps              = rec_msg.id.b.ps;
+    rec_msg.id.b.ps = rec_msg.id.b.ps;
+    rec_msg.id.b.sa = ps;
+
+    push_can_message_to_queue(rec_msg.id.r, 2, rec_msg.data.u8_buf);
+
+    return 0;
+}
+
 uint32_t get_ota_id(uint8_t pf)
 {
     new_can_id.r    = g_can_ota_id;

+ 52 - 0
modules/can/dev_can_id.h

@@ -0,0 +1,52 @@
+#ifndef __DEV_CAN_ID_H
+#define __DEV_CAN_ID_H
+
+#include "queue.h"
+#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __DEV_CAN_ID_H_GLOBAL
+#define __DEV_CAN_ID_H_EXTERN
+#else
+#define __DEV_CAN_ID_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                           *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                         *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+extern uint32_t g_ps;
+extern uint32_t g_can_id;
+extern uint32_t g_can_ota_id;
+
+#define CAN_IAP  0xDF
+#define CAN_CALI 0xFF
+
+uint32_t get_new_can_id(uint8_t pf);
+uint32_t set_new_can_id(uint16_t pgn);
+uint32_t get_ota_id(uint8_t pf);
+uint8_t  can_id_set(pdu_tag rec_msg);
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __DEV_CAN_ID_H

+ 3 - 3
cli/cli.c → modules/cli/cli.c

@@ -1,11 +1,11 @@
 #include "cli.h"
 #include "cli_inter_actor.h"
 #include "cli_inter_preter.h"
-#include "hal_flash.h"
+#include "flash.h"
 #include <stdint.h>
 #include <stdio.h>
-// #include "hal_led.h"
-// #include "hal_usart.h"
+// #include "led.h"
+// #include "usart.h"
 // #include <stdio.h>
 
 #define CLI_ANSI_ENABLE 1

+ 0 - 0
cli/cli.h → modules/cli/cli.h


+ 0 - 0
cli/cli_inter_actor.c → modules/cli/cli_inter_actor.c


+ 0 - 0
cli/cli_inter_actor.h → modules/cli/cli_inter_actor.h


+ 0 - 0
cli/cli_inter_preter.c → modules/cli/cli_inter_preter.c


+ 9 - 12
cli/cli_inter_preter.h → modules/cli/cli_inter_preter.h

@@ -18,18 +18,15 @@ typedef void (*command_action)();
 
 #define command_entry_action_with_detail(name, action, arg_type, desp, arg_desp) \
     {                                                                            \
-        (name), (command_action)(action), (arg_type), (desp), (arg_desp)         \
-    }
-
-#define command_entry_sub_menu(name, sub_menu, desp)                                              \
-    {                                                                                             \
-        (name), (command_action)NULL, (const char *)(sub_menu), (desp), (const char *const *)NULL \
-    }
-
-#define command_entry_terminator()                                                                                  \
-    {                                                                                                               \
-        (const char *)NULL, (command_action)NULL, (const char *)NULL, (const char *)NULL, (const char *const *)NULL \
-    }
+        (name), (command_action)(action), (arg_type), (desp), (arg_desp)}
+
+#define command_entry_sub_menu(name, sub_menu, desp) \
+    {                                                \
+        (name), (command_action)NULL, (const char *)(sub_menu), (desp), (const char *const *)NULL}
+
+#define command_entry_terminator() \
+    {                              \
+        (const char *)NULL, (command_action)NULL, (const char *)NULL, (const char *)NULL, (const char *const *)NULL}
 
 typedef const struct
 {

+ 0 - 1
app/app_eeprom.c → modules/eeprom/app_eeprom.c

@@ -1,5 +1,4 @@
 #include "app_eeprom.h"
-#include <stdint.h>
 #include <string.h>
 
 uint8_t app_eeprom_read_buf[520];

+ 3 - 1
app/app_eeprom.h → modules/eeprom/app_eeprom.h

@@ -1,11 +1,13 @@
 #ifndef __APP_EEPROM_H
 #define __APP_EEPROM_H
 
-#include "dev_conf.h"
+#include "dev_at24cxx.h"
+#include <stdint.h>
 
 extern uint8_t app_eeprom_read_buf[520];
 extern uint8_t app_eeprom_write_buf[520];
 extern uint8_t read_buf[50];
+
 #define APP_EEPROM_READ_STEP  PAGESIZE
 #define APP_EEPROM_WRITE_STEP PAGESIZE
 

+ 5 - 5
dev/dev_at24cxx.c → modules/eeprom/dev_at24cxx.c

@@ -572,15 +572,15 @@ int32_t dev_at24cxx_read_bytes(uint8_t addr, uint8_t *buf, uint8_t size)
 {
     if (1 == size)
     {
-        return dev_at24cxx_read_one_byte(HAL_I2C1_ADDRESS, addr, 1, buf);
+        return dev_at24cxx_read_one_byte(I2C1_ADDRESS, addr, 1, buf);
     }
     else if (2 == size)
     {
-        return dev_at24cxx_read_two_bytes(HAL_I2C1_ADDRESS, addr, 1, buf, 1);
+        return dev_at24cxx_read_two_bytes(I2C1_ADDRESS, addr, 1, buf, 1);
     }
     else
     {
-        return dev_at24cxx_read_more_bytes(HAL_I2C1_ADDRESS, addr, 1, buf, size);
+        return dev_at24cxx_read_more_bytes(I2C1_ADDRESS, addr, 1, buf, size);
     }
 }
 
@@ -588,10 +588,10 @@ int32_t dev_at24cxx_write_bytes(uint8_t addr, uint8_t *buf, uint8_t size)
 {
     if (1 == size)
     {
-        return dev_at24cxx_write_one_byte(HAL_I2C1_ADDRESS, addr, 1, *buf);
+        return dev_at24cxx_write_one_byte(I2C1_ADDRESS, addr, 1, *buf);
     }
     else
     {
-        return dev_at24cxx_write_more_bytes(HAL_I2C1_ADDRESS, addr, 1, buf, size);
+        return dev_at24cxx_write_more_bytes(I2C1_ADDRESS, addr, 1, buf, size);
     }
 }

+ 1 - 1
dev/dev_at24cxx.h → modules/eeprom/dev_at24cxx.h

@@ -1,7 +1,7 @@
 #ifndef __DEV_AT24CXX_H
 #define __DEV_AT24CXX_H
 
-#include "hal_i2c1.h"
+#include "i2c1.h"
 
 #define bool uint8_t
 

+ 0 - 0
modules/esp8266/esp8266_cmd.c


+ 13 - 0
modules/esp8266/esp8266_cmd.h

@@ -0,0 +1,13 @@
+
+// #ifndef __ESP8266_CMD_H
+// #define __ESP8266_CMD_H
+
+// #include <stdint.h>
+// #pragma pack(1)
+// typedef struct
+// {
+//     uint8_t *cmd;
+//     uint8_t  len;
+// } esp8266_rsp_cmd;
+
+// #endif // __ESP8266_CMD_H

+ 59 - 0
modules/esp8266/esp8266_core.c

@@ -0,0 +1,59 @@
+
+
+// #include "esp8266_core.h"
+// #include "hal_systick.h"
+// #include <stdint.h>
+// #include <stdio.h>
+// #include <string.h>
+
+// uint8_t *cmd_ok       = "AT";
+// uint8_t *cmd_sta      = "AT+CWMODE=1";
+// uint8_t *cmd_ap       = "AT+CWMODE=2";
+// uint8_t *cmd_ap_sta   = "AT+CWMODE=3";
+// uint8_t *cmd_rst      = "AT+RST";
+// uint8_t *cmd_join     = "AT+CWJAP=\"ufi_24913\",\"1234567890\"";
+// uint8_t *cmd_cipmux   = "AT+CIPMUX=0";
+// uint8_t *cmd_cipstart = "AT+CIPSTART=\"TCP\",\"192.168.0.245\",\"8086\"";
+// uint8_t *cmd_cipmode  = "AT+CIPMODE=1";
+// uint8_t *cmd_cipsend  = "AT+CIPSEND";
+
+// esp8266_cmd_cache g_esp8266_cache;
+
+// uint8_t esp8266_send_cmd(uint8_t *cmd, uint8_t *ack, uint32_t wait_time)
+// {
+//     g_esp8266_cache.is_used = 1;
+//     g_esp8266_cache.cmd     = cmd;
+//     memcpy(g_esp8266_cache.cmd, cmd, sizeof(cmd));
+//     g_esp8266_cache.tick   = 0;
+//     g_esp8266_cache.p_func = 0;
+//     return 0;
+// }
+
+// uint8_t esp8266_tx_loop(void)
+// {
+//     static uint8_t count = 0;
+
+//     if (g_esp8266_cache.is_used == 1)
+//     {
+//         if (g_esp8266_cache.tick == 0)
+//         {
+//             g_esp8266_cache.tick = get_systick_ms();
+//             count                = 0;
+//         }
+
+//         if (count == 0 || get_systick_ms() - g_esp8266_cache.tick > 3000)
+//         {
+//             for (uint8_t i = 0; i < 10; i++)
+//             {
+//                 if (g_)
+//             }
+//         }
+//     }
+// }
+
+// void esp8266_task(void)
+// {
+//     static uint8_t status = 0;
+
+//     esp8266_send_cmd(cmd_ok);
+// }

+ 50 - 0
modules/esp8266/esp8266_core.h

@@ -0,0 +1,50 @@
+// #ifndef __ESP8266_CORE_H
+// #define __ESP8266_CORE_H
+
+// #include <stdint.h>
+// #ifdef __cplusplus
+// extern 'C'
+// {
+// #endif
+// #ifdef __ESP8266_CORE_H_GLOBAL
+// #define __ESP8266_CORE_H_EXTERN
+// #else
+// #define __ESP8266_CORE_H_EXTERN extern
+// #endif
+
+//     /************************************************************************************************
+//      *                                          Version                                              *
+//      ************************************************************************************************/
+
+//     /************************************************************************************************
+//      *                                          How to use                                           *
+//      ************************************************************************************************/
+
+//     /************************************************************************************************
+//      *                                          Enable config                                         *
+//      ************************************************************************************************/
+
+//     /************************************************************************************************
+//      *                                          Includes                                             *
+//      ************************************************************************************************/
+
+//     /************************************************************************************************
+//      *                                          Defines                                              *
+//      ************************************************************************************************/
+//     typedef struct
+//     {
+//         uint8_t  is_used; // 0 未使用,1,使用
+//         uint8_t *cmd;
+//         uint8_t *ack;
+//         uint32_t tick;
+//         uint16_t wait_time;
+//         int32_t (*p_func)(uint8_t *cmd, uint8_t *ack, uint16_t wait_time)
+//     } esp8266_cmd_cache;
+// /************************************************************************************************
+//  *                                          Defines                                              *
+//  ************************************************************************************************/
+// #ifdef __cplusplus
+// };
+// #endif
+
+// #endif // __ESP8266_CORE_H

+ 85 - 0
modules/gpio/key.c

@@ -0,0 +1,85 @@
+#include "key.h"
+#include "gpio.h"
+#include "multi_button.h"
+
+struct Button *btn_press;
+
+struct Button btn_key_up;
+struct Button btn_key0;
+struct Button btn_key1;
+struct Button btn_key2;
+
+key_button_t key_button;
+
+#define KEY_UP_ACTIVE_LEVEL 1
+#define KEY0_ACTIVE_LEVEL   0
+#define KEY1_ACTIVE_LEVEL   0
+#define KEY2_ACTIVE_LEVEL   0
+
+uint8_t key_read_button(uint8_t button_id)
+{
+    // you can share the GPIO read function with multiple Buttons
+    switch (button_id)
+    {
+    case 0:
+        return GPIO_ReadInputDataBit(GPIO_KEY_UP_PORT, GPIO_KEY_UP_PIN);
+    case 1:
+        return GPIO_ReadInputDataBit(GPIO_KEY0_PORT, GPIO_KEY0_PIN);
+    case 2:
+        return GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1_PIN);
+    case 3:
+        return GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2_PIN);
+    default:
+        return 0;
+    }
+}
+
+void key_press_down(void *btn)
+{
+    if (NULL == btn)
+    {
+        key_button.i = 0;
+    }
+    else
+    {
+        btn_press = (struct Button *)btn;
+        if (btn_press == &btn_key_up)
+        {
+            key_button.key_state.bits.key_up_state_flag = 1;
+        }
+        if (btn_press == &btn_key0)
+        {
+            key_button.key_state.bits.key0_state_flag = 1;
+        }
+        if (btn_press == &btn_key1)
+        {
+            key_button.key_state.bits.key1_state_flag = 1;
+        }
+        if (btn_press == &btn_key2)
+        {
+            key_button.key_state.bits.key2_state_flag = 1;
+        }
+    }
+}
+
+void key_button_init(void)
+{
+    button_init(&btn_key_up, key_read_button, KEY_UP_ACTIVE_LEVEL, 0);
+    button_init(&btn_key0, key_read_button, KEY0_ACTIVE_LEVEL, 1);
+    button_init(&btn_key1, key_read_button, KEY1_ACTIVE_LEVEL, 2);
+    button_init(&btn_key2, key_read_button, KEY2_ACTIVE_LEVEL, 3);
+
+    button_attach(&btn_key_up, PRESS_DOWN, key_press_down);
+    button_attach(&btn_key0, PRESS_DOWN, key_press_down);
+    button_attach(&btn_key1, PRESS_DOWN, key_press_down);
+    button_attach(&btn_key2, PRESS_DOWN, key_press_down);
+
+    button_start(&btn_key_up);
+    button_start(&btn_key0);
+    button_start(&btn_key1);
+    button_start(&btn_key2);
+}
+void key_button_task()
+{
+    button_ticks();
+}

+ 70 - 0
modules/gpio/key.h

@@ -0,0 +1,70 @@
+#ifndef __KEY_H
+#define __KEY_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __KEY_H_GLOBAL
+#define __KEY_H_EXTERN
+#else
+#define __KEY_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+
+#include <stdint.h>
+
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+typedef union
+{
+    struct
+    {
+        uint8_t key_up_state_flag : 1;
+        uint8_t key0_state_flag   : 1;
+        uint8_t key1_state_flag   : 1;
+        uint8_t key2_state_flag   : 1;
+    } bits;
+    uint8_t key_all_state;
+} key_state_t;
+
+typedef struct
+{
+    key_state_t key_state;
+    uint8_t     i;
+} key_button_t;
+
+extern key_button_t key_button;
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void key_button_init(void);
+void key_button_task(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __KEY_H

+ 42 - 0
modules/gpio/led.c

@@ -0,0 +1,42 @@
+#include "led.h"
+#include "gpio.h"
+
+led_t led; // 自定义led结构体;
+
+// 驱动led开关
+void led_on_off(uint8_t leds, uint8_t on_off)
+{
+    switch (leds)
+    {
+    case 0:
+    {
+        if (0x00 == on_off)
+        {
+            // GPIO_LED0(0x00);
+            led.led_state.bits.led0_state_flag = 0;
+        }
+        else
+        {
+            // GPIO_LED0(0x01);
+            led.led_state.bits.led0_state_flag = 1;
+        }
+        break;
+    }
+    case 1:
+    {
+        if (0x00 == on_off)
+        {
+            GPIO_LED1(0x00);
+            led.led_state.bits.led1_state_flag = 0;
+        }
+        else
+        {
+            GPIO_LED1(0x01);
+            led.led_state.bits.led1_state_flag = 1;
+        }
+        break;
+    }
+    default:
+        break;
+    }
+}

+ 9 - 9
dev/dev_led.h → modules/gpio/led.h

@@ -1,5 +1,5 @@
-#ifndef __DEV_LED_H
-#define __DEV_LED_H
+#ifndef __LED_H
+#define __LED_H
 
 #include <stdint.h>
 typedef union
@@ -16,16 +16,16 @@ typedef union
         uint8_t led7_state_flag : 1;
     } bits;
     uint8_t led_all_state;
-} dev_led_state_t;
+} led_state_t;
 
 typedef struct
 {
-    dev_led_state_t dev_led_state;
-    uint8_t         i;
-} dev_led_t;
+    led_state_t led_state;
+    uint8_t     i;
+} led_t;
 
-extern dev_led_t dev_led;
+extern led_t led;
 
-void dev_led_on_off(uint8_t leds, uint8_t on_off);
+void led_on_off(uint8_t leds, uint8_t on_off);
 
-#endif // __DEV_LED_H
+#endif // __LED_H

+ 4 - 5
dev/dev_iap.c → modules/iap/iap.c

@@ -1,10 +1,9 @@
 /************************************************************************************************
  *                                          Include                                              *
  ************************************************************************************************/
-#include "dev_iap.h"
-#include "dev_at24cxx.h"
+#include "iap.h"
 #include "dev_can_id.h"
-#include "hal_flash.h"
+#include "flash.h"
 #include "queue.h"
 #include <stdint.h>
 #include <string.h>
@@ -111,7 +110,7 @@ void iap_flag_release(void)
         {
             send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
         }
-        hal_can_msg_tx(0x18000001, CAN_Id_Extended, send_buff, 8);
+        can_msg_tx(0x18000001, CAN_Id_Extended, send_buff, 8);
         CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
     }
 }
@@ -303,5 +302,5 @@ void iap_rec_handler(pdu_tag rec_msg)
         }
     }
 
-    push_can_message_to_queue(get_ota_id(g_can_iap), 8, send_buff);
+    push_can_message_to_queue(get_ota_id(CAN_IAP), 8, send_buff);
 }

+ 45 - 47
dev/dev_iap.h → modules/iap/iap.h

@@ -2,8 +2,7 @@
 #define __DEV_IAP_H
 
 #ifdef __cplusplus
-extern 'C'
-{
+extern "C" {
 #endif
 #ifdef __DEV_IAP_H_GLOBAL
 #define __DEV_IAP_H_EXTERN
@@ -27,12 +26,11 @@ extern 'C'
  *                                          Includes                                             *
  ************************************************************************************************/
 #include "dev_can.h"
-#include "hal_conf.h"
 #include <stdint.h>
 
-    /************************************************************************************************
-     *                                          Defines                                              *
-     ************************************************************************************************/
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
 
 #define UPGRADE_FLAG_DATA_SIZE 4                                                // 升级参数数组元素个数
 #define UPGRADE_APP_DATA_SIZE  4                                                // 升级参数数组元素个数
@@ -61,50 +59,50 @@ extern 'C'
 #define UPGRADE_SIGN 0x5AA5
 #define DEVICE_ID    0x5AA4
 
-    typedef union
-    {
-        uint16_t u8_buf[8];
-        uint16_t u16_buf[4];
-    } u16_u8;
+typedef union
+{
+    uint16_t u8_buf[8];
+    uint16_t u16_buf[4];
+} u16_u8;
 
-    typedef union
-    {
-        uint16_t u8_save[200];
-        uint16_t u16_save[100];
-    } u16save_u8save;
+typedef union
+{
+    uint16_t u8_save[200];
+    uint16_t u16_save[100];
+} u16save_u8save;
 
-    typedef struct
-    {
-        uint16_t       total_packages;
-        uint16_t       current_packages;
-        uint16_t       count_packages;
-        uint16_t       recv_len;
-        uint16_t       crc16;
-        uint16_t       firmware_crc16;
-        uint16_t       firmware_ver;
-        uint16_t       total_bytes;
-        uint16_t       count_bytes;
-        u16_u8         flash_data;
-        u16_u8         recheck_data;
-        u16save_u8save save_data;
-    } iap_type;
-
-    typedef union
+typedef struct
+{
+    uint16_t       total_packages;
+    uint16_t       current_packages;
+    uint16_t       count_packages;
+    uint16_t       recv_len;
+    uint16_t       crc16;
+    uint16_t       firmware_crc16;
+    uint16_t       firmware_ver;
+    uint16_t       total_bytes;
+    uint16_t       count_bytes;
+    u16_u8         flash_data;
+    u16_u8         recheck_data;
+    u16save_u8save save_data;
+} iap_type;
+
+typedef union
+{
+    struct
     {
-        struct
-        {
-            uint16_t sign;        // 首地址 buf[0]
-            uint16_t crc16;       // 中地址 buf[1]
-            uint32_t moving_size; // 高地址 buf[2] buf[3]
-        } param;
-        uint16_t infor_buf[UPGRADE_FLAG_DATA_SIZE];
-    } upgrade_param; // 升级参数使用
-
-    void set_firmver(void);
-    void iap_flag_release(void);
-    void iap_param_init(void);
-    void jump_boot_time_ctrl(void);
-    void iap_rec_handler(pdu_tag rec_msg);
+        uint16_t sign;        // 首地址 buf[0]
+        uint16_t crc16;       // 中地址 buf[1]
+        uint32_t moving_size; // 高地址 buf[2] buf[3]
+    } param;
+    uint16_t infor_buf[UPGRADE_FLAG_DATA_SIZE];
+} upgrade_param; // 升级参数使用
+
+void set_firmver(void);
+void iap_flag_release(void);
+void iap_param_init(void);
+void jump_boot_time_ctrl(void);
+void iap_rec_handler(pdu_tag rec_msg);
 /************************************************************************************************
  *                                          Defines                                              *
  ************************************************************************************************/

+ 14 - 30
app/app_task.c → modules/task/app_task.c

@@ -1,16 +1,14 @@
-#include "app_task.h"
 #include "J1939.H"
 #include "app_eeprom.h"
 #include "ble_core.h"
 #include "cli.h"
 #include "dev_at24cxx.h"
 #include "dev_can.h"
-#include "dev_uart4.h"
-#include "hal_systick.h"
-#include "hal_uart4.h"
-#include "stm32f4xx_tim.h"
+#include "dev_task.h"
+#include "gpio.h"
+#include "key.h"
+#include "uart_task.h"
 #include <stdint.h>
-#include <stdio.h>
 
 uint8_t beep_status = 1;
 
@@ -31,7 +29,7 @@ static uint32_t systick_ms_clock = 0;
 static void app_task_5ms(void)
 {
     uart_task();
-    dev_key_button_task();
+    key_button_task();
     can_process();
     cli_loop();
 }
@@ -70,33 +68,29 @@ static void app_task_10ms(void)
 static void app_task_50ms(void)
 {
     uint8_t buf[200] = {0};
-    if (dev_key.dev_key_state.bits.key_up_state_flag == 1)
+    if (key_button.key_state.bits.key_up_state_flag == 1)
     {
         buf[0] = 10;
         ble_send();
-        // dev_at24cxx_write_bytes(0, &buf[99], 1);
         app_eeprom_write_50_zhuzu();
-        dev_key.dev_key_state.bits.key_up_state_flag = 0;
+        key_button.key_state.bits.key_up_state_flag = 0;
     }
-    if (dev_key.dev_key_state.bits.key0_state_flag == 1)
+    if (key_button.key_state.bits.key0_state_flag == 1)
     {
         app_eeprom_read_50_zhuzu();
-        // printf("%d\r\n", read_buf[33]);
-        dev_key.dev_key_state.bits.key0_state_flag = 0;
+        key_button.key_state.bits.key0_state_flag = 0;
     }
-    if (dev_key.dev_key_state.bits.key1_state_flag == 1)
+    if (key_button.key_state.bits.key1_state_flag == 1)
     {
         dev_at24cxx_read_bytes(0, buf, 1);
-        printf("%d\r\n,%d\r\n", buf[0], buf[0]);
-        dev_key.dev_key_state.bits.key1_state_flag = 0;
+        key_button.key_state.bits.key1_state_flag = 0;
     }
-    if (dev_key.dev_key_state.bits.key2_state_flag == 1)
+    if (key_button.key_state.bits.key2_state_flag == 1)
     {
-        printf("33333333\r\n");
-        HAL_GPIO_BEEP(beep_status);
+        GPIO_BEEP(beep_status);
         beep_status = !beep_status;
 
-        dev_key.dev_key_state.bits.key2_state_flag = 0;
+        key_button.key_state.bits.key2_state_flag = 0;
     }
 }
 
@@ -186,16 +180,6 @@ static void app_task_2000ms(void)
 {
 }
 
-void SysTick_Handler(void)
-{
-    dev_task_clock();
-    dev_systick_decrement();
-    dev_systick_increase();
-    uart4_rx_ticks();
-    systick_handler();
-    jump_boot_time_ctrl();
-}
-
 void app_task_schdule(void)
 {
     if (dev_task_flag.bits.task0_flag)

+ 1 - 1
app/app_task.h → modules/task/app_task.h

@@ -1,7 +1,7 @@
 #ifndef __APP_TASK_H
 #define __APP_TASK_H
 
-#include "dev_conf.h"
+#include <stdint.h>
 
 #define task0_mask 0x0000
 #define task1_mask 0x0001

+ 0 - 0
dev/dev_task.c → modules/task/dev_task.c


+ 0 - 0
dev/dev_task.h → modules/task/dev_task.h


+ 89 - 0
modules/uart/uart_interface.c

@@ -0,0 +1,89 @@
+#include "uart_interface.h"
+#include <uart4.h>
+
+uart_type uart_msg = {
+    .rx_count_u8        = 0,
+    .tx_count_u8        = 0,
+    .rx_finished_flg    = 0,
+    .disconnect_flg     = 0,
+    .rx_over_time       = 5,
+    .rx_over_time_count = 0xFF,
+    .disconnect_count   = 0,
+};
+
+void uart_rx_ticks(void)
+{
+    if (uart_msg.rx_over_time_count != 0xFF)
+    {
+        if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time)
+        {
+            uart_msg.rx_over_time_count = 0xFF;
+            uart_msg.rx_finished_flg    = 1;
+        }
+    }
+
+    if (uart_msg.disconnect_count != 0xFFFF)
+    {
+        if (++uart_msg.disconnect_count >= 2500) // 5000ms
+        {
+            uart_msg.disconnect_count = 0xFFFF;
+            uart_msg.disconnect_flg   = 1;
+        }
+    }
+}
+
+void uart_start_send(uart_type *p_msg)
+{
+    p_msg->tx_count_u8 = 0;
+
+    UART_RXNE_IT_DISABLE();
+    USART_SendData(UART4, p_msg->tx[p_msg->tx_count_u8++]);
+    UART_TXE_IT_ENABLE();
+}
+
+void uart4_it(void)
+{
+    // 接收中断处理
+    if (RESET != USART_GetITStatus(UART4, USART_IT_RXNE))
+    {
+        USART_ClearITPendingBit(UART4, USART_IT_RXNE);
+
+        uart_msg.rx_over_time_count = 0;
+        uart_msg.disconnect_count   = 0;
+        uart_msg.disconnect_flg     = 0;
+
+        if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN)
+        {
+            uart_msg.rx_count_u8 = 0;
+        }
+
+        uart_msg.rx[uart_msg.rx_count_u8++] = USART_ReceiveData(UART4);
+    }
+
+    // 发送中断处理
+    if (RESET != USART_GetITStatus(UART4, USART_IT_TXE))
+    {
+        USART_ClearITPendingBit(UART4, USART_IT_TXE);
+
+        USART_SendData(UART4, uart_msg.tx[uart_msg.tx_count_u8++]);
+
+        if (uart_msg.tx_count_u8 >= uart_msg.tx_len)
+        {
+            USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
+            USART_ITConfig(UART4, USART_IT_TC, ENABLE);
+        }
+    }
+
+    // 发送完成中断
+    if (RESET != USART_GetITStatus(UART4, USART_IT_TC))
+    {
+        USART_ClearITPendingBit(UART4, USART_IT_TC);
+
+        USART_ITConfig(UART4, USART_IT_TC, DISABLE);
+        USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
+    }
+
+    // 发送错误
+    if (RESET != USART_GetITStatus(UART4, USART_IT_ORE))
+        USART_ClearITPendingBit(UART4, USART_IT_ORE);
+}

+ 68 - 0
modules/uart/uart_interface.h

@@ -0,0 +1,68 @@
+#ifndef __UART_INTERFACE_H
+#define __UART_INTERFACE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __UART_INTERFACE_H_GLOBAL
+#define __UART_INTERFACE_H_EXTERN
+#else
+#define __UART_INTERFACE_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+#include "stm32f4xx.h"
+#include "uart4.h"
+#include <stdint.h>
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+#define RX_TX_BUF_LEN          (50)
+#define UART_RXNE_IT_DISABLE() USART_ITConfig(UART4, USART_IT_RXNE, DISABLE)
+#define UART_TXE_IT_ENABLE()   USART_ITConfig(UART4, USART_IT_TXE, ENABLE)
+/************************************************************************************************
+ *                                          Typedefs *
+ ************************************************************************************************/
+typedef struct
+{
+    uint8_t  rx[RX_TX_BUF_LEN];
+    uint8_t  tx[RX_TX_BUF_LEN];
+    uint8_t  rx_len;
+    uint8_t  tx_len;
+    uint8_t  rx_count_u8;
+    uint8_t  tx_count_u8;
+    uint8_t  device_type;
+    uint8_t  rx_finished_flg;
+    uint8_t  disconnect_flg;
+    uint8_t  rx_over_time;
+    uint8_t  rx_over_time_count;
+    uint16_t disconnect_count;
+} uart_type;
+
+/************************************************************************************************
+ *                                          Interfaces *
+ ************************************************************************************************/
+
+extern uart_type uart_msg;
+void             uart_rx_ticks(void);
+void             uart_start_send(uart_type *p_msg);
+void             uart4_it(void);
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __UART_INTERFACE_H

+ 2 - 3
dev/dev_uart_table.c → modules/uart/uart_table.c

@@ -1,8 +1,7 @@
 
 
-#include "dev_uart_table.h"
-#include "dev_can.h"
-#include "hal_uart4.h"
+#include "uart_table.h"
+#include "utils.h"
 #include <stdint.h>
 
 static void read_1000(uint8_t memoft, uint8_t *data, uint8_t *read_bytes)

+ 55 - 0
modules/uart/uart_table.h

@@ -0,0 +1,55 @@
+#ifndef __UART_TABLE_H
+#define __UART_TABLE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __UART_TABLE_H_GLOBAL
+#define __UART_TABLE_H_EXTERN
+#else
+#define __UART_TABLE_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+#include "uart_interface.h"
+#include <stdint.h>
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+
+typedef struct
+{
+    uint16_t addr;
+    void (*p_func)(uint8_t memoft, uint8_t *data, uint8_t *read_bytes);
+} regist_read_type;
+
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+uint8_t get_regist_value(uart_type *p_data, uint8_t mem_start_addr, uint8_t read_len);
+uint8_t set_regist_value(uart_type *p_data, uint8_t mem_start_addr, uint8_t read_len);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __UART_TABLE_H

+ 7 - 7
dev/dev_uart4.c → modules/uart/uart_task.c

@@ -1,7 +1,7 @@
-#include "dev_uart4.h"
-#include "dev_uart_table.h"
-#include "hal_uart4.h"
-#include "queue.h"
+#include "uart_task.h"
+#include "uart_interface.h"
+#include "uart_table.h"
+#include "utils.h"
 #include <stdint.h>
 #include <time.h>
 
@@ -122,7 +122,7 @@ uint8_t uart_receive_analysis(uart_type *p_data)
 {
     uint8_t i, len;
 
-    len = UART_ARR_SIZE(com_protocol_table);
+    len = ARR_SIZE(com_protocol_table);
 
     for (i = 0; i < len; i++)
     {
@@ -149,9 +149,9 @@ void uart_task(void)
 
         if (1 == uart_receive_analysis(&uart_msg))
         {
-            uart4_start_send(&uart_msg);
+            uart_start_send(&uart_msg);
         }
     }
 
     uart4_conn_check();
-}
+}

+ 53 - 0
modules/uart/uart_task.h

@@ -0,0 +1,53 @@
+#ifndef __UART_TASK_H
+#define __UART_TASK_H
+
+#include "uart_interface.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifdef __UART_TASK_H_GLOBAL
+#define __UART_TASK_H_EXTERN
+#else
+#define __UART_TASK_H_EXTERN extern
+#endif
+
+/************************************************************************************************
+ *                                          Version                                             *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          How to use                                          *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Enable config                                        *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Includes                                            *
+ ************************************************************************************************/
+#include "uart_interface.h"
+#include <stdint.h>
+/************************************************************************************************
+ *                                          Defines                                              *
+ ************************************************************************************************/
+
+/************************************************************************************************
+ *                                          Typedefs                                            *
+ ************************************************************************************************/
+typedef struct
+{
+    uint8_t cmd;
+    uint8_t (*p_func)(uart_type *data);
+} com_protocol_type;
+/************************************************************************************************
+ *                                          Interfaces                                          *
+ ************************************************************************************************/
+
+void uart_task(void);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif // __UART_TASK_H

+ 0 - 0
project/gcc/STM32F40x.svd → project/STM32F40x.svd


+ 0 - 0
project/gcc/STM32F417IG_FLASH.ld → project/STM32F417IG_FLASH.ld


+ 13 - 14
user/main.c → project/main.c

@@ -1,13 +1,13 @@
-
-#include "app_conf.h"
-// #include "core_cmFunc.h"
+#include "app_task.h"
 #include "ble_core.h"
-#include "dev_conf.h"
-#include "hal_can.h"
-#include "hal_conf.h"
-#include "hal_pwm.h"
-#include "hal_uart4.h"
+#include "gpio.h"
+#include "i2c1.h"
+#include "iap.h"
+#include "key.h"
 #include "misc.h"
+#include "pwm.h"
+#include "systick.h"
+#include "uart4.h"
 
 #define APP_FLASH_OFFSET 0x8000
 #define BOOT_EN          1
@@ -20,19 +20,18 @@ int main(void)
 #endif
 
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
-    hal_systick_init();
-    hal_gpio_init();
+    systick_init();
+    gpio_init();
     pwm_init(&pwm_timer3_content);
     pwm_mode(&pwm_timer3_content, 1);
     dev_can_network_init();
     usart_config_init(&usart1_context, 115200);
     usart_config_init(&usart2_context, 9600);
     usart_config_init(&usart3_context, 115200);
-    hal_uart4_init();
+    uart4_init();
     ble_init();
-    hal_i2c1_init();
-    dev_key_button_init();
-
+    i2c1_init();
+    key_button_init();
     set_firmver();
     iap_flag_release();
     iap_param_init();

+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f40_41xxx.s → project/startup/gcc/startup_stm32f40_41xxx.s


+ 0 - 1
user/stm32f4xx_conf.h → project/stm32f4xx_conf.h

@@ -51,7 +51,6 @@
 #include "stm32f4xx_usart.h"
 #include "stm32f4xx_wwdg.h"
 
-
 #if defined(STM32F429_439xx) || defined(STM32F446xx)
 #include "stm32f4xx_can.h"
 #include "stm32f4xx_cryp.h"

+ 17 - 1
user/stm32f4xx_it.c → project/stm32f4xx_it.c

@@ -29,6 +29,10 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm32f4xx_it.h"
+#include "dev_task.h"
+#include "iap.h"
+#include "systick.h"
+#include "uart_interface.h"
 
 /** @addtogroup Template_Project
  * @{
@@ -128,7 +132,15 @@ void PendSV_Handler(void) {}
  * @param  None
  * @retval None
  */
-// void SysTick_Handler(void) {}
+void SysTick_Handler(void)
+{
+    dev_task_clock();
+    dev_systick_decrement();
+    dev_systick_increase();
+    uart_rx_ticks();
+    systick_handler();
+    jump_boot_time_ctrl();
+}
 
 /******************************************************************************/
 /*                 STM32F4xx Peripherals Interrupt Handlers                   */
@@ -149,5 +161,9 @@ void PendSV_Handler(void) {}
 /**
  * @}
  */
+void UART4_IRQHandler(void)
+{
+    uart4_it();
+}
 
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 15 - 16
user/stm32f4xx_it.h → project/stm32f4xx_it.h

@@ -30,27 +30,26 @@
 #define __STM32F4xx_IT_H
 
 #ifdef __cplusplus
-extern "C"
-{
+extern "C" {
 #endif
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm32f4xx.h"
 
-    /* Exported types ------------------------------------------------------------*/
-    /* Exported constants --------------------------------------------------------*/
-    /* Exported macro ------------------------------------------------------------*/
-    /* Exported functions ------------------------------------------------------- */
-
-    void NMI_Handler(void);
-    void HardFault_Handler(void);
-    void MemManage_Handler(void);
-    void BusFault_Handler(void);
-    void UsageFault_Handler(void);
-    void SVC_Handler(void);
-    void DebugMon_Handler(void);
-    void PendSV_Handler(void);
-    void SysTick_Handler(void);
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/* Exported macro ------------------------------------------------------------*/
+/* Exported functions ------------------------------------------------------- */
+
+void NMI_Handler(void);
+void HardFault_Handler(void);
+void MemManage_Handler(void);
+void BusFault_Handler(void);
+void UsageFault_Handler(void);
+void SVC_Handler(void);
+void DebugMon_Handler(void);
+void PendSV_Handler(void);
+void SysTick_Handler(void);
 
 #ifdef __cplusplus
 }

+ 0 - 0
cmsis/system_stm32f4xx.c → project/system_stm32f4xx.c


+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h → vendors/ST/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h


+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h → vendors/ST/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h


+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Release_Notes.html → vendors/ST/CMSIS/Device/ST/STM32F4xx/Release_Notes.html


+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/TASKING/cstart_thumb2.asm → vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TASKING/cstart_thumb2.asm


+ 0 - 0
libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f401xx.s → vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f401xx.s


+ 0 - 0
startup/gcc/startup_stm32f40_41xxx.s → vendors/ST/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f40_41xxx.s


この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません