Browse Source

项目优化

樊春春 5 months ago
parent
commit
1cbd730a4b

+ 15 - 12
CMakeLists.txt

@@ -27,10 +27,11 @@ set(MCU_PARAMS
 
 # C 源文件
 file(GLOB_RECURSE C_SRCS
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/app/*.c
         ${CMAKE_CURRENT_SOURCE_DIR}/project/gcc/startup_stm32f40xx.s
         ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/*.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/board/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/*.c
+        ${CMAKE_CURRENT_SOURCE_DIR}/dev/*.c
         )
 file(GLOB_RECURSE R_SRCS
         ${CMAKE_CURRENT_SOURCE_DIR}/libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c
@@ -44,19 +45,21 @@ list(REMOVE_ITEM C_SRCS ${R_SRCS})
 
 # C .h文件
 set(INC_C_DIRS
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications/bootloader
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications/global
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications/iap
-        ${CMAKE_CURRENT_SOURCE_DIR}/applications/queue
+        ${CMAKE_CURRENT_SOURCE_DIR}/app
+        ${CMAKE_CURRENT_SOURCE_DIR}/app/task
         ${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}/board
-        ${CMAKE_CURRENT_SOURCE_DIR}/board/driver
-        ${CMAKE_CURRENT_SOURCE_DIR}/board/driver/gpio
-        ${CMAKE_CURRENT_SOURCE_DIR}/board/can
-        ${CMAKE_CURRENT_SOURCE_DIR}/board/dwt
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/gpio
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/can
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/dwt
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/flash
+        ${CMAKE_CURRENT_SOURCE_DIR}/driver/systick
+        ${CMAKE_CURRENT_SOURCE_DIR}/dev/boot
+        ${CMAKE_CURRENT_SOURCE_DIR}/dev/iap
+        ${CMAKE_CURRENT_SOURCE_DIR}/dev/can
+        ${CMAKE_CURRENT_SOURCE_DIR}/dev/queue
         )
 
 set(INC_CXX_DIRS)

+ 15 - 43
applications/main.c → app/main.c

@@ -1,30 +1,20 @@
 #include "board.h"
-#include "bootloader.h"
 #include "core_cmFunc.h"
+#include "dev_boot.h"
 #include "dev_iap.h"
-#include "global.h"
-#include "hal_flash.h"
-#include "iap.h"
-#include "queue.h"
-#include "stm32f4xx_gpio.h"
-#include "system_stm32f4xx.h"
+#include "drv_flash.h"
+#include "schedule.h"
 #include <stdint.h>
 #include <stm32f4xx.h>
 
-extern SqQueue CanQueueImportant; // CAN��Ҫ���ݶ���
-uint16_t       cache_buf[1024] = {0};
+uint16_t cache_buf[1024] = {0};
 
 int main(void)
 {
-    rt_hw_board_init();
-    uint32_t        w_size = 0;
-    u16             cnt    = 0;
-    CanData_TypeDef element;
-
-    rt_pin_mode(RED_LED_PIN, PIN_MODE_OUTPUT);
-    rt_pin_mode(GREEN_LED_PIN, PIN_MODE_OUTPUT);
-    InitQueue(&CanQueueImportant);
-    can_init();
+    drv_board_init();
+    dev_can_network_init();
+    uint32_t w_size = 0;
+    u16      cnt    = 0;
 
     iap_param_init();
     fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, 4);
@@ -58,7 +48,7 @@ int main(void)
             {
                 CAN_DeInit(CAN1);
                 __set_PRIMASK(1);
-                boot_goto_app();
+                run_app();
             }
             else
             {
@@ -70,7 +60,7 @@ int main(void)
             iap_flag_release();
             CAN_DeInit(CAN1);
             __set_PRIMASK(1);
-            boot_goto_app();
+            run_app();
         }
     }
 
@@ -103,7 +93,7 @@ int main(void)
             {
                 CAN_DeInit(CAN1);
                 __set_PRIMASK(1);
-                boot_goto_app();
+                run_app();
             }
         }
     }
@@ -111,31 +101,13 @@ int main(void)
     {
         CAN_DeInit(CAN1);
         __set_PRIMASK(1);
-        boot_goto_app();
+        run_app();
     }
 
+    schedule_init(TASK_PERIOD);
+
     while (1)
     {
-        // if (GetHead(&CanQueueImportant, &element) != ERROR)
-        // {
-        //     cnt = 0;
-        //     // delay_ms(1000);
-        //     GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
-        //     iap_process(&element);
-        // }
-        // else if (flash_up_is_ok() == TRUE)
-        // {
-        //     iap_from_flash();
-        //     boot_goto_app();
-        // }
-        // else
-        // {
-        //     cnt++;
-        //     delay_ms(10);
-        //     if (cnt > 100)
-        //     {
-        //         boot_goto_app();
-        //     }
-        // }
+        schedule();
     }
 }

+ 0 - 0
applications/stm32f4xx_conf.h → app/stm32f4xx_conf.h


+ 7 - 1
applications/stm32f4xx_it.c → app/stm32f4xx_it.c

@@ -29,6 +29,8 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "stm32f4xx_it.h"
+#include "dev_iap.h"
+#include "schedule.h"
 
 /** @addtogroup Template_Project
  * @{
@@ -140,7 +142,11 @@ void PendSV_Handler(void)
  */
 void SysTick_Handler(void)
 {
-    // TimingDelay_Decrement();
+    schedule_clock();
+    // if (jump_boot_time == 10)
+    // {
+    //     NVIC_SystemReset();
+    // }
 }
 
 /******************************************************************************/

+ 0 - 0
applications/stm32f4xx_it.h → app/stm32f4xx_it.h


+ 0 - 0
applications/system_stm32f4xx.c → app/system_stm32f4xx.c


+ 1 - 5
task/app_task.c → app/task/app_task.c

@@ -1,7 +1,5 @@
 #include "app_task.h"
-#include "app_can.h"
-#include "cli.h"
-#include "hal_led.h"
+#include "dev_can.h"
 #include "stdio.h"
 
 #define DB_TASK_LED_IDX (10)
@@ -32,8 +30,6 @@ void task4(void) {};
 void task5(void)
 {
     sys_run_time_u32++;
-    // printf("RUNNING\r\n");
-    LED_RUN = !LED_RUN;
 };
 
 void task6(void) {};

+ 0 - 0
task/app_task.h → app/task/app_task.h


+ 0 - 0
task/schedule.c → app/task/schedule.c


+ 0 - 0
task/schedule.h → app/task/schedule.h


+ 0 - 61
applications/global/global.h

@@ -1,61 +0,0 @@
-#ifndef __GLOBAL_H
-#define __GLOBAL_H
-
-#include <stm32f4xx.h>
-
-typedef uint32_t u32;
-typedef uint16_t u16;
-typedef uint8_t  u8;
-
-// typedef void (*pFunction)(void);
-
-typedef struct
-{
-    u8  buf[8];
-    u32 can_id;
-} CanData_TypeDef;
-
-typedef struct
-{
-    u8 result;
-} updata;
-
-updata *var_get_info(void);
-
-#ifndef VAR_DEFINE
-#define VAR_FUNC_DEF(TYPE, VAR) \
-    TYPE var_get_##VAR(void);   \
-    void var_set_##VAR(value);
-#else
-#define VAR_FUNC_DEF(TYPE, VAR)      \
-    TYPE var_get_##VAR(void)         \
-    {                                \
-        TYPE value;                  \
-        value = var_get_info()->VAR; \
-        return value;                \
-    }                                \
-    void var_set_##VAR(value)        \
-    {                                \
-        var_get_info()->VAR = value; \
-    }
-#endif
-
-#define APP_ADDRESS 0x8010000
-#define DLC_8       8
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-// Ñ¡ÔñCANͨѶ²¨ÌØÂÊ
-// #define  __CAN_BAUD_500K
-#define __CAN_BAUD_250K
-
-// VAR_FUNC_DEF(u8, result)
-u8   var_get_result(void);
-void var_set_result(u8 value);
-#endif

+ 0 - 25
applications/global/var.c

@@ -1,25 +0,0 @@
-#include "global.h"
-#include "queue.h"
-
-// #define VAR_DEFINE
-
-SqQueue CanQueueImportant; // CANÖØÒªÊý¾Ý¶ÓÁÐ
-
-updata update_info = {0};
-
-updata *var_get_info(void)
-{
-    return &update_info;
-}
-
-u8 var_get_result(void)
-{
-    u8 value = 0;
-    value    = var_get_info()->result;
-    return value;
-}
-
-void var_set_result(u8 value)
-{
-    var_get_info()->result = value;
-}

+ 0 - 396
applications/iap/iap.c

@@ -1,396 +0,0 @@
-// #include "iap.h"
-// #include "bootloader.h"
-// #include "can.h"
-// #include "dwt.h"
-// #include "global.h"
-// #include "stm32f4xx_flash.h"
-// #include <string.h>
-
-// static u8 iap_index = 0;
-// // static u16 iap_buf[IAP_BUF_SIZE] = {0};
-
-// void iap_connect_clear(void){
-//     iap_}
-
-// uint16_t STMFLASH_GetFlashSector(u32 addr)
-// {
-//     if (addr < ADDR_FLASH_SECTOR_1)
-//         return FLASH_Sector_0;
-//     else if (addr < ADDR_FLASH_SECTOR_2)
-//         return FLASH_Sector_1;
-//     else if (addr < ADDR_FLASH_SECTOR_3)
-//         return FLASH_Sector_2;
-//     else if (addr < ADDR_FLASH_SECTOR_4)
-//         return FLASH_Sector_3;
-//     else if (addr < ADDR_FLASH_SECTOR_5)
-//         return FLASH_Sector_4;
-//     else if (addr < ADDR_FLASH_SECTOR_6)
-//         return FLASH_Sector_5;
-//     else if (addr < ADDR_FLASH_SECTOR_7)
-//         return FLASH_Sector_6;
-//     else if (addr < ADDR_FLASH_SECTOR_8)
-//         return FLASH_Sector_7;
-//     else if (addr < ADDR_FLASH_SECTOR_9)
-//         return FLASH_Sector_8;
-//     else if (addr < ADDR_FLASH_SECTOR_10)
-//         return FLASH_Sector_9;
-//     else if (addr < ADDR_FLASH_SECTOR_11)
-//         return FLASH_Sector_10;
-//     return FLASH_Sector_11;
-// }
-
-// u8 flash_read_byte(u32 faddr)
-// {
-//     return *(vu8 *)faddr;
-// }
-
-// u16 STMFLASH_ReadHalfWord(u32 faddr)
-// {
-//     return *(vu16 *)faddr;
-// }
-
-// u32 flash_read_page(u32 faddr)
-// {
-//     return *(vu32 *)faddr;
-// }
-
-// void copy_to_runaddr(u32 end_addr)
-// {
-//     FLASH_Status status      = FLASH_COMPLETE;
-//     u32          write_addr1 = IAP_RUN_START_SECTOR, read_addr = IAP_DOWN_BEGIN_ADDR;
-//     // u8  erase_sector = 0, index = 0;
-//     u32 temp_value = 0;
-//     // erase_sector = STMFLASH_GetFlashSector(IAP_RUN_START_SECTOR);
-//     FLASH_Unlock();              // 解锁
-//     FLASH_DataCacheCmd(DISABLE); // FLASH擦除期间,必须禁止数据缓存
-//     FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
-
-//     status = FLASH_EraseSector(FLASH_Sector_4, VoltageRange_3);
-//     if (status != FLASH_COMPLETE)
-//     {
-//         FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
-//         FLASH_Lock();               // 上锁
-//         return;
-//     }
-//     status = FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3);
-//     if (status != FLASH_COMPLETE)
-//     {
-//         FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
-//         FLASH_Lock();               // 上锁
-//         return;
-//     }
-
-//     while (end_addr > read_addr)
-//     {
-//         temp_value = flash_read_page(read_addr);
-//         if (FLASH_ProgramWord(write_addr1, temp_value) != FLASH_COMPLETE) // 写入数据
-//         {
-//             break; // 写入异常
-//         }
-
-//         write_addr1 += 4;
-//         read_addr += 4;
-//     }
-
-//     FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
-//     FLASH_Lock();               // 上锁
-// }
-
-// void iap_send_dataresp(u8 res, u32 addr, u32 sum)
-// {
-//     u8 send_buf[8] = {0};
-//     send_buf[0]    = IAP_CMD_DOWNLOAD;
-
-//     send_buf[1] = addr;
-//     send_buf[2] = addr >> 8;
-//     send_buf[3] = addr >> 16;
-//     send_buf[4] = res;
-//     send_buf[5] = addr >> 24;
-//     send_buf[6] = (u8)(sum >> 8);
-//     send_buf[7] = (u8)sum;
-//     can_send_msg(send_buf, DLC_8, IAP_MCU_DATA_FRAME);
-// }
-
-// void iap_send_datafinish(u16 page_num)
-// {
-//     u8 send_buf[8] = {0};
-//     send_buf[0]    = IAP_CMD_DOWNLOAD_END;
-//     send_buf[1]    = (u8)page_num;
-//     send_buf[2]    = (u8)(page_num >> 8);
-//     send_buf[3]    = 0xFF;
-//     send_buf[4]    = 0xFF;
-//     send_buf[5]    = 0xFF;
-//     send_buf[6]    = 0xFF;
-//     send_buf[7]    = 0xFF;
-//     can_send_msg(send_buf, DLC_8, IAP_MCU_DATA_FRAME);
-// }
-
-// u8 stm_flash_write(u32 addr, u16 *array, u32 len)
-// {
-//     u8           res   = TRUE;
-//     u32          index = 0, temp_addr = addr;
-//     FLASH_Status status = FLASH_COMPLETE;
-//     FLASH_Unlock();
-//     FLASH_DataCacheCmd(DISABLE);
-//     for (index = 0; index < len;)
-//     {
-//         if (STMFLASH_ReadHalfWord(temp_addr) != 0xFFFF)
-//         {
-//             status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
-//             if (status != FLASH_COMPLETE)
-//             {
-//                 res = FALSE;
-//                 break;
-//             }
-//         }
-//         else
-//         {
-//             index++;
-//             temp_addr += 2;
-//         }
-//     }
-//     if (status == FLASH_COMPLETE)
-//     {
-//         for (index = 0; index < len; index++)
-//         {
-//             if (FLASH_ProgramHalfWord(addr, array[index]) != FLASH_COMPLETE) // 写入数据
-//             {
-//                 res = FALSE;
-//                 // 写入异常
-//                 break;
-//             }
-//             addr += 2;
-//         }
-//     }
-//     FLASH_DataCacheCmd(ENABLE);
-//     FLASH_Lock();
-
-//     return res;
-// }
-
-// void stm_flash_write_word(u32 addr, u32 value)
-// {
-//     u32          temp_addr = addr;
-//     FLASH_Status status    = FLASH_COMPLETE;
-//     FLASH_Unlock();
-//     FLASH_DataCacheCmd(DISABLE);
-
-//     if (flash_read_page(temp_addr) != 0xFFFFFFFF)
-//     {
-//         status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
-//         if (status != FLASH_COMPLETE)
-//         {
-//             return;
-//         }
-//     }
-
-//     if (status == FLASH_COMPLETE)
-//     {
-//         if (FLASH_ProgramWord(addr, value) != FLASH_COMPLETE) // 写入数据
-//         {
-//             return;
-//         }
-//     }
-//     FLASH_DataCacheCmd(ENABLE);
-//     FLASH_Lock();
-// }
-
-// void stm_flash_write_byte(u32 addr, u8 value)
-// {
-//     u32          temp_addr = addr;
-//     FLASH_Status status    = FLASH_COMPLETE;
-//     FLASH_Unlock();
-//     FLASH_DataCacheCmd(DISABLE);
-
-//     if (flash_read_byte(temp_addr) != 0xFF)
-//     {
-//         status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
-//         if (status != FLASH_COMPLETE)
-//         {
-//             return;
-//         }
-//     }
-
-//     if (status == FLASH_COMPLETE)
-//     {
-//         if (FLASH_ProgramByte(addr, value) != FLASH_COMPLETE) // 写入数据
-//         {
-//             return;
-//         }
-//     }
-//     FLASH_DataCacheCmd(ENABLE);
-//     FLASH_Lock();
-// }
-
-// void iap_process(CanData_TypeDef *rx_msg)
-// {
-//     // 握手帧回复,最后一个字节为前7个字节累加和
-//     u8  handle_buf[DLC_8] = {'S', 'U', 'C', 'C', 'E', 'S', 'S', 0x19};
-//     u8  pc_frame[DLC_8]   = {'A', 'P', 'P', 'L', 'O', 'A', 'D', 0x01};
-//     u8  index = 0, pc_cmd = 0, pack_num = 0, res = 0;
-//     u16 rec_page_num = 0;
-//     u32 temp_addr    = 0;
-
-//     static u16 cnt            = 0;
-//     static u16 total_page_num = 0;
-//     static u32 iap_sum        = 0;
-//     static u32 pack_receive   = 0;
-//     static u32 write_addr = 0, old_write_addr = 0;
-
-//     if (rx_msg->can_id == IAP_PC_BEGINE_FRAME)
-//     {
-//         cnt = 0;
-//         if (memcmp(rx_msg->buf, pc_frame, 7) == 0)
-//         {
-//             total_page_num = 0;
-//             if (res == 0)
-//             {
-//                 InitQueue(&CanQueueImportant);
-//                 can_send_msg(handle_buf, DLC_8, IAP_MCU_BIGEN_FRAME);
-//                 write_addr = IAP_RUN_START_SECTOR; // IAP_DOWN_BEGIN_ADDR;
-//             }
-//             else
-//             {
-//                 return;
-//             }
-//         }
-//     }
-//     else if (rx_msg->can_id == IAP_PC_DATA_FRAME)
-//     {
-//         cnt    = 0;
-//         pc_cmd = rx_msg->buf[0];
-//         switch (pc_cmd)
-//         {
-//         case IAP_CMD_DOWNLOAD:
-//             pack_num = rx_msg->buf[1];
-//             if (pack_num == 0x01)
-//             {
-//                 iap_sum      = 0;
-//                 iap_index    = 0;
-//                 pack_receive = 0;
-//                 pack_receive |= (1 << (pack_num - 1));
-//                 write_addr = (rx_msg->buf[6] << 24) + (rx_msg->buf[5] << 16) + (rx_msg->buf[4] << 8) + rx_msg->buf[3];
-//             }
-//             else if ((pack_num > 0x01) && (pack_num < 0x17))
-//             {
-//                 if (((pack_receive & 0x00000001) != 0x00000001) || ((pack_receive & (1 << (pack_num - 1))) == (1 << (pack_num - 1))))
-//                 {
-//                     break;
-//                 }
-//                 iap_index = (pack_num - 2) * 3;
-//                 for (index = 0; index < 3; index++)
-//                 {
-//                     if (iap_index < (IAP_BUF_SIZE - 1))
-//                     {
-//                         pack_receive |= 1 << (pack_num - 1);
-//                         iap_buf[iap_index] = ((u16)rx_msg->buf[2 * index + 3] << 8) + rx_msg->buf[2 * index + 2];
-//                         iap_sum += iap_buf[iap_index];
-//                         iap_index++;
-//                     }
-//                 }
-//             }
-//             else if (pack_num == 0x17)
-//             {
-//                 if (((pack_receive & 0x00000001) != 0x00000001) || ((pack_receive & (1 << (pack_num - 1))) == (1 << (pack_num - 1))))
-//                 {
-//                     break;
-//                 }
-//                 iap_index = 63;
-//                 pack_receive |= 1 << (pack_num - 1);
-//                 iap_buf[iap_index] = ((u16)rx_msg->buf[3] << 8) + rx_msg->buf[2];
-//                 iap_sum += iap_buf[iap_index];
-//             }
-//             if ((pack_receive & 0x007FFFFF) == 0x007FFFFF)
-//             {
-//                 temp_addr = write_addr;
-//                 if ((write_addr >= ADDR_FLASH_SECTOR_4) && (write_addr <= ADDR_FLASH_SECTOR_END) && (stm_flash_write(temp_addr, iap_buf, IAP_BUF_SIZE) == TRUE))
-//                 {
-//                     iap_send_dataresp(MCU_DOWNLOAD_SUCC, write_addr, iap_sum);
-//                     if (write_addr != old_write_addr)
-//                     {
-//                         total_page_num++;
-//                         old_write_addr = write_addr;
-//                     }
-//                 }
-//                 else
-//                 {
-//                     iap_send_dataresp(MCU_DOWNLOAD_FAIL, write_addr, iap_sum);
-//                 }
-
-//                 iap_sum      = 0;
-//                 iap_index    = 0;
-//                 pack_receive = 0;
-//             }
-//             break;
-//         case IAP_CMD_DOWNLOAD_END:
-//             rec_page_num = rx_msg->buf[2];
-//             rec_page_num = (rec_page_num << 8) | rx_msg->buf[1];
-//             if (rec_page_num != total_page_num)
-//             {
-//                 break;
-//             }
-//             iap_send_datafinish(total_page_num);
-//             write_addr     = IAP_RUN_START_SECTOR;
-//             total_page_num = 0;
-//             delay_ms(300);
-//             boot_goto_app(APP_ADDRESS);
-//             break;
-//         }
-//     }
-//     else
-//     {
-//         cnt++;
-//         delay_ms(10);
-//         if (cnt > 50)
-//         {
-//             boot_goto_app(APP_ADDRESS);
-//         }
-//     }
-// }
-
-// u8 flash_up_is_ok(void)
-// {
-//     u32 len = 0;
-//     len     = flash_read_page(IAP_FLASH_UP_LEN);
-//     if ((len > 0) && (len < 455680)) // 445K
-//     {
-//         return TRUE;
-//     }
-//     else
-//     {
-//         return FALSE;
-//     }
-// }
-
-// void iap_from_flash(void)
-// {
-//     u32 value = 0, read_add = IAP_FLASH_UP_ADDR, write_add = IAP_RUN_START_SECTOR; // IAP_DOWN_BEGIN_ADDR;
-//     u32 len = 0;
-
-//     len = flash_read_page(IAP_FLASH_UP_LEN);
-
-//     while (len >= 4)
-//     {
-//         value = flash_read_page(read_add);
-//         stm_flash_write_word(write_add, value);
-//         read_add += 4;
-//         write_add += 4;
-//         len -= 4;
-//     }
-
-//     for (; len > 0; len--)
-//     {
-//         value = flash_read_byte(read_add);
-//         stm_flash_write_byte(write_add, (u8)value);
-//         write_add++;
-//     }
-
-//     FLASH_Unlock();
-//     FLASH_DataCacheCmd(DISABLE);
-//     FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3);
-//     FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3);
-//     FLASH_EraseSector(FLASH_Sector_10, VoltageRange_3);
-//     FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
-//     FLASH_DataCacheCmd(ENABLE);
-//     FLASH_Lock();
-// }

+ 0 - 104
applications/iap/iap.h

@@ -1,104 +0,0 @@
-// #ifndef __IAP_H
-// #define __IAP_H
-// #include "global.h"
-// #include <stdint.h>
-// #include <stm32f4xx.h>
-
-// #define IAP_FLASH_UP_LEN  0x8080000
-// #define IAP_FLASH_UP_ADDR 0X8080004
-
-// #define IAP_RUN_START_SECTOR 0x8010000
-// #define IAP_DOWN_BEGIN_ADDR  0x8080000 // iap代码下载起始地址
-
-// #define IAP_BACKUP_SECTOR1 10
-// #define IAP_BACKUP_SECTOR2 11
-
-// #define IAP_PC_BEGINE_FRAME 0x18CFD0EF
-// #define IAP_PC_DATA_FRAME   0x18CED0EF
-
-// #define IAP_MCU_BIGEN_FRAME 0x18CFEFD0
-// #define IAP_MCU_DATA_FRAME  0x18CEEFD0
-
-// #define UPGRADE_FLASH_DATA_SIZE 4                                                 // 升级参数数组元素个数
-// #define UPGRADE_APP_DATA_SIZE   4                                                 // 升级参数数组元素个数
-// #define UPGRADE_BUFF_DATA_SIZE  (UPGRADE_FLASH_DATA_SIZE + UPGRADE_APP_DATA_SIZE) // 升级参数数组元素个数
-
-// #define IAP_CMD_VERSION 0x71 // 查询版本号
-// #define IAP_CMD_INFO    0x72 // 包信息
-// #define IAP_CMD_ERASE   0x73 // 擦除命令
-// #define IAP_CMD_DATA    0x80 // 数据帧 IAP_CMD_DATA |帧号
-// #define IAP_CMD_CRC     0x74 // 传输结果校验
-// #define IAP_CMD_OVER    0x76 // 执行跳转
-
-// 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 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
-//     {
-//         uint16_t sign;        // 首地址 buf[0]
-//         uint16_t crc16;       // 中地址 buf[1]
-//         uint32_t moving_size; // 高地址 buf[2] buf[3]
-//     } param;
-//     uint16_t infor_buf[UPGRADE_FLASH_DATA_SIZE];
-// } upgrade_param; // 升级参数使用
-
-// // FLASH 扇区的起始地址
-// #define ADDR_FLASH_SECTOR_0   ((u32)0x08000000) // 扇区0起始地址, 16 Kbytes
-// #define ADDR_FLASH_SECTOR_1   ((u32)0x08004000) // 扇区1起始地址, 16 Kbytes
-// #define ADDR_FLASH_SECTOR_2   ((u32)0x08008000) // 扇区2起始地址, 16 Kbytes
-// #define ADDR_FLASH_SECTOR_3   ((u32)0x0800C000) // 扇区3起始地址, 16 Kbytes
-// #define ADDR_FLASH_SECTOR_4   ((u32)0x08010000) // 扇区4起始地址, 64 Kbytes
-// #define ADDR_FLASH_SECTOR_5   ((u32)0x08020000) // 扇区5起始地址, 128 Kbytes
-// #define ADDR_FLASH_SECTOR_6   ((u32)0x08040000) // 扇区6起始地址, 128 Kbytes
-// #define ADDR_FLASH_SECTOR_7   ((u32)0x08060000) // 扇区7起始地址, 128 Kbytes
-// #define ADDR_FLASH_SECTOR_8   ((u32)0x08080000) // 扇区8起始地址, 128 Kbytes
-// #define ADDR_FLASH_SECTOR_9   ((u32)0x080A0000) // 扇区9起始地址, 128 Kbytes
-// #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)
-
-// typedef enum
-// {
-//     DISCONNECT = 0,
-//     HANDSHAKE,
-//     PROGRAMING,
-//     PROGRAMFinish,
-// } workmode;
-
-// typedef union
-// {
-//     u8  value[4];
-//     u32 cmd;
-// } IAP_Union;
-
-// void iap_process(CanData_TypeDef *rx_msg);
-// u8   flash_up_is_ok(void);
-// void iap_from_flash(void);
-// #endif

+ 0 - 83
applications/queue/queue.c

@@ -1,83 +0,0 @@
-
-#include "queue.h"
-#include "stm32f4xx.h"
-
-///   front   ...         rear      数据方向 ->
-/* 队列的顺序存储结构(循环队列) */
-
-/****************************************************
- *  函 数 名:InitQueue
- *  函数功能:初始化队列
- *  入口参数:无
- *  说    明:
- ****************************************************/
-void InitQueue(SqQueue *Q)
-{ /* 构造一个空队列Q */
-
-    Q->front = Q->rear = 0; /*空队列*/
-}
-
-/****************************************************
- *  函 数 名:IsQueueEmpty
- *  函数功能:查询队列是否为空
- *  入口参数:Q 队列
- *  说    明:空队列,返回TRUE;否则返回FALSE
- ****************************************************/
-u8 IsQueueEmpty(const SqQueue *Q)
-{ /* 若*/
-    if (Q->front == Q->rear)
-        return TRUE;
-    else
-        return FALSE;
-}
-
-/****************************************************
- *  函 数 名:QueueLength
- *  函数功能:初始化长度
- *  入口参数:Q 队列
- *  说    明:
- ****************************************************/
-int QueueLength(SqQueue Q)
-{ /* 返回Q的元素个数,即队列的长度 */
-    return (Q.rear - Q.front + MAX_QSIZE) % MAX_QSIZE;
-}
-
-/****************************************************
- *  函 数 名:GetHead
- *  函数功能:获取对头数据
- *  入口参数:Q 队列
- *  说    明:
- ****************************************************/
-u8 GetHead(SqQueue *Q, CanData_TypeDef *e)
-{                            /* 若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR */
-    if (Q->front == Q->rear) /* 队列空 */
-        return ERROR;
-    *e = Q->CanBuf[Q->front];
-
-    Q->front = (Q->front + 1) % MAX_QSIZE;
-    return SUCCESS;
-}
-
-/****************************************************
- *  函 数 名:InsertQueue
- *  函数功能:队列插入数据
- *  入口参数:Q 待插入队列    e 待插入数据
- *  说    明:
- ****************************************************/
-u8 InsertQueue(SqQueue *Q, CanData_TypeDef e)
-{                                              /* 插入元素e为Q的新的队尾元素 */
-    if ((Q->rear + 1) % MAX_QSIZE == Q->front) /* 队列满 */
-        return ERROR;
-    Q->CanBuf[Q->rear] = e;
-    Q->rear            = (Q->rear + 1) % MAX_QSIZE;
-    return SUCCESS;
-}
-
-// u8 DeQueue(SqQueue *Q, Can1Buf_TypeDef *e)
-//{ /* 若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR */
-//   if(Q-&gt;front==Q-&gt;rear) /* 队列空 */
-//     return ERROR;
-//   *e=Q-&gt;base[Q-&gt;front];
-//   Q-&gt;front=(Q-&gt;front+1)%MAX_QSIZE;
-//   return OK;
-// }

+ 0 - 21
applications/queue/queue.h

@@ -1,21 +0,0 @@
-#ifndef __QUEUE_H
-#define __QUEUE_H
-
-#include "global.h"
-#include "stm32f4xx.h"
-
-#define MAX_QSIZE (60u) /* 最大队列长度 */
-
-typedef struct
-{
-    CanData_TypeDef CanBuf[MAX_QSIZE];
-    u16             front;
-    u16             rear;
-} SqQueue;
-
-void InitQueue(SqQueue *Q);                      // 初始化队列
-u8   GetHead(SqQueue *Q, CanData_TypeDef *e);    // 获取对头数据
-u8   InsertQueue(SqQueue *Q, CanData_TypeDef e); // 队列插入数据
-u8   IsQueueEmpty(const SqQueue *Q);             // 查询队列是否为空
-
-#endif

+ 0 - 13
board/board.h

@@ -1,13 +0,0 @@
-#ifndef __BOARD_H
-#define __BOARD_H
-
-#include "can.h"
-#include "drv_gpio.h"
-#include "dwt.h"
-
-#define RED_LED_PIN   GET_PIN(F, 9)
-#define GREEN_LED_PIN GET_PIN(F, 10)
-
-void rt_hw_board_init(void);
-
-#endif

+ 0 - 153
board/can/can.c

@@ -1,153 +0,0 @@
-#include "can.h"
-#include "global.h"
-#include <string.h>
-
-#define MASK_ID 0x18CE00EF
-
-CanRxMsg RxMessageImportant;
-CanRxMsg RxMessageNormal;
-
-CanData_TypeDef CanDataImportant; //  重要数据
-extern SqQueue  CanQueueImportant;
-// extern _declare_box8(can1_mpool, BlockSizeCan1, BlockCountCan1);
-// extern  os_mbx_declare (can1_mailbox, sizeof(can1_mpool)/sizeof(CanRxMsg));//声明消息邮箱  读取can1数据
-// CAN初始化
-// tsjw:重新同步跳跃时间单元.范围:CAN_SJW_1tq~ CAN_SJW_4tq
-// tbs2:时间段2的时间单元.   范围:CAN_BS2_1tq~CAN_BS2_8tq;
-// tbs1:时间段1的时间单元.   范围:CAN_BS1_1tq ~CAN_BS1_16tq
-// brp :波特率分频器.范围:1~1024; tq=(brp)*tpclk1
-// 波特率=Fpclk1/((tbs1+1+tbs2+1+1)*brp);
-// mode:CAN_Mode_Normal,普通模式;CAN_Mode_LoopBack,回环模式;
-// Fpclk1的时钟在初始化的时候设置为42M,如果设置CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_LoopBack);
-// 则波特率为:42M/((6+7+1)*6)=500Kbps
-
-u32 result;
-
-u8 CAN2_Mode_Init(u8 tsjw, u8 tbs2, u8 tbs1, u16 brp, u8 mode)
-{
-    GPIO_InitTypeDef      GPIO_InitStructure;
-    CAN_InitTypeDef       CAN_InitStructure;
-    CAN_FilterInitTypeDef CAN_FilterInitStructure;
-    NVIC_InitTypeDef      NVIC_InitStructure;
-
-    u32 mask_id = MASK_ID;
-
-    // 使能相关时钟
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能PORTA时钟
-    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);  // 使能CAN1时钟
-
-    // 初始化GPIO
-    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_11 | GPIO_Pin_12;
-    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;      // 复用功能
-    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     // 推挽输出
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 100MHz
-    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;      // 上拉
-    GPIO_Init(GPIOA, &GPIO_InitStructure);             // 初始化PA11,PA12
-
-    // 引脚复用映射配置
-    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1); // GPIOB11复用为CAN1
-    GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1); // GPIOB12复用为CAN1
-
-    // CAN单元设置
-    CAN_InitStructure.CAN_TTCM      = DISABLE; // 非时间触发通信模式
-    CAN_InitStructure.CAN_ABOM      = DISABLE; // 软件自动离线管理
-    CAN_InitStructure.CAN_AWUM      = DISABLE; // 睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
-    CAN_InitStructure.CAN_NART      = DISABLE; // 禁止报文自动传送
-    CAN_InitStructure.CAN_RFLM      = DISABLE; // 报文不锁定,新的覆盖旧的
-    CAN_InitStructure.CAN_TXFP      = DISABLE; // 优先级由报文标识符决定
-    CAN_InitStructure.CAN_Mode      = mode;    // 模式设置
-    CAN_InitStructure.CAN_SJW       = tsjw;    // 重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
-    CAN_InitStructure.CAN_BS1       = tbs1;    // Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
-    CAN_InitStructure.CAN_BS2       = tbs2;    // Tbs2范围CAN_BS2_1tq ~	CAN_BS2_8tq
-    CAN_InitStructure.CAN_Prescaler = brp;     // 分频系数(Fdiv)为brp+1
-    CAN_Init(CAN1, &CAN_InitStructure);        // 初始化CAN1
-
-    // 配置过滤器0
-    CAN_FilterInitStructure.CAN_FilterNumber = 0; // 过滤器0
-    CAN_FilterInitStructure.CAN_FilterMode   = CAN_FilterMode_IdMask;
-    CAN_FilterInitStructure.CAN_FilterScale  = CAN_FilterScale_32bit; // 32位
-
-    CAN_FilterInitStructure.CAN_FilterIdHigh = (u16)((mask_id << 3) >> 16);      // 0xFFFF;
-    CAN_FilterInitStructure.CAN_FilterIdLow  = (u16)((mask_id << 3) | (1 << 2)); // 0xFFFF;
-
-    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFF0; // 0x0000;//32位MASK
-    //    CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xfff8; // 最后3位不管
-    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x07FF; // 0; // 最后3位不管
-
-    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; // 过滤器0关联到FIFO0
-    CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;           // 激活过滤器0
-    CAN_FilterInit(&CAN_FilterInitStructure);                            // 滤波器初始化
-
-    CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // FIFO0消息挂号中断允许.
-
-    NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5; // 主优先级为
-                                                              // 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
-    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
-    NVIC_Init(&NVIC_InitStructure);
-    ////////////////////////////////////////////////
-
-    return 0;
-}
-
-/**********************************************************************************************************
- *	函 数 名: CAN1_Init
- *	功能说明: CAN1初始化
- *	形    参: 无
- *	返 回 值: 无
- **********************************************************************************************************/
-void can_init(void)
-{
-#if defined(__CAN_BAUD_500K)
-    CAN2_Mode_Init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 6, CAN_Mode_Normal); // CAN1初始化环回模式,波特率500Kbps
-#elif defined(__CAN_BAUD_250K)
-    CAN2_Mode_Init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 12, CAN_Mode_Normal); // CAN1初始化环回模式,波特率250Kbps
-#else
-    //  #error no can baud macro!;
-#endif
-}
-
-//(固定格式:ID为0X12,标准帧,数据帧)
-// len:数据长度(最大为8)
-// msg:数据指针,最大为8个字节.
-// 返回值:0,成功;
-//		 其他,失败;
-/****************************************************
- *  函数功能:can1发送一组数据
- *  入口参数:msg 数据  len  数据长度  can_id  CAN id
- *  说    明:group_id 从1开始
- *****************************************************/
-u8 can_send_msg(u8 *msg, u8 len, u32 can_id)
-{
-    u16      mbox, i;
-    CanTxMsg TxMessage;
-
-    TxMessage.StdId = 0x00;            // 标准标识符为0
-    TxMessage.ExtId = can_id;          // 设置扩展标示符(29位)
-    TxMessage.IDE   = CAN_Id_Extended; // 使用扩展标识符
-    TxMessage.RTR   = CAN_RTR_Data;    // 消息类型为数据帧
-    TxMessage.DLC   = len;             // 发送两帧信息
-
-    for (i = 0; i < len; i++)
-        TxMessage.Data[i] = msg[i]; // 第一帧信息
-    mbox = CAN_Transmit(CAN1, &TxMessage);
-    i    = 0;
-    while ((CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed) && (i < 0XFFF))
-        i++; // 等待发送结束
-    if (i >= 0XFFF)
-        return 1;
-    return 0;
-}
-
-// CAN1中断服务函数
-void CAN1_RX0_IRQHandler(void)
-{
-    // 1.6us  ucos时间戳、示波器测量都是1.6us
-    CAN_Receive(CAN1, CAN_FIFO0, &RxMessageImportant);
-
-    memcpy(CanDataImportant.buf, RxMessageImportant.Data, RxMessageImportant.DLC);
-    CanDataImportant.can_id = RxMessageImportant.ExtId;
-    if (InsertQueue(&CanQueueImportant, CanDataImportant) == ERROR) // 队列插入数据
-    {
-    }
-}

