Skip to content

Commit 428f7d2

Browse files
committed
add MF_SerialUartTask
1 parent 0140e23 commit 428f7d2

File tree

3 files changed

+150
-15
lines changed

3 files changed

+150
-15
lines changed

src/madflight/hal/ESP32/hal_ESP32_cpp.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,11 @@ MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert
257257
}
258258

259259
//get ser from MF_SerialPtrWrapper, and (re)configure it
260-
HardwareSerial *ser = ((MF_SerialPtrWrapper<HardwareSerial*>*)hal_ser[bus_id])->_serial;
260+
*ser = ((MF_SerialPtrWrapper<HardwareSerial*>*)hal_ser[bus_id])->_serial;
261261
ser->end();
262262
ser->setTxBufferSize(256);
263263
ser->setRxBufferSize(256);
264264
ser->begin(baud, config, pin_rx, pin_tx, invert);
265-
break;
266-
267-
268-
269265

270266
return hal_ser[bus_id];
271267
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Wrapper around SerialUART, uses a StreamBuffer to buffer TX
2+
3+
#pragma once
4+
5+
#include "hal_RP2040.h"
6+
#include "../MF_Serial.h"
7+
#include <stream_buffer.h>
8+
9+
void MF_SerialUartTask_task( void * pvParameters );
10+
11+
// Serial Interface
12+
class MF_SerialUartTask : public MF_Serial {
13+
public:
14+
SerialUART *_serial;
15+
StreamBufferHandle_t xStreamBuffer = NULL;
16+
17+
MF_SerialUartTask(SerialUART *_serial, const char* taskname) {
18+
19+
this->_serial = _serial;
20+
21+
xStreamBuffer = xStreamBufferCreate(256, 1); //lenght, triggerlevel
22+
23+
xTaskCreate( MF_SerialUartTask_task, /* The function that implements the task. */
24+
taskname, /* Human readable name for the task. */
25+
configMINIMAL_STACK_SIZE, /* Stack size (in words!). */
26+
(void*)this, /* Task parameter . */
27+
uxTaskPriorityGet(NULL), /* The priority at which the task is created. */
28+
NULL ); /* No use for the task handle. */
29+
}
30+
31+
void begin(int baud) override {
32+
_serial->begin(baud);
33+
}
34+
35+
int read(uint8_t *buf, int len) override {
36+
return _serial->readBytes(buf, len);
37+
}
38+
39+
int available() override {
40+
return _serial->available();
41+
}
42+
43+
int availableForWrite() override {
44+
return xStreamBufferSpacesAvailable(xStreamBuffer);
45+
}
46+
47+
int write(uint8_t *buf, int len) override {
48+
if(availableForWrite() < len) return 0;
49+
return xStreamBufferSend( xStreamBuffer, (const void *)buf, len, 0 ); //0 = no wait
50+
}
51+
};
52+
53+
void MF_SerialUartTask_task( void * pvParameters ) {
54+
MF_SerialUartTask *self = (MF_SerialUartTask*) pvParameters;
55+
uint8_t b;
56+
for( ;; ) {
57+
xStreamBufferReceive( self->xStreamBuffer, &b, 1, portMAX_DELAY );
58+
self->_serial->write(b);
59+
}
60+
}

src/madflight/hal/RP2040/hal_RP2040_cpp.h

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "MF_SerialUartTask_RP2040.h"
2+
13

24
//Arduino version string
35
#define HAL_ARDUINO_STR "Arduino-Pico v" ARDUINO_PICO_VERSION_STR
@@ -165,9 +167,12 @@ void hal_print_pin_name(int pinnum) {
165167
}
166168

167169

170+
171+
//SerialUartTask version
172+
#include "MF_SerialUartTask_RP2040.h"
173+
168174
//create/get Serial bus (late binding)
169175
//Serial BUS (&Serial, &Serial1, &Serial2) - ser0 &Serial is used for CLI via uart->USB converter
170-
171176
MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert) {
172177
if(bus_id < 0 || bus_id >= HAL_SER_NUM) return nullptr;
173178

@@ -186,16 +191,73 @@ MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert
186191
break;
187192
}
188193

