8.Embedded Programming:

18-03-2021 | Jai Hanani

1. Objectives:

2.Group Assignment: compare the performance and development workflows for other architectures

3.Individual Assignment:

4. Files, Planning, Tools:

Files:

Planning:

Tools:


Reading the Datasheet:

This is the datasheet for the ATTiny84 - here

The Datasheet of a microcontroller is vast compost of information, much of it being not immediately useful.
The main things to look out for are:

Features:

  1. High Performance, Low Power AVR® 8-Bit Microcontroller
  2. RISC Architecture
  3. Program Memory Type: Flash
  4. Program Memory (KB): 4
  5. CPU Speed (MIPS): 20
  6. RAM Bytes: 256
  7. Data EEPROM (bytes): 256
  8. Digital Communication Peripherals: 1-SPI, 1-I2C
  9. Capture/Compare/PWM: 1 Input Capture, 1 CCP, 4PWM
  10. Operating Voltage: 1.8 – 5.5V
  11. Speed Grade:
  12. 0 – 10 MHz @ 2.7 – 5.5V
  13. 0 – 20 MHz @ 4.5 – 5.5V
  14. Temperature Range: -40°C to +85°C
  15. Internal Calibrated Oscillator of 8MHz

Pinout:

  1. VCC: Supply voltage. 
  2. GND:  Ground 
  3. Port B (PB3:PB0):  Port B is a 4-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability except PB3 which has the RESET capability. To use pin PB3 as an I/O pin, instead of RESET pin, program (‘0’) RSTDISBL fuse. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. 
  4. RESET:  Reset input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running and provided the reset pin has not been disabled. The minimum pulse length is given page 177 of the datasheet. Shorter pulses are not guaranteed to generate a reset. The reset pin can also be used as a (weak) I/O pin. 
  5. Port A (PA7:PA0):  Port A is a 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port A pins that are externally pulled low will source current if the pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running.

Registers:

