bwack / C64-Saver-bwack

Other
97 stars 14 forks source link

Code locks up when using the ssd1306 and ina216 libraries together #15

Closed bwack closed 5 years ago

bwack commented 5 years ago

@discoHR From what I remember when debugging this I saw that after the sd1306 library had the i2c and the ina216 was taking over, the code got stuck in a wait for high or wait for low call.. The libraries works fine on their own. Find out why one module screws up for the other.

Btw this is why I have included the usi_i2c_master library. This library is from the nunchuk64 project. From what I saw, the sd1306 library is a bitbanging library while the other two use the TWI module.

bwack commented 5 years ago

I think it got stuck here in the usi_i2c_master library

    while (!(PIN_USI & (1<<PIN_USI_SCL)));      //Wait for SCL to go high

    #ifdef I2C_FAST_MODE
        USI_I2C_WAIT_HIGH();
    #else
USI_I2C_WAIT_LOW();

sorry too long ago I don't remember.. I suspect the ssd1306 library, or the way we use it. maybe it needs to be freed or something. At least the i2c_master expects a state that it never gets. Could be a state set in the TWI register. Not sure.

Back when I tested this I had my scope up and I could see the I/Os got stuck.

discoHR commented 5 years ago

It's not the code, it's INA. I removed both usi_i2c_master and tinusaur-ssd1306xle code, disabled all interrupts and wrote my own I2C bitbanging code including autodetecting INA's slave address. There is aboslutely no response from INA at any slave address, it's not sending ACK at all. Thought it was working earlier but I was mistaking SDA going up for an ACK (technically, it's a NAK and it was going up because of the pullup, INA had nothing to do with it).

There is a lot of garbage on I2C bus immediately after power-up. Could be it's messing up INA's I2C but I added procedure for resetting INA's I2C bus before trying to talk to INA, same problem. INA's bus is reset when SDA or SCL are pulled low for at least 28ms.

Replaced R4 with 0 ohm resistor, same problem.

Checked optoisolator, signals seem to pass to both ends. There is about 4.2V on one end and 5V on the other but that shouldn't matter.

Removed optoisolator and tried accessing INA directly, same problem.

Either INA you sent me is dead or it's I2C doesn't work without a load. There is only 2.45 mV on the shunt resistor while the red LED is on, if it matters.

Ordered 2x INA219 from an EU seller, it will be faster than ordering from China.

Some screenshots...

I2C bus garbage on boot:

i2c-boot-garbage

Autodetection of INA's slave address, no ACK was ever received:

ina-autodetect-fail

Same as above but zoomed in and after improved ACK/NAK timing in the code. INA is not sending ACK. Pullup resistor is doing NAK, INA must pull it low for ACK.

ina-ack-fail

Further I2C timing improvements with releasing SDA earlier and proper termination of INA's reset sequence. Every xfer start has a green dot and every xfer stop has a red dot. 5 us minimum per high/low and still no sign of life from INA.

further-i2c-timing-improvements
bwack commented 5 years ago

It could be both the ina and the issue I descibed above. Just beware of that.

bwack commented 5 years ago

maybe the voltage on the ina's VDD is too low because of the drop-out voltage in the 5V regulator driven at V_IN=5V. Edit: just checked. recommended is 3.3V, and absolute max is 6V. No problem there then.

bwack commented 5 years ago

Great work btw. Tell you what. I will hook up the addon-board to my c64 DIY PSU with ina219 voltage meter. I can easily unscrew the cables going to the ina and connect the addonboard and see if I get read-out on the PSU display (if you know, I have a C64 DIY PSU with ina219 and arduino pro micro)

bwack commented 5 years ago

Here are the I2C data recorded from my C64 DIY PSU with only INA219 on the I2C bus. The data rate is 100kHz. I have used the adafruits ina219 library. You can see how it at very first writes calibration bits, and it does that several times too. Second transfer is the configuration h00. No reset command (that would be most significant bit set to h00.

recorded from power on:

Start, h80 [ h40 | WR ], h05, h10, h00, Stop     set calibration register
Start, h80 [ h40 | WR ], h00, h3C, h1F, Stop     set config register
Start, h80 [ h40 | WR ], h02, Stop               request bus voltage register
Start, h81 [ h40 | RD ], h27, hD2 NAK, Stop
Start, h80 [ h40 | WR ], h05, h10, h00, Stop     another set config register
Start, h80 [ h40 | WR ], h04, Stop               request current register
Start, h81 [ h40 | RD ], h02, h9C NAK, Stop
Start, h80 [ h40 | WR ], h02, Stop
Start, h81 [ h40 | RD ], h27, hCA NAK, Stop
Start, h80 [ h40 | WR ], h05, h10, h00, Stop
Start, h80 [ h40 | WR ], h04, Stop
Start, h81 [ h40 | RD ], h02, h5D NAK, Stop
Start, h80 [ h40 | WR ], h02, Stop
bwack commented 5 years ago

This is from with your code. I have replaced the pullups on my board now. oled disconnected.


Start, h80 [ h40 | WR ] NAK, h05 NAK, h10 NAK, h00 NAK, Stop
Error
Error
Start, h80 [ h40 | WR ] NAK, h00 NAK, h3C NAK, h1F NAK, Stop
Error
Error
Start, h80 [ h40 | WR ] NAK, h01 NAK, Stop
Error
Error
Start, h81 [ h40 | RD ] NAK, hFF, hFF NAK, Stop
Error
Error
Start, h80 [ h40 | WR ] NAK, h02 NAK, Stop
Error
Error
Start, h81 [ h40 | RD ] NAK, hFF, hFF NAK, Stop
Error
discoHR commented 5 years ago

I tried with TinyWireM for Arduino, same problem. It's highly unlikely that both codes are faulty. The problem is most likely on the addon board itself. I've replaced 3 INAs so far and none of them sent any ACKs ever no matter what code I use. Please fix the board.

screenshot 2018-11-03 at 13 02 51 screenshot 2018-11-03 at 13 03 20
bwack commented 5 years ago

let me try this first.

bwack commented 5 years ago

SDA and SCL are mixed up.

bwack commented 5 years ago

Regarding the title. The two libraries ssd1306 and discoHR's ina219 go well together. Example shown in the hans-doodle branch. https://github.com/bwack/C64-Saver-bwack/tree/hans-doodle/addon%20board/src

bwack commented 5 years ago

I'm closing this issue. Open a new one if you encounter a new problem.