Preperation for session 2


#1

With the second session fast approaching here are few things that we need to keep in mind. All the code teaching and development for the microcontroller will be done on a Linux based OS. For those who have got a Linux distro working, please follow the steps below. Those who still haven’t got a Linux OS up and running please get an empty pen-drive, we’ll create a live boot disk.

Steps for setting up the development environment

These instructions are valid for a x64 Linux system with a Debian based environment. The commands listed below need to be executed in a terminal (Ctrl + Alt + t)

  • Install ARM GCC compiler.

  • sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa

  • sudo apt-get update

  • sudo apt-get install gcc-arm-embedded

  • Install git by sudo apt-get install git Git is a software used for version control of software.

  • Install the front-end GUI for git with sudo apt-get install git-gui

  • Clone the repository used for first session by git clone https://gitlab.com/fsmk/EfH-nRF52-basic.git {Directory} Replace {Directory} to the path where you want the repository to reside.

  • Install Cutecom to send and receive data over the serial terminal sudo apt-get install cutecom

  • Perform this step to use the serial ports without requiring root access sudo usermod -a -G dialout $USER

  • Install Doxygen for documentation by sudo apt-get install doxygen

  • Get all the documents of the Bluey board by cloning Electronut’s repository git clone https://github.com/electronut/ElectronutLabs-bluey.git {Directory}

  • You can use any editor for writing and working with the code. We recommend Atom. Download and install it from the package at https://atom.io/download/deb

  • We’ll be using StLink Clone programmers reflashed with Black Magic Probe (BMP) software. As mentioned here create a file /etc/udev/rules.d/99-blackmagic.rules with the following contents:

    # Black Magic Probe
    # there are two connections, one for GDB and one for uart debugging
    SUBSYSTEM=="tty", ATTRS{interface}=="Black Magic GDB Server", SYMLINK+="ttyBmpGdb"
    SUBSYSTEM=="tty", ATTRS{interface}=="Black Magic UART Port", SYMLINK+="ttyBmpTarg"
    

Location and other details

The second session will happen at LatLong, located at http://www.openstreetmap.org/node/3054883895 Please make sure that you reach by 10 am. As mentioed above, do get an empty pen-drive if you don’t have a Linux OS up.


#2

Code sample : blinks the 3 leds in a different pattern every time the button is pressed.
rough algo:

  1. set up inputs (3 leds)
  2. set up outputs (button)
  3. in a while loop
    (check if button active, and keep changing state everytime a press is detected. based on state, blink a leds in a particular pattern).

notes: the code to check button active only succeeds if the processor is not busy doing delays etc when the button is actually pressed. so keep ur cutecom open and keep the button pressed till u see the msg saying button pressed.

#include <stdbool.h>
#include <stdint.h>
#include "hal_nop_delay.h"
#include "hal_gpio.h"
#include "bluey_1v1.h"
#include "tinyprintf.h"
#include "uart_printf.h"
#include "pri_pins.h"

#define ORIG 0
#define FIRST_PRESS 1
#define SECOND_PRESS 2
#define THIRD_PRESS 3

/**
 * @brief Function for the main entry of the application.
 */
int main(void){
  bool pressed=false;
  short pinval=0;
  short state = ORIG;

  uart_printf_init(UART_PRINTF_BAUD_9600);

  /* Configure all the LEDs as output. */
  hal_gpio_cfg_output(LED_1, 1);
  hal_gpio_cfg_output(LED_2, 1);
  hal_gpio_cfg_output(LED_3, 1);

  // configure the button as an input
  hal_gpio_cfg_input(BUTTON_1, HAL_GPIO_PULL_DISABLED);
  hal_gpio_cfg_input(SWITCH_1, HAL_GPIO_PULL_UP);

  tfp_printf("Hello there ");

  //init output pins
  hal_gpio_pin_clear(LED_1);
  hal_gpio_pin_clear(LED_2);
  hal_gpio_pin_clear(LED_3);

  while (1) {
    ///check if button pressed
    pinval = hal_gpio_pin_read(BUTTON_1);
    //tfp_printf("Read Button value %d \n", pinval);
    if ((pinval == BUTTONS_ACTIVE_STATE)){
        if (!pressed) {
          pressed = true;
          tfp_printf("Read Button value %d \n", pinval);
          tfp_printf("Button pressed, changing state\n");
          switch(state) {
            case ORIG:
              tfp_printf("Changing state from orig to first\n");
              state = FIRST_PRESS;
              break;
            case FIRST_PRESS:
              tfp_printf("Changing state from first to second\n");
              state = SECOND_PRESS;
              break;
            case SECOND_PRESS:
              tfp_printf("Changing state from second to third\n");
              state = THIRD_PRESS;
              break;
            case THIRD_PRESS:
              tfp_printf("Changing state from third to orig\n");
              state = ORIG;
              break;
            default:
              state = ORIG;
              break;
          }
        }
        pressed = false;
      }
      //tfp_printf("State set, turn the leds on accordingly\n");
      tfp_printf("State = %d\n", state);
      switch (state) {
        case ORIG:
            //descending red blue green
            hal_gpio_pin_write(LED_1, LED_RED);
            hal_gpio_pin_write(LED_2, LED_GREEN);
            hal_gpio_pin_write(LED_3, LED_BLUE);
            hal_gpio_pin_toggle(LED_1);
            hal_nop_delay_ms(400);
            hal_gpio_pin_toggle(LED_2);
            hal_nop_delay_ms(400);
            hal_gpio_pin_toggle(LED_3);
            hal_nop_delay_ms(400);
            break;
        case FIRST_PRESS:
            hal_gpio_pin_write(LED_2, LED_GREEN);
            hal_gpio_pin_clear(LED_1);
            hal_gpio_pin_clear(LED_3);
            hal_nop_delay_ms(400);
            break;
        case SECOND_PRESS:
            hal_gpio_pin_write(LED_3, LED_BLUE);
            hal_gpio_pin_clear(LED_1);
            hal_gpio_pin_clear(LED_2);
            hal_nop_delay_ms(400);
            break;
        case THIRD_PRESS:
            hal_gpio_pin_write(LED_1, LED_RED);
            hal_gpio_pin_clear(LED_2);
            hal_gpio_pin_clear(LED_3);
            hal_nop_delay_ms(400);
            break;
        default:
            break;
          }
        }
}