Skip to content

Commit bccb118

Browse files
committed
Add UART support to STM32 platform
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent a88c90c commit bccb118

14 files changed

Lines changed: 1425 additions & 64 deletions

File tree

.github/workflows/stm32-build.yaml

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ jobs:
185185
mkdir build-host
186186
cd build-host
187187
cmake .. -G Ninja
188-
cmake --build . -t stm32_boot_test stm32_gpio_test stm32_i2c_test stm32_spi_test
188+
cmake --build . -t stm32_boot_test stm32_gpio_test stm32_i2c_test stm32_spi_test stm32_uart_test
189189
190190
- name: Install Renode
191191
if: matrix.renode_platform
@@ -197,7 +197,7 @@ jobs:
197197
echo "$PWD/renode-portable" >> $GITHUB_PATH
198198
pip install -r renode-portable/tests/requirements.txt
199199
200-
- name: Run Renode boot test
200+
- name: Run Renode tests
201201
if: matrix.renode_platform
202202
run: |
203203
LOCAL_REPL="src/platforms/stm32/tests/renode/${{ matrix.renode_platform }}"
@@ -206,53 +206,15 @@ jobs:
206206
else
207207
PLATFORM="@platforms/cpus/${{ matrix.renode_platform }}"
208208
fi
209-
renode-test src/platforms/stm32/tests/renode/stm32_boot_test.robot \
210-
--variable ELF:@$PWD/src/platforms/stm32/build/AtomVM-${{ matrix.device }}.elf \
211-
--variable AVM:@$PWD/build-host/src/platforms/stm32/tests/test_erl_sources/stm32_boot_test.avm \
212-
--variable AVM_ADDRESS:${{ matrix.avm_address }} \
213-
--variable PLATFORM:$PLATFORM
214-
215-
- name: Run Renode GPIO test
216-
if: matrix.renode_platform
217-
run: |
218-
LOCAL_REPL="src/platforms/stm32/tests/renode/${{ matrix.renode_platform }}"
219-
if [ -f "$LOCAL_REPL" ]; then
220-
PLATFORM="@$PWD/$LOCAL_REPL"
221-
else
222-
PLATFORM="@platforms/cpus/${{ matrix.renode_platform }}"
223-
fi
224-
renode-test src/platforms/stm32/tests/renode/stm32_gpio_test.robot \
225-
--variable ELF:@$PWD/src/platforms/stm32/build/AtomVM-${{ matrix.device }}.elf \
226-
--variable AVM:@$PWD/build-host/src/platforms/stm32/tests/test_erl_sources/stm32_gpio_test.avm \
227-
--variable AVM_ADDRESS:${{ matrix.avm_address }} \
228-
--variable PLATFORM:$PLATFORM
229-
230-
- name: Run Renode I2C test
231-
if: matrix.renode_platform && !matrix.skip_i2c_test
232-
run: |
233-
LOCAL_REPL="src/platforms/stm32/tests/renode/${{ matrix.renode_platform }}"
234-
if [ -f "$LOCAL_REPL" ]; then
235-
PLATFORM="@$PWD/$LOCAL_REPL"
236-
else
237-
PLATFORM="@platforms/cpus/${{ matrix.renode_platform }}"
238-
fi
239-
renode-test src/platforms/stm32/tests/renode/stm32_i2c_test.robot \
240-
--variable ELF:@$PWD/src/platforms/stm32/build/AtomVM-${{ matrix.device }}.elf \
241-
--variable AVM:@$PWD/build-host/src/platforms/stm32/tests/test_erl_sources/stm32_i2c_test.avm \
242-
--variable AVM_ADDRESS:${{ matrix.avm_address }} \
243-
--variable PLATFORM:$PLATFORM
244-
245-
- name: Run Renode SPI test
246-
if: matrix.renode_platform
247-
run: |
248-
LOCAL_REPL="src/platforms/stm32/tests/renode/${{ matrix.renode_platform }}"
249-
if [ -f "$LOCAL_REPL" ]; then
250-
PLATFORM="@$PWD/$LOCAL_REPL"
209+
if [ "${{ matrix.skip_i2c_test }}" = "true" ]; then
210+
TESTS="boot gpio spi uart"
251211
else
252-
PLATFORM="@platforms/cpus/${{ matrix.renode_platform }}"
212+
TESTS="boot gpio i2c spi uart"
253213
fi
254-
renode-test src/platforms/stm32/tests/renode/stm32_spi_test.robot \
255-
--variable ELF:@$PWD/src/platforms/stm32/build/AtomVM-${{ matrix.device }}.elf \
256-
--variable AVM:@$PWD/build-host/src/platforms/stm32/tests/test_erl_sources/stm32_spi_test.avm \
257-
--variable AVM_ADDRESS:${{ matrix.avm_address }} \
258-
--variable PLATFORM:$PLATFORM
214+
for TEST in $TESTS; do
215+
renode-test "src/platforms/stm32/tests/renode/stm32_${TEST}_test.robot" \
216+
--variable ELF:@$PWD/src/platforms/stm32/build/AtomVM-${{ matrix.device }}.elf \
217+
--variable AVM:@$PWD/build-host/src/platforms/stm32/tests/test_erl_sources/stm32_${TEST}_test.avm \
218+
--variable AVM_ADDRESS:${{ matrix.avm_address }} \
219+
--variable PLATFORM:$PLATFORM
220+
done

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added Erlang distribution over serial (uart)
1111
- Added WASM32 JIT backend for Emscripten platform
1212
- Added UART API to rp2 platform
13-
- Added I2C and SPI APIs to stm32 platform
13+
- Added I2C, SPI and UART APIs to stm32 platform
1414

