Exercise 1

v2.x.x

Printing to the console

In this exercise, we will practice using the user-friendly printk() function to print strings to the console.

We will modify Lesson 2 Exercise 2 so that when Button 1 is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below. We are using the UART console, which is the default set in the board configuration file.

Exercise steps

1. In the GitHub repository for this course, use the base code for this exercise, found in lesson4/fund_less4_exer1  of whichever version directory you are using (v2.x.x or v1.6.0-v1.9.1)

2. Use the Add an existing application option in nRF Connect for VS Code to open the base code for this exercise, as we have done in lesson 2 and lesson 3.

3. Make sure that your development kit is powered on and connected to your computer.

4. Configure a terminal emulator on your machine. This has already been covered in Lesson 3 Exercise 1 step 8.

Note

The nRF Connect for VS code extension pack comes with a built-in terminal emulator called nRF Terminal. You can use it instead of PuTTY by clicking on the Connect to serial port button next to the COM port in the Connected Devices View:

5. Open the main.c file inside fund_less4_exer1/src.

6. Under STEP 6, include the header file of the printk() function by adding the following line:

#include <sys/printk.h>

Note

The console driver (CONFIG_CONSOLE) and console type (e.g. CONFIG_UART_CONSOLE) configuration options are already enabled in the board configuration file. Hence, we do not have to do anything in the application configuration file (prj.conf).

7. Let’s print out our first message, a simple banner that says nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1. Search for STEP 7 and add the following line:

printk("nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1\n\r");

Build and flash the application to your board as we have done in previous lessons, and you should see this message printed on the console:

8. Now let’s replace the button callback function (ISR) with a code that generates numbers’ factorials:

8.1 Define the macro MAX_NUMBER_FACT that represents the maximum number to calculate its factorial and set its value to 10.

#define MAX_NUMBER_FACT 10

Since code in ISR runs at a high priority (higher than all system and user threads), it should be written with timing in mind. Tasks that are too lengthy or complex should not be performed by an ISR, they should be deferred to a thread. This will be covered in-depth in Lesson 7.

8.2 In main.c, replace the callback function button_pressed() with the following:

void button_pressed(const struct device *dev, struct gpio_callback *cb,
            uint32_t pins)
{
  int i;
  int j;
  long int factorial;
  printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);
  for (i=1;i<=MAX_NUMBER_FACT;i++){
       factorial =1;
        for (j=1;j<=i;j++){
            factorial = factorial*j;
        }
        printk("The factorial of %2d = %ld \n\r",i,factorial);
  }
  printk("_______________________________________________________\r");
  /*Important note!
  Code in ISR runs at a high priority, therefore, it should be written with timing in mind.
  Too lengthy or too complex tasks should not be performed by an ISR, they should be deferred to a thread. 
  */
}

Copy the code above and use it to replace the button_pressed() function in main.c as shown in the screenshot below:

Replace contents of button_pressed() function

In this code, we are using printk() in three different locations.

The first call is to print a formatted string that contains one integer %d.  

printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);

The second call is to print a formatted string containing an integer with a width specifier of %2d, and a long integer %ld. This is placed inside a loop, so it will be executed multiple times.

printk("The factorial of %2d = %ld \n\r",i,factorial);

The third and last call to printk() is to print a simple string that contains one long underline.

printk("_______________________________________________________\r");

9. Add a build configuration, build the exercise, and flash it to the board as we have done in the previous exercises.

Observe that when button 1 on the board is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below:

The solution for this exercise can be found in the GitHub repository, lesson4/fund_less4_exer1_solution of whichever version directory you are using (v2.x.x or v1.6.0-v1.9.1)

v1.6.0 – v1.9.1

Printing to the console

In this exercise, we will practice using the user-friendly printk() function to print strings to the console.

We will modify Lesson 2 Exercise 2 so that when Button 1 is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below. We are using the UART console, which is the default set in the board configuration file.

Exercise steps

1. In the GitHub repository for this course, use the base code for this exercise, found in lesson4/fund_less4_exer1  of whichever version directory you are using (v2.x.x or v1.6.0-v1.9.1)

2. Use the Add an existing application option in nRF Connect for VS Code to open the base code for this exercise, as we have done in lesson 2 and lesson 3.

3. Make sure that your development kit is powered on and connected to your computer.

4. Configure a terminal emulator on your machine. This has already been covered in Lesson 3 Exercise 1 step 8.

Note

The nRF Connect for VS code extension pack comes with a built-in terminal emulator called nRF Terminal. You can use it instead of PuTTY clicking on the Connect to serial port button next to the COM port in the Connected Devices View:

5. Open the main.c file inside fund_less4_exer1/src.

6. Under STEP 6, include the header file of the printk() function by adding the following line:

#include <sys/printk.h>

Note

The console driver (CONFIG_CONSOLE) and console type (e.g. CONFIG_UART_CONSOLE) configuration options are already enabled in the board configuration file. Hence, we do not have to do anything in the application configuration file (prj.conf).

7. Let’s print out our first message, a simple banner that says nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1. Search for STEP 7 and add the following line

printk("nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1\n\r");

Build and flash the application to your board as we have done in previous lessons, and you should see this message is printed on the console.

8. Now let’s replace the button callback function (ISR) with a code that generates numbers’ factorials:

8.1 Define the macro MAX_NUMBER_FACT that represents the maximum number to calculate its factorial and set its value to 10.

#define MAX_NUMBER_FACT 10

Since code in ISR runs at a high priority (higher than all system and user threads), it should be written with timing in mind. Tasks that are too lengthy or complex should not be performed by an ISR, they should be deferred to a thread. This will be covered in-depth in Lesson 7.

8.2 In main.c, replace the callback function button_pressed() with the following:

void button_pressed(const struct device *dev, struct gpio_callback *cb,
            uint32_t pins)
{
  int i;
  int j;
  long int factorial;
  printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);
  for (i=1;i<=MAX_NUMBER_FACT;i++){
       factorial =1;
        for (j=1;j<=i;j++){
            factorial = factorial*j;
        }
        printk("The factorial of %2d = %ld\n\r",i,factorial);
  }
  printk("_______________________________________________________\n\r");
  /*Important note!
  Code in ISR runs at a high priority, therefore, it should be written with timing in mind.
  Too lengthy or too complex tasks should not be performed by an ISR, they should be deferred to a thread. 
  */
}

Copy the code above and use it to replace the button_pressed() function in main.c as shown in the screenshot below:

Replace contents of button_pressed() function

In this code, we are using printk() in three different locations:

The first call is to print a formatted string that contains one integer %d.  

printk("Calculating the factorials of numbers from 1 to %d:\n\r",MAX_NUMBER_FACT);

The second call is to print a formatted string containing an integer with a width specifier of %2d, and a long integer %ld. This is placed inside a loop, so it will be executed multiple times.

printk("The factorial of %2d = %ld\n\r",i,factorial);

The third and last call to printk() is to print a simple string that contains one long underline.

printk("_______________________________________________________\n\r");

9. Build the exercise and flash it to the board.

Observe that when button 1 on the board is pressed, the factorials of the numbers from 1 to 10 are calculated and printed on the console as shown below:

The solution for this exercise can be found in the GitHub repository, lesson4/fund_less4_exer1_solution of whichever version directory you are using (v2.x.x or v1.6.0-v1.9.1)

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

Forgot your password?
Enter your email address, and we will send a link to reset your password.