yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
MSPM0 Smart Robot Car📖
1.Quick Start Guide
  • 1. Assembly Videos
  • 2. Quick Start Videos
  • 3. Battery charging
  • 4. Notes
2.Introduction to the MSPM0 Car
  • 1.About MSPM0
  • 2.Development board interface description
3.Development environment setup and usage
  • 1.CCS Theia development
  • 2.MDK Arm development
  • 3.Uniflash burning
  • 4.External crystal oscillator configuration
  • 5.Project Migration
4.MSPM0 basic course
  • 1. LED light control (GPIO)
  • 2. Buzzer Control (GPIO)
  • 3. Button Control (EXTI)
  • 4. Serial Port (UART)
  • 5. Voltage Detection (ADC)
  • 6. Temperature Detection (ADC)
  • 7. Independent Watchdog (WWDG)
  • 8. Window Watchdog (WWDG)
  • 9. Timer (TIM)
  • 10. Breathing LED (PWM)
  • 11. I2C Communication (I2C)
  • 12. External Flash (SPI)
  • 13. Internal Flash (Flash)
  • 14. DMA
  • 15. FreeRTOS
5.Sensor basic course
  • 1.Servo control
  • 2.Ultrasonic servo PTZ basic use
  • 3.Motor PID control
  • 4.OLED display photosensitive value
  • 5.Infrared avoid sensor
  • 6.1 Infrared line tracking sensor-8 channel infrared tracking sensor module
  • 6.2 Infrared line tracking sensor-4 channel infrared tracking sensor module
  • 7.Infrared remote control
  • 8.Ultrasonic ranging
  • 9.RGB light drive
  • 10.MPU6050 raw data acquisition
  • 11.MPU6050 data fusion
  • 12.K210 vision recognition module
  • 13.K230 vision recognition module
6.Smart car course
  • 1.1 Car line tracking-8 channel infrared tracking sensor module
  • 1.2 Car line tracking-4 channel infrared tracking sensor module
  • 2.1 Drawing a circle-8 channel infrared tracking sensor module
  • 2.2 Drawing a circle-4 channel infrared tracking sensor module
  • 3.Ultrasonic following
  • 4.Ultrasonic avoid
  • 5.Ultrasonic avoid(Servo PTZ)
  • 6.Infrared sensor avoid
  • 7.Light following
  • 8.Gyroscope calibration straight-line walk
  • 9.Infrared remote control
  • 10.Autonomous driving car
7.Vision application course
  • 1.K210 autonomous driving
  • 2.K210 color tracking
  • 3.K210 handwritten digit action
  • 4.K210 color response
  • 5.K210 road sign indication action
  • 6.K210 following machine code
  • 7.K210 QR code command
  • 8.K230 vision line tracking
  • 9.K230 color tracking
  • 10.K230 machine code tracking
  • 11.K230 QR code command
  • 12.K230 autonomous avoid
  • 13.K230 license plate tracking
  • 14.K230 face tracking
  • 15.K230 gaze direction tracking
  • 16.K230 palm tracking
  • 17.K230 target tracking
  • 18.K230 human body tracking
  • 19.K230 OCR character recognition
8.Extended application course (Sold separately)
  • 1.Voice interaction module communication
  • 2.Voice interaction control
  • 3.K230 road sign indication action(Voice)
  • 4.K230 color response(Voice)
  • 5.K230 gesture control(Voice)
  • 6.K230 rock-paper-scissors(Voice)
  • 7.8-channel grayscale color line tracking
  • 8.Bluetooth remote control
  • 9.K210 Scenario-based autonomous driving
  • 10.K230 Scenario-based autonomous driving
Download
  • Code
  • SCH
  • 3D Model File
  • MSPM0 Data Sheet
  • Voice firmware
  • Hardware interface manual
Download ZIP

Welcome to MSPM0 Smart Robot Car repository

12. External Flash (SPI)

External Flash (SPI)

External Flash (SPI)I. Learning ObjectivesII. SPI IntroductionIII. Hardware SetupIV. Environment SetupV. Main FunctionsVI. Experiment Phenomenon

I. Learning Objectives

  1. Learn and understand basic SPI communication knowledge.
  2. Learn to drive W25Q64 through SPI.

II. SPI Introduction

SPI protocol (Serial Peripheral Interface) is a commonly used synchronous serial communication protocol, specially designed for high-speed, short-distance communication between microcontrollers and external devices. It supports full-duplex communication, meaning data can be sent and received simultaneously between master and slave devices, commonly used for memory chips, sensors, display drivers, and wireless modules.

SPI Hardware Interface

SPI protocol typically uses four lines for data transmission:

  • SCLK (Serial Clock): Clock signal generated by master device, used for synchronizing communication.
  • MOSI/PICO (Master Output Slave Input): Master device sends data to slave device.
  • MISO/POCI (Master Input Slave Output): Slave device sends data to master device.
  • SS/CS (Slave Select): Master device activates target slave device through chip select signal.

SPI Advantages

  • High communication speed, suitable for high-bandwidth applications.
  • Supports full-duplex transmission, improving efficiency.
  • Simple hardware implementation, direct interface.
  • Supports multiple slave device connections.

