9. Embedded programming¶
Assignments¶
- Documented what you learned from reading a microcontroller datasheet.
- Programmed your board
- Described the programming process/es you used
- Included your code
Trying Arduino¶
During week07, I have already tested and programmed my board with Arduino. See my assignment here
I film my test here :
So, this week, I tried the serial link. To do this, I used the library SoftwareSerial and modified my previous code with the button and the LED, so when I press the button, the board send to the serial Button pressed
Here is the code :
#include <SoftwareSerial.h> SoftwareSerial mySerial(0, 1); // RX, TX int buttonPin = 2; int ledPin = 6; bool buttonState = 0; void setup() { mySerial.begin(4800); mySerial.println("Hello, world"); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); digitalWrite(ledPin, HIGH); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { digitalWrite(ledPin, LOW); mySerial.println("Button pressed"); } else { digitalWrite(ledPin, HIGH); } }
And here is the result :
So as it work with Arduino I decided to test with Platform IO.
Trying PlatformIO with Atom and Visual Studio Code¶
First, with PlatformIO into Atom, I created my project. PIO generate automatically all the file and folder you need. Just select the Board and the Framework.
After that, I paste my Arduino program into Atom and try to upload it :
#include <Arduino.h> int buttonPin = 2; int ledPin = 6; bool buttonState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); digitalWrite(ledPin, HIGH); } void loop() { buttonState = digitalRead(buttonPin); if (buttonState == LOW) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); } }
But I got an “Error1” :
Uploading .pioenvs/attiny44/firmware.hex avrdude done. Thank you. *** [upload] Error 1 ========================== [ERROR] Took 2.17 seconds ==========================
Searching for the meaning of this ERROR on internet and I got a new error message :
avrdude: ser_open(): can't open device "unknown": No such file or directory
Finally, I go the solution here : https://docs.platformio.org/en/latest/platforms/atmelavr.html
To upload firmware using programmer you need to use program target instead of upload for platformio run --target command. For example, platformio run -t program.
and :
Upload options like upload_port don’t work as expected with platformio run -t program. You need to use upload_flags if you want to specify custom port or speed (see examples below).
So You need to add these line into the platformio.ini :
; each flag in a new line upload_flags = -P$UPLOAD_PORT ; edit this line with valid upload port upload_port = SERIAL_PORT_HERE
And change the SERIAL_PORT_HERE
by the ref to your serial port.
I also added these line for the cpu speed :
; change microcontroller board_build.mcu = attiny44 ; change MCU frequency board_build.f_cpu = 20000000L
After that, the upload was done correctly, but nothing happened on my board. It was like I uploaded a blank firmware to my board …
So I get back to arduino, just to check that I didn’t break my board. And every thing work like a charm.
So, I try my code into visual Studio Code and find an interesting error :
cannot open source file arduino.h platformIO
Trying to rebuild the index with Intellisense in VSC. The problem didn’t re-appeared but it still didn’t work when uploading the firmware to the board …
I find out an interesting pages by FabLab Barcelona here : http://fab.academany.org/2019/labs/barcelona/local/clubs/electroclub/embeddedprogramming/
I was thinking that my fuses wasn’t ok, but on VSC, I’ve got the confirmation that it is ok :
avrdude: safemode: Fuses OK (E:FF, H:DF, L:FE)
So everything seems to be ok … Perhaps, my firmware didn’t start … And I found that my LED SCK still ON on my TinyISP board. And it didn’t do that when I program it with Arduino.
Trying the same thing with the board of another student Benjamin Lemay, and the same issue appeared …
So I took a look at the datasheet of the Attiny and I discovered that, as I use th ISP pins for other function, I forgot to add resistor in series with the line, like in this picture ( taken from this site) :
The resistors should be 4,7K. I will think about it for the next board.
BUT ! Something is wrong with this hypothesis :
- Why Arduino succeed in programming my board ?
- The board of Benjamin don’t use the ISP pins …
So, I think it’s another thing …
Conclusion :¶
At this time (20/03/2019), I didn’t manage to make PlatformIO working with my board or other board, even with Atom or Visual Code Studio …
Edit : In fact, I discovered that the pinout of the ATTiny44 is not the same on Arduino and on PlatformIO. In fact, It’s using a alternative Pinout. To solve that, you need to use the updated core for attiny you can find here, and burn the new sequence and it works like a charm.