+ 0 - 12
board/can/can.h

@@ -1,12 +0,0 @@
-#ifndef __CAN_H
-#define __CAN_H
-
-#include "queue.h"
-
-void can_init(void);
-u8   can_send_msg(u8 *msg, u8 len, u32 can_id);
-u8   can_receive_msg(u8 *buf);
-
-#define CAN_BUF1_BLOCK_LEN 100
-
-#endif

+ 0 - 200
board/driver/gpio/drv_gpio.c

@@ -1,200 +0,0 @@
-#include "drv_gpio.h"
-#include "pin.h"
-#include "stm32f4xx_gpio.h"
-
-#define PIN_NUM(port, no) (((((port)&0xFu) << 4) | ((no)&0xFu)))
-#define PIN_PORT(pin)     ((uint8_t)(((pin) >> 4) & 0xFu))
-#define PIN_NO(pin)       ((uint8_t)((pin)&0xFu))
-
-#define PIN_STPORT(pin) ((GPIO_TypeDef *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
-#define PIN_STPIN(pin)  ((uint16_t)(1u << PIN_NO(pin)))
-
-#define PIN_STPORT_MAX 16
-
-#define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
-
-rt_size_t rt_strlen(const char *s)
-{
-    const char *sc;
-
-    for (sc = s; *sc != '\0'; ++sc) /* nothing */
-        ;
-
-    return sc - s;
-}
-
-rt_base_t stm32_pin_get(const char *name)
-{
-    rt_base_t pin = 0;
-    int       hw_port_num, hw_pin_num = 0;
-    int       i, name_len;
-
-    name_len = rt_strlen(name);
-
-    if ((name_len < 4) || (name_len >= 6))
-    {
-        return -RT_EINVAL;
-    }
-    if ((name[0] != 'P') || (name[2] != '.'))
-    {
-        return -RT_EINVAL;
-    }
-
-    if ((name[1] >= 'A') && (name[1] <= 'Z'))
-    {
-        hw_port_num = (int)(name[1] - 'A');
-    }
-    else
-    {
-        return -RT_EINVAL;
-    }
-
-    for (i = 3; i < name_len; i++)
-    {
-        hw_pin_num *= 10;
-        hw_pin_num += name[i] - '0';
-    }
-
-    pin = PIN_NUM(hw_port_num, hw_pin_num);
-
-    return pin;
-}
-
-void rt_pin_write(rt_base_t pin, rt_base_t value)
-{
-    GPIO_TypeDef *gpio_port;
-    uint16_t      gpio_pin;
-
-    if (PIN_PORT(pin) < PIN_STPORT_MAX)
-    {
-        gpio_port = PIN_STPORT(pin);
-        gpio_pin  = PIN_STPIN(pin);
-
-        GPIO_WriteBit(gpio_port, gpio_pin, value);
-    }
-}
-
-int rt_pin_read(rt_base_t pin)
-{
-    GPIO_TypeDef *gpio_port;
-    uint16_t      gpio_pin;
-    int           value = PIN_LOW;
-
-    if (PIN_PORT(pin) < PIN_STPORT_MAX)
-    {
-        gpio_port = PIN_STPORT(pin);
-        gpio_pin  = PIN_STPIN(pin);
-        value     = GPIO_ReadInputDataBit(gpio_port, gpio_pin);
-    }
-
-    return value;
-}
-
-void rt_pin_mode(rt_base_t pin, rt_base_t mode)
-{
-    GPIO_InitTypeDef GPIO_InitStruct;
-
-    if (PIN_PORT(pin) >= PIN_STPORT_MAX)
-    {
-        return;
-    }
-
-    /* Configure GPIO_InitStructure */
-    GPIO_InitStruct.GPIO_Pin   = PIN_STPIN(pin);
-    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_OUT;
-    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
-    GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
-    GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-
-    if (mode == PIN_MODE_OUTPUT)
-    {
-        /* output setting */
-        GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_OUT;
-        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
-        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-    }
-    else if (mode == PIN_MODE_INPUT)
-    {
-        /* input setting: not GPIO_OType. */
-        GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IN;
-        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
-        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-    }
-    else if (mode == PIN_MODE_INPUT_PULLUP)
-    {
-        /* input setting: GPIO_OType up. */
-        GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IN;
-        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
-        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
-    }
-    else if (mode == PIN_MODE_INPUT_PULLDOWN)
-    {
-        /* input setting: GPIO_OType down. */
-        GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IN;
-        GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
-        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_DOWN;
-    }
-    else if (mode == PIN_MODE_OUTPUT_OD)
-    {
-        /* output setting: od. */
-        GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_OUT;
-        GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
-        GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
-    }
-
-    GPIO_Init(PIN_STPORT(pin), &GPIO_InitStruct);
-}
-
-rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
-{
-    int i;
-    for (i = 0; i < 32; i++)
-    {
-        if ((0x01 << i) == bit)
-        {
-            return i;
-        }
-    }
-    return -1;
-}
-
-int rt_hw_pin_init(void)
-{
-#if defined(RCC_GPIOA_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
-#endif
-
-#if defined(RCC_GPIOB_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
-#endif
-
-#if defined(RCC_GPIOC_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
-#endif
-
-#if defined(RCC_GPIOD_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
-#endif
-
-#if defined(RCC_GPIOE_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
-#endif
-
-#if defined(RCC_GPIOF_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
-#endif
-
-#if defined(RCC_GPIOG_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
-#endif
-
-#if defined(RCC_GPIOH_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
-#endif
-
-#if defined(RCC_GPIOI_CLK_ENABLE)
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
-#endif
-
-    return 0;
-}

