physical computing with linux

27
Linux and Physical Computing

Upload: olimex-bulgaria

Post on 12-Jul-2015

4.873 views

Category:

Technology


0 download

TRANSCRIPT

Linux and Physical Computing

Linux and Physical Computing

$whoami

OLIMEX Ltd – established 23+ years ago

We love to do things better and to share how we do it!

Designs and produces: development boards, programmers, JTAGs, Linux computers, OSHW

Distributors in 50+ countries: Mouser, Digikey, Farnell, Conrad, Rapid...

Linux and Physical Computing

So what is Physical Computing?

Linux and Physical Computing

Classic computer configuration

Linux and Physical Computing

Physical computing means building interactive physical systems by the use of software and hardware that can sense and respond to the

analog world.

Linux and Physical Computing

Interractive LED table which sense when something is put on it and do animated images

Linux and Physical Computing

Sensors: -movement accelerometers, gyroscopes, GPS

- PIR, infrared fences, distance meters- Magnetometers

- Ultrasonic distance meters- Temperature, Pressure, Humidity

- fluids: LPG, CO2, Methane, Alcohol, pH- Light, Color, Camera

- Pulse, EKG, EEG, biofeedback- encoders, vibration detectors

Linux and Physical Computing

Actuators:

- Relays, LEDs- Addressable RGB LEDs

- Servo Motors- Stepper Motors

- Heaters

Linux and Physical Computing

To talk to each other two electronic devices must use same Interfaces

GPIO, I2C, SPI, UART are the most popular

Linux and Physical Computing

I2CSpeed: 100-400kHz (up to 5 Mhz rare)

Distance: up to 200-300 cm (depend on speed)One Master up to 128 Slaves on the bus

Only 2 lines for communicationUsually slow devices: ADC, DAC, sensors

Advantage: many can share same bus

Linux and Physical Computing

SPISpeed: up to 100 Mhz

Distance: up to 200-300 cm (depend on speed)Master - Slave

Usually 4 lines for communication: MISO, MOSI, CLK, (CS)If Master have to talk to more

than one Slave must have CS for each of themUsed by fast devices and sensors – image, video, sound

Linux and Physical Computing

UARTSpeed (baudrate): up to 1 Mhz

Distance: up to 200-300 cm (depend on speed)start bit – data – stop bit – (optional parity bit)

Transmitter → ReceiverOne of the oldest interface used now by GPS, GSM etc.

Linux and Physical Computing

GPIO

Logic 1 and 0 (3.3V and 0V)Switch something ON and OFF

Read button state: pressed / released

Linux and Physical Computing

OK How we control GPIO, I2C, SPI, UART interfaces from Linux OS?

All examples below are for A10-OLinuXino-LIME with Debian 7 Kernel 3.4.90+

Linux and Physical Computing

In Linux all hardware resources are build-in the file system and accessed with file read and write

operation. Including information for the processor, memory, GPIO etc.

Linux and Physical Computing

For instance if we want to see what is our CPU:

# cat /proc/cpuinfo

Processor : ARMv7 Processor rev 2 (v7l)BogoMIPS : 1001.88Features : swp half thumb fastmult vfp edsp neon vfpv3 tlsCPU implementer : 0x41CPU architecture: 7CPU variant : 0x3CPU part : 0xc08CPU revision : 2Hardware : sun4iRevision : 0000Serial : 0000000000000000

Linux and Physical Computing

We can change processor frequency by writing it in special file:

# echo 1008000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

# echo 408000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq

Linux and Physical Computing

How GPIO is controlled:

# cd /sys/class/gpio/ //special file# cat gpio73_pg9/direction //direction show if the GPIO is input or

// outputin# echo out > gpio73_pg9/direction //we can change it by writing in this file# cat gpio73_pg9/directionout# echo 1 > gpio73_pg9/value //also the value of the GPIO# cat gpio73_pg9/value // 1 = 3.3V, 0 = 0V1

Linux and Physical Computing

How SPI is controlled:

static const char *device = "/dev/spidev2.0"; //special filestatic uint8_t mode = 0; static uint8_t bits = 8;static uint32_t speed = 50000;int write_on = 6;int write_off = 5;

fd = open(device, O_RDWR); //open the file ioctl(fd, SPI_IOC_WR_MODE, &mode); //set some parameters ioctl(fd, SPI_IOC_RD_MODE, &mode); ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);

write(fd_cs, &write_on, 4); sleep(1); //write to SPI device read(fd, buff, 2); //read from SPI device

Linux and Physical Computing

How I2C is controlled:

int fd; uint8_t buffer_in[6]; uint8_t buffer_out[2]; const char *device = "/dev/i2c-2"; //special file uint8_t address = 0x52; //slave address

fd = open(device, O_RDWR); //open the special file

if(ioctl(fd, I2C_SLAVE, address) < 0) exit(1); // check if slave is present

if(write(fd, buffer_out, 2) < 0) exit(1); //write to I2C slave device

if(read(fd, buffer_in, 6) < 0) exit(1); //read from I2C slave device

Linux and Physical Computing

How UART is controlled:

# ls /dev/ttyS1/dev/ttyS1 //UART4 on UEXT from GPIO1 connector # echo 123 > /dev/ttyS0 //console UART1123

Linux and Physical Computing

Power Supply Managment:

A10-OLinuXino-LIME can be powered from three sources:

External 5V supplyUSB-OTG connector

LiPo battery

# cd /sys/class/power_supply/# lsac/ battery/ usb/

Linux and Physical Computing

LiPo rechargable Battery:# cd /sys/class/power_supply/battery/# lsadcfreq energy_full_design technology capacity health temp chgcsttimemin ihold type chgen iholden uevent chgendcur model_name vhold chgintmicrocur online vholden chgmicrovol power/ voltage_max_design chgpretimemin present voltage_min_design current_now status voltage_now device/ subsystem/

# cat power/autosuspend_delay_ms wakeup wakeup_expire_count control wakeup_abort_count wakeup_last_time_ms runtime_active_time wakeup_active wakeup_max_time_ms runtime_status wakeup_active_count wakeup_total_time_msruntime_suspended_time wakeup_count

# cat voltage_now4130000 // 4.13 V

# cat statusCharging //battery is charging

# cat current_now247000 // 0.247 A current

# cat temp300 // 30 C temperature

Linux and Physical Computing

External 5V PWR JACK:# cd /sys/class/power_supply/ac# lscurrent_now model_name power subsystem ueventdevice online present type voltage_now

# cat present1 //when external power is connected# cat present0 //when external power is disconnected

# cat voltage_now4839000 //4.84 V is present on power jack

# cat current_now552000 //0.552A consumption

Linux and Physical Computing

USB-OTG connector:# cd /sys/class/power_supply/usb# lscurrent_now model_name power subsystem ueventdevice online present type voltage_now

root@a10Lime:/sys/class/power_supply/usb# cat voltage_now4261000 //4.26V applied to USB-OTG connector

root@a10Lime:/sys/class/power_supply/usb# cat current_now275000 //0.275A power consumption from the USB-OTG source

root@a10Lime:/sys/class/power_supply/usb# cat present1 //USB-OTG is connected

root@a10Lime:/sys/class/power_supply/usb# cat present0 //USB-OTG is disconnected

Linux and Physical Computing

You can see lot of examples for working with sensors and actuators on GitHub:

https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE

Linux and Physical Computing

© 2014, OLIMEX LtdReleased under CC BY-SA 2.0