summaryrefslogtreecommitdiff
path: root/esp8266/hspi.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-08-14 12:21:43 +1000
committerDamien George <damien.p.george@gmail.com>2017-08-14 12:21:43 +1000
commit1db008349cc970fd83b85574492badbf0460ad51 (patch)
tree76ca40810fd44eec799ff01e3c3a753c494bae9e /esp8266/hspi.c
parentbb254ba0ea89ce60dd6deab94991b2651c00dff3 (diff)
parent3611dcc260cef08eaa497cea4e3ca17977848b6c (diff)
Merge tag 'v1.8.4' into parse-bytecode
Support for stream decompression in uzlib, and more ESP8266 features This release includes some bug fixes, code clean-up, updates to the docs, more tests, and various feature additions. The uzlib module now supports efficient stream decompression in the form of the uzlib.DecompIO class. Freezing of bytecode now supports floats for the ESP8266 port, as well as complex numbers for all ports. The stmhal port has ADC working on L4 microcontrollers, fixed initialisation for DAC, and addition of the machine.WDT class and machine.reset_cause function. For the ESP8266 port Pin(16) now works as an input pin and the hardware SPI peripheral is exposed as machine.SPI(1). The os.umount function is implemented and the port supports mounting of externally connected SD cards. The machine.WDT class is added, wlan.scan() is fixed to return all access points, and there is support for DS18S20 devices. py core: - runtime: factor out exception raising helpers - runtime: define mp_check_self(pred) helper macro - objdict: get rid of asserts (remove/replace with mp_check_self()) - get rid of assert() in method argument checking functions - objtuple: in tuple_cmp_helper, use mp_check_self instead of raising - objstr: use mp_raise_{Type,Value}Error instead of mp_raise_msg - obj.h: for obj reprs A,B,C use void* explicitly for mp_obj_t typedef - mpconfigport.h: remove typedef of machine_ptr_t, it's no longer needed - sequence: allow to use bignums as indices in slice objects - stream.c: use mp_obj_get_type in mp_get_stream_raise - gc: add MICROPY_GC_CONSERVATIVE_CLEAR option to always zero memory - compile: don't compile assert statements when optimisations enabled - modstruct: use more compact mp_raise_ValueError function - emitglue: use more compact mp_raise_ValueError function - rename struct mp_code_state to mp_code_state_t - mkrules.mk: allow to override name of libmicropython.a - mpprint: fail an assertion with unsupported format specifiers - makeqstrdata.py: compute the qstr hash from bytes, not characters - if str/bytes hash is 0 then explicitly compute it - emitglue.c: provide mp_raw_code_load_file for any unix architecture - add MICROPY_USE_INTERNAL_PRINTF option, defaults to enabled extmod: - modwebrepl: set_password(): raise exception for too long password - uzlib/: update uzlib to v2.0: new API supporting stream decompression - moduzlib: refactor to new stream-compatible uzlib 2.0 API - uzlib/: update uzlib to v2.0.1: fixes for pedantic compiler warnings - uzlib/: update uzlib to v2.0.2: consistently use stdint types - modbtree: do CHECK_ERROR after __bt_seq() - modubinascii: implement binascii.crc32 - modubinascii: make crc32() support configurable - modframebuf: fix pixel accessor to return a 1-bit result - add machine_spi with generic SPI C-protocol and helper methods - modframebuf: fix fill and scroll when height not divisible by 8 - moduzlib: implement zlib stream decompressor class, DecompIO - moduzlib: use mperrno.h for error constants - modframebuf: include font from stmhal directory explicitly - moduzlib: support wbits arg to DecompIO - framebuf: add the xstep!=0 case to scroll() method lib: - utils/stdout_helpers: fix function signature to match py/mphal.h - berkeley-db-1.xx: update to upstream, fixes MacOSX build - utils/pyexec: qstr_pool_info() requires size_t* parameters drivers: - sdcard: port the SDCard driver to new machine API, with backwards compatibility for pyboard tools: - mpy-tool.py: support freezing float literals with obj-repr C - mpy-tool.py: store qstr config values in global config object - mpy-tool.py: compute the hash value for str/bytes objects - mpy-tool.py: support freezing of complex numbers tests: - rename zlibd_decompress.py -> uzlib_decompress.py - basics: add more tuple tests to improve coverage testing - basics: add more list tests to improve coverage testing - misc/non_compliant: add tests to improve coverage testing - basics: add test for break from within try within a for-loop - basics: add a test file for overriding special methods - basics/special_methods: enable tests for extra special methods - uzlib_decompress: actually test raw DEFLATE stream - run-tests: disable thread/thread_lock4.py on Travis - run-tests: disable thread/stress_heap.py when running on Travis - cmdline: add test for -O option to check optimisation value - extmod/vfs_fat_ramdisk: add tests for VFS.umount() - run-tests: disable thread_gc1.py test on Travis - unix/extra_coverage: add test for str/bytes with invalid hash - extmod: add test for uzlib.DecompIO - extmod: add a test for framebuf module, tested by coverage build - extmod/uzlib_decompio: add zlib bitstream testcases - extmod/framebuf1: add tests for scrolling in the x-direction - run-tests: disable thread/stress_recurse.py test on Travis unix port: - mpconfigport.h: don't include stdio.h on MacOS - when find'ing frozen files don't use extra slash, do follow symlinks qemu-arm port: - enable MICROPY_PY_ALL_SPECIAL_METHODS stmhal port: - boards: update STM32L476 pin defs to include ADC channels - adc.c: get ADC working on STM32L4 MCUs - fix timer capture/compare interrupt handling for TIM1 and TIM8 - remove obsolete code for special handling of TIM3 irq settings - make ADC channel 16 available on L4 MCUs - update pin print to print new constants - modusocket: set self->nic to MP_OBJ_NULL after socket close - update boot.py files to use VCP instead of CDC - spi: factor out SPI transfer code to a single function - spi: support new machine SPI methods in legacy SPI object - add machine.WDT class - set STM32F7DISC CPU Frequency to 216 MHz - dac: fix DAC (re-)initialisation by resetting DMA - wdt: implement keyword args to WDT constructor - modmachine: implement machine.reset_cause() function, and consts - machine.POWER_ON is renamed to machine.PWRON_RESET - when find'ing frozen files don't use extra slash, do follow symlinks cc3200 port: - add machine.PWRON_RESET constant (machine.POWER_ON is now deprecated) teensy port: - fix execution of frozen boot.py and main.py esp8266 port: - fix reading of pin object for GPIO16; Pin(16) now works as an input - PULL_UP is not supported on Pin(16), so raise an exception in this case - enable support for all special methods - modpybhspi: add a HSPI module for hardware SPI support - modmachinespi: add a factory method for SoftSPI/HSPI - esp_mphal: no longer disable watchdog on startup - modpybrtc: use 64-bit arithmetic when computing alarm expiry - hspi: enable duplex operation of hardware SPI - modous: add os.umount method to unmount a filesystem - modmachinewdt: implement machine.WDT class - modules: split onewire.py into OneWire and DS18X20 driver - modules/onewire: change onewire.read() to onewire.readinto() - modules/ds18x20.py: add support for DS18S20 devices - modpybspi: use generic SPI helper methods to implement SPI - modpybhspi: simplify HSPI driver by using 1 function for xfers - modmachinewdt: add .deinit() method - modmachine: add WDT_RESET and SOFT_RESET constants - modmachine: don't expose internal SoftSPI and HSPI classes - modmachine: simplify SPI class implementation multiplexing - espneopixel: disable IRQs during eps.neopixel_write - modnetwork: fix wlan.scan() method so it returns all networks - modmachine: map PWR_ON_RESET to vendor's REASON_DEFAULT_RST - machine.PWR_ON_RESET is renamed to machine.PWRON_RESET - when find'ing frozen files don't use extra slash, do follow symlinks docs: - esp8266/tutorial/pins: fix typo in commands for pin input mode - esp8266/intro: add command to install esptool.py 1.0.1 via pip - library/machine.WDT: add note that WDT is only available on WiPy - esp8266/quickref: fix and update the SPI docs - esp8266: update quickref and tutorial for OneWire/DS18X20 driver - pyboard: update USB mouse tutorial to use VCP instead of CDC - pyboard: update USB mouse tutorial to use pyb.USB_HID() - library: add reference for pyb.usb_mode and pyb.USB_HID - pyboard/quickref: add links to pinouts for other pyboard variants - pyboard/quickref: add section on "delay and timing" for utime mod - esp8266/quickref: add internal links to docs for some modules - esp8266/quickref: update information on SPI classes - esp8266/quickref: further improvements for SPI subsections - library/machine.WDT: add that WDT is available on pyboard - reference/isr_rules.rst: two minor additions to docs for using ISR misc: - add *.pyc to .gitignore, because Python 2 doesn't use __pycache__ - build mpy-cross as part of the Travis process
Diffstat (limited to 'esp8266/hspi.c')
-rw-r--r--esp8266/hspi.c331
1 files changed, 331 insertions, 0 deletions
diff --git a/esp8266/hspi.c b/esp8266/hspi.c
new file mode 100644
index 000000000..436fb4f6f
--- /dev/null
+++ b/esp8266/hspi.c
@@ -0,0 +1,331 @@
+/*
+* The MIT License (MIT)
+*
+* Copyright (c) 2015 David Ogilvy (MetalPhreak)
+* Modified 2016 by Radomir Dopieralski
+*
+* 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 "hspi.h"
+
+/*
+Wrapper to setup HSPI/SPI GPIO pins and default SPI clock
+ spi_no - SPI (0) or HSPI (1)
+Not used in Micropython.
+*/
+void spi_init(uint8_t spi_no) {
+ spi_init_gpio(spi_no, SPI_CLK_USE_DIV);
+ spi_clock(spi_no, SPI_CLK_PREDIV, SPI_CLK_CNTDIV);
+ spi_tx_byte_order(spi_no, SPI_BYTE_ORDER_HIGH_TO_LOW);
+ spi_rx_byte_order(spi_no, SPI_BYTE_ORDER_HIGH_TO_LOW);
+
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD);
+ CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);
+}
+
+
+/*
+Configures SPI mode parameters for clock edge and clock polarity.
+ spi_no - SPI (0) or HSPI (1)
+ spi_cpha - (0) Data is valid on clock leading edge
+ (1) Data is valid on clock trailing edge
+ spi_cpol - (0) Clock is low when inactive
+ (1) Clock is high when inactive
+For Micropython this version is different from original.
+*/
+void spi_mode(uint8_t spi_no, uint8_t spi_cpha, uint8_t spi_cpol) {
+ if (spi_cpol) {
+ SET_PERI_REG_MASK(SPI_PIN(HSPI), SPI_IDLE_EDGE);
+ } else {
+ CLEAR_PERI_REG_MASK(SPI_PIN(HSPI), SPI_IDLE_EDGE);
+ }
+ if (spi_cpha == spi_cpol) {
+ // Mode 3 - MOSI is set on falling edge of clock
+ // Mode 0 - MOSI is set on falling edge of clock
+ CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_CK_OUT_EDGE);
+ SET_PERI_REG_MASK(SPI_USER(HSPI), SPI_CK_I_EDGE);
+ } else {
+ // Mode 2 - MOSI is set on rising edge of clock
+ // Mode 1 - MOSI is set on rising edge of clock
+ SET_PERI_REG_MASK(SPI_USER(HSPI), SPI_CK_OUT_EDGE);
+ CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_CK_I_EDGE);
+ }
+}
+
+
+/*
+Initialise the GPIO pins for use as SPI pins.
+ spi_no - SPI (0) or HSPI (1)
+ sysclk_as_spiclk -
+ SPI_CLK_80MHZ_NODIV (1) if using 80MHz for SPI clock.
+ SPI_CLK_USE_DIV (0) if using divider for lower speed.
+*/
+void spi_init_gpio(uint8_t spi_no, uint8_t sysclk_as_spiclk) {
+ uint32_t clock_div_flag = 0;
+ if (sysclk_as_spiclk) {
+ clock_div_flag = 0x0001;
+ }
+ if (spi_no == SPI) {
+ // Set bit 8 if 80MHz sysclock required
+ WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005 | (clock_div_flag<<8));
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CLK_U, 1);
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_CMD_U, 1);
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA0_U, 1);
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_SD_DATA1_U, 1);
+ } else if (spi_no == HSPI) {
+ // Set bit 9 if 80MHz sysclock required
+ WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div_flag<<9));
+ // GPIO12 is HSPI MISO pin (Master Data In)
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2);
+ // GPIO13 is HSPI MOSI pin (Master Data Out)
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2);
+ // GPIO14 is HSPI CLK pin (Clock)
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2);
+ // GPIO15 is HSPI CS pin (Chip Select / Slave Select)
+ // In Micropython, we are handling CS ourself in drivers.
+ // PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);
+ }
+}
+
+
+/*
+Set up the control registers for the SPI clock
+ spi_no - SPI (0) or HSPI (1)
+ prediv - predivider value (actual division value)
+ cntdiv - postdivider value (actual division value)
+Set either divider to 0 to disable all division (80MHz sysclock)
+*/
+void spi_clock(uint8_t spi_no, uint16_t prediv, uint8_t cntdiv) {
+ if (prediv == 0 || cntdiv == 0) {
+ WRITE_PERI_REG(SPI_CLOCK(spi_no), SPI_CLK_EQU_SYSCLK);
+ } else {
+ WRITE_PERI_REG(SPI_CLOCK(spi_no),
+ (((prediv - 1) & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
+ (((cntdiv - 1) & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
+ (((cntdiv >> 1) & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
+ ((0 & SPI_CLKCNT_L) << SPI_CLKCNT_L_S)
+ );
+ }
+}
+
+
+/*
+Setup the byte order for shifting data out of buffer
+ spi_no - SPI (0) or HSPI (1)
+ byte_order -
+ SPI_BYTE_ORDER_HIGH_TO_LOW (1)
+ Data is sent out starting with Bit31 and down to Bit0
+ SPI_BYTE_ORDER_LOW_TO_HIGH (0)
+ Data is sent out starting with the lowest BYTE, from MSB to LSB,
+ followed by the second lowest BYTE, from MSB to LSB, followed by
+ the second highest BYTE, from MSB to LSB, followed by the highest
+ BYTE, from MSB to LSB 0xABCDEFGH would be sent as 0xGHEFCDAB.
+*/
+void spi_tx_byte_order(uint8_t spi_no, uint8_t byte_order) {
+ if (byte_order) {
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_WR_BYTE_ORDER);
+ } else {
+ CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_WR_BYTE_ORDER);
+ }
+}
+
+
+/*
+Setup the byte order for shifting data into buffer
+ spi_no - SPI (0) or HSPI (1)
+ byte_order -
+ SPI_BYTE_ORDER_HIGH_TO_LOW (1)
+ Data is read in starting with Bit31 and down to Bit0
+ SPI_BYTE_ORDER_LOW_TO_HIGH (0)
+ Data is read in starting with the lowest BYTE, from MSB to LSB,
+ followed by the second lowest BYTE, from MSB to LSB, followed by
+ the second highest BYTE, from MSB to LSB, followed by the highest
+ BYTE, from MSB to LSB 0xABCDEFGH would be read as 0xGHEFCDAB
+*/
+void spi_rx_byte_order(uint8_t spi_no, uint8_t byte_order) {
+ if (byte_order) {
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_RD_BYTE_ORDER);
+ } else {
+ CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_RD_BYTE_ORDER);
+ }
+}
+
+
+/*
+SPI transaction function
+ spi_no - SPI (0) or HSPI (1)
+ cmd_bits - actual number of bits to transmit
+ cmd_data - command data
+ addr_bits - actual number of bits to transmit
+ addr_data - address data
+ dout_bits - actual number of bits to transmit
+ dout_data - output data
+ din_bits - actual number of bits to receive
+Returns: read data - uint32_t containing read in data only if RX was set
+ 0 - something went wrong (or actual read data was 0)
+ 1 - data sent ok (or actual read data is 1)
+Note: all data is assumed to be stored in the lower bits of the data variables
+(for anything <32 bits).
+*/
+uint32_t spi_transaction(uint8_t spi_no, uint8_t cmd_bits, uint16_t cmd_data,
+ uint32_t addr_bits, uint32_t addr_data,
+ uint32_t dout_bits, uint32_t dout_data,
+ uint32_t din_bits, uint32_t dummy_bits) {
+ while (spi_busy(spi_no)) {}; // Wait for SPI to be ready
+
+// Enable SPI Functions
+ // Disable MOSI, MISO, ADDR, COMMAND, DUMMY in case previously set.
+ CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI | SPI_USR_MISO |
+ SPI_USR_COMMAND | SPI_USR_ADDR | SPI_USR_DUMMY);
+
+ // Enable functions based on number of bits. 0 bits = disabled.
+ // This is rather inefficient but allows for a very generic function.
+ // CMD ADDR and MOSI are set below to save on an extra if statement.
+ if (din_bits) {
+ if (dout_bits) {
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_DOUTDIN);
+ } else {
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MISO);
+ }
+ }
+ if (dummy_bits) {
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_DUMMY);
+ }
+
+// Setup Bitlengths
+ WRITE_PERI_REG(SPI_USER1(spi_no),
+ // Number of bits in Address
+ ((addr_bits - 1) & SPI_USR_ADDR_BITLEN) << SPI_USR_ADDR_BITLEN_S |
+ // Number of bits to Send
+ ((dout_bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S |
+ // Number of bits to receive
+ ((din_bits - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S |
+ // Number of Dummy bits to insert
+ ((dummy_bits - 1) & SPI_USR_DUMMY_CYCLELEN) << SPI_USR_DUMMY_CYCLELEN_S);
+
+// Setup Command Data
+ if (cmd_bits) {
+ // Enable COMMAND function in SPI module
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_COMMAND);
+ // Align command data to high bits
+ uint16_t command = cmd_data << (16-cmd_bits);
+ // Swap byte order
+ command = ((command>>8)&0xff) | ((command<<8)&0xff00);
+ WRITE_PERI_REG(SPI_USER2(spi_no), (
+ (((cmd_bits - 1) & SPI_USR_COMMAND_BITLEN) << SPI_USR_COMMAND_BITLEN_S) |
+ (command & SPI_USR_COMMAND_VALUE)
+ ));
+ }
+
+// Setup Address Data
+ if (addr_bits) {
+ // Enable ADDRess function in SPI module
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_ADDR);
+ // Align address data to high bits
+ WRITE_PERI_REG(SPI_ADDR(spi_no), addr_data << (32 - addr_bits));
+ }
+
+// Setup DOUT data
+ if (dout_bits) {
+ // Enable MOSI function in SPI module
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI);
+ // Copy data to W0
+ if (READ_PERI_REG(SPI_USER(spi_no))&SPI_WR_BYTE_ORDER) {
+ WRITE_PERI_REG(SPI_W0(spi_no), dout_data << (32 - dout_bits));
+ } else {
+ uint8_t dout_extra_bits = dout_bits%8;
+
+ if (dout_extra_bits) {
+ // If your data isn't a byte multiple (8/16/24/32 bits) and you
+ // don't have SPI_WR_BYTE_ORDER set, you need this to move the
+ // non-8bit remainder to the MSBs. Not sure if there's even a use
+ // case for this, but it's here if you need it... For example,
+ // 0xDA4 12 bits without SPI_WR_BYTE_ORDER would usually be output
+ // as if it were 0x0DA4, of which 0xA4, and then 0x0 would be
+ // shifted out (first 8 bits of low byte, then 4 MSB bits of high
+ // byte - ie reverse byte order).
+ // The code below shifts it out as 0xA4 followed by 0xD as you
+ // might require.
+ WRITE_PERI_REG(SPI_W0(spi_no), (
+ (0xFFFFFFFF << (dout_bits - dout_extra_bits) & dout_data)
+ << (8-dout_extra_bits) |
+ ((0xFFFFFFFF >> (32 - (dout_bits - dout_extra_bits)))
+ & dout_data)
+ ));
+ } else {
+ WRITE_PERI_REG(SPI_W0(spi_no), dout_data);
+ }
+ }
+}
+
+// Begin SPI Transaction
+ SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
+
+// Return DIN data
+ if (din_bits) {
+ while (spi_busy(spi_no)) {}; // Wait for SPI transaction to complete
+ if (READ_PERI_REG(SPI_USER(spi_no))&SPI_RD_BYTE_ORDER) {
+ // Assuming data in is written to MSB. TBC
+ return READ_PERI_REG(SPI_W0(spi_no)) >> (32 - din_bits);
+ } else {
+ // Read in the same way as DOUT is sent. Note existing contents of
+ // SPI_W0 remain unless overwritten!
+ return READ_PERI_REG(SPI_W0(spi_no));
+ }
+ return 0; // Something went wrong
+ }
+
+ // Transaction completed
+ return 1; // Success
+}
+
+
+/*
+Just do minimal work needed to send 8 bits.
+*/
+inline void spi_tx8fast(uint8_t spi_no, uint8_t dout_data) {
+ while (spi_busy(spi_no)) {}; // Wait for SPI to be ready
+
+// Enable SPI Functions
+ // Disable MOSI, MISO, ADDR, COMMAND, DUMMY in case previously set.
+ CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI | SPI_USR_MISO |
+ SPI_USR_COMMAND | SPI_USR_ADDR | SPI_USR_DUMMY);
+
+// Setup Bitlengths
+ WRITE_PERI_REG(SPI_USER1(spi_no),
+ // Number of bits to Send
+ ((8 - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S |
+ // Number of bits to receive
+ ((8 - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S);
+
+
+// Setup DOUT data
+ // Enable MOSI function in SPI module
+ SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI);
+ // Copy data to W0
+ if (READ_PERI_REG(SPI_USER(spi_no)) & SPI_WR_BYTE_ORDER) {
+ WRITE_PERI_REG(SPI_W0(spi_no), dout_data << (32 - 8));
+ } else {
+ WRITE_PERI_REG(SPI_W0(spi_no), dout_data);
+ }
+
+// Begin SPI Transaction
+ SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
+}