Refactor mtd to make available to board startup

This commit is contained in:
David Sidrane
2020-11-16 13:34:46 -08:00
committed by Daniel Agar
parent 1b9ab2a3e2
commit 68ab736b16
11 changed files with 895 additions and 467 deletions

View File

@@ -35,6 +35,7 @@ __BEGIN_DECLS
int px4_platform_init(void);
int px4_platform_console_init(void);
int px4_platform_configure(void);
__END_DECLS

View File

@@ -0,0 +1,80 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
#pragma once
#include <stdint.h>
typedef enum {
MTD_PARAMETERS = 1,
MTD_WAYPOINTS = 2,
MTD_CALDATA = 3,
MTD_MFT = 4,
MTD_ID = 5,
MTD_NET = 6,
} px4_mtd_types_t;
typedef struct {
const px4_mtd_types_t type;
const char *path;
const uint32_t nblocks;
} px4_mtd_part_t;
typedef struct {
const px4_mft_device_t *device;
const uint32_t npart;
const px4_mtd_part_t partd[];
} px4_mtd_entry_t;
typedef struct {
const uint32_t nconfigs;
const px4_mtd_entry_t *entries[];
} px4_mtd_manifest_t;
__BEGIN_DECLS
/************************************************************************************
* Name: px4_mtd_config
*
* Description:
* A board will call this function, to set up the mtd partitions
*
* Input Parameters:
* mtd_list - px4_mtd_config list/count
*
* Returned Value:
* non zero if error
*
************************************************************************************/
__EXPORT int px4_mtd_config(const px4_mtd_manifest_t *mft_mtd);
__END_DECLS

View File

@@ -0,0 +1,102 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
#pragma once
#include <stdint.h>
typedef enum {
MFT = 0,
MTD = 1,
} px4_manifest_types_e;
typedef struct {
enum px4_bus_type {
I2C = 0,
SPI = 1,
} bus_type;
uint32_t devid;
} px4_mft_device_t;
#define PX4_MK_I2C_DEVID(b,a) ((b) << 16 | ((a) & 0xffff))
#define PX4_I2C_DEVID_BUS(d) (((d) >> 16) & 0xffff)
#define PX4_I2C_DEVID_ADDR(d) ((d) & 0xffff)
typedef struct {
const px4_manifest_types_e type;
const void *pmft;
} px4_mft_entry_s;
typedef struct {
const uint32_t nmft;
const px4_mft_entry_s *mfts;
} px4_mft_s;
#include "px4_platform_common/mtd_manifest.h"
__BEGIN_DECLS
/************************************************************************************
* Name: board_get_manifest
*
* Description:
* A board will provide this function to return the manifest
*
* Input Parameters:
* mft - a pointer to the receive the manifest
*
* Returned Value:
* non zero if error
*
************************************************************************************/
__EXPORT const px4_mft_s *board_get_manifest(void);
/************************************************************************************
* Name: px4_mft_configure
*
* Description:
* The Px4 layer will provide this interface to start/configure the
* hardware.
*
* Input Parameters:
* mft - a pointer to the manifest
*
* Returned Value:
* non zero if error
*
************************************************************************************/
__EXPORT int px4_mft_configure(const px4_mft_s *mft);
__END_DECLS

View File

@@ -0,0 +1,80 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
#pragma once
#include <stdint.h>
__BEGIN_DECLS
// The data needed to interface with mtd device's
typedef struct {
struct mtd_dev_s *mtd_dev;
int *partition_block_counts;
const char **partition_names;
struct mtd_dev_s **part_dev;
uint32_t devid;
unsigned n_partitions_current;
} mtd_instance_s;
/*
mtd operations
*/
/*
* Get device an pinter to the array of mtd_instance_s of the system
* count - receives the number of instances pointed to by the pointer
* retunred.
*
* returns: - A pointer to the mtd_instance_s of the system
* This can be Null if there are no mtd instances.
*
*/
__EXPORT mtd_instance_s *px4_mtd_get_instances(unsigned int *count);
/*
Get device complete geometry or a device
*/
__EXPORT int px4_mtd_get_geometry(const mtd_instance_s *instance, unsigned long *blocksize, unsigned long *erasesize,
unsigned long *neraseblocks, unsigned *blkpererase, unsigned *nblocks,
unsigned *partsize);
/*
Get size of a parttion on an instance.
*/
__EXPORT ssize_t px4_mtd_get_partition_size(const mtd_instance_s *instance, const char *partname);
FAR struct mtd_dev_s *px4_at24c_initialize(FAR struct i2c_master_s *dev,
uint8_t address);
__END_DECLS

