Browse Source

add spi flash test function for arm

pull/3/head
Liu_Weichao 1 year ago
parent
commit
f48da2b6f9
  1. 8
      applications/app_test/Kconfig
  2. 8
      applications/app_test/Makefile
  3. 175
      applications/app_test/test_spi_flash.c
  4. 8
      board/aiit-arm32-board/third_party_driver/i2c/connect_i2c.c
  5. 2
      board/aiit-arm32-board/third_party_driver/spi/connect_flash_spi.c
  6. 8
      board/aiit-arm32-board/third_party_driver/spi/connect_spi.c
  7. 8
      board/aiit-riscv64-board/third_party_driver/i2c/connect_i2c.c
  8. 8
      board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c
  9. 8
      board/k210-emulator/third_party_driver/i2c/connect_i2c.c
  10. 8
      board/k210-emulator/third_party_driver/spi/connect_spi.c
  11. 8
      board/kd233/third_party_driver/i2c/connect_i2c.c
  12. 8
      board/kd233/third_party_driver/spi/connect_spi.c
  13. 8
      board/stm32f407-st-discovery/third_party_driver/i2c/connect_i2c.c
  14. 2
      board/stm32f407-st-discovery/third_party_driver/spi/connect_flash_spi.c
  15. 8
      board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c
  16. 4
      resources/i2c/dev_i2c.c
  17. 8
      resources/include/dev_i2c.h
  18. 8
      resources/include/dev_spi.h
  19. 6
      resources/spi/dev_spi.c
  20. 32
      resources/spi/flash_spi.c

8
applications/app_test/Kconfig

@ -7,4 +7,12 @@ menuconfig USER_TEST
bool "Config test lora adhoc"
default n
config USER_TEST_SPI_LORA
bool "Config test spi lora"
default n
config USER_TEST_SPI_FLASH
bool "Config test spi flash"
default n
endif

8
applications/app_test/Makefile

@ -4,4 +4,12 @@ ifeq ($(CONFIG_USER_TEST_LORA_ADHOC),y)
SRC_FILES += test_adhoc_lora.c
endif
ifeq ($(CONFIG_USER_TEST_SPI_LORA),y)
SRC_FILES += test_spi_lora.c
endif
ifeq ($(CONFIG_USER_TEST_SPI_FLASH),y)
SRC_FILES += test_spi_flash.c
endif
include $(KERNEL_ROOT)/compiler.mk

175
applications/app_test/test_spi_flash.c

