Browse Source

debug lfs

Signed-off-by: surenyi <surenyi82@163.com>
master
surenyi 2 weeks ago
parent
commit
8fa064739f
  1. 2
      .clang-format
  2. 3
      config.h
  3. 2
      ft2000-4.h
  4. 32
      hwconf.c
  5. 973
      sysLib.c
  6. 23
      usrStubs.c
  7. 1782
      vxbFtSpi.c
  8. 9
      vxbFtSpi.h
  9. 501
      vxbLfsLib.c
  10. 232
      vxbSm2130SpiDev.c
  11. 22
      vxbSm2130SpiDev.h

2
.clang-format

@ -53,7 +53,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false BreakStringLiterals: false
ColumnLimit: 80 ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:' CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerAllOnOneLineOrOnePerLine: false

3
config.h

@ -101,6 +101,7 @@ extern "C" {
#define INCLUDE_DISK_UTIL #define INCLUDE_DISK_UTIL
#endif #endif
#define DRV_FS_LITTLEFS 1
/*#define INCLUDE_HRFS*/ /*#define INCLUDE_HRFS*/
#define DRV_VXBEND_FTGMAC #define DRV_VXBEND_FTGMAC
@ -190,7 +191,7 @@ extern "C" {
#define SPI_BOOTROM_SIZE (0x400000) #define SPI_BOOTROM_SIZE (0x400000)
#define SPI_KERNEL_SIZE (0x600000) #define SPI_KERNEL_SIZE (0x600000)
#ifdef INCLUDE_TFFS #if defined(INCLUDE_TFFS) || defined(DRV_FS_LITTLEFS)
# define INCLUDE_TFFS_MOUNT # define INCLUDE_TFFS_MOUNT
# define INCLUDE_TFFS_SHOW # define INCLUDE_TFFS_SHOW

2
ft2000-4.h

@ -97,6 +97,8 @@ enum can_bitrate{
#define REG208 0x208 #define REG208 0x208
#define REG210 0x210 #define REG210 0x210
#define REG214 0x214 #define REG214 0x214
#define REG218 0x218
#define CAN_TXD_0 (1<<24) #define CAN_TXD_0 (1<<24)
#define CAN_RXD_0 (1<<12) #define CAN_RXD_0 (1<<12)
#define CAN_TXD_1 (1<<20) #define CAN_TXD_1 (1<<20)

32
hwconf.c

@ -259,21 +259,29 @@ struct hcfResource qspiResources[] = {
#endif /* DRV_FTQSPI */ #endif /* DRV_FTQSPI */
#ifdef DRV_FTSPI #ifdef DRV_FTSPI
struct vxbSpiDevInfo spiDevTbl1[] = { struct vxbSpiDevInfo sm2130DevRes[] = {
/* name cs width freq mode */ /* name cs width freq mode */
{ "SM2130", 0, 8, 5000000, 3}, { "SM2130", 0, 8, 5000000, 3},
}; };
struct hcfResource spiResources0[] = {
{ "regBase", HCF_RES_INT, { (void *)(FTSPI0_BASE) } },
{ "clkFreq", HCF_RES_INT, { (void *)(FT2000_SPI_CLK) } },
{ "speed", HCF_RES_INT, { (void *)(4000000) } },
{ "mode", HCF_RES_INT, { (void *)3} },
};
#define spiNum0 NELEMENTS(spiResources0)
struct hcfResource spiResources1[] = { struct hcfResource spiResources1[] = {
{ "regBase", HCF_RES_INT, { (void *)(FTSPI1_BASE) } }, { "regBase", HCF_RES_INT, { (void *)(FTSPI1_BASE) } },
{ "clkFreq", HCF_RES_INT, { (void *)(FT2000_SPI_CLK) } }, { "clkFreq", HCF_RES_INT, { (void *)(FT2000_SPI_CLK) } },
{ "speed", HCF_RES_INT, { (void *)(4000000) } }, { "speed", HCF_RES_INT, { (void *)(4000000) } },
{ "mode", HCF_RES_INT, { (void *)3} }, { "mode", HCF_RES_INT, { (void *)3} },
{ "spiDev", HCF_RES_ADDR, { (void *)&spiDevTbl1[0]} }, { "spiDev", HCF_RES_ADDR, { (void *)&sm2130DevRes[0]} },
{ "spiDevNum", HCF_RES_INT, { (void *)NELEMENTS(spiDevTbl1)}}, { "spiDevNum", HCF_RES_INT, { (void *)NELEMENTS(sm2130DevRes)}},
}; };
# define spiNum1 NELEMENTS(spiResources1) #define spiNum1 NELEMENTS(spiResources1)
#endif /* DRV_FTQSPI */ #endif /* DRV_FTSPI */
#ifdef DRV_FTI2C #ifdef DRV_FTI2C
@ -506,6 +514,16 @@ struct hcfResource gmac1Resources[] = {
#define gmac1Num NELEMENTS(gmac1Resources) #define gmac1Num NELEMENTS(gmac1Resources)
#endif /* DRV_VXBEND_FTGMAC */ #endif /* DRV_VXBEND_FTGMAC */
#ifdef DRV_FS_LITTLEFS
struct hcfResource lfsResources[] = {
{ "mountPoint" , HCF_RES_STRING, {(void *)"/lfs0"}},
{ "flashName" , HCF_RES_STRING, {(void *)TFFS_PART0_FLASH_NAME}},
{ "flashOffset", HCF_RES_INT, {(void *)(TFFS_PART0_FLASH_BOOT_SIZE)}},
{ "flashSize" , HCF_RES_INT, {(void *)((TFFS_PART0_FLASH_SIZE) - (TFFS_PART0_FLASH_BOOT_SIZE))}},
};
#define lfsResNum NELEMENTS(lfsResources)
#endif
const struct hcfDevice hcfDeviceList[] = { const struct hcfDevice hcfDeviceList[] = {
#ifdef DRV_ARM_GICV3 #ifdef DRV_ARM_GICV3
{ "armGicDev", 0, VXB_BUSID_PLB, 0, armGICv3Num, armGICv3Resources}, { "armGicDev", 0, VXB_BUSID_PLB, 0, armGICv3Num, armGICv3Resources},
@ -557,6 +575,7 @@ const struct hcfDevice hcfDeviceList[] = {
#endif #endif
#ifdef DRV_FTSPI #ifdef DRV_FTSPI
{ FT_SPI_NAME, 0, VXB_BUSID_PLB, 0, spiNum0, spiResources0 },
{ FT_SPI_NAME, 1, VXB_BUSID_PLB, 0, spiNum1, spiResources1 }, { FT_SPI_NAME, 1, VXB_BUSID_PLB, 0, spiNum1, spiResources1 },
#endif #endif
@ -564,6 +583,9 @@ const struct hcfDevice hcfDeviceList[] = {
{ "ftGpio", 0, VXB_BUSID_PLB, 0, gpioDev0Num, gpioDev0Resources }, { "ftGpio", 0, VXB_BUSID_PLB, 0, gpioDev0Num, gpioDev0Resources },
{ "ftGpio", 1, VXB_BUSID_PLB, 0, gpioDev1Num, gpioDev1Resources }, { "ftGpio", 1, VXB_BUSID_PLB, 0, gpioDev1Num, gpioDev1Resources },
#endif #endif
#ifdef DRV_FS_LITTLEFS
{ "lfs", 0, VXB_BUSID_PLB, 0, lfsResNum, lfsResources},
#endif
}; };
const int hcfDeviceNum = NELEMENTS(hcfDeviceList); const int hcfDeviceNum = NELEMENTS(hcfDeviceList);

973
sysLib.c

File diff suppressed because it is too large

23
usrStubs.c

@ -9,21 +9,24 @@
int usr_ipftps_authenticate_nopasswd(Ipftps_session *session, char *password) int usr_ipftps_authenticate_nopasswd(Ipftps_session *session, char *password)
{ {
printf("auth: it\r\n"); printf("ftp passthor\r\n");
return 0; return 0;
} }
#endif #endif
#if 0 #if defined(__DCC__)
void usrAppInit (void) void usrAppInit(void)
{ {
printf("appInited\r\n");
printf("appInited\r\n");
} }
#endif #endif
#if defined(__GNUC__) int mw(unsigned long addr, unsigned int val)
#include "lfs/lfs.c" {
#include "lfs/lfs_util.c" volatile unsigned int long *ptr = (volatile unsigned long *)(addr);
#endif
*ptr = val;
return 0;
}

1782
vxbFtSpi.c

File diff suppressed because it is too large

9
vxbFtSpi.h

@ -13,6 +13,8 @@
#define FT_SPI_NAME "ftSpi" #define FT_SPI_NAME "ftSpi"
#define SPI_MAX_CS_NUM 4 #define SPI_MAX_CS_NUM 4
#define FSPIM_DEFAULT_FRF 0
#define FSPIM_DEFAULT_CFS 0
/* structure holding the instance specific details */ /* structure holding the instance specific details */
typedef struct _ft_spi_drv_ctrl { typedef struct _ft_spi_drv_ctrl {
VXB_DEVICE_ID pDev; VXB_DEVICE_ID pDev;
@ -24,6 +26,13 @@ typedef struct _ft_spi_drv_ctrl {
int dataWidth; int dataWidth;
int cs; int cs;
UINT32 length; UINT32 length;
UINT32 tx_fifo_len;
UINT32 rx_fifo_len;
UINT32 tx_count;
UINT32 rx_count;
const UINT8* tx_buff;
UINT8* rx_buff;
BOOL en_test;
UINT32 spiDevNum; UINT32 spiDevNum;
UINT32 initPhase; UINT32 initPhase;
SEM_ID muxSem; SEM_ID muxSem;

501
vxbLfsLib.c

@ -1,3 +1,4 @@
/* vim: set ts=4 sw=4 et fdm=marker: */
#include <vxWorks.h> #include <vxWorks.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -7,12 +8,508 @@
#include <taskLib.h> #include <taskLib.h>
#include <sysLib.h> #include <sysLib.h>
#include <tickLib.h> #include <tickLib.h>
#include <iosLib.h>
#include <hwif/vxbus/vxBus.h> #include <hwif/vxbus/vxBus.h>
#include <hwif/vxbus/hwConf.h> #include <hwif/vxbus/hwConf.h>
#include <hwif/vxbus/vxbPlbLib.h> #include <hwif/vxbus/vxbPlbLib.h>
#include <hwif/util/hwMemLib.h> #include <hwif/util/hwMemLib.h>
#include <hwif/util/vxbParamSys.h> #include <hwif/util/vxbParamSys.h>
#include <hwif/vxbus/vxbSpiLib.h> #include <hwif/vxbus/vxbSpiLib.h>
#include "vxbSp25SpiFlash.h" #include <../h/flash/vxbFlash.h>
#include <../h/flash/vxbFlashCommon.h>
#include <usrLib.h> #include <usrLib.h>
#if !defined(__DCC__)
#define LFS_THREADSAFE 1
#include "lfs/lfs.c"
#include "lfs/lfs_util.c"
#endif
typedef struct __lfs_drv_ctrl lfsDrvCtrl;
typedef struct lfs_ios_drv lfsIosDrv;
struct __lfs_file_descr {
union {
lfs_file_t file;
lfs_dir_t dir;
} u;
BOOL isDir;
lfsDrvCtrl *ctrl;
};
typedef struct __lfs_file_descr LFS_FILE_DESC;
struct lfs_ios_drv {
DEV_HDR devHdr;
lfsDrvCtrl *pDrvCtrl;
};
struct __lfs_drv_ctrl {
VXB_DEVICE_ID pDev;
/* per-driver info */
void *pDrvCtrl;
const char *mountPoint;
const char *flashDrvName;
int flashUnit;
UINT32 flashOffset;
UINT32 flashSize;
UINT32 pageSize;
UINT32 blkNum;
int lfsDrvNum;
lfsIosDrv lfsDrv;
FLASH_CHIP_ID mtd;
struct lfs_config lfsConfig;
SEM_ID lock;
lfs_t fsh;
};
/* {{{ lfsDevFuncs */
LOCAL void lfsDevInstInit(VXB_DEVICE_ID pDev)
{
lfsDrvCtrl *drv;
struct hcfDevice *pHcf;
drv = (lfsDrvCtrl *)hwMemAlloc(sizeof(*drv));
if (drv == NULL) {
return;
}
drv->pDrvCtrl = pDev;
pDev->pDrvCtrl = drv;
drv->mtd = NULL;
pHcf = (struct hcfDevice *)hcfDeviceGet(pDev);
if (pHcf == NULL) {
#ifndef _VXBUS_BASIC_HWMEMLIB
hwMemFree((char *)drv);
#endif /* _VXBUS_BASIC_HWMEMLIB */
pDev->pDrvCtrl = NULL;
return;
}
if (devResourceGet(pHcf, "flashName", HCF_RES_STRING, (void *)&drv->flashDrvName) != OK) {
goto err;
}
if (devResourceGet(pHcf, "flashUnit", HCF_RES_INT, (void *)&drv->flashUnit) != OK) {
drv->flashUnit = 0;
}
if (devResourceGet(pHcf, "flashOffset", HCF_RES_INT, (void *)&drv->flashOffset) != OK) {
drv->flashOffset = 0;
}
if (devResourceGet(pHcf, "flashSize", HCF_RES_INT, (void *)&drv->flashSize) != OK) {
drv->flashSize = 0;
}
if (devResourceGet(pHcf, "pageSize", HCF_RES_INT, (void *)&drv->pageSize) != OK) {
drv->pageSize = 256;
}
if (devResourceGet(pHcf, "mountPoint", HCF_RES_STRING, (void *)&drv->mountPoint) != OK) {
drv->mountPoint = "/lfs0";
}
vxbNextUnitGet(pDev);
return;
err:
#ifndef _VXBUS_BASIC_HWMEMLIB
hwMemFree((char *)drv);
#endif /* _VXBUS_BASIC_HWMEMLIB */
pDev->pDrvCtrl = NULL;
}
LOCAL void lfsDevInstInit2(VXB_DEVICE_ID pDev)
{
lfsDrvCtrl *pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
pDrvCtrl->lock = semBCreate(SEM_Q_FIFO, SEM_FULL);
}
/* {{{ lfs handler */
LOCAL LFS_FILE_DESC *lfsFuncOpen(lfsIosDrv *dev, const char *name, int flags, int mode)
{
int err;
LFS_FILE_DESC *ptr;
if (S_ISDIR(mode)) {
printf("is dir not support\r\n");
return (LFS_FILE_DESC *)(ERROR);
}
printf("mode: %x, flags: %x, name=%p, %c(%s)\r\n", mode, flags, name, name);
ptr = calloc(1, sizeof *ptr);
if (!ptr) {
goto end;
}
ptr->ctrl = dev->pDrvCtrl;
if (name == NULL || name[0] == '\0') {
err = lfs_dir_open(&ptr->ctrl->fsh, &ptr->u.dir, ".");
printf("dir_open: %d\r\n", err);
if (err != LFS_ERR_OK) {
free(ptr);
goto end;
}
printf("open: %p\r\n", ptr);
ptr->isDir = 1;
return ptr;
}
printf("open: %s\r\n", name);
if (lfs_file_open(&ptr->ctrl->fsh, &ptr->u.file, name, LFS_O_RDWR) == LFS_ERR_OK) {
return ptr;
} else {
free(ptr);
}
end:
return (LFS_FILE_DESC *)(ERROR);
}
LOCAL int lfsFuncClose(LFS_FILE_DESC *pfd)
{
lfsDrvCtrl *pDrvCtrl = pfd->ctrl;
printf("close: %p\r\n", pfd);
if (pfd->isDir) {
lfs_dir_close(&pDrvCtrl->fsh, &pfd->u.dir);
} else {
lfs_file_close(&pDrvCtrl->fsh, &pfd->u.file);
}
free(pfd);
return OK;
}
LOCAL int lfsFuncIoctl(LFS_FILE_DESC *pfd, int cmd, _Vx_ioctl_arg_t arg)
{
struct stat *stbuf;
lfsDrvCtrl *pDrvCtrl = pfd->ctrl;
printf("ioctl: %d\r\n", cmd);
switch (cmd) {
case FIOFSTATGET:
stbuf = (struct stat *)(arg);
stbuf->st_dev = (u_long)&pDrvCtrl->lfsDrv.devHdr;
stbuf->st_nlink = 1;
stbuf->st_attrib = 0;
if (pfd->isDir) {
stbuf->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR;
stbuf->st_size = pDrvCtrl->pageSize;
stbuf->st_blocks = 1;
break;
}
}
return (OK);
}
LOCAL ssize_t lfsFuncRead(LFS_FILE_DESC *pfd, char *buffer, size_t maxbytes)
{
lfsDrvCtrl *pDrvCtrl = pfd->ctrl;
printf("read: %p\r\n", pfd);
if (pfd->isDir) {
printf("read dir\r\n");
return 0;
}
return lfs_file_read(&pDrvCtrl->fsh, &pfd->u.file, buffer, maxbytes);
}
LOCAL ssize_t lfsFuncWrite(LFS_FILE_DESC *pfd, char *buffer, size_t nbytes)
{
lfsDrvCtrl *pDrvCtrl = pfd->ctrl;
printf("write: %p\r\n", pfd);
if (pfd->isDir) {
printf("write dir\r\n");
return 0;
}
return lfs_file_write(&pDrvCtrl->fsh, &pfd->u.file, buffer, nbytes);
}
LOCAL STATUS lfsFuncRemove(LFS_FILE_DESC *pfd, char *pPath)
{
lfsDrvCtrl *pDrvCtrl = pfd->ctrl;
printf("remove: %s\r\n", pPath);
lfs_remove(&pDrvCtrl->fsh, pPath);
return OK;
}
/* }}} */
/* {{{ lfs flash operations */
LOCAL int lfsFlashRead(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
UINT32 pos;
lfsDrvCtrl *pDrvCtrl = cfg->context;
UINT8 *buffers[2] = {buffer, NULL};
pos = pDrvCtrl->flashOffset + block * cfg->block_size + off;
printf("%s: block: %lu, off: %lu, size: %lu, pos: 0x%lx\n", __FUNCTION__, block, off, size, pos);
if (pDrvCtrl->mtd->flashOPs.read(pDrvCtrl->mtd, pos, 1, size, buffers, NULL) == OK) {
return LFS_ERR_OK;
}
return LFS_ERR_IO;
}
LOCAL int lfsFlashProg(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer,
lfs_size_t size)
{
STATUS r;
UINT32 pos;
lfsDrvCtrl *pDrvCtrl = cfg->context;
UINT8 *buffers[2] = {(void *)buffer, NULL};
pos = pDrvCtrl->flashOffset + block * cfg->block_size + off;
printf("%s: block: %lu, off: %lu, size: %lu, pos: 0x%lx\n", __FUNCTION__, block, off, size, pos);
r = pDrvCtrl->mtd->flashOPs.write(pDrvCtrl->mtd, pos, 1, size, buffers, NULL);
printf("prog done: %d\r\n", r);
return r == OK ? LFS_ERR_OK : LFS_ERR_IO;
}
LOCAL int lfsFlashErase(const struct lfs_config *cfg, lfs_block_t block)
{
UINT32 pos;
lfsDrvCtrl *pDrvCtrl = cfg->context;
printf("%s: block: %lu\n", __FUNCTION__, block);
pos = pDrvCtrl->flashOffset + block * cfg->block_size;
if (pDrvCtrl->mtd->flashOPs.blkErase(pDrvCtrl->mtd, pos, 1) == OK) {
return LFS_ERR_OK;
}
return LFS_ERR_IO;
}
LOCAL int lfsFlashSync(const struct lfs_config *c)
{
return LFS_ERR_OK;
}
#ifdef LFS_THREADSAFE
LOCAL int lfsFlashLock(const struct lfs_config *cfg)
{
lfsDrvCtrl *pDrvCtrl = cfg->context;
semTake(pDrvCtrl->lock, WAIT_FOREVER);
return LFS_ERR_OK;
}
LOCAL int lfsFlashUnlock(const struct lfs_config *cfg)
{
lfsDrvCtrl *pDrvCtrl = cfg->context;
semGive(pDrvCtrl->lock);
return LFS_ERR_OK;
}
#endif
LOCAL void lfsCfgInit(lfsDrvCtrl *drv, FLASH_CHIP_ID flash)
{
int buffer_size = flash->uPageSize > 0 ? flash->uPageSize : drv->pageSize;
memset(&drv->lfsConfig, 0, sizeof drv->lfsConfig);
drv->lfsConfig.context = drv;
drv->lfsConfig.read = lfsFlashRead;
drv->lfsConfig.prog = lfsFlashProg;
drv->lfsConfig.erase = lfsFlashErase;
drv->lfsConfig.sync = lfsFlashSync;
drv->lfsConfig.read_size = buffer_size;
drv->lfsConfig.prog_size = buffer_size;
drv->lfsConfig.block_size = flash->uEraseSize;
drv->lfsConfig.block_count = drv->blkNum;
drv->lfsConfig.cache_size = buffer_size;
drv->lfsConfig.lookahead_size = flash->uEraseSize / 8;
drv->lfsConfig.block_cycles = 100;
#ifdef LFS_THREADSAFE
drv->lfsConfig.lock = lfsFlashLock;
drv->lfsConfig.unlock = lfsFlashUnlock;
#endif
}
/* }}} */
LOCAL void lfsDevInstConnect(VXB_DEVICE_ID pDev)
{
lfsDrvCtrl *pDrvCtrl;
VXB_DEVICE_ID pFlashDev;
FLASH_CHIP_ID (*flashChipInfoGet)(VXB_DEVICE_ID pDev, UINT32 chipId);
pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
pFlashDev = vxbInstByNameFind((char *)pDrvCtrl->flashDrvName, pDrvCtrl->flashUnit);
if (!pFlashDev) {
return;
}
semTake(pDrvCtrl->lock, WAIT_FOREVER);
flashChipInfoGet = (void *)vxbDevMethodGet(pFlashDev, DEVMETHOD_CALL(vxbFlashChipInfoGet));
pDrvCtrl->mtd = flashChipInfoGet(pFlashDev, 0);
if (pDrvCtrl->mtd) {
if (pDrvCtrl->flashSize <= 0) {
pDrvCtrl->flashSize = pDrvCtrl->mtd->uChipSize - pDrvCtrl->flashOffset;
}
pDrvCtrl->blkNum = pDrvCtrl->flashSize / pDrvCtrl->mtd->uEraseSize;
/* clang-format off */
pDrvCtrl->lfsDrvNum = iosDrvInstall(
(DRV_CREATE_PTR) lfsFuncOpen,
(DRV_REMOVE_PTR) lfsFuncRemove,
(DRV_OPEN_PTR) lfsFuncOpen,
(DRV_CLOSE_PTR) lfsFuncClose,
(DRV_READ_PTR) lfsFuncRead,
(DRV_WRITE_PTR) lfsFuncWrite,
(DRV_IOCTL_PTR) lfsFuncIoctl
);
/* clang-format on */
if (pDrvCtrl->lfsDrvNum > 0) {
if (iosDevAdd(&pDrvCtrl->lfsDrv.devHdr, pDrvCtrl->mountPoint, pDrvCtrl->lfsDrvNum) == OK) {
pDrvCtrl->lfsDrv.pDrvCtrl = pDrvCtrl;
lfsCfgInit(pDrvCtrl, pDrvCtrl->mtd);
} else {
pDrvCtrl->mtd = NULL;
}
} else {
pDrvCtrl->mtd = NULL;
}
}
semGive(pDrvCtrl->lock);
if (pDrvCtrl->mtd)
lfs_mount(&pDrvCtrl->fsh, &pDrvCtrl->lfsConfig);
}
LOCAL struct drvBusFuncs lfsDevFuncs = {
lfsDevInstInit, /* devInstanceInit */
lfsDevInstInit2, /* devInstanceInit2 */
lfsDevInstConnect /* devConnect */
};
/* }}} */
/* {{{ lfsMethods */
LOCAL void lfsDevShow(VXB_DEVICE_ID pDev, int verbose)
{
lfsDrvCtrl *pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
printf(" %s unit %d on %s @ 0x%08x", pDev->pName, pDev->unitNumber, vxbBusTypeString(pDev->busID), pDev);
printf(" with busInfo %p\n", pDev->u.pSubordinateBus);
if (verbose) {
printf(" mountPoint: %s\n", pDrvCtrl->mountPoint);
printf(" flashName: %s\n", pDrvCtrl->flashDrvName);
printf(" mtd: %p\n", pDrvCtrl->mtd);
printf(" read_size: %d\n", pDrvCtrl->lfsConfig.read_size);
printf(" prog_size: %d\n", pDrvCtrl->lfsConfig.prog_size);
printf(" block_size: %d\n", pDrvCtrl->lfsConfig.block_size);
if (pDrvCtrl->mtd) {
printf(" flashChip: %s\n", pDrvCtrl->mtd->chipName);
}
}
}
LOCAL STATUS lfsDevInstUnlink(VXB_DEVICE_ID pDev, void *unused)
{
lfsDrvCtrl *pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
/* Check for vaild parameter */
VXB_ASSERT_NONNULL_V(pDev);
#ifndef _VXBUS_BASIC_HWMEMLIB
hwMemFree((char *)pDrvCtrl);
#endif /* _VXBUS_BASIC_HWMEMLIB */
pDev->pDrvCtrl = NULL;
return OK;
}
/* }}} */
/* Publish the methods for the resources controlled with this file */
/* clang-format off */
LOCAL struct vxbDeviceMethod lfsDevMethods[] = {
DEVMETHOD(busDevShow , lfsDevShow),
DEVMETHOD(vxbDrvUnlink, lfsDevInstUnlink),
{ 0, 0 }
};
LOCAL struct vxbSpiRegister lfsDevDriver = {
{
NULL, /* pNext */
VXB_DEVID_DEVICE, /* devID */
VXB_BUSID_PLB, /* busID */
VXBUS_VERSION_5, /* vxbVersion */
"lfs", /* drvName */
&lfsDevFuncs, /* pDrvBusFuncs */
lfsDevMethods, /* pMethods */
NULL, /* devProbe */
NULL, /* pParamDefaults */
},
};
/* clang-format on */
void lfsDevRegister(void)
{
(void)vxbDevRegister((struct vxbDevRegInfo *)&lfsDevDriver);
}
STATUS lfsFormat(int unit)
{
VXB_DEVICE_ID pDev;
lfsDrvCtrl *pDrvCtrl;
pDev = vxbInstByNameFind("lfs", unit);
if (pDev == NULL) {
printf("Can't find lfs%d\n", unit);
return ERROR;
}
pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
if (!pDrvCtrl->mtd) {
printf("mtd is null\n");
return ERROR;
}
return lfs_format(&pDrvCtrl->fsh, &pDrvCtrl->lfsConfig);
}
STATUS lfsMount(int unit)
{
VXB_DEVICE_ID pDev;
lfsDrvCtrl *pDrvCtrl;
pDev = vxbInstByNameFind("lfs", unit);
if (pDev == NULL) {
printf("Can't find lfs%d\n", unit);
return ERROR;
}
pDrvCtrl = (lfsDrvCtrl *)pDev->pDrvCtrl;
if (!pDrvCtrl->mtd) {
printf("mtd is null\n");
return ERROR;
}
return lfs_mount(&pDrvCtrl->fsh, &pDrvCtrl->lfsConfig);
}

232
vxbSm2130SpiDev.c

@ -1,3 +1,4 @@
/* vim: set ts=4 sw=4 et fdm=marker: */
#include <vxWorks.h> #include <vxWorks.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -7,6 +8,9 @@
#include <taskLib.h> #include <taskLib.h>
#include <sysLib.h> #include <sysLib.h>
#include <tickLib.h> #include <tickLib.h>
#include <iosLib.h>
#include <selectLib.h>
#include <errnoLib.h>
#include <hwif/vxbus/vxBus.h> #include <hwif/vxbus/vxBus.h>
#include <hwif/vxbus/hwConf.h> #include <hwif/vxbus/hwConf.h>
#include <hwif/vxbus/vxbPlbLib.h> #include <hwif/vxbus/vxbPlbLib.h>
@ -17,10 +21,20 @@
#include "vxbSm2130SpiDev.h" #include "vxbSm2130SpiDev.h"
#include <usrLib.h> #include <usrLib.h>
#define SPI_DEV_MUTEX_OPT (SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE)
typedef struct _Sm2310_DEV_HANDLE {
DEV_HDR devHdr;
SEL_WAKEUP_LIST selList;
SM2130_SPI_DEV *pDevCtrl;
} SM2310_DEV_HANDLE;
/* locals */ /* locals */
LOCAL void sm2130SpiDevShow(VXB_DEVICE_ID pDev, int verbose); LOCAL void sm2130SpiDevShow(VXB_DEVICE_ID pDev, int verbose);
LOCAL INT32 spiDevRead(VXB_DEVICE_ID pDev, UINT8 cmd); LOCAL INT32 sm2130BusDevRead(VXB_DEVICE_ID pDev, UINT8 cmd);
LOCAL STATUS spiDevWrite(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var); LOCAL STATUS sm2130BusDevWrite(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var);
/* VxBus methods */ /* VxBus methods */
LOCAL void sm2130SpiDevInstInit(VXB_DEVICE_ID pDev); LOCAL void sm2130SpiDevInstInit(VXB_DEVICE_ID pDev);
@ -37,26 +51,28 @@ LOCAL struct drvBusFuncs sm2130SpiDevFuncs = {
}; };
/* Publish the methods for the resources controlled with this file */ /* Publish the methods for the resources controlled with this file */
/* clang-format off */
LOCAL struct vxbDeviceMethod sm2130SpiDevMethods[] = { LOCAL struct vxbDeviceMethod sm2130SpiDevMethods[] = {
DEVMETHOD(busDevShow, sm2130SpiDevShow), DEVMETHOD(busDevShow , sm2130SpiDevShow),
DEVMETHOD(vxbDrvUnlink, sm2130SpiDevInstUnlink), DEVMETHOD(vxbDrvUnlink, sm2130SpiDevInstUnlink),
{ 0, 0 } { 0, 0 }
}; };
LOCAL struct vxbSpiRegister sm2130SpiDevRegister = { LOCAL struct vxbSpiRegister sm2130SpiDevRegister = {
{ {
NULL, /* pNext */ NULL, /* pNext */
VXB_DEVID_DEVICE, /* devID */ VXB_DEVID_DEVICE, /* devID */
VXB_BUSID_SPI, /* busID = SPI */ VXB_BUSID_SPI, /* busID = SPI */
VXB_VER_4_0_0, /* vxbVersion */ VXB_VER_4_0_0, /* vxbVersion */
SPI_DEV_SM2130, /* drvName */ SPI_DEV_SM2130, /* drvName */
&sm2130SpiDevFuncs, /* pDrvBusFuncs */ &sm2130SpiDevFuncs, /* pDrvBusFuncs */
sm2130SpiDevMethods, /* pMethods */ sm2130SpiDevMethods, /* pMethods */
NULL, //sm2130Probe, /* devProbe */ NULL, /* devProbe */
NULL, /* pParamDefaults */ NULL, /* pParamDefaults */
}, },
}; };
/* clang-format on */
/* /*
* *
* vxbSm2130SpiDevRegister - register with the VxBus subsystem * vxbSm2130SpiDevRegister - register with the VxBus subsystem
@ -85,6 +101,7 @@ void vxbSm2130SpiDevRegister(void)
LOCAL void sm2130SpiDevInstInit(VXB_DEVICE_ID pDev) LOCAL void sm2130SpiDevInstInit(VXB_DEVICE_ID pDev)
{ {
SM2130_SPI_DEV *pDrvCtrl; SM2130_SPI_DEV *pDrvCtrl;
struct hcfDevice *pHcf;
/* Check for vaild parameter */ /* Check for vaild parameter */
VXB_ASSERT_NONNULL_V(pDev); VXB_ASSERT_NONNULL_V(pDev);
@ -97,9 +114,19 @@ LOCAL void sm2130SpiDevInstInit(VXB_DEVICE_ID pDev)
pDrvCtrl->pDev = pDev; pDrvCtrl->pDev = pDev;
pDev->pDrvCtrl = pDrvCtrl; pDev->pDrvCtrl = pDrvCtrl;
pDrvCtrl->read = spiDevRead; pDrvCtrl->drvNode = -1;
pDrvCtrl->write = spiDevWrite; pDrvCtrl->devHandle = NULL;
pDrvCtrl->devName = SM2310_DEV_NAME;
pHcf = (struct hcfDevice *)hcfDeviceGet(pDev);
if (pHcf) {
if (devResourceGet(pHcf, "devName", HCF_RES_STRING, (void *)&pDrvCtrl->devName) != OK) {
pDrvCtrl->devName = SM2310_DEV_NAME;
}
}
pDrvCtrl->read = sm2130BusDevRead;
pDrvCtrl->write = sm2130BusDevWrite;
vxbNextUnitGet(pDev); vxbNextUnitGet(pDev);
} }
@ -125,14 +152,75 @@ LOCAL void sm2130SpiDevInstInit2(VXB_DEVICE_ID pDev)
/* Mutex semaphore is initialized and necessary at this point */ /* Mutex semaphore is initialized and necessary at this point */
pDrvCtrl->muteSem = semMCreate(SPI_DEV_MUTEX_OPT); pDrvCtrl->muteSem = semMCreate(SPI_DEV_MUTEX_OPT);
pFunc = vxbDevMethodGet(vxbDevParent(pDev), pFunc = vxbDevMethodGet(vxbDevParent(pDev), (VXB_METHOD_ID)vxbSpiSpecialGet_desc);
(VXB_METHOD_ID)vxbSpiSpecialGet_desc);
/* Retrieve the SPI master special information */ /* Retrieve the SPI master special information */
if (pFunc != NULL) if (pFunc != NULL)
(*pFunc)(vxbDevParent(pDev), &pDrvCtrl->specialInfo); (*pFunc)(vxbDevParent(pDev), &pDrvCtrl->specialInfo);
} }
/* {{{ device driver api */
LOCAL SM2310_DEV_HANDLE *drv1553bOpen(SM2310_DEV_HANDLE *dev, const char *name, int flags, int mode)
{
SM2130_SPI_DEV *pDrvCtrl = dev->pDevCtrl;
(void)semTake(pDrvCtrl->muteSem, WAIT_FOREVER);
++pDrvCtrl->refcount;
(void)semGive(pDrvCtrl->muteSem);
printf("open: %p\n", dev);
return dev;
}
LOCAL int drv1553bClose(SM2310_DEV_HANDLE *dev)
{
SM2130_SPI_DEV *pDrvCtrl = dev->pDevCtrl;
(void)semTake(pDrvCtrl->muteSem, WAIT_FOREVER);
--pDrvCtrl->refcount;
(void)semGive(pDrvCtrl->muteSem);
printf("close: %p\n", dev);
return (OK);
}
LOCAL int drv1553bIoctl(SM2310_DEV_HANDLE *dev, int cmd, _Vx_ioctl_arg_t arg)
{
SM2130_XFER *xfer;
SM2130_SPI_DEV *pDrvCtrl = dev->pDevCtrl;
STATUS ret = OK;
printf("ioctl: %p\n", dev);
(void)semTake(pDrvCtrl->muteSem, WAIT_FOREVER);
switch (cmd) {
case FIOSELECT:
ret = selNodeAdd(&dev->selList, (SEL_WAKEUP_NODE *)arg);
break;
case FIOUNSELECT:
ret = selNodeDelete(&dev->selList, (SEL_WAKEUP_NODE *)arg);
break;
case FIO_XFER:
xfer = (SM2130_XFER *)(arg);
switch (xfer->type) {
case FIO_XFER_TX:
pDrvCtrl->write(pDrvCtrl->pDev, xfer->reg, xfer->val);
break;
case FIO_XFER_RX:
xfer->val = pDrvCtrl->read(pDrvCtrl->pDev, xfer->reg);
break;
default:
errnoSet(EINVAL);
ret = ERROR;
}
break;
default:
break;
}
(void)semGive(pDrvCtrl->muteSem);
return (ret);
}
/* }}} */
/* /*
* sm2130SpiDevInstConnect - third level initialization routine of spi flash * sm2130SpiDevInstConnect - third level initialization routine of spi flash
* *
@ -146,11 +234,41 @@ LOCAL void sm2130SpiDevInstInit2(VXB_DEVICE_ID pDev)
LOCAL void sm2130SpiDevInstConnect(VXB_DEVICE_ID pDev) LOCAL void sm2130SpiDevInstConnect(VXB_DEVICE_ID pDev)
{ {
SM2130_SPI_DEV *pDrvCtrl; SM2130_SPI_DEV *pDrvCtrl;
SM2310_DEV_HANDLE *dev;
/* Check for vaild parameter */ /* Check for vaild parameter */
VXB_ASSERT_NONNULL_V(pDev); VXB_ASSERT_NONNULL_V(pDev);
pDrvCtrl = (SM2130_SPI_DEV *)pDev->pDrvCtrl; pDrvCtrl = (SM2130_SPI_DEV *)pDev->pDrvCtrl;
(void)semTake(pDrvCtrl->muteSem, WAIT_FOREVER);
if (pDrvCtrl->drvNode < 0) {
/* clang-format off */
pDrvCtrl->drvNode = iosDrvInstall(
(DRV_CREATE_PTR)drv1553bOpen ,
(DRV_REMOVE_PTR)NULL ,
(DRV_OPEN_PTR)drv1553bOpen ,
(DRV_CLOSE_PTR)drv1553bClose ,
(DRV_READ_PTR)NULL ,
(DRV_WRITE_PTR)NULL ,
(DRV_IOCTL_PTR)drv1553bIoctl);
/* clang-format on */
if (pDrvCtrl->drvNode > 0) {
dev = (SM2310_DEV_HANDLE *)hwMemAlloc(sizeof(SM2310_DEV_HANDLE));
if (dev) {
pDrvCtrl->refcount = 0;
selWakeupListInit(&dev->selList);
if (iosDevAdd(&dev->devHdr, pDrvCtrl->devName, pDrvCtrl->drvNode) == OK) {
pDrvCtrl->devHandle = dev;
dev->pDevCtrl = pDrvCtrl;
} else {
#ifndef _VXBUS_BASIC_HWMEMLIB
hwMemFree((char *)dev);
#endif /* _VXBUS_BASIC_HWMEMLIB */
}
}
}
}
(void)semGive(pDrvCtrl->muteSem);
} }
/* /*
@ -198,19 +316,26 @@ LOCAL STATUS sm2130SpiDevInstUnlink(VXB_DEVICE_ID pDev, void *unused)
*/ */
LOCAL void sm2130SpiDevShow(VXB_DEVICE_ID pDev, int verbose) LOCAL void sm2130SpiDevShow(VXB_DEVICE_ID pDev, int verbose)
{ {
printf(" %s unit %d on %s @ 0x%08x", pDev->pName, pDev->unitNumber, SM2130_SPI_DEV *pDrvCtrl = (SM2130_SPI_DEV *)pDev->pDrvCtrl;
vxbBusTypeString(pDev->busID), pDev);
printf(" %s unit %d on %s @ 0x%08x", pDev->pName, pDev->unitNumber, vxbBusTypeString(pDev->busID), pDev);
printf(" with busInfo %p\n", pDev->u.pSubordinateBus); printf(" with busInfo %p\n", pDev->u.pSubordinateBus);
if (verbose) { if (verbose) {
printf(" chipSelect: %d\n", pDrvCtrl->cs);
printf(" mode: %d\n", pDrvCtrl->mode);
printf(" speed: %u\n", pDrvCtrl->freq);
printf(" devName: %s\n", pDrvCtrl->devName);
printf(" devHandle: %p\n", pDrvCtrl->devHandle);
printf(" drvNode: %d\n", pDrvCtrl->drvNode);
printf(" refCount: %d\n", pDrvCtrl->refcount);
} }
return; return;
} }
/* /*
* spiDevWrite - VxBus SPI device write by name support routine * sm2130BusDevWrite - VxBus SPI device write by name support routine
* *
* This routine firstly finds the VXB_DEVICE_ID for a given instance * This routine firstly finds the VXB_DEVICE_ID for a given instance
* identified by name and unit number, then call vxbI2cDevWrite() routine to * identified by name and unit number, then call vxbI2cDevWrite() routine to
@ -220,25 +345,25 @@ LOCAL void sm2130SpiDevShow(VXB_DEVICE_ID pDev, int verbose)
* *
* ERRNO : N/A * ERRNO : N/A
*/ */
LOCAL STATUS spiDevWrite(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var) LOCAL STATUS sm2130BusDevWrite(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var)
{ {
SPI_TRANSFER transInfo = {NULL, NULL, 0, 0, 0}; SPI_TRANSFER transInfo = { NULL, NULL, 0, 0, 0 };
UINT8 buf[3]; UINT8 buf[3];
/* Check if the pDev pointer is valid */
/* Check if the pDev pointer is valid */
VXB_ASSERT(pDev != NULL, ERROR) VXB_ASSERT(pDev != NULL, ERROR)
buf[0] = cmd; buf[0] = cmd;
buf[1] = var & 0xff; buf[1] = var & 0xff;
buf[1] = (var >> 8) & 0xff; buf[1] = (var >> 8) & 0xff;
transInfo.txBuf = buf; transInfo.txBuf = buf;
transInfo.txLen = 3; transInfo.txLen = 3;
return (vxbSpiTransfer(pDev, &transInfo)); return vxbSpiTransfer(pDev, &transInfo);
} }
/* /*
* spiDevRead - read register routine * sm2130BusDevRead - read register routine
* *
* This is the SPI flash status /config register read out routine. * This is the SPI flash status /config register read out routine.
* *
@ -246,57 +371,62 @@ LOCAL STATUS spiDevWrite(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var)
* *
* ERRNO: N/A * ERRNO: N/A
*/ */
LOCAL INT32 spiDevRead(VXB_DEVICE_ID pDev, UINT8 cmd) LOCAL INT32 sm2130BusDevRead(VXB_DEVICE_ID pDev, UINT8 cmd)
{ {
SPI_TRANSFER transInfo = {NULL, NULL, 0, 0, 0}; SPI_TRANSFER transInfo = { NULL, NULL, 0, 0, 0 };
UINT16 buffer = 0xffff; UINT16 buffer;
UINT8 buf[3] = { 0xff, 0xff, 0xff };
/* check if the pDev pointer is valid */ /* check if the pDev pointer is valid */
VXB_ASSERT(pDev != NULL, ERROR) VXB_ASSERT(pDev != NULL, ERROR)
transInfo.txBuf = &cmd; buf[0] = cmd;
transInfo.txBuf = buf;
transInfo.txLen = 1; transInfo.txLen = 1;
transInfo.rxBuf = (UINT8 *)&buffer; transInfo.rxBuf = buf;
transInfo.rxLen = 2; transInfo.rxLen = 3;
if (vxbSpiTransfer(pDev, &transInfo) != OK) if (vxbSpiTransfer(pDev, &transInfo) != OK)
return ERROR; return ERROR;
else
return (buffer);
}
buffer = (buf[2] << 8) | buf[1];
return (buffer);
}
/* {{{ Test Read write */
INT32 sm2130Read(UINT8 cmd) INT32 sm2130Read(UINT8 cmd)
{ {
VXB_DEVICE_ID pDev; VXB_DEVICE_ID pDev;
SM2130_SPI_DEV * pCtrl; SM2130_SPI_DEV *pCtrl;
pDev = vxbInstByNameFind(SPI_DEV_SM2130, 0); pDev = vxbInstByNameFind(SPI_DEV_SM2130, 0);
if (pDev == NULL) { if (pDev == NULL) {
return ERROR; return ERROR;
} }
pCtrl = pDev->pDrvCtrl; pCtrl = pDev->pDrvCtrl;
/* printf("pDev @ %p, pCtrl %p\n", pDev, pCtrl); */ /* printf("pDev @ %p, pCtrl %p\n", pDev, pCtrl); */
return pCtrl->read(pDev, cmd); return pCtrl->read(pDev, cmd);
} }
STATUS sm2130Write(UINT8 cmd, UINT16 var) STATUS sm2130Write(UINT8 cmd, UINT16 var)
{ {
VXB_DEVICE_ID pDev; VXB_DEVICE_ID pDev;
SM2130_SPI_DEV * pCtrl; SM2130_SPI_DEV *pCtrl;
pDev = vxbInstByNameFind(SPI_DEV_SM2130, 0); pDev = vxbInstByNameFind(SPI_DEV_SM2130, 0);
if (pDev == NULL) { if (pDev == NULL) {
return ERROR; return ERROR;
} }
pCtrl = pDev->pDrvCtrl; pCtrl = pDev->pDrvCtrl;
/* printf("pDev @ %p, pCtrl %p\n", pDev, pCtrl); */ /* printf("pDev @ %p, pCtrl %p\n", pDev, pCtrl); */
return pCtrl->write(pDev, cmd, var); return pCtrl->write(pDev, cmd, var);
} }
/* }}} */

22
vxbSm2130SpiDev.h

@ -3,8 +3,19 @@
#define SPI_DEV_SM2130 "SM2130" #define SPI_DEV_SM2130 "SM2130"
#define SPI_DEV_MUTEX_OPT \ #define SM2310_DEV_NAME "/sm2130"
(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE)
#define FIO_XFER_TX (1)
#define FIO_XFER_RX (2)
#define FIO_XFER (200)
struct sm2130_xfer {
UINT8 type;
UINT8 reg;
UINT16 val;
};
typedef struct sm2130_xfer SM2130_XFER;
typedef struct _spi_1553b_drv_ctrl { typedef struct _spi_1553b_drv_ctrl {
VXB_DEVICE_ID pDev; VXB_DEVICE_ID pDev;
@ -12,6 +23,13 @@ typedef struct _spi_1553b_drv_ctrl {
int cs; int cs;
int mode; int mode;
unsigned int freq; unsigned int freq;
const char *devName;
int refcount;
int drvNode;
void *devHandle;
INT32 (*read)(VXB_DEVICE_ID pDev, UINT8 cmd); INT32 (*read)(VXB_DEVICE_ID pDev, UINT8 cmd);
STATUS (*write)(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var); STATUS (*write)(VXB_DEVICE_ID pDev, UINT8 cmd, UINT16 var);

Loading…
Cancel
Save