View File

@@ -45,6 +45,9 @@ if(NOT PX4_BOARD MATCHES "px4_io")
tasks.cpp
px4_nuttx_impl.cpp
px4_init.cpp
px4_manifest.cpp
px4_mtd.cpp
px4_24xxxx_mtd.c
)
target_link_libraries(px4_layer
PRIVATE

View File

@@ -0,0 +1,617 @@
/************************************************************************************
* Driver for 24xxxx-style I2C EEPROMs.
*
* Adapted from:
*
* drivers/mtd/at24xx.c
* Driver for I2C-based at24cxx EEPROM(at24c32,at24c64,at24c128,at24c256)
*
* Copyright (C) 2011 Li Zhuoyi. All rights reserved.
* Author: Li Zhuoyi <lzyy.cn@gmail.com>
* History: 0.1 2011-08-20 initial version
*
* 2011-11-1 Added support for larger MTD block sizes: Hal Glenn <hglenn@2g-eng.com>
*
* Derived from drivers/mtd/m25px.c
*
* Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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.
*
************************************************************************************/
/************************************************************************************
* Included Files
************************************************************************************/
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/px4_mtd.h>
#include <px4_platform_common/time.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/mtd/mtd.h>
#include <perf/perf_counter.h>
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
/*
* Configuration is as for the AT24 driver, but the CONFIG_MTD_AT24XX define should be
* omitted in order to avoid building the AT24XX driver as well.
*/
/* As a minimum, the size of the AT24 part and its 7-bit I2C address are required. */
#ifndef CONFIG_AT24XX_SIZE
/* XXX this is a well vetted special case,
* do not issue a warning any more
* # warning "Assuming AT24 size 64"
*/
# define CONFIG_AT24XX_SIZE 64
#endif
#ifndef CONFIG_AT24XX_ADDR
/* XXX this is a well vetted special case,
* do not issue a warning any more
* # warning "Assuming AT24 address of 0x50"
*/
# define CONFIG_AT24XX_ADDR 0x50
#endif
/* Get the part configuration based on the size configuration */
#if CONFIG_AT24XX_SIZE == 2 /* AT24C02: 2Kbits = 256; 16 * 16 = 256 */
# define AT24XX_NPAGES 16
# define AT24XX_PAGESIZE 16
# define AT24XX_ADDRSIZE 1
#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
#elif CONFIG_AT24XX_SIZE == 32
# define AT24XX_NPAGES 128
# define AT24XX_PAGESIZE 32
#elif CONFIG_AT24XX_SIZE == 48
# define AT24XX_NPAGES 192
# define AT24XX_PAGESIZE 32
#elif CONFIG_AT24XX_SIZE == 64
# define AT24XX_NPAGES 256
# define AT24XX_PAGESIZE 32
#elif CONFIG_AT24XX_SIZE == 128
# define AT24XX_NPAGES 256
# define AT24XX_PAGESIZE 64
#elif CONFIG_AT24XX_SIZE == 256
# define AT24XX_NPAGES 512
# define AT24XX_PAGESIZE 64
#endif
/* 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.
*/
#ifndef CONFIG_AT24XX_MTD_BLOCKSIZE
/* XXX this is a well vetted special case,
* do not issue a warning any more
* # warning "Assuming driver block size is the same as the FLASH page size"
*/
# define CONFIG_AT24XX_MTD_BLOCKSIZE AT24XX_PAGESIZE
#endif
/* The AT24 does not respond on the bus during write cycles, so we depend on a long
* timeout to detect problems. The max program time is typically ~5ms.
*/
#ifndef CONFIG_AT24XX_WRITE_TIMEOUT_MS
# define CONFIG_AT24XX_WRITE_TIMEOUT_MS 20
#endif
/************************************************************************************
* Private Types
************************************************************************************/
/* 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 {
struct mtd_dev_s mtd; /* MTD interface */
FAR struct i2c_master_s *dev; /* Saved I2C interface instance */
uint8_t addr; /* I2C address */
uint16_t pagesize; /* 32, 63 */
uint16_t npages; /* 128, 256, 512, 1024 */
perf_counter_t perf_transfers;
perf_counter_t perf_resets_retries;
perf_counter_t perf_errors;
};
/************************************************************************************
* Private Function Prototypes
************************************************************************************/
/* MTD driver methods */
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,
size_t nblocks, FAR uint8_t *buf);
static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
size_t nblocks, FAR const uint8_t *buf);
static int at24c_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg);
void at24c_test(void);
int at24c_nuke(void);
/************************************************************************************
* Private Data
************************************************************************************/
/* At present, only a single AT24 part is supported. In this case, a statically
* allocated state structure may be used.
*/
static struct at24c_dev_s g_at24c;
/************************************************************************************
* Private Functions
************************************************************************************/
static int at24c_eraseall(FAR struct at24c_dev_s *priv)
{
int startblock = 0;
uint8_t buf[AT24XX_PAGESIZE + 2];
struct i2c_msg_s msgv[1] = {
{
.frequency = 400000,
.addr = priv->addr,
.flags = 0,
.buffer = &buf[0],
.length = sizeof(buf),
}
};
memset(&buf[2], 0xff, priv->pagesize);
BOARD_EEPROM_WP_CTRL(false);
for (startblock = 0; startblock < priv->npages; startblock++) {
uint16_t offset = startblock * priv->pagesize;
buf[1] = offset & 0xff;
buf[0] = (offset >> 8) & 0xff;
while (I2C_TRANSFER(priv->dev, &msgv[0], 1) < 0) {
fwarn("erase stall\n");
px4_usleep(10000);
}
}
BOARD_EEPROM_WP_CTRL(true);
return OK;
}
/************************************************************************************
* Name: at24c_erase
************************************************************************************/
static int at24c_erase(FAR struct mtd_dev_s *dev, off_t startblock, size_t nblocks)
{
/* EEprom need not erase */
return (int)nblocks;
}
/************************************************************************************
* Name: at24c_test
************************************************************************************/
void at24c_test(void)
{
uint8_t buf[CONFIG_AT24XX_MTD_BLOCKSIZE];
unsigned count = 0;
unsigned errors = 0;
for (count = 0; count < 10000; count++) {
ssize_t result = at24c_bread(&g_at24c.mtd, 0, 1, buf);
if (result == ERROR) {
if (errors++ > 2) {
syslog(LOG_INFO, "too many errors\n");
return;
}
} else if (result != 1) {
syslog(LOG_INFO, "unexpected %u\n", result);
}
if ((count % 100) == 0) {
syslog(LOG_INFO, "test %u errors %u\n", count, errors);
}
}
}
/************************************************************************************
* Name: at24c_bread
************************************************************************************/
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;
size_t blocksleft;
uint8_t addr[2];
int ret;
struct i2c_msg_s msgv[2] = {
{
.frequency = 400000,
.addr = priv->addr,
.flags = 0,
.buffer = &addr[0],
.length = sizeof(addr),
},
{
.frequency = 400000,
.addr = priv->addr,
.flags = I2C_M_READ,
.buffer = 0,
.length = priv->pagesize,
}
};
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
#endif
blocksleft = nblocks;
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
if (startblock >= priv->npages) {
return 0;
}
if (startblock + nblocks > priv->npages) {
nblocks = priv->npages - startblock;
}
while (blocksleft-- > 0) {
uint16_t offset = startblock * priv->pagesize;
unsigned tries = CONFIG_AT24XX_WRITE_TIMEOUT_MS;
addr[1] = offset & 0xff;
addr[0] = (offset >> 8) & 0xff;
msgv[1].buffer = buffer;
for (;;) {
perf_begin(priv->perf_transfers);
ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
perf_end(priv->perf_transfers);
if (ret >= 0) {
break;
}
finfo("read stall");
px4_usleep(1000);
/* We should normally only be here on the first read after
* a write.
*
* XXX maybe do special first-read handling with optional
* bus reset as well?
*/
perf_count(priv->perf_resets_retries);
if (--tries == 0) {
perf_count(priv->perf_errors);
return ERROR;
}
}
startblock++;
buffer += priv->pagesize;
}
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
#else
return nblocks;
#endif
}
/************************************************************************************
* Name: at24c_bwrite
*
* Operates on MTD block's and translates to FLASH pages
*
************************************************************************************/
static ssize_t at24c_bwrite(FAR struct mtd_dev_s *dev, off_t startblock, size_t nblocks,
FAR const uint8_t *buffer)
{
FAR struct at24c_dev_s *priv = (FAR struct at24c_dev_s *)dev;
size_t blocksleft;
uint8_t buf[AT24XX_PAGESIZE + 2];
int ret;
struct i2c_msg_s msgv[1] = {
{
.frequency = 400000,
.addr = priv->addr,
.flags = 0,
.buffer = &buf[0],
.length = sizeof(buf),
}
};
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
startblock *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
nblocks *= (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
#endif
blocksleft = nblocks;
if (startblock >= priv->npages) {
return 0;
}
if (startblock + nblocks > priv->npages) {
nblocks = priv->npages - startblock;
}
finfo("startblock: %08lx nblocks: %d\n", (long)startblock, (int)nblocks);
BOARD_EEPROM_WP_CTRL(false);
while (blocksleft-- > 0) {
uint16_t offset = startblock * priv->pagesize;
unsigned tries = CONFIG_AT24XX_WRITE_TIMEOUT_MS;
buf[1] = offset & 0xff;
buf[0] = (offset >> 8) & 0xff;
memcpy(&buf[2], buffer, priv->pagesize);
for (;;) {
perf_begin(priv->perf_transfers);
ret = I2C_TRANSFER(priv->dev, &msgv[0], 1);
perf_end(priv->perf_transfers);
if (ret >= 0) {
break;
}
finfo("write stall");
px4_usleep(1000);
/* We expect to see a number of retries per write cycle as we
* poll for write completion.
*/
if (--tries == 0) {
perf_count(priv->perf_errors);
BOARD_EEPROM_WP_CTRL(true);
return ERROR;
}
}
startblock++;
buffer += priv->pagesize;
}
BOARD_EEPROM_WP_CTRL(true);
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
return nblocks / (CONFIG_AT24XX_MTD_BLOCKSIZE / AT24XX_PAGESIZE);
#else
return nblocks;
#endif
}
/************************************************************************************
* Name: at24c_ioctl
************************************************************************************/
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 */
finfo("cmd: %d \n", cmd);
switch (cmd) {
case MTDIOC_GEOMETRY: {
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)((uintptr_t)arg);
if (geo) {
/* Populate the geometry structure with information need to know
* the capacity and how to access the device.
*
* 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.
*
* blocksize:
* 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
* erasesize:
* It has to be at least as big as the blocksize, bigger serves no
* purpose.
* neraseblocks
* Note that the device size is in kilobits and must be scaled by
* 1024 / 8
*/
#if CONFIG_AT24XX_MTD_BLOCKSIZE > AT24XX_PAGESIZE
geo->blocksize = CONFIG_AT24XX_MTD_BLOCKSIZE;
geo->erasesize = CONFIG_AT24XX_MTD_BLOCKSIZE;
geo->neraseblocks = (CONFIG_AT24XX_SIZE * 1024 / 8) / CONFIG_AT24XX_MTD_BLOCKSIZE;
#else
geo->blocksize = priv->pagesize;
geo->erasesize = priv->pagesize;
geo->neraseblocks = priv->npages;
#endif
ret = OK;
finfo("blocksize: %d erasesize: %d neraseblocks: %d\n",
geo->blocksize, geo->erasesize, geo->neraseblocks);
}
}
break;
case MTDIOC_BULKERASE:
ret = at24c_eraseall(priv);
break;
case MTDIOC_XIPBASE:
default:
ret = -ENOTTY; /* Bad command */
break;
}
return ret;
}
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: at24c_initialize
*
* Description:
* Create an initialize 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).
*
************************************************************************************/
FAR struct mtd_dev_s *px4_at24c_initialize(FAR struct i2c_master_s *dev,
uint8_t address)
{
FAR struct at24c_dev_s *priv;
finfo("dev: %p\n", dev);
/* 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
* device (only because of the SPIDEV_FLASH definition) and so would have
* to be extended to handle multiple FLASH parts on the same I2C bus.
*/
priv = &g_at24c;
if (priv) {
/* Initialize the allocated structure */
priv->addr = address;
priv->pagesize = AT24XX_PAGESIZE;
priv->npages = AT24XX_NPAGES;
priv->mtd.erase = at24c_erase;
priv->mtd.bread = at24c_bread;
priv->mtd.bwrite = at24c_bwrite;
priv->mtd.ioctl = at24c_ioctl;
priv->dev = dev;
priv->perf_transfers = perf_alloc(PC_ELAPSED, "eeprom_trans");
priv->perf_resets_retries = perf_alloc(PC_COUNT, "eeprom_rst");
priv->perf_errors = perf_alloc(PC_COUNT, "eeprom_errs");
}
/* attempt to read to validate device is present */
unsigned char buf[5];
uint8_t addrbuf[2] = {0, 0};
struct i2c_msg_s msgv[2] = {
{
.frequency = 400000,
.addr = priv->addr,
.flags = 0,
.buffer = &addrbuf[0],
.length = sizeof(addrbuf),
},
{
.frequency = 400000,
.addr = priv->addr,
.flags = I2C_M_READ,
.buffer = &buf[0],
.length = sizeof(buf),
}
};
BOARD_EEPROM_WP_CTRL(true);
perf_begin(priv->perf_transfers);
int ret = I2C_TRANSFER(priv->dev, &msgv[0], 2);
perf_end(priv->perf_transfers);
if (ret < 0) {
return NULL;
}
/* Return the implementation-specific state structure as the MTD device */
finfo("Return %p\n", priv);
return (FAR struct mtd_dev_s *)priv;
}
/*
* XXX: debug hackery
*/
int at24c_nuke(void)
{
return at24c_eraseall(&g_at24c);
}

