Skip to content

Commit

Permalink
Merge pull request open-dynamic-robot-initiative#16 from AlexisPotier…
Browse files Browse the repository at this point in the history
…/wifi_eth_autoselect

Autoselection between wifi and ethernet
  • Loading branch information
maximekli authored Jul 6, 2020
2 parents c3fc74a + 5f3fb8d commit 9fa1b5b
Show file tree
Hide file tree
Showing 7 changed files with 713 additions and 78 deletions.
4 changes: 2 additions & 2 deletions documentation/masterboard_state_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ The master board runs a state machine that handles the differents stages of comm

State machine description and behaviour
---
[![master board state machine diagram](../images/master_board_state_machine.svg)](https://mermaid-js.github.io/mermaid-live-editor/#/edit/eyJjb2RlIjoic3RhdGVEaWFncmFtXG4gICAgc3RhdGUgXCJTRVRVUFwiIGFzIFNFVFVQOiBTZXR0aW5nIHVwIGFsbCBjb21wb25lbnRzXG4gICAgc3RhdGUgXCJXQUlUSU5HX0ZPUl9JTklUXCIgYXMgV0FJVElOR19GT1JfSU5JVDogV2FpdGluZyBmb3IgYW4gSW5pdCBwYWNrZXQgZnJvbSBQQ1xuICAgIHN0YXRlIFwiU1BJX0FVVE9ERVRFQ1RcIiBhcyBTUElfQVVUT0RFVEVDVDogRGV0ZWN0aW5nIGNvbm5lY3RlZCBTUEkgc2xhdmVzXG4gICAgc3RhdGUgXCJTRU5ESU5HX0lOSVRfQUNLXCIgYXMgU0VORElOR19JTklUX0FDSzogU2VuZGluZyBBY2tub3dsZWRnZW1lbnQgcGFja2V0IHRvIFBDXG4gICAgc3RhdGUgXCJBQ1RJVkVfQ09OVFJPTFwiIGFzIEFDVElWRV9DT05UUk9MOiBSb2JvdCBhY3RpdmVseSBjb250cm9sbGVkIGJ5IHRoZSBQQ1xuICAgIHN0YXRlIFwiV0lGSV9FVEhfTElOS19ET1dOXCIgYXMgV0lGSV9FVEhfTElOS19ET1dOOiBFdGhlcm5ldCBsaW5rIGlzIGRvd25cbiAgICBzdGF0ZSBcIldJRklfRVRIX0VSUk9SXCIgYXMgV0lGSV9FVEhfRVJST1I6IFRpbWVvdXQsIG5vIHBhY2tldCBmcm9tIFBDXG5cbiAgICBbKl0gLS0-IFNFVFVQXG4gICAgU0VUVVAgLS0-IFdBSVRJTkdfRk9SX0lOSVQ6IFNldHVwIGRvbmUgaW4gV2lGaVxuICAgIFNFVFVQIC0tPiBXSUZJX0VUSF9MSU5LX0RPV046IFNldHVwIGRvbmUgaW4gRXRoZXJuZXRcblxuICAgIFdJRklfRVRIX0xJTktfRE9XTiAtLT4gV0FJVElOR19GT1JfSU5JVDogRXRoZXJuZXQgTGluayBVcFxuXG4gICAgV0FJVElOR19GT1JfSU5JVCAtLT4gU1BJX0FVVE9ERVRFQ1Q6IEluaXQgcGFja2V0IHJlY2VpdmVkXG4gICAgXG4gICAgU1BJX0FVVE9ERVRFQ1QgLS0-IFNFTkRJTkdfSU5JVF9BQ0s6IE51bWJlciBvZiB0ZXN0IHRyYW5zYWN0aW9ucyBleGNlZWRlZFxuXG4gICAgU0VORElOR19JTklUX0FDSyAtLT4gV0lGSV9FVEhfRVJST1I6IFRpbWVvdXQgYWNrbm93bGVkZ2VtZW50XG4gICAgU0VORElOR19JTklUX0FDSyAtLT4gQUNUSVZFX0NPTlRST0w6IEZpcnN0IGNvbW1hbmQgcGFja2V0IHJlY2VpdmVkXG5cbiAgICBBQ1RJVkVfQ09OVFJPTCAtLT4gV0lGSV9FVEhfRVJST1I6IFRpbWVvdXQgY29udHJvbFxuXG4gICAgV0lGSV9FVEhfRVJST1IgLS0-IFNQSV9BVVRPREVURUNUOiBJbml0IHBhY2tldCByZWNlaXZlZFxuIiwibWVybWFpZCI6eyJ0aGVtZSI6ImRlZmF1bHQifSwidXBkYXRlRWRpdG9yIjpmYWxzZX0)
[![master board state machine diagram](../images/master_board_state_machine.svg)](https://mermaid-js.github.io/mermaid-live-editor/#/edit/eyJjb2RlIjoic3RhdGVEaWFncmFtXG4gICAgc3RhdGUgXCJTRVRVUFwiIGFzIFNFVFVQOiBTZXR0aW5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ZWRcblxuICAgIFNFTkRJTkdfSU5JVF9BQ0sgLS0-IFdJRklfRVRIX0VSUk9SOiBUaW1lb3V0IGFja25vd2xlZGdlbWVudFxuICAgIFNFTkRJTkdfSU5JVF9BQ0sgLS0-IEFDVElWRV9DT05UUk9MOiBGaXJzdCBjb21tYW5kIHBhY2tldCByZWNlaXZlZFxuXG4gICAgQUNUSVZFX0NPTlRST0wgLS0-IFdJRklfRVRIX0VSUk9SOiBUaW1lb3V0IGNvbnRyb2xcblxuICAgIFdJRklfRVRIX0VSUk9SIC0tPiBTUElfQVVUT0RFVEVDVDogSW5pdCBwYWNrZXQgcmVjZWl2ZWRcbiIsIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19)

Diagram generated using [Mermaid](https://github.com/mermaid-js/mermaid) Live Editor, click on picture to edit.

Please note that **WIFI_ETH_LINK_DOWN** state is accessible from any state via a callback on the ethernet link down event, but links have not been drawn for the sake of readability.
Please note that **WIFI_ETH_LINK_DOWN** state is accessible from any state except WAITING_FOR_INIT via a callback on the ethernet link down event, but links have not been drawn for the sake of readability.

State machine LED code
---
Expand Down
20 changes: 13 additions & 7 deletions firmware/components/direct_ethernet/direct_ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
uint8_t eth_src_mac[6] = {0};
uint8_t eth_dst_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void (*eth_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len) = NULL;
void (*eth_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi) = NULL; // eth_or_wifi = 'e' when eth is used, 'w' when wifi is used

void (*eth_link_state_cb)(bool link_state) = NULL;

Expand All @@ -22,7 +22,7 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
case ETHERNET_EVENT_CONNECTED:
if (eth_link_state_cb == NULL)
{
ESP_LOGW(ETH_TAG, "Ethernet link Up but no callback function is set");
ESP_LOGW(ETH_TAG, "Ethernet link Up but no callback function is set");
}
else
{
Expand All @@ -38,13 +38,13 @@ static void eth_event_handler(void *arg, esp_event_base_t event_base,
case ETHERNET_EVENT_DISCONNECTED:
if (eth_link_state_cb == NULL)
{
ESP_LOGW(ETH_TAG, "Ethernet link Down but no callback function is set");
ESP_LOGW(ETH_TAG, "Ethernet link Down but no callback function is set");
}
else
{
eth_link_state_cb(false);
}

ESP_LOGI(ETH_TAG, "Ethernet Link Down");
break;
case ETHERNET_EVENT_START:
Expand Down Expand Up @@ -84,7 +84,7 @@ static esp_err_t eth_recv_func(esp_eth_handle_t eth_handle, uint8_t *buffer, uin
}
else
{
eth_recv_cb(frame->dst_mac, frame->data, frame->data_len);
eth_recv_cb(frame->dst_mac, frame->data, frame->data_len, 'e');
}
free(buffer);
return ESP_OK;
Expand Down Expand Up @@ -124,7 +124,7 @@ void eth_detach_recv_cb()
eth_recv_cb = NULL;
}

void eth_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len))
void eth_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi))
{
eth_recv_cb = cb;
}
Expand All @@ -136,7 +136,7 @@ void eth_detach_link_state_cb()

