@@ -31,20 +31,13 @@ static const char *TAG = "eppp_link";
31
31
static int s_retry_num = 0 ;
32
32
static int s_eppp_netif_count = 0 ; // used as a suffix for the netif key
33
33
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
-
44
34
struct eppp_handle {
45
35
#if CONFIG_EPPP_LINK_DEVICE_SPI
46
36
QueueHandle_t out_queue ;
47
37
QueueHandle_t ready_semaphore ;
38
+ spi_device_handle_t spi_device ;
39
+ spi_host_device_t spi_host ;
40
+ int gpio_intr ;
48
41
#elif CONFIG_EPPP_LINK_DEVICE_UART
49
42
QueueHandle_t uart_event_queue ;
50
43
uart_port_t uart_port ;
@@ -101,7 +94,7 @@ static void netif_deinit(esp_netif_t *netif)
101
94
}
102
95
#if CONFIG_EPPP_LINK_DEVICE_SPI
103
96
vQueueDelete (h -> out_queue );
104
- if (role == EPPP_CLIENT ) {
97
+ if (h -> role == EPPP_CLIENT ) {
105
98
vSemaphoreDelete (h -> ready_semaphore );
106
99
}
107
100
#endif
@@ -299,26 +292,34 @@ static void IRAM_ATTR gpio_isr_handler(void *arg)
299
292
}
300
293
}
301
294
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 )
303
296
{
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 ;
304
305
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 ;
308
309
bus_cfg .quadwp_io_num = -1 ;
309
310
bus_cfg .quadhd_io_num = -1 ;
310
- bus_cfg .max_transfer_sz = 14000 ;
311
+ bus_cfg .max_transfer_sz = 2000 ;
311
312
bus_cfg .flags = 0 ;
312
313
bus_cfg .intr_flags = 0 ;
313
314
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 ) {
315
316
return ESP_FAIL ;
316
317
}
317
318
318
319
spi_device_interface_config_t dev_cfg = {};
319
- dev_cfg .clock_speed_hz = 40 * 1000 * 1000 ;
320
+ dev_cfg .clock_speed_hz = config -> freq ;
320
321
dev_cfg .mode = 0 ;
321
- dev_cfg .spics_io_num = GPIO_CS ;
322
+ dev_cfg .spics_io_num = config -> cs ;
322
323
dev_cfg .cs_ena_pretrans = 0 ;
323
324
dev_cfg .cs_ena_posttrans = 0 ;
324
325
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)
328
329
dev_cfg .cs_ena_posttrans = 3 ;
329
330
dev_cfg .queue_size = 3 ;
330
331
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 ) {
332
333
return ESP_FAIL ;
333
334
}
334
335
@@ -337,32 +338,40 @@ static esp_err_t init_master(spi_device_handle_t *spi, esp_netif_t *netif)
337
338
.intr_type = GPIO_INTR_POSEDGE ,
338
339
.mode = GPIO_MODE_INPUT ,
339
340
.pull_up_en = 1 ,
340
- .pin_bit_mask = BIT64 (GPIO_INTR ),
341
+ .pin_bit_mask = BIT64 (config -> intr ),
341
342
};
342
343
343
344
gpio_config (& io_conf );
344
345
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 ));
347
348
return ESP_OK ;
348
349
}
349
350
350
351
static void post_setup (spi_slave_transaction_t * trans )
351
352
{
352
- gpio_set_level (GPIO_INTR , 1 );
353
+ gpio_set_level (( int ) trans -> user , 1 );
353
354
}
354
355
355
356
static void post_trans (spi_slave_transaction_t * trans )
356
357
{
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 ;
358
364
}
359
365
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 )
361
367
{
368
+ struct eppp_handle * h = esp_netif_get_io_driver (netif );
369
+ h -> spi_host = config -> host ;
370
+ h -> gpio_intr = config -> intr ;
362
371
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 ;
366
375
bus_cfg .quadwp_io_num = -1 ;
367
376
bus_cfg .quadhd_io_num = -1 ;
368
377
bus_cfg .flags = 0 ;
@@ -371,27 +380,27 @@ static esp_err_t init_slave(spi_device_handle_t *spi, esp_netif_t *netif)
371
380
//Configuration for the SPI slave interface
372
381
spi_slave_interface_config_t slvcfg = {
373
382
.mode = 0 ,
374
- .spics_io_num = GPIO_CS ,
383
+ .spics_io_num = config -> cs ,
375
384
.queue_size = 3 ,
376
385
.flags = 0 ,
377
386
.post_setup_cb = post_setup ,
378
- .post_trans_cb = post_trans
387
+ .post_trans_cb = post_trans ,
379
388
};
380
389
381
390
//Configuration for the handshake line
382
391
gpio_config_t io_conf = {
383
392
.intr_type = GPIO_INTR_DISABLE ,
384
393
.mode = GPIO_MODE_OUTPUT ,
385
- .pin_bit_mask = BIT64 (GPIO_INTR ),
394
+ .pin_bit_mask = BIT64 (config -> intr ),
386
395
};
387
396
388
397
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 );
392
401
393
402
//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 ) {
395
404
return ESP_FAIL ;
396
405
}
397
406
return ESP_OK ;
@@ -402,18 +411,19 @@ union transaction {
402
411
spi_slave_transaction_t slave ;
403
412
};
404
413
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 );
406
415
typedef esp_err_t (* perform_transaction_t )(union transaction * t , struct eppp_handle * h );
407
416
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 )
409
418
{
410
419
t -> master .length = len * 8 ;
411
420
t -> master .tx_buffer = tx_buffer ;
412
421
t -> master .rx_buffer = rx_buffer ;
413
422
}
414
423
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 )
416
425
{
426
+ t -> slave .user = (void * )gpio_intr ;
417
427
t -> slave .length = len * 8 ;
418
428
t -> slave .tx_buffer = tx_buffer ;
419
429
t -> slave .rx_buffer = rx_buffer ;
@@ -422,12 +432,12 @@ static void set_transaction_slave(union transaction *t, size_t len, const void *
422
432
static esp_err_t perform_transaction_master (union transaction * t , struct eppp_handle * h )
423
433
{
424
434
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 );
426
436
}
427
437
428
438
static esp_err_t perform_transaction_slave (union transaction * t , struct eppp_handle * h )
429
439
{
430
- return spi_slave_transmit (EPPP_SPI_HOST , & t -> slave , portMAX_DELAY );
440
+ return spi_slave_transmit (h -> spi_host , & t -> slave , portMAX_DELAY );
431
441
}
432
442
433
443
_Noreturn static void ppp_task (void * args )
@@ -477,7 +487,7 @@ _Noreturn static void ppp_task(void *args)
477
487
}
478
488
}
479
489
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 );
481
491
for (int i = 0 ; i < sizeof (struct header ) - 1 ; ++ i ) {
482
492
head -> checksum += out_buf [i ];
483
493
}
@@ -527,7 +537,7 @@ _Noreturn static void ppp_task(void *args)
527
537
}
528
538
529
539
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 );
531
541
532
542
ret = perform_transaction (& t , h );
533
543
if (ret != ESP_OK ) {
@@ -637,10 +647,11 @@ static void remove_handlers(void)
637
647
void eppp_deinit (esp_netif_t * netif )
638
648
{
639
649
#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);
642
653
} else {
643
- deinit_slave (& s_spi_device , netif );
654
+ // deinit_slave(&s_spi_device, netif);
644
655
}
645
656
#elif CONFIG_EPPP_LINK_DEVICE_UART
646
657
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)
658
669
}
659
670
esp_netif_ppp_config_t netif_params ;
660
671
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 ;
663
674
netif_params .ppp_error_event_enabled = true;
664
675
ESP_ERROR_CHECK (esp_netif_ppp_set_params (netif , & netif_params ));
665
676
#if CONFIG_EPPP_LINK_DEVICE_SPI
666
677
if (role == EPPP_CLIENT ) {
667
- init_master (& s_spi_device , netif );
678
+ init_master (& config -> spi , netif );
668
679
} else {
669
- init_slave (& s_spi_device , netif );
680
+ init_slave (& config -> spi , netif );
670
681
}
671
682
#elif CONFIG_EPPP_LINK_DEVICE_UART
672
683
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)
676
687
677
688
esp_netif_t * eppp_open (enum eppp_type role , eppp_config_t * config , TickType_t connect_timeout )
678
689
{
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
+
679
703
if (config -> task .run_task == false) {
680
704
ESP_LOGE (TAG , "task.run_task == false is invalid in this API. Please use eppp_init()" );
681
705
return NULL ;
0 commit comments