My WiFi101 library port

Started 1Nov2017. Updated 21Jan2018. In work

This page is in group Technology and is a blog note where I will try to work myself through..

  • ..having run the Arduino WiFi101 library on an Adafruit Feather ARM M0 basic proto board, a variant of the Arduino Zero using a WiFi board that contains the Atmel/Microchip ATWINC1500 module that sits on an Adafruit ATWINC1500 WiFi Breakout board, using the ARDUINO IDE
  • having that software “more than just recompiled” for the XMOS XCORE boards like startKIT and xCORE-200 eXplorerKIT. The startKIT has 8 “logical threads” for built-in HW concurrency and the eXplorerKIT has two cores with 8 logical threads in each
  • Since embedded concurrent SW is what I am used to, I will try to replace callbacks and objects with interface calls and real tasks, like the XC language makes possible. I will use the XMOS xTIMEcomposer IDE
  • I will all the time have in mind the XMOS Wifi sliceCARD‘s drivers and example SW when I do this. But even that SW looks a like they haven’t used the full potential of the XCORE architecture; it looks like they have modified as little as possible of the SW of the obsoleted Texas Instruments TI CC3000 module. But they do use XC, so maybe I will change my mind on this when I get there
  • There is an overview of related notes at “My XMOS notes”, chapter MY OTHER NOTES WHERE XMOS MATTERS.


Standard disclaimer, this is just for fun. No money, gifts, ads etc. Also, my non-association with XMOS, see here.

Fold handling

This blog note uses the Collapse-O-Matic WordPress plugin to handle text folding, as you may see in the “Limited selfrefs” chapter below. It’s used mostly for code examples. In addition to expanding and closing them individually you may here:

Expand All
Collapse All


This fold is expanded.
Use “Expand All” (above) to make all folds expanded: visible, searchable and printable!

You may want to use Expand All while printing from the browser or while doing text searches.

3 What I already have done

Fig.2 – Aquarium controller based on XMOS startKIT, design Øyvind Teig, 2017 (Press for full resolution)

My goal is to have a small web server running on the box above. It contains a startKIT, controlling my aquarium. Fishes, a snail and a shrimp plus plants seem to be in good balance. I will blog about it later.

  1. The long start that caused me to see the the TI CC3000 module has become obsoleted: My xCORE-200 eXplorerKIT notes (WiFi)
  2. Selecting the right solution for me (making things fun, not easy), the Microchip ATWINC1500 module, the ARM-based ARDUINO and then two XMOS boards:  My single-board boards and why notes
  3. Making the Adafruit ATWINC1500 WiFi Breakout pluggable on all my boards: My Piggyback WiFi1500 sliceCARD

And now, this blog note: My WiFi101 library port

4 Resources

[4.x] I have added the main contents as well:

  1. Microchip ATWINC1500 module
  3. Microchip ATWINC15x0 Wi-Fi Network Controller Software Design Guide
    © 2017 Microchip Technology Inc, Application Note DS00002389A
    1. Host Driver Architecture
    2. ATWINC15x0 System Architecture
    3. WINC Initialization and Simple Application 4. WINC Configuration
    5. Wi-Fi Station Mode
    6. WINC Socket Programming
    7. Transport Layer Security (TLS)
    8. Wi-Fi AP Mode
    9. Wi-Fi Direct P2P Mode 10. Provisioning
    11. Over-The-Air Upgrade
    12. Multicast Sockets
    13. WINC Serial Flash Memory
    14. Wi-Fi Sniffer Mode
    15. Host Interface (HIF) Protocol
    16. WINC SPI Protocol
    17. Appendix A. How to Generate Certificates
    18. Appendix B. X.509 Certificate Format and Conversion
  4. Arduino WiFi101 library
    WiFi class
    IPAddress class
    Server class
    Client class
    UDP class
  5. XMOS WiFi sliceCARD drivers and example. My blog note contains an overiew of what I needed: [3.1] chapter My xCORE-200 eXplorerKIT notes (WiFi) (Installing the drivers)

Making the WiFi board pluggable on all my boards

I did this in the previous blog note [3.3]. All three connections are described:

  • Adafruit ATWINC1500 WiFi Breakout connected to:
    1. Adafruit Feather M0 basic proto
    2.  J1 on XMOS xCORE-200 eXplorerKIT
    3. PCIe breakout card for XMOS startKIT

I also tested installed the Arduino IDE, downloaded the WiFi101 and compiled and ran examples on the Adafruit Feather M0 basic proto. It works!

Observation of the WiFi101 original code

