Skip to content

Commit 2f7ae50

Browse files
added support for STM32F4 and Ethernet (#593) (#602)
* added support for STM32F4 and Ethernet * Revert library changes Signed-off-by: Pablo Garrido <[email protected]> Co-authored-by: Pablo Garrido <[email protected]> (cherry picked from commit 03779a8) Co-authored-by: Dominik Nowak <[email protected]>
1 parent 6c270b5 commit 2f7ae50

File tree

5 files changed

+155
-1
lines changed

5 files changed

+155
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
SET(CMAKE_SYSTEM_NAME Generic)
2+
set(CMAKE_CROSSCOMPILING 1)
3+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4+
5+
set(CMAKE_C_COMPILER $ENV{TOOLCHAIN_PREFIX}gcc)
6+
set(CMAKE_CXX_COMPILER $ENV{TOOLCHAIN_PREFIX}g++)
7+
8+
SET(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
9+
SET(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")
10+
11+
set(FLAGS "-O2 -mfloat-abi=hard -mfpu=vfpv3-d16 -ffunction-sections -fdata-sections -fno-exceptions -mcpu=cortex-m4 -nostdlib -DARDUINO=10813 -mthumb --param max-inline-insns-single=500 -DF_CPU=168000000L -D'RCUTILS_LOG_MIN_SEVERITY=RCUTILS_LOG_MIN_SEVERITY_NONE'" CACHE STRING "" FORCE)
12+
13+
set(CMAKE_C_FLAGS_INIT "-std=c11 ${FLAGS} -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
14+
set(CMAKE_CXX_FLAGS_INIT "-std=c++14 ${FLAGS} -fno-rtti -DCLOCK_MONOTONIC=0 -D'__attribute__(x)='" CACHE STRING "" FORCE)
15+
16+
set(__BIG_ENDIAN__ 0)

extras/library_generation/library_generation.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ if [ $OPTIND -eq 1 ]; then
1616
PLATFORMS+=("teensy35")
1717
PLATFORMS+=("cortex_m0")
1818
PLATFORMS+=("cortex_m3")
19+
PLATFORMS+=("cortex_m4")
1920
# PLATFORMS+=("portenta-m4")
2021
PLATFORMS+=("portenta-m7")
2122
PLATFORMS+=("kakutef7-m7")
@@ -96,6 +97,20 @@ if [[ " ${PLATFORMS[@]} " =~ " cortex_m3 " ]]; then
9697
cp -R firmware/build/libmicroros.a /project/src/cortex-m3/libmicroros.a
9798
fi
9899

100+
######## Build for STM32F4 ########
101+
if [[ " ${PLATFORMS[@]} " =~ " cortex_m4 " ]]; then
102+
rm -rf firmware/build
103+
104+
export TOOLCHAIN_PREFIX=/uros_ws/gcc-arm-none-eabi-9-2020-q2-update/bin/arm-none-eabi-
105+
ros2 run micro_ros_setup build_firmware.sh /project/extras/library_generation/cortex_m4_toolchain.cmake /project/extras/library_generation/colcon_lowmem.meta
106+
107+
find firmware/build/include/ -name "*.c" -delete
108+
cp -R firmware/build/include/* /project/src/
109+
110+
mkdir -p /project/src/cortex-m4
111+
cp -R firmware/build/libmicroros.a /project/src/cortex-m4/libmicroros.a
112+
fi
113+
99114
######## Build for Teensy 3.2 ########
100115
if [[ " ${PLATFORMS[@]} " =~ " teensy32 " ]]; then
101116
rm -rf firmware/build

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=micro-ROS Arduino library
77
url=https://github.com/micro-ROS
88
precompiled=true
99
category=Other
10-
architectures=OpenCR,Teensyduino,samd,sam,mbed
10+
architectures=stm32,OpenCR,Teensyduino,samd,sam,mbed

src/micro_ros_arduino.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,49 @@ static inline void set_microros_transports(){
3232
);
3333
}
3434

35+
#if defined(TARGET_STM32F4)
36+
37+
#include <Arduino.h>
38+
#include <EthernetUdp.h>
39+
#include <LwIP.h>
40+
#include <STM32Ethernet.h>
41+
42+
#include <uxr/client/transport.h>
43+
#include <rmw_microros/rmw_microros.h>
44+
#include "IPAddress.h"
45+
46+
extern "C" bool arduino_native_ethernet_udp_transport_open(struct uxrCustomTransport * transport);
47+
extern "C" bool arduino_native_ethernet_udp_transport_close(struct uxrCustomTransport * transport);
48+
extern "C" size_t arduino_native_ethernet_udp_transport_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err);
49+
extern "C" size_t arduino_native_ethernet_udp_transport_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* err);
50+
51+
struct micro_ros_agent_locator {
52+
IPAddress address;
53+
int port;
54+
};
55+
56+
static inline void set_microros_native_ethernet_udp_transports(byte mac[], IPAddress client_ip, IPAddress agent_ip, uint16_t agent_port){
57+
58+
static struct micro_ros_agent_locator locator;
59+
60+
Ethernet.begin(mac, client_ip);
61+
delay(1000);
62+
63+
locator.address = agent_ip;
64+
locator.port = agent_port;
65+
66+
rmw_uros_set_custom_transport(
67+
false,
68+
(void *) &locator,
69+
arduino_native_ethernet_udp_transport_open,
70+
arduino_native_ethernet_udp_transport_close,
71+
arduino_native_ethernet_udp_transport_write,
72+
arduino_native_ethernet_udp_transport_read
73+
);
74+
}
75+
76+
#endif
77+
3578
#if defined(TARGET_PORTENTA_H7_M7) || defined(ARDUINO_NANO_RP2040_CONNECT)
3679

3780
#if defined(TARGET_PORTENTA_H7_M7)

src/native_ethernet_transport.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#if defined(TARGET_STM32F4)
2+
3+
#include <Arduino.h>
4+
#include <EthernetUdp.h>
5+
#include <STM32Ethernet.h>
6+
#include <micro_ros_arduino.h>
7+
8+
extern "C" {
9+
10+
#include <stdbool.h>
11+
#include <stdio.h>
12+
#include <sys/time.h>
13+
14+
static EthernetUDP udp_client;
15+
16+
bool arduino_native_ethernet_udp_transport_open(
17+
struct uxrCustomTransport *transport) {
18+
struct micro_ros_agent_locator *locator =
19+
(struct micro_ros_agent_locator *)transport->args;
20+
udp_client.begin(locator->port);
21+
return true;
22+
}
23+
24+
bool arduino_native_ethernet_udp_transport_close(
25+
struct uxrCustomTransport *transport) {
26+
udp_client.stop();
27+
return true;
28+
}
29+
30+
size_t arduino_native_ethernet_udp_transport_write(
31+
struct uxrCustomTransport *transport, const uint8_t *buf, size_t len,
32+
uint8_t *errcode) {
33+
(void)errcode;
34+
struct micro_ros_agent_locator *locator =
35+
(struct micro_ros_agent_locator *)transport->args;
36+
37+
udp_client.beginPacket(locator->address, locator->port);
38+
size_t sent = udp_client.write(buf, len);
39+
udp_client.endPacket();
40+
udp_client.flush();
41+
42+
return sent;
43+
}
44+
45+
size_t arduino_native_ethernet_udp_transport_read(
46+
struct uxrCustomTransport *transport, uint8_t *buf, size_t len, int timeout,
47+
uint8_t *errcode) {
48+
(void)errcode;
49+
uint32_t start_time = millis();
50+
51+
while (millis() - start_time < ((uint32_t)timeout) &&
52+
udp_client.parsePacket() == 0) {
53+
delay(1);
54+
}
55+
56+
size_t readed = udp_client.read(buf, len);
57+
return (readed < 0) ? 0 : readed;
58+
}
59+
60+
#define micro_rollover_useconds 4294967295
61+
62+
int clock_gettime(clockid_t unused, struct timespec *tp) {
63+
(void)unused;
64+
static uint32_t rollover = 0;
65+
static uint32_t last_measure = 0;
66+
67+
uint32_t m = micros();
68+
rollover += (m < last_measure) ? 1 : 0;
69+
70+
uint64_t real_us = (uint64_t)(m + rollover * micro_rollover_useconds);
71+
tp->tv_sec = real_us / 1000000;
72+
tp->tv_nsec = (real_us % 1000000) * 1000;
73+
last_measure = m;
74+
75+
return 0;
76+
}
77+
78+
}
79+
80+
#endif

0 commit comments

Comments
 (0)