Skip to content

Commit c7d1c32

Browse files
Upload method support for Nucleo WL55JC, fix common tickers test for STM32 (#522)
1 parent 8f48d0d commit c7d1c32

File tree

8 files changed

+89
-15
lines changed

8 files changed

+89
-15
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@ cmake-variants.yaml
107107
CMakeUserPresets.json
108108

109109
# CLion
110-
cmake-build-*/
110+
cmake-build-*/
111+
112+
# Test executor runs
113+
test-executor-build/
114+
test-executor-results/

hal/include/hal/lp_ticker_api.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ uint32_t lp_ticker_read(void);
188188
* we want to wake up far in the future, we will instead set a wakeup for about (rollover period/2) ticks
189189
* in the future, then reschedule the timer for the correct time.
190190
*
191+
* @note Some hardware implementations do not support setting an interrupt for after the ticker rolls over
192+
* (e.g. the STM32 LPTIM, which implements a >= comparison for interrupts rather than an == comparison).
193+
* For these implementations, it is acceptable to schedule the interrupt for the time that the ticker
194+
* rolls over. Higher level code will then reschedule the interrupt for the correct time.
195+
*
191196
* Calling this function with timestamp of more than the supported
192197
* number of bits returned by ::lp_ticker_get_info results in undefined
193198
* behavior.

hal/include/hal/us_ticker_api.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ uint32_t (us_ticker_read)(void);
247247
* we want to wake up far in the future, we will instead set a wakeup for about (rollover period/2) ticks
248248
* in the future, then reschedule the timer for the correct time.
249249
*
250+
* @note Some hardware implementations do not support setting an interrupt for after the ticker rolls over
251+
* (e.g. the STM32 LPTIM, which implements a >= comparison for interrupts rather than an == comparison).
252+
* For these implementations, it is acceptable to schedule the interrupt for the time that the ticker
253+
* rolls over. Higher level code will then reschedule the interrupt for the correct time.
254+
*
250255
* Calling this function with timestamp of more than the supported
251256
* number of bits returned by ::us_ticker_get_info results in undefined
252257
* behavior.

hal/tests/TESTS/mbed_hal/common_tickers/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ void ticker_overflow_test(void)
398398

399399
const uint32_t after_overflow = intf->read();
400400

401-
// Interrupt should NOT have fired yet
402-
TEST_ASSERT_EQUAL_INT(0, intFlag);
401+
// Note: at this point the interrupt may or may not have fired, depending on implementation.
402+
// See lp_ticker_set_interrupt() docs for details.
403403

404404
/* Now we are just after overflow. Wait a while assuming that ticker still counts. */
405405
while (intf->read() < isrTicksAfterOverflow + TICKER_DELTA) {

hal/tests/TESTS/mbed_hal/rtc_reset/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if(NOT "DEVICE_RTC=1" IN_LIST MBED_TARGET_DEFINITIONS)
55
set(TEST_SKIPPED "RTC is not supported for this target")
66
endif()
77

8-
if(NOT "TARGET_K64F" IN_LIST MBED_TARGET_DEFINITIONS)
8+
if("TARGET_K64F" IN_LIST MBED_TARGET_DEFINITIONS)
99
# Currently this test works OK, but causes the next 5-10 programming operations with OpenOCD to fail
1010
set(TEST_SKIPPED "This test causes issues with this target's debug interface")
1111
endif()

targets/TARGET_STM/lp_ticker.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123

124124
#endif // (DUAL_CORE) && (TARGET_STM32H7)
125125

126-
126+
#define LPTIM_PERIOD 0xFFFF
127127

128128
LPTIM_HandleTypeDef LptimHandle;
129129

@@ -267,7 +267,7 @@ void lp_ticker_init(void)
267267
LptimHandle.Init.Trigger.ActiveEdge = LPTIM_ACTIVEEDGE_FALLING;
268268
#endif
269269
#if defined(TARGET_STM32U5) || defined(TARGET_STM32U0)
270-
LptimHandle.Init.Period = 0xFFFF;
270+
LptimHandle.Init.Period = LPTIM_PERIOD;
271271
#endif
272272
#if defined (LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION)
273273
LptimHandle.Init.Trigger.SampleTime = LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION;
@@ -333,7 +333,7 @@ void lp_ticker_init(void)
333333
__HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_CMPM);
334334
__HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_CMPOK);
335335

336-
HAL_LPTIM_Counter_Start(&LptimHandle, 0xFFFF);
336+
HAL_LPTIM_Counter_Start(&LptimHandle, LPTIM_PERIOD);
337337

338338
/* Need to write a compare value in order to get LPTIM_FLAG_CMPOK in set_interrupt */
339339
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK);
@@ -465,9 +465,9 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
465465

