@facchinm, I will try to take a look again. It is interesting between different groups, want PRs in different ways... Like some want all the changes squashed into one, as it makes it easier to then rebase and you don't have to deal with the intermediate rebase errors on stuff that changed again in the next commit... Note: Some of memory may be a bit hazy on some of the details as it was something like four to six months ago.
Note: the issue is not really availableForWrite() always returns 0 but is the symptom of the writes to Serial ports is not cached at all and is always blocking. So currently the hard coded 0 is correct, that is you cannot write out anything without blocking.
Some of the steps and problems I ran into, include:
a) The TX Fifo object that was built into the Hardware Serial object is not used. First step, Have the SerialX.write(), now put things into the software FIFO, have ISR pull things out of the FIFO. Note: Used a simple buffer within the Serial object to pass to HAL to do the write (buffer, size).
When, the HAL is done with the data in the temporary buffer, it calls back to us, where if appropriate I refill the temporary buffer and call back to the HAL to continue writing.
Ran into problem with the HAL. When it stuffs out the last character in your buffer into the hardware register, it then does the callback, and if I tell it to output more stuff, the HAL code assumes that the write register is empty and blindly stuffs the first character of your new buffer into the output register. Unfortunately, at times it is not empty, and you just trashed the last character from the previous write.. This issue manifests as well with the current code base. For example Serial.println() would randomly trash the last character with the CR/LF output.
b) If the HAL is busted, you do you try to fix the HAL, or avoid it? I avoided trying to change the HAL, as I recall, it was not clear of all of the steps you had to do, to build a new static library...
The implantation would be cleaner having the ISR grab the data directly from the FIFO software queue... Don't remember if I did this or not.
c) Optional: - I really do not like your standard (API) software FIFO queue implementation. It is completely unsafe. As both writing to and reading from them both use a common variable (count), and I saw several places that used it, where they ran into issues with counts getting out of sync. .. So I implemented my own, which is the same as many others, where the count is calculated from head and tail... And the Writer and Reader only update either the head or the tail... But could go back to using the current one SafeRingBuffer which disables interrupts each time you add a character to it or remove one...
d) availableForWrite: can simply then return the count unused in the software FIFO.
e) Hardware FIFO - If I remember correctly a couple of the hardware uarts have a hardware FIFO. Don't remember if we made use of this or not.
f) SerialX.flush(): Current code does nothing as it simply checks that the unused TX Fifo is empty...
However, this is not sufficient. The flush should not return, until all of the bits of the last character have been transmitted. I am trying to remember if I put something in to check the actual transmission completed or not... If I remember correctly you tried to address this as well.
I am probably forgetting some of the other details. WIll soon go back and take a look.