I will try to explain some small quirks in the software implementation of the uart.
First looking at it it loads a value of 9 into a variable "putch_cnt", but the comment says "1 start bit + 8 bit + 1 stop bit" which is 10. The reason is that the loop-checks are done at the end of the loop not in the beginning, compare do...while(); and for(){...} loops in C. On the first iteration the counter is 9 and on the last it is 0, but the loop exits only when the zero:th iteration is executed.
The variable containing the output char is used as a 1-bit 8-deep shift register.
Before going into the first iteration the Carry is set high, to eventually output a stop bit, and output pin is set low, to output a start bit.
The first part of the loop a bit delay busy-wait inner loop.
Then the next bit value is shifted into the carry flag (thereby shifting the "stop bit" into the "shift register", and that will come out again on the last iteration.
The carry flag is double checked to avoid glitches in the output.
The loop counter is decremented and te loop starts over with the bit delay.
Due to the delay in the beginning of the loop the last iteration (stop bit) will get no delay, so an extra delay loop is placed afterwards together with some 2-cycle NOPs (goto $+1). The extra delay is to make sure that the stop bit is the same width as all other bits.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.