v1
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
/*!
|
||||
* \file gpio.c
|
||||
*
|
||||
* \brief Target gpio implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "gpio.h"
|
||||
#include "xc6xxx_hal_gpio.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define LED1_PIN GPIO_25
|
||||
#define LED2_PIN GPIO_24
|
||||
#define KEY1_PIN GPIO_6
|
||||
#define KEY2_PIN GPIO_8
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t gpio_intr_flag = false;
|
||||
uint32_t intr_val = 0;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief gpio_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void gpio_demo(void) {
|
||||
DEBUG("__GPIO_DEMO__\r");
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
// Board Led1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pin = LED1_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Led2 Initialization
|
||||
GPIO_InitCfg.Pin = LED2_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Key1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = FAIL_EDGE_INT;
|
||||
GPIO_InitCfg.Pin = KEY1_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Key2 Initialization
|
||||
GPIO_InitCfg.Pin = KEY2_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
|
||||
GPIO_Output_High(LED1_PIN);
|
||||
GPIO_Output_High(LED2_PIN);
|
||||
|
||||
while (1) {
|
||||
if (gpio_intr_flag == true) {
|
||||
DEBUG("Intr Status Val: 0x%08x\n", intr_val);
|
||||
gpio_intr_flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gpio_Intr_Callback
|
||||
* @details Gpio interrupt callback function
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Gpio_Intr_Callback(uint32_t intr_sta) {
|
||||
intr_val = intr_sta;
|
||||
DEBUG("intr_sta: %08x\n", intr_sta);
|
||||
// Determine the interrupt of Key1
|
||||
if ((intr_sta & (1 << KEY1_PIN)) != 0) {
|
||||
GPIO_Output_Toggle(LED1_PIN);
|
||||
gpio_intr_flag = true;
|
||||
}
|
||||
// Determine the interrupt of Key=2
|
||||
if ((intr_sta & (1 << KEY2_PIN)) != 0) {
|
||||
GPIO_Output_Toggle(LED2_PIN);
|
||||
gpio_intr_flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pulldown_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pullup_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_nopull_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pulldown_input_inter_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = RIS_EDGE_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
}
|
||||
|
||||
void gpio_pullup_input_inter_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = FAIL_EDGE_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
}
|
||||
|
||||
void gpio_output_test(void) {
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
GPIO_Output_Low(i);
|
||||
}
|
||||
Delay_Ms(100);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_Output_High(i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file gpio.h
|
||||
*
|
||||
* \brief The head file of gpio.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __GPIO_H__
|
||||
#define __GPIO_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void gpio_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __GPIO_H__ */
|
||||
Reference in New Issue
Block a user