Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MiscDrivers): Add Ability to Set Pushbutton Polarity #1166

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/MAX32690/Bluetooth/Bootloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ the main flash section is erased and replaced with the update image. If no valid
is identified, the Bootloader will boot the exiting image in the main flash space.

__0x10000000__: Bootloader
__0x10004000__: Main flash space
__0x10008000__: Main flash space
__0x10300000__: Update flash space

## Software
Expand Down
2 changes: 1 addition & 1 deletion Examples/MAX32690/Bluetooth/Bootloader/boot_lower.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/* Boot from the lower flash array */
Boot_Lower:

ldr r0,=0x10004000 /* Address for main flash image */
ldr r0,=0x10008000 /* Address for main flash image */
ldr r1,=0xE000ED08 /* Address for SCB_VTOR_REG */

/* First 32-bit word in image is initial stack pointer */
Expand Down
16 changes: 9 additions & 7 deletions Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
*
******************************************************************************/

BOOTLOADER_ORIGIN = 0x10000000;
BOOTLOADER_LEN = 0x4000;
FLASH_SECTION_LEN = 0x40000 - BOOTLOADER_LEN;
/* Note: Flash 0 page size is 16KB (0x4000). Flash 1 page size is 8KB (0x2000) */
BOOTLOADER_ORIGIN = 0x10000000; /* Bootloader resides in Flash 0 */
BOOTLOADER_LEN = 0x8000; /* 2 Flash 0 pages length (total 32KB) */
FLASH0_SECTION_LEN = 0x300000 - BOOTLOADER_LEN; /* 3MB - 2 pages (16KB/page) */
FLASH1_SECTION_LEN = 0x40000; /* 256KB */
FLASH0_ORIGIN = BOOTLOADER_ORIGIN + BOOTLOADER_LEN;

MEMORY {
FLASH (rx) : ORIGIN = BOOTLOADER_ORIGIN, LENGTH = BOOTLOADER_LEN
FLASH0 (rx) : ORIGIN = FLASH0_ORIGIN, LENGTH = FLASH_SECTION_LEN
FLASH1 (rx) : ORIGIN = 0x10300000, LENGTH = FLASH_SECTION_LEN
FLASH0 (rx) : ORIGIN = FLASH0_ORIGIN, LENGTH = FLASH0_SECTION_LEN
FLASH1 (rx) : ORIGIN = 0x10300000, LENGTH = FLASH1_SECTION_LEN
SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x20000
}

Expand All @@ -50,14 +52,14 @@ SECTIONS {
.flash0 (NOLOAD) :
{
_flash0 = ALIGN(., 4);
. = . + FLASH_SECTION_LEN;
. = . + FLASH0_SECTION_LEN;
_eflash0 = ALIGN(., 4);
} > FLASH0

.flash1 (NOLOAD) :
{
_flash1 = ALIGN(., 4);
. = . + FLASH_SECTION_LEN;
. = . + FLASH1_SECTION_LEN;
_eflash1 = ALIGN(., 4);
} > FLASH1

Expand Down
2 changes: 2 additions & 0 deletions Libraries/Boards/MAX32690/APARD/Source/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ int Board_Init(void)
return err;
}

PB_Set_Polarity(0, PB_POLARITY_HIGH);

if ((err = LED_Init()) != E_NO_ERROR) {
MXC_ASSERT_FAIL();
return err;
Expand Down
37 changes: 35 additions & 2 deletions Libraries/MiscDrivers/PushButton/pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
******************************************************************************/

#include <stddef.h>
#include <stdlib.h>
#include "board.h"
#include "mxc_device.h"
#include "mxc_assert.h"
#include "pb.h"

// State array for holding PB polarity settings
static pb_polarity_t *g_pb_polarity = NULL;

/******************************************************************************/
int PB_Init(void)
{
Expand All @@ -37,9 +41,30 @@ int PB_Init(void)
}
}

if (g_pb_polarity == NULL) {
g_pb_polarity = malloc(num_pbs * sizeof(pb_polarity_t));
// Note we have to use malloc here because we decided to make "num_pbs" a
// const variable instead of a compiler definition...
}

for (unsigned int i = 0; i < num_pbs; i++) {
g_pb_polarity[i] = PB_POLARITY_LOW;
}

return retval;
}

/******************************************************************************/
int PB_Set_Polarity(unsigned int pb, pb_polarity_t polarity)
{
if (pb >= num_pbs) {
return E_BAD_PARAM;
}

g_pb_polarity[pb] = polarity;
return E_NO_ERROR;
}

/******************************************************************************/
int PB_RegisterCallback(unsigned int pb, pb_callback callback)
{
Expand All @@ -52,7 +77,11 @@ int PB_RegisterCallback(unsigned int pb, pb_callback callback)
MXC_GPIO_RegisterCallback(&pb_pin[pb], callback, (void *)pb);

// Configure and enable interrupt
MXC_GPIO_IntConfig(&pb_pin[pb], MXC_GPIO_INT_FALLING);
mxc_gpio_int_pol_t interrupt_polarity = MXC_GPIO_INT_FALLING;
if (g_pb_polarity[pb] == PB_POLARITY_HIGH) {
interrupt_polarity = MXC_GPIO_INT_RISING;
}
MXC_GPIO_IntConfig(&pb_pin[pb], interrupt_polarity);
MXC_GPIO_EnableInt(pb_pin[pb].port, pb_pin[pb].mask);
NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(MXC_GPIO_GET_IDX(pb_pin[pb].port)));
} else {
Expand Down Expand Up @@ -113,7 +142,11 @@ void PB_IntClear(unsigned int pb)
int PB_Get(unsigned int pb)
{
MXC_ASSERT(pb < num_pbs);
return !MXC_GPIO_InGet(pb_pin[pb].port, pb_pin[pb].mask);
uint32_t state = MXC_GPIO_InGet(pb_pin[pb].port, pb_pin[pb].mask);
if (g_pb_polarity[pb] == PB_POLARITY_LOW) {
state = !state;
}
return state;
}

//******************************************************************************
Expand Down
12 changes: 12 additions & 0 deletions Libraries/MiscDrivers/PushButton/pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ extern "C" {
extern const mxc_gpio_cfg_t pb_pin[];
extern const unsigned int num_pbs;

typedef enum pb_polarity {
PB_POLARITY_LOW, ///< Pushing the button sends the signal LOW. When the button is open, the signal is HIGH
PB_POLARITY_HIGH ///< Pushing the button sends the signal HIGH. When the button is open, the signal is LOW.
} pb_polarity_t;

/* **** Function Prototypes **** */

/**
Expand All @@ -50,6 +55,13 @@ extern const unsigned int num_pbs;
*/
int PB_Init(void);

/**
* @brief Set the voltage polarity of the pushbutton.
* @param pb Pushbutton to configure
* @param polarity Desired polarity of the push-button (i.e. what voltage level the signal goes to when the button is pushed).
*/
int PB_Set_Polarity(unsigned int pb, pb_polarity_t polarity);

/**
* Type alias @c pb_callback for the push button callback.
* @details The function is of type:
Expand Down
Loading