@@ -38,6 +38,9 @@ rt_err_t spi_bus_register(struct rt_spi_bus *bus,
3838
3939 /* initialize mutex lock */
4040 rt_mutex_init (& (bus -> lock ), name , RT_IPC_FLAG_PRIO );
41+ #ifdef RT_USING_SPI_ISR
42+ rt_spin_lock_init (& bus -> _spinlock );
43+ #endif /* RT_USING_SPI_ISR */
4144 /* set ops */
4245 bus -> ops = ops ;
4346 /* initialize owner */
@@ -164,13 +167,53 @@ rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device)
164167 return rt_spi_bus_detach_device_cspin (device );
165168}
166169
170+ static rt_err_t spi_lock (struct rt_spi_bus * bus )
171+ {
172+ RT_ASSERT (bus );
173+
174+ rt_err_t ret = - RT_ERROR ;
175+ /* If the scheduler is started and in thread context */
176+ if (rt_scheduler_is_available ())
177+ {
178+ ret = rt_mutex_take (& (bus -> lock ), RT_WAITING_FOREVER );
179+ }
180+ else
181+ {
182+ #ifdef RT_USING_SPI_ISR
183+ bus -> _isr_lvl = rt_spin_lock_irqsave (& bus -> _spinlock );
184+ ret = RT_EOK ;
185+ #endif /* RT_USING_SPI_ISR */
186+ }
187+ return ret ;
188+ }
189+
190+ static rt_err_t spi_unlock (struct rt_spi_bus * bus )
191+ {
192+ RT_ASSERT (bus );
193+
194+ rt_err_t ret = - RT_ERROR ;
195+ /* If the scheduler is started and in thread context */
196+ if (rt_scheduler_is_available ())
197+ {
198+ ret = rt_mutex_release (& (bus -> lock ));
199+ }
200+ else
201+ {
202+ #ifdef RT_USING_SPI_ISR
203+ rt_spin_unlock_irqrestore (& bus -> _spinlock , bus -> _isr_lvl );
204+ ret = RT_EOK ;
205+ #endif /* RT_USING_SPI_ISR */
206+ }
207+ return ret ;
208+ }
209+
167210rt_err_t rt_spi_bus_configure (struct rt_spi_device * device )
168211{
169212 rt_err_t result = - RT_ERROR ;
170213
171214 if (device -> bus != RT_NULL )
172215 {
173- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
216+ result = spi_lock ( device -> bus );
174217 if (result == RT_EOK )
175218 {
176219 if (device -> bus -> owner == device )
@@ -191,7 +234,7 @@ rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
191234 result = - RT_EBUSY ;
192235 }
193236 /* release lock */
194- rt_mutex_release ( & ( device -> bus -> lock ) );
237+ spi_unlock ( device -> bus );
195238 }
196239 }
197240 else
@@ -211,7 +254,7 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device,
211254 /* reset the CS pin */
212255 if (device -> cs_pin != PIN_NONE )
213256 {
214- rt_err_t result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
257+ rt_err_t result = spi_lock ( device -> bus );
215258 if (result == RT_EOK )
216259 {
217260 if (cfg -> mode & RT_SPI_CS_HIGH )
@@ -222,7 +265,7 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device,
222265 {
223266 rt_pin_write (device -> cs_pin , PIN_HIGH );
224267 }
225- rt_mutex_release ( & ( device -> bus -> lock ) );
268+ spi_unlock ( device -> bus );
226269 }
227270 else
228271 {
@@ -258,7 +301,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
258301 RT_ASSERT (device != RT_NULL );
259302 RT_ASSERT (device -> bus != RT_NULL );
260303
261- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
304+ result = spi_lock ( device -> bus );
262305 if (result == RT_EOK )
263306 {
264307 if (device -> bus -> owner != device )
@@ -316,7 +359,7 @@ rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
316359 }
317360
318361__exit :
319- rt_mutex_release ( & ( device -> bus -> lock ) );
362+ spi_unlock ( device -> bus );
320363
321364 return result ;
322365}
@@ -333,7 +376,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
333376 RT_ASSERT (device != RT_NULL );
334377 RT_ASSERT (device -> bus != RT_NULL );
335378
336- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
379+ result = spi_lock ( device -> bus );
337380 if (result == RT_EOK )
338381 {
339382 if (device -> bus -> owner != device )
@@ -391,7 +434,7 @@ rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
391434 }
392435
393436__exit :
394- rt_mutex_release ( & ( device -> bus -> lock ) );
437+ spi_unlock ( device -> bus );
395438
396439 return result ;
397440}
@@ -407,7 +450,7 @@ rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
407450 RT_ASSERT (device != RT_NULL );
408451 RT_ASSERT (device -> bus != RT_NULL );
409452
410- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
453+ result = spi_lock ( device -> bus );
411454 if (result == RT_EOK )
412455 {
413456 if (device -> bus -> owner != device )
@@ -449,7 +492,7 @@ rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
449492 }
450493
451494__exit :
452- rt_mutex_release ( & ( device -> bus -> lock ) );
495+ spi_unlock ( device -> bus );
453496
454497 return result ;
455498}
@@ -510,7 +553,7 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
510553 if (index == RT_NULL )
511554 return index ;
512555
513- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
556+ result = spi_lock ( device -> bus );
514557 if (result != RT_EOK )
515558 {
516559 return index ;
@@ -548,7 +591,7 @@ struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
548591
549592__exit :
550593 /* release bus lock */
551- rt_mutex_release ( & ( device -> bus -> lock ) );
594+ spi_unlock ( device -> bus );
552595
553596 return index ;
554597}
@@ -560,7 +603,7 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
560603 RT_ASSERT (device != RT_NULL );
561604 RT_ASSERT (device -> bus != RT_NULL );
562605
563- result = rt_mutex_take ( & ( device -> bus -> lock ), RT_WAITING_FOREVER );
606+ result = spi_lock ( device -> bus );
564607 if (result != RT_EOK )
565608 {
566609 return - RT_EBUSY ;
@@ -579,7 +622,7 @@ rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
579622 else
580623 {
581624 /* configure SPI bus failed */
582- rt_mutex_release ( & ( device -> bus -> lock ) );
625+ spi_unlock ( device -> bus );
583626
584627 return result ;
585628 }
@@ -595,7 +638,7 @@ rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
595638 RT_ASSERT (device -> bus -> owner == device );
596639
597640 /* release lock */
598- return rt_mutex_release ( & ( device -> bus -> lock ) );
641+ return spi_unlock ( device -> bus );
599642}
600643
601644rt_err_t rt_spi_take (struct rt_spi_device * device )
0 commit comments