Shop OBEX P1 Docs P2 Docs Learn Events
Requesting Guidance on Proper Way to Call rd_block(p_block, count, ackbit) in jm_i2c.spin2 — Parallax Forums

Requesting Guidance on Proper Way to Call rd_block(p_block, count, ackbit) in jm_i2c.spin2

@JonnyMac What's the proper way to use this call with an array of bytes? Currently, I'm trying the following:

obj
i2c : "jm_i2c"
var
byte data[30]

pub go()
...
i2c.rd_block(@data,7,i2c.NAK)
...

I just want to verify I'm call the method correctly.

Comments

  • Your syntax is correct. Are you having problems? I do my best to test code very carefully, but I am in fact human. :)
  • I doubt it's your code. I'm sure I'm doing something wrong. I'm trying to read multiple registers at a time from a RV-3028-C7 RTC. I can write to it fine and read each register independently but I've yet to have a successful multiple register read. I may need to set a mode or something. I'm getting all $FF's instead of the expected values 00,01,02,03,etc. As soon as I finish helping my brother from Lake Charles unload his truck, I'll get back to the fun stuff. If I can't get it by tonight I'll post some code. Thanks.
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-10-09 15:27
    I just finished coding the Click RTC 10 object a few days ago. This method lets you do that -- should be similar for your RTC.
    pub generic_read(r, p_buf, len)
    
    '' Read block of registers from RTC to p_buf
    '' -- r is first register
    '' -- p_buf is pointer to destination bytes
    '' -- len is number of registers to read
    
      i2c.start()
      i2c.write(DS3231M_WR)
      i2c.write(r)
      i2c.start()
      i2c.write(DS3231M_RD)
      i2c.rd_block(p_buf, len, i2c.NAK)
      i2c.stop()
    
    Remember... to do an I2C read you have to restart and send the slave address with bit 0 set to 1. I have these constants in my object.
      DS3231M_WR = %1101_000_0                                      ' i2c slave id
      DS3231M_RD = %1101_000_1
    
  • JonnyMacJonnyMac Posts: 9,104
    edited 2020-10-09 19:00
    A few minutes later...
    You should be able to write a routine like this for your RTC (Warning: not tested, not polished -- change magic #s to constants)
    pub get_unix_time() : unixtime
    
      i2c.start()
      i2c.write($A4)
      i2c.write($1B)
      i2c.start()
      i2c.write($A5)
      i2c.rd_block(@unixtime, 4, i2c.NAK)
      i2c.stop()
    
  • Your generic_read method made me realize, when I started using your rd_block call I hadn't added the i2c.start() and i2c.write(slaveReadAddress) calls before it. Stupid mistake on my part. This damn hurricane has me distracted. Everything is working as expected now. Thanks for the help.
  • Glad to help. I hope you and your family are safe from the storm.
Sign In or Register to comment.