This commit is contained in:
2020-06-11 11:25:19 +02:00
parent 5cb02880b3
commit 5177cd5cbd
15 changed files with 1090 additions and 411 deletions

View File

@@ -8,7 +8,7 @@
#include "fastGPIO.h"
struct GPIO {
struct MyGPIO {
static inline bool get(const uint8_t num) {
return GPIO_INPUT_GET(num);
@@ -64,6 +64,34 @@
}
}
static inline void setPullUp(const uint8_t num) {
switch(num) {
case 0: GPIO0_INPUT_PULLUP_SET; break;
case 1: GPIO1_INPUT_PULLUP_SET; break;
case 2: GPIO2_INPUT_PULLUP_SET; break;
case 3: GPIO3_INPUT_PULLUP_SET; break;
case 4: GPIO4_INPUT_PULLUP_SET; break;
case 5: GPIO5_INPUT_PULLUP_SET; break;
//case 6: GPIO6_INPUT_PULLUP_SET; break;
//case 7: GPIO7_INPUT_PULLUP_SET; break;
//case 8: GPIO8_INPUT_PULLUP_SET; break;
case 9: GPIO9_INPUT_PULLUP_SET; break;
case 10: GPIO10_INPUT_PULLUP_SET; break;
//case 11: GPIO11_INPUT_PULLUP_SET; break;
case 12: GPIO12_INPUT_PULLUP_SET; break;
case 13: GPIO13_INPUT_PULLUP_SET; break;
case 14: GPIO14_INPUT_PULLUP_SET; break;
case 15: GPIO15_INPUT_PULLUP_SET; break;
}
}
static void toggleBuiltInLED() {
static bool level = false;
setOutput(2);
level = !level;
if (level) {set(2);} else {clear(2);}
}
};
#elif ESP32
@@ -87,24 +115,40 @@
static inline void set(const uint8_t num) {
WRITE_PERI_REG(GPIO_OUT_W1TS_REG, 1 << num);
//gpio_set_level((gpio_num_t)num, 1);
}
static inline void clear(const uint8_t num) {
WRITE_PERI_REG(GPIO_OUT_W1TC_REG, 1 << num);
//gpio_set_level((gpio_num_t)num, 0);
}
static inline void setOutput(const uint8_t num) {
setOutput((gpio_num_t)num);
}
static inline void setOutput(const gpio_num_t num) {
gpio_set_direction(num, GPIO_MODE_OUTPUT);
//gpio_set_direction(num, GPIO_MODE_OUTPUT); // does not always suffice?!
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1<<num);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
}
static inline void setInput(const uint8_t num) {
setInput((gpio_num_t)num);
}
static inline void setInput(const gpio_num_t num) {
gpio_set_direction(num, GPIO_MODE_INPUT);
//gpio_set_direction(num, GPIO_MODE_INPUT); // does not always suffice?!
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1<<num);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
}
static void toggleBuiltInLED() {

109
io/I2S.h Normal file
View File

@@ -0,0 +1,109 @@
#ifndef I2S_H
#define I2S_H
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2s.h"
template <int PIN_DATA, int PIN_CLK, int PIN_LR> class I2S {
static constexpr i2s_port_t port = I2S_NUM_0;
i2s_config_t cfg;
struct Setup {
uint32_t sampleRate = 0;
i2s_bits_per_sample_t bitsPerSample = I2S_BITS_PER_SAMPLE_8BIT;
i2s_channel_t channels = I2S_CHANNEL_MONO;
} cur;
public:
/** ctor */
I2S() {
#ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
cfg.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
#else
cfg.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX);
#endif
cfg.dma_buf_count = 6;
cfg.dma_buf_len = 128;
cfg.sample_rate = 44100;
cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
cfg.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; //2-channels
cfg.communication_format = I2S_COMM_FORMAT_I2S_MSB;
cfg.intr_alloc_flags = 0; //Default interrupt priority
cfg.tx_desc_auto_clear = true; //Auto clear tx descriptor on underflow
i2s_driver_install(port, &cfg, 0, nullptr);
#ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
i2s_set_pin(0, NULL);
#else
i2s_pin_config_t pins;
pins.bck_io_num = PIN_CLK;//CONFIG_I2S_BCK_PIN;
pins.ws_io_num = PIN_LR;//CONFIG_I2S_LRCK_PIN;
pins.data_out_num = PIN_DATA;//CONFIG_I2S_DATA_PIN;
pins.data_in_num = -1; //Not used
i2s_set_pin(port, &pins);
#endif
configure(44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
}
/** change the i2s configuration */
void configure(const uint32_t sampleRate, const i2s_bits_per_sample_t bitsPerSample, const i2s_channel_t channels) {
// only update when changed!
if (cur.sampleRate != sampleRate || cur.bitsPerSample != bitsPerSample || cur.channels != channels) {
i2s_set_clk(port, sampleRate, bitsPerSample, channels); // update
cur.sampleRate = sampleRate;
cur.bitsPerSample = bitsPerSample;
cur.channels = channels;
}
}
/** add the given samples for playback. returns the number of added samples, which might be less in case of timeouts */
size_t add(const uint8_t* data, const size_t len) {
size_t written;
i2s_write(port, data, len, &written, portMAX_DELAY);
return written;
}
uint16_t tmp[2048];
size_t addSignedToUnsigned(const uint8_t* data, const size_t len) {
const int16_t* src = (const int16_t*) data;
uint16_t* dst = (uint16_t*) data;
for (int i = 0; i < len/2; ++i) {
//dst[i] = (int)src[i] + (int)32768;
//dst[i] = (int)src[i] / 270 + 128;
tmp[i] = (int)src[i] + 32768;
}
//return add(data, len);
return add((const uint8_t*)tmp, len);
}
// /** add the given samples for playback. blocks until all have been added */
// void addAll(const uint8_t* data, const size_t len) {
// size_t added = add(data, len);
// len -= added;
// data +=
// }
};
#endif // I2S_H

View File

@@ -6,10 +6,284 @@
#include "GPIO.h"
/** new implementation */
template <int PIN_SDA, int PIN_SCL, bool fast> class SoftI2C {
static constexpr const char* NAME = "softI2C";
inline void sdaDirOut() {MyGPIO::setOutput(PIN_SDA);}
inline void sdaDirIn() {MyGPIO::setInput(PIN_SDA);}
inline void sclDirOut() {MyGPIO::setOutput(PIN_SCL);}
inline void sdaHi() {MyGPIO::set(PIN_SDA);}
inline void sdaLo() {MyGPIO::clear(PIN_SDA);}
inline bool sdaRead() {return MyGPIO::get(PIN_SDA);}
inline void sclHi() {MyGPIO::set(PIN_SCL);}
inline void sclLo() {MyGPIO::clear(PIN_SCL);}
inline void waitLong() {
for (uint8_t i = 0; i < 240; ++i) {
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
}
}
inline void waitShort() {
for (uint8_t i = 0; i < 240; ++i) {
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
}
}
void init() {
debugMod2(NAME, "init. SDA: %d, SCL: %d", PIN_SDA, PIN_SCL);
}
public:
/** ctor */
SoftI2C() {
init();
}
/** start communication */
inline void start() {
sdaDirOut();
sclDirOut();
sdaHi();
sclHi();
waitLong();
sdaLo();
waitLong();
sclLo();
waitLong();
}
/** stop communication */
inline void stop() {
sdaDirOut();
sclDirOut();
sdaLo();
sclLo();
waitLong();
sclHi();
waitLong();
sdaHi();
sdaDirIn(); // free the bus
waitLong();
}
/** read the given number of bytes from the slave and generate ACK/NACK as needed */
void readBytes(uint8_t* dst, uint8_t len) {
while(len) {
*dst = _readByte();
--len;
++dst;
if (len) {_writeAck();} else {_writeNAck();} // done? or want more?
}
}
// write one byte to the bus and check slave's ACK/NACK
bool IN_FLASH writeByteAndCheck(const uint8_t byte) {
_writeByte(byte);
return _readAck();
}
// write several bytes to the bus and check slave's ACK/NACK
bool IN_FLASH writeBytesAndCheck(const uint8_t* src, uint8_t len) {
while(len) {
const bool ok = writeByteAndCheck(*src);
if (!ok) {return false;}
--len; ++src;
}
return true;
}
inline bool startWrite(const uint8_t addr7) {
start();
_writeByte( (addr7<<1) | 0 );
const bool ok = _readAck();
return ok;
}
inline bool startRead(const uint8_t addr7) {
start();
_writeByte( (addr7<<1) | 1 );
const bool ok = _readAck();
return ok;
}
inline bool query(const uint8_t addr7) {
const bool ok = startWrite(addr7);
stop();
return ok;
}
public:
uint8_t readReg8(const uint8_t addr, const uint8_t reg) {
uint8_t dst = 0xFF;
readReg(addr, reg, 1, &dst);
return dst;
}
bool readReg(const uint8_t addr, const uint8_t reg, const uint8_t len, uint8_t* dst) {
bool ok;
// select register(s) to read
ok = startWrite(addr);
if (!ok) {debugMod(NAME, "failed start write1\n"); return false;}
ok = writeByteAndCheck(reg);
if (!ok) {debugMod(NAME, "failed select register\n"); return false;}
stop();
// read register(s)
ok = startRead(addr);
if (!ok) {debugMod(NAME, "failed start write2\n"); return false;}
readBytes(dst, len);
stop();
return true;
}
bool writeReg(const uint8_t addr, const uint8_t reg, const uint8_t len, const uint8_t* src) {
bool ok;
// address the slave in write mode and select the first register to read
ok = startWrite(addr);
if (!ok) {debugMod(NAME, "failed start write\n"); return false;}
ok = writeByteAndCheck(reg);
if (!ok) {debugMod1(NAME, "failed to select register %d\n", addr); return false;}
ok = writeBytesAndCheck(src, len);
if (!ok) {debugMod(NAME, "failed to write register contents \n"); return false;}
// done
stop();
return true;
}
bool writeReg8(const uint8_t addr, const uint8_t reg, const uint8_t val) {
return writeReg(addr, reg, 1, &val);
}
private:
/** write one bit to the bus */
inline void _writeBit(const bool out) {
//sdaDirOut(); // switch to output mode
if(out) {sdaHi();} else {sdaLo();} // apply data
sclHi(); // clock pulse
waitShort();
sclLo();
waitShort();
//sdaDirIn(); // free the bus
}
/** read one bit from the bus */
inline uint8_t _readBit() {
//sdaDirIn(); // switch to input mode
sclHi(); // clock pulse and read
waitShort();
const uint8_t val = sdaRead();
sclLo();
waitShort();
return val;
}
// read 8 bits from the bus, WITHOUT sending ACK/NACK
uint8_t _readByte() {
sdaDirIn(); // switch SDA to input mode
return
(_readBit() << 7) |
(_readBit() << 6) |
(_readBit() << 5) |
(_readBit() << 4) |
(_readBit() << 3) |
(_readBit() << 2) |
(_readBit() << 1) |
(_readBit() << 0);
}
inline bool _readAck() {
sdaDirIn(); // switch to input mode
const uint8_t res = _readBit();
return res == 0; // ACK when input pulled low
}
// write one byte to the bus
void _writeByte(const uint8_t byte) {
sdaDirOut(); // switch SDA to output mode
_writeBit(byte & (1<<7));
_writeBit(byte & (1<<6));
_writeBit(byte & (1<<5));
_writeBit(byte & (1<<4));
_writeBit(byte & (1<<3));
_writeBit(byte & (1<<2));
_writeBit(byte & (1<<1));
_writeBit(byte & (1<<0));
}
inline void _writeAck() {
sdaDirOut(); // switch SDA to output mode
_writeBit(0);
}
inline void _writeNAck() {
sdaDirOut(); // switch SDA to output mode
_writeBit(1);
}
};
/*
#if ESP8266
#include "fastGPIO.h"
#elif ESP32
# error "Not yet supported"
#include "GPIO.h"
#endif
// https://www.best-microcontroller-projects.com/i2c-tutorial.html
@@ -42,6 +316,8 @@ namespace i2c {
#elif ESP32
//# error "Not yet supported"
#endif
@@ -127,7 +403,7 @@ namespace i2c {
}
/** write one bit to the bus */
// write one bit to the bus
static inline void writeBit(const bool out) {
sdaDirOut(); // switch to output mode
if(out) {sdaHi();} else {sdaLo();} // apply data
@@ -138,7 +414,7 @@ namespace i2c {
sdaDirIn(); // free the bus
}
/** read one bit from the bus */
// read one bit from the bus
static inline uint8_t readBit() {
sdaDirIn(); // switch to input mode
sclHi(); // clock pulse and read
@@ -163,7 +439,7 @@ namespace i2c {
writeBit(1);
}
/** read 8 bits from the bus, WITHOUT sending ACK/NACK */
// read 8 bits from the bus, WITHOUT sending ACK/NACK
static uint8_t IN_FLASH readByte() {
return
(readBit() << 7) |
@@ -176,7 +452,7 @@ namespace i2c {
(readBit() << 0);
}
/** read the given number of bytes from the slave and generate ACK/NACK as needed */
// read the given number of bytes from the slave and generate ACK/NACK as needed
static void readBytes(uint8_t* dst, uint8_t len) {
while(len) {
*dst = readByte();
@@ -186,7 +462,7 @@ namespace i2c {
}
}
/** write one byte to the bus */
// write one byte to the bus
static void IN_FLASH writeByte(const uint8_t byte) {
writeBit(byte & BIT( 7));
writeBit(byte & BIT( 6));
@@ -198,13 +474,13 @@ namespace i2c {
writeBit(byte & BIT( 0));
}
/** write one byte to the bus and check slave's ACK/NACK */
// write one byte to the bus and check slave's ACK/NACK
static bool IN_FLASH writeByteAndCheck(const uint8_t byte) {
writeByte(byte);
return readAck();
}
/** write several bytes to the bus and check slave's ACK/NACK */
// write several bytes to the bus and check slave's ACK/NACK
static bool IN_FLASH writeBytesAndCheck(const uint8_t* src, uint8_t len) {
while(len) {
const bool ok = writeByteAndCheck(*src);
@@ -236,5 +512,6 @@ namespace i2c {
}
}
*/
#endif // SOFTSPI_H

View File

@@ -347,10 +347,10 @@ public:
private:
void init() {
debugMod(NAME, "init()");
GPIO::setInput(PIN_MISO);
GPIO::setOutput(PIN_MOSI);
GPIO::setOutput(PIN_CLK);
debugMod3(NAME, "init() MISO:%d MOSI:%d CLK:%d", PIN_MISO, PIN_MOSI, PIN_CLK);
if (PIN_MISO) {MyGPIO::setInput(PIN_MISO);}
if (PIN_MOSI) {MyGPIO::setOutput(PIN_MOSI);}
MyGPIO::setOutput(PIN_CLK);
}
private:
@@ -363,12 +363,12 @@ private:
}
}
inline void clkLo() { GPIO::clear(PIN_CLK); }
inline void clkHi() { GPIO::set(PIN_CLK); }
inline void clkLo() { MyGPIO::clear(PIN_CLK); }
inline void clkHi() { MyGPIO::set(PIN_CLK); }
/** write one bit to the bus */
inline void writeBit(const bool out) {
if(out) {GPIO::set(PIN_MOSI);} else {GPIO::clear(PIN_MOSI);}
if(out) {MyGPIO::set(PIN_MOSI);} else {MyGPIO::clear(PIN_MOSI);}
wait();
clkHi();
wait();
@@ -380,7 +380,7 @@ private:
inline uint8_t readBit() {
clkHi();
wait();
const bool val = GPIO::get(PIN_MISO);
const bool val = MyGPIO::get(PIN_MISO);
wait();
clkLo();
wait();
@@ -388,11 +388,11 @@ private:
}
inline uint8_t readWriteBit(const bool out) {
if(out) {GPIO::set(PIN_MOSI);} else {GPIO::clear(PIN_MOSI);}
if(out) {MyGPIO::set(PIN_MOSI);} else {MyGPIO::clear(PIN_MOSI);}
wait();
clkHi();
wait();
const bool inp = GPIO::get(PIN_MISO);
const bool inp = MyGPIO::get(PIN_MISO);
wait();
clkLo();
wait();
@@ -474,7 +474,7 @@ public:
/** read 8 bits */
uint8_t readByte() {
GPIO::clear(PIN_MOSI);
MyGPIO::clear(PIN_MOSI);
return
(readBit() << 7) |
(readBit() << 6) |