#include "../support/support.h"
|
#include "../support/support.h"
|
#include "../support/board.h"
|
#include "../support/board.h"
|
|
|
#include "../support/spr_defs.h"
|
#include "../support/spr_defs.h"
|
|
|
#include "../drivers/uart.h"
|
#include "../drivers/uart.h"
|
|
|
#include "gpio.h"
|
#include "gpio.h"
|
|
|
void gpio_init(gpio_t *gpio, long instance_num, unsigned long base_addr)
|
void gpio_init(gpio_t *gpio, long instance_num, unsigned long base_addr)
|
{
|
{
|
int i = MIN_GPIO_BIT;
|
int i = MIN_GPIO_BIT;
|
|
|
if ( gpio != NULL ) {
|
if ( gpio != NULL ) {
|
gpio->instance_num = instance_num;
|
gpio->instance_num = instance_num;
|
gpio->base_addr = (unsigned char*)base_addr;
|
gpio->base_addr = (unsigned char*)base_addr;
|
for ( ;i<=MAX_GPIO_BIT;i++)
|
for ( ;i<=MAX_GPIO_BIT;i++)
|
gpio->vectors[i].vec = NULL;
|
gpio->vectors[i].vec = NULL;
|
return;
|
return;
|
} else {
|
} else {
|
// Print the error msgs here
|
// Print the error msgs here
|
//
|
//
|
uart_print_str("gpio inst in NULL.\n");
|
uart_print_str("gpio inst in NULL.\n");
|
return;
|
return;
|
}
|
}
|
}
|
}
|
|
|
void gpio_config_bit(gpio_t *gpio, unsigned long bit, iotype_t io)
|
void gpio_config_bit(gpio_t *gpio, unsigned long bit, iotype_t io)
|
{
|
{
|
if ( gpio != NULL ) {
|
if ( gpio != NULL ) {
|
if ( io == IO_INPUT ) {
|
if ( io == IO_INPUT ) {
|
gpio->io_config |= (1 << bit);
|
gpio->io_config |= (1 << bit);
|
*(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) &= (~(1 << bit));
|
*(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) &= (~(1 << bit));
|
} else {
|
} else {
|
gpio->io_config &= (~(1 << bit));
|
gpio->io_config &= (~(1 << bit));
|
*(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) |= (1 << bit);
|
*(unsigned long*)(gpio->base_addr + OE_REG_OFFSET) |= (1 << bit);
|
}
|
}
|
return;
|
return;
|
} else {
|
} else {
|
// Print the error msgs here
|
// Print the error msgs here
|
//
|
//
|
uart_print_str("gpio inst in NULL.\n");
|
uart_print_str("gpio inst in NULL.\n");
|
return;
|
return;
|
}
|
}
|
}
|
}
|
|
|
void gpio_set_bit(gpio_t *gpio, unsigned long bit, unsigned long val)
|
void gpio_set_bit(gpio_t *gpio, unsigned long bit, unsigned long val)
|
{
|
{
|
if ( gpio != NULL ) {
|
if ( gpio != NULL ) {
|
if ( val != 0 )
|
if ( val != 0 )
|
*(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) |= (1 << bit);
|
*(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) |= (1 << bit);
|
else
|
else
|
*(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) &= (~(1 << bit));
|
*(unsigned long*)(gpio->base_addr + OUT_REG_OFFSET) &= (~(1 << bit));
|
return;
|
return;
|
} else {
|
} else {
|
// Print the error msgs here
|
// Print the error msgs here
|
//
|
//
|
uart_print_str("gpio inst in NULL.\n");
|
uart_print_str("gpio inst in NULL.\n");
|
return;
|
return;
|
}
|
}
|
}
|
}
|
|
|
void gpio_get_bit(gpio_t *gpio, unsigned long bit, unsigned long *val)
|
void gpio_get_bit(gpio_t *gpio, unsigned long bit, unsigned long *val)
|
{
|
{
|
unsigned long temp;
|
unsigned long temp;
|
|
|
if ( gpio != NULL ) {
|
if ( gpio != NULL ) {
|
temp = *(unsigned long*)(gpio->base_addr + IN_REG_OFFSET);
|
temp = *(unsigned long*)(gpio->base_addr + IN_REG_OFFSET);
|
*val = (temp & (1 << bit))? 1 : 0;
|
*val = (temp & (1 << bit))? 1 : 0;
|
return;
|
return;
|
} else {
|
} else {
|
// Print the error msgs here
|
// Print the error msgs here
|
//
|
//
|
uart_print_str("gpio inst in NULL.\n");
|
uart_print_str("gpio inst in NULL.\n");
|
return;
|
return;
|
}
|
}
|
}
|
}
|
|
|
|
|
void gpio_add_interrupt(gpio_t *gpio, unsigned int bit, edge_t edge,void (*func)() )
|
void gpio_add_interrupt(gpio_t *gpio, unsigned int bit, edge_t edge,void (*func)() )
|
{
|
{
|
if ( gpio != NULL ) {
|
if ( gpio != NULL ) {
|
if ( ( gpio->io_config &(1 << bit)) != 0 ) { // Port bit is configured as IO_INPUT
|
if ( ( gpio->io_config &(1 << bit)) != 0 ) { // Port bit is configured as IO_INPUT
|
//
|
//
|
// Disable the interrupts
|
// Disable the interrupts
|
//
|
//
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x01);
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x01);
|
|
|
// Enable the interrupt bit
|
// Enable the interrupt bit
|
//
|
//
|
*(unsigned long*)(gpio->base_addr + INTE_REG_OFFSET) |= (1 << bit);
|
*(unsigned long*)(gpio->base_addr + INTE_REG_OFFSET) |= (1 << bit);
|
|
|
// Enable the edge type
|
// Enable the edge type
|
//
|
//
|
if ( edge == POS_EDGE )
|
if ( edge == POS_EDGE )
|
*(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) |= (1 << bit);
|
*(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) |= (1 << bit);
|
else
|
else
|
*(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) &= (~(1 << bit));
|
*(unsigned long*)(gpio->base_addr + PTRIG_REG_OFFSET) &= (~(1 << bit));
|
|
|
// Set the function vector
|
// Set the function vector
|
//
|
//
|
gpio->vectors[bit].vec = func;
|
gpio->vectors[bit].vec = func;
|
|
|
int_add( 6, gpio_interrupt, gpio );
|
int_add( 6, gpio_interrupt, gpio );
|
|
|
// Re-enable the global control bit
|
// Re-enable the global control bit
|
//
|
//
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) |= 0x01;
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) |= 0x01;
|
} else {
|
} else {
|
// Port is configured as IO_OUTPUT
|
// Port is configured as IO_OUTPUT
|
uart_print_str("gpio pin is not an input pin.\n");
|
uart_print_str("gpio pin is not an input pin.\n");
|
return;
|
return;
|
}
|
}
|
|
|
} else {
|
} else {
|
// Print the error msgs here
|
// Print the error msgs here
|
//
|
//
|
uart_print_str("gpio inst in NULL.\n");
|
uart_print_str("gpio inst in NULL.\n");
|
return;
|
return;
|
}
|
}
|
|
|
}
|
}
|
|
|
void gpio_interrupt(gpio_t *gpio)
|
void gpio_interrupt(gpio_t *gpio)
|
{
|
{
|
int i;
|
int i;
|
unsigned long int interrupt_status;
|
unsigned long int interrupt_status;
|
|
|
if ( (*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET)) & 0x02 )
|
if ( (*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET)) & 0x02 )
|
{
|
{
|
// Interrupt is pending here
|
// Interrupt is pending here
|
//
|
//
|
interrupt_status = *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET);
|
interrupt_status = *(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET);
|
|
|
// Prioritize from lower bits(0) to higher ones(31)
|
// Prioritize from lower bits(0) to higher ones(31)
|
//
|
//
|
|
|
for ( i=MIN_GPIO_BIT; i<=MAX_GPIO_BIT; i++ ) {
|
for ( i=MIN_GPIO_BIT; i<=MAX_GPIO_BIT; i++ ) {
|
if ( (interrupt_status & (1<<i)) ) {
|
if ( (interrupt_status & (1<<i)) ) {
|
*(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET) &= (~( 1 << i ));
|
*(unsigned long*)(gpio->base_addr + INTS_REG_OFFSET) &= (~( 1 << i ));
|
(gpio->vectors[i].vec)();
|
(gpio->vectors[i].vec)();
|
}
|
}
|
}
|
}
|
|
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x02);
|
*(unsigned long*)(gpio->base_addr + CTRL_REG_OFFSET) &= (~0x02);
|
|
|
}
|
}
|
}
|
}
|
|
|
void hello_east()
|
void hello_east()
|
{
|
{
|
uart_print_str("Hello from PUSH Button EAST.\n");
|
uart_print_str("Hello from PUSH Button EAST.\n");
|
}
|
}
|
|
|
|
|
void hello_west()
|
void hello_west()
|
{
|
{
|
uart_print_str("Hello from PUSH Button WEST.\n");
|
uart_print_str("Hello from PUSH Button WEST.\n");
|
}
|
}
|
|
|
|
|
void hello_south()
|
void hello_south()
|
{
|
{
|
uart_print_str("Hello from PUSH Button SOUTH.\n");
|
uart_print_str("Hello from PUSH Button SOUTH.\n");
|
}
|
}
|
|
|
|
|
|
|
|
|
#define MAX_COUNT 10
|
#define MAX_COUNT 10
|
|
|
int main()
|
int main()
|
{
|
{
|
gpio_t gpio_1;
|
gpio_t gpio_1;
|
unsigned long t0, t1, t2, t3;
|
unsigned long t0, t1, t2, t3;
|
unsigned long count = 0;
|
unsigned long count = 0;
|
|
|
tick_init();
|
tick_init();
|
uart_init();
|
uart_init();
|
int_init();
|
int_init();
|
int_add(2,&uart_interrupt);
|
int_add(2,&uart_interrupt);
|
|
|
gpio_init( &gpio_1, 1, GPIO_BASE );
|
gpio_init( &gpio_1, 1, GPIO_BASE );
|
|
|
gpio_config_bit( &gpio_1, LED_0, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_0, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_1, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_1, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_2, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_2, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_3, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_3, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_4, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_4, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_5, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_5, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_6, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_6, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_7, IO_OUTPUT);
|
gpio_config_bit( &gpio_1, LED_7, IO_OUTPUT);
|
|
|
while ( count++ < MAX_COUNT ) {
|
while ( count++ < MAX_COUNT ) {
|
gpio_set_bit( &gpio_1, LED_7, 0 );
|
gpio_set_bit( &gpio_1, LED_7, 0 );
|
gpio_set_bit( &gpio_1, LED_0, 1 );
|
gpio_set_bit( &gpio_1, LED_0, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_0, 0 );
|
gpio_set_bit( &gpio_1, LED_0, 0 );
|
gpio_set_bit( &gpio_1, LED_1, 1 );
|
gpio_set_bit( &gpio_1, LED_1, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_1, 0 );
|
gpio_set_bit( &gpio_1, LED_1, 0 );
|
gpio_set_bit( &gpio_1, LED_2, 1 );
|
gpio_set_bit( &gpio_1, LED_2, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_2, 0 );
|
gpio_set_bit( &gpio_1, LED_2, 0 );
|
gpio_set_bit( &gpio_1, LED_3, 1 );
|
gpio_set_bit( &gpio_1, LED_3, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_3, 0 );
|
gpio_set_bit( &gpio_1, LED_3, 0 );
|
gpio_set_bit( &gpio_1, LED_4, 1 );
|
gpio_set_bit( &gpio_1, LED_4, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_4, 0 );
|
gpio_set_bit( &gpio_1, LED_4, 0 );
|
gpio_set_bit( &gpio_1, LED_5, 1 );
|
gpio_set_bit( &gpio_1, LED_5, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_5, 0 );
|
gpio_set_bit( &gpio_1, LED_5, 0 );
|
gpio_set_bit( &gpio_1, LED_6, 1 );
|
gpio_set_bit( &gpio_1, LED_6, 1 );
|
udelay();
|
udelay();
|
gpio_set_bit( &gpio_1, LED_6, 0 );
|
gpio_set_bit( &gpio_1, LED_6, 0 );
|
gpio_set_bit( &gpio_1, LED_7, 1 );
|
gpio_set_bit( &gpio_1, LED_7, 1 );
|
udelay();
|
udelay();
|
}
|
}
|
|
|
gpio_set_bit( &gpio_1, LED_7, 0 );
|
gpio_set_bit( &gpio_1, LED_7, 0 );
|
|
|
report(0xdeaddead);
|
report(0xdeaddead);
|
or32_exit(0);
|
or32_exit(0);
|
}
|
}
|
|
|