NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (I)

Publisher:SereneHarmonyLatest update time:2021-04-06 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The KEA128 chip has 7 groups of IO, PORTA--H has 8 pins in each group, and PORTI has 7 pins, totaling 71 pins.


The driving capacity of each pin is 2.5mA, and all pins can be pulled up to VDD internally, without internal pull-down. Unused pins should be pulled up internally through programming.


When the MCU is in the running, waiting, or debugging mode, the GPIO works normally. In the stop mode, the GPIO stops working.


The following is an introduction to the port control registers, mainly the port filter register, the pull-up enable register and the high drive capability enable register

GPIO has three groups of registers, namely GPIOA, GPIOB, and GPIOC. Each group has 7 registers, namely output register (PDOR), output set register (PSOR), output clear register (PCOR), output reverse register (PTOR), input register (PDIR), data direction register (PDDR), and input disable register (PIDR).


GPIO programming steps:


1. Set GPIO as input or output and set the data direction register


2. If it is output, set the value of GPIO, low 0 high 1


3. If it is input, get the pin status through the input register. Low 0 High 1.


//===========================================================================

//File name: common.h

//Functional overview: Common element header file

//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)

//Version update: 2015-06-05 V2.0

//Chip type: KEA128

//===========================================================================

 

#ifndef __COMMON_H //Prevent duplicate definition (starting with _COMMON_H)

#define __COMMON_H

 

// 1. Chip register mapping file and processor core attribute file

#include "core_cmFunc.h"

#include "core_cmInstr.h"

#include "core_cm0plus.h"

#include "SKEAZ1284.h" // Include chip header file

#include "system_SKEAZ1284.h" // Include chip system initialization file

 

#define SYSTEM_CLK_KHZ DEFAULT_SYSTEM_CLOCK/1000 // Chip system clock frequency (KHz)

#define CORE_CLK_KHZ SYSTEM_CLK_KHZ //Chip core clock frequency (KHz)

#define BUS_CLK_KHZ SYSTEM_CLK_KHZ/2 //Chip bus clock frequency (KHz)

 

// 2. Define the switch interrupt

#define ENABLE_INTERRUPTS __enable_irq // Enable general interrupt

#define DISABLE_INTERRUPTS __disable_irq // Disable total interrupt

 

// 3. Bit operation macro function (setting, clearing, obtaining the status of a register bit)

#define BSET(bit,Register) ((Register)|= (1<<(bit))) // Set a bit in the register

#define BCLR(bit,Register) ((Register) &= ~(1<<(bit))) // Clear one bit of the register

#define BGET(bit,Register) (((Register) >> (bit)) & 1) // Get the status of a bit in the register

 

// 4. Redefine basic data types (type alias macro definition)

typedef unsigned char uint_8; // unsigned 8-bit number, byte

typedef unsigned short int uint_16; // unsigned 16-bit number, word

typedef unsigned long int uint_32; // unsigned 32-bit number, long word

typedef char int_8; // Signed 8-bit number

typedef short int int_16; // signed 16-bit number

typedef int int_32; // signed 32-bit number

// Do not optimize type

typedef volatile uint_8 vuint_8; // Do not optimize unsigned 8-bit numbers, bytes

typedef volatile uint_16 vuint_16; // Do not optimize unsigned 16-bit numbers, word

typedef volatile uint_32 vuint_32; // Do not optimize unsigned 32-bit numbers, long words

typedef volatile int_8 vint_8; // Do not optimize signed 8-bit numbers

typedef volatile int_16 vint_16; // Do not optimize signed 16-bit numbers

typedef volatile int_32 vint_32; // Do not optimize signed 32-bit numbers

 

#endif //Prevent duplicate definition (ending with _COMMON_H)

//===========================================================================

//File name: gpio.h

//Functional summary: GPIO underlying driver component header file

//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)

//Version update: 2015-06-05 V2.0

//Chip type: KEA128

//===========================================================================

 

#ifndef GPIO_H //Prevent duplicate definition (starting with GPIO_H)

#define GPIO_H

 

#include "common.h" //Includes common element header files

 

//Port number address offset macro definition

#define PORTA    (0<<8)

#define PORTB    (1<<8)

#define PORTC    (2<<8)

#define PORTD    (3<<8)

#define PORTE    (4<<8)

#define PORTF    (5<<8)

#define PORTG    (6<<8)

#define PORT (7<<8)

#define PORTS (8<<8)

//pin direction macro definition

#define GPIO_IN      0

#define GPIO_OUTPUT  1

 

//===========================================================================

//Function name: gpio_init

//Function returns: None

//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)

// dir: pin direction (0 = input, 1 = output, can be defined by pin direction macro)

// state: initial state of the port pin (0 = low level, 1 = high level)

//Functional summary: Initialize the specified port pin as a GPIO pin function and define it as input or output. If it is output,

// Also specify whether the initial state is low or high

//===========================================================================

void gpio_init(uint_16 port_pin, uint_8 dir, uint_8 state);

 

//===========================================================================

//Function name: gpio_set

//Function returns: None

//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)

// state: the port pin state you want to set (0 = low level, 1 = high level)

//Function Summary: When the specified port pin is defined as GPIO function and is output, this function sets the pin status

//===========================================================================

void gpio_set(uint_16 port_pin, uint_8 state);

 

//===========================================================================

//Function name: gpio_get

//Function returns: the state of the specified port pin (1 or 0)

//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)

//Function summary: When the specified port pin is defined as GPIO function and is input, this function obtains the specified pin status

//===========================================================================

