1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9 
10 #include "aos/kernel.h"
11 #include "aos/hal/spi.h"
12 #include "spi_test.h"
13 #include "board.h"
14 #include "stm32f4xx_hal.h"
15 
16 /* Private define ------------------------------------------------------------*/
17 #define W25X_WriteEnable              0x06
18 #define W25X_WriteDisable             0x04
19 #define W25X_ReadStatusReg          0x05
20 #define W25X_WriteStatusReg         0x01
21 #define W25X_ReadData                   0x03
22 #define W25X_FastReadData             0x0B
23 #define W25X_FastReadDual             0x3B
24 #define W25X_PageProgram              0x02
25 #define W25X_BlockErase               0xD8
26 #define W25X_SectorErase              0x20
27 #define W25X_ChipErase                0xC7
28 #define W25X_PowerDown                0xB9
29 #define W25X_ReleasePowerDown       0xAB
30 #define W25X_DeviceID                   0xAB
31 #define W25X_ManufactDeviceID       0x90
32 #define W25X_JedecDeviceID          0x9F
33 
34 #define WIP_Flag                  0x01  /* Write In Progress (WIP) flag */
35 
36 #define Dummy_Byte                0xFF
37 
38 #define TEST_DATA_BYTES 128
39 
40 static spi_dev_t spi_dev;
41 
42 static uint8_t write_buf[TEST_DATA_BYTES];
43 static uint8_t read_buf[TEST_DATA_BYTES];
44 
45 static int32_t flash_spi_init(void);
46 static int32_t flash_spi_read(void *buf, uint32_t bytes);
47 static int32_t flash_spi_write(void *buf, uint32_t bytes);
48 static int32_t flash_spi_erase(void);
49 
50 SPI_HandleTypeDef hspi1;
51 /* SPI1 init function */
MX_SPI1_Init(void)52 static void MX_SPI1_Init(void)
53 {
54 
55   /* SPI1 parameter configuration*/
56   hspi1.Instance = SPI1;
57 
58   hspi1.Init.Mode = SPI_MODE_MASTER;
59 
60   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
61   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
62   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
63   hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
64   hspi1.Init.NSS = SPI_NSS_SOFT;
65   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
66   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
67   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
68   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
69   hspi1.Init.CRCPolynomial = 10;
70   if (HAL_SPI_Init(&hspi1) != HAL_OK)
71   {
72     _Error_Handler(__FILE__, __LINE__);
73   }
74 
75 }
76 
flash_spi_read_device_id(void)77 uint8_t flash_spi_read_device_id(void)
78 {
79    uint8_t data = 0;
80    uint8_t data_r = 0;
81    int ret = -1;
82 
83    /* Select the FLASH: Chip Select low */
84    //SPI_FLASH_CS_LOW();
85 
86    /* Send "RDID " instruction */
87     data = W25X_DeviceID;
88     ret = HAL_SPI_Transmit(&hspi1, &data, 1, 100);
89     printf("ret %d\n", ret);
90 
91     data = Dummy_Byte;
92     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
93     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
94     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
95     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
96 
97     while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY)
98     {
99     }
100 
101    /* Read a byte from the FLASH */
102     data = Dummy_Byte;
103     ret = HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
104     printf("ret %d\n", ret);
105    /* Deselect the FLASH: Chip Select high */
106    //SPI_FLASH_CS_HIGH();
107 
108    return data_r;
109 }
110 
flash_spi_read_id(void)111 uint32_t flash_spi_read_id(void)
112 {
113    uint32_t data1 = 0;
114    uint32_t data2 = 0;
115    uint32_t data3 = 0;
116    uint32_t data4 = 0;
117    uint8_t data = 0;
118    uint8_t data_r = 0;
119    int ret = -1;
120 
121    /* Select the FLASH: Chip Select low */
122    //SPI_FLASH_CS_LOW();
123 
124    /* Send "RDID " instruction */
125     data = 0x90;
126     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
127     data = 0x00;
128     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
129     data = 0x00;
130     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
131     data = 0x00;
132     HAL_SPI_TransmitReceive(&hspi1, &data, &data_r, 1, 1000);
133 
134     data = 0xFF;
135     HAL_SPI_TransmitReceive(&hspi1, &data, &data1, 1, 1000);
136     data = 0xFF;
137     HAL_SPI_TransmitReceive(&hspi1, &data, &data2, 1, 1000);
138 
139 
140     data4 = (data1 << 8) | data2;
141 
142 
143    return data4;
144 }
145 
146 #if 0
147 uint8_t flash_spi_read_device_id(void)
148 {
149    uint8_t data = 0;
150    int ret = -1;
151 
152    /* Select the FLASH: Chip Select low */
153    //SPI_FLASH_CS_LOW();
154 
155    /* Send "RDID " instruction */
156     data = W25X_DeviceID;
157     ret = hal_spi_send(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
158     printf("ret %d\n", ret);
159 
160     data = Dummy_Byte;
161     hal_spi_send(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
162     hal_spi_send(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
163     hal_spi_send(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
164     hal_spi_send(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
165 
166    /* Read a byte from the FLASH */
167     hal_spi_recv(&spi_dev, &data, 1, AOS_WAIT_FOREVER);
168 
169    /* Deselect the FLASH: Chip Select high */
170    //SPI_FLASH_CS_HIGH();
171 
172    return data;
173 }
174 #endif
175 
176 /* a spi interface rtc(ds1307) must be connetted to the mcu before testing */
hal_spi_test(void)177 void hal_spi_test(void)
178 {
179     int ret = -1;
180     int i   = 0;
181 
182     printf("*********** spi test start ! ***********\n");
183 
184     memset(read_buf, 0, sizeof(read_buf));
185 
186     //ret = flash_spi_init();
187     //if (ret != 0) {
188     //    printf("flash_spi_init error ! test failed !\n");
189     //    return;
190     //}
191     MX_SPI1_Init();
192 
193     //uint8_t data = flash_spi_read_device_id();
194     //printf("flash_spi_read_device_id: %x\n", data);
195 
196     uint32_t data32 = flash_spi_read_id();
197     printf("flash_spi_read_id: %x\n", data32);
198     return;
199 
200     ret = flash_spi_erase();
201     if (ret != 0) {
202         printf("flash_spi_erase error ! test failed !\n");
203         return;
204     }
205 
206     for (i = 0; i < TEST_DATA_BYTES; i++)
207     {
208         write_buf[i] = i;
209     }
210 
211     ret = flash_spi_write(write_buf, TEST_DATA_BYTES);
212     if (ret != 0) {
213         printf("flash_spi_write error ! test failed !\n");
214         return;
215     }
216 
217     ret = flash_spi_read(read_buf, TEST_DATA_BYTES);
218     if (ret != 0) {
219         printf("flash_spi_read error ! test failed !\n");
220         return;
221     }
222 
223     for (i = 0; i < TEST_DATA_BYTES; i++)
224     {
225         if (write_buf[i] != read_buf[i]) {
226             printf("error ! read data diff with write data ! test failed !\n");
227             return;
228         }
229     }
230 
231     ret = hal_spi_finalize(&spi_dev);
232     if (ret != 0) {
233         printf("hal_spi_finalize error ! test failed !\n");
234         return;
235     }
236 
237     printf("spi test result: succeed !\n");
238 
239     printf("*********** spi test end ! ***********\n");
240 }
241 
flash_spi_init(void)242 int32_t flash_spi_init(void)
243 {
244     int ret = -1;
245 
246     spi_dev.port = PORT_SPI_1;
247     spi_dev.config.mode = HAL_SPI_MODE_MASTER;
248     spi_dev.config.freq = 15000000;
249 
250     ret = hal_spi_init(&spi_dev);
251     if (ret != 0) {
252         printf("hal_spi_init error !\n");
253     }
254 
255     return ret;
256 }
257 
flash_spi_read(void * buf,uint32_t bytes)258 int32_t flash_spi_read(void *buf, uint32_t bytes)
259 {
260     int ret = -1;
261 
262 
263 
264     return ret;
265 }
266 
flash_spi_write(void * buf,uint32_t bytes)267 int32_t flash_spi_write(void *buf, uint32_t bytes)
268 {
269     int ret = -1;
270 
271 
272 
273     return ret;
274 }
275 
flash_spi_erase(void)276 int32_t flash_spi_erase(void)
277 {
278     int ret = -1;
279 
280 
281 
282     return ret;
283 }
284 
285