A list

  1. The code static const SPISettings wifi_SPISettings takes 12000000L  (shown above) is not my way of coding. SPI speed should be set-able by the user. Using a numbered value for the speed, without #define for it isn’t much readable. Especially when there’s no comment either. #define SPI_CLK_SPEED_HZ 12000000L is how we also were required to do it at work
  2. When I have not got my wired setup to work then the CheckWifi101FirmwareVersion_Teig writes this the to Serial Monitor:
    WiFi101 firmware check.
    WiFi101 shield:

    This indicates to me that neither this code (nor the XMOS code shown in note 143) has any type of timeout when the HW goes wrong. I would have coded with some error return instead of a hang there. Waiting for some value definitively is no good idea

  3. When the web server hung so much when low signal levels (dBm) without a proper warning on the web server side (no log at all) it may be a sign that the error handling is so and so?
  4. In m2m_wifi.h it says that the “Firmware can be downloaded through a number of interfaces, UART, I2C and SPI.” However, in the ATWINC1500-MR210PB data sheet (here) it says that I2C.SDL and I2C.SCL are “Currently used only for Atmel debug. Not for customer use. Leave unconnected.”. So no wonder that I2C is mentioned only in m2m_wifi.h, there is no sign of any code

Looping as fast as possible leaves no room for concurrency

SimpleWebServerWiFi_Teig.ino contains loop(). It just loops. I made a counter (unsigned long) and printed out its value every one mill counts. It took about 56 seconds to count to 5 mill, so it’s about 11.2 μs to loop around just counting up that value. It runs as fast as it can.

This is bad architecture. Lucky for the demos that there are no background or foreground tasks that have own state. There is no task / process / thread here. It’s as single threaded as possibly possible.

Disclaimer: Of course the ARM processors have a lot of hardware blocks that would live their own lives. And they would communicate with the main loop of running objects through interrupts. This is concurrency that all processors would need and have, and they have had it since 1954? They certainly make even the simplest of processors do more than one thing quite seamlessly. But it’s not concurrency at the occam, Ada, go, XC etc. level that I want to have here. The quite new (2017) Apache mynewt OS is also interesting in this context. I do not want my aquarium code and the communication to be in some one outer loop. One could say that each process, task or thread consists of local loops all over the place in the software. Then there is a scheduler that controls when each loop is run. Basically this is the basic theme of all of my Technology blogs and papers. But there’s more to it:

Arduino Scheduler concurrency is low-level yield-based

Arduino supplies an “experimental” Scheduler,  Scheduler (see that’s “cooperative” (they don’t cooperate on data, just on passing of control). It’s based on starting an additional loop with Scheduler.startLoop(new_loop). Then new_loop must run a Scheduler.yield so that the original loop also will run. This is the lowest level of concurrency with no support for communication between them and no structured way to avoid busy poll.

Have a look at their blinking LED example (here). No no-skew timer. No communication between the loops. No communication into or out of the loops. Besides, all cycles are burned. And the cognitive understanding av what a task is probably isn’t easy to grasp in the light of how to next solve the missing points.

Later, working with the port of the RadioHead library My aquarium’s data radioed through the shelf, I discovered lots of YIELD; in the code. Like in file RHGenericDriver.cpp:

// Blocks until a valid message is received
void RHGenericDriver::waitAvailable()
    while (!available())

Now it can do other things instead of blocking. (Aside: I have discussed blocking in Not so blocking after all.) In file RadioHead.h YIELD is defined as yield(); for Arduino is 1.55 or more. I then searched my installed libraries and found the Scheduler files. In Scheduler.h:

// Copyright (C) 2012 The Android Open Source Project
class SchedulerClass {
	static void startLoop(SchedulerTask task, uint32_t stackSize = 1024);
	static void start(SchedulerTask task, uint32_t stackSize = 1024);
	static void start(SchedulerParametricTask task, void *data, uint32_t stackSize = 1024);

	static void yield() { ::yield(); };

We see that it’s based on what we saw above, new Loops may be started as tasks. Looking inside Scheduler.cpp then coopTaskStart, coopDoYield are assembly code, for ARM Arduinos (I think).

Adding YIELD; or yield(); to my .ino top level file for testing the RadioHead library did compile. I use ARM Arduinos. It compiles, yes, but yield(); probably is empty code. I edited in Scheduler.cpp and made sure it would not compile. It did. I then did Sketch | Include Library | Scheduler, and #include <Scheduler.h> arrived in the code. Then it didn’t compile before I had fixed the code again. So, in the RadioHead library blocking seems to block since I cannot find and start or startLoop in the code that would give the code any place to yield to.

I have queried about the sanity of the above inferring on the radiohead-arduino group, see YIELD; in RadioHead code.

Later I also found Ivan Seidel’s ArduinoThread ( and downloaded it. It’s a wrapper around callbacks for «blinking of LEDs» and it does not use C++11’s std::thread class. I have found no Arduino stuff that uses those native threads. Also, there is no communication between them

I will not do my whole story here. Almost all of my technical blog notes deal with concurrency. Just search for yield, generator, CSP, concurrency or blocking in the search window above, or visit the main Technology blog note list here and see if you find a thread to pull.

That being said, to me this Scheduler.yield may be a starting point for building a CSP-type Scheduler on top. This may be interesting in the future.


I think I will continue to keep all my conclusions to the same chapter, in a previous blog note. See My single-board boards and why note, chapter Conclusion.

Leave a Reply

Your email address will not be published. Required fields are marked *


* Copy This Password *

* Type Or Paste Password Here *

16,301 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>