Prusa MINI Firmware overview
w25x.c File Reference
#include <w25x.h>
#include "main.h"

Macros

#define _MFRID   0xEF
 
#define _DEVID   0x13
 
#define _DEVID_NEW   0x16
 
#define _CMD_ENABLE_WR   0x06
 
#define _CMD_ENABLE_WR_VSR   0x50
 
#define _CMD_DISABLE_WR   0x04
 
#define _CMD_RD_STATUS_REG   0x05
 
#define _CMD_WR_STATUS_REG   0x01
 
#define _CMD_RD_DATA   0x03
 
#define _CMD_RD_FAST   0x0b
 
#define _CMD_RD_FAST_D_O   0x3b
 
#define _CMD_RD_FAST_D_IO   0xbb
 
#define _CMD_PAGE_PROGRAM   0x02
 
#define _CMD_SECTOR_ERASE   0x20
 
#define _CMD_BLOCK32_ERASE   0x52
 
#define _CMD_BLOCK64_ERASE   0xd8
 
#define _CMD_CHIP_ERASE   0xc7
 
#define _CMD_CHIP_ERASE2   0x60
 
#define _CMD_PWR_DOWN   0xb9
 
#define _CMD_PWR_DOWN_REL   0xab
 
#define _CMD_MFRID_DEVID   0x90
 
#define _CMD_MFRID_DEVID_D   0x92
 
#define _CMD_JEDEC_ID   0x9f
 
#define _CMD_RD_UID   0x4b
 
#define _CS_LOW()   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, 0)
 
#define _CS_HIGH()   HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, 1)
 
#define _SPI_TX(b)   spi_txrx(b)
 
#define _SPI_RX()   spi_txrx(0xff)
 

Functions

static uint8_t spi_txrx (uint8_t tx)
 
int w25x_mfrid_devid (void)
 
int8_t w25x_init (void)
 
void w25x_enable_wr (void)
 
void w25x_disable_wr (void)
 
uint8_t w25x_rd_status_reg (void)
 
void w25x_wr_status_reg (uint8_t val)
 
void w25x_rd_data (uint32_t addr, uint8_t *data, uint16_t cnt)
 
void w25x_page_program (uint32_t addr, uint8_t *data, uint16_t cnt)
 
void w25x_page_program_P (uint32_t addr, uint8_t *data, uint16_t cnt)
 
void w25x_erase (uint8_t cmd, uint32_t addr)
 
void w25x_sector_erase (uint32_t addr)
 
void w25x_block32_erase (uint32_t addr)
 
void w25x_block64_erase (uint32_t addr)
 
void w25x_chip_erase (void)
 
void w25x_rd_uid (uint8_t *uid)
 
void w25x_wait_busy (void)
 

Variables

SPI_HandleTypeDef hspi3
 

Macro Definition Documentation

◆ _MFRID

#define _MFRID   0xEF

◆ _DEVID

#define _DEVID   0x13

◆ _DEVID_NEW

#define _DEVID_NEW   0x16

◆ _CMD_ENABLE_WR

#define _CMD_ENABLE_WR   0x06

◆ _CMD_ENABLE_WR_VSR

#define _CMD_ENABLE_WR_VSR   0x50

◆ _CMD_DISABLE_WR

#define _CMD_DISABLE_WR   0x04

◆ _CMD_RD_STATUS_REG

#define _CMD_RD_STATUS_REG   0x05

◆ _CMD_WR_STATUS_REG

#define _CMD_WR_STATUS_REG   0x01

◆ _CMD_RD_DATA

#define _CMD_RD_DATA   0x03

◆ _CMD_RD_FAST

#define _CMD_RD_FAST   0x0b

◆ _CMD_RD_FAST_D_O

#define _CMD_RD_FAST_D_O   0x3b

◆ _CMD_RD_FAST_D_IO

#define _CMD_RD_FAST_D_IO   0xbb

◆ _CMD_PAGE_PROGRAM

#define _CMD_PAGE_PROGRAM   0x02

◆ _CMD_SECTOR_ERASE

#define _CMD_SECTOR_ERASE   0x20

◆ _CMD_BLOCK32_ERASE

#define _CMD_BLOCK32_ERASE   0x52

◆ _CMD_BLOCK64_ERASE

#define _CMD_BLOCK64_ERASE   0xd8

◆ _CMD_CHIP_ERASE

#define _CMD_CHIP_ERASE   0xc7

◆ _CMD_CHIP_ERASE2

#define _CMD_CHIP_ERASE2   0x60

◆ _CMD_PWR_DOWN

#define _CMD_PWR_DOWN   0xb9

◆ _CMD_PWR_DOWN_REL

#define _CMD_PWR_DOWN_REL   0xab

◆ _CMD_MFRID_DEVID

#define _CMD_MFRID_DEVID   0x90

◆ _CMD_MFRID_DEVID_D

