Skip to content

Commit 796fe19

Browse files
committed
fix(eppp_link): Fixes to SPI transport
1 parent 5d7b715 commit 796fe19

File tree

5 files changed

+128
-69
lines changed

5 files changed

+128
-69
lines changed

components/eppp_link/Kconfig

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,4 @@ menu "eppp_link"
3232
Size of the Tx packet queue.
3333
You can decrease the number for slower bit rates.
3434

35-
config EPPP_LINK_SERVER_IP
36-
hex "Server IP address"
37-
range 0 0xFFFFFFFF
38-
default 0xc0a80b01
39-
help
40-
Preferred IP address of the server side.
41-
42-
config EPPP_LINK_CLIENT_IP
43-
hex "Client IP address"
44-
range 0 0xFFFFFFFF
45-
default 0xc0a80b02
46-
help
47-
Preferred IP address of the client side.
48-
49-
5035
endmenu

components/eppp_link/eppp_link.c

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@ static const char *TAG = "eppp_link";
3131
static int s_retry_num = 0;
3232
static int s_eppp_netif_count = 0; // used as a suffix for the netif key
3333

34-
#if CONFIG_EPPP_LINK_DEVICE_SPI
35-
static spi_device_handle_t s_spi_device;
36-
#define EPPP_SPI_HOST SPI2_HOST
37-
#define GPIO_MOSI 11
38-
#define GPIO_MISO 13
39-
#define GPIO_SCLK 12
40-
#define GPIO_CS 10
41-
#define GPIO_INTR 2
42-
#endif // CONFIG_EPPP_LINK_DEVICE_SPI
43-
4434
struct eppp_handle {
4535
#if CONFIG_EPPP_LINK_DEVICE_SPI
4636
QueueHandle_t out_queue;
4737
QueueHandle_t ready_semaphore;
38+
spi_device_handle_t spi_device;
39+
spi_host_device_t spi_host;
40+
int gpio_intr;
4841
#elif CONFIG_EPPP_LINK_DEVICE_UART
4942
QueueHandle_t uart_event_queue;
5043
uart_port_t uart_port;
@@ -101,7 +94,7 @@ static void netif_deinit(esp_netif_t *netif)
10194
}
10295
#if CONFIG_EPPP_LINK_DEVICE_SPI
10396
vQueueDelete(h->out_queue);
104-
if (role == EPPP_CLIENT) {
97+
if (h->role == EPPP_CLIENT) {
10598
vSemaphoreDelete(h->ready_semaphore);
10699
}
107100
#endif
@@ -299,26 +292,34 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
299292
}
300293
}
301294

