Skip to content
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

[GLOBAL] Refactor: HAL #664

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1fc1e9c
HAL: PMIC - First Commit, Replaces XPowersLib
rouing Jan 8, 2025
3fd047d
Attempt 1: Fixing CTOR
rouing Jan 8, 2025
682217a
Attempt 2: Fixing
rouing Jan 9, 2025
7455157
Attempt 3: Fixing M5SCS3
rouing Jan 9, 2025
24411dd
Attempt 3: Pointers?
rouing Jan 9, 2025
24b2f33
Attempt 4: Fix Includes
rouing Jan 9, 2025
274305d
Attempt 5: Pointers
rouing Jan 9, 2025
aab3a33
Attempt 6: More fixes
rouing Jan 9, 2025
39c3d1c
Attempt 7: Typedef issues
rouing Jan 9, 2025
656647b
Milestone?
rouing Jan 9, 2025
e895167
Fix Smoochie board
rouing Jan 9, 2025
3112f9e
Merge branch 'pr3y:main' into dev-hal
rouing Jan 9, 2025
fa8e098
[PMIC] [SY6970] Fix/Cleanup
rouing Jan 9, 2025
26b8acb
[RFID] [PN532] Add Datasheet
rouing Jan 9, 2025
3c79349
[FUEL] [BQ7220] Add Registers
rouing Jan 9, 2025
9f77274
[FUEL] [BQ7220] Header Guard
rouing Jan 9, 2025
8bf2893
[RTC] [BM8563] Add Registers
rouing Jan 10, 2025
53ed10b
Merge branch 'dev-hal' of https://github.com/rouing/Bruce into dev-hal
rouing Jan 10, 2025
a09ff24
[RTC] [BM8563] Add Datasheet
rouing Jan 10, 2025
4280867
[RTC] Library Prep
rouing Jan 10, 2025
dd4efa6
[RTC] Prep
rouing Jan 11, 2025
8c8f264
[PMIC] Cleanup
rouing Jan 11, 2025
7944a04
[FUEL] Prep
rouing Jan 11, 2025
604d226
[m5stack-cplus1_1] Prep conversion to HAL lib
rouing Jan 11, 2025
bcf47be
Add clang-format
rouing Jan 11, 2025
d066eb3
Merge branch 'pr3y:main' into dev-hal
rouing Jan 11, 2025
740fb2a
Formatting
rouing Jan 11, 2025
c1626bb
[Global] Cang formatter adjust
rouing Jan 11, 2025
f502dd0
add to gitignore
rouing Jan 11, 2025
9bdeb96
.
rouing Jan 11, 2025
5d50371
[PMIC] Chores
rouing Jan 11, 2025
740b00e
Datasheets
rouing Jan 12, 2025
fad566e
Clang Formatter
rouing Jan 12, 2025
cc74d1c
.
rouing Jan 12, 2025
c153bf6
[RTC] More Prep
rouing Jan 12, 2025
b9d838f
Chores
rouing Jan 12, 2025
0c13f13
[RTC] Additions
rouing Jan 16, 2025
23af6e4
[PMIC] .
rouing Jan 16, 2025
3793c09
[BUS] Add SMBus
rouing Jan 17, 2025
76fee60
[PMIC] Datasheet Updates
rouing Jan 18, 2025
ea97d10
Merge branch 'pr3y:main' into dev-hal
rouing Jan 19, 2025
529e9bb
Merge branch 'pr3y:main' into dev-hal
rouing Jan 21, 2025
4f01f00
Merge branch 'pr3y:main' into dev-hal
rouing Jan 22, 2025
54b68cf
Bunch of stuff
rouing Jan 22, 2025
0549c19
[PMIC] More changes
rouing Jan 22, 2025
db0324c
Fix?
rouing Jan 22, 2025
607ff33
[SMBUS[ Fix
rouing Jan 22, 2025
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
46 changes: 46 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 4
TabWidth: 2
BreakBeforeBraces: Allman
IncludeBlocks: Regroup
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: true
BitFieldColonSpacingStyle: Both
AllowShortIfStatementsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortNamespacesOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlignConsecutiveDeclarations: true
AlignArrayOfStructures: Left
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCaseColons: true
AlignConsecutiveTableGenDefinitionColons: true
AlignTrailingComments: true
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: true
SortUsingDeclarations: LexicographicNumeric
SpaceBeforeCaseColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeJsonColon: true
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
IndentExternBlock: Indent
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Checks: '-*,clang-analyzer-cplusplus*,llvm-header-guard'
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3208150197b1bd422021c608f57b9b3e4e7040f9

1b84d09669504d4c0c6ab25678ed7c15bd610432
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.vscode/launch.json
.vscode/extensions.json
.vscode/ipch
.vscode/settings.json
Bruce3_*.bin
bruce.conf
_*.sh
Expand Down
9 changes: 5 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"idf.portWin": "COM4",
"files.associations": {
"iosfwd": "cpp",
"iostream": "cpp",
Expand Down Expand Up @@ -57,10 +56,12 @@
"iomanip": "cpp",
"chrono": "cpp",
"ratio": "cpp",
"*.tpp": "cpp"
"*.tpp": "cpp",
"condition_variable": "cpp",
"mutex": "cpp",
"thread": "cpp"
},
"githubPullRequests.ignoredPullRequestBranches": [
"main"
],
"cmake.sourceDirectory": "C:/Projetos/Bruce/lib/M5GFX"
]
}
1 change: 0 additions & 1 deletion boards/lilygo-t-embed-cc1101.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ build_flags =

