樊春春 5 mesiacov pred
rodič
commit
aa16e56260
8 zmenil súbory, kde vykonal 162 pridanie a 99 odobranie
  1. 5 4
      dev/dev_can.c
  2. 5 4
      dev/dev_can.h
  3. 1 1
      dev/dev_conf.h
  4. 71 66
      dev/dev_iap.c
  5. 13 6
      dev/dev_iap.h
  6. 61 16
      hal/hal_flash.c
  7. 1 1
      project/gcc/STM32F417IG_FLASH.ld
  8. 5 1
      user/main.c

+ 5 - 4
dev/dev_can.c

@@ -1,4 +1,5 @@
 #include "dev_can.h"
+#include "dev_iap.h"
 #include "hal_can.h"
 #include "hal_conf.h"
 #include "queue.h"
@@ -142,8 +143,8 @@ 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);
         en_queue(&can_rx_queue, data);
-    case 0x18CFD0EF:
-        data.id.r    = 0x18CFD0EF;
+    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);
@@ -228,8 +229,8 @@ can_rx_tab can_tab[] = {
     can_rx_param,
     0x18011801,
     can_rx_ctl,
-    0x18CFD0EF,
-    can_rx_update,
+    0x18DFF4E1,
+    iap_rec_handler,
 
 };
 

+ 5 - 4
dev/dev_can.h

@@ -20,9 +20,10 @@ typedef struct
 
 // #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();
+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

+ 1 - 1
dev/dev_conf.h

@@ -3,9 +3,9 @@
 
 #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

+ 71 - 66
dev/dev_iap.c

@@ -3,7 +3,6 @@
  ************************************************************************************************/
 #include "dev_iap.h"
 #include "dev_at24cxx.h"
-#include "hal_conf.h"
 #include "hal_flash.h"
 #include "queue.h"
 #include <stdint.h>
@@ -78,6 +77,10 @@ void set_firmver(void)
     fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
     fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
 
+    for (uint8_t i = 0; i < UPGRADE_APP_DATA_SIZE; i++)
+    {
+        firm_ver_buff[i + UPGRADE_FLAG_DATA_SIZE] = firm_ver[i];
+    }
     fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
     fmc_clear_flag_end();
 }
@@ -105,8 +108,8 @@ uint8_t erase_app_flash(uint32_t start_address, uint32_t end_addr)
 {
     uint8_t  status   = 0;
     uint16_t page_len = 0;
-    page_len          = (end_addr - start_address) / FMC_PAGE_SIZE;
-    status            = fmc_erase_pages(start_address, page_len);
+    // page_len          = (end_addr - start_address) / FMC_PAGE_SIZE;
+    status = fmc_erase_pages(start_address, page_len);
     return status;
 }
 
@@ -192,73 +195,75 @@ void iap_rec_handler(pdu_tag rec_msg)
             send_buff[4] = g_iap.count_packages >> 8;
             send_buff[5] = g_iap.count_packages & 0xFF;
         }
-        else
+    }
+    else
+    {
+        switch (rec_msg.data.u8_buf[0])
         {
-            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++)
             {
-            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_START_ADDR * 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_END_ADDR / 2);
-                    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;
-            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;
-                }
-                break;
-            case IAP_CMD_MODEL_TYPE:
-                send_buff[0] = IAP_CMD_MODEL_TYPE;
-                send_buff[1] = 0x01;
+                send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
+            }
 
-                send_buff[2] = DEVICE_ID >> 8;
-                send_buff[3] = DEVICE_ID & 0xFF;
+            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;
 
-        // push_can_message_to_queue(IAP, 8, send_buff);
+            send_buff[2] = DEVICE_ID >> 8;
+            send_buff[3] = DEVICE_ID & 0xFF;
+        }
     }
+
+    push_can_message_to_queue(0x18DFE1F4, 8, send_buff);
 }

+ 13 - 6
dev/dev_iap.h

@@ -26,6 +26,8 @@ extern 'C'
 /************************************************************************************************
  *                                          Includes                                             *
  ************************************************************************************************/
+#include "dev_can.h"
+#include "hal_conf.h"
 #include <stdint.h>
 
     /************************************************************************************************
@@ -36,14 +38,15 @@ extern 'C'
 #define UPGRADE_APP_DATA_SIZE  4                                                // 升级参数数组元素个数
 #define UPGRADE_BUFF_DATA_SIZE (UPGRADE_FLAG_DATA_SIZE + UPGRADE_APP_DATA_SIZE) // 升级参数数组元素个数
 
-#define APP_START_ADDR ((uint32_t)0x08007000)
-#define APP_END_ADDR   ((uint32_t)0x08039000)
+#define APP_START_ADDR ((uint32_t)0x08010000)
+#define APP_END_ADDR   ((uint32_t)0x08060000)
 
-#define UPGRADE_FLAG_START_ADDR ((uint32_t)0x08039000)
-#define UPGRADE_FLAG_END_ADDR   ((uint32_t)0x0803A000)
+#define UPGRADE_FLAG_START_ADDR ((uint32_t)0x08060000)
+#define UPGRADE_FLAG_END_ADDR   ((uint32_t)0x08061000)
 
-#define DOWNLOAD_START_ADDR ((uint32_t)0x0803A000)
-#define DOWNLOAD_END_ADDR   ((uint32_t)0x0806C000)
+#define DOWNLOAD_START_ADDR  ((uint32_t)0x08080000)
+#define DOWNLOAD_END_ADDR    ((uint32_t)0x080C0000)
+#define DOWNLOAD_MIDDLE_ADDR ((uint32_t)0x080A0000)
 
 // 升级命令
 #define IAP_CMD_VERSION    0x71   // 查询版本号
@@ -97,6 +100,10 @@ extern 'C'
         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 iap_rec_handler(pdu_tag rec_msg);
 /************************************************************************************************
  *                                          Defines                                              *
  ************************************************************************************************/

