White Papers

9
for ( i = PIN_GPO0 ; i <= PIN_GPO7 ; i++ ) {
gc->pins[i].mode = GPIO_MODE_OUTPUT;
// we do not allow user to change pin direction as input
gc->pins[i].capability = GPIO_CAP_OUTPUT;
}
for ( i = PIN_GPI0 ; i <= PIN_GPI7 ; i++ ) {
gc->pins[i].mode = GPIO_MODE_INPUT;
// we do not allow user to change pin direction as output
gc->pins[i].capability = GPIO_CAP_INPUT;
}
enter_extended_function_mode();
int bmp = 0;
for ( i = 0 ; i < MAX_PIN_NUM ; i++ ) {
int bit_index = i % 8;
if ( gc->pins[i].mode == GPIO_MODE_INPUT )
bmp |= (1 << bit_index);
if ( bit_index == 7 ) {
int byte_index = i / 8;
int device_num = sio_devices[byte_index].device_num;
int base_port = sio_devices[byte_index].base_port;
// Select device
lpc_reg_write(0x07, device_num);
// Write bmp to base_port of GPIO
lpc_reg_write(base_port, bmp);
bmp = 0;
}
}
leave_extended_function_mode();
gc->signature = MAGIC_KEY;
return gc;
}
void gpio_exit(gpio_controller *gc)
{
if ( gc == NULL || gc->signature != MAGIC_KEY )
return;
// Release the IO ports
ioperm(LPC_ADDR_PORT, 2, 0);
gc->signature = 0;
free(gc);
}
#define VERIFY_PARAMETERS(handle, pin) \
if ( handle == NULL || handle->signature != MAGIC_KEY ) { \
fprintf(stderr, "%s: the given handle is invalid\n", __FUNCTION__); \
return -EINVAL; \
} \
else if ( pin < 0 || pin >= MAX_PIN_NUM ) { \
fprintf(stderr, "%s: pin number %d is invalid\n", __FUNCTION__, pin); \
return -EINVAL; \
}
int gpio_pin_set_level(gpio_controller *gc, int pin, int level)
{
int bit, bmp;
VERIFY_PARAMETERS(gc, pin);