Skip to content

Commit 334e0b5

Browse files
committed
[drv] Add parameter "flags" to SPI read / write functions
- Temporarily raise the priority of "Arduino" thread when startup
1 parent 958a5a2 commit 334e0b5

File tree

17 files changed

+179
-138
lines changed

17 files changed

+179
-138
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RT-Thread
2-
version=0.6.1
2+
version=0.6.2
33
author=Bernard Xiong <[email protected]>, onelife <[email protected]>
44
maintainer=onelife <[email protected]>
55
sentence=Real Time Operating System porting for Arduino SAM and SAMD boards

src/components/arduino/drv_spi.cpp

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ extern "C" {
3535
# include "components/utilities/ulog/ulog.h"
3636
#else /* RT_USING_ULOG */
3737
# define LOG_E(format, args...) rt_kprintf(format "\n", ##args)
38+
# define LOG_W LOG_E
3839
# ifdef BSP_SPI_DEBUG
39-
# define LOG_D(format, args...) rt_kprintf(format "\n", ##args)
40+
# define LOG_I(format, args...) rt_kprintf(format "\n", ##args)
4041
# else
41-
# define LOG_D(format, args...)
42+
# define LOG_I(format, args...)
4243
# endif
44+
# define LOG_D LOG_I
45+
# define LOG_HEX(format, args...)
4346
#endif /* RT_USING_ULOG */
4447

4548
#if CONFIG_USING_SPI0
@@ -51,10 +54,20 @@ extern "C" {
5154
#define SPI_NAME(ch) "SPI"#ch
5255
#define SPI_CTX(idx) spi_ctx[idx]
5356
#define SPI_DEV(ctx) ((SPIClass *)((ctx)->ldev))
54-
#define SPI_START(ctx) LOG_D("[SPI%d] START", ctx->chn); \
55-
SPI_DEV(ctx)->beginTransaction(ctx->set)
56-
#define SPI_STOP(ctx) LOG_D("[SPI%d] STOP", ctx->chn); \
57-
SPI_DEV(ctx)->endTransaction()
57+
#define SPI_START(ctx) { \
58+
if (!ctx->start) { \
59+
LOG_D("[SPI%d] START", ctx->chn); \
60+
SPI_DEV(ctx)->beginTransaction(ctx->set); \
61+
ctx->start = RT_TRUE; \
62+
} \
63+
}
64+
#define SPI_STOP(ctx) { \
65+
if (ctx->start) { \
66+
LOG_D("[SPI%d] STOP", ctx->chn); \
67+
SPI_DEV(ctx)->endTransaction(); \
68+
ctx->start = RT_FALSE; \
69+
} \
70+
}
5871
#define SPI_TX(ctx, data) (void)SPI_DEV(ctx)->transfer((rt_uint8_t)data)
5972
#define SPI_RX(ctx) SPI_DEV(ctx)->transfer(0xff)
6073
#define SPI_SET_SPEED(ctx, target) \
@@ -64,28 +77,22 @@ if (target != ctx->spd) { \
6477
LOG_D("[SPI%d] speed=%d", ctx->chn, target); \
6578
}
6679

80+
#define WAIT_IDLE(flags) (!!(flags & 0x000000ff))
81+
#define WAIT_READ(flags) (!!(flags & 0x0000ff00))
82+
#define IS_PENDING(flags) (!!(flags & SPI_FLAG_MORE))
83+
#define IDLE_TOKEN(flags) (rt_uint8_t)(flags & 0x000000ff)
84+
#define READ_TOKEN(flags) (rt_uint8_t)((flags & 0x0000ff00) >> 8)
85+
6786
/* Private variables ---------------------------------------------------------*/
6887
static struct bsp_spi_contex spi_ctx[CH_NUM];
6988

