You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/03/20 17:10:25 UTC
[GitHub] [incubator-nuttx] acassis opened a new pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
acassis opened a new pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126
## Summary
risc-v/esp32-c3: Add support to SPI Flash
## Impact
Only ESP32-C3
## Testing
ESP32-Devikit
Erase the entire flash:
```
$ sudo esptool.py --chip esp32-c3 --port /dev/ttyUSB0 --baud 921600 erase_flash
```
Flash to bootloader, the partition table and the NuttX:
```
$ make download ESPTOOL_PORT=/dev/ttyUSB0 ESPTOOL_BAUD=115200 ESPTOOL_BINDIR=../esp-bins
```
Restart the board and execute:
```
NuttShell (NSH) NuttX-10.0.1
nsh> ls /dev
/dev:
console
null
smart0
ttyS0
zero
nsh> mksmartfs /dev/smart0
nsh> mount -t smartfs /dev/smart0 /mnt
nsh> cd /mnt
nsh> ls
/mnt:
nsh> echo "Say hello to SmartFS" > test.txt
nsh> ls
/mnt:
test.txt
nsh> cat test.txt
Say hello to SmartFS
nsh>
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598767783
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+ esp32c3_spiflash_chip_t *chip = priv->chip;
+ FAR struct mtd_dev_s *mtd_part;
+ uint32_t blocks;
+ uint32_t startblock;
+ uint32_t size;
+
+ ASSERT((ESP32C3_MTD_OFFSET + ESP32C3_MTD_SIZE) <= chip->chip_size);
+ ASSERT((ESP32C3_MTD_OFFSET % chip->sector_size) == 0);
+ ASSERT((ESP32C3_MTD_SIZE % chip->sector_size) == 0);
+
+ finfo("ESP32 SPI Flash information:\n");
+ finfo("\tID = 0x%" PRIx32 "\n", chip->device_id);
+ finfo("\tStatus mask = 0x%" PRIx32 "\n", chip->status_mask);
+ finfo("\tChip size = %" PRId32 " KB\n", chip->chip_size / 1024);
+ finfo("\tPage size = %" PRId32 " B\n", chip->page_size);
+ finfo("\tSector size = %" PRId32 " KB\n", chip->sector_size / 1024);
+ finfo("\tBlock size = %" PRId32 " KB\n", chip->block_size / 1024);
+
+#if ESP32C3_MTD_SIZE == 0
+ size = chip->chip_size - ESP32C3_MTD_OFFSET;
+#else
+ size = ESP32C3_MTD_SIZE;
+#endif
+
+ finfo("\tMTD offset = 0x%x\n", ESP32C3_MTD_OFFSET);
+ finfo("\tMTD size = 0x%" PRIx32 "\n", size);
+
+ startblock = MTD_SIZE2BLK(priv, ESP32C3_MTD_OFFSET);
+ blocks = MTD_SIZE2BLK(priv, size);
+
+ mtd_part = mtd_partition(&priv->mtd, startblock, blocks);
+ if (!mtd_part)
+ {
+ ferr("ERROR: create MTD partition");
Review comment:
```suggestion
ferr("ERROR: Failed to create MTD partition\n");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] Ouss4 merged pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
Ouss4 merged pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r600512854
##########
File path: arch/risc-v/src/esp32c3/rom/esp32c3_spiflash.h
##########
@@ -0,0 +1,826 @@
+/*****************************************************************************
+ * arch/risc-v/src/esp32c3/rom/esp32c3_spiflash.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ *****************************************************************************/
+
+#ifndef _ROM_SPI_FLASH_H_
+#define _ROM_SPI_FLASH_H_
+
+/*****************************************************************************
+ * Included Files
+ *****************************************************************************/
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* spi_flash_apis, spi flash operation related apis */
+
+/*****************************************************************************
+ * Note
+ *****************************************************************************
+ * 1. ESP32C3 chip have 4 SPI slave/master, however, SPI0 is
+ * used as an SPI master to access Flash and ext-SRAM by
+ * Cache module. It will support Decryto read for Flash,
+ * read/write for ext-SRAM. And SPI1 is also used as an
+ * SPI master for Flash read/write and ext-SRAM read/write.
+ * It will support Encrypto write for Flash.
+ * 2. As an SPI master, SPI support Highest clock to 80M,
+ * however, Flash with 80M Clock should be configured
+ * for different Flash chips. If you want to use 80M
+ * clock We should use the SPI that is certified by
+ * Espressif. However, the certification is not started
+ * at the time, so please use 40M clock at the moment.
+ * 3. SPI Flash can use 2 lines or 4 lines mode. If you
+ * use 2 lines mode, you can save two pad SPIHD and
+ * SPIWP for gpio. ESP32C3 support configured SPI pad for
+ * Flash, the configuration is stored in efuse and flash.
+ * However, the configurations of pads should be certified
+ * by Espressif. If you use this function, please use 40M
+ * clock at the moment.
+ * 4. ESP32C3 support to use Common SPI command to configure
+ * Flash to QIO mode, if you failed to configure with fix
+ * command. With Common SPI Command, ESP32C3 can also provide
+ * a way to use same Common SPI command groups on different
+ * Flash chips.
Review comment:
```suggestion
* 1. ESP32-C3 chip have 4 SPI slave/master, however, SPI0 is
* used as an SPI master to access Flash and ext-SRAM by
* Cache module. It will support Decryto read for Flash,
* read/write for ext-SRAM. And SPI1 is also used as an
* SPI master for Flash read/write and ext-SRAM read/write.
* It will support Encrypto write for Flash.
* 2. As an SPI master, SPI support Highest clock to 80M,
* however, Flash with 80M Clock should be configured
* for different Flash chips. If you want to use 80M
* clock We should use the SPI that is certified by
* Espressif. However, the certification is not started
* at the time, so please use 40M clock at the moment.
* 3. SPI Flash can use 2 lines or 4 lines mode. If you
* use 2 lines mode, you can save two pad SPIHD and
* SPIWP for gpio. ESP32-C3 support configured SPI pad for
* Flash, the configuration is stored in efuse and flash.
* However, the configurations of pads should be certified
* by Espressif. If you use this function, please use 40M
* clock at the moment.
* 4. ESP32-C3 support to use Common SPI command to configure
* Flash to QIO mode, if you failed to configure with fix
* command. With Common SPI Command, ESP32C3 can also provide
* a way to use same Common SPI command groups on different
* Flash chips.
```
Only fixing the missing hyphen the ESP32-C3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598749600
##########
File path: arch/risc-v/src/esp32c3/esp32c3_partition.h
##########
@@ -0,0 +1,72 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_partition.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP32C3_PARTITION_H
+#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_PARTITION_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <nuttx/mtd/mtd.h>
+
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_partition_init
+ *
+ * Description:
+ * Initialize ESP32C3 partition. Read partition information of esp-idf,
+ * and create MTD by these data
Review comment:
```suggestion
* Initialize ESP32-C3 partition. Read partition information
* and use these data for creating MTD.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598777219
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
Review comment:
```suggestion
/* ESP32-C3 SPI Flash device private data */
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598760622
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
Review comment:
```suggestion
* nblocks - Number of blocks
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] Ouss4 commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
Ouss4 commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598482459
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.h
##########
@@ -0,0 +1,251 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP32C3_SPIFLASH_H
+#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_SPIFLASH_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <nuttx/mtd/mtd.h>
+
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_mtd(void);
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void);
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_encrypt_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash encryption MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash encryption MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_encrypt_mtd(void);
+
+/****************************************************************************
+ * Name: spi_flash_write_encrypted
+ *
+ * Description:
+ * Write data encrypted to Flash.
+ *
+ * Flash encryption must be enabled for this function to work.
+ *
+ * Flash encryption must be enabled when calling this function.
+ * If flash encryption is disabled, the function returns
+ * ESP_ERR_INVALID_STATE. Use esp_flash_encryption_enabled()
+ * function to determine if flash encryption is enabled.
+ *
+ * Both dest_addr and size must be multiples of 16 bytes. For
+ * absolute best performance, both dest_addr and size arguments should
+ * be multiples of 32 bytes.
+ *
+ * Input Parameters:
+ * dest_addr - Destination address in Flash. Must be a multiple of 16
+ * bytes.
+ * src - Pointer to the source buffer.
+ * size - Length of data, in bytes. Must be a multiple of 16 bytes.
+ *
+ * Returned Values:
+ * Zero (OK) is returned or a negative error.
+ *
+ ****************************************************************************/
+
+int spi_flash_write_encrypted(uint32_t dest_addr, const void *src,
Review comment:
The following group of functions are public and thus have to be prefixed with `esp32c3_`, however I don't see them used anywhere, what do they do?
##########
File path: boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_spiflash.c
##########
@@ -0,0 +1,262 @@
+/****************************************************************************
+ * boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/mount.h>
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/spi/spi.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/fs/nxffs.h>
+
+#include "esp32c3_spiflash.h"
+#include "esp32c3-devkit.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_init
+ *
+ * Description:
+ * Initialize the SPIFLASH and register the MTD device.
+ ****************************************************************************/
+
+int esp32c3_spiflash_init(void)
+{
+ FAR struct mtd_dev_s *mtd;
+ int ret = ERROR;
+
+ mtd = esp32c3_spiflash_alloc_mtdpart();
+
+#if defined (CONFIG_ESP32C3_SPIFLASH_SMARTFS)
+ ret = smart_initialize(0, mtd, NULL);
+ if (ret < 0)
+ {
+ finfo("smart_initialize failed, Trying to erase first...\n");
+ ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0);
+ if (ret < 0)
+ {
+ ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret);
+ return ret;
+ }
+
+ finfo("Erase successful, initializing it again.\n");
+ ret = smart_initialize(0, mtd, NULL);
+ if (ret < 0)
+ {
+ ferr("ERROR: smart_initialize failed: %d\n", ret);
+ return ret;
+ }
+ }
+
+#elif defined (CONFIG_ESP32C3_SPIFLASH_NXFFS)
+ ret = nxffs_initialize(mtd);
+ if (ret < 0)
+ {
+ ferr("ERROR: NXFFS init failed: %d\n", ret);
+ return ret;
+ }
+
+#else
+ ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL);
Review comment:
```suggestion
ret = register_mtddriver("/dev/esp32c3flash", mtd, 0755, NULL);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598764088
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
Review comment:
```suggestion
finfo("(%p, %d, %d)\n", dev, startblock, nblocks);
```
`finfo` already includes the function name on the log line, is it really needed to add `__func__`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598760326
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
Review comment:
```suggestion
* startblock - start block number, it is not equal to SPI Flash's block
* nblocks - Number of blocks
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598776135
##########
File path: arch/risc-v/src/esp32c3/Kconfig
##########
@@ -379,4 +394,42 @@ config ESP32C3_WIFI_FS_MOUNTPT
endmenu # ESP32C3_WIRELESS
+menu "SPI Flash configuration"
+ depends on ESP32C3_SPIFLASH
+
+config ESP32C3_MTD_OFFSET
+ hex "MTD base address in SPI Flash"
+ default 0x180000
+ help
+ MTD base address in SPI Flash.
+
+config ESP32C3_MTD_SIZE
+ hex "MTD size in SPI Flash"
+ default 0x100000
+ help
+ MTD size in SPI Flash.
+
+config ESP32C3_SPIFLASH_DEBUG
+ bool "Debug SPI Flash"
+ default n
+ depends on DEBUG_FS_INFO
+ help
+ Enable this option, read and write of SPI Flash
+ will show input arguments and result.
Review comment:
```suggestion
If this option is enabled, SPI Flash driver read and write functions
will output input parameters and return values (if applicable).
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598776994
##########
File path: arch/risc-v/src/esp32c3/Kconfig
##########
@@ -297,6 +304,14 @@ config ESP32C3_UART1_RXPIN
endif # ESP32C3_UART1
+config ESP32C3_PARTITION
+ bool "ESP32-C3 Partition"
+ default n
+ select ESP32C3_SPIFLASH
+ ---help---
+ Decode esp-idf's partition file and initialize
+ partition by nuttx MTD.
Review comment:
```suggestion
Decode partition file and initialize partition as MTD.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] acassis commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r599875550
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.h
##########
@@ -0,0 +1,251 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP32C3_SPIFLASH_H
+#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_SPIFLASH_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <stdint.h>
+#include <nuttx/mtd/mtd.h>
+
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_mtd(void);
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void);
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_encrypt_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash encryption MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash encryption MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_encrypt_mtd(void);
+
+/****************************************************************************
+ * Name: spi_flash_write_encrypted
+ *
+ * Description:
+ * Write data encrypted to Flash.
+ *
+ * Flash encryption must be enabled for this function to work.
+ *
+ * Flash encryption must be enabled when calling this function.
+ * If flash encryption is disabled, the function returns
+ * ESP_ERR_INVALID_STATE. Use esp_flash_encryption_enabled()
+ * function to determine if flash encryption is enabled.
+ *
+ * Both dest_addr and size must be multiples of 16 bytes. For
+ * absolute best performance, both dest_addr and size arguments should
+ * be multiples of 32 bytes.
+ *
+ * Input Parameters:
+ * dest_addr - Destination address in Flash. Must be a multiple of 16
+ * bytes.
+ * src - Pointer to the source buffer.
+ * size - Length of data, in bytes. Must be a multiple of 16 bytes.
+ *
+ * Returned Values:
+ * Zero (OK) is returned or a negative error.
+ *
+ ****************************************************************************/
+
+int spi_flash_write_encrypted(uint32_t dest_addr, const void *src,
Review comment:
This is a ROM function, it is provided by boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3_rom.ld
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598762010
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
Review comment:
This is a bit confusing. Usually sectors and blocks do not relate to the same thing. What does this function actually receive as input parameters?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598766893
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
Review comment:
```suggestion
* Allocate SPI Flash MTD.
```
I think it is redundant to state that this is related to ESP32-C3.
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
Review comment:
```suggestion
* SPI Flash MTD data pointer if success or NULL if fail
```
I think it is redundant to state that this is related to ESP32-C3.
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
Review comment:
```suggestion
* SPI Flash MTD data pointer if success or NULL if fail.
```
I think it is redundant to state that this is related to ESP32-C3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598768497
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+ esp32c3_spiflash_chip_t *chip = priv->chip;
+ FAR struct mtd_dev_s *mtd_part;
+ uint32_t blocks;
+ uint32_t startblock;
+ uint32_t size;
+
+ ASSERT((ESP32C3_MTD_OFFSET + ESP32C3_MTD_SIZE) <= chip->chip_size);
+ ASSERT((ESP32C3_MTD_OFFSET % chip->sector_size) == 0);
+ ASSERT((ESP32C3_MTD_SIZE % chip->sector_size) == 0);
+
+ finfo("ESP32 SPI Flash information:\n");
+ finfo("\tID = 0x%" PRIx32 "\n", chip->device_id);
+ finfo("\tStatus mask = 0x%" PRIx32 "\n", chip->status_mask);
+ finfo("\tChip size = %" PRId32 " KB\n", chip->chip_size / 1024);
+ finfo("\tPage size = %" PRId32 " B\n", chip->page_size);
+ finfo("\tSector size = %" PRId32 " KB\n", chip->sector_size / 1024);
+ finfo("\tBlock size = %" PRId32 " KB\n", chip->block_size / 1024);
+
+#if ESP32C3_MTD_SIZE == 0
+ size = chip->chip_size - ESP32C3_MTD_OFFSET;
+#else
+ size = ESP32C3_MTD_SIZE;
+#endif
+
+ finfo("\tMTD offset = 0x%x\n", ESP32C3_MTD_OFFSET);
+ finfo("\tMTD size = 0x%" PRIx32 "\n", size);
+
+ startblock = MTD_SIZE2BLK(priv, ESP32C3_MTD_OFFSET);
+ blocks = MTD_SIZE2BLK(priv, size);
+
+ mtd_part = mtd_partition(&priv->mtd, startblock, blocks);
+ if (!mtd_part)
+ {
+ ferr("ERROR: create MTD partition");
+ return NULL;
+ }
+
+ return mtd_part;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_mtd(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+
+ return &priv->mtd;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_encrypt_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash encryption MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash encryption MTD pointer.
Review comment:
```suggestion
* SPI Flash encryption MTD pointer.
```
I think it is redundant to state that this is related to ESP32-C3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598768343
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+ esp32c3_spiflash_chip_t *chip = priv->chip;
+ FAR struct mtd_dev_s *mtd_part;
+ uint32_t blocks;
+ uint32_t startblock;
+ uint32_t size;
+
+ ASSERT((ESP32C3_MTD_OFFSET + ESP32C3_MTD_SIZE) <= chip->chip_size);
+ ASSERT((ESP32C3_MTD_OFFSET % chip->sector_size) == 0);
+ ASSERT((ESP32C3_MTD_SIZE % chip->sector_size) == 0);
+
+ finfo("ESP32 SPI Flash information:\n");
+ finfo("\tID = 0x%" PRIx32 "\n", chip->device_id);
+ finfo("\tStatus mask = 0x%" PRIx32 "\n", chip->status_mask);
+ finfo("\tChip size = %" PRId32 " KB\n", chip->chip_size / 1024);
+ finfo("\tPage size = %" PRId32 " B\n", chip->page_size);
+ finfo("\tSector size = %" PRId32 " KB\n", chip->sector_size / 1024);
+ finfo("\tBlock size = %" PRId32 " KB\n", chip->block_size / 1024);
+
+#if ESP32C3_MTD_SIZE == 0
+ size = chip->chip_size - ESP32C3_MTD_OFFSET;
+#else
+ size = ESP32C3_MTD_SIZE;
+#endif
+
+ finfo("\tMTD offset = 0x%x\n", ESP32C3_MTD_OFFSET);
+ finfo("\tMTD size = 0x%" PRIx32 "\n", size);
+
+ startblock = MTD_SIZE2BLK(priv, ESP32C3_MTD_OFFSET);
+ blocks = MTD_SIZE2BLK(priv, size);
+
+ mtd_part = mtd_partition(&priv->mtd, startblock, blocks);
+ if (!mtd_part)
+ {
+ ferr("ERROR: create MTD partition");
+ return NULL;
+ }
+
+ return mtd_part;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash MTD.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD pointer.
+ *
+ ****************************************************************************/
+
+struct mtd_dev_s *esp32c3_spiflash_mtd(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+
+ return &priv->mtd;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_encrypt_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash encryption MTD.
Review comment:
```suggestion
* Get SPI Flash encryption MTD.
```
I think it is redundant to state that this is related to ESP32-C3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598770891
##########
File path: boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_spiflash.c
##########
@@ -0,0 +1,262 @@
+/****************************************************************************
+ * boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/mount.h>
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/spi/spi.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/fs/nxffs.h>
+
+#include "esp32c3_spiflash.h"
+#include "esp32c3-devkit.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_init
+ *
+ * Description:
+ * Initialize the SPIFLASH and register the MTD device.
+ ****************************************************************************/
+
+int esp32c3_spiflash_init(void)
+{
+ FAR struct mtd_dev_s *mtd;
+ int ret = ERROR;
+
+ mtd = esp32c3_spiflash_alloc_mtdpart();
+
+#if defined (CONFIG_ESP32C3_SPIFLASH_SMARTFS)
+ ret = smart_initialize(0, mtd, NULL);
+ if (ret < 0)
+ {
+ finfo("smart_initialize failed, Trying to erase first...\n");
+ ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0);
+ if (ret < 0)
+ {
+ ferr("ERROR: ioctl(BULKERASE) failed: %d\n", ret);
+ return ret;
+ }
+
+ finfo("Erase successful, initializing it again.\n");
+ ret = smart_initialize(0, mtd, NULL);
+ if (ret < 0)
+ {
+ ferr("ERROR: smart_initialize failed: %d\n", ret);
+ return ret;
+ }
+ }
+
+#elif defined (CONFIG_ESP32C3_SPIFLASH_NXFFS)
+ ret = nxffs_initialize(mtd);
+ if (ret < 0)
+ {
+ ferr("ERROR: NXFFS init failed: %d\n", ret);
+ return ret;
+ }
+
+#else
+ ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL);
+ if (ret < 0)
+ {
+ ferr("ERROR: Register MTD failed: %d\n", ret);
+ return ret;
+ }
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_encrypt_test
+ *
+ * Description:
+ * Test ESP32-C3 SPI Flash driver read/write with encryption.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_ENCRYPTION_TEST
+
+void esp32c3_spiflash_encrypt_test(void)
+{
+ int i;
+ int ret;
+ uint8_t *wbuf;
+ uint8_t *rbuf;
+ struct mtd_geometry_s geo;
+ uint32_t erase_block;
+ uint32_t erase_nblocks;
+ uint32_t rw_block;
+ uint32_t rw_nblocks;
+ struct mtd_dev_s *mtd = esp32c3_spiflash_get_mtd();
+ struct mtd_dev_s *enc_mtd = esp32c3_spiflash_encrypt_get_mtd();
+ const uint32_t address = CONFIG_ESP32C3_SPIFLASH_TEST_ADDRESS;
+ const uint32_t size = 4096;
+
+ ret = MTD_IOCTL(enc_mtd, MTDIOC_GEOMETRY,
+ (unsigned long)(uintptr_t)&geo);
+ if (ret < 0)
+ {
+ ferr("ERROR: Failed to get GEO errno =%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ wbuf = kmm_malloc(size);
+ if (!wbuf)
+ {
+ ferr("ERROR: Failed to alloc %d heap\n", size);
+ DEBUGASSERT(0);
+ }
+
+ rbuf = kmm_malloc(size);
+ if (!rbuf)
+ {
+ ferr("ERROR: Failed to alloc %d heap\n", size);
+ DEBUGASSERT(0);
+ }
+
+ for (i = 0; i < size; i++)
+ {
+ wbuf[i] = (uint8_t)random();
+ }
+
+ erase_block = address / geo.erasesize;
+ erase_nblocks = size / geo.erasesize;
+
+ rw_block = address / geo.blocksize;
+ rw_nblocks = size / geo.blocksize;
+
+ ret = MTD_ERASE(enc_mtd, erase_block, erase_nblocks);
+ if (ret != erase_nblocks)
+ {
+ ferr("ERROR: Failed to erase block errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ ret = MTD_BWRITE(enc_mtd, rw_block, rw_nblocks, wbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to encrypt write errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ memset(rbuf, 0, size);
+ ret = MTD_BREAD(enc_mtd, rw_block, rw_nblocks, rbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to decrypt read errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ if (memcmp(wbuf, rbuf, size))
+ {
+ ferr("ASSERT: Encrypted and decrypted data is not same\n");
+ DEBUGASSERT(0);
+ }
+
+ memset(rbuf, 0, size);
+ ret = MTD_BREAD(mtd, rw_block, rw_nblocks, rbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to read errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ if (!memcmp(wbuf, rbuf, size))
+ {
+ ferr("ASSERT: Encrypted and normal data is same\n");
+ DEBUGASSERT(0);
+ }
+
+ for (i = 0; i < size; i++)
+ {
+ wbuf[i] = (uint8_t)random();
+ }
+
+ ret = MTD_ERASE(enc_mtd, erase_block, erase_nblocks);
+ if (ret != erase_nblocks)
+ {
+ ferr("ERROR: Failed to erase errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ ret = MTD_BWRITE(mtd, rw_block, rw_nblocks, wbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to write errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ memset(rbuf, 0, size);
+ ret = MTD_BREAD(enc_mtd, rw_block, rw_nblocks, rbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to decrypt read errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ if (!memcmp(wbuf, rbuf, size))
+ {
+ ferr("ASSERT: Normal and decrypted data is same\n");
+ DEBUGASSERT(0);
+ }
+
+ memset(rbuf, 0, size);
+ ret = MTD_BREAD(mtd, rw_block, rw_nblocks, rbuf);
+ if (ret != rw_nblocks)
+ {
+ ferr("ERROR: Failed to read errno=%d\n", ret);
+ DEBUGASSERT(0);
+ }
+
+ if (memcmp(wbuf, rbuf, size))
+ {
+ ferr("ASSERT: Normal and normal data is not same\n");
+ DEBUGASSERT(0);
+ }
+
+ kmm_free(wbuf);
+ kmm_free(rbuf);
+
+ finfo("INFO: ESP32 SPI Flash encryption test successfully\n");
Review comment:
```suggestion
finfo("INFO: SPI Flash encryption test success\n");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r598768101
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ *
+ * Returned Value:
+ * Erased blocks if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks)
+{
+ ssize_t ret;
+ uint32_t offset = startblock * SPI_FLASH_ERASE_SIZE;
+ uint32_t nbytes = nblocks * SPI_FLASH_ERASE_SIZE;
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, %d, %d)\n", __func__, dev, startblock, nblocks);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("spi_flash_erase_range(%p, 0x%x, %d)\n", dev, offset, nbytes);
+#endif
+
+ ret = spi_flash_erase_range(offset, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+ else
+ {
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("Failed to erase the flash range!\n");
+#endif
+ ret = -1;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read
+ *
+ * Description:
+ * Read data from SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_read(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread
+ *
+ * Description:
+ * Read data from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_read_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from SPI Flash
+ * at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read data bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bread_decrypt
+ *
+ * Description:
+ * Read encrypted data and decrypt automatically from designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start block number, it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Read block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock, nblocks,
+ buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_read_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_write
+ *
+ * Description:
+ * write data to SPI Flash at designated address.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * offset - target address offset
+ * nbytes - data number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen bytes if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer)
+{
+ int ret;
+
+ ASSERT(buffer);
+
+ if ((offset > SPI_FLASH_SIZE) || ((offset + nbytes) > SPI_FLASH_SIZE))
+ {
+ return -EINVAL;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, offset, nbytes, buffer);
+#endif
+
+ /* Acquire the semaphore. */
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write(offset, buffer, nbytes);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nbytes;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return (ssize_t)ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite
+ *
+ * Description:
+ * Write data to designated blocks.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ret = spi_flash_write(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_bwrite_encrypt
+ *
+ * Description:
+ * Write data to designated blocks by SPI Flash hardware encryption.
+ *
+ * Input Parameters:
+ * dev - MTD device data
+ * startblock - start MTD block number,
+ * it is not equal to SPI Flash's block
+ * nblocks - blocks number
+ * buffer - data buffer pointer
+ *
+ * Returned Value:
+ * Writen block number if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer)
+{
+ ssize_t ret;
+ uint32_t addr = startblock * SPI_FLASH_BLK_SIZE;
+ uint32_t size = nblocks * SPI_FLASH_BLK_SIZE;
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s(%p, 0x%x, %d, %p)\n", __func__, dev, startblock,
+ nblocks, buffer);
+#endif
+
+ ret = nxsem_wait(&g_exclsem);
+ if (ret < 0)
+ {
+ goto error_with_buffer;
+ }
+
+ ret = spi_flash_write_encrypted(addr, buffer, size);
+
+ nxsem_post(&g_exclsem);
+
+ if (ret == OK)
+ {
+ ret = nblocks;
+ }
+
+#ifdef CONFIG_ESP32C3_SPIFLASH_DEBUG
+ finfo("%s()=%d\n", __func__, ret);
+#endif
+
+error_with_buffer:
+
+ return ret;
+}
+
+/****************************************************************************
+ * Name: esp32c3_ioctl
+ *
+ * Description:
+ * Set/Get option to/from ESP32-C3 SPI Flash MTD device data.
+ *
+ * Input Parameters:
+ * dev - ESP32-C3 MTD device data
+ * cmd - operation command
+ * arg - operation argument
+ *
+ * Returned Value:
+ * 0 if success or a negative value if fail.
+ *
+ ****************************************************************************/
+
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg)
+{
+ int ret = OK;
+ struct mtd_geometry_s *geo;
+
+ finfo("cmd: %d \n", cmd);
+
+ switch (cmd)
+ {
+ case MTDIOC_GEOMETRY:
+ {
+ geo = (struct mtd_geometry_s *)arg;
+ if (geo)
+ {
+ geo->blocksize = SPI_FLASH_BLK_SIZE;
+ geo->erasesize = SPI_FLASH_ERASE_SIZE;
+ geo->neraseblocks = SPI_FLASH_SIZE / SPI_FLASH_ERASE_SIZE;
+ ret = OK;
+
+ finfo("blocksize: %" PRId32 " erasesize: %" PRId32 \
+ " neraseblocks: %" PRId32 "\n",
+ geo->blocksize, geo->erasesize, geo->neraseblocks);
+ }
+ }
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
+ }
+
+ finfo("return %d\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_alloc_mtdpart
+ *
+ * Description:
+ * Alloc ESP32-C3 SPI Flash MTD
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * ESP32-C3 SPI Flash MTD data pointer if success or NULL if fail
+ *
+ ****************************************************************************/
+
+FAR struct mtd_dev_s *esp32c3_spiflash_alloc_mtdpart(void)
+{
+ struct esp32c3_spiflash_s *priv = &g_esp32c3_spiflash;
+ esp32c3_spiflash_chip_t *chip = priv->chip;
+ FAR struct mtd_dev_s *mtd_part;
+ uint32_t blocks;
+ uint32_t startblock;
+ uint32_t size;
+
+ ASSERT((ESP32C3_MTD_OFFSET + ESP32C3_MTD_SIZE) <= chip->chip_size);
+ ASSERT((ESP32C3_MTD_OFFSET % chip->sector_size) == 0);
+ ASSERT((ESP32C3_MTD_SIZE % chip->sector_size) == 0);
+
+ finfo("ESP32 SPI Flash information:\n");
+ finfo("\tID = 0x%" PRIx32 "\n", chip->device_id);
+ finfo("\tStatus mask = 0x%" PRIx32 "\n", chip->status_mask);
+ finfo("\tChip size = %" PRId32 " KB\n", chip->chip_size / 1024);
+ finfo("\tPage size = %" PRId32 " B\n", chip->page_size);
+ finfo("\tSector size = %" PRId32 " KB\n", chip->sector_size / 1024);
+ finfo("\tBlock size = %" PRId32 " KB\n", chip->block_size / 1024);
+
+#if ESP32C3_MTD_SIZE == 0
+ size = chip->chip_size - ESP32C3_MTD_OFFSET;
+#else
+ size = ESP32C3_MTD_SIZE;
+#endif
+
+ finfo("\tMTD offset = 0x%x\n", ESP32C3_MTD_OFFSET);
+ finfo("\tMTD size = 0x%" PRIx32 "\n", size);
+
+ startblock = MTD_SIZE2BLK(priv, ESP32C3_MTD_OFFSET);
+ blocks = MTD_SIZE2BLK(priv, size);
+
+ mtd_part = mtd_partition(&priv->mtd, startblock, blocks);
+ if (!mtd_part)
+ {
+ ferr("ERROR: create MTD partition");
+ return NULL;
+ }
+
+ return mtd_part;
+}
+
+/****************************************************************************
+ * Name: esp32c3_spiflash_mtd
+ *
+ * Description:
+ * Get ESP32-C3 SPI Flash MTD.
Review comment:
```suggestion
* Get SPI Flash MTD.
```
I think it is redundant to state that this is related to ESP32-C3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] acassis commented on a change in pull request #3126: risc-v/esp32-c3: Add support to SPI Flash
Posted by GitBox <gi...@apache.org>.
acassis commented on a change in pull request #3126:
URL: https://github.com/apache/incubator-nuttx/pull/3126#discussion_r599884715
##########
File path: arch/risc-v/src/esp32c3/esp32c3_spiflash.c
##########
@@ -0,0 +1,745 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_spiflash.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/init.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/mtd/mtd.h>
+
+#include "esp32c3_spiflash.h"
+#include "rom/esp32c3_spiflash.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SPI_FLASH_BLK_SIZE 256
+#define SPI_FLASH_ERASE_SIZE 4096
+#define SPI_FLASH_SIZE (4 * 1024 * 1024)
+
+#define ESP32C3_MTD_OFFSET CONFIG_ESP32C3_MTD_OFFSET
+#define ESP32C3_MTD_SIZE CONFIG_ESP32C3_MTD_SIZE
+
+#define MTD2PRIV(_dev) ((FAR struct esp32c3_spiflash_s *)_dev)
+#define MTD_SIZE(_priv) ((_priv)->chip->chip_size)
+#define MTD_BLKSIZE(_priv) ((_priv)->chip->page_size)
+#define MTD_ERASESIZE(_priv) ((_priv)->chip->sector_size)
+#define MTD_BLK2SIZE(_priv, _b) (MTD_BLKSIZE(_priv) * (_b))
+#define MTD_SIZE2BLK(_priv, _s) ((_s) / MTD_BLKSIZE(_priv))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* ESP32C3 SPI Flash device private data */
+
+struct esp32c3_spiflash_s
+{
+ struct mtd_dev_s mtd;
+
+ /* SPI Flash data */
+
+ esp32c3_spiflash_chip_t *chip;
+};
+
+/****************************************************************************
+ * Private Functions Prototypes
+ ****************************************************************************/
+
+/* MTD driver methods */
+
+static int esp32c3_erase(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks);
+static ssize_t esp32c3_read(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, uint8_t *buffer);
+static ssize_t esp32c3_read_decrypt(struct mtd_dev_s *dev,
+ off_t offset,
+ size_t nbytes,
+ uint8_t *buffer);
+static ssize_t esp32c3_bread(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, uint8_t *buffer);
+static ssize_t esp32c3_bread_decrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ uint8_t *buffer);
+static ssize_t esp32c3_write(struct mtd_dev_s *dev, off_t offset,
+ size_t nbytes, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite(struct mtd_dev_s *dev, off_t startblock,
+ size_t nblocks, const uint8_t *buffer);
+static ssize_t esp32c3_bwrite_encrypt(struct mtd_dev_s *dev,
+ off_t startblock,
+ size_t nblocks,
+ const uint8_t *buffer);
+static int esp32c3_ioctl(struct mtd_dev_s *dev, int cmd,
+ unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread,
+ .bwrite = esp32c3_bwrite,
+ .read = esp32c3_read,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = esp32c3_write,
+#endif
+ .name = "esp32c3_spiflash"
+ },
+ .chip = &g_rom_flashchip,
+};
+
+static struct esp32c3_spiflash_s g_esp32c3_spiflash_encrypt =
+{
+ .mtd =
+ {
+ .erase = esp32c3_erase,
+ .bread = esp32c3_bread_decrypt,
+ .bwrite = esp32c3_bwrite_encrypt,
+ .read = esp32c3_read_decrypt,
+ .ioctl = esp32c3_ioctl,
+#ifdef CONFIG_MTD_BYTE_WRITE
+ .write = NULL,
+#endif
+ .name = "esp32c3_spiflash_encrypt"
+ }
+};
+
+/* Ensure exclusive access to the driver */
+
+static sem_t g_exclsem = SEM_INITIALIZER(1);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_erase
+ *
+ * Description:
+ * Erase SPI Flash designated sectors.
Review comment:
Sectors, Blocks and Offsets are mixed here: basically a block refer to the index of a sector I want to erase and nblocks the amount of sectors I want to erase. The offset is the physical position of the sector, just multiplying startblock * ERASE_SIZE I got the offset.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org