In the Linux how to use one driver in another driver - linux-device-driver

I have a situation where I have led-gpio driver, which drives the GPIO line, and fec driver for ethernet.
On ethernet fec probe call, I need to set GPIOs and reset GPIO lines using led-gpios. Is this possible? Has any one came across?
I need a multiple byte read and write to GPIO driver which communicates via SPI, that should sending via fec driver.

Related

Will an I2C device get detected when it is connected to Raspberry Pi when there is no Driver and dts related to it?

I'm in confusion that will an I2C device get detected in raspberry pi even when there
are no device drivers and DTS files related to it.
Will it show up when we use this command
ls /dev/i2c-*
and are we able to detect its address when I try to probe using
i2cdetect -y bus_number
In short:
... when there are no device drivers and DTS files related to it.
Will it show up when we use this command
ls /dev/i2c-*
No. This command will list available I2C buses, not devices.
and are we able to detect its address when I try to probe using
i2cdetect -y bus_number
Maybe. In most cases yes.
A bit more elaborated:
Depending of what kind of I2C device it is, and what you want to do with it, you might still be able to communicate with it.
driver - best case
If you have relevant device tree change to describe this I2C device (on what bus it is located, its address, extra signals if needed - like interrupt pin, etc) and associated driver is present (built-in or as a module, check *_defconfig options in Linux kernel source) - driver should probe device during either boot or manual module loading.
Why best case? If you have a driver you don't have to think about protocols and programming, and, as an example, reading a value from ADC device might be as simple as:
root#pi:~# cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw
291
i2ctools
Another approach would be to use i2cget/i2cset tools from i2ctools package. No device tree changes needed. With these tools you can talk with any unclaimed I2C device on any enabled I2C bus in device tree.
You'll need to implement communication with I2C device by your own. From security and stability perspective - IMO this is the worst case to go, but is good for hardware debugging and, in some cases, initial bring-up.
Example is here.
Note regarding i2cdetect - this command tries to detect devices on particular bus, but gives no warranty. As per man i2cdetect:
As there is no standard I2C detection command, i2cdetect uses arbitrary SMBus commands (namely SMBus quick write and SMBus receive byte) to probe for devices.

Connect two raspberry Pis using USB cable or USB-serial

I'm working on a project where I need two raspberry pis to communicate and the ethernet port is not free, I'm not allowed to make any changes to the GPIO pins, and I'm forced to use the USB port due to hardware considerations. Is it possible for them to communicate using a direct USB cable, or perhaps using two USB-RS232 cables?
Thanks!
Siddharth
I would use the TTL serial pins on the GIO header. See https://elinux.org/images/1/13/Adafruit-connection.jpg
You could create your own simple null modem serial cable - consisting of 3 jumper cables.
Connect pins
6 <--> 6
8 <--> 10
10 <--> 8
If you can't use the GPIO header - you can do a USB to USB connection using USBNET. http://www.linux-usb.org/usbnet/
Is it possible for them to communicate using a direct USB cable, or perhaps using two USB-RS232 cables?
You fail to mention exactly which Raspberry Pi version(s) you are using.
Only the Raspberry PI Zero can be used as a USB Gadget.
Since USB is a master-slave(s) interface & protocol, you cannot simply connect two Raspberry PI 1/2/3 boards together using USB, because that would be a master-to-master connection.
If you look hard enough for a (passive) USB Type A (male) to Type A (male) cable, you can find them, but it's a bogus connection that will not work.
There are active USB host-to-host cables (which contain a shared gadget), but support can be an issue.
You could connect a Raspberry PI 1/2/3 to a Raspberry PI Zero by USB, so long as the Zero's USB port was configured as a USB ACM CDC gadget.
However one simple solution is your alternative of installing USB-to-RS232 adapters to each board. A null-modem cable of three wires would suffice unless you needed hardware flow-control.
An alternative solution is installing USB-to-Ethernet adapters to each board (with static IP addresses, i.e. an ad-hoc connection). This approach provides a much faster connection than an RS-232 link, and is easily utilized by applications.

SPI Reset on Raspberry Pi after GPIO usage

I found out when I used GPIO Pins which are also SPI Pins (GPIO8,9,10,11) and clean them up. I can not reuse this Pins for an SPI connection.
I have to restart the Raspberry Pi first to use SPI again.
Do you have an idea how I can reset this Pins for SPI usage without restart?
Regards
use the command lsmod to get the name of your spi module and simply remove it as follows (assuming spi_bcm2708 is module name),
rmmod spi_bcm2708
then reload it as follows,
modprobe spi_bcm2708
try with sudo if doesn't work.

Communicate over UART in linux driver

I need to implement a linux driver that communicates to a MCU over UART but since we already have a UART driver I am wondering how can I have a driver that communicates through the UART driver?
This seems to be common to do with I2C but I can't find any information about anyone doing it with UART.
/Stefan

Serial communication with Raspberry pi

