Chapter 4 - Creating your first program (Firmware)

Setting up our Programming environment - Arduino IDE

When you ever hear people say they used an Arduino for their project, they’re probably talking about the Arduino Uno. In this workshop, interfacing the TomatoCube* C3 RISC-V Core Module to Arduino IDE provides us with the same simple and easy-to-use UI’s loved by the Maker community. Furthermore, if you have used Arduino with a UNO before, you will find lots of the coding syntaxes remain the same.

This chapter focuses on configuring Arduino IDE to recognize & support our C3 RISC-V Core Module with Arduino IDE. Please do follow the steps carefully.


STEP 1: Installing & Launching Arduino® IDE

Follow the official installation guide found on the Arduino® website.
 https://www.arduino.cc/en/guide/windows 
 https://www.arduino.cc/en/Guide/macOS




STEP 2: Installing the required Windows Driver
  Windows only

Before you plug in the CoreModule to you Laptop or PC, it is recommended to get the Driver pre-installed.


Download the latest Windows driver from the USB interface chip's manufacturer website.

 http://www.wch.cn/downloads/CH341SER_ZIP.html





Once the driver installation is completed, plug the CoreModule into any Available USB port on your PC/Laptop using a micro-USB cable.

After a series of Chimes coming from your PC/Laptop speakers, signifying a new piece of hardware is detected. Open your Windows “Device Manager” and check for the new entry under the “Ports” sub-menu.

Do take note of the enumerated Port name. “COM5” for our case.




STEP 3: Adding additional board support URL

A fresh installation of the Arduino® IDE software will not be compatible with the CoreModule Hardware. Luckily for us, Espressif has been pretty diligent in maintaining a healthy Arduino® IDE add-ons. All the files can be found in Espressif's Github repository.
https://github.com/espressif/arduino-esp32

We are going to choose an easier method & rely on preconfigured script to update our Arduino® IDE & the new files in the correct locations.





STEP 4: Adding additional board support through Arduino Board Manager

Pop over to the board menu to bring up Arduino's Board manager tool window.

  • In your Arduino IDE window, under the Tools menu
  • Choose the Board:... menu.
  • Select Board Manager once the submenu popped up.


In the Boards Manager, search esp32 by Espressif Systems. Install the latest version (version 2.0.1 as of this writing).


STEP 5: Choosing the right board

It is going to take a couple of minutes (Dependent on your internet speed) Once completed, we will need to instruct Arduino to generate the code (Compile the code) for our RISC-V CPU architecture based ESP32C3 Core module.

  • In your Arduino IDE window, under the Tools menu
  • Choose the Board:... menu.
  • Mouse over the entry ESP32 Arduino.
  • Pick the very first board entry in the popup submenu ESP32C3 Dev Module.

STEP 6: Setting the C3 RISC-V Core Module Uploading parameters

Next, we will need to configure the upload speed from your computer to the RISC-V Core Module. We will go with a slower speed for reliability (You are free to explore the higher speed at your own leisure).

At the same time, we will also inform Arduino® IDE which UART port enumerated by your Operating System to use for uploading your core (firmware).

  • In your Arduino IDE window, under the Tools menu
  • Mouse over the entry Upload Speed:....
  • Chose the upload speed (in bps - bits per second) 115200
  • Plug your CS RISC-V Core Module to your computer now. 
  • Once again, in your Arduino IDE window, under the Tools menu
  • Mouse over the entry Port.
  • For  Windows user, please choose the COM port shown on your Device manager in Step 2.
    For  Mac user, an entry with the name cu.USB.. or cu.TTY... needs to be selected.
    (Talk to your instructor if you have trouble locating the correct USB Serial Port).


STEP 7: Compile & Upload the code to the Core Module

The last step required to get your Blinky code onto your Core Module would be to start the code compilation & follow with uploading the code to the Core Module.

Arduino® IDE simplify the entire process down to a click of the Play  button found on the top left corner of your Program window.



If the upload is successful, the message "Done uploading" will appear in the status bar.



What is a Sketch?

Sketch − A new terminology invented by the Arduino foundation, it refer to an embedded software project or simply your source code. It has a extension of .ino


Sketch Structure

A basic Arduino programs can be divided in three main parts: 
(a) Some variable declarations,
(b) a Setup() function
(c) a Loop() function.

The setup() function is called when a sketch starts. it will only run once, upon power up or reset of your embedded board. 
Think of it as a perfect place to initialize the variables, pin modes (Input or Output) & initialize some custom libraries/hardware.

loop() function loops consecutively after the setup() has been executed, It would allow your program(Firmware) to response indefinitely.
 

 Todo:

  • Change the rate of blinking by modifying the parameters to the delay() procedure.
  • What would happen if you were to have a different rate for both the delay()?
  • What would happen if you were to remove one of the delay() statement?