302-
static esp_err_t init_master(spi_device_handle_t *spi, esp_netif_t *netif)
295+
static esp_err_t deinit_master(esp_netif_t *netif)
303296
{
297+
return ESP_OK;
298+
}
299+
300+
static esp_err_t init_master(struct eppp_config_spi_s *config, esp_netif_t *netif)
301+
{
302+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
303+
h->spi_host = config->host;
304+
h->gpio_intr = config->intr;
304305
spi_bus_config_t bus_cfg = {};
305-
bus_cfg.mosi_io_num = GPIO_MOSI;
306-
bus_cfg.miso_io_num = GPIO_MISO;
307-
bus_cfg.sclk_io_num = GPIO_SCLK;
306+
bus_cfg.mosi_io_num = config->mosi;
307+
bus_cfg.miso_io_num = config->miso;
308+
bus_cfg.sclk_io_num = config->sclk;
308309
bus_cfg.quadwp_io_num = -1;
309310
bus_cfg.quadhd_io_num = -1;
310-
bus_cfg.max_transfer_sz = 14000;
311+
bus_cfg.max_transfer_sz = 2000;
311312
bus_cfg.flags = 0;
312313
bus_cfg.intr_flags = 0;
313314

314-
if (spi_bus_initialize(EPPP_SPI_HOST, &bus_cfg, SPI_DMA_CH_AUTO) != ESP_OK) {
315+
if (spi_bus_initialize(config->host, &bus_cfg, SPI_DMA_CH_AUTO) != ESP_OK) {
315316
return ESP_FAIL;
316317
}
317318

318319
spi_device_interface_config_t dev_cfg = {};
319-
dev_cfg.clock_speed_hz = 40 * 1000 * 1000;
320+
dev_cfg.clock_speed_hz = config->freq;
320321
dev_cfg.mode = 0;
321-
dev_cfg.spics_io_num = GPIO_CS;
322+
dev_cfg.spics_io_num = config->cs;
322323
dev_cfg.cs_ena_pretrans = 0;
323324
dev_cfg.cs_ena_posttrans = 0;
324325
dev_cfg.duty_cycle_pos = 128;
@@ -328,7 +329,7 @@ static esp_err_t init_master(spi_device_handle_t *spi, esp_netif_t *netif)
328329
dev_cfg.cs_ena_posttrans = 3;
329330
dev_cfg.queue_size = 3;
330331

331-
if (spi_bus_add_device(EPPP_SPI_HOST, &dev_cfg, spi) != ESP_OK) {
332+
if (spi_bus_add_device(config->host, &dev_cfg, &h->spi_device) != ESP_OK) {
332333
return ESP_FAIL;
333334
}
334335

@@ -337,32 +338,40 @@ static esp_err_t init_master(spi_device_handle_t *spi, esp_netif_t *netif)
337338
.intr_type = GPIO_INTR_POSEDGE,
338339
.mode = GPIO_MODE_INPUT,
339340
.pull_up_en = 1,
340-
.pin_bit_mask = BIT64(GPIO_INTR),
341+
.pin_bit_mask = BIT64(config->intr),
341342
};
342343

343344
gpio_config(&io_conf);
344345
gpio_install_isr_service(0);
345-
gpio_set_intr_type(GPIO_INTR, GPIO_INTR_POSEDGE);
346-
gpio_isr_handler_add(GPIO_INTR, gpio_isr_handler, esp_netif_get_io_driver(netif));
346+
gpio_set_intr_type(config->intr, GPIO_INTR_POSEDGE);
347+
gpio_isr_handler_add(config->intr, gpio_isr_handler, esp_netif_get_io_driver(netif));
347348
return ESP_OK;
348349
}
349350

350351
static void post_setup(spi_slave_transaction_t *trans)
351352
{
352-
gpio_set_level(GPIO_INTR, 1);
353+
gpio_set_level((int)trans->user, 1);
353354
}
354355

355356
static void post_trans(spi_slave_transaction_t *trans)
356357
{
357-
gpio_set_level(GPIO_INTR, 0);
358+
gpio_set_level((int)trans->user, 0);
359+
}
360+
361+
static esp_err_t deinit_slave(spi_device_handle_t *spi, esp_netif_t *netif)
362+
{
363+
return ESP_OK;
358364
}
359365

360-
static esp_err_t init_slave(spi_device_handle_t *spi, esp_netif_t *netif)
366+
static esp_err_t init_slave(struct eppp_config_spi_s *config, esp_netif_t *netif)
361367
{
368+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
369+
h->spi_host = config->host;
370+
h->gpio_intr = config->intr;
362371
spi_bus_config_t bus_cfg = {};
363-
bus_cfg.mosi_io_num = GPIO_MOSI;
364-
bus_cfg.miso_io_num = GPIO_MISO;
365-
bus_cfg.sclk_io_num = GPIO_SCLK;
372+
bus_cfg.mosi_io_num = config->mosi;
373+
bus_cfg.miso_io_num = config->miso;
374+
bus_cfg.sclk_io_num = config->sclk;
366375
bus_cfg.quadwp_io_num = -1;
367376
bus_cfg.quadhd_io_num = -1;
368377
bus_cfg.flags = 0;
@@ -371,27 +380,27 @@ static esp_err_t init_slave(spi_device_handle_t *spi, esp_netif_t *netif)
371380
//Configuration for the SPI slave interface
372381
spi_slave_interface_config_t slvcfg = {
373382
.mode = 0,
374-
.spics_io_num = GPIO_CS,
383+
.spics_io_num = config->cs,
375384
.queue_size = 3,
376385
.flags = 0,
377386
.post_setup_cb = post_setup,
378-
.post_trans_cb = post_trans
387+
.post_trans_cb = post_trans,
379388
};
380389

381390
//Configuration for the handshake line
382391
gpio_config_t io_conf = {
383392
.intr_type = GPIO_INTR_DISABLE,
384393
.mode = GPIO_MODE_OUTPUT,
385-
.pin_bit_mask = BIT64(GPIO_INTR),
394+
.pin_bit_mask = BIT64(config->intr),
386395
};
387396

388397
gpio_config(&io_conf);
389-
gpio_set_pull_mode(GPIO_MOSI, GPIO_PULLUP_ONLY);
390-
gpio_set_pull_mode(GPIO_SCLK, GPIO_PULLUP_ONLY);
391-
gpio_set_pull_mode(GPIO_CS, GPIO_PULLUP_ONLY);
398+
gpio_set_pull_mode(config->mosi, GPIO_PULLUP_ONLY);
399+
gpio_set_pull_mode(config->sclk, GPIO_PULLUP_ONLY);
400+
gpio_set_pull_mode(config->cs, GPIO_PULLUP_ONLY);
392401

393402
//Initialize SPI slave interface
394-
if (spi_slave_initialize(EPPP_SPI_HOST, &bus_cfg, &slvcfg, SPI_DMA_CH_AUTO) != ESP_OK) {
403+
if (spi_slave_initialize(config->host, &bus_cfg, &slvcfg, SPI_DMA_CH_AUTO) != ESP_OK) {
395404
return ESP_FAIL;
396405
}
397406
return ESP_OK;
@@ -402,18 +411,19 @@ union transaction {
402411
spi_slave_transaction_t slave;
403412
};
404413

405-
typedef void (*set_transaction_t)(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer);
414+
typedef void (*set_transaction_t)(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer, int gpio_intr);
406415
typedef esp_err_t (*perform_transaction_t)(union transaction *t, struct eppp_handle *h);
407416

408-
static void set_transaction_master(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer)
417+
static void set_transaction_master(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer, int gpio_intr)
409418
{
410419
t->master.length = len * 8;
411420
t->master.tx_buffer = tx_buffer;
412421
t->master.rx_buffer = rx_buffer;
413422
}
414423

415-
static void set_transaction_slave(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer)
424+
static void set_transaction_slave(union transaction *t, size_t len, const void *tx_buffer, void *rx_buffer, int gpio_intr)
416425
{
426+
t->slave.user = (void *)gpio_intr;
417427
t->slave.length = len * 8;
418428
t->slave.tx_buffer = tx_buffer;
419429
t->slave.rx_buffer = rx_buffer;
@@ -422,12 +432,12 @@ static void set_transaction_slave(union transaction *t, size_t len, const void *
422432
static esp_err_t perform_transaction_master(union transaction *t, struct eppp_handle *h)
423433
{
424434
xSemaphoreTake(h->ready_semaphore, portMAX_DELAY); // Wait until slave is ready
425-
return spi_device_transmit(s_spi_device, &t->master);
435+
return spi_device_transmit(h->spi_device, &t->master);
426436
}
427437

428438
static esp_err_t perform_transaction_slave(union transaction *t, struct eppp_handle *h)
429439
{
430-
return spi_slave_transmit(EPPP_SPI_HOST, &t->slave, portMAX_DELAY);
440+
return spi_slave_transmit(h->spi_host, &t->slave, portMAX_DELAY);
431441
}
432442

433443
_Noreturn static void ppp_task(void *args)
@@ -477,7 +487,7 @@ _Noreturn static void ppp_task(void *args)
477487
}
478488
}
479489
memset(&t, 0, sizeof(t));
480-
set_transaction(&t, CONTROL_SIZE, out_buf, in_buf);
490+
set_transaction(&t, CONTROL_SIZE, out_buf, in_buf, h->gpio_intr);
481491
for (int i = 0; i < sizeof(struct header) - 1; ++i) {
482492
head->checksum += out_buf[i];
483493
}
@@ -527,7 +537,7 @@ _Noreturn static void ppp_task(void *args)
527537
}
528538

529539
memset(&t, 0, sizeof(t));
530-
set_transaction(&t, MAX(in_long_payload, out_long_payload) + sizeof(struct header), out_buf, in_buf);
540+
set_transaction(&t, MAX(in_long_payload, out_long_payload) + sizeof(struct header), out_buf, in_buf, h->gpio_intr);
531541

532542
ret = perform_transaction(&t, h);
533543
if (ret != ESP_OK) {
@@ -637,10 +647,11 @@ static void remove_handlers(void)
637647
void eppp_deinit(esp_netif_t *netif)
638648
{
639649
#if CONFIG_EPPP_LINK_DEVICE_SPI
640-
if (role == EPPP_CLIENT) {
641-
deinit_master(&s_spi_device, netif);
650+
struct eppp_handle *h = esp_netif_get_io_driver(netif);
651+
if (h->role == EPPP_CLIENT) {
652+
// deinit_master(&h->spi_device, netif);
642653
} else {
643-
deinit_slave(&s_spi_device, netif);
654+
// deinit_slave(&s_spi_device, netif);
644655
}
645656
#elif CONFIG_EPPP_LINK_DEVICE_UART
646657
deinit_uart(esp_netif_get_io_driver(netif));
@@ -658,15 +669,15 @@ esp_netif_t *eppp_init(enum eppp_type role, eppp_config_t *config)
658669
}
659670
esp_netif_ppp_config_t netif_params;
660671
ESP_ERROR_CHECK(esp_netif_ppp_get_params(netif, &netif_params));
661-
netif_params.ppp_our_ip4_addr = esp_netif_htonl(role == EPPP_SERVER ? CONFIG_EPPP_LINK_SERVER_IP : CONFIG_EPPP_LINK_CLIENT_IP);
662-
netif_params.ppp_their_ip4_addr = esp_netif_htonl(role == EPPP_SERVER ? CONFIG_EPPP_LINK_CLIENT_IP : CONFIG_EPPP_LINK_SERVER_IP);
672+
netif_params.ppp_our_ip4_addr = config->ppp.our_ip4_addr;
673+
netif_params.ppp_their_ip4_addr = config->ppp.their_ip4_addr;
663674
netif_params.ppp_error_event_enabled = true;
664675
ESP_ERROR_CHECK(esp_netif_ppp_set_params(netif, &netif_params));
665676
#if CONFIG_EPPP_LINK_DEVICE_SPI
666677
if (role == EPPP_CLIENT) {
667-
init_master(&s_spi_device, netif);
678+
init_master(&config->spi, netif);
668679
} else {
669-
init_slave(&s_spi_device, netif);
680+
init_slave(&config->spi, netif);
670681
}
671682
#elif CONFIG_EPPP_LINK_DEVICE_UART
672683
init_uart(esp_netif_get_io_driver(netif), config);
@@ -676,6 +687,19 @@ esp_netif_t *eppp_init(enum eppp_type role, eppp_config_t *config)
676687

677688
esp_netif_t *eppp_open(enum eppp_type role, eppp_config_t *config, TickType_t connect_timeout)
678689
{
690+
#if CONFIG_EPPP_LINK_DEVICE_UART
691+
if (config->transport != EPPP_TRANSPORT_UART) {
692+
ESP_LOGE(TAG, "Invalid transport: UART device must be enabled in Kconfig");
693+
return NULL;
694+
}
695+
#endif
696+
#if CONFIG_EPPP_LINK_DEVICE_SPI
697+
if (config->transport != EPPP_TRANSPORT_SPI) {
698+
ESP_LOGE(TAG, "Invalid transport: SPI device must be enabled in Kconfig");
699+
return NULL;
700+
}
701+
#endif
702+
679703
if (config->task.run_task == false) {
680704
ESP_LOGE(TAG, "task.run_task == false is invalid in this API. Please use eppp_init()");
681705
return NULL;

components/eppp_link/examples/host/main/app_main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -18,6 +18,9 @@
1818
#include "mqtt_client.h"
1919
#include "ping/ping_sock.h"
2020
#include "esp_console.h"
21+
#include "driver/uart.h"
22+
#include "driver/spi_master.h"
23+
#include "driver/spi_slave.h"
2124

2225
void register_iperf(void);
2326

@@ -140,7 +143,15 @@ void app_main(void)
140143

141144
/* Sets up the default EPPP-connection
142145
*/
143-
esp_netif_t *eppp_netif = eppp_connect();
146+
eppp_config_t config = EPPP_DEFAULT_CLIENT_CONFIG();
147+
#if CONFIG_EPPP_LINK_DEVICE_SPI
148+
config.transport = EPPP_TRANSPORT_SPI;
149+
#else
150+
config.transport = EPPP_TRANSPORT_UART;
151+
config.uart.tx_io = 10;
152+
config.uart.rx_io = 11;
153+
#endif
154+
esp_netif_t *eppp_netif = eppp_connect(&config);
144155
if (eppp_netif == NULL) {
145156
ESP_LOGE(TAG, "Failed to connect");
146157
return ;

components/eppp_link/examples/slave/main/station_example_main.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -14,6 +14,9 @@
1414
#include "esp_log.h"
1515
#include "nvs_flash.h"
1616
#include "eppp_link.h"
17+
#include "driver/uart.h"
18+
#include "driver/spi_master.h"
19+
#include "driver/spi_slave.h"
1720

1821
#include "lwip/err.h"
1922
#include "lwip/sys.h"
@@ -123,7 +126,15 @@ void app_main(void)
123126
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
124127
wifi_init_sta();
125128

126-
esp_netif_t *eppp_netif = eppp_listen();
129+
eppp_config_t config = EPPP_DEFAULT_SERVER_CONFIG();
130+
#if CONFIG_EPPP_LINK_DEVICE_SPI
131+
config.transport = EPPP_TRANSPORT_SPI;
132+
#else
133+
config.transport = EPPP_TRANSPORT_UART;
134+
config.uart.tx_io = 11;
135+
config.uart.rx_io = 10;
136+
#endif
137+
esp_netif_t *eppp_netif = eppp_listen(&config);
127138
if (eppp_netif == NULL) {
128139
ESP_LOGE(TAG, "Failed to setup connection");
129140
return ;

0 commit comments

Comments
 (0)