void eth_attach_link_state_cb(void (*cb)(bool link_state))
{
eth_link_state_cb = cb;
eth_link_state_cb = cb;
}

void eth_init()
Expand All @@ -158,4 +158,10 @@ void eth_init()
printf("Driver installed\n");
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
printf("Driver started\n");
}

void eth_deinit()
{
ESP_ERROR_CHECK(esp_eth_stop(eth_handle));
ESP_ERROR_CHECK(esp_eth_driver_uninstall(eth_handle));
}
10 changes: 6 additions & 4 deletions firmware/components/direct_ethernet/direct_ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#define ETHERTYPE 0xb588

typedef struct {
typedef struct
{
uint8_t dst_mac[6];
uint8_t src_mac[6];
uint16_t ethertype;
Expand All @@ -29,15 +30,16 @@ typedef struct {
extern uint8_t eth_src_mac[6];
extern uint8_t eth_dst_mac[6];

void (*eth_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len);
void (*eth_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi); // eth_or_wifi = 'e' when eth is used, 'w' when wifi is used

void (*eth_link_state_cb)(bool link_state);

void eth_init();
void eth_deinit();
esp_err_t eth_send_frame(eth_frame *p_frame);
void eth_init_frame(eth_frame *p_frame);
void eth_send_data(uint8_t* data, int len);
void eth_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len));
void eth_send_data(uint8_t *data, int len);
void eth_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi));
void eth_detach_recv_cb();
void eth_attach_link_state_cb(void (*eth_link_state_cb)(bool link_state));
void eth_detach_link_state_cb();
Expand Down
110 changes: 71 additions & 39 deletions firmware/components/direct_wifi/direct_wifi.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,93 @@
#include "direct_wifi.h"

