@@ -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 ---------------------------------------------------------*/
6887static 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-
8391static 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 ();
0 commit comments