-2

I'm a beginner in c language.

I have a function which writes some specific value into a register:

  if(something == someCondition )
  {

    Write_into_register( Msg->R_addres, Msg->value );
  }

This works so far. I have also a read function, which just reads the value which is written into the register:

uint read_value_from_register( R_addres addr ){}

This works also fine.

Now I would like to have something like this:

  1. Each time the value is written to register, check if it is really the correct value written into register (with my read function)
  2. If the value is not written or it is not the same value, try 3 times to write the correct value
  3. If the 3rd attempt is not successful, raise an error

How to represent this algorithm and combine it with my existing functions? Can you represent it just as pseudo-code? Will be thankful!

Have tried something like this:

int index;
Write_into_register(addres, value );
for (index=0;index<3;i++)
if (value== read_value_from_register(addres))
   return 1;
else
   return ERROR;
JohnDoe
  • 825
  • 1
  • 13
  • 31
  • 1
    What are these registers? Why would writing fail and have to be verified? Typically registers are very low-level, and writing to them basically always works. The exception would be peripheral or I/O-related registers more commonly found in microcontrollers. You're not being very clear. – unwind Feb 05 '16 at 10:46
  • 1
    It looks trivial for any programmer who knows about loops and conditionals. What have you tried? Is there a specific problem with it? If you don't know how to use loops and conditionals, maybe you need some programming intro class first? – Ivan Aksamentov - Drop Feb 05 '16 at 10:48
  • These registers are within a Integrated Voltage Control Unit . These values are sent from an other component via the i2c bus and it's a safety relevant system. – JohnDoe Feb 05 '16 at 10:50
  • 1
    Show what you have tried, it's trivial. – Jabberwocky Feb 05 '16 at 10:52
  • Actually you almost have written the pseudocode yourself in your question. – Jabberwocky Feb 05 '16 at 10:59
  • @milexy86 it looks more or less correct, where is the problem ? – Jabberwocky Feb 05 '16 at 11:09
  • 1
    You should learn C++ before you start programming in it. C++ is different from Ruby and harder in many ways. Pick a book from here: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Feb 05 '16 at 11:51

1 Answers1

1

Asssuming that your data to be written is an integer.

int Write_verify( R_addres addr, int data)
{
    Write_into_register(addr, data );
    if (data == read_value_from_register(addr))
       return 1;
    else
       return 0;
}

This function will write and verify the data. It will return 1 if verified and 0, if not OK.

If you want to write the data three times, you have to modify the above code like this.

int Write_verify( R_addres addr, int data)
{
    Write_into_register(addr, data );
    for (int i=0; i<2; i++)
    {
      if (data == read_value_from_register(addr))
         return 1;
      else
         Write_into_register(addr, data );
    }

    // Check for last value
    if (data == read_value_from_register(addr))
       return 1;
    else
       return 0;
}
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31