2019-05-02 16:30:13 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-02 16:30:13 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 08:10:21 +01:00
|
|
|
#include "boot_i2c_utils.hpp"
|
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::boot {
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
constexpr Result RetryUntilSuccess(F f) {
|
2020-11-01 05:52:43 +00:00
|
|
|
constexpr auto Timeout = TimeSpan::FromSeconds(10);
|
|
|
|
constexpr auto RetryInterval = TimeSpan::FromMilliSeconds(20);
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
TimeSpan cur_time = TimeSpan(0);
|
2019-06-22 08:10:21 +01:00
|
|
|
while (true) {
|
2019-10-24 09:40:44 +01:00
|
|
|
const auto retry_result = f();
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(R_SUCCEEDED(retry_result));
|
2019-10-24 09:40:44 +01:00
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
cur_time += RetryInterval;
|
|
|
|
R_UNLESS(cur_time < Timeout, retry_result);
|
2019-10-24 09:40:44 +01:00
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
os::SleepThread(RetryInterval);
|
2019-05-02 16:30:13 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-02 16:30:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
Result ReadI2cRegister(i2c::driver::I2cSession &session, u8 *dst, size_t dst_size, const u8 *cmd, size_t cmd_size) {
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(dst != nullptr && dst_size > 0);
|
|
|
|
AMS_ABORT_UNLESS(cmd != nullptr && cmd_size > 0);
|
2019-05-02 16:30:13 +01:00
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
u8 cmd_list[i2c::CommandListLengthMax];
|
2019-06-22 08:10:21 +01:00
|
|
|
i2c::CommandListFormatter formatter(cmd_list, sizeof(cmd_list));
|
2019-05-02 16:30:13 +01:00
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
R_ABORT_UNLESS(formatter.EnqueueSendCommand(i2c::TransactionOption_StartCondition, cmd, cmd_size));
|
|
|
|
R_ABORT_UNLESS(formatter.EnqueueReceiveCommand(static_cast<i2c::TransactionOption>(i2c::TransactionOption_StartCondition | i2c::TransactionOption_StopCondition), dst_size));
|
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(RetryUntilSuccess([&]() { R_RETURN(i2c::driver::ExecuteCommandList(dst, dst_size, session, cmd_list, formatter.GetCurrentLength())); }));
|
2019-05-02 16:30:13 +01:00
|
|
|
}
|
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
Result WriteI2cRegister(i2c::driver::I2cSession &session, const u8 *src, size_t src_size, const u8 *cmd, size_t cmd_size) {
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(src != nullptr && src_size > 0);
|
|
|
|
AMS_ABORT_UNLESS(cmd != nullptr && cmd_size > 0);
|
2019-05-02 16:30:13 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
u8 cmd_list[0x20];
|
2019-05-02 16:30:13 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* N doesn't use a CommandListFormatter here... */
|
2020-11-01 05:52:43 +00:00
|
|
|
std::memcpy(cmd_list + 0, cmd, cmd_size);
|
|
|
|
std::memcpy(cmd_list + cmd_size, src, src_size);
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(RetryUntilSuccess([&]() { R_RETURN(i2c::driver::Send(session, cmd_list, src_size + cmd_size, static_cast<i2c::TransactionOption>(i2c::TransactionOption_StartCondition | i2c::TransactionOption_StopCondition))); }));
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-01 05:52:43 +00:00
|
|
|
Result WriteI2cRegister(i2c::driver::I2cSession &session, const u8 address, const u8 value) {
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(WriteI2cRegister(session, std::addressof(value), sizeof(value), &address, sizeof(address)));
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-03 13:00:03 +01:00
|
|
|
|
|
|
|
}
|