├── .github └── FUNDING.yml ├── LICENSE.TXT ├── README.md ├── NimaLTD.I-CUBE-EE24_conf.h ├── ee24.h └── ee24.c /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | github: [nimaltd] 3 | patreon: # Replace with a single Patreon username 4 | open_collective: # Replace with a single Open Collective username 5 | ko_fi: nimaltd 6 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 7 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 8 | liberapay: #nimaltd 9 | issuehunt: #nimaltd 10 | otechie: # Replace with a single Otechie username 11 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 12 | -------------------------------------------------------------------------------- /LICENSE.TXT: -------------------------------------------------------------------------------- 1 | Software License Terms 2 | 3 | This software is dual-licensed: 4 | 5 | 1. GNU General Public License v2 (GPLv2): 6 | - You may redistribute and/or modify this software under the terms of GPLv2 as published by the Free Software Foundation. 7 | - This license does not provide any warranty of any kind, including but not limited to MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | - See for details. 9 | 10 | 2. Commercial License: 11 | - The commercial license removes all GPLv2 restrictions and allows you to redistribute your closed-source products with the Library embedded. 12 | - This license includes access to software maintenance, such as updates and upgrades. Customers who purchase this license are eligible to receive updates whenever 13 | they are released. However, the software provider is not obligated to release updates on a specific schedule. 14 | - The commercial license is available in two models: 15 | - Single-Product License: Allows usage in one specific product. 16 | - Company-Wide License: Allows usage across all products of the company. 17 | - No Warranty: The software is provided "AS IS" without any warranties, express or implied, including but not limited to the implied warranties of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, or NON-INFRINGEMENT. 18 | - Liability Disclaimer: In no event shall the copyright holder or contributors be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software. 19 | 20 | By using this software under the commercial license, you agree to these terms and acknowledge that no additional guarantees or obligations are provided beyond what is explicitly stated in this document. 21 | 22 | --- 23 | 24 | Copyright © Nima Askari, 2025. All rights reserved. 25 | For inquiries, contact: nima.askari@gmail.com All rights reserved 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 24xx EEPROM library for stm32 HAL 2 | --- 3 | ## Please Do not Forget to get STAR, DONATE and support me on social networks. Thank you. :sparkling_heart: 4 | --- 5 | - Author: Nima Askari 6 | - Github: https://www.github.com/NimaLTD 7 | - Youtube: https://www.youtube.com/@nimaltd 8 | - LinkedIn: https://www.linkedin.com/in/nimaltd 9 | - Instagram: https://instagram.com/github.NimaLTD 10 | --- 11 | * Install Library from https://github.com/nimaltd/STM32-PACK/raw/main/EE24/NimaLTD.I-CUBE-EE24.pdsc 12 | * Add and enable it. 13 | * Enable I2C. 14 | * Select 'Generate peripheral initialization as a pair of .c/.h files per peripheral' on the Code Generator Tab. 15 | * Generate code. 16 | * Define a structure of `EE24_HandleTypeDef`. 17 | * Call `EE24_Init()` and enjoy. 18 | --- 19 | solve F1 i2c problem 20 | ``` c 21 | 22 | void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) 23 | { 24 | 25 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 26 | if(i2cHandle->Instance==I2C2) 27 | { 28 | /* USER CODE BEGIN I2C2_MspInit 0 */ 29 | __HAL_RCC_I2C2_CLK_ENABLE(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< add manualy 30 | /* USER CODE END I2C2_MspInit 0 */ 31 | 32 | __HAL_RCC_GPIOB_CLK_ENABLE(); 33 | /**I2C2 GPIO Configuration 34 | PB10 ------> I2C2_SCL 35 | PB11 ------> I2C2_SDA 36 | */ 37 | GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; 38 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; 39 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 40 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 41 | 42 | /* I2C2 clock enable */ 43 | __HAL_RCC_I2C2_CLK_ENABLE(); 44 | /* USER CODE BEGIN I2C2_MspInit 1 */ 45 | 46 | /* USER CODE END I2C2_MspInit 1 */ 47 | } 48 | } 49 | ``` 50 | example: 51 | ``` c 52 | #include "ee24.h" 53 | 54 | EE24_HandleTypeDef ee24; 55 | uint8_t data[1024]; 56 | int main(void) 57 | { 58 | ... 59 | ... 60 | ... 61 | if (EE24_Init(&ee24, &hi2c1, EE24_ADDRESS_DEFAULT)) 62 | { 63 | EE24_Read(&ee24, 0, data, 1024, 1000); 64 | } 65 | while(1) 66 | { 67 | 68 | } 69 | } 70 | ``` 71 | -------------------------------------------------------------------------------- /NimaLTD.I-CUBE-EE24_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * File Name : NimaLTD.I-CUBE-EE24_conf.h 4 | * Description : This file provides code for the configuration 5 | * of the NimaLTD.I-CUBE-EE24_conf.h instances. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2024 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef _NIMALTD_I_CUBE_EE24_CONF_H_ 20 | #define _NIMALTD_I_CUBE_EE24_CONF_H_ 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | #define EE24_1KBIT 1 27 | #define EE24_2KBIT 2 28 | #define EE24_4KBIT 4 29 | #define EE24_8KBIT 8 30 | #define EE24_16KBIT 16 31 | #define EE24_32KBIT 32 32 | #define EE24_64KBIT 64 33 | #define EE24_128KBIT 128 34 | #define EE24_256KBIT 256 35 | #define EE24_512KBIT 512 36 | 37 | #define EE24_RTOS_DISABLE 0 38 | #define EE24_RTOS_CMSIS_V1 1 39 | #define EE24_RTOS_CMSIS_V2 2 40 | #define EE24_RTOS_THREADX 3 41 | 42 | /** 43 | MiddleWare name : NimaLTD.I-CUBE-EE24.3.2.0 44 | MiddleWare fileName : ./NimaLTD.I-CUBE-EE24_conf.h 45 | */ 46 | /*---------- EE24_SIZE -----------*/ 47 | #define EE24_SIZE EE24_1KBIT 48 | 49 | /*---------- EE24_RTOS -----------*/ 50 | #define EE24_RTOS EE24_RTOS_DISABLE 51 | 52 | /*---------- EE24_USE_WP_PIN -----------*/ 53 | #define EE24_USE_WP_PIN false 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | #endif /* _NIMALTD_I_CUBE_EE24_CONF_H_ */ 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 65 | 66 | -------------------------------------------------------------------------------- /ee24.h: -------------------------------------------------------------------------------- 1 | #ifndef _EE24_H_ 2 | #define _EE24_H_ 3 | 4 | /*********************************************************************************************************** 5 | 6 | Author: Nima Askari 7 | Github: https://www.github.com/NimaLTD 8 | LinkedIn: https://www.linkedin.com/in/nimaltd 9 | Youtube: https://www.youtube.com/@nimaltd 10 | Instagram: https://instagram.com/github.NimaLTD 11 | 12 | Version: 3.2.0 13 | 14 | History: 15 | 3.2.0 16 | - Added ThreadX Configuration 17 | 18 | 3.1.0 19 | - Added WP pin control 20 | - Fix 1Kb and 2Kb memory writing 21 | 22 | 3.0.1 23 | - Fixed Address 24 | 25 | 3.0.0 26 | - Rewrite again 27 | - Support STM32CubeMx Packet installer 28 | 29 | ***********************************************************************************************************/ 30 | 31 | #ifdef __cplusplus 32 | extern "C" 33 | { 34 | #endif 35 | 36 | /************************************************************************************************************ 37 | ************** Include Headers 38 | ************************************************************************************************************/ 39 | 40 | #include 41 | #include "i2c.h" 42 | #include "NimaLTD.I-CUBE-EE24_conf.h" 43 | 44 | /************************************************************************************************************ 45 | ************** Public Definitions 46 | ************************************************************************************************************/ 47 | 48 | #define EE24_ADDRESS_DEFAULT 0xA0 49 | 50 | /************************************************************************************************************ 51 | ************** Public struct/enum 52 | ************************************************************************************************************/ 53 | 54 | typedef struct 55 | { 56 | I2C_HandleTypeDef *HI2c; 57 | uint8_t Address; 58 | uint8_t Lock; 59 | #if EE24_USE_WP_PIN == true 60 | GPIO_TypeDef *WpGpio; 61 | uint16_t WpPin; 62 | #endif 63 | 64 | } EE24_HandleTypeDef; 65 | 66 | /************************************************************************************************************ 67 | ************** Public Functions 68 | ************************************************************************************************************/ 69 | 70 | #if EE24_USE_WP_PIN == false 71 | bool EE24_Init(EE24_HandleTypeDef *Handle, I2C_HandleTypeDef *HI2c, uint8_t I2CAddress); 72 | #else 73 | bool EE24_Init(EE24_HandleTypeDef *Handle, I2C_HandleTypeDef *HI2c, uint8_t I2CAddress, GPIO_TypeDef *WpGpio, uint16_t WpPin); 74 | #endif 75 | bool EE24_Read(EE24_HandleTypeDef *Handle, uint32_t Address, uint8_t *Data, size_t Len, uint32_t Timeout); 76 | bool EE24_Write(EE24_HandleTypeDef *Handle, uint32_t Address, uint8_t *Data, size_t Len, uint32_t Timeout); 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | #endif 82 | -------------------------------------------------------------------------------- /ee24.c: -------------------------------------------------------------------------------- 1 | 2 | /************************************************************************************************************ 3 | ************** Include Headers 4 | ************************************************************************************************************/ 5 | 6 | #include "ee24.h" 7 | #if EE24_RTOS == EE24_RTOS_DISABLE 8 | #elif EE24_RTOS == EE24_RTOS_CMSIS_V1 9 | #include "cmsis_os.h" 10 | #include "freertos.h" 11 | #elif EE24_RTOS == EE24_RTOS_CMSIS_V2 12 | #include "cmsis_os2.h" 13 | #include "freertos.h" 14 | #elif EE24_RTOS == EE24_RTOS_THREADX 15 | #include "app_threadx.h" 16 | #endif 17 | 18 | /************************************************************************************************************ 19 | ************** Private Definitions 20 | ************************************************************************************************************/ 21 | 22 | #if (EE24_SIZE <= 2) 23 | #define EE24_PSIZE 8 24 | #elif (EE24_SIZE <= 16) 25 | #define EE24_PSIZE 16 26 | #else 27 | #define EE24_PSIZE 32 28 | #endif 29 | 30 | /************************************************************************************************************ 31 | ************** Private Functions 32 | ************************************************************************************************************/ 33 | 34 | void EE24_Delay(uint32_t Delay); 35 | void EE24_Lock(EE24_HandleTypeDef *Handle); 36 | void EE24_UnLock(EE24_HandleTypeDef *Handle); 37 | 38 | /***********************************************************************************************************/ 39 | 40 | void EE24_Delay(uint32_t Delay) 41 | { 42 | #if EE24_RTOS == EE24_RTOS_DISABLE 43 | HAL_Delay(Delay); 44 | #elif (EE24_RTOS == EE24_RTOS_CMSIS_V1) || (EE24_RTOS == EE24_RTOS_CMSIS_V2) 45 | uint32_t d = (configTICK_RATE_HZ * Delay) / 1000; 46 | if (d == 0) 47 | d = 1; 48 | osDelay(d); 49 | #elif EE24_RTOS == EE24_RTOS_THREADX 50 | uint32_t d = (TX_TIMER_TICKS_PER_SECOND * Delay) / 1000; 51 | if (d == 0) 52 | d = 1; 53 | tx_thread_sleep(d); 54 | #endif 55 | } 56 | 57 | /***********************************************************************************************************/ 58 | 59 | void EE24_Lock(EE24_HandleTypeDef *Handle) 60 | { 61 | while (Handle->Lock) 62 | { 63 | EE24_Delay(1); 64 | } 65 | Handle->Lock = 1; 66 | } 67 | 68 | /***********************************************************************************************************/ 69 | 70 | void EE24_UnLock(EE24_HandleTypeDef *Handle) 71 | { 72 | Handle->Lock = 0; 73 | } 74 | 75 | /************************************************************************************************************ 76 | ************** Public Functions 77 | ************************************************************************************************************/ 78 | 79 | #if EE24_USE_WP_PIN == false 80 | /** 81 | * @brief Initialize EEPROM handle 82 | * @note Initialize EEPROM handle and set EEPROM address 83 | * 84 | * @param *Handle: Pointer to EE24_HandleTypeDef structure 85 | * @param *HI2c: Pointer to I2C_HandleTypeDef structure 86 | * @param I2CAddress: I2C Memory address 87 | * 88 | * @retval bool: true or false 89 | */ 90 | bool EE24_Init(EE24_HandleTypeDef *Handle, I2C_HandleTypeDef *HI2c, uint8_t I2CAddress) 91 | { 92 | bool answer = false; 93 | do 94 | { 95 | if ((Handle == NULL) || (HI2c == NULL)) 96 | { 97 | break; 98 | } 99 | Handle->HI2c = HI2c; 100 | Handle->Address = I2CAddress; 101 | if (HAL_I2C_IsDeviceReady(Handle->HI2c, Handle->Address, 2, 100) == HAL_OK) 102 | { 103 | answer = true; 104 | } 105 | } 106 | while (0); 107 | 108 | return answer; 109 | } 110 | #else 111 | /** 112 | * @brief Initialize EEPROM handle 113 | * @note Initialize EEPROM handle and set memory address 114 | * 115 | * @param *Handle: Pointer to EE24_HandleTypeDef structure 116 | * @param *HI2c: Pointer to I2C_HandleTypeDef structure 117 | * @param I2CAddress: I2C Memory address 118 | * @param *GPIO_TypeDef: Pointer to GPIO_TypeDef structure for Write protected pin 119 | * @param WpPin: PinNumber of write protected pin 120 | * 121 | * @retval bool: true or false 122 | */ 123 | bool EE24_Init(EE24_HandleTypeDef *Handle, I2C_HandleTypeDef *HI2c, uint8_t I2CAddress, GPIO_TypeDef *WpGpio, uint16_t WpPin) 124 | { 125 | bool answer = false; 126 | do 127 | { 128 | if ((Handle == NULL) || (HI2c == NULL) || (WpGpio == NULL)) 129 | { 130 | break; 131 | } 132 | Handle->HI2c = HI2c; 133 | Handle->Address = I2CAddress; 134 | Handle->WpGpio = WpGpio; 135 | Handle->WpPin = WpPin; 136 | HAL_GPIO_WritePin(Handle->WpGpio, Handle->WpPin, GPIO_PIN_SET); 137 | if (HAL_I2C_IsDeviceReady(Handle->HI2c, Handle->Address, 2, 100) == HAL_OK) 138 | { 139 | answer = true; 140 | } 141 | } 142 | while (0); 143 | 144 | return answer; 145 | } 146 | #endif 147 | 148 | /***********************************************************************************************************/ 149 | 150 | /** 151 | * @brief Read from Memory 152 | * @note Read data from memory and copy to an array 153 | * 154 | * @param *Handle: Pointer to EE24_HandleTypeDef structure 155 | * @param Address: Start address for read 156 | * @param *Data: Pointer to copy data from EEPROM 157 | * @param Len: Data length to read 158 | * @param Timeout: Timeout of this operation in ms 159 | * 160 | * @retval bool: true or false 161 | */ 162 | bool EE24_Read(EE24_HandleTypeDef *Handle, uint32_t Address, uint8_t *Data, size_t Len, uint32_t Timeout) 163 | { 164 | EE24_Lock(Handle); 165 | bool answer = false; 166 | do 167 | { 168 | #if ((EE24_SIZE == EE24_1KBIT) || (EE24_SIZE == EE24_2KBIT)) 169 | if (HAL_I2C_Mem_Read(Handle->HI2c, Handle->Address, Address, I2C_MEMADD_SIZE_8BIT, Data, Len, Timeout) == HAL_OK) 170 | #elif (EE24_SIZE == EE24_4KBIT) 171 | if (HAL_I2C_Mem_Read(Handle->HI2c, Handle->Address | ((Address & 0x0100) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, Len, Timeout) == HAL_OK) 172 | #elif (EE24_SIZE == EE24_8KBIT) 173 | if (HAL_I2C_Mem_Read(Handle->HI2c, Handle->Address | ((Address & 0x0300) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, Len, Timeout) == HAL_OK) 174 | #elif (EE24_SIZE == EE24_16KBIT) 175 | if (HAL_I2C_Mem_Read(Handle->HI2c, Handle->Address | ((Address & 0x0700) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, Len, Timeout) == HAL_OK) 176 | #else 177 | if (HAL_I2C_Mem_Read(Handle->HI2c, Handle->Address, Address, I2C_MEMADD_SIZE_16BIT, Data, Len, Timeout) == HAL_OK) 178 | #endif 179 | { 180 | answer = true; 181 | } 182 | } 183 | while (0); 184 | 185 | EE24_UnLock(Handle); 186 | return answer; 187 | } 188 | 189 | /***********************************************************************************************************/ 190 | 191 | /** 192 | * @brief Write to Memory 193 | * @note Write an array to memory 194 | * 195 | * @param *Handle: Pointer to EE24_HandleTypeDef structure 196 | * @param Address: Start address for write 197 | * @param *Data: Pointer to copy data to EEPEOM 198 | * @param Len: Data length to write 199 | * @param Timeout: Timeout of this operation in ms 200 | * 201 | * @retval bool: true or false 202 | */ 203 | bool EE24_Write(EE24_HandleTypeDef *Handle, uint32_t Address, uint8_t *Data, size_t Len, uint32_t Timeout) 204 | { 205 | EE24_Lock(Handle); 206 | bool answer = false; 207 | do 208 | { 209 | uint16_t w; 210 | uint32_t startTime = HAL_GetTick(); 211 | #if EE24_USE_WP_PIN == true 212 | HAL_GPIO_WritePin(Handle->WpGpio, Handle->WpPin, GPIO_PIN_RESET); 213 | #endif 214 | while (1) 215 | { 216 | w = EE24_PSIZE - (Address % EE24_PSIZE); 217 | if (w > Len) 218 | { 219 | w = Len; 220 | } 221 | #if ((EE24_SIZE == EE24_1KBIT) || (EE24_SIZE == EE24_2KBIT)) 222 | if (HAL_I2C_Mem_Write(Handle->HI2c, Handle->Address, Address, I2C_MEMADD_SIZE_8BIT, Data, w, Timeout) == HAL_OK) 223 | #elif (EE24_SIZE == EE24_4KBIT) 224 | if (HAL_I2C_Mem_Write(Handle->HI2c, Handle->Address | ((Address & 0x0100) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, w, Timeout) == HAL_OK) 225 | #elif (EE24_SIZE == EE24_8KBIT) 226 | if (HAL_I2C_Mem_Write(Handle->HI2c, Handle->Address | ((Address & 0x0300) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, w, Timeout) == HAL_OK) 227 | #elif (EE24_SIZE == EE24_16KBIT) 228 | if (HAL_I2C_Mem_Write(Handle->HI2c, Handle->Address | ((Address & 0x0700) >> 7), (Address & 0xff), I2C_MEMADD_SIZE_8BIT, Data, w, Timeout) == HAL_OK) 229 | #else 230 | if (HAL_I2C_Mem_Write(Handle->HI2c, Handle->Address, Address, I2C_MEMADD_SIZE_16BIT, Data, w, Timeout) == HAL_OK) 231 | #endif 232 | { 233 | EE24_Delay(10); 234 | Len -= w; 235 | Data += w; 236 | Address += w; 237 | if (Len == 0) 238 | { 239 | answer = true; 240 | break; 241 | } 242 | if (HAL_GetTick() - startTime >= Timeout) 243 | { 244 | break; 245 | } 246 | } 247 | else 248 | { 249 | break; 250 | } 251 | } 252 | } 253 | while (0); 254 | 255 | #if EE24_USE_WP_PIN == true 256 | HAL_GPIO_WritePin(Handle->WpGpio, Handle->WpPin, GPIO_PIN_SET); 257 | #endif 258 | EE24_UnLock(Handle); 259 | return answer; 260 | } 261 | 262 | /***********************************************************************************************************/ 263 | --------------------------------------------------------------------------------