Skip to content

Commit 3d6369f

Browse files
committed
ADDS sector erase
1 parent 65ee0f6 commit 3d6369f

File tree

12 files changed

+5711
-6774
lines changed

12 files changed

+5711
-6774
lines changed

playground/stm32/blackpill-winbond-flash-memory/Core/Inc/W25Qxx.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ extern SPI_HandleTypeDef hspi3;
1414
#define READ_DATA_4_BYTE_INSTR (0x13)
1515
#define FAST_READ_DATA_3_BYTE_INSTR (0x0B)
1616
#define FAST_READ_DATA_4_BYTE_INSTR (0x0C)
17+
#define ENABLE_WRITE (0x06)
18+
#define DISABLE_WRITE (0x04)
19+
#define ERASE_SECTOR_3_BYTE_INSTR (0x20)
20+
#define ERASE_SECTOR_4_BYTE_INSTR (0x21)
1721

1822
#define W25Q_SPI hspi3
1923
#define NUM_BLOCKS (32)
@@ -53,6 +57,16 @@ void external_flash_spi_read(uint8_t* data, uint8_t len);
5357
*/
5458
void external_flash_reset(void);
5559

60+
/**
61+
* @brief This function enables writing to the flash memory
62+
*/
63+
void external_flash_enable_write(void);
64+
65+
/**
66+
* @brief This function disables writing to the flash memory
67+
*/
68+
void external_flash_disable_write(void);
69+
5670
/**
5771
* @brief This function read the flash memory ID
5872
*/
@@ -73,6 +87,11 @@ void external_flash_read(uint32_t start_page, uint8_t offset, uint32_t size, uin
7387
*/
7488
void external_flash_fast_read(uint32_t start_page, uint8_t offset, uint32_t size, uint8_t *buffer);
7589

90+
/**
91+
* @brief This function sets a sector of memory to 0xFF
92+
*/
93+
void external_flash_erase_sector(uint16_t num_sector);
94+
7695

7796
uint8_t external_flash_test();
7897

playground/stm32/blackpill-winbond-flash-memory/Core/Src/W25Qxx.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,109 @@ void external_flash_fast_read(uint32_t start_page, uint8_t offset, uint32_t size
121121

122122
cs_high();
123123
}
124+
125+
/**
126+
* @brief This function enables writing to the flash memory
127+
* Write is automatically disabled after power-up
128+
*/
129+
void external_flash_enable_write(void) {
130+
uint8_t t_data = ENABLE_WRITE;
131+
cs_low();
132+
external_flash_spi_write(&t_data, 1);
133+
cs_high();
134+
external_flash_delay(5);
135+
}
136+
137+
/**
138+
* @brief This function disables writing to the flash memory
139+
*/
140+
void external_flash_disable_write(void) {
141+
uint8_t t_data = DISABLE_WRITE;
142+
cs_low();
143+
external_flash_spi_write(&t_data, 1);
144+
cs_high();
145+
external_flash_delay(5);
146+
}
147+
148+
/**
149+
* @brief This function sets a sector of memory to 0xFF
150+
*/
151+
void external_flash_erase_sector(uint16_t num_sector) {
152+
uint8_t t_data[6];
153+
uint32_t mem_addr = num_sector * 16 * 256;
154+
155+
external_flash_enable_write();
156+
157+
if (NUM_BLOCKS < 512) {
158+
t_data[0] = ERASE_SECTOR_3_BYTE_INSTR;
159+
t_data[1] = (mem_addr >> 16) & 0xFF; /* MSB */
160+
t_data[2] = (mem_addr >> 8) & 0xFF;
161+
t_data[3] = (mem_addr) & 0xFF; /* LSB */
162+
163+
cs_low();
164+
external_flash_spi_write(t_data, 4);
165+
cs_low();
166+
167+
} else {
168+
t_data[0] = ERASE_SECTOR_4_BYTE_INSTR;
169+
t_data[1] = (mem_addr >> 24) & 0xFF; /* MSB */
170+
t_data[2] = (mem_addr >> 16) & 0xFF;
171+
t_data[3] = (mem_addr >> 8) & 0xFF;
172+
t_data[4] = (mem_addr) & 0xFF; /* LSB */
173+
174+
cs_low();
175+
external_flash_spi_write(t_data, 5);
176+
cs_low();
177+
}
178+
179+
external_flash_delay(450); /* sector erase takes ~400ms*/
180+
181+
external_flash_disable_write();
182+
183+
}
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+

playground/stm32/blackpill-winbond-flash-memory/Core/Src/main.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ UART_HandleTypeDef huart1;
4949

5050
/* USER CODE BEGIN PV */
5151

52+
char msg[64];
53+
uint8_t flash_data[1024]; /* to hold data read from the flash memory */
54+
55+
5256
/* USER CODE END PV */
5357

5458
/* Private function prototypes -----------------------------------------------*/
@@ -58,12 +62,40 @@ static void MX_SPI3_Init(void);
5862
static void MX_USART1_UART_Init(void);
5963
/* USER CODE BEGIN PFP */
6064

65+
void flash_test_sector_erase();
66+
void flash_test_sector_read();
67+
6168
/* USER CODE END PFP */
6269

6370
/* Private user code ---------------------------------------------------------*/
6471
/* USER CODE BEGIN 0 */
6572
uint32_t flash_id = 0;
6673

74+
void flash_test_sector_read() {
75+
/* read 200 bytes from page 20 */
76+
external_flash_read(20, 1, 200, flash_data);
77+
HAL_UART_Transmit(&huart1, (uint8_t*) "Flash data: \r\n", strlen("Flash data: \r\n"), HAL_MAX_DELAY);
78+
HAL_UART_Transmit(&huart1, (uint8_t*) flash_data, strlen(flash_data), HAL_MAX_DELAY);
79+
80+
}
81+
82+
void flash_test_sector_erase() {
83+
/* read 20 bytes starting at byte 85 in sector 1 */
84+
external_flash_read(1, 85, 20, flash_data);
85+
HAL_UART_Transmit(&huart1, (uint8_t*) "Flash data: \r\n", strlen("Flash data: \r\n"), HAL_MAX_DELAY);
86+
HAL_UART_Transmit(&huart1, (uint8_t*) flash_data, strlen(flash_data), HAL_MAX_DELAY);
87+
88+
/* erase sector 1 */
89+
external_flash_erase_sector(1);
90+
91+
/* re-check */
92+
external_flash_read(1, 85, 20, flash_data);
93+
HAL_UART_Transmit(&huart1, (uint8_t*) "sector data after erase: \r\n", strlen("sector data after erase: \r\n"), HAL_MAX_DELAY);
94+
HAL_UART_Transmit(&huart1, (uint8_t*) flash_data, strlen(flash_data), HAL_MAX_DELAY);
95+
96+
97+
}
98+
6799
/* USER CODE END 0 */
68100

69101
/**
@@ -100,12 +132,14 @@ int main(void)
100132
/* USER CODE BEGIN 2 */
101133

102134
/* get the flash memory ID */
103-
external_flash_reset();
104-
flash_id = external_flash_read_ID();
135+
// external_flash_reset();
136+
// flash_id = external_flash_read_ID();
137+
138+
//sprintf(msg, "Flash ID: %02X\r\n", flash_id);
139+
// HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 500);
105140

106-
char msg[64];
107-
sprintf(msg, "Flash ID: %02X\r\n", flash_id);
108-
HAL_UART_Transmit(&huart1, (uint8_t*)msg, strlen(msg), 500);
141+
flash_test_sector_read();
142+
//flash_test_sector_erase();
109143

110144
/* USER CODE END 2 */
111145

playground/stm32/blackpill-winbond-flash-memory/Debug/Core/Src/W25Qxx.cyclo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
../Core/Src/W25Qxx.c:36:10:external_flash_read_ID 1
88
../Core/Src/W25Qxx.c:55:6:external_flash_read 1
99
../Core/Src/W25Qxx.c:90:6:external_flash_fast_read 1
10+
../Core/Src/W25Qxx.c:129:6:external_flash_enable_write 1
11+
../Core/Src/W25Qxx.c:140:6:external_flash_disable_write 1
12+
../Core/Src/W25Qxx.c:151:6:external_flash_erase_sector 1
Binary file not shown.

playground/stm32/blackpill-winbond-flash-memory/Debug/Core/Src/W25Qxx.su

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
../Core/Src/W25Qxx.c:36:10:external_flash_read_ID 16 static
88
../Core/Src/W25Qxx.c:55:6:external_flash_read 40 static
99
../Core/Src/W25Qxx.c:90:6:external_flash_fast_read 40 static
10+
../Core/Src/W25Qxx.c:129:6:external_flash_enable_write 16 static
11+
../Core/Src/W25Qxx.c:140:6:external_flash_disable_write 16 static
12+
../Core/Src/W25Qxx.c:151:6:external_flash_erase_sector 32 static
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
../Core/Src/main.c:73:5:main 1
2-
../Core/Src/main.c:131:6:SystemClock_Config 3
3-
../Core/Src/main.c:173:13:MX_SPI3_Init 2
4-
../Core/Src/main.c:211:13:MX_USART1_UART_Init 2
5-
../Core/Src/main.c:244:13:MX_GPIO_Init 1
6-
../Core/Src/main.c:298:6:Error_Handler 1
1+
../Core/Src/main.c:74:6:flash_test_sector_read 1
2+
../Core/Src/main.c:82:6:flash_test_sector_erase 1
3+
../Core/Src/main.c:105:5:main 1
4+
../Core/Src/main.c:165:6:SystemClock_Config 3
5+
../Core/Src/main.c:207:13:MX_SPI3_Init 2
6+
../Core/Src/main.c:245:13:MX_USART1_UART_Init 2
7+
../Core/Src/main.c:278:13:MX_GPIO_Init 1
8+
../Core/Src/main.c:332:6:Error_Handler 1
Binary file not shown.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
../Core/Src/main.c:73:5:main 72 static
2-
../Core/Src/main.c:131:6:SystemClock_Config 88 static
3-
../Core/Src/main.c:173:13:MX_SPI3_Init 8 static
4-
../Core/Src/main.c:211:13:MX_USART1_UART_Init 8 static
5-
../Core/Src/main.c:244:13:MX_GPIO_Init 48 static
6-
../Core/Src/main.c:298:6:Error_Handler 4 static,ignoring_inline_asm
1+
../Core/Src/main.c:74:6:flash_test_sector_read 8 static
2+
../Core/Src/main.c:82:6:flash_test_sector_erase 8 static
3+
../Core/Src/main.c:105:5:main 8 static
4+
../Core/Src/main.c:165:6:SystemClock_Config 88 static
5+
../Core/Src/main.c:207:13:MX_SPI3_Init 8 static
6+
../Core/Src/main.c:245:13:MX_USART1_UART_Init 8 static
7+
../Core/Src/main.c:278:13:MX_GPIO_Init 48 static
8+
../Core/Src/main.c:332:6:Error_Handler 4 static,ignoring_inline_asm
Binary file not shown.

0 commit comments

Comments
 (0)