Skip to content

Commit d3c2704

Browse files
authored
Merge pull request #3685 from armink/fix_spi
完善 SPI 框架及驱动相关功能
2 parents b2e30a7 + b7a9312 commit d3c2704

File tree

6 files changed

+94
-86
lines changed

6 files changed

+94
-86
lines changed

bsp/stm32/libraries/HAL_Drivers/drv_spi.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
* 2020-01-15 whj4674672 Porting for stm32h7xx
1212
*/
1313

14+
#include <rtthread.h>
15+
#include <rtdevice.h>
1416
#include "board.h"
1517

1618
#ifdef RT_USING_SPI
1719

1820
#if defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4) || defined(BSP_USING_SPI5) || defined(BSP_USING_SPI6)
19-
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
2021

2122
#include "drv_spi.h"
2223
#include "drv_config.h"
@@ -278,7 +279,7 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
278279
SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
279280
struct stm32_hw_spi_cs *cs = device->parent.user_data;
280281

281-
if (message->cs_take)
282+
if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
282283
{
283284
HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET);
284285
}
@@ -333,6 +334,12 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
333334
{
334335
state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000);
335336
}
337+
338+
if (message->cs_release && (device->config.mode & RT_SPI_3WIRE))
339+
{
340+
/* release the CS by disable SPI when using 3 wires SPI */
341+
__HAL_SPI_DISABLE(spi_handle);
342+
}
336343
}
337344
else
338345
{
@@ -343,6 +350,8 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
343350
}
344351
else
345352
{
353+
/* clear the old error flag */
354+
__HAL_SPI_CLEAR_OVRFLAG(spi_handle);
346355
state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
347356
}
348357
}
@@ -364,7 +373,7 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
364373
while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY);
365374
}
366375

367-
if (message->cs_release)
376+
if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
368377
{
369378
HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET);
370379
}

components/drivers/include/drivers/spi.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (c) 2006-2018, RT-Thread Development Team
2+
* Copyright (c) 2006-2020, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
88
* 2012-11-23 Bernard Add extern "C"
9+
* 2020-06-13 armink fix the 3 wires issue
910
*/
1011

