parent
72fe82ebc4
commit
3670859d16
@ -0,0 +1,31 @@ |
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au> |
||||
* |
||||
* SPDX-License-Identifier: GPL-3.0-only |
||||
*/ |
||||
#pragma once |
||||
|
||||
#include <cstdint> |
||||
#include <optional> |
||||
|
||||
namespace drivers { |
||||
namespace wm8523 { |
||||
|
||||
enum class Register : uint8_t { |
||||
kReset = 0, |
||||
kRevision = 1, |
||||
kPsCtrl = 2, |
||||
kAifCtrl1 = 3, |
||||
kAifCtrl2 = 4, |
||||
kDacCtrl = 5, |
||||
kDacGainLeft = 6, |
||||
kDacGainRight = 7, |
||||
kZeroDetect = 8, |
||||
}; |
||||
|
||||
auto ReadRegister(Register reg) -> std::optional<uint16_t>; |
||||
auto WriteRegister(Register reg, uint16_t data) -> bool; |
||||
auto WriteRegister(Register reg, uint8_t msb, uint8_t lsb) -> bool; |
||||
|
||||
} |
||||
} |
@ -0,0 +1,51 @@ |
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au> |
||||
* |
||||
* SPDX-License-Identifier: GPL-3.0-only |
||||
*/ |
||||
#include "wm8523.hpp" |
||||
|
||||
#include <cstdint> |
||||
|
||||
#include "esp_err.h" |
||||
|
||||
#include "hal/i2c_types.h" |
||||
#include "i2c.hpp" |
||||
|
||||
namespace drivers { |
||||
namespace wm8523 { |
||||
|
||||
static const uint8_t kAddress = 0b0011010; |
||||
|
||||
auto ReadRegister(Register reg) -> std::optional<uint16_t> { |
||||
uint8_t msb, lsb; |
||||
I2CTransaction transaction; |
||||
transaction.start() |
||||
.write_addr(kAddress, I2C_MASTER_WRITE) |
||||
.write_ack(static_cast<uint8_t>(reg)) |
||||
.start() |
||||
.write_addr(kAddress, I2C_MASTER_READ) |
||||
.read(&msb, I2C_MASTER_ACK) |
||||
.read(&lsb, I2C_MASTER_LAST_NACK) |
||||
.stop(); |
||||
if (transaction.Execute() != ESP_OK) { |
||||
return {}; |
||||
} |
||||
return (msb << 8) & lsb; |
||||
} |
||||
|
||||
auto WriteRegister(Register reg, uint16_t data) -> bool { |
||||
return WriteRegister(reg, (data >> 8) & 0xFF, data & 0xFF); |
||||
} |
||||
|
||||
auto WriteRegister(Register reg, uint8_t msb, uint8_t lsb) -> bool { |
||||
I2CTransaction transaction; |
||||
transaction.start() |
||||
.write_addr(kAddress, I2C_MASTER_WRITE) |
||||
.write_ack(static_cast<uint8_t>(reg), msb, lsb) |
||||
.stop(); |
||||
return transaction.Execute() == ESP_OK; |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue