Skip to content

Commit 4820869

Browse files
committedAug 15, 2022
[Gen3] hal: change the hal_i2c_reset() signature to propagate errors.
1 parent 35858bc commit 4820869

File tree

8 files changed

+39
-29
lines changed

8 files changed

+39
-29
lines changed
 

‎hal/inc/hal_dynalib_i2c.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ DYNALIB_FN(13, hal_i2c, hal_i2c_is_enabled, bool(hal_i2c_interface_t, void*))
5858
DYNALIB_FN(14, hal_i2c, hal_i2c_set_callback_on_received, void(hal_i2c_interface_t, void(*)(int), void*))
5959
DYNALIB_FN(15, hal_i2c, hal_i2c_set_callback_on_requested, void(hal_i2c_interface_t, void(*)(void), void*))
6060
DYNALIB_FN(16, hal_i2c, hal_i2c_init, int(hal_i2c_interface_t, const hal_i2c_config_t*))
61-
DYNALIB_FN(17, hal_i2c, hal_i2c_reset, uint8_t(hal_i2c_interface_t, uint32_t, void*))
61+
DYNALIB_FN(17, hal_i2c, hal_i2c_reset, int(hal_i2c_interface_t, uint32_t, void*))
6262
DYNALIB_FN(18, hal_i2c, hal_i2c_lock, int32_t(hal_i2c_interface_t, void*))
6363
DYNALIB_FN(19, hal_i2c, hal_i2c_unlock, int32_t(hal_i2c_interface_t, void*))
6464
DYNALIB_FN(20, hal_i2c, hal_i2c_request_ex, int32_t(hal_i2c_interface_t, const hal_i2c_transmission_config_t*, void*))

‎hal/inc/i2c_hal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void hal_i2c_flush(hal_i2c_interface_t i2c, void* reserved);
105105
bool hal_i2c_is_enabled(hal_i2c_interface_t i2c, void* reserved);
106106
void hal_i2c_set_callback_on_received(hal_i2c_interface_t i2c, void (*function)(int), void* reserved);
107107
void hal_i2c_set_callback_on_requested(hal_i2c_interface_t i2c, void (*function)(void), void* reserved);
108-
uint8_t hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserve1);
108+
int hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserve1);
109109
int hal_i2c_sleep(hal_i2c_interface_t i2c, bool sleep, void* reserved);
110110
int32_t hal_i2c_lock(hal_i2c_interface_t i2c, void* reserved);
111111
int32_t hal_i2c_unlock(hal_i2c_interface_t i2c, void* reserved);

‎hal/src/nRF52840/i2c_hal.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,14 @@ void hal_i2c_enable_dma_mode(hal_i2c_interface_t i2c, bool enable,void* reserved
643643
// use DMA to send data by default
644644
}
645645

646-
uint8_t hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1) {
646+
int hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1) {
647647
if (i2c >= HAL_PLATFORM_I2C_NUM) {
648-
return 1;
648+
return SYSTEM_ERROR_NOT_FOUND;
649649
}
650650

651651
I2cLock lk(i2c);
652652
if (!hal_i2c_is_enabled(i2c, nullptr) || (i2cMap[i2c].mode != I2C_MODE_MASTER)) {
653-
return 1;
653+
return SYSTEM_ERROR_INVALID_STATE;
654654
}
655655

656656
// Important: we keep GPIO configuration intact
@@ -700,7 +700,8 @@ uint8_t hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved
700700
hal_pin_set_function(i2cMap[i2c].scl_pin, PF_I2C);
701701

702702
hal_i2c_begin(i2c, i2cMap[i2c].mode, i2cMap[i2c].address, nullptr);
703-
return !hal_i2c_is_enabled(i2c, nullptr);
703+
CHECK_TRUE(hal_i2c_is_enabled(i2c, nullptr), SYSTEM_ERROR_INTERNAL);
704+
return SYSTEM_ERROR_NONE;
704705
}
705706

