From 4006fca8adfa087ab63a0f00268b16f4ff5519b7 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 24 Jul 2025 00:10:37 -0700 Subject: [PATCH 1/5] Fix some Apollo3 UART issues, fix test skipped message not printing --- features/frameworks/unity/source/unity.c | 1 + .../TARGET_Apollo3/device/serial_api.c | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/features/frameworks/unity/source/unity.c b/features/frameworks/unity/source/unity.c index a3a823687c5..c71bc4d02aa 100644 --- a/features/frameworks/unity/source/unity.c +++ b/features/frameworks/unity/source/unity.c @@ -1240,6 +1240,7 @@ void UnitySkipPrint(const char* msg, const UNITY_LINE_TYPE line) UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } + UNITY_OUTPUT_CHAR('\n'); } /*-----------------------------------------------*/ diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c index 2aa68311e2e..8eafcf93fc4 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c @@ -26,6 +26,7 @@ #if DEVICE_SERIAL #include "serial_api.h" +#include "mbed_wait_api.h" #include "mbed_assert.h" #include "PeripheralPins.h" @@ -142,7 +143,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) obj->serial.uart_control->cfg.ui32RxBufferSize = 0; obj->serial.uart_control->cfg.ui32TxBufferSize = 0; - obj->serial.uart_control->cfg.ui32FifoLevels = AM_HAL_UART_RX_FIFO_7_8; + // Mbed expects an interrupt whenever we have at least one char in the Rx FIFO. + obj->serial.uart_control->cfg.ui32FifoLevels = AM_HAL_UART_RX_FIFO_1_8; // start UART instance MBED_ASSERT(am_hal_uart_initialize(uart, &(obj->serial.uart_control->handle)) == AM_HAL_STATUS_SUCCESS); @@ -244,6 +246,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) break; } // NVIC_SetVector(uart_irqs[obj->serial.index], vector); + NVIC_ClearPendingIRQ((IRQn_Type)(UART0_IRQn + obj->serial.uart_control->inst)); NVIC_EnableIRQ((IRQn_Type)(UART0_IRQn + obj->serial.uart_control->inst)); } else { // disable switch (irq) { @@ -275,6 +278,20 @@ int serial_getc(serial_t *obj) do { am_hal_uart_transfer(obj->serial.uart_control->handle, &am_hal_uart_xfer_read_single); + + // Seeing very odd behavior with this uart, where digital glitches on the line can cause the + // framing error bit to set and then cause at least some of the data within the Rx FIFO to be + // deleted. This causes an infinite hang, as Mbed requires serial_getc() to return a character + // if serial_readable() returns true. This UART is not well documented, so unable to say if this + // is an errata or some sort of odd design choice. + // To avoid this, if we did not get any data and the framing error bit is set, simply clear the flag + // and return an arbitrary character. This is a little awkward but prevents a hard-to-debug hang. + if(bytes_read == 0 && UARTn(obj->serial.uart_control->inst)->RSR_b.FESTAT) + { + UARTn(obj->serial.uart_control->inst)->RSR_b.FESTAT = 0; + return 'x'; + } + } while (bytes_read == 0); return (int)rx_c; @@ -372,6 +389,8 @@ const PinMap *serial_rts_pinmap(void) } #endif +static volatile uint32_t foo = 0; + static inline void uart_irq(uint32_t instance) { void *handle = ap3_uart_control[instance].handle; From 9b06a99a5c1e6b434832c0252e3bafff986e9a96 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 24 Jul 2025 00:57:41 -0700 Subject: [PATCH 2/5] Augh fix memap, fix referencing nonexistent target --- targets/targets.json5 | 2 +- tools/cmake/mbed_target_functions.cmake | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/targets/targets.json5 b/targets/targets.json5 index 7101154834c..ebe038f828c 100644 --- a/targets/targets.json5 +++ b/targets/targets.json5 @@ -3594,7 +3594,7 @@ mode is recommended for target MCUs with small amounts of flash and RAM.", // DISCO_H747I's attributes. "inherits": [ "MCU_STM32H747xI_CM7", - "DISCO_H747" + "DISCO_H747I" ] }, "DISCO_H747I_CM4": { diff --git a/tools/cmake/mbed_target_functions.cmake b/tools/cmake/mbed_target_functions.cmake index 10d1ad484c3..e785e06139a 100644 --- a/tools/cmake/mbed_target_functions.cmake +++ b/tools/cmake/mbed_target_functions.cmake @@ -187,9 +187,7 @@ function(mbed_set_post_build target) mbed_post_build_function(${target}) endif() - if(HAVE_MEMAP_DEPS) - mbed_generate_map_file(${target}) - endif() + mbed_generate_map_file(${target}) # Give chance to adjust MBED_UPLOAD_LAUNCH_COMMANDS or MBED_UPLOAD_RESTART_COMMANDS # for debug launch From 5557f664d91f5d1e54c3f21e7339aea296eb5845 Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Thu, 24 Jul 2025 21:24:05 -0700 Subject: [PATCH 3/5] Fix arduino pinmap test failing --- .../TARGET_Apollo3/device/PeripheralPins.c | 65 +++++++++++++++++++ .../TARGET_Apollo3/device/PeripheralPins.h | 3 + .../TARGET_Apollo3/device/gpio_api.c | 4 +- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.c b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.c index 89781cb10cc..978770e760d 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.c +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.c @@ -312,3 +312,68 @@ const PinMap PinMap_PWM_OUT[] = { {NC, NC, 0} }; + +/************ GPIO ***************/ + +// Note that this is only used for testing, so that the test knows what are valid GPIO pins. +// It's not used in normal usage. +// Also, only the "pin" field is significant here. Other fields are don't cares. + +const PinMap PinMap_GPIO[] = { + {IO_0, 0, 0}, + {IO_1, 0, 0}, + {IO_2, 0, 0}, + {IO_3, 0, 0}, + {IO_4, 0, 0}, + {IO_5, 0, 0}, + {IO_6, 0, 0}, + {IO_7, 0, 0}, + {IO_8, 0, 0}, + {IO_9, 0, 0}, + {IO_10, 0, 0}, + {IO_11, 0, 0}, + {IO_12, 0, 0}, + {IO_13, 0, 0}, + {IO_14, 0, 0}, + {IO_15, 0, 0}, + {IO_16, 0, 0}, + {IO_17, 0, 0}, + {IO_18, 0, 0}, + {IO_19, 0, 0}, + {IO_20, 0, 0}, + {IO_21, 0, 0}, + {IO_22, 0, 0}, + {IO_23, 0, 0}, + {IO_24, 0, 0}, + {IO_25, 0, 0}, + {IO_26, 0, 0}, + {IO_27, 0, 0}, + {IO_28, 0, 0}, + {IO_29, 0, 0}, + {IO_39, 0, 0}, + {IO_40, 0, 0}, + {IO_41, 0, 0}, + {IO_44, 0, 0}, + {IO_47, 0, 0}, + {IO_48, 0, 0}, + {IO_49, 0, 0}, + + // Apollo3 I/O pins - BGA package only + {IO_30, 0, 0}, + {IO_31, 0, 0}, + {IO_32, 0, 0}, + {IO_33, 0, 0}, + {IO_34, 0, 0}, + {IO_35, 0, 0}, + {IO_36, 0, 0}, + {IO_37, 0, 0}, + {IO_38, 0, 0}, + {IO_42, 0, 0}, + {IO_43, 0, 0}, + {IO_45, 0, 0}, + {IO_46, 0, 0}, + + {NC, NC, 0} +}; + + diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.h b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.h index f0d1c3c54b4..67889da03ad 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.h +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/PeripheralPins.h @@ -23,6 +23,7 @@ #include "pinmap.h" #include "PeripheralNames.h" + //*** I2C *** #if DEVICE_I2C extern const PinMap PinMap_I2C_SDA[]; @@ -70,4 +71,6 @@ extern const PinMap PinMap_QSPI_DATA2[]; extern const PinMap PinMap_QSPI_DATA3[]; #endif +extern const PinMap PinMap_GPIO[]; + #endif diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_api.c b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_api.c index 85c1deda7d3..8565e5943b3 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_api.c +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/gpio_api.c @@ -23,6 +23,7 @@ #include "mbed_assert.h" #include "gpio_api.h" +#include "PeripheralPins.h" /** Set the given pin as GPIO * @@ -224,6 +225,5 @@ int gpio_read(gpio_t *obj) */ const PinMap *gpio_pinmap(void) { - MBED_ASSERT(false); - return NULL; + return PinMap_GPIO; } From 98417eadd13c71ae6eac495414e9533763b01dae Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Fri, 25 Jul 2025 01:03:31 -0700 Subject: [PATCH 4/5] Fix another hang bug, fix not printing certain test output --- .../TARGET_Apollo3/device/serial_api.c | 9 +++++++++ .../test/host_tests_conn_proxy/conn_proxy.py | 2 ++ 2 files changed, 11 insertions(+) diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c index 8eafcf93fc4..c717806f261 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c @@ -292,6 +292,15 @@ int serial_getc(serial_t *obj) return 'x'; } + // Similar to above but with the overflow flag. Without this logic we can hang when receiving + // at 921600 baud. Oddly, the overflow flag in RSR does not seem to be reliable, but the overflow + // flag in IES seems to be. Not sure why this UART has two overflow flags in the first place, smh... + if(bytes_read == 0 && UARTn(obj->serial.uart_control->inst)->IES_b.OERIS) + { + UARTn(obj->serial.uart_control->inst)->IEC_b.OEIC = 1; + return 'x'; + } + } while (bytes_read == 0); return (int)rx_c; diff --git a/tools/python/mbed_os_tools/test/host_tests_conn_proxy/conn_proxy.py b/tools/python/mbed_os_tools/test/host_tests_conn_proxy/conn_proxy.py index 6725079db4f..4382acaaf3d 100644 --- a/tools/python/mbed_os_tools/test/host_tests_conn_proxy/conn_proxy.py +++ b/tools/python/mbed_os_tools/test/host_tests_conn_proxy/conn_proxy.py @@ -62,9 +62,11 @@ def append(self, payload: bytes): before = line[:pos] after = line[pos + len(match):] if len(before) > 0: + logger.prn_rxd(before) discarded.append(before) if len(after) > 0: # not a K,V pair part + logger.prn_rxd(after) discarded.append(after) logger.prn_inf("found KV pair in stream: {{%s;%s}}, queued..."% (key, value)) else: From 19b1646621c687db944d7083a403c70a37af92bb Mon Sep 17 00:00:00 2001 From: Jamie Smith Date: Fri, 25 Jul 2025 09:14:12 -0700 Subject: [PATCH 5/5] Remove some unused code --- .../TARGET_Apollo3/device/serial_api.c | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c index c717806f261..5a771e910a4 100644 --- a/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c +++ b/targets/TARGET_Ambiq_Micro/TARGET_Apollo3/device/serial_api.c @@ -360,21 +360,6 @@ void serial_pinout_tx(PinName tx) MBED_ASSERT(0); } -#if DEVICE_SERIAL_FC - -void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow) -{ - // todo: - MBED_ASSERT(0); -} - -void serial_set_flow_control_direct(serial_t *obj, FlowControl type, const serial_fc_pinmap_t *pinmap) -{ - // todo: - MBED_ASSERT(0); -} -#endif - const PinMap *serial_tx_pinmap(void) { return PinMap_UART_TX; @@ -385,21 +370,6 @@ const PinMap *serial_rx_pinmap(void) return PinMap_UART_RX; } -#if DEVICE_SERIAL_FC - -const PinMap *serial_cts_pinmap(void) -{ - return PinMap_UART_CTS; -} - -const PinMap *serial_rts_pinmap(void) -{ - return PinMap_UART_RTS; -} -#endif - -static volatile uint32_t foo = 0; - static inline void uart_irq(uint32_t instance) { void *handle = ap3_uart_control[instance].handle;