-
Notifications
You must be signed in to change notification settings - Fork 0
feat: eeprom drivers #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: eeprom drivers #141
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| #include "eeprom.h" | ||
|
|
||
| #define EEPROM_ADDRESS 0x50 | ||
|
|
||
| void setWriteProtect(int enable) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| if (enable) { | ||
| HAL_GPIO_WritePin(WriteProtect_EEPROM_GPIO_Port, WriteProtect_EEPROM_Pin, GPIO_PIN_SET); | ||
| } else { | ||
| HAL_GPIO_WritePin(WriteProtect_EEPROM_GPIO_Port, WriteProtect_EEPROM_Pin, GPIO_PIN_RESET); | ||
| } | ||
| } | ||
|
|
||
| int eeprom_init() | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this function need a return type? |
||
| setWriteProtect(1); | ||
| return 0; | ||
| } | ||
|
|
||
| int eeprom_ready() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this function is limited to |
||
| { | ||
| HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(&hi2c1, (EEPROM_ADDRESS << 1), 3, 1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the try to find a way to make them passed as variables or at the very least named
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing with |
||
|
|
||
| if (status == HAL_OK) | ||
| { | ||
| return 1; | ||
| } | ||
| else if (status == HAL_BUSY || status == HAL_TIMEOUT) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| int eeprom_wait_until_ready(uint32_t timeout_ms) | ||
| { | ||
| uint32_t tickstart = HAL_GetTick(); | ||
| while ((HAL_GetTick() - tickstart) < timeout_ms) | ||
| { | ||
| int ready = eeprom_ready(); | ||
| if (ready == 1) return 0; | ||
| else if (ready == -1) return -1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| HAL_Delay(5); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| int eeprom_write(uint16_t address, uint8_t *data, size_t size) | ||
| { | ||
| if (size > 128) | ||
| return -1; | ||
|
|
||
| setWriteProtect(0); | ||
|
|
||
| HAL_StatusTypeDef status = HAL_I2C_Mem_Write( | ||
| &hi2c1, | ||
| (EEPROM_ADDRESS << 1), | ||
| address, | ||
| I2C_MEMADD_SIZE_16BIT, | ||
| data, | ||
| size, | ||
| 10); | ||
|
|
||
| setWriteProtect(1); | ||
|
|
||
| if (status != HAL_OK) | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int eeprom_write_dma(uint16_t address, uint8_t *data, size_t size) { | ||
| if (hi2c1.State != HAL_I2C_STATE_READY) return -1; | ||
|
|
||
| setWriteProtect(0); | ||
|
|
||
| HAL_StatusTypeDef status = HAL_I2C_Mem_Write_DMA( | ||
| &hi2c1, | ||
| (EEPROM_ADDRESS << 1), | ||
| address, | ||
| I2C_MEMADD_SIZE_16BIT, | ||
| data, | ||
| size | ||
| ); | ||
|
|
||
| if (status != HAL_OK) { | ||
| setWriteProtect(1); | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| void eeprom_TxCpltCallback() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| setWriteProtect(1); | ||
| } | ||
|
|
||
| int eeprom_read(uint16_t address, uint8_t *buffer, size_t size) | ||
| { | ||
| if (buffer == NULL || size == 0) | ||
| return -1; | ||
|
|
||
| HAL_StatusTypeDef status = HAL_I2C_Mem_Read( | ||
| &hi2c1, | ||
| (EEPROM_ADDRESS << 1), | ||
| address, | ||
| I2C_MEMADD_SIZE_16BIT, | ||
| buffer, | ||
| size, | ||
| 10); | ||
|
|
||
| if (status != HAL_OK) | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| int eeprom_read_dma(uint16_t address, uint8_t *buffer, size_t size) { | ||
| if (hi2c1.State != HAL_I2C_STATE_READY) return -1; | ||
|
|
||
| HAL_StatusTypeDef status = HAL_I2C_Mem_Read_DMA( | ||
| &hi2c1, | ||
| (EEPROM_ADDRESS << 1), | ||
| address, | ||
| I2C_MEMADD_SIZE_16BIT, | ||
| buffer, | ||
| size | ||
| ); | ||
|
|
||
| if (status != HAL_OK) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good use of extern |
||
|
|
||
| int eeprom_init(); | ||
|
|
||
| int eeprom_ready(); | ||
| int eeprom_wait_until_ready(uint32_t timeout_ms); | ||
|
|
||
| int eeprom_write(uint16_t address, uint8_t* data, size_t size); | ||
| int eeprom_read(uint16_t address, uint8_t* buffer, size_t size); | ||
|
|
||
| int eeprom_write_dma(uint16_t address, uint8_t* data, size_t size); | ||
| int eeprom_read_dma(uint16_t address, uint8_t* buffer, size_t size); | ||
|
|
||
| void eeprom_TxCpltCallback(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,7 @@ target_link_libraries(${PROJECT_NAME} | |
| adbms | ||
| segment | ||
| charger | ||
| eeprom | ||
| program | ||
| daemons | ||
| threads | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,8 @@ void Error_Handler(void); | |
| #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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| #define WriteProtect_EEPROM_GPIO_Port GPIOC | ||
|
|
||
| /* USER CODE BEGIN Private defines */ | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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