Assignments for session 3


#1

Hey everyone,

Here are the videos that we showed the during the presentation yesterday:

The assignments for this session are given below. Since we had a lot to cover this session, it is important that one goes through these assignments to get a feeling of how these libraries work.

  • Download the datasheet for nRF52, the chip that we’re working with. It can be found here. Read and understand chapter 2 About this document, 15 Peripheral interface and 24 TIMER — Timer/counter. Come back to us if you don’t understand any part of this.
  • Make the red LED blink every half second for 10 seconds and then not blink anymore
  • Make a LED glow when the word ‘glow’ is sent and stop glowing when the word ‘stop’ is sent to the serial port of the nRF52 from the computer.
  • Control the brightness of a LED by reading the analog value of a voltage divider of a potentiometer.

As always get in touch with us if you have any questions. Cheers!


#2

Posting the code and output from serial console for questions 1 and 2 of assignment 3.

Q1:

/*  Copyright (c) 2016, Appiko
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 *  3. Neither the name of the copyright holder nor the names of its contributors
 *  may be used to endorse or promote products derived from this software without
 *  specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @addtogroup group_appln
 * @{
 *
 * @defgroup bluey_demo A application to demo the Bluey's capabilities
 * @brief A Bluey demo
 * @{
 */

#include <stdbool.h>
#include <stdint.h>

#include "nrf.h"

#include "boards.h"
#include "hal_clocks.h"
#include "hal_nop_delay.h"
#include "hal_gpio.h"
#include "common_util.h"
#include "tinyprintf.h"
#include "uart_printf.h"
#include "ms_timer.h"
#include "nrf_util.h"

/** @brief Configure the RED LED pins as output and turn off LED */
static void led_init(void)
{
    hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
}

/** @brief turn off Red LED */
static void led_red_off(void)
{
    hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
}

/** @brief turn on Red LED */
static void led_red_on(void)
{
    hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
}

/** @brief Blink the RED led*/
static void led_red_blink(void)
{
    tfp_printf("LED RED blinks\n");
    led_red_on();
    hal_nop_delay_ms(10);
    led_red_off();
}


void stop_timers() {
    tfp_printf("Stop blinking\n");
    ms_timer_stop(MS_TIMER0);
    led_red_off();
}


/**
*/
 /* @brief Function for application main entry.*/
int main(void)
{
    void (*led_blink)(), (*stopper)();

    //set up function pointers
    led_blink = &led_red_blink;
    stopper = &stop_timers;

    led_init();

    /* Initial printf */
    uart_printf_init(UART_PRINTF_BAUD_9600);
    tfp_printf("Assignment 3 Part 1: Blink the Red LED every half second for ten seconds!\n");

    hfclk_xtal_init_blocking();
    lfclk_init(LFCLK_SRC_Xtal);

    ms_timer_init(APP_IRQ_PRIORITY_HIGH);

    //set up timer to blink led every half second = 500ms
    ms_timer_start(MS_TIMER0, MS_REPEATED_CALL, RTC_TICKS_MS(500), led_blink);

    //set up timer to stop all timers after 10s
    ms_timer_start(MS_TIMER1, MS_SINGLE_CALL, RTC_TICKS_MS(10000), stopper);
    while (true)
    {
        __WFI();

    }
}

/** @} */
/** @} */

Output from serial console for this code

[17-06-27 10:31:33 447.672742] �Assignment 3 Part 1: Blink the Red LED every half second for ten seconds!
[17-06-27 10:31:34 0.473666] LED RED blinks
[17-06-27 10:31:34 0.472145] LED RED blinks
[17-06-27 10:31:35 0.472291] LED RED blinks
[17-06-27 10:31:35 0.472332] LED RED blinks
[17-06-27 10:31:36 0.472214] LED RED blinks
[17-06-27 10:31:36 0.472161] LED RED blinks
[17-06-27 10:31:37 0.472214] LED RED blinks
[17-06-27 10:31:37 0.472207] LED RED blinks
[17-06-27 10:31:38 0.472197] LED RED blinks
[17-06-27 10:31:38 0.472262] LED RED blinks
[17-06-27 10:31:39 0.472232] LED RED blinks
[17-06-27 10:31:39 0.472169] LED RED blinks
[17-06-27 10:31:40 0.472186] LED RED blinks
[17-06-27 10:31:40 0.472117] LED RED blinks
[17-06-27 10:31:40 0.472403] LED RED blinks
[17-06-27 10:31:41 0.472174] LED RED blinks
[17-06-27 10:31:41 0.472240] LED RED blinks
[17-06-27 10:31:42 0.472156] LED RED blinks
[17-06-27 10:31:42 0.472240] LED RED blinks
[17-06-27 10:31:43 0.472236] LED RED blinks
[17-06-27 10:31:43 0.009908] Stop blinking

