cpp state machine for ethernet

This commit is contained in:
Sebastien L
2021-11-16 10:11:38 -05:00
parent 699c1da42f
commit 81756a7649
43 changed files with 4463 additions and 2774 deletions

View File

@@ -15,7 +15,6 @@
#include "accessors.h"
#include "globdefs.h"
#include "display.h"
#include "display.h"
#include "cJSON.h"
#include "driver/gpio.h"
#include "stdbool.h"
@@ -118,13 +117,8 @@ static void set_i2s_pin(char *config, i2s_pin_config_t *pin_config) {
* Get i2s config structure from config string
*/
const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
static EXT_RAM_ATTR i2s_platform_config_t i2s_dac_pin = {
.i2c_addr = -1,
.sda= -1,
.scl = -1,
.mute_gpio = -1,
.mute_level = -1
};
static EXT_RAM_ATTR i2s_platform_config_t i2s_dac_pin;
memset(&i2s_dac_pin, 0xFF, sizeof(i2s_dac_pin));
set_i2s_pin(dac_config, &i2s_dac_pin.pin);
strcpy(i2s_dac_pin.model, "i2s");
char * p=NULL;
@@ -146,11 +140,12 @@ const i2s_platform_config_t * config_get_i2s_from_str(char * dac_config ){
* Get eth config structure from config string
*/
const eth_config_t * config_get_eth_from_str(char * eth_config ){
static EXT_RAM_ATTR eth_config_t eth_pin = {
.rmii = false,
.model = "",
};
char * p=NULL;
static EXT_RAM_ATTR eth_config_t eth_pin;
memset(&eth_pin, 0xFF, sizeof(eth_pin));
memset(&eth_pin.model, 0xFF, sizeof(eth_pin.model));
eth_pin.spi = false;
eth_pin.rmii = false;
if ((p = strcasestr(eth_config, "model")) != NULL) sscanf(p, "%*[^=]=%15[^,]", eth_pin.model);
if ((p = strcasestr(eth_config, "mdc")) != NULL) eth_pin.mdc = atoi(strchr(p, '=') + 1);
@@ -163,9 +158,16 @@ const eth_config_t * config_get_eth_from_str(char * eth_config ){
if ((p = strcasestr(eth_config, "speed")) != NULL) eth_pin.speed = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "clk")) != NULL) eth_pin.clk = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(eth_config, "host")) != NULL) eth_pin.host = atoi(strchr(p, '=') + 1);
eth_pin.valid = eth_pin.model && strlen(eth_pin.model)>0 && GPIO_IS_VALID_GPIO(eth_pin.mdio) && GPIO_IS_VALID_GPIO(eth_pin.mdc);
if (strcasestr(eth_pin.model, "lan8720")) eth_pin.rmii = true;
if(strcasestr(eth_pin.model, "LAN8720")){
eth_pin.rmii = true;
}
else {
eth_pin.spi = true;
/* here we must also check that we have at least a CS gpio */
eth_pin.valid = eth_pin.valid && GPIO_IS_VALID_GPIO(eth_pin.cs);
}
return &eth_pin;
}
@@ -196,22 +198,37 @@ const i2s_platform_config_t * config_dac_get(){
*/
const eth_config_t * config_eth_get( ){
char * config = config_alloc_get_str("eth_config", CONFIG_ETH_CONFIG, "rst=" STR(CONFIG_ETH_PHY_RST_IO)
#if defined(CONFIG_ETH_LAN8720)
",model=lan8720"
#elif defined(CONFIG_ETH_DM9051)
#if defined(ETH_LAN8720)
#else
#if defined(CONFIG_ETH_USE_SPI_ETHERNET)
#if defined(CONFIG_ETH_DM9051)
",model=dm9051"
#endif
",mdc=" STR(CONFIG_ETH_MDC_IO) ",mdio=" STR(CONFIG_ETH_MDIO_IO)
#elif defined(CONFIG_ETH_W5500)
",model=w5500"
#endif
",host=" STR(CONFIG_ETH_SPI_HOST) ",cs=" STR(CONFIG_ETH_SPI_CS_IO)
",mosi=" STR(CONFIG_ETH_SPI_MOSI_IO) ",miso=" STR(CONFIG_ETH_SPI_MISO_IO)
",intr=" STR(CONFIG_ETH_SPI_INTR_IO)
",clk=" STR(CONFIG_ETH_SPI_CLK_IO) ",speed=" STR(CONFIG_ETH_SPI_SPEED) );
",clk=" STR(CONFIG_ETH_SPI_CLK_IO) ",speed=" STR(CONFIG_ETH_SPI_SPEED)
#elif defined(CONFIG_ETH_PHY_INTERFACE_RMII)
",model=lan8720, tx_en=21, tx0=19, tx1=22, rx0=25, rx1=26, crs_dv=27"
#endif
#endif
",mdc=" STR(CONFIG_ETH_MDC_IO) ",mdio=" STR(CONFIG_ETH_MDIO_IO)) ;
static EXT_RAM_ATTR eth_config_t eth_config;
ESP_LOGD(TAG, "Ethernet config string %s", config);
memcpy(&eth_config, config_get_eth_from_str(config), sizeof(eth_config));
free(config);
return &eth_config;
}
/****************************************************************************************
* Get ethernet config structure and assign to eth config structure
*/
void config_eth_init( eth_config_t * target ){
const eth_config_t * source = config_eth_get();
memcpy(target,source,sizeof(eth_config_t));
}
/****************************************************************************************
*
@@ -561,14 +578,9 @@ const set_GPIO_struct_t * get_gpio_struct(){
*/
const spi_bus_config_t * config_spi_get(spi_host_device_t * spi_host) {
char *nvs_item, *p;
static EXT_RAM_ATTR spi_bus_config_t spi = {
.mosi_io_num = -1,
.sclk_io_num = -1,
.miso_io_num = -1,
.quadwp_io_num = -1,
.quadhd_io_num = -1
};
static EXT_RAM_ATTR spi_bus_config_t spi;
memset(&spi, 0xFF, sizeof(spi));
nvs_item = config_alloc_get_str("spi_config", CONFIG_SPI_CONFIG, NULL);
if (nvs_item) {
if ((p = strcasestr(nvs_item, "data")) != NULL) spi.mosi_io_num = atoi(strchr(p, '=') + 1);
@@ -770,6 +782,46 @@ cJSON * get_SPI_GPIO(cJSON * list){
return llist;
}
/****************************************************************************************
*
*/
cJSON * get_eth_GPIO(cJSON * list){
cJSON * llist = list;
if(!llist){
llist = cJSON_CreateArray();
}
spi_host_device_t spi_host;
const eth_config_t * eth_config = config_eth_get(&spi_host);
#if defined(CONFIG_ETH_CONFIG)
bool fixed = strlen(CONFIG_ETH_CONFIG)>0;
#else
bool fixed =false;
#endif
if(eth_config->valid ){
add_gpio_for_value(llist,"mdc",eth_config->mdc,"ethernet",fixed);
add_gpio_for_value(llist,"rst",eth_config->rst,"ethernet",fixed);
add_gpio_for_value(llist,"mdio",eth_config->mdio,"ethernet",fixed);
if(eth_config->rmii){
add_gpio_for_value(llist,"tx_en", 21,"ethernet",true);
add_gpio_for_value(llist,"tx0", 19 ,"ethernet",true);
add_gpio_for_value(llist,"tx1", 22 ,"ethernet",true);
add_gpio_for_value(llist,"rx0", 25 ,"ethernet",true);
add_gpio_for_value(llist,"rx1", 26 ,"ethernet",true);
add_gpio_for_value(llist,"crs_dv",27 ,"ethernet",true);
}
else if(eth_config->spi) {
/* SPI ethernet */
add_gpio_for_value(llist,"cs",eth_config->cs,"ethernet",fixed);
add_gpio_for_value(llist,"mosi",eth_config->mosi,"ethernet",fixed);
add_gpio_for_value(llist,"miso",eth_config->miso,"ethernet",fixed);
add_gpio_for_value(llist,"intr",eth_config->intr,"ethernet",fixed);
add_gpio_for_value(llist,"clk",eth_config->clk,"ethernet",fixed);
}
}
return llist;
}
/****************************************************************************************
*
*/
@@ -1013,5 +1065,6 @@ cJSON * get_gpio_list(bool refresh) {
gpio_list=get_I2C_GPIO(gpio_list);
gpio_list=get_DAC_GPIO(gpio_list);
gpio_list=get_psram_gpio_list(gpio_list);
gpio_list=get_eth_GPIO(gpio_list);
return gpio_list;
}

View File

@@ -31,8 +31,10 @@ typedef struct {
} display_config_t;
typedef struct {
bool rmii;
char model[16];
bool valid;
bool rmii;
bool spi;
int rst;
int mdc, mdio;
int host;
@@ -86,6 +88,7 @@ typedef struct {
const display_config_t * config_display_get();
const eth_config_t * config_eth_get( );
void config_eth_init( eth_config_t * target );
esp_err_t config_display_set(const display_config_t * config);
esp_err_t config_i2c_set(const i2c_config_t * config, int port);
esp_err_t config_i2s_set(const i2s_platform_config_t * config, const char * nvs_name);

View File

@@ -47,7 +47,7 @@ static EXT_RAM_ATTR struct button_s {
TimerHandle_t timer;
} buttons[MAX_BUTTONS];
static EXT_RAM_ATTR struct {
static struct {
int gpio, level;
struct button_s *button;
} polled_gpio[] = { {36, -1, NULL}, {39, -1, NULL}, {-1, -1, NULL} };

View File

@@ -28,7 +28,7 @@
#include "driver/i2s.h"
#include "driver/rtc_io.h"
#include "driver/dac.h"
#include <adc1_i2s_private.h>
#include "adc1_private.h"
#include "esp_intr_alloc.h"
#include "esp_err.h"
@@ -880,7 +880,7 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co
//initialize the specific ADC channel.
//in the current stage, we only support ADC1 and single channel mode.
//In default data mode, the ADC data is in 12-bit resolution mode.
adc_power_always_on();
// todo: fix this - adc_power_always_on();
}
// configure I2S data port interface.
i2s_reset_fifo(i2s_num);
@@ -974,35 +974,36 @@ static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_co
I2S[i2s_num]->pdm_conf.rx_pdm_en = 0;
I2S[i2s_num]->pdm_conf.tx_pdm_en = 0;
}
if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S) {
I2S[i2s_num]->conf.tx_short_sync = 0;
I2S[i2s_num]->conf.rx_short_sync = 0;
I2S[i2s_num]->conf.tx_msb_shift = 1;
I2S[i2s_num]->conf.rx_msb_shift = 1;
if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S_LSB) {
if (i2s_config->mode & I2S_MODE_TX) {
I2S[i2s_num]->conf.tx_msb_shift = 0;
}
if (i2s_config->mode & I2S_MODE_RX) {
I2S[i2s_num]->conf.rx_msb_shift = 0;
}
}
}
// todo: fix this below!!
// if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S) {
// I2S[i2s_num]->conf.tx_short_sync = 0;
// I2S[i2s_num]->conf.rx_short_sync = 0;
// I2S[i2s_num]->conf.tx_msb_shift = 1;
// I2S[i2s_num]->conf.rx_msb_shift = 1;
// if (i2s_config->communication_format & I2S_COMM_FORMAT_I2S_LSB) {
// if (i2s_config->mode & I2S_MODE_TX) {
// I2S[i2s_num]->conf.tx_msb_shift = 0;
// }
// if (i2s_config->mode & I2S_MODE_RX) {
// I2S[i2s_num]->conf.rx_msb_shift = 0;
// }
// }
// }
if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM) {
I2S[i2s_num]->conf.tx_msb_shift = 0;
I2S[i2s_num]->conf.rx_msb_shift = 0;
I2S[i2s_num]->conf.tx_short_sync = 0;
I2S[i2s_num]->conf.rx_short_sync = 0;
if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM_SHORT) {
if (i2s_config->mode & I2S_MODE_TX) {
I2S[i2s_num]->conf.tx_short_sync = 1;
}
if (i2s_config->mode & I2S_MODE_RX) {
I2S[i2s_num]->conf.rx_short_sync = 1;
}
}
}
// if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM) {
// I2S[i2s_num]->conf.tx_msb_shift = 0;
// I2S[i2s_num]->conf.rx_msb_shift = 0;
// I2S[i2s_num]->conf.tx_short_sync = 0;
// I2S[i2s_num]->conf.rx_short_sync = 0;
// if (i2s_config->communication_format & I2S_COMM_FORMAT_PCM_SHORT) {
// if (i2s_config->mode & I2S_MODE_TX) {
// I2S[i2s_num]->conf.tx_short_sync = 1;
// }
// if (i2s_config->mode & I2S_MODE_RX) {
// I2S[i2s_num]->conf.rx_short_sync = 1;
// }
// }
// }
if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
I2S[i2s_num]->conf.sig_loopback = 1;
if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
@@ -1217,7 +1218,7 @@ esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
adc1_i2s_mode_acquire();
// todo: fix this adc1_i2s_mode_acquire();
_i2s_adc_mode_recover();
return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
}

View File

@@ -238,7 +238,16 @@ void messaging_post_message(messaging_types type,messaging_classes msg_class, co
message->type = type;
message->msg_class = msg_class;
message->sent_time = esp_timer_get_time() / 1000;
ESP_LOGD(tag,"Post: %s",message->message);
if(type==MESSAGING_WARNING) {
ESP_LOGW(tag,"%s",message->message);
}
else if(type==MESSAGING_ERROR) {
ESP_LOGE(tag,"%s",message->message);
}
else {
ESP_LOGD(tag,"Post: %s",message->message);
}
while(cur){
messaging_post_to_queue(get_handle_ptr(cur), message, msg_size);
cur = get_struct_ptr(cur->next);

View File

@@ -3,6 +3,10 @@
#include "freertos/ringbuf.h"
#include "cJSON.h"
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
MESSAGING_INFO,
MESSAGING_WARNING,
@@ -48,3 +52,6 @@ messaging_post_message(y, MESSAGING_CLASS_SYSTEM, ##__VA_ARGS__); }
#define LOG_SEND_WARN( ...) LOG_SEND(MESSAGING_WARNING,##__VA_ARGS__)
#ifdef __cplusplus
}
#endif