473,799 Members | 3,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An alternative register access..?

Hi Guys, I am looking for an alternative means of register access in a
microcontroller . Currently my get() function looks like this..
------------------------
implementation in comm.c
------------------------
/*A simple get() function*/
unsigned char port0_get_char( void)
{
unsigned char input_c;

// Poll the SSR RDRF bit until it is ready
while ( !(Serial_Port0_ Status_Register .BIT.RDRF))
{ ; }

input_c = Serial_Port0_Re ceive_Data_Reg;

// Clear the SSR RDRF bit
Serial_Port0_St atus_Register.B IT.RDRF = SET_BIT_LOW;

return input_c;
}
............... ............... ............... ............

This means if I have 4 ports I have to define get()/put() functions for 4
serial ports 4 different times.. although the procedure is the same while
the registers are different. Is there a smater way of doing it... where
there could be only one function and access the registers in the functions
using pointers.. does any have a suggestion.. Here is how I defined the
header files.. is there an alternative way of defining the registers..

Thanks in advance
Densil
-------------------------------------------------
Header file
------------------------
definition in hardware.h
------------------------
struct st_sci { /* struct SCI
*/
union { /* SMR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char CA :1; /* C/A
*/
unsigned char CHR :1; /* CHR
*/
unsigned char PE :1; /* PE
*/
unsigned char OE :1; /* O/E
*/
unsigned char STOP:1; /* STOP
*/
unsigned char MP :1; /* MP
*/
unsigned char CKS :2; /* CKS
*/
} BIT; /*
*/
} SMR; /*
*/
unsigned char BRR; /* BRR
*/
union { /* SCR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char TIE :1; /* TIE
*/
unsigned char RIE :1; /* RIE
*/
unsigned char TE :1; /* TE
*/
unsigned char RE :1; /* RE
*/
unsigned char MPIE:1; /* MPIE
*/
unsigned char TEIE:1; /* TEIE
*/
unsigned char CKE :2; /* CKE
*/
} BIT; /*
*/
} SCR; /*
*/
unsigned char TDR; /* TDR
*/
union { /* SSR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char TDRE:1; /* TDRE
*/
unsigned char RDRF:1; /* RDRF
*/
unsigned char ORER:1; /* ORER
*/
unsigned char FER :1; /* FER
*/
unsigned char PER :1; /* PER
*/
unsigned char TEND:1; /* TEND
*/
unsigned char MPB :1; /* MPB
*/
unsigned char MPBT:1; /* MPBT
*/
} BIT; /*
*/
} SSR; /*
*/
};

#define P_SCI0 (*(volatile struct st_sci *)0xFFFF78) /* SCI0
Address*/
#define P_SCI1 (*(volatile struct st_sci *)0xFFFF80) /* SCI1
Address*/
#define P_SCI2 (*(volatile struct st_sci *)0xFFFF88) /* SCI2
Address*/
#define P_SCI3 (*(volatile struct st_sci *)0xFFFDD0) /* SCI3
Address*/

--------------------
definition in comm.h
--------------------
#define Serial_Port0_Co ntrol_Register P_SCI0.SCR
#define Serial_Port0_St atus_Register P_SCI0.SSR

#define Serial_Port1_Co ntrol_Register P_SCI1.SCR
#define Serial_Port1_St atus_Register P_SCI1.SSR
....
----------------------------------------------------------

Nov 14 '05 #1
16 2968
silentlights wrote:
Hi Guys, I am looking for an alternative means of register access in a
microcontroller . Currently my get() function looks like this..
------------------------
implementation in comm.c
------------------------
/*A simple get() function*/
unsigned char port0_get_char( void)
{
unsigned char input_c;

// Poll the SSR RDRF bit until it is ready
while ( !(Serial_Port0_ Status_Register .BIT.RDRF))
{ ; }

input_c = Serial_Port0_Re ceive_Data_Reg;

// Clear the SSR RDRF bit
Serial_Port0_St atus_Register.B IT.RDRF = SET_BIT_LOW;

return input_c;
}
............... ............... ............... ...........

This means if I have 4 ports I have to define get()/put() functions for 4
serial ports 4 different times.. although the procedure is the same while
the registers are different. Is there a smater way of doing it... where
there could be only one function and access the registers in the functions
using pointers.. does any have a suggestion.. Here is how I defined the
header files.. is there an alternative way of defining the registers..
You can simply define the function like this:

unsigned char get_char(int port)
{
volatile struct st_sci *sci;

switch(port)
{
case 0; sci = 0xFFFF78; break;
case 1; sci = 0xFFFF80; break;
case 2; sci = 0xFFFF88; break;
case 3; sci = 0xFFFDD0; break;
}

while (!sci->SSR.BIT.RDRF )
{
/* empty */
}

.......
}

You may want to create new defines for the addresses.

Thanks in advance
Densil
-------------------------------------------------
Header file
------------------------
definition in hardware.h
------------------------
struct st_sci { /* struct SCI
*/
union { /* SMR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char CA :1; /* C/A
*/
unsigned char CHR :1; /* CHR
*/
unsigned char PE :1; /* PE
*/
unsigned char OE :1; /* O/E
*/
unsigned char STOP:1; /* STOP
*/
unsigned char MP :1; /* MP
*/
unsigned char CKS :2; /* CKS
*/
} BIT; /*
*/
} SMR; /*
*/
unsigned char BRR; /* BRR
*/
union { /* SCR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char TIE :1; /* TIE
*/
unsigned char RIE :1; /* RIE
*/
unsigned char TE :1; /* TE
*/
unsigned char RE :1; /* RE
*/
unsigned char MPIE:1; /* MPIE
*/
unsigned char TEIE:1; /* TEIE
*/
unsigned char CKE :2; /* CKE
*/
} BIT; /*
*/
} SCR; /*
*/
unsigned char TDR; /* TDR
*/
union { /* SSR
*/
unsigned char BYTE; /* Byte Access
*/
struct { /* Bit Access
*/
unsigned char TDRE:1; /* TDRE
*/
unsigned char RDRF:1; /* RDRF
*/
unsigned char ORER:1; /* ORER
*/
unsigned char FER :1; /* FER
*/
unsigned char PER :1; /* PER
*/
unsigned char TEND:1; /* TEND
*/
unsigned char MPB :1; /* MPB
*/
unsigned char MPBT:1; /* MPBT
*/
} BIT; /*
*/
} SSR; /*
*/
};

#define P_SCI0 (*(volatile struct st_sci *)0xFFFF78) /* SCI0
Address*/
#define P_SCI1 (*(volatile struct st_sci *)0xFFFF80) /* SCI1
Address*/
#define P_SCI2 (*(volatile struct st_sci *)0xFFFF88) /* SCI2
Address*/
#define P_SCI3 (*(volatile struct st_sci *)0xFFFDD0) /* SCI3
Address*/

--------------------
definition in comm.h
--------------------
#define Serial_Port0_Co ntrol_Register P_SCI0.SCR
#define Serial_Port0_St atus_Register P_SCI0.SSR

#define Serial_Port1_Co ntrol_Register P_SCI1.SCR
#define Serial_Port1_St atus_Register P_SCI1.SSR
...
----------------------------------------------------------


Nov 14 '05 #2
"silentligh ts" <si**********@y ahoo.co.uk> wrote in
news:7e******** *************** *******@localho st.talkaboutpro gramming.com:
Hi Guys, I am looking for an alternative means of register access in a
microcontroller . Currently my get() function looks like this..
------------------------
implementation in comm.c
------------------------
/*A simple get() function*/
unsigned char port0_get_char( void)
{
unsigned char input_c;

// Poll the SSR RDRF bit until it is ready
while ( !(Serial_Port0_ Status_Register .BIT.RDRF))
{ ; }

input_c = Serial_Port0_Re ceive_Data_Reg;

// Clear the SSR RDRF bit
Serial_Port0_St atus_Register.B IT.RDRF = SET_BIT_LOW;

return input_c;
}


Purely from a C perspective (since there are no comm. ports in C) I'd
write a getchar() to read from a default comm. port and have it call
lower level function that take an arg. E.g.

int myStdin;

static int getCharFromPort (int port, unsigned char *pInChar)
{
static const unsigned long commPortAddr[] =
{
0xFFFF78, /* SCI0 */
0xFFFF80, /* SCI1 */
0xFFFF88, /* SCI2 */
0xFFFDD0 /* SCI3 */
}
struct CommPort *pCommPort = (struct CommPort *) commPortAddr[port];
int failures = !0;

if (!pCommPort->err)
{
if (pInChar) *pInChar = pCommPort->rbuf;
failures = 0;
}

return failures;
};
/* Yes 'int' */
int getchar(void)
{
/* getchar() defaults to myStdin which is a global
*/
unsigned char inChar;
int failuures;

failures = getCharFromPort (myStdin, &inChar);
if (failures)
{
failures = EOF;
/* also set error indicator
*/
}

return failures;
}
--
- Mark ->
--
Nov 14 '05 #3
"Mark A. Odell" <od*******@hotm ail.com> wrote in
news:Xn******** *************** *********@130.1 33.1.4:
struct CommPort *pCommPort = (struct CommPort *) commPortAddr[port];


A little check would be good here.

struct CommPort *pCommPort;

if (port < sizeof commPortAddr / sizeof *commPortAddr)
{
pCommPort = (struct CommPort *) commPortAddr[port];
}
else
{
/* failure */
}

--
- Mark ->
--
Nov 14 '05 #4
In 'comp.lang.c', "silentligh ts" <si**********@y ahoo.co.uk> wrote:
Hi Guys, I am looking for an alternative means of register access in a
microcontroller .


The bitfield trick you are using is attractive but deceiving. It's not
portable and should not be used in an external interface. It is recommended
to use bitwise operators. This may help:

http://mapage.noos.fr/emdel/clib/ed/inc/BITS.H

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #5
Emmanuel Delahaye <em**********@n oos.fr> wrote in
news:Xn******** *************** ****@212.27.42. 66:
In 'comp.lang.c', "silentligh ts" <si**********@y ahoo.co.uk> wrote:
Hi Guys, I am looking for an alternative means of register access in a
microcontroller .


The bitfield trick you are using is attractive but deceiving. It's not
portable and should not be used in an external interface. It is
recommended to use bitwise operators. This may help:


Although I agree with you in principle, I must say I use carefully packed
bit-fields often. It greatly enhances debugging with an ICE and make the
code far more readable, IMHO. One must decide how likely one's driver is
to be ported from one microcontroller to another or from one compiler to
another. In fact, many microcontroller drivers are for built-in
peripherals so they cannot be moved to another CPU type. As for compilers
changing, I don't change them for a given CPU, ever. Everything in
balance...

--
- Mark ->
--
Nov 14 '05 #6
In 'comp.lang.c', "Mark A. Odell" <od*******@hotm ail.com> wrote:
Although I agree with you in principle, I must say I use carefully packed
bit-fields often.


I once have been bitten by an Motorola to Intel bit order changes. Once was
fine. More would be silly.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
Emmanuel Delahaye <em**********@n oos.fr> wrote in
news:Xn******** *************** ****@212.27.42. 74:
Although I agree with you in principle, I must say I use carefully
packed bit-fields often.


I once have been bitten by an Motorola to Intel bit order changes. Once
was fine. More would be silly.


So you switched CPUs in mid-project? How, praytell, did you expect your
Intel CPU integrated peripheral driver to work on a Motorola CPU? Of
course you didn't because it was not a built-in peripheral driver was it?
For anything not built into the CPU, I would not use bit-fields either.
However, for built-in, CPU-specific peripherals I do use bit-fields.

