Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[components][SPI][spi-bit-ops]修复可能的异常操作 #9124

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bsp/Infineon/libraries/HAL_Drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define __DRV_SPI__

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>

#include "drv_gpio.h"

Expand Down
2 changes: 1 addition & 1 deletion bsp/airm2m/air32f103/libraries/rt_drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "board.h"
#include "drv_spi.h"

Expand Down
2 changes: 1 addition & 1 deletion bsp/apm32/libraries/Drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "board.h"
#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>

struct apm32_spi_config
{
Expand Down
2 changes: 1 addition & 1 deletion bsp/at32/libraries/rt_drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define __DRV_SPI__

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "drv_common.h"
#include "drv_dma.h"

Expand Down
2 changes: 1 addition & 1 deletion bsp/hk32/libraries/rt_drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define __DRV_SPI__

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include <hk32f0xx.h>

struct hk32_spi_config
Expand Down
2 changes: 1 addition & 1 deletion bsp/loongson/ls1cdev/drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include <rthw.h>
#include "drv_spi.h"
#include "ls1c_public.h"
Expand Down
2 changes: 1 addition & 1 deletion bsp/loongson/ls1cdev/drivers/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <stdbool.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "ls1c.h"
#include "ls1c_gpio.h"
#include "ls1c_spi.h"
Expand Down
2 changes: 1 addition & 1 deletion bsp/loongson/ls2kdev/drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <ctype.h>
#include <stdint.h>
#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "drv_spi.h"

#ifdef RT_USING_SPI
Expand Down
2 changes: 1 addition & 1 deletion bsp/raspberry-pi/raspi3-32/driver/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <rtthread.h>
#include <rtdevice.h>

//#include <drivers/spi.h>
//#include <drivers/spi_core.h>
#include "board.h"

#define SPI0_BASE_ADDR (PER_BASE + BCM283X_SPI0_BASE)
Expand Down
2 changes: 1 addition & 1 deletion bsp/raspberry-pi/raspi3-64/driver/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <rtthread.h>
#include <rtdevice.h>

//#include <drivers/spi.h>
//#include <drivers/spi_core.h>
#include "board.h"

#define SPI0_BASE_ADDR (PER_BASE + BCM283X_SPI0_BASE)
Expand Down
2 changes: 1 addition & 1 deletion bsp/simulator/drivers/sst25vfxx_mtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define SST25VFXX_MTD_H

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>

rt_err_t sst25vfxx_mtd_init(const char *spi_device_name, rt_uint32_t block_start, rt_uint32_t block_end);

Expand Down
34 changes: 2 additions & 32 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -156,36 +156,6 @@ void stm32_dir_miso(void *data, rt_int32_t state)
}
}

static void stm32_udelay(rt_uint32_t us)
{
rt_uint32_t ticks;
rt_uint32_t told, tnow, tcnt = 0;
rt_uint32_t reload = SysTick->LOAD;

ticks = us * reload / (1000000UL / RT_TICK_PER_SECOND);
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}

static void stm32_pin_init(void)
{
rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct stm32_soft_spi);
Expand All @@ -209,7 +179,7 @@ static struct rt_spi_bit_ops stm32_soft_spi_ops =
.get_miso = stm32_get_miso,
.dir_mosi = stm32_dir_mosi,
.dir_miso = stm32_dir_miso,
.udelay = stm32_udelay,
.udelay = rt_hw_us_delay,
.delay_us = 1,
};

Expand Down
16 changes: 10 additions & 6 deletions bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
spi_handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
}

cfg->usage_freq = SPI_CLOCK / (rt_size_t)pow(2,(spi_handle->Init.BaudRatePrescaler >> 28) + 1);

LOG_D("sys freq: %d, pclk freq: %d, SPI limiting freq: %d, SPI usage freq: %d",
#if defined(SOC_SERIES_STM32MP1)
HAL_RCC_GetSystemCoreClockFreq(),
Expand All @@ -203,7 +205,7 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur
#endif
SPI_CLOCK,
cfg->max_hz,
SPI_CLOCK / (rt_size_t)pow(2,(spi_handle->Init.BaudRatePrescaler >> 28) + 1));
cfg->usage_freq);

if (cfg->mode & RT_SPI_MSB)
{
Expand Down Expand Up @@ -280,7 +282,7 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur

static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
#define DMA_TRANS_MIN_LEN 10 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */
#define DMA_TRANS_MIN_LEN 10 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */

HAL_StatusTypeDef state = HAL_OK;
rt_size_t message_length, already_send_length;
Expand All @@ -294,6 +296,8 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m

struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus);
SPI_HandleTypeDef *spi_handle = &spi_drv->handle;
rt_uint32_t timeout = message->length / (1000 * spi_drv->cfg->usage_freq) + 5;


