Solved

how to set a bit of register in linux

Posted on 2010-11-09
18
466 Views
Last Modified: 2012-05-10
as we have sbit command to set a bit of registers in controllers ...how can i set the bit of a16-bit register in linux...
0
Comment
Question by:raulapati
  • 9
  • 6
  • 3
18 Comments
 
LVL 5

Expert Comment

by:Kendor
Comment Utility
you might be looking for something like:

`setpci -v -H1 -s 0:0.0 x=y`


 * `-H1` lets setpci to have direct hardware access (intel configuration mode)
 * `-s 0:0.0` means Bus (0:) Slot (.0) and function (.0) (check out setpci(8))
 * `x=y` writes value (y) in register (x)

0
 

Author Comment

by:raulapati
Comment Utility
no no sir that was not my doubt...

if i take
unsigned char INVERSE_FLAGS;
 now i have to set each bit of INVERSE_FLAGS...
like first bit as some Negative flag
       second bit as zero flag...

as in microcontroller we can do it by  sbit Negative=INVERSE_FLAGS^0;


how can i do the same in linux..
0
 
LVL 12

Expert Comment

by:HappyCactus
Comment Utility
What kind of register is it? a cpu register? An I/O memory mapped register? an i/o on a device connected via i2c?
0
 

Author Comment

by:raulapati
Comment Utility
it is a general register...

actually my intention is ,in my project in which i have to read adc (ADE7758) there are some flags which are to be set.so i have to intialise them in avariable which is of byte width....
0
 
LVL 12

Expert Comment

by:HappyCactus
Comment Utility
So the register is an i2c DEVICE register ?
 
0
 

Author Comment

by:raulapati
Comment Utility
spi device register
0
 
LVL 12

Expert Comment

by:HappyCactus
Comment Utility
Follow the protocol as specified in the ade7758 datasheet (page 60)

first you must write the comm. register to specify the register you want to read/write.
Then you must write/read the length required.
For example, to read the AWATTHR register, write the value 0x01, write 0x00, read the first byte (MSB) of the register, write 0x00, read the second byte (LSB).
To write the OPMODE register, write 0x83 (0x13 is the address, 0x80 the write command), then write the byte.

All you the details you need are written in the datasheet.
0
 
LVL 5

Expert Comment

by:Kendor
Comment Utility
ah ok -
so have you checked out: http://www.kernel.org/doc/htmldocs/device-drivers/

namely you are looking for the SPI functions, right?
you find them here: http://www.kernel.org/doc/htmldocs/device-drivers/ch09.html
more specific you can set up your device using this structure: http://www.kernel.org/doc/htmldocs/device-drivers/re747.html
where you have all the bits/flags to set

is this what you are looking for?
(as far as I understand you are trying to access your ADc over SPI from a Linux)
0
 

Author Comment

by:raulapati
Comment Utility
i have solved this question
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:raulapati
Comment Utility
still i have not solved this problem
0
 

Author Comment

by:raulapati
Comment Utility
how to set a single bit in a register in linux...
0
 
LVL 12

Expert Comment

by:HappyCactus
Comment Utility
What is your problem? Please describe carefully the difficulties you have, and eventually post some code.
0
 
LVL 5

Expert Comment

by:Kendor
Comment Utility
raulapati -
you see that it is not easy for you to help you on your problem as you do not provide us with any information. post some code, clear problems, error messages, detailed infrmation on your setup etc... otherwise this is leading nowhere...
0
 

Author Comment

by:raulapati
Comment Utility
in ade7758 datasheets page no.68 describes about interrupt register.
here i have to compare the status of each and every bit of that registers and do some manupulations by the status of that registers so i want to set these values to a general register by taking unsigned char variable.so i have to set each and every bit of that register(0x18)to a general char variable..and proceed by comparing each bit...the same task can be done in microcontroller flatform by initialising as below:;;

bdata unsigned char REVERSE_FLAGS;

bdata unsigned char REVERSE;



sbit WATT_REV_R=REVERSE_FLAGS^2;

sbit WATT_REV_Y=REVERSE_FLAGS^1;

sbit WATT_REV_B=REVERSE_FLAGS^0;



sbit VAR_REV_R=REVERSE^5;

sbit VAR_REV_Y=REVERSE^4;

sbit VAR_REV_B=REVERSE^3;
unsigned char bdata INTERRUPT_STATUS_REG1;

unsigned char bdata INTERRUPT_STATUS_REG2;

unsigned char bdata INTERRUPT_STATUS_REG3;



sbit AEHF       = INTERRUPT_STATUS_REG1^0;

sbit REHF       = INTERRUPT_STATUS_REG1^1;

sbit VAEHF      = INTERRUPT_STATUS_REG1^2;

sbit SAGA       = INTERRUPT_STATUS_REG1^3;

sbit SAGB       = INTERRUPT_STATUS_REG1^4;

sbit SAGC       = INTERRUPT_STATUS_REG1^5;

sbit ZXTOA      = INTERRUPT_STATUS_REG1^6;

sbit ZXTOB      = INTERRUPT_STATUS_REG1^7;

sbit ZXTOC      = INTERRUPT_STATUS_REG2^0;

sbit ZXA        = INTERRUPT_STATUS_REG2^1;

sbit ZXB        = INTERRUPT_STATUS_REG2^2;

sbit ZXC        = INTERRUPT_STATUS_REG2^3;

sbit LENERGY    = INTERRUPT_STATUS_REG2^4;

sbit PKV        = INTERRUPT_STATUS_REG2^6;

so how can i perform the same task in linux?

means how to initialise each bit of a char variable..?



internally by comparing i have to do some task as below...

else if((VAR_REV_R==1)&&(WATT_REV_R==1))

      {//iflag_r

      if(energy2[0]>(0x8000))

      totreacteng_lag[0]=totreacteng_lag[0]+(((65535-energy2[0])+1)/1000.0)*kvarh_mf[0]*energy_mf;

      }//iflag_r

etc....

so please give mee any idea...
0
 
LVL 12

Expert Comment

by:HappyCactus
Comment Utility
If WATT_REV_R is the bit 2 (3rd bit) of REVERSE_FLAGS, you can test it as

if ( ( REVERSE_FLAGS & (1 << 2) ) != 0 )

If you aren't familiar with the bitwise operators in C, please look at this: [1] or [2]


[1] http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitwise.html
[2] http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/bitwise.html

0
 

Author Comment

by:raulapati
Comment Utility
first of all, how i have to intialise WATT_REV_R as a 3rd bit of REVERSE_FLAGS?
0
 
LVL 12

Accepted Solution

by:
HappyCactus earned 125 total points
Comment Utility
First, read the REVERSE_FLAG register.
Then set the 3rd bit with the "bitwise or" operator, i.e.

revfag = revflag | (1 << 2)  

Or, as equivalent

revflag |= 0x04
This sets the bit. To clear the bit, you must use the "bitwise and" and the "bit inversion" operators:

revflag = revflag & ~(1 << 2)

or

revflag &= ~0x04

or

revflag &= 0xFB
 
Finally write back the REVERSE_FLAG register.

Please READ the documents I posted you before.
0
 

Author Closing Comment

by:raulapati
Comment Utility
this solution is some what help ful to solve my technical
issue
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now