boot: refactor for R_TRY
This commit is contained in:
@@ -31,10 +31,7 @@ static Result I2cSendHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl& s
|
||||
size_t num_bytes = (**cur_cmd);
|
||||
(*cur_cmd)++;
|
||||
|
||||
Result rc = I2cDriver::Send(session, *cur_cmd, num_bytes, option);
|
||||
if (R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(I2cDriver::Send(session, *cur_cmd, num_bytes, option));
|
||||
(*cur_cmd) += num_bytes;
|
||||
|
||||
return ResultSuccess;
|
||||
@@ -49,10 +46,7 @@ static Result I2cReceiveHandler(const u8 **cur_cmd, u8 **cur_dst, I2cSessionImpl
|
||||
size_t num_bytes = (**cur_cmd);
|
||||
(*cur_cmd)++;
|
||||
|
||||
Result rc = I2cDriver::Receive(session, *cur_dst, num_bytes, option);
|
||||
if (R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(I2cDriver::Receive(session, *cur_dst, num_bytes, option));
|
||||
(*cur_dst) += num_bytes;
|
||||
|
||||
return ResultSuccess;
|
||||
@@ -156,10 +150,7 @@ Result I2cDriver::ExecuteCommandList(I2cSessionImpl &session, void *dst, size_t
|
||||
std::abort();
|
||||
}
|
||||
|
||||
Result rc = g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session);
|
||||
if (R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(g_cmd_handlers[cmd](&cur_cmd, &cur_dst, session));
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
|
||||
@@ -146,7 +146,6 @@ Result I2cBusAccessor::Send(const u8 *data, size_t num_bytes, I2cTransactionOpti
|
||||
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
||||
const u8 *cur_src = data;
|
||||
size_t remaining = num_bytes;
|
||||
Result rc;
|
||||
|
||||
/* Set interrupt enable, clear interrupt status. */
|
||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8E);
|
||||
@@ -185,25 +184,19 @@ Result I2cBusAccessor::Send(const u8 *data, size_t num_bytes, I2cTransactionOpti
|
||||
return ResultI2cTimedOut;
|
||||
}
|
||||
|
||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(this->GetAndHandleTransactionResult());
|
||||
}
|
||||
|
||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8C);
|
||||
|
||||
/* Wait for successful completion. */
|
||||
while (true) {
|
||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(this->GetAndHandleTransactionResult());
|
||||
|
||||
/* Check PACKET_XFER_COMPLETE */
|
||||
const u32 interrupt_status = ReadRegister(&this->i2c_registers->I2C_INTERRUPT_STATUS_REGISTER_0);
|
||||
if (interrupt_status & 0x80) {
|
||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(this->GetAndHandleTransactionResult());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -222,7 +215,6 @@ Result I2cBusAccessor::Receive(u8 *out_data, size_t num_bytes, I2cTransactionOpt
|
||||
std::scoped_lock<HosMutex> lk(this->register_mutex);
|
||||
u8 *cur_dst = out_data;
|
||||
size_t remaining = num_bytes;
|
||||
Result rc;
|
||||
|
||||
/* Set interrupt enable, clear interrupt status. */
|
||||
WriteRegister(&this->i2c_registers->I2C_INTERRUPT_MASK_REGISTER_0, 0x8D);
|
||||
@@ -241,9 +233,7 @@ Result I2cBusAccessor::Receive(u8 *out_data, size_t num_bytes, I2cTransactionOpt
|
||||
return ResultI2cTimedOut;
|
||||
}
|
||||
|
||||
if (R_FAILED((rc = this->GetAndHandleTransactionResult()))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(this->GetAndHandleTransactionResult());
|
||||
|
||||
const u32 fifo_status = ReadRegister(&this->i2c_registers->I2C_FIFO_STATUS_0);
|
||||
const size_t fifo_cnt = std::min((remaining + 3) >> 2, static_cast<size_t>(fifo_status & 0xF));
|
||||
@@ -477,14 +467,12 @@ void I2cBusAccessor::HandleTransactionResult(Result result) {
|
||||
}
|
||||
|
||||
Result I2cBusAccessor::GetAndHandleTransactionResult() {
|
||||
Result rc = this->GetTransactionResult();
|
||||
this->HandleTransactionResult(rc);
|
||||
|
||||
if (R_FAILED(rc)) {
|
||||
const Result transaction_res = this->GetTransactionResult();
|
||||
R_TRY_CLEANUP(transaction_res, {
|
||||
this->HandleTransactionResult(transaction_res);
|
||||
this->ClearInterruptMask();
|
||||
eventClear(&this->interrupt_event);
|
||||
return rc;
|
||||
}
|
||||
});
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,7 @@ Result I2cCommandListFormatter::CanEnqueue(size_t size) const {
|
||||
}
|
||||
|
||||
Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option, const void *src, size_t size) {
|
||||
Result rc = this->CanEnqueue(SendCommandSize + size);
|
||||
if (R_FAILED(rc)) { return rc; }
|
||||
R_TRY(this->CanEnqueue(SendCommandSize + size));
|
||||
|
||||
this->cmd_list[this->cur_index] = I2cCommand_Send;
|
||||
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
||||
@@ -45,8 +44,7 @@ Result I2cCommandListFormatter::EnqueueSendCommand(I2cTransactionOption option,
|
||||
}
|
||||
|
||||
Result I2cCommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption option, size_t size) {
|
||||
Result rc = this->CanEnqueue(ReceiveCommandSize);
|
||||
if (R_FAILED(rc)) { return rc; }
|
||||
R_TRY(this->CanEnqueue(ReceiveCommandSize));
|
||||
|
||||
this->cmd_list[this->cur_index] = I2cCommand_Receive;
|
||||
this->cmd_list[this->cur_index] |= ((option & I2cTransactionOption_Start) != 0) << 6;
|
||||
@@ -58,8 +56,7 @@ Result I2cCommandListFormatter::EnqueueReceiveCommand(I2cTransactionOption optio
|
||||
}
|
||||
|
||||
Result I2cCommandListFormatter::EnqueueSleepCommand(size_t us) {
|
||||
Result rc = this->CanEnqueue(SleepCommandSize);
|
||||
if (R_FAILED(rc)) { return rc; }
|
||||
R_TRY(this->CanEnqueue(SleepCommandSize));
|
||||
|
||||
this->cmd_list[this->cur_index] = I2cCommand_SubCommand;
|
||||
this->cmd_list[this->cur_index] |= I2cSubCommand_Sleep << 2;
|
||||
|
||||
@@ -57,49 +57,43 @@ bool I2cDriverSession::IsOpen() const{
|
||||
|
||||
Result I2cDriverSession::DoTransaction(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
||||
std::scoped_lock<HosMutex> lk(this->bus_accessor_mutex);
|
||||
Result rc;
|
||||
|
||||
if (this->bus_accessor->GetBusy()) {
|
||||
return ResultI2cBusBusy;
|
||||
}
|
||||
|
||||
this->bus_accessor->OnStartTransaction();
|
||||
ON_SCOPE_EXIT { this->bus_accessor->OnStopTransaction(); };
|
||||
|
||||
if (R_SUCCEEDED((rc = this->bus_accessor->StartTransaction(command, this->addressing_mode, this->slave_address)))) {
|
||||
switch (command) {
|
||||
case DriverCommand_Send:
|
||||
rc = this->bus_accessor->Send(reinterpret_cast<const u8 *>(src), num_bytes, option, this->addressing_mode, this->slave_address);
|
||||
break;
|
||||
case DriverCommand_Receive:
|
||||
rc = this->bus_accessor->Receive(reinterpret_cast<u8 *>(dst), num_bytes, option, this->addressing_mode, this->slave_address);
|
||||
break;
|
||||
default:
|
||||
std::abort();
|
||||
}
|
||||
R_TRY(this->bus_accessor->StartTransaction(command, this->addressing_mode, this->slave_address));
|
||||
|
||||
switch (command) {
|
||||
case DriverCommand_Send:
|
||||
R_TRY(this->bus_accessor->Send(reinterpret_cast<const u8 *>(src), num_bytes, option, this->addressing_mode, this->slave_address));
|
||||
break;
|
||||
case DriverCommand_Receive:
|
||||
R_TRY(this->bus_accessor->Receive(reinterpret_cast<u8 *>(dst), num_bytes, option, this->addressing_mode, this->slave_address));
|
||||
break;
|
||||
default:
|
||||
std::abort();
|
||||
}
|
||||
|
||||
this->bus_accessor->OnStopTransaction();
|
||||
|
||||
return rc;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result I2cDriverSession::DoTransactionWithRetry(void *dst, const void *src, size_t num_bytes, I2cTransactionOption option, DriverCommand command){
|
||||
Result rc;
|
||||
|
||||
size_t i = 0;
|
||||
while (true) {
|
||||
rc = this->DoTransaction(dst, src, num_bytes, option, command);
|
||||
if (rc == ResultI2cTimedOut) {
|
||||
i++;
|
||||
if (i <= this->max_retries) {
|
||||
svcSleepThread(this->retry_wait_time);
|
||||
continue;
|
||||
R_TRY_CATCH(this->DoTransaction(dst, src, num_bytes, option, command)) {
|
||||
R_CATCH(ResultI2cTimedOut) {
|
||||
i++;
|
||||
if (i <= this->max_retries) {
|
||||
svcSleepThread(this->retry_wait_time);
|
||||
continue;
|
||||
}
|
||||
return ResultI2cBusBusy;
|
||||
}
|
||||
return ResultI2cBusBusy;
|
||||
} else if (R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
} R_END_TRY_CATCH;
|
||||
return ResultSuccess;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user