# How to read/write to an SPI bus without a while(1)-loop?

**URL:** <https://forum.legato.io/t/how-to-read-write-to-an-spi-bus-without-a-while-1-loop/2998>\
**Category:** Uncategorized\
**Created:** [January 31, 2018, 7:23pm UTC](https://forum.legato.io/t/how-to-read-write-to-an-spi-bus-without-a-while-1-loop/2998 "2018-01-31T19:23:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![annaertd](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@annaertd](https://forum.legato.io/u/annaertd)\
**Post date:** [January 31, 2018, 7:23pm UTC](https://forum.legato.io/t/how-to-read-write-to-an-spi-bus-without-a-while-1-loop/2998/1 "2018-01-31T19:23:26Z")

</div>

I use Legato 16.10.4 on my mangOH Green board with an WP8548 target.  
I successfully managed to install _spidev_ and _spisvc_ (I’ve built my own kernel module spisvc.ko based on the yocto-install).  
The spiService application is running and I can call _le\_spi\_Open()_ and _le\_spi\_Configure()_ from withing the routine COMPONENT\_INIT.  
But, how I can handle the different SPI read and write calls?

The Legato docs indicate that a COMPONENT\_INIT should always return, so, I can’t use a while(1).  
I’ve used the _le\_fdMonitor_ API to handle my different UART read and write calls, using the file descriptor that I received from my _le\_tty\_Open()_ call, and that worked fine.  
But, I think I can’t use le\_fdMonitor for my SPI handling because SPI works with an _le\_spi\_DeviceHandle_ instead of a file descriptor.

Any suggestion to handle my different SPI read and write calls?

Greetings,  
annaertd

---

<div class="post-metadata">

**Author:** ![muralinagraj](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@muralinagraj](https://forum.legato.io/u/muralinagraj)\
**Post date:** [February 3, 2018, 11:52am UTC](https://forum.legato.io/t/how-to-read-write-to-an-spi-bus-without-a-while-1-loop/2998/2 "2018-02-03T11:52:51Z")

</div>

Hi,  
Yes, COMPONENT\_INIT should always return and we should not use any infinite loop(while(1)) inside that.  
Coming to your use case “to handle my different SPI read and write calls” , I can suggest you to create two different threads in the COMPONENT\_INIT to handle the read and write calls. See that i have attached the example application code[thread\_test.c](https://forum.legato.io/uploads/short-url/nnSAe9ACsiUMZy5CnTSLMWf11DF.c) (936 Bytes)  
where i am creating 2 threads with while loop inside . But be careful using while(1) loop.

Regards,  
Muralidhara N.
