Getting Started SPIN 2 Serial Monitor - owntech-foundation/Tutorials GitHub Wiki

Objective

The goal of this tutorial is to turn ON and OFF the LED of the O2, via the serial port using the keyboard of your computer. We will start from the Blinky tutorial.

Required hardware

  • SPIN v_0_9
  • STLinkV3
  • PC 64-bits (windows or linux)

Required software

  • Git
  • Visual Studio Code with PlatformIO (see Blinky tutorial)

Create the project

  1. We will create the project by copying the Blinky tutorial in a new branch, that will be called serial_monitor. In the bottom menu, click on the New Terminal icon new_terminal_icon . This will open a new terminal into which you can write the following commands to create a new git branch.
git status
git branch serial_monitor
git checkout serial_monitor

This will create a new branch in which all of your work will be written. The main branch from which you started will remain as it was when you cloned it. It is important that this branch remain untouched as you might use it in the future to get new upgrades from the OwnTech team.

  1. In the bottom menu, check that you are now in the serial_monitor branch.

switch_to_serial_monitor

Step-by-step implementation

  1. Define the variables

In src/main.cpp, in the section USER VARIABLE DECLARATIONS, do not modify anything.

  1. Configure the hardware peripherals

In src/main.cpp, in the function setup_hardware(), check that we load the version v_1_1_2 of the O2 and that the console is activated for exchanging messages via serial.

void setup_hardware(){
    hwConfig.setBoardVersion(SPIN_v_0_9);
    console_init();
}
  1. Configure the software scheduling

In src/main.cpp, in the function setup_software(), check that the communication and application tasks are initialized.

void setup_software()
{
    scheduling.startCommunicationTask(loop_communication_task,COMMUNICATION_THREAD_PRIORITY);
    scheduling.startApplicationTask(loop_application_task,APPLICATION_THREAD_PRIORITY);
}
  1. Loop communication task

To define the communication task, we implement a communication loop function that will treat the characters it receives via the serial port. Each character will be associated with a different mode.

In src/main.cpp, in the list enum serial_interface_menu_mode, declare a new mode called SERIALMODE.

enum serial_interface_menu_mode //LIST OF POSSIBLE MODES FOR THE OWNTECH CONVERTER
{
    IDLEMODE =0,
    SERIALMODE
};

In src/main.cpp, in the function loop_communication_task(), add the following code. This code listens to the serial port and performs an action depending on the character it has received. The character h displays the help menu. The character i switches to IDLEMODE. The character s switches to SERIALMODE.

void loop_communication_task()
{
    while(1) {
        received_serial_char = console_getchar();
        switch (received_serial_char) {
            case 'h':
                //----------SERIAL INTERFACE MENU-----------------------
	        printk(" _____________________________________\n");
                printk("|     ------- MENU ---------          |\n");
                printk("|     press i : idle mode             |\n");
                printk("|     press s : serial mode           |\n");
                printk("|_____________________________________|\n\n");
                //------------------------------------------------------
                break;
            case 'i':
                printk("idle mode\n");
                mode = IDLEMODE;
                break;
            case 's':
                printk("serial mode\n");
                mode = SERIALMODE;
                break;
            default:
                break;
        }
    }
}
  1. Loop application task

Let's create an application task that turns the LED1 ON or OFF depending on the mode. In src/main.cpp, in the function loop_application_task(), add the following code.

void loop_application_task()
{
    while(1){

        if(mode==IDLEMODE) {
            hwConfig.setLedOff();

        }else if(mode==SERIALMODE) {
            hwConfig.setLedOn();
        }        
        k_msleep(100);    
    }
}
  1. Loop control task

In src/main.cpp, in the loop_control_task() function, do not modify anything. This control task will remain empty for this tutorial.

  1. Connect hardware (see Blinky tutorial)
  • Connect the USB power supply cable. The LED2 of the SPIN should be ON.
  • Connect the micro-JTAG connector of the SPIN to the PC thanks to the STLinkV3.

Hardware connection

  1. Build and Upload (build_icon+ flash_icon).
  2. In the bottom toolbar, click on the Serial Monitor icon serial_icon. Select it and press the h key.

Expected outputs

  • Press the h key to display the help menu.
  • Press the s key to switch to SERIALMODE and turn the LED1 ON.
  • Press the i key to switch to IDLEMODE and turn the LED1 OFF.

idle_serial_mode

That’s it!

Contributors

  • 2021.11.04: Romain Delpoux, Loïc Quéval, Adrien Prévost
  • 2021.11.07: Luiz Villa, Antoine Boche
  • 2022.01.24: Luiz Villa, Adrien Prevost, Loïc Quéval
  • 2022.02.01: Luiz Villa, Clement Foucher
  • 2022.02.22: Luiz Villa
  • 2022.03.13: Luiz Villa
  • 2022.06.23: Loïc Quéval