#define _CMD_MFRID_DEVID_D   0x92

◆ _CMD_JEDEC_ID

#define _CMD_JEDEC_ID   0x9f

◆ _CMD_RD_UID

#define _CMD_RD_UID   0x4b

◆ _CS_LOW

#define _CS_LOW ( )    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, 0)

◆ _CS_HIGH

#define _CS_HIGH ( )    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, 1)

◆ _SPI_TX

#define _SPI_TX (   b)    spi_txrx(b)

◆ _SPI_RX

#define _SPI_RX ( )    spi_txrx(0xff)

Function Documentation

◆ spi_txrx()

static uint8_t spi_txrx ( uint8_t  tx)
static
49  {
50  uint8_t _in = 0;
52  return _in;
53 }
Here is the call graph for this function:

◆ w25x_mfrid_devid()

int w25x_mfrid_devid ( void  )
165  {
166  _CS_LOW();
167  _SPI_TX(_CMD_MFRID_DEVID); // send command 0x90
168  uint8_t cnt = 3; // 3 address bytes
169  while (cnt--) // send address bytes
170  _SPI_TX(0x00);
171  uint8_t w25x_mfrid = _SPI_RX(); // receive mfrid
172  uint8_t w25x_devid = _SPI_RX(); // receive devid
173  _CS_HIGH();
174  return ((w25x_mfrid == _MFRID) && ((w25x_devid == _DEVID) || (w25x_devid == _DEVID_NEW)));
175 }
Here is the caller graph for this function:

◆ w25x_init()