uint_8 gpio_get(uint_16 port_pin);

 

//===========================================================================

//Function name: gpio_reverse

//Function returns: None

//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)

//Function Summary: When the specified port pin is defined as GPIO function and is output, this function inverts the pin state

//===========================================================================

void gpio_reverse(uint_16 port_pin);

 

//===========================================================================

//Function name: gpio_pull

//Function returns: None

//Parameter description: port_pin: port number | pin number (e.g.: PORTB|(5) means pin 5 of port B)

// pullselect: pin pull-up enable selection (0 = pull-up disabled, 1 = pull-up enabled)

//Function summary: Pull the specified pin high

//===========================================================================

void gpio_pull(uint_16 port_pin, uint_8 pullselect);

 

#endif //Prevent duplicate definition (end of GPIO_H)

//===========================================================================

//statement:

//(1)The source code we developed has passed the test of the hardware system provided by our center. We sincerely contribute it to the society. If there are any shortcomings, please feel free to point them out.

//(2) For users who use hardware systems other than those of our center, please carefully match the system according to your own hardware when porting the code.

//

//Freescale Embedded Center, Suzhou University

//Technical consultation: 0512-65214835 http://sumcu.suda.edu.cn

//===========================================================================

//File name: gpio.c

//Functional summary: GPIO underlying driver component source file

//Copyright: Freescale Embedded Center, Soochow University (sumcu.suda.edu.cn)

//Version update: 2015-06-05 V2.0

//Chip type: KEA128

//===========================================================================

#include "gpio.h" //Include this component header file

 

uint_32 bit; //Internal variable used to record the offset of the pin in the port register

//Internal function declaration

//Parse the port number and pin

static void gpio_port_pin_num(uint_16 port_pin,uint_8* port,uint_8* pin);

// Parse the base address and the pin's offset in the register

static void gpio_ptr_bit(uint_16 port_pin,GPIO_MemMapPtr* gpio_ptr,uint_32* bit);

//===========================================================================

//Function name: gpio_init

//Function returns: None

//Parameter description: port_pin: (port number)|(pin number) (e.g.: PORTB|(5) means pin 5 of port B)

// dir: pin direction (0 = input, 1 = output, can be defined by pin direction macro)

// state: initial state of the port pin (0 = low level, 1 = high level)

//Functional summary: Initialize the specified port pin as a GPIO pin function and define it as input or output. If it is output,

// Also specify whether the initial state is low or high

//===========================================================================

void gpio_init(uint_16 port_pin, uint_8 dir, uint_8 state)

{

//Local variable declaration

GPIO_MemMapPtr gpio_ptr; //Declare port_ptr as a GPIO structure type pointer

gpio_ptr_bit(port_pin,&gpio_ptr,&bit); //Calculate the base address and the offset of the pin in the register

//Determine whether the pin is output or input based on the parameter dir

if (1 == dir) // hope for output

{

//Port data direction register is defined as output

BSET(bit, GPIO_PDDR_REG(gpio_ptr)); //1 is general output, 0 is input

// Output clear register

BSET(bit, GPIO_PCOR_REG(gpio_ptr)); //This register is set to 1, and the pin output is set to 0

//The initial state is low level

gpio_set(port_pin, state); //Call gpio_set function to set the initial state of the pin

else 

[1] [2]
Keywords:Freescale Reference address:NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (I)

Previous article:Freescale S12 series (based on MC9S12XET256MAA and/MC9S12XEP100) CAN data
Next article:NXP Freescale Kinetis KEA128 Study Notes 3--GPIO Module (Part 2)

Recommended ReadingLatest update time:2024-11-17 00:07

Freescale MC9S12G series MCU flash erase and write
I have been studying the low-level drivers of Freescale MC9S12G series microcontrollers recently. I have read a lot, but I still don’t have a deep understanding of them. I am at the entry-level level. I only record my learning experience here. I will strengthen my low-level driver development ability in practice later
[Microcontroller]
Freescale MC9S12G series MCU flash erase and write
Design of farmland irrigation control board based on Freescale Kinetis
0 Introduction At present, the problem of water resources in China is becoming increasingly prominent. The lack of water resources and unreasonable distribution are its objective reasons. To fundamentally alleviate this problem, we must start from saving water and improving the utilization rate of water resources. Ch
[Microcontroller]
Design of farmland irrigation control board based on Freescale Kinetis
Freescale 16-bit MCU (XI) - Digital tube dynamic scanning test
1. Introduction to digital tube scanning This experiment is an extension of the GPIO output function. It uses GPIO to drive the digital tube to perform dynamic scanning to achieve the purpose of displaying numbers. The hardware circuit of this experiment uses a 4-bit 8-segment common cathode digital tube. The so-call
[Microcontroller]
Freescale 16-bit MCU (XI) - Digital tube dynamic scanning test
Freescale Microcontroller and MSCAN ----Self-study Notes
Overview Freescale Semiconductor is one of the largest semiconductor companies in the world. Freescale is the number one supplier of automotive semiconductor microcontrollers (MCUs). Freescale offers the highest quality Flash memory in the industry. Freescale's microcontrollers are divided into 8-bit, 16-bit and 32-bi
[Microcontroller]
Freescale Xtrinsic smart sensor applications enable advanced automotive active safety
It is reported that Freescale Semiconductor is currently expanding its Xtrinsic smart sensor solution series by adding MMA6900Q and MMA6901Q accelerometers to achieve advanced automotive active safety applications. Cars have been equipped with passive safety systems, such as airbags and seat belt systems, to mi
[Automotive Electronics]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号