H753_CPP_W25Q_SPI_Flash_Read_Write_01 1.0
STM32H753 W25Q SPI 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 */
18/* USER CODE END Header */
19/* Includes ------------------------------------------------------------------*/
20#include "main.h"
21
22/* Private includes ----------------------------------------------------------*/
23/* USER CODE BEGIN Includes */
24#include <stdio.h>
25#include <string.h>
26#include "entryPointCPP.hpp"
27
28/* USER CODE END Includes */
29
30/* Private typedef -----------------------------------------------------------*/
31/* USER CODE BEGIN PTD */
32
33/* USER CODE END PTD */
34
35/* Private define ------------------------------------------------------------*/
36/* USER CODE BEGIN PD */
37
38// Uncomment to run the destructive W25QValidation suite instead of the normal
39// JEDEC-ID demo. Overwrites the first sector -- see w25q_validation.hpp.
40 // #define W25Q_VALIDATION_TEST_ENABLE
41
42/* USER CODE END PD */
43
44/* Private macro -------------------------------------------------------------*/
45/* USER CODE BEGIN PM */
46
47/* USER CODE END PM */
48
49/* Private variables ---------------------------------------------------------*/
50
51SPI_HandleTypeDef hspi1;
52
53UART_HandleTypeDef huart1;
54
55/* USER CODE BEGIN PV */
56
57/* USER CODE END PV */
58
59/* Private function prototypes -----------------------------------------------*/
60void SystemClock_Config(void);
61static void MX_GPIO_Init(void);
62static void MX_USART1_UART_Init(void);
63static void MX_SPI1_Init(void);
64/* USER CODE BEGIN PFP */
65
66/* USER CODE END PFP */
67
68/* Private user code ---------------------------------------------------------*/
69/* USER CODE BEGIN 0 */
70
71#ifdef REDIRECT_PRINTF
72#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
73
74PUTCHAR_PROTOTYPE
75{
76 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
77
78 return ch;
79}
80#endif
81
82/* USER CODE END 0 */
83
88int main(void)
89{
90
91 /* USER CODE BEGIN 1 */
92
93 /* USER CODE END 1 */
94
95 /* MCU Configuration--------------------------------------------------------*/
96
97 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
98 HAL_Init();
99
100 /* USER CODE BEGIN Init */
101
102 /* USER CODE END Init */
103
104 /* Configure the system clock */
106
107 /* USER CODE BEGIN SysInit */
108
109 /* USER CODE END SysInit */
110
111 /* Initialize all configured peripherals */
112 MX_GPIO_Init();
114 MX_SPI1_Init();
115 /* USER CODE BEGIN 2 */
116
117 printf("\x1b[2J\x1b[H"); // Clear the dumb terminal screen
118 printf("Hello World!!!!!\r\n");
119 fflush(stdout);
120
121
122 initW25QFlash();
123 if (w25qIsReady()) {
124#ifdef W25Q_VALIDATION_TEST_ENABLE
125 w25qRunValidation();
126#else
127 uint32_t jedecId = 0;
128 if (w25qReadJEDECID(&jedecId)) {
129 printf("W25Q flash detected, JEDEC ID: 0x%06lX\r\n", (unsigned long)jedecId);
130 } else {
131 printf("W25Q flash SPI read failed after Init() succeeded -- check for a loose connection\r\n");
132 }
133
134 // Demo: erase a sector, write a short message, read it back, and
135 // confirm it round-tripped -- the actual read/write this tutorial is
136 // about, as opposed to the identify-only JEDEC ID check above.
137 const uint32_t demoAddr = 0x000000;
138 const char demoMessage[] = "Hello from the H753, stored in W25Q flash!";
139
140 if (!w25qEraseSector(demoAddr)) {
141 printf("Demo erase failed\r\n");
142 } else if (!w25qWriteBuffer(demoAddr, (const uint8_t *)demoMessage, sizeof(demoMessage))) {
143 printf("Demo write failed\r\n");
144 } else {
145 uint8_t readBack[sizeof(demoMessage)] = { 0 };
146 if (!w25qReadBuffer(demoAddr, readBack, sizeof(readBack))) {
147 printf("Demo read failed\r\n");
148 } else {
149 printf("Wrote: \"%s\"\r\n", demoMessage);
150 printf("Read : \"%s\"\r\n", readBack);
151 if (memcmp(demoMessage, readBack, sizeof(demoMessage)) == 0) {
152 printf("Read-back matches -- write/read round trip OK\r\n");
153 } else {
154 printf("Read-back MISMATCH\r\n");
155 }
156 }
157 }
158#endif
159 } else {
160 printf("W25Q flash NOT detected -- check wiring\r\n");
161 }
162
163 /* USER CODE END 2 */
164
165 /* Infinite loop */
166 /* USER CODE BEGIN WHILE */
167 while (1)
168 {
169 /* USER CODE END WHILE */
170
171 /* USER CODE BEGIN 3 */
172 }
173 /* USER CODE END 3 */
174}
175
181{
182 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
183 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
184
187 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
188
191 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
192
193 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
194
198 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
199 RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
200 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
201 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
202 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
203 RCC_OscInitStruct.PLL.PLLM = 4;
204 RCC_OscInitStruct.PLL.PLLN = 60;
205 RCC_OscInitStruct.PLL.PLLP = 2;
206 RCC_OscInitStruct.PLL.PLLQ = 5;
207 RCC_OscInitStruct.PLL.PLLR = 2;
208 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
209 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
210 RCC_OscInitStruct.PLL.PLLFRACN = 0;
211 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
212 {
213 Error_Handler();
214 }
215
218 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
219 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
220 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
221 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
222 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
223 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
224 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
225 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
226 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
227 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
228
229 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
230 {
231 Error_Handler();
232 }
233}
234
240static void MX_SPI1_Init(void)
241{
242
243 /* USER CODE BEGIN SPI1_Init 0 */
244
245 /* USER CODE END SPI1_Init 0 */
246
247 /* USER CODE BEGIN SPI1_Init 1 */
248
249 /* USER CODE END SPI1_Init 1 */
250 /* SPI1 parameter configuration*/
251 hspi1.Instance = SPI1;
252 hspi1.Init.Mode = SPI_MODE_MASTER;
253 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
254 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
255 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
256 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
257 hspi1.Init.NSS = SPI_NSS_SOFT;
258 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
259 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
260 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
261 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
262 hspi1.Init.CRCPolynomial = 0x0;
263 hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
264 hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
265 hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
266 hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
267 hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
268 hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
269 hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
270 hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
271 hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
272 hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
273 if (HAL_SPI_Init(&hspi1) != HAL_OK)
274 {
275 Error_Handler();
276 }
277 /* USER CODE BEGIN SPI1_Init 2 */
278
279 /* USER CODE END SPI1_Init 2 */
280
281}
282
288static void MX_USART1_UART_Init(void)
289{
290
291 /* USER CODE BEGIN USART1_Init 0 */
292
293 /* USER CODE END USART1_Init 0 */
294
295 /* USER CODE BEGIN USART1_Init 1 */
296
297 /* USER CODE END USART1_Init 1 */
298 huart1.Instance = USART1;
299 huart1.Init.BaudRate = 19200;
300 huart1.Init.WordLength = UART_WORDLENGTH_8B;
301 huart1.Init.StopBits = UART_STOPBITS_1;
302 huart1.Init.Parity = UART_PARITY_NONE;
303 huart1.Init.Mode = UART_MODE_TX_RX;
304 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
305 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
306 huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
307 huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
308 huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
309 if (HAL_UART_Init(&huart1) != HAL_OK)
310 {
311 Error_Handler();
312 }
313 if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
314 {
315 Error_Handler();
316 }
317 if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
318 {
319 Error_Handler();
320 }
321 if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
322 {
323 Error_Handler();
324 }
325 /* USER CODE BEGIN USART1_Init 2 */
326
327 /* USER CODE END USART1_Init 2 */
328
329}
330
336static void MX_GPIO_Init(void)
337{
338 GPIO_InitTypeDef GPIO_InitStruct = {0};
339 /* USER CODE BEGIN MX_GPIO_Init_1 */
340
341 /* USER CODE END MX_GPIO_Init_1 */
342
343 /* GPIO Ports Clock Enable */
344 __HAL_RCC_GPIOC_CLK_ENABLE();
345 __HAL_RCC_GPIOH_CLK_ENABLE();
346 __HAL_RCC_GPIOA_CLK_ENABLE();
347 __HAL_RCC_GPIOB_CLK_ENABLE();
348
349 /*Configure GPIO pin Output Level */
350 HAL_GPIO_WritePin(WQ25_CS_GPIO_Port, WQ25_CS_Pin, GPIO_PIN_RESET);
351
352 /*Configure GPIO pin : WQ25_CS_Pin */
353 GPIO_InitStruct.Pin = WQ25_CS_Pin;
354 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
355 GPIO_InitStruct.Pull = GPIO_NOPULL;
356 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
357 HAL_GPIO_Init(WQ25_CS_GPIO_Port, &GPIO_InitStruct);
358
359 /* USER CODE BEGIN MX_GPIO_Init_2 */
360
361 /* USER CODE END MX_GPIO_Init_2 */
362}
363
364/* USER CODE BEGIN 4 */
365
366
373void _Error_Handler(const char *file, int line)
374{
375 __disable_irq();
376
377#ifdef REDIRECT_PRINTF
378 char buf[80];
379
380 snprintf(buf, sizeof(buf),
381 "Trapped in _Error_Handler(). Called from: %s, line: %d\r\n",
382 file, line);
383 printf("%s", buf);
384#endif
385
386 /* User can add an implementation to report the HAL error return state. */
387 while(1)
388 {
389 }
390}
391
392/* USER CODE END 4 */
393
394
395#ifdef USE_FULL_ASSERT
403void assert_failed(uint8_t *file, uint32_t line)
404{
405 /* USER CODE BEGIN 6 */
406 /* assert_param() can fire from inside any HAL call -- including before
407 * REDIRECT_PRINTF's UART is fully set up, or from an unusual calling
408 * context -- so this only attempts to report when REDIRECT_PRINTF is
409 * available, then halts either way. Continuing after a failed HAL
410 * parameter check is undefined behavior, not something safe to log
411 * and ignore. */
412 __disable_irq();
413
414#ifdef REDIRECT_PRINTF
415 {
416 char buf[96];
417
418 snprintf(buf, sizeof(buf),
419 "assert_param failed: file %s, line %lu\r\n",
420 (char *)file, (unsigned long)line);
421 printf("%s", buf);
422 }
423#endif
424
425 while (1)
426 {
427 }
428 /* USER CODE END 6 */
429}
430#endif /* USE_FULL_ASSERT */
static void MX_SPI1_Init(void)
SPI1 Initialization Function.
Definition main.c:240
static void MX_USART1_UART_Init(void)
USART1 Initialization Function.
Definition main.c:288
void SystemClock_Config(void)
System Clock Configuration.
Definition main.c:180
int main(void)
The application entry point.
Definition main.c:88
void _Error_Handler(const char *file, int line)
This function is executed in case of error occurrence.
Definition main.c:373
static void MX_GPIO_Init(void)
GPIO Initialization Function.
Definition main.c:336
: Header for main.c file. This file contains the common defines of the application.