Solved

how to set a bit of register in linux

Posted on 2010-11-09
18
475 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
ID: 34106977
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
ID: 34109636
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
ID: 34109654
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:raulapati
ID: 34109855
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
ID: 34109863
So the register is an i2c DEVICE register ?
 
0
 

Author Comment

by:raulapati
ID: 34109881
spi device register
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34109918
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
ID: 34109924
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
ID: 34135984
i have solved this question
0
 

Author Comment

by:raulapati
ID: 34142537
still i have not solved this problem
0
 

Author Comment

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

Expert Comment

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

Expert Comment

by:Kendor
ID: 34144238
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
ID: 34154409
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
ID: 34154985
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
ID: 34161585
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
ID: 34162337
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
ID: 34170847
this solution is some what help ful to solve my technical
issue
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HP ProBook 4540S 2 3,894
AMD FX-9590 Vishera Black Edition- Number of PCI Lanes 6 3,248
Microsoft Surface 2 Fails to Boot 4 745
How to communicate multiple RS485 over TCP/IP network. 3 106
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This problem is more common than not and I will show you some things to check to solve this problem.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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