Sample SPI drivers for a number of the Adesto Technologies flash devices.
Macros | Functions
spi_driver.h File Reference

Detailed Description

Declarations of spi_driver functions.

This is the Adesto SPI driver. It supports standard SPI, dual SPI, and Quad SPI. Functionality is built in for a trigger GPIO as well as a JEDEC reset.

Initialization of the driver takes place with the SPI_ConfigureSingleSPIIOs() function which should be called after the pins have been set as GPIOs but before adesto layer commands are run.

Definition in file spi_driver.h.

#include "user_config.h"

Go to the source code of this file.

Macros

#define SPI_CSB_PORT   USER_CONFIG_CSB_PORT
 Base register used for CSb control. More...
 
#define SPI_TRIGGER_PORT   USER_CONFIG_TRIGGER_PORT
 Base register used for TRIGGER control. More...
 
#define SPI_SCK_PORT   USER_CONFIG_SCK_PORT
 Base register used for SCK control. More...
 
#define SPI_MOSI_PORT   USER_CONFIG_MOSI_PORT
 Base register used for MOSI control. More...
 
#define SPI_MISO_PORT   USER_CONFIG_MISO_PORT
 Base register used for MISO control. More...
 
#define SPI_WPB_PORT   USER_CONFIG_IO2_PORT
 Base register used for WPb control. More...
 
#define SPI_HOLDB_PORT   USER_CONFIG_IO3_PORT
 Base register used for HOLDb control. More...
 
#define SPI_CSB_PIN   USER_CONFIG_CSB_PIN
 
#define SPI_TRIGGER_PIN   USER_CONFIG_TRIGGER_PIN
 Pin number for TRIGGER. More...
 
#define SPI_SCK_PIN   USER_CONFIG_SCK_PIN
 Pin number for SCK. More...
 
#define SPI_MOSI_PIN   USER_CONFIG_MOSI_PIN
 Pin number for MOSI. More...
 
#define SPI_MISO_PIN   USER_CONFIG_MISO_PIN
 Pin number for MISO. More...
 
#define SPI_WPB_PIN   USER_CONFIG_IO2_PIN
 Pin number for WPb - IO2. More...
 
#define SPI_HOLDB_PIN   USER_CONFIG_IO3_PIN
 Pin number for HOLDb - IO3. More...
 
#define DELAY   5U
 Half clock period delay interval. More...
 
#define SPI   0
 
#define QPI   1
 

Functions

void SPI_PinInit (uint32_t port, uint32_t pin, enum directionIO direction)
 Initializes a given pin as either an input or output. More...
 
void SPI_PinClear (uint32_t port, uint32_t pin)
 Clears a given pin on a port to LOW. More...
 
void SPI_PinSet (uint32_t port, uint32_t pin)
 Sets a given pin on a port to HIGH. More...
 
uint32_t SPI_PinRead (uint32_t port, uint32_t pin)
 Reads the voltage on a given pin. More...
 