static const char* WIFI_TAG = "Direct_Wifi";
static const char *WIFI_TAG = "Direct_Wifi";

void (*wifi_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi) = NULL; // eth_or_wifi = 'e' when eth is used, 'w' when wifi is used

esp_now_peer_info_t peer;
static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void wifi_send_data(uint8_t *data, int len)
{
esp_now_send(peer.peer_addr, data, len);
}

void wifi_send_data(uint8_t *data, int len) {
esp_now_send(peer.peer_addr, data, len);
static void wifi_recv_func(uint8_t src_mac[6], uint8_t *data, int len)
{
if (wifi_recv_cb == NULL)
{
ESP_LOGW(WIFI_TAG, "Wifi frame received but no callback function is set on received...");
}
else
{
wifi_recv_cb(src_mac, data, len, 'w');
}
}

static void wifi_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if(status != ESP_OK) {
if (status != ESP_OK)
{
//error_send++;
}
}

void wifi_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len)) {
ESP_ERROR_CHECK( esp_now_register_recv_cb(cb) );
void wifi_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi))
{
wifi_recv_cb = cb;
}

void wifi_detach_recv_cb() {
esp_now_unregister_recv_cb();
void wifi_detach_recv_cb()
{
wifi_recv_cb = NULL;
}

void wifi_init() {
/* Init NVS */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );

/* Init WiFi */
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
cfg.ampdu_tx_enable = 0;
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_country_t country = {.cc="JP", .schan=1, .nchan=14, .policy=WIFI_COUNTRY_POLICY_MANUAL};
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_set_country(&country) );
ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE) );
ESP_ERROR_CHECK( esp_wifi_internal_set_fix_rate(ESP_IF_WIFI_STA, true, CONFIG_WIFI_DATARATE) );
void wifi_init()
{
/* Init NVS */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

/* Init WiFi */
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
cfg.ampdu_tx_enable = 0;
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
wifi_country_t country = {.cc = "JP", .schan = 1, .nchan = 14, .policy = WIFI_COUNTRY_POLICY_MANUAL};
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_country(&country));
ESP_ERROR_CHECK(esp_wifi_set_channel(CONFIG_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE));
ESP_ERROR_CHECK(esp_wifi_internal_set_fix_rate(ESP_IF_WIFI_STA, true, CONFIG_WIFI_DATARATE));

/* Init ESPNOW */
ESP_ERROR_CHECK( esp_now_init() );

memset(&peer, 0, sizeof(esp_now_peer_info_t));
peer.channel = CONFIG_WIFI_CHANNEL;
peer.ifidx = ESP_IF_WIFI_STA;
peer.encrypt = false;
memcpy(peer.peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
ESP_ERROR_CHECK( esp_now_add_peer(&peer) );
}
ESP_ERROR_CHECK(esp_now_init());
ESP_ERROR_CHECK(esp_now_register_recv_cb(wifi_recv_func));

memset(&peer, 0, sizeof(esp_now_peer_info_t));
peer.channel = CONFIG_WIFI_CHANNEL;
peer.ifidx = ESP_IF_WIFI_STA;
peer.encrypt = false;
memcpy(peer.peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
ESP_ERROR_CHECK(esp_now_add_peer(&peer));
}

void wifi_deinit_func()
{
ESP_ERROR_CHECK(esp_now_deinit());
ESP_ERROR_CHECK(esp_wifi_stop());
ESP_ERROR_CHECK(esp_wifi_deinit());
}

void wifi_change_channel(uint8_t wifi_channel)
{
ESP_ERROR_CHECK(esp_wifi_set_channel(wifi_channel, WIFI_SECOND_CHAN_NONE));
peer.channel = wifi_channel;
ESP_ERROR_CHECK(esp_now_mod_peer(&peer));
}
8 changes: 6 additions & 2 deletions firmware/components/direct_wifi/direct_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ esp_err_t esp_wifi_internal_set_fix_rate(wifi_interface_t ifx, bool en, wifi_phy
#error No valid WIFI datarate specified
#endif

void (*wifi_recv_cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi); // eth_or_wifi = 'e' when eth is used, 'w' when wifi is used

void wifi_init();
void wifi_deinit_func();
void wifi_send_data(uint8_t *data, int len);
void wifi_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len));
void wifi_attach_recv_cb(void (*cb)(uint8_t src_mac[6], uint8_t *data, int len, char eth_or_wifi));
void wifi_detach_recv_cb();

void wifi_change_channel(uint8_t wifi_channel);
static void wifi_recv_func(uint8_t src_mac[6], uint8_t *data, int len);

#endif
Loading

0 comments on commit 9fa1b5b

Please sign in to comment.