Skip to content

Commit cbaf4ac

Browse files
committed
STM32F411Disco
1 parent 528ff23 commit cbaf4ac

File tree

9 files changed

+58
-93
lines changed

9 files changed

+58
-93
lines changed

src/AudioBoard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AudioBoard {
2323
}
2424

2525
bool begin(){
26+
AD_LOGD("AudioBoard::begin");
2627
bool result = pins.begin() && driver->begin(codec_cfg, pins);
2728
setVolume(DRIVER_DEFAULT_VOLUME);
2829
return result;
@@ -71,6 +72,8 @@ static AudioBoard LyratMini{AudioDriverLyratMini, PinsLyratMini};
7172
static AudioBoard NoBoard{NoDriver, NoPins};
7273
/// @ingroup audio_driver
7374
static AudioBoard GenericWM8960{AudioDriverWM8960, NoPins};
75+
/// @ingroup audio_driver
76+
static AudioBoard GenericCS43l22{AudioDriverCS43l22, NoPins};
7477
#if defined(ARDUINO_GENERIC_F411VETX)
7578
/// @ingroup audio_driver
7679
static AudioBoard STM32F411Disco{AudioDriverCS43l22, PinsSTM32F411Disco};

src/Driver.h

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class CodecConfig : public codec_config_t {
157157
class AudioDriver {
158158
public:
159159
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
160+
AD_LOGD("AudioDriver::begin");
160161
codec_cfg = codecCfg;
161162
p_pins = &pins;
162163
pins.setSPIActiveForSD(codecCfg.sd_active);
@@ -192,7 +193,7 @@ class AudioDriver {
192193
GpioPin pin = pins().getPinID(PinFunction::PA);
193194
if (pin == -1)
194195
return false;
195-
AD_LOGI("setPAPower pin %d -> %d", pin, enable);
196+
//AD_LOGI("setPAPower pin %d -> %d", pin, enable);
196197
digitalWrite(pin, enable ? HIGH : LOW);
197198
return true;
198199
}
@@ -201,6 +202,16 @@ class AudioDriver {
201202
CodecConfig codec_cfg;
202203
DriverPins *p_pins = nullptr;
203204

205+
/// Detemine the TwoWire object from the I2C config or use Wire
206+
TwoWire* getI2C() {
207+
if (p_pins == nullptr) return &Wire;
208+
auto i2c = pins().getI2CPins(PinFunction::CODEC);
209+
if (!i2c) {
210+
return &Wire;
211+
}
212+
return i2c.value().p_wire;
213+
}
214+
204215
virtual bool init(codec_config_t codec_cfg) { return false; }
205216
virtual bool deinit() { return false; }
206217
virtual bool controlState(codec_mode_t mode) { return false; };
@@ -262,12 +273,7 @@ class AudioDriverAC101Class : public AudioDriver {
262273

263274
protected:
264275
bool init(codec_config_t codec_cfg) {
265-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
266-
if (!i2c) {
267-
AD_LOGE("i2c pins not defined");
268-
return false;
269-
}
270-
ac101_set_i2c_handle(i2c.value().p_wire);
276+
ac101_set_i2c_handle(getI2C());
271277
return ac101_init(&codec_cfg) == RESULT_OK;
272278
};
273279
bool deinit() { return ac101_deinit() == RESULT_OK; }
@@ -293,15 +299,17 @@ class AudioDriverCS43l22Class : public AudioDriver {
293299
void setI2CAddress(uint16_t adr) { deviceAddr = adr; }
294300

295301
virtual bool begin(CodecConfig codecCfg, DriverPins &pins) {
302+
AD_LOGD("AudioDriverCS43l22Class::begin");
303+
p_pins = &pins;
296304
codec_cfg = codecCfg;
297305
// manage reset pin -> acive high
298306
setPAPower(true);
299307
delay(10);
300-
p_pins = &pins;
301308
int vol = map(volume, 0, 100, DEFAULT_VOLMIN, DEFAULT_VOLMAX);
302309
uint32_t freq = getFrequency(codec_cfg.i2s.rate);
303310
uint16_t outputDevice = getOutput(codec_cfg.output_device);
304-
return cs43l22_Init(deviceAddr, outputDevice, vol, freq) == 0;
311+
AD_LOGD("cs43l22_Init");
312+
return cs43l22_Init(deviceAddr, outputDevice, vol, freq, getI2C()) == 0;
305313
}
306314

307315
bool setMute(bool mute) {
@@ -382,12 +390,7 @@ class AudioDriverES7210Class : public AudioDriver {
382390
int volume;
383391

384392
bool init(codec_config_t codec_cfg) {
385-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
386-
if (!i2c) {
387-
AD_LOGE("i2c pins not defined");
388-
return false;
389-
}
390-
return es7210_adc_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
393+
return es7210_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
391394
}
392395
bool deinit() { return es7210_adc_deinit() == RESULT_OK; }
393396

@@ -420,12 +423,7 @@ class AudioDriverES7243Class : public AudioDriver {
420423

421424
protected:
422425
bool init(codec_config_t codec_cfg) {
423-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
424-
if (!i2c) {
425-
AD_LOGE("i2c pins not defined");
426-
return false;
427-
}
428-
return es7243_adc_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
426+
return es7243_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
429427
}
430428
bool deinit() { return es7243_adc_deinit() == RESULT_OK; }
431429

@@ -462,12 +460,7 @@ class AudioDriverES7243eClass : public AudioDriver {
462460
int volume = 0;
463461

464462
bool init(codec_config_t codec_cfg) {
465-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
466-
if (!i2c) {
467-
AD_LOGE("i2c pins not defined");
468-
return false;
469-
}
470-
return es7243e_adc_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
463+
return es7243e_adc_init(&codec_cfg, getI2C()) == RESULT_OK;
471464
}
472465
bool deinit() { return es7243e_adc_deinit() == RESULT_OK; }
473466

@@ -501,13 +494,7 @@ class AudioDriverES8156Class : public AudioDriver {
501494

502495
protected:
503496
bool init(codec_config_t codec_cfg) {
504-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
505-
if (!i2c) {
506-
AD_LOGE("i2c pins not defined");
507-
return false;
508-
}
509-
510-
return es8156_codec_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
497+
return es8156_codec_init(&codec_cfg, getI2C()) == RESULT_OK;
511498
}
512499
bool deinit() { return es8156_codec_deinit() == RESULT_OK; }
513500

@@ -538,15 +525,10 @@ class AudioDriverES8311Class : public AudioDriver {
538525

539526
protected:
540527
bool init(codec_config_t codec_cfg) {
541-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
542-
if (!i2c) {
543-
AD_LOGE("i2c pins not defined");
544-
return false;
545-
}
546528
int mclk_src = pins().getPinID(PinFunction::MCLK_SOURCE);
547529
if (mclk_src == -1)
548530
return false;
549-
return es8311_codec_init(&codec_cfg, i2c.value().p_wire, mclk_src) ==
531+
return es8311_codec_init(&codec_cfg, getI2C(), mclk_src) ==
550532
RESULT_OK;
551533
}
552534
bool deinit() { return es8311_codec_deinit() == RESULT_OK; }
@@ -579,13 +561,8 @@ class AudioDriverES8374Class : public AudioDriver {
579561

580562
protected:
581563
bool init(codec_config_t codec_cfg) {
582-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
583-
if (!i2c) {
584-
AD_LOGE("i2c pins not defined");
585-
return false;
586-
}
587564
auto codec_mode = this->codec_cfg.get_mode();
588-
return es8374_codec_init(&codec_cfg, codec_mode, i2c.value().p_wire) ==
565+
return es8374_codec_init(&codec_cfg, codec_mode, getI2C()) ==
589566
RESULT_OK;
590567
}
591568
bool deinit() { return es8374_codec_deinit() == RESULT_OK; }
@@ -631,13 +608,7 @@ class AudioDriverES8388Class : public AudioDriver {
631608

632609
protected:
633610
bool init(codec_config_t codec_cfg) {
634-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
635-
if (!i2c) {
636-
AD_LOGE("i2c pins not defined");
637-
return false;
638-
}
639-
640-
return es8388_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
611+
return es8388_init(&codec_cfg, getI2C()) == RESULT_OK;
641612
}
642613
bool deinit() { return es8388_deinit() == RESULT_OK; }
643614

@@ -669,12 +640,7 @@ class AudioDriverTAS5805MClass : public AudioDriver {
669640

670641
protected:
671642
bool init(codec_config_t codec_cfg) {
672-
auto i2c = pins().getI2CPins(PinFunction::CODEC);
673-
if (!i2c) {
674-
AD_LOGE("i2c pins not defined");
675-
return false;
676-
}
677-
return tas5805m_init(&codec_cfg, i2c.value().p_wire) == RESULT_OK;
643+
return tas5805m_init(&codec_cfg, getI2C()) == RESULT_OK;
678644
}
679645
bool deinit() { return tas5805m_deinit() == RESULT_OK; }
680646
};
@@ -689,13 +655,8 @@ class AudioDriverWM8960Class : public AudioDriver {
689655
bool begin(CodecConfig codecCfg, DriverPins &pins) {
690656
codec_cfg = codecCfg;
691657

692-
auto i2c = pins.getI2CPins(PinFunction::CODEC);
693-
if (!i2c) {
694-
AD_LOGE("i2c pins not defined");
695-
return false;
696-
}
697658
// define wire object
698-
mtb_wm8960_set_wire(i2c.value().p_wire);
659+
mtb_wm8960_set_wire(getI2C());
699660
mtb_wm8960_set_write_retry_count(i2c_retry_count);
700661

701662
// setup wm8960
@@ -881,14 +842,8 @@ class AudioDriverWM8994Class : public AudioDriver {
881842
uint32_t freq = codecCfg.getRateNumeric();
882843
uint16_t outputDevice = getOutput(codec_cfg.output_device);
883844

884-
auto i2c = pins.getI2CPins(PinFunction::CODEC);
885-
if (!i2c) {
886-
AD_LOGE("i2c pins not defined");
887-
return false;
888-
}
889-
890845
return wm8994_Init(deviceAddr, outputDevice, vol, freq,
891-
i2c.value().p_wire) == 0;
846+
getI2C()) == 0;
892847
}
893848

894849
bool setMute(bool mute) {

src/Driver/cs43l22/cs43l22.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
130130
* @param Volume: Initial volume level (from 0 (Mute) to 100 (Max))
131131
* @retval 0 if correct communication, else wrong communication
132132
*/
133-
uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq)
133+
uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq, i2c_bus_handle_t handle)
134134
{
135135
uint32_t counter = 0;
136+
i2c_handle = handle;
136137

137138
/* Initialize the Control interface of the Audio Codec */
138139
AUDIO_IO_Init();
@@ -474,11 +475,6 @@ static uint8_t CODEC_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
474475
return result;
475476
}
476477

477-
478-
// I2C Integration
479-
void cs43l22_set_i2c_handle(i2c_bus_handle_t handle){
480-
i2c_handle = handle;
481-
}
482478
void AUDIO_IO_Init(void) {}
483479
void AUDIO_IO_DeInit(void) {}
484480
void AUDIO_IO_Write(uint8_t addr, uint16_t reg, uint8_t value) {

src/Driver/cs43l22/cs43l22.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,12 @@ extern "C" {
175175
* @{
176176
*/
177177

178-
void cs43l22_set_i2c_handle(i2c_bus_handle_t i2c_handle);
179178

180179
/*------------------------------------------------------------------------------
181180
Audio Codec functions
182181
------------------------------------------------------------------------------*/
183182
/* High Layer codec functions */
184-
uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq);
183+
uint32_t cs43l22_Init(uint16_t DeviceAddr, uint16_t OutputDevice, uint8_t Volume, uint32_t AudioFreq, i2c_bus_handle_t i2c_handle);
185184
void cs43l22_DeInit(void);
186185
uint32_t cs43l22_ReadID(uint16_t DeviceAddr);
187186
uint32_t cs43l22_Play(uint16_t DeviceAddr, uint16_t* pBuffer, uint16_t Size);

src/DriverPins.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct PinsSPI {
9999
operator bool() { return pinsAvailable(); }
100100
bool begin() {
101101
if (set_active) {
102+
AD_LOGD("PinsSPI::begin for %d", function);
102103
// setup chip select
103104
if (cs != -1) {
104105
pinMode(cs, OUTPUT);
@@ -167,6 +168,7 @@ struct PinsI2C {
167168

168169
bool begin() {
169170
if (set_active) {
171+
AD_LOGD("PinsI2C::begin for %d", function);
170172
// if no pins are defined, just call begin
171173
if (!pinsAvailable()) {
172174
AD_LOGI("setting up I2C w/o pins");
@@ -309,6 +311,7 @@ class DriverPins {
309311
}
310312

311313
virtual bool begin() {
314+
AD_LOGD("DriverPins::begin");
312315
// setup spi
313316
bool result = true;
314317
for (auto &tmp : spi) {
@@ -325,6 +328,7 @@ class DriverPins {
325328
for (auto &tmp : pins) {
326329
if (tmp.pin != -1) {
327330
if (!hasConflict(tmp.pin)) {
331+
AD_LOGD("pinMode %d", tmp.pin);
328332
switch (tmp.pin_logic) {
329333
case PinLogic::InputActiveHigh:
330334
pinMode(tmp.pin, INPUT);
@@ -589,7 +593,7 @@ class PinsSTM32F411DiscoClass : public DriverPins {
589593
addPin(PinFunction::LED, PD14, PinLogic::Output, 3); // red
590594
addPin(PinFunction::LED, PD15, PinLogic::Output, 4); // blue
591595
addPin(PinFunction::PA, PD4, PinLogic::Output); // reset pin (active high)
592-
addPin(PinFunction::CODEC_ADC, PC3, PinLogic::Input); // Microphone
596+
//addPin(PinFunction::CODEC_ADC, PC3, PinLogic::Input); // Microphone
593597
}
594598
};
595599

src/Utils/I2C.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ error_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
1111
addr, reglen, datalen, reg[0], data[0]);
1212
TwoWire *p_wire = (TwoWire *)bus;
1313
assert(p_wire!=nullptr);
14-
assert(reglen == 1);
15-
assert(datalen == 1);
14+
//assert(reglen == 1);
15+
//assert(datalen == 1);
1616

1717
int result = RESULT_OK;
1818
p_wire->beginTransmission(addr >> 1);
19-
p_wire->write(reg[0]);
20-
p_wire->write(data[0]);
19+
p_wire->write(reg, reglen);
20+
p_wire->write(data, datalen);
2121
int rc = p_wire->endTransmission(I2C_END);
2222
if (rc != 0) {
2323
AD_LOGE("->p_wire->endTransmission: %d", rc);

src/Utils/Logger.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Utils/Logger.h"
2+
#include "Utils/etc.h"
23
#include <string.h>
34
#include <stdio.h>
45
#include <stdarg.h>
56

6-
7+
// Default log level is warning
78
int LOGLEVEL_AUDIODRIVER = AudioDriverWarning;
89

910
void AD_LOGD(const char* fmr, ...) {
@@ -12,10 +13,10 @@ void AD_LOGD(const char* fmr, ...) {
1213
strcpy(log_buffer, "Debug: ");
1314
va_list arg;
1415
va_start(arg, fmr);
15-
vsprintf(log_buffer+9, fmr, arg);
16+
vsnprintf(log_buffer+9, AD_LOGLENGTH-9, fmr, arg);
1617
va_end(arg);
17-
strcat(log_buffer, "\n");
18-
printf("%s",log_buffer);
18+
// strcat(log_buffer, "\n");
19+
logStr(log_buffer);
1920
}
2021
}
2122

@@ -25,7 +26,7 @@ void AD_LOGI(const char* fmr, ...) {
2526
strcpy(log_buffer, "Info: ");
2627
va_list arg;
2728
va_start(arg, fmr);
28-
vsprintf(log_buffer+9, fmr, arg);
29+
vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg);
2930
va_end(arg);
3031
strcat(log_buffer, "\n");
3132
printf("%s",log_buffer);
@@ -38,7 +39,7 @@ void AD_LOGW(const char* fmr, ...) {
3839
strcpy(log_buffer, "Warning: ");
3940
va_list arg;
4041
va_start(arg, fmr);
41-
vsprintf(log_buffer+9, fmr, arg);
42+
vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg);
4243
va_end(arg);
4344
strcat(log_buffer, "\n");
4445
printf("%s",log_buffer);
@@ -51,7 +52,7 @@ void AD_LOGE(const char* fmr, ...) {
5152
strcpy(log_buffer, "Error: ");
5253
va_list arg;
5354
va_start(arg, fmr);
54-
vsprintf(log_buffer+9, fmr, arg);
55+
vsnprintf(log_buffer+9,AD_LOGLENGTH-9, fmr, arg);
5556
va_end(arg);
5657
strcat(log_buffer, "\n");
5758
printf("%s",log_buffer);

src/Utils/etc.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Arduino.h"
2+
#include "Utils/etc.h"
3+
4+
/// Arcuino c++ println function
5+
void logStr(const char* msg){
6+
Serial.println(msg);
7+
}

0 commit comments

Comments
 (0)