SPI Disadvantages

  • Lacks standardized error detection mechanism.
  • Master device pin count increases with number of slave devices.
  • Communication distance is limited, typically used for onboard communication.

SPI Transmission Modes

Clock Polarity (CPOL): Controls the idle and active states of the clock signal (0: idle state low level, 1: idle state high level)

Clock Phase (CPHA): Controls data sampling and transmission timing (0: sample on odd edge, 1: sample on even edge)

Clock Polarity (CPOL)Clock Phase (CPHA) 
00SCL idle state low level; data sampled on rising edge, transmitted on falling edge
01SCL idle state low level; data sampled on falling edge, transmitted on rising edge
10SCL idle state high level; data sampled on falling edge, transmitted on rising edge
11SCL idle state high level; data sampled on rising edge, transmitted on falling edge

CPOL:0 CPHA:0

image-20251021194247402

CPOL:0 CPHA:1image-20251021194557026

CPOL:1 CPHA:0

image-20251021194643505

CPOL:1 CPHA:1

image-20251021194706485

III. Hardware Setup

W25Q64 is a common serial flash memory device that uses SPI (Serial Peripheral Interface) protocol. It has high-speed read, write, and erase functions, widely used in embedded systems, storage devices, routers, and other high-performance electronic devices. W25Q32 has a capacity of 64 Mbit (i.e., 8 MB). The numbers in the model represent different capacity options, such as W25Q16, W25Q32, W25Q128, etc., to meet the needs of different application scenarios.

W25Q64 divides 8M bytes of capacity into 128 blocks, each block size is 64K bytes, each block is divided into 16 sectors, each sector is 4K bytes. The minimum erase unit of W25Q64 is one sector, which means 4K bytes must be erased each time. Partial instruction table content is as follows

image-20231026182949170

This course requires no additional hardware equipment, directly using the onboard LCD screen and external storage flash on the MSPM0G3507 development board.

Connection relationship is as follows

image-20251021195438920

image-20251021195504892

IV. Environment Setup

First, we add configuration through sysconfig as follows

image-20251021195921026

image-20251021200028008

image-20251021200230145

Unlike the previous section, this section uses hardware I2C to drive the OLED screen

image-20251021200343922

image-20251021200421200

image-20251021200439789

V. Main Functions

Mainly introduces the user-written functional code, for detailed code, you can open the project files we provide and enter the Bsp folder to view the source code.

W25Qxx_Init

Function Prototypevoid W25Qxx_Init(void)
Function DescriptionInitialize W25Q64 Flash, main operation is to pull SPI_CS pin high and delay 300ms to ensure stable initialization
Input ParametersNone
Return ValueNone

spi_read_write_byte

Function Prototypeuint8_t spi_read_write_byte(uint8_t dat)
Function DescriptionComplete SPI single byte transmit/receive: first send input dat byte, wait for SPI bus idle, then receive byte returned from slave device
Input Parametersdat (uint8_t type, single byte data to be sent through SPI)
Return Valueuint8_t type, byte returned from slave device received by SPI bus

W25QXX_Self_Test

Function Prototypevoid W25QXX_Self_Test(void)
Function DescriptionFlash self-test: loop read Flash ID and identify model (W25Q32/W25Q64/W25Q128), display model through OLED and print via serial port; simultaneously test Flash read/write function (write "Yahboom" and verify), print read/write results
Input ParametersNone
Return ValueNone

W25Qxx_readID

Function Prototypeuint16_t W25Qxx_readID(void)
Function DescriptionRead W25Q64 device ID: send 0x90 instruction and 0x000000 address, receive 2-byte ID and concatenate to return
Input ParametersNone
Return Valueuint16_t type, Flash device ID (e.g., W25Q64 corresponding ID is 0xEF16)

W25Qxx_write_enable

Function Prototypevoid W25Qxx_write_enable(void)
Function DescriptionSend "write enable" instruction (0x06) to Flash, allowing subsequent write/erase operations
Input ParametersNone
Return ValueNone

W25Qxx_wait_busy

Function Prototypevoid W25Qxx_wait_busy(void)
Function DescriptionWait for Flash operation to complete: loop read status register, detect BUSY bit (lowest bit), until BUSY bit is 0 (indicating Flash is idle)
Input ParametersNone
Return ValueNone

W25Qxx_erase_sector

Function Prototypevoid W25Qxx_erase_sector(uint32_t addr)
Function DescriptionErase specified sector: first convert input address to sector address (1 sector = 4096 bytes), send sector erase instruction (0x20) and address, wait for erase completion
Input Parametersaddr (uint32_t type, sector number, note: actual erase address = addr×4096)
Return ValueNone

W25Qxx_write

Function Prototypevoid W25Qxx_write(uint8_t* buffer, uint32_t addr, uint16_t numbyte)
Function DescriptionWrite data to specified Flash address: first erase sector containing target address, send write enable, then send page write instruction (0x02) and address, finally continuously write numbyte bytes of data
Input Parametersbuffer (uint8_t * type, data source buffer to write) addr (uint32_t type, Flash target write address (24-bit)) numbyte (uint16_t type, total bytes to write)
Return ValueNone