+ 0 - 15
board/driver/gpio/drv_gpio.h

@@ -1,15 +0,0 @@
-#ifndef __DRV_GPIO_H__
-#define __DRV_GPIO_H__
-
-#include "pin.h"
-#include <stm32f4xx.h>
-
-#define RCC_GPIOF_CLK_ENABLE
-
-#define __STM32_PORT(port) GPIO##port##_BASE
-
-#define GET_PIN(PORTx, PIN) (rt_base_t)((16 * (((rt_base_t)__STM32_PORT(PORTx) - (rt_base_t)GPIOA_BASE) / (0x0400UL))) + PIN)
-
-int rt_hw_pin_init(void);
-
-#endif /* __DRV_GPIO_H__ */

+ 0 - 19
board/driver/pin.h

@@ -1,19 +0,0 @@
-#ifndef PIN_H__
-#define PIN_H__
-
-#include "rtdef.h"
-
-#define PIN_LOW  0x00
-#define PIN_HIGH 0x01
-
-#define PIN_MODE_OUTPUT         0x00
-#define PIN_MODE_INPUT          0x01
-#define PIN_MODE_INPUT_PULLUP   0x02
-#define PIN_MODE_INPUT_PULLDOWN 0x03
-#define PIN_MODE_OUTPUT_OD      0x04
-
-void rt_pin_mode(rt_base_t pin, rt_base_t mode);
-void rt_pin_write(rt_base_t pin, rt_base_t value);
-int  rt_pin_read(rt_base_t pin);
-
-#endif