@ -0,0 +1,175 @@
/*
* File : spi_flash_sfud.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2016-09-28 armink first version.
*/
/**
* @file test_spi_flash.c
* @brief support to test spi flash function
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021-05-17
*/
/*************************************************
File name: test_spi_flash.c
Description: support spi flash function test
Others: add spi flash test cmd from SFUD/blob/master/demo/stm32f2xx_rtt/RT-Thread-2.1.0/components/drivers/spi/spi_flash_sfud.c
https://github.com/armink/SFUD/
History:
1. Date: 2021-05-17
Author: AIIT XUOS Lab
Modification:
1. support spi flash open, read and write function
*************************************************/
#include <xiuos.h>
#include <device.h>
#include <flash_spi.h>
#include <user_api.h>
#define SPI_FLASH_PATH "/dev/spi1_W25Q64"
#define FlashDataPrint(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
static int spi_flash_fd;
void FlashOpen(void)
{
x_err_t ret = EOK;
spi_flash_fd = open(SPI_FLASH_PATH, O_RDWR);
if (spi_flash_fd < 0) {
KPrintf("open spi flash fd error %d\n", spi_flash_fd);
}
KPrintf("Spi Flash init succeed\n");
return;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
FlashOpen, FlashOpen, open spi flash device);
void FlashRead(int argc, char *argv[])
{
x_size_t i, j = 0;
uint32 addr;
uint32 size;
uint8 data[16];
struct BusBlockReadParam read_param;
memset(&read_param, 0, sizeof(struct BusBlockReadParam));
memset(data, 0, 16);
if (3 != argc) {
KPrintf("FlashRead cmd format: FlashRead addr size.\n");
return;
} else {
addr = strtol(argv[1], NULL, 0);
size = strtol(argv[2], NULL, 0);
read_param.buffer = data;
read_param.pos = addr;
read_param.size = size;
if (read_param.buffer) {
read(spi_flash_fd, &read_param, size);
if (size == read_param.read_length) {
KPrintf("Read the %s flash data success. Start from 0x%08X, size is %ld. The data is:\n",
SPI_FLASH_PATH, addr, size);
KPrintf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
for (i = 0; i < size; i += 16) {
KPrintf("[%08X] ", addr + i);
/* dump hex */
for (j = 0; j < 16; j++) {
if (i + j < size) {
KPrintf("%02X ", data[i + j]);
} else {
KPrintf(" ");
}
}
/* dump char for hex */
for (j = 0; j < 16; j++) {
if (i + j < size) {
KPrintf("%c", FlashDataPrint(data[i + j]) ? data[i + j] : '.');
}
}
KPrintf("\n");
}
KPrintf("\n");
}
} else {
KPrintf("SpiFlashRead alloc read buffer failed!\n");
}
}
return;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
FlashRead, FlashRead, read data from spi flash device);
void FlashWrite(int argc, char *argv[])
{
x_err_t ret = EOK;
x_size_t i, j = 0;
uint32 addr;
uint32 size;
uint8 data[16];
struct BusBlockWriteParam write_param;
memset(&write_param, 0, sizeof(struct BusBlockWriteParam));
memset(data, 0, 16);
if (argc < 3) {
KPrintf("FlashWrite cmd format: FlashWrite addr data.\n");
return;
} else {
addr = strtol(argv[1], NULL, 0);
size = argc - 2;
write_param.buffer = data;
write_param.pos = addr;
write_param.size = size;
if (data) {
for (i = 0; i < size; i++) {
data[i] = strtol(argv[2 + i], NULL, 0);
}
ret = write(spi_flash_fd, &write_param, size);
if (EOK == ret) {
KPrintf("Write the %s flash data success. Start from 0x%08X, size is %ld.\n",
SPI_FLASH_PATH, addr, size);
KPrintf("Write data: ");
for (i = 0; i < size; i++) {
KPrintf("%d ", data[i]);
}
KPrintf(".\n");
}
} else {
KPrintf("SpiFlashWrite alloc write buffer failed!\n");
}
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
FlashWrite, FlashWrite, write data to spi flash device);

8
board/aiit-arm32-board/third_party_driver/i2c/connect_i2c.c

@ -607,10 +607,10 @@ out:
/*manage the i2c device operations*/
static const struct I2cDevDone i2c_dev_done =
{
.open = NONE,
.close = NONE,
.write = I2cWriteData,
.read = I2cReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = I2cWriteData,
.dev_read = I2cReadData,
};
/*Init i2c bus*/

2
board/aiit-arm32-board/third_party_driver/spi/connect_flash_spi.c

@ -46,7 +46,7 @@ int FlashW25qxxSpiDeviceInit(void)
return ERROR;
}
if (NONE == SpiFlashInit(SPI_BUS_NAME_1, "spi1_dev0", SPI_1_DRV_NAME, "W25Q64")) {
if (NONE == SpiFlashInit(SPI_BUS_NAME_1, "spi1_dev0", SPI_1_DRV_NAME, "spi1_W25Q64")) {
return ERROR;
}

8
board/aiit-arm32-board/third_party_driver/spi/connect_spi.c

@ -1347,10 +1347,10 @@ static uint32 Stm32SpiDrvConfigure(void *drv, struct BusConfigureInfo *configure
/*manage the spi device operations*/
static const struct SpiDevDone spi_dev_done =
{
.open = NONE,
.close = NONE,
.write = Stm32SpiWriteData,
.read = Stm32SpiReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = Stm32SpiWriteData,
.dev_read = Stm32SpiReadData,
};
#if defined(BSP_USING_SPI1)

8
board/aiit-riscv64-board/third_party_driver/i2c/connect_i2c.c

@ -515,10 +515,10 @@ out:
/*manage the i2c device operations*/
static const struct I2cDevDone i2c_dev_done =
{
.open = NONE,
.close = NONE,
.write = I2cWriteData,
.read = I2cReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = I2cWriteData,
.dev_read = I2cReadData,
};
/*Init i2c bus*/

8
board/aiit-riscv64-board/third_party_driver/spi/connect_spi.c

@ -291,10 +291,10 @@ static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStand
/*manage the spi device operations*/
static const struct SpiDevDone spi_dev_done =
{
.open = NONE,
.close = NONE,
.write = SpiWriteData,
.read = SpiReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = SpiWriteData,
.dev_read = SpiReadData,
};
static int BoardSpiBusInit(struct SpiBus *spi_bus, struct SpiDriver *spi_driver)

8
board/k210-emulator/third_party_driver/i2c/connect_i2c.c

@ -503,10 +503,10 @@ out:
/*manage the i2c device operations*/
static const struct I2cDevDone i2c_dev_done =
{
.open = NONE,
.close = NONE,
.write = I2cWriteData,
.read = I2cReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = I2cWriteData,
.dev_read = I2cReadData,
};
/*Init i2c bus*/

8
board/k210-emulator/third_party_driver/spi/connect_spi.c

@ -290,10 +290,10 @@ static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStand
/*manage the spi device operations*/
static const struct SpiDevDone spi_dev_done =
{
.open = NONE,
.close = NONE,
.write = SpiWriteData,
.read = SpiReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = SpiWriteData,
.dev_read = SpiReadData,
};
static int BoardSpiBusInit(struct SpiBus *spi_bus, struct SpiDriver *spi_driver)

8
board/kd233/third_party_driver/i2c/connect_i2c.c

@ -503,10 +503,10 @@ out:
/*manage the i2c device operations*/
static const struct I2cDevDone i2c_dev_done =
{
.open = NONE,
.close = NONE,
.write = I2cWriteData,
.read = I2cReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = I2cWriteData,
.dev_read = I2cReadData,
};
/*Init i2c bus*/

8
board/kd233/third_party_driver/spi/connect_spi.c

@ -290,10 +290,10 @@ static uint32 SpiReadData(struct SpiHardwareDevice *spi_dev, struct SpiDataStand
/*manage the spi device operations*/
static const struct SpiDevDone spi_dev_done =
{
.open = NONE,
.close = NONE,
.write = SpiWriteData,
.read = SpiReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = SpiWriteData,
.dev_read = SpiReadData,
};
static int BoardSpiBusInit(struct SpiBus *spi_bus, struct SpiDriver *spi_driver)

8
board/stm32f407-st-discovery/third_party_driver/i2c/connect_i2c.c

@ -603,10 +603,10 @@ out:
/*manage the i2c device operations*/
static const struct I2cDevDone i2c_dev_done =
{
.open = NONE,
.close = NONE,
.write = I2cWriteData,
.read = I2cReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = I2cWriteData,
.dev_read = I2cReadData,
};
/*Init i2c bus*/

2
board/stm32f407-st-discovery/third_party_driver/spi/connect_flash_spi.c

@ -46,7 +46,7 @@ int FlashW25qxxSpiDeviceInit(void)
return ERROR;
}
if (NONE == SpiFlashInit(SPI_BUS_NAME_1, "spi1_dev0", SPI_1_DRV_NAME, "W25Q64")) {
if (NONE == SpiFlashInit(SPI_BUS_NAME_1, "spi1_dev0", SPI_1_DRV_NAME, "spi1_W25Q64")) {
return ERROR;
}

8
board/stm32f407-st-discovery/third_party_driver/spi/connect_spi.c

@ -1251,10 +1251,10 @@ static uint32 Stm32SpiDrvConfigure(void *drv, struct BusConfigureInfo *configure
/*manage the spi device operations*/
static const struct SpiDevDone spi_dev_done =
{
.open = NONE,
.close = NONE,
.write = Stm32SpiWriteData,
.read = Stm32SpiReadData,
.dev_open = NONE,
.dev_close = NONE,
.dev_write = Stm32SpiWriteData,
.dev_read = Stm32SpiReadData,
};
#if defined(BSP_USING_SPI1)

4
resources/i2c/dev_i2c.c

@ -44,7 +44,7 @@ static uint32 I2cDeviceWrite(void *dev, struct BusBlockWriteParam *write_param)
i2c_msg.retries = 1;
i2c_msg.next = NONE;
return i2c_dev->i2c_dev_done->write(i2c_dev, &i2c_msg);
return i2c_dev->i2c_dev_done->dev_write(i2c_dev, &i2c_msg);
}
static uint32 I2cDeviceRead(void *dev, struct BusBlockReadParam *read_param)
@ -62,7 +62,7 @@ static uint32 I2cDeviceRead(void *dev, struct BusBlockReadParam *read_param)
i2c_msg.retries = 1;
i2c_msg.next = NONE;
return i2c_dev->i2c_dev_done->read(i2c_dev, &i2c_msg);
return i2c_dev->i2c_dev_done->dev_read(i2c_dev, &i2c_msg);
}
static const struct HalDevDone dev_done =

8
resources/include/dev_i2c.h

@ -50,10 +50,10 @@ struct I2cHardwareDevice;
struct I2cDevDone
{
uint32 (*open) (struct I2cHardwareDevice *i2c_device);
uint32 (*close) (struct I2cHardwareDevice *i2c_device);
uint32 (*write) (struct I2cHardwareDevice *i2c_device, struct I2cDataStandard *msg);
uint32 (*read) (struct I2cHardwareDevice *i2c_device, struct I2cDataStandard *msg);
uint32 (*dev_open) (struct I2cHardwareDevice *i2c_device);
uint32 (*dev_close) (struct I2cHardwareDevice *i2c_device);
uint32 (*dev_write) (struct I2cHardwareDevice *i2c_device, struct I2cDataStandard *msg);
uint32 (*dev_read) (struct I2cHardwareDevice *i2c_device, struct I2cDataStandard *msg);
};
struct I2cHardwareDevice

8
resources/include/dev_spi.h

@ -102,10 +102,10 @@ struct SpiHardwareDevice;
struct SpiDevDone
{
uint32 (*open) (struct SpiHardwareDevice *dev);
uint32 (*close) (struct SpiHardwareDevice *dev);
uint32 (*write) (struct SpiHardwareDevice *dev, struct SpiDataStandard *msg);
uint32 (*read) (struct SpiHardwareDevice *dev, struct SpiDataStandard *msg);
uint32 (*dev_open) (struct SpiHardwareDevice *dev);
uint32 (*dev_close) (struct SpiHardwareDevice *dev);
uint32 (*dev_write) (struct SpiHardwareDevice *dev, struct SpiDataStandard *msg);
uint32 (*dev_read) (struct SpiHardwareDevice *dev, struct SpiDataStandard *msg);
};
struct SpiHardwareDevice

6
resources/spi/dev_spi.c

@ -62,7 +62,7 @@ static uint32 SpiDeviceWrite(void *dev, struct BusBlockWriteParam *write_param)
spi_msg.spi_cs_release = 0;
spi_msg.next = NONE;
return spi_dev->spi_dev_done->write(spi_dev, &spi_msg);
return spi_dev->spi_dev_done->dev_write(spi_dev, &spi_msg);
}
static uint32 SpiDeviceRead(void *dev, struct BusBlockReadParam *read_param)
@ -80,7 +80,7 @@ static uint32 SpiDeviceRead(void *dev, struct BusBlockReadParam *read_param)
spi_msg.spi_cs_release = 0;
spi_msg.next = NONE;
return spi_dev->spi_dev_done->read(spi_dev, &spi_msg);
return spi_dev->spi_dev_done->dev_read(spi_dev, &spi_msg);
}
static const struct HalDevDone dev_done =
@ -190,5 +190,5 @@ int SpiDevConfigureCs(struct HardwareDev *dev, uint8 spi_chip_select, uint8 spi_
msg.spi_chip_select = spi_chip_select;
msg.spi_cs_release = spi_cs_release;
return spi_dev->spi_dev_done->write(spi_dev, &msg);
return spi_dev->spi_dev_done->dev_write(spi_dev, &msg);
}

