CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Compiler implements hardware Uart or not?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Torello



Joined: 29 Sep 2006
Posts: 120

View user's profile Send private message

Compiler implements hardware Uart or not?
PostPosted: Wed Sep 11, 2024 9:54 am     Reply with quote

Hi,

Is there a simple way to check if the compiler uses the hardware UART for the written code or not? Especially for parts that have a lot of pin select options
_________________
Regards, Edwin. PCWHD v5.114
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Wed Sep 11, 2024 1:53 pm     Reply with quote

Look at the C/ASM list file.
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

Re: Compiler implements hardware Uart or not?
PostPosted: Wed Sep 11, 2024 6:07 pm     Reply with quote

Torello wrote:
Hi,

Is there a simple way to check if the compiler uses the hardware UART for the written code or not? Especially for parts that have a lot of pin select options


If you specify the #use rs232 with a specific UART moniker and also omit / remove any pin assignments, it will use a hardware UART:

Code:
 
#USE RS232 (UART1, BAUD=9600, ERRORS)


If you are using a UART that needs pin assignments and still want to ensure it is hardware you can use #pin_select
Code:

#PIN_SELECT U1RX=PIN_B11
#PIN_SELECT U1TX=PIN_B10
#USE RS232 (UART1, BAUD=9600, ERRORS)
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Thu Sep 12, 2024 9:12 am     Reply with quote

Thanks for raising this point, it was something I was curious about as well. Had a little poke around with a program that just does putc('A');.

With no FORCE_SW specified in the header file, i.e. like this:

Code:
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


it produces the following assembler (comments are mine):

Code:
....................    putc('A');
0085:  MOVLW  41      Move 41h (the hex for "A") into W register
0086:  BTFSS  0C.4      Test bit 4 (TXIF) of register 0Ch (PIR1), skip next instruction if set
0087:  GOTO   086      i.e. loop until TXIF=1, i.e. the transmit register can accept data
0088:  MOVWF  19      Move W register (41h) into register 19h (TXREG). This now sends the character.


So that's definitely hardware UART. If FORCE_SW is specified, i.e. like:

Code:
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,FORCE_SW)


then it produces:

Code:
....................    putc('A');
0048:  MOVLW  41      Move 41h (the hex for "A") into W register
0049:  MOVWF  21      Move W register (41h) into register 21h, a general-purpose register
004A:  CALL   004      Call some subroutine at 004h, this presumably is the software UART thing.


and there's a big bunch of stuff also at 004h, presumably however it bit-bashes the UART.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Sep 12, 2024 11:40 am     Reply with quote

Exactly.
And as you see, easy to do.
There is a code trick that will allow you to detect it as well. When the
hardware UART is used the error byte is a copy of the status register.
This has bit 5 set (to say the receive is enabled), while the software
UART won't have this bit set.
Torello



Joined: 29 Sep 2006
Posts: 120

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 6:57 am     Reply with quote

thanx all!
Code:

#use delay(internal=8000000)

#PIN_SELECT U1RX=PIN_B7
#PIN_SELECT U1TX=PIN_B6
#use RS232(stream=debug, UART1, Baud=38400, ERRORS)

Works, but doesn't raise a warning. however it not use the internal UART1, I see the Goto xx in the ASM file.

If I put the #use delay() -after- the #use RS232(..) the compiler does throws an error that it needs this declaration.
Isn't that also a good indication? Or does the HW-UART implementation still use delays bases on that declaration?
_________________
Regards, Edwin. PCWHD v5.114
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 7:25 am     Reply with quote

No. Both the UART, and the software UART require the clock setup. How
else do you think they can setup the baud rate?.
What makes you say this doesn't select the UART?. It should. You don't
tell us what chip though, which leaves the possibility the compiler is
not selecting the hardware because these pins can't handle the UART.
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 9:53 am     Reply with quote

Torello wrote:
thanx all!
Code:

#use delay(internal=8000000)

#PIN_SELECT U1RX=PIN_B7
#PIN_SELECT U1TX=PIN_B6
#use RS232(stream=debug, UART1, Baud=38400, ERRORS)

Works, but doesn't raise a warning. however it not use the internal UART1, I see the Goto xx in the ASM file.

If I put the #use delay() -after- the #use RS232(..) the compiler does throws an error that it needs this declaration.
Isn't that also a good indication? Or does the HW-UART implementation still use delays bases on that declaration?


Looks like a hardware UART to me when I look at the ASM:
Code:

#case

#include <24fj64ga004.h>

#use delay(internal=8000000)

#PIN_SELECT U1RX=PIN_B7
#PIN_SELECT U1TX=PIN_B6
#use RS232(stream=debug, UART1, Baud=38400, ERRORS)

void main(){
  fputc('0', debug);
}



ASM:
Code:

....................   fputc('0', debug);
0242:  MOV.B   #30,W0L
0244:  BTSC.B  223.1
0246:  BRA     244
0248:  MOV.B   W0L,224
024A:  CLR.B   225


0x223 - 0x225 are all HW registers for the chip I put in the example.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 11:45 am     Reply with quote

Exactly. Using 'UART1', forces the compiler to use the hardware. I've never
seen it not do so.
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 12:00 pm     Reply with quote

So is either using UART1, or specifying the pins (e.g. xmit=PIN_C6,rcv=PIN_C7), both do the same thing?

I'm guessing that specifying the pins is superfluous since you can't pick and choose the pins anyway on most devices?
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 1:03 pm     Reply with quote

lindsay.wilson.88 wrote:
So is either using UART1, or specifying the pins (e.g. xmit=PIN_C6,rcv=PIN_C7), both do the same thing?

I'm guessing that specifying the pins is superfluous since you can't pick and choose the pins anyway on most devices?


Specifying pins in the #use also has the potential to make it a software UART. I've had older compiler versions in the past that did that. Best practice for hardware UARTS is to specify the uart (UART1, etc) and not specify the pins
lindsay.wilson.88



Joined: 11 Sep 2024
Posts: 40

View user's profile Send private message

PostPosted: Wed Sep 18, 2024 3:26 pm     Reply with quote

That's good to know, thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Sep 19, 2024 1:27 am     Reply with quote

This is why you will see me saying to always use the UARTx syntax. Very Happy

The 'pin' syntax is fine on older chips with the pins fixed in one place. On
later chips that had the pins movable by a fuse, this can easily get confused.
Then when PPS came along it got even easier to confuse it more!....

Set the fuse or PPS to select where the UART is
Then just tell the compiler to use the UART.

The most reliable way to ensure you are using the hardware UART.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group