1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00
esp8266/cores/esp8266/spi_utils.h
Mike Nix 60c8975feb Spi0command (#6674)
* precache() - preload code into the flash cache.

By preloading code into the flash cache we can take control over when
SPI Flash reads will occur when code is executing.
This can be useful where the timing of a section of code is extremely
critical and we don't want random pauses to pull code in from the SPI
flash chip.

It can also be useful for code that accesses/uses SPI0 which is connected
to the flash chip.

Non interrupt handler code that is infrequently called but might otherwise
require being in valuable IRAM - such as bit-banging I/O code or some code
run at bootup can avoid being permanently in IRAM.

Macros are provided to make precaching one or more blocks of code in any
function easy.

* Fix missing include

* Make precache extern "C"

* Attempt 2 at making precache extern "C"

* Fix calculation of number of cache lines to preload

With certain alignments/lengths of code it was possible to not read enough
into the flash cache.

This commit makes the length calculation clearer and adds an extra cache
line to ensure we precache enough code.

* SPI0Command - A utility function for generic SPI commands on SPI0

The rom code does not support some flash functions, or have a generic
way of sending custom commands to the flash chip.
In particular XMC flash chips have a third status register, and the
ROM only supports two.

There are also certain requirements for using SPI0 such as waiting
for the flash to be idle and not allowing your code to trigger a flash
cache miss while using SPI0.

* Clean some trailing spaces

* Upgrade _SPI0Command to _SPICommand

We needed to assess the SPI registers as base+offset to avoid referring to the
registers using constant addresses as these addresses were loaded from flash
and had the potential to trigger a flash cache miss.
For similar reasons functions need to be called via function pointers stored
in RAM. Also avoid constants in FLASH, use a copy stored in RAM.

As a side effect we can now select which controller to access as a parameter.

* Tidy up a comment thats no longer applicable

* Comments, formatting and variable renames

Added a number of comments to better explain the code and improved the
formatting.

Also renamed some variables for consistency.

* put SPI0Command in namespace experimental

* Add a comment noting that the code has only been tested on bus 0

* Replace use of memcpy with for loops in _SPICommand()

memcpy is not guaranteed to be safe (IRAM_ATTR or ROM) like I thought.
As a bonus the for loop is guaranteed to do 32-bit wide transfers, unlike memcpy.

* Typo fix

what happens when you forget to edit after copy/paste

* Move the SpiOpResult enum into experimental namespace
2019-11-04 23:14:35 -03:00

47 lines
1.2 KiB
C++

/*
spi_utils.h - SPI utility function
Copyright (c) 2015 Ivan Grokhotkov. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SPI_UTILS_H
#define SPI_UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
namespace experimental {
typedef enum {
SPI_RESULT_OK,
SPI_RESULT_ERR,
SPI_RESULT_TIMEOUT
} SpiOpResult;
SpiOpResult SPI0Command(uint8_t cmd, uint32_t *data, uint32_t mosi_bits, uint32_t miso_bits);
}
#ifdef __cplusplus
}
#endif
#endif //SPI_UTILS_H