void SPI_Exchange (uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
 Sends and receives bytes based on the function parameters. MISO and MOSI fill their standard SPI roles. More...
 
void SPI_DualExchange (uint8_t standardSPINumBytes, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
 Sends and receives bytes based on the function parameters. MOSI is used for the opcode and address, then MISO or MOSI are switched for transmission and receiving such that data is transmitted from/to the flash device along both lines. Their state will then be reverted to original SPI functionality. More...
 
void SPI_QuadExchange (uint8_t standardSPINumBytes, uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
 Sends and receives bytes based on the function parameters. MOSI can used for the opcode and address, then MISO, MOSI, HOLDb, WPb are switched for transmission and receiving such that data is transmitted from/to the flash device along all 4 lines. Their state will then be reverted to original SPI functionality. More...
 
uint8_t SPI_ReceiveByte ()
 Receives a byte along MISO and returns the value received. More...
 
uint8_t SPI_DualReceiveByte ()
 Receives a byte along both MOSI and MISO and returns the value received. More...
 
void SPI_SendByte (uint8_t transmittedByte)
 Sends a byte along MOSI. More...
 
void SPI_DualSendByte (uint8_t transmittedByte)
 Sends a byte along both MOSI and MISO. More...
 
void SPI_ClockTick ()
 Toggles the clock: current_state->high->low. More...
 
void SPI_SendBit (uint8_t transmittedBit)
 Sends a single bit along MOSI while toggling the clock. More...
 
void SPI_Delay (uint32_t delayTime)
 Performs a delayTime number of NOPs. More...
 
void SPI_ConfigureSingleSPIIOs ()
 Configure the IOs for SPI bit banging usage. 4 pins are needed: CSb, SCK, MOSI, MISO. More...
 
void SPI_ReturnToSingleSPIIOs ()
 Returns MISO and MOSI pins to their standard SPI state as an input and output. More...
 
void SPI_ConfigureDualSPIIOsInput ()
 Configures SPIO IOs for dual input. This changes the MOSI pin from an output to an input so that the slave can drive both SI and SO. More...
 
void SPI_ConfigureDualSPIIOsOutput ()
 Configures SPIO IOs for dual output. This changes the MISO pin from an input to an output so that the master can drive both SI and SO. More...
 
void SPI_ConfigureQuadSPIIOsInput ()
 Configures SPIO IOs for quad input. This changes the MOSI, WPb, and HOLDb pins to inputs so that the slave can drive all 4 IOs. More...
 
void SPI_ConfigureQuadSPIIOsOutput ()
 Configures SPIO IOs for quad output. This changes the MISO pin to an output so that the master can drive all 4 IOs. More...
 
void SPI_QuadSendByte (uint8_t transmittedByte)
 Sends a byte along MISO, MOSI, WPb, and HOLDb. More...
 
uint8_t SPI_QuadReceiveByte ()
 Receives a byte along MOSI, MISO, HOLDb, and WPb and returns the value received. More...
 
void SPI_Trigger ()
 Triggers a falling edge on the SPI_TRIGGER_PORT/PIN output. More...
 
void SPI_JEDECReset ()
 Performs a JEDEC reset on the SPI device. More...
 

Macro Definition Documentation

◆ DELAY

#define DELAY   5U

Half clock period delay interval.

Definition at line 108 of file spi_driver.h.

◆ QPI

#define QPI   1

Definition at line 111 of file spi_driver.h.

◆ SPI

#define SPI   0

Definition at line 110 of file spi_driver.h.

◆ SPI_CSB_PIN

#define SPI_CSB_PIN   USER_CONFIG_CSB_PIN

Pin number for CSb

Warning
Changes based on adapter used! Use pin 4 with Moneta, pin 1 with others on K82 board.
Don't forget to update USER_CONFIG_BoardInit() called from main! As is, both CSb pins are configured as outputs so the changes above and below in this file are the only things needed (pin and port numbers for CSb).

Definition at line 94 of file spi_driver.h.

◆ SPI_CSB_PORT

#define SPI_CSB_PORT   USER_CONFIG_CSB_PORT

Base register used for CSb control.

Warning
Use port D for Moneta shield, port C for Dataflash/AT25 shield. on K82 board. Port D = 0x400FF0C0U, Port C = 0x400FF080U

Definition at line 61 of file spi_driver.h.

◆ SPI_HOLDB_PIN

#define SPI_HOLDB_PIN   USER_CONFIG_IO3_PIN

Pin number for HOLDb - IO3.

Definition at line 106 of file spi_driver.h.

◆ SPI_HOLDB_PORT

#define SPI_HOLDB_PORT   USER_CONFIG_IO3_PORT

Base register used for HOLDb control.

Definition at line 85 of file spi_driver.h.

◆ SPI_MISO_PIN

#define SPI_MISO_PIN   USER_CONFIG_MISO_PIN

Pin number for MISO.

Definition at line 102 of file spi_driver.h.

◆ SPI_MISO_PORT

#define SPI_MISO_PORT   USER_CONFIG_MISO_PORT

Base register used for MISO control.

Definition at line 77 of file spi_driver.h.

◆ SPI_MOSI_PIN

#define SPI_MOSI_PIN   USER_CONFIG_MOSI_PIN

Pin number for MOSI.

Definition at line 100 of file spi_driver.h.

◆ SPI_MOSI_PORT

#define SPI_MOSI_PORT   USER_CONFIG_MOSI_PORT

Base register used for MOSI control.

Definition at line 73 of file spi_driver.h.

◆ SPI_SCK_PIN

#define SPI_SCK_PIN   USER_CONFIG_SCK_PIN

Pin number for SCK.

Definition at line 98 of file spi_driver.h.

◆ SPI_SCK_PORT

#define SPI_SCK_PORT   USER_CONFIG_SCK_PORT

Base register used for SCK control.

Definition at line 69 of file spi_driver.h.

◆ SPI_TRIGGER_PIN

#define SPI_TRIGGER_PIN   USER_CONFIG_TRIGGER_PIN

Pin number for TRIGGER.

Definition at line 96 of file spi_driver.h.

◆ SPI_TRIGGER_PORT

#define SPI_TRIGGER_PORT   USER_CONFIG_TRIGGER_PORT

Base register used for TRIGGER control.

Definition at line 65 of file spi_driver.h.

◆ SPI_WPB_PIN

#define SPI_WPB_PIN   USER_CONFIG_IO2_PIN

Pin number for WPb - IO2.

Definition at line 104 of file spi_driver.h.

◆ SPI_WPB_PORT

#define SPI_WPB_PORT   USER_CONFIG_IO2_PORT

Base register used for WPb control.

Definition at line 81 of file spi_driver.h.

Function Documentation

◆ SPI_ClockTick()

void SPI_ClockTick ( )

Toggles the clock: current_state->high->low.

Return values
void

Definition at line 149 of file spi_driver.c.

◆ SPI_ConfigureDualSPIIOsInput()

void SPI_ConfigureDualSPIIOsInput ( )

Configures SPIO IOs for dual input. This changes the MOSI pin from an output to an input so that the slave can drive both SI and SO.

Return values
void
Warning
Make sure to reconfigure the SPIIOs using SPI_ReturnToSingleSPIIOs() when this is no longer needed.

Definition at line 101 of file spi_driver.c.

◆ SPI_ConfigureDualSPIIOsOutput()

void SPI_ConfigureDualSPIIOsOutput ( )

Configures SPIO IOs for dual output. This changes the MISO pin from an input to an output so that the master can drive both SI and SO.

Return values
void
Warning
Make sure to reconfigure the SPIIOs using SPI_ReturnToSingleSPIIOs() when this is no longer needed.

Definition at line 121 of file spi_driver.c.

◆ SPI_ConfigureQuadSPIIOsInput()

void SPI_ConfigureQuadSPIIOsInput ( )

Configures SPIO IOs for quad input. This changes the MOSI, WPb, and HOLDb pins to inputs so that the slave can drive all 4 IOs.

Return values
void
Warning
Make sure to reconfigure the SPIIOs using SPI_ReturnToSingleSPIIOs() when this is no longer needed.

Definition at line 108 of file spi_driver.c.

◆ SPI_ConfigureQuadSPIIOsOutput()

void SPI_ConfigureQuadSPIIOsOutput ( )

Configures SPIO IOs for quad output. This changes the MISO pin to an output so that the master can drive all 4 IOs.

Return values
void
Warning
Make sure to reconfigure the SPIIOs using SPI_ReturnToSingleSPIIOs() when this is no longer needed.

Definition at line 128 of file spi_driver.c.

◆ SPI_ConfigureSingleSPIIOs()

void SPI_ConfigureSingleSPIIOs ( )

Configure the IOs for SPI bit banging usage. 4 pins are needed: CSb, SCK, MOSI, MISO.

Return values
void
Warning
Moneta and Dataflash shields differ. Moneta uses PTD4 for CSb, while Dataflash uses PTC2-8.
Only call this function once at the beginning of the program to configure the IOs. Calling this function a second time will cause CSb to go low temporarily if it was high. Use SPI_ReturnToSingleSPIIOs() if a return to single SPI is required mid program.

Definition at line 61 of file spi_driver.c.

◆ SPI_Delay()

void SPI_Delay ( uint32_t  delayTime)

Performs a delayTime number of NOPs.

Parameters
delayTimeThe number of NOPs to be run.
Return values
void

Definition at line 140 of file spi_driver.c.

◆ SPI_DualExchange()

void SPI_DualExchange ( uint8_t  standardSPINumBytes,
uint8_t *  txBuffer,
uint32_t  txNumBytes,
uint8_t *  rxBuffer,
uint32_t  rxNumBytes,
uint32_t  dummyNumBytes 
)

Sends and receives bytes based on the function parameters. MOSI is used for the opcode and address, then MISO or MOSI are switched for transmission and receiving such that data is transmitted from/to the flash device along both lines. Their state will then be reverted to original SPI functionality.

Parameters
standardSPINumBytesThe number of bytes to be sent in standard single SPI mode. Note: Dummy bytes will be sent in standard single SPI mode.
*txBufferA pointer to the tx byte array to be transmitted. Should have tx_bytes elements.
txNumBytesThe total number of bytes to be transmitted including those in standard SPI.
*rxBufferA pointer to the rx byte array where received data will be stored. Should have rx_bytes elements.
rxNumBytesThe number of bytes to be received.
dummyNumBytesThe number of dummy bytes to be sent.
Return values
void

Definition at line 355 of file spi_driver.c.

◆ SPI_DualReceiveByte()

uint8_t SPI_DualReceiveByte ( )

Receives a byte along both MOSI and MISO and returns the value received.

Return values
uint8_tThe byte received in Little Endian format.

Definition at line 268 of file spi_driver.c.

◆ SPI_DualSendByte()

void SPI_DualSendByte ( uint8_t  transmittedByte)

Sends a byte along both MOSI and MISO.

Parameters
transmittedByteByte to be sent.
Return values
void

Definition at line 180 of file spi_driver.c.

◆ SPI_Exchange()

void SPI_Exchange ( uint8_t *  txBuffer,
uint32_t  txNumBytes,
uint8_t *  rxBuffer,
uint32_t  rxNumBytes,
uint32_t  dummyNumBytes 
)

Sends and receives bytes based on the function parameters. MISO and MOSI fill their standard SPI roles.

Parameters
*txBufferA pointer to the tx byte array to be transmitted. Should have tx_bytes elements.
txNumBytesThe number of bytes to be transmitted.
*rxBufferA pointer to the rx byte array where received data will be stored. Should have rx_bytes elements.
rxNumBytesThe number of bytes to be received.
dummyNumBytesThe number of dummy bytes to be sent.
Return values
void

Definition at line 325 of file spi_driver.c.

◆ SPI_JEDECReset()

void SPI_JEDECReset ( )

Performs a JEDEC reset on the SPI device.

Return values
void
Warning
There must be a short, user inserted, delay after resetting with a JEDEC reset.

Definition at line 492 of file spi_driver.c.

◆ SPI_PinClear()

void SPI_PinClear ( uint32_t  port,
uint32_t  pin 
)

Clears a given pin on a port to LOW.

Parameters
portThe port number.
pinThe pin number.
Return values
void

Definition at line 47 of file spi_driver.c.

◆ SPI_PinInit()

void SPI_PinInit ( uint32_t  port,
uint32_t  pin,
enum directionIO  direction 
)

Initializes a given pin as either an input or output.

Parameters
portThe port number. The type must be redefined based on the GPIO driver being used.
pinThe pin number.
directionINPUT or OUTPUT. See the directionIO enum type.
Return values
void

Definition at line 43 of file spi_driver.c.

◆ SPI_PinRead()

uint32_t SPI_PinRead ( uint32_t  port,
uint32_t  pin 
)

Reads the voltage on a given pin.

Parameters
portThe port number.
pinThe pin number.
Return values
32-bitword, 0 = LOW, 1 = HIGH.

Definition at line 56 of file spi_driver.c.

◆ SPI_PinSet()

void SPI_PinSet ( uint32_t  port,
uint32_t  pin 
)

Sets a given pin on a port to HIGH.

Parameters
portThe port number.
pinThe pin number.
Return values
void

Definition at line 51 of file spi_driver.c.

◆ SPI_QuadExchange()

void SPI_QuadExchange ( uint8_t  standardSPINumBytes,
uint8_t *  txBuffer,
uint32_t  txNumBytes,
uint8_t *  rxBuffer,
uint32_t  rxNumBytes,
uint32_t  dummyNumBytes 
)

Sends and receives bytes based on the function parameters. MOSI can used for the opcode and address, then MISO, MOSI, HOLDb, WPb are switched for transmission and receiving such that data is transmitted from/to the flash device along all 4 lines. Their state will then be reverted to original SPI functionality.

Parameters
standardSPINumBytesThe number of bytes to be sent in standard single SPI mode. Note: Dummy bytes will be sent in standard single SPI mode based on the input parameters.
*txBufferA pointer to the tx byte array to be transmitted. Should have tx_bytes elements.
txNumBytesThe total number of bytes to be transmitted including those in standard SPI.
*rxBufferA pointer to the rx byte array where received data will be stored. Should have rx_bytes elements.
rxNumBytesThe number of bytes to be received.
dummyNumBytesThe number of dummy bytes to be sent.
Return values
void

Definition at line 406 of file spi_driver.c.

◆ SPI_QuadReceiveByte()

uint8_t SPI_QuadReceiveByte ( )

Receives a byte along MOSI, MISO, HOLDb, and WPb and returns the value received.

Return values
uint8_tThe byte received in Little Endian format.

Definition at line 292 of file spi_driver.c.

◆ SPI_QuadSendByte()

void SPI_QuadSendByte ( uint8_t  transmittedByte)

Sends a byte along MISO, MOSI, WPb, and HOLDb.

Parameters
transmittedByteByte to be sent.
Return values
void

Definition at line 211 of file spi_driver.c.

◆ SPI_ReceiveByte()

uint8_t SPI_ReceiveByte ( )

Receives a byte along MISO and returns the value received.

Return values
uint8_tThe byte received in Little Endian format.

Definition at line 251 of file spi_driver.c.

◆ SPI_ReturnToSingleSPIIOs()

void SPI_ReturnToSingleSPIIOs ( )

Returns MISO and MOSI pins to their standard SPI state as an input and output.

Return values
void

Definition at line 85 of file spi_driver.c.

◆ SPI_SendBit()

void SPI_SendBit ( uint8_t  transmittedBit)

Sends a single bit along MOSI while toggling the clock.

Parameters
transmittedBitBit to be sent, either 1 or 0.
Return values
void

Definition at line 157 of file spi_driver.c.

◆ SPI_SendByte()

void SPI_SendByte ( uint8_t  transmittedByte)

Sends a byte along MOSI.

Parameters
transmittedByteByte to be sent.
Return values
void

Definition at line 170 of file spi_driver.c.

◆ SPI_Trigger()

void SPI_Trigger ( )

Triggers a falling edge on the SPI_TRIGGER_PORT/PIN output.

Return values
void

Definition at line 485 of file spi_driver.c.