2018-02-13 11:21:46 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2018 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2018-03-09 11:22:29 +00:00
|
|
|
#include "py/mperrno.h"
|
2018-03-27 10:38:57 +01:00
|
|
|
#include "irq.h"
|
2018-03-02 12:11:47 +00:00
|
|
|
#include "led.h"
|
2018-02-13 11:21:46 +00:00
|
|
|
#include "storage.h"
|
|
|
|
|
2018-05-28 01:57:27 +01:00
|
|
|
#if MICROPY_HW_ENABLE_STORAGE
|
|
|
|
|
2018-03-09 13:22:38 +00:00
|
|
|
int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) {
|
2018-03-09 11:22:29 +00:00
|
|
|
switch (op) {
|
|
|
|
case BDEV_IOCTL_INIT:
|
2018-03-09 13:22:38 +00:00
|
|
|
bdev->spiflash.config = (const mp_spiflash_config_t *)arg;
|
|
|
|
mp_spiflash_init(&bdev->spiflash);
|
|
|
|
bdev->flash_tick_counter_last_write = 0;
|
2018-03-09 11:22:29 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case BDEV_IOCTL_IRQ_HANDLER:
|
2020-12-17 05:59:54 +00:00
|
|
|
#if MICROPY_HW_SPIFLASH_ENABLE_CACHE
|
2018-03-27 10:38:57 +01:00
|
|
|
if ((bdev->spiflash.flags & 1) && HAL_GetTick() - bdev->flash_tick_counter_last_write >= 1000) {
|
2018-06-07 06:36:27 +01:00
|
|
|
mp_spiflash_cache_flush(&bdev->spiflash);
|
2018-03-09 11:22:29 +00:00
|
|
|
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
|
|
|
|
}
|
2020-12-17 05:59:54 +00:00
|
|
|
#endif
|
2018-03-09 11:22:29 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case BDEV_IOCTL_SYNC:
|
2020-12-17 05:59:54 +00:00
|
|
|
#if MICROPY_HW_SPIFLASH_ENABLE_CACHE
|
2018-03-09 13:22:38 +00:00
|
|
|
if (bdev->spiflash.flags & 1) {
|
2018-09-12 06:46:04 +01:00
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
2018-06-07 06:36:27 +01:00
|
|
|
mp_spiflash_cache_flush(&bdev->spiflash);
|
2018-03-09 11:22:29 +00:00
|
|
|
led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
|
|
|
|
restore_irq_pri(basepri);
|
|
|
|
}
|
2020-12-17 05:59:54 +00:00
|
|
|
#endif
|
2018-03-09 11:22:29 +00:00
|
|
|
return 0;
|
2018-03-02 12:11:47 +00:00
|
|
|
}
|
2018-03-09 11:22:29 +00:00
|
|
|
return -MP_EINVAL;
|
2018-02-13 11:21:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 05:59:54 +00:00
|
|
|
#if MICROPY_HW_SPIFLASH_ENABLE_CACHE
|
2018-03-09 13:22:38 +00:00
|
|
|
int spi_bdev_readblocks(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2018-09-12 06:46:04 +01:00
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
2018-06-07 06:36:27 +01:00
|
|
|
mp_spiflash_cached_read(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, dest);
|
2018-02-13 11:21:46 +00:00
|
|
|
restore_irq_pri(basepri);
|
|
|
|
|
2018-03-02 13:13:15 +00:00
|
|
|
return 0;
|
2018-02-13 11:21:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 13:22:38 +00:00
|
|
|
int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2018-09-12 06:46:04 +01:00
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
2018-06-07 06:36:27 +01:00
|
|
|
int ret = mp_spiflash_cached_write(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, src);
|
2018-03-09 13:22:38 +00:00
|
|
|
if (bdev->spiflash.flags & 1) {
|
2018-03-02 12:11:47 +00:00
|
|
|
led_state(PYB_LED_RED, 1); // indicate a dirty cache with LED on
|
2018-03-09 13:22:38 +00:00
|
|
|
bdev->flash_tick_counter_last_write = HAL_GetTick();
|
2018-03-02 12:11:47 +00:00
|
|
|
}
|
2018-02-13 11:21:46 +00:00
|
|
|
restore_irq_pri(basepri);
|
|
|
|
|
2018-03-02 13:13:15 +00:00
|
|
|
return ret;
|
2018-02-13 11:21:46 +00:00
|
|
|
}
|
2020-12-17 05:59:54 +00:00
|
|
|
#endif // MICROPY_HW_SPIFLASH_ENABLE_CACHE
|
2018-05-28 01:57:27 +01:00
|
|
|
|
2019-11-13 06:31:35 +00:00
|
|
|
int spi_bdev_readblocks_raw(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes) {
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
|
|
|
mp_spiflash_read(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE + block_offset, num_bytes, dest);
|
|
|
|
restore_irq_pri(basepri);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_bdev_writeblocks_raw(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes) {
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
|
|
|
int ret = mp_spiflash_write(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE + block_offset, num_bytes, src);
|
|
|
|
restore_irq_pri(basepri);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:38:29 +01:00
|
|
|
int spi_bdev_eraseblocks_raw(spi_bdev_t *bdev, uint32_t block_num, uint32_t num_bytes) {
|
|
|
|
int ret = 0;
|
|
|
|
while (num_bytes >= MP_SPIFLASH_ERASE_BLOCK_SIZE) {
|
|
|
|
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
|
|
|
|
ret = mp_spiflash_erase_block(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE);
|
|
|
|
restore_irq_pri(basepri);
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
block_num += 1;
|
|
|
|
num_bytes -= MP_SPIFLASH_ERASE_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-28 01:57:27 +01:00
|
|
|
#endif
|