+ 0 - 50
board/rtdef.h

@@ -1,50 +0,0 @@
-#ifndef __RT_DEF_H__
-#define __RT_DEF_H__
-#include "stdio.h"
-
-/* RT-Thread basic data type definitions */
-#ifndef RT_USING_ARCH_DATA_TYPE
-typedef int8_t   rt_int8_t;   /**<  8bit integer type */
-typedef int16_t  rt_int16_t;  /**< 16bit integer type */
-typedef int32_t  rt_int32_t;  /**< 32bit integer type */
-typedef uint8_t  rt_uint8_t;  /**<  8bit unsigned integer type */
-typedef uint16_t rt_uint16_t; /**< 16bit unsigned integer type */
-typedef uint32_t rt_uint32_t; /**< 32bit unsigned integer type */
-
-#ifdef ARCH_CPU_64BIT
-typedef signed long   rt_int64_t;  /**< 64bit integer type */
-typedef unsigned long rt_uint64_t; /**< 64bit unsigned integer type */
-#else
-typedef int64_t  rt_int64_t;  /**< 64bit integer type */
-typedef uint64_t rt_uint64_t; /**< 64bit unsigned integer type */
-#endif
-#endif
-
-typedef int           rt_bool_t;  /**< boolean type */
-typedef long          rt_base_t;  /**< Nbit CPU related date type */
-typedef unsigned long rt_ubase_t; /**< Nbit unsigned CPU related data type */
-
-typedef rt_base_t   rt_err_t;  /**< Type for error number */
-typedef rt_uint32_t rt_time_t; /**< Type for time stamp */
-typedef rt_uint32_t rt_tick_t; /**< Type for tick count */
-typedef rt_base_t   rt_flag_t; /**< Type for flags */
-typedef rt_ubase_t  rt_size_t; /**< Type for size number */
-typedef rt_ubase_t  rt_dev_t;  /**< Type for device */
-typedef rt_base_t   rt_off_t;  /**< Type for offset */
-
-/* RT-Thread error code definitions */
-#define RT_EOK      0  /**< There is no error */
-#define RT_ERROR    1  /**< A generic error happens */
-#define RT_ETIMEOUT 2  /**< Timed out */
-#define RT_EFULL    3  /**< The resource is full */
-#define RT_EEMPTY   4  /**< The resource is empty */
-#define RT_ENOMEM   5  /**< No memory */
-#define RT_ENOSYS   6  /**< No system */
-#define RT_EBUSY    7  /**< Busy */
-#define RT_EIO      8  /**< IO error */
-#define RT_EINTR    9  /**< Interrupted system call */
-#define RT_EINVAL   10 /**< Invalid argument */
-
-#define rt_inline static __inline
-
-#endif

