Wednesday 29 January 2020

MicroPython

Micropython was invented by an Australian programmer Damien George in 2013.  It is a lightweight version of python, compatible with version 3, suitable for microcontrollers.  It strikes me that I should concentrate more on python programming and this is part of the suite.  In particular I can use it for  ESP8266, ESP32, Arduino and micro:bits.

micro:bit


It couldn't be simpler to get started with micropython as explained at microbit.org.  Just use an on-line editor to create your program - initially you can amend the sample provided.  Once happy with the program it is downloaded to your PC as a hex file.  You connect the micro:bit to the PC which gives you a new drive.  Copy the .HEX file across to the micro:bit drive and your program runs.

ESP32


Install a python utility called esptool in my Windows environment using pip
download and install firmware as described at micropython.org
Install the Thonny IDE as described by RandomNerdTutorials.

Now we select micropython on ESP32 in Thonny options, select the ESP32 com port and see the Thonny shell running on ESP32.


First ESP32 Project

I really want projects to be web based to reduce connection hassles.  So first of all I want to establish wifi connectivity.
RandomNerdTutorials.com have a very simple project which starts wifi and establishes a webserver.
Two files called boot.py and main.py are downloaded to the ESP32.  These are run automatically at reset.  In this case boot.py establishes the wifi connection and main.py causes a webserver to listen for and process connections.
Connections from a browser cause a webpage to be displayed allowing control of a GPIO.
The GPIO in question is the inbuilt LED (pin 2) so clicking a button causes a response to be sent to the webserver turning it on or off.
This gives us everything we need to use the ESP32 as a basic standalone webserver reporting on its local environment.

Webserver

J-Christophe Bos has created a wonderful small webserver which is contained in 3 files:


It allows you to serve static pages, server-side scripting (pyyhon/html), websockets and supports GET/PUT, JSON, AJAX all in 65KB of program.  ESP32 has 2MB flash for filespace so this leaves plenty of room for data and static pages.


There is a microwebsrv2 update on github but as of December 2019 there seems to be a little problem making it trickier (for newbies like me) to setup. 

Tuesday 28 January 2020

Django CSS,JS,images and databases

I mentioned in the previous post that django isn't very interested in client side processing.  Our static web page code will look very much as before.  We need to be able to include other files, images, javascript, css etc within this code.

I created a test application (called jonas) to check this out.  Its job is simply to display text, formatted by CSS and display a button, which when clicked, calls a javascript function to display a message.
I did find it easier to read the tutorial in pdf format so I referred to django manual

We put the html in a file jonas/templates/jonas/index.html
We put css and js in jonas/static/jonas/style.css and jonas/static/jonas/mess.js
We put a background image in jonas/static/jonas/images/mybgc.jpg
We create a view in jonas/views.py to display jonas/index.html.
You have to add your app name into the INSTALLED_APPS setting and Django then searches for index.html, style.css and mess.js in the appropriate places and displays the appropriate page including css and js.

jonas/views.py
jonas/templates/jonas/index.html
jonas/static/jonas/mess.js
jonas/static/jonas/style.css 
Here is the result:

To make the pages visible from other local devices I used runserver 0:8000 and added the setting ALLOWED HOSTS['*'].  I could now access django from other devices on my network to my WSL IP address.

Databases

Using MariaDB


For the tutorial I used the sqlite database which comes in the django package.  I use MariaDB on an RPI for my databases and some care was required to allow me to use them.  
Firstly I needed to point django at my database server:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tutorial',
        'USER': 'djangouser',
        'PASSWORD': '######',
        'HOST': '192.168.0.##',
        'PORT': '3306',
    },

We have to create the tutorial database ourselves on the database server. I used a digitalocean tutorial to help me. Briefly you:
signon to mysql on RPI
create a database called tutorial
create a user djangouser@192.168.0.#
grant all privileges to the user

To check that the database was working I could use python in my django environment:
  from MySQLdb import _mysql
  db=_mysql.connect(host=”192.168.0.24”, user=”djangouser”, passwd=”secret”, db=”tutorial”)

It is now possible to use migrate as shown in tutorial part 2.  After doing this I could continue with the tutorial updating the RPI tutorial database.

Using existing databases

To use my existing databases on RPI I looked at django documentation for "legacy" databases.
First  I made my picture database the default in settings.py.  I then ran inspectdb to create models.py with the appropriate database fields.  Checking models.py I saw the primary key field was missing so added it.  I now ran migrate which sets up the tables django requires for database access.  I tested database access using the django shell to ensure I could see data.

Using Multiple databases


I expect my apps to use different databases.  There are a number of ways to set this up.  The simplest is to add .using('dbname') in your database access code.  If you omit it django will access the default database.





Saturday 25 January 2020

Django

Django provides a framework, written in python, for a webserver.  It appears to be a "professional" product suitable for public web-sites and seems to be well regarded.  I also wanted to see how appropriate it is to use python for web-site implementation and polish my rusty python skills.

Installation


