Softwire question

Working libraries, libraries being ported and related hardware
Post Reply
profdc9
Posts: 13
Joined: Mon Mar 25, 2019 1:01 am
OS: Linux & Windows 10
IDE: Arduino
Core: Roger & Official
Board: Bluepill mostly

Softwire question

Post by profdc9 » Mon Dec 02, 2019 5:03 am

So I am using Softwire because I can't get the HardWire functionality to be reliable. I have been reading about the notorious errata for the I2C peripheral on STM32F1. But Softwire seems to work. But I have a question about how its implemented...

Everything in this library seems to use the pinmaps for higher speed, for example looking in SoftWire.cpp

Code: Select all

void SoftWire::begin(uint8 self_addr) {
    tx_buf_idx = 0;
    tx_buf_overflow = false;
    rx_buf_idx = 0;
    rx_buf_len = 0;
    pinMode(this->scl_pin, OUTPUT_OPEN_DRAIN);
    pinMode(this->sda_pin, OUTPUT_OPEN_DRAIN);
	
	sclDevice = PIN_MAP[this->scl_pin].gpio_device;
	sclBit = PIN_MAP[this->scl_pin].gpio_bit;	
	sdaDevice = PIN_MAP[this->sda_pin].gpio_device;
	sdaBit = PIN_MAP[this->sda_pin].gpio_bit;		
    set_scl(HIGH);
    set_sda(HIGH);
}
But if you look at this function, it uses digitalRead

Code: Select all

uint8 SoftWire::i2c_shift_in() {
    uint8 data = 0;
    set_sda(HIGH);

    int i;
    for (i = 0; i < 8; i++) {
        set_scl(HIGH);
        data |= digitalRead(this->sda_pin) << (7-i);
        set_scl(LOW);
    }

    return data;
}
This is just weird. Why isn't the instruction this way?

Code: Select all

     data |= gpio_read_bit(sdaDevice, sdaBit) ? (1 << (7-i)) : 0;
I just think it's weird that gpio_write_bit is used but not gpio_read_bit.

Dan

stevestrong
Posts: 81
Joined: Tue Mar 05, 2019 7:49 am
Location: Munich
OS: Win7 & 10
IDE: Arduino 1.8.8, Sloeber
Core: Libmaple
Board: Bluepill, Generic F4VET6 black & mini
Contact:

Re: Softwire question

Post by stevestrong » Mon Dec 02, 2019 9:03 am

Please open an issue on Github, and eventually also a pull request.

profdc9
Posts: 13
Joined: Mon Mar 25, 2019 1:01 am
OS: Linux & Windows 10
IDE: Arduino
Core: Roger & Official
Board: Bluepill mostly

Re: Softwire question

Post by profdc9 » Mon Dec 02, 2019 1:44 pm

Ok I submitted an issue.

Post Reply