scanning push button input polling setup using SysTick irq

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

recklesshao
Posts: 3
Joined: Mon Jun 30, 2014 11:30 pm

scanning push button input polling setup using SysTick irq

Sat Jul 05, 2014 8:57 am

Hi all,
i am any to ARM. i bought a sam D20 xplained board to play with at home. right now i have made a small program that uses SysTick irq to scan the push button input on PB02 and to turn on an led on PA08. The SysTick irq is working. but the code in my main loop to turn LED on and off does not get to run at all. the code stack in my setup function for SysTick. I even tried to put my main loop code inside my SysTick handler, since it is working at the time. I still can not get it to run. I scoped that SysTick frequency, because i use it to turn an output enable on a latch on and off. Systick is running at 20ms period. it should not block my main code execution , 20ms isa lot of time. can someone tell me what i did wrong? here is my code:

Code: Select all

#include <asf.h>

static void configure_systick_handler(void);

static struct port_config GPIO_config;
static uint8_t tenMS;
static uint32_t PB_Mask;
static uint32_t PB_Reg;
static uint32_t PB_Reg_buf;


int main (void)
{
	system_init();
	
	tenMS = 0x00;
	
	//configuring PB01 as output, use for bus output enable
	GPIO_config.direction = PORT_PIN_DIR_OUTPUT;
	GPIO_config.input_pull = PORT_PIN_PULL_NONE;
	port_pin_set_config(0x21, &GPIO_config);
	
	GPIO_config.direction = PORT_PIN_DIR_OUTPUT;
	GPIO_config.input_pull = PORT_PIN_PULL_NONE;
	port_pin_set_config(PIN_PA08, &GPIO_config);
	
	//configuring group of PB to be input
	GPIO_config.direction = PORT_PIN_DIR_INPUT;
	GPIO_config.input_pull = PORT_PIN_PULL_NONE;
	//&PB_Group = 0x41004480;//base address of PB register
	PB_Mask = 0x00000ffc;
	
	port_group_set_config(&PORTB, PB_Mask, &GPIO_config);
	
	//port_pin_set_output_level(0x21, 0x01);//bus output enable is off, active low
	
	configure_systick_handler();
	
	// Insert application code here, after the board has been initialized.

	// This skeleton code simply sets the LED to the state of the button.
	while (1) {
		if((PB_Reg & 0x00000004 == 0x00000004) && (PB_Reg_buf & 0x00000004 == 0x00000000))
			port_pin_toggle_output_level(PIN_PA08);
		tenMS = 0x01;
		while(tenMS == 0x01);
	}
}

static void configure_systick_handler(void)
{
	SysTick->CTRL = 0;
	SysTick->LOAD = 80000;
	SysTick->VAL  = 0;
	SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
}

void SysTick_Handler (void)
{
	port_pin_toggle_output_level(0x21);// bus output enable on
	//cpu_irq_enter_critical();
	PB_Reg_buf = PB_Reg;
	PB_Reg = port_group_get_input_level(&PORTB, PB_Mask);
	if((PB_Reg & 0x00000004 == 0x00000004) && (PB_Reg_buf & 0x00000004 == 0x00000000))
		port_pin_toggle_output_level(PIN_PA08);
	tenMS = 0x00;
	//cpu_irq_leave_critical();
}
recklesshao
Posts: 3
Joined: Mon Jun 30, 2014 11:30 pm

Re: scanning push button input polling setup using SysTick i

Sat Jul 05, 2014 6:16 pm

That is exactly what the LED_TOGGLE example does - have you tried comparing your code to that example?
Yes, I have seen the TOGGLE_LED example. What I am trying to do is a little different. First, I setup multiple pins on PB group to be input, and multiple pins on PA as output. Every time i read data, i read the whole PB register, and later i put masks on to look at different pin value. Secondly, I used edge detection in my polling loop to detect positive edge on my input pin. that is the only time i make decisions on my output level. whereas TOGGLE_LED is just following the logic level from the input to output. right now, i am just testing my setup, so i am only using one input and one output. but the code is based on group read to PB.
pozz
Posts: 67
Joined: Fri Jun 13, 2014 2:55 pm

Re: scanning push button input polling setup using SysTick i

Mon Jul 07, 2014 9:12 am

tenMS, PB_Reg and Reg_buf are variables that can be changed in ISR (so at the foreground level). Your main loop (background) must knows that, so you have to add volatile keyword to the definition of this kind of variables.

Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 1 guest