加入EEPROM-BL24C02

This commit is contained in:
不吃油炸鸡
2025-10-20 20:16:11 +08:00
parent a43110f04a
commit 1d413c16c4
4 changed files with 192 additions and 11 deletions

View File

@@ -2,10 +2,10 @@
#include "i2c.h" #include "i2c.h"
SysSettings_T sys_settings = { SysSettings_T sys_settings = {
.backlight_level = 80, .backlight_level = 50,
.key_sound_enable = 1, .key_sound_enable = 1,
.language_select = 0, .language_select = 0,
.rotation = 180 .rotation = 0
}; };
static void BL24C02_Write(uint8_t addr, uint16_t length, uint8_t buff[]) 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: EEPROM Data description:
[0x00]:0x55 for check [0x00]: 0x55 for check
[0x01]:0xAA for check [0x01]: 0xAA for check
[0x10]:user wrist setting, HWInterface.IMU.wrist_is_enabled [0x10]: user backlight_level setting
[0x11]:user ui_APPSy_EN 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 EEPROM_Init_Check(void)
{ {
uint8_t check_buff[2]; uint8_t check_buff[2];
@@ -60,7 +63,7 @@ uint8_t EEPROM_Init_Check(void)
// to Save the settings // to Save the settings
void Sys_Setting_Save(void) void EEPROM_SysSetting_Save(void)
{ {
uint8_t buff[5]; uint8_t buff[5];
buff[0] = sys_settings.backlight_level; buff[0] = sys_settings.backlight_level;
@@ -73,7 +76,7 @@ void Sys_Setting_Save(void)
// to Get the settings // to Get the settings
void Sys_Setting_Get(void) void EEPROM_SysSetting_Get(void)
{ {
uint8_t buff[5]; uint8_t buff[5];
BL24C02_Read(0x10,5,buff); BL24C02_Read(0x10,5,buff);
@@ -96,3 +99,27 @@ void Sys_Setting_Get(void)
else else
sys_settings.rotation = 180; 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;
}
}

View File

@@ -2,6 +2,8 @@
#define __BL24C02_H #define __BL24C02_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <string.h>
#define BL24C02_ADDRESS 0x50 #define BL24C02_ADDRESS 0x50
@@ -15,7 +17,9 @@ typedef struct {
extern SysSettings_T sys_settings; extern SysSettings_T sys_settings;
uint8_t EEPROM_Init_Check(void); uint8_t EEPROM_Init_Check(void);
void Sys_Setting_Save(void); void EEPROM_SysSetting_Save(void);
void Sys_Setting_Get(void); void EEPROM_SysSetting_Get(void);
void EEPROM_UpdateCommand_Write(bool is_update);
bool EEPROM_UpdateCommand_Check(void);
#endif #endif

View File

@@ -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;
}
}

View File

@@ -0,0 +1,25 @@
#ifndef __BL24C02_H
#define __BL24C02_H
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#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