-2
\$\begingroup\$
u8 i2c_read_register(u8 mem_address, u8 *data_read) { u8 temp = 0; u8 ack = 0; u8 device_address = (IQS_ADDR << 1); u8 polling_attempt = 0; // send address ack = i2c_send_byte(device_address); #ifdef POLLING while ((ack) && (polling_attempt < POLLING_ATTEMPTS)) { i2c_wait(); i2c_wait(); i2c_start(); ack = i2c_send_byte(device_address); polling_attempt++; } #endif if (!ack) { i2c_send_byte(mem_address); i2c_repeat_start(); device_address = (IQS_ADDR << 1) | 0x01; ack = i2c_send_byte(device_address); temp = i2c_read_byte(1); (*data_read) = temp; } return ack; } 
  • Notes In order to read two Byes I need to call the function Like This:

    i2c_read_register(DEBUGGING_EVENTS, &data_buffer[1], 2);

With (2) representing the Number of Bytes. But as the function stand it only takes two arguments not three.

\$\endgroup\$
9
  • 4
    \$\begingroup\$ Have you considered calling i2c_read_byte() twice? \$\endgroup\$ Commented Aug 12, 2015 at 11:20
  • \$\begingroup\$ @EJP Yes, but its not working accordingly \$\endgroup\$ Commented Aug 12, 2015 at 11:22
  • \$\begingroup\$ The Memory Address that I'm trying to read always return two bytes, First byte for Events, and 2nd Bytes for movement, so I have to specify reading the two bytes in my function. \$\endgroup\$ Commented Aug 12, 2015 at 11:26
  • 1
    \$\begingroup\$ As suggested - call i2c_read_byte() with the two different addresses you are trying to read from. If this doesn't work, it implies that the device is expecting a multi-byte read and the data is not valid if this is not the case (you should already have seen this in the datasheet). In this case, try Googling "i2c multi-byte read" and "i2c repeated start condition". This info is normally included as a simple timing diagram in the datasheet too. \$\endgroup\$ Commented Aug 12, 2015 at 11:40
  • 2
    \$\begingroup\$ OK - so this is really about STM8 multi-byte reads. I haven't worked with STM8 parts before, but a quick Google on this brought up ST application note AN3281. That looks worth a read - but the fact I found something that quickly suggests you have not done much research on this. \$\endgroup\$ Commented Aug 12, 2015 at 12:07

2 Answers 2

1
\$\begingroup\$

In the software package for the stm8 Eval board is an example project which is using a serial eeprom which is able to read multiple bytes.

sEE example software

\$\endgroup\$
1
  • \$\begingroup\$ Yes I do I have the file, as you may have noticed I already Have a function that was working but only for reading a single byte. So I only wanted to modify it to read two Bytes instead of writing a whole new Function. \$\endgroup\$ Commented Aug 13, 2015 at 6:57
0
\$\begingroup\$

After Hours of Trying to modify the Code I finally managed to get the Specified two Bytes from my i2c_read_register() Function.

u8 i2c_read_register(u8 register_address, u8 *data_read, u8 NumbOfBytes) { u8 i; u8 temp = 0; u8 ack = 0; u8 device_address = (IQS_ADDR << 1); u8 polling_attempt = 0; // send address ack = i2c_send_byte(device_address); #ifdef POLLING while ((ack) && (polling_attempt < POLLING_ATTEMPTS)) { i2c_wait(); i2c_wait(); i2c_start(); ack = i2c_send_byte(device_address); polling_attempt++; } #endif if (!ack) { i2c_send_byte(register_address); i2c_repeat_start(); device_address = (IQS_ADDR << 1) | 0x01; ack = i2c_send_byte(device_address); //temp = i2c_read_byte(1); //(*data_read) = temp; if(NumbOfBytes > 1) { for(i = 0; i < NumbOfBytes -1; i++) data_read[i] = i2c_read_byte(0); } data_read[i] = i2c_read_byte(1); } return ack; } 

All That was needed, was to add a for Loop Incrementing the Number of Bytes to read as input by user with the u8 NumbOfBytes variable.

And you call the function Like This:

i2c_start(); i2c_read_register(DEBUGGING_EVENTS, &data_buffer[0], 2); i2c_stop(); events = data_buffer[0]; // Main Events 1st Byte movement = data_buffer[1]; // Debugging Events 2nd Byte 
\$\endgroup\$
2
  • \$\begingroup\$ You might want to flag your own answer as correct, so that others know that this is now solved. \$\endgroup\$ Commented Aug 12, 2015 at 20:06
  • \$\begingroup\$ @ stefandz, Yes The u=rules says u have to wait for 48 Hours to Flag your own answer as solution. \$\endgroup\$ Commented Aug 13, 2015 at 6:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.