[89] | 1 | /* USER CODE BEGIN Header */ |
---|
| 2 | /** |
---|
| 3 | ****************************************************************************** |
---|
| 4 | * @file user_diskio.c |
---|
| 5 | * @brief This file includes a diskio driver skeleton to be completed by the user. |
---|
| 6 | ****************************************************************************** |
---|
| 7 | * @attention |
---|
| 8 | * |
---|
| 9 | * Copyright (c) 2025 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 | /* USER CODE END Header */ |
---|
| 19 | |
---|
| 20 | #ifdef USE_OBSOLETE_USER_CODE_SECTION_0 |
---|
| 21 | /* |
---|
| 22 | * Warning: the user section 0 is no more in use (starting from CubeMx version 4.16.0) |
---|
| 23 | * To be suppressed in the future. |
---|
| 24 | * Kept to ensure backward compatibility with previous CubeMx versions when |
---|
| 25 | * migrating projects. |
---|
| 26 | * User code previously added there should be copied in the new user sections before |
---|
| 27 | * the section contents can be deleted. |
---|
| 28 | */ |
---|
| 29 | /* USER CODE BEGIN 0 */ |
---|
| 30 | /* USER CODE END 0 */ |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | /* USER CODE BEGIN DECL */ |
---|
| 34 | |
---|
| 35 | /* Includes ------------------------------------------------------------------*/ |
---|
[90] | 36 | #include <stdio.h> |
---|
[89] | 37 | #include <string.h> |
---|
[90] | 38 | |
---|
[89] | 39 | #include "ff_gen_drv.h" |
---|
| 40 | |
---|
[90] | 41 | #include "eeprom_diskio.h" |
---|
| 42 | #include "eeprom_conf.h" |
---|
| 43 | #include "m24256e.h" |
---|
| 44 | |
---|
[89] | 45 | /* Private typedef -----------------------------------------------------------*/ |
---|
| 46 | /* Private define ------------------------------------------------------------*/ |
---|
| 47 | |
---|
| 48 | /* Private variables ---------------------------------------------------------*/ |
---|
| 49 | /* Disk status */ |
---|
| 50 | static volatile DSTATUS Stat = STA_NOINIT; |
---|
| 51 | |
---|
[90] | 52 | uint8_t M24256E_CDA_DevSelCode = 0xB0; |
---|
| 53 | uint8_t M24256E_IDPage_DevSelCode = 0xB0; |
---|
| 54 | uint8_t M24256E_Memory_DevSelCode = 0xA0; |
---|
| 55 | |
---|
| 56 | static M24256E_EEPROM_CommonDrv_t *M24256EDrv[EEICA1_M24_INSTANCES_NBR]; |
---|
| 57 | void *M24256ECompObj[EEICA1_M24_INSTANCES_NBR]; |
---|
| 58 | |
---|
[89] | 59 | /* USER CODE END DECL */ |
---|
| 60 | |
---|
[90] | 61 | static int32_t M24256E_Probe(void); |
---|
| 62 | int32_t M24256E_IsDeviceReady(uint32_t Instance, const uint32_t Trials); |
---|
| 63 | int32_t M24256E_ReadPage(uint32_t Instance, uint8_t * const pData, const uint32_t TarAddr, const uint16_t Size); |
---|
| 64 | |
---|
[89] | 65 | /* Private function prototypes -----------------------------------------------*/ |
---|
| 66 | DSTATUS EEPROM_initialize (BYTE pdrv); |
---|
| 67 | DSTATUS EEPROM_status (BYTE pdrv); |
---|
| 68 | DRESULT EEPROM_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count); |
---|
| 69 | #if _USE_WRITE == 1 |
---|
| 70 | DRESULT EEPROM_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count); |
---|
| 71 | #endif /* _USE_WRITE == 1 */ |
---|
| 72 | #if _USE_IOCTL == 1 |
---|
| 73 | DRESULT EEPROM_ioctl (BYTE pdrv, BYTE cmd, void *buff); |
---|
| 74 | #endif /* _USE_IOCTL == 1 */ |
---|
| 75 | |
---|
| 76 | Diskio_drvTypeDef EEPROM_Driver = |
---|
| 77 | { |
---|
| 78 | EEPROM_initialize, |
---|
| 79 | EEPROM_status, |
---|
| 80 | EEPROM_read, |
---|
| 81 | #if _USE_WRITE |
---|
| 82 | EEPROM_write, |
---|
| 83 | #endif /* _USE_WRITE == 1 */ |
---|
| 84 | #if _USE_IOCTL == 1 |
---|
| 85 | EEPROM_ioctl, |
---|
| 86 | #endif /* _USE_IOCTL == 1 */ |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | /* Private functions ---------------------------------------------------------*/ |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * @brief Initializes a Drive |
---|
| 93 | * @param pdrv: Physical drive number (0..) |
---|
| 94 | * @retval DSTATUS: Operation status |
---|
| 95 | */ |
---|
[90] | 96 | DSTATUS EEPROM_initialize (BYTE pdrv /* Physical drive nmuber to identify the drive */ |
---|
| 97 | ) |
---|
[89] | 98 | { |
---|
[90] | 99 | /* USER CODE BEGIN INIT */ |
---|
| 100 | UNUSED(pdrv); // We have only one EEPROM on the bus |
---|
| 101 | |
---|
[89] | 102 | Stat = STA_NOINIT; |
---|
[90] | 103 | |
---|
| 104 | if (M24256E_Probe() == BSP_ERROR_NONE) |
---|
| 105 | Stat = 0; |
---|
| 106 | |
---|
[89] | 107 | return Stat; |
---|
[90] | 108 | /* USER CODE END INIT */ |
---|
[89] | 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * @brief Gets Disk Status |
---|
| 113 | * @param pdrv: Physical drive number (0..) |
---|
| 114 | * @retval DSTATUS: Operation status |
---|
| 115 | */ |
---|
| 116 | DSTATUS EEPROM_status ( |
---|
| 117 | BYTE pdrv /* Physical drive number to identify the drive */ |
---|
| 118 | ) |
---|
| 119 | { |
---|
[90] | 120 | /* USER CODE BEGIN STATUS */ |
---|
| 121 | UNUSED(pdrv); // We have only one EEPROM on the bus |
---|
[89] | 122 | Stat = STA_NOINIT; |
---|
[90] | 123 | |
---|
| 124 | if (M24256E_IsDeviceReady(EEICA1_M24256E, MAX_TRIALS) == BSP_ERROR_NONE) |
---|
| 125 | Stat = 0; |
---|
| 126 | |
---|
[89] | 127 | return Stat; |
---|
[90] | 128 | /* USER CODE END STATUS */ |
---|
[89] | 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * @brief Reads Sector(s) |
---|
| 133 | * @param pdrv: Physical drive number (0..) |
---|
| 134 | * @param *buff: Data buffer to store read data |
---|
| 135 | * @param sector: Sector address (LBA) |
---|
| 136 | * @param count: Number of sectors to read (1..128) |
---|
| 137 | * @retval DRESULT: Operation result |
---|
| 138 | */ |
---|
| 139 | DRESULT EEPROM_read ( |
---|
| 140 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ |
---|
| 141 | BYTE *buff, /* Data buffer to store read data */ |
---|
| 142 | DWORD sector, /* Sector address in LBA */ |
---|
| 143 | UINT count /* Number of sectors to read */ |
---|
| 144 | ) |
---|
| 145 | { |
---|
[90] | 146 | /* USER CODE BEGIN READ */ |
---|
| 147 | UNUSED(pdrv); |
---|
| 148 | printf("Requested %u sectors starting at %lu.\n", count, sector); |
---|
| 149 | |
---|
| 150 | if (M24256E_ReadPage(EEICA1_M24256E, buff, sector * _MIN_SS, count * _MIN_SS) != BSP_ERROR_NONE) |
---|
| 151 | return RES_ERROR; |
---|
| 152 | |
---|
[89] | 153 | return RES_OK; |
---|
[90] | 154 | /* USER CODE END READ */ |
---|
[89] | 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * @brief Writes Sector(s) |
---|
| 159 | * @param pdrv: Physical drive number (0..) |
---|
| 160 | * @param *buff: Data to be written |
---|
| 161 | * @param sector: Sector address (LBA) |
---|
| 162 | * @param count: Number of sectors to write (1..128) |
---|
| 163 | * @retval DRESULT: Operation result |
---|
| 164 | */ |
---|
| 165 | #if _USE_WRITE == 1 |
---|
| 166 | DRESULT EEPROM_write ( |
---|
| 167 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ |
---|
| 168 | const BYTE *buff, /* Data to be written */ |
---|
| 169 | DWORD sector, /* Sector address in LBA */ |
---|
| 170 | UINT count /* Number of sectors to write */ |
---|
| 171 | ) |
---|
| 172 | { |
---|
| 173 | /* USER CODE BEGIN WRITE */ |
---|
| 174 | /* USER CODE HERE */ |
---|
| 175 | return RES_OK; |
---|
| 176 | /* USER CODE END WRITE */ |
---|
| 177 | } |
---|
| 178 | #endif /* _USE_WRITE == 1 */ |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * @brief I/O control operation |
---|
| 182 | * @param pdrv: Physical drive number (0..) |
---|
| 183 | * @param cmd: Control code |
---|
| 184 | * @param *buff: Buffer to send/receive control data |
---|
| 185 | * @retval DRESULT: Operation result |
---|
| 186 | */ |
---|
| 187 | #if _USE_IOCTL == 1 |
---|
| 188 | DRESULT EEPROM_ioctl ( |
---|
| 189 | BYTE pdrv, /* Physical drive nmuber (0..) */ |
---|
| 190 | BYTE cmd, /* Control code */ |
---|
| 191 | void *buff /* Buffer to send/receive control data */ |
---|
| 192 | ) |
---|
| 193 | { |
---|
[90] | 194 | /* USER CODE BEGIN IOCTL */ |
---|
| 195 | UNUSED(pdrv); |
---|
| 196 | |
---|
[89] | 197 | DRESULT res = RES_ERROR; |
---|
[90] | 198 | |
---|
| 199 | if (Stat & STA_NOINIT) return RES_NOTRDY; |
---|
| 200 | |
---|
| 201 | switch (cmd) |
---|
| 202 | { |
---|
| 203 | case CTRL_SYNC: // Make sure that no pending write process |
---|
| 204 | res = RES_OK; |
---|
| 205 | break; |
---|
| 206 | |
---|
| 207 | case GET_SECTOR_COUNT: // Get number of sectors on the disk (DWORD) |
---|
| 208 | *(DWORD*)buff = M24256E_MEMORYSIZE / _MIN_SS; |
---|
| 209 | res = RES_OK; |
---|
| 210 | break; |
---|
| 211 | |
---|
| 212 | case GET_SECTOR_SIZE: // Get R/W sector size (WORD) |
---|
| 213 | *(WORD*)buff = _MIN_SS; |
---|
| 214 | res = RES_OK; |
---|
| 215 | break; |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | case GET_BLOCK_SIZE: // Get erase block size in unit of sector (DWORD) |
---|
| 219 | *(DWORD*)buff = 1; |
---|
| 220 | res = RES_OK; |
---|
| 221 | break; |
---|
| 222 | |
---|
| 223 | default: |
---|
| 224 | res = RES_PARERR; |
---|
| 225 | } |
---|
| 226 | |
---|
[89] | 227 | return res; |
---|
[90] | 228 | /* USER CODE END IOCTL */ |
---|
[89] | 229 | } |
---|
| 230 | #endif /* _USE_IOCTL == 1 */ |
---|
| 231 | |
---|
[90] | 232 | //------------------------------------------------------------------------------ |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * @brief Reads complete page from the memory at page start address |
---|
| 236 | * @param Instance : I2C EEPROM instance to be used |
---|
| 237 | * @param pData : pointer to the data to read |
---|
| 238 | * @param TarAddr : starting page address to read |
---|
| 239 | * @param Size : Size in bytes of the value to be written |
---|
| 240 | * @retval BSP status |
---|
| 241 | */ |
---|
| 242 | int32_t M24256E_ReadPage(uint32_t Instance, uint8_t * const pData, const uint32_t TarAddr, const uint16_t Size) |
---|
| 243 | { |
---|
| 244 | int32_t ret = BSP_ERROR_NONE; |
---|
| 245 | |
---|
| 246 | if ((TarAddr + Size)> M24256E_MEMORYSIZE) |
---|
| 247 | return BSP_ERROR_WRONG_PARAM; |
---|
| 248 | |
---|
| 249 | uint32_t iNumberOfPage = (TarAddr + Size) / M24256E_PAGESIZE; |
---|
| 250 | uint32_t iRemainder = (TarAddr + Size) % M24256E_PAGESIZE; |
---|
| 251 | |
---|
| 252 | uint32_t PageAddress = TarAddr * M24256E_PAGESIZE; |
---|
| 253 | uint32_t iPageNumber = TarAddr; |
---|
| 254 | if (iRemainder != 0U) |
---|
| 255 | { |
---|
| 256 | iNumberOfPage += 1U; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | if (iNumberOfPage <= 1U) |
---|
| 260 | { |
---|
| 261 | if (M24256EDrv[Instance]->ReadPage(M24256ECompObj[Instance], pData, PageAddress, M24256E_PAGESIZE) != BSP_ERROR_NONE) |
---|
| 262 | { |
---|
| 263 | ret = BSP_ERROR_COMPONENT_FAILURE; |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | { |
---|
| 267 | ret = BSP_ERROR_NONE; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
| 272 | for (uint32_t iCounter=0; iCounter<iNumberOfPage; iCounter++) |
---|
| 273 | { |
---|
| 274 | uint32_t iPageAddress = iPageNumber * M24256E_PAGESIZE; |
---|
| 275 | ret = M24256EDrv[Instance]->ReadPage(M24256ECompObj[Instance], &pData[0U + (iCounter*M24256E_PAGESIZE)], iPageAddress, M24256E_PAGESIZE); |
---|
| 276 | iPageNumber++; |
---|
| 277 | HAL_Delay(5); |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | return ret; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | //------------------------------------------------------------------------------ |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * @brief Checks if the memory is available |
---|
| 288 | * @param Instance : I2C EEPROM instance to be used |
---|
| 289 | * @param Trials : Number of trials |
---|
| 290 | * @retval BSP status |
---|
| 291 | */ |
---|
| 292 | int32_t M24256E_IsDeviceReady(uint32_t Instance, const uint32_t Trials) |
---|
| 293 | { |
---|
| 294 | int32_t ret; |
---|
| 295 | |
---|
| 296 | if (M24256EDrv[Instance]->IsReady(M24256ECompObj[Instance], Trials) != BSP_ERROR_NONE) |
---|
| 297 | { |
---|
| 298 | ret = BSP_ERROR_COMPONENT_FAILURE; |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | { |
---|
| 302 | ret = BSP_ERROR_NONE; |
---|
| 303 | } |
---|
| 304 | return ret; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | //------------------------------------------------------------------------------ |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * @brief Register Bus IOs for instance M24256E |
---|
| 311 | * @retval BSP status |
---|
| 312 | */ |
---|
| 313 | static int32_t M24256E_Probe(void) |
---|
| 314 | { |
---|
| 315 | M24256E_IO_t io_ctx256; |
---|
| 316 | int32_t ret = BSP_ERROR_NONE; |
---|
| 317 | static M24256E_Object_t M24256_obj_0; |
---|
| 318 | |
---|
| 319 | io_ctx256.Address = M24256E_CDA_DevSelCode; |
---|
| 320 | io_ctx256.Init = EEICA1_I2C_INIT; |
---|
| 321 | io_ctx256.DeInit = EEICA1_I2C_DEINIT; |
---|
| 322 | io_ctx256.ReadReg = EEICA1_I2C_READREG; |
---|
| 323 | io_ctx256.WriteReg = EEICA1_I2C_WRITEREG; |
---|
| 324 | io_ctx256.ReadReg16 = EEICA1_I2C_READREG16; |
---|
| 325 | io_ctx256.WriteReg16 = EEICA1_I2C_WRITEREG16; |
---|
| 326 | io_ctx256.Transmit = EEICA1_I2C_SEND; |
---|
| 327 | io_ctx256.IsReady = EEICA1_I2C_ISREADY; |
---|
| 328 | io_ctx256.Delay = EEICA1_M24_DELAY; |
---|
| 329 | |
---|
| 330 | if (M24256E_RegisterBusIO(&M24256_obj_0, &io_ctx256) != M24_OK) |
---|
| 331 | { |
---|
| 332 | ret = BSP_ERROR_UNKNOWN_COMPONENT; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | M24256ECompObj[EEICA1_M24256E] = &M24256_obj_0; |
---|
| 336 | M24256EDrv[EEICA1_M24256E] = (M24256E_EEPROM_CommonDrv_t *)(void *)&M24256E_i2c_Drv; |
---|
| 337 | |
---|
| 338 | if (M24256EDrv[EEICA1_M24256E]->Init(M24256ECompObj[EEICA1_M24256E]) != M24_OK) |
---|
| 339 | { |
---|
| 340 | ret = BSP_ERROR_COMPONENT_FAILURE; |
---|
| 341 | } |
---|
| 342 | else |
---|
| 343 | { |
---|
| 344 | ret = BSP_ERROR_NONE; |
---|
| 345 | } |
---|
| 346 | return ret; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | //------------------------------------------------------------------------------ |
---|