You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Felipe Moura Oliveira <mo...@gmail.com> on 2024/10/15 00:08:44 UTC
The right way to implement an event-driven application - SPI SLAVE
The right way to implement an event-driven application - SPI SLAVE Hello
everyone, in the last few weeks I have been developing a solution in
esp32c6 that communicates with another mcu from an SPI, the application I
am developing is an spi slave. Up until now, my application has been
executing reads inside a while(1) (I know, this mode is very dummy) and
when I receive n_bytes > 0 I have been handling this. Now I need to improve
my solution so that my application stays in "sleep" and when an spi
interruption occurs it can continue with the execution. I am having
difficulty understanding the right way to do this in Nuttx. Has anyone done
something similar or have tips on where I should go next?
Regards!
--
*Felipe Moura de Oliveira*
*Universidade Federal de Minas Gerais*
Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
<https://twitter.com/FelipeMOliveir?lang=pt-br>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Tomek CEDRO <to...@cedro.info>.
Awesome! Thank you Xiang :-)
--
CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
On Tue, Oct 15, 2024, 16:21 Xiang Xiao <xi...@gmail.com> wrote:
> Yes, we use DMA for both SPI master/slave, but DMA is more related to the
> driver not the framework.
>
> On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
>
> > One question by the way someone asked me recently - can NuttX SPI Slave
> > work in DMA mode? :-)
> >
> > --
> > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
> >
>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Xiang Xiao <xi...@gmail.com>.
Please create an issue on github, I can add Donny or other people to ask
your question.
On Wed, Oct 16, 2024 at 1:27 AM Felipe Moura Oliveira <mo...@gmail.com>
wrote:
> Hello Xiang Xiao,
>
> Can you connect me with Donny or can you help me to understand spi_slave
> semaphore "unlock" ?
>
> Looking into spi_slave_driver.c:330
> while (priv->rx_length == 0)
> {
> remaining_words = SPIS_CTRLR_QPOLL(priv->ctrlr);
> if (remaining_words == 0)
> {
> spiinfo("All words retrieved!\n");
> }
> else
> {
> spiinfo("%zu words left in the buffer\n", remaining_words);
> }
>
> if (priv->rx_length == 0)
> {
> nxmutex_unlock(&priv->lock);
> if (filep->f_oflags & O_NONBLOCK)
> {
> return -EAGAIN;
> }
>
> ret = nxsem_wait(&priv->wait);
> if (ret < 0)
> {
> return ret;
> }
>
> ret = nxmutex_lock(&priv->lock);
> if (ret < 0)
> {
> spierr("Failed to get exclusive access: %d\n", ret);
> return ret;
> }
> }
> }
>
> we have:
> ret = nxsem_wait(&priv->wait);
>
> I think read operation will be locked until the function spi_slave_notify
> has been called, but I cannot find any call of SPIS_DEV_NOTIFY in nuttx
> repo. Did you have different implementation in your side ?
>
> Em ter., 15 de out. de 2024 às 12:28, Felipe Moura Oliveira <
> moura.fmo@gmail.com> escreveu:
>
> > Hello All,
> >
> > I did a dummy question, in master branch, spi slave implementation
> already
> > blocked until new data is available. I wasn't working in the master
> branch
> > but I will switch to.
> >
> > Em ter., 15 de out. de 2024 às 11:45, Felipe Moura Oliveira <
> > moura.fmo@gmail.com> escreveu:
> >
> >> Hello Xiang Xiao,
> >>
> >> thanks for sharing with me about rpmsg, to be honest it is new for me
> =/,
> >> I was locked in baremetal for several years. For now I cannot use it
> >> because my host mcu is running baremetal (maybe in the future we will
> >> change it). I developed a simple protocol using ack and nak whey to
> handle
> >> communication with master and slave. About "lock" my app until data is
> >> available, I am thinking use semaphore to wait to do it, so my solution
> >> will be able to sleep my mcu until spi releases it.
> >>
> >> Em ter., 15 de out. de 2024 às 11:24, Xiang Xiao <
> >> xiaoxiang781216@gmail.com> escreveu:
> >>
> >>> Yes, we use DMA for both SPI master/slave, but DMA is more related to
> the
> >>> driver not the framework.
> >>>
> >>> On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
> >>>
> >>> > One question by the way someone asked me recently - can NuttX SPI
> Slave
> >>> > work in DMA mode? :-)
> >>> >
> >>> > --
> >>> > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
> >>> >
> >>>
> >>
> >>
> >> --
> >> *Felipe Moura de Oliveira*
> >> *Universidade Federal de Minas Gerais*
> >> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> >> <https://twitter.com/FelipeMOliveir?lang=pt-br>
> >>
> >
> >
> > --
> > *Felipe Moura de Oliveira*
> > *Universidade Federal de Minas Gerais*
> > Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> > <https://twitter.com/FelipeMOliveir?lang=pt-br>
> >
>
>
> --
> *Felipe Moura de Oliveira*
> *Universidade Federal de Minas Gerais*
> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Felipe Moura Oliveira <mo...@gmail.com>.
Hello Xiang Xiao,
Can you connect me with Donny or can you help me to understand spi_slave
semaphore "unlock" ?
Looking into spi_slave_driver.c:330
while (priv->rx_length == 0)
{
remaining_words = SPIS_CTRLR_QPOLL(priv->ctrlr);
if (remaining_words == 0)
{
spiinfo("All words retrieved!\n");
}
else
{
spiinfo("%zu words left in the buffer\n", remaining_words);
}
if (priv->rx_length == 0)
{
nxmutex_unlock(&priv->lock);
if (filep->f_oflags & O_NONBLOCK)
{
return -EAGAIN;
}
ret = nxsem_wait(&priv->wait);
if (ret < 0)
{
return ret;
}
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
spierr("Failed to get exclusive access: %d\n", ret);
return ret;
}
}
}
we have:
ret = nxsem_wait(&priv->wait);
I think read operation will be locked until the function spi_slave_notify
has been called, but I cannot find any call of SPIS_DEV_NOTIFY in nuttx
repo. Did you have different implementation in your side ?
Em ter., 15 de out. de 2024 às 12:28, Felipe Moura Oliveira <
moura.fmo@gmail.com> escreveu:
> Hello All,
>
> I did a dummy question, in master branch, spi slave implementation already
> blocked until new data is available. I wasn't working in the master branch
> but I will switch to.
>
> Em ter., 15 de out. de 2024 às 11:45, Felipe Moura Oliveira <
> moura.fmo@gmail.com> escreveu:
>
>> Hello Xiang Xiao,
>>
>> thanks for sharing with me about rpmsg, to be honest it is new for me =/,
>> I was locked in baremetal for several years. For now I cannot use it
>> because my host mcu is running baremetal (maybe in the future we will
>> change it). I developed a simple protocol using ack and nak whey to handle
>> communication with master and slave. About "lock" my app until data is
>> available, I am thinking use semaphore to wait to do it, so my solution
>> will be able to sleep my mcu until spi releases it.
>>
>> Em ter., 15 de out. de 2024 às 11:24, Xiang Xiao <
>> xiaoxiang781216@gmail.com> escreveu:
>>
>>> Yes, we use DMA for both SPI master/slave, but DMA is more related to the
>>> driver not the framework.
>>>
>>> On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
>>>
>>> > One question by the way someone asked me recently - can NuttX SPI Slave
>>> > work in DMA mode? :-)
>>> >
>>> > --
>>> > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
>>> >
>>>
>>
>>
>> --
>> *Felipe Moura de Oliveira*
>> *Universidade Federal de Minas Gerais*
>> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
>> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>>
>
>
> --
> *Felipe Moura de Oliveira*
> *Universidade Federal de Minas Gerais*
> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>
--
*Felipe Moura de Oliveira*
*Universidade Federal de Minas Gerais*
Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
<https://twitter.com/FelipeMOliveir?lang=pt-br>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Felipe Moura Oliveira <mo...@gmail.com>.
Hello All,
I did a dummy question, in master branch, spi slave implementation already
blocked until new data is available. I wasn't working in the master branch
but I will switch to.
Em ter., 15 de out. de 2024 às 11:45, Felipe Moura Oliveira <
moura.fmo@gmail.com> escreveu:
> Hello Xiang Xiao,
>
> thanks for sharing with me about rpmsg, to be honest it is new for me =/,
> I was locked in baremetal for several years. For now I cannot use it
> because my host mcu is running baremetal (maybe in the future we will
> change it). I developed a simple protocol using ack and nak whey to handle
> communication with master and slave. About "lock" my app until data is
> available, I am thinking use semaphore to wait to do it, so my solution
> will be able to sleep my mcu until spi releases it.
>
> Em ter., 15 de out. de 2024 às 11:24, Xiang Xiao <
> xiaoxiang781216@gmail.com> escreveu:
>
>> Yes, we use DMA for both SPI master/slave, but DMA is more related to the
>> driver not the framework.
>>
>> On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
>>
>> > One question by the way someone asked me recently - can NuttX SPI Slave
>> > work in DMA mode? :-)
>> >
>> > --
>> > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
>> >
>>
>
>
> --
> *Felipe Moura de Oliveira*
> *Universidade Federal de Minas Gerais*
> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>
--
*Felipe Moura de Oliveira*
*Universidade Federal de Minas Gerais*
Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
<https://twitter.com/FelipeMOliveir?lang=pt-br>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Xiang Xiao <xi...@gmail.com>.
In this case, you can reference:
https://github.com/apache/nuttx/blob/master/drivers/rpmsg/rpmsg_port_spi.c
https://github.com/apache/nuttx/blob/master/drivers/rpmsg/rpmsg_port_spi_slave.c
which contains the common logic to send/recv messages on spi bus async.
On Tue, Oct 15, 2024 at 10:46 PM Felipe Moura Oliveira <mo...@gmail.com>
wrote:
> Hello Xiang Xiao,
>
> thanks for sharing with me about rpmsg, to be honest it is new for me =/, I
> was locked in baremetal for several years. For now I cannot use it because
> my host mcu is running baremetal (maybe in the future we will change it). I
> developed a simple protocol using ack and nak whey to handle communication
> with master and slave. About "lock" my app until data is available, I am
> thinking use semaphore to wait to do it, so my solution will be able to
> sleep my mcu until spi releases it.
>
> Em ter., 15 de out. de 2024 às 11:24, Xiang Xiao <
> xiaoxiang781216@gmail.com>
> escreveu:
>
> > Yes, we use DMA for both SPI master/slave, but DMA is more related to the
> > driver not the framework.
> >
> > On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
> >
> > > One question by the way someone asked me recently - can NuttX SPI Slave
> > > work in DMA mode? :-)
> > >
> > > --
> > > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
> > >
> >
>
>
> --
> *Felipe Moura de Oliveira*
> *Universidade Federal de Minas Gerais*
> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Felipe Moura Oliveira <mo...@gmail.com>.
Hello Xiang Xiao,
thanks for sharing with me about rpmsg, to be honest it is new for me =/, I
was locked in baremetal for several years. For now I cannot use it because
my host mcu is running baremetal (maybe in the future we will change it). I
developed a simple protocol using ack and nak whey to handle communication
with master and slave. About "lock" my app until data is available, I am
thinking use semaphore to wait to do it, so my solution will be able to
sleep my mcu until spi releases it.
Em ter., 15 de out. de 2024 às 11:24, Xiang Xiao <xi...@gmail.com>
escreveu:
> Yes, we use DMA for both SPI master/slave, but DMA is more related to the
> driver not the framework.
>
> On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
>
> > One question by the way someone asked me recently - can NuttX SPI Slave
> > work in DMA mode? :-)
> >
> > --
> > CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
> >
>
--
*Felipe Moura de Oliveira*
*Universidade Federal de Minas Gerais*
Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
<https://twitter.com/FelipeMOliveir?lang=pt-br>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Xiang Xiao <xi...@gmail.com>.
Yes, we use DMA for both SPI master/slave, but DMA is more related to the
driver not the framework.
On Tue, Oct 15, 2024 at 2:08 PM Tomek CEDRO <to...@cedro.info> wrote:
> One question by the way someone asked me recently - can NuttX SPI Slave
> work in DMA mode? :-)
>
> --
> CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
>
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Tomek CEDRO <to...@cedro.info>.
One question by the way someone asked me recently - can NuttX SPI Slave
work in DMA mode? :-)
--
CeDeROM, SQ7MHZ, http://www.tomek.cedro.info
Re: The right way to implement an event-driven application - SPI SLAVE
Posted by Xiang Xiao <xi...@gmail.com>.
We just upstreamed a general SPI master/slave solution you can use directly
or as a reference:
https://github.com/apache/nuttx/blob/master/drivers/rpmsg/rpmsg_port.c
https://github.com/apache/nuttx/blob/master/drivers/rpmsg/rpmsg_port_spi.c
https://github.com/apache/nuttx/blob/master/drivers/rpmsg/rpmsg_port_spi_slave.c
It is based on rpmsg, so it can work with nuttx-nuttx and linux-nuttx and
all rpmsg services can work very well.
For the detailed info, you can watch the topic come from NuttX workshop:
Day2: 02 - Linux to NuttX communication in Xiaomi (youtube.com)
<https://www.youtube.com/watch?v=-YLAQlJR1vA&list=PLd73yQk5Fd8JJ5vfwN9z16emlqm36bNNF&index=18>
Day2: 07 RPTUN_framework_&_services_between_multi-CPUs (youtube.com)
<https://www.youtube.com/watch?v=6uehz3WQ0vU&list=PLd73yQk5Fd8JFJW0cIHsDcG-kd6OHiHEz&index=17>
Day2: 03 NuttX VirtIO Framework and Future Works (youtube.com)
<https://www.youtube.com/watch?v=CYMkAv-WjQg&list=PLd73yQk5Fd8JlsCoUYGyAf7W63BekgPup&index=8>
Thanks
Xiang
On Tue, Oct 15, 2024 at 8:09 AM Felipe Moura Oliveira <mo...@gmail.com>
wrote:
> The right way to implement an event-driven application - SPI SLAVE Hello
> everyone, in the last few weeks I have been developing a solution in
> esp32c6 that communicates with another mcu from an SPI, the application I
> am developing is an spi slave. Up until now, my application has been
> executing reads inside a while(1) (I know, this mode is very dummy) and
> when I receive n_bytes > 0 I have been handling this. Now I need to improve
> my solution so that my application stays in "sleep" and when an spi
> interruption occurs it can continue with the execution. I am having
> difficulty understanding the right way to do this in Nuttx. Has anyone done
> something similar or have tips on where I should go next?
>
> Regards!
> --
> *Felipe Moura de Oliveira*
> *Universidade Federal de Minas Gerais*
> Linkedin <https://www.linkedin.com/in/felipe-oliveira-75a651a0>
> <https://twitter.com/FelipeMOliveir?lang=pt-br>
>