706707
int32_t hal_i2c_lock(hal_i2c_interface_t i2c, void* reserved) {

‎hal/src/rtl872x/i2c_hal.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class I2cClass {
200200
return begin((i2cInitStruct_.I2CMaster == I2C_MASTER_MODE) ? I2C_MODE_MASTER : I2C_MODE_SLAVE, i2cInitStruct_.I2CAckAddr);
201201
}
202202

203-
uint8_t reset() {
203+
int reset() {
204204
end();
205205

206206
// Just in case make sure that the pins are correctly configured (they should anyway be at this point)
@@ -215,7 +215,7 @@ class I2cClass {
215215

216216
// Check if slave is stretching the SCL
217217
if (!WAIT_TIMED(HAL_I2C_DEFAULT_TIMEOUT_MS, hal_gpio_read(sclPin_) == 0)) {
218-
return 1;
218+
return SYSTEM_ERROR_I2C_BUS_BUSY;
219219
}
220220

221221
// Generate up to 9 pulses on SCL to tell slave to release the bus
@@ -245,7 +245,8 @@ class I2cClass {
245245

246246
hal_i2c_mode_t mode = (i2cInitStruct_.I2CMaster == I2C_MASTER_MODE) ? I2C_MODE_MASTER : I2C_MODE_SLAVE;
247247
begin(mode, i2cInitStruct_.I2CAckAddr);
248-
return !isEnabled(); // 0 for success, to be consistent with other Gen3 platforms
248+
CHECK_TRUE(isEnabled(), SYSTEM_ERROR_INTERNAL);
249+
return SYSTEM_ERROR_NONE;
249250
}
250251

251252
int setSpeed(uint32_t speed) {
@@ -730,8 +731,8 @@ void hal_i2c_enable_dma_mode(hal_i2c_interface_t i2c, bool enable, void* reserve
730731
// use DMA to send data by default
731732
}
732733

733-
uint8_t hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1) {
734-
auto instance = CHECK_TRUE_RETURN(I2cClass::getInstance(i2c), 1);
734+
int hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1) {
735+
auto instance = CHECK_TRUE_RETURN(I2cClass::getInstance(i2c), SYSTEM_ERROR_NOT_FOUND);
735736
I2cLock lk(instance);
736737
return instance->reset();
737738
}

‎hal/src/template/i2c_hal.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void hal_i2c_enable_dma_mode(hal_i2c_interface_t i2c, bool enable,void* reserved
134134
{
135135
}
136136

137-
uint8_t hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1)
137+
int hal_i2c_reset(hal_i2c_interface_t i2c, uint32_t reserved, void* reserved1)
138138
{
139139
return SYSTEM_ERROR_NONE;
140140
}

‎user/tests/wiring/no_fixture/gpio.cpp

+22-14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ test(GPIO_01_PinModeSetResultsInCorrectMode) {
3939
INPUT_PULLUP,
4040
INPUT_PULLDOWN,
4141
OUTPUT_OPEN_DRAIN,
42+
OUTPUT_OPEN_DRAIN_PULLUP,
4243
#if !HAL_PLATFORM_NRF52840 && !HAL_PLATFORM_RTL872X
4344
// AF_OUTPUT_PUSHPULL,
4445
// AN_INPUT,
@@ -48,7 +49,7 @@ test(GPIO_01_PinModeSetResultsInCorrectMode) {
4849
};
4950
int n = sizeof(mode) / sizeof(mode[0]);
5051
hal_pin_t pin = A0;//pin under test
51-
for(int i=0;i<n;i++)
52+
for (int i = 0; i < n; i++)
5253
{
5354
// when
5455
pinMode(pin, mode[i]);
@@ -81,19 +82,26 @@ test(GPIO_03_NoDigitalWriteWhenPinSelectedIsOutOfRange) {
8182

8283
test(GPIO_04_DigitalWriteOnPinResultsInCorrectDigitalRead) {
8384
hal_pin_t pin = A0;//pin under test
84-
// when
85-
pinMode(pin, OUTPUT);
86-
digitalWrite(pin, HIGH);
87-
PinState pinState = (PinState)digitalRead(pin);
88-
// then
89-
assertEqual(pinState, HIGH);
90-
delay(100);
91-
// when
92-
digitalWrite(pin, LOW);
93-
pinState = (PinState)digitalRead(pin);
94-
// then
95-
assertEqual(pinState, LOW);
96-
//To Do : Add test for remaining pins if required
85+
PinMode mode[] = {
86+
OUTPUT,
87+
OUTPUT_OPEN_DRAIN_PULLUP,
88+
};
89+
int n = sizeof(mode) / sizeof(mode[0]);
90+
for (int i = 0; i < n; i++) {
91+
// when
92+
pinMode(pin, mode[i]);
93+
digitalWrite(pin, HIGH);
94+
PinState pinState = (PinState)digitalRead(pin);
95+
// then
96+
assertEqual(pinState, HIGH);
97+
delay(100);
98+
// when
99+
digitalWrite(pin, LOW);
100+
pinState = (PinState)digitalRead(pin);
101+
// then
102+
assertEqual(pinState, LOW);
103+
//To Do : Add test for remaining pins if required
104+
}
97105
}
98106

99107
#if !HAL_PLATFORM_RTL872X

‎wiring/inc/spark_wiring_i2c.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TwoWire : public Stream
131131
/**
132132
* Attempts to reset this I2C bus.
133133
*/
134-
void reset();
134+
int reset();
135135

136136
hal_i2c_interface_t interface() const {
137137
return _i2c;

‎wiring/src/spark_wiring_i2c.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ bool TwoWire::isEnabled()
199199
return hal_i2c_is_enabled(_i2c, NULL);
200200
}
201201

202-
void TwoWire::reset()
202+
int TwoWire::reset()
203203
{
204-
hal_i2c_reset(_i2c, 0, NULL);
204+
return hal_i2c_reset(_i2c, 0, NULL);
205205
}
206206

207207
bool TwoWire::lock()

0 commit comments

Comments
 (0)
Please sign in to comment.