32
resources/spi/flash_spi.c

@ -18,6 +18,8 @@
* @date 2021-04-24
*/
#include <bus_spi.h>
#include <dev_spi.h>
#include <flash_spi.h>
/**
@ -43,13 +45,20 @@ static uint32 SpiFlashWrite(void *dev, struct BusBlockWriteParam *write_param)
HardwareDevType haldev = (struct HardwareDev *)dev;
struct SpiHardwareDevice *flash_dev;
struct BusBlockWriteParam *flash_write_param = (struct BusBlockWriteParam *)write_param->buffer;
flash_dev = CONTAINER_OF(haldev, struct SpiHardwareDevice, haldev);
spi_flash_dev = CONTAINER_OF(flash_dev, struct SpiFlashDevice, flash_dev);
sfud_flash_dev = (sfud_flash *)spi_flash_dev->flash_param.flash_private_data;
pos = write_param->pos * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
size = write_param->size * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
write_buffer = (uint8 *)write_param->buffer;
pos = flash_write_param->pos;// * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
size = flash_write_param->size;// * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
write_buffer = (uint8 *)flash_write_param->buffer;
KPrintf("flash write pos %u sector_bytes %u size %u\n",
flash_write_param->pos,
spi_flash_dev->flash_param.flash_block_param.sector_bytes,
flash_write_param->size);
ret = sfud_erase_write(sfud_flash_dev, pos, size, write_buffer);
if (SFUD_SUCCESS != ret) {
@ -57,6 +66,8 @@ static uint32 SpiFlashWrite(void *dev, struct BusBlockWriteParam *write_param)
return ERROR;
}
haldev->owner_bus->owner_haldev = haldev;
return ret;
}
@ -82,13 +93,15 @@ static uint32 SpiFlashRead(void *dev, struct BusBlockReadParam *read_param)
HardwareDevType haldev = (struct HardwareDev *)dev;
struct SpiHardwareDevice *flash_dev;
struct BusBlockReadParam *flash_read_param = (struct BusBlockReadParam *)read_param->buffer;
flash_dev = CONTAINER_OF(haldev, struct SpiHardwareDevice, haldev);
spi_flash_dev = CONTAINER_OF(flash_dev, struct SpiFlashDevice, flash_dev);
sfud_flash_dev = (sfud_flash *)spi_flash_dev->flash_param.flash_private_data;
pos = read_param->pos * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
size = read_param->size * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
read_buffer = (uint8 *)read_param->buffer;
pos = flash_read_param->pos;// * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
size = flash_read_param->size;// * spi_flash_dev->flash_param.flash_block_param.sector_bytes;
read_buffer = (uint8 *)flash_read_param->buffer;
ret = sfud_read(sfud_flash_dev, pos, size, read_buffer);
if (SFUD_SUCCESS != ret) {
@ -96,6 +109,13 @@ static uint32 SpiFlashRead(void *dev, struct BusBlockReadParam *read_param)
return ERROR;
}
flash_read_param->read_length = flash_read_param->size;
read_param->read_length = flash_read_param->size;
haldev->owner_bus->owner_haldev = haldev;
KPrintf("SpiFlashRead read buffer done\n");
return ret;
}

Loading…
Cancel
Save