Q2:

/*  Copyright (c) 2016, Appiko
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 *  3. Neither the name of the copyright holder nor the names of its contributors
 *  may be used to endorse or promote products derived from this software without
 *  specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @addtogroup group_appln
 * @{
 *
 * @defgroup bluey_demo A application to demo the Bluey's capabilities
 * @brief A Bluey demo
 * @{
 */

#include <stdbool.h>
#include <stdint.h>

#include "nrf.h"

#include "boards.h"
#include "hal_clocks.h"
#include "hal_nop_delay.h"
#include "hal_gpio.h"
#include "common_util.h"
#include "tinyprintf.h"
#include "uart_printf.h"
#include "ms_timer.h"
#include "nrf_util.h"
#include "hal_uart.h"
#include "string.h"

char on_str[]="glow";
char off_str[]="stop";

/** @brief Configure the RED LED pins as output and turn off LED */
static void led_init(void)
{
    hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
}

/** @brief turn off Red LED */
static void led_red_off(void)
{
    hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
}


/** @brief turn on red LED */
static void led_red_on(void)
{
    hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
}

void stop_timers() {
    tfp_printf("Stop blinking\n");
    ms_timer_stop(MS_TIMER0);
    led_red_off();
}

bool is_word(uint8_t*ptr, const char *str) {
  tfp_printf("Checking the input word \"%s\" against \"%s\"\n", ptr, str);
  if (strcmp(ptr, str)==0) {
    tfp_printf("It is %s\n", str);
    return true;
  }
  tfp_printf("It is not %s\n", str);
  return false;
}

void uart_rx(uint8_t *ptr)
{
  tfp_printf("Serial port Cmd = %s\n", ptr);
  if (is_word(ptr, on_str)) {
    tfp_printf("Turning LED on\n");
    led_red_on();
    return;
  }
  tfp_printf("Word is not %s\n", on_str);
  if (is_word(ptr, off_str)) {
    tfp_printf("Turning LED off\n");
    led_red_off();
    return;
  }
  tfp_printf("Word is neither, leave things be\n");
}

/**
*/
 /* @brief Function for application main entry.*/
int main(void)
{
    void (*hdlr)(uint8_t *);

    //set up uart handler
    hdlr = uart_rx;

    led_init();

    /* Initial printf */
    hal_uart_init(HAL_UART_BAUD_9600, hdlr);
    tfp_printf("Assignment 3 Part 2 : Glow based on serial port cmd!\n");

    hfclk_xtal_init_blocking();
    lfclk_init(LFCLK_SRC_Xtal);

    while (true)
    {
        __WFI();

    }
}

/** @} */
/** @} */

Output from serial console for Q2.

Assignment 3 Part 2 : Glow based on serial port cmd!
Serial port Cmd = glow
Checking the input word "glow" against "glow"
It is glow
Turning LED on
Serial port Cmd = stop
Checking the input word "stop" against "glow"
It is not glow
Word is not glow
Checking the input word "stop" against "stop"
It is stop
Turning LED off
Serial port Cmd = dfg
Checking the input word "dfg" against "glow"
It is not glow
Word is not glow
Checking the input word "dfg" against "stop"
It is not stop
Word is neither, leave things be
Serial port Cmd = gloe
Checking the input word "gloe" against "glow"
It is not glow
Word is not glow
Checking the input word "gloe" against "stop"
It is not stop
Word is neither, leave things be

#3

This is amazing Priya! I like the fact that you’ve abstracted the LED on and off, this increases code readability. A big thumbs up for the initial print stating the purpose of the program. And the fact that you’ve documented most of the functions. I’m assuming the lack of indentation in main() is the forum’s editor’s fault. The only nitpicking that I can do is the use of char instead of uint8_t for on_str and off_str. :slight_smile:

This part is regarding your question of optimized implementations for the platform. The standard library (stdlib) of GCC offers printf. And it also has a size optimized small size version - newlib nano. Still, the printf offered by newlib-nano was quite bulky compared to other implementations. This is the reason for using the an optimized version of printf found here. When it comes to string and memory operations I haven’t looked into alternative implementations. I’ve measured the time for some memcpy operations and it was quite fast with stdlib and didn’t increase the space much. So I’m happy with these.


#4

Hey guys,

Here are the slides of Session 3.
https://drive.google.com/file/d/0B2hYc2N1G-0AcnI5LVBFNXZjb1k/view?usp=sharing (I was unable to attach the file here (it allows only image files), hence the drive link)

If you have any questions or comments, do shoot a mail.
And gear up for the next class! =)

Cheers,
Nirzaree