Skip to content

Commit 69c2678

Browse files
CYFS3Rbb666
authored andcommitted
[bsp/renesas][drivers] Fix SPI runtime configuration
1 parent fcc4d6c commit 69c2678

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

  • bsp/renesas/libraries/HAL_Drivers/drivers

bsp/renesas/libraries/HAL_Drivers/drivers/drv_spi.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static struct rt_event complete_event = { 0 };
3636
#define R_SPI_Read R_SPI_B_Read
3737
#define R_SPI_WriteRead R_SPI_B_WriteRead
3838
#define R_SPI_Open R_SPI_B_Open
39+
#define R_SPI_Close R_SPI_B_Close
3940
#define spi_extended_cfg_t spi_b_extended_cfg_t
4041

4142
#elif defined(SOC_SERIES_R7SA6W1)
@@ -44,6 +45,7 @@ static struct rt_event complete_event = { 0 };
4445
#define R_SPI_Read R_SPI_W_Read
4546
#define R_SPI_WriteRead R_SPI_W_WriteRead
4647
#define R_SPI_Open R_SPI_W_Open
48+
#define R_SPI_Close R_SPI_W_Close
4749
#define spi_extended_cfg_t spi_w_extended_cfg_t
4850

4951
#endif
@@ -75,6 +77,8 @@ static struct ra_spi_handle spi_handle[] = {
7577
};
7678

7779
static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = { 0 };
80+
static spi_cfg_t fsp_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = { 0 };
81+
static spi_extended_cfg_t fsp_extended_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = { 0 };
7882

7983
void spi0_callback(spi_callback_args_t *p_args)
8084
{
@@ -232,17 +236,32 @@ static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
232236
rt_err_t err = RT_EOK;
233237

234238
struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
239+
rt_size_t index = spi_dev - spi_config;
240+
spi_cfg_t *fsp_cfg = &fsp_config[index];
241+
spi_extended_cfg_t *spi_cfg = &fsp_extended_config[index];
235242

236243
/**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
237244
rt_uint8_t data_width = configuration->data_width / 8;
238245
RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
239246
configuration->data_width = configuration->data_width / 8;
240247
spi_dev->rt_spi_cfg_t = configuration;
241248

242-
spi_extended_cfg_t *spi_cfg = (spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
249+
*fsp_cfg = *spi_dev->ra_spi_handle_t->spi_cfg_t;
250+
*spi_cfg = *(const spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
251+
fsp_cfg->p_extend = spi_cfg;
243252

244253
/**< Configure Select Line */
245-
rt_pin_write(device->cs_pin, PIN_HIGH);
254+
if (!(configuration->mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
255+
{
256+
if (configuration->mode & RT_SPI_CS_HIGH)
257+
{
258+
rt_pin_write(device->cs_pin, PIN_LOW);
259+
}
260+
else
261+
{
262+
rt_pin_write(device->cs_pin, PIN_HIGH);
263+
}
264+
}
246265

247266
/**< config bitrate */
248267
#if defined(SOC_SERIES_R7FA8M85) || defined(SOC_SERIES_R7KA8P1)
@@ -256,7 +275,12 @@ static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
256275
#endif
257276

258277
/**< init */
259-
err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, (spi_cfg_t const * const)spi_dev->ra_spi_handle_t->spi_cfg_t);
278+
err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, fsp_cfg);
279+
if (err == FSP_ERR_ALREADY_OPEN)
280+
{
281+
R_SPI_Close((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t);
282+
err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, fsp_cfg);
283+
}
260284
/* handle error */
261285
if (RT_EOK != err)
262286
{

0 commit comments

Comments
 (0)