2019-06-22 03:25:27 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-06-22 03:25:27 +00: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 22:02:10 +00:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 03:25:27 +00:00
|
|
|
#include "i2c_types.hpp"
|
|
|
|
#include "i2c_command_list.hpp"
|
|
|
|
|
2019-10-24 09:30:10 +00:00
|
|
|
namespace ams::i2c {
|
2019-06-22 03:25:27 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Useful definitions. */
|
|
|
|
constexpr size_t SendCommandSize = 2;
|
|
|
|
constexpr size_t ReceiveCommandSize = 2;
|
|
|
|
constexpr size_t SleepCommandSize = 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Result CommandListFormatter::CanEnqueue(size_t size) const {
|
2019-10-24 08:40:44 +00:00
|
|
|
R_UNLESS(this->cmd_list_size - this->cur_index >= size, ResultFullCommandList());
|
|
|
|
return ResultSuccess();
|
2019-06-22 03:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, const void *src, size_t size) {
|
|
|
|
R_TRY(this->CanEnqueue(SendCommandSize + size));
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index] = static_cast<u8>(Command::Send);
|
|
|
|
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
|
|
|
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Stop) != 0) << 7;
|
|
|
|
this->cur_index++;
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index++] = size;
|
|
|
|
|
|
|
|
const u8 *src_u8 = reinterpret_cast<const u8 *>(src);
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
this->cmd_list[this->cur_index++] = src_u8[i];
|
|
|
|
}
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-06-22 03:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption option, size_t size) {
|
|
|
|
R_TRY(this->CanEnqueue(ReceiveCommandSize));
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index] = static_cast<u8>(Command::Receive);
|
|
|
|
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
|
|
|
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Stop) != 0) << 7;
|
|
|
|
this->cur_index++;
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index++] = size;
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-06-22 03:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CommandListFormatter::EnqueueSleepCommand(size_t us) {
|
|
|
|
R_TRY(this->CanEnqueue(SleepCommandSize));
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index] = static_cast<u8>(Command::SubCommand);
|
|
|
|
this->cmd_list[this->cur_index] |= static_cast<u8>(SubCommand::Sleep) << 2;
|
|
|
|
this->cur_index++;
|
|
|
|
|
|
|
|
this->cmd_list[this->cur_index++] = us;
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-06-22 03:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|