Skip to content

Commit

Permalink
code formating, CR changes corrected spi_init() to properly handle re…
Browse files Browse the repository at this point in the history
…-initialization… #3842
  • Loading branch information
nvlsianpu authored and adbridge committed Mar 24, 2017
1 parent 6501389 commit 12e1e7c
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 372 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,33 @@ extern "C" {
#define STDIO_UART_RX RX_PIN_NUMBER
#define STDIO_UART UART_0

typedef enum {
typedef enum
{
UART_0 = (int)NRF_UART0_BASE
} UARTName;


typedef enum {
typedef enum
{
SPI_0 = (int)NRF_SPI0_BASE,
SPI_1 = (int)NRF_SPI1_BASE,
SPIS = (int)NRF_SPIS1_BASE
} SPIName;

typedef enum {
typedef enum
{
PWM_1 = 0,
PWM_2
} PWMName;

typedef enum {
typedef enum
{
I2C_0 = (int)NRF_TWI0_BASE,
I2C_1 = (int)NRF_TWI1_BASE
} I2CName;

typedef enum {
typedef enum
{
ADC0_0 = (int)0
} ADCName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
extern "C" {
#endif

typedef enum {
typedef enum
{
Port0 = 0, //GPIO pins 0-31 -> 0.0-0.31
Port1 = 1 //GPIO pins 32-47 -> 1.0-1.15
} PortName;
Expand Down
89 changes: 57 additions & 32 deletions targets/TARGET_NORDIC/TARGET_NRF5_SDK13/gpio_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#error not recognized gpio count for mcu
#endif

typedef struct {
typedef struct
{
bool used_as_gpio : 1;
PinDirection direction : 1;
bool init_high : 1;
Expand All @@ -45,29 +46,31 @@ typedef struct {
typedef uint32_t gpio_mask_t;
#endif

gpio_mask_t m_gpio_initialized;
gpio_cfg_t m_gpio_cfg[GPIO_PIN_COUNT];
static gpio_mask_t m_gpio_initialized;
static gpio_cfg_t m_gpio_cfg[GPIO_PIN_COUNT];


/***********
GPIO IRQ
***********/

static gpio_irq_handler m_irq_handler;
static uint32_t m_channel_ids[GPIO_PIN_COUNT] = {0};
gpio_mask_t m_gpio_irq_enabled;
static uint32_t m_channel_ids[GPIO_PIN_COUNT] = {0};
static gpio_mask_t m_gpio_irq_enabled;


static void gpiote_irq_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
nrf_gpio_pin_sense_t sense = nrf_gpio_pin_sense_get(pin);
gpio_irq_event event = (sense == NRF_GPIO_PIN_SENSE_LOW) ? IRQ_RISE : IRQ_FALL;

if (m_gpio_irq_enabled & ((gpio_mask_t)1 << pin)) {

if (m_gpio_irq_enabled & ((gpio_mask_t)1 << pin))
{
if (((event == IRQ_RISE) && m_gpio_cfg[pin].irq_rise)
|| ((event == IRQ_FALL) && m_gpio_cfg[pin].irq_fall)) {
m_irq_handler(m_channel_ids[pin], event);
}
|| ((event == IRQ_FALL) && m_gpio_cfg[pin].irq_fall))
{
m_irq_handler(m_channel_ids[pin], event);
}
}
}

Expand All @@ -76,7 +79,8 @@ void GPIOTE_IRQHandler(void);// exported from nrf_drv_gpiote.c
void gpio_init(gpio_t *obj, PinName pin)
{
obj->pin = pin;
if (pin == (PinName)NC) {
if (pin == (PinName)NC)
{
return;
}
MBED_ASSERT((uint32_t)pin < GPIO_PIN_COUNT);
Expand All @@ -92,37 +96,46 @@ void gpio_init(gpio_t *obj, PinName pin)
int gpio_read(gpio_t *obj)
{
MBED_ASSERT(obj->pin != (PinName)NC);
if (m_gpio_cfg[obj->pin].direction == PIN_OUTPUT) {
if (m_gpio_cfg[obj->pin].direction == PIN_OUTPUT)
{
return (nrf_gpio_pin_out_read(obj->pin) ? 1 : 0);
} else {
}
else
{
return nrf_gpio_pin_read(obj->pin);
}
}

static void gpiote_pin_uninit(uint8_t pin)
{
if (m_gpio_initialized & ((gpio_mask_t)1 << pin)) {
if ((m_gpio_cfg[pin].direction == PIN_OUTPUT) && (!m_gpio_cfg[pin].used_as_irq)) {
if (m_gpio_initialized & ((gpio_mask_t)1 << pin))
{
if ((m_gpio_cfg[pin].direction == PIN_OUTPUT) && (!m_gpio_cfg[pin].used_as_irq))
{
nrf_drv_gpiote_out_uninit(pin);
}
else {
else
{
nrf_drv_gpiote_in_uninit(pin);
}
}
}

static void gpio_apply_config(uint8_t pin)
{
if (m_gpio_cfg[pin].used_as_gpio || m_gpio_cfg[pin].used_as_irq) {
if (m_gpio_cfg[pin].used_as_gpio || m_gpio_cfg[pin].used_as_irq)
{
if ((m_gpio_cfg[pin].direction == PIN_INPUT)
|| (m_gpio_cfg[pin].used_as_irq)) {
|| (m_gpio_cfg[pin].used_as_irq))
{
//Configure as input.
nrf_drv_gpiote_in_config_t cfg;

cfg.hi_accuracy = false;
cfg.is_watcher = false;
cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE;
if (m_gpio_cfg[pin].used_as_irq) {
if (m_gpio_cfg[pin].used_as_irq)
{
cfg.pull = NRF_GPIO_PIN_PULLUP;
nrf_drv_gpiote_in_init(pin, &cfg, gpiote_irq_handler);
if ((m_gpio_irq_enabled & ((gpio_mask_t)1 << pin))
Expand All @@ -131,8 +144,10 @@ static void gpio_apply_config(uint8_t pin)
nrf_drv_gpiote_in_event_enable(pin, true);
}
}
else {
switch(m_gpio_cfg[pin].pull) {
else
{
switch (m_gpio_cfg[pin].pull)
{
case PullUp:
cfg.pull = NRF_GPIO_PIN_PULLUP;
break;
Expand All @@ -146,22 +161,24 @@ static void gpio_apply_config(uint8_t pin)
nrf_drv_gpiote_in_init(pin, &cfg, NULL);
}
}
else {
else
{
// Configure as output.
nrf_drv_gpiote_out_config_t cfg = GPIOTE_CONFIG_OUT_SIMPLE(m_gpio_cfg[pin].init_high);
nrf_drv_gpiote_out_init(pin, &cfg);
}
m_gpio_initialized |= ((gpio_mask_t)1 << pin);
}
else {
else
{
m_gpio_initialized &= ~((gpio_mask_t)1 << pin);
}
}


void gpio_mode(gpio_t *obj, PinMode mode)
{
MBED_ASSERT(obj->pin <= GPIO_PIN_COUNT);
MBED_ASSERT(obj->pin != (PinName)NC);

gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.

Expand All @@ -172,7 +189,7 @@ void gpio_mode(gpio_t *obj, PinMode mode)

void gpio_dir(gpio_t *obj, PinDirection direction)
{
MBED_ASSERT(obj->pin <= GPIO_PIN_COUNT);
MBED_ASSERT(obj->pin != (PinName)NC);

gpiote_pin_uninit(obj->pin); // try to uninitialize gpio before a change.

Expand All @@ -187,7 +204,8 @@ void gpio_dir(gpio_t *obj, PinDirection direction)

int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
{
if (pin == NC) {
if (pin == NC)
{
return -1;
}
MBED_ASSERT((uint32_t)pin < GPIO_PIN_COUNT);
Expand Down Expand Up @@ -223,19 +241,25 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
(m_gpio_irq_enabled & ((gpio_mask_t)1 << obj->ch)) &&
(cfg->irq_rise || cfg->irq_fall);

if (event == IRQ_RISE) {
if (event == IRQ_RISE)
{
cfg->irq_rise = enable ? true : false;
}
else if (event == IRQ_FALL) {
else if (event == IRQ_FALL)
{
cfg->irq_fall = enable ? true : false;
}

bool irq_enabled_after = cfg->irq_rise || cfg->irq_fall;

if (irq_enabled_before != irq_enabled_after) {
if (irq_enabled_after) {
if (irq_enabled_before != irq_enabled_after)
{
if (irq_enabled_after)
{
gpio_irq_enable(obj);
} else {
}
else
{
gpio_irq_disable(obj);
}
}
Expand All @@ -245,7 +269,8 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable)
void gpio_irq_enable(gpio_irq_t *obj)
{
m_gpio_irq_enabled |= ((gpio_mask_t)1 << obj->ch);
if (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall) {
if (m_gpio_cfg[obj->ch].irq_rise || m_gpio_cfg[obj->ch].irq_fall)
{
nrf_drv_gpiote_in_event_enable(obj->ch, true);
}
}
Expand Down
16 changes: 11 additions & 5 deletions targets/TARGET_NORDIC/TARGET_NRF5_SDK13/gpio_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
extern "C" {
#endif

typedef struct {
typedef struct
{
PinName pin;
} gpio_t;

static inline void gpio_write(gpio_t *obj, int value) {
static inline void gpio_write(gpio_t *obj, int value)
{
MBED_ASSERT(obj->pin != (PinName)NC);
if (value) {
if (value)
{
nrf_gpio_pin_set(obj->pin);
} else {
}
else
{
nrf_gpio_pin_clear(obj->pin);
}
}

static inline int gpio_is_connected(const gpio_t *obj) {
static inline int gpio_is_connected(const gpio_t *obj)
{
return obj->pin != (PinName)NC;
}

Expand Down
Loading

0 comments on commit 12e1e7c

Please sign in to comment.