Saturday, July 25, 2015

First steps coding and speed testing the STM32F746 Break Out Board



It's a few days since I received my prototype STM32F745 Breakout boards and I have slowly been writing some basic firmware functions to test out the hardware.

Whilst I have done a fair bit of code development for the STM32F4, F373 and F103 microcontrollers, this was my first exposure to the awesome Cortex M7.

Every programmer wants to create a comfortable coding environment around any new microcontroller platform, and often this is done by porting familiar routines across to the new target and re-using tried and tested libraries in order to develop the application.

With a few basic functions, you can do a surprising amount - here's one approach

digital input and output - flash a LED to confirm code is running
serial USART - allows text output and a simple command interface from PC
timers - by establishing a millisecond and microsecond tick you have precision timing
timers also allow pwm to be generated
ADCs - 12bit or even 16bit resolution measurements become possible
SPI  - connect to SD-Card,  Colour TFT LCD, etc
I2C - add an eeprom
RTC - and battery backup.  The STM32F series often have on chip RTC and non-volatile RAM

The Arduino project has done this time and again for the various microcontrollers used on their boards - proving that code originally written for an AVR, can be ported to various manufacturers ARM processors.  The STM32duino project has gained a fair amount of traction porting the Arduino environment so that it will run on STM32F1xx  M3 and STM32F4xx M4 processors.

The Arduino language successfully removes much of the complexity, or possibly quirkiness, from the C programming language, and at the same time creates a robust application framework onto which the hardware specific functions and libraries may be placed.

Purists, or old-school C programmers, may delight in criticising it's oversimplification of the language, but for beginners or those that have had no exposure to traditional C and C++, it's a fast way of getting simple applications running - and now with the benefit of moving between a range of more capable processors.

Tool Chain

I found that I could download a codesize limited (32KB) version of the Keil ARM-MDK, which runs under their uVision 5 toolchain. This was the easiest route to take, as it includes all the library headers, and numerous examples of how to configure the various on-chip peripherals.

If you are familiar with the STM32F  Standard Peripheral Libraries,  then this may be a mixed blessing.  STM have ported their functionality across to their new Hardware Abstraction Layer HAL, under their new development suite known as STM32 Cube.

There is no question that STM32 Cube will provide faster development for complex applications, as it combines graphical libraries, ethernet and TCP/IP stacks in an integrated design environment. However, the initial barrier to learning enough about HAL to reach the point of understanding it's various structures can be quite high, leaving the user somewhat perplexed that what should be easy, turns out to be really quite a challenge.

Once over the HAL Hurdle - progress will be much quicker.

Get it Running

The usual route to getting a new board up and running is to start with the bare minimum of code to prove that it will actually run.  Traditionally this is normally a simple LED flashing routine.
With the STM32 series of microcontrollers - and any 32bit mcu, for that matter, there is quite often a considerable quantity of registers to initialise first.

I started with the LED flash example, that came with the ARM-MDK download. It is often wiser to start with a known working example, rather than start from scratch, especially with an unfamiliar microcontroller.

As it happened, my problems were initially hardware related - as I had forgotten to connect the all-important analogue VDDA supply. Once this was connected, the microcontroller accepted code and sprang into life.  Without VDDA, the clock oscillator and the PLL blocks are not powered - so the mcu cannot run.

Once I had the LED flashing, it was time to check out the system timing, and make sure when you used the 100mS delay routine - that it was actually 100mS.  I had used an 8MHz crystal and so I had to change the PLL divisor ratio, in order to get the right timing, as the code in the example was for an EVAL kit - which used a 25MHz crystal.

Once this was done, I wrote the code to configure the GPIO pins so that I could toggle more of the I/O pins.

Next - USART

Basic GPIO is always the starting point for code development, but pretty soon you need to get a serial connection to a terminal program - so you can see numerical and text output plus the means of controlling the application with a simple command interpreter. With about 3 or 4 basic functions you can implement a surprisingly flexible user interface.

1.  putchar  - send a single ASCII character to the serial port.  This is the basis of all serial output.
2.  getchar  - receive a character from the serial interface
3.  print_num  - this will send an integer number to the serial interface
4.  Get_Serial  - a routine that accepts simple alpha-numeric serial commands for machine control

The USART proved somewhat trickier to implement, mainly because the HAL routines were somewhat different to the old STM32Fxxx_USART library code that I was used to. After a day spent failing to import the USART initialisation into my code, I decided to port my code into the HAL USART example - starting with a known working example.

This approach was a lot more successful, and soon I had printf(), putchar() and getchar() all working nicely.

Benchmarking the STM32F7xx.

Having got the first prototype running to the point where I could run code and get numerical output, I decided that it was time to run a standard benchmark test on it - so that I could compare it with other devices.

