Welcome to mirror list, hosted at ThFree Co, Russian Federation.

qspi.c « Src « BSP « Examples « STM32WB5MM-DK « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6a83e3cb6be16d1e31d377a79dcab9eb7165e834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
  ******************************************************************************
  * @file    qspi.c 
  * @author  MCD Application Team
  * @brief   This example code shows how to use the qspi supported by the 
  *          STM32WB55MM-DK board
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/** @addtogroup STM32WBxx_HAL_Examples
  * @{
  */

/** @addtogroup BSP
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BUFFER_SIZE         ((uint32_t)0x0200)
#define WRITE_READ_ADDR     ((uint32_t)0x0050)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern __IO uint32_t ButtonState;
uint8_t qspi_aTxBuffer[BUFFER_SIZE];
uint8_t qspi_aRxBuffer[BUFFER_SIZE];
uint8_t qspi_eraseBuffer[BUFFER_SIZE];

/* Private function prototypes -----------------------------------------------*/
static void Fill_Buffer (uint8_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset);
static uint8_t Buffercmp (uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  demo of qspi.
  */
void QSPI_demo (void)
{
  /* QSPI info structure */
  BSP_QSPI_Info_t pQSPI_Info;
  uint8_t status;
  S25FL128S_Interface_t Tab[3]={S25FL128S_SPI_MODE, S25FL128S_DPI_MODE, S25FL128S_QPI_MODE};
  uint8_t* Tab1[3]={"SPI MODE", "DPI MODE", "QPI MODE"};
  
  /*##-1- Configure the QSPI device ##########################################*/
  /* QSPI device configuration */
  BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
  BSP_LCD_Refresh(0);
  
  UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI Tests", CENTER_MODE);
  UTIL_LCD_DrawRect(0, 0, 127 , 11, SSD1315_COLOR_WHITE);
  UTIL_LCD_DisplayStringAt(0, 15, (uint8_t*)"This example shows", CENTER_MODE);
  UTIL_LCD_DisplayStringAt(0, 27, (uint8_t*)"how to write and", CENTER_MODE);
  UTIL_LCD_DisplayStringAt(0, 39, (uint8_t*)"read data on QSPI", CENTER_MODE);
  UTIL_LCD_DisplayStringAt(0, 51, (uint8_t*)"memory", CENTER_MODE);

  BSP_LCD_Refresh(0);
  HAL_Delay(5000);
  BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
  BSP_LCD_Refresh(0);
  
  BSP_QSPI_Init_t init ;
  init.TransferRate= S25FL128S_STR_TRANSFER ;
  init.DualFlashMode= S25FL128S_DUALFLASH_DISABLE;
  
  for(int i=0; i<3; i++)
  {
    if(CheckForUserInput() > 0)
    {
      ButtonState = 0;
      return;
    }
    init.InterfaceMode = Tab[i];
    status = BSP_QSPI_Init(0,&init);
    UTIL_LCD_DisplayStringAt(0, 30, (uint8_t*)Tab1[i], CENTER_MODE);
    UTIL_LCD_DrawRect(0, 30, 127 , 11, SSD1315_COLOR_WHITE);
    BSP_LCD_Refresh(0);
    HAL_Delay(2000);
    BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
    BSP_LCD_Refresh(0);
    
    if (status != BSP_ERROR_NONE)
    {
      UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI Init:FAILED", LEFT_MODE);
      BSP_LCD_Refresh(0);
    }
    
    else
    {
      UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI Init:OK", LEFT_MODE);
      BSP_LCD_Refresh(0);
      HAL_Delay(500);
      
      /*##-2- Read & check the QSPI info #######################################*/
      /* Initialize the structure */
      pQSPI_Info.FlashSize          = (uint32_t)0x00;
      pQSPI_Info.EraseSectorSize    = (uint32_t)0x00;
      pQSPI_Info.EraseSectorsNumber = (uint32_t)0x00;
      pQSPI_Info.ProgPageSize       = (uint32_t)0x00;
      pQSPI_Info.ProgPagesNumber    = (uint32_t)0x00;
      
      /* Read the QSPI memory info */
      BSP_QSPI_GetInfo(0,&pQSPI_Info);
      
      /* Test the correctness */
      if((pQSPI_Info.FlashSize != 0x1000000) || (pQSPI_Info.EraseSectorSize != 0x1000)  ||
         (pQSPI_Info.ProgPageSize != 0x100)  || (pQSPI_Info.EraseSectorsNumber != 0x1000) ||
           (pQSPI_Info.ProgPagesNumber != 0x10000))
      {
        
        UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI GET INFO:FAILED", LEFT_MODE);
        BSP_LCD_Refresh(0);
      }
      else
      {
        UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI GET INFO:OK", LEFT_MODE);
        BSP_LCD_Refresh(0);
        HAL_Delay(500);
        /*##-3- Erase QSPI memory ################################################*/
        if(BSP_QSPI_EraseBlock(0,WRITE_READ_ADDR,S25FL128S_ERASE_64K) != BSP_ERROR_NONE)
        {
          UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI ERASE:FAILED", LEFT_MODE);
          BSP_LCD_Refresh(0);
        }
        else
        {
          UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI ERASE:OK", LEFT_MODE);
          BSP_LCD_Refresh(0);
          HAL_Delay(500);
          /*##-4- QSPI memory read/write access  #################################*/
          /* Fill the buffer to write */
          Fill_Buffer(qspi_aTxBuffer, BUFFER_SIZE, 0xD20F);
          
          /* Write data to the QSPI memory */
          if(BSP_QSPI_Write(0,qspi_aTxBuffer, WRITE_READ_ADDR, BUFFER_SIZE) != BSP_ERROR_NONE)
          {
            UTIL_LCD_DisplayStringAt(0, 48, (uint8_t*)"QSPI WRITE:FAILED", LEFT_MODE);
            BSP_LCD_Refresh(0);
          }
          else
          {
            UTIL_LCD_DisplayStringAt(0, 48, (uint8_t*)"QSPI WRITE:OK", LEFT_MODE);
            BSP_LCD_Refresh(0);
            HAL_Delay(500);
            BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
            BSP_LCD_Refresh(0);
            
            /* Read back data from the QSPI memory */
            if(BSP_QSPI_Read(0,qspi_aRxBuffer, WRITE_READ_ADDR, BUFFER_SIZE) != BSP_ERROR_NONE)
            {
              UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI READ:FAILED", LEFT_MODE);
              BSP_LCD_Refresh(0);
            }
            else
            {
              UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI READ:OK", LEFT_MODE);
              BSP_LCD_Refresh(0);
              HAL_Delay(500);
              
              /*##-5- Checking data integrity ############################################*/
              if(Buffercmp(qspi_aRxBuffer, qspi_aTxBuffer, BUFFER_SIZE) > 0)
              {
                UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI COMPARE:FAILED", LEFT_MODE);
                BSP_LCD_Refresh(0);
              }
              else
              {
                UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI COMPARE:OK", LEFT_MODE);
                BSP_LCD_Refresh(0);
                HAL_Delay(500);
                /*##-6- Erase QSPI memory ################################################*/
                if(BSP_QSPI_EraseBlock(0,WRITE_READ_ADDR,S25FL128S_ERASE_64K) != BSP_ERROR_NONE)
                {
                  UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI ERASE:FAILED", LEFT_MODE);
                  BSP_LCD_Refresh(0);
                }
                else
                {
                  UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI ERASE:OK", LEFT_MODE);
                  BSP_LCD_Refresh(0);
                  HAL_Delay(500);
                  
                  /* Read back data from the QSPI memory */
                  if(BSP_QSPI_Read(0,qspi_aRxBuffer, WRITE_READ_ADDR, BUFFER_SIZE) != BSP_ERROR_NONE)
                  {
                    UTIL_LCD_DisplayStringAt(0, 48, (uint8_t*)"QSPI READ:FAILED", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                  }
                  else
                  {
                    UTIL_LCD_DisplayStringAt(0, 48, (uint8_t*)"QSPI READ:OK", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                    HAL_Delay(500);
                    BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
                    BSP_LCD_Refresh(0);
                  }
                  /*##-7- Fill erase Buffer ############################################*/
                  for (int i=0; i < BUFFER_SIZE; i++)
                  {
                    qspi_eraseBuffer[i]= 0xff;
                  }
                  /*##-8- Checking data integrity ############################################*/
                  if(Buffercmp(qspi_aRxBuffer, qspi_eraseBuffer, BUFFER_SIZE) > 0)
                  {
                    UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI COMPARE:FAILED", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                  }
                  else
                  {
                    UTIL_LCD_DisplayStringAt(0, 0, (uint8_t*)"QSPI COMPARE:OK", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                    HAL_Delay(500);
                  }
                  /*##-9-Enable Memory Mapped Mode ###############################################*/
                  if(BSP_QSPI_EnableMemoryMappedMode(0)!=BSP_ERROR_NONE)
                  {
                    UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI MMP:FAILED", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                  }
                  else
                  {
                    UTIL_LCD_DisplayStringAt(0, 16, (uint8_t*)"QSPI MMP:OK", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                    HAL_Delay(500);
                  }
                  /*##-10- Disable Memory Mapped Mode ###############################################*/
                  if(BSP_QSPI_DisableMemoryMappedMode(0)!=BSP_ERROR_NONE)
                  {
                    UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI DMMP:FAILED", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                  }
                  else
                  {
                    UTIL_LCD_DisplayStringAt(0, 32, (uint8_t*)"QSPI DMMP:OK", LEFT_MODE);
                    BSP_LCD_Refresh(0);
                    HAL_Delay(500);
                    BSP_LCD_Clear(0,SSD1315_COLOR_BLACK);
                    BSP_LCD_Refresh(0);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

/**
  * @brief  Fills buffer with user predefined data.
  * @param  pBuffer: pointer on the buffer to fill
  * @param  uwBufferLenght: size of the buffer to fill
  * @param  uwOffset: first value to fill on the buffer
  * @retval None
  */
static void Fill_Buffer(uint8_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset)
{
  uint32_t tmpIndex = 0;

  /* Put in global buffer different values */
  for (tmpIndex = 0; tmpIndex < uwBufferLenght; tmpIndex++ )
  {
    pBuffer[tmpIndex] = tmpIndex + uwOffset;
  }
}

/**
  * @brief  Compares two buffers.
  * @param  pBuffer1, pBuffer2: buffers to be compared.
  * @param  BufferLength: buffer's length
  * @retval 1: pBuffer identical to pBuffer1
  *         0: pBuffer differs from pBuffer1
  */
static uint8_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
{
  while (BufferLength--)
  {
    if (*pBuffer1 != *pBuffer2)
    {
      return 1;
    }

    pBuffer1++;
    pBuffer2++;
  }

  return 0;
}

/**
  * @}
  */ 

/**
  * @}
  */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/