I decided to use the tutorial under WSL (Windows Sub-system for Linux) although a proper implementation would be on my internet-facing RPi. As WSL is not updated automatically with Windows I had to update WSL to a newer release first, then had to install pip and a "lightweight" virtual environment for running python.  Finally I could follow djangoproject website instructions to install Django.  I also installed mysqlclient so that I could access MariaDB on my back-end server but ended up using the django internal sqlite for the tutorial as it was easier.

Tutorial

There is a long and clearly written tutorial to get you started on django.   Django uses the python package directory layout so files are grouped in highly organised structures.  There is a folder for the webserver and a folder for each "app".  Three principle types of python files are used to create a basic app:
  • views - display data in response to a query (URL) submitted to the server
  • models - define the database layout and structure for data stored on the site
  • urls - link the user query(URL) to the correct app and pass parameters (within the URL).
Within the first two parts of the tutorial I was able to create a couple of database fields, generate maintenance pages for those fields and display pages containing them.  This only took an hour or so (I cheated by doing plenty of cut and pasting).  In comparison with PHP/mysql it was much easier to access and maintain data.  

My first app


Most of my website content comprises static pages plus javascript with php used to obtain server information.  Django isn't musch concerned with web pages themselves or client side processing, the server will send out pages but it is only "interested" in python programs, so it replaces the php programming component.

Just from the information in the first tutorial I can easily, create an app and display a web page including javascript.  All I have to do is create an app, create a view and link a url to it.

view

html

result

Without any of the faffing about associated with php I can focus the power of python scripts on the creation of web pages.  In particular it is much easier to incorporate database operations into a python script and code is simpler to understand, write test and read.  

With great power comes great responsiblity.  I will have to check security before I let any web pages out in the wild as python has wide access to local resources.  I also need to check the capabilities of the django server in comparison with lighttpd.  It my be best to use a combination of both, lighttpd for static pages with lots of formatting and django for database, file and communications based work.








Monday 20 January 2020

Arduino MKR Vidor 4000 - Getting Started

At the Arduino site there are a couple of tutorials by Daniel Hertz which show you how to build / download a (pre-existing) design for the Vidor Cyclone10 FPGAand how to create a Quartus project  from scratch.  The best example to use is provided by Al Williams in a Hackaday article and can be downloaded from github.

Building and Loading an FPGA design

The first thing to do is to set up Quartus, I haven't used a Cyclone 10 processor previously so I needed to download and install the appropriate Quartus repository. I then downloaded Al Williams example code which includes a Quartus project file (.QPF) so that I could synthesise an image straightaway.

Normally I would then download a .SOF file to the FPGA using a USB Blaster.  A confusing wrinkle with the arduino setup is that the .TTF file created during Quartus synthesis needs to be converted - its nibbles are in the wrong order - before downloading to the FPGA.  Al Williams provides a simple C program vidorcvt which converts this file to one called app.h.

The second part of the design is an Arduino sketch (provided) which sets up the FPGA and runs SAMD code which interfaces with the FPGA.  When this sketch is compiled and loaded we have a working SAMD/Cyclone implementation running on the Vidor.

Al Williams example is a simple blink program which runs on the FPGA.  A 28 bit counter is used to determine the blink speed.  Bit 27 is used for a 1 second blink and bit 21 gives quicker flashing.  The FPGA code is stored in user.v.  Arduino pin D6 is used for the LED output.  If the user sends a character using the serial monitor, the Arduino sketch toggles pin D5.  user.v then toggles which counter bit is used, i.e. it changes the flashing speed.  This demonstrates simple inter-communication using shared GPIOs.

Creating a Quartus Project

There are a number of things you need to create a Quartus project:
  • pin outs for the various components (.QSF)
  • system constraints file (.SDC)
  • IP code for all the peripheral components
  • Top level Verilog code to initialise the system

These are provided in EmptyVidorProject folders so that it is quick to start writing your own code.
It is instructive to build your own project using the tutorial, although in practice you could copy a skeleton quartus project structure.

It is particularly useful to have the pinouts and SDC as I usually need to spend a lot of time trying to get them right.
The top level verilog code is setup as a boiler plate - you can add your own code in a user.v include file.

Baby Steps

As a check that I can change the code myself I changed the LED output used for blinking.

Copy the blink Quartus project directory to blink2
In user.v assign counter value to bMKR_D[4]  (pin D4) instead of bMKR_D[6] (pin D6)
Synthesise the project
Convert .TTF output to app.h and save in the blink-sketch folder
Compile and upload blink-sketch to Vidor
Now pin D4 blinks instead of D6

This demonstrates that I only need to change code in two places user.v and blink-sketch.





Tuesday 7 January 2020

Arduino MKR Vidor 4000

Intro

After the excitement of the Atlas-SoC I saw that Arduino have come up with similar hardware.  The Arduino MKR range provides low cost/power 32-bit micro-controllers.  The Vidor 4000 contains an Intel Cyclone 10 FPGA together with an Arm Cortex M0+ MCU.  So we have an Altera FPGA which we can program/configure with Quartus and a Microchip SAMD21 Arm processor which we can program with the Arduino IDE.  Arduino simplicity and the size of the user community should make this potentially widespread in its appeal.  It is also new in late 2018 so its use is just beginning to evolve. It is so exciting that I asked Harry to buy me one for Christmas.