The dhrystone is the classic integer math benchmark that has been run on various computers for over 30 years.  A C code version (actually Arduino) of it can be downloaded from here

 http://www.saanlima.com/download/dhry21a.zip

Some months ago, I tested the standard Arduino MEGA, which uses an ATmega2560 running with a 16MHz clock.

The results were as follows

ATmega2560 16MHz

Dhrystone Benchmark, Version 2.1 (Language: C)
Execution starts, 300000 runs through Dhrystone
Execution ends
Microseconds for one run through Dhrystone: 78.67
Dhrystones per Second: 12711.22
VAX MIPS rating = 7.23


STM32F746 216MHz

Dhrystone Benchmark, Version 2.1 (Language: C)
Execution starts, 300000 runs through Dhrystone
Execution ends
Microseconds for one run through Dhrystone: 3.33
Dhrystones per Second: 300000
VAX MIPS rating = 170.74

So the STM32F7xx is approximately 24 times faster than the Arduino on integer maths. But when you look at the whole package:

1M Flash
320KB  SRAM
4 USARTS
USB full-speed
Up to 6 SPI
Up to 18 timers
etc, etc

The list of peripherals is most comprehensive.  Best check the datasheet for full details.

And all available in LQFP packages that the hobbyist can actually solder.

Conclusion.

This week I have made great progress getting the basics of a code development environment put together for the STM32F746.

The board was cheap and easy to build, with a record short development time of just 14 days from starting the pcb layout to first having the LED flashing, and so far all appears to be working as intended.

This is certainly a powerful processor, and with the right coding environment, such as Arduino or mbed will make a great little target for those wanting to move into 32bit  ARM bare metal programming.








Wednesday, July 22, 2015

How Ragworm helped me create a STM32F746 breakout board in under 2 weeks

EagleCAD Layout


Last time I described how ST Microelectronics had finally released their long awaited STM32F7xx series of ARM Cortex M7 microcontrollers, and how, conveniently for the hobbyist, that a device was available in a 100 pin LQFP package.  All that was needed was a suitable break out board so that the new device could be evaluated and built into cool projects.

The Solution

The solution was to design a simple breakout board, and so the idea of BOB - the STM32F746 break out board came to fruition.

I wanted a fairly rapid turn-around on this board - so without delay I set about the CAD design - commencing after lunch on Tuesday July 7th.

Mini-Projects like this can be undertaken quickly and cheaply by the hobbyist  by using free to use (eg KiCAD, EagleCAD) printed circuit design tools that run on all common platforms.  An idea can readily be turned into a real pcb and a real design with the use of these tools plus the relatively recent online pcb manufacturing services offered by a number of companies.

Previously, I had looked at some of the very low cost pcb services - like SeeedStudio Fusion pcb - which is a Hong Kong/Shenzhen based outfit which will offer very economical pcbs - provided that your design will fit into a 5 x 5cm or 10 x 10cm board size. Outside these board formats, things become a lot more expensive. In addition, overseas shipping charges from mainland China, may cost more than the actual pcbs. Whilst these services will deliver very cheap pcbs - it may take 3 weeks or more from sending the files, to getting the boards in your hand.

Fortunately, there are a number of local suppliers in the UK, who have set up services aimed specifically at the hobbyist and prototyping marketplace. They offer typically a 10 day turnaround for 2 layer, plated through boards.  Provided that your design rules are not too extreme - and you are happy with a minimum track and gap size of 6 thou, and a minimum drill of 0.5mm then these services are fast and economical.

The Design.

The schematic design took an afternoon, as I had to create a new symbol for the STM32F746. Another half day of pushing tracks around and the layout was nearly finished.

This is an easy board as boards go - as the dual row header connectors are almost an exact 1:1 match to the pin-out of the 100 pin LQFP mcu package.  I just had to add the crystal, RTC backup super capacitor and the connectors for connecting FTDI and USB.

It was then a case of finalising the board layout in EagleCAD and then sending off the gerber files to a local pcb manufacturer.

The Sting

Departing from my supplier of over 10 years, who had become increasingly expensive, and had recently made a number of manufacturing errors and delivery cock-ups, I decided to move my business elsewhere and choose a new pcb supplier.

The Worm

This time I chose Ragworm, based in Kent, whom I had had recommended by a number of fellow open source hardware enthusiasts, in particular Glyn Hudson at Openenergymonitor - who regularly uses them for his designs.

The Ragworm operation is one part of a small family run firm, with a traditional pcb manufacturing operation, that has developed an online service specifically aimed at the needs of hobbyists or those that need a small quantity of  economically priced pcbs.

Ouch!

I first checked my existing supplier's prices,  5 off, 2 layer pcbs size 66mm x 66mm on a 10 day turnaround. The price came back at £92.66 - however this is not the full story - once you add 20% VAT and £10 for shipping we have a bill of  £121.19.

