SAM4S Xplained Pro Parallel IO Demo

Posted on Sep 12, 2016 | 0 comments


SAM4S Xplained Pro Parallel IO Demo

ASF version

Tested for ASF version 3.31.0

ASF Modules required 

  • PIO – Parallel Input/Output Controller
  • GPIO – General Purpose Input/Output Service
  • IOPORT – General Purpose I/O Service

Basic Demo

The GPIO Basic Demo blinks all the LEDs on the main and daughter boards, one after another.

Code

#include <asf.h>

#define LED0 IOPORT_CREATE_PIN( PIOC, 23 ) 
#define LED1 IOPORT_CREATE_PIN( PIOC, 20 )  
#define LED2 IOPORT_CREATE_PIN( PIOA, 16 ) 
#define LED3 IOPORT_CREATE_PIN( PIOC, 22 )
#define LED4 IOPORT_CREATE_PIN( PIOC, 19 ) 

int main(void){
	
	board_init();
	sysclk_init();
	
	ioport_set_pin_dir( LED0, IOPORT_DIR_OUTPUT );
	ioport_set_pin_dir( LED1, IOPORT_DIR_OUTPUT );
	ioport_set_pin_dir( LED2, IOPORT_DIR_OUTPUT );
	ioport_set_pin_dir( LED3, IOPORT_DIR_OUTPUT );
	ioport_set_pin_dir( LED4, IOPORT_DIR_OUTPUT );

	bool state = true;
	while(1){
		ioport_set_pin_level( LED0, state ); delay_ms(50);
		ioport_set_pin_level( LED1, state ); delay_ms(50);
		ioport_set_pin_level( LED2, state ); delay_ms(50);
		ioport_set_pin_level( LED3, state ); delay_ms(50);
		ioport_set_pin_level( LED4, state ); delay_ms(50);
		
		state = !state;
	}
	
}

 

Advanced Demo

The GPIO Advanced Demo uses the PROTO1’s GPIO1 and GPIO2 pins. GPIO1 is connected to a LED in a breadboard, and GPIO2 is connected to a PIR motion sensor (PIR SE-10). The sensor is very sensitive and does not latches the output. This is fine, but for demo purposes extra logic was added in code to a) keep it high for a few seconds after activated and b) filter flickering.  To power the PIR sensor a 5v power supply was used (even though the PROTO1 board can provide 5v via the pins EP5V0 and P5V0)

Code

#include <asf.h>

#define GPIO1 IOPORT_CREATE_PIN( PIOA, 24 )
#define GPIO2 IOPORT_CREATE_PIN( PIOA, 25 )


int main(void){	
	
	sysclk_init();
	board_init();
	
	ioport_set_pin_dir( GPIO1, IOPORT_DIR_OUTPUT );
	ioport_set_pin_dir( GPIO2, IOPORT_DIR_INPUT );
	ioport_set_pin_mode( GPIO2, IOPORT_MODE_OPEN_DRAIN );
	
	//bool old_state = false;
	//bool state;
	bool was_activated;
	
	while(true){
		//start off
		ioport_set_pin_level( GPIO1, false );

		was_activated = !ioport_get_pin_level( GPIO2 );
			
		if( was_activated ){
			
			//wait and read again
			delay_ms(100);
			was_activated = !ioport_get_pin_level( GPIO2 );
			
			if( was_activated ){ 
			
				//wait and read again	
				delay_ms(100);
				was_activated = !ioport_get_pin_level( GPIO2 );
				
				if( was_activated ){
					
					//wait and read again
					delay_ms(100);
					was_activated = !ioport_get_pin_level( GPIO2 );
					
					if( was_activated ){
						//Sensor was activated
						ioport_set_pin_level( GPIO1, true );
						
						//leave as active for a 2 secs
						delay_ms(2000);
					}
				}
			}
		}
	}
}

 

Advanced Demo 2

Most EXT pins on the main board can be configured as GPIO. This example demonstrates 48 EXT pins configured as GPIO. 24 as input, 24 as output.

Code
#include <asf.h>

//Pins in EXT1
#define O1 IOPORT_CREATE_PIN(PIOA, 17) //pin 3
#define O2 IOPORT_CREATE_PIN(PIOA, 18)//pin 4
#define O3 IOPORT_CREATE_PIN(PIOA, 24)//pin 5
#define O4 IOPORT_CREATE_PIN(PIOA, 25)//pin 6
#define O5 IOPORT_CREATE_PIN(PIOA, 23)//pin 7
#define O6 IOPORT_CREATE_PIN(PIOA, 19)//pin 8
#define O7 IOPORT_CREATE_PIN(PIOA, 1)//pin 9
#define O8 IOPORT_CREATE_PIN(PIOA, 6)//pin 10
#define O9 IOPORT_CREATE_PIN(PIOA, 3)//pin 11
#define O10 IOPORT_CREATE_PIN(PIOA, 4)//pin 12
#define O11 IOPORT_CREATE_PIN(PIOA, 21)//pin 13
#define O12 IOPORT_CREATE_PIN(PIOA, 22)//pin 14
#define O13 IOPORT_CREATE_PIN(PIOA, 11)//pin 15
#define O14 IOPORT_CREATE_PIN(PIOA, 13)//pin 16
#define O15 IOPORT_CREATE_PIN(PIOA, 12)//pin 17
#define O16 IOPORT_CREATE_PIN(PIOA, 14)//pin 18