How can we do serial communication using the raspberry pi with a python script to send/receive data to my laptop? If yes can we use the RS232 cable to connect to the TX/RX ports on the raspberry pi directly? If yes, what pins must be used from the RS232 cable? It would be help full if anyone can post a example python script?
Since my first aim is to send/receive data to the laptop to/from the raspberry pi, i'm using a RS232 to USB cable at both end to connect to the laptop as well as the raspberry pi.
RS-232/RS-485 to PC and USB to PC
For RS-232
1) Download Putty.
2) Buy a Serial Port RS232 to TTL Converter Module and a RS-232 serial cable for PC.
3) Follow the steps in Connection to a microcontroller or other peripheral on this
link. Actually read the whole thing for better understanding.
4) Power your converter module with either 3.3V (pin 1) or 5.0V (pin 2), connect Rxd pin of the module to Rxd (pin 8) on Rpi and Txd pin to Txd (pin 10) on Rpi.
5) Connect your RS-232 (from PC) cable to the converter module
Now you are ready to do some coding in Python. But before that make sure that you have the library called serial for python to create the communication. You can easliy get it via terminal by typing sudo apt-get install python-serial. Also you will see the baudrate in the code is 7200. It could be less or more depending on the synchronization. Also make sure that baudrate should be same in putty and COM1, which is the port that RS-232 is connected to your PC. You can check and set it from device manager in Windows. By the way, timeout is the time gap between each message you are receiving.
In case you can't run the code from idle (that happens for some libraries), do it in terminal. For that go to the folder where you keep your python code and type python name.py.
import serial
import time
def readlineCR(port):
rv = ""
while True:
ch = port.read()
rv += ch
if ch == '\r' or ch == '':
return rv
port = serial.Serial("/dev/ttyAMA0", baudrate = 7200, timeout = 2)
while True:
rcv = readlıneCR(port)
port.write("I typed: " + repr(rcv))
print(rcv)
For USB serial to PC
You have two options
First, you can buy a USB dongle for RS-232/RS-485 so that you would not use GPIO pins. But it is better to get a USB hub for all mouse, keyboard and dongle.
Second and easier, you can buy a FTDI USB to TTL converter and use GPIOs to have a serial communication with Rpi. The code for this is exactly the same the one with above. Connection for this is easy.
Module -- Rpi
Txd -- > Txd
Rxd -- > Rxd
Gnd ---> Gnd
Are you trying to issue commands to the Raspberry Pi? (like a console?) I would suggest a plain jane 3.3V FTDI cable. You can get them from Digikey or Sparkfun. Note: the Raspberry Pi runs on 3.3V so you must be sure that anything you connect to it is running 3.3V or has a level shifter. See more information about level shifters here. (go down to the Logic voltage levels section)
First, you need to make sure that Raspbian has released the serial console. You can do that with the script located here.
If you're more interested with communicating with other devices then maybe the following suits you:
Do you have an Arduino? You can run a simple test by putting this sketch on your Arduino:
#define SERIAL_BAUD 115200
void setup() {
//Init serial connection
Serial.begin(SERIAL_BAUD);
Serial.setTimeout(1);
}
void loop() {
if ( Serial.available() ) {
byte type = Serial.read();
Serial.write(type);
}
}
And wire it up using the following diagram:
Note: make sure you don't connect a usb cable to the Arduino. It will be powered by the Raspberry Pi.
You can then install and run screen. Screen is a dead simple way of connecting to a serial port.
apt-get install screen
Then run the following:
screen /dev/ttyAMA0 115200
The screen will show up blank. But, when you start typing you will notice that the characters you're writing are getting looped back to your terminal.
Note: If the screen is still blank you should double check the connections (power led on the Arduino is a good thing to check).
When in doubt you can see my whole example here.
The Raspberry Pi's serial port uses 3.3v logic; RS232 uses 12v, so a level shifter would be needed to use those pins so you cannot use RS232 directly.
Serial interfacing in Python using the PySerial module (http://pyserial.sourceforge.net) it is pretty straightforward to send and recieve data. There are examples in the documentation, but essentially to send data:
import serial
port = serial.Serial(portname, baudrate, timeout)
port.write("message to send")
port.close()
It depends what you're doing with the data and what sort of data you're recieving to decide the best way to recieve data, but a very simple example:
import serial
port = serial.Serial(portname, baudrate, timeout)
data = port.read(numberofbytes)
print data
This simply waits until the number of bytes specified has been retrieved or the timeout value is reached.
You can use port.inWaiting() to return how many bytes are currently in the buffer.
you should install python library for serial drivers. you can not use rs232 directly, instead you can use a max232 chip in between rx(gpio15) tx(gpio 14) pins and your usb to serial converter. or you can use usb to ttl serial cable from adafruit. here is the link for setup : http://learn.adafruit.com/adafruits-raspberry-pi-lesson-5-using-a-console-cable/overview
Serial Communication in Raspberry pi.
There are plenty of options for serial communication
Use Visual GDB Plugin in visual studio and deploy code remotely in raspberry pi
and loop back the txd and rxd pins and check if the send message is received or not.
If you are familiar with java Install pi4j and in the example folder there is a sample program , compile and run using terminal. and check the output.
if you are connecting using RS232 and Max232 , please note that uses 3.3 volt not 5 or 12v. , it may burn your board.