sysparam fixes, tests, spi flash refactoring (#299)
Original work by @ourairquality * Sysparam threadsafe and SPI access * Sysparam test cases * Fix for negative int8 * Sysparam getting bool without memory allocation. Bool tests. * SPI flash refactoring. * Extract common spiflash.c into core. * Use spiflash.c in sysparam. * Use memcpy in spiflash.c insted of hand-written version. * Tests for spiflash.c
This commit is contained in:
parent
07ca0d2e9e
commit
a91ec6eb61
10 changed files with 724 additions and 406 deletions
|
@ -7,11 +7,10 @@
|
|||
*/
|
||||
#include "esp_spiffs.h"
|
||||
#include "spiffs.h"
|
||||
#include <espressif/spi_flash.h>
|
||||
#include <spiflash.h>
|
||||
#include <stdbool.h>
|
||||
#include <esp/uart.h>
|
||||
#include <fcntl.h>
|
||||
#include "esp_spiffs_flash.h"
|
||||
|
||||
spiffs fs;
|
||||
|
||||
|
@ -34,7 +33,7 @@ static fs_buf_t cache_buf = {0};
|
|||
|
||||
static s32_t esp_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
|
||||
{
|
||||
if (esp_spiffs_flash_read(addr, dst, size) == ESP_SPIFFS_FLASH_ERROR) {
|
||||
if (!spiflash_read(addr, dst, size)) {
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
|
||||
|
@ -43,7 +42,7 @@ static s32_t esp_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
|
|||
|
||||
static s32_t esp_spiffs_write(u32_t addr, u32_t size, u8_t *src)
|
||||
{
|
||||
if (esp_spiffs_flash_write(addr, src, size) == ESP_SPIFFS_FLASH_ERROR) {
|
||||
if (!spiflash_write(addr, src, size)) {
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
|
||||
|
@ -52,11 +51,10 @@ static s32_t esp_spiffs_write(u32_t addr, u32_t size, u8_t *src)
|
|||
|
||||
static s32_t esp_spiffs_erase(u32_t addr, u32_t size)
|
||||
{
|
||||
uint32_t sectors = size / SPI_FLASH_SEC_SIZE;
|
||||
uint32_t sectors = size / SPI_FLASH_SECTOR_SIZE;
|
||||
|
||||
for (uint32_t i = 0; i < sectors; i++) {
|
||||
if (esp_spiffs_flash_erase_sector(addr + (SPI_FLASH_SEC_SIZE * i))
|
||||
== ESP_SPIFFS_FLASH_ERROR) {
|
||||
if (!spiflash_erase_sector(addr + (SPI_FLASH_SECTOR_SIZE * i))) {
|
||||
return SPIFFS_ERR_INTERNAL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,273 +0,0 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 sheinz (https://github.com/sheinz)
|
||||
*
|
||||
* 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 "esp_spiffs_flash.h"
|
||||
#include "flashchip.h"
|
||||
#include "espressif/spi_flash.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "esp/rom.h"
|
||||
#include "esp/spi_regs.h"
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Note about Wait_SPI_Idle.
|
||||
*
|
||||
* Each write/erase flash operation sets BUSY bit in flash status register.
|
||||
* If attempt to access flash while BUSY bit is set operation will fail.
|
||||
* Function Wait_SPI_Idle loops until this bit is not cleared.
|
||||
*
|
||||
* The approach in the following code is that each write function that is
|
||||
* accessible from the outside should leave flash in Idle state.
|
||||
* The read operations doesn't set BUSY bit in a flash. So they do not wait.
|
||||
* They relay that previous operation is completely finished.
|
||||
*
|
||||
* This approach is different from ESP8266 bootrom where Wait_SPI_Idle is
|
||||
* called where it needed and not.
|
||||
*/
|
||||
|
||||
#define SPI_WRITE_MAX_SIZE 64
|
||||
|
||||
// 64 bytes read causes hang
|
||||
// http://bbs.espressif.com/viewtopic.php?f=6&t=2439
|
||||
#define SPI_READ_MAX_SIZE 60
|
||||
|
||||
|
||||
/**
|
||||
* Copy unaligned data to 4-byte aligned destination buffer.
|
||||
*
|
||||
* @param words Number of 4-byte words to write.
|
||||
*
|
||||
* @see unaligned_memcpy.S
|
||||
*/
|
||||
void memcpy_unaligned_src(volatile uint32_t *dst, uint8_t *src, uint8_t words);
|
||||
|
||||
/**
|
||||
* Copy 4-byte aligned source data to unaligned destination buffer.
|
||||
*
|
||||
* @param bytes Number of byte to copy to dst.
|
||||
*
|
||||
* @see unaligned_memcpy.S
|
||||
*/
|
||||
void memcpy_unaligned_dst(uint8_t *dst, volatile uint32_t *src, uint8_t bytes);
|
||||
|
||||
|
||||
/**
|
||||
* Low level SPI flash write. Write block of data up to 64 bytes.
|
||||
*/
|
||||
static inline void IRAM spi_write_data(sdk_flashchip_t *chip, uint32_t addr,
|
||||
uint8_t *buf, uint32_t size)
|
||||
{
|
||||
uint32_t words = size >> 2;
|
||||
if (size & 0b11) {
|
||||
words++;
|
||||
}
|
||||
|
||||
Wait_SPI_Idle(chip); // wait for previous write to finish
|
||||
|
||||
SPI(0).ADDR = (addr & 0x00FFFFFF) | (size << 24);
|
||||
|
||||
memcpy_unaligned_src(SPI(0).W, buf, words);
|
||||
|
||||
SPI_write_enable(chip);
|
||||
|
||||
SPI(0).CMD = SPI_CMD_PP;
|
||||
while (SPI(0).CMD) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a page of flash. Data block should not cross page boundary.
|
||||
*/
|
||||
static uint32_t IRAM spi_write_page(sdk_flashchip_t *flashchip, uint32_t dest_addr,
|
||||
uint8_t *buf, uint32_t size)
|
||||
{
|
||||
// check if block to write doesn't cross page boundary
|
||||
if (flashchip->page_size < size + (dest_addr % flashchip->page_size)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
if (size < 1) {
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
|
||||
while (size >= SPI_WRITE_MAX_SIZE) {
|
||||
spi_write_data(flashchip, dest_addr, buf, SPI_WRITE_MAX_SIZE);
|
||||
|
||||
size -= SPI_WRITE_MAX_SIZE;
|
||||
dest_addr += SPI_WRITE_MAX_SIZE;
|
||||
buf += SPI_WRITE_MAX_SIZE;
|
||||
|
||||
if (size < 1) {
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
}
|
||||
|
||||
spi_write_data(flashchip, dest_addr, buf, size);
|
||||
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split block of data into pages and write pages.
|
||||
*/
|
||||
static uint32_t IRAM spi_write(uint32_t addr, uint8_t *dst, uint32_t size)
|
||||
{
|
||||
if (sdk_flashchip.chip_size < (addr + size)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
uint32_t write_bytes_to_page = sdk_flashchip.page_size -
|
||||
(addr % sdk_flashchip.page_size); // TODO: place for optimization
|
||||
|
||||
if (size < write_bytes_to_page) {
|
||||
if (spi_write_page(&sdk_flashchip, addr, dst, size)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (spi_write_page(&sdk_flashchip, addr, dst, write_bytes_to_page)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
uint32_t offset = write_bytes_to_page;
|
||||
uint32_t pages_to_write = (size - offset) / sdk_flashchip.page_size;
|
||||
for (uint8_t i = 0; i != pages_to_write; i++) {
|
||||
if (spi_write_page(&sdk_flashchip, addr + offset,
|
||||
dst + offset, sdk_flashchip.page_size)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
offset += sdk_flashchip.page_size;
|
||||
}
|
||||
|
||||
if (spi_write_page(&sdk_flashchip, addr + offset,
|
||||
dst + offset, size - offset)) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
|
||||
uint32_t IRAM esp_spiffs_flash_write(uint32_t addr, uint8_t *buf, uint32_t size)
|
||||
{
|
||||
uint32_t result = ESP_SPIFFS_FLASH_ERROR;
|
||||
|
||||
if (buf) {
|
||||
vPortEnterCritical();
|
||||
Cache_Read_Disable();
|
||||
|
||||
result = spi_write(addr, buf, size);
|
||||
|
||||
// make sure all write operations is finished before exiting
|
||||
Wait_SPI_Idle(&sdk_flashchip);
|
||||
|
||||
Cache_Read_Enable(0, 0, 1);
|
||||
vPortExitCritical();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read SPI flash up to 64 bytes.
|
||||
*/
|
||||
static inline void IRAM read_block(sdk_flashchip_t *chip, uint32_t addr,
|
||||
uint8_t *buf, uint32_t size)
|
||||
{
|
||||
SPI(0).ADDR = (addr & 0x00FFFFFF) | (size << 24);
|
||||
SPI(0).CMD = SPI_CMD_READ;
|
||||
|
||||
while (SPI(0).CMD) {};
|
||||
|
||||
memcpy_unaligned_dst(buf, SPI(0).W, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read SPI flash data. Data region doesn't need to be page aligned.
|
||||
*/
|
||||
static inline uint32_t IRAM read_data(sdk_flashchip_t *flashchip, uint32_t addr,
|
||||
uint8_t *dst, uint32_t size)
|
||||
{
|
||||
if (size < 1) {
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
|
||||
if ((addr + size) > flashchip->chip_size) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
while (size >= SPI_READ_MAX_SIZE) {
|
||||
read_block(flashchip, addr, dst, SPI_READ_MAX_SIZE);
|
||||
dst += SPI_READ_MAX_SIZE;
|
||||
size -= SPI_READ_MAX_SIZE;
|
||||
addr += SPI_READ_MAX_SIZE;
|
||||
}
|
||||
|
||||
if (size > 0) {
|
||||
read_block(flashchip, addr, dst, size);
|
||||
}
|
||||
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
||||
|
||||
uint32_t IRAM esp_spiffs_flash_read(uint32_t dest_addr, uint8_t *buf, uint32_t size)
|
||||
{
|
||||
uint32_t result = ESP_SPIFFS_FLASH_ERROR;
|
||||
|
||||
if (buf) {
|
||||
vPortEnterCritical();
|
||||
Cache_Read_Disable();
|
||||
|
||||
result = read_data(&sdk_flashchip, dest_addr, buf, size);
|
||||
|
||||
Cache_Read_Enable(0, 0, 1);
|
||||
vPortExitCritical();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t IRAM esp_spiffs_flash_erase_sector(uint32_t addr)
|
||||
{
|
||||
if ((addr + sdk_flashchip.sector_size) > sdk_flashchip.chip_size) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
if (addr & 0xFFF) {
|
||||
return ESP_SPIFFS_FLASH_ERROR;
|
||||
}
|
||||
|
||||
vPortEnterCritical();
|
||||
Cache_Read_Disable();
|
||||
|
||||
SPI_write_enable(&sdk_flashchip);
|
||||
|
||||
SPI(0).ADDR = addr & 0x00FFFFFF;
|
||||
SPI(0).CMD = SPI_CMD_SE;
|
||||
while (SPI(0).CMD) {};
|
||||
|
||||
Wait_SPI_Idle(&sdk_flashchip);
|
||||
|
||||
Cache_Read_Enable(0, 0, 1);
|
||||
vPortExitCritical();
|
||||
|
||||
return ESP_SPIFFS_FLASH_OK;
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 sheinz (https://github.com/sheinz)
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef __ESP_SPIFFS_FLASH_H__
|
||||
#define __ESP_SPIFFS_FLASH_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "common_macros.h"
|
||||
|
||||
#define ESP_SPIFFS_FLASH_OK 0
|
||||
#define ESP_SPIFFS_FLASH_ERROR 1
|
||||
|
||||
/**
|
||||
* Read data from SPI flash.
|
||||
*
|
||||
* @param addr Address to read from. Can be not aligned.
|
||||
* @param buf Buffer to read to. Doesn't have to be aligned.
|
||||
* @param size Size of data to read. Buffer size must be >= than data size.
|
||||
*
|
||||
* @return ESP_SPIFFS_FLASH_OK or ESP_SPIFFS_FLASH_ERROR
|
||||
*/
|
||||
uint32_t IRAM esp_spiffs_flash_read(uint32_t addr, uint8_t *buf, uint32_t size);
|
||||
|
||||
/**
|
||||
* Write data to SPI flash.
|
||||
*
|
||||
* @param addr Address to write to. Can be not aligned.
|
||||
* @param buf Buffer of data to write to flash. Doesn't have to be aligned.
|
||||
* @param size Size of data to write. Buffer size must be >= than data size.
|
||||
*
|
||||
* @return ESP_SPIFFS_FLASH_OK or ESP_SPIFFS_FLASH_ERROR
|
||||
*/
|
||||
uint32_t IRAM esp_spiffs_flash_write(uint32_t addr, uint8_t *buf, uint32_t size);
|
||||
|
||||
/**
|
||||
* Erase a sector.
|
||||
*
|
||||
* @param addr Address of sector to erase. Must be sector aligned.
|
||||
*
|
||||
* @return ESP_SPIFFS_FLASH_OK or ESP_SPIFFS_FLASH_ERROR
|
||||
*/
|
||||
uint32_t IRAM esp_spiffs_flash_erase_sector(uint32_t addr);
|
||||
|
||||
#endif // __ESP_SPIFFS_FLASH_H__
|
|
@ -1,112 +0,0 @@
|
|||
/**
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 sheinz (https://github.com/sheinz)
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
.text
|
||||
.section .iram1.text, "x"
|
||||
.literal_position
|
||||
|
||||
/**
|
||||
* Copy unaligned data to 4-byte aligned buffer.
|
||||
*/
|
||||
.align 4
|
||||
.global memcpy_unaligned_src
|
||||
.type memcpy_unaligned_src, @function
|
||||
memcpy_unaligned_src:
|
||||
/* a2: dst, a3: src, a4: size */
|
||||
ssa8l a3
|
||||
srli a3, a3, 2
|
||||
slli a3, a3, 2
|
||||
beqz a4, u_src_end
|
||||
l32i a6, a3, 0
|
||||
u_src_loop:
|
||||
l32i a7, a3, 4
|
||||
src a8, a7, a6
|
||||
memw
|
||||
s32i a8, a2, 0
|
||||
mov a6, a7
|
||||
addi a3, a3, 4
|
||||
addi a2, a2, 4
|
||||
addi a4, a4, -1
|
||||
bnez a4, u_src_loop
|
||||
u_src_end:
|
||||
movi a2, 0
|
||||
ret.n
|
||||
|
||||
|
||||
/**
|
||||
* Copy data from 4-byte aligned source to unaligned destination buffer.
|
||||
*/
|
||||
.align 4
|
||||
.global memcpy_unaligned_dst
|
||||
.type memcpy_unaligned_dst, @function
|
||||
memcpy_unaligned_dst:
|
||||
/* a2: dst, a3: src, a4: size */
|
||||
beqz.n a4, u_dst_end
|
||||
extui a5, a4, 0, 2
|
||||
beqz.n a5, aligned_dst_loop
|
||||
u_dst_loop:
|
||||
/* Load data word */
|
||||
memw
|
||||
l32i.n a5, a3, 0
|
||||
|
||||
/* Save byte number 0 */
|
||||
s8i a5, a2, 0
|
||||
addi.n a4, a4, -1
|
||||
beqz a4, u_dst_end
|
||||
addi.n a2, a2, 1
|
||||
|
||||
/* Shift and save byte number 1 */
|
||||
srli a5, a5, 8
|
||||
s8i a5, a2, 0
|
||||
addi.n a4, a4, -1
|
||||
beqz a4, u_dst_end
|
||||
addi.n a2, a2, 1
|
||||
|
||||
/* Shift and save byte number 2 */
|
||||
srli a5, a5, 8
|
||||
s8i a5, a2, 0
|
||||
addi.n a4, a4, -1
|
||||
beqz a4, u_dst_end
|
||||
addi.n a2, a2, 1
|
||||
|
||||
/* Shift and save byte number 3 */
|
||||
srli a5, a5, 8
|
||||
s8i a5, a2, 0
|
||||
addi.n a4, a4, -1
|
||||
addi.n a2, a2, 1
|
||||
|
||||
/* Next word */
|
||||
addi.n a3, a3, 4
|
||||
bnez.n a4, u_dst_loop
|
||||
ret.n
|
||||
aligned_dst_loop:
|
||||
memw
|
||||
l32i a5, a3, 0
|
||||
s32i a5, a2, 0
|
||||
addi.n a3, a3, 4
|
||||
addi.n a2, a2, 4
|
||||
addi.n a4, a4, -4
|
||||
bnez.n a4, aligned_dst_loop
|
||||
u_dst_end: ret.n
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue