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 support@ccsinfo.com

passing uart stream value as an arg

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



Joined: 15 Jul 2005
Posts: 59

View user's profile Send private message

passing uart stream value as an arg
PostPosted: Thu Aug 09, 2012 10:58 am     Reply with quote

Hi all,

Hope all is well with ya all!

i was wondering if it is possible to pass uart "stream" type as an arg?

example:

Code:

#use rs232(UART1, baud=38400,parity=N,bits=8, STREAM=RS1)
#use rs232(UART2, baud=38400,parity=N,bits=8, STREAM=RS2)
.
.
.
c=getc(RS1);     // currently supported
c=getc(RS2);     // currently supported
.
.
c=kamsGetC(RS1);   <--- what would be the function prototype for this?
.
.

char kamsGetC(STREAM rs)   <--- what is the type???
{
     return getc(rs);
}
.
.
.


Granted I can do this via an ENUM just fine, I was just wondering if it could be done...

~Kam
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Thu Aug 09, 2012 11:08 am     Reply with quote

Can't do it, but you can work around it by putting the uart calls in a case statement, switching on some variable to call with the appropriate stream.
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Sun Aug 12, 2012 5:02 am     Reply with quote

It is also important to perhaps understand 'why'.
The stream name, is not really a variable. It is effectively a 'preprocessor directive', telling the compiler to generate different code. So it may switch between generating code for a software UART, hardware UART, or (as in the example), two different hardware UART's.
So when you call 'fputc', with UART1, code to access the first UART registers is generated, while with UART2, code for the second set of hardware is generated.
Simplest way to switch with a variable, is to just generate you own putc_tostream, and getc_fromstream functions, and have these test a global variable, and call fputc/fgetc, with the required stream name. Setup the global variable, and use these functions instead of the normal ones.

Best Wishes
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Sun Aug 12, 2012 9:17 am     Reply with quote

Ttelmah wrote:

Simplest way to switch with a variable, is to just generate you own putc_tostream, and getc_fromstream functions, and have these test a global variable, and call fputc/fgetc, with the required stream name. Setup the global variable, and use these functions instead of the normal ones.


The selection variable doesn't need to be global, does it?

Can't the user have a fully "self contained" function with both fget's pointing to his 2 different streams returning a local result var?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Sun Aug 12, 2012 9:49 am     Reply with quote

Oh, yes, but in general, if he wants overall 'switching', it is nicer to have his own functions, and use these like the normal ones. So, something like:
Code:

int1 second_stream=FALSE;

#define select_stream1() second_stream=FALSE
#define select_stream2() second_stream=TRUE

void putc_to_stream(char chr) {
   if (select_stream) fputc(chr,UART2);
   else fputc(chr,UART1);
}

char getc_from_stream(void) {
   if select_stream return(fgetc(UART2));
   return fgetc(UART1);
}

    //Then to send/get from stream1
    select_stream1();
    printf(putc_to_stream,"Message to stream1");

    //Then to talk to stream 2
    select_stream2();
    printf(putc_to_stream(),"Message to stream2");


Making the variable 'global', means the stream can be switched anywhere, and apply to all code that uses the putc_to_stream, or getc_from_stream functions.

Best Wishes
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