Hello All,
This document explains how to add GPIO pins to the default BSP configurations. This example shows how add a LED pin for the BSP TWR-K70FN1M board. The procedure is the same for other ports and peripherals, using Kinetis devices.
MODIFY THE TWRK70F120M.h FILE
The TWR-K70F120M Tower Module contains two pushbutton switches connected to GPIO/interrupt signals, one pushbutton connected to the master reset signal, four capacitive touch pad electrodes, four user controllable LEDs, and a potentiometer connected to an ADC input signal. The following table provides details on which K70FN1M0 pins are used to communicate with the LEDs and switches onboard the TWR-K70F120M.
Feature | Connection | Port Pin | Pin Function |
Pushbuttons | SW1 (IRQ0) | PTD0 | PTD0 |
SW2 (IRQ1) | PTE26 | PTE26 | |
SW3 (RESET) | RESET_b | RESET_b | |
LEDs | E1 / Orange LED | PTA11 | PTA11 |
E2 / Yellow LED | PTA28 | PTA28 | |
E3 / Green LED | PTA29 | PTA29 | |
E4 / Blue LED | PTA10 | PTA10 | |
Touch Pads | E1 / Touch | PTA4 | TSI0_CH5 |
E2 / Touch | PTB3 | TSI0_CH8 | |
E3 / Touch | PTB2 | TSI0_CH7 | |
E4 / Touch | PTB16 | TSI0_CH9 |
Table 1. I/O Connectors and Pin Usage Table
NOTE: Some port pins are used in multiple interfaces on-board and many are potentially connected to off-board resources via the Primary and Secondary Connectors. Take care to avoid attempted simultaneous usage of mutually exclusive features. For more information please consult the “TWR-K70F120M Tower Module User's Manual”.
The <board>.h file is used to provide information needed by an application program using the kernel running on the Freescale Evaluation board. This file contains the GPIO board specifications. It is located at the next path:
<Freescale_MQX_4_x_install_path>\mqx\source\bsp\<name_board>
Figure 1. GPIO board specifications for the TWR-K70F120M.
The twrk70f120m.h file contains the GPIO board definition for the TWR-K70F120M, however if customer creates his own board and he needs to connect LEDs in deferent pins to the TWR-K70F120M, it is necessary to modify this file.
Suppose a custom_board where is connected a LED to PTE18 pin, in order to add this pin definition it is necessary to follow the below steps, the pin will be called BSP_LEDCustom.
1. Define the pin name and assign the port and pin number.
#define BSP_LEDCustom (GPIO_PORT_E | GPIO_PIN18)
2. Assigning the requested functionality to the pin for the GPIO mode or any other peripheral mode.
#define BSP_LEDCustom_MUX_GPIO (LWGPIO_MUX_E18_GPIO)
3. Save and close the twrk70f120m.h file.
USE THE NEW LED IN A PROJECT
The following example shows one task named LEDCustom_TASK, where the LEDCustom is used.
1. Open a new MQX project.
2. In the main.h module replace the following lines:
#define MAIN_TASK 1
extern void Main_task(uint_32);
With the next lines:
#define LEDCustom_TASK 5
extern void ledCustom_task(uint_32);
3. In main.c, modify the MQX_template_list entry for the Main_Task. Replace the following line: {MAIN_TASK, Main_task, 1500, 9, "main", MQX_AUTO_START_TASK},
With the next line:
{ LEDCustom_TASK, ledCustom_task, 1500, 9, "init", MQX_AUTO_START_TASK, 0, 0},
NOTE
The last entry in the MQX_template_list must be all zeros.
4. In main.c, comment out the Main_task.
/*void Main_task(uint_32 initial_data)
{
print(“/n Hello World /n”);
_mqx_exit(0);
}
*/
5. It is necessary to write the tasks code. This task uses LWGPIO driver in order to toggle a pin.
The lwgpio_init() function has to be called prior to calling any other API function of the LWGPIO driver. This function initializes the LWGPIO_STRUCT structure. The pointer to the LWGPIO_STRUCT is passed as a handle parameter. To identify the pin, platform-specific LWGPIO_PIN_ID number is used. It is necessary to have a variable to assign the pointer to the LWGPIO_STRUCT, for do that it is required to add the following line:
LWGPIO_STRUCT ledC;
The following lines initialize lwgpio handle for LWGPIO_MUX_E18_GPIO pin. The BSP already knows the address and the pin number for each GPIO PORT and each PIN. This is defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file. That is the reason the twrk70f120m.h file was modified.
if (!lwgpio_init(&ledC, BSP_LEDCustom, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
{
printf("Initializing Port TE pin18 GPIO of the K70 as output failed.\n");
_task_block();
}
6. Once it is initialized, it is necessary to assign the requested functionality to the pin like GPIO mode or any other peripheral mode. The lwgpio_set_functionality() function sets the functionality of the pin. The value of the functionality parameter represents the number stored in the multiplexer register field which selects the desired functionality. For the GPIO mode, you can use the pre-defined macros which can be found in the lwgpio_ <mcu>.h file. Add the following lines switch pin functionality to GPIO mode:
lwgpio_set_functionality(&ledC, BSP_LEDCustom_MUX_GPIO);
The lwgpio_set_value() function sets the pin state (low or high) of the specified pin. In order to toggle the pin value write the following code:
while (TRUE)
{
_time_delay(1000);
lwgpio_set_value(&ledC, value); /* toggle pin value */
value = value^1;
}
7. Create the other three functions. The name of each function, delay, and the first parameter must be different in LWGPIO functions use (lwgpio_init(), lwgpio_set_functionality(), lwgpio_set_value()).
8. At this point it is possible to build the project, flash it to the board and run the project; for more details about building, flashing and running please refer to the compiler documentation of your choice, that is located at the next path: C:\Freescale\Freescale_MQX_4_x\doc\tools\cw
Thanks!!!!
a. main.c
/****************************************************************************
*
* This file contains MQX only stationery code.
*
****************************************************************************/
#include "main.h"
#if !BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
/* Task number, Entry point, Stack, Pri, String, Auto? */
{LEDCUSTOM_TASK, ledCustom_task, 1500, 9, "init", MQX_AUTO_START_TASK, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
/*TASK*-----------------------------------------------------
*
* Task Name : Main_task
* Comments :
* This task prints " Hello World "
*
*END*-----------------------------------------------------*/
void ledCustom_task(uint_32 initial_data)
{
int value = 0;
LWGPIO_STRUCT ledC;
printf("\n LedCustom task \n");
/* initialize lwgpio handle for LWGPIO_MUX_TC0_GPIO pin (defined in mqx/source/bsp/<bsp_name>/<bsp_name>.h file) */
if (!lwgpio_init(&ledC, BSP_LEDCustom, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE))
{
printf("Initializing Port TE pin18 GPIO of the K70 as output failed.\n");
_task_block();
}
/* swich pin functionality to GPIO mode */
lwgpio_set_functionality(&ledC, BSP_LEDCustom_MUX_GPIO);
while (TRUE)
{
_time_delay(5000);
lwgpio_set_value(&ledC, value); /* toggle pin value */
value = value^1;
}
}
/* EOF */
b. main.h
#ifndef __main_h_
#define __main_h_
#include <mqx.h>
#include <bsp.h>
#define LEDCUSTOM_TASK 5
extern void ledCustom_task(uint_32);
/* PPP device must be set manually and
** must be different from the default IO channel (BSP_DEFAULT_IO_CHANNEL)
*/
#define PPP_DEVICE "ittyb:"
/*
** Define PPP_DEVICE_DUN only when using PPP to communicate
** to Win9x Dial-Up Networking over a null-modem
** This is ignored if PPP_DEVICE is not #define'd
*/
#define PPP_DEVICE_DUN 1
/*
** Define the local and remote IP addresses for the PPP link
** These are ignored if PPP_DEVICE is not #define'd
*/
#define PPP_LOCADDR IPADDR(192,168,0,216)
#define PPP_PEERADDR IPADDR(192,168,0,217)
/*
** Define a default gateway
*/
#define GATE_ADDR IPADDR(192,168,0,1)
#endif /* __main_h_ */