View File

@@ -33,6 +33,7 @@
#include <px4_platform_common/init.h>
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/px4_manifest.h>
#include <px4_platform_common/console_buffer.h>
#include <px4_platform_common/defines.h>
#include <drivers/drv_hrt.h>
@@ -119,3 +120,9 @@ int px4_platform_init(void)
return PX4_OK;
}
int px4_platform_configure(void)
{
return px4_mft_configure(board_get_manifest());
}

View File

@@ -0,0 +1,89 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
/**
* @file px4_manifest.cpp
*
* manifest utilites
*
* @author David Sidrane <david.sidrane@nscdg.com>
*/
#ifndef MODULE_NAME
#define MODULE_NAME "PX4_MANIFEST"
#endif
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/px4_manifest.h>
#include <px4_platform_common/log.h>
#include <errno.h>
__EXPORT const px4_mft_s *board_get_manifest(void) weak_function;
/* This is the default manifest when no MTD driver is installed */
static const px4_mft_entry_s mtd_mft = {
.type = MTD,
};
static const px4_mft_s default_mft = {
.nmft = 1,
.mfts = &mtd_mft
};
const px4_mft_s *board_get_manifest(void)
{
return &default_mft;
}
__EXPORT int px4_mft_configure(const px4_mft_s *mft)
{
if (mft != nullptr) {
for (uint32_t m = 0; m < mft->nmft; m++) {
switch (mft->mfts[m].type) {
case MTD:
px4_mtd_config(static_cast<const px4_mtd_manifest_t *>(mft->mfts[m].pmft));
break;
case MFT:
default:
break;
}
}
}
return 0;
}

