UARTS in ATXMega's

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

UARTS in ATXMega's

Post by jethronull »

In FC V5 for AVR the RS232 component (shouldn't it really be a called a serial or UART component?) is configured as Port 1,2,3 or 4. But the AVR (at least the ATXMEGA128A1I'm using) has USARTC0/1, USARTD0/1, USARTE0/1 and USARTF0/1 How are Ports 1 to 4 in FC mapped to these USARTS? And is there anyway to access all 8? Lastly, how are the XMega's USART interrupts used?

Thanks for any insights.

JethroNull
Jon/JethroNull

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: UARTS in ATXMega's

Post by Enamul »

Hi

Can you please post the ATXMEGA128.FCD file from the FCD folder to check this for you? I don't have FCv5 professional version..I have checked in V4 which says only 2 USARTs are enabled in FC.
how are the XMega's USART interrupts used?
RXINT interrupt is used for USART interrupt..If you pick interrupt macro and can select RXINT from there.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: UARTS in ATXMega's

Post by Benj »

Hello,
(shouldn't it really be a called a serial or UART component?)
Yes it should really, RS232 is just the name of the component for legacy reasons.

Here are the pin layouts of the UART channels.
Define60=#define MX_UART_1
Define61=#define MX_UART_1_TX_PORT PORTC.OUT
Define62=#define MX_UART_1_TX_TRIS PORTC.DIR
Define63=#define MX_UART_1_TX_PIN 3
Define64=#define MX_UART_1_RX_PORT PORTC.OUT
Define65=#define MX_UART_1_RX_TRIS PORTC.DIR
Define66=#define MX_UART_1_RX_PIN 2
Define67=#define MX_UART_2
Define68=#define MX_UART_2_TX_PORT PORTC.OUT
Define69=#define MX_UART_2_TX_TRIS PORTC.DIR
Define70=#define MX_UART_2_TX_PIN 7
Define71=#define MX_UART_2_RX_PORT PORTC.OUT
Define72=#define MX_UART_2_RX_TRIS PORTC.DIR
Define73=#define MX_UART_2_RX_PIN 6
Define74=#define MX_UART_3
Define75=#define MX_UART_3_TX_PORT PORTD.OUT
Define76=#define MX_UART_3_TX_TRIS PORTD.DIR
Define77=#define MX_UART_3_TX_PIN 3
Define78=#define MX_UART_3_RX_PORT PORTD.OUT
Define79=#define MX_UART_3_RX_TRIS PORTD.DIR
Define80=#define MX_UART_3_RX_PIN 2
Define81=#define MX_UART_4
Define82=#define MX_UART_4_TX_PORT PORTD.OUT
Define83=#define MX_UART_4_TX_TRIS PORTD.DIR
Define84=#define MX_UART_4_TX_PIN 7
Define85=#define MX_UART_4_RX_PORT PORTD.OUT
Define86=#define MX_UART_4_RX_TRIS PORTD.DIR
Define87=#define MX_UART_4_RX_PIN 6
Define88=#define MX_UART_5
Define89=#define MX_UART_5_TX_PORT PORTE.OUT
Define90=#define MX_UART_5_TX_TRIS PORTE.DIR
Define91=#define MX_UART_5_TX_PIN 3
Define92=#define MX_UART_5_RX_PORT PORTE.OUT
Define93=#define MX_UART_5_RX_TRIS PORTE.DIR
Define94=#define MX_UART_5_RX_PIN 2
Define95=#define MX_UART_6
Define96=#define MX_UART_6_TX_PORT PORTE.OUT
Define97=#define MX_UART_6_TX_TRIS PORTE.DIR
Define98=#define MX_UART_6_TX_PIN 7
Define99=#define MX_UART_6_RX_PORT PORTE.OUT
Define100=#define MX_UART_6_RX_TRIS PORTE.DIR
Define101=#define MX_UART_6_RX_PIN 6
Define102=#define MX_UART_7
Define103=#define MX_UART_7_TX_PORT PORTF.OUT
Define104=#define MX_UART_7_TX_TRIS PORTF.DIR
Define105=#define MX_UART_7_TX_PIN 3
Define106=#define MX_UART_7_RX_PORT PORTF.OUT
Define107=#define MX_UART_7_RX_TRIS PORTF.DIR
Define108=#define MX_UART_7_RX_PIN 2
Define109=#define MX_UART_8
Define110=#define MX_UART_8_TX_PORT PORTF.OUT
Define111=#define MX_UART_8_TX_TRIS PORTF.DIR
Define112=#define MX_UART_8_TX_PIN 7
Define113=#define MX_UART_8_RX_PORT PORTF.OUT
Define114=#define MX_UART_8_RX_TRIS PORTF.DIR
Define115=#define MX_UART_8_RX_PIN 6
You can refer to all 8 UARTS on this device but you have to use a bit of C code customization to do so. To do this right click the RS232 component and click custom code.

Select the defines section and click edit code.

Find these lines of code.

#define MX_UART_CHANNEL_1 %b
#define MX_UART_CHANNEL_2 %b
#define MX_UART_CHANNEL_3 %b
#define MX_UART_CHANNEL_4 %b

and change them to this

#define MX_UART_CHANNEL_1 x
#define MX_UART_CHANNEL_2 x
#define MX_UART_CHANNEL_3 x
#define MX_UART_CHANNEL_4 x

Where x is the number from 1 to 8 of the hardware UART you wish to use. Channels 1-4 can be selected via the UART properties so you will only have to do this for UART channels 5 - 8.

All 8 RX interrupts should be available from the interrupt icon.

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Thanks Enamul and Ben,

So, if I understand you correctly, I should change/add the following in the custom code page:

#define MX_UART_CHANNEL_1 %b
#define MX_UART_CHANNEL_2 %b
#define MX_UART_CHANNEL_3 %b
#define MX_UART_CHANNEL_4 %b
#define MX_UART_CHANNEL_5 5
#define MX_UART_CHANNEL_6 6
#define MX_UART_CHANNEL_7 7
#define MX_UART_CHANNEL_8 8

And if so, how do I now use the upper 8? or is FC limited to the first 4 but I could map them differently?

I found the interrupts, that's straightforward since it uses the chips terminology.

Thanks again.

Jon
Jon/JethroNull

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Playing further today I discovered that the baud rates seem to be off, or at least I'm assuming that because serial strings are garbled and sometimes a few bytes longer or shorter. I've tried tweaking the baud rates but can't find a successful value. I've timed a pulsing output and can get that pretty accurate with a 2Mhz clock (which is what I believe the XMega defaults to). So is there a divider that needs to be set to get the baud rate right? Or is there something else going on here?

I'm getting worried that I'm going to need to change a lot of registers and fuses and while FC is great with the flowchart stuff, it looks like I'm on my own for the Xmega registers and fuses. Wondering if I should have stuck to PICs!

Thanks,

Jon/JethroNull
Jon/JethroNull

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Hi Guys,

More weirdness in the Xmega Usart. I found that at 9600 (and only 9600) baud the string comes through just fine. Any other baud rate it's still garbled. But even at 9600 the string is truncated to 20 characters. I'm thinking that might be a clue? Just to rule out anything stupid I might have done (entirely likely) I am now using the simple String Send.fcf example, though it must be pointed out that this was a PIC example that I imported, so possibly the Xmega is not getting critical registers set appropriately.

Jon/JethroNull
Jon/JethroNull

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: UARTS in ATXMega's

Post by Benj »

Hello,
So, if I understand you correctly, I should change/add the following in the custom code page:

#define MX_UART_CHANNEL_1 %b
#define MX_UART_CHANNEL_2 %b
#define MX_UART_CHANNEL_3 %b
#define MX_UART_CHANNEL_4 %b
#define MX_UART_CHANNEL_5 5
#define MX_UART_CHANNEL_6 6
#define MX_UART_CHANNEL_7 7
#define MX_UART_CHANNEL_8 8
No you set all the channels to the number of the UART you want to use if it is a UART that is between 5 and 8.
#define MX_UART_CHANNEL_1 %b
#define MX_UART_CHANNEL_2 %b
#define MX_UART_CHANNEL_3 %b
#define MX_UART_CHANNEL_4 %b
#define MX_UART_CHANNEL_1 5
#define MX_UART_CHANNEL_2 5
#define MX_UART_CHANNEL_3 5
#define MX_UART_CHANNEL_4 5
Can you attach a copy of your program and I will have a look just to see if your doing anything wrong.

The PPP tool should also let you configure your device rather then just using the text entry field as in v4 of Flowcode for AVR.

I find AVR's are very good devices in that they run 4 times faster then the equivalent PIC device however there are also some downsides in the form of slightly unintelligible datasheets and I've also experienced a few silicone bugs on some devices when using lots of peripherals in combination that caused me to have to put workarounds in place.

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Hi Ben,

Thanks for the encouraging words on the AVR. The specs certainly impress but the number and uniqueness of the registers is a little worrying.
#define MX_UART_CHANNEL_1 %b
#define MX_UART_CHANNEL_2 %b
#define MX_UART_CHANNEL_3 %b
#define MX_UART_CHANNEL_4 %b
#define MX_UART_CHANNEL_1 5
#define MX_UART_CHANNEL_2 5
#define MX_UART_CHANNEL_3 5
#define MX_UART_CHANNEL_4 5
I have to admit that makes no sense to me. Looks like FC uart 1-4 would all point to xmega uart 5 and xmega uarts 5-8 would still be unused.

As I said, my last experiment was using the FC example String Send.fcf, but attached is my avr'd version. About the only mod I've made is to extend the string a little, set the baud to 9600 and the chip clock to 2Mhz. I don't know what importing to AVR has done to it. To recap, this only seems to work when set to 9600, but even then the strings are still cropped at 20 chars. I also tried the echo example, it would only echo the first one or two characters sent to it.

Really appreciate your help.

Jon/JethroNull
Attachments
6. String Send.fcf_avr
(6.5 KiB) Downloaded 245 times
Jon/JethroNull

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: UARTS in ATXMega's

Post by Benj »

Hello,

Ok looking at your program the variable "string" you are using to hold the message is only 20 characters in length which is probably why you are having issues.

If you click Edit -> Variables then double click the string variable and edit the value between the square brackets to the maximum length you want the variable to be then the program should work correctly.

Do you have a scope or any other hardware you can use to try and pinpoint the baud rate problem when not using 9600 bps? Could be we have our UART baud calculation slightly incorrect for the Xmega range of devices as these were a recent addition to Flowcode AVR.

Regarding the UART slots we basically allow up to 4 UART's to be referenced from the Flowcode components. Each component will only ever talk to a single UART channel and therefore a single slot but you do not know which UART slot the component will use so we change the value for all slots for that specific component to ensure our required UART is used. As you only have one UART component in your program you should only have to change the first slot allocation.
#define MX_UART_CHANNEL_1 5

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Ben,

You're quite correct, the string was set to only 20 chars, my boo boo. I had previously set it longer when trying other baud rates and somehow it got reset to 20 for this last test and I didn't notice. So you've easily solved that one. But, still there are problems. Using the echo example, I get only 2 or sometimes three characters back. The fact that this varies does suggest a timing problem. I'll put the board on a scope today to look at the timing.

I have not really started my real application software for which I'd like to try and get all eight possible ports working. When I found the other problems I reverted to the simpler examples to remove as many 'variables' as possible. So, if I understand you, there is no way to use more than 4 serial ports in FC (except maybe s/w ports)? Maybe I'll have to do all the FC development with 4, then hack the generated C code to add the other 4. Having problems with the ol' stiff upper lip.

Thanks Ben, I'll let you know on the baud rate timing.
Jon/JethroNull

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Ben,

OK, I've scoped the output. The pulse period at 9600 baud looks like about 100uS maybe slightly less. So somewhere between 9800 and 10000. Set to 38400 the period is about 23.7uS so around 42200 baud (10% error). With this information I programmed the chip for 38400 baud but set the terminal program for 42200 and, tada, got the string output ungarbled. I tried the reverse, setting the baud rate in FC at 10% less and keeping the terminal at 38400, but that didn't work. Anyway, it looks like you do have a baud rate issue in your Xmega code. I can get back to my application with this adjustment but, obviously, it would be nice if you could fix the hiccup in your code for the Xmega. If you need me to scope any other rate just let me know.
Jon/JethroNull

jethronull
Posts: 30
Joined: Tue Dec 04, 2012 4:03 am
Has thanked: 1 time
Contact:

Re: UARTS in ATXMega's

Post by jethronull »

Hi Ben,

I don't suppoed you've had a chance to check into the xmega baud rate weirdness yet?

Jon/JethroNull
Jon/JethroNull

Post Reply