Skip to content

feat: eeprom drivers - #141

Open
jratke587 wants to merge 3 commits into
masterfrom
eeprom
Open

feat: eeprom drivers#141
jratke587 wants to merge 3 commits into
masterfrom
eeprom

Conversation

@jratke587

@jratke587 jratke587 commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Implements basic read and write functions with DMA and blocking modes and uses write protect pin. Untested but it builds. I'm not sure exactly what data is being stored or how it should be structured on the eeprom, so i haven't done anything for that.

closes #131
closes #132

@jratke587
jratke587 requested a review from wxkim as a code owner April 29, 2026 23:52
@wxkim wxkim changed the title some eeprom code feat: eeprom drivers Apr 30, 2026

@wxkim wxkim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work. i have left file specific comments below.

some overall feedback:

your error handling is a good start, but is a little weak right now. eventually, logging at the api level will simply call log(data), and you will need to handle everything under the hood, including errors, automatically moving the write address, and keeping track of where the last write/read operation was under power loss, etc

in order for your code to support that, you will need to use objects to keep track of everything, so i recommend integrating your errors and states into enums and structs to hold all of that.

also make sure to use fixed width integers (and specify signed or unsigned).

keep in mind that you will eventually have to start thinking about how to format data upon log being called. you will have to design your code around EEPROM read and write lifecycles as well


#define EEPROM_ADDRESS 0x50

void setWriteProtect(int enable) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would make these separate functions

return 0;
}

void eeprom_TxCpltCallback() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is there, but call back functions must match the function signature as called by the IRQ.

in this instance, you should use HAL_I2C_MemTxCpltCallback or whichever is applicable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eeprom_TxCpltCallback will never get called by the IRQ


#define EEPROM_ADDRESS 0x50

void setWriteProtect(int enable) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in firmware we want to use fixed width integers. so int should be uint8_t or uint16_t or int32_t etc

}

int eeprom_init()
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this function need a return type?


int eeprom_ready()
{
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(&hi2c1, (EEPROM_ADDRESS << 1), 3, 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the 3 and 1 here are called magic numbers, which you should avoid.

try to find a way to make them passed as variables or at the very least named

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing with EEPROM_ADDRESS. why isnt it <<1'd to begin with? (rhetorical)

return 0;
}

int eeprom_ready()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this function is limited to core/ only, i suggest taking the HAL return types and handling them.

{
int ready = eeprom_ready();
if (ready == 1) return 0;
else if (ready == -1) return -1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors need to be handled more granularly. -1 is not enough information, and when you test code, you will definitely want more info

@@ -0,0 +1,18 @@
#pragma once

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this is correct and works, we use standard include guards across all project modules, so this should be updated accordingly.

#include "stm32g4xx_hal.h"
#include "main.h"

extern I2C_HandleTypeDef hi2c1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good use of extern

Comment thread bms/Core/Inc/main.h
#define T_SWDIO_GPIO_Port GPIOA
#define T_SWCLK_Pin GPIO_PIN_14
#define T_SWCLK_GPIO_Port GPIOA
#define WriteProtect_EEPROM_Pin GPIO_PIN_11

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would use your own copy in side eeprom.h and not use these incase codegen messes up one day

@wxkim wxkim assigned wxkim, jratke587 and alexshidagoatnocap and unassigned wxkim Apr 30, 2026
@wxkim wxkim added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed area:eeprom labels Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:eeprom enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MEM-002 | Implement EEPROM write API MEM-001 | Implement EEPROM read API

3 participants