Skip to content

Commit cb97958

Browse files
author
Kyle Wenner
committed
GPIO Interrupts working, ambiq apollo3 mbed port
1 parent 52c4383 commit cb97958

File tree

9 files changed

+1337
-934
lines changed

9 files changed

+1337
-934
lines changed

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/apollo3.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ typedef enum {
147147
#define __IOM __IO
148148
#endif
149149

150-
#ifdef OVERFLOW
151-
#undef OVERFLOW
152-
#endif
153-
154150
/* ======================================== Start of section using anonymous unions ======================================== */
155151
#if defined (__CC_ARM)
156152
#pragma push

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/cmsis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef MBED_CMSIS_H
88
#define MBED_CMSIS_H
99

10-
#include "apollo3.h"
10+
#include "am_mcu_apollo.h"
1111
#include "cmsis_nvic.h"
1212

1313
#endif

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_api.c

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ SOFTWARE.
2828
* @param pin The pin to be set as GPIO
2929
* @return The GPIO port mask for this pin
3030
**/
31-
uint32_t gpio_set(PinName pin){
31+
uint32_t gpio_set(PinName pin)
32+
{
3233
MBED_ASSERT(pin != (PinName)NC);
3334
return (uint32_t)AM_HAL_GPIO_BIT(pin);
3435
}
@@ -38,7 +39,8 @@ uint32_t gpio_set(PinName pin){
3839
* @return 0 if object was initialized with NC
3940
* @return non-zero if object was initialized with a valid PinName
4041
**/
41-
int gpio_is_connected(const gpio_t *obj){
42+
int gpio_is_connected(const gpio_t *obj)
43+
{
4244
MBED_ASSERT(obj != NULL);
4345
return (int)(((PinName)obj->pad == (PinName)NC) ? 0 : 1);
4446
}
@@ -48,82 +50,170 @@ int gpio_is_connected(const gpio_t *obj){
4850
* @param obj The GPIO object to initialize
4951
* @param pin The GPIO pin to initialize (may be NC)
5052
*/
51-
void gpio_init(gpio_t *obj, PinName pin){
53+
void gpio_init(gpio_t *obj, PinName pin)
54+
{
5255
MBED_ASSERT(obj != NULL);
5356
obj->pad = (ap3_uart_pad_t)pin;
5457
return;
5558
}
56-
5759
/** Set the input pin mode
5860
*
5961
* @param obj The GPIO object (must be connected)
6062
* @param mode The pin mode to be set
6163
*/
62-
void gpio_mode(gpio_t *obj, PinMode mode){
64+
void gpio_mode(gpio_t *obj, PinMode mode)
65+
{
6366
MBED_ASSERT(gpio_is_connected(obj));
6467
MBED_ASSERT(mode < (PinMode)PinModeElements);
68+
am_hal_gpio_pincfg_allow_t pinConfigBools;
6569

66-
obj->cfg.uFuncSel = 3; // gpio
70+
obj->cfg.uFuncSel = AP3_PINCFG_FUNCSEL_GPIO; // gpio
6771

68-
if( mode & (PinMode)PowerSwNone ){ obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_NONE; }
69-
if( mode & (PinMode)PowerSwVDD ){ obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_VDD; }
70-
if( mode & (PinMode)PowerSwVSS ){ obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_VSS; }
72+
if (mode & (PinMode)PowerSwNone)
73+
{
74+
obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_NONE;
75+
pinConfigBools.powerSw = true;
76+
}
77+
if (mode & (PinMode)PowerSwVDD)
78+
{
79+
obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_VDD;
80+
pinConfigBools.powerSw = true;
81+
}
82+
if (mode & (PinMode)PowerSwVSS)
83+
{
84+
obj->cfg.ePowerSw = AM_HAL_GPIO_PIN_POWERSW_VSS;
85+
pinConfigBools.powerSw = true;
86+
}
7187

72-
if( mode & (PinMode)PullNone ){ obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_NONE; }
73-
if( mode & (PinMode)PullUp ){ obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_WEAK; }
74-
if( mode & (PinMode)PullDown ){ obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLDOWN; }
88+
if (mode & (PinMode)PullNone)
89+
{
90+
obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_NONE;
91+
pinConfigBools.pullup = true;
92+
}
93+
if (mode & (PinMode)PullUp)
94+
{
95+
obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_WEAK;
96+
pinConfigBools.pullup = true;
97+
}
98+
if (mode & (PinMode)PullDown)
99+
{
100+
obj->cfg.ePullup = AM_HAL_GPIO_PIN_PULLDOWN;
101+
pinConfigBools.pullup = true;
102+
}
75103

76-
if( mode & (PinMode)DriveStrength2mA ){ obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_2MA; }
77-
if( mode & (PinMode)DriveStrength4mA ){ obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_4MA; }
78-
if( mode & (PinMode)DriveStrength8mA ){ obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_8MA; }
79-
if( mode & (PinMode)DriveStrength12mA ){ obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA; }
104+
if (mode & (PinMode)DriveStrength2mA)
105+
{
106+
obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_2MA;
107+
pinConfigBools.driveStrength = true;
108+
}
109+
if (mode & (PinMode)DriveStrength4mA)
110+
{
111+
obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_4MA;
112+
pinConfigBools.driveStrength = true;
113+
}
114+
if (mode & (PinMode)DriveStrength8mA)
115+
{
116+
obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_8MA;
117+
pinConfigBools.driveStrength = true;
118+
}
119+
if (mode & (PinMode)DriveStrength12mA)
120+
{
121+
obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
122+
pinConfigBools.driveStrength = true;
123+
}
80124

81-
if( mode & (PinMode)OutDisable ){ obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_DISABLE; }
82-
if( mode & (PinMode)OutPushPull ){ obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL; }
83-
if( mode & (PinMode)OutOpenDrain ){ obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN; }
84-
if( mode & (PinMode)OutTristate ){ obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_TRISTATE; }
125+
if (mode & (PinMode)OutDisable)
126+
{
127+
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_DISABLE;
128+
pinConfigBools.gpOutcfg = true;
129+
}
130+
if (mode & (PinMode)OutPushPull)
131+
{
132+
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL;
133+
pinConfigBools.gpOutcfg = true;
134+
}
135+
if (mode & (PinMode)OutOpenDrain)
136+
{
137+
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
138+
pinConfigBools.gpOutcfg = true;
139+
}
140+
if (mode & (PinMode)OutTristate)
141+
{
142+
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_TRISTATE;
143+
pinConfigBools.gpOutcfg = true;
144+
}
85145

86-
if( mode & (PinMode)InAuto ){ obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_AUTO; }
87-
if( mode & (PinMode)InNone ){ obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_NONE; }
88-
if( mode & (PinMode)InEnable ){ obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE; }
146+
if (mode & (PinMode)InAuto)
147+
{
148+
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_AUTO;
149+
pinConfigBools.gpInput = true;
150+
}
151+
if (mode & (PinMode)InNone)
152+
{
153+
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_NONE;
154+
pinConfigBools.gpInput = true;
155+
}
156+
if (mode & (PinMode)InEnable)
157+
{
158+
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE;
159+
pinConfigBools.gpInput = true;
160+
}
89161

90-
if( mode & (PinMode)ReadPin ){ obj->cfg.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_READPIN; }
91-
if( mode & (PinMode)ReadZero ){ obj->cfg.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_ZERO; }
162+
if (mode & (PinMode)ReadPin)
163+
{
164+
obj->cfg.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_READPIN;
165+
pinConfigBools.gprdZero = true;
166+
}
167+
if (mode & (PinMode)ReadZero)
168+
{
169+
obj->cfg.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_ZERO;
170+
pinConfigBools.gprdZero = true;
171+
}
92172

93-
am_hal_gpio_pinconfig((uint32_t)(obj->pad), obj->cfg); // apply configuration
173+
ap3_hal_gpio_pinconfig_partial((uint32_t)(obj->pad), obj->cfg, pinConfigBools); //padRegMsk.byte, GPConfigMsk.byte, padAltCfgMsk.byte); // apply configuration
94174
}
95175

96176
/** Set the pin direction
97177
*
98178
* @param obj The GPIO object (must be connected)
99179
* @param direction The pin direction to be set
100180
*/
101-
void gpio_dir(gpio_t *obj, PinDirection direction){
181+
void gpio_dir(gpio_t *obj, PinDirection direction)
182+
{
102183
MBED_ASSERT(gpio_is_connected(obj));
103184
MBED_ASSERT(direction < (PinDirection)PIN_DIR_ELEMENTS);
185+
am_hal_gpio_pincfg_allow_t pinConfigBools;
104186

105-
if( direction == (PinDirection)PIN_INPUT ){
187+
if (direction == (PinDirection)PIN_INPUT)
188+
{
106189
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE;
190+
pinConfigBools.gpInput = true;
107191
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_DISABLE;
192+
pinConfigBools.gpOutcfg = true;
108193
}
109-
else if ( direction == (PinDirection)PIN_OUTPUT ){
194+
else if (direction == (PinDirection)PIN_OUTPUT)
195+
{
110196
obj->cfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL;
197+
pinConfigBools.gpOutcfg = false;
111198
obj->cfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
112-
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE;
113-
obj->cfg.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_READPIN;
114-
}else{
199+
obj->cfg.eGPInput = AM_HAL_GPIO_PIN_INPUT_NONE;
200+
pinConfigBools.gpInput = false;
201+
}
202+
else
203+
{
115204
MBED_ASSERT(false);
116205
}
117206

118-
am_hal_gpio_pinconfig((uint32_t)(obj->pad), obj->cfg); // apply configuration
207+
ap3_hal_gpio_pinconfig_partial((uint32_t)(obj->pad), obj->cfg, pinConfigBools); //padRegMsk.byte, GPConfigMsk.byte, padAltCfgMsk.byte); // apply configuration
119208
}
120209

121210
/** Set the output value
122211
*
123212
* @param obj The GPIO object (must be connected)
124213
* @param value The value to be set
125214
*/
126-
void gpio_write(gpio_t *obj, int value){
215+
void gpio_write(gpio_t *obj, int value)
216+
{
127217
MBED_ASSERT(gpio_is_connected(obj));
128218
(value) ? am_hal_gpio_output_set(obj->pad) : am_hal_gpio_output_clear(obj->pad);
129219
}
@@ -133,7 +223,8 @@ void gpio_write(gpio_t *obj, int value){
133223
* @param obj The GPIO object (must be connected)
134224
* @return An integer value 1 or 0
135225
*/
136-
int gpio_read(gpio_t *obj){
226+
int gpio_read(gpio_t *obj)
227+
{
137228
MBED_ASSERT(gpio_is_connected(obj));
138229
uint32_t ui32BaseAddr = (obj->pad) / 8;
139230
uint32_t ui32BaseShift = (((obj->pad) % 8) * 4) + 1;
@@ -153,7 +244,8 @@ int gpio_read(gpio_t *obj){
153244
*
154245
* @return PinMap array
155246
*/
156-
const PinMap *gpio_pinmap(void){
247+
const PinMap *gpio_pinmap(void)
248+
{
157249
MBED_ASSERT(false);
158250
return NULL;
159-
}
251+
}

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_irq_api.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ extern "C"
99
#endif
1010

1111
uint32_t ap3_gpio_enable_interrupts(uint32_t ui32Pin, am_hal_gpio_intdir_e eIntDir);
12-
1312
/** GPIO IRQ HAL structure. gpio_irq_s is declared in the target's HAL
1413
*/
1514
typedef struct gpio_irq_s gpio_irq_t;
@@ -46,21 +45,22 @@ extern "C"
4645
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id)
4746
{
4847
//grab the correct irq control object
49-
ap3_gpio_irq_control_t control = gpio_irq_control[pin];
48+
ap3_gpio_irq_control_t *control = &gpio_irq_control[pin];
5049

5150
//Register locally
52-
control.pad = pin;
53-
control.handler = handler;
54-
control.id = id;
55-
control.events = IRQ_NONE;
51+
control->pad = pin;
52+
control->handler = handler;
53+
control->id = id;
54+
control->events = IRQ_NONE;
5655

5756
//Attach to object
58-
obj->control = &control;
57+
obj->control = control;
5958

6059
//Make sure the interrupt is set to none to reflect the new events value
61-
ap3_gpio_enable_interrupts(control.pad, AM_HAL_GPIO_PIN_INTDIR_NONE);
60+
ap3_gpio_enable_interrupts(control->pad, AM_HAL_GPIO_PIN_INTDIR_NONE);
6261

6362
//Enable GPIO IRQ's in the NVIC
63+
gpio_irq_enable(obj);
6464
NVIC_SetVector((IRQn_Type)GPIO_IRQn, (uint32_t)am_gpio_isr);
6565
NVIC_EnableIRQ((IRQn_Type)GPIO_IRQn);
6666
return 0;
@@ -74,7 +74,7 @@ extern "C"
7474
uint32_t pinNum = 0;
7575
while (gpio_int_mask)
7676
{
77-
if (gpio_int_mask |= 0x0000000000000001)
77+
if (gpio_int_mask & 0x0000000000000001)
7878
{
7979
am_hal_gpio_interrupt_clear(AM_HAL_GPIO_BIT(pinNum));
8080
ap3_gpio_irq_control_t irq_ctrl = gpio_irq_control[pinNum];
@@ -155,6 +155,8 @@ extern "C"
155155
/**@}*/
156156
uint32_t ap3_gpio_enable_interrupts(uint32_t ui32Pin, am_hal_gpio_intdir_e eIntDir)
157157
{
158+
//GPConfigReg_t GPCfgMask = {.bit.INCFG = 1, .bit.INTD = 1};
159+
// uint32_t ap3_hal_gpio_pinconfig_partial(ui32Pin, bfGpioCfg, 0, uint8_t GPCfgMask, 0);
158160
uint32_t ui32Padreg, ui32AltPadCfg, ui32GPCfg;
159161
uint32_t ui32Funcsel, ui32PowerSw;
160162
bool bClearEnable = false;

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/objects_gpio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ extern "C"
122122
ap3_gpio_irq_control_t *control;
123123
} gpio_irq_s;
124124

125+
#define AP3_PINCFG_FUNCSEL_GPIO 3
126+
125127
#define AP3_GPIO_MAX_PADS (50)
126128
#define PADREG_FLD_76_S 6
127129
#define PADREG_FLD_FNSEL_S 3

targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void uart_configure_pin_function(PinName pin, UARTName uart, const PinMap *map);
133133
* @param rx The RX pin name
134134
*/
135135
void serial_init(serial_t *obj, PinName tx, PinName rx)
136-
{
136+
{ //TODO: we should be able to call this multiple times
137137
// determine the UART to use
138138
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, serial_tx_pinmap());
139139
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, serial_rx_pinmap());

0 commit comments

Comments
 (0)