http://blog.csdn.net/caogos/article/details/70240878
用龙芯1c库在RT-Thread下实现外部中断(GPIO中断、按键中断)http://blog.csdn.net/caogos/article/details/75648063
在RTT中gpio相关操作,通常叫xxx_pin_xxx。也就是用pin表示是gpio相关的函数。
int hw_pin_init(void)
hw_pin_init();
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
#define PIN_MODE_OUTPUT 0x00
#define PIN_MODE_INPUT 0x01
rt_base_t pin_led = 32;
rt_pin_mode(pin_led, PIN_MODE_OUTPUT);
把gpio32设为输出模式
void rt_pin_write(rt_base_t pin, rt_base_t value)
#define PIN_LOW 0x00
#define PIN_HIGH 0x01
rt_base_t pin_led = 32;
rt_pin_write(pin_led, PIN_LOW);
rt_pin_write(pin_led, PIN_HIGH);
int rt_pin_read(rt_base_t pin)
int status;
status = rt_pin_read(32);
点灯的源码很简单,这里就直接贴源码了
bsp\ls1cdev\applications\application.c
/*
* File : application.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2012, RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2010-06-25 Bernard first version
* 2011-08-08 lgnq modified for Loongson LS1B
* 2015-07-06 chinesebear modified for Loongson LS1C
*/
#include
#include "net/synopGMAC.h"
#include
#include
#include "../drivers/drv_gpio.h"
// 测试用的线程
#define THREAD_TEST_PRIORITY (25)
#define THREAD_TEST_STACK_SIZE (4*1024) // 4k
#define THREAD_TEST_TIMESLICE (10)
struct rt_thread thread_test;
ALIGN(8) rt_uint8_t thread_test_stack[THREAD_TEST_STACK_SIZE];
// 测试用的线程的入口
void thread_test_entry(void *parameter)
{
rt_base_t pin_led = 32;
// pin初始化
hw_pin_init();
// 把相应gpio设为输出模式
rt_pin_mode(pin_led, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(pin_led, PIN_LOW);
rt_thread_delay(1 * RT_TICK_PER_SECOND);
rt_pin_write(pin_led, PIN_HIGH);
rt_thread_delay(1 * RT_TICK_PER_SECOND);
}
}
void rt_init_thread_entry(void *parameter)
{
/* initialization RT-Thread Components */
rt_components_init();
// 网口EMAC初始化
rt_hw_eth_init();
}
int rt_application_init(void)
{
rt_thread_t tid;
rt_err_t result;
/* create initialization thread */
tid = rt_thread_create("init",
rt_init_thread_entry, RT_NULL,
4096, RT_THREAD_PRIORITY_MAX/3, 20);
if (tid != RT_NULL)
rt_thread_startup(tid);
// 初始化测试用的线程
result = rt_thread_init(&thread_test,
"thread_test",
thread_test_entry,
RT_NULL,
&thread_test_stack[0],
sizeof(thread_test_stack),
THREAD_TEST_PRIORITY,
THREAD_TEST_TIMESLICE);
if (RT_EOK == result)
{
rt_thread_startup(&thread_test);
}
else
{
return -1;
}
return 0;
}
移植时,只需要实现设置模式,输出,和读取状态三个接口,即
const static struct rt_pin_ops _ls1c_pin_ops =
{
ls1c_pin_mode,
ls1c_pin_write,
ls1c_pin_read,
};
int hw_pin_init(void)
{
rt_device_pin_register("pin", &_ls1c_pin_ops, RT_NULL);
return 0;
}
bsp\ls1cdev\drivers\drv_gpio.c
/*
* File : drv_gpio.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-11-24 勤为本 first version
*/
#include
#include
#include "../libraries/ls1c_gpio.h"
void ls1c_pin_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
{
unsigned int gpio = pin;
if (PIN_MODE_OUTPUT == mode)
{
gpio_init(gpio, gpio_mode_output);
}
else
{
gpio_init(gpio, gpio_mode_input);
}
return ;
}
void ls1c_pin_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
{
unsigned int gpio = pin;
if (PIN_LOW == value)
{
gpio_set(gpio, gpio_level_low);
}
else
{
gpio_set(gpio, gpio_level_high);
}
return ;
}
int ls1c_pin_read(struct rt_device *device, rt_base_t pin)
{
unsigned int gpio = pin;
int value = PIN_LOW;
if (0 == gpio_get(gpio))
{
value = PIN_LOW;
}
else
{
value = PIN_HIGH;
}
return value;
}
const static struct rt_pin_ops _ls1c_pin_ops =
{
ls1c_pin_mode,
ls1c_pin_write,
ls1c_pin_read,
};
int hw_pin_init(void)
{
rt_device_pin_register("pin", &_ls1c_pin_ops, RT_NULL);
return 0;
}
INIT_BOARD_EXPORT(hw_pin_init);