+ 2 - 3
applications/bootloader/bootloader.c → dev/boot/dev_boot.c

@@ -1,9 +1,8 @@
-#include "bootloader.h"
+#include "dev_boot.h"
 #include "dev_iap.h"
-#include "global.h"
 #include <stdint.h>
 
-void boot_goto_app(void)
+void run_app(void)
 {
     pFunction     Jump_To_Application;
     __IO uint32_t JumpAddress;

+ 2 - 3
applications/bootloader/bootloader.h → dev/boot/dev_boot.h

@@ -1,8 +1,7 @@
 #ifndef __BOOTLOADER_H
 #define __BOOTLOADER_H
 
-#include "global.h"
-#include <stdint.h>
+#include <stm32f4xx.h>
 
 typedef enum
 {
@@ -12,7 +11,7 @@ typedef enum
 
 typedef void (*pFunction)(void);
 
-void    boot_goto_app(void);
+void    run_app(void);
 uint8_t check_addr_sp(uint32_t addr);
 uint8_t check_addr_pc(uint32_t addr);
 

+ 175 - 0
dev/can/dev_can.c

@@ -0,0 +1,175 @@
+#include "dev_can.h"
+#include "dev_iap.h"
+#include "queue.h"
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.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 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;
+
+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;
+    base_data |= (mask & source_data) << start_bit;
+}
+
+void product_array(uint8_t send_array[8])
+{
+    send_array[0] = (uint8_t)base_data;
+    send_array[1] = (uint8_t)(base_data >> 8);
+    send_array[2] = (uint8_t)(base_data >> 16);
+    send_array[3] = (uint8_t)(base_data >> 24);
+    send_array[4] = (uint8_t)(base_data >> 32);
+    send_array[5] = (uint8_t)(base_data >> 40);
+    send_array[6] = (uint8_t)(base_data >> 48);
+    send_array[7] = (uint8_t)(base_data >> 56);
+    base_data     = 0;
+}
+
+uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
+{
+    pdu_tag response_msg;
+    response_msg.id.r    = id;
+    response_msg.reg.dlc = len;
+    memcpy(&response_msg.data.u8_buf[0], p_data, len);
+
+    if (id > 0x7FF)
+    {
+        response_msg.reg.ide = CAN_Id_Extended;
+    }
+    else
+    {
+        response_msg.reg.ide = CAN_Id_Standard;
+    }
+
+    if (en_queue(&can_tx_queue, response_msg) != Q_OK)
+    {
+        return 0;
+    }
+
+    return 1;
+}
+
+static uint8_t can_tx_frame(pdu_tag can_message)
+{
+    uint8_t result = CAN_TxStatus_NoMailBox;
+    result         = drv_can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
+    return result;
+}
+
+void can_tx_callback(void)
+{
+    pdu_tag tx_data;
+
+    if (de_queue(&can_tx_queue, &tx_data) == Q_OK) // 返回值为1代表读取成功
+    {
+        can_tx_frame(tx_data);
+        CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
+    }
+    else
+    {
+        CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);
+    }
+}
+
+void can_rx_callback(CanRxMsg rx_message)
+{
+    pdu_tag data;
+    uint8_t ps;
+    uint8_t pf;
+    switch (rx_message.ExtId)
+    {
+    case 0x18DFF4E1:
+        data.id.r    = 0x18DFF4E1;
+        data.reg.dlc = rx_message.DLC;
+        memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
+        en_queue(&can_rx_queue, data);
+        break;
+    default:
+        return;
+    }
+}
+
+can_rx_tab can_tab[] = {
+    0x18DFF4E1,
+    iap_rec_handler,
+
+};
+
+void can_start_send(void)
+{
+    pdu_tag tx_msg;
+    if (RESET == CAN_GetITStatus(CAN1, CAN_IT_TME))
+    {
+
+        if (de_queue(&can_tx_queue, &tx_msg) == Q_OK) // 返回值为1代表读取成功
+        {
+            can_tx_frame(tx_msg);
+            CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
+        }
+    }
+}
+
+static uint8_t can_rx_process(void)
+{
+    uint8_t i;
+    uint8_t flg = 0;
+    pdu_tag rec_msg;
+
+    for (uint8_t j = 0; j < 15; j++)
+    {
+        if (Q_OK == de_queue(&can_rx_queue, &rec_msg))
+        {
+            for (i = 0; i < ARR_SIZE(can_tab); i++)
+            {
+                if (can_tab[i].id == rec_msg.id.r)
+                {
+                    can_tab[i].p_func(rec_msg);
+                    break;
+                }
+            }
+        }
+    }
+}
+
+void dev_can_network_init(void)
+{
+    drv_can_init();
+    drv_can_rx_back_init(can_rx_callback);
+    drv_can_tx_back_init(can_tx_callback);
+}
+
+void can_process(void)
+{
+    can_rx_process();
+    can_start_send();
+}

