SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions
sys-spi-nor.h File Reference
#include <io.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <types.h>
#include <sys-clk.h>
#include <sys-gpio.h>
#include <sys-spi.h>
#include <log.h>
Include dependency graph for sys-spi-nor.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sfdp_header
 
struct  sfdp_parameter_header
 
struct  sfdp_basic_table
 
struct  sfdp
 
struct  spi_nor_info
 

Macros

#define SFDP_MAX_NPH   (6)
 

Typedefs

typedef struct sfdp_header sfdp_header_t
 
typedef struct sfdp_parameter_header sfdp_parameter_header_t
 
typedef struct sfdp_basic_table sfdp_basic_table_t
 
typedef struct sfdp sfdp_t
 
typedef struct spi_nor_info spi_nor_info_t
 

Enumerations

enum  SPI_NOR_OPS {
  NOR_OPCODE_SFDP = 0x5a , NOR_OPCODE_RDID = 0x9f , NOR_OPCODE_WRSR = 0x01 , NOR_OPCODE_RDSR = 0x05 ,
  NOR_OPCODE_WREN = 0x06 , NOR_OPCODE_READ = 0x03 , NOR_OPCODE_PROG = 0x02 , NOR_OPCODE_E4K = 0x20 ,
  NOR_OPCODE_E32K = 0x52 , NOR_OPCODE_E64K = 0xd8 , NOR_OPCODE_ENTER_4B = 0xb7 , NOR_OPCODE_EXIT_4B = 0xe9
}
 Enumeration of SPI NOR Flash operation opcodes. More...
 
enum  SPI_CMD_OPS {
  SPI_CMD_END = 0x00 , SPI_CMD_INIT = 0x01 , SPI_CMD_SELECT = 0x02 , SPI_CMD_DESELECT = 0x03 ,
  SPI_CMD_FAST = 0x04 , SPI_CMD_TXBUF = 0x05 , SPI_CMD_RXBUF = 0x06 , SPI_CMD_SPINOR_WAIT = 0x07 ,
  SPI_CMD_SPINAND_WAIT = 0x08
}
 Enumeration of SPI command operations. More...
 

Functions

int spi_nor_detect (sunxi_spi_t *spi)
 Detects the presence of an SPI NOR flash chip.
 
uint32_t spi_nor_read_block (sunxi_spi_t *spi, uint8_t *buf, uint32_t blk_no, uint32_t blk_cnt)
 Reads a block or multiple blocks of data from the SPI NAND flash memory.
 
uint32_t spi_nor_read (sunxi_spi_t *spi, uint8_t *buf, uint32_t addr, uint32_t rxlen)
 Reads data from the SPI NOR flash memory.
 

Macro Definition Documentation

◆ SFDP_MAX_NPH

#define SFDP_MAX_NPH   (6)

Typedef Documentation

◆ sfdp_basic_table_t

◆ sfdp_header_t

typedef struct sfdp_header sfdp_header_t

◆ sfdp_parameter_header_t

◆ sfdp_t

typedef struct sfdp sfdp_t

◆ spi_nor_info_t

typedef struct spi_nor_info spi_nor_info_t

Enumeration Type Documentation

◆ SPI_CMD_OPS

Enumeration of SPI command operations.

This enumeration defines a set of commands used to control the behavior of SPI operations. These commands may include initialization, selection, deselection, and data transmission commands to facilitate communication with peripheral devices.

Enumerator
SPI_CMD_END 

End Command: Mark the end of a command sequence.

SPI_CMD_INIT 

Initialization Command: Initialize the SPI device.

SPI_CMD_SELECT 

Select Command: Select the SPI device for communication.

SPI_CMD_DESELECT 

Deselect Command: Deselect the SPI device to end communication.

SPI_CMD_FAST 

Fast Command: Execute a fast operation or mode.

SPI_CMD_TXBUF 

Transmit Buffer Command: Transmit data from a buffer to the SPI device.

SPI_CMD_RXBUF 

Receive Buffer Command: Receive data from the SPI device to a buffer.

SPI_CMD_SPINOR_WAIT 

Wait Command for SPI NOR: Wait for the SPI NOR flash to complete its operation.

SPI_CMD_SPINAND_WAIT 

Wait Command for SPI NAND: Wait for the SPI NAND flash to complete its operation.

◆ SPI_NOR_OPS

Enumeration of SPI NOR Flash operation opcodes.

