@@ -121,6 +121,8 @@ void UARTComponent::write_str(const char *str) {
121
121
this ->hw_serial_ ->write (str);
122
122
ESP_LOGVV (TAG, " Wrote \" %s\" " , str);
123
123
}
124
+ void UARTComponent::end () { this ->hw_serial_ ->end (); }
125
+ void UARTComponent::begin () { this ->hw_serial_ ->begin (this ->baud_rate_ , get_config ()); }
124
126
bool UARTComponent::read_byte (uint8_t *data) {
125
127
if (!this ->check_read_timeout_ ())
126
128
return false ;
@@ -273,6 +275,18 @@ void UARTComponent::write_str(const char *str) {
273
275
}
274
276
ESP_LOGVV (TAG, " Wrote \" %s\" " , str);
275
277
}
278
+ void UARTComponent::end () {
279
+ if (this ->hw_serial_ != nullptr )
280
+ this ->hw_serial_ ->end ();
281
+ else if (this ->sw_serial_ != nullptr )
282
+ this ->sw_serial_ ->end ();
283
+ }
284
+ void UARTComponent::begin () {
285
+ if (this ->hw_serial_ != nullptr )
286
+ this ->hw_serial_ ->begin (this ->baud_rate_ , static_cast <SerialConfig>(get_config ()));
287
+ else if (this ->sw_serial_ != nullptr )
288
+ this ->sw_serial_ ->begin ();
289
+ }
276
290
bool UARTComponent::read_byte (uint8_t *data) {
277
291
if (!this ->check_read_timeout_ ())
278
292
return false ;
@@ -338,7 +352,22 @@ void UARTComponent::flush() {
338
352
this ->sw_serial_ ->flush ();
339
353
}
340
354
}
341
-
355
+ void ESP8266SoftwareSerial::end () {
356
+ /* Because of this bug: https://github.com/esp8266/Arduino/issues/6049
357
+ * detach_interrupt can't called.
358
+ * So simply reset rx_in_pos and rx_out_pos even if it's totally racy with
359
+ * the interrupt.
360
+ */
361
+ // this->gpio_rx_pin_->detach_interrupt();
362
+ this ->rx_in_pos_ = 0 ;
363
+ this ->rx_out_pos_ = 0 ;
364
+ }
365
+ void ESP8266SoftwareSerial::begin () {
366
+ /* attach_interrupt() is also not safe because gpio_intr() may
367
+ * endup with arg == nullptr.
368
+ */
369
+ // this->gpio_rx_pin_->attach_interrupt(ESP8266SoftwareSerial::gpio_intr, this, FALLING);
370
+ }
342
371
void ESP8266SoftwareSerial::setup (int8_t tx_pin, int8_t rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t nr_bits,
343
372
std::string &parity) {
344
373
this ->bit_time_ = F_CPU / baud_rate;
@@ -347,13 +376,15 @@ void ESP8266SoftwareSerial::setup(int8_t tx_pin, int8_t rx_pin, uint32_t baud_ra
347
376
this ->parity_ = parity;
348
377
if (tx_pin != -1 ) {
349
378
auto pin = GPIOPin (tx_pin, OUTPUT);
379
+ this ->gpio_tx_pin_ = &pin;
350
380
pin.setup ();
351
381
this ->tx_pin_ = pin.to_isr ();
352
382
this ->tx_pin_ ->digital_write (true );
353
383
}
354
384
if (rx_pin != -1 ) {
355
385
auto pin = GPIOPin (rx_pin, INPUT);
356
386
pin.setup ();
387
+ this ->gpio_rx_pin_ = &pin;
357
388
this ->rx_pin_ = pin.to_isr ();
358
389
this ->rx_buffer_ = new uint8_t [this ->rx_buffer_size_ ];
359
390
pin.attach_interrupt (ESP8266SoftwareSerial::gpio_intr, this , FALLING);
0 commit comments