+ 29 - 0
dev/can/dev_can.h

@@ -0,0 +1,29 @@
+#ifndef __APP_CAN_H
+#define __APP_CAN_H
+
+#include "drv_can.h"
+#include "queue.h"
+
+#define CAN_PGN_PF 0xFF
+#define CAN_PGN_PS 0xFF
+extern uint8_t recv_can_id;
+
+typedef struct
+{
+    uint32_t id;
+    void (*p_func)(pdu_tag);
+} can_rx_tab;
+
+// #define ARR_SIZE(a) sizeof(a) / a[0]
+
+#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);
+void    dev_can_network_init();
+uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data);
+
+#endif // __APP_CAN_H

+ 122 - 2
applications/dev_iap.c → dev/iap/dev_iap.c

@@ -2,8 +2,7 @@
  *                                          Include                                              *
  ************************************************************************************************/
 #include "dev_iap.h"
-#include "hal_flash.h"
-#include "queue.h"
+#include "drv_flash.h"
 #include <stdint.h>
 #include <string.h>
 
@@ -142,6 +141,127 @@ void read_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
     fmc_read_n_half_word(w_start_addr, buf, pack_len);
 }
 
+void iap_rec_handler(pdu_tag rec_msg)
+{
+    uint8_t erase_flag = 0x01;
+
+    // ota_can_id = rec_msg.id.r;
+    if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
+        iap_connect_clear();
+
+    if (rec_msg.data.u8_buf[0] >> 7)
+    {
+        if (rec_msg.id.b.sa == 0xE1)
+        {
+            g_iap.recv_len         = rec_msg.reg.dlc - 2;
+            g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
+            memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
+
+            if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
+            {
+                g_iap.count_bytes += g_iap.recv_len;
+
+                if ((g_iap.recv_len % 2) != 0)
+                {
+                    g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
+                    g_iap.recv_len++;
+                }
+
+                fmc_clear_flag_star();
+                erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
+                fmc_clear_flag_end();
+
+                g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
+
+                send_buff[0] = IAP_CMD_DATA;
+                send_buff[1] = !erase_flag;
+                g_iap.count_packages++;
+            }
+            else
+            {
+                send_buff[0] = IAP_CMD_DATA;
+                send_buff[1] = 0x06;
+            }
+
+            send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
+            send_buff[3] = rec_msg.data.u8_buf[1];
+
+            send_buff[4] = g_iap.count_packages >> 8;
+            send_buff[5] = g_iap.count_packages & 0xFF;
+        }
+    }
+    else
+    {
+        switch (rec_msg.data.u8_buf[0])
+        {
+        case IAP_CMD_VERSION:
+            iap_param_init();
+            for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
+            {
+                send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
+            }
+
+            send_buff[0] = IAP_CMD_VERSION;
+            send_buff[1] = 0x01;
+            break;
+        case IAP_CMD_INFO:
+            g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
+            g_iap.total_bytes <<= 16;
+            g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
+            g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
+            send_buff[0]         = IAP_CMD_INFO;
+            send_buff[1]         = 0x01;
+            break;
+        case IAP_CMD_ERASE:
+            g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
+            if (rec_msg.data.u8_buf[1])
+            {
+                // 喂狗
+                fmc_clear_flag_star();
+                erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_MIDDLE_ADDR);
+                fmc_clear_flag_end();
+                // 喂狗
+                g_iap.count_bytes    = 0;
+                g_iap.count_packages = 0;
+            }
+            send_buff[0] = IAP_CMD_ERASE;
+            send_buff[1] = !erase_flag;
+            break;
+        case IAP_CMD_CRC:
+            send_buff[0] = IAP_CMD_CRC;
+            send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
+            break;
+        case IAP_CMD_JUMP:
+            if (g_iap.crc16 != g_iap.firmware_crc16)
+            {
+                send_buff[1] = 0x00;
+            }
+            else
+            {
+                fmc_clear_flag_star();
+                fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
+                upgrade.param.sign        = UPGRADE_SIGN;
+                upgrade.param.crc16       = g_iap.crc16;
+                upgrade.param.moving_size = g_iap.total_bytes;
+                fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
+                fmc_clear_flag_end();
+                jump_boot_time = 10;
+                send_buff[1]   = 0x01;
+            }
+            send_buff[0] = IAP_CMD_JUMP;
+            break;
+        case IAP_CMD_MODEL_TYPE:
+            send_buff[0] = IAP_CMD_MODEL_TYPE;
+            send_buff[1] = 0x01;
+
+            send_buff[2] = DEVICE_ID >> 8;
+            send_buff[3] = DEVICE_ID & 0xFF;
+        }
+    }
+
+    push_can_message_to_queue(0x18DFE1F4, 8, send_buff);
+}
+
 // void iap_rec_handler(pdu_tag rec_msg)
 // {
 //     uint8_t erase_flag = 0x01;

