Skip to content

Commit ce9132c

Browse files
committed
[bsp/nrf5x]fixed spi driver bugs
1 parent 60e9808 commit ce9132c

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

bsp/nrf5x/libraries/drivers/drv_spi.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2020-05-22 Sherman first version
9+
* 2020-11-02 xckhmf fixed bug
910
*/
1011

1112
#include <stdint.h>
@@ -19,6 +20,7 @@
1920

2021
#ifdef BSP_USING_SPI
2122

23+
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
2224
static struct nrfx_drv_spi_config spi_config[] =
2325
{
2426
#ifdef BSP_USING_SPI0
@@ -117,12 +119,12 @@ static rt_err_t spi_configure(struct rt_spi_device *device,
117119

118120
nrfx_spi_t spi = spi_bus_obj[index].spi;
119121
nrfx_spi_config_t config = NRFX_SPI_DEFAULT_CONFIG(bsp_spi_pin[index].sck_pin,
120-
bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, bsp_spi_pin[index].ss_pin);
122+
bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, NRFX_SPI_PIN_NOT_USED);
121123

122124
/* spi config ss pin */
123-
if(device->user_data != RT_NULL)
125+
if(device->parent.user_data != RT_NULL)
124126
{
125-
config.ss_pin = (rt_uint8_t)device->user_data;
127+
nrf_gpio_cfg_output((uint32_t)device->parent.user_data);
126128
}
127129
/* spi config bit order */
128130
if(configuration->mode & RT_SPI_MSB)
@@ -198,32 +200,43 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
198200
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
199201

200202
rt_uint8_t index = spi_index_find(device->bus);
203+
nrfx_err_t nrf_ret;
201204
RT_ASSERT(index != 0xFF);
202205

203206
nrfx_spi_t * p_instance = &spi_bus_obj[index].spi;
204207
nrfx_spi_xfer_desc_t p_xfer_desc;
208+
209+
if(message->cs_take == 1)
210+
{
211+
nrf_gpio_pin_clear((uint32_t)device->parent.user_data);
212+
}
213+
p_xfer_desc.p_rx_buffer = message->recv_buf;
214+
p_xfer_desc.rx_length = message->length;
215+
p_xfer_desc.p_tx_buffer = message->send_buf;
216+
p_xfer_desc.tx_length = message->length ;
205217
if(message->send_buf == RT_NULL)
206218
{
207-
p_xfer_desc.p_rx_buffer = message->recv_buf;
208-
p_xfer_desc.rx_length = message->length;
209-
210-
p_xfer_desc.p_tx_buffer = RT_NULL;
211219
p_xfer_desc.tx_length = 0;
212220
}
213-
else
221+
if(message->recv_buf == RT_NULL)
214222
{
215-
p_xfer_desc.p_tx_buffer = message->send_buf;
216-
p_xfer_desc.tx_length = message->length ;
217-
218-
p_xfer_desc.p_rx_buffer = RT_NULL;
219-
p_xfer_desc.rx_length = 0;
223+
p_xfer_desc.rx_length = 0;
220224
}
221225

222-
nrfx_err_t nrf_ret = nrfx_spi_xfer(p_instance, &p_xfer_desc, 0);
223-
if( NRFX_SUCCESS == nrf_ret)
224-
return message->length;
225-
else
226+
nrf_ret = nrfx_spi_xfer(p_instance, &p_xfer_desc, 0);
227+
if(message->cs_release == 1)
228+
{
229+
nrf_gpio_pin_set((uint32_t)device->parent.user_data);
230+
}
231+
232+
if( NRFX_SUCCESS != nrf_ret)
233+
{
226234
return 0;
235+
}
236+
else
237+
{
238+
return message->length;
239+
}
227240
}
228241

229242
/* spi bus callback function */
@@ -260,15 +273,15 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
260273
{
261274
RT_ASSERT(bus_name != RT_NULL);
262275
RT_ASSERT(device_name != RT_NULL);
276+
RT_ASSERT(ss_pin != RT_NULL);
263277

264278
rt_err_t result;
265279
struct rt_spi_device *spi_device;
266280
/* attach the device to spi bus*/
267281
spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
268282
RT_ASSERT(spi_device != RT_NULL);
269283
/* initialize the cs pin */
270-
spi_device->user_data = (void*)ss_pin;
271-
result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);
284+
result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void*)ss_pin);
272285
if (result != RT_EOK)
273286
{
274287
LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
@@ -278,4 +291,5 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
278291
return result;
279292
}
280293

294+
#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
281295
#endif /*BSP_USING_SPI*/

0 commit comments

Comments
 (0)