Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] Accessing port pin using bit-field structure.

Status
Not open for further replies.

Rustum

Newbie level 4
Joined
Sep 25, 2016
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
107
Hi All,

I am trying to access a port pin using bit-field structure.I do not have very much expertise in embedded C. All I am doing is below.Please advice.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
typedef union
        {
    unsigned char Byte;
    struct
    {
         unsigned char bit0 : 1;
         unsigned char bit1 : 1;
         unsigned char bit2 : 1;
         unsigned char bit3 : 1;
         unsigned char bit4 : 1;
         unsigned char bit5 : 1;
         unsigned char bit6 : 1;
         unsigned char bit7 : 1;        
     }bits;
}registerType;
int main(void)
{
    
   registerType *pReg = ( registerType*)0x40040200;// port register address
    pReg->bits.bit1 = 0x00;
    pReg->bits.bit6 = 0; 
 
    for(;;)
    {
        /* Place your application code here. */
        
    }
}


The problem is Cortex-M4 CPU is going to an infinite interrupt handler loop when it hits the statement "pReg->bits.bit1 = 0;"

What am I doing wrong? Is it write way to access a hardware register and modify using structure and union?Please help.
 
Last edited by a moderator:

Regardless of the issue itself, accessing absolute addresses in code is always a bad practice. Instead, include the header for the device you are using, and make reference to that address by the alias of the registrar you wish to access.
 

Regardless of the issue itself, accessing absolute addresses in code is always a bad practice. Instead, include the header for the device you are using, and make reference to that address by the alias of the registrar you wish to access.



Hi,

Thanks for your reply.

Could you please help me with some reference code on how to do this.

Just for refresh....

I have a 32 bit port register and I know its physical address. I want to access the register and modifying individual bits of it through the structure. Thanks in advance.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top