+ 3 - 2
applications/dev_iap.h → dev/iap/dev_iap.h

@@ -26,8 +26,8 @@ extern 'C'
 /************************************************************************************************
  *                                          Includes                                             *
  ************************************************************************************************/
+#include "dev_can.h"
 #include <stdint.h>
-
     /************************************************************************************************
      *                                          Defines                                              *
      ************************************************************************************************/
@@ -57,7 +57,7 @@ extern 'C'
 #define IAP_CMD_MODEL_TYPE 0x76   // 执行跳转
 
 #define UPGRADE_SIGN 0x5AA5
-#define DEVICE_ID    0x5AA4
+#define DEVICE_ID    0x5AA5
 
     typedef union
     {
@@ -103,6 +103,7 @@ extern 'C'
     void    iap_param_init(void);
     void    iap_flag_release(void);
     uint8_t erase_app_flash(uint32_t start_address, uint32_t end_addr);
+    void    iap_rec_handler(pdu_tag rec_msg);
 
 /************************************************************************************************
  *                                          Defines                                              *

+ 104 - 0
dev/queue/queue.c

@@ -0,0 +1,104 @@
+#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,
+                      p_pdu_tag       p_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;
+    *p_data       = p_queue->can_message[p_queue->tail];
+    p_queue->tail = (p_queue->tail + 1) % MAX_QSIZE;
+    p_queue->count--;
+
+    return Q_OK;
+}

+ 70 - 0
dev/queue/queue.h

@@ -0,0 +1,70 @@
+#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;
+
+extern can_queue_tag can_tx_queue;
+extern can_queue_tag can_rx_queue;
+
+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, p_pdu_tag);
+#endif

+ 4 - 7
board/board.c → driver/board.c

@@ -1,12 +1,9 @@
 #include "board.h"
 
-void rt_hw_board_init()
+void drv_board_init()
 {
-
-    rt_hw_pin_init();
-
-    dwt_init();
-    // can_init();
-
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
+    drv_systick_init();
+    drv_gpio_init();
+    dwt_init();
 }

+ 11 - 0
driver/board.h

@@ -0,0 +1,11 @@
+#ifndef __BOARD_H
+#define __BOARD_H
+
+#include "drv_can.h"
+#include "drv_gpio.h"
+#include "drv_systick.h"
+#include "dwt.h"
+
+void drv_board_init(void);
+
+#endif

+ 156 - 0
driver/can/drv_can.c

@@ -0,0 +1,156 @@
+#include "drv_can.h"
+#include "stm32f4xx.h"
+#include "stm32f4xx_gpio.h"
+#include <string.h>
+
+CanTxMsg tx_message;
+CanRxMsg rx_message;
+pcan_rx  pcan_rx_back = NULL;
+pcan_tx  pcan_tx_back = NULL;
+
+// CAN初始化
+// tsjw:重新同步跳跃时间单元.范围:CAN_SJW_1tq~ CAN_SJW_4tq
+// tbs2:时间段2的时间单元.   范围:CAN_BS2_1tq~CAN_BS2_8tq;
+// tbs1:时间段1的时间单元.   范围:CAN_BS1_1tq ~CAN_BS1_16tq
+// brp :波特率分频器.范围:1~1024; tq=(brp)*tpclk1
+// 波特率=Fpclk1/((tbs1+1+tbs2+1+1)*brp);
+// mode:CAN_Mode_Normal,普通模式;CAN_Mode_LoopBack,回环模式;
+// 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 drv_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;
+    CAN_FilterInitTypeDef CAN_FilterInitStructure;
+    NVIC_InitTypeDef      NVIC_InitStructure;
+
+    CAN_DeInit(CAN1);
+
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 使能PORTA时钟
+    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);  // 使能CAN1时钟
+
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
+    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_11 | GPIO_Pin_12;
+    GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化IO
+
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
+    GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
+
+    // CAN单元设置
+    CAN_InitStructure.CAN_TTCM      = DISABLE; // 非时间触发通信模式
+    CAN_InitStructure.CAN_ABOM      = DISABLE; // 软件自动离线管理
+    CAN_InitStructure.CAN_AWUM      = DISABLE; // 睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
+    CAN_InitStructure.CAN_NART      = DISABLE; // 禁止报文自动传送
+    CAN_InitStructure.CAN_RFLM      = DISABLE; // 报文不锁定,新的覆盖旧的
+    CAN_InitStructure.CAN_TXFP      = DISABLE; // 优先级由报文标识符决定
+    CAN_InitStructure.CAN_Mode      = mode;    // 模式设置
+    CAN_InitStructure.CAN_SJW       = tsjw;    // 重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
+    CAN_InitStructure.CAN_BS1       = tbs1;    // Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
+    CAN_InitStructure.CAN_BS2       = tbs2;    // Tbs2范围CAN_BS2_1tq ~	CAN_BS2_8tq
+    CAN_InitStructure.CAN_Prescaler = brp;     // 分频系数(Fdiv)为brp+1
+    CAN_Init(CAN1, &CAN_InitStructure);        // 初始化CAN1
+
+    // 配置过滤器0
+    CAN_FilterInitStructure.CAN_FilterNumber = 0; // 过滤器0
+    CAN_FilterInitStructure.CAN_FilterMode   = CAN_FilterMode_IdMask;
+    CAN_FilterInitStructure.CAN_FilterScale  = CAN_FilterScale_32bit; // 32位
+
+    // 只接收标识符位0x0666报文  注意:RTR  IDE  SRR也参与构成标识符
+    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //(((u32)0x0666<<3)&0xFFFF0000)>>16;				//要过滤的ID高位
+    CAN_FilterInitStructure.CAN_FilterIdLow  = 0x0000; //(((u32)0x0666<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; //要过滤的ID低位
+
+    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; // 32位MASK
+    //    CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xfff8; // 最后3位不管
+    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; // 最后3位不管
+
+    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; // 过滤器0关联到FIFO0
+    CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;           // 激活过滤器0
+    CAN_FilterInit(&CAN_FilterInitStructure);                            // 滤波器初始化
+
+    CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // FIFO0消息挂号中断允许.
+    NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0; // 次优先级为0
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
+    NVIC_Init(&NVIC_InitStructure);
+
+    CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE); // FIFO0消息挂号中断允许.
+    NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_TX_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为1
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0; // 次优先级为0
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
+    NVIC_Init(&NVIC_InitStructure);
+    ////////////////////////////////////////////////
+
+    return 0;
+}
+
+/**********************************************************************************************************
+ *	函 数 名: CAN1_Init
+ *	功能说明: CAN1初始化
+ *	形    参: 无
+ *	返 回 值: 无
+ **********************************************************************************************************/
+void drv_can_init(void)
+{
+#if defined(__CAN_BAUD_500K)
+    drv_can1_mode_init(CAN_SJW_1tq, CAN_BS2_6tq, CAN_BS1_7tq, 6, CAN_Mode_Normal); // CAN1初始化环回模式,波特率500Kbps
+#elif defined(__CAN_BAUD_250K)
+    drv_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 drv_can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len)
+{
+    uint8_t flg = 0;
+    if (id_type)
+    {
+        tx_message.ExtId = id; // 设置扩展标示符(29位)
+    }
+    else
+    {
+        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);
+    flg = CAN_Transmit(CAN1, &tx_message);
+
+    return flg;
+}
+
+void drv_can_rx_back_init(pcan_rx p_fun)
+{
+    pcan_rx_back = p_fun;
+}
+
+void drv_can_tx_back_init(pcan_tx p_fun)
+{
+    pcan_tx_back = p_fun;
+}
+
+void CAN1_TX_IRQHandler(void)
+{
+    if (pcan_tx_back != NULL)
+    {
+        pcan_tx_back();
+    }
+}
+
+void CAN1_RX0_IRQHandler(void)
+{
+    CAN_Receive(CAN1, CAN_FIFO0, &rx_message);
+    if (pcan_rx_back != NULL)
+    {
+        pcan_rx_back(rx_message);
+    }
+}

+ 16 - 0
driver/can/drv_can.h

@@ -0,0 +1,16 @@
+#ifndef __DRV_CAN_H
+#define __DRV_CAN_H
+
+#include "stm32f4xx.h"
+
+#define __CAN_BAUD_250K
+
+typedef void (*pcan_tx)(void);
+typedef void (*pcan_rx)(CanRxMsg);
+
+void    drv_can_init(); // CAN初始化
+uint8_t drv_can_msg_tx(uint32_t id, uint8_t id_type, uint8_t *p_data, uint8_t len);
+void    drv_can_rx_back_init(pcan_rx p_fun);
+void    drv_can_tx_back_init(pcan_tx p_fun);
+
+#endif

+ 1 - 1
board/dwt/dwt.c → driver/dwt/dwt.c

@@ -15,7 +15,7 @@
 *
 *********************************************************************************************************
 */
-#include "global.h"
+#include "dwt.h"
 
 /*
 *********************************************************************************************************

+ 2 - 0
board/dwt/dwt.h → driver/dwt/dwt.h

@@ -11,6 +11,8 @@
 #ifndef __BSP_DWT_H
 #define __BSP_DWT_H
 
+#include <stm32f4xx.h>
+
 void dwt_init(void);
 void delay_us(uint32_t us_time);
 void delay_ms(uint32_t us_time);

+ 1 - 1
board/hal_flash.c → driver/flash/drv_flash.c

@@ -2,7 +2,7 @@
  *                                          Include                                              *
  ************************************************************************************************/
 
-#include "hal_flash.h"
+#include "drv_flash.h"
 #include "stm32f4xx.h"
 #include <stdint.h>
 

+ 6 - 6
board/hal_flash.h → driver/flash/drv_flash.h

@@ -1,14 +1,14 @@
-#ifndef __HAL_FLASH_H
-#define __HAL_FLASH_H
+#ifndef __DRV_FLASH_H
+#define __DRV_FLASH_H
 
 #ifdef __cplusplus
 extern 'C'
 {
 #endif
-#ifdef __HAL_FLASH_H_GLOBAL
-#define __HAL_FLASH_H_EXTERN
+#ifdef __DRV_FLASH_H_GLOBAL
+#define __DRV_FLASH_H_EXTERN
 #else
-#define __HAL_FLASH_H_EXTERN extern
+#define __DRV_FLASH_H_EXTERN extern
 #endif
 
 /************************************************************************************************
@@ -66,4 +66,4 @@ extern 'C'
 };
 #endif
 
-#endif // __HAL_FLASH_H
+#endif // __DRV_FLASH_H

+ 55 - 0
driver/gpio/drv_gpio.c

@@ -0,0 +1,55 @@
+#include "drv_gpio.h"
+
+void drv_beep_init(void)
+{
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
+
+    GPIO_InitTypeDef GPIO_StructInit;
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_OUT;
+    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   = DRV_GPIO_BEEP_PIN;
+
+    GPIO_Init(DRV_GPIO_BEEP_PORT, &GPIO_StructInit);
+}
+
+void drv_led_init(void)
+{
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
+
+    GPIO_InitTypeDef GPIO_StructInit;
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_OUT;
+    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   = DRV_GPIO_LED0_PIN | DRV_GPIO_LED1_PIN;
+
+    GPIO_Init(DRV_GPIO_LED0_PORT, &GPIO_StructInit);
+}
+
+void drv_key_init(void)
+{
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
+
+    GPIO_InitTypeDef GPIO_StructInit;
+    GPIO_StructInit.GPIO_Mode  = GPIO_Mode_IN;
+    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   = DRV_GPIO_KEY0_PIN | DRV_GPIO_KEY1_PIN | DRV_GPIO_KEY2_PIN;
+
+    GPIO_Init(DRV_GPIO_KEY0_PORT, &GPIO_StructInit);
+
+    GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_DOWN;
+    GPIO_StructInit.GPIO_Pin  = DRV_GPIO_KEY_UP_PIN;
+    GPIO_Init(DRV_GPIO_KEY_UP_PORT, &GPIO_StructInit);
+}
+
+void drv_gpio_init(void)
+{
+    drv_led_init();
+    drv_key_init();
+    drv_beep_init();
+}

+ 27 - 0
driver/gpio/drv_gpio.h

@@ -0,0 +1,27 @@
+#ifndef __DRV_GPIO_H
+#define __DRV_GPIO_H
+
+#include "stm32f4xx.h"
+
+#define DRV_GPIO_BEEP_PORT   GPIOF
+#define DRV_GPIO_BEEP_PIN    GPIO_Pin_8
+#define DRV_GPIO_LED0_PORT   GPIOF
+#define DRV_GPIO_LED0_PIN    GPIO_Pin_9
+#define DRV_GPIO_LED1_PORT   GPIOF
+#define DRV_GPIO_LED1_PIN    GPIO_Pin_10
+#define DRV_GPIO_KEY_UP_PORT GPIOA
+#define DRV_GPIO_KEY_UP_PIN  GPIO_Pin_0
+#define DRV_GPIO_KEY0_PORT   GPIOE
+#define DRV_GPIO_KEY0_PIN    GPIO_Pin_4
+#define DRV_GPIO_KEY1_PORT   GPIOE
+#define DRV_GPIO_KEY1_PIN    GPIO_Pin_3
+#define DRV_GPIO_KEY2_PORT   GPIOE
+#define DRV_GPIO_KEY2_PIN    GPIO_Pin_2
+
+#define DRV_GPIO_LED0(on_off)      ((on_off == 0) ? (GPIO_ResetBits(DRV_GPIO_LED0_PORT, DRV_GPIO_LED0_PIN)) : (GPIO_SetBits(DRV_GPIO_LED0_PORT, DRV_GPIO_LED0_PIN)))
+#define DRV_GPIO_LED1(on_off)      ((on_off == 0) ? (GPIO_ResetBits(DRV_GPIO_LED1_PORT, DRV_GPIO_LED1_PIN)) : (GPIO_SetBits(DRV_GPIO_LED1_PORT, DRV_GPIO_LED1_PIN)))
+#define DRV_GPIO_BEEP(beep_status) ((beep_status == 0) ? (GPIO_ResetBits(DRV_GPIO_BEEP_PORT, DRV_GPIO_BEEP_PIN)) : (GPIO_SetBits(DRV_GPIO_BEEP_PORT, DRV_GPIO_BEEP_PIN)))
+
+void drv_gpio_init(void);
+
+#endif // __DRV_GPIO_H

+ 70 - 0
driver/systick/drv_systick.c

@@ -0,0 +1,70 @@
+#include "drv_systick.h"
+#include "misc.h"
+#include <stdint.h>
+
+static u8  fac_us = 0; // us延时倍乘数
+static u16 fac_ms = 0; // ms延时倍乘数
+
+volatile uint32_t systick_ms = 0;
+// 初始化延迟函数
+// 当使用ucos的时候,此函数会初始化ucos的时钟节拍
+// SYSTICK的时钟固定为HCLK时钟的1/8
+// SYSCLK:系统时钟
+void drv_systick_init()
+{
+    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
+    if (SysTick_Config(SystemCoreClock / 1000))
+    {
+        while (1)
+        {
+        }
+    }
+    NVIC_SetPriority(SysTick_IRQn, 0x00);
+}
+
+// 延时nus
+// nus为要延时的us数.
+void drv_delay_us(uint32_t nus)
+{
+    uint32_t temp;
+    SysTick->LOAD = nus * fac_us;             // 时间加载
+    SysTick->VAL  = 0x00;                     // 清空计数器
+    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // 开始倒数
+    do
+    {
+        temp = SysTick->CTRL;
+    } while (temp & 0x01 && !(temp & (1 << 16))); // 等待时间到达
+    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // 关闭计数器
+    SysTick->VAL = 0X00;                       // 清空计数器
+}
+// 延时nms
+// 注意nms的范围
+// SysTick->LOAD为24位寄存器,所以,最大延时为:
+// nms<=0xffffff*8*1000/SYSCLK
+// SYSCLK单位为Hz,nms单位为ms
+// 对72M条件下,nms<=1864
+void drv_delay_ms(u16 nms)
+{
+    uint32_t temp;
+    SysTick->LOAD = (uint32_t)nms * fac_ms;   // 时间加载(SysTick->LOAD为24bit)
+    SysTick->VAL  = 0x00;                     // 清空计数器
+    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // 开始倒数
+    do
+    {
+        temp = SysTick->CTRL;
+    } while (temp & 0x01 && !(temp & (1 << 16))); // 等待时间到达
+    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // 关闭计数器
+    SysTick->VAL = 0X00;                       // 清空计数器
+}
+
+uint32_t get_systick_ms(void)
+{
+    uint32_t tick, tick_ms;
+    __disable_irq();
+    tick    = (SYSTICK_EXT_CLOCK_1MS_CNT - SysTick->VAL);
+    tick_ms = tick / (SYSTICK_EXT_CLOCK_1MS_CNT);
+    tick_ms = tick_ms + systick_ms;
+    __enable_irq();
+
+    return tick_ms;
+}

+ 14 - 0
driver/systick/drv_systick.h

@@ -0,0 +1,14 @@
+#ifndef __HAL_SYSTICK_H
+#define __HAL_SYSTICK_H
+
+#include <stdint.h>
+
+#define SYSTICK_EXT_CLOCK_1MS_CNT (SystemCoreClock / 1000)
+
+void drv_systick_init(void);
+
+uint32_t get_systick_ms(void);
+void     drv_delay_us(uint32_t nus);
+void     drv_delay_ms(uint16_t nms);
+
+#endif