Ritchieduino a pure C99 implementation of the Arduino library

Hello,
I would like to share my C99 implementation of the Arduino Library. Some changes were made and it is different in the API level and even some internal changes were made. The motivation for the library was because i needed extra control regarding the size of the binaries and the C++ Arduino library, through the use inheritance became too complicated too mantain.

I striped Arduino Core down, so that only the C files as headers remained providing the basic functionality of functions like the pin numbers, analog/digital read/write. In my opinion with only the C approach, it is possible to have a cleaner approach than mixing the C and C++ styles..

The Ethernet, Serial and SPI library were reimplemented. Most of the code can be related to the original Arduino libraries only with some changes regarding structural decisions.

The Ethernet library is considerably different than the Arduino one, because now W5100 chip will only have one socket (removing all reference of sockets to the application API). Because only one socket is available the buffer in W5100 will now hold 8Kb of information instead of the 2Kb in the original one. I chose only one socket because AVR is single threaded and it simplifies a lot the code. For now the Ethernet library is basically a TCP only library since, again, the overhead of implementing other protocols is unnecessary for my uses and most usage scenarios. The only scenario UDP seems useful is for real time sensor data and NTP, but i dont have that needs now and this was a project of a project so i did not spend time implementing it. All in all, the size of the WebServer example in the new Ritchieduino is half the size of the normal example the is included in the Arduino distributed library, which i think proves the point of my work.

So in the end, what i would like from the community is some support reviewing it and maybe finding it useful and giving feedback.

Thanks
Paulo Neves

Thanks for sharing, how long did it take?

You are welcome.
I assume you are asking about how long to make it. It took more or less five days, but it's not all the library only the ones mentioned.

By the way i forgot to mention there are two possible bugs in the normal Arduino code base.

void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek)
{
  uint16_t ptr;
  ptr = readSnRX_RD(s);
  read_data(s, (uint8_t *)ptr, data, len);
  if (!peek)
  {
    ptr += len;
    writeSnRX_RD(s, ptr);
  }
}

void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len)
{
  uint16_t size;
  uint16_t src_mask;
  uint16_t src_ptr;

  src_mask = (uint16_t)src & RMASK;
  src_ptr = RBASE[s] + src_mask;

  if( (src_mask + len) > RSIZE )
  {
    size = RSIZE - src_mask;
    read(src_ptr, (uint8_t *)dst, size);
    dst += size;
    read(RBASE[s], (uint8_t *) dst, len - size);
  }
  else
    read(src_ptr, (uint8_t *) dst, len);
}

The problems starts with casting of an uint16_t to (uint8_t *), which absolutely doesn't make sense since the pointer is an internal W5100 pointer and not AVR accessible. That casting causes no problem in avr because luckily pointers are uint16_t but that is bad programing practices to assume that. Also once the new chips come into place that assumption may fail badly and be hard to debug.

Another point is the use of the volatile keyword which is completely meaningless and can cause the compiler to not do its best in optimizing the code. It is meaningless because no data is read in an interrupt and there are no threads in AVR.

The following casts to (uint8_t *) are also useless since dst is already of that type.

The code style is also not really good throughout the library with the socket function argument being called _s, socket, sock or s. I would be glad to correct some of the style if there were any guidelines

Very nice!

Stupid question. How do you install this into the IDE?

Thanks for the interest.

In the case of the Arduino IDE, i must say i don't know even though you could try to replace the ArduinoCore files in the Arduino folder but, alas, i dont advise you to do that. Unfortunately this Ritchieduino is not so friendly as the original :D. I would suggest you tried the eclipse tutorial and follow the steps with the difference that the Arduino Core mentioned in the guide should be the one you pulled form Ritchieduino repository.
If you would really some help to try it out i could make a small step-by-step tutorial to compile the example of RitchieServer.

Paulo Neves

I know i'm replying to myself and maybe bumping the thread but i have made a guide and a more thorough blog post on what is Ritchieduino, which can enable new people to try the Ritchieduino library

The guide shows how to completely configure Ritchieduino in Eclipse IDE and may be usefull for people that wanted to try Ritchieduino and a full IDE like Eclipse. The guide is here:
http://www.pneves.net/2012/03/how-to-setup-and-run-ritchieduino.html

And the better explanation is here:
http://www.pneves.net/2012/03/ritchieduino-what-it-is-and-why.html

I know i'm replying to myself and maybe bumping the thread

bumping your own thread if you have something to say like new documentation is good use of the forum imho. Better than start a new thread every update :wink: