mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-22 20:06:40 +00:00
Add missing dummy reads in gpio code
This commit is contained in:
parent
0c688189f6
commit
00f4e5158f
4 changed files with 88 additions and 324 deletions
|
@ -21,125 +21,66 @@
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/**
|
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin) {
|
||||||
* Returns a GPIO bank object that corresponds to the given GPIO pin,
|
|
||||||
* which can be created using the TEGRA_GPIO macro or passed from the name macro.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO to get the bank for.
|
|
||||||
* @return The GPIO bank object to use for working with the given bank.
|
|
||||||
*/
|
|
||||||
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin)
|
|
||||||
{
|
|
||||||
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
||||||
uint32_t bank_number = pin >> GPIO_BANK_SHIFT;
|
uint32_t bank_number = (pin >> GPIO_BANK_SHIFT);
|
||||||
|
|
||||||
return &gpio->bank[bank_number];
|
return &gpio->bank[bank_number];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_port(uint32_t pin) {
|
||||||
* @return the port number for working with the given GPIO.
|
return ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_port(uint32_t pin)
|
|
||||||
{
|
|
||||||
return (pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_mask(uint32_t pin) {
|
||||||
* @return a mask to be used to work with the given GPIO
|
uint32_t pin_number = (pin & GPIO_PIN_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_mask(uint32_t pin)
|
|
||||||
{
|
|
||||||
uint32_t pin_number = pin & GPIO_PIN_MASK;
|
|
||||||
return (1 << pin_number);
|
return (1 << pin_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Set or clear the bit, as appropriate.
|
/* Set or clear the bit, as appropriate. */
|
||||||
if (should_be_set)
|
if (should_be_set)
|
||||||
cluster[port] |= mask;
|
cluster[port] |= mask;
|
||||||
else
|
else
|
||||||
cluster[port] &= ~mask;
|
cluster[port] &= ~mask;
|
||||||
|
|
||||||
|
/* Dummy read. */
|
||||||
|
(void)cluster[port];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Convert the given value to a boolean.
|
/* Convert the given value to a boolean. */
|
||||||
return !!(cluster[port] & mask);
|
return !!(cluster[port] & mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_mode(uint32_t pin, uint32_t mode) {
|
||||||
* Configures a given pin as either GPIO or SFIO.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_configure_mode(uint32_t pin, uint32_t mode)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_direction(uint32_t pin, uint32_t dir) {
|
||||||
* Configures a given pin as either INPUT or OUPUT.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param direction The relevant direction.
|
|
||||||
*/
|
|
||||||
void gpio_configure_direction(uint32_t pin, uint32_t dir)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_write(uint32_t pin, uint32_t value) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_write(uint32_t pin, uint32_t value)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint32_t gpio_read(uint32_t pin) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
uint32_t gpio_read(uint32_t pin)
|
|
||||||
{
|
|
||||||
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,125 +21,66 @@
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/**
|
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin) {
|
||||||
* Returns a GPIO bank object that corresponds to the given GPIO pin,
|
|
||||||
* which can be created using the TEGRA_GPIO macro or passed from the name macro.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO to get the bank for.
|
|
||||||
* @return The GPIO bank object to use for working with the given bank.
|
|
||||||
*/
|
|
||||||
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin)
|
|
||||||
{
|
|
||||||
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
||||||
uint32_t bank_number = pin >> GPIO_BANK_SHIFT;
|
uint32_t bank_number = (pin >> GPIO_BANK_SHIFT);
|
||||||
|
|
||||||
return &gpio->bank[bank_number];
|
return &gpio->bank[bank_number];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_port(uint32_t pin) {
|
||||||
* @return the port number for working with the given GPIO.
|
return ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_port(uint32_t pin)
|
|
||||||
{
|
|
||||||
return (pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_mask(uint32_t pin) {
|
||||||
* @return a mask to be used to work with the given GPIO
|
uint32_t pin_number = (pin & GPIO_PIN_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_mask(uint32_t pin)
|
|
||||||
{
|
|
||||||
uint32_t pin_number = pin & GPIO_PIN_MASK;
|
|
||||||
return (1 << pin_number);
|
return (1 << pin_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Set or clear the bit, as appropriate.
|
/* Set or clear the bit, as appropriate. */
|
||||||
if (should_be_set)
|
if (should_be_set)
|
||||||
cluster[port] |= mask;
|
cluster[port] |= mask;
|
||||||
else
|
else
|
||||||
cluster[port] &= ~mask;
|
cluster[port] &= ~mask;
|
||||||
|
|
||||||
|
/* Dummy read. */
|
||||||
|
(void)cluster[port];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Convert the given value to a boolean.
|
/* Convert the given value to a boolean. */
|
||||||
return !!(cluster[port] & mask);
|
return !!(cluster[port] & mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_mode(uint32_t pin, uint32_t mode) {
|
||||||
* Configures a given pin as either GPIO or SFIO.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_configure_mode(uint32_t pin, uint32_t mode)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_direction(uint32_t pin, uint32_t dir) {
|
||||||
* Configures a given pin as either INPUT or OUPUT.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param direction The relevant direction.
|
|
||||||
*/
|
|
||||||
void gpio_configure_direction(uint32_t pin, uint32_t dir)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_write(uint32_t pin, uint32_t value) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_write(uint32_t pin, uint32_t value)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint32_t gpio_read(uint32_t pin) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
uint32_t gpio_read(uint32_t pin)
|
|
||||||
{
|
|
||||||
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,125 +21,66 @@
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/**
|
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin) {
|
||||||
* Returns a GPIO bank object that corresponds to the given GPIO pin,
|
|
||||||
* which can be created using the TEGRA_GPIO macro or passed from the name macro.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO to get the bank for.
|
|
||||||
* @return The GPIO bank object to use for working with the given bank.
|
|
||||||
*/
|
|
||||||
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin)
|
|
||||||
{
|
|
||||||
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
||||||
uint32_t bank_number = pin >> GPIO_BANK_SHIFT;
|
uint32_t bank_number = (pin >> GPIO_BANK_SHIFT);
|
||||||
|
|
||||||
return &gpio->bank[bank_number];
|
return &gpio->bank[bank_number];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_port(uint32_t pin) {
|
||||||
* @return the port number for working with the given GPIO.
|
return ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_port(uint32_t pin)
|
|
||||||
{
|
|
||||||
return (pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_mask(uint32_t pin) {
|
||||||
* @return a mask to be used to work with the given GPIO
|
uint32_t pin_number = (pin & GPIO_PIN_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_mask(uint32_t pin)
|
|
||||||
{
|
|
||||||
uint32_t pin_number = pin & GPIO_PIN_MASK;
|
|
||||||
return (1 << pin_number);
|
return (1 << pin_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Set or clear the bit, as appropriate.
|
/* Set or clear the bit, as appropriate. */
|
||||||
if (should_be_set)
|
if (should_be_set)
|
||||||
cluster[port] |= mask;
|
cluster[port] |= mask;
|
||||||
else
|
else
|
||||||
cluster[port] &= ~mask;
|
cluster[port] &= ~mask;
|
||||||
|
|
||||||
|
/* Dummy read. */
|
||||||
|
(void)cluster[port];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Convert the given value to a boolean.
|
/* Convert the given value to a boolean. */
|
||||||
return !!(cluster[port] & mask);
|
return !!(cluster[port] & mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_mode(uint32_t pin, uint32_t mode) {
|
||||||
* Configures a given pin as either GPIO or SFIO.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_configure_mode(uint32_t pin, uint32_t mode)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_direction(uint32_t pin, uint32_t dir) {
|
||||||
* Configures a given pin as either INPUT or OUPUT.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param direction The relevant direction.
|
|
||||||
*/
|
|
||||||
void gpio_configure_direction(uint32_t pin, uint32_t dir)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_write(uint32_t pin, uint32_t value) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_write(uint32_t pin, uint32_t value)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint32_t gpio_read(uint32_t pin) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
uint32_t gpio_read(uint32_t pin)
|
|
||||||
{
|
|
||||||
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,125 +21,66 @@
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/**
|
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin) {
|
||||||
* Returns a GPIO bank object that corresponds to the given GPIO pin,
|
|
||||||
* which can be created using the TEGRA_GPIO macro or passed from the name macro.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO to get the bank for.
|
|
||||||
* @return The GPIO bank object to use for working with the given bank.
|
|
||||||
*/
|
|
||||||
static volatile tegra_gpio_bank_t *gpio_get_bank(uint32_t pin)
|
|
||||||
{
|
|
||||||
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
||||||
uint32_t bank_number = pin >> GPIO_BANK_SHIFT;
|
uint32_t bank_number = (pin >> GPIO_BANK_SHIFT);
|
||||||
|
|
||||||
return &gpio->bank[bank_number];
|
return &gpio->bank[bank_number];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_port(uint32_t pin) {
|
||||||
* @return the port number for working with the given GPIO.
|
return ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_port(uint32_t pin)
|
|
||||||
{
|
|
||||||
return (pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static volatile uint32_t gpio_get_mask(uint32_t pin) {
|
||||||
* @return a mask to be used to work with the given GPIO
|
uint32_t pin_number = (pin & GPIO_PIN_MASK);
|
||||||
*/
|
|
||||||
static volatile uint32_t gpio_get_mask(uint32_t pin)
|
|
||||||
{
|
|
||||||
uint32_t pin_number = pin & GPIO_PIN_MASK;
|
|
||||||
return (1 << pin_number);
|
return (1 << pin_number);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static void gpio_simple_register_set(uint32_t pin, bool should_be_set, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Set or clear the bit, as appropriate.
|
/* Set or clear the bit, as appropriate. */
|
||||||
if (should_be_set)
|
if (should_be_set)
|
||||||
cluster[port] |= mask;
|
cluster[port] |= mask;
|
||||||
else
|
else
|
||||||
cluster[port] &= ~mask;
|
cluster[port] &= ~mask;
|
||||||
|
|
||||||
|
/* Dummy read. */
|
||||||
|
(void)cluster[port];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset) {
|
||||||
* Performs a simple GPIO configuration operation.
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param should_be_set True iff the relevant bit should be set; or false if it should be cleared.
|
|
||||||
* @param offset The offset into a gpio_bank structure
|
|
||||||
*/
|
|
||||||
static bool gpio_simple_register_get(uint32_t pin, uint32_t offset)
|
|
||||||
{
|
|
||||||
// Retrieve the register set that corresponds to the given pin and offset.
|
|
||||||
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
uintptr_t cluster_addr = (uintptr_t)gpio_get_bank(pin) + offset;
|
||||||
uint32_t *cluster = (uint32_t *)cluster_addr;
|
uint32_t *cluster = (uint32_t *)cluster_addr;
|
||||||
|
|
||||||
// Figure out the offset into the cluster,
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
||||||
// and the mask to be used.
|
|
||||||
uint32_t port = gpio_get_port(pin);
|
uint32_t port = gpio_get_port(pin);
|
||||||
uint32_t mask = gpio_get_mask(pin);
|
uint32_t mask = gpio_get_mask(pin);
|
||||||
|
|
||||||
// Convert the given value to a boolean.
|
/* Convert the given value to a boolean. */
|
||||||
return !!(cluster[port] & mask);
|
return !!(cluster[port] & mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_mode(uint32_t pin, uint32_t mode) {
|
||||||
* Configures a given pin as either GPIO or SFIO.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_configure_mode(uint32_t pin, uint32_t mode)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
gpio_simple_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_configure_direction(uint32_t pin, uint32_t dir) {
|
||||||
* Configures a given pin as either INPUT or OUPUT.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param direction The relevant direction.
|
|
||||||
*/
|
|
||||||
void gpio_configure_direction(uint32_t pin, uint32_t dir)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
gpio_simple_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
void gpio_write(uint32_t pin, uint32_t value) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
void gpio_write(uint32_t pin, uint32_t value)
|
|
||||||
{
|
|
||||||
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
gpio_simple_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
uint32_t gpio_read(uint32_t pin) {
|
||||||
* Drives a relevant GPIO pin as either HIGH or LOW.
|
|
||||||
*
|
|
||||||
* @param pin The GPIO pin to work with, as created with TEGRA_GPIO, or a named GPIO.
|
|
||||||
* @param mode The relevant mode.
|
|
||||||
*/
|
|
||||||
uint32_t gpio_read(uint32_t pin)
|
|
||||||
{
|
|
||||||
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
return gpio_simple_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue