Usart - Page 1
Forum section: electronics / general

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:10:31 am
content: Hello,I want to implement a serial communication between my PC and my card. I just want to initialize USARTIf anyone can help me ...Thank you

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 12:48:06 pm
content: 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 loopCode:while(!SERCOM0->USART.INTFLAG.bit.DRE);I don't understand why my orders ( here A for exemple) on the terminal are not taken considered

author: ajb
author_rank: Super Contributor
timestamp: June 06, 2016, 12:53:58 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 01:07:26 pm
content: Ok so I made the modification but my orderis not handled by the terminal why?By reducing F_CPU ,my instructions are not processed

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 04:22:07 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 04:43:21 pm
content: Code: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:your code will always print "Bye", since you will never be able to press a button fast enough.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 04:46:54 pm
content: Quote from: paul8454 on June 06, 2016, 04:43:21 pmWhere 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?Quote from: paul8454 on June 06, 2016, 04:43:21 pmYou configured SERCOM module itself, but you did not configure PORT (GPIO) module to release pins to SERCOM.Quote from: paul8454 on June 06, 2016, 04:43:21 pmYou 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 05:03:19 pm
content: QuoteI must add this code of configure port in the function uarIinit()

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 05:08:33 pm
content: Quote from: paul8454 on June 06, 2016, 05:03:19 pmSomething 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.Quote from: paul8454 on June 06, 2016, 05:03:19 pmIn 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.Quote from: paul8454 on June 06, 2016, 05:03:19 pmNo 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 05:25:05 pm
content: Quote from: paul8454 on June 06, 2016, 05:22:18 pmI'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 fromhttps://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?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 05:53:45 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 05:55:33 pm
content: Just in case, AS projects are located herehttps://github.com/ataradov/mcu-starter-projects/tree/master/samd11/astudio

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 06:10:42 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:13:27 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:30:29 pm
content: 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.

author: westfw
author_rank: Super Contributor
timestamp: June 06, 2016, 06:34:46 pm
content: Also see here:https://github.com/WestfW/SAMD10-experiments/blob/master/D10-LED_TOGGLE0/src/UserSource/hardwareserial.cfor some simple, non-ASF UART code for the SAMD10 Xplained Mini. You might have to filter out the arduino-like pin indirection.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:35:11 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:38:44 pm
content: Show your complete code. There is something wrong with SERCOM initialization, so it does not work.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:43:30 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:49:15 pm
content: Quote from: paul8454 on June 06, 2016, 06:46:30 pmThat dose not explain why you are configuring the wrong pins.I can't deal with this anymore. Take all the code fromhttps://github.com/ataradov/mcu-starter-projects/tree/master/samd10and 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 06:50:48 pm
content: Also, which is it D11 or D10? The title says D10, your post says D11. They are different devices and are not binary compatible.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:01:11 pm
content: Quoteis 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 07:14:01 pm
content: can you tell me what's wrong, please?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:16:40 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 07:19:31 pm
content: Well imagine that I take your project. and I want to make the current main that you advise me to do?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:21:24 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:29:24 pm
content: I would start from trying a real terminal program, like TeraTerm, for example.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:42:19 pm
content: You can also try to remove "+1" in the lineCode:SERCOM0->USART.BAUD.reg = (uint16_t)br+1;. But I doubt it will help given that characters that are received, received correctly.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:46:12 pm
content: And yes, there is a mistake in uart_sync() function, aswestfwpointed 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:48:55 pm
content: Quote from: paul8454 on June 06, 2016, 07:47:46 pmThis sentence makes no sense, but if you are asking how do you send things using TeraTerm, then just type it into main window.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 07:56:10 pm
content: Why do you define a file startup_samd11? Since basic present. why now it appears in the src files?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 07:57:54 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:00:57 pm
content: it is different from yours. Why?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:03:23 pm
content: Quote from: paul8454 on June 06, 2016, 08:00:57 pmBecause 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:06:03 pm
content: 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?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:08:07 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:10:12 pm
content: So later when I would like to integrate your code to mine it will in no possible because the file is not the same?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:12:37 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:32:53 pm
content: 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?

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:34:44 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:41:01 pm
content: The only error that occurred is:F_CPU undefined so I do a "#define" has 8M

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:42:06 pm
content: In my projects it is defined in the project settings, which is the right place for it.

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 08:49:46 pm
content: Just in the project settings. Projects -> Properties -> Toolchain -> ARM/GNU C Compiler -> Symbols.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 08:58:18 pm
content: Perfect. I understood. However I do not see why the Atmel Studio terminal does work contrary to TeraThank you very much

author: ataradov
author_rank: Super Contributor
timestamp: June 06, 2016, 09:00:35 pm
content: 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.

author: paul8454
author_rank: Contributor
timestamp: June 06, 2016, 09:08:46 pm
content: OK THANK YOU