From f6fced2e954f5d8bee5443a8006f3fcc95e9b230 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 25 Jan 2022 13:56:37 +0100 Subject: [PATCH 1/5] Remove total redundant statement. --- src/Braccio++.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index 61eae7f..8a93789 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -73,11 +73,8 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) SPI.begin(); - int ret = _expander.testConnection(); - - if (ret == false) { - return ret; - } + if (!_expander.testConnection()) + return false; for (int i = 0; i < 14; i++) { _expander.setPinDirection(i, 0); From 3eb0154168f3215525fd3afcb4a7ab5ead919800 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 25 Jan 2022 13:57:09 +0100 Subject: [PATCH 2/5] Move SPI.begin() to the start of BraccioClass::begin(). --- src/Braccio++.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index 8a93789..f077ecb 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -44,6 +44,7 @@ BraccioClass::BraccioClass() bool BraccioClass::begin(voidFuncPtr custom_menu) { + SPI.begin(); Wire.begin(); Serial.begin(115200); @@ -71,7 +72,6 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) return false; _bl.turnOn(); - SPI.begin(); if (!_expander.testConnection()) return false; From 04149e32551d61dbe9616d6215c73643fe5070ab Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 25 Jan 2022 14:05:45 +0100 Subject: [PATCH 3/5] Aggregate IO expander code initialisation within expander_init() function. --- src/Braccio++.cpp | 55 +++++++++++++++++++++++++++-------------------- src/Braccio++.h | 1 + 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index f077ecb..cd72f87 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -72,31 +72,9 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) return false; _bl.turnOn(); - - if (!_expander.testConnection()) + if (!expander_init()) return false; - for (int i = 0; i < 14; i++) { - _expander.setPinDirection(i, 0); - } - - // Set SLEW to low - _expander.setPinDirection(21, 0); // P25 = 8 * 2 + 5 - _expander.writePin(21, 0); - - // Set TERM to HIGH (default) - _expander.setPinDirection(19, 0); // P23 = 8 * 2 + 3 - _expander.writePin(19, 1); - - _expander.setPinDirection(18, 0); // P22 = 8 * 2 + 2 - _expander.writePin(18, 0); // reset LCD - _expander.writePin(18, 1); // LCD out of reset - - /* Set all motor status LEDs to red. */ - for (int id = SmartServoClass::MIN_MOTOR_ID; id <= SmartServoClass::MAX_MOTOR_ID; id++) { - setRed(id); - } - pinMode(BTN_LEFT, INPUT_PULLUP); pinMode(BTN_RIGHT, INPUT_PULLUP); pinMode(BTN_UP, INPUT_PULLUP); @@ -332,6 +310,37 @@ void BraccioClass::lvgl_defaultMenu() lv_obj_set_pos(label1, 0, 0); } +bool BraccioClass::expander_init() +{ + if (!_expander.testConnection()) + return false; + + /* Init IO expander outputs of RGB LEDs */ + for (int i = 0; i < 14; i++) { + _expander.setPinDirection(i, 0); + } + + /* Set SLEW to low */ + _expander.setPinDirection(21, 0); // P25 = 8 * 2 + 5 + _expander.writePin(21, 0); + + /* Set TERM to HIGH (default) */ + _expander.setPinDirection(19, 0); // P23 = 8 * 2 + 3 + _expander.writePin(19, 1); + + /* Reset GLCD */ + _expander.setPinDirection(18, 0); // P22 = 8 * 2 + 2 + _expander.writePin(18, 0); // reset LCD + _expander.writePin(18, 1); // LCD out of reset + + /* Set all motor status LEDs to red. */ + for (int id = SmartServoClass::MIN_MOTOR_ID; id <= SmartServoClass::MAX_MOTOR_ID; id++) { + setRed(id); + } + + return true; +} + bool BraccioClass::isPingAllowed() { mbed::ScopedLock lock(_motors_connected_mtx); diff --git a/src/Braccio++.h b/src/Braccio++.h index 2e75ed5..968c1d9 100644 --- a/src/Braccio++.h +++ b/src/Braccio++.h @@ -77,6 +77,7 @@ class BraccioClass SmartServoClass servos; PD_UFP_log_c PD_UFP; TCA6424A _expander; + bool expander_init(); bool _is_ping_allowed; bool _is_motor_connected[SmartServoClass::NUM_MOTORS]; From 2f5fe586d00c52929cebd5c6f4907cb8ea709654 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 25 Jan 2022 14:11:33 +0100 Subject: [PATCH 4/5] Using named constants instead of magic numbers. --- src/Braccio++.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index cd72f87..c52ba7f 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -312,6 +312,10 @@ void BraccioClass::lvgl_defaultMenu() bool BraccioClass::expander_init() { + static uint16_t constexpr EXPANDER_RS485_SLEW_PIN = 21; /* P25 = 8 * 2 + 5 = 21 */ + static uint16_t constexpr EXPANDER_RS485_TERM_PIN = 19; /* P23 = 8 * 2 + 3 = 19 */ + static uint16_t constexpr EXPANDER_RS485_RST_DP_PIN = 18; /* P22 = 8 * 2 + 2 = 18 */ + if (!_expander.testConnection()) return false; @@ -321,17 +325,17 @@ bool BraccioClass::expander_init() } /* Set SLEW to low */ - _expander.setPinDirection(21, 0); // P25 = 8 * 2 + 5 - _expander.writePin(21, 0); + _expander.setPinDirection(EXPANDER_RS485_SLEW_PIN, 0); + _expander.writePin(EXPANDER_RS485_SLEW_PIN, 0); /* Set TERM to HIGH (default) */ - _expander.setPinDirection(19, 0); // P23 = 8 * 2 + 3 - _expander.writePin(19, 1); + _expander.setPinDirection(EXPANDER_RS485_TERM_PIN, 0); + _expander.writePin(EXPANDER_RS485_TERM_PIN, 1); /* Reset GLCD */ - _expander.setPinDirection(18, 0); // P22 = 8 * 2 + 2 - _expander.writePin(18, 0); // reset LCD - _expander.writePin(18, 1); // LCD out of reset + _expander.setPinDirection(EXPANDER_RS485_RST_DP_PIN, 0); // P22 = 8 * 2 + 2 + _expander.writePin(EXPANDER_RS485_RST_DP_PIN, 0); // reset LCD + _expander.writePin(EXPANDER_RS485_RST_DP_PIN, 1); // LCD out of reset /* Set all motor status LEDs to red. */ for (int id = SmartServoClass::MIN_MOTOR_ID; id <= SmartServoClass::MAX_MOTOR_ID; id++) { From cc81e78ebab204626afe7c020ca55beda21bb246 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 25 Jan 2022 14:15:18 +0100 Subject: [PATCH 5/5] Removing __MBED__ includes, moving i2c_mutex up and renaming to _i2c_mtx. --- src/Braccio++.cpp | 17 ++++++++--------- src/Braccio++.h | 6 ++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index c52ba7f..b1c9384 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -21,15 +21,16 @@ extern "C" { using namespace std::chrono_literals; BraccioClass::BraccioClass() -: serial485{Serial1, 0, 7, 8} /* TX, DE, RE */ +: _i2c_mtx{} +, serial485{Serial1, 0, 7, 8} /* TX, DE, RE */ , servos{serial485} , PD_UFP{PD_LOG_LEVEL_VERBOSE} -, _expander{TCA6424A_ADDRESS_ADDR_HIGH, i2c_mutex} +, _expander{TCA6424A_ADDRESS_ADDR_HIGH, _i2c_mtx} , _is_ping_allowed{true} , _is_motor_connected{false} , _motors_connected_mtx{} , _motors_connected_thd{} -, _bl{i2c_mutex} +, _bl{_i2c_mtx} , _gfx{} , _lvgl_disp_drv{} , _lvgl_indev_drv{} @@ -50,12 +51,10 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) pinMode(PIN_FUSB302_INT, INPUT_PULLUP); -#ifdef __MBED__ static rtos::Thread th(osPriorityHigh); th.start(mbed::callback(this, &BraccioClass::pd_thread)); attachInterrupt(PIN_FUSB302_INT, mbed::callback(this, &BraccioClass::unlock_pd_semaphore_irq), FALLING); pd_timer.attach(mbed::callback(this, &BraccioClass::unlock_pd_semaphore), 10ms); -#endif PD_UFP.init_PPS(PPS_V(7.2), PPS_A(2.0)); @@ -119,11 +118,11 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) { if (!PD_UFP.is_PPS_ready()) { - i2c_mutex.lock(); + _i2c_mtx.lock(); PD_UFP.print_status(Serial); PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0)); delay(10); - i2c_mutex.unlock(); + _i2c_mtx.unlock(); } }; @@ -243,13 +242,13 @@ void BraccioClass::pd_thread() { pd_timer.detach(); pd_timer.attach(mbed::callback(this, &BraccioClass::unlock_pd_semaphore), 50ms); } - i2c_mutex.lock(); + _i2c_mtx.lock(); if (millis() - last_time_ask_pps > 5000) { PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0)); last_time_ask_pps = millis(); } PD_UFP.run(); - i2c_mutex.unlock(); + _i2c_mtx.unlock(); if (PD_UFP.is_power_ready() && PD_UFP.is_PPS_ready()) { } diff --git a/src/Braccio++.h b/src/Braccio++.h index 968c1d9..5c66713 100644 --- a/src/Braccio++.h +++ b/src/Braccio++.h @@ -73,6 +73,7 @@ class BraccioClass private: + rtos::Mutex _i2c_mtx; RS485Class serial485; SmartServoClass servos; PD_UFP_log_c PD_UFP; @@ -113,9 +114,8 @@ class BraccioClass void lvgl_pleaseConnectPower(); void lvgl_defaultMenu(); -#ifdef __MBED__ + rtos::EventFlags pd_events; - rtos::Mutex i2c_mutex; mbed::Ticker pd_timer; unsigned int start_pd_burst = 0xFFFFFFFF; @@ -140,8 +140,6 @@ class BraccioClass } void pd_thread(); -#endif - }; #define Braccio BraccioClass::get_default_instance()