From 1d413c16c46e2275179ba57c0f8a57d4ba2d8c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=90=83=E6=B2=B9=E7=82=B8=E9=B8=A1?= <1425962791@qq.com> Date: Mon, 20 Oct 2025 20:16:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5EEPROM-BL24C02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Power_Pico/BSP/{BL24C02 => EEPROM}/BL24C02.c | 45 +++++-- Power_Pico/BSP/{BL24C02 => EEPROM}/BL24C02.h | 8 +- bootloader/BSP/EEPROM/BL24C02.c | 125 +++++++++++++++++++ bootloader/BSP/EEPROM/BL24C02.h | 25 ++++ 4 files changed, 192 insertions(+), 11 deletions(-) rename Power_Pico/BSP/{BL24C02 => EEPROM}/BL24C02.c (69%) rename Power_Pico/BSP/{BL24C02 => EEPROM}/BL24C02.h (66%) create mode 100644 bootloader/BSP/EEPROM/BL24C02.c create mode 100644 bootloader/BSP/EEPROM/BL24C02.h diff --git a/Power_Pico/BSP/BL24C02/BL24C02.c b/Power_Pico/BSP/EEPROM/BL24C02.c similarity index 69% rename from Power_Pico/BSP/BL24C02/BL24C02.c rename to Power_Pico/BSP/EEPROM/BL24C02.c index dcaa7d1..131fecb 100644 --- a/Power_Pico/BSP/BL24C02/BL24C02.c +++ b/Power_Pico/BSP/EEPROM/BL24C02.c @@ -2,10 +2,10 @@ #include "i2c.h" SysSettings_T sys_settings = { - .backlight_level = 80, + .backlight_level = 50, .key_sound_enable = 1, .language_select = 0, - .rotation = 180 + .rotation = 0 }; static void BL24C02_Write(uint8_t addr, uint16_t length, uint8_t buff[]) @@ -25,15 +25,18 @@ static void delay_ms(uint32_t ms) /****************************************** EEPROM Data description: -[0x00]:0x55 for check -[0x01]:0xAA for check +[0x00]: 0x55 for check +[0x01]: 0xAA for check -[0x10]:user wrist setting, HWInterface.IMU.wrist_is_enabled -[0x11]:user ui_APPSy_EN setting +[0x10]: user backlight_level setting +[0x11]: user key_sound_enable setting +[0x12]: user language_select setting +[0x13-14]: user rotation setting +[0x20-]: update command storage area, "update\r\n" *******************************************/ -// to check the Data is right and the EEPROM is working +// to check the Data is right and the EEPROM is working, 0-ok, 1-erro uint8_t EEPROM_Init_Check(void) { uint8_t check_buff[2]; @@ -60,7 +63,7 @@ uint8_t EEPROM_Init_Check(void) // to Save the settings -void Sys_Setting_Save(void) +void EEPROM_SysSetting_Save(void) { uint8_t buff[5]; buff[0] = sys_settings.backlight_level; @@ -73,7 +76,7 @@ void Sys_Setting_Save(void) // to Get the settings -void Sys_Setting_Get(void) +void EEPROM_SysSetting_Get(void) { uint8_t buff[5]; BL24C02_Read(0x10,5,buff); @@ -96,3 +99,27 @@ void Sys_Setting_Get(void) else sys_settings.rotation = 180; } + +// to write the update command +void EEPROM_UpdateCommand_Write(bool is_update) +{ + if(is_update) { + char cmd[] = "update\r\n"; + BL24C02_Write(0x20, strlen(cmd), (uint8_t *)cmd); + } else { + char cmd[] = "-nope-\r\n"; + BL24C02_Write(0x20, strlen(cmd), (uint8_t *)cmd); + } +} + +// to check the update command +bool EEPROM_UpdateCommand_Check(void) +{ + char cmd[10] = {0}; + BL24C02_Read(0x20, 8, (uint8_t *)cmd); + if(strcmp(cmd, "update\r\n") == 0) { + return true; + } else { + return false; + } +} diff --git a/Power_Pico/BSP/BL24C02/BL24C02.h b/Power_Pico/BSP/EEPROM/BL24C02.h similarity index 66% rename from Power_Pico/BSP/BL24C02/BL24C02.h rename to Power_Pico/BSP/EEPROM/BL24C02.h index d96dd1c..c20df71 100644 --- a/Power_Pico/BSP/BL24C02/BL24C02.h +++ b/Power_Pico/BSP/EEPROM/BL24C02.h @@ -2,6 +2,8 @@ #define __BL24C02_H #include +#include +#include #define BL24C02_ADDRESS 0x50 @@ -15,7 +17,9 @@ typedef struct { extern SysSettings_T sys_settings; uint8_t EEPROM_Init_Check(void); -void Sys_Setting_Save(void); -void Sys_Setting_Get(void); +void EEPROM_SysSetting_Save(void); +void EEPROM_SysSetting_Get(void); +void EEPROM_UpdateCommand_Write(bool is_update); +bool EEPROM_UpdateCommand_Check(void); #endif diff --git a/bootloader/BSP/EEPROM/BL24C02.c b/bootloader/BSP/EEPROM/BL24C02.c new file mode 100644 index 0000000..131fecb --- /dev/null +++ b/bootloader/BSP/EEPROM/BL24C02.c @@ -0,0 +1,125 @@ +#include "BL24C02.h" +#include "i2c.h" + +SysSettings_T sys_settings = { + .backlight_level = 50, + .key_sound_enable = 1, + .language_select = 0, + .rotation = 0 +}; + +static void BL24C02_Write(uint8_t addr, uint16_t length, uint8_t buff[]) +{ + HAL_I2C_Mem_Write(&hi2c1, BL24C02_ADDRESS<<1, addr, I2C_MEMADD_SIZE_8BIT, buff, length, 10); +} + +static void BL24C02_Read(uint8_t addr, uint8_t length, uint8_t buff[]) +{ + HAL_I2C_Mem_Read(&hi2c1, BL24C02_ADDRESS<<1, addr, I2C_MEMADD_SIZE_8BIT, buff, length, 10); +} + +static void delay_ms(uint32_t ms) +{ + HAL_Delay(ms); +} + +/****************************************** +EEPROM Data description: +[0x00]: 0x55 for check +[0x01]: 0xAA for check + +[0x10]: user backlight_level setting +[0x11]: user key_sound_enable setting +[0x12]: user language_select setting +[0x13-14]: user rotation setting + +[0x20-]: update command storage area, "update\r\n" +*******************************************/ + +// to check the Data is right and the EEPROM is working, 0-ok, 1-erro +uint8_t EEPROM_Init_Check(void) +{ + uint8_t check_buff[2]; + delay_ms(10); + BL24C02_Read(0,2,check_buff); + if(check_buff[0] == 0x55 && check_buff[1] == 0xAA) + { + return 0;//check OK + } + else + { + check_buff[0] = 0x55; + check_buff[1] = 0xAA; + delay_ms(10); + BL24C02_Write(0,2,check_buff); + memset(check_buff,0,2); + delay_ms(10); + BL24C02_Read(0,2,check_buff); + if(check_buff[0] == 0x55 && check_buff[1] == 0xAA) + return 0;//check ok + } + return 1;//check erro +} + + +// to Save the settings +void EEPROM_SysSetting_Save(void) +{ + uint8_t buff[5]; + buff[0] = sys_settings.backlight_level; + buff[1] = sys_settings.key_sound_enable; + buff[2] = sys_settings.language_select; + buff[3] = (uint8_t)(sys_settings.rotation & 0x00FF); + buff[4] = (uint8_t)((sys_settings.rotation >> 8) & 0x00FF); + BL24C02_Write(0x10,5,buff); +} + + +// to Get the settings +void EEPROM_SysSetting_Get(void) +{ + uint8_t buff[5]; + BL24C02_Read(0x10,5,buff); + // 判断是否在范围内 + if(buff[0] <= 100) + sys_settings.backlight_level = buff[0]; + else + sys_settings.backlight_level = 80; + if(buff[1] <= 1) + sys_settings.key_sound_enable = buff[1]; + else + sys_settings.key_sound_enable = 1; + if(buff[2] <= 1) + sys_settings.language_select = buff[2]; + else + sys_settings.language_select = 0; + uint16_t rotation = (uint16_t)buff[3] | ((uint16_t)buff[4] << 8); + if(rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270) + sys_settings.rotation = rotation; + else + sys_settings.rotation = 180; +} + +// to write the update command +void EEPROM_UpdateCommand_Write(bool is_update) +{ + if(is_update) { + char cmd[] = "update\r\n"; + BL24C02_Write(0x20, strlen(cmd), (uint8_t *)cmd); + } else { + char cmd[] = "-nope-\r\n"; + BL24C02_Write(0x20, strlen(cmd), (uint8_t *)cmd); + } +} + +// to check the update command +bool EEPROM_UpdateCommand_Check(void) +{ + char cmd[10] = {0}; + BL24C02_Read(0x20, 8, (uint8_t *)cmd); + if(strcmp(cmd, "update\r\n") == 0) { + return true; + } else { + return false; + } +} diff --git a/bootloader/BSP/EEPROM/BL24C02.h b/bootloader/BSP/EEPROM/BL24C02.h new file mode 100644 index 0000000..c20df71 --- /dev/null +++ b/bootloader/BSP/EEPROM/BL24C02.h @@ -0,0 +1,25 @@ +#ifndef __BL24C02_H +#define __BL24C02_H + +#include +#include +#include + +#define BL24C02_ADDRESS 0x50 + +typedef struct { + uint8_t backlight_level; // 0-100 + uint8_t key_sound_enable; // 0:disable, 1:enable + uint8_t language_select; // 0:English, 1:Chinese + uint16_t rotation; // 0, 90, 180, 270 +} SysSettings_T; + +extern SysSettings_T sys_settings; + +uint8_t EEPROM_Init_Check(void); +void EEPROM_SysSetting_Save(void); +void EEPROM_SysSetting_Get(void); +void EEPROM_UpdateCommand_Write(bool is_update); +bool EEPROM_UpdateCommand_Check(void); + +#endif