//Pins in EXT2
#define O17 IOPORT_CREATE_PIN(PIOB, 0)//pin 3
#define O18 IOPORT_CREATE_PIN(PIOB, 1)//pin 4
#define O19 IOPORT_CREATE_PIN(PIOC, 24)//pin 5
#define O20 IOPORT_CREATE_PIN(PIOC, 25)//pin 6
#define O21 IOPORT_CREATE_PIN(PIOC, 19)//pin 7
#define O22 IOPORT_CREATE_PIN(PIOA, 20)//pin 8
#define O23 IOPORT_CREATE_PIN(PIOC, 26)//pin 9
#define O24 IOPORT_CREATE_PIN(PIOC, 27)//pin 10
#define I1 IOPORT_CREATE_PIN(PIOA, 21)//pin 13
#define I2 IOPORT_CREATE_PIN(PIOA, 22)//pin 14
#define I3 IOPORT_CREATE_PIN(PIOA, 9)//pin 15

//Pins in EXT3
#define I4 IOPORT_CREATE_PIN(PIOC, 29)//pin 3
#define I5 IOPORT_CREATE_PIN(PIOC, 30)//pin 4
#define I6 IOPORT_CREATE_PIN(PIOC, 21)//pin 5
#define I7 IOPORT_CREATE_PIN(PIOC, 22)//pin 6
#define I8 IOPORT_CREATE_PIN(PIOC, 20)//pin 7
#define I9 IOPORT_CREATE_PIN(PIOA, 16)//pin 8
#define I10 IOPORT_CREATE_PIN(PIOA, 0)//pin 9
#define I11 IOPORT_CREATE_PIN(PIOC, 31)//pin 10
#define I12 IOPORT_CREATE_PIN(PIOB, 2)//pin 13
#define I13 IOPORT_CREATE_PIN(PIOB, 3)//pin 14
#define I14 IOPORT_CREATE_PIN(PIOA, 10)//pin 15

//Pins in PIOD Interface
#define I15 IOPORT_CREATE_PIN(PIOA, 15)//pin 1
#define I16 IOPORT_CREATE_PIN(PIOA, 16)//pin 2
#define I17 IOPORT_CREATE_PIN(PIOA, 26)//pin 6
#define I18 IOPORT_CREATE_PIN(PIOA, 27)//pin 7
#define I19 IOPORT_CREATE_PIN(PIOA, 28)//pin 8
#define I20 IOPORT_CREATE_PIN(PIOA, 29)//pin 9
#define I21 IOPORT_CREATE_PIN(PIOA, 30)//pin 10
#define I22 IOPORT_CREATE_PIN(PIOA, 31)//pin 11

//Pins SPARE Signals
#define I23 IOPORT_CREATE_PIN(PIOA, 2)//pin 1
#define I24 IOPORT_CREATE_PIN(PIOB, 13)//pin 2
#define EXTRA IOPORT_CREATE_PIN(PIOB, 14) //Pin 11 -- we won’t do anything with it


static const int dirOut = 1; //Output
static const int dirIn = 0; //Input

static int outputs[] = { O1, O2, O3, O4, O5, O6, O7, O8, O9, 
		   O10, O11, O12, O13, O14, O15, O16, O17, 
		   O18, O19, O20, O21, O22, O23, O24 };

static int inputs[] = { I1, I2, I3, I4, I5, I6, I7, I8, I9, 
		  I10, I11, I12, I13, I14, I15, I16, I17, 
		  I18, I19, I20, I21, I22, I23, I24 };

int main(void){
	sysclk_init(); 
	board_init(); 
	
	//set pins directions
	for(int i=0; i<24; i++)
		ioport_set_pin_dir( outputs[i], dirOut );
	for(int i=0; i<24; i++)
		ioport_set_pin_dir( inputs[i], dirIn );	
		
	while(1){
		//for all: output(i) = input(i)
		for(int i=0; i<24; i++)
			ioport_set_pin_level( outputs[i], ioport_get_pin_level( inputs[i] ) );
	}
}

 

Interrupt Demo

See Buttons Demo

Submit a Comment

Your email address will not be published. Required fields are marked *