30
30
31
31
#include "uart.h"
32
32
#include "driver/uart.h"
33
- #include "soc/uart_periph.h"
33
+ // #include "soc/uart_periph.h"
34
+ #include "driver/uart_select.h"
34
35
35
36
#include "py/runtime.h"
36
37
#include "py/mphal.h"
37
38
38
- static QueueHandle_t uart0_queue ;
39
+ // static QueueHandle_t uart0_queue;
39
40
40
- static void uart_event_task (void * pvParameters );
41
+ // static void uart_event_task(void *pvParameters);
41
42
43
+ static char int_pattern = 0 ;
44
+
45
+ static void select_notif_callback_isr (uart_port_t uart_num , uart_select_notif_t uart_select_notif , BaseType_t * task_woken )
46
+ {
47
+ int ret ;
48
+ uint8_t c = 0 ;
49
+ switch (uart_select_notif ) {
50
+ case UART_SELECT_READ_NOTIF :
51
+ while (true) {
52
+ ret = uart_read_bytes (UART_NUM_0 , & c , 1 , 0 );
53
+ if (ret <= 0 ) {
54
+ break ;
55
+ }
56
+ if (c == int_pattern ) {
57
+ mp_sched_keyboard_interrupt ();
58
+ } else {
59
+ ringbuf_put (& stdin_ringbuf , c );
60
+ }
61
+ }
62
+ break ;
63
+ default :
64
+ break ;
65
+ }
66
+ }
42
67
43
68
void uart_init (void ) {
44
69
uart_config_t uartcfg = {
@@ -54,104 +79,112 @@ void uart_init(void) {
54
79
const uint32_t rxbuf = 260 ;
55
80
const uint32_t txbuf = 260 ;
56
81
57
- uart_driver_install (UART_NUM_0 , rxbuf , txbuf , 20 , & uart0_queue , 0 );
82
+ // uart_driver_install(UART_NUM_0, rxbuf, txbuf, 20, &uart0_queue, 0);
83
+ uart_driver_install (UART_NUM_0 , rxbuf , txbuf , 0 , NULL , 0 );
58
84
59
85
uart_repl_set_pattern_detect (mp_interrupt_char );
60
86
61
- //Create a task to handler UART event from ISR
62
- xTaskCreate ( uart_event_task , "uart_event_task" , 2048 , NULL , 12 , NULL );
87
+ //uart_set_select_notif_callback sets the callbacks in UART ISR
88
+ uart_set_select_notif_callback ( UART_NUM_0 , select_notif_callback_isr );
63
89
90
+ //Create a task to handler UART event from ISR
91
+ // xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
64
92
}
65
93
66
94
void uart_repl_set_pattern_detect (const char pattern ) {
67
95
//Set uart pattern detect function.
68
- uart_enable_pattern_det_baud_intr (UART_NUM_0 , pattern , 1 , 9 , 0 , 0 );
96
+ //#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0)
97
+ //uart_enable_pattern_det_baud_intr(UART_NUM_0, pattern, 1, 9, 0, 0);
98
+ //#else
99
+ //uart_enable_pattern_det_intr(UART_NUM_0, pattern, 1, 9, 0, 0);
100
+ //#endif
69
101
//Reset the pattern queue length to record at most 20 pattern positions.
70
- uart_pattern_queue_reset (UART_NUM_0 , 20 );
102
+ // uart_pattern_queue_reset(UART_NUM_0, 20);
103
+ int_pattern = pattern ;
71
104
}
72
105
73
- void uart_event_task (void * pvParameters )
74
- {
75
- uart_event_t event ;
76
- size_t buffered_size ;
77
- // uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
78
- for (;;) {
79
- //Waiting for UART event.
80
- if (xQueueReceive (uart0_queue , (void * )& event , (portTickType )portMAX_DELAY )) {
81
- // bzero(dtmp, RD_BUF_SIZE);
82
- // ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_0);
83
- switch (event .type ) {
84
- //Event of UART receving data
85
- /*We'd better handler data event fast, there would be much more data events than
86
- other types of events. If we take too much time on data event, the queue might
87
- be full.*/
88
- // case UART_DATA:
89
- // ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
90
- // uart_read_bytes(UART_NUM_0, dtmp, event.size, portMAX_DELAY);
91
- // ESP_LOGI(TAG, "[DATA EVT]:");
92
- // uart_write_bytes(UART_NUM_0, (const char*) dtmp, event.size);
93
- // break;
94
- //Event of HW FIFO overflow detected
95
- // case UART_FIFO_OVF:
96
- // ESP_LOGI(TAG, "hw fifo overflow");
97
- // // If fifo overflow happened, you should consider adding flow control for your application.
98
- // // The ISR has already reset the rx FIFO,
99
- // // As an example, we directly flush the rx buffer here in order to read more data.
100
- // uart_flush_input(UART_NUM_0);
101
- // xQueueReset(uart0_queue);
102
- // break;
103
- //Event of UART ring buffer full
104
- // case UART_BUFFER_FULL:
105
- // ESP_LOGI(TAG, "ring buffer full");
106
- // // If buffer full happened, you should consider encreasing your buffer size
107
- // // As an example, we directly flush the rx buffer here in order to read more data.
108
- // uart_flush_input(UART_NUM_0);
109
- // xQueueReset(uart0_queue);
110
- // break;
111
- //Event of UART RX break detected
112
- // case UART_BREAK:
113
- // ESP_LOGI(TAG, "uart rx break");
114
- // break;
115
- //Event of UART parity check error
116
- // case UART_PARITY_ERR:
117
- // ESP_LOGI(TAG, "uart parity error");
118
- // break;
119
- //Event of UART frame error
120
- // case UART_FRAME_ERR:
121
- // ESP_LOGI(TAG, "uart frame error");
122
- // break;
123
- //UART_PATTERN_DET
124
- case UART_PATTERN_DET :
125
- uart_get_buffered_data_len (UART_NUM_0 , & buffered_size );
126
- int pos = uart_pattern_pop_pos (UART_NUM_0 );
127
- // ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, buffered_size);
128
- if (pos == -1 ) {
129
- // There used to be a UART_PATTERN_DET event, but the pattern position queue is full so that it can not
130
- // record the position. We should set a larger queue size.
131
- // As an example, we directly flush the rx buffer here.
132
- // uart_flush_input(UART_NUM_0);
133
- } else {
134
- uint8_t c = 0 ;
135
- for (int i = 0 ; i < pos ; i ++ ) {
136
- uart_read_bytes (UART_NUM_0 , & c , 1 , 100 / portTICK_PERIOD_MS );
137
- ringbuf_put (& stdin_ringbuf , c );
138
- }
139
- // uint8_t pat[PATTERN_CHR_NUM + 1];
140
- // memset(pat, 0, sizeof(pat));
141
- uart_read_bytes (UART_NUM_0 , & c , 1 , 100 / portTICK_PERIOD_MS );
142
- // ESP_LOGI(TAG, "read data: %s", dtmp);
143
- // ESP_LOGI(TAG, "read pat : %s", pat);
144
- }
145
- mp_sched_keyboard_interrupt ();
146
- break ;
147
- //Others
148
- default :
149
- // ESP_LOGI(TAG, "uart event type: %d", event.type);
150
- break ;
151
- }
152
- }
153
- }
154
- // free(dtmp);
155
- // dtmp = NULL;
156
- vTaskDelete (NULL );
157
- }
106
+ // void uart_event_task(void *pvParameters)
107
+ // {
108
+ // uart_event_t event;
109
+ // size_t buffered_size;
110
+ // // uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
111
+ // for(;;) {
112
+ // //Waiting for UART event.
113
+ // if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
114
+ // // bzero(dtmp, RD_BUF_SIZE);
115
+ // // ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_0);
116
+ // switch(event.type) {
117
+ // //Event of UART receving data
118
+ // /*We'd better handler data event fast, there would be much more data events than
119
+ // other types of events. If we take too much time on data event, the queue might
120
+ // be full.*/
121
+ // // case UART_DATA:
122
+ // // ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
123
+ // // uart_read_bytes(UART_NUM_0, dtmp, event.size, portMAX_DELAY);
124
+ // // ESP_LOGI(TAG, "[DATA EVT]:");
125
+ // // uart_write_bytes(UART_NUM_0, (const char*) dtmp, event.size);
126
+ // // break;
127
+ // //Event of HW FIFO overflow detected
128
+ // // case UART_FIFO_OVF:
129
+ // // ESP_LOGI(TAG, "hw fifo overflow");
130
+ // // // If fifo overflow happened, you should consider adding flow control for your application.
131
+ // // // The ISR has already reset the rx FIFO,
132
+ // // // As an example, we directly flush the rx buffer here in order to read more data.
133
+ // // uart_flush_input(UART_NUM_0);
134
+ // // xQueueReset(uart0_queue);
135
+ // // break;
136
+ // //Event of UART ring buffer full
137
+ // // case UART_BUFFER_FULL:
138
+ // // ESP_LOGI(TAG, "ring buffer full");
139
+ // // // If buffer full happened, you should consider encreasing your buffer size
140
+ // // // As an example, we directly flush the rx buffer here in order to read more data.
141
+ // // uart_flush_input(UART_NUM_0);
142
+ // // xQueueReset(uart0_queue);
143
+ // // break;
144
+ // //Event of UART RX break detected
145
+ // // case UART_BREAK:
146
+ // // ESP_LOGI(TAG, "uart rx break");
147
+ // // break;
148
+ // //Event of UART parity check error
149
+ // // case UART_PARITY_ERR:
150
+ // // ESP_LOGI(TAG, "uart parity error");
151
+ // // break;
152
+ // //Event of UART frame error
153
+ // // case UART_FRAME_ERR:
154
+ // // ESP_LOGI(TAG, "uart frame error");
155
+ // // break;
156
+ // //UART_PATTERN_DET
157
+ // case UART_PATTERN_DET:
158
+ // uart_get_buffered_data_len(UART_NUM_0, &buffered_size);
159
+ // int pos = uart_pattern_pop_pos(UART_NUM_0);
160
+ // // ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, buffered_size);
161
+ // if (pos == -1) {
162
+ // // There used to be a UART_PATTERN_DET event, but the pattern position queue is full so that it can not
163
+ // // record the position. We should set a larger queue size.
164
+ // // As an example, we directly flush the rx buffer here.
165
+ // // uart_flush_input(UART_NUM_0);
166
+ // } else {
167
+ // uint8_t c = 0;
168
+ // for (int i = 0; i < pos; i++) {
169
+ // uart_read_bytes(UART_NUM_0, &c, 1, 100 / portTICK_PERIOD_MS);
170
+ // ringbuf_put(&stdin_ringbuf, c);
171
+ // }
172
+ // // uint8_t pat[PATTERN_CHR_NUM + 1];
173
+ // // memset(pat, 0, sizeof(pat));
174
+ // uart_read_bytes(UART_NUM_0, &c, 1, 100 / portTICK_PERIOD_MS);
175
+ // // ESP_LOGI(TAG, "read data: %s", dtmp);
176
+ // // ESP_LOGI(TAG, "read pat : %s", pat);
177
+ // }
178
+ // mp_sched_keyboard_interrupt();
179
+ // break;
180
+ // //Others
181
+ // default:
182
+ // // ESP_LOGI(TAG, "uart event type: %d", event.type);
183
+ // break;
184
+ // }
185
+ // }
186
+ // }
187
+ // // free(dtmp);
188
+ // // dtmp = NULL;
189
+ // vTaskDelete(NULL);
190
+ // }
0 commit comments