i2c: add register acccessor helpers

This commit is contained in:
Michael Scire 2020-11-09 17:34:47 -08:00 committed by SciresM
parent 5bd02f128d
commit 7c58a21c4c
6 changed files with 250 additions and 0 deletions

View file

@ -28,3 +28,4 @@
#include <stratosphere/i2c/driver/impl/i2c_i2c_session_impl.hpp>
#include <stratosphere/i2c/i2c_api.hpp>
#include <stratosphere/i2c/i2c_bus_api.hpp>
#include <stratosphere/i2c/i2c_register_accessor.hpp>

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/i2c/i2c_types.hpp>
#include <stratosphere/i2c/i2c_command_list_formatter.hpp>
#include <stratosphere/i2c/i2c_bus_api.hpp>
namespace ams::i2c {
template<typename RegType> requires std::unsigned_integral<RegType>
Result ReadSingleRegister(const I2cSession &session, u8 address, RegType *out) {
/* Check pre-conditions. */
AMS_ABORT_UNLESS(out != nullptr);
constexpr i2c::TransactionOption StartOption = i2c::TransactionOption_StartCondition;
constexpr i2c::TransactionOption StopOption = static_cast<i2c::TransactionOption>(i2c::TransactionOption_StartCondition | i2c::TransactionOption_StopCondition);
u8 cmd_list[CommandListLengthMax];
i2c::CommandListFormatter formatter(cmd_list, sizeof(cmd_list));
R_TRY(formatter.EnqueueSendCommand(StartOption, std::addressof(address), sizeof(address)));
R_TRY(formatter.EnqueueReceiveCommand(StopOption, sizeof(*out)));
R_TRY(i2c::ExecuteCommandList(out, sizeof(*out), session, cmd_list, formatter.GetCurrentLength()));
return ResultSuccess();
}
template<typename RegType> requires std::unsigned_integral<RegType>
Result WriteSingleRegister(const I2cSession &session, u8 address, RegType value) {
/* Prepare buffer. */
u8 buf[sizeof(address) + sizeof(value)];
std::memcpy(buf + 0, std::addressof(address), sizeof(address));
std::memcpy(buf + sizeof(address), std::addressof(value), sizeof(value));
constexpr i2c::TransactionOption StopOption = static_cast<i2c::TransactionOption>(i2c::TransactionOption_StartCondition | i2c::TransactionOption_StopCondition);
R_TRY(i2c::Send(session, buf, sizeof(buf), StopOption));
return ResultSuccess();
}
}

View file

@ -0,0 +1,169 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "powctl_bq24193_driver.hpp"
namespace ams::powctl::impl::board::nintendo_nx {
Result Bq24193Driver::InitializeSession() {
/* Set fast charge current limit. */
R_TRY(this->SetFastChargeCurrentLimit(512));
/* Disable force 20 percent charge. */
R_TRY(this->SetForce20PercentChargeCurrent(false));
/* Set pre-charge current limit. */
R_TRY(this->SetPreChargeCurrentLimit(128));
/* Set termination current limit. */
R_TRY(this->SetTerminationCurrentLimit(128));
/* Set minimum system voltage limit. */
R_TRY(this->SetMinimumSystemVoltageLimit(3000));
/* Set watchdog timer setting. */
R_TRY(this->SetWatchdogTimerSetting(0));
/* Disable charging safety timer. */
R_TRY(this->SetChargingSafetyTimerEnabled(false));
/* Reset the watchdog timer. */
R_TRY(this->ResetWatchdogTimer());
return ResultSuccess();
}
Result Bq24193Driver::SetPreChargeCurrentLimit(int ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetTerminationCurrentLimit(int ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetMinimumSystemVoltageLimit(int mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetChargingSafetyTimerEnabled(bool en) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetForce20PercentChargeCurrent(bool *out) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetForce20PercentChargeCurrent(bool en) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetFastChargeCurrentLimit(int *out_ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetFastChargeCurrentLimit(int ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetChargeVoltageLimit(int *out_mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetChargeVoltageLimit(int mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetChargerConfiguration(bq24193::ChargerConfiguration cfg) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::IsHiZEnabled(bool *out) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetHiZEnabled(bool en) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetInputCurrentLimit(int *out_ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetInputCurrentLimit(int ma) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetInputVoltageLimit(int *out_mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetInputVoltageLimit(int mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetChargerStatus(bq24193::ChargerStatus *out) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::ResetWatchdogTimer() {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetWatchdogTimerSetting(int seconds) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetBatteryCompensation(int *out_mo) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetBatteryCompensation(int mo) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::GetVoltageClamp(int *out_mv) {
/* TODO */
AMS_ABORT();
}
Result Bq24193Driver::SetVoltageClamp(int mv) {
/* TODO */
AMS_ABORT();
}
}

View file

@ -52,6 +52,13 @@ namespace ams::powctl::impl::board::nintendo_nx {
R_ABORT_UNLESS(this->InitializeSession());
}
Result SetPreChargeCurrentLimit(int ma);
Result SetTerminationCurrentLimit(int ma);
Result SetMinimumSystemVoltageLimit(int mv);
Result SetChargingSafetyTimerEnabled(bool en);
Result GetForce20PercentChargeCurrent(bool *out);
Result SetForce20PercentChargeCurrent(bool en);

View file

@ -32,6 +32,19 @@ namespace ams::powctl::impl::board::nintendo_nx {
}
ChargerDevice::ChargerDevice(bool ev) : gpio_pad_session(), watchdog_timer_enabled(false), watchdog_timer_timeout(0), use_event_handler(ev), event_handler() {
if (this->use_event_handler) {
/* Create the system event. */
os::CreateSystemEvent(std::addressof(this->system_event), os::EventClearMode_ManualClear, true);
/* Create the handler. */
this->event_handler.emplace(this);
/* Register the event handler. */
powctl::impl::RegisterInterruptHandler(std::addressof(*this->event_handler));
}
}
/* Generic API. */
void ChargerDriver::InitializeDriver() {
/* Initialize Bq24193Driver */

View file

@ -77,6 +77,8 @@ namespace ams::powctl::impl::board::nintendo_nx {
}
void SignalEvent(IDevice *device);
public:
ChargerInterruptEventHandler(IDevice *dv) : InterruptEventHandler<ChargerInterruptEventHandler>(dv) { /* ... */ }
};
class BatteryInterruptEventHandler : public InterruptEventHandler<BatteryInterruptEventHandler> {
@ -95,6 +97,8 @@ namespace ams::powctl::impl::board::nintendo_nx {
}
void SignalEvent(IDevice *device);
public:
BatteryInterruptEventHandler(IDevice *dv) : InterruptEventHandler<BatteryInterruptEventHandler>(dv) { /* ... */ }
};
}