Author Topic: Usart  (Read 7751 times)

0 Members and 1 Guest are viewing this topic.

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Usart
« on: June 06, 2016, 08:10:31 am »
Hello,

I want to implement a serial communication between my PC and my card. I just want to initialize  USART

If anyone can help me ...

Thank you
« Last Edit: June 07, 2016, 07:13:14 am by paul8454 »
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #1 on: June 06, 2016, 12:48:06 pm »

When I write on the Windows terminal, the command doesn't send, so I'll necessarily in the else and I stay blocked in this loop
Code: [Select]
while(!SERCOM0->USART.INTFLAG.bit.DRE);
I don't understand why my orders ( here A for exemple) on the terminal are not taken considered
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2599
  • Country: us
Re: SERCOM USART SAMD10
« Reply #2 on: June 06, 2016, 12:53:58 pm »
I haven't looked at your SERCOM configuration in detail since it's been a while since I messed with SAMD series, but you're powering on the peripheral and hooking it up to a clock, so that's good. 

Two much more fundamental problems jump out, though:

1) You're calling writeData_uart with a string instead of the 8 bit uint that it expects.  I think you meant to call uart_send_string.  Your compiler should be giving you warnings about this.

2) You have the code that handles the UART before the main while(1) loop.  That means that the only time your MCU could possible receive and respond to any UART data is the time between enabling the UART and getting to your "if" statement.  At 48MHz, that's going to be a few hundred nanoseconds at absolute most, while it takes about one millisecond to transmit a single character at 9600 baud, so there's no way your MCU will ever manage to catch a character before it hits the empty while loop!  You need to move the whole if() block inside the while loop.
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #3 on: June 06, 2016, 01:07:26 pm »
Ok so I made the modification but my orderis not handled by the terminal why?
By reducing F_CPU ,my instructions are not processed
« Last Edit: June 07, 2016, 07:14:10 am by paul8454 »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #4 on: June 06, 2016, 04:22:07 pm »
There is a number of problems with your code:
1. You never initialize the pins to be connected to SERCOM module, so it never gets to send or receive anything. The code looks a lot like my MCU starter projects, so look for HAL macros in the same place and they are there for a reason.
2. Your readData_uart() simply reads the contents of the DATA register without waiting for something to be there. So after you correct item 1, your code will always print "Bye", since you will never be able to press a button fast enough.

Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
USART
« Reply #5 on: June 06, 2016, 04:43:21 pm »
Code: [Select]
Your read_uart() simply reads the contents of the DATA register without waiting for something to be there.
With the read function, it expects something on the part of Windows terminal. It should read my character I understand that communication between the character back into the terminal and my code don't interact.But the character is not sent to the terminal.
Code: [Select]
your code will always print "Bye", since you will never be able to press a button fast enough.

« Last Edit: June 07, 2016, 07:15:37 am by paul8454 »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #6 on: June 06, 2016, 04:46:54 pm »
I have to change the CPU frequency? But so what value I can go down?
Where did you get this idea from? Although, yes, your frequency may also be incorrect at the moment. That depends on what SystemInit() is doing.

Where did you get this code from? Why not use full working code first and go from there?

I use the table below
You configured SERCOM module itself, but you did not configure PORT (GPIO) module to release pins to SERCOM.

With the read function, it expects something on the part of Windows terminal. It should read my character 'A' I understand that communication between the character back into the terminal and my code don't interact.But the character 'A' is not sent to the terminal.
You need to wait for a flag that there is a byte in the data register and then read that register. Just like you are waiting for a flag that indicated that register is empty before you send a new byte.

PS: Please don't quote with code tags.
« Last Edit: June 06, 2016, 04:50:08 pm by ataradov »
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
USART
« Reply #7 on: June 06, 2016, 05:03:19 pm »
Quote
You configured SERCOM module itself, but you did not configure PORT (GPIO) module to release pins to SERCOM.

I must add this code of configure port in the function uarIinit()




« Last Edit: June 07, 2016, 07:17:21 am by paul8454 »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #8 on: June 06, 2016, 05:08:33 pm »
PORT->Group[1].PINCFG[4].bit.PMUXEN = 1;
PORT->Group[1].PINCFG[5].bit.PMUXEN = 1;
Something like this. I have not checked exact numbers, since I wrote macros once and I don't ever want to think about this again.

You will also need to select actual PMUX value. Seriously, look at my hal_gpio.h file. It is a single file that will do all that work for you.