189-
/*
190-
//uncomment one: SerialIRQ, SerialUART or SerialPIO and use uart0 or uart1
191-
auto *ser = new SerialIRQ(uart1, cfg.pin_ser1_tx, ser1_txbuf, sizeof(ser1_txbuf), cfg.pin_ser1_rx, ser1_rxbuf, sizeof(ser1_rxbuf));
192-
//auto *ser = new SerialIRQ(uart1, cfg.pin_ser1_tx, pin_ser1_rx, 256, 256); //TODO
193-
//auto *ser = new SerialDMA(uart1, cfg.pin_ser1_tx, pin_ser1_rx, 256, 256); //TODO
194-
//auto *ser = new SerialUART(uart1, cfg.pin_ser1_tx, pin_ser1_rx); //SerialUART default Arduino impementation (had some problems with this)
195-
//auto *ser = new SerialPIO(cfg.pin_ser1_tx, pin_ser1_rx, 32); //PIO uarts, any pin allowed (not tested, but expect same as SerialUART)
196-
hal_ser[1] = new MF_SerialPtrWrapper<decltype(ser)>( ser );
197-
*/
194+
//SerialUART default Arduino impementation (does not have TX buffer -> problem for ublox gps and mavlink ...)
195+
int pin_tx = -1;
196+
int pin_rx = -1;
197+
uart_inst_t *uart;
198+
char taskname[16];
199+
switch(bus_id) {
200+
case 0:
201+
pin_tx = cfg.pin_ser0_tx;
202+
pin_rx = cfg.pin_ser0_rx;
203+
uart = uart0;
204+
strcpy(taskname, "uart0");
205+
break;
206+
case 1:
207+
pin_tx = cfg.pin_ser1_tx;
208+
pin_rx = cfg.pin_ser1_rx;
209+
uart = uart1;
210+
strcpy(taskname, "uart1");
211+
break;
212+
default:
213+
return nullptr;
214+
}
215+
216+
//exit if no pins defined
217+
if(pin_tx < 0 && pin_rx < 0) return nullptr;
198218

219+
//create new MF_Serial
220+
if(!hal_ser[bus_id]) {
221+
SerialUART *ser = new SerialUART(uart, pin_rx, pin_tx);
222+
hal_ser[bus_id] = new MF_SerialUartTask(ser, taskname);
223+
}
224+
225+
//get ser from MF_SerialPtrWrapper, and configure it
226+
SerialUART *ser = ((MF_SerialUartTask*)hal_ser[bus_id])->_serial;
227+
ser->end();
228+
ser->setRX(pin_rx);
229+
ser->setTX(pin_tx);
230+
ser->setFIFOSize(256);
231+
ser->setInvertTX(invert);
232+
ser->setInvertRX(invert);
233+
ser->begin(baud, config);
234+
235+
return hal_ser[bus_id];
236+
}
237+
238+
239+
240+
/* //SerialUART version - works
241+
242+
//create/get Serial bus (late binding)
243+
//Serial BUS (&Serial, &Serial1, &Serial2) - ser0 &Serial is used for CLI via uart->USB converter
244+
MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert) {
245+
if(bus_id < 0 || bus_id >= HAL_SER_NUM) return nullptr;
246+
247+
uint16_t config;
248+
249+
switch(mode) {
250+
case MF_SerialMode::mf_SERIAL_8N1:
251+
config=SERIAL_8N1;
252+
break;
253+
case MF_SerialMode::mf_SERIAL_8E2:
254+
config=SERIAL_8E2;
255+
break;
256+
default:
257+
Serial.printf("\nERROR: hal_get_ser_bus bus_id=%d invalid mode\n\n", bus_id);
258+
return nullptr;
259+
break;
260+
}
199261
200262
//SerialUART default Arduino impementation (does not have TX buffer -> problem for ublox gps and mavlink ...)
201263
int pin_tx = -1;
@@ -238,6 +300,23 @@ MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert
238300
return hal_ser[bus_id];
239301
}
240302
303+
304+
*/
305+
306+
307+
308+
309+
310+
/*
311+
//uncomment one: SerialIRQ, SerialUART or SerialPIO and use uart0 or uart1
312+
auto *ser = new SerialIRQ(uart1, cfg.pin_ser1_tx, ser1_txbuf, sizeof(ser1_txbuf), cfg.pin_ser1_rx, ser1_rxbuf, sizeof(ser1_rxbuf));
313+
//auto *ser = new SerialIRQ(uart1, cfg.pin_ser1_tx, pin_ser1_rx, 256, 256); //TODO
314+
//auto *ser = new SerialDMA(uart1, cfg.pin_ser1_tx, pin_ser1_rx, 256, 256); //TODO
315+
//auto *ser = new SerialUART(uart1, cfg.pin_ser1_tx, pin_ser1_rx); //SerialUART default Arduino impementation (had some problems with this)
316+
//auto *ser = new SerialPIO(cfg.pin_ser1_tx, pin_ser1_rx, 32); //PIO uarts, any pin allowed (not tested, but expect same as SerialUART)
317+
hal_ser[1] = new MF_SerialPtrWrapper<decltype(ser)>( ser );
318+
*/
319+
241320
/*
242321
243322
MF_Serial* hal_get_ser_bus(int bus_id, int baud, MF_SerialMode mode, bool invert) {

0 commit comments

Comments
 (0)