Reuse UART driver for multiple UARTs in one project

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again everyone!

 

So I am continuing my controller project that requires me to communicate between a head-end and remotes, and I have worked out my radio hardware selection thanks to all your help. I'm using the ATMega324PB (although any AT with two UARTs and an I2C port should work for this purpose, so let's not get bogged down with that) to communicate between the PC and a data radio, both with UART interfaces. 

 

What I'm having trouble with today is, I want to re-use my driver I wrote for using the ISR for both UART connections. I have been using it for PC to MCU communication, and I have worked out a lot of nice conventions and tested the code pretty thoroughly, so re-use is the best bet. However, I used macros for setting the ports and ISR vectors. I would define the ISR_USART_PORT in the Symbols subsection of the Toolchain section of the project properties (where I set the F_CPU so all headers pick it up) and the correct port would be used. 

 

Now I can't do that if I want to use it twice, so I tried 

#define ISR_USART_PORT 0 // init the usart on port 1 for talkback to PC
#include "hardware/isr_usart.h"

but it still compiles as if port 1 was selected, because of this in the header

#ifndef ISR_USART_PORT
	#define ISR_USART_PORT 1 // 0, 1 or 2
#endif

but if I remove that line I get an error that ISR_USART_PORT is undefined.

 

Can anyone help me with the structure of the program in order to allow multiple usages of the same source files? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to use "token pasting".

 

I did this for UART also, and I think I posted a bunch of it.  Searching...

https://www.avrfreaks.net/forum/p...

(and maybe https://www.avrfreaks.net/forum/r... and https://www.avrfreaks.net/forum/x... )

 

Now, this use was more to configure a "generic I/O remote module" in an app series according to clock speed and TE port/pin and so on.  The 'Freaks helped me -- a search for "mkname" might uncover discussion. [yes those search results will be pertinent for you]

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Jul 12, 2017 - 07:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it worth it?

 

For just a couple of UARTs, I find it just as easy to take a copy of the file for, say, UART1 and just do a few global search-and-replace options to make it for, say, UART2.

 

Simple - but effective.

 

---------

 

If you do want to see lots of token-pasting tricks, look at ASF ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I get that, but I come from the world of re-use and version control and the like, so the idea of COPYING files around like that is just icky to me. I think I'm going to shove the whole thing into a single header file because that'll do what I want. C is just not my normal cup o' tea, so wrapping my head around the "correct" way to do things takes some time. I had a similar problem with SQL until I realized it's set theory and linear algebra. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I guess I just make it all in one header file. Seems cool. I'll give it a try. Thanks for the links. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did someone mention "C++"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

take a look at Arduino HardwareSerial class. They do exactly that!

 

Superficially, there seems to be a LOT of code, but most of that gets resolved at compile time.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Thu. Jul 13, 2017 - 07:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Did someone mention "C++"?


No. I hate you. Shut up. d:

C++ and I don't really get along. I'd rather have the system tell me exactly how it'll screw me, so I'm using C. There just has to be better ways to reuse stuff while still being functional/imperative rather than OOP.