int8_t w25x_init ( void  )
57  {
58  //PIN_OUT(W25X20CL_PIN_CS);
59  _CS_HIGH();
61  if (!w25x_mfrid_devid())
62  return 0;
63  return 1;
64 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ w25x_enable_wr()

void w25x_enable_wr ( void  )
66  {
67  _CS_LOW();
68  _SPI_TX(_CMD_ENABLE_WR); // send command 0x06
69  _CS_HIGH();
70 }

◆ w25x_disable_wr()

void w25x_disable_wr ( void  )
72  {
73  _CS_LOW();
74  _SPI_TX(_CMD_DISABLE_WR); // send command 0x04
75  _CS_HIGH();
76 }

◆ w25x_rd_status_reg()

uint8_t w25x_rd_status_reg ( void  )
78  {
79  _CS_LOW();
80  _SPI_TX(_CMD_RD_STATUS_REG); // send command 0x90
81  uint8_t val = _SPI_RX(); // receive value
82  _CS_HIGH();
83  return val;
84 }
Here is the caller graph for this function:

◆ w25x_wr_status_reg()

void w25x_wr_status_reg ( uint8_t  val)
86  {
87  _CS_LOW();
88  _SPI_TX(_CMD_WR_STATUS_REG); // send command 0x90
89  _SPI_TX(val); // send value
90  _CS_HIGH();
91 }

◆ w25x_rd_data()

void w25x_rd_data ( uint32_t  addr,
uint8_t data,
uint16_t  cnt 
)
93  {
94  _CS_LOW();
95  _SPI_TX(_CMD_RD_DATA); // send command 0x03
96  _SPI_TX(((uint8_t *)&addr)[2]); // send addr bits 16..23
97  _SPI_TX(((uint8_t *)&addr)[1]); // send addr bits 8..15
98  _SPI_TX(((uint8_t *)&addr)[0]); // send addr bits 0..7
99  while (cnt--) // receive data
100  *(data++) = _SPI_RX();
101  _CS_HIGH();
102 }

◆ w25x_page_program()

void w25x_page_program ( uint32_t  addr,
uint8_t data,
uint16_t  cnt 
)
104  {
105  _CS_LOW();
106  _SPI_TX(_CMD_PAGE_PROGRAM); // send command 0x02
107  _SPI_TX(((uint8_t *)&addr)[2]); // send addr bits 16..23
108  _SPI_TX(((uint8_t *)&addr)[1]); // send addr bits 8..15
109  _SPI_TX(((uint8_t *)&addr)[0]); // send addr bits 0..7
110  while (cnt--) // send data
111  _SPI_TX(*(data++));
112  _CS_HIGH();
113 }

◆ w25x_page_program_P()

void w25x_page_program_P ( uint32_t  addr,
uint8_t data,
uint16_t  cnt 
)
115  {
116  _CS_LOW();
117  _SPI_TX(_CMD_PAGE_PROGRAM); // send command 0x02
118  _SPI_TX(((uint8_t *)&addr)[2]); // send addr bits 16..23
119  _SPI_TX(((uint8_t *)&addr)[1]); // send addr bits 8..15
120  _SPI_TX(((uint8_t *)&addr)[0]); // send addr bits 0..7
121  while (cnt--) // send data
122  _SPI_TX(*(data++));
123  _CS_HIGH();
124 }

◆ w25x_erase()

void w25x_erase ( uint8_t  cmd,
uint32_t  addr 
)
126  {
127  _CS_LOW();
128  _SPI_TX(cmd); // send command 0x20
129  _SPI_TX(((uint8_t *)&addr)[2]); // send addr bits 16..23
130  _SPI_TX(((uint8_t *)&addr)[1]); // send addr bits 8..15
131  _SPI_TX(((uint8_t *)&addr)[0]); // send addr bits 0..7
132  _CS_HIGH();
133 }
Here is the caller graph for this function:

◆ w25x_sector_erase()

void w25x_sector_erase ( uint32_t  addr)
135  {
136  return w25x_erase(_CMD_SECTOR_ERASE, addr);
137 }
Here is the call graph for this function:

◆ w25x_block32_erase()

void w25x_block32_erase ( uint32_t  addr)
139  {
140  return w25x_erase(_CMD_BLOCK32_ERASE, addr);
141 }
Here is the call graph for this function:

◆ w25x_block64_erase()

void w25x_block64_erase ( uint32_t  addr)
143  {
144  return w25x_erase(_CMD_BLOCK64_ERASE, addr);
145 }
Here is the call graph for this function:

◆ w25x_chip_erase()

void w25x_chip_erase ( void  )
147  {
148  _CS_LOW();
149  _SPI_TX(_CMD_CHIP_ERASE); // send command 0xc7
150  _CS_HIGH();
151 }

◆ w25x_rd_uid()

void w25x_rd_uid ( uint8_t uid)
153  {
154  _CS_LOW();
155  _SPI_TX(_CMD_RD_UID); // send command 0x4b
156  uint8_t cnt = 4; // 4 dummy bytes
157  while (cnt--) // receive dummy bytes
158  _SPI_RX();
159  cnt = 8; // 8 bytes UID
160  while (cnt--) // receive UID
161  uid[7 - cnt] = _SPI_RX();
162  _CS_HIGH();
163 }

◆ w25x_wait_busy()

void w25x_wait_busy ( void  )
177  {
179  ;
180 }
Here is the call graph for this function:

Variable Documentation

◆ hspi3

_CMD_MFRID_DEVID
#define _CMD_MFRID_DEVID
Definition: w25x.c:34
_CMD_PAGE_PROGRAM
#define _CMD_PAGE_PROGRAM
Definition: w25x.c:26
_CMD_RD_UID
#define _CMD_RD_UID
Definition: w25x.c:37
_CMD_RD_STATUS_REG
#define _CMD_RD_STATUS_REG
Definition: w25x.c:20
_SPI_RX
#define _SPI_RX()
Definition: w25x.c:45
data
uint8_t data[8]
Definition: masstorage.h:49
hspi3
SPI_HandleTypeDef hspi3
Definition: main.c:82
_DEVID_NEW
#define _DEVID_NEW
Definition: w25x.c:15
w25x_mfrid_devid
int w25x_mfrid_devid(void)
Definition: w25x.c:165
_CMD_WR_STATUS_REG
#define _CMD_WR_STATUS_REG
Definition: w25x.c:21
W25X_SPI_ENTER
#define W25X_SPI_ENTER()
Definition: w25x.h:14
w25x_erase
void w25x_erase(uint8_t cmd, uint32_t addr)
Definition: w25x.c:126
_DEVID
#define _DEVID
Definition: w25x.c:14
_CMD_BLOCK64_ERASE
#define _CMD_BLOCK64_ERASE
Definition: w25x.c:29
_CMD_CHIP_ERASE
#define _CMD_CHIP_ERASE
Definition: w25x.c:30
w25x_rd_status_reg
uint8_t w25x_rd_status_reg(void)
Definition: w25x.c:78
_CS_HIGH
#define _CS_HIGH()
Definition: w25x.c:40
_MFRID
#define _MFRID
Definition: w25x.c:13
uint8_t
const uint8_t[]
Definition: 404_html.c:3
W25X_STATUS_BUSY
#define W25X_STATUS_BUSY
Definition: w25x.h:7
_CMD_ENABLE_WR
#define _CMD_ENABLE_WR
Definition: w25x.c:17
_CMD_BLOCK32_ERASE
#define _CMD_BLOCK32_ERASE
Definition: w25x.c:28
_CS_LOW
#define _CS_LOW()
Definition: w25x.c:39
_CMD_DISABLE_WR
#define _CMD_DISABLE_WR
Definition: w25x.c:19
HAL_MAX_DELAY
#define HAL_MAX_DELAY
Definition: stm32f4xx_hal_def.h:76
HAL_SPI_TransmitReceive
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
_CMD_SECTOR_ERASE
#define _CMD_SECTOR_ERASE
Definition: w25x.c:27
_SPI_TX
#define _SPI_TX(b)
Definition: w25x.c:44
_CMD_RD_DATA
#define _CMD_RD_DATA
Definition: w25x.c:22