2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* drivers/mtd/at24xx.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Li Zhuoyi. All rights reserved.
|
2016-02-01 15:57:22 +01:00
|
|
|
* Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved.
|
2011-11-01 14:08:19 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Included Files
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-11-30 04:50:17 +01:00
|
|
|
#include <inttypes.h>
|
2011-11-01 14:08:19 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2011-11-01 14:08:19 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_AT24XX
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Pre-processor Definitions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* As a minimum,
|
|
|
|
* the size of the AT24 part and its 7-bit I2C address are required.
|
|
|
|
*/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_AT24XX_SIZE
|
|
|
|
# warning "Assuming AT24 size 64"
|
|
|
|
# define CONFIG_AT24XX_SIZE 64
|
|
|
|
#endif
|
2015-11-17 14:40:17 +01:00
|
|
|
#if !defined(CONFIG_AT24XX_ADDR) && !defined(CONFIG_AT24XX_MULTI)
|
|
|
|
# warning "Assuming AT24 I2C address of 0x50"
|
2011-11-01 14:08:19 +01:00
|
|
|
# define CONFIG_AT24XX_ADDR 0x50
|
|
|
|
#endif
|
2015-10-21 05:34:34 +02:00
|
|
|
#ifndef CONFIG_AT24XX_FREQUENCY
|
2015-11-17 14:40:17 +01:00
|
|
|
# warning "Assuming AT24 I2C frequency of 100KHz"
|
2015-10-21 05:34:34 +02:00
|
|
|
# define CONFIG_AT24XX_FREQUENCY 100000
|
|
|
|
#endif
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
/* Get the part configuration based on the size configuration */
|
|
|
|
|
2018-01-24 13:51:34 +01:00
|
|
|
#if CONFIG_AT24XX_SIZE == 2 /* AT24C02: 2Kbits = 256; 32 * 8 = 256 */
|
|
|
|
# define AT24XX_NPAGES 32
|
|
|
|
# define AT24XX_PAGESIZE 8
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 1
|
2015-10-21 05:32:57 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 4 /* AT24C04: 4Kbits = 512B; 32 * 16 = 512 */
|
|
|
|
# define AT24XX_NPAGES 32
|
|
|
|
# define AT24XX_PAGESIZE 16
|
|
|
|
# define AT24XX_ADDRSIZE 1
|
|
|
|
#elif CONFIG_AT24XX_SIZE == 8 /* AT24C08: 8Kbits = 1KiB; 64 * 16 = 1024 */
|
|
|
|
# define AT24XX_NPAGES 64
|
|
|
|
# define AT24XX_PAGESIZE 16
|
|
|
|
# define AT24XX_ADDRSIZE 1
|
|
|
|
#elif CONFIG_AT24XX_SIZE == 16 /* AT24C16: 16Kbits = 2KiB; 128 * 16 = 2048 */
|
|
|
|
# define AT24XX_NPAGES 128
|
|
|
|
# define AT24XX_PAGESIZE 16
|
|
|
|
# define AT24XX_ADDRSIZE 1
|
2015-03-17 21:27:27 +01:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 32 /* AT24C32: 32Kbits = 4KiB; 128 * 32 = 4096 */
|
2011-11-01 14:08:19 +01:00
|
|
|
# define AT24XX_NPAGES 128
|
|
|
|
# define AT24XX_PAGESIZE 32
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2013-09-12 17:44:38 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 48 /* AT24C48: 48Kbits = 6KiB; 192 * 32 = 6144 */
|
2011-11-01 14:08:19 +01:00
|
|
|
# define AT24XX_NPAGES 192
|
|
|
|
# define AT24XX_PAGESIZE 32
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2013-09-12 17:44:38 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 64 /* AT24C64: 64Kbits = 8KiB; 256 * 32 = 8192 */
|
2011-11-01 14:08:19 +01:00
|
|
|
# define AT24XX_NPAGES 256
|
|
|
|
# define AT24XX_PAGESIZE 32
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2013-09-12 17:44:38 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 128 /* AT24C128: 128Kbits = 16KiB; 256 * 64 = 16384 */
|
2011-11-01 14:08:19 +01:00
|
|
|
# define AT24XX_NPAGES 256
|
|
|
|
# define AT24XX_PAGESIZE 64
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2013-09-12 17:44:38 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 256 /* AT24C256: 256Kbits = 32KiB; 512 * 64 = 32768 */
|
2011-11-01 14:08:19 +01:00
|
|
|
# define AT24XX_NPAGES 512
|
|
|
|
# define AT24XX_PAGESIZE 64
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2013-09-12 17:44:38 +02:00
|
|
|
#elif CONFIG_AT24XX_SIZE == 512 /* AT24C512: 512Kbits = 64KiB; 512 * 128 = 65536 */
|
|
|
|
# define AT24XX_NPAGES 512
|
|
|
|
# define AT24XX_PAGESIZE 128
|
2015-03-17 21:27:27 +01:00
|
|
|
# define AT24XX_ADDRSIZE 2
|
2011-11-01 14:08:19 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* For applications where a file system is used on the AT24, the tiny page
|
|
|
|
* sizes will result in very inefficient FLASH usage. In such cases, it is
|
|
|
|
* better if blocks are comprised of "clusters" of pages so that the file
|
|
|
|
* system block size is, say, 256 or 512 bytes.
|
|
|
|
* In any event, the block size *must* be an even multiple of the pages.
|
2011-11-01 14:08:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AT24XX_MTD_BLOCKSIZE
|
2015-11-17 14:40:17 +01:00
|
|
|
# warning "Assuming MTD driver block size is the same as the FLASH page size"
|
2011-11-01 14:08:19 +01:00
|
|
|
# define CONFIG_AT24XX_MTD_BLOCKSIZE AT24XX_PAGESIZE
|
|
|
|
#endif
|
|
|
|
|
2016-11-01 22:03:16 +01:00
|
|
|
#ifndef CONFIG_AT24XX_TIMEOUT_MS
|
|
|
|
# define CONFIG_AT24XX_TIMEOUT_MS 10
|
|
|
|
#endif
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Private Types
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
/* This type represents the state of the MTD device. The struct mtd_dev_s
|
|
|
|
* must appear at the beginning of the definition so that you can freely
|
|
|
|
* cast between pointers to struct mtd_dev_s and struct at24c_dev_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct at24c_dev_s
|
|
|
|
{
|
2016-01-30 15:36:47 +01:00
|
|
|
struct mtd_dev_s mtd; /* MTD interface */
|
|
|
|
FAR struct i2c_master_s *dev; /* Saved I2C interface instance */
|
|
|
|
bool initd; /* True: The device has been initialize */
|
2015-03-17 21:27:27 +01:00
|
|
|
#ifdef CONFIG_AT24XX_EXTENDED
|
2016-01-30 15:36:47 +01:00
|
|
|
bool extended; /* True: use extended memory region */
|
2015-03-17 21:27:27 +01:00
|
|
|
#endif
|
2016-01-30 15:36:47 +01:00
|
|
|
uint8_t addr; /* I2C address */
|
|
|
|
uint16_t pagesize; /* 32, 63 */
|
|
|
|
uint16_t npages; /* 128, 256, 512, 1024 */
|
2011-11-01 14:08:19 +01:00
|
|
|
};
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Private Function Prototypes
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int at24c_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t at24c_bread(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
2011-11-01 14:08:19 +01:00
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks,
|
|
|
|
FAR const uint8_t *buf);
|
|
|
|
static ssize_t at24c_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2015-03-17 21:27:27 +01:00
|
|
|
FAR uint8_t *buffer);
|
2021-01-27 16:48:40 +01:00
|
|
|
static int at24c_ioctl(FAR struct mtd_dev_s *dev,
|
|
|
|
int cmd,
|
|
|
|
unsigned long arg);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Private Data
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2015-11-17 14:40:17 +01:00
|
|
|
#ifndef CONFIG_AT24XX_MULTI
|
|
|
|
/* If only a signal AT24 part is supported then a statically allocated state
|
|
|
|
* structure may be used.
|
2011-11-01 14:08:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static struct at24c_dev_s g_at24c;
|
2015-11-17 14:40:17 +01:00
|
|
|
#endif
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Private Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-02-01 15:57:22 +01:00
|
|
|
* Name: at24c_i2c_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the I2C device.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
static int at24c_i2c_write(FAR struct at24c_dev_s *priv, uint16_t at24addr,
|
2016-02-01 15:57:22 +01:00
|
|
|
FAR const uint8_t *buffer, int buflen)
|
|
|
|
{
|
2016-02-02 14:13:03 +01:00
|
|
|
struct i2c_msg_s msg;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Setup for the transfer */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
msg.frequency = CONFIG_AT24XX_FREQUENCY,
|
|
|
|
msg.addr = at24addr;
|
|
|
|
msg.flags = 0;
|
|
|
|
msg.buffer = (FAR uint8_t *)buffer; /* Override const */
|
|
|
|
msg.length = buflen;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Then perform the transfer. */
|
|
|
|
|
|
|
|
return I2C_TRANSFER(priv->dev, &msg, 1);
|
2016-02-01 15:57:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-02-01 15:57:22 +01:00
|
|
|
* Name: at24c_i2c_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the I2C device.
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
static int at24c_i2c_read(FAR struct at24c_dev_s *priv, uint16_t at24addr,
|
2016-02-01 15:57:22 +01:00
|
|
|
FAR uint8_t *buffer, int buflen)
|
|
|
|
{
|
2016-02-02 14:13:03 +01:00
|
|
|
struct i2c_msg_s msg;
|
|
|
|
|
|
|
|
/* Setup for the transfer */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
msg.frequency = CONFIG_AT24XX_FREQUENCY,
|
|
|
|
msg.addr = at24addr,
|
|
|
|
msg.flags = I2C_M_READ;
|
|
|
|
msg.buffer = buffer;
|
|
|
|
msg.length = buflen;
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
/* Then perform the transfer. */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2016-02-02 14:13:03 +01:00
|
|
|
return I2C_TRANSFER(priv->dev, &msg, 1);
|
2016-02-01 15:57:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2016-02-01 15:57:22 +01:00
|
|
|
* Name: at24c_eraseall
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2016-02-01 15:57:22 +01:00
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
static int at24c_eraseall(FAR struct at24c_dev_s *priv)
|
|
|
|
{
|
2015-03-17 21:27:27 +01:00
|
|
|
uint8_t buf[AT24XX_PAGESIZE + AT24XX_ADDRSIZE];
|
2016-02-01 19:14:31 +01:00
|
|
|
int startblock = 0;
|
2016-11-01 22:03:16 +01:00
|
|
|
uint16_t wait;
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
memset(&buf[AT24XX_ADDRSIZE], 0xff, priv->pagesize);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
for (startblock = 0; startblock < priv->npages; startblock++)
|
|
|
|
{
|
|
|
|
uint16_t offset = startblock * priv->pagesize;
|
2016-02-01 19:14:31 +01:00
|
|
|
uint16_t at24addr;
|
|
|
|
|
2015-10-21 05:32:57 +02:00
|
|
|
#if AT24XX_ADDRSIZE == 2
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[1] = offset & 0xff;
|
|
|
|
buf[0] = (offset >> 8) & 0xff;
|
|
|
|
at24addr = priv->addr;
|
2015-03-17 21:27:27 +01:00
|
|
|
#else
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[0] = offset & 0xff;
|
|
|
|
at24addr = (priv->addr | ((offset >> 8) & 0x07));
|
2015-03-17 21:27:27 +01:00
|
|
|
#endif
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2016-11-01 22:03:16 +01:00
|
|
|
wait = CONFIG_AT24XX_TIMEOUT_MS;
|
2021-01-27 16:48:40 +01:00
|
|
|
while (at24c_i2c_write(priv,
|
|
|
|
at24addr,
|
|
|
|
buf,
|
|
|
|
AT24XX_ADDRSIZE) < 0)
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
2016-11-01 22:03:16 +01:00
|
|
|
finfo("wait\n");
|
2017-06-28 21:17:17 +02:00
|
|
|
if (!wait--)
|
2016-11-01 22:03:16 +01:00
|
|
|
{
|
2016-11-01 22:13:09 +01:00
|
|
|
return -ETIMEDOUT;
|
2016-11-01 22:03:16 +01:00
|
|
|
}
|
2016-11-01 22:13:09 +01:00
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2011-11-01 14:08:19 +01:00
|
|
|
}
|
2015-03-17 21:27:27 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
at24c_i2c_write(priv,
|
|
|
|
at24addr,
|
|
|
|
buf,
|
|
|
|
AT24XX_PAGESIZE + AT24XX_ADDRSIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Name: at24c_erase
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static int at24c_erase(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t startblock,
|
|
|
|
size_t nblocks)
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
|
|
|
/* EEprom need not erase */
|
|
|
|
|
|
|
|
return (int)nblocks;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2015-03-17 21:27:27 +01:00
|
|
|
* Name: at24c_read_internal
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2015-03-17 21:27:27 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t at24c_read_internal(FAR struct at24c_dev_s *priv,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
|
|
|
FAR uint8_t *buffer,
|
2015-03-17 21:27:27 +01:00
|
|
|
uint8_t address)
|
|
|
|
{
|
|
|
|
uint8_t buf[AT24XX_ADDRSIZE];
|
2016-02-01 19:14:31 +01:00
|
|
|
uint16_t at24addr;
|
2016-11-01 22:03:16 +01:00
|
|
|
uint16_t wait;
|
2015-03-17 21:27:27 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("offset: %lu nbytes: %lu address: %02x\n",
|
2015-03-17 21:27:27 +01:00
|
|
|
(unsigned long)offset, (unsigned long)nbytes, address);
|
|
|
|
|
2020-11-30 04:49:08 +01:00
|
|
|
/* "Random Read: A Random Read requires a dummy byte write sequence to
|
|
|
|
* load in the data word address. Once the device address word and data
|
|
|
|
* word address are clocked in and acknowledged by the EEPROM, the
|
|
|
|
* microcontroller must generate another Start condition. The
|
|
|
|
* microcontroller now initiates a current address read by sending a
|
|
|
|
* device address with the read/write select bit high. The EEPROM
|
|
|
|
* acknowledges the device address and serially clocks out the data word.
|
|
|
|
* To end the random read sequence, the microcontroller does not respond
|
|
|
|
* with a zero but does generate a Stop condition in the subsequent clock
|
|
|
|
* cycle."
|
2015-03-17 21:27:27 +01:00
|
|
|
*
|
2020-11-30 04:49:08 +01:00
|
|
|
* A repeated START after the address is suggested, however, this simple
|
|
|
|
* driver just performs the write as a sepate transfer with an additional,
|
|
|
|
* unnecessary STOP.
|
2015-03-17 21:27:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if AT24XX_ADDRSIZE == 2
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[1] = offset & 0xff;
|
|
|
|
buf[0] = (offset >> 8) & 0xff;
|
|
|
|
at24addr = address;
|
2015-03-17 21:27:27 +01:00
|
|
|
#else
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[0] = offset & 0xff;
|
|
|
|
at24addr = (address | ((offset >> 8) & 0x07));
|
2015-03-17 21:27:27 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-01 22:03:16 +01:00
|
|
|
wait = CONFIG_AT24XX_TIMEOUT_MS;
|
2016-02-01 19:14:31 +01:00
|
|
|
while (at24c_i2c_write(priv, at24addr, buf, AT24XX_ADDRSIZE) < 0)
|
2015-03-17 21:27:27 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("wait\n");
|
2017-06-28 21:17:17 +02:00
|
|
|
if (!wait--)
|
2016-11-01 22:03:16 +01:00
|
|
|
{
|
2016-11-01 22:13:09 +01:00
|
|
|
return -ETIMEDOUT;
|
2016-11-01 22:03:16 +01:00
|
|
|
}
|
2016-11-01 22:13:09 +01:00
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2015-03-17 21:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then transfer the following bytes */
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
at24c_i2c_read(priv, at24addr, buffer, nbytes);
|
2015-03-17 21:27:27 +01:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Name: at24c_bread
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
static ssize_t at24c_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
|
2015-03-17 21:27:27 +01:00
|
|
|
off_t offset;
|
|
|
|
ssize_t nread;
|
|
|
|
size_t i;
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
2011-11-01 17:48:21 +01:00
|
|
|
startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
|
|
|
nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %lu\n",
|
2015-03-17 21:27:27 +01:00
|
|
|
(unsigned long)startblock, (unsigned long)nblocks);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
if (startblock >= priv->npages)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startblock + nblocks > priv->npages)
|
|
|
|
{
|
|
|
|
nblocks = priv->npages - startblock;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
/* Convert the access from startblock and number of blocks to a byte
|
|
|
|
* offset and number of bytes.
|
|
|
|
*/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
offset = startblock * priv->pagesize;
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
/* Then perform the byte-oriented read for each block separately */
|
|
|
|
|
|
|
|
for (i = 0; i < nblocks; i++)
|
|
|
|
{
|
|
|
|
nread = at24c_read_internal(priv, offset, priv->pagesize, buffer,
|
|
|
|
priv->addr);
|
|
|
|
if (nread < 0)
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
2015-03-17 21:27:27 +01:00
|
|
|
return nread;
|
2011-11-01 14:08:19 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
offset += priv->pagesize;
|
2016-03-08 22:31:23 +01:00
|
|
|
buffer += priv->pagesize;
|
2016-03-08 22:33:33 +01:00
|
|
|
}
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
2020-11-30 04:49:08 +01:00
|
|
|
return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
#else
|
2020-11-30 04:49:08 +01:00
|
|
|
return nblocks;
|
2011-11-01 14:08:19 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Name: at24c_bwrite
|
|
|
|
*
|
|
|
|
* Operates on MTD block's and translates to FLASH pages
|
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2020-11-30 04:49:08 +01:00
|
|
|
static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks,
|
2011-11-01 14:08:19 +01:00
|
|
|
FAR const uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
|
|
|
|
size_t blocksleft;
|
2015-03-17 21:27:27 +01:00
|
|
|
uint8_t buf[AT24XX_PAGESIZE + AT24XX_ADDRSIZE];
|
2016-11-01 22:03:16 +01:00
|
|
|
uint16_t wait;
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
2011-11-01 17:48:21 +01:00
|
|
|
startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
|
|
|
nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
#endif
|
|
|
|
blocksleft = nblocks;
|
|
|
|
|
|
|
|
if (startblock >= priv->npages)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startblock + nblocks > priv->npages)
|
|
|
|
{
|
|
|
|
nblocks = priv->npages - startblock;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
while (blocksleft-- > 0)
|
|
|
|
{
|
|
|
|
uint16_t offset = startblock * priv->pagesize;
|
2016-02-01 19:14:31 +01:00
|
|
|
uint16_t at24addr;
|
|
|
|
|
2015-10-21 05:32:57 +02:00
|
|
|
#if AT24XX_ADDRSIZE == 2
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[1] = offset & 0xff;
|
|
|
|
buf[0] = (offset >> 8) & 0xff;
|
|
|
|
at24addr = priv->addr;
|
2015-03-17 21:27:27 +01:00
|
|
|
#else
|
2016-02-01 19:14:31 +01:00
|
|
|
buf[0] = offset & 0xff;
|
|
|
|
at24addr = (priv->addr | ((offset >> 8) & 0x07));
|
2015-03-17 21:27:27 +01:00
|
|
|
#endif
|
|
|
|
|
2016-11-01 22:03:16 +01:00
|
|
|
wait = CONFIG_AT24XX_TIMEOUT_MS;
|
2016-02-01 19:14:31 +01:00
|
|
|
while (at24c_i2c_write(priv, at24addr, buf, AT24XX_ADDRSIZE) < 0)
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("wait\n");
|
2017-06-28 21:17:17 +02:00
|
|
|
if (!wait--)
|
2016-11-01 22:03:16 +01:00
|
|
|
{
|
2016-11-01 22:13:09 +01:00
|
|
|
return -ETIMEDOUT;
|
2016-11-01 22:03:16 +01:00
|
|
|
}
|
2016-11-01 22:13:09 +01:00
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2011-11-01 14:08:19 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
memcpy(&buf[AT24XX_ADDRSIZE], buffer, priv->pagesize);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
at24c_i2c_write(priv, at24addr, buf, priv->pagesize + AT24XX_ADDRSIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
startblock++;
|
|
|
|
buffer += priv->pagesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
2011-11-01 17:48:21 +01:00
|
|
|
return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
|
2011-11-01 14:08:19 +01:00
|
|
|
#else
|
|
|
|
return nblocks;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2015-03-17 21:27:27 +01:00
|
|
|
* Name: at24c_read
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2015-03-17 21:27:27 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
static ssize_t at24c_read(FAR struct mtd_dev_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
size_t nbytes,
|
2015-03-17 21:27:27 +01:00
|
|
|
FAR uint8_t *buffer)
|
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
|
|
|
|
size_t memsize;
|
|
|
|
uint8_t addr;
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
finfo("offset: %lu nbytes: %lu\n",
|
|
|
|
(unsigned long)offset,
|
|
|
|
(unsigned long)nbytes);
|
2015-03-17 21:27:27 +01:00
|
|
|
|
|
|
|
/* Don't permit reads beyond the end of the memory region */
|
|
|
|
|
2015-05-15 14:15:58 +02:00
|
|
|
#ifdef CONFIG_AT24XX_EXTENDED
|
2015-03-17 21:27:27 +01:00
|
|
|
if (priv->extended)
|
|
|
|
{
|
|
|
|
memsize = CONFIG_AT24XX_EXTSIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
memsize = AT24XX_NPAGES * AT24XX_PAGESIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset + nbytes > memsize)
|
|
|
|
{
|
|
|
|
/* Return 0 meaning end-of-file */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the I2C address, converting it to the extended I2C if needed */
|
|
|
|
|
|
|
|
addr = priv->addr;
|
2015-05-15 14:15:58 +02:00
|
|
|
#ifdef CONFIG_AT24XX_EXTENDED
|
2015-03-17 21:27:27 +01:00
|
|
|
if (priv->extended)
|
|
|
|
{
|
|
|
|
addr |= 0x08;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then perform the byte-oriented read using common logic */
|
|
|
|
|
|
|
|
return at24c_read_internal(priv, offset, nbytes, buffer, addr);
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Name: at24c_ioctl
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
static int at24c_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
2021-12-26 23:18:22 +01:00
|
|
|
finfo("cmd: %d\n", cmd);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
2020-11-30 04:49:08 +01:00
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)
|
|
|
|
((uintptr_t)arg);
|
2011-11-01 14:08:19 +01:00
|
|
|
if (geo)
|
|
|
|
{
|
2021-08-11 06:21:39 +02:00
|
|
|
memset(geo, 0, sizeof(*geo));
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* Populate the geometry structure with information need to
|
|
|
|
* know the capacity and how to access the device.
|
2011-11-01 14:08:19 +01:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
* NOTE:
|
|
|
|
* that the device is treated as though it where just an array
|
|
|
|
* of fixed size blocks.
|
|
|
|
* That is most likely not true, but the client will expect the
|
|
|
|
* device logic to do whatever is necessary to make it appear
|
|
|
|
* so.
|
2011-11-01 14:08:19 +01:00
|
|
|
*
|
|
|
|
* blocksize:
|
2021-01-27 16:48:40 +01:00
|
|
|
* May be user defined.
|
|
|
|
* The block size for the at24XX devices may be larger than
|
|
|
|
* the page size in order to better support file systems.
|
|
|
|
* The read and write functions translate BLOCKS to pages
|
|
|
|
* for the small flash devices
|
2011-11-01 14:08:19 +01:00
|
|
|
* erasesize:
|
2021-01-27 16:48:40 +01:00
|
|
|
* It has to be at least as big as the blocksize, bigger
|
|
|
|
* serves no purpose.
|
2011-11-01 14:08:19 +01:00
|
|
|
* neraseblocks
|
2021-01-27 16:48:40 +01:00
|
|
|
* Note that the device size is in kilobits and must be
|
|
|
|
* scaled by 1024 / 8
|
2011-11-01 14:08:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
|
|
|
geo->blocksize = CONFIG_AT24XX_MTD_BLOCKSIZE;
|
2011-11-01 17:48:21 +01:00
|
|
|
geo->erasesize = CONFIG_AT24XX_MTD_BLOCKSIZE;
|
2020-11-30 04:49:08 +01:00
|
|
|
geo->neraseblocks = (CONFIG_AT24XX_SIZE * 1024 / 8) /
|
|
|
|
CONFIG_AT24XX_MTD_BLOCKSIZE;
|
2011-11-01 14:08:19 +01:00
|
|
|
#else
|
|
|
|
geo->blocksize = priv->pagesize;
|
|
|
|
geo->erasesize = priv->pagesize;
|
|
|
|
geo->neraseblocks = priv->npages;
|
|
|
|
#endif
|
|
|
|
ret = OK;
|
|
|
|
|
2020-11-30 04:50:17 +01:00
|
|
|
finfo("blocksize: %" PRId32 " erasesize: %" PRId32
|
|
|
|
" neraseblocks: %" PRId32 "\n",
|
2011-11-01 14:08:19 +01:00
|
|
|
geo->blocksize, geo->erasesize, geo->neraseblocks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-15 20:10:23 +02:00
|
|
|
case BIOC_PARTINFO:
|
|
|
|
{
|
|
|
|
FAR struct partition_info_s *info =
|
|
|
|
(FAR struct partition_info_s *)arg;
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
|
|
|
|
info->numsectors = (CONFIG_AT24XX_SIZE * 1024 / 8) /
|
|
|
|
CONFIG_AT24XX_MTD_BLOCKSIZE;
|
|
|
|
info->sectorsize = CONFIG_AT24XX_MTD_BLOCKSIZE;
|
|
|
|
#else
|
|
|
|
info->numsectors = priv->npages;
|
|
|
|
info->sectorsize = priv->pagesize;
|
|
|
|
#endif
|
|
|
|
info->startsector = 0;
|
|
|
|
info->parent[0] = '\0';
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
case MTDIOC_BULKERASE:
|
2013-05-01 18:59:57 +02:00
|
|
|
ret = at24c_eraseall(priv);
|
2011-11-01 14:08:19 +01:00
|
|
|
break;
|
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
#ifdef CONFIG_AT24XX_EXTENDED
|
|
|
|
case MTDIOC_EXTENDED:
|
|
|
|
priv->extended = (arg != 0);
|
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY; /* Bad command */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Public Functions
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2011-11-01 14:08:19 +01:00
|
|
|
* Name: at24c_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Create an initialized MTD device instance.
|
|
|
|
* MTD devices are not registered in the file system, but are created
|
|
|
|
* as instances that can be bound to other functions
|
|
|
|
* (such as a block or character driver front end).
|
2011-11-01 14:08:19 +01:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2011-11-01 14:08:19 +01:00
|
|
|
|
2015-11-17 14:40:17 +01:00
|
|
|
#ifdef CONFIG_AT24XX_MULTI
|
2021-01-27 16:48:40 +01:00
|
|
|
FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_master_s *dev,
|
|
|
|
uint8_t address)
|
2015-11-17 14:40:17 +01:00
|
|
|
#else
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct mtd_dev_s *at24c_initialize(FAR struct i2c_master_s *dev)
|
2015-11-17 14:40:17 +01:00
|
|
|
#endif
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv;
|
|
|
|
|
2015-11-17 14:40:17 +01:00
|
|
|
#ifdef CONFIG_AT24XX_MULTI
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("dev: %p address: %02x\n", dev, address);
|
2011-11-01 14:08:19 +01:00
|
|
|
|
|
|
|
/* Allocate a state structure (we allocate the structure instead of using
|
|
|
|
* a fixed, static allocation so that we can handle multiple FLASH devices.
|
|
|
|
* The current implementation would handle only one FLASH part per I2C
|
2021-01-27 16:48:40 +01:00
|
|
|
* device (only because of the SPIDEV_FLASH(0) definition) and so would
|
|
|
|
* have to be extended to handle multiple FLASH parts on the same I2C bus.
|
2011-11-01 14:08:19 +01:00
|
|
|
*/
|
|
|
|
|
2015-11-17 14:40:17 +01:00
|
|
|
priv = (FAR struct at24c_dev_s *)kmm_zalloc(sizeof(struct at24c_dev_s));
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate device structure\n");
|
2015-11-17 14:40:17 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("dev: %p\n", dev);
|
2015-11-17 14:40:17 +01:00
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/* If only a signal AT24 part is supported then a statically allocated
|
|
|
|
* state structure is used.
|
2015-11-17 14:40:17 +01:00
|
|
|
*/
|
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
priv = &g_at24c;
|
2015-11-17 14:40:17 +01:00
|
|
|
#endif
|
|
|
|
|
2015-03-17 21:27:27 +01:00
|
|
|
if (!priv->initd)
|
2011-11-01 14:08:19 +01:00
|
|
|
{
|
|
|
|
/* Initialize the allocated structure */
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2015-11-17 14:40:17 +01:00
|
|
|
#ifdef CONFIG_AT24XX_MULTI
|
|
|
|
priv->addr = address;
|
|
|
|
#else
|
2011-11-01 14:08:19 +01:00
|
|
|
priv->addr = CONFIG_AT24XX_ADDR;
|
2015-11-17 14:40:17 +01:00
|
|
|
#endif
|
2011-11-01 14:08:19 +01:00
|
|
|
priv->pagesize = AT24XX_PAGESIZE;
|
|
|
|
priv->npages = AT24XX_NPAGES;
|
|
|
|
|
2013-05-01 18:59:57 +02:00
|
|
|
/* NOTE since the pre-allocated device structure lies in .bss, any
|
|
|
|
* unsupported methods should have been nullified.
|
|
|
|
*/
|
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
priv->mtd.erase = at24c_erase;
|
|
|
|
priv->mtd.bread = at24c_bread;
|
|
|
|
priv->mtd.bwrite = at24c_bwrite;
|
2015-03-17 21:27:27 +01:00
|
|
|
priv->mtd.read = at24c_read;
|
2011-11-01 14:08:19 +01:00
|
|
|
priv->mtd.ioctl = at24c_ioctl;
|
2018-11-08 16:46:11 +01:00
|
|
|
priv->mtd.name = "at24xx";
|
2011-11-01 14:08:19 +01:00
|
|
|
priv->dev = dev;
|
2015-03-17 21:27:27 +01:00
|
|
|
priv->initd = true;
|
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2011-11-01 14:08:19 +01:00
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Return %p\n", priv);
|
2011-11-01 14:08:19 +01:00
|
|
|
return (FAR struct mtd_dev_s *)priv;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:48:40 +01:00
|
|
|
/****************************************************************************
|
2015-11-17 15:39:06 +01:00
|
|
|
* Name: at24c_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
2021-01-27 16:48:40 +01:00
|
|
|
* Release resources held by an allocated MTD device instance.
|
|
|
|
* Resources are only allocated for the case where multiple AT24xx
|
|
|
|
* devices are support.
|
2015-11-17 15:39:06 +01:00
|
|
|
*
|
2021-01-27 16:48:40 +01:00
|
|
|
****************************************************************************/
|
2015-11-17 15:39:06 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_AT24XX_MULTI
|
|
|
|
void at24c_uninitialize(FAR struct mtd_dev_s *mtd)
|
|
|
|
{
|
|
|
|
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)mtd;
|
|
|
|
DEBUGASSERT(priv != NULL);
|
2016-03-17 16:55:10 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_REGISTRATION
|
|
|
|
/* Unregister the MTD with the procfs system if enabled */
|
|
|
|
|
|
|
|
mtd_unregister(&priv->mtd);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Free the MTD driver instance */
|
|
|
|
|
2015-11-17 15:39:06 +01:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_AT24XX_MULTI */
|
2015-03-17 21:27:27 +01:00
|
|
|
#endif /* CONFIG_MTD_AT24XX */
|