Week 9:
Embedded Programming
Assignment
Individual assignment:
Read a microcontroller data sheet program your board to do something, with as many different programming languages and programming environments as possibleGroup assignment :
Compare the performance and development workflows for other architecturesWhat I made this Week
Reading a Datasheet: ATTINY44
The ATTINY44 is a low power AVR® 8-bit microcontroller based on the RISC architecture.It
have 12 GPIO pin's.
The Atmel ATtiny24/44/84 provides the following features:
2/4/8K bytes of in-system
programmable flash, 128/256/512 bytes
EEPROM, 128/256/512 bytes SRAM, 12 general purpose I/O lines, 32 general purpose working
registers, an 8-bit Timer/Counter with
two PWM channels, a 16-bit Timer/Counter with two PWM channels, internal and external
interrupts, an 8-channel 10-bit ADC, programmable
gain stage (1x, 20x) for 12 differential ADC channel pairs, a programmable watchdog timer
with internal oscillator, internal calibrated
oscillator, and three software selectable power saving modes
The best way to know about a microcontroller and Instruction set is reading its Datasheet.we will get all the information from the Datasheet , only problem is that its not beginner friendly. I started Reading ATtiny44 Datasheet that I got from the Micorchip's website as it was a part of the assignment this week..
Specifitcations
Name | Value |
---|---|
Program Memory Type | Flash |
Program Memory Size (KB) | 4 |
CPU Speed (MIPS/DMIPS) | 20 |
SRAM Bytes | 256 |
Data EEPROM/HEF (bytes) | 256 |
Digital Communication Peripherals | 1-SPI, 1-I2C |
Capture/Compare/PWM Peripherals | 1 Input Capture, 1 CCP, 4PWM |
Timers | 1 x 8-bit, 1 x 16-bit |
Number of Comparators | 1 |
Temperature Range (C) | -40 to 85 |
Operating Voltage Range (V) | 1.8 to 5.5 |
Pin Count | 14 |
Architecture
The ATTINY44 runs with the RISC Architecture. RISC or reduced instruction set computer is a computer which only uses simple commands that can be divided into several instructions which achieve low-level operation within a single CLK cycle, as its name proposes “Reduced Instruction Set”. It's performance is optimized with more focus on software. RISC mainly Used in Microcontroller.
The Block Diagram for the ATTINY44, according to the datasheet has four main sections,
- The ALU
- Timer/RAM/Counter/EEPROM
- The Data Registers
- and The Programming interface
ALU Unit:
The ALU - Arithmetic Logic Unit - operations are divided in some categories like: arithmetic, logical and bit-functions. Status register contains the most recently executed arithmetic instruction.
Timer & Counter:
The main features of the timer - counter are:
ADC Characteristics:
The ADC table characteristic are:
In order to choose an ADC, you need to choose multiplexers according to this table.
Pin descriptions
Port A Configuration:
Port B Configuration:
Programming the ATTINY44 with Arduino
The Arduino Development Environment can be used to program the ATTINY44. It makes life much easier for the everyday maker like me by getting the full support of the Arduino IDE.
Installation
Installing Arduino:
Arduino can be downloaded free from the official website here. Installing it is very straight forward.
Adding TinyCore (support for ATTiny MCU Series to Arduino)
To program the ATTINY microcontroller family using Arduino, I will be using ATTinyCore, which is an Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8 series of microntrollers. The installation instructions are given on the github page and I just followed the Instructions for the Boards Manager Installation Method.
Board Manager Link:
http://drazzy.com/package_drazzy.com_index.json
Usage
The usage is straightforward, it accepts arduino code syntax as it is. Heavy
libraries like the servo.h doesn't work, but there are many ways to work around it.
To set the fuse for the 20Mhz external oscillator, the bootloader needs to burned
before the initial use.
Software Serial is based on the PCINT_VECT() for which, Pin change interrupts can not be used directly. I faced this issue in my Input Device Week when I was working on the rotary Encoder.
Code:
This code below blinks two LEDs on pin PA3 and PA4.
Command Line Output
Output
HEX footprint:
The generated HEX can be exported from arduino by clicking on the 'Export Compiled Binary' option.
HEX Output:
Download Files:
Download Arduino Project Folder (Compiled Binary inlcuded)Programming the ATTINY44 with GCC C
AVR microcontrollers can be programmed using GCC C. I'll be using Atmel Studio as the IDE. Most of Neil's code is written in C, which I have been having a hard time understanding. Hopefully after tinkering with Atmel Studio a bit, I'll be able to write my onw C code.
Setting up Atmel Studio
Atmel Studio is free to download from Atmel's official website. Installation is straightforward but it takes a long time since it is based on visual studio.
Usage
The Usage is not as straight forward as Arduino. First we have to create a project, we'll select a 'GCC C Executable Project' and for device, we'll select 'ATTINY44'
The UI:
Adding Support for the FABISP:
The FABISP needs to be added as an external tool to Atmel Studio if we want to use it to upload our code.
External Tool Settings for FabISP
- Title: USBTiny ISP.
- avrdude.exe
- -c usbtiny -p attiny44 -Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i
** (avrdude path variable needs to be added)
Code:
Command Line Output for Build:
Command Line Output for Flashing:
The Output on the board was the same as the code was the same, just written in different languages
HEX Footprint:
Download Files:
Download Atmel Studio Project FolderComparison of Programming Methods for ATTINY44
Arduino | Atmel Studio | |
---|---|---|
Ease of Installation: | One Click Installation. | Based on Visual Studio, takes a long time to install. |
Ease of Use: | Very intuiitive UI, very easy to use | Complicated UI but has alot of features |
Debugging: | No Debugging tools, Serial monitor built in. | Debugging tool present, but takes a while to get used to |
HEX Footprint Size: | Huge HEX footprint, about 60 line(930 bytes) | Much Smaller HEX footprint, just 13 lines (182 bytes) |
Conclusion:
I definitely prefer the arduino IDE and the arduino programming language, but GCC C is much more optimized and lighter. Maybe I'll try learning more C code as Fab Academy progresses, but the Arduino programming language will definitely be my primary language for the time being.
STM32F103C8T6 Datasheet
The STM32F103xx medium-density performance line family incorporates the
high-performance
ARM®Cortex®-M3 32-bit RISC core
operating at a 72 MHz frequency, high-speed embedded memories (Flash memory up to
128
Kbytes and SRAM up to 20 Kbytes),
and an extensive range of enhanced I/Os and peripherals connected to two APB buses.
All
devices offer two 12-bit ADCs, three
general purpose 16-bit timers plus one PWM timer, as well as standard and advanced
communication interfaces: up to two I2Cs
and SPIs, three USARTs, an USB and a CAN.
Specifications:
- 72 MHz maximum frequency,1.25 DMIPS/MHz (Dhrystone 2.1) performance at 0 wait state memory access
- Single-cycle multiplication and hardware division
- 64 or 128 Kbytes of Flash memory
- 20 Kbytes of SRAM
- 2.0 to 3.6 V application supply and I/Os
- POR, PDR, and programmable voltage detector (PVD)
- 4-to-16 MHz crystal oscillator
- Internal 8 MHz factory-trimmed RC
- Internal 40 kHz RC
- PLL for CPU clock
- 32 kHz oscillator for RTC with calibration
- Sleep, Stop and Standby modes
- VBAT supply for RTC and backup registers
ARM®32-bit Cortex®-M3 CPU Core
Memories
Clock, reset and supply management
Low-power
- Conversion range: 0 to 3.6 V
- Dual-sample and hold capability
- Temperature sensor
- 7-channel DMA controller
- Peripherals supported: timers, ADC, SPIs, I2Cs and USARTs
- 26/37/51/80 I/Os, all mappable on 16 external interrupt vectors and almostall 5 V-tolerant
- Serial wire debug (SWD) & JTAG interfaces
- Three 16-bit timers, each with up to 4 IC/OC/PWM or pulse counter and quadrature (incremental) encoder input
- 16-bit, motor control PWM timer with dead-time generation and emergency stop
- 2 watchdog timers (Independent and Window)
- SysTick timer 24-bit downcounter
- Up to 2 x I2C interfaces (SMBus/PMBus)
- Up to 3 USARTs (ISO 7816 interface, LIN, IrDA capability, modem control)
- Up to 2 SPIs (18 Mbit/s)
- CAN interface (2.0B Active)
- USB 2.0 full-speed interface
2 x 12-bit, 1 μs A/D converters (up to 16 channels)
DMA
Up to 80 fast I/O ports
Debug mode
7 timers
Up to 9 communication interfaces
CRC calculation unit, 96-bit unique ID
Packages are ECOPACK®
Making a STM32F103C8T6 board
Making an STM32F103C8T6 is much harder than making a ATMEGA328p or ATTINY44 board because of
the large number of peripheral components required.
The programming procedure is also quite difficult requiring to load an OS on the device
first, then working from there. The OS can be flashed using
a JTAG prorammer or an ST-link official programmer.
The Minimal circuit for the STM32F103C8T6 can be found in the official ST documentation for Getting started with STM32F10xxx hardware development application note.
LQFP48 Breakout Board for STM32F103C8T6
The footprint for the STM32F103C8T6 we have at the lab is LQFP48 (7mmx7mm) which is not possible to mill with the (1/64) bit we have at the lab. So I went ahead and bought a breakout board for the LQFP48 package and soldered the STM32F103C8T6 onto it. But I didn't realize how hard it would be to get all the peripheral components connected to the breakout board. I ran out of time I allocated to explore the STM32F103C8T6.
Programming the STM32F103C8T6 Development board
I resorted to using an STM32F103C8T6 development board instead of making my breakout
board
work. It was pretty straightforward from there. It came with all peripheral
components on
board and Serial/USB and SWIM interface for programming.
All information on these generic STM32F103C8T6 development board, also known as
the Blue
Pill development
board can be found here.
Pin Out Diagram
Installing ST-Link v2 Drivers:
The programmer we have at the lab is an ST-Link v2. The drivers for it can be downloaded freely from ST's official Site.
Using STM32Duino to program the Blue Pill Dev. Board
To program the generic blue pill development board I have, I'll be using
STM32duino, the arduino Core
for STM32.
Installation instructions can be found here.
To use the board manager installation method, the following url needs to be added to
the Additional Board Manager URLs,
https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
Uploading a Blink Code:
The blue pill development board is a minimal one and only has one programmable LED on PC13 for test programming. I wrote a blink code for it and uploaded the program using Arduino. The Connection from the SWIM interface on the blue pill to the JTAG breakout on the ST-Link is explained on the stm32duino wiki. The Pinout is as follows;
- 3v3--------------------------------------pin 1
- SWO------------------------------------ pin 7
- SWCLK-----------------------------------pin 9
- GND-------------------------------------GND
Blue Pill ---------------------ST-Link
Code:
Hex footprint:
Surprisingly, the hex footprint to run this small code is 715 lines long. (32,768 bytes)
Output:
Download:
Download STM32duino Project folder(compiled binary included)Using Arm Mbed to program the Blue Pill Development Board
Mbed is an online embedded device platform which gives you an embedded operating system, transport, security and cloud services to create Embedded solutions. Although it is designed mostly for IoT applications, the generic chinese blue pill development board can also be programmed using it. I think it is an integrated solution that is easy to work on and easy to deploy. More about what is Mbed and how it works, can be found here.
Getting Started with Mbed and the Generic Blue Pill Development board:
Signing Up:
Getting started with Mbed is easy, just sign up for and account at their sign up page.Adding Hardware for Generic Blue Pill in our console:
After you sign up, You should be able to navigate to your console.Before we can get started with programming, we need to add our device to the hardware list in our console. The generic blue pill development board for the STM32F103C8T6 is not supported officially by mbed. So we have to add it as a Nucleo-F103RB. It uses the same chip but has 128kb flash memory instead of our 64kb board. I found this nifty guide, on how to program our generic board using Mbed.
Using the Online Mbed Compiler
The compiler UI is a bit confusing at first. I would recomend using the many example
programs
given by Mbed and writing your own code
by modifying the templates. First we'll create a new program, and I named it
"HelloWorld_blinky" and is based on the 'Display a message
on PC using UART' example.
The program project folder will be created along with the 'main.c' file, which we
will be
editing to write our own code.
The example code looks a bit like this, but it didn't work out of the box because
the
default Serial Tx/Rx pins for the nucleo
and the blue pill are different. So after going through the Generic Blue Pill Guide for Mbed,
I
wrote the following code.
Compiling and optimizing:
The Blue Pill board has 64KB memory, while the nucleo has 128kb. So after we click compile, we have to check on the 'Build Details', and manually ensure the Flash size does not exceed 64kb. For our case, the Flash Size of the build was 27.6kb which was well under 64kb. Once you click compile, the compiled .bin file should automatically downloaded in your browser.
Uploading the Code:
To upload the .bin file we need to download the ST link utility Once downloaded, the utility program can be run and should look like this. Connect your Blue pill with the ST-link, the pinout diagram is the same as when we connected it to upload code using STM32duino.
- 3v3--------------------------------------pin 1
- SWO------------------------------------ pin 7
- SWCLK-----------------------------------pin 9
- GND-------------------------------------GND
Blue Pill ---------------------ST-Link
ST-link Verbose Output
Viewing the Output:
To view the UART output, connect the FTDI module with the blue pill dev. board, make sure the FTDI is in 3.3v mode or you'll destroy the board. The connection should be;
- 3v3--------------------------------------3.3v
- A3--------------------------------------- TX
- A2----------------------------------------RX
- GND-------------------------------------GND
Blue Pill ---------------------FTDI
Download Project Files:
Download HelloWorld_Blinky_zip_nucleo_f103rb.zipDownloads
ATTINY44- Arduino | Download Arduino Project Folder (Compiled Binary inlcuded) |
ATTINY44- Atmel Studio | Download Atmel Studio Project Folder |
STM32F103C8T6- STM32Duino | Download STM32duino Project folder(compiled binary included) |
STM32F103C8T6- Arm Mbed | Download HelloWorld_Blinky_zip_nucleo_f103rb.zip |
Learning Outcomes
- I read through the datasheet for ATTINY44 and STM32F103C8T6
- I learnt how to program the ATTINY44 with GCC C through Atmel Studio
- I learnt how to use the Fab ISP with Atmel Studio
- I learn't how to use the STM32F103C8T6 in a board and the minimum circuit requirements to use it.
- I learnt to use the Blue Pill Development board for the STM32F103C8T6 and program it using the ST-Link v2
- I learnt how to use STM32duino
- I learnt how to use Arm Mbed
- I now have a better understanding how the instruction sets work in a microcontroller, how a program is compiled and how it is flashed.