H753_CPP_W25Q_QSPI_Flash_Read_Write_01 1.0
STM32H753 W25Q QSPI flash (Adafruit 5632/5633/5634) read/write tutorial
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/* USER CODE BEGIN Header */
20/* USER CODE END Header */
21/* Includes ------------------------------------------------------------------*/
22#include "main.h"
23
24/* Private includes ----------------------------------------------------------*/
25/* USER CODE BEGIN Includes */
26#include <stdio.h>
27#include <string.h>
28#include "entryPointCPP.hpp"
29
30/* USER CODE END Includes */
31
32/* Private typedef -----------------------------------------------------------*/
33/* USER CODE BEGIN PTD */
34
35/* USER CODE END PTD */
36
37/* Private define ------------------------------------------------------------*/
38/* USER CODE BEGIN PD */
39
40// Uncomment to run the destructive W25QValidation suite instead of the normal
41// JEDEC-ID demo. Overwrites the first sector -- see w25q_validation.hpp.
42 // #define W25Q_VALIDATION_TEST_ENABLE
43
44/* USER CODE END PD */
45
46/* Private macro -------------------------------------------------------------*/
47/* USER CODE BEGIN PM */
48
49/* USER CODE END PM */
50
51/* Private variables ---------------------------------------------------------*/
52
53QSPI_HandleTypeDef hqspi;
54
55UART_HandleTypeDef huart1;
56
57/* USER CODE BEGIN PV */
58
59/* USER CODE END PV */
60
61/* Private function prototypes -----------------------------------------------*/
62void SystemClock_Config(void);
63static void MX_GPIO_Init(void);
64static void MX_USART1_UART_Init(void);
65static void MX_QUADSPI_Init(void);
66/* USER CODE BEGIN PFP */
67
68/* USER CODE END PFP */
69
70/* Private user code ---------------------------------------------------------*/
71/* USER CODE BEGIN 0 */
72
73#ifdef REDIRECT_PRINTF
74#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
75
76PUTCHAR_PROTOTYPE
77{
78 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
79
80 return ch;
81}
82#endif
83
84/* USER CODE END 0 */
85
90int main(void)
91{
92
93 /* USER CODE BEGIN 1 */
94
95 /* USER CODE END 1 */
96
97 /* MCU Configuration--------------------------------------------------------*/
98
99 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
100 HAL_Init();
101
102 /* USER CODE BEGIN Init */
103
104 /* USER CODE END Init */
105
106 /* Configure the system clock */
108
109 /* USER CODE BEGIN SysInit */
110
111 /* USER CODE END SysInit */
112
113 /* Initialize all configured peripherals */
114 MX_GPIO_Init();
117 /* USER CODE BEGIN 2 */
118
119 printf("\x1b[2J\x1b[H"); // Clear the dumb terminal screen
120 printf("Hello World!!!!!\r\n");
121 printf("Project: H753_CPP_W25Q_QSPI_Flash_Read_Write_01\r\n");
122 fflush(stdout);
123
124
125 initW25QFlash();
126 if (w25qIsReady()) {
127#ifdef W25Q_VALIDATION_TEST_ENABLE
128 w25qRunValidation();
129#else
130 uint32_t jedecId = 0;
131 if (w25qReadJEDECID(&jedecId)) {
132 printf("W25Q flash detected, JEDEC ID: 0x%06lX\r\n", (unsigned long)jedecId);
133 } else {
134 printf("W25Q flash QUADSPI read failed after Init() succeeded -- check for a loose connection\r\n");
135 }
136
137 // Demo: erase a sector, write a short message, read it back, and
138 // confirm it round-tripped -- the actual read/write this tutorial is
139 // about, as opposed to the identify-only JEDEC ID check above.
140 const uint32_t demoAddr = 0x000000;
141 const char demoMessage[] = "Hello from the H753, stored in W25Q flash via QSPI!";
142
143 if (!w25qEraseSector(demoAddr)) {
144 printf("Demo erase failed\r\n");
145 } else if (!w25qWriteBuffer(demoAddr, (const uint8_t *)demoMessage, sizeof(demoMessage))) {
146 printf("Demo write failed\r\n");
147 } else {
148 uint8_t readBack[sizeof(demoMessage)] = { 0 };
149 if (!w25qReadBuffer(demoAddr, readBack, sizeof(readBack))) {
150 printf("Demo read failed\r\n");
151 } else {
152 printf("Wrote: \"%s\"\r\n", demoMessage);
153 printf("Read : \"%s\"\r\n", readBack);
154 if (memcmp(demoMessage, readBack, sizeof(demoMessage)) == 0) {
155 printf("Read-back matches -- write/read round trip OK\r\n");
156 } else {
157 printf("Read-back MISMATCH\r\n");
158 }
159 }
160 }
161#endif
162 } else {
163 printf("W25Q flash NOT detected -- check wiring\r\n");
164 uint32_t rawId = 0;
165 bool transferOk = w25qReadJEDECIDRaw(&rawId);
166 printf(" diagnostic raw JEDEC read: transfer %s, bytes 0x%06lX\r\n",
167 transferOk ? "OK" : "FAILED", (unsigned long)rawId);
168 }
169
170 /* USER CODE END 2 */
171
172 /* Infinite loop */
173 /* USER CODE BEGIN WHILE */
174 while (1)
175 {
176 /* USER CODE END WHILE */
177
178 /* USER CODE BEGIN 3 */
179 }
180 /* USER CODE END 3 */
181}
182
188{
189 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
190 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
191
194 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
195
198 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
199
200 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
201
205 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
206 RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
207 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
208 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
209 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
210 RCC_OscInitStruct.PLL.PLLM = 4;
211 RCC_OscInitStruct.PLL.PLLN = 60;
212 RCC_OscInitStruct.PLL.PLLP = 2;
213 RCC_OscInitStruct.PLL.PLLQ = 4;
214 RCC_OscInitStruct.PLL.PLLR = 2;
215 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
216 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
217 RCC_OscInitStruct.PLL.PLLFRACN = 0;
218 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
219 {
220 Error_Handler();
221 }
222
225 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
226 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
227 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
228 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
229 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
230 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
231 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
232 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
233 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
234 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
235
236 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
237 {
238 Error_Handler();
239 }
240}
241
247static void MX_QUADSPI_Init(void)
248{
249
250 /* USER CODE BEGIN QUADSPI_Init 0 */
251
252 /* USER CODE END QUADSPI_Init 0 */
253
254 /* USER CODE BEGIN QUADSPI_Init 1 */
255
256 /* USER CODE END QUADSPI_Init 1 */
257 /* QUADSPI parameter configuration*/
258 hqspi.Instance = QUADSPI;
259 hqspi.Init.ClockPrescaler = 255;
260 hqspi.Init.FifoThreshold = 1;
261 hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
262 hqspi.Init.FlashSize = 23;
263 hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
264 hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
265 hqspi.Init.FlashID = QSPI_FLASH_ID_1;
266 hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
267 if (HAL_QSPI_Init(&hqspi) != HAL_OK)
268 {
269 Error_Handler();
270 }
271 /* USER CODE BEGIN QUADSPI_Init 2 */
272
273 /* USER CODE END QUADSPI_Init 2 */
274
275}
276
282static void MX_USART1_UART_Init(void)
283{
284
285 /* USER CODE BEGIN USART1_Init 0 */
286
287 /* USER CODE END USART1_Init 0 */
288
289 /* USER CODE BEGIN USART1_Init 1 */
290
291 /* USER CODE END USART1_Init 1 */
292 huart1.Instance = USART1;
293 huart1.Init.BaudRate = 19200;
294 huart1.Init.WordLength = UART_WORDLENGTH_8B;
295 huart1.Init.StopBits = UART_STOPBITS_1;
296 huart1.Init.Parity = UART_PARITY_NONE;
297 huart1.Init.Mode = UART_MODE_TX_RX;
298 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
299 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
300 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
301 huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
302 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
303 if (HAL_UART_Init(&huart1) != HAL_OK)
304 {
305 Error_Handler();
306 }
307 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
308 {
309 Error_Handler();
310 }
311 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
312 {
313 Error_Handler();
314 }
315 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
316 {
317 Error_Handler();
318 }
319 /* USER CODE BEGIN USART1_Init 2 */
320
321 /* USER CODE END USART1_Init 2 */
322
323}
324
330static void MX_GPIO_Init(void)
331{
332 GPIO_InitTypeDef GPIO_InitStruct = {0};
333 /* USER CODE BEGIN MX_GPIO_Init_1 */
334
335 /* USER CODE END MX_GPIO_Init_1 */
336
337 /* GPIO Ports Clock Enable */
338 __HAL_RCC_GPIOC_CLK_ENABLE();
339 __HAL_RCC_GPIOF_CLK_ENABLE();
340 __HAL_RCC_GPIOH_CLK_ENABLE();
341 __HAL_RCC_GPIOB_CLK_ENABLE();
342 __HAL_RCC_GPIOD_CLK_ENABLE();
343 __HAL_RCC_GPIOA_CLK_ENABLE();
344
345 /*Configure GPIO pins : STLINK_RX_Pin STLINK_TX_Pin */
346 GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
347 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
348 GPIO_InitStruct.Pull = GPIO_NOPULL;
349 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
350 GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
351 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
352
353 /* USER CODE BEGIN MX_GPIO_Init_2 */
354
355 /* USER CODE END MX_GPIO_Init_2 */
356}
357
358/* USER CODE BEGIN 4 */
359
360
367void _Error_Handler(const char *file, int line)
368{
369 __disable_irq();
370
371#ifdef REDIRECT_PRINTF
372 char buf[80];
373
374 snprintf(buf, sizeof(buf),
375 "Trapped in _Error_Handler(). Called from: %s, line: %d\r\n",
376 file, line);
377 printf("%s", buf);
378#endif
379
380 /* User can add an implementation to report the HAL error return state. */
381 while(1)
382 {
383 }
384}
385
386/* USER CODE END 4 */
387
388
389#ifdef USE_FULL_ASSERT
397void assert_failed(uint8_t *file, uint32_t line)
398{
399 /* USER CODE BEGIN 6 */
400 /* assert_param() can fire from inside any HAL call -- including before
401 * REDIRECT_PRINTF's UART is fully set up, or from an unusual calling
402 * context -- so this only attempts to report when REDIRECT_PRINTF is
403 * available, then halts either way. Continuing after a failed HAL
404 * parameter check is undefined behavior, not something safe to log
405 * and ignore. */
406 __disable_irq();
407
408#ifdef REDIRECT_PRINTF
409 {
410 char buf[96];
411
412 snprintf(buf, sizeof(buf),
413 "assert_param failed: file %s, line %lu\r\n",
414 (char *)file, (unsigned long)line);
415 printf("%s", buf);
416 }
417#endif
418
419 while (1)
420 {
421 }
422 /* USER CODE END 6 */
423}
424#endif /* USE_FULL_ASSERT */
static void MX_QUADSPI_Init(void)
QUADSPI Initialization Function.
Definition main.c:247
static void MX_USART1_UART_Init(void)
USART1 Initialization Function.
Definition main.c:282
void SystemClock_Config(void)
System Clock Configuration.
Definition main.c:187
int main(void)
The application entry point.
Definition main.c:90
void _Error_Handler(const char *file, int line)
This function is executed in case of error occurrence.
Definition main.c:367
static void MX_GPIO_Init(void)
GPIO Initialization Function.
Definition main.c:330
: Header for main.c file. This file contains the common defines of the application.