By contrast, the Ragworm online ordering process is a breeze - just enter the X and Y dimensions of the pcb and the quantity required, upload the zipped gerbers and the price you see, is the price you pay. In the case of the Bob board  the grand total was £81.02 - a saving of just over £40.  This  33% saving was such a no-brainer, and with the payment process handled by PayPal made the whole process very quick and easy.

The order was placed on the morning of Thursday 9th July, and at all stages of the process, I was kept updated by email by one of the team at Ragworm.  On the afternoon of July 16th, I was informed by email that my pcbs were to be dispatched that day - barely a week after I sent the order. A 10 day service turned around in only a week!

One of the ways in which Ragworm keep their shipping costs down, is by using UK 2nd class post. This is not the fastest service - but under the circumstances it was not a problem, and my boards arrived on the morning of Monday 20th July.

The freshly minted board back from Ragworm

The bare boards were exceptionally well made, with good definition both of the tracking and the screen printed legend.  In talking with Stacey Driver at a local Raspberry Pi event, I found out that this is because they were using a better type of white screen printing ink - that gives better clarity.  It stands out well against the orange background. Whilst these boards use ROHS compliant HASL (hot  air solder leveling) this is ideal for prototype boards  - making them easy to solder by hand.


The next challenge was to make up the pcb to the point where it would run code.  This involves soldering down the processor, the crystal, a voltage regulator and a few decoupling capacitors and connectors.  The picture above shows the minimum build of the board. Not fitted at this stage were the boost regulator (to allow operation from a Li Ion battery) and the USB components.

The board has been designed to provide a STM32F7  equivalent of the STM32F4 Discovery board. This means that almost all of the 80 or so GPIO lines are brought out directly to the dual row 50 way header connectors underneath the board.  For those familiar with the F4 Discovery board - this is a very close copy - with the important interfaces unimpeded by additional hardware. The only thing missing is the ST-Link programmer section - but if you already have a Discovery or a Nucleo board, you can use this to program and debug the board.

Initial Testing.

The FTDI cable provides a source of 5V to power this board - as well as a serial debug port.  The on board 3V3 regulator provides a source of VDD and VDDA.  Remember to connect VDDA - otherwise the PLL and clock circuit will not work, and your board will appear lifeless.

A single orange LED, connected to PC13 shows that there is life.

I had hoped to use mbed to program this board - but their compiler (at the time of writing) does not yet support the STM32F7. Instead I downloaded the 32KB codesize limited version of Keil's uVision 5 - as this provided all the toolchain and libraries that I needed.

After hacking their blinky example - so that it uses PC13 instead of PI1, I very soon had the orange led winking at me - and with the fundamentals of the pcb proven to be working, it was time to get down to some more serious code development.

I hope to use the STM32 HAL libraries to give "Arduino-like" functionality to this board. Additionally, I am recutting the Bob design onto a n Arduino MEGA compatible footprint. This will be the subject of a future post.

In the spirit of open source hardware - if you you would like the EagleCAD design files for the STM32F746  BOB board - you can find them here.  Unfortunately I cannot offer any assistance at this stage with firmware, except for the most simple of Blinky code.

The next blogpost will have more details of how I managed to get some simple firmware running on this board - plus some speed tests - relative to the Arduino.

If you are in the UK, please consider using Ragworm for your latest pcb projects.



From Ragworm - with Love :)


Thanks to Stacey, Stephanie, Adam and all the pcb staff at Ragworm, who turned this mini-project around in record quick time.

Ragworm can be found at the following site: http://www.ragworm.eu/ and also have Twitter presence @ragworm




Saturday, July 11, 2015

At last - The STM32F7xx a 216MHz ARM Cortex M7 core for the hobbyist

ARM core microcontrollers have made significant advances over the last few years, and last week saw the commercial release of the STM32F746 - an ARM Cortex M7 core that runs at 216MHz.

With this newly released microcontroller, we now have the means to make a very fast "Arduino".

At the same time as releasing the STM32F7xx range,  ST have made a low cost Discovery Board available.  This breaks with tradition over previous Discovery boards  and is very much all the bells and whistles needed to showcase the new F7 family.

It comes with a 4.3" capacitative TFT colour LCD, and some fairly sophisticated demo software.
In addition it has Ethernet, additional external SDRAM, NAND flash, audio interfaces, camera interface, microSD socket - and what little I/O is left is brought out to Arduino compatible shield headers!

You can read all about the new STM32F7-DISCO board here

More importantly for the hobbyist, the STM32F7xx part is available in a series of LQFP packages - which whist still quite a challenge to solder to a pcb - is not as impossible as Ball Grid Array BGA packages - which need to be machine placed and reflow soldered.

