Martin's corner on the web

Using Jeelab’s EtherCard library on the Micro Web Server (uIoT)

I finished the painful assembly of Simon K.’s Micro Web Server, but was too tired afterwards to see if it works. With all these tiny SMD components, there is always the chance that something goes wrong. So today, I finally found some time to try it out.

First thing to to is add the new board to Arduino IDE. Why Arduino IDE? Simply because it is more accessible. This would also allow me to program it directly from there, also set the fuses. Here is my entry to Arduino’s boards.txt file:

##############################################################
uiot.name=Simon K. Micro Web Server
uiot.upload.using=arduino:arduinoisp
uiot.upload.maximum_size=32000
uiot.bootloader.low_fuses=0xE0
uiot.bootloader.high_fuses=0xDD
uiot.bootloader.extended_fuses=0x05
uiot.bootloader.path=atmega
uiot.bootloader.file=uiot.hex
uiot.bootloader.unlock_bits=0x3F
uiot.bootloader.lock_bits=0x0F
uiot.build.mcu=atmega328p
uiot.build.f_cpu=12500000L
uiot.build.core=arduino
uiot.build.variant=standard
##############################################################

A few things are happening here. First we need to define that uploads will happen through an ISP programmer, the Micro Web Server doesn’t have other option for sketch uploading. Next, we set the fuses so that the Atmega328 is clocked externally, BOD level is set to 2.7V as we will run on 3.3V. To set the fuses we use the Arduino IDE function ‘boot bootloader’, just as it is done with the Tiny core. I practically don’t need a bootloader, since I will be flashing sketches with the ISP programmer, so I created a dummy .hex file to use as bootloader file. I simply compiled this sketch:

void setup(){}
void loop(){}

..and compiled it while holding the left shift key. This turns on verbose mode, and revealed the location of the generated .hex file. I placed that in the “arduino-1.0.1\hardware\arduino\bootloaders\atmega” folder.

Final setting is to adjust the FCPU frequency to 12.5Mhz, this will be the speed at which I will run the board (remember, it runs off ENC28J60‘s clock out).

With these changes I was able to set the fuses and upload a simple blink sketch to see the heartbeat. It worked right away, I was surprised myself 🙂

So next to do is actually make use of the ENC28J60 module. For the purpose I used Jeelabs’ EtherCard library. It works right away, just mind that the ENC28J60 chip select is pin 10:

ether.begin(sizeof Ethernet::buffer, mymac, 10)

Simon K. uses the uIP stack , it is superior to functionality to the EtherCard, but I am more used to the later one. I have used/created examples for NTP, Basic HTTP Authentication, simple JQuery pages for configuration and UI, DynDns client and so forth with this library and don’t want to start from scratch (see my NaNode based heat pump controller project).

Since the ENC28J60 outputs 6.25Mhz to the clock output pin upon powering up, this is the frequency that our MCU runs at as well. I want to run faster though, so I need to set the prescaler to 2, meaning the 25Mhz clock on the ENC28J60/2 = 12.5Mhz. This is perfectly fine for the 3.3V that the module runs on. To do this, we need a small patch for the file enc28j60.cpp, we just need a new function to set the prescaler added there:

void enc28j60clkout(uint8_t clk)
{
 //setup clkout: 2 is 12.5MHz:
 writeRegByte(ECOCON, clk & 0x7);
}

Then a call to that function within the body of “ENC28J60::initialize” method:

 enc28j60clkout(2);   // place that at the end of the method

So I tested it using poor man’s debugging tools (i.e. a LED) and it blinks twice faster after I call the initialization function, meaning we run twice faster. Cool.

So to test everything, I uploaded the “Back Soon” example and it worked nicely. Phew, what a relief 🙂 I am loving this little gadget.

 

One thought on “Using Jeelab’s EtherCard library on the Micro Web Server (uIoT)

  1. Pingback: Optiboot on the micro IoT gateway | Martin's corner on the web