cancel
Showing results for 
Search instead for 
Did you mean: 

How to write good cellular modem driver?

kaamil1984
Associate II

Hi guys,

I'm struggling with modem communication on embedded system that does few other tasks in realtime. I'm using TCP stack built in modem (Fibocom G510). 

This seem to be not complicated, but every time I'm doing it - I'm doing it diffrent way and I end up with pretty big and not perfect code, because there is always URC or condition that I have not thought of.

To establish TCP socket connection I have to:

- reset modem hardware

- wait for "SIM READY"

- enter pin (optional)

- wait for cellular network registration

- wait for 2G/3G/4G registration

- configure APN

- configure other things (optional)

- activate internet connection

- activate TCP connection

After everything is initialized and connected I have to handle many things, including URC (unsolicited result codes):

- cellular network connection lost (so AP and TCP connection is also lost)

- AP connection lost (so TCP connection is also lost)

- TCP connection lost

- maybe even SIM LOST scenario

- errors

- incoming data from TCP socket

- incoming SMS messages (optional)

And I also have to support timeouts.

Is there any way to avoid spaghetti state machine?

Is there any reference design for embedded modem driver that I can find on Github or some manufacturer website?

1 ACCEPTED SOLUTION

Accepted Solutions

The golden rule is: Do not use blind delays!

eg,

https://www.avrfreaks.net/comment/2209136#comment-2209136

https://www.avrfreaks.net/comment/2212166#comment-2212166

and many. many more.

@kaamil1984​ "Is there any way to avoid spaghetti state machine?"

A well-written State Machine should not be "spaghetti"!

You will probably need several state machines; eg,

  • the basic AT Command handler
  • the network state of your unit - waiting for SIM, registered to network, etc
  • etc, ...

"any reference design for embedded modem driver that I can find on Github"

Some here:

https://www.avrfreaks.net/comment/2247176#comment-2247176

View solution in original post

3 REPLIES 3

The golden rule is: Do not use blind delays!

eg,

https://www.avrfreaks.net/comment/2209136#comment-2209136

https://www.avrfreaks.net/comment/2212166#comment-2212166

and many. many more.

@kaamil1984​ "Is there any way to avoid spaghetti state machine?"

A well-written State Machine should not be "spaghetti"!

You will probably need several state machines; eg,

  • the basic AT Command handler
  • the network state of your unit - waiting for SIM, registered to network, etc
  • etc, ...

"any reference design for embedded modem driver that I can find on Github"

Some here:

https://www.avrfreaks.net/comment/2247176#comment-2247176

jjhon.1
Associate

The main objective of the modem driver is to translate the operating system’s requests to the wireless device into the wireless device’s internal representation. Therefore the modem drivers code should be as generic as possible and it should be as small as possible.

kaamil1984
Associate II

Thanks guys,

Thanks @Andrew Neil​  for posting link that goes to attentive which is very interesting library.