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_Receive_Data_Reg;
// Clear the SSR RDRF bit
Serial_Port0_Status_Register.BIT.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_Control_Register P_SCI0.SCR
#define Serial_Port0_Status_Register P_SCI0.SSR
#define Serial_Port1_Control_Register P_SCI1.SCR
#define Serial_Port1_Status_Register P_SCI1.SSR
....
---------------------------------------------------------- 16 2933
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_Receive_Data_Reg;
// Clear the SSR RDRF bit Serial_Port0_Status_Register.BIT.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_Control_Register P_SCI0.SCR #define Serial_Port0_Status_Register P_SCI0.SSR
#define Serial_Port1_Control_Register P_SCI1.SCR #define Serial_Port1_Status_Register P_SCI1.SSR ... ----------------------------------------------------------
"silentlights" <si**********@yahoo.co.uk> wrote in
news:7e******************************@localhost.ta lkaboutprogramming.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_Receive_Data_Reg;
// Clear the SSR RDRF bit Serial_Port0_Status_Register.BIT.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 ->
--
"Mark A. Odell" <od*******@hotmail.com> wrote in
news:Xn********************************@130.133.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 ->
--
Emmanuel Delahaye <em**********@noos.fr> wrote in
news:Xn***************************@212.27.42.66: In 'comp.lang.c', "silentlights" <si**********@yahoo.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 ->
--
Emmanuel Delahaye <em**********@noos.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 ->
--
In 'comp.lang.c', "Mark A. Odell" <od*******@hotmail.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/
In <Xn***************************@212.27.42.74> Emmanuel Delahaye <em**********@noos.fr> writes: In 'comp.lang.c', "Mark A. Odell" <od*******@hotmail.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
In <Xn**************************@212.27.42.74> Emmanuel Delahaye <em**********@noos.fr> writes: In 'comp.lang.c', "Mark A. Odell" <od*******@hotmail.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.
So, it had exactly zilch to do with our discussion. Of course, it is
sheer stupidity to try to map an externally imposed data format with a
structure, if you expect any kind of code portability.
But even in this case, you still use a structure and a couple of
conversion functions from external buffer format to internal structure
format and from internal structure format to external buffer format,
so that all the ugliness is restricted to these two functions.
And these two functions can be portably implemented, even if the
internal structure layout is implementation-specific.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Hi,
The compiler tell that, SCI is not type compatable. Could you let me know
what could be the reason.
Thanks
Densil
Hi,
The other methods you had suggested were little confusing for me. I could
get the switch case suggestion, it is simple. I am also trying something
like,
#define ACCESS(TYPE, LADR) (*((TYPE volatile *) (LADR)))
#define S_PORT(ADR) (ACCESS(st_sci, ADR))
#define SPORT0_TYPE S_PORT
#define SPORT0_ADR 0xFFFF78
#define SPORT1_TYPE S_PORT
#define SPORT1_ADR 0xFFFF80
#define SPORT2_TYPE S_PORT
#define SPORT2_ADR 0xFFFF88
void putChar(S_PORT PORTNAME, char VAL);
char getChar(S_PORT PORTNAME);
Could someone tell me what I made as mistake here..? Do you have an idea
of how I can do the header definitions in this way..?
Thanks in advance
Densil
"silentlights" <si**********@yahoo.co.uk> wrote in
news:57******************************@localhost.ta lkaboutprogramming.com: Hi, The other methods you had suggested were little confusing for me. I could get the switch case suggestion, it is simple. I am also trying something like,
#define ACCESS(TYPE, LADR) (*((TYPE volatile *) (LADR))) #define S_PORT(ADR) (ACCESS(st_sci, ADR))
#define SPORT0_TYPE S_PORT #define SPORT0_ADR 0xFFFF78 #define SPORT1_TYPE S_PORT #define SPORT1_ADR 0xFFFF80 #define SPORT2_TYPE S_PORT #define SPORT2_ADR 0xFFFF88
void putChar(S_PORT PORTNAME, char VAL); char getChar(S_PORT PORTNAME);
Could someone tell me what I made as mistake here..? Do you have an idea of how I can do the header definitions in this way..?
I think you need to dispense with the macros. What was so confusing about
my serial port picker table? Macros have problems that functions do not
and should only be used where a function cannot reasonably do the job.
--
- Mark ->
--
On Wed, 14 Jul 2004, silentlights wrote: #define ACCESS(TYPE, LADR) (*((TYPE volatile *) (LADR))) #define S_PORT(ADR) (ACCESS(st_sci, ADR))
void putChar(S_PORT PORTNAME, char VAL); char getChar(S_PORT PORTNAME);
Could someone tell me what I made as mistake here..?
Simple --- expand the macros and look for syntax errors.
void putChar(S_PORT PORTNAME, char VAL);
Here's a syntax error already --- S_PORT is an undefined identifier.
Did you mean perhaps
void putChar(S_PORT() PORTNAME, char VAL);
? That would expand to
void putChar((*((st_sci volatile *) ())) PORTNAME, char VAL);
which is again a syntax error. Or you might have meant
void putChar(S_PORT(PORTNAME), char VAL);
which would expand to
void putChar((*((st_sci volatile *) (PORTNAME))), char VAL);
which is /again/ a syntax error. What are you trying to do here?
Declare a function parameter, obviously, but what type is it supposed
to have? If it's a pointer to 'volatile st_sci', then just write
void putChar(st_sci volatile *PORTNAME, char VAL);
and be done with it. Make a macro
#define S_PORTD(ident) st_sci volatile *ident
void putChar(S_PORTD(PORTNAME), char VAL);
if you really want to, but that looks a lot like overkill to me.
Perhaps you'd better give some context...
-Arthur
What are you trying to do here? Declare a function parameter, obviously,
but what type is it supposed
to have?
I am trying to declare a function Eg. getChar. I have 4 serial ports
(Ports 0,1,2 & 3)and I want to write a single function putChar() common
for all ports. Inside the function I have to access the registers of
individual ports like PORT1->Data_Register or something like PORT1.DR. The
structure of the registers are defined in the st_sci structure.
In which case, I have pass an argument which has the address of the group
of registers which belong to Port 1. Inside the function, I have to handle
a particular register using the passed parameter. And then I landed up in
a piece of code like,
#define ACCESS(TYPE, LADR) (*((TYPE volatile *) (LADR)))
#define S_PORT(ADR) (ACCESS(st_sci, ADR))
#define SPORT0_TYPE S_PORT
#define SPORT0_ADR 0xFFFF78
#define SPORT1_TYPE S_PORT
#define SPORT1_ADR 0xFFFF80
#define SPORT2_TYPE S_PORT
#define SPORT2_ADR 0xFFFF88
#define sci S_PORT
//typedef S_PORT sci;
char getChar(sci PORTNAME);
char getChar(sci PORTNAME)
{
char data = PORTNAME.RDR;
return data;
}
Thanks in advance
Densil
You are right abt the code Mark, I appreciate your reply. I dont want to
execute a function which finds me an address everytime I am into a
getChar() function. Instead, I want to pass the address of the register
directly into the getChar function which would read the registers of the
corresponding ports. And this I want to reach using macros. I could use a
simple switch case block or the way you had suggested. A call to a
function costs me 8 cycles and then the execution of the function takes me
more. If I have a chance to pass it directly, then I have the read done
with less overhead.
Cheers
Densil This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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!
|
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...
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |