Skip to content

Commit a7e5b22

Browse files
isircujeppefrandsenhansbinderupbogomhd
committed
MOZ-2554: Merge fixes from mbed-os-6.13.0-jfd-fixes (#14)
* Changed to C++20 * applied patch from fep low power * Changed to C++20 * Updated compile options * Added more fixes * Added event queue changes * Changed to hard float and fixed sleep * Added werror * Corrected compile warnings/errors * Updated system clock settings * Reverted change * Downgraded STM32Cube to 1.23.0 * Corrected build issues * Corrected mimimal printf compile warning * Revert "Downgraded STM32Cube to 1.23.0" This reverts commit ae19071. * Corrected build issues * Added I2C slave changes * Updated from old branch * Corrected I2C slave command * Minor rename * Corrected name * Virtual i2c read write Made i2c read/write functions virtual so that they can be overridden. * Removed obsolete file * Corrected unused variable in mbed_board.c * Added maybe_unused to validate_errno_values * Fix build errors Co-authored-by: Jeppe Frandsen <[email protected]> Co-authored-by: Hans Binderup <[email protected]> Co-authored-by: Michael Jakobsen Højgaard <[email protected]>
1 parent f6e86bd commit a7e5b22

File tree

24 files changed

+323
-69
lines changed

24 files changed

+323
-69
lines changed

drivers/include/drivers/I2C.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "hal/i2c_api.h"
2626
#include "platform/SingletonPtr.h"
2727
#include "platform/PlatformMutex.h"
28-
#include "platform/NonCopyable.h"
2928

3029
#if DEVICE_I2C_ASYNCH
3130
#include "platform/CThunk.h"
@@ -79,7 +78,7 @@ namespace mbed {
7978
* }
8079
* @endcode
8180
*/
82-
class I2C : private NonCopyable<I2C> {
81+
class I2C {
8382

8483
public:
8584
enum RxStatus {
@@ -108,6 +107,10 @@ class I2C : private NonCopyable<I2C> {
108107
I2C(const i2c_pinmap_t &static_pinmap);
109108
I2C(const i2c_pinmap_t &&) = delete; // prevent passing of temporary objects
110109

110+
/** Create an I2C Master interface from an existing object
111+
*/
112+
I2C(const I2C&) = default;
113+
111114
/** Set the frequency of the I2C interface
112115
*
113116
* @param hz The bus frequency in hertz
@@ -129,7 +132,7 @@ class I2C : private NonCopyable<I2C> {
129132
* 0 on success (ack),
130133
* nonzero on failure (nack)
131134
*/
132-
int read(int address, char *data, int length, bool repeated = false);
135+
virtual int read(int address, char *data, int length, bool repeated = false);
133136

134137
/** Read a single byte from the I2C bus
135138
*
@@ -155,7 +158,7 @@ class I2C : private NonCopyable<I2C> {
155158
* 0 on success (ack),
156159
* nonzero on failure (nack)
157160
*/
158-
int write(int address, const char *data, int length, bool repeated = false);
161+
virtual int write(int address, const char *data, int length, bool repeated = false);
159162

160163
/** Write single byte out on the I2C bus
161164
*
@@ -176,6 +179,14 @@ class I2C : private NonCopyable<I2C> {
176179
*/
177180
void stop(void);
178181

182+
/** Recover I2C bus, when stuck with SDA low
183+
*
184+
* @returns
185+
* '0' - Successfully recovered
186+
* 'I2C_ERROR_BUS_BUSY' - In case of failure
187+
*/
188+
int recover(void);
189+
179190
/** Acquire exclusive access to this I2C bus
180191
*/
181192
virtual void lock(void);

drivers/source/I2C.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ void I2C::stop(void)
149149
unlock();
150150
}
151151

152+
int I2C::recover(void)
153+
{
154+
lock();
155+
int ret = recover(_sda, _scl);
156+
i2c_init(&_i2c, _sda, _scl);
157+
unlock();
158+
return ret;
159+
}
160+
152161
void I2C::lock()
153162
{
154163
_mutex->lock();

drivers/source/SerialBase.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,9 @@ void SerialBase::attach(Callback<void()> func, IrqType type)
9595
// Disable interrupts when attaching interrupt handler
9696
core_util_critical_section_enter();
9797
if (func) {
98-
// lock deep sleep only the first time
99-
if (!_irq[type]) {
100-
sleep_manager_lock_deep_sleep();
101-
}
10298
_irq[type] = func;
10399
serial_irq_set(&_serial, (SerialIrq)type, 1);
104100
} else {
105-
// unlock deep sleep only the first time
106-
if (_irq[type]) {
107-
sleep_manager_unlock_deep_sleep();
108-
}
109101
_irq[type] = nullptr;
110102
serial_irq_set(&_serial, (SerialIrq)type, 0);
111103
}

events/include/events/EventQueue.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,16 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
775775
{
776776
void *p = equeue_alloc(&_equeue, sizeof(F));
777777
if (!p) {
778+
queue_full(QUEUE_FULL_CALL, sizeof(F));
778779
return 0;
779780
}
780781

781782
F *e = new (p) F(std::move(f));
782783
equeue_event_dtor(e, &EventQueue::function_dtor<F>);
783-
return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
784+
int id = equeue_post(&_equeue, &EventQueue::function_call<F>, e);
785+
if (!id)
786+
queue_full(QUEUE_FULL_CALL, sizeof(F));
787+
return id;
784788
}
785789

786790

@@ -850,13 +854,17 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
850854
{
851855
void *p = equeue_alloc(&_equeue, sizeof(F));
852856
if (!p) {
857+
queue_full(QUEUE_FULL_CALL_IN, sizeof(F));
853858
return 0;
854859
}
855860

856861
F *e = new (p) F(std::move(f));
857862
equeue_event_delay(e, ms.count());
858863
equeue_event_dtor(e, &EventQueue::function_dtor<F>);
859-
return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
864+
int id = equeue_post(&_equeue, &EventQueue::function_call<F>, e);
865+
if (!id)
866+
queue_full(QUEUE_FULL_CALL_IN, sizeof(F));
867+
return id;
860868
}
861869

862870
/** Calls an event on the queue after a specified delay
@@ -1003,14 +1011,18 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
10031011
{
10041012
void *p = equeue_alloc(&_equeue, sizeof(F));
10051013
if (!p) {
1014+
queue_full(QUEUE_FULL_CALL_EVERY, sizeof(F));
10061015
return 0;
10071016
}
10081017

10091018
F *e = new (p) F(std::move(f));
10101019
equeue_event_delay(e, ms.count());
10111020
equeue_event_period(e, ms.count());
10121021
equeue_event_dtor(e, &EventQueue::function_dtor<F>);
1013-
return equeue_post(&_equeue, &EventQueue::function_call<F>, e);
1022+
int id = equeue_post(&_equeue, &EventQueue::function_call<F>, e);
1023+
if (!id)
1024+
queue_full(QUEUE_FULL_CALL_EVERY, sizeof(F));
1025+
return id;
10141026
}
10151027

10161028
/** Calls an event on the queue periodically
@@ -1398,6 +1410,18 @@ class EventQueue : private mbed::NonCopyable<EventQueue> {
13981410
#endif
13991411

14001412
protected:
1413+
enum queue_full_call_type {
1414+
QUEUE_FULL_CALL,
1415+
QUEUE_FULL_CALL_IN,
1416+
QUEUE_FULL_CALL_EVERY,
1417+
};
1418+
1419+
/** Error callback. Called when task is rejected because queue is full */
1420+
virtual void queue_full(queue_full_call_type call_type, size_t function_size) const
1421+
{
1422+
// Can be overridden in subclass
1423+
}
1424+
14011425
#if !defined(DOXYGEN_ONLY)
14021426
template <typename F>
14031427
friend class Event;

platform/include/platform/mbed_rtc_time.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef __MBED_RTC_TIME_H__
1919
#define __MBED_RTC_TIME_H__
2020

21-
#include <time.h>
21+
#include <sys/time.h>
2222

2323
#ifdef __cplusplus
2424
extern "C" {

platform/source/mbed_board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ WEAK MBED_NORETURN void mbed_die(void)
2828
#if !defined(TARGET_EFM32)
2929
core_util_critical_section_enter();
3030
#endif
31-
gpio_t led_err;
3231
#ifdef LED1
32+
gpio_t led_err;
3333
gpio_init_out(&led_err, LED1);
3434
#endif
3535

platform/source/mbed_error.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
6565
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
6666
#define report_error_ctx MBED_CRASH_DATA.error.context
6767
static bool is_reboot_error_valid = false;
68+
69+
//Helper function to calculate CRC
70+
//NOTE: It would have been better to use MbedCRC implementation. But
71+
//MbedCRC uses table based calculation and we dont want to keep that table memory
72+
//used up for this purpose. Also we cannot force bitwise calculation in MbedCRC
73+
//and it also requires a new wrapper to be called from C implementation. Since
74+
//we dont have many uses cases to create a C wrapper for MbedCRC and the data
75+
//we calculate CRC on in this context is very less we will use a local
76+
//implementation here.
77+
__attribute__ ((unused)) static unsigned int compute_crc32(const void *data, int datalen)
78+
{
79+
const unsigned int polynomial = 0x04C11DB7; /* divisor is 32bit */
80+
unsigned int crc = 0; /* CRC value is 32bit */
81+
unsigned char *buf = (unsigned char *)data;//use a temp variable to make code readable and to avoid typecasting issues.
82+
83+
for (; datalen > 0; datalen--) {
84+
unsigned char b = *buf++;
85+
crc ^= (unsigned int)(b << 24); /* move byte into upper 8bit */
86+
for (int i = 0; i < 8; i++) {
87+
/* is MSB 1 */
88+
if ((crc & 0x80000000) != 0) {
89+
crc = (unsigned int)((crc << 1) ^ polynomial);
90+
} else {
91+
crc <<= 1;
92+
}
93+
}
94+
}
95+
96+
return crc;
97+
}
6898
#endif
6999

70100
extern uint32_t __INITIAL_SP;
@@ -119,6 +149,7 @@ static inline bool mbed_error_is_hw_fault(mbed_error_status_t error_status)
119149
error_status == MBED_ERROR_HARDFAULT_EXCEPTION);
120150
}
121151

152+
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
122153
static bool mbed_error_is_handler(const mbed_error_ctx *ctx)
123154
{
124155
bool is_handler = false;
@@ -136,6 +167,7 @@ static bool mbed_error_is_handler(const mbed_error_ctx *ctx)
136167
}
137168
return is_handler;
138169
}
170+
#endif
139171

140172
//Set an error status with the error handling system
141173
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller)
@@ -179,7 +211,13 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
179211
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
180212
//Capture filename/linenumber if provided
181213
//Index for tracking error_filename
182-
strncpy(current_error_ctx.error_filename, filename, MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN);
214+
const char* copy_from = strrchr(filename, '/');
215+
if (!copy_from) {
216+
copy_from = filename;
217+
} else {
218+
++copy_from; // Skip the '/'
219+
}
220+
strncpy(current_error_ctx.error_filename, copy_from, MBED_CONF_PLATFORM_MAX_ERROR_FILENAME_LEN);
183221
current_error_ctx.error_line_number = line_number;
184222
#endif
185223

platform/source/mbed_os_timer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace internal {
3838
OsTimer *os_timer;
3939

4040
namespace {
41-
uint64_t os_timer_data[(sizeof(OsTimer) + 7) / 8];
41+
[[maybe_unused]] uint64_t os_timer_data[(sizeof(OsTimer) + 7) / 8];
4242
}
4343

4444
OsTimer *init_os_timer()

platform/source/mbed_power_mgmt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "platform/mbed_wait_api.h"
2929

3030
#include <stdio.h>
31+
#include <string.h>
3132

3233
#if DEVICE_SLEEP
3334

platform/source/mbed_retarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ extern "C" MBED_WEAK long int _scanf_mbtowc(
19351935
return 0;
19361936
}
19371937

1938-
static void validate_errno_values(int value)
1938+
[[maybe_unused]] static void validate_errno_values(int value)
19391939
{
19401940

19411941
switch (value) {

0 commit comments

Comments
 (0)