Dimensions:

  1. First, I installed 'avrdude', 'gcc-avr', 'avr-libc' using sudo apt install.
  2. You can check if the installation was success by entering 'avrdude' in the terminal.
  3. In the Electronics Design week, we have fabricated a board using ATTiny84
  4. I really want to code using 'Rust Lang' instead of 'C' or 'Assembly', but I do not know if Rust is supported in AVR boards, yet.
  5. By the way, 'avr-libc' is the toolchain and 'avr-gcc' is the compiler.
  6. Toolchain vs Compiler: If we define the word "host" to mean a computer on which you are compiling, and "target" as the computer on which you want to run the code, then a native compiler is one where the target and the host are the same (kind). A cross-compiler is a compiler where the target is different from the host. A toolchain is the set of compiler + linker + librarian + any other tools you need to produce the executable (+ shared libraries, etc) for the target. A debugger and/or IDE may also count as part of a toolchain. here
  7.                     
                            void setup() {
                                DDRA = (1 << PA2);//OUTPUT LED on PA2
                              }
                              
                              void loop() {
                                PORTA = (1 << PA2);//LED is ON
                                delay(1000);
                                PORTA &= ~(1 << PA2);//LED is OFF
                                delay(1000);
                              }
                        
                    
    I tried uploading this code using Arduino IDE. It was working before, but it isn't now. Tried for an hour or so. The problem turned out to be with the resonators.
  8. I re-soldered it the oscillator again. Tried uploading again, It worked.
  9. Lost the image while minimizing
  10. DDRB |= (1 << 2) This particular code changes the value in 2nd pin of DDRB into '1', and leaves the rest as it was before.
  11. Making Hex. Flashing.
  12. How do we know the register names DDRA, PORTA, PORTB? - it is in the datasheet
  13. Configured the Makefile for programming using avrdude toolchain.
  14. I enetered make hex into the terminal. It failed.
  15. Re-ordered the #define F_CPU 20000000 line, and it worked
  16. You can tryout ASCII-HEX converter here, for better understanding: here
  17. Next, use this command to flash the connected ATTiny84 controller
  18. The breakdown:
  19. The thing is, I have programmed relatively non-trivial things in ATTiny84 in the Electronics Design week. Therefore, I intend to move on to other chips as soon as possible.
  20. I am following this article to see how to set-up ESP32 and start programming it: here
  21. I connected ESP32 to my PC using USB.
  22. Windows was smart enough to install the required driver.
  23. The device set up and ready for serial communication in COM Port 8.
  24. Next thing is establishing serial communication through a SSH client. Conventionally, it is done using something like PuTTY SSH. But, fortunately, this microsoft article says that the new microsoft terminal has a built-in SSH Client, which I am going to use - Windows-Terminal SSH.
  25. I am encountering some problems while trying to change the permissions for my COM Port. This step is essential to be able to write to the COM PORT.
  26. This seems to be WSL specific problem - Github Issue.
  27. Later, I learned that Microsoft has recently upgraded WSL to be able to communicate with COM Ports directly - WSL support for linux
  28. I have successfully changed the permissions of COMM PORT 8, and then I am installing the necessary toolchain and frameworks for ESP32.
  29. Okay, I have tried hard to do it without PuTTY, but I think I am gonna do it with PuTTY itself - here
  30. Click on Serial. Put in the relevant COM Port. Enter the relevant details
  31. By the way, remember to set the environment variables everytime a new instance of the terminal is opened. I wasted 1 hr on this.
  32. Now, I am going back to ATTiny84, we built in the Electronics Design week and programming it using Arduino IDE.
  33. When you read the pinout of any chip, it is important to realize the pin no. for the relevant environment. For example, For arduino pins, if it is 0, 1, then it is 12, 13 in PORT Pins
  34. This is a simple code that will just output 'ch'.
  35. #define RX 0
                      #define TX 1
                      #include 
                      
                      SoftwareSerial myserial(RX, TX);
                      
                      void setup() 
                      {
                      myserial.begin(9600);
                      }
                      
                      void loop() {
                      myserial.print("ch")
                      }
                      
  36. SoftwareSerial is an inbuilt library in Arduino IDE that enables Serial Communication.
  37. 9600 is the baud rate.
  38. Upload using programmer
  39. You will see the green LED light up, at the moment of upload.
  40. Once the uploading is done, Choose the appropriate port by checking in the Device Manager.
  41. Lost image while minimizing
  42. Serial Communication Success!
  43. [UPDATED]
  44. Video of serial communication[UPDATED].
  45. Trying out the morse code program, I wrote in the Electronics Design week
  46. It is a simple code which outputs '.' and '-' in morsecode fashion for 'dits' and 'dahs' respectively, and lights up the LED for 1 second, if it is a 'dit' and 3 seconds if it is a 'dah'.
  47. It works!
  48. Now coming back to the ESP32 toolchain installation, I consulted the official ESP-IDF documentation, and got it successfully installed.
  49. Lost image while minimizing
  50. Nonetheless, if you are using Windows like I am, I would recommend you take the standard windows installation route, instead of the one I took: ESP-IDF Windows Installation Process.
  51. Next up, I am going to use Arduino IDE to program the ESP32 board.
  52. This is the installation process, I followed: here
  53. Next, I verified the installation
  54. NodeMCU ESP32S

  55. The NodeMCU ESP-32S is one of the development board created by NodeMcu to evaluate the ESP-WROOM-32 module. It is based on the ESP32 microcontroller that boasts Wifi, Bluetooth, Ethernet and Low Power support all in a single chip.
  56. NodeMCU Documentation
  57. I have learned that there are essentially three ways to build your NodeMCU firmware: cloud build service, Docker image, dedicated Linux environment (possibly VM).
  58. This is the cloud service to build the firmware for your node-mcu devices: node-mcu-build
  59. I am, preliminarily, choosing as few modules as possible:
  60. NodeMCU Cloud-Based custom build is finished, and ready to download!
  61. You can use this cool GUI console to flash your Node-MCU, if you are on windows: here
  62. Failed to connect.
  63. Lost image while minimizing
  64. This article says that this problem can be solved by holding down the BOOT button: here
  65. Did not work.
  66. NodeMCU ESP32s in Arduino IDE

  67. I have decided to use Arduino IDE to program my NodeMCU ESP32S.
  68. Follow this tutorial to set-up the ESP32S board in your Arduino IDE: here
  69. Still did not work. This was the error:
    Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "NodeMCU-32S, 80MHz, 921600"
    
                      Sketch uses 198856 bytes (15%) of program storage space. Maximum is 1310720 bytes.
                      
                      Global variables use 13276 bytes (4%) of dynamic memory, leaving 314404 bytes for local variables. 
                      Maximum is 327680 bytes.
                      
                      esptool.py v3.0-dev
                      
                      Serial port COM8
                      
                      Traceback (most recent call last):
                      
                        File "esptool.py", line 3682, in 
                      
                        File "esptool.py", line 3675, in _main
                      
                        File "esptool.py", line 3329, in main
                      
                        File "esptool.py", line 263, in __init__
                      
                        File "site-packages\serial\__init__.py", line 88, in serial_for_url
                      
                        File "site-packages\serial\serialwin32.py", line 62, in open
                      
                      serial.serialutil.SerialException: could not open port 'COM8': WindowsError(5, 'Access is denied.')
                      
                      Failed to execute script esptool
                      
                      the selected serial port Failed to execute script esptool
                      
                       does not exist or your board is not connected
                      
                      
                      
                      This report would have more information with
                      "Show verbose output during compilation"
                      option enabled in File -> Preferences.
                      
  70. I am troubleshooting it by going through this github issue: github issue
  71. They recommend me to check the driver-installaton. I checked. The Windows says everything is fine:
  72. At last, got Arduino IDE working.
  73. The problem was I had another program trying to connect to COM PORT 8. I closed it. It worked.
  74. Using Mircopython Online.

  75. I would have installed micropython locally, but I am away from a good source of internet connection. I shall do it in the future.
  76. This is the interface of micropython-online:
  77. It has a terminal, editor, circuit. Beginners can try different scripts to get acquianted with the workflow without having to download and install the whole thing.
  78. Documentation for micropython lang and their std-lib: here
  79. Code for changing the angle of a servo in micropython:
                      
    # Make sure you have the Servo checkbox marked!
    
    import machine
    import pyb
    
    # The pyboard has four simple servo connections
    servo = pyb.Servo(1)
    servo.angle(0)
    
                      
  80. It is definitely a cool implementation of the python std-lib, I will do more with Micropython in the future.

  81. Group Assignment:

  82. Now coming back to ESP32S by Node-MCU, which I am doing for the Group Assignment. How does it compare to it's closer cousin ESP8266
  83. Go through this instructables article for more nitty-gritties: here
  84. Another cool thing about ESP32S is it has 10 in-built touch sensors. This is the code to on-off the LED based on touch.
  85.                     #define TOUTCH_PIN T0 // ESP32 Pin D4
    #define LED_PIN 2
    int touch_value = 100;
    
    void setup()
    {
      Serial.begin(115200);
      delay(1000); // give me time to bring up serial monitor
      Serial.println("ESP32 Touch Test");
      pinMode(LED_PIN, OUTPUT);
      digitalWrite (LED_PIN, LOW);
    }
    
    void loop()
    {
      touch_value = touchRead(TOUTCH_PIN);
      Serial.println(touch_value);  // get value using T0 
      if (touch_value < 50)
      {
        digitalWrite (LED_PIN, HIGH);
      }
      else
      {
        digitalWrite (LED_PIN, LOW);
      }
      delay(1000);
    }
                      
  86. It worked. Here's the video(Since this vid is non-essential, I chose to put it in YT):
  87. Finally, ESP32S specifications, in one place:
  88. Scanning for WiFi using ESP32S WiFi Module:

  89.                   #include "WiFi.h"
    
    String networkSSID = "";
    int strengthSignal = -9999;
    
    void setup()
    {
       // Initialize Serial to log in Serial Monitor
       Serial.begin(9600);
    
          // configuring the mode of operation of WiFi as station
          WiFi.mode(WIFI_STA);//WIFI_STA is a constant indicating the station mode
    
          // disconnect from the access point if it is already connected
          WiFi.disconnect(); 
          delay(100);
    
    //    Serial.println("Setup done");
    }
    
    void loop()
    {
    //    Serial.println("scan start");
    
      // performs the scanning of available networks
        int n = WiFi.scanNetworks();
        Serial.println("Scan performed");
    
        //check if you have found any network 
        if (n == 0) {
            Serial.println("No network found");
        } else {
            networkSSID = "";
            strengthSignal= -9999;
            Serial.print(n);
            Serial.println(" networks found\n");
            for (int i = 0; i < n; ++i) {
            //print on serial monitor each of the networks found
              Serial.print("SSID: ");
              Serial.println(WiFi.SSID(i)); //network name (ssid)
              Serial.print("SIGNAL: ");
              Serial.print(WiFi.RSSI(i)); //signal strength
              Serial.print("\t\tCHANNEL: ");
              Serial.print((int)WiFi.channel(i));
              Serial.print("\t\tMAC: ");
              Serial.print(WiFi.BSSIDstr(i));
              Serial.println("\n\n");
              
     
              if(abs(WiFi.RSSI(i)) < abs(strengthSignal))
              {
                 strengthSignal = WiFi.RSSI(i);
                 networkSSID = WiFi.SSID(i);
                 Serial.print("NETWORK WITH THE BEST SIGNAL FOUND: ( ");
                 Serial.print(networkSSID);
                 Serial.print(" ) - SIGNAL : ( ");
                 Serial.print(strengthSignal );
                 Serial.println(" )");
              }                        
             
              delay(10);
            }
        }
        Serial.println("\n------------------------------------------------------------------------------------\n");
    
    // interval of 5 seconds to perform a new scan
        delay(5000);
    }
                    
  90. For STM32 Nucleo, Go here