+ 61 - 16
hal/hal_flash.c

@@ -108,25 +108,32 @@ uint8_t fmc_read_one_page(uint32_t read_addr, uint16_t *buff)
 // 擦出N页, 地址必须对齐页首
 uint8_t fmc_erase_pages(uint32_t erase_addr, uint16_t len)
 {
-    uint16_t i = 0, j = 0;
-
-    uint32_t *addr = (uint32_t *)erase_addr;
-
-    if (erase_addr % FMC_PAGE_SIZE != 0)
-        return 1; // 错误返回
-
-    for (i = 0; i < len; i++)
+    uint8_t status = 0;
+    status         = FLASH_EraseSector(fmc_get_flash_sector(erase_addr), VoltageRange_3);
+    if (status != FLASH_COMPLETE)
     {
-        FLASH_EraseSector(fmc_get_flash_sector(erase_addr), VoltageRange_3);
-        for (j = 0; j < 512; j++)
-        {
-            if (*addr != 0xFFFFFFFF)
-                return 1;
-            else
-                addr++;
-        }
+        return 1;
     }
     return 0;
+    // uint16_t i = 0, j = 0;
+
+    // uint32_t *addr = (uint32_t *)erase_addr;
+
+    // if (erase_addr % FMC_PAGE_SIZE != 0)
+    //     return 1; // 错误返回
+
+    // for (i = 0; i < len; i++)
+    // {
+    //     FLASH_EraseSector(fmc_get_flash_sector(erase_addr), VoltageRange_3);
+    //     for (j = 0; j < 512; j++)
+    //     {
+    //         if (*addr != 0xFFFFFFFF)
+    //             return 1;
+    //         else
+    //             addr++;
+    //     }
+    // }
+    // return 0;
 }
 
 // 只写入,写入前需擦除,地址必须两字节对齐
@@ -152,6 +159,44 @@ uint8_t fmc_write_n_half_word(uint32_t write_addr, uint16_t *buff, uint16_t len)
     return 0;
 }
 
+// uint8_t stm_flash_write(uint32_t write_addr, uint16_t *buff, uint32_t len)
+// {
+//     uint8_t      res   = 1;
+//     uint32_t     index = 0, temp_addr = write_addr;
+//     FLASH_Status status = FLASH_COMPLETE;
+//     for (index = 0; index < len;)
+//     {
+//         if (fmc_read_half_word(temp_addr) != 0xFFFF)
+//         {
+//             status = FLASH_EraseSector(fmc_get_flash_sector(temp_addr), VoltageRange_3);
+//             if (status != FLASH_COMPLETE)
+//             {
+//                 res = 0;
+//                 break;
+//             }
+//         }
+//         else
+//         {
+//             index++;
+//             temp_addr += 2;
+//         }
+//     }
+//     if (status == FLASH_COMPLETE)
+//     {
+//         for (index = 0; index < len; index++)
+//         {
+//             if (FLASH_ProgramHalfWord(write_addr, buff[index]) != FLASH_COMPLETE) // 写入数据
+//             {
+//                 res = 0;
+//                 // 写入异常
+//                 break;
+//             }
+//             write_addr += 2;
+//         }
+//     }
+//     return res;
+// }
+
 uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t *pbuff, uint32_t len)
 {
     uint16_t temp = 0;

+ 1 - 1
project/gcc/STM32F417IG_FLASH.ld

@@ -40,7 +40,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
 /* Specify the memory areas */
 MEMORY
 {
-  FLASH (rx)      : ORIGIN = 0x08007000, LENGTH = 960K
+  FLASH (rx)      : ORIGIN = 0x08010000, LENGTH = 960K
   RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 128K
   MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
   CCMRAM (rw)     : ORIGIN = 0x10000000, LENGTH = 64K

+ 5 - 1
user/main.c

@@ -6,7 +6,7 @@
 #include "hal_conf.h"
 #include "misc.h"
 
-#define APP_FLASH_OFFSET 0x7000
+#define APP_FLASH_OFFSET 0x10000
 #define BOOT_EN          1
 
 int main(void)
@@ -25,6 +25,10 @@ int main(void)
     hal_i2c1_init();
     dev_key_button_init();
 
+    set_firmver();
+    iap_flag_release();
+    iap_param_init();
+
     while (1)
     {
         app_task_schdule();