W25Qxx_read

Function Prototypevoid W25Qxx_read(uint8_t* buffer, uint32_t read_addr, uint16_t read_length)
Function DescriptionRead data from specified Flash address: send read instruction (0x03) and target address, continuously read read_length bytes of data and store in buffer
Input Parametersbuffer (uint8_t * type, target buffer to store read data) read_addr (uint32_t type, Flash target read address (24-bit)) read_length (uint16_t type, total bytes to read)
Return ValueNone

OLED_Init

Function Prototypevoid OLED_Init(void)
Function DescriptionOLED initialization, execute SSD1306 initialization commands, clear screen and update display, set cursor default values, finally display "OLED init Success!"
Input ParametersNone
Return ValueNone

SSD1306_UpdateScreen

Function Prototypevoid SSD1306_UpdateScreen(void)
Function DescriptionOLED screen update display, loop write SSD1306_Buffer data to screen
Input ParametersNone
Return ValueNone

SSD1306_Fill

Function Prototypevoid SSD1306_Fill(SSD1306_COLOR_t color)
Function DescriptionOLED screen clear (not refreshed display), fill SSD1306_Buffer with all black or all white according to color
Input Parameterscolor (SSD1306_COLOR_BLACK or SSD1306_COLOR_WHITE)
Return ValueNone

SSD1306_DrawPixel

Function Prototypevoid SSD1306_DrawPixel(uint16_t x, uint16_t y, SSD1306_COLOR_t color)
Function DescriptionDraw pixel at specified coordinates (x,y), set SSD1306_Buffer according to color and whether inverted
Input Parametersx (horizontal coordinate), y (vertical coordinate), color (SSD1306_COLOR_BLACK or SSD1306_COLOR_WHITE)
Return ValueNone

SSD1306_GotoXY

Function Prototypevoid SSD1306_GotoXY(uint16_t x, uint16_t y)
Function DescriptionSet current cursor position
Input Parametersx (horizontal coordinate), y (vertical coordinate)
Return ValueNone

SSD1306_Putc

Function Prototypechar SSD1306_Putc(char ch, FontDef_t *Font, SSD1306_COLOR_t color)
Function DescriptionDraw single character, draw character at current cursor position according to font and color and update cursor
Input Parametersch (character to draw), Font (font structure pointer), color (character color)
Return ValueReturn drawn character on success, return error character on failure

SSD1306_Puts

Function Prototypechar SSD1306_Puts(char *str, FontDef_t *Font, SSD1306_COLOR_t color)
Function DescriptionDraw string, call SSD1306_Putc one by one to draw each character in the string
Input Parametersstr (pointer to string to draw), Font (font structure pointer), color (character color)
Return ValueReturn '\0' at end of string on success, return error character on failure

OLED_ON

Function Prototypevoid OLED_ON(void)
Function DescriptionWake OLED from sleep, send wake command
Input ParametersNone
Return ValueNone

OLED_OFF

Function Prototypevoid OLED_OFF(void)
Function DescriptionPut OLED to sleep (very low power consumption in sleep mode), send sleep command
Input ParametersNone
Return ValueNone

OLED_Clear

Function Prototypevoid OLED_Clear(void)
Function DescriptionOLED clear screen, call SSD1306_Fill to fill with black
Input ParametersNone
Return ValueNone

OLED_Refresh

Function Prototypevoid OLED_Refresh(void)
Function DescriptionRefresh OLED screen, call SSD1306_UpdateScreen
Input ParametersNone
Return ValueNone

OLED_Draw_String

Function Prototypevoid OLED_Draw_String(char *data, uint8_t x, uint8_t y, bool clear, bool refresh)
Function DescriptionWrite characters, clear screen according to parameters, set cursor position to draw string, refresh screen
Input Parametersdata (string to display), x (horizontal coordinate), y (vertical coordinate), clear (whether to clear screen), refresh (whether to refresh)
Return ValueNone

OLED_Draw_Line

Function Prototypevoid OLED_Draw_Line(char *data, uint8_t line, bool clear, bool refresh)
Function DescriptionWrite a line of characters, call OLED_Draw_String to display on corresponding line according to line number (1-3)
Input Parametersdata (string to display), line (line number, 1-3), clear (whether to clear screen), refresh (whether to refresh)
Return ValueNone

SSD1306_DrawLine

Function Prototypevoid SSD1306_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, SSD1306_COLOR_t c)
Function DescriptionDraw line segment, draw line segment between (x0,y0) to (x1,y1)
Input Parametersx0 (start horizontal coordinate), y0 (start vertical coordinate), x1 (end horizontal coordinate), y1 (end vertical coordinate), c (line segment color)
Return ValueNone

VI. Experiment Phenomenon

After the program is flashed, it will run the flash self-test program, write "yahboom" to flash, then read flash content. If it is "yahboom", the OLED screen will display "Flash RDWT OK!"

image-20251022142748420

I2C Communication (I2C)
Internal Flash (Flash)

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文