rtt-f030/bsp/samd21/applications/application.c

120 lines
2.6 KiB
C
Raw Permalink Normal View History

2017-08-30 12:18:28 +08:00
#include <rtthread.h>
#include <rthw.h>
#include "board.h"
#include "port.h"
#include "extint.h"
#include "sleep_timer.h"
void LED_Init(void)
{
2017-08-30 21:13:46 +08:00
struct port_config config_port_pin;
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
port_get_config_defaults(&config_port_pin);
config_port_pin.direction = PORT_PIN_DIR_INPUT;
config_port_pin.input_pull = PORT_PIN_PULL_UP;
port_pin_set_config(PIN_PA15, &config_port_pin);
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
config_port_pin.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(PIN_PB30, &config_port_pin);
2017-08-30 12:18:28 +08:00
}
void LED_ON(void)
{
2017-08-30 21:13:46 +08:00
port_pin_set_output_level(PIN_PB30, false);
2017-08-30 12:18:28 +08:00
}
void LED_OFF(void)
{
port_pin_set_output_level(PIN_PB30, true);
}
void extint_detection_callback(void);
void configure_extint_channel(void)
{
//! [setup_1]
2017-08-30 21:13:46 +08:00
struct extint_chan_conf config_extint_chan;
2017-08-30 12:18:28 +08:00
//! [setup_1]
//! [setup_2]
2017-08-30 21:13:46 +08:00
extint_chan_get_config_defaults(&config_extint_chan);
2017-08-30 12:18:28 +08:00
//! [setup_2]
//! [setup_3]
2017-08-30 21:13:46 +08:00
config_extint_chan.gpio_pin = PIN_PA15A_EIC_EXTINT15;
config_extint_chan.gpio_pin_mux = MUX_PA15A_EIC_EXTINT15;
config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
config_extint_chan.detection_criteria = EXTINT_DETECT_BOTH;
2017-08-30 12:18:28 +08:00
//! [setup_3]
//! [setup_4]
2017-08-30 21:13:46 +08:00
extint_chan_set_config(15, &config_extint_chan);
2017-08-30 12:18:28 +08:00
//! [setup_4]
}
void configure_extint_callbacks(void)
{
//! [setup_5]
2017-08-30 21:13:46 +08:00
extint_register_callback(extint_detection_callback, 15, EXTINT_CALLBACK_TYPE_DETECT);
2017-08-30 12:18:28 +08:00
//! [setup_5]
//! [setup_6]
2017-08-30 21:13:46 +08:00
extint_chan_enable_callback(15, EXTINT_CALLBACK_TYPE_DETECT);
2017-08-30 12:18:28 +08:00
//! [setup_6]
}
//! [setup_7]
void extint_detection_callback(void)
{
2017-08-30 21:13:46 +08:00
bool pin_state = port_pin_get_input_level(PIN_PA15);
port_pin_set_output_level(PIN_PB30, pin_state);
2017-08-30 12:18:28 +08:00
}
static struct rt_semaphore _rx_sem;
static rt_err_t _rx_ind(rt_device_t dev, rt_size_t size)
{
2017-08-30 21:13:46 +08:00
return rt_sem_release(&_rx_sem);
2017-08-30 12:18:28 +08:00
}
void rt_init_thread_entry(void* parameter)
{
2017-08-30 21:13:46 +08:00
rt_thread_t thread;
rt_device_t dev;
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
rt_kprintf("SYSTEM running at %uhz\n", SystemCoreClock);
#ifdef RT_USING_FINSH
/* init finsh */
finsh_system_init();
finsh_set_device("uart1");
#endif
2017-08-30 21:13:46 +08:00
LED_Init();
configure_extint_channel();
configure_extint_callbacks();
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
sleep_timer_init();
// sleep_timer_start(1500);
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
while (1)
{
rt_kprintf("init thread running tick:%u\n", rt_tick_get());
rt_thread_delay(2*RT_TICK_PER_SECOND);
}
2017-08-30 12:18:28 +08:00
}
int rt_application_init(void)
{
2017-08-30 21:13:46 +08:00
rt_thread_t tid;
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
tid = rt_thread_create("init", rt_init_thread_entry, RT_NULL,
2017-08-30 12:18:28 +08:00
512, RT_THREAD_PRIORITY_MAX / 3, 20);
2017-08-30 21:13:46 +08:00
if (tid != RT_NULL)
rt_thread_startup(tid);
2017-08-30 12:18:28 +08:00
2017-08-30 21:13:46 +08:00
return 0;
2017-08-30 12:18:28 +08:00
}