This enumeration defines the opcodes used for various operations on SPI NOR Flash memory. Each opcode corresponds to a specific command that can be issued to the NOR Flash for performing read, write, erase, and other operational tasks.

Enumerator
NOR_OPCODE_SFDP 

SFDP Read Command: Read Serial Flash Discoverable Parameters.

NOR_OPCODE_RDID 

Read ID Command: Retrieve the identity of the memory device.

NOR_OPCODE_WRSR 

Write Status Register Command: Write to the status register.

NOR_OPCODE_RDSR 

Read Status Register Command: Read the current status register.

NOR_OPCODE_WREN 

Write Enable Command: Enable write operations on the memory.

NOR_OPCODE_READ 

Read Data Command: Read data from the memory.

NOR_OPCODE_PROG 

Page Program Command: Program data into a memory page.

NOR_OPCODE_E4K 

4K Block Erase Command: Erase a 4K block of memory

NOR_OPCODE_E32K 

32K Block Erase Command: Erase a 32K block of memory

NOR_OPCODE_E64K 

64K Block Erase Command: Erase a 64K block of memory

NOR_OPCODE_ENTER_4B 

Enter 4-Byte Address Mode Command: Switch to 4-byte addressing mode.

NOR_OPCODE_EXIT_4B 

Exit 4-Byte Address Mode Command: Return to 3-byte addressing mode.

Function Documentation

◆ spi_nor_detect()

int spi_nor_detect ( sunxi_spi_t spi)

Detects the presence of an SPI NOR flash chip.

This function attempts to identify and initialize the SPI NOR flash chip connected to the specified SPI interface. It resets the chip, waits for it to be ready, and retrieves its information. If successful, it logs the chip ID and its capacity.

Parameters
[in]spiPointer to the SPI interface structure. This should be initialized and configured before calling this function.
Returns
  • 0 on successful detection and initialization of the SPI NOR chip.
  • -1 if no supported SPI NOR chip is found.

    The function performs the following steps:

    1. Resets the SPI NOR chip.
    2. Waits until the chip is not busy.
    3. Checks the chip information. If no supported chip is found, a warning is logged and the function returns -1.
    4. If a chip is detected, its ID and capacity are logged to inform the user.

◆ spi_nor_read()

uint32_t spi_nor_read ( sunxi_spi_t spi,
uint8_t buf,
uint32_t  addr,
uint32_t  rxlen 
)

Reads data from the SPI NOR flash memory.

This function reads a specified length of data from a given address in the SPI NOR flash memory into a provided buffer. The reading is performed in blocks, and it handles cases where the read address is not aligned to the block size.

Parameters
[in]spiPointer to the SPI interface structure.
[out]bufPointer to the buffer where the read data will be stored.
[in]addrThe starting address from which to read data in the SPI NOR.
[in]rxlenThe number of bytes to read from the SPI NOR.
Returns
The number of bytes successfully read from the SPI NOR. This can be less than rxlen if an error occurs or if the end of the memory space is reached.
Note
This function assumes that the buffer provided by the caller is large enough to accommodate the data being read.

The function first checks if the read address is misaligned with the block size. If so, it reads a partial block. Then, it reads as many complete blocks as possible before potentially reading another partial block at the end.

◆ spi_nor_read_block()

uint32_t spi_nor_read_block ( sunxi_spi_t spi,
uint8_t buf,
uint32_t  blk_no,
uint32_t  blk_cnt 
)

Reads a block or multiple blocks of data from the SPI NAND flash memory.

This function reads one or more contiguous blocks from the SPI NAND flash memory into a provided buffer. It handles reading the data in chunks defined by the read granularity and performs the necessary address calculations to handle multiple blocks.

Parameters
[in]spiPointer to the SPI interface structure.
[out]bufPointer to the buffer where the read data will be stored.
[in]blk_noThe starting block number from which to read data.
[in]blk_cntThe number of blocks to read from the SPI NAND.
Returns
The number of blocks successfully read. In case of an error, it will return the requested block count, indicating the operation was completed.
Note
This function assumes that the buffer provided by the caller is large enough to accommodate the data being read. If the block count is large, ensure the buffer has sufficient space for all the blocks.

The function reads data from the SPI NAND flash memory in chunks based on the configured read granularity (info.read_granularity). It waits for the SPI bus to be ready before each read operation. The function will continue reading until the requested number of blocks has been fetched. If the data to be read exceeds the maximum read length (0x7FFFFFFF bytes), it adjusts the size of each read operation accordingly.