# Manage the GPIO lines using the Microchip Peripheral I/O Python Package

<abstract>
MPIO is a package that provides easy access to various hardware peripherals found on Microchip AT91/SAMA5 processors. 
The API is clean, consistent, flexible, documented, and well tested to 
make navigating and exercising even the most complex hardware peripherals a trivial task.

This article explains how to install it on Raspbian distribution on RoadRunner
</abstract>

### Installing MPIO

	sudo apt update
	sudo apt install python-pip
	pip install mpio

### Examples

#### Set hi PA25 line for 1 sec

	import mpio
	import time
	
	gpio = mpio.GPIO(25, mpio.GPIO.OUT)
	gpio.set(True)
	time.sleep(1)	
	gpio.set(False)

#### Read PA25 line state

	import mpio

	gpio = mpio.GPIO(25, mpio.GPIO.IN)
	print gpio.get()

### Pin name to Kernel GPIO ID conversion table

	// 0  ~ 31  PA0 -> PA31
	// 32 ~ 63  PB0 -> PB31
	// 33 ~ 95  PC0 -> PC31
	// 96 ~ 127 PD0 -> PD31

to convert the MCU pin name to Kernen GPIO id use this function:

	def pin2id(pinname):
	
		"""
		Return the Kernel ID of any Pin using the MCU name
		or the board name
		"""
	
		offset=None
		if pinname[0:2]=="PA":
			offset=0
		if pinname[0:2]=="PB":
			offset=32
		if pinname[0:2]=="PC":
			offset=64
		if pinname[0:2]=="PD":
			offset=96

		if offset==None:
			return None
		else:	
			return offset+int(pinname[2:4])

example of use (add pin2id function in the following code):

	import mpio
	import time
	
	gpio = mpio.GPIO(pin2id("PA25"), mpio.GPIO.OUT)
	gpio.set(True)
	time.sleep(1)	
	gpio.set(False)

#### Prepare RoadRunner for Suspend To Ram

	import mpio
	
	mpio.DevMem.write_reg(0xfc040018,0x300)

then to enter in Suspend To Ram for 5s:

	sudo rtcwake -m mem -s 5
	
## Links

* [Microchip example in Python](https://github.com/linux4sam/mpio/tree/master/examples)
* [Other Microchip example in Python](https://github.com/linux4sam/mpio/blob/master/docs/examples.rst)
* [MPIO sources on GitHub](https://github.com/linux4sam/mpio)
* [Microchip Peripheral I/O Python Package](https://pypi.org/project/mpio/)
* [AN3293 How to Use the SAMA5D2 GPIO Under Linux](http://ww1.microchip.com/downloads/en/Appnotes/AN_3293-How-to-Use-SAMA5D2-GPIO-Under-Linux-00003293a.pdf)
 
@include='adv_roadrunner'