But there are a lot of files that are associated
In my projects there are exactly 3 files that matter. The rest of them are standard header files, you don't need to do anything with them and they also  present in your ASF project.

I must add this code in main before if...
No in the main(), in the receive byte function. You want to wait for a new character every time you want to receive something, not just once.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #9 on: June 06, 2016, 05:25:05 pm »
the characters are not sent
I'm not going to check your magic numbers. It is a waste of my time, use hal_gpio.h, or waste your own time on your own.

Take the whole project from https://github.com/ataradov/mcu-starter-projects/tree/master/samd11 . It is a fully self contained project that is known to work. And start your modifications from there.

Right now, do you know that your SystemInit() configures the system for 48 MHz?
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #10 on: June 06, 2016, 05:53:45 pm »
You need to take the whole project. Don't just add files to your existing project. In this case you are missing a linker script, which provides all those missing symbols.

Alternatively you can take two files (main and hal_gpio.h), and skip the startup file. But in this case you will have to rename interrupt handler name for the timer (otherwise you won't see LED blinking). And you may need to do some other cleanups.

Learning to reuse other people's code is an important part of embedded development. Don't just discard the whole thing if something did not work right away.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #11 on: June 06, 2016, 05:55:33 pm »
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #12 on: June 06, 2016, 06:10:42 pm »
Yes it's good thank you I launched the demo with the LED blinking. But I find it far too complex for my little program, pines, I just use the code above.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #13 on: June 06, 2016, 06:13:27 pm »
You find my simple starter project too complex? That's like the simplest thing you can do.

Is there something specifically you don't understand about it?

ASF code includes all the same parts and more. You just don't see them if you specifically don't look for them.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #14 on: June 06, 2016, 06:30:29 pm »
1. Do usart_send_string() at the beginning of the program (just like the original one does). This will let you test that sending works. There is no point in trying to receive the data if you can't send anything.

2. When 1 works, show us how you readData_uart() is implemented.

There is no point of sending screnshoot, send actual the code.
Alex
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: SERCOM USART SAMD10
« Reply #15 on: June 06, 2016, 06:34:46 pm »
Also see here: https://github.com/WestfW/SAMD10-experiments/blob/master/D10-LED_TOGGLE0/src/UserSource/hardwareserial.c
for some simple, non-ASF UART code for the SAMD10 Xplained Mini.  You might have to filter out the arduino-like pin indirection.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #16 on: June 06, 2016, 06:35:11 pm »
Well, again, you defined F_CPU to 48 MHz. My projects are designed for 8 MHz and sys_init() sets 8 MHz frequency.

I strongly suggest starting entirely with my projects. You are creating problems for yourself by trying to use a mix of ASF and register-based code.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #17 on: June 06, 2016, 06:38:44 pm »
Show your complete code. There is something wrong with SERCOM initialization, so it does not work.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #18 on: June 06, 2016, 06:43:30 pm »
On D10 Xplained board, UART is connected to pins A10 and A11. Why are you trying to configure pins PB8 and PB9 (they don't even exist on this device)?

That's it. I'm not going to respond anymore until you start using GPIO macros from hal_gpio.h. I've created them to not waste my time, and you are doing exactly this by no using them.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #19 on: June 06, 2016, 06:49:15 pm »
I have three evaluation board I launched this code on SAMD11
That dose not explain why you are configuring the wrong pins.

I can't deal with this anymore. Take all the code from https://github.com/ataradov/mcu-starter-projects/tree/master/samd10 and try to run that. Without any changes at all. Don't take partial files, etc.

In your latest example, I don't see interrupt handler for the timer interrupt. The first timer interrupt that happens will create problems.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #20 on: June 06, 2016, 06:50:48 pm »
Also, which is it D11 or D10? The title says D10, your post says D11. They are different devices and are not binary compatible.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #21 on: June 06, 2016, 07:01:11 pm »
Quote
PORT->Group[1].
is a configuration for PORT B.  And pins 8 and 9 are the wrong pins for the board you are using. 0x22 may also be a wrong value, depending on your intent. I'm stopping here. You don't want to listen to a reasonable advice, deal with this on your own.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #22 on: June 06, 2016, 07:14:01 pm »
can you tell me what's wrong, please?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #23 on: June 06, 2016, 07:16:40 pm »
You are configuring the wrong pins.  I can't tell what configuration is correct,  I have macros for this, I'm not going to reverse engineer them to tell you what values to write.  You don't have you use them,  but at least have a look at what they are doing.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #24 on: June 06, 2016, 07:19:31 pm »

Well imagine that I take your project. and I want to make the current main that you advise me to do?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #25 on: June 06, 2016, 07:21:24 pm »
I don't understand the question.

If you take my project,  you will have a working good example of sending strings to UART. Add your receive function and you are done.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #26 on: June 06, 2016, 07:29:24 pm »
I would start from trying a real terminal program,  like TeraTerm,  for example.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #27 on: June 06, 2016, 07:42:19 pm »
You can also try to remove "+1" in the line
Code: [Select]
  SERCOM0->USART.BAUD.reg = (uint16_t)br+1;. But I doubt it will help given that characters that are received, received correctly.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #28 on: June 06, 2016, 07:46:12 pm »
And yes, there is a mistake in uart_sync() function, as westfw pointed out on AVRFreaks.

But all those syncs are useless anyway, so I'll remove them altogether.

Edit: Projects on GitHub no longer have syncs. I was not adding them for new projects anyway.
« Last Edit: June 06, 2016, 07:54:48 pm by ataradov »
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #29 on: June 06, 2016, 07:48:55 pm »
But if I now want to write about the Tera term A terminal where can I do this TeraTerm software?
This sentence makes no sense, but if you are asking how do you send things using TeraTerm, then just type it into main window.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #30 on: June 06, 2016, 07:56:10 pm »
Why do you define a file startup_samd11? Since basic present. why now it appears in the src files?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #31 on: June 06, 2016, 07:57:54 pm »
Because that is startup code required for normal operation. Before main() can be called, MCU needs to be prepared, memories initialized. That's what startup code does.

It is also present in your ASF project, it is just buried real deep, so you don't see it.

irq_handler_reset() is actually the first thing that gets called after reset, not main(). You don't really need to have main() at all.
« Last Edit: June 06, 2016, 07:59:25 pm by ataradov »
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #32 on: June 06, 2016, 08:00:57 pm »
it is different from yours. Why?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #33 on: June 06, 2016, 08:03:23 pm »
it is different from yours. Why?
Because I wrote my own. It is a part of the program, and all programs are different. There is no reason all startup code must be the same.

You can make your own as well, if you have good enough reason.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #34 on: June 06, 2016, 08:06:03 pm »
If I use the ASF file with the same name in your code that does not work. there are compilation errors. How can it be?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #35 on: June 06, 2016, 08:08:07 pm »
I don't understand what you are doing. There may be conflicting variables, for example. Those "compilation errors" will tell you exactly what is wrong.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #36 on: June 06, 2016, 08:10:12 pm »
So later when I would like to integrate your code to mine it will in no possible because the file is not the same?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #37 on: June 06, 2016, 08:12:37 pm »
Depends on what you want to integrate. You can take ASF project and put  main.c and hal_gpio.h into that project and it should compile and work.

The only thing you will have to change is the name of the function irq_handler_tc1() to whatever it is called in ASF. Or remove timer code altogether if you don't need it.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #38 on: June 06, 2016, 08:32:53 pm »

Finally, it works perfectly just a question about the file hal_gpio why you put '\ at the end of each line? if I remove it puts me expressions underlined in red?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #39 on: June 06, 2016, 08:34:44 pm »
Because that's how macros work in C. "\" is a line continuation character and macros in C have to be defined in a single logical line.

So that single logical line is split into many physical lines using "\" character.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #40 on: June 06, 2016, 08:41:01 pm »

The only error that occurred is:
F_CPU undefined  so I do a "#define" has 8M
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #41 on: June 06, 2016, 08:42:06 pm »
In my projects it is defined in the project settings, which is the right place for it.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #42 on: June 06, 2016, 08:49:46 pm »
Just in the project settings. Projects -> Properties -> Toolchain -> ARM/GNU C Compiler -> Symbols.
Alex
 
The following users thanked this post: paul8454

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #43 on: June 06, 2016, 08:58:18 pm »

Perfect. I understood. However I do not see why the Atmel Studio terminal does work contrary to Tera


Thank you very much
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11236
  • Country: us
    • Personal site
Re: SERCOM USART SAMD10
« Reply #44 on: June 06, 2016, 09:00:35 pm »
Because it is just that bad. AS itself barely works as an IDE, and you are asking that marginal extensions works well.

TeraTerm is used by way more people than AS is, so it is well debugged and known to work fine.
Alex
 

Offline paul8454Topic starter

  • Contributor
  • Posts: 16
  • Country: fr
Re: SERCOM USART SAMD10
« Reply #45 on: June 06, 2016, 09:08:46 pm »
OK THANK YOU
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf