Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SerialCommunicationBufferedReceive suggestion #429

Open
daantimmer opened this issue Oct 4, 2023 · 0 comments
Open

SerialCommunicationBufferedReceive suggestion #429

daantimmer opened this issue Oct 4, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@daantimmer
Copy link
Contributor

daantimmer commented Oct 4, 2023

I have a suggestion to add to EmIL:

    struct SerialCommunicationBufferedReceive: hal::SerialCommunication
    {
        SerialCommunicationBufferedReceive(hal::SerialCommunication& sercom)
            : sercom{sercom}
            , queue{[this]() {
                while (!queue.Empty())
                {
                    const auto range = queue.ContiguousRange();
                    dataReceived(range);
                    queue.Consume(range.size());
                }
            }}
        {
            sercom.ReceiveData([this](infra::ConstByteRange range) { queue.AddFromInterrupt(range); });
        }

        void SendData(infra::ConstByteRange data, infra::Function<void()> actionOnCompletion) override
        {
            sercom.SendData(data, actionOnCompletion);
        }

        void ReceiveData(infra::Function<void(infra::ConstByteRange data)> dataReceived) override
        {
            this->dataReceived = dataReceived;
        }

        hal::SerialCommunication& sercom;
        infra::QueueForOneReaderOneIrqWriter<std::uint8_t>::WithStorage<128> queue;
        infra::Function<void(infra::ConstByteRange data)> dataReceived;
    };

queue size could be made configurable or as a ::WithSize type of implementation.

Some modules (EchoOnSerial for instance) perform all the handling on interrupt. In a "normal" situation where CPU speed is high and baudrate is slow this is fine. However, once your baudrate is high enough bytes will be missed.

There are two ways to tackle such situation. Adapt EchoOnSerial to not process the bytes on interrupt and do the receive queue manually. Or by adding a decorater like the above. The latter having my preference as this class is versatile enough that it could be used for other situations as well.

@daantimmer daantimmer added the enhancement New feature or request label Oct 4, 2023
@daantimmer daantimmer changed the title SerialCommunicationBufferedReceive suggestiong SerialCommunicationBufferedReceive suggestion Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant