USART DR register

Post your cool example code here.
Post Reply
User avatar
mack
Posts: 37
Joined: Fri Mar 08, 2019 7:59 pm
Location: Australia
OS: Windows 10
IDE: Arduino 188
Core: Roger (BluePill),StevStrong (F407 Black)
Board: BluePill,F407 Black

USART DR register

Post by mack » Thu Aug 15, 2019 9:40 pm

I had a frustrated day yesterday trying to find an exposed Define of the USARTs DR register.
With the Stm32duino forum down searches of previous work is lost.
I'm using Rogers core on the Bluepill.
I needed a very low overhead method of send one char at a time.
I ended up with this.

Code: Select all

volatile uint8 *txdr = NULL;


void setup() 
{
  txdr = (uint8*)0x40013804;  // USART1-DR address
  Serial.begin(9600);
Then sending a byte is simply

Code: Select all

*txdr = sbuff[ptr];
If someone knows of pre-defined or a more elegant method don't hold back.

Cheers
Andrew

stevestrong
Posts: 60
Joined: Tue Mar 05, 2019 7:49 am
Location: Munich
OS: Win7 & 10
IDE: Arduino 1.8.8, Sloeber
Core: Libmaple
Board: Bluepill, Generic F4VET6 black & mini
Contact:

Re: USART DR register

Post by stevestrong » Fri Aug 16, 2019 7:51 am

That method should work, but what is your final target?

User avatar
mack
Posts: 37
Joined: Fri Mar 08, 2019 7:59 pm
Location: Australia
OS: Windows 10
IDE: Arduino 188
Core: Roger (BluePill),StevStrong (F407 Black)
Board: BluePill,F407 Black

Re: USART DR register

Post by mack » Fri Aug 16, 2019 11:20 am

Oh yes, it is working.
I just expected to find the DR register to be pre-defined.

I'm using this in a sine wave inverter project (SPWM) . I transmit a small amount of data which can be logged etc by an external device. Using this method I send one byte at a fixed timebase (currently once a 50Hz cycle at zero degrees) so checks etc isn't required.

I could investigate using DMA but this method works well and its simple.

I was originally going to use a nano and it will work, but I'm getting used to the extra cycles and timers the BluePill gives me for much the same cost. The larger memory also allows me to make a lot more use of pre-defined tables. In another inverter design (not SPWM) I make full use of the 128k of flash by filling up with tables, saving lots of cycles. :D

Cheers
Andrew

stevestrong
Posts: 60
Joined: Tue Mar 05, 2019 7:49 am
Location: Munich
OS: Win7 & 10
IDE: Arduino 1.8.8, Sloeber
Core: Libmaple
Board: Bluepill, Generic F4VET6 black & mini
Contact:

Re: USART DR register

Post by stevestrong » Fri Aug 16, 2019 6:47 pm

Why don't you use

Code: Select all

Serial1.write(my_byte); // first should call Serial1.begin(<baud_rate>) in setup()
?
Or directly:

Code: Select all

USART1->regs->DR = my_byte;

User avatar
mack
Posts: 37
Joined: Fri Mar 08, 2019 7:59 pm
Location: Australia
OS: Windows 10
IDE: Arduino 188
Core: Roger (BluePill),StevStrong (F407 Black)
Board: BluePill,F407 Black

Re: USART DR register

Post by mack » Fri Aug 16, 2019 8:37 pm

Thanks for replying Steve.

I'm writing directly to the DR register purely to minimize the cycles cost.

Yes I tried

Code: Select all

USART1->regs->DR = my_byte;
(Rogers core) but I get

Code: Select all

exit status 1
'USART1' was not declared in this scope

stevestrong
Posts: 60
Joined: Tue Mar 05, 2019 7:49 am
Location: Munich
OS: Win7 & 10
IDE: Arduino 1.8.8, Sloeber
Core: Libmaple
Board: Bluepill, Generic F4VET6 black & mini
Contact:

Re: USART DR register

Post by stevestrong » Sat Aug 17, 2019 5:37 pm

In that case you may need to #include <libmaple/usart.h>.

User avatar
mack
Posts: 37
Joined: Fri Mar 08, 2019 7:59 pm
Location: Australia
OS: Windows 10
IDE: Arduino 188
Core: Roger (BluePill),StevStrong (F407 Black)
Board: BluePill,F407 Black

Re: USART DR register

Post by mack » Sat Aug 17, 2019 10:25 pm

Steve, that worked thanks .
I suppose I should have thought of that. I can get a bit fuddled in the labyrinth of c++ defs.

Cheers
Andrew

Post Reply