~aleteoryx/muditaos

ref: 6888e44c329cee239730f0caa00ec907e3e22053 muditaos/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.cpp -rw-r--r-- 5.6 KiB
6888e44c — Tigran Soghbatyan [BH-1025] Fix timer callbacks in switches 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CodecAW8898.hpp"
#include "AW8898regs.hpp"
#include "AW8898driver.hpp"
#include "board/BoardDefinitions.hpp"

#include <log/log.hpp>

extern "C"
{
#include "FreeRTOS.h"
#include "task.h"
}

#include <cstdint>

using namespace drivers;
using namespace bsp::audio;

namespace
{
    constexpr auto ReadStatusRetries  = 5;
    constexpr auto OneByteAddressing  = 1;
    constexpr auto PositiveLogic      = 0;
    constexpr auto defaultVolumeLevel = 3.0f;
} // namespace

CodecAW8898::CodecAW8898()
{
    LOG_INFO("Initializing AW8898 audio codec");
    AW8898::i2cAddr.deviceAddress  = AW8898_I2C_ADDR;
    AW8898::i2cAddr.subAddressSize = OneByteAddressing; // AW8898 uses 1byte addressing
    AW8898::i2c                    = DriverI2C::Create(
        static_cast<I2CInstances>(BoardDefinitions::AUDIOCODEC_I2C),
        DriverI2CParams{.baudrate = static_cast<uint32_t>(BoardDefinitions::AUDIOCODEC_I2C_BAUDRATE)});

    AW8898::gpio =
        DriverGPIO::Create(static_cast<GPIOInstances>(BoardDefinitions::BELL_AUDIOCODEC_GPIO), DriverGPIOParams{});

    AW8898::gpio->ConfPin(
        DriverGPIOPinParams{.dir      = DriverGPIOPinParams::Direction::Output,
                            .irqMode  = DriverGPIOPinParams::InterruptMode::NoIntmode,
                            .defLogic = PositiveLogic,
                            .pin      = static_cast<uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_RSTN_PA_PIN)});

    AW8898::gpio->ConfPin(
        DriverGPIOPinParams{.dir      = DriverGPIOPinParams::Direction::Input,
                            .irqMode  = DriverGPIOPinParams::InterruptMode::IntFallingEdge,
                            .defLogic = PositiveLogic,
                            .pin      = static_cast<uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_INTN_PA_PIN)});

    AW8898::gpio->ClearPortInterrupts(1 << static_cast<uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_INTN_PA_PIN));
    AW8898::gpio->DisableInterrupt(1 << static_cast<uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_INTN_PA_PIN));

    AW8898::gpio->WritePin(static_cast<std::uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_RSTN_PA_PIN),
                           AW8898::LogicLow); // reset chip
    vTaskDelay(pdMS_TO_TICKS(AW8898::OperationWaitTimeMS));
    AW8898::gpio->WritePin(static_cast<std::uint32_t>(BoardDefinitions::BELL_AUDIOCODEC_RSTN_PA_PIN),
                           AW8898::LogicHigh); // clear reset
    vTaskDelay(pdMS_TO_TICKS(AW8898::OperationWaitTimeMS));

    LOG_DEBUG("Probing AW8898 ...");
    auto ret = Probe();
    LOG_DEBUG("AW8898 Probe: 0x%04lX", ret.value());
}

CodecAW8898::~CodecAW8898()
{
    Reset();
}

CodecRetCode CodecAW8898::Start(const CodecParams &param)
{
    CodecParamsAW8898 params;
    params.opCmd      = static_cast<CodecParamsAW8898::Cmd>(param.opCmd);
    params.outVolume  = param.outVolume;
    params.sampleRate = static_cast<CodecParamsAW8898::SampleRate>(param.sampleRate);

    AW8898::Init(params.MonoStereoToCodecChsel(), params.SampleRateToCodecFreq());

    // Store param configuration
    currentParams = params;

    auto currVol = currentParams.outVolume;
    // remove this once volume control in application is working
    currVol = defaultVolumeLevel;
    SetOutputVolume(currVol);

    AW8898::ReadAllReg();

    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::Pause()
{
    AW8898::Stop();

    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::Resume()
{
    AW8898::Start();

    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::Stop()
{
    AW8898::Stop();
    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::Ioctrl(const CodecParams &param)
{

    const CodecParamsAW8898 &params = static_cast<const CodecParamsAW8898 &>(param);

    CodecRetCode ret = CodecRetCode::Success;

    switch (params.opCmd) {
    case CodecParamsAW8898::Cmd::SetOutVolume:
        ret = SetOutputVolume(params.outVolume);
        break;

    case CodecParamsAW8898::Cmd::SetInGain:
        ret = CodecRetCode::Success;
        break;
    case CodecParamsAW8898::Cmd::SetInput:
        ret = CodecRetCode::Success;
        break;
    case CodecParamsAW8898::Cmd::SetOutput:
        ret = CodecRetCode::Success;
        break;
    case CodecParamsAW8898::Cmd::MicBiasCtrl:
        ret = CodecRetCode::Success;
        break;
    case CodecParamsAW8898::Cmd::Reset:
        ret = Reset();
        break;
    case CodecParamsAW8898::Cmd::SetMute:
        ret = SetMute(true);
        break;
    default:
        break;
    }

    return ret;
}

std::uint8_t CodecAW8898::VolumeTo8Bit(const float vol)
{
    static constexpr auto maxVolumeLevel  = 10.0f;
    static constexpr auto conversionRatio = static_cast<float>(UINT8_MAX) / maxVolumeLevel;

    if (vol < 0) {
        return 0;
    }

    if (vol > maxVolumeLevel) {
        return UINT8_MAX;
    }

    return static_cast<uint8_t>(conversionRatio * vol);
}

CodecRetCode CodecAW8898::SetOutputVolume(const float vol)
{
    uint8_t mute = 0;

    // If volume set to 0 then mute output
    AW8898::RunMute(vol == 0); //(PWMCTRL.HMUTE=0) - disable mute

    AW8898::SetVolume(VolumeTo8Bit(vol));
    currentParams.outVolume = vol;
    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::Reset()
{
    AW8898::HwReset();
    return CodecRetCode::Success;
}

CodecRetCode CodecAW8898::SetMute(const bool enable)
{
    AW8898::RunMute(enable);
    return CodecRetCode::Success;
}

std::optional<uint32_t> CodecAW8898::Probe()
{
    if (AW8898::ReadChipid() == kStatus_Success) {
        return AW8898::AW8898_CHIP_ID;
    }
    else {
        return std::nullopt;
    }
}