Files
IronOS/source/Core/Drivers/Si7210.cpp
Ben V. Brown baf2f26e59 Big overhaul of the UI framework (#1749)
* Starting GUI render refactor to be more immediate mode

Update TemperatureAdjust.cpp

.

Cleanup Soldering

Sleep

SolderingProfiles

Soldering Rework

Rough pass GUI

Temp Adjust

Cleanup old OperatingMode

Debug Menu

* Update TemperatureAdjust.cpp

* Roughing some transition work

* Fixup! Hook in the init starter helper

* Better home screen button handler

* FIXUP! Fix typo's

.

* Update SettingsMenu.cpp

* More settings rework

* More settings rendering

* Fixup

* Transitions

Update SolderingProfile.cpp

Hook in transistions

* Update TemperatureAdjust.cpp

* Update push.yml

* Add auto-repeat to settings menu

* Miniware: Use IT for I2C writes

* Update USBPDDebug_HUSB238.cpp

* Force write screen on side animation cancel

.

* Refactor moving down the settings list

* Update settingsGUI.cpp

* Update I2C_Wrapper.cpp

* Update OLED.cpp

* Rework button handling

* Fix PD debug at boot

* Fixup not showing right menu options

* silence some warnings

* Style cleanup

* Fkit use bit-bang I2C for Miniware

* Update GUIRendering.md

* Fixup transition on enter soldering mode

* Save Settings

* Fixes for some animations not running

Dont bail on animations if keypress is still held

* Fixup settings acceleration

* OLED Up animation

* Link up/down on debug meny

* Make all accelerometers I2C bus aware

Update accelerometers_common.h

* Make I2C mag optional

* Miniware -> Only Bit-Bang I2C

* Fixup for scrollbar

FIXUP! Debug menu returns to home screen

FIXUP! Up oled animation

Fix temp exit

* Settings menu -> Both buttons return a menu layer

* Merge fixup

* Update BMA223.cpp

* Re-Enable OLED sleep

* Save Setting on temp adjust exit

* WiP on startup mode

* Some autostart working

* Add hibernation mode & more autostart fixes

* If cant CJC; go to startup

* Hibernate in sleep

* Cleanup scroll indicator

* FIXUP! Ensure startup warnings are linked in

* FIXUP! Ensure we render out temp change before timing out

* Ensure 100ms delay between CJC samples

* Fix not re-calculating menu length on entering menu

* Implement NegotiationinProgress for USB-PD

* Mask heating until PD finishes negotiation

* Fixup staying in hibernate correctly

* Warning timeout

* Show reset settings warning

* Correctly compensate help text start time

* Update GUIThread.cpp

* Update USBPD.cpp

* .

* Fixup sleep time

* Update printSleepCountdown.cpp

* replacing countdown with big plus while in boost mode

* bringing back the + 1 since it was missing when not in boost mode

* Bail on USB-PD check after 3 seconds incase of DC source

* Fix hibernate

* Update PIDThread.cpp

* did center plus symbol (boost mode)

* Big refactor to not make settings increment handler handle the "is last item" return

* Fixup boot logo

* Fix flashing

* Fixup recalculate the menu length on long hold

* Fixup missing menu entries

* Fix junk left on screen after user confirmation

* Re-order button handler to use custom, then default order to allow setting associated setting

* Attach setting for settings using custom handler

* Fix swap +/- keys

* Fix boost temp

* Implement last menu option for Language selector

* Wait for init before CJC runs

* Check last setting via increment value

* Update BSP.cpp

* removed = from >=

Otherwise incrementing would stop and the scroll bar would already flash at the second to last value.

* (Hacky) Fix for Settings reset

---------

Co-authored-by: discip <53649486+discip@users.noreply.github.com>
2024-02-18 09:42:08 +11:00

189 lines
5.6 KiB
C++

/*
* Si7210.cpp
*
* Created on: 5 Oct. 2020
* Author: Ralim
*
* This is based on the very nice sample code by Sean Farrelly (@FARLY7)
* Over here : https://github.com/FARLY7/si7210-driver
*
* This class is licensed as MIT to match this code base
*/
#include "Si7210_defines.h"
#include "accelerometers_common.h"
#include <Si7210.h>
#ifdef MAG_SLEEP_SUPPORT
bool Si7210::detect() { return FRToSI2C::wakePart(SI7210_ADDRESS); }
bool Si7210::init() {
// Turn on auto increment and sanity check ID
// Load OTP cal
uint8_t temp;
if (ACCEL_I2C_CLASS::Mem_Read(SI7210_ADDRESS, SI7210_REG_ID, &temp, 1)) {
// We don't really care what model it is etc, just probing to check its probably this iC
if (temp != 0x00 && temp != 0xFF) {
temp = 0x00;
/* Set device and internal driver settings */
if (!write_reg(SI7210_CTRL1, (uint8_t)~SW_LOW4FIELD_MASK, 0)) {
return false;
}
/* Disable periodic auto-wakeup by device, and tamper detect. */
if ((!write_reg(SI7210_CTRL3, (uint8_t)~SL_TIMEENA_MASK, 0))) {
return false;
}
/* Disable tamper detection by setting sw_tamper to 63 */
if (!write_reg(SI7210_CTRL3, SL_FAST_MASK | SL_TIMEENA_MASK, 63 << 2)) {
return false;
}
if (!set_high_range()) {
return false;
}
/* Stop the control loop by setting stop bit */
if (!write_reg(SI7210_POWER_CTRL, MEAS_MASK | USESTORE_MASK, STOP_MASK)) { /* WARNING: Removed USE_STORE MASK */
return false;
}
/* Use a burst size of 128/4096 samples in FIR and IIR modes */
if (!write_reg(SI7210_CTRL4, 0, DF_BURSTSIZE_128 | DF_BW_4096)) {
return false;
}
/* Select field strength measurement */
if (!write_reg(SI7210_DSPSIGSEL, 0, DSP_SIGSEL_FIELD_MASK)) {
return false;
}
return true; // start_periodic_measurement();
}
}
return false;
}
int16_t Si7210::read() {
// Read the two regs
int16_t temp = 0;
if (!get_field_strength(&temp)) {
temp = 0;
}
return temp;
}
bool Si7210::write_reg(const uint8_t reg, const uint8_t mask, const uint8_t val) {
uint8_t temp = 0;
if (mask) {
if (!read_reg(reg, &temp)) {
return false;
}
temp &= mask;
}
temp |= val;
return ACCEL_I2C_CLASS::Mem_Write(SI7210_ADDRESS, reg, &temp, 1);
}
bool Si7210::read_reg(const uint8_t reg, uint8_t *val) { return ACCEL_I2C_CLASS::Mem_Read(SI7210_ADDRESS, reg, val, 1); }
bool Si7210::start_periodic_measurement() {
/* Enable periodic wakeup */
if (!write_reg(SI7210_CTRL3, (uint8_t)~SL_TIMEENA_MASK, SL_TIMEENA_MASK)) {
return false;
}
/* Start measurement */
/* Change to ~STOP_MASK with STOP_MASK */
return write_reg(SI7210_POWER_CTRL, MEAS_MASK | USESTORE_MASK, 0);
}
bool Si7210::get_field_strength(int16_t *field) {
*field = 0;
uint8_t val = 0;
ACCEL_I2C_CLASS::wakePart(SI7210_ADDRESS);
if (!write_reg(SI7210_POWER_CTRL, MEAS_MASK | USESTORE_MASK, STOP_MASK)) {
return false;
}
/* Read most-significant byte */
if (!read_reg(SI7210_DSPSIGM, &val)) {
return false;
}
*field = (val & DSP_SIGM_DATA_MASK) << 8;
/* Read least-significant byte of data */
if (!read_reg(SI7210_DSPSIGL, &val)) {
return false;
}
*field += val;
*field -= 16384U;
// field is now a +- measurement
// In units of 0.0125 mT
// Aka 12.5uT
// Clear flags
read_reg(SI7210_CTRL1, &val);
read_reg(SI7210_CTRL2, &val);
// Start next one
/* Use a burst size of 128/4096 samples in FIR and IIR modes */
write_reg(SI7210_CTRL4, 0, DF_BURSTSIZE_128 | DF_BW_4096);
/* Selet field strength measurement */
write_reg(SI7210_DSPSIGSEL, 0, DSP_SIGSEL_FIELD_MASK);
/* Start measurement */
write_reg(SI7210_POWER_CTRL, MEAS_MASK | USESTORE_MASK, ONEBURST_MASK);
return true;
}
bool Si7210::set_high_range() {
// To set the unit into 200mT range, no magnet temperature calibration
// We want to copy OTP 0x27->0x2C into a0->a5
uint8_t base_addr = 0x27; // You can change this to pick the temp calibration
bool worked = true;
uint8_t val = 0;
/* Load A0 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A0, 0, val);
/* Load A1 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr + 1);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A1, 0, val);
/* Load A2 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr + 2);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A2, 0, val);
/* Load A3 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr + 3);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A3, 0, val);
/* Load A4 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr + 4);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A4, 0, val);
/* Load A5 register */
worked &= write_reg(SI7210_OTP_ADDR, 0, base_addr + 5);
worked &= write_reg(SI7210_OTP_CTRL, 0, OTP_READ_EN_MASK);
worked &= read_reg(SI7210_OTP_DATA, &val);
worked &= write_reg(SI7210_A5, 0, val);
return worked;
}
#endif // MAG_SLEEP_SUPPORT