6
6
#include <string.h>
7
7
#include <stdint.h>
8
8
#include <inttypes.h>
9
+ #include "sdkconfig.h"
9
10
#include "esp_log.h"
10
11
#include "esp_netif.h"
11
12
#include "esp_check.h"
24
25
25
26
#define MAX_PAYLOAD 1500
26
27
#define MIN_TRIGGER_US 20
27
- #define SPI_HEADER_MAGIC 0x1234
28
+ #define PPP_SOF 0x7E
29
+ #define SPI_HEADER_MAGIC PPP_SOF
28
30
#define SPI_ALIGN (size ) (((size) + 3U) & ~(3U))
29
31
#define TRANSFER_SIZE SPI_ALIGN((MAX_PAYLOAD + 6))
30
32
#define NEXT_TRANSACTION_SIZE (a ,b ) (((a)>(b))?(a):(b)) /* next transaction: whichever is bigger */
31
33
32
34
struct packet {
33
35
size_t len ;
34
36
uint8_t * data ;
37
+ int channel ;
35
38
};
36
39
37
40
struct header {
38
- uint16_t magic ;
41
+ uint8_t magic ;
42
+ uint8_t channel ;
39
43
uint16_t size ;
40
44
uint16_t next_size ;
41
45
uint16_t check ;
@@ -65,12 +69,10 @@ struct eppp_spi {
65
69
esp_timer_handle_t timer ;
66
70
};
67
71
68
- static esp_err_t transmit ( void * h , void * buffer , size_t len )
72
+ static esp_err_t transmit_generic ( struct eppp_spi * handle , int channel , void * buffer , size_t len )
69
73
{
70
- struct eppp_handle * common = h ;
71
- struct eppp_spi * handle = __containerof (common , struct eppp_spi , parent );;
72
- #if CONFIG_EPPP_LINK_DEVICE_SPI
73
- struct packet buf = { };
74
+
75
+ struct packet buf = { .channel = channel };
74
76
uint8_t * current_buffer = buffer ;
75
77
size_t remaining = len ;
76
78
do { // TODO(IDF-9194): Refactor this loop to allocate only once and perform
@@ -100,14 +102,24 @@ static esp_err_t transmit(void *h, void *buffer, size_t len)
100
102
}
101
103
gpio_set_level (handle -> gpio_intr , 0 );
102
104
}
103
-
104
- #elif CONFIG_EPPP_LINK_DEVICE_UART
105
- ESP_LOG_BUFFER_HEXDUMP ("ppp_uart_send" , buffer , len , ESP_LOG_WARN );
106
- uart_write_bytes (handle -> uart_port , buffer , len );
107
- #endif // DEVICE UART or SPI
108
105
return ESP_OK ;
109
106
}
110
107
108
+ static esp_err_t transmit (void * h , void * buffer , size_t len )
109
+ {
110
+ struct eppp_handle * handle = h ;
111
+ struct eppp_spi * spi_handle = __containerof (handle , struct eppp_spi , parent );;
112
+ return transmit_generic (spi_handle , 0 , buffer , len );
113
+ }
114
+
115
+ static esp_err_t transmit_channel (esp_netif_t * netif , int channel , void * buffer , size_t len )
116
+ {
117
+ // printf("transmit_channel %d %p %zu\n", channel, buffer, len);
118
+ struct eppp_handle * handle = esp_netif_get_io_driver (netif );
119
+ struct eppp_spi * spi_handle = __containerof (handle , struct eppp_spi , parent );;
120
+ return transmit_generic (spi_handle , channel , buffer , len );
121
+ }
122
+
111
123
static void IRAM_ATTR timer_callback (void * arg )
112
124
{
113
125
struct eppp_spi * h = arg ;
@@ -339,6 +351,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
339
351
if (h -> outbound .len <= h -> transaction_size && allow_test_tx == false) {
340
352
// sending outbound
341
353
head -> size = h -> outbound .len ;
354
+ head -> channel = h -> outbound .channel ;
342
355
if (h -> outbound .len > 0 ) {
343
356
memcpy (out_buf + sizeof (struct header ), h -> outbound .data , h -> outbound .len );
344
357
free (h -> outbound .data );
@@ -355,6 +368,7 @@ esp_err_t eppp_perform(esp_netif_t *netif)
355
368
} else {
356
369
// outbound is bigger, need to transmit in another transaction (keep this empty)
357
370
head -> size = 0 ;
371
+ head -> channel = 0 ;
358
372
}
359
373
next_tx_size = head -> next_size = h -> outbound .len ;
360
374
head -> magic = SPI_HEADER_MAGIC ;
@@ -367,17 +381,25 @@ esp_err_t eppp_perform(esp_netif_t *netif)
367
381
}
368
382
head = (void * )in_buf ;
369
383
uint16_t check = esp_rom_crc16_le (0 , in_buf , sizeof (struct header ) - sizeof (uint16_t ));
370
- if (check != head -> check || head -> magic != SPI_HEADER_MAGIC ) {
384
+ if (check != head -> check || head -> magic != SPI_HEADER_MAGIC || head -> channel >= NR_OF_CHANNELS ) {
371
385
h -> transaction_size = 0 ; // need to start with HEADER only transaction
372
386
if (allow_test_tx ) {
373
387
return ESP_OK ;
374
388
}
375
- ESP_LOGE (TAG , "Wrong checksum or magic" );
389
+ ESP_LOGE (TAG , "Wrong checksum, magic, or channel: %x %x %x" , check , head -> magic , head -> channel );
376
390
return ESP_FAIL ;
377
391
}
378
392
if (head -> size > 0 ) {
379
393
ESP_LOG_BUFFER_HEXDUMP (TAG , in_buf + sizeof (struct header ), head -> size , ESP_LOG_VERBOSE );
380
- esp_netif_receive (netif , in_buf + sizeof (struct header ), head -> size , NULL );
394
+ if (head -> channel == 0 ) {
395
+ esp_netif_receive (netif , in_buf + sizeof (struct header ), head -> size , NULL );
396
+ } else {
397
+ // printf("channel receive %d\n", head->channel);
398
+ // printf("channel_rx %p\n", h->parent.channel_rx);
399
+ if (h -> parent .channel_rx ) {
400
+ h -> parent .channel_rx (netif , head -> channel , in_buf + sizeof (struct header ), head -> size );
401
+ }
402
+ }
381
403
}
382
404
h -> transaction_size = NEXT_TRANSACTION_SIZE (next_tx_size , head -> next_size );
383
405
return ESP_OK ;
@@ -413,6 +435,7 @@ eppp_transport_handle_t eppp_spi_init(struct eppp_config_spi_s *config)
413
435
__attribute__((unused )) esp_err_t ret = ESP_OK ;
414
436
struct eppp_spi * h = calloc (1 , sizeof (struct eppp_spi ));
415
437
ESP_RETURN_ON_FALSE (h , NULL , TAG , "Failed to allocate eppp_handle" );
438
+ h -> parent .channel_tx = transmit_channel ;
416
439
h -> is_master = config -> is_master ;
417
440
h -> parent .base .post_attach = post_attach ;
418
441
h -> out_queue = xQueueCreate (CONFIG_EPPP_LINK_PACKET_QUEUE_SIZE , sizeof (struct packet ));
0 commit comments