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 (button 0 on nRF54 Series DKs) 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 nRF Connect SDK v2.8.0-preview1-11645184a54d ***
*** Using Zephyr OS v3.7.99-adcffa835a8e ***
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
_______________________________________________________
Terminal1. In the GitHub repository for this course, use the base code for this exercise, found in l4/l4_e1
of whichever version directory you are using.
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.
5. Open the main.c
file inside l4_e1/src
.
6. Under STEP 6, include the header file of the printk()
function by adding the following line:
#include <zephyr/sys/printk.h>
CThe 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");
CBuild 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 nRF Connect SDK v2.8.0-preview1-11645184a54d ***
*** Using Zephyr OS v3.7.99-adcffa835a8e ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Terminal8. 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
CSince 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", 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", i, factorial);
}
printk("_______________________________________________________\n");
/*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.
*/
}
CCopy 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",MAX_NUMBER_FACT);
CThe 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",i,factorial);
CThe third and last call to printk()
is to print a simple string that contains one long underline.
printk("_______________________________________________________\n");
C9. 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 (button 0 on nRF54 Series DKs) 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 nRF Connect SDK v2.8.0-preview1-11645184a54d ***
*** Using Zephyr OS v3.7.99-adcffa835a8e ***
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
_______________________________________________________
TerminalThe solution for this exercise can be found in the GitHub repository, l4/l4_e1_sol
of whichever version directory you are using.
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 nRF Connect SDK 2.6.1-3758bcbfa5cd ***
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
_______________________________________________________
Terminal1. In the GitHub repository for this course, use the base code for this exercise, in l4/l4_e1
of whichever version directory you are using.
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.
5. Open the main.c
file inside l4_e1/src
.
6. Under STEP 6, include the header file of the printk()
function by adding the following line:
#include <zephyr/sys/printk.h>
CThe 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");
CBuild 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 nRF Connect SDK 2.6.1-3758bcbfa5cd ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Terminal8. 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
CSince 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", 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", i, factorial);
}
printk("_______________________________________________________\n");
/*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.
*/
}
CCopy 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",MAX_NUMBER_FACT);
CThe 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",i,factorial);
CThe third and last call to printk()
is to print a simple string that contains one long underline.
printk("_______________________________________________________\n");
C9. 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 nRF Connect SDK 2.6.1-3758bcbfa5cd ***
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
_______________________________________________________
TerminalThe solution for this exercise can be found in the GitHub repository, l4/l4_e1_sol
of whichever version directory you are using.
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 v1.9.1-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
_______________________________________________________
1. In the GitHub repository for this course, use the base code for this exercise, in l4/l4_e1
of whichever version directory you are using.
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.
5. Open the main.c
file inside l4_e1/src
.
6. Under STEP 6, include the header file of the printk()
function by adding the following line:
#include <sys/printk.h>
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 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, l4/l4_e1_sol
of whichever version directory you are using.