lib_deps =
${env.lib_deps}
lewisxhe/XPowersLib @0.2.6
mathertel/RotaryEncoder @1.5.3
fastled/FastLED @3.9.4
35 changes: 18 additions & 17 deletions boards/lilygo-t-embed-cc1101/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ IRAM_ATTR void checkPosition();
#if defined(T_EMBED_1101)
// Power handler for battery detection
#include <Wire.h>
#include <XPowersLib.h>
#include <HAL.hpp>
#include <esp32-hal-dac.h>
XPowersPPM PPM;
PMIC pmic;
#elif defined(T_EMBED)
#include <driver/adc.h>
#include <esp_adc_cal.h>
Expand All @@ -25,6 +25,7 @@ IRAM_ATTR void checkPosition();
#include <bq27220.h>
BQ27220 bq;
#endif
TwoWire* myWire = &Wire;
/***************************************************************************************
** Function name: _setup_gpio()
** Description: initial setup for the device
Expand All @@ -47,22 +48,22 @@ void _setup_gpio() {
pinMode(PIN_POWER_ON, OUTPUT);
digitalWrite(PIN_POWER_ON, HIGH); // Power on CC1101 and LED
bool pmu_ret = false;
Wire.begin(GROVE_SDA, GROVE_SCL);
pmu_ret = PPM.init(Wire, GROVE_SDA, GROVE_SCL, BQ25896_SLAVE_ADDRESS);
myWire->begin(GROVE_SDA, GROVE_SCL);
pmu_ret = pmic.init(Wire, GROVE_SDA, GROVE_SCL, BQ25896_SLAVE_ADDRESS);
if(pmu_ret) {
PPM.setSysPowerDownVoltage(3300);
PPM.setInputCurrentLimit(3250);
Serial.printf("getInputCurrentLimit: %d mA\n",PPM.getInputCurrentLimit());
PPM.disableCurrentLimitPin();
PPM.setChargeTargetVoltage(4208);
PPM.setPrechargeCurr(64);
PPM.setChargerConstantCurr(832);
PPM.getChargerConstantCurr();
Serial.printf("getChargerConstantCurr: %d mA\n",PPM.getChargerConstantCurr());
PPM.enableADCMeasure();
PPM.enableCharge();
PPM.enableOTG();
PPM.disableOTG();
pmic.setSysPowerDownVoltage(3300);
pmic.setInputCurrentLimit(3250);
Serial.printf("getInputCurrentLimit: %d mA\n",pmic.getInputCurrentLimit());
pmic.disableCurrentLimitPin();
pmic.setChargeTargetVoltage(4208);
pmic.setPrechargeCurr(64);
pmic.setChargerConstantCurr(832);
pmic.getChargerConstantCurr();
Serial.printf("getChargerConstantCurr: %d mA\n",pmic.getChargerConstantCurr());
pmic.enableMeasure(HAL::PMIC::MeasureMode::CONTINUOUS);
pmic.enableCharge();
pmic.enableOTG();
pmic.disableOTG();
}
#else
pinMode(BAT_PIN,INPUT); // Battery value
Expand Down
1 change: 0 additions & 1 deletion boards/lilygo-t-embed.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ build_flags =

lib_deps =
${env.lib_deps}
lewisxhe/XPowersLib @0.2.6
mathertel/RotaryEncoder @1.5.3
fastled/FastLED @3.9.4
8 changes: 6 additions & 2 deletions boards/m5stack-cores3/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "core/utils.h"

#include <M5Unified.h>
#include <HAL.hpp>
PMIC pmic;

/***************************************************************************************
** Function name: _setup_gpio()
Expand All @@ -21,7 +23,7 @@ void _setup_gpio() {
***************************************************************************************/
int getBattery() {
int percent;
percent = M5.Power.getBatteryLevel();
percent = pmic.getBatteryPercent();
return (percent < 0) ? 0
: (percent >= 100) ? 100
: percent;
Expand Down Expand Up @@ -88,7 +90,9 @@ void InputHandler(void) {
** location: mykeyboard.cpp
** Turns off the device (or try to)
**********************************************************************/
void powerOff() { }
void powerOff() {
pmic.shutdown();
}


/*********************************************************************
Expand Down
4 changes: 4 additions & 0 deletions boards/pinouts/lilygo-t-embed-cc1101.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <stdint.h>
#include "soc/soc_caps.h"

//HAL Definitions
#define HAL_PMIC
#define PMIC_BQ25896

// Main I2C Bus
#define SPI_SS_PIN 8
#define SPI_MOSI_PIN 9
Expand Down
3 changes: 3 additions & 0 deletions boards/pinouts/m5stack-cores3.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ static const uint8_t G46 = 46;

static const uint8_t ADC = 10;

#define HAL_PMIC
#define PMIC_AXP2101

#define HAS_RTC
#define USB_as_HID

Expand Down
2 changes: 2 additions & 0 deletions boards/pinouts/m5stack-cplus1_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ static const uint8_t DAC2 = 26;
static const uint8_t ADC1 = 35;
static const uint8_t ADC2 = 36;

#define PMIC_AXP192

#endif /* Pins_Arduino_h */
10 changes: 7 additions & 3 deletions boards/pinouts/smoochiee-board.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef Pins_Arduino_h
#define Pins_Arduino_h
#ifndef __SMOOCHIEE_BOARD__
#define __SMOOCHIEE_BOARD__

#include <stdint.h>
#include "soc/soc_caps.h"
Expand Down Expand Up @@ -46,6 +46,10 @@ static const uint8_t G46 = 46;
static const uint8_t ADC1 = 7;
static const uint8_t ADC2 = 8;

// HAL Definitions
#define HAL_PMIC
#define PMIC_BQ25896

#define SERIAL_RX 2
#define SERIAL_TX 1
#define BAD_RX SERIAL_RX
Expand Down Expand Up @@ -121,4 +125,4 @@ static const uint8_t ADC2 = 8;
#define SPI_MISO_PIN 11
#define SPI_SS_PIN 43

#endif /* Pins_Arduino_h */
#endif /* __SMOOCHIEE_BOARD__ */
3 changes: 1 addition & 2 deletions boards/smoochiee-board.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ build_flags =
-DRF_RX_PINS='{{"M5 RF433R", GROVE_SCL}, {"Groove W", GROVE_SCL}, {"GROVE Y", GROVE_SDA}, {"GPIO 1", 1}, {"GPIO 2", 2}}'

lib_deps =
${env.lib_deps}
lewisxhe/XPowersLib @0.2.6
${env.lib_deps}
34 changes: 17 additions & 17 deletions boards/smoochiee-board/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Power handler for battery detection
#include <Wire.h>
#include <XPowersLib.h>
XPowersPPM PPM;
#include <HAL.hpp>
PMIC pmic;


void _setup_gpio() {
Expand All @@ -42,21 +42,21 @@ void _setup_gpio() {

bool pmu_ret = false;
Wire.begin(GROVE_SDA, GROVE_SCL);
pmu_ret = PPM.init(Wire, GROVE_SDA, GROVE_SCL, BQ25896_SLAVE_ADDRESS);
pmu_ret = pmic.init(Wire, GROVE_SDA, GROVE_SCL, BQ25896_SLAVE_ADDRESS);
if(pmu_ret) {
PPM.setSysPowerDownVoltage(3300);
PPM.setInputCurrentLimit(3250);
Serial.printf("getInputCurrentLimit: %d mA\n",PPM.getInputCurrentLimit());
PPM.disableCurrentLimitPin();
PPM.setChargeTargetVoltage(4208);
PPM.setPrechargeCurr(64);
PPM.setChargerConstantCurr(832);
PPM.getChargerConstantCurr();
Serial.printf("getChargerConstantCurr: %d mA\n",PPM.getChargerConstantCurr());
PPM.enableADCMeasure();
PPM.enableCharge();
PPM.enableOTG();
PPM.disableOTG();
pmic.setSysPowerDownVoltage(3300);
pmic.setInputCurrentLimit(3250);
Serial.printf("getInputCurrentLimit: %d mA\n",pmic.getInputCurrentLimit());
pmic.disableCurrentLimitPin();
pmic.setChargeTargetVoltage(4208);
pmic.setPrechargeCurr(64);
pmic.setChargerConstantCurr(832);
pmic.getChargerConstantCurr();
Serial.printf("getChargerConstantCurr: %d mA\n",pmic.getChargerConstantCurr());
pmic.enableMeasure(HAL::PMIC::MeasureMode::CONTINUOUS);
pmic.enableCharge();
pmic.enableOTG();
pmic.disableOTG();
}

}
Expand All @@ -69,7 +69,7 @@ void _setup_gpio() {
***************************************************************************************/
int getBattery() {
uint8_t percent=0;
percent=(PPM.getSystemVoltage()-3300)*100/(float)(4150-3350);
percent=(pmic.getSystemVoltage()-3300)*100/(float)(4150-3350);

return (percent < 0) ? 0
: (percent >= 100) ? 100
Expand Down
84 changes: 84 additions & 0 deletions lib/HAL/BUS/I2C/I2C_Device.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <Wire.h>
#include <Arduino.h>
namespace HAL::BUS
{
template <class Device>
class I2C_Device
{
public:
int readRegister(uint8_t reg)
{
uint8_t val = 0;
return readRegister(reg, &val, 1) == -1 ? -1 : val;
}

int writeRegister(uint8_t reg, uint8_t val)
{
return writeRegister(reg, &val, 1);
}

int readRegister(uint8_t reg, uint8_t *buf, uint8_t length = 1)
{
if (myWire)
{
myWire->beginTransmission(myADDR);
myWire->write(reg);
if (myWire->endTransmission() != 0)
{
return -1;
}
myWire->requestFrom(myADDR, length);
return myWire->readBytes(buf, length) == length ? 0 : -1;
}
return -1;
}

int writeRegister(uint8_t reg, uint8_t *buf, uint8_t length)
{
if (myWire)
{
myWire->beginTransmission(myADDR);
myWire->write(reg);
myWire->write(buf, length);
return (myWire->endTransmission() == 0) ? 0 : -1;
}
return -1;
}

bool inline clrRegisterBit(uint8_t registers, uint8_t bit)
{
int val = readRegister(registers);
if (val == -1)
{
return false;
}
return writeRegister(registers, (val & (~_BV(bit)))) == 0;
}

bool inline setRegisterBit(uint8_t registers, uint8_t bit)
{
int val = readRegister(registers);
if (val == -1)
{
return false;
}
return writeRegister(registers, (val | (_BV(bit)))) == 0;
}

bool inline getRegisterBit(uint8_t registers, uint8_t bit)
{
int val = readRegister(registers);
if (val == -1)
{
return false;
}
return val & _BV(bit);
}

protected:
TwoWire *myWire = NULL;
int mySDA = -1;
int mySCL = -1;
uint8_t myADDR = 0xFF;
};
}
Loading
Loading