Accessing HW registers using bit fields and UINT32

This is a discussion on Accessing HW registers using bit fields and UINT32 within the C Programming forums, part of the General Programming Boards category; Hi - This is a follow up from my last post: http://cboard.cprogramming.com/showthread.php?t=109945 but need help with my code. I would ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    14

    Accessing HW registers using bit fields and UINT32

    Hi - This is a follow up from my last post: How do I force my compiler to access HW registers as 32 bit instead of bytes?

    but need help with my code. I would like to test that accessing the HW reg using a struct unionized with a UINT32 word, will actually use 32-bits. Here is my code:

    Code:
    #include <stdio.h>
    
    
    typedef union 
    {
        struct {
    
        /// Reserved - read as 0
        unsigned reserved : 30;
    
        /** This bit contains the interrupt status. Reading this register
         * clears this bit.
         */
        unsigned stats : 1;
    
        /** This field masks the interrupt.
         *	0 - Masked
         *	1 - Enabled
         */
        unsigned maskn : 1;
    
        } bits;
    
        unsigned long word;
    
    } volatile RegStruct;
    
    RegStruct *hwReg = (RegStruct *)0x08002000
    
    
    void SetReg0(void)
    {
    	int newVal = 0;
    	RegStruct val;
    	val.word = hwReg->word;
    	val.bits.maskn = newVal;
    	hwReg->word = val.word;	
    }
    
    
    void SetReg1(void)
    {
    	int newVal = 1;
    	RegStruct val;
    	val.word = hwReg->word;
    	val.bits.maskn = newVal;
    	hwReg->word = val.word;	
    }
    
    int main(void)
    {
    
    	RegStruct reg;
    	
    	SetReg0();
       	int x = reg.bits.maskn;
    	printf("This is setting to 0: %d \n",x);
    
    	SetReg1(); 
    	int y = reg.bits.maskn;
    	printf("This is setting to 1 : %d \n", y);
    
    	return 0;
    
    }
    ...but when I run gcc, I get the following errors:
    testdriver2.c:31: error: syntax error before 'void'
    testdriver2.c:35: error: syntax error before '.' token

    Why is this simple test failing? Please help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The error is "before void", and the thing before void is a statement missing a semicolon....

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    GREAT - thanks for your help. I put the semicolon in the right spot.

    However, when I run the program I get a segmentation fault. Someone please tell me why - it it because I don't actually have any hardware connected - or should it work on its own as is? I am new to accessing HW using bit fields. Please help!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    All I can say is that at the present time, 0x8002000 does not correspond to memory you "own" (either in hardware or in virtual memory). If you want to test, I suppose instead of declaring a pointer you could just declare an instance perhaps (perhaps with a different name, so that you can then declare the pointer to point to that instance).

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    All I can say is that at the present time, 0x8002000 does not correspond to memory you "own" (either in hardware or in virtual memory). If you want to test, I suppose instead of declaring a pointer you could just declare an instance perhaps (perhaps with a different name, so that you can then declare the pointer to point to that instance).
    thanks for your help. this was my bad because i wasn't connected to the hardware in the correct way...i had never done this before. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing bit fields using a union problem.
    By samdomville in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21