The STM32F746 is available in 100, 144, 176 and 208 pin LQFP packages.  The three larger sizes have additional GPIO ports, and are ideal if you want to drive an LCD using 8 bit RGB parallel data. However, I chose the 100pin package - to keep it simpler for the hobbyist.

I have been using the smaller packages of M4 ARM for about 18 months, and was keen to upgrade to the M7 and at least evaluate the 2X performance increase and the faster I/O.

My current application is particularly I/O intensive - so I needed unhindered access to all the pins. It is a multi-axis motor control board and as such needs 4 quadrature decoders - for rotary position sensing. It also needs 4 USARTS, USB, and complementary PWM for driving two dc motor H-bridges.  There is a user interface consisting of a small (2.8") colour TFT display driven by a SPI bus, 5 user buttons and a scroll wheel encoder - for menu item selection. Further inputs from sensors and safety limit switches mean that virtually all of the 80 I/O lines are  committed.

Having read the datasheet, I found that the 100 pin LQFP package is not quite pin compatible with the STM32F407 Cortex M4 device.  ST Microelectronics have chosen to add another power pin, and as a result, shifted 30 of the I/O pins one place to the left.  (The other 70 are in their original locations). This was going to need some board modifications compared to my M4 designs - but then I had the idea of  making a simple Break Out Board, which would be pin header compatible with the STM F4 Discovery Board.

Whilst the new STM32F7-DISCO board is a superb showcase for the new F7 microcontroller - the lack of free GPIO and access to peripherals made it too limiting for my application - so I decided to bite the bullet and make a very simple dev board - with every signal broken out to pin headers.




Anyone familiar with the F4 Discovery board will see that this has the same pin headers. The ST-Link section of the board is not present - as it is assumed that the user will already have a ST-Link or other means to program the board.

On the left is a mini-B USB connector which will power the board, and the mcu can run USB VCP firmware to communicate with a PC. In addition USART 1 and USART 3 are broken out in the form of FTDI compatible headers.  USART 1 can be used to bootload firmware onto the board - which I  have conveniently included RESET and BOOT switches for this purpose.  All STM32F devices (and virtually all manufacturers ARM core devices come with a factory bootloader, to allow for some simple means of programming).

In addition to a 5V supply, the board uses a MCP1640 boost regulator - and can run on voltages down to about 1V. This allows the use of LiPo or even alkaline cells to power the board.

The board is fitted with an 8MHz crystal for the main oscillator - with an on-chip PLL that synthesises the main clock at 216MHz, and a 32768Hz watch crystal for the Real Time Clock. There is also an 0.22F supercapacitor to maintain the RTC registers and the non volatile RAM.

The STM32F7xx offers a performance increase of approximately 2.2 over the previous F4 series.  It has built in display controller - so ideally suited to driving LCD panels in parallel mode. The larger LQFP packages (144, 176, 208) are ideal for direct drive of LCD panels using 8 bit RGB. There is 128K extra RAM and an external memory interface  - over and above what the F4 offered.

Costing.

Digikey, Farnell/Newark and Mouser hold stock of the STM32F746VGT6 100 pin  microcontroller.

It's one off price is about $17.75 or  $16.16 in 10 off

The 66 x 66mm pcb could be sourced from low cost board manufacturers for about $2.20 in 10 off quantity - such as SeeedStudio PCB service

So for about $25 outlay  - you could have a very capable 216MHz ARM board which can ultimately be programmed with open source tools - such as GCC, Arduino, Python, Javascript etc.

Final Impressions.

The STM32F7xx range represent the current state of the art in ARM devices to be used stand-alone in microcontroller mode- without the use of an operating system.

They contain a very rich mix of GPIO and periperals, plus the ability to drive a colour LCD - for applications where a modern user interface is needed.

At the same time, you have full access to the I/O ports - allowing you to drive I/O at up to 108MHz  - which makes for a very versatile real time controller - to interface with sensors and actuators.

USB and ethernet is on-chip (but the ethernet will need an external phy device).  ST Microelectronics provide a library of standard peripheral firmware and USB plus TCP/IP stack to allow these devices to be used to maximum advantage.

Whilst this device runs at better than 200MHz - it is only a single core device.  It is not in the same league as the 900MHz 4 core Raspberry-Pi 2, nor should it be considered to be.  It's more equivalent to a high speed Arduino Due, than a low end Pi.

The difference is that a hobbyist skilled at soldering can make and repair one of these for themself, and use it as the basis of new designs, whereas the Pi, relying heavily on BGA devices is well beyond the scope of amateur construction. The breakout board shows that you can still put a microcontroller, but a fast one, down onto a simple pcb, with crystal and reset circuit, and a serial USART connection, and with minimum effort using opensource programming tools have it up and running code.