 Feedback
If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files. # 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 base this exercise on Lesson 2-Exercise 2 and modify it 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.

``````*** Booting Zephyr OS build v3.0.99-ncs1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800

_______________________________________________________
``````

### 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 Open 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 <zephyr/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, which can be found in <sdk_folder>/<sdk_version>/zephyr/boards/arm/<your_board>/*_defconfig. 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:

``````*** Booting Zephyr OS build v3.0.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1``````

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:

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. 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:

``````*** Booting Zephyr OS build v3.0.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800
_______________________________________________________
``````

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.

``````*** Booting Zephyr OS build v2.7.0-ncs1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800

_______________________________________________________
``````

### 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.

``````*** Booting Zephyr OS build v2.6.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1``````

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:

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:

``````*** Booting Zephyr OS build v2.6.99-ncs1-1  ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of  1 = 1
The factorial of  2 = 2
The factorial of  3 = 6
The factorial of  4 = 24
The factorial of  5 = 120
The factorial of  6 = 720
The factorial of  7 = 5040
The factorial of  8 = 40320
The factorial of  9 = 362880
The factorial of 10 = 3628800
_______________________________________________________
``````

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