Skip to content

General SPI Operation External Flash Example*

Tip

1. Overview*

This app uses the general SPI driver API to operate an external flash. It introduces the API usage through test cases, including read/write/erase/write-protect/OTP operations.

2. Features*

  • Refer to the app_init() function to see how to initialize SPI
  • Refer to the flash_complete_test() function to see read/write/erase calls
  • Refer to the flash_write_protect_test() function to see write-protect usage
  • Refer to the flash_otp_test() function to see how to perform read/write/erase operations on the OTP area
  • Call gx_genspi_for_flash_otp_lock() to lock the OTP; after locking, read/write/erase operations on this area will no longer be possible

3. Directory Structure*

app/general_spi_flash
├── app.c
├── app.mk
├── app.name
├── Kconfig
└── README.md
File Description
app.c Defines the OVP_APP structure, implements standard callbacks, and calls OVP_REGISTER_APP() to register
app.mk Compiles app.c, controlled by CONFIG_GENERAL_SPI_FLASH
app.name Defines the CONFIG_GENERAL_SPI_FLASH option, displayed as "General spi flash" in menuconfig
Kconfig Empty framework; this App has no additional configuration items

4. Configuration*

This App has no additional Kconfig sub-configurations or macro definitions. Simply select "General spi flash" in menuconfig.

To adjust the log level, modify the LOG_LVL macro in app.c:

#define LOG_LVL 3   // 0: no log, 1: ERROR, 2: WARN, 3: INFO, 4: DEBUG

5. Working Principle*

5.1 Initialization Flow*

app_init() sets the SPI module frequency, configures pin multiplexing, and initializes the SPI device.

5.2 Main Loop*

app_task_loop() sequentially performs the test calls.

6. Usage*

Note

Not all flash models are supported. The p25q80l NOR Flash can be used for testing. Contact NationalChip for support of other models.

Ensure the hardware connections are correct. The current software uses the following hardware connections:

GPIO Function
11 SPI2_CS
12 SPI2_CLK
13 SPI2_MOSI
14 SPI2_MISO

6.1 Compilation*

# Use the hello_world configuration
cp configs/example/app/8006_genspi_app.config .config
make defconfig
make clean; make

6.2 Flashing and Running*

cd tools/bootx/
./flash_nor.sh 0 -r 1000000

7. Expected Output*

UART0 (921600 baud rate) outputs the following log:

[APP_GENERAL_SPI_FLASH] [I]  ---- app_init, 776 ----
[APP_GENERAL_SPI_FLASH] [I] cpu freq = 172032000 Hz
[APP_GENERAL_SPI_FLASH] [I] spi freq = 172032000 Hz
spi_hw_init: freq_src=172032000, clk_div=4, speed_hz=43008000
NOR Flash, model: p25q80l, size: 1 MB
[APP_GENERAL_SPI_FLASH] [I] genspi flash init success
[APP_GENERAL_SPI_FLASH] [I] ========== [TEST] comtest start ==========
[APP_GENERAL_SPI_FLASH] [I] >>>>> sflash_test: addr=0x0, len=0x1
[APP_GENERAL_SPI_FLASH] [I] >>>>> sflash_test_success.
[APP_GENERAL_SPI_FLASH] [I] sample_delay best value: 6
[APP_GENERAL_SPI_FLASH] [I] ==============================
......
[APP_GENERAL_SPI_FLASH] [I] ========== [TEST] sample delay test PASS ==========
[APP_GENERAL_SPI_FLASH] [I] ========== [TEST] uid test start ==========
[APP_GENERAL_SPI_FLASH] [I] flash uid len:16, uid:
00000000: 42 50 31 32 35 36 34 0c 00 50 a9 26 56 04 e0 78 | BP12564..P.&V..x
[APP_GENERAL_SPI_FLASH] [I] ========== [TEST] uid test PASS ==========
[APP_GENERAL_SPI_FLASH] [I] ========================================
[APP_GENERAL_SPI_FLASH] [I]   All flash tests completed!
[APP_GENERAL_SPI_FLASH] [I]   Test results summary:
[APP_GENERAL_SPI_FLASH] [I]   --------------------  ------
[APP_GENERAL_SPI_FLASH] [I]   idle                : PASS
[APP_GENERAL_SPI_FLASH] [I]   init                : PASS
[APP_GENERAL_SPI_FLASH] [I]   comtest             : PASS
[APP_GENERAL_SPI_FLASH] [I]   write protect       : PASS
[APP_GENERAL_SPI_FLASH] [I]   otp                 : FAILED
[APP_GENERAL_SPI_FLASH] [I]   sample delay        : PASS
[APP_GENERAL_SPI_FLASH] [I]   uid                 : PASS
[APP_GENERAL_SPI_FLASH] [I]   --------------------  ------
[APP_GENERAL_SPI_FLASH] [I]   Final result        : FAILED
[APP_GENERAL_SPI_FLASH] [I] ========================================

8. Notes*

  1. Call gx_genspi_for_flash_init() to initialize the flash. The speed setting should not exceed 43008000 (¼ of the CPU frequency) to avoid untimely CPU data reads.
  2. Pay special attention to ensure the comtest read/write/erase operations work correctly.
  3. The OTP can be locked using gx_genspi_for_flash_otp_lock(). However, after locking, read/write/erase operations will no longer be possible. This may cause the test item to fail, which is normal behavior.
  4. Write protection can be set from address 0 to a specified length, but not all lengths are supported. After setting, use gx_genspi_for_flash_write_protect_status() to obtain the actual protected region length.