-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat(board): add support for Arduino Nesso N1 #11985
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
+352
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0756741
feat: add Arduino Nesso N1
facchinm c2b13eb
feat: nesso_n1: protect digital* calls from misuse
facchinm bf961e1
feat: nesso_n1: enable battery
facchinm b29670c
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] 914165a
fix: Spelling error
P-R-O-C-H-Y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #define TwoWire TwoWireInternal | ||
| #define Wire WireInternal | ||
| #define Wire1 WireInternal1 | ||
|
|
||
| #include "pins_arduino.h" | ||
| #include "../../libraries/Wire/src/Wire.h" | ||
| #include "../../libraries/Wire/src/Wire.cpp" | ||
|
|
||
| static bool wireInitialized = false; | ||
|
|
||
| // From https://www.diodes.com/datasheet/download/PI4IOE5V6408.pdf | ||
| static void writeRegister(uint8_t address, uint8_t reg, uint8_t value) { | ||
| WireInternal.beginTransmission(address); | ||
| WireInternal.write(reg); | ||
| WireInternal.write(value); | ||
| WireInternal.endTransmission(); | ||
| } | ||
|
|
||
| static uint8_t readRegister(uint8_t address, uint8_t reg) { | ||
| WireInternal.beginTransmission(address); | ||
| WireInternal.write(reg); | ||
| WireInternal.endTransmission(false); | ||
| WireInternal.requestFrom(address, 1); | ||
| return WireInternal.read(); | ||
| } | ||
|
|
||
| static void writeBitRegister(uint8_t address, uint8_t reg, uint8_t bit, uint8_t value) { | ||
| uint8_t val = readRegister(address, reg); | ||
| if (value) { | ||
| writeRegister(address, reg, val | (1 << bit)); | ||
| } else { | ||
| writeRegister(address, reg, val & ~(1 << bit)); | ||
| } | ||
| } | ||
|
|
||
| static bool readBitRegister(uint8_t address, uint8_t reg, uint8_t bit) { | ||
| uint8_t val = readRegister(address, reg); | ||
| return ((val & (1 << bit)) > 0); | ||
| } | ||
|
|
||
| void pinMode(ExpanderPin pin, uint8_t mode) { | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| // reset all registers to default state | ||
| writeRegister(pin.address, 0x1, 0x1); | ||
| // set all pins as high as default state | ||
| writeRegister(pin.address, 0x9, 0xFF); | ||
| // interrupt mask to all pins | ||
| writeRegister(pin.address, 0x11, 0xFF); | ||
| // all input | ||
| writeRegister(pin.address, 0x3, 0); | ||
| } | ||
| writeBitRegister(pin.address, 0x3, pin.pin, mode == OUTPUT); | ||
| if (mode == OUTPUT) { | ||
| // remove high impedance | ||
| writeBitRegister(pin.address, 0x7, pin.pin, false); | ||
| } else if (mode == INPUT_PULLUP) { | ||
| // set pull-up resistor | ||
| writeBitRegister(pin.address, 0xB, pin.pin, true); | ||
| writeBitRegister(pin.address, 0xD, pin.pin, true); | ||
| } else if (mode == INPUT_PULLDOWN) { | ||
| // disable pull-up resistor | ||
| writeBitRegister(pin.address, 0xB, pin.pin, true); | ||
| writeBitRegister(pin.address, 0xD, pin.pin, false); | ||
| } else if (mode == INPUT) { | ||
| // disable pull selector resistor | ||
| writeBitRegister(pin.address, 0xB, pin.pin, false); | ||
| } | ||
| } | ||
|
|
||
| void digitalWrite(ExpanderPin pin, uint8_t val) { | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| } | ||
| writeBitRegister(pin.address, 0x5, pin.pin, val == HIGH); | ||
| } | ||
|
|
||
| int digitalRead(ExpanderPin pin) { | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| } | ||
| return readBitRegister(pin.address, 0xF, pin.pin); | ||
| } | ||
|
|
||
| void NessoBattery::enableCharge() { | ||
| // AW32001E - address 0x49 | ||
| // set CEB bit low (charge enable) | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| } | ||
| writeBitRegister(0x49, 0x1, 3, false); | ||
| } | ||
|
|
||
| float NessoBattery::getVoltage() { | ||
| // BQ27220 - address 0x55 | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| } | ||
| uint16_t voltage = (readRegister(0x55, 0x9) << 8) | readRegister(0x55, 0x8); | ||
| return (float)voltage / 1000.0f; | ||
| } | ||
|
|
||
| uint16_t NessoBattery::getChargeLevel() { | ||
| // BQ27220 - address 0x55 | ||
| if (!wireInitialized) { | ||
| WireInternal.begin(SDA, SCL); | ||
| wireInitialized = true; | ||
| } | ||
| uint16_t current_capacity = readRegister(0x55, 0x11) << 8 | readRegister(0x55, 0x10); | ||
| uint16_t total_capacity = readRegister(0x55, 0x13) << 8 | readRegister(0x55, 0x12); | ||
| return (current_capacity * 100) / total_capacity; | ||
| } | ||
|
|
||
| ExpanderPin LORA_LNA_ENABLE(5); | ||
| ExpanderPin LORA_ANTENNA_SWITCH(6); | ||
| ExpanderPin LORA_ENABLE(7); | ||
| ExpanderPin KEY1(0); | ||
| ExpanderPin KEY2(1); | ||
| ExpanderPin POWEROFF((1 << 8) | 0); | ||
| ExpanderPin LCD_RESET((1 << 8) | 1); | ||
| ExpanderPin GROVE_POWER_EN((1 << 8) | 2); | ||
| ExpanderPin VIN_DETECT((1 << 8) | 5); | ||
| ExpanderPin LCD_BACKLIGHT((1 << 8) | 6); | ||
| ExpanderPin LED_BUILTIN((1 << 8) | 7); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #ifndef Pins_Arduino_h | ||
| #define Pins_Arduino_h | ||
|
|
||
| #include <stdint.h> | ||
| #include "soc/soc_caps.h" | ||
|
|
||
| #define USB_VID 0x303A | ||
| #define USB_PID 0x1001 | ||
| #define USB_MANUFACTURER "Arduino" | ||
| #define USB_PRODUCT "Nesso N1" | ||
| #define USB_SERIAL "" | ||
|
|
||
| static const uint8_t TX = -1; | ||
| static const uint8_t RX = -1; | ||
|
|
||
| static const uint8_t SDA = 10; | ||
| static const uint8_t SCL = 8; | ||
|
|
||
| static const uint8_t MOSI = 21; | ||
| static const uint8_t MISO = 22; | ||
| static const uint8_t SCK = 20; | ||
| static const uint8_t SS = 23; | ||
|
|
||
| static const uint8_t D1 = 7; | ||
| static const uint8_t D2 = 2; | ||
| static const uint8_t D3 = 6; | ||
|
|
||
| static const uint8_t IR_TX_PIN = 9; | ||
| static const uint8_t BEEP_PIN = 11; | ||
|
|
||
| static const uint8_t GROVE_IO_0 = 5; | ||
| static const uint8_t GROVE_IO_1 = 4; | ||
|
|
||
| static const uint8_t LORA_IRQ = 15; | ||
| static const uint8_t LORA_CS = 23; | ||
| static const uint8_t LORA_BUSY = 19; | ||
|
|
||
| static const uint8_t SYS_IRQ = 3; | ||
|
|
||
| static const uint8_t LCD_CS = 17; | ||
| static const uint8_t LCD_RS = 16; | ||
|
|
||
| #if !defined(MAIN_ESP32_HAL_GPIO_H_) && defined(__cplusplus) | ||
| /* address: 0x43/0x44 */ | ||
| class ExpanderPin { | ||
| public: | ||
| ExpanderPin(uint16_t _pin) : pin(_pin & 0xFF), address(_pin & 0x100 ? 0x44 : 0x43){}; | ||
| uint8_t pin; | ||
| uint8_t address; | ||
| }; | ||
|
|
||
| class NessoBattery { | ||
| public: | ||
| NessoBattery(){}; | ||
| void enableCharge(); // enable charging | ||
| float getVoltage(); // get battery voltage in Volts | ||
| uint16_t getChargeLevel(); // get battery charge level in percents | ||
| }; | ||
|
|
||
| extern ExpanderPin LORA_LNA_ENABLE; | ||
| extern ExpanderPin LORA_ANTENNA_SWITCH; | ||
| extern ExpanderPin LORA_ENABLE; | ||
| extern ExpanderPin POWEROFF; | ||
| extern ExpanderPin GROVE_POWER_EN; | ||
| extern ExpanderPin VIN_DETECT; | ||
| extern ExpanderPin LCD_RESET; | ||
| extern ExpanderPin LCD_BACKLIGHT; | ||
| extern ExpanderPin LED_BUILTIN; | ||
| extern ExpanderPin KEY1; | ||
| extern ExpanderPin KEY2; | ||
|
|
||
| void pinMode(ExpanderPin pin, uint8_t mode); | ||
| void digitalWrite(ExpanderPin pin, uint8_t val); | ||
| int digitalRead(ExpanderPin pin); | ||
| #endif | ||
|
|
||
| #endif /* Pins_Arduino_h */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Sign check of bitwise operation Warning