1112
#ifndef __SPI_H__
@@ -18,8 +19,6 @@
1819
extern "C"{
1920
#endif
2021

21-
#define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
22-
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
2322
/**
2423
* At CPOL=0 the base value of the clock is zero
2524
* - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
@@ -32,26 +31,29 @@ extern "C"{
3231
* - For CPHA=1, data are captured on clock's rising edge and data are propagated
3332
* on a falling edge.
3433
*/
34+
#define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
35+
#define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
36+
3537
#define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
3638
#define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
3739

3840
#define RT_SPI_MASTER (0<<3) /* SPI master device */
3941
#define RT_SPI_SLAVE (1<<3) /* SPI slave device */
4042

43+
#define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */
44+
#define RT_SPI_NO_CS (1<<5) /* No chipselect */
45+
#define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */
46+
#define RT_SPI_READY (1<<7) /* Slave pulls low to pause */
47+
48+
#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE | RT_SPI_CS_HIGH | RT_SPI_NO_CS | RT_SPI_3WIRE | RT_SPI_READY)
49+
4150
#define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
4251
#define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
4352
#define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
4453
#define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
4554

46-
#define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE)
47-
48-
#define RT_SPI_BUS_MODE_SPI (1<<0)
49-
#define RT_SPI_BUS_MODE_QSPI (1<<1)
50-
51-
#define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */
52-
#define RT_SPI_NO_CS (1<<5) /* No chipselect */
53-
#define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */
54-
#define RT_SPI_READY (1<<7) /* Slave pulls low to pause */
55+
#define RT_SPI_BUS_MODE_SPI (1<<0)
56+
#define RT_SPI_BUS_MODE_QSPI (1<<1)
5557

5658
/**
5759
* SPI message structure

components/drivers/spi/sfud/inc/sfud_cfg.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
#define SFUD_DEBUG_MODE
3939
#endif
4040

41+
#ifdef RT_DEBUG_SFUD
42+
#define DBG_LVL DBG_LOG
43+
#define SFUD_DEBUG(fmt, ...) LOG_D("(%s:%ld) "fmt"", __FILE__, __LINE__, ##__VA_ARGS__)
44+
#else
45+
#define DBG_LVL DBG_INFO
46+
#endif /* RT_DEBUG_SFUD */
47+
48+
#define DBG_TAG "SFUD"
49+
#include <rtdbg.h>
50+
extern void rt_kprintf(const char *fmt, ...);
51+
#define SFUD_INFO(...) LOG_I(__VA_ARGS__)
52+
4153
/**
4254
* Using probe flash JEDEC SFDP parameter.
4355
*/

components/drivers/spi/sfud/inc/sfud_flash_def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ typedef struct {
124124
{"AT45DB161E", SFUD_MF_ID_ATMEL, 0x26, 0x00, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_DUAL_BUFFER, 512, 0x81}, \
125125
{"W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
126126
{"W25Q16BV", SFUD_MF_ID_WINBOND, 0x40, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
127-
{"W25Q64DW", SFUD_MF_ID_WINBOND, 0x60, 0x17, 8L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
127+
{"W25Q64DW", SFUD_MF_ID_WINBOND, 0x40, 0x17, 8L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
128128
{"W25Q128BV", SFUD_MF_ID_WINBOND, 0x40, 0x18, 16L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
129129
{"W25Q256FV", SFUD_MF_ID_WINBOND, 0x40, 0x19, 32L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20}, \
130130
{"SST25VF016B", SFUD_MF_ID_SST, 0x25, 0x41, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_AAI, 4096, 0x20}, \

components/drivers/spi/spi_flash_sfud.c

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616
#ifdef RT_USING_SFUD
1717

18-
#ifdef RT_DEBUG_SFUD
19-
#define DEBUG_TRACE rt_kprintf("[SFUD] "); rt_kprintf
20-
#else
21-
#define DEBUG_TRACE(...)
22-
#endif /* RT_DEBUG_SFUD */
23-
2418
#ifndef RT_SFUD_DEFAULT_SPI_CFG
2519

2620
#ifndef RT_SFUD_SPI_MAX_HZ
@@ -34,7 +28,7 @@
3428
.data_width = 8, \
3529
.max_hz = RT_SFUD_SPI_MAX_HZ, \
3630
}
37-
#endif
31+
#endif /* RT_SFUD_DEFAULT_SPI_CFG */
3832

3933
#ifdef SFUD_USING_QSPI
4034
#define RT_SFUD_DEFAULT_QSPI_CFG \
@@ -44,11 +38,7 @@
4438
.ddr_mode = 0, \
4539
.qspi_dl_width = 4, \
4640
}
47-
#endif
48-
49-
static char log_buf[RT_CONSOLEBUF_SIZE];
50-
51-
void sfud_log_debug(const char *file, const long line, const char *format, ...);
41+
#endif /* SFUD_USING_QSPI */
5242

5343
static rt_err_t rt_sfud_control(rt_device_t dev, int cmd, void *args) {
5444
RT_ASSERT(dev);
@@ -259,44 +249,6 @@ static void retry_delay_100us(void) {
259249
rt_thread_delay((RT_TICK_PER_SECOND * 1 + 9999) / 10000);
260250
}
261251

262-
/**
263-
* This function is print debug info.
264-
*
265-
* @param file the file which has call this function
266-
* @param line the line number which has call this function
267-
* @param format output format
268-
* @param ... args
269-
*/
270-
void sfud_log_debug(const char *file, const long line, const char *format, ...) {
271-
va_list args;
272-
273-
/* args point to the first variable parameter */
274-
va_start(args, format);
275-
rt_kprintf("[SFUD] (%s:%ld) ", file, line);
276-
/* must use vprintf to print */
277-
rt_vsnprintf(log_buf, sizeof(log_buf), format, args);
278-
rt_kprintf("%s\n", log_buf);
279-
va_end(args);
280-
}
281-
282-
/**
283-
* This function is print routine info.
284-
*
285-
* @param format output format
286-
* @param ... args
287-
*/
288-
void sfud_log_info(const char *format, ...) {
289-
va_list args;
290-
291-
/* args point to the first variable parameter */
292-
va_start(args, format);
293-
rt_kprintf("[SFUD] ");
294-
/* must use vprintf to print */
295-
rt_vsnprintf(log_buf, sizeof(log_buf), format, args);
296-
rt_kprintf("%s\n", log_buf);
297-
va_end(args);
298-
}
299-
300252
sfud_err sfud_spi_port_init(sfud_flash *flash) {
301253
sfud_err result = SFUD_SUCCESS;
302254

@@ -311,7 +263,7 @@ sfud_err sfud_spi_port_init(sfud_flash *flash) {
311263
flash->spi.unlock = spi_unlock;
312264
flash->spi.user_data = flash;
313265
if (RT_TICK_PER_SECOND < 1000) {
314-
rt_kprintf("[SFUD] Warning: The OS tick(%d) is less than 1000. So the flash write will take more time.\n", RT_TICK_PER_SECOND);
266+
LOG_W("[SFUD] Warning: The OS tick(%d) is less than 1000. So the flash write will take more time.", RT_TICK_PER_SECOND);
315267
}
316268
/* 100 microsecond delay */
317269
flash->retry.delay = retry_delay_100us;
@@ -334,23 +286,23 @@ const static struct rt_device_ops flash_device_ops =
334286
#endif
335287

336288
/**
337-
* Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
289+
* Probe SPI flash by SFUD (Serial Flash Universal Driver) driver library and though SPI device by specified configuration.
338290
*
339291
* @param spi_flash_dev_name the name which will create SPI flash device
340292
* @param spi_dev_name using SPI device name
293+
* @param spi_cfg SPI device configuration
294+
* @param qspi_cfg QSPI device configuration
341295
*
342296
* @return probed SPI flash device, probe failed will return RT_NULL
343297
*/
344-
rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name) {
298+
rt_spi_flash_device_t rt_sfud_flash_probe_ex(const char *spi_flash_dev_name, const char *spi_dev_name,
299+
struct rt_spi_configuration *spi_cfg, struct rt_qspi_configuration *qspi_cfg)
300+
{
345301
rt_spi_flash_device_t rtt_dev = RT_NULL;
346302
sfud_flash *sfud_dev = RT_NULL;
347303
char *spi_flash_dev_name_bak = RT_NULL, *spi_dev_name_bak = RT_NULL;
348-
/* using default flash SPI configuration for initialize SPI Flash
349-
* @note you also can change the SPI to other configuration after initialized finish */
350-
struct rt_spi_configuration cfg = RT_SFUD_DEFAULT_SPI_CFG;
351304
extern sfud_err sfud_device_init(sfud_flash *flash);
352305
#ifdef SFUD_USING_QSPI
353-
struct rt_qspi_configuration qspi_cfg = RT_SFUD_DEFAULT_QSPI_CFG;
354306
struct rt_qspi_device *qspi_dev = RT_NULL;
355307
#endif
356308

@@ -380,7 +332,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
380332
/* RT-Thread SPI device initialize */
381333
rtt_dev->rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
382334
if (rtt_dev->rt_spi_device == RT_NULL || rtt_dev->rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
383-
rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
335+
LOG_E("ERROR: SPI device %s not found!", spi_dev_name);
384336
goto error;
385337
}
386338
sfud_dev->spi.name = spi_dev_name_bak;
@@ -389,12 +341,12 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
389341
/* set the qspi line number and configure the QSPI bus */
390342
if(rtt_dev->rt_spi_device->bus->mode &RT_SPI_BUS_MODE_QSPI) {
391343
qspi_dev = (struct rt_qspi_device *)rtt_dev->rt_spi_device;
392-
qspi_cfg.qspi_dl_width = qspi_dev->config.qspi_dl_width;
393-
rt_qspi_configure(qspi_dev, &qspi_cfg);
344+
qspi_cfg->qspi_dl_width = qspi_dev->config.qspi_dl_width;
345+
rt_qspi_configure(qspi_dev, qspi_cfg);
394346
}
395347
else
396348
#endif
397-
rt_spi_configure(rtt_dev->rt_spi_device, &cfg);
349+
rt_spi_configure(rtt_dev->rt_spi_device, spi_cfg);
398350
}
399351
/* SFUD flash device initialize */
400352
{
@@ -406,7 +358,7 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
406358
sfud_dev->user_data = rtt_dev;
407359
/* initialize SFUD device */
408360
if (sfud_device_init(sfud_dev) != SFUD_SUCCESS) {
409-
rt_kprintf("ERROR: SPI flash probe failed by SPI device %s.\n", spi_dev_name);
361+
LOG_E("ERROR: SPI flash probe failed by SPI device %s.", spi_dev_name);
410362
goto error;
411363
}
412364
/* when initialize success, then copy SFUD flash device's geometry to RT-Thread SPI flash device */
@@ -416,8 +368,8 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
416368
#ifdef SFUD_USING_QSPI
417369
/* reconfigure the QSPI bus for medium size */
418370
if(rtt_dev->rt_spi_device->bus->mode &RT_SPI_BUS_MODE_QSPI) {
419-
qspi_cfg.medium_size = sfud_dev->chip.capacity;
420-
rt_qspi_configure(qspi_dev, &qspi_cfg);
371+
qspi_cfg->medium_size = sfud_dev->chip.capacity;
372+
rt_qspi_configure(qspi_dev, qspi_cfg);
421373
if(qspi_dev->enter_qspi_mode != RT_NULL)
422374
qspi_dev->enter_qspi_mode(qspi_dev);
423375

@@ -442,10 +394,10 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
442394

443395
rt_device_register(&(rtt_dev->flash_device), spi_flash_dev_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
444396

445-
DEBUG_TRACE("Probe SPI flash %s by SPI device %s success.\n",spi_flash_dev_name, spi_dev_name);
397+
LOG_I("Probe SPI flash %s by SPI device %s success.",spi_flash_dev_name, spi_dev_name);
446398
return rtt_dev;
447399
} else {
448-
rt_kprintf("ERROR: Low memory.\n");
400+
LOG_E("ERROR: Low memory.");
449401
goto error;
450402
}
451403

@@ -463,6 +415,26 @@ rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const
463415
return RT_NULL;
464416
}
465417

418+
/**
419+
* Probe SPI flash by SFUD(Serial Flash Universal Driver) driver library and though SPI device.
420+
*
421+
* @param spi_flash_dev_name the name which will create SPI flash device
422+
* @param spi_dev_name using SPI device name
423+
*
424+
* @return probed SPI flash device, probe failed will return RT_NULL
425+
*/
426+
rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name)
427+
{
428+
struct rt_spi_configuration cfg = RT_SFUD_DEFAULT_SPI_CFG;
429+
#ifndef SFUD_USING_QSPI
430+
return rt_sfud_flash_probe_ex(spi_flash_dev_name, spi_dev_name, &cfg, RT_NULL);
431+
#else
432+
struct rt_qspi_configuration qspi_cfg = RT_SFUD_DEFAULT_QSPI_CFG;
433+
434+
return rt_sfud_flash_probe_ex(spi_flash_dev_name, spi_dev_name, &cfg, &qspi_cfg);
435+
#endif
436+
}
437+
466438
/**
467439
* Delete SPI flash device
468440
*
@@ -496,7 +468,7 @@ sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name)
496468

497469
rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
498470
if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
499-
rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
471+
LOG_E("ERROR: SPI device %s not found!", spi_dev_name);
500472
goto __error;
501473
}
502474

@@ -505,7 +477,7 @@ sfud_flash_t rt_sfud_flash_find(const char *spi_dev_name)
505477
sfud_dev = (sfud_flash_t) (rtt_dev->user_data);
506478
return sfud_dev;
507479
} else {
508-
rt_kprintf("ERROR: SFUD flash device not found!\n");
480+
LOG_E("ERROR: SFUD flash device not found!");
509481
goto __error;
510482
}
511483

@@ -520,15 +492,15 @@ sfud_flash_t rt_sfud_flash_find_by_dev_name(const char *flash_dev_name)
520492

521493
rtt_dev = (rt_spi_flash_device_t) rt_device_find(flash_dev_name);
522494
if (rtt_dev == RT_NULL || rtt_dev->flash_device.type != RT_Device_Class_Block) {
523-
rt_kprintf("ERROR: Flash device %s not found!\n", flash_dev_name);
495+
LOG_E("ERROR: Flash device %s not found!", flash_dev_name);
524496
goto __error;
525497
}
526498

527499
if (rtt_dev->user_data) {
528500
sfud_dev = (sfud_flash_t) (rtt_dev->user_data);
529501
return sfud_dev;
530502
} else {
531-
rt_kprintf("ERROR: SFUD flash device not found!\n");
503+
LOG_E("ERROR: SFUD flash device not found!");
532504
goto __error;
533505
}
534506

0 commit comments

Comments
 (0)