1515
### Fixed
1616
- Stop using deprecated `term_from_int32` on STM32 platform

examples/erlang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ pack_runnable(i2c_scanner i2c_scanner eavmlib estdlib DIALYZE_AGAINST avm_esp32
4848
pack_runnable(i2c_lis3dh i2c_lis3dh eavmlib estdlib DIALYZE_AGAINST avm_esp32 avm_rp2 avm_stm32)
4949
pack_runnable(spi_flash spi_flash eavmlib estdlib DIALYZE_AGAINST avm_esp32 avm_rp2 avm_stm32)
5050
pack_runnable(spi_lis3dh spi_lis3dh eavmlib estdlib DIALYZE_AGAINST avm_esp32 avm_rp2 avm_stm32)
51-
pack_runnable(sim800l sim800l eavmlib estdlib DIALYZE_AGAINST avm_esp32 avm_rp2)
51+
pack_runnable(sim800l sim800l eavmlib estdlib DIALYZE_AGAINST avm_esp32 avm_rp2 avm_stm32)

examples/erlang/sim800l.erl

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
%% Default pins are auto-detected from the platform and chip model:
3535
%%
3636
%% Pico (UART1): TX=GP4, RX=GP5
37+
%% STM32 (USART1): TX=PA9, RX=PA10, AF=7
3738
%% ESP32/S2/S3 (UART1): TX=17, RX=16
3839
%% ESP32-C2 (UART1): TX=4, RX=5
3940
%% ESP32-C3/C5 (UART1): TX=4, RX=5
@@ -46,13 +47,8 @@
4647
-define(AT_TIMEOUT, 2000).
4748

4849
start() ->
49-
{TX, RX} = default_pins(),
50-
io:format("Opening UART1 on TX=~B RX=~B~n", [TX, RX]),
51-
UART = uart:open("UART1", [
52-
{tx, TX},
53-
{rx, RX},
54-
{speed, 115200}
55-
]),
50+
Platform = atomvm:platform(),
51+
UART = open_uart(Platform),
5652
%% SIM800L takes 3-5 seconds to boot after power-on
5753
case wait_for_module(UART, 5) of
5854
ok ->
@@ -156,13 +152,33 @@ drain(UART) ->
156152
end.
157153

158154
%%-----------------------------------------------------------------------------
159-
%% Platform-specific default pins
155+
%% Platform-specific UART operations
160156
%%-----------------------------------------------------------------------------
161-
default_pins() ->
162-
default_pins(atomvm:platform()).
163157

164-
%% {TX, RX}
158+
open_uart(stm32) ->
159+
{TX, RX} = default_pins(stm32),
160+
io:format("Opening USART1 on TX=~p RX=~p~n", [TX, RX]),
161+
uart:open([
162+
{peripheral, 1},
163+
{tx, TX},
164+
{rx, RX},
165+
{af, 7},
166+
{speed, 115200}
167+
]);
168+
open_uart(Platform) ->
169+
{TX, RX} = default_pins(Platform),
170+
io:format("Opening UART1 on TX=~p RX=~p~n", [TX, RX]),
171+
uart:open("UART1", [
172+
{tx, TX},
173+
{rx, RX},
174+
{speed, 115200}
175+
]).
176+
177+
%%-----------------------------------------------------------------------------
178+
%% Platform-specific default pins
179+
%%-----------------------------------------------------------------------------
165180
default_pins(pico) -> {4, 5};
181+
default_pins(stm32) -> {{a, 9}, {a, 10}};
166182
default_pins(esp32) -> esp32_default_pins().
167183

168184
esp32_default_pins() ->

examples/erlang/stm32/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,19 @@ add_custom_command(
7070
VERBATIM
7171
)
7272
add_custom_target(stm32_spi_flash ALL DEPENDS stm32_spi_flash.avm)
73+
74+
set(SIM800L_BEAM ${CMAKE_BINARY_DIR}/examples/erlang/sim800l.beam)
75+
add_custom_command(
76+
OUTPUT stm32_sim800l.avm
77+
DEPENDS sim800l_main ${SIM800L_BEAM}
78+
${CMAKE_BINARY_DIR}/libs/estdlib/src/estdlib.avm estdlib
79+
${CMAKE_BINARY_DIR}/libs/avm_stm32/src/avm_stm32.avm avm_stm32
80+
PackBEAM
81+
COMMAND ${CMAKE_BINARY_DIR}/tools/packbeam/packbeam create ${INCLUDE_LINES} stm32_sim800l.avm
82+
${SIM800L_BEAM}
83+
${CMAKE_BINARY_DIR}/libs/estdlib/src/estdlib.avm
84+
${CMAKE_BINARY_DIR}/libs/avm_stm32/src/avm_stm32.avm
85+
COMMENT "Packing runnable stm32_sim800l.avm"
86+
VERBATIM
87+
)
88+
add_custom_target(stm32_sim800l ALL DEPENDS stm32_sim800l.avm)

libs/avm_stm32/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(ERLANG_MODULES
2626
gpio
2727
i2c
2828
spi
29+
uart
2930
)
3031

3132
pack_archive(avm_stm32 DEPENDS_ON eavmlib ERLC_FLAGS +warnings_as_errors MODULES ${ERLANG_MODULES})

0 commit comments

Comments
 (0)