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: [email protected]