Install

Getting Started is straightforward, as you would expect.  In board manager I needed to add SAMD beta boards then add the Vidor 4000 board in the Arduino IDE.  As usual I connected a microUSB cable to the PC and, after installing a driver, I was able to see the device and load a standard blink "hello world" program.  Next I downloaded VidorGraphics, VidorPeripherals libraries and could then try the Vidor specific examples. The first one of interest is to display an Arduino logo on an HDMI screen.  Initially this didn't work on my HDMI monitor but when I connected to a HDMI TV port it worked fine.

Familiarisation

The best explanation I have found for the Vidor 4000 is provided by Philippe at systemes-embarques.fr who also provides some tutorials.  Components on the card are:
  • ATSAMD21G18A microcontroller with 256 kB of Flash and 32 kB of RAM.
  • a Cyclone 10CL016 FPGA with 15408 logic elements, 504kbits of RAM and 56 multiplier 18 × 18.
  • a 16 Mbits FLASH SPI.
  • a 64 Mbits SDRAM (4M x 16 bits)
  • a NINA W102 WiFi / BLE module incorporating an ESP32 dual-core microcontroller.
  • an ATECC508A cryptographic chip improving the processing speed for secure connections.
  • MiniPCIe, USB, battery, I2C, MKR, MIPI for a camera, HDMI for video output connectors.

Most resources are attached to the FPGA but can be routed to SAMD21 or ESP32.

When Vidor is switched on MCU and ESP32 are initialised from non-volatile memory and the FPGA configuration is loaded from the Flash memory.

If you use Arduino IDE to upload the example "blink" sketch it is loaded via USB cable to MCU and run without touching FPGA.

If however you upload Examples>VidorPeripherals>VidorTestSketch one part of the .HEX file is loaded to MCU and the rest (app.ttf), is loaded via JTAG into FPGA and saved in SPI FLASH as shown in the diagram below.
When FPGA.begin() is executed on SAMD21 it enables FPGA clock, initialises JTAG port and sends a command to FPGA telling it load app.ttf from flash.  The JTAG port is used for subsequent MCU - FPGA intercommunication.




Monday 6 January 2020

Atlas SoC

Purchase

One problem with buying an FPGA card from ebay is that it is difficult to find appropriate tutorials which are designed for similar configurations.  I regularly see tutorials for Altera development boards but initially I thought the boards were all terribly expensive.  However when I saw the Atlas-SoC board for about £100 at Mouser I had to buy one.  It comes as a specific learning package, it is based on a cyclone V, has an ethernet port, and runs linux from an SD-Card.  That seems like nirvana and without a trace of indecisiveness I sent off for one.

Getting Started

The board arrives with some instructions (a pleasant novelty) and when I plug the micro-USB connector in to the PC and power it up I see a new F: drive!  This tells me to install a driver  then I can open a browser session to the board!  I did have a little bit of faffing about to get the driver to work, perhaps because the package was put together in 2015, but soon I had a welcome screen on the FPGA IP address.  A wonderful start to the experience!

The board comes in two flavours:
Atlas-SoC - targetted at Embedded software developers
DE0-Nano-SoC - targetted at Hardware developers

Both have exactly the same hardware but the getting started instructions are different.

Having followed the "software" instructions initially I wanted to understand a little bit about the hardware so followed the DE0-Nano-SoC Getting started guide.  This took me through 5 steps:

1 Add Cyclone V device support to Quartus (I previously had Cyclone IV and MAX1000)
2 Install SoC FPGA EDS (Embedded Development Suite)
3 Setup the board, including configuration switches
4 FPGA System Test - use Quartus to load and run an LED counter sketch
5 Run Linux on the board - format an SD card for linux, boot up and connect via Putty.

This provided an even more wonderful experience.  We are now able to program the FPGA part of the board and have Linux running on the HPS (Hardware Processor System) part.



Sunday 5 January 2020

Altera Cyclone IV Ethernet development board

There is always a need to hava an FPGA which will do more.  The Cyclone IV board is great for simple output and testing my NIOS processor.  Matrix LED and 7 Segment output provide physical output in addition to screen I/O using the UART.  What I really need is a network connected FPGA.  Ebay provides the answer with a powerful cyclone IV development board complete with gigabit ethernet, an SD card and audio i/o suitable for DSP.  It also has plenty of memory, lots of I/O and a VGA port.
After a few weeks the board duly arrived from Aliexpress in China.
I was able to run LED and KEY test programs provided to check that it is working.
There was also a good QPF project to test Audio input output.

Unfortunately (maybe) the test programs for Ethernet and SD card dont work so I have some investigation, learning and programming work to do before I can use it properly.  My aim is to build a web-server which serves files from the SD card.  A secondary (stretch) goal is to build an audio processor to show waveforms and signals on an attached VGA display.  It may be some time before I can report back on progress.