View File

@@ -0,0 +1,408 @@
/****************************************************************************
*
* Copyright (c) 2020 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
/**
* @file px4_mtd.cpp
*
* mtd services.
*
* @author David Sidrane <david.sidrane@nscdg.com>
*/
#ifndef MODULE_NAME
#define MODULE_NAME "PX4_MTD"
#endif
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/px4_mtd.h>
#include <px4_platform_common/px4_manifest.h>
#include <px4_platform_common/log.h>
#include <px4_platform_common/spi.h>
#include <errno.h>
#include <stdbool.h>
#include <nuttx/drivers/drivers.h>
#include <nuttx/spi/spi.h>
#include <nuttx/mtd/mtd.h>
extern "C" {
struct mtd_dev_s *ramtron_initialize(FAR struct spi_dev_s *dev);
struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd,
off_t firstblock, off_t nblocks);
}
static int num_instances = 0;
static mtd_instance_s *instances = nullptr;
static int ramtron_attach(mtd_instance_s &instance)
{
#if !defined(CONFIG_MTD_RAMTRON)
PX4_ERR("Misconfiguration CONFIG_MTD_RAMTRON not set");
return ENXIO;
#else
/* initialize the right spi */
struct spi_dev_s *spi = px4_spibus_initialize(px4_find_spi_bus(instance.devid));
if (spi == nullptr) {
PX4_ERR("failed to locate spi bus");
return -ENXIO;
}
/* this resets the spi bus, set correct bus speed again */
SPI_SETFREQUENCY(spi, 10 * 1000 * 1000);
SPI_SETBITS(spi, 8);
SPI_SETMODE(spi, SPIDEV_MODE3);
SPI_SELECT(spi, instance.devid, false);
/* start the RAMTRON driver, attempt 5 times */
for (int i = 0; i < 5; i++) {
instance.mtd_dev = ramtron_initialize(spi);
if (instance.mtd_dev) {
/* abort on first valid result */
if (i > 0) {
PX4_WARN("mtd needed %d attempts to attach", i + 1);
}
break;
}
}
/* if last attempt is still unsuccessful, abort */
if (instance.mtd_dev == nullptr) {
PX4_ERR("failed to initialize mtd driver");
return -EIO;
}
int ret = instance.mtd_dev->ioctl(instance.mtd_dev, MTDIOC_SETSPEED, (unsigned long)10 * 1000 * 1000);
if (ret != OK) {
// FIXME: From the previous warning call, it looked like this should have been fatal error instead. Tried
// that but setting the bus speed does fail all the time. Which was then exiting and the board would
// not run correctly. So changed to PX4_WARN.
PX4_WARN("failed to set bus speed");
}
return 0;
#endif
}
static int at24xxx_attach(mtd_instance_s &instance)
{
#if !defined(PX4_I2C_BUS_MTD)
PX4_ERR("Misconfiguration PX4_I2C_BUS_MTD not set");
return -ENXIO;
#else
struct i2c_master_s *i2c = px4_i2cbus_initialize(PX4_I2C_DEVID_BUS(instance.devid));
if (i2c == nullptr) {
PX4_ERR("failed to locate I2C bus");
return -ENXIO;
}
/* start the MTD driver, attempt 5 times */
for (int i = 0; i < 5; i++) {
instance.mtd_dev = px4_at24c_initialize(i2c, PX4_I2C_DEVID_ADDR(instance.devid));
if (instance.mtd_dev) {
/* abort on first valid result */
if (i > 0) {
PX4_WARN("EEPROM needed %d attempts to attach", i + 1);
}
break;
}
}
/* if last attempt is still unsuccessful, abort */
if (instance.mtd_dev == nullptr) {
PX4_ERR("failed to initialize EEPROM driver");
return -EIO;
}
return 0;
#endif
}
int px4_mtd_get_geometry(const mtd_instance_s *instance, unsigned long *blocksize, unsigned long *erasesize,
unsigned long *neraseblocks,
unsigned *blkpererase, unsigned *nblocks, unsigned *partsize)
{
/* Get the geometry of the FLASH device */
FAR struct mtd_geometry_s geo;
int ret = instance->mtd_dev->ioctl(instance->mtd_dev, MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo));
if (ret < 0) {
PX4_ERR("mtd->ioctl failed: %d", ret);
return ret;
}
*blocksize = geo.blocksize;
*erasesize = geo.erasesize;
*neraseblocks = geo.neraseblocks;
/* Determine the size of each partition. Make each partition an even
* multiple of the erase block size (perhaps not using some space at the
* end of the FLASH).
*/
*blkpererase = geo.erasesize / geo.blocksize;
*nblocks = (geo.neraseblocks / instance->n_partitions_current) * *blkpererase;
*partsize = *nblocks * geo.blocksize;
return ret;
}
/*
get partition size in bytes
*/
ssize_t px4_mtd_get_partition_size(const mtd_instance_s *instance, const char *partname)
{
unsigned long blocksize, erasesize, neraseblocks;
unsigned blkpererase, nblocks, partsize = 0;
int ret = px4_mtd_get_geometry(instance, &blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize);
if (ret != OK) {
PX4_ERR("Failed to get geometry");
return 0;
}
unsigned partn = 0;
for (unsigned n = 0; n < instance->n_partitions_current; n++) {
if (instance->partition_names[n] != nullptr &&
partname != nullptr &&
strcmp(instance->partition_names[n], partname) == 0) {
partn = n;
break;
}
}
return instance->partition_block_counts[partn] * blocksize;
}
mtd_instance_s *px4_mtd_get_instances(unsigned int *count)
{
*count = num_instances;
return instances;
}
// Define the default FRAM usage
#if !defined(CONFIG_MTD_RAMTRON)
static const px4_mtd_manifest_t default_mtd_config = {
};
#else
const px4_mft_device_t spifram = { // FM25V02A on FMUM 32K 512 X 64
.bus_type = px4_mft_device_t::SPI,
.devid = SPIDEV_FLASH(0)
};
const px4_mtd_entry_t fram = {
.device = &spifram,
.npart = 2,
.partd = {
{
.type = MTD_PARAMETERS,
.path = "/fs/mtd_params",
.nblocks = 32
},
{
.type = MTD_WAYPOINTS,
.path = "/fs/mtd_waypoints",
.nblocks = 32
}
},
};
static const px4_mtd_manifest_t default_mtd_config = {
.nconfigs = 1,
.entries = {
&fram,
}
};
#endif
int px4_mtd_config(const px4_mtd_manifest_t *mft_mtd)
{
int rv = -EINVAL;
const px4_mtd_manifest_t *mtd_list = mft_mtd ? mft_mtd : &default_mtd_config;
if (mtd_list == nullptr) {
PX4_ERR("Invalid mtd configuration!");
return rv;
}
if (mtd_list->nconfigs == 0) {
return 0;
}
rv = -ENOMEM;
int total_blocks = 0;
instances = new mtd_instance_s[mtd_list->nconfigs];
if (instances == nullptr) {
memoryout:
PX4_ERR("failed to allocate memory!");
return rv;
}
for (uint32_t i = 0; i < mtd_list->nconfigs; i++) {
num_instances++;
uint32_t nparts = mtd_list->entries[i]->npart;
instances[i].devid = mtd_list->entries[i]->device->devid;
instances[i].mtd_dev = nullptr;
instances[i].n_partitions_current = 0;
rv = -ENOMEM;
instances[i].part_dev = new FAR struct mtd_dev_s *[nparts];
if (instances[i].part_dev == nullptr) {
goto memoryout;
}
instances[i].partition_block_counts = new int[nparts];
if (instances[i].partition_block_counts == nullptr) {
goto memoryout;
}
instances[i].partition_names = new const char *[nparts];
if (instances[i].partition_names == nullptr) {
goto memoryout;
}
for (uint32_t p = 0; p < nparts; p++) {
instances[i].partition_block_counts[p] = mtd_list->entries[i]->partd[p].nblocks;
instances[i].partition_names[p] = mtd_list->entries[i]->partd[p].path;
}
if (mtd_list->entries[i]->device->bus_type == px4_mft_device_t::I2C) {
rv = at24xxx_attach(instances[i]);
} else {
rv = ramtron_attach(instances[i]);
}
if (rv != 0) {
goto errout;
}
unsigned long blocksize;
unsigned long erasesize;
unsigned long neraseblocks;
unsigned int blkpererase;
unsigned int nblocks;
unsigned int partsize;
rv = px4_mtd_get_geometry(&instances[i], &blocksize, &erasesize, &neraseblocks, &blkpererase, &nblocks, &partsize);
if (rv != 0) {
goto errout;
}
/* Now create MTD FLASH partitions */
char blockname[32];
unsigned offset;
unsigned part;
for (offset = 0, part = 0; rv == 0 && part < nparts; offset += instances[i].partition_block_counts[part], part++) {
/* Create the partition */
instances[i].part_dev[part] = mtd_partition(instances[i].mtd_dev, offset, instances[i].partition_block_counts[part]);
if (instances[i].part_dev[part] == nullptr) {
PX4_ERR("mtd_partition failed. offset=%lu nblocks=%lu",
(unsigned long)offset, (unsigned long)nblocks);
rv = -ENOSPC;
goto errout;
}
/* Initialize to provide an FTL block driver on the MTD FLASH interface */
snprintf(blockname, sizeof(blockname), "/dev/mtdblock%d", total_blocks);
rv = ftl_initialize(total_blocks, instances[i].part_dev[part]);
if (rv < 0) {
PX4_ERR("ftl_initialize %s failed: %d", blockname, rv);
goto errout;
}
total_blocks++;
/* Now create a character device on the block device */
rv = bchdev_register(blockname, instances[i].partition_names[part], false);
if (rv < 0) {
PX4_ERR("bchdev_register %s failed: %d", instances[i].partition_names[part], rv);
goto errout;
}
instances[i].n_partitions_current++;
}
errout:
if (rv < 0) {
PX4_ERR("mtd failure: %d bus %d address %d class %d",
rv,
PX4_I2C_DEVID_BUS(instances[i].devid),
PX4_I2C_DEVID_ADDR(instances[i].devid),
mtd_list->entries[i]->partd[instances[i].n_partitions_current].type);
break;
}
}
return rv;
}