Skip to main content
Visitor II
July 17, 2025
Question

STM32N657X0-Q write to spi flash using code

  • July 17, 2025
  • 2 replies
  • 313 views

For a project i want to save log files in the external SPI of the Stm32n6 for this is already tried to configure xspi2 and the external memory, but i am not sure if i have done it right. I also have found no source on how to continue with the Code after this maybe someone can help with this.

Screenshot 2025-07-17 103829.pngScreenshot 2025-07-17 105040.png

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    July 17, 2025

    Hello @Nixxen 

    Please check these examples below as starting point to configure your XSPI in CubeMX.

    STM32CubeN6/Projects/NUCLEO-N657X0-Q/Examples/XSPI at main · STMicroelectronics/STM32CubeN6 · GitHub

    Graduate
    July 19, 2025

    Sometimes from examples is not easy to understand, and you have test the setup step by step by debugging, there is my working code, you just need to adjust PHY_LINK_1S1S1S by your flash type if you want to use 4 lines, in anyway if you use 1 line data mode then there must work without changes (I also attached files, I do not tested memory map mode), and I see what you do not use CS pin, you plan to switch manually ?

    // include in inludes and declare object
    #include "stm32_sal_xspi.h"
    SAL_XSPI_ObjectTypeDef salXspi;
    // test code in main loop
     	XSPI_ResetFlash();
     	XSPI_Init(&salXspi, (XSPI_HandleTypeDef*)&hxspi1);
     	XSPI_MemoryConfig(&salXspi, PARAM_PHY_LINK, PHY_LINK_1S1S1S);
     	HAL_Delay(1);
     	XSPI_EraseSector(&salXspi, 0, 0);
     	uint8 dbuff[256];
     // filling test buffer
     	for (uint8 n = 0; n < 32; n++) dbuff[n] = n + 1;
     // writing 32 bytes at 0x32000 address in flash
     	XSPI_WriteFlashData(&salXspi, 0x32000, dbuff, 32); 
    	uint8 rbuff[32];
    	XSPI_ReadFlashData(&salXspi, 0x32000, rbuff, 8);
    	printf("Fl %u %u %u %u %u %u %u %u\n", rbuff[0], rbuff[1], rbuff[2], rbuff[3], rbuff[4], rbuff[5], rbuff[6], rbuff[7]);