--
- Mark ->
--
Nov 14 '05 #8
In 'comp.lang.c', "Mark A. Odell" <od*******@hotm ail.com> wrote:
Although I agree with you in principle, I must say I use carefully
packed bit-fields often.
I once have been bitten by an Motorola to Intel bit order changes. Once
was fine. More would be silly.


So you switched CPUs in mid-project? How, praytell, did you expect your


No. It was more a problem of data exchange between two different
architectures.
Intel CPU integrated peripheral driver to work on a Motorola CPU? Of
course you didn't because it was not a built-in peripheral driver was it?
For anything not built into the CPU, I would not use bit-fields either.
Agreed.
However, for built-in, CPU-specific peripherals I do use bit-fields.


Possibly, but the true problem was about some asynchronous data link between
a PC and a 68k machine. Trust me, bitwise (well named!) rocks, and bitfields
simply don't work.

And to conclude, I'm used I write and debug on x86, and then recompile and
download to different targets like 68k or PowerPC.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #9
In <Xn************ *************** @212.27.42.74> Emmanuel Delahaye <em**********@n oos.fr> writes:
In 'comp.lang.c', "Mark A. Odell" <od*******@hotm ail.com> wrote:
Although I agree with you in principle, I must say I use carefully packed
bit-fields often.


I once have been bitten by an Motorola to Intel bit order changes. Once was
fine. More would be silly.


As long as you can rewrite the structure definition, the bit-field
approach is still, by far, the preferable solution, in terms of code
readability/maintenability. As Mark said, most of such code is so
hardware specific (drivers for built-in processor features) that it
doesn't even make sense to talk about porting issues.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4287
by: Robert M. | last post by:
Information: Server A: SQL Server 2000 Enterprise Edition. OS is Windows 2003 Server Enterprise Edition. SQL Service pack is 3a. Member of domain ABCDomain. Server A is going to function as a Distributor. Location: New York. Server B: SQL Server 2000 Enterprise Edition. OS is Windows 2003 Server Standard Edition. SQL Service pack is 3a. Member of domain ABCDomain. Server B is going to function as a Subscriber.
2
2977
by: war_wheelan | last post by:
I have two servers running SQL Server on the same network and I am receiving errors registering the various instances as follows: SERVER1 tried to register the local instance (by netname, ip address and local) ERROR Msg: SQL Server does not exist or access denied (ConnectionOpen | Connect() SERVER1 trying to register Server2's default instance (by netname, ip address and local) COMPLETES SUCESSFULLY (THIS INSTANCE CAN BE REGISTER
1
4210
by: Earl Anderson | last post by:
My brother is in the process of purchasing a neighborhood dry cleaners store. Having seen some of the process applications I've written in MS Access, he asked me if I could develop an application to use in his new dry cleaning store since the existing one is of 1988 vintage. I told him that although I thought the 'process' involved in a dry cleaners couldn't be too complex and probably could be developed in Access, I had absolutely no...
115
14195
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i prefer a solution that (also) supports Windows. On the net I found a number of products that i looked at, but none of them gave me the impression of a serious candidate at this moment (KNoda, Gnome DB Manager, InterBase...). 2 additional...
20
3504
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
9
8612
by: Jackie | last post by:
Hi everyone, Does anyone know when "register" declarations should be used and when "register" must not be used? If possible please give examples for both cases. Thanks
29
2481
by: orium69 | last post by:
hi everyone, i'm wondering if there is a way to have sure that a variable is allocated in the cache, after its declaration with "register"? Tks!
1
2339
by: Hitchkas | last post by:
I posted this in PocketPC newsgroup with no response yet, hopefully somebody here has an answer. I have an application that runs locally on PocketPC. The user interface for this application is HTML with Javascript. The Javascript on the page instantiates an ActiveX control written in C++ ATL. The ActiveX does not have a visual interface and its basic functionality is to access the serial port and do some calculations and return the...
33
3293
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a full 10 minutes every time a "register" declared variable is read from or written to. Besides this lag, everything else runs as expected. Then my compiler is still C compliant, aye? If so, then it is unwise for any programmer to ever use the...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.