466466
/* If this target timestamp is close to the roll over of the ticker counter
467467
* and current tick is also close to the roll over, then we are in danger zone.*/
468-
if (((0xFFFF - LP_TIMER_SAFE_GUARD < timestamp) || (timestamp < LP_TIMER_SAFE_GUARD)) && (0xFFFA < last_read_counter)) {
468+
if (((LPTIM_PERIOD - LP_TIMER_SAFE_GUARD < timestamp) || (timestamp < LP_TIMER_SAFE_GUARD)) && (0xFFFA < last_read_counter)) {
469469
roll_over_flag = true;
470-
/* Change the lp_delayed_counter buffer in that way so the value of (0xFFFF - LP_TIMER_SAFE_GUARD) is equal to 0.
470+
/* Change the lp_delayed_counter buffer in that way so the value of (LPTIM_PERIOD - LP_TIMER_SAFE_GUARD) is equal to 0.
471471
* By doing this it is easy to check if the value of timestamp get outdated by delaying its programming
472472
* For example if LP_TIMER_SAFE_GUARD is set to 5
473473
* (0xFFFA + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 0
@@ -499,23 +499,26 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
499499
}
500500
}
501501

502-
/* Then check if this target timestamp is not in the past, or close to wrap-around
502+
/* Then check if this target timestamp is in the past and we are not close to wrap-around.
503503
* Let's assume last_read_counter = 0xFFFC, and we want to program timestamp = 0x100
504504
* The interrupt will not fire before the CMPOK flag is OK, so there are 2 cases:
505505
* in case CMPOK flag is set by HW after or at wrap-around, then this will fire only @0x100
506506
* in case CMPOK flag is set before, it will indeed fire early, as for the wrap-around case.
507+
* (this is because the comparison is implemented as ">= CMP" rather than "== CMP" as indicated
508+
* by the reference manual, see https://community.st.com/t5/stm32-mcus-embedded-software/lptim-compare-interruption-sometimes-is-triggered-when-it-should/td-p/85513 )
509+
*
507510
* But that will take at least 3 cycles and the interrupt fires at the end of a cycle.
508511
* In our case 0xFFFC + 3 => at the transition between 0xFFFF and 0.
509512
* If last_read_counter was 0xFFFB, it should be at the transition between 0xFFFE and 0xFFFF.
510513
* There might be crossing cases where it would also fire @ 0xFFFE, but by the time we read the counter,
511514
* it may already have moved to the next one, so for now we've taken this as margin of error.
512515
*/
513-
if ((timestamp < last_read_counter) && (last_read_counter <= (0xFFFF - LP_TIMER_SAFE_GUARD))) {
514-
/* Workaround, because limitation */
516+
if ((timestamp < last_read_counter) && (last_read_counter <= (LPTIM_PERIOD - LP_TIMER_SAFE_GUARD))) {
517+
/* Workaround, because limitation. Trigger the interrupt when we are about to roll over. */
515518
#if defined(LPTIM_CHANNEL_1)
516-
__HAL_LPTIM_COMPARE_SET(&LptimHandle, LPTIM_CHANNEL_1, ~0);
519+
__HAL_LPTIM_COMPARE_SET(&LptimHandle, LPTIM_CHANNEL_1, LPTIM_PERIOD);
517520
#else
518-
__HAL_LPTIM_COMPARE_SET(&LptimHandle, ~0);
521+
__HAL_LPTIM_COMPARE_SET(&LptimHandle, LPTIM_PERIOD);
519522
#endif
520523
} else {
521524
/* It is safe to write */
@@ -524,7 +527,6 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
524527
#else
525528
__HAL_LPTIM_COMPARE_SET(&LptimHandle, timestamp);
526529
#endif
527-
528530
}
529531

530532
/* We just programed the CMP so we'll need to wait for cmpok before
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Mbed OS upload method configuration file for target NUCLEO_WL55JC.
2+
# To change any of these parameters from their default values, set them in your build script between where you
3+
# include mbed_toolchain_setup and where you add mbed os as a subdirectory.
4+
5+
# Notes:
6+
# 1. Using this device with PyOCD requires installing a pack:
7+
# $ pyocd pack install stm32wl55jcix
8+
# However, in my testing, PyOCD appeared to flash successfully but the code does not run correctly.
9+
10+
11+
# General config parameters
12+
# -------------------------------------------------------------
13+
set(UPLOAD_METHOD_DEFAULT MBED)
14+
15+
# Config options for MBED
16+
# -------------------------------------------------------------
17+
18+
set(MBED_UPLOAD_ENABLED TRUE)
19+
set(MBED_RESET_BAUDRATE 115200)
20+
21+
# Config options for PYOCD
22+
# -------------------------------------------------------------
23+
24+
#set(PYOCD_UPLOAD_ENABLED TRUE)
25+
#set(PYOCD_TARGET_NAME stm32wl55jcix)
26+
#set(PYOCD_CLOCK_SPEED 4000k)
27+
28+
# Config options for OPENOCD
29+
# -------------------------------------------------------------
30+
31+
set(OPENOCD_UPLOAD_ENABLED TRUE)
32+
set(OPENOCD_CHIP_CONFIG_COMMANDS
33+
-f ${CMAKE_CURRENT_LIST_DIR}/openocd_cfgs/st_nucleo_wlx.cfg)
34+
35+
# Config options for STM32Cube
36+
# -------------------------------------------------------------
37+
38+
set(STM32CUBE_UPLOAD_ENABLED TRUE)
39+
set(STM32CUBE_CONNECT_COMMAND -c port=SWD reset=HWrst)
40+
set(STM32CUBE_GDBSERVER_ARGS --swd)
41+
42+
# Config options for stlink
43+
# -------------------------------------------------------------
44+
45+
set(STLINK_UPLOAD_ENABLED TRUE)
46+
set(STLINK_ARGS --connect-under-reset)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# OpenOCD config file for Nucleo WLx boards
2+
3+
adapter driver hla
4+
hla_layout stlink
5+
hla_device_desc "ST-LINK/V3"
6+
7+
# ST-LINK V3 0483:374e
8+
hla_vid_pid 0x0483 0x374e
9+
10+
transport select hla_swd
11+
12+
source [find target/stm32wlx.cfg]

0 commit comments

Comments
 (0)