Ignore:
Timestamp:
Feb 12, 2025, 3:03:55 PM (3 months ago)
Author:
Zed
Message:

Communication with M24256E EEPROM works.

Location:
ctrl/firmware/Main/CubeMX/FATFS
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ctrl/firmware/Main/CubeMX/FATFS/App/fatfs.c

    r89 r90  
    2323FATFS SDFatFS;    /* File system object for SD logical drive */
    2424FIL SDFile;       /* File object for SD */
     25
     26/* USER CODE BEGIN Variables */
     27
    2528uint8_t retEEPROM;    /* Return value for USER */
    2629char EEPROMPath[4];   /* USER logical drive path */
    27 FATFS USERFatFS;    /* File system object for USER logical drive */
     30FATFS EEPROMFatFS;        /* File system object for USER logical drive */
    2831FIL EEPROMFile;       /* File object for USER */
    29 
    30 /* USER CODE BEGIN Variables */
    3132
    3233/* USER CODE END Variables */
     
    3637  /*## FatFS: Link the SD driver ###########################*/
    3738  retSD = FATFS_LinkDriver(&SD_Driver, SDPath);
    38   /*## FatFS: Link the USER driver ###########################*/
    39   retEEPROM = FATFS_LinkDriver(&EEPROM_Driver, EEPROMPath);
    4039
    4140  /* USER CODE BEGIN Init */
    4241  /* additional user code for init */
     42
     43  retEEPROM = FATFS_LinkDriver(&EEPROM_Driver, EEPROMPath);
     44
    4345  /* USER CODE END Init */
    4446}
  • ctrl/firmware/Main/CubeMX/FATFS/App/fatfs.h

    r89 r90  
    2727#include "ff_gen_drv.h"
    2828#include "sd_diskio.h" /* defines SD_Driver as external */
    29 #include "eeprom_diskio.h" /* defines USER_Driver as external */
    3029
    3130/* USER CODE BEGIN Includes */
    3231
     32#include "eeprom_diskio.h" /* defines USER_Driver as external */
     33
     34extern uint8_t retEEPROM;       /* Return value for USER */
     35extern char EEPROMPath[4];      /* USER logical drive path */
     36extern FATFS EEPROMFatFS;       /* File system object for USER logical drive */
     37extern FIL EEPROMFile;          /* File object for USER */
     38
    3339/* USER CODE END Includes */
    3440
    35 extern uint8_t retSD; /* Return value for SD */
    36 extern char SDPath[4]; /* SD logical drive path */
    37 extern FATFS SDFatFS; /* File system object for SD logical drive */
    38 extern FIL SDFile; /* File object for SD */
    39 extern uint8_t retEEPROM; /* Return value for USER */
    40 extern char EEPROMPath[4]; /* USER logical drive path */
    41 extern FATFS EEPROMFatFS; /* File system object for USER logical drive */
    42 extern FIL EEPROMFile; /* File object for USER */
     41extern uint8_t retSD;   /* Return value for SD */
     42extern char SDPath[4];  /* SD logical drive path */
     43extern FATFS SDFatFS;   /* File system object for SD logical drive */
     44extern FIL SDFile;              /* File object for SD */
    4345
    4446void MX_FATFS_Init(void);
  • ctrl/firmware/Main/CubeMX/FATFS/Target/eeprom_diskio.c

    r89 r90  
    3434
    3535/* Includes ------------------------------------------------------------------*/
     36#include <stdio.h>
    3637#include <string.h>
     38
    3739#include "ff_gen_drv.h"
     40
     41#include "eeprom_diskio.h"
     42#include "eeprom_conf.h"
     43#include "m24256e.h"
    3844
    3945/* Private typedef -----------------------------------------------------------*/
     
    4450static volatile DSTATUS Stat = STA_NOINIT;
    4551
     52uint8_t M24256E_CDA_DevSelCode = 0xB0;
     53uint8_t M24256E_IDPage_DevSelCode = 0xB0;
     54uint8_t M24256E_Memory_DevSelCode = 0xA0;
     55
     56static M24256E_EEPROM_CommonDrv_t *M24256EDrv[EEICA1_M24_INSTANCES_NBR];
     57void *M24256ECompObj[EEICA1_M24_INSTANCES_NBR];
     58
    4659/* USER CODE END DECL */
     60
     61static int32_t M24256E_Probe(void);
     62int32_t M24256E_IsDeviceReady(uint32_t Instance, const uint32_t Trials);
     63int32_t M24256E_ReadPage(uint32_t Instance, uint8_t * const pData, const uint32_t TarAddr, const uint16_t Size);
    4764
    4865/* Private function prototypes -----------------------------------------------*/
     
    7794  * @retval DSTATUS: Operation status
    7895  */
    79 DSTATUS EEPROM_initialize (
    80         BYTE pdrv           /* Physical drive nmuber to identify the drive */
    81 )
    82 {
    83   /* USER CODE BEGIN INIT */
     96DSTATUS EEPROM_initialize (BYTE pdrv /* Physical drive nmuber to identify the drive */
     97                                                                          )
     98{
     99        /* USER CODE BEGIN INIT */
     100        UNUSED(pdrv);                                                                                                                           // We have only one EEPROM on the bus
     101
    84102    Stat = STA_NOINIT;
     103
     104        if (M24256E_Probe() == BSP_ERROR_NONE)
     105                Stat = 0;
     106
    85107    return Stat;
    86   /* USER CODE END INIT */
     108        /* USER CODE END INIT */
    87109}
    88110
     
    96118)
    97119{
    98   /* USER CODE BEGIN STATUS */
     120        /* USER CODE BEGIN STATUS */
     121        UNUSED(pdrv);                                                                                                                           // We have only one EEPROM on the bus
    99122    Stat = STA_NOINIT;
     123
     124        if (M24256E_IsDeviceReady(EEICA1_M24256E, MAX_TRIALS) == BSP_ERROR_NONE)
     125                Stat = 0;
     126
    100127    return Stat;
    101   /* USER CODE END STATUS */
     128        /* USER CODE END STATUS */
    102129}
    103130
     
    117144)
    118145{
    119   /* USER CODE BEGIN READ */
     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
    120153    return RES_OK;
    121   /* USER CODE END READ */
     154        /* USER CODE END READ */
    122155}
    123156
     
    159192)
    160193{
    161   /* USER CODE BEGIN IOCTL */
     194        /* USER CODE BEGIN IOCTL */
     195        UNUSED(pdrv);
     196
    162197    DRESULT res = RES_ERROR;
     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
    163227    return res;
    164   /* USER CODE END IOCTL */
     228        /* USER CODE END IOCTL */
    165229}
    166230#endif /* _USE_IOCTL == 1 */
    167231
     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*/
     242int32_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*/
     292int32_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*/
     313static 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//------------------------------------------------------------------------------
  • ctrl/firmware/Main/CubeMX/FATFS/Target/eeprom_diskio.h

    r89 r90  
    3232/* Exported types ------------------------------------------------------------*/
    3333/* Exported constants --------------------------------------------------------*/
     34
     35#define M24256E_PAGESIZE                                        64U
     36#define M24256E_MEMORYSIZE                                      32768U // in bytes, do not change
     37
     38#define EEICA1_M24_INSTANCES_NBR                        2U
     39#define EEICA1_M24256E                                          0U
     40#define EEICA1_M24xx                                            1U
     41
     42#define MAX_TRIALS                                                      3U
     43
    3444/* Exported functions ------------------------------------------------------- */
    3545extern Diskio_drvTypeDef  EEPROM_Driver;
Note: See TracChangeset for help on using the changeset viewer.