From 3f3f041f879459df512e2ba835e8cb14a71a2ee5 Mon Sep 17 00:00:00 2001 From: Jake Carter Date: Tue, 10 Sep 2024 18:13:34 -0600 Subject: [PATCH 1/6] Add ability to set PB polarity --- Libraries/MiscDrivers/PushButton/pb.c | 33 +++++++++++++++++++++++++-- Libraries/MiscDrivers/PushButton/pb.h | 12 ++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Libraries/MiscDrivers/PushButton/pb.c b/Libraries/MiscDrivers/PushButton/pb.c index 017614621cf..3524ad14d7f 100644 --- a/Libraries/MiscDrivers/PushButton/pb.c +++ b/Libraries/MiscDrivers/PushButton/pb.c @@ -19,11 +19,15 @@ ******************************************************************************/ #include +#include #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) { @@ -37,9 +41,26 @@ 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) +{ + g_pb_polarity[pb] = polarity; + return E_NO_ERROR; +} + /******************************************************************************/ int PB_RegisterCallback(unsigned int pb, pb_callback callback) { @@ -52,7 +73,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 { @@ -113,7 +138,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; } //****************************************************************************** diff --git a/Libraries/MiscDrivers/PushButton/pb.h b/Libraries/MiscDrivers/PushButton/pb.h index 2a78f41f690..3f172098242 100644 --- a/Libraries/MiscDrivers/PushButton/pb.h +++ b/Libraries/MiscDrivers/PushButton/pb.h @@ -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 **** */ /** @@ -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: From 582da65a18bb064f5f8a41fc95cb7dcd44cea33a Mon Sep 17 00:00:00 2001 From: Jake Carter Date: Tue, 10 Sep 2024 18:14:52 -0600 Subject: [PATCH 2/6] Set MAX32690 APARD PB polarity HIGH --- Libraries/Boards/MAX32690/APARD/Source/board.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Libraries/Boards/MAX32690/APARD/Source/board.c b/Libraries/Boards/MAX32690/APARD/Source/board.c index a239698e495..0889bad3443 100644 --- a/Libraries/Boards/MAX32690/APARD/Source/board.c +++ b/Libraries/Boards/MAX32690/APARD/Source/board.c @@ -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; From e8ee82f95387aba70df680e31b25b1baf066fa10 Mon Sep 17 00:00:00 2001 From: Jake Carter Date: Tue, 10 Sep 2024 18:15:48 -0600 Subject: [PATCH 3/6] clang-format --- Libraries/MiscDrivers/PushButton/pb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Libraries/MiscDrivers/PushButton/pb.c b/Libraries/MiscDrivers/PushButton/pb.c index 3524ad14d7f..553f71c6d0f 100644 --- a/Libraries/MiscDrivers/PushButton/pb.c +++ b/Libraries/MiscDrivers/PushButton/pb.c @@ -42,9 +42,9 @@ 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... + 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++) { From 9ac2a8158ba87d9989de6eac6c3273b5f322563c Mon Sep 17 00:00:00 2001 From: Jake Carter Date: Tue, 10 Sep 2024 18:23:45 -0600 Subject: [PATCH 4/6] Protect against array overflow --- Libraries/MiscDrivers/PushButton/pb.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Libraries/MiscDrivers/PushButton/pb.c b/Libraries/MiscDrivers/PushButton/pb.c index 553f71c6d0f..20684c201ff 100644 --- a/Libraries/MiscDrivers/PushButton/pb.c +++ b/Libraries/MiscDrivers/PushButton/pb.c @@ -57,6 +57,10 @@ int PB_Init(void) /******************************************************************************/ 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; } From b654431a557a7c6a1e1024c72a63462807dbff6d Mon Sep 17 00:00:00 2001 From: Woo Date: Tue, 24 Sep 2024 16:19:43 -0500 Subject: [PATCH 5/6] Fix Flash 0 page size in Bootloader example --- Examples/MAX32690/Bluetooth/Bootloader/README.md | 2 +- .../MAX32690/Bluetooth/Bootloader/boot_lower.S | 2 +- .../MAX32690/Bluetooth/Bootloader/bootloader.ld | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Examples/MAX32690/Bluetooth/Bootloader/README.md b/Examples/MAX32690/Bluetooth/Bootloader/README.md index e988d5bf3ac..06cdf6c11e0 100644 --- a/Examples/MAX32690/Bluetooth/Bootloader/README.md +++ b/Examples/MAX32690/Bluetooth/Bootloader/README.md @@ -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 diff --git a/Examples/MAX32690/Bluetooth/Bootloader/boot_lower.S b/Examples/MAX32690/Bluetooth/Bootloader/boot_lower.S index 6a2646f91c4..1dff366bfd1 100644 --- a/Examples/MAX32690/Bluetooth/Bootloader/boot_lower.S +++ b/Examples/MAX32690/Bluetooth/Bootloader/boot_lower.S @@ -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 */ diff --git a/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld b/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld index 564c592524c..a79cf502d07 100644 --- a/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld +++ b/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld @@ -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 page 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 } @@ -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 From 55454f90bfd3db58ddec4fecc84f1024b5ac6ad9 Mon Sep 17 00:00:00 2001 From: Sihyung Woo <75494566+sihyung-maxim@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:27:47 -0500 Subject: [PATCH 6/6] Fix comments typo --- Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld b/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld index a79cf502d07..cb0fdfd4d45 100644 --- a/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld +++ b/Examples/MAX32690/Bluetooth/Bootloader/bootloader.ld @@ -19,7 +19,7 @@ ******************************************************************************/ /* Note: Flash 0 page size is 16KB (0x4000). Flash 1 page size is 8KB (0x2000) */ -BOOTLOADER_ORIGIN = 0x10000000; /* Bootloader resides in page 0 */ +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 */