if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
{
Expand Down Expand Up @@ -424,7 +428,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
}
else
{
state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, 1000);
state = HAL_SPI_TransmitReceive(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length, timeout);
}
}
else if (message->send_buf)
Expand All @@ -435,7 +439,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
}
else
{
state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, 1000);
state = HAL_SPI_Transmit(spi_handle, (uint8_t *)send_buf, send_length, timeout);
}

if (message->cs_release && (device->config.mode & RT_SPI_3WIRE))
Expand All @@ -455,7 +459,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
{
/* clear the old error flag */
__HAL_SPI_CLEAR_OVRFLAG(spi_handle);
state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, 1000);
state = HAL_SPI_Receive(spi_handle, (uint8_t *)recv_buf, send_length, timeout);
}
}
else
Expand All @@ -482,7 +486,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
if ((spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG)) && (send_length >= DMA_TRANS_MIN_LEN))
{
/* blocking the thread,and the other tasks can run */
if (rt_completion_wait(&spi_drv->cpt, 1000) != RT_EOK)
if (rt_completion_wait(&spi_drv->cpt, timeout) != RT_EOK)
{
state = HAL_ERROR;
LOG_E("wait for DMA interrupt overtime!");
Expand Down
2 changes: 1 addition & 1 deletion bsp/tm4c123bsp/libraries/Drivers/drv_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <stdint.h>
#include <rtdevice.h>
#include <rthw.h>
#include "drivers/spi.h"
#include "drivers/spi_core.h"

rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, uint32_t portindex, uint32_t cs_gpiobase, uint32_t cs_gpio_pin);

Expand Down
2 changes: 1 addition & 1 deletion bsp/wch/arm/Libraries/ch32_drivers/drv_spi_ch32f10x.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "board.h"
#include "drv_spi.h"
#include "ch32f10x_spi.h"
Expand Down
2 changes: 1 addition & 1 deletion bsp/wch/arm/Libraries/ch32_drivers/drv_spi_ch32f20x.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include "board.h"
#include "drv_spi.h"
#include "ch32f20x_spi.h"
Expand Down
2 changes: 1 addition & 1 deletion bsp/wch/risc-v/Libraries/ch56x_drivers/ch56x_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* 2022-07-30 Emuzit first version
*/
#include <rthw.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include <drivers/pin.h>
#include "ch56x_spi.h"
#include "ch56x_sys.h"
Expand Down
2 changes: 1 addition & 1 deletion bsp/wch/risc-v/ch569w-evt/applications/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <drivers/pin.h>
#include <drivers/watchdog.h>
#include <drivers/hwtimer.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include <drivers/rt_drv_pwm.h>
#include "board.h"

Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件重命名为spi_core.h可能会引入一些软件包编译问题

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -10,8 +10,8 @@
* 2022-09-01 liYony fix api rt_spi_sendrecv16 about MSB and LSB bug
*/

#ifndef __SPI_H__
#define __SPI_H__
#ifndef __SPI_CORE_H__
#define __SPI_CORE_H__

#include <stdlib.h>
#include <rtthread.h>
Expand Down Expand Up @@ -81,6 +81,7 @@ struct rt_spi_configuration
rt_uint16_t reserved;

rt_uint32_t max_hz;
rt_uint32_t usage_freq;
};

struct rt_spi_ops;
Expand Down
4 changes: 2 additions & 2 deletions components/drivers/include/rtdevice.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down Expand Up @@ -66,7 +66,7 @@ extern "C" {
#endif /* RT_USING_RTC */

#ifdef RT_USING_SPI
#include "drivers/spi.h"
#include "drivers/spi_core.h"
#endif /* RT_USING_SPI */

#ifdef RT_USING_MTD_NOR
Expand Down
2 changes: 1 addition & 1 deletion components/drivers/spi/enc28j60.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand Down
4 changes: 2 additions & 2 deletions components/drivers/spi/enc28j60.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -12,7 +12,7 @@
#include <stdint.h>

#include <rtthread.h>
#include <drivers/spi.h>
#include <drivers/spi_core.h>
#include <netif/ethernetif.h>

// ENC28J60 Control Registers
Expand Down
4 changes: 2 additions & 2 deletions components/drivers/spi/qspi_core.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -8,7 +8,7 @@
* 2018-11-16 zylx first version.
*/

#include <drivers/spi.h>
#include <drivers/spi_core.h>

rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg)
{
Expand Down
Loading
Loading