@@ -21,6 +21,9 @@ LOG_MODULE_REGISTER(spi_numaker, CONFIG_SPI_LOG_LEVEL);
21
21
#include <NuMicro.h>
22
22
23
23
#define SPI_NUMAKER_TX_NOP 0x00
24
+ #define SPI_NUMAKER_CTL_MSK (SPI_CTL_DWIDTH_Msk | SPI_CTL_SLAVE_Msk | \
25
+ SPI_CTL_CLKPOL_Msk | SPI_CTL_RXNEG_Msk | \
26
+ SPI_CTL_TXNEG_Msk)
24
27
25
28
struct spi_numaker_config {
26
29
SPI_T * spi ;
@@ -91,6 +94,8 @@ static int spi_numaker_configure(const struct device *dev, const struct spi_conf
91
94
qsmode_tbl [mode ],
92
95
SPI_WORD_SIZE_GET (config -> operation ), config -> frequency );
93
96
} else {
97
+ /* Clear SPI CTL before set */
98
+ dev_cfg -> spi -> CTL &= ~SPI_NUMAKER_CTL_MSK ;
94
99
SPI_Open (dev_cfg -> spi ,
95
100
(SPI_OP_MODE_GET (config -> operation ) == SPI_OP_MODE_SLAVE ) ? SPI_SLAVE
96
101
: SPI_MASTER ,
@@ -137,80 +142,58 @@ static int spi_numaker_configure(const struct device *dev, const struct spi_conf
137
142
return 0 ;
138
143
}
139
144
140
- static int spi_numaker_txrx (const struct device * dev )
145
+ static int spi_numaker_txrx (const struct device * dev , uint8_t spi_dfs )
141
146
{
142
147
struct spi_numaker_data * data = dev -> data ;
143
148
const struct spi_numaker_config * dev_cfg = dev -> config ;
144
149
struct spi_context * ctx = & data -> ctx ;
145
150
uint32_t tx_frame , rx_frame ;
146
- uint8_t word_size , spi_dfs ;
147
151
uint32_t time_out_cnt ;
148
152
149
153
LOG_DBG ("%s" , __func__ );
150
- word_size = SPI_WORD_SIZE_GET (ctx -> config -> operation );
151
-
152
- switch (word_size ) {
153
- case 8 :
154
- spi_dfs = 1 ;
155
- break ;
156
- case 16 :
157
- spi_dfs = 2 ;
158
- break ;
159
- case 24 :
160
- spi_dfs = 3 ;
161
- break ;
162
- case 32 :
163
- spi_dfs = 4 ;
164
- break ;
165
- default :
166
- spi_dfs = 0 ;
167
- LOG_ERR ("Not support SPI WORD size as [%d] bits" , word_size );
168
- return - EIO ;
169
- }
170
-
171
- LOG_DBG ("%s -->word_size [%d]" , __func__ , word_size );
172
154
173
155
if (spi_context_tx_on (ctx )) {
174
156
tx_frame = ((ctx -> tx_buf == NULL ) ? SPI_NUMAKER_TX_NOP
175
- : UNALIGNED_GET ((uint8_t * )( data -> ctx .tx_buf ) ));
157
+ : UNALIGNED_GET ((uint32_t * )data -> ctx .tx_buf ));
176
158
/* Write to TX register */
177
159
SPI_WRITE_TX (dev_cfg -> spi , tx_frame );
178
160
spi_context_update_tx (ctx , spi_dfs , 1 );
179
-
180
- /* Check SPI busy status */
181
- time_out_cnt = SystemCoreClock ; /* 1 second time-out */
182
- while (SPI_IS_BUSY (dev_cfg -> spi )) {
183
- if (-- time_out_cnt == 0 ) {
184
- LOG_ERR ("Wait for SPI time-out" );
185
- return - EIO ;
186
- }
187
- }
188
-
189
161
LOG_DBG ("%s --> TX [0x%x] done" , __func__ , tx_frame );
190
162
} else {
191
163
/* Write dummy data to TX register */
192
164
SPI_WRITE_TX (dev_cfg -> spi , 0x00U );
193
- time_out_cnt = SystemCoreClock ; /* 1 second time-out */
194
- while (SPI_IS_BUSY (dev_cfg -> spi )) {
195
- if (-- time_out_cnt == 0 ) {
196
- LOG_ERR ("Wait for SPI time-out" );
197
- return - EIO ;
198
- }
199
- }
200
165
}
201
166
202
167
/* Read received data */
203
168
if (spi_context_rx_on (ctx )) {
204
169
if (SPI_GET_RX_FIFO_COUNT (dev_cfg -> spi ) > 0 ) {
205
170
rx_frame = SPI_READ_RX (dev_cfg -> spi );
206
171
if (ctx -> rx_buf != NULL ) {
207
- UNALIGNED_PUT (rx_frame , (uint8_t * )data -> ctx .rx_buf );
172
+ if (spi_dfs > 2 ) {
173
+ UNALIGNED_PUT (rx_frame ,
174
+ (uint32_t * )data -> ctx .rx_buf );
175
+ } else if (spi_dfs > 1 ) {
176
+ UNALIGNED_PUT (rx_frame ,
177
+ (uint16_t * )data -> ctx .rx_buf );
178
+ } else {
179
+ UNALIGNED_PUT (rx_frame ,
180
+ (uint8_t * )data -> ctx .rx_buf );
181
+ }
208
182
}
209
183
spi_context_update_rx (ctx , spi_dfs , 1 );
210
184
LOG_DBG ("%s --> RX [0x%x] done" , __func__ , rx_frame );
211
185
}
212
186
}
213
187
188
+ /* Check SPI busy status */
189
+ time_out_cnt = SystemCoreClock ; /* 1 second time-out */
190
+ while (SPI_IS_BUSY (dev_cfg -> spi )) {
191
+ if (-- time_out_cnt == 0 ) {
192
+ LOG_ERR ("Wait for SPI time-out" );
193
+ return - EIO ;
194
+ }
195
+ }
196
+
214
197
LOG_DBG ("%s --> exit" , __func__ );
215
198
return 0 ;
216
199
}
@@ -229,19 +212,43 @@ static int spi_numaker_transceive(const struct device *dev, const struct spi_con
229
212
struct spi_context * ctx = & data -> ctx ;
230
213
const struct spi_numaker_config * dev_cfg = dev -> config ;
231
214
int ret ;
215
+ uint8_t word_size , spi_dfs ;
232
216
233
217
LOG_DBG ("%s" , __func__ );
234
218
spi_context_lock (ctx , false, NULL , NULL , config );
235
- ctx -> config = config ;
236
219
237
220
ret = spi_numaker_configure (dev , config );
238
221
if (ret < 0 ) {
239
222
goto done ;
240
223
}
241
224
225
+ word_size = SPI_WORD_SIZE_GET (ctx -> config -> operation );
226
+
227
+ switch (word_size ) {
228
+ case 8 :
229
+ spi_dfs = 1 ;
230
+ break ;
231
+ case 16 :
232
+ spi_dfs = 2 ;
233
+ break ;
234
+ case 24 :
235
+ spi_dfs = 3 ;
236
+ break ;
237
+ case 32 :
238
+ spi_dfs = 4 ;
239
+ break ;
240
+ default :
241
+ spi_dfs = 0 ;
242
+ LOG_ERR ("Not support SPI WORD size as [%d] bits" , word_size );
243
+ ret = - ENOTSUP ;
244
+ goto done ;
245
+ }
246
+
247
+ LOG_DBG ("%s -->word_size [%d]" , __func__ , word_size );
248
+
242
249
SPI_ENABLE (dev_cfg -> spi );
243
250
244
- spi_context_buffers_setup (& data -> ctx , tx_bufs , rx_bufs , 1 );
251
+ spi_context_buffers_setup (& data -> ctx , tx_bufs , rx_bufs , spi_dfs );
245
252
246
253
/* if cs is defined: software cs control, set active true */
247
254
if (spi_cs_is_gpio (config )) {
@@ -250,7 +257,7 @@ static int spi_numaker_transceive(const struct device *dev, const struct spi_con
250
257
251
258
/* transceive tx/rx data */
252
259
do {
253
- ret = spi_numaker_txrx (dev );
260
+ ret = spi_numaker_txrx (dev , spi_dfs );
254
261
if (ret < 0 ) {
255
262
break ;
256
263
}
0 commit comments