samd: button code updated for libopencm3
This commit is contained in:
parent
b91a975d56
commit
92ed79e68e
@ -33,6 +33,9 @@
|
|||||||
#include <libopencm3/cm3/systick.h>
|
#include <libopencm3/cm3/systick.h>
|
||||||
#include <libopencm3/cm3/scb.h>
|
#include <libopencm3/cm3/scb.h>
|
||||||
|
|
||||||
|
#include <libopencm3/sam/d/tc.h>
|
||||||
|
#include <libopencm3/sam/d/eic.h>
|
||||||
|
|
||||||
static struct gclk_hw clock = {
|
static struct gclk_hw clock = {
|
||||||
.gclk0 = SRC_DFLL48M,
|
.gclk0 = SRC_DFLL48M,
|
||||||
.gclk1 = SRC_OSC8M,
|
.gclk1 = SRC_OSC8M,
|
||||||
@ -52,7 +55,6 @@ uint8_t running_status;
|
|||||||
static volatile uint32_t time_ms;
|
static volatile uint32_t time_ms;
|
||||||
|
|
||||||
uint8_t button_pressed;
|
uint8_t button_pressed;
|
||||||
//static volatile uint32_t button_counter;
|
|
||||||
|
|
||||||
void sys_tick_handler(void)
|
void sys_tick_handler(void)
|
||||||
{
|
{
|
||||||
@ -106,12 +108,6 @@ static void adc_init(void)
|
|||||||
adc_enable(ADC_REFCTRL_VREFA,0,ADC_INPUTCTRL_GND,ADC_MUXPOS);
|
adc_enable(ADC_REFCTRL_VREFA,0,ADC_INPUTCTRL_GND,ADC_MUXPOS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define EIC 0x40001800
|
|
||||||
#define EIC_CONFIG1 0x4000181C
|
|
||||||
#define EIC_INTENSET 0x4000180C
|
|
||||||
|
|
||||||
#define TC3 0x42002C00
|
|
||||||
|
|
||||||
static void counter_init(void)
|
static void counter_init(void)
|
||||||
{
|
{
|
||||||
/* enable bus and clock */
|
/* enable bus and clock */
|
||||||
@ -121,49 +117,38 @@ static void counter_init(void)
|
|||||||
periph_clk_en(GCLK_ID_TC3, 1);
|
periph_clk_en(GCLK_ID_TC3, 1);
|
||||||
|
|
||||||
/* reset */
|
/* reset */
|
||||||
*((uint16_t*)TC3) = 1;
|
tc_reset(3);
|
||||||
|
|
||||||
/* set CTRLA.MODE (default 16-bit) */
|
|
||||||
/* set CTRLA.WAVEGEN */
|
|
||||||
/* set CTRLA.PRESCALER and CTRLA.PRESYNC */
|
/* set CTRLA.PRESCALER and CTRLA.PRESYNC */
|
||||||
*((uint16_t*)TC3) = (7<<8); //| (1<<12);
|
tc_config_ctrla(3,1,(7<<8));
|
||||||
|
|
||||||
/* set CC0 (approx. 5 seconds delay) */
|
/* set CC0 (approx. 5 seconds delay) */
|
||||||
*((uint16_t*)(0x42002C18)) = 1000;
|
tc_set_cc(3,0,1000);
|
||||||
|
|
||||||
/* enable MC0 interrupt */
|
/* enable MC0 interrupt */
|
||||||
*((uint8_t*)(0x42002C0D)) = (1<<4);
|
tc_enable_interrupt(3,(1<<4));
|
||||||
nvic_enable_irq(NVIC_TC3_IRQ);
|
nvic_enable_irq(NVIC_TC3_IRQ);
|
||||||
|
|
||||||
/* set CTRLBSET.ONESHOT */
|
|
||||||
/* enable the TC */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void button_init(void)
|
static void button_init(void)
|
||||||
{
|
{
|
||||||
#if 1
|
|
||||||
gpio_config_special(BUTTON_PORT, BUTTON_PIN, SOC_GPIO_PERIPH_A);
|
gpio_config_special(BUTTON_PORT, BUTTON_PIN, SOC_GPIO_PERIPH_A);
|
||||||
|
|
||||||
|
/* enable bus and clock */
|
||||||
INSERTBF(PM_APBAMASK_EIC, 1, PM->apbamask);
|
INSERTBF(PM_APBAMASK_EIC, 1, PM->apbamask);
|
||||||
|
|
||||||
set_periph_clk(GCLK0, GCLK_ID_EIC);
|
set_periph_clk(GCLK0, GCLK_ID_EIC);
|
||||||
periph_clk_en(GCLK_ID_EIC, 1);
|
periph_clk_en(GCLK_ID_EIC, 1);
|
||||||
|
|
||||||
/* configure r/f edge, enable filtering */
|
/* configure r/f edge, enable filtering */
|
||||||
//EIC->evctrl = (1<<15);
|
eic_set_config(15, 1, EIC_FALL);
|
||||||
*((uint32_t*)EIC_CONFIG1) = ((2+8)<<28);
|
|
||||||
|
|
||||||
/* enable the IEC */
|
/* enable the IEC */
|
||||||
*((uint8_t*)EIC) = (1<<1);
|
eic_enable(1);
|
||||||
|
|
||||||
/* enable interrupts */
|
/* enable interrupts */
|
||||||
*((uint32_t*)EIC_INTENSET) = (1<<15);
|
eic_enable_interrupt((1<<15));
|
||||||
nvic_enable_irq(NVIC_EIC_IRQ);
|
nvic_enable_irq(NVIC_EIC_IRQ);
|
||||||
|
|
||||||
//button_counter = 0;
|
|
||||||
#else
|
|
||||||
gpio_config_input(BUTTON_PORT, BUTTON_PIN, 0);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void platform_init(void)
|
void platform_init(void)
|
||||||
@ -273,30 +258,28 @@ void eic_isr(void)
|
|||||||
{
|
{
|
||||||
if (!button_pressed){
|
if (!button_pressed){
|
||||||
/* set to rising-edge detection */
|
/* set to rising-edge detection */
|
||||||
*((uint32_t*)EIC_CONFIG1) = ((1+8)<<28);
|
eic_set_config(15, 1, EIC_RISE);
|
||||||
|
|
||||||
/* enable counter */
|
/* enable counter */
|
||||||
*((uint16_t*)TC3) |= 2;
|
tc_enable(3,1);
|
||||||
//*((uint8_t*)(0x42002C05)) = (1<<6);
|
|
||||||
|
|
||||||
button_pressed = 1;
|
button_pressed = 1;
|
||||||
} else {
|
} else {
|
||||||
/* set to falling-edge detection */
|
/* set to falling-edge detection */
|
||||||
*((uint32_t*)EIC_CONFIG1) = ((2+8)<<28);
|
eic_set_config(15, 1, EIC_FALL);
|
||||||
|
|
||||||
/* disable and reset counter */
|
/* disable and reset counter */
|
||||||
*((uint16_t*)TC3) &= ~2;
|
tc_enable(3,0);
|
||||||
*((uint16_t*)(0x42002C10)) = 0;
|
|
||||||
|
|
||||||
button_pressed = 0;
|
button_pressed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* clear the interrupt */
|
/* clear the interrupt */
|
||||||
*((uint32_t*)0x40001810) = (1<<15);
|
eic_clr_interrupt((1<<15));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tc3_isr(void)
|
void tc3_isr(void)
|
||||||
{
|
{
|
||||||
if ((*((uint8_t*)(0x42002C0E)) & 16))
|
if (tc_interrupt_flag(3) & 16)
|
||||||
scb_reset_system();
|
scb_reset_system();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user