Skip to content

Commit f733042

Browse files
authored
Merge pull request #190 from facchinm/post_2_improvements
Various post 2.0.0 improvements
2 parents bd529a0 + c271a17 commit f733042

38 files changed

+257
-729
lines changed

cores/arduino/Serial.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ void UART::end() {
133133
if (_serial->obj != NULL) {
134134
delete _serial->obj;
135135
_serial->obj = NULL;
136+
delete _serial;
137+
_serial = NULL;
136138
}
139+
rx_buffer.clear();
137140
}
138141

139142
int UART::available() {

cores/arduino/mbed/targets/TARGET_RASPBERRYPI/TARGET_RP2040/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma GCC diagnostic push
2121
#pragma GCC diagnostic ignored "-Wtype-limits"
2222
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
23+
#pragma GCC diagnostic ignored "-Wunused-value"
2324

2425
#ifdef __cplusplus
2526
#include <stdint.h>

cores/arduino/wiring_pulse.cpp

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,84 @@ unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
190190
return pulseTime;
191191
}
192192

193+
194+
#elif defined(TARGET_RP2040)
195+
196+
#include "pinDefinitions.h"
197+
198+
unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
199+
{
200+
unsigned long startMicros = micros();
201+
202+
// wait for any previous pulse to end
203+
while (gpio_get(pin) == state) {
204+
tight_loop_contents();
205+
if (micros() - startMicros > timeout)
206+
return 0;
207+
}
208+
209+
// wait for the pulse to start
210+
while (gpio_get(pin) != state) {
211+
tight_loop_contents();
212+
if (micros() - startMicros > timeout)
213+
return 0;
214+
}
215+
216+
unsigned long start = micros();
217+
// wait for the pulse to stop
218+
while (gpio_get(pin) == state) {
219+
tight_loop_contents();
220+
if (micros() - startMicros > timeout)
221+
return 0;
222+
}
223+
return micros() - start;
224+
}
225+
226+
#elif defined(TARGET_STM32H7)
227+
228+
extern "C" {
229+
#include "gpio_api.h"
230+
GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
231+
}
232+
233+
#include "pinDefinitions.h"
234+
235+
unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
236+
{
237+
238+
uint32_t port_index = STM_PORT(pin);
239+
GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
240+
241+
volatile uint32_t *reg_in = &gpio->IDR;
242+
uint32_t mask = gpio_set(pin);
243+
244+
unsigned long startMicros = micros();
245+
246+
// wait for any previous pulse to end
247+
while ((*reg_in & mask) == state) {
248+
if (micros() - startMicros > timeout)
249+
return 0;
250+
}
251+
252+
// wait for the pulse to start
253+
while ((*reg_in & mask) != state) {
254+
if (micros() - startMicros > timeout)
255+
return 0;
256+
}
257+
258+
unsigned long start = micros();
259+
// wait for the pulse to stop
260+
while ((*reg_in & mask) == state) {
261+
if (micros() - startMicros > timeout)
262+
return 0;
263+
}
264+
return micros() - start;
265+
}
266+
267+
#endif
268+
269+
// generic, overloaded implementations
270+
193271
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
194272
{
195273
return pulseIn(digitalPinToPinName(pin), (PinStatus)state, timeout);
@@ -203,6 +281,4 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
203281
unsigned long pulseInLong(PinName pin, PinStatus state, unsigned long timeout)
204282
{
205283
return pulseIn(pin, state, timeout);
206-
}
207-
208-
#endif
284+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// placeholder to allow the discovery of this library

libraries/SPI/SPI.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ struct _mbed_spi {
3333
};
3434

3535

36-
arduino::MbedSPI::MbedSPI(int miso, int mosi, int sck) : _miso(miso), _mosi(mosi), _sck(sck) {
36+
arduino::MbedSPI::MbedSPI(int miso, int mosi, int sck) :
37+
_miso(digitalPinToPinName(miso)), _mosi(digitalPinToPinName(mosi)), _sck(digitalPinToPinName(sck)) {
38+
39+
}
40+
41+
arduino::MbedSPI::MbedSPI(PinName miso, PinName mosi, PinName sck) : _miso(miso), _mosi(mosi), _sck(sck) {
42+
3743
}
3844

3945
uint8_t arduino::MbedSPI::transfer(uint8_t data) {
@@ -95,7 +101,7 @@ void arduino::MbedSPI::begin() {
95101
dev->obj = NULL;
96102
}
97103
if (dev->obj == NULL) {
98-
dev->obj = new mbed::SPI((PinName)_mosi, (PinName)_miso, (PinName)_sck);
104+
dev->obj = new mbed::SPI(_mosi, _miso, _sck);
99105
}
100106
}
101107

libraries/SPI/SPI.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MbedSPI : public SPIClass
2929
{
3030
public:
3131
MbedSPI(int miso, int mosi, int sck);
32+
MbedSPI(PinName miso, PinName mosi, PinName sck);
3233
virtual uint8_t transfer(uint8_t data);
3334
virtual uint16_t transfer16(uint16_t data);
3435
virtual void transfer(void *buf, size_t count);
@@ -49,9 +50,9 @@ class MbedSPI : public SPIClass
4950
private:
5051
SPISettings settings = SPISettings(0, MSBFIRST, SPI_MODE0);
5152
_mbed_spi* dev = NULL;
52-
int _miso;
53-
int _mosi;
54-
int _sck;
53+
PinName _miso;
54+
PinName _mosi;
55+
PinName _sck;
5556
};
5657

5758
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "mbed_config.h"
2+
3+
#if !defined(NS_USE_EXTERNAL_MBED_TLS)
4+
5+
#include "ns_types.h"
6+
#include "arm_hal_random.h"
7+
8+
#include "mbedtls/entropy_poll.h"
9+
10+
#ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
11+
#if defined(MBEDTLS_PLATFORM_C)
12+
#include "mbedtls/platform.h"
13+
#endif
14+
#endif
15+
16+
void arm_random_module_init(void)
17+
{
18+
}
19+
20+
uint32_t arm_random_seed_get(void)
21+
{
22+
uint32_t result = 0;
23+
#ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
24+
#if defined(MBEDTLS_PLATFORM_C)
25+
int ret = mbedtls_platform_setup(NULL);
26+
if (ret != 0) {
27+
return result;
28+
}
29+
#endif /* MBEDTLS_PLATFORM_C */
30+
/* Grab a seed from a function we provide for mbedtls */
31+
size_t len;
32+
mbedtls_hardware_poll(NULL, (uint8_t *) &result, sizeof result, &len);
33+
#if defined(MBEDTLS_PLATFORM_C)
34+
mbedtls_platform_teardown(NULL);
35+
#endif /* MBEDTLS_PLATFORM_C */
36+
#endif
37+
return result;
38+
}
39+
40+
#endif

libraries/Wire/Wire.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
#include "Wire.h"
2424
#include "pinDefinitions.h"
2525

26-
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
26+
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0) {}
27+
28+
arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
2729

2830
void arduino::MbedI2C::begin() {
29-
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
31+
master = new mbed::I2C(_sda, _scl);
3032
}
3133

3234
void arduino::MbedI2C::begin(uint8_t slaveAddr) {

libraries/Wire/Wire.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class MbedI2C : public HardwareI2C
3535
{
3636
public:
3737
MbedI2C(int sda, int scl);
38+
MbedI2C(PinName sda, PinName scl);
3839
virtual void begin();
3940
#ifndef DEVICE_I2CSLAVE
4041
virtual void __attribute__ ((error("I2C Slave mode is not supported"))) begin(uint8_t address);
@@ -72,8 +73,8 @@ class MbedI2C : public HardwareI2C
7273
mbed::I2CSlave* slave = NULL;
7374
#endif
7475
mbed::I2C* master = NULL;
75-
int _sda;
76-
int _scl;
76+
PinName _sda;
77+
PinName _scl;
7778
int _address;
7879
RingBufferN<256> rxBuffer;
7980
uint8_t txBuffer[256];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
From e8e2a401550e456862311fcd191a2abb7bd3a7b7 Mon Sep 17 00:00:00 2001
2+
From: Martino Facchin <m.facchin@arduino.cc>
3+
Date: Tue, 13 Apr 2021 12:20:06 +0200
4+
Subject: [PATCH] RP2040: FLASH: fix multipage write
5+
6+
---
7+
.../TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c | 16 ++++++----------
8+
1 file changed, 6 insertions(+), 10 deletions(-)
9+
10+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
11+
index fb1a28dd7f..2ebfc40a04 100644
12+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
13+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
14+
@@ -71,18 +71,14 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
15+
return -1;
16+
}
17+
18+
- uint8_t buf[FLASH_PAGE_SIZE];
19+
-
20+
- for (int j = 0; j < size/FLASH_PAGE_SIZE; j++) {
21+
- for (int i = 0; i < FLASH_PAGE_SIZE; i++) {
22+
- buf[i] = data[j*FLASH_PAGE_SIZE + i];
23+
- }
24+
- address = address + j*FLASH_PAGE_SIZE;
25+
- core_util_critical_section_enter();
26+
- flash_range_program(address, buf, FLASH_PAGE_SIZE);
27+
- core_util_critical_section_exit();
28+
+ size_t pages = size/FLASH_PAGE_SIZE;
29+
+ if (size%FLASH_PAGE_SIZE != 0) {
30+
+ pages += 1;
31+
}
32+
33+
+ core_util_critical_section_enter();
34+
+ flash_range_program(address, data, FLASH_PAGE_SIZE * pages);
35+
+ core_util_critical_section_exit();
36+
37+
return 0;
38+
39+
--
40+
2.30.1
41+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From 47e01b04b6ea5d97d0f37a54ec6dd011916ddf1a Mon Sep 17 00:00:00 2001
2+
From: Martino Facchin <m.facchin@arduino.cc>
3+
Date: Tue, 16 Mar 2021 17:19:42 +0100
4+
Subject: [PATCH 1/3] RP2040: i2c: remove debug prints
5+
6+
---
7+
targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c | 2 +-
8+
1 file changed, 1 insertion(+), 1 deletion(-)
9+
10+
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
11+
index 9d7510bedf..2b776562a1 100644
12+
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
13+
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
14+
@@ -13,7 +13,7 @@
15+
* DEFINE
16+
******************************************************************************/
17+
18+
-#if 1
19+
+#if 0
20+
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
21+
#else
22+
#define DEBUG_PRINTF(...)
23+
--
24+
2.30.1
25+

portenta.variables

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export FLAVOUR="portenta"
22
export VARIANTS=("PORTENTA_H7_M7 PORTENTA_H7_M4")
33
export FQBNS=("envie_m7 envie_m4")
4-
export LIBRARIES=("doom MRI Portenta_SDRAM SPI WiFi ea_malloc openamp_arduino Portenta_System ThreadDebug Himax_HM01B0 PDM Portenta_Video USBAudio KernelDebug Portenta_Audio RPC USBHID Wire LittleVGL Portenta_Camera rpclib USBHOST mbed-memory-status Portenta_SDCARD Scheduler USBMSD")
4+
export LIBRARIES=("doom Ethernet MRI Portenta_SDRAM SPI WiFi ea_malloc openamp_arduino Portenta_System ThreadDebug Himax_HM01B0 PDM Portenta_Video USBAudio KernelDebug Portenta_Audio RPC USBHID Wire LittleVGL Portenta_Camera rpclib USBHOST mbed-memory-status Portenta_SDCARD Scheduler USBMSD")
55
export BOOTLOADERS=("PORTENTA_H7")

variants/ARDUINO_NANO33BLE/defines.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
-DFEATURE_CRYPTOCELL310=1
3131
-DFEATURE_STORAGE=1
3232
-DMBEDTLS_CONFIG_HW_SUPPORT
33-
-DMBED_BUILD_TIMESTAMP=1617783867.1831396
33+
-DMBED_BUILD_TIMESTAMP=1618322697.3620598
3434
-DMBED_MPU_CUSTOM
3535
-DMBED_TICKLESS
3636
-DNRF52840_XXAA

0 commit comments

Comments
 (0)