Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit 05e29ac

Browse files
committed
Wire support
1 parent a6fc21d commit 05e29ac

File tree

8 files changed

+109
-107
lines changed

8 files changed

+109
-107
lines changed

src/AudioKit.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#define I2S_COMM_FORMAT_STAND_I2S \
3636
(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
3737
#define I2S_COMM_FORMAT_STAND_MSB \
38-
(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
39-
#define I2S_COMM_FORMAT_STAND_LSB \
4038
(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
4139
#define I2S_COMM_FORMAT_STAND_PCM_LONG \
4240
(I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
@@ -88,7 +86,7 @@ struct AudioKitConfig {
8886
}
8987

9088
/// Provides the sample rate in samples per second
91-
int sampleRate() {
89+
uint32_t sampleRate() {
9290
switch (sample_rate) {
9391
case AUDIO_HAL_08K_SAMPLES: /*!< set to 8k samples per second */
9492
return 8000;
@@ -144,7 +142,7 @@ struct AudioKitConfig {
144142
if (fmt==AUDIO_HAL_I2S_LEFT){
145143
its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
146144
} else if(fmt==AUDIO_HAL_I2S_RIGHT){
147-
its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_LSB;
145+
its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
148146
} else if(fmt==AUDIO_HAL_I2S_DSP){
149147
its_com_fmt = (i2s_comm_format_t )I2S_COMM_FORMAT_STAND_PCM_SHORT;
150148
}

src/AudioKitSettings.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@
2929
#define AUDIOKIT_DEFAULT_I2S_FMT AUDIO_HAL_I2S_NORMAL
3030

3131
// Activate or deactivate the following
32-
// #define AUDIOKIT_USE_WIRE
32+
#define AUDIOKIT_USE_WIRE
3333

34-
#ifdef ESP32
3534

35+
#ifdef ESP32
3636
#define AUDIOKIT_FREE_RTOS
3737
#define AUDIOKIT_MUTEX_SUPPORT
3838
#define AUDIOKIT_SETUP_SD
39-
4039
#define PIN_AUDIO_KIT_SD_CARD_CS 13
4140
#define PIN_AUDIO_KIT_SD_CARD_MISO 2
4241
#define PIN_AUDIO_KIT_SD_CARD_MOSI 15
4342
#define PIN_AUDIO_KIT_SD_CARD_CLK 14
44-
4543
#else
4644

4745
#endif

src/audio_board/ai_thinker_v2_2/board_def.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@
3030
#define SDCARD_OPEN_FILE_NUM_MAX 5
3131
#define SDCARD_INTR_GPIO GPIO_NUM_34
3232

33-
#define BUTTON_REC_ID GPIO_NUM_36
34-
#define BUTTON_MODE_ID GPIO_NUM_39
35-
#define BUTTON_SET_ID TOUCH_PAD_NUM9
36-
#define BUTTON_PLAY_ID TOUCH_PAD_NUM8
37-
#define BUTTON_VOLUP_ID TOUCH_PAD_NUM7
38-
#define BUTTON_VOLDOWN_ID TOUCH_PAD_NUM4
33+
#define PIN_KEY1 36
34+
#define PIN_KEY2 13
35+
#define PIN_KEY3 19
36+
#define PIN_KEY4 23
37+
#define PIN_KEY5 18
38+
#define PIN_KEY6 5
39+
40+
#define BUTTON_REC_ID PIN_KEY1
41+
#define BUTTON_MODE_ID PIN_KEY2
42+
#define BUTTON_SET_ID PIN_KEY3
43+
#define BUTTON_PLAY_ID PIN_KEY4
44+
#define BUTTON_VOLUP_ID PIN_KEY5
45+
#define BUTTON_VOLDOWN_ID PIN_KEY6
3946

4047
#define AUXIN_DETECT_GPIO GPIO_NUM_12
4148
#define HEADPHONE_DETECT GPIO_NUM_39

src/audio_board/generic_es8388/board_def.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@
5050
#define SDCARD_OPEN_FILE_NUM_MAX 5
5151
#define SDCARD_INTR_GPIO GPIO_NUM_34
5252

53-
#define BUTTON_REC_ID GPIO_NUM_36
54-
#define BUTTON_MODE_ID GPIO_NUM_39
55-
#define BUTTON_SET_ID TOUCH_PAD_NUM9
56-
#define BUTTON_PLAY_ID TOUCH_PAD_NUM8
57-
#define BUTTON_VOLUP_ID TOUCH_PAD_NUM7
58-
#define BUTTON_VOLDOWN_ID TOUCH_PAD_NUM4
53+
#define PIN_KEY1 36
54+
#define PIN_KEY2 13
55+
#define PIN_KEY3 19
56+
#define PIN_KEY4 23
57+
#define PIN_KEY5 18
58+
#define PIN_KEY6 5
59+
60+
#define BUTTON_REC_ID PIN_KEY1
61+
#define BUTTON_MODE_ID PIN_KEY2
62+
#define BUTTON_SET_ID PIN_KEY3
63+
#define BUTTON_PLAY_ID PIN_KEY4
64+
#define BUTTON_VOLUP_ID PIN_KEY5
65+
#define BUTTON_VOLDOWN_ID PIN_KEY6
66+
5967

6068
#define AUXIN_DETECT_GPIO GPIO_NUM_12
6169
#define HEADPHONE_DETECT GPIO_NUM_39

src/audio_hal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern "C" {
5555

5656

5757
#define AUDIO_HAL_VOL_DEFAULT 20
58-
#define ets_printf KIT_LOGD
58+
#define ets_printf KIT_LOGI
5959

6060
typedef struct audio_hal *audio_hal_handle_t;
6161

src/i2c_bus.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@ esp_err_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg, int r
110110
*/
111111
esp_err_t i2c_bus_delete(i2c_bus_handle_t bus);
112112

113-
// /**
114-
// * @brief I2C start sending buffered commands
115-
// *
116-
// * @param bus I2C bus handle
117-
// * @param cmd I2C cmd handle
118-
// * @param ticks_to_wait Maximum blocking time
119-
// *
120-
// * @return
121-
// * - ESP_OK Success
122-
// * - ESP_FAIL Fail
123-
// */
124-
// esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait);
125113

126114
#ifdef __cplusplus
127115
}

src/peripherals/i2c_bus.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t *conf)
6565
KIT_LOGI("scl: %d",conf->scl_io_num);
6666
KIT_LOGI("port: %d",port);
6767

68-
69-
7068
i2c_bus[port] = (i2c_bus_t *) audio_calloc(1, sizeof(i2c_bus_t));
7169
i2c_bus[port]->i2c_conf = *conf;
7270
i2c_bus[port]->i2c_port = port;
@@ -184,15 +182,4 @@ esp_err_t i2c_bus_delete(i2c_bus_handle_t bus)
184182
return ESP_OK;
185183
}
186184

187-
// esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait)
188-
// {
189-
// KIT_LOGD(LOG_METHOD);
190-
// I2C_BUS_CHECK(bus != NULL, "Handle error", ESP_FAIL);
191-
// I2C_BUS_CHECK(cmd != NULL, "I2C cmd error", ESP_FAIL);
192-
// i2c_bus_t *p_bus = (i2c_bus_t *) bus;
193-
194-
// esp_err_t ret = i2c_master_cmd_begin(p_bus->i2c_port, cmd, ticks_to_wait);
195-
// return ret;
196-
// }
197-
198185
#endif

src/peripherals/i2c_wire.cpp

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,116 @@
1+
/**
2+
* @file i2c_wire.cpp
3+
* @author Phil Schatzmann
4+
* @brief I2S Implementation using Arduino Wire Library
5+
* @date 2021-12-12
6+
*
7+
* @copyright Copyright (c) 2021
8+
*
9+
*/
110
#include "AudioKitSettings.h"
211

312
#ifdef AUDIOKIT_USE_WIRE
413

514
#include <Wire.h>
615
#include <stdio.h>
7-
#include "i2c_bus.h"
8-
#include "audiokit_logger.h"
16+
917
#include "audio_error.h"
18+
#include "audiokit_logger.h"
19+
#include "i2c_bus.h"
20+
21+
// 0x20
22+
#define PORT 0x20
23+
#define END true
24+
25+
i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t* conf)
26+
{
27+
KIT_LOGD(LOG_METHOD);
1028

29+
KIT_LOGD("sda: %d", conf->sda_io_num);
30+
KIT_LOGD("scl: %d", conf->scl_io_num);
31+
Wire.setPins(conf->sda_io_num, conf->scl_io_num);
1132

12-
i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t *conf){
13-
// Wire.begin(I2C_SDA, I2C_SCL);
14-
if (!Wire.begin(conf->sda_io_num,conf->scl_io_num)){
15-
LOGE("->Wire.begin");
33+
KIT_LOGD("clk_speed: %d", conf->master.clk_speed);
34+
Wire.setClock(conf->master.clk_speed);
35+
36+
if (!Wire.begin()) {
37+
KIT_LOGE("->Wire.begin");
1638
}
39+
1740
return nullptr;
1841
}
1942

20-
esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait){
21-
LOGD(LOG_METHOD);
43+
esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE_TYPE ticks_to_wait)
44+
{
45+
KIT_LOGD(LOG_METHOD);
2246
return ESP_OK;
2347
}
2448

25-
// 0x20
26-
#define PORT 0x20
27-
2849
// this method is used !
29-
esp_err_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg, int reglen, uint8_t *data, int datalen){
30-
LOGD(LOG_METHOD);
31-
assert(addr == PORT);
32-
LOGD("->i2c addr: %d", addr);
50+
esp_err_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t* reg, int reglen, uint8_t* data, int datalen)
51+
{
52+
KIT_LOGD("i2c_bus_write_bytes: addr=%d reglen=%d datalen=%d - reg=%d value=%d", addr, reglen, datalen, reg[0], data[0]);
53+
54+
assert(reglen == 1);
55+
assert(datalen == 1);
56+
3357
int result = ESP_OK;
34-
Wire.beginTransmission(addr);
35-
int len = Wire.write(reg, reglen);
36-
if (len!=reglen){
37-
LOGE("->Wire.write addr");
38-
result = ESP_FAIL;
39-
}
40-
len = Wire.write(data, datalen);
41-
if (len!=reglen){
42-
LOGE("->Wire.write data");
43-
result = ESP_FAIL;
44-
}
45-
int rc = Wire.endTransmission();
46-
if (rc!=0){
47-
LOGE("->Wire.endTransmission: %d",rc);
58+
Wire.beginTransmission(addr >> 1);
59+
Wire.write(reg[0]);
60+
Wire.write(data[0]);
61+
int rc = Wire.endTransmission(END);
62+
if (rc != 0) {
63+
KIT_LOGE("->Wire.endTransmission: %d", rc);
4864
result = ESP_FAIL;
4965
}
5066
return result;
5167
}
5268

53-
esp_err_t i2c_bus_write_data(i2c_bus_handle_t bus, int addr, uint8_t *data, int datalen){
54-
LOGD(LOG_METHOD);
55-
assert(addr == PORT);
69+
esp_err_t i2c_bus_write_data(i2c_bus_handle_t bus, int addr, uint8_t* data, int datalen)
70+
{
71+
KIT_LOGD("i2c_bus_write_data: addr=%d len=%d", addr, datalen);
72+
assert(datalen == 1);
73+
5674
int result = ESP_OK;
57-
Wire.beginTransmission(addr);
58-
int len = Wire.write(data,datalen);
59-
if (len!=datalen){
60-
LOGE("->Wire.write data");
61-
result = ESP_FAIL;
62-
}
63-
int rc = Wire.endTransmission();
64-
if (rc!=0){
65-
LOGE("->Wire.endTransmission: %d", rc);
75+
Wire.beginTransmission(addr >> 1);
76+
Wire.write(data, datalen);
77+
int rc = Wire.endTransmission(END);
78+
if (rc != 0) {
79+
KIT_LOGE("->Wire.endTransmission: %d", rc);
6680
result = ESP_FAIL;
6781
}
6882
return result;
6983
}
7084

7185
/// This method is used
72-
esp_err_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg, int reglen, uint8_t *outdata, int datalen){
73-
LOGD(LOG_METHOD);
74-
assert(addr == PORT);
75-
int result = i2c_bus_write_bytes(bus, addr, reg, reglen, outdata, datalen);
76-
77-
uint8_t result_len = Wire.requestFrom((uint16_t)addr, (uint8_t)datalen,(bool) true);
78-
LOGD("->Wire.requestFrom %d->%d", datalen, result_len);
79-
// wait for result
80-
while(Wire.available()<datalen){
81-
delay(10);
82-
}
83-
LOGD("->Wire.available %d->%d", datalen, Wire.available());
84-
for (int j=0;j<datalen;j++){
85-
outdata[j]=Wire.read();
86+
esp_err_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t* reg, int reglen, uint8_t* outdata, int datalen)
87+
{
88+
KIT_LOGD("i2c_bus_read_bytes: addr=%d reglen=%d datalen=%d - reg=%d", addr, reglen, datalen, reg[0]);
89+
assert(reglen == 1);
90+
assert(datalen == 1);
91+
92+
outdata[0] = 0;
93+
int result = ESP_OK;
94+
95+
Wire.beginTransmission(addr >> 1);
96+
Wire.write(reg[0]);
97+
int rc = Wire.endTransmission();
98+
99+
uint8_t result_len = Wire.requestFrom((uint16_t)(addr >> 1), (uint8_t)1, true);
100+
if (result_len > 0) {
101+
result_len = Wire.readBytes(outdata, datalen);
102+
} else {
103+
KIT_LOGE("->Wire.requestFrom %d->%d", datalen, result_len);
104+
result = ESP_FAIL;
86105
}
87106
return result;
88107
}
89108

90-
esp_err_t i2c_bus_delete(i2c_bus_handle_t bus){
91-
LOGD(LOG_METHOD);
92-
#ifndef ESP32
109+
esp_err_t i2c_bus_delete(i2c_bus_handle_t bus)
110+
{
111+
KIT_LOGD(LOG_METHOD);
93112
Wire.end();
94-
#endif
95113
return ESP_OK;
96114
}
97115

98-
99-
100116
#endif

0 commit comments

Comments
 (0)