Files
kunlun/dtest/bee_gpio_test/gpio_test.c
2024-09-28 14:24:04 +08:00

350 lines
6.8 KiB
C
Executable File

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_utils.h"
/* common includes */
#include "iot_io.h"
#include "iot_bitops.h"
#include "iot_pkt.h"
#include "iot_ipc.h"
#include "iot_dbglog_api.h"
#include "iot_config.h"
/* driver includes */
#include "iot_clock.h"
#include "iot_uart.h"
#include "iot_led.h"
/* cli includes */
#include "iot_cli.h"
#include "iot_uart_h.h"
/* debug includes*/
#include "dbg_io.h"
#include "iot_gpio_api.h"
#include "hw_reg_api.h"
#include "gpio_mtx_reg.h"
#include "pin_rf.h"
#include "iot_share_task.h"
#define BIT_INT_CLR 0
#define BIT_INT_ENA 1
#define BIT_INT_ST 2
#define BIT_INT_RAW 3
#define BIT_INT_TYP 4
#define BIT_WUP_ENA 7
#define BIT_IN_DATA 8
#define BIT_IN_ENA 9
#define BIT_OUT_ENA 10
#define BIT_OUT_DATA 11
#define BIT_OD_MODE 13
#define TEST_GPIO_INPUT 0
#define TEST_GPIO_OUTPUT 1
#define TEST_GPIO_INT 2
char test_case = TEST_GPIO_INT;
extern int platform_init();
os_task_h test_init_handle;
static void gpio_func_set(uint8_t gpio_no, uint8_t func)
{
uint32_t tmp;
uint32_t addr;
if(gpio_no < 52){
addr = CFG_GPIO00_PIN_CFG_ADDR + (gpio_no << 2);
}
else
{
return ;
}
tmp = PIN_RF_READ_REG(addr);
/* Set function */
tmp &= GPIO00_FUNC_SEL_MASK;
tmp |= (func<<GPIO00_FUNC_SEL_OFFSET)&GPIO00_FUNC_SEL_MASK;
/* Set pull-up */
tmp &= ~(GPIO00_FUNC_WPD_MASK);
tmp |= (GPIO00_FUNC_WPU_MASK);
PIN_RF_WRITE_REG(addr, tmp);
return ;
}
void test_task_output(){
int v=0, r;
iot_printf("test_task_output....\n");
#define TEST_OUTPUT 0
#define TEST_INPUT 1
r = iot_gpio_open_as_output(TEST_OUTPUT);
r |= iot_gpio_open_as_output(TEST_INPUT);
if(r != 0)
{
iot_printf("\ngpio_set_direction failed!\n");
}
for(;;) {
if(v)
{
iot_printf("\nSet low:");
v = 0;
}
else
{
iot_printf("\nSet High:");
v = 1;
}
if(0 != iot_gpio_value_set(TEST_OUTPUT, v))
{
iot_printf("WRITE FAILED ");
}
os_delay(1000);
r = iot_gpio_value_get(TEST_INPUT);
if(r)
{
iot_printf("Get High\n");
}
else
{
iot_printf("Get Low:\n");
}
}
}
void test_task_input(){
int i, r=0;
iot_printf("test_task_input....\n");
gpio_func_set(0, 0);
gpio_func_set(1, 0);
gpio_func_set(2, 0);
r |= iot_gpio_open_as_input(0);
r |= iot_gpio_open_as_input(1);
r |= iot_gpio_open_as_input(2);
gpio_func_set(3, 0);
gpio_func_set(4, 0);
r |= iot_gpio_open_as_output(3);
r |= iot_gpio_open_as_output(4);
r |= iot_gpio_value_set(3, 1);
r |= iot_gpio_value_set(4, 0);
// r |= gpio_set_direction(CFG_GPIO19_CFG_ADDR, DIR_INPUT);
if(r != 0)
{
iot_printf("\ngpio_set_direction failed!\n");
}
for(;;)
{
iot_printf("GPIO input status:\n");
for(i=0; i<3; i++)
{
if(iot_gpio_value_get(i)==1)
{
iot_printf("GPIO%02d input: High Levle\n", i);
}
else
{
iot_printf("GPIO%02d input: Low Levle\n", i);
}
os_delay(1000);
}
}
}
void test_task_int_isr(void)
{
iot_printf("GPIO interrupt triggered");
}
void gpio_task_hander(int pin)
{
iot_printf("GPIO%02d has been triggered!\n", pin);
os_delay(500);
iot_gpio_interrupt_enable(pin, 1);
}
void test_task_intterrupt(){
int r=0;
gpio_func_set(0, 0);
gpio_func_set(1, 0);
iot_share_task_init();
r |= iot_gpio_open_as_interrupt(0);
r |= iot_gpio_open_as_interrupt(1);
r |= iot_gpio_interrupt_config
(0, GPIO_INT_EDGE_FALLING, (iot_gpio_isr_func)gpio_task_hander, 0,
GPIO_INT_FUNC_ENABLE_AUTOSTOP);
r |= iot_gpio_interrupt_config
(1, GPIO_INT_LEVEL_LOW, (iot_gpio_isr_func)gpio_task_hander, 1,
GPIO_INT_FUNC_ENABLE_AUTOSTOP);
r |= iot_gpio_interrupt_enable(0, 1);
r |= iot_gpio_interrupt_enable(1, 1);
if(r != 0)
{
iot_printf("\ngpio_set_interrupt failed!\n");
}
else
{
iot_printf("\ngpio_set_interrupt successfully!\n");
}
while(1)
{
;
}
}
void test_task_init()
{
os_task_h handle;
if(test_case == TEST_GPIO_OUTPUT){
handle = os_create_task(test_task_output, NULL, 6);
}
else
{
if(test_case == TEST_GPIO_INPUT)
{
handle = os_create_task(test_task_input, NULL, 6);
}
else
{
handle = os_create_task(test_task_intterrupt, NULL, 6);
}
}
//create the tasks;
if(handle != NULL) {
iot_printf("task create successfully...\n");
}
}
void test_init()
{
/* init common modules */
iot_bitops_init();
/* init os related modules and utilities */
os_utils_init();
/* init dbglog module */
iot_dbglog_init();
/* init ipc module */
iot_ipc_init();
/*init uart module*/
iot_uart_init(1);
test_task_init();
}
void init_task(void *arg)
{
iot_printf("task 1 entry....\n");
for(;;) {
test_init();
os_delete_task(test_init_handle);
}
}
int32_t iot_task_init()
{
/* start plc lib task */
test_init_handle = os_create_task(init_task, NULL, 9);
//create the tasks;
if(test_init_handle != NULL) {
iot_printf("task 1 init successfully...\n");
}
return 0;
}
int32_t iot_task_start()
{
//start the tasks;
os_start_kernel();
return 0;
}
static int32_t iot_platform_init()
{
/*platform intialization*/
platform_init();
//resource initializations;
system_clock_init();
system_uart_init();
dbg_uart_init();
iot_led_init();
return 0;
}
int32_t iot_module_init(void)
{
//platform intialization;
iot_platform_init();
//create all the tasks;
iot_task_init();
iot_printf("starting...\n");
return 0;
}
int main(void)
{
//module init;
iot_module_init();
//module start;
iot_task_start();
return 0;
}