Skip to content

Various post 2.0.0 improvements #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cores/arduino/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ void UART::end() {
if (_serial->obj != NULL) {
delete _serial->obj;
_serial->obj = NULL;
delete _serial;
_serial = NULL;
}
rx_buffer.clear();
}

int UART::available() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#pragma GCC diagnostic ignored "-Wunused-value"

#ifdef __cplusplus
#include <stdint.h>
Expand Down
82 changes: 79 additions & 3 deletions cores/arduino/wiring_pulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,84 @@ unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
return pulseTime;
}


#elif defined(TARGET_RP2040)

#include "pinDefinitions.h"

unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
{
unsigned long startMicros = micros();

// wait for any previous pulse to end
while (gpio_get(pin) == state) {
tight_loop_contents();
if (micros() - startMicros > timeout)
return 0;
}

// wait for the pulse to start
while (gpio_get(pin) != state) {
tight_loop_contents();
if (micros() - startMicros > timeout)
return 0;
}

unsigned long start = micros();
// wait for the pulse to stop
while (gpio_get(pin) == state) {
tight_loop_contents();
if (micros() - startMicros > timeout)
return 0;
}
return micros() - start;
}

#elif defined(TARGET_STM32H7)

extern "C" {
#include "gpio_api.h"
GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
}

#include "pinDefinitions.h"

unsigned long pulseIn(PinName pin, PinStatus state, unsigned long timeout)
{

uint32_t port_index = STM_PORT(pin);
GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);

volatile uint32_t *reg_in = &gpio->IDR;
uint32_t mask = gpio_set(pin);

unsigned long startMicros = micros();

// wait for any previous pulse to end
while ((*reg_in & mask) == state) {
if (micros() - startMicros > timeout)
return 0;
}

// wait for the pulse to start
while ((*reg_in & mask) != state) {
if (micros() - startMicros > timeout)
return 0;
}

unsigned long start = micros();
// wait for the pulse to stop
while ((*reg_in & mask) == state) {
if (micros() - startMicros > timeout)
return 0;
}
return micros() - start;
}

#endif

// generic, overloaded implementations

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
return pulseIn(digitalPinToPinName(pin), (PinStatus)state, timeout);
Expand All @@ -203,6 +281,4 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
unsigned long pulseInLong(PinName pin, PinStatus state, unsigned long timeout)
{
return pulseIn(pin, state, timeout);
}

#endif
}
1 change: 1 addition & 0 deletions libraries/Ethernet/src/Portenta_Ethernet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// placeholder to allow the discovery of this library
10 changes: 8 additions & 2 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ struct _mbed_spi {
};


arduino::MbedSPI::MbedSPI(int miso, int mosi, int sck) : _miso(miso), _mosi(mosi), _sck(sck) {
arduino::MbedSPI::MbedSPI(int miso, int mosi, int sck) :
_miso(digitalPinToPinName(miso)), _mosi(digitalPinToPinName(mosi)), _sck(digitalPinToPinName(sck)) {

}

arduino::MbedSPI::MbedSPI(PinName miso, PinName mosi, PinName sck) : _miso(miso), _mosi(mosi), _sck(sck) {

}

uint8_t arduino::MbedSPI::transfer(uint8_t data) {
Expand Down Expand Up @@ -95,7 +101,7 @@ void arduino::MbedSPI::begin() {
dev->obj = NULL;
}
if (dev->obj == NULL) {
dev->obj = new mbed::SPI((PinName)_mosi, (PinName)_miso, (PinName)_sck);
dev->obj = new mbed::SPI(_mosi, _miso, _sck);
}
}

Expand Down
7 changes: 4 additions & 3 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MbedSPI : public SPIClass
{
public:
MbedSPI(int miso, int mosi, int sck);
MbedSPI(PinName miso, PinName mosi, PinName sck);
virtual uint8_t transfer(uint8_t data);
virtual uint16_t transfer16(uint16_t data);
virtual void transfer(void *buf, size_t count);
Expand All @@ -49,9 +50,9 @@ class MbedSPI : public SPIClass
private:
SPISettings settings = SPISettings(0, MSBFIRST, SPI_MODE0);
_mbed_spi* dev = NULL;
int _miso;
int _mosi;
int _sck;
PinName _miso;
PinName _mosi;
PinName _sck;
};

}
Expand Down
40 changes: 40 additions & 0 deletions libraries/WiFi/src/utility/arm_hal_random.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "mbed_config.h"

#if !defined(NS_USE_EXTERNAL_MBED_TLS)

#include "ns_types.h"
#include "arm_hal_random.h"

#include "mbedtls/entropy_poll.h"

#ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#endif
#endif

void arm_random_module_init(void)
{
}

uint32_t arm_random_seed_get(void)
{
uint32_t result = 0;
#ifdef MBEDTLS_ENTROPY_HARDWARE_ALT
#if defined(MBEDTLS_PLATFORM_C)
int ret = mbedtls_platform_setup(NULL);
if (ret != 0) {
return result;
}
#endif /* MBEDTLS_PLATFORM_C */
/* Grab a seed from a function we provide for mbedtls */
size_t len;
mbedtls_hardware_poll(NULL, (uint8_t *) &result, sizeof result, &len);
#if defined(MBEDTLS_PLATFORM_C)
mbedtls_platform_teardown(NULL);
#endif /* MBEDTLS_PLATFORM_C */
#endif
return result;
}

#endif
6 changes: 4 additions & 2 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include "Wire.h"
#include "pinDefinitions.h"

arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0) {}

arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}

void arduino::MbedI2C::begin() {
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
master = new mbed::I2C(_sda, _scl);
}

void arduino::MbedI2C::begin(uint8_t slaveAddr) {
Expand Down
5 changes: 3 additions & 2 deletions libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MbedI2C : public HardwareI2C
{
public:
MbedI2C(int sda, int scl);
MbedI2C(PinName sda, PinName scl);
virtual void begin();
#ifndef DEVICE_I2CSLAVE
virtual void __attribute__ ((error("I2C Slave mode is not supported"))) begin(uint8_t address);
Expand Down Expand Up @@ -72,8 +73,8 @@ class MbedI2C : public HardwareI2C
mbed::I2CSlave* slave = NULL;
#endif
mbed::I2C* master = NULL;
int _sda;
int _scl;
PinName _sda;
PinName _scl;
int _address;
RingBufferN<256> rxBuffer;
uint8_t txBuffer[256];
Expand Down
41 changes: 41 additions & 0 deletions patches/0084-RP2040-FLASH-fix-multipage-write.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
From e8e2a401550e456862311fcd191a2abb7bd3a7b7 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Tue, 13 Apr 2021 12:20:06 +0200
Subject: [PATCH] RP2040: FLASH: fix multipage write

---
.../TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
index fb1a28dd7f..2ebfc40a04 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/flash_api.c
@@ -71,18 +71,14 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
return -1;
}

- uint8_t buf[FLASH_PAGE_SIZE];
-
- for (int j = 0; j < size/FLASH_PAGE_SIZE; j++) {
- for (int i = 0; i < FLASH_PAGE_SIZE; i++) {
- buf[i] = data[j*FLASH_PAGE_SIZE + i];
- }
- address = address + j*FLASH_PAGE_SIZE;
- core_util_critical_section_enter();
- flash_range_program(address, buf, FLASH_PAGE_SIZE);
- core_util_critical_section_exit();
+ size_t pages = size/FLASH_PAGE_SIZE;
+ if (size%FLASH_PAGE_SIZE != 0) {
+ pages += 1;
}

+ core_util_critical_section_enter();
+ flash_range_program(address, data, FLASH_PAGE_SIZE * pages);
+ core_util_critical_section_exit();

return 0;

--
2.30.1

25 changes: 25 additions & 0 deletions patches/0085-RP2040-i2c-remove-debug-prints.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From 47e01b04b6ea5d97d0f37a54ec6dd011916ddf1a Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Tue, 16 Mar 2021 17:19:42 +0100
Subject: [PATCH 1/3] RP2040: i2c: remove debug prints

---
targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
index 9d7510bedf..2b776562a1 100644
--- a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/i2c_api.c
@@ -13,7 +13,7 @@
* DEFINE
******************************************************************************/

-#if 1
+#if 0
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
--
2.30.1

2 changes: 1 addition & 1 deletion portenta.variables
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export FLAVOUR="portenta"
export VARIANTS=("PORTENTA_H7_M7 PORTENTA_H7_M4")
export FQBNS=("envie_m7 envie_m4")
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")
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")
export BOOTLOADERS=("PORTENTA_H7")
2 changes: 1 addition & 1 deletion variants/ARDUINO_NANO33BLE/defines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
-DFEATURE_CRYPTOCELL310=1
-DFEATURE_STORAGE=1
-DMBEDTLS_CONFIG_HW_SUPPORT
-DMBED_BUILD_TIMESTAMP=1617783867.1831396
-DMBED_BUILD_TIMESTAMP=1618322697.3620598
-DMBED_MPU_CUSTOM
-DMBED_TICKLESS
-DNRF52840_XXAA
Expand Down
Loading