FTDI I2C libMPSSE - c

I'm working on reading flow rates from a sensrion lg16 flow rate sensor using a USB FTDI I2C dongle. I have all the wiring done and I am able to write to it and verify I am able to write to it correctly using an oscilloscope with an I2C analyzer. My problem is when i try to do a read from the sensor, even when reading to a register after writing to it i dont get missing Acks on the I2C bus.
here my code to read the register i want to see at the moment
bytesToTransfer=0;
bytesTransfered=0;
buffer[bytesToTransfer++]=0xE5;
//buffer[bytesToTransfer++]= 0x7f;
status = I2C_DeviceWrite(ftHandle, slaveAddress, bytesToTransfer, buffer, \
&bytesTransfered, I2C_TRANSFER_OPTIONS_START_BIT|I2C_TRANSFER_OPTIONS_STOP_BIT | I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES);
printf("bytestxd=%d\n",bytesTransfered);
printf("status=%d\n",status);
APP_CHECK_STATUS(status);

Related

Raspberry Pi - UART - disable TX and enable RX in program C

I need connect my raspberry pi 4 model b with a servo via UART, but it is possible only via 1 wire. That means I must connect pin TX and RX together. In order to do so, I must have a way how to manually disable only TX or RX in my C program.
I am able to easily disable RX thanks to termios.h library, but I didn't find any way how to disable TX.
I was trying to disable it through this
tcflow(fd_myUART, TCOOFF); // it should suspend output
But that didn't work, so I thought that maybe if I change the pin of TX to INPUT, it will change the pin from UART to GPIO, but that didn't work either.
Do you have a way, how to do that, please?
First of all, just "randomly" connecting both wires is a bad idea.
Below image shows how to do it better for a prototype.
Slave devices are able to pull the IO line low during a read bit or a reset while the TX signal is high.
When used in this configuration, you should not disable RX nor TX. You can use "normal" UART operation.
More information can be found here (maxim integrated tutorial 214 "USING A UART TO IMPLEMENT A 1-WIRE BUS MASTER")
Since you will have a lot of connected slave, you should consider using a dedicated chip:
I use a DS2482S-100 over I2C.

Is there a way to increase i2c read in linux

I have an LSM6DS3 motion sensor connected to the i2c bus of a Qualcomm processor running Linux OS. I have been trying to read the FIFO buffer of the sensor every 20s. It takes around 2 to 3 seconds to read all the data from the sensor FIFO buffer(The FIFO buffer of the sensor is exposed through LSM6DS3_FIFO_DATA_OUT_L and LSM6DS3_FIFO_DATA_OUT_H registers in the sensor).Im using i2c_smbus_read_word_data(int file, __u8 command) APIs to read.In order to reduce this time delay I have set my i2c master clock to 400KHz and tried IOCTLs instead of above APIs but none of the workaround worked.The same sensor I connected to Arduino and tried the same read operation. It took only 1.4s to read the data.Is there any way to increase the i2c read speed in Linux?.

ESP32 Analog pin not reading when bluetooth is connected

I have been using ESP32 and writing code in Arduino. However, when I enable Bluetooth, GPIO4 and GPIO15 does not work to read analog inputs. I have connected IR LED's on both pins and reading analog signals. If Bluetooth code is not used, ESP32 is reading the analog signals and displaying it on serial monitor. If the below Bluetooth code is used, the reading is shown as 255 on both pins (reading 5v. Yes, they are 5v for now and will be level shifted to 3.3v).
Can someone please check and suggest a solution?
I have switched the sensors, removed them and whatever I do, the reading is the same when Bluetooth is read
Bluetooth code:
void init_bluetooth() {
ESP_BT.begin("EKA Robot"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
}
IR code:
// Read Infrared LED on GPIO4. Similar function exists to read GPIO15
int readIR() {
int sensorValue = analogRead(oaPinL);
delay(5);
sensorValue = map(sensorValue, 0, 4095, 0, 255);
return (sensorValue);
}
I expect to read the sensor values. However, all it shows is 255. Does it mean it is somehow pulled high?
There are two A/D converters in the ESP32 chip, ADC1 and ADC2. ADC2 pins can not be used when Wi-Fi or Bluetooth is used. Pins connected to ADC1 do work. These are GPIO32-36 and GPIO39.

How does I2C Protocol work

I have visited on some links and looked for some example programs for I2C programming. I want to write my own code for I2C protocol. Suppose DS1307 RTC and LCD connected to 8051. I am using Keil software to write a C program. It's very difficult to write whole program of I2C for me, so I tried to break program in small parts:
Module 1: define and set pins for LCD and DS1307 RTC
Module 2: write C code for DS1307 (make functions for DS1307 such as read, write)
Module 3: write C code for LCD (data, command initialize, etc)
Module 4: main function
I understand module 1 but I am looking help to understand module 2. So again I want break module 2 in small parts.
How to break module 2 in small parts for easy understanding? How many functions should be in module2?
The Module 2 is essentially I2C driver using bit banging of 8051 port. I2C protocol follows a sequence. It is started by start sequence and stopped by stop sequence. You can have different functions. The communication is started by the master and each slave has an address. So in module2, you will write all below functions.
For example, the I2 read sequence will be following
I2C_Start(); // set I2C start sequence
I2C_Send(slave_address|1); Send I2C slave address in read mode
I2C_read_ACK(); //master will know slave got data
while(number_of bytes){
I2C_Read();
I2C_send_ACK();
number_of bytes--;
}
I2C_NAK(); //slave need to know so it will not prepare next data.
I2C_Stop(); //stop communication
Again the write on slave will have below steps
I2C_Start(); // set I2C start sequence
I2C_Send(slave_address); Send I2C slave address in write mode
I2C_read_ACK(); //master will know slave got data
while(number_of bytes){
I2C_Write();
I2C_read_ACK(); //master will know slave got data
number_of bytes--;
}
I2C_Stop(); //stop communication
I also see driver at
https://circuitdigest.com/microcontroller-projects/digital-clock-using-8051-microcontroller
Official I2C protocol is here
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwici4Ocn6jVAhUIwlQKHV_zAJ8QFggoMAA&url=https%3A%2F%2Fwww.nxp.com%2Fdocuments%2Fuser_manual%2FUM10204.pdf&usg=AFQjCNHgNi6wOD4MjIDsnT0DXTYLS_-gPQ

Raspberry Pi can't send data to PC through serial communication

I am trying to send/receive data over the serial connection (GPIO UART pins) between a Raspberry Pi 2(raspian wheezy) and an STM32F4 board. I am using the sample code in the link: http://www.raspberry-projects.com/pi/programming-in-c/uart-serial-port/using-the-uart.
It works when I connect the TX and RX pins on the board together. However, when I connect RPI to my laptop by module USB-TTL PL2303 and use hyper terminal to see the result, the received characters are garbage characters. I don't understand why. Is there anything I missed?
Could you give me some advice I could look for, please? Thank you!
Most likely you are using incorrect baud rate. It should be 115200 (115200-8-N-1) (you can use else but then you need to set both endpoints to the same baud). Check the baud rate of the serial connection using stty
stty -F /dev/ttyX
or setserial. In case of baud rate error you can try what authors say:
Try using a slower BAUD rate (or a single 0xFF byte which only has the
start bit low) and see if it works. We had a problem using 115k2 baud
rate where our microcontroller communicating with the RPi could hit
113636baud or 119047baud. 113636baud had the lowest error margin so
we used it and TX from the RPi being received by the microcontroller
worked fine. However when transmitting to the RPi nothing was ever
received. Changing the microcontroller to use 119047baud caused RX to
work.

Resources