Go Down

Topic: Ritchieduino a pure C99 implementation of the Arduino library (Read 2 times) previous topic - next topic


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.

Paulo Neves


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


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.

Code: [Select]
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);
    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


When the testing is complete there will be... cake.


Stupid question.  How do you install this into the IDE?
If you fall... I'll be there for you!

Skype Brighteyes3333
(262) 696-9619

Go Up