Page 2 of 2

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 2:37 pm
by stevestrong
Can you please a simple project which shows the problem?
Your code is too complex to find the error.

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 2:48 pm
by cpt_cyp
i did a project on arduino mega which they are communicate with other boards on rs485.
i have one arduino mega which is connected to pc via ethernet. its getting some variables and delivering other modules via rs485.
i used stm32f103 (bluepill) and getting messages well. but it cant send messages on rs485
if you read my prev. posts i did serial.flush() command but code stuck there.

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 3:09 pm
by cpt_cyp
i tried serial.flush is working
but serial1.flush not working
code stuck and i cant upload new code from ide until unplug connection (turn off the board power)

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 4:06 pm
by stevestrong
As I wrote, please post a simple sketch which shows the problem case.

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 4:25 pm
by ag123
the flush codes looks like this
https://github.com/rogerclarkmelbourne/ ... l.cpp#L139

Code: Select all

/* edogaldo: Waits for the transmission of outgoing serial data to complete (Arduino 1.0 api specs) */
void HardwareSerial::flush(void) {
    while(!rb_is_empty(this->usart_device->wb)); // wait for TX buffer empty
    while(!((this->usart_device->regs->SR) & (1<<USART_SR_TC_BIT))); // wait for TC (Transmission Complete) flag set 
}
if you have a setup something like eclipse or vscode done appropriately, they would be able to make a reference jump to the code.
i'd suggest pick up some skills with a st-link and play with debug, it is kind of fun for someone learning it for a first time, there is a learning curve there but it is well worth it

another thing to try, try writing some data

Code: Select all

Serial1.write(0x00)
before doing a flush, at least try to narrow down the problem. libmaple core is a 'community core' so it'd pretty much be like you have downloaded public domain codes and u'd need to try to figure things out as well.

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 4:26 pm
by stevestrong
@ag123 , I know how it looks.
I just wanted to have from the op a simple sketch which shows the problem.

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 4:42 pm
by cpt_cyp
---------------

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 5:09 pm
by ag123
there seem to be one thing that may be relevant, uart signals tend to be 'inverted', that down edge on serial output if i understand it correctly could be the *start bit*
https://arduino.stackexchange.com/quest ... ctive-high
the default line discipline is 8N1 in libmaple core, actually those flags won't change the line discipline if i read the codes correctly.
so if you need a different line discipline you may need to call other codes or set the registers directly

i'm not sure if it seems like the data has actually been successfully transmitted between the toggle on the enable pin as your codes suggests.
i.e. Serial1.flush() actually ran and returned.

it seemed as well (non stm32) uart implementations have 'weird caveats' some are expecting no gaps between bytes (more correctly) 8N1 frames transmitted, while stm32 allows and expects gaps between byte (8N1) frames

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 5:41 pm
by stas2z
ive made a simple test using classic bluepill board and logic analyzer

here the sample code

Code: Select all

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  pinMode(PA3, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  Serial.println("Starting...");
  digitalWrite(LED_BUILTIN, HIGH);
  
  digitalWrite(PA3, HIGH);
  Serial1.println("TEST");
  Serial1.flush();
  digitalWrite(PA3, LOW);
  
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("Sent...");
  delay(1000);
}
both cores working well

STM32 HAL based core
Selection_342.jpg
Selection_342.jpg (70.96 KiB) Viewed 6430 times
roger's libmaple based core
Selection_341.jpg
Selection_341.jpg (66.7 KiB) Viewed 6430 times
purple is Serial1 TX pin
blue - PA3 pin (acts as tx enable pin for rs485)

P.s.
I hope your rotary_read is not an irq handler

Re: Wait Serial1 transmission to complete CODE

Posted: Sat May 02, 2020 6:02 pm
by cpt_cyp
@stas2z
simple sketch is working well
but i dont know why my sketch is not working serial1.flush
maybe it stuck in rotary encoder routin..

i will find failure and post the solution

thank you soo much