7089
/* Private function prototypes -----------------------------------------------*/
7190
/* Private functions ---------------------------------------------------------*/
72-
static rt_bool_t wait_idle(struct bsp_spi_contex *ctx) {
73-
rt_uint32_t i;
74-
75-
for (i = 0; i < SPI_DEFAULT_LIMIT; i++) {
76-
if (0xff == SPI_RX(ctx)) break;
77-
}
78-
79-
LOG_D("[SPI%d] wait %d", ctx->chn, i);
80-
return (i < SPI_DEFAULT_LIMIT);
81-
}
82-
8391
static rt_bool_t wait_token(struct bsp_spi_contex *ctx, rt_uint8_t token) {
8492
rt_uint32_t i;
8593

86-
for (i = 0; i < SPI_DEFAULT_LIMIT; i++) {
94+
for (i = 0; i < SPI_DEFAULT_LIMIT; i++)
8795
if (token == SPI_RX(ctx)) break;
88-
}
8996

9097
LOG_D("[SPI%d] wait token %d", ctx->chn, i);
9198
return (i < SPI_DEFAULT_LIMIT);
@@ -126,7 +133,7 @@ static rt_err_t bsp_spi_close(rt_device_t dev) {
126133
return ret;
127134
}
128135

129-
static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t token, void *buf,
136+
static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t flags, void *buf,
130137
rt_size_t size) {
131138
struct bsp_spi_contex *ctx = (struct bsp_spi_contex *)(dev->user_data);
132139
rt_bool_t locked;
@@ -170,13 +177,15 @@ static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t token, void *buf,
170177

171178
/* wait for idle */
172179
SPI_START(ctx);
173-
for (i = 0; i < SPI_DEFAULT_RETRY; i++)
174-
if (wait_idle(ctx)) break;
175-
if (i >= SPI_DEFAULT_RETRY) {
176-
SPI_STOP(ctx);
177-
err = -RT_EBUSY;
178-
LOG_W("[SPI%d E] read busy", ctx->chn);
179-
break;
180+
if (WAIT_IDLE(flags)) {
181+
for (i = 0; i < SPI_DEFAULT_RETRY; i++)
182+
if (wait_token(ctx, IDLE_TOKEN(flags))) break;
183+
if (i >= SPI_DEFAULT_RETRY) {
184+
SPI_STOP(ctx);
185+
err = -RT_EBUSY;
186+
LOG_W("[SPI%d E] read busy", ctx->chn);
187+
break;
188+
}
180189
}
181190

182191
/* send instruction */
@@ -186,9 +195,9 @@ static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t token, void *buf,
186195

187196
/* receive data */
188197
LOG_D("[SPI%d] rx data [%d]", ctx->chn, size);
189-
if (0 != token) {
198+
if (WAIT_READ(flags)) {
190199
for (i = 0; i < SPI_DEFAULT_RETRY; i++)
191-
if (wait_token(ctx, (rt_uint8_t)token)) break;
200+
if (wait_token(ctx, READ_TOKEN(flags))) break;
192201
if (i >= SPI_DEFAULT_RETRY) {
193202
SPI_STOP(ctx);
194203
err = -RT_EIO;
@@ -198,7 +207,7 @@ static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t token, void *buf,
198207
}
199208
for (i = 0; i < size; i++)
200209
*(rx_buf++) = SPI_RX(ctx);
201-
SPI_STOP(ctx);
210+
if (!IS_PENDING(flags)) SPI_STOP(ctx);
202211

203212
ret = size;
204213
}
@@ -210,14 +219,13 @@ static rt_size_t bsp_spi_read(rt_device_t dev, rt_off_t token, void *buf,
210219
return ret;
211220
}
212221

213-
static rt_size_t bsp_spi_write(rt_device_t dev, rt_off_t pos, const void *buf,
222+
static rt_size_t bsp_spi_write(rt_device_t dev, rt_off_t flags, const void *buf,
214223
rt_size_t size) {
215224
struct bsp_spi_contex *ctx = (struct bsp_spi_contex *)(dev->user_data);
216225
rt_bool_t locked;
217226
rt_err_t err;
218227
rt_size_t ret;
219228

220-
(void)pos;
221229
if (RT_NULL == buf) return -RT_EINVAL;
222230
if (RT_DEVICE_OFLAG_RDONLY == (ctx->dev.open_flag & 0x0003))
223231
return -RT_EINVAL;
@@ -246,13 +254,15 @@ static rt_size_t bsp_spi_write(rt_device_t dev, rt_off_t pos, const void *buf,
246254

247255
/* busy wait */
248256
SPI_START(ctx);
249-
for (i = 0; i < SPI_DEFAULT_RETRY; i++)
250-
if (wait_idle(ctx)) break;
251-
if (i >= SPI_DEFAULT_RETRY) {
252-
SPI_STOP(ctx);
253-
err = -RT_EBUSY;
254-
LOG_W("[SPI%d E] write busy", ctx->chn);
255-
break;
257+
if (WAIT_IDLE(flags)) {
258+
for (i = 0; i < SPI_DEFAULT_RETRY; i++)
259+
if (wait_token(ctx, (IDLE_TOKEN(flags)))) break;
260+
if (i >= SPI_DEFAULT_RETRY) {
261+
SPI_STOP(ctx);
262+
err = -RT_EBUSY;
263+
LOG_W("[SPI%d E] write busy", ctx->chn);
264+
break;
265+
}
256266
}
257267

258268
/* send instruction */
@@ -264,7 +274,7 @@ static rt_size_t bsp_spi_write(rt_device_t dev, rt_off_t pos, const void *buf,
264274
LOG_D("[SPI%d] tx data [%d]", ctx->chn, size);
265275
for (i = 0; i < size; i++)
266276
SPI_TX(ctx, *(tx_buf + i));
267-
SPI_STOP(ctx);
277+
if (!IS_PENDING(flags)) SPI_STOP(ctx);
268278

269279
ret = size;
270280
} while (0);
@@ -318,13 +328,13 @@ static rt_err_t bsp_spi_control(rt_device_t dev, rt_int32_t cmd, void *args) {
318328
* @return rt_err_t - Error code
319329
*
320330
******************************************************************************/
321-
static rt_err_t bsp_spi_contex_init(struct bsp_spi_contex *ctx,
322-
rt_uint8_t chn, const char *name, rt_uint8_t cfg, void *ldev) {
331+
static rt_err_t bsp_spi_contex_init(struct bsp_spi_contex *ctx, rt_uint8_t chn,
332+
const char *name, void *ldev) {
323333
rt_err_t ret;
324334

325335
do {
326336
ctx->chn = chn;
327-
ctx->cfg = cfg;
337+
ctx->start = RT_FALSE;
328338
SPI_SET_SPEED(ctx, SPI_DEFAULT_SPEED);
329339
ctx->ldev = ldev;
330340

@@ -391,8 +401,7 @@ rt_err_t bsp_hw_spi_init(void) {
391401
return -RT_ERROR;
392402
}
393403

394-
ret = bsp_spi_contex_init(&SPI_CTX(i), chn, name, SPI_DEFAULT_CONFIG,
395-
ldev);
404+
ret = bsp_spi_contex_init(&SPI_CTX(i), chn, name, ldev);
396405
if (RT_EOK != ret) break;
397406

398407
SPI_DEV(&SPI_CTX(i))->begin();

src/components/arduino/drv_spi.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
#include "drv_common.h"
1212

1313
/* Exported defines ----------------------------------------------------------*/
14-
#define SPI_DEFAULT_CONFIG (SPI_CONFIG_MASTER)
1514
#define SPI_DEFAULT_SPEED (250000)
15+
#define SPI_MAX_SPEED (24000000)
1616
#define SPI_DEFAULT_RETRY (3)
1717
#define SPI_DEFAULT_LIMIT (512)
18-
19-
#define SPI_CONFIG_MASTER (1 << 0) /* Master mode */
18+
#define SPI_FLAG_MORE (rt_uint32_t)(0x01 << 16)
19+
#define SPI_FLAG_READ_TOKEN(tk) (rt_uint32_t)((tk & 0xff) << 8)
20+
#define SPI_FLAG_IDLE_TOKEN(tk) (rt_uint32_t)((tk & 0xff) << 0)
2021

2122
/* Exported types ------------------------------------------------------------*/
2223
enum bsp_spi_channel {
@@ -31,7 +32,7 @@ enum bsp_spi_channel {
3132

3233
struct bsp_spi_contex {
3334
rt_uint8_t chn; /* channel number */
34-
rt_uint8_t cfg; /* config */
35+
rt_bool_t start;
3536
rt_uint32_t spd; /* speed */
3637
SPISettings set; /* setting */
3738
void *ldev; /* lower level device (Arduino SPI) */

0 commit comments

Comments
 (0)