Wait Serial1 transmission to complete CODE

Post here all questions related to LibMaple core if you can't find a relevant section!
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Wait Serial1 transmission to complete CODE

Post by stevestrong »

Can you please a simple project which shows the problem?
Your code is too complex to find the error.
cpt_cyp
Posts: 8
Joined: Fri May 01, 2020 10:43 pm

Re: Wait Serial1 transmission to complete CODE

Post 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.
cpt_cyp
Posts: 8
Joined: Fri May 01, 2020 10:43 pm

Re: Wait Serial1 transmission to complete CODE

Post 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)
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Wait Serial1 transmission to complete CODE

Post by stevestrong »

As I wrote, please post a simple sketch which shows the problem case.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Wait Serial1 transmission to complete CODE

Post 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.
Last edited by ag123 on Sat May 02, 2020 4:33 pm, edited 3 times in total.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Wait Serial1 transmission to complete CODE

Post by stevestrong »

@ag123 , I know how it looks.
I just wanted to have from the op a simple sketch which shows the problem.
cpt_cyp
Posts: 8
Joined: Fri May 01, 2020 10:43 pm

Re: Wait Serial1 transmission to complete CODE

Post by cpt_cyp »

---------------
Last edited by cpt_cyp on Tue Aug 24, 2021 10:39 pm, edited 1 time in total.
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Wait Serial1 transmission to complete CODE

Post 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
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: Wait Serial1 transmission to complete CODE

Post 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 6346 times
roger's libmaple based core
Selection_341.jpg
Selection_341.jpg (66.7 KiB) Viewed 6346 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
cpt_cyp
Posts: 8
Joined: Fri May 01, 2020 10:43 pm

Re: Wait Serial1 transmission to complete CODE

Post 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
Post Reply

Return to “General discussion”