Programming Single Board Computers and Embedded PC

The electronic forum deals with the topics related to analog and digital circuits and systems (i.e. ASIC, FPGA, DSPs, Microcontroller, Single/Multi Processors etc) and their programming such as HDL, C/C++, etc.
Forum rules
The electronic forum deals with the topics related to analog and digital circuits and systems (i.e. ASIC, FPGA, DSPs, Microcontroller, Single/Multi Processors, PCBs etc) and their programming such as HDL, C/C++, etc.

Programming Single Board Computers and Embedded PC

Unread postby UCERD.COM » Sun Nov 02, 2014 3:17 pm

Some Video Lectures about Programming.
What is Programming : 
Watch on facebook.com

This Course uses Single Board Computers (Embedded PC) environment to execute different applications.
Click here to see details of Single Board Computer Systems

Raspberry Pi (RPi)
Download latest version of Broadcom BCM 2835 chip, which allows access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices.
* # download the latest version of the library, say bcm2835-1.xx.tar.gz, then:
* tar zxvf bcm2835-1.xx.tar.gz
* cd bcm2835-1.xx
* ./configure
* make
* sudo make check
* sudo make install
*

Blink : 
Code: Select all
Blinks Raspberry Pi GPIO pin 11 on and off
// blink.c
//
// Example program for bcm2835 library
// Blinks a pin on an off every 0.5 secs
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o blink blink.c -l bcm2835
// sudo ./blink
//
// Or you can test it before installing with:
// gcc -o blink -I ../../src ../../src/bcm2835.c blink.c
// sudo ./blink
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
// Blinks on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define PIN RPI_GPIO_P1_11
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
// Set the pin to be an output
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
// Blink
while (1)
{
// Turn it on
bcm2835_gpio_write(PIN, HIGH);
// wait a bit
bcm2835_delay(500);
// turn it off
bcm2835_gpio_write(PIN, LOW);
// wait a bit
bcm2835_delay(500);
}
bcm2835_close();
return 0;
}

Event : 
Code: Select all
// event.c
//
// Example program for bcm2835 library
// Event detection of an input pin
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o event event.c -l bcm2835
// sudo ./event
//
// Or you can test it before installing with:
// gcc -o event -I ../../src ../../src/bcm2835.c event.c
// sudo ./event
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
// Input on RPi pin GPIO 15
#define PIN RPI_GPIO_P1_15
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
// Set RPI pin P1-15 to be an input
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
// with a pullup
bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
// And a low detect enable
bcm2835_gpio_len(PIN);
while (1)
{
if (bcm2835_gpio_eds(PIN))
{
// Now clear the eds flag by setting it to 1
bcm2835_gpio_set_eds(PIN);
printf("low event detect for pin 15\n");
}
// wait a bit
delay(500);
}
bcm2835_close();
return 0;
}

Input : 
Code: Select all
// input.c
//
// Example program for bcm2835 library
// Reads and prints the state of an input pin
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o input input.c -l bcm2835
// sudo ./input
//
// Or you can test it before installing with:
// gcc -o input -I ../../src ../../src/bcm2835.c input.c
// sudo ./input
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
// Input on RPi pin GPIO 15
#define PIN RPI_GPIO_P1_15
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
// Set RPI pin P1-15 to be an input
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
// with a pullup
bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
// Blink
while (1)
{
// Read some data
uint8_t value = bcm2835_gpio_lev(PIN);
printf("read from pin 15: %d\n", value);
// wait a bit
delay(500);
}
bcm2835_close();
return 0;
}

I2C : 
Code: Select all
/*******************************************************************************
*
* i2c.c
*
*
* Description:
* i2c is a command-line utility for executing i2c commands with the
* Broadcom bcm2835. It was developed and tested on a Raspberry Pi single-board
* computer model B.
* Invoking spincl results in a read or write I2C transfer. Options include the
* the I2C clock frequency, read/write, address, and port initialization/closing
* procedures. The command usage and command-line parameters are described below
* in the showusage function, which prints the usage if no command-line parameters
* are included or if there are any command-line parameter errors. Invoking i2c
* requires root privilege.
*
* This file contains the main function as well as functions for displaying
* usage and for parsing the command line.
*
* Open Source Licensing GNU GPLv3
*
* Building:
* After installing bcm2835, you can build this
* with something like:
* gcc -o i2c i2c.c -l bcm2835
* sudo ./i2c
*
* Or you can test it before installing with:
* gcc -o i2c -I ../../src ../../src/bcm2835.c i2c.c
* sudo ./i2c
*
* History:
* 11/05 VERSION 1.0.0: Original
*
* User input parsing (comparse) and showusage\
* have been adapted from: http://ipsolutionscorp.com/raspberry-pi-spi-utility/
* mostly to keep consistence with the spincl tool usage.
*
* Compile with: gcc -o i2c i2c.c bcm2835.c
*
* Examples:
*
* Set up ADC (Arduino: ADC1015)
* sudo ./i2c -s72 -dw -ib 3 0x01 0x44 0x00 (select config register, setup mux, etc.)
* sudo ./i2c -s72 -dw -ib 1 0x00 (select ADC data register)
*
* Bias DAC (Arduino: MCP4725) at some voltage
* sudo ./i2c -s99 -dw -ib 3 0x60 0x7F 0xF0 (FS output is with 0xFF 0xF0)
* Read ADC convergence result
* sudo ./i2c -s72 -dr -ib 2 (FS output is 0x7FF0 with PGA1 = 1)
*
* In a DAC to ADC loop back typical results are:
*
* DAC VOUT ADC
* 7FFh 1.6V 677h Note ratio is FS_ADC*PGA_GAIN/FS_DAC = 4.096/3.3 = 1.23
* 5FFh 1.2V 4DCh
* 8F0h 1.8V 745h
* 9D0h 2V 7EAh
* 000h 10mV 004h
*
********************************************************************************/
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define MODE_READ 0
#define MODE_WRITE 1
#define MAX_LEN 32
char wbuf[MAX_LEN];
typedef enum {
NO_ACTION,
I2C_BEGIN,
I2C_END
} i2c_init;
uint8_t init = NO_ACTION;
uint16_t clk_div = BCM2835_I2C_CLOCK_DIVIDER_148;
uint8_t slave_address = 0x00;
uint32_t len = 0;
uint8_t mode = MODE_READ;
//*******************************************************************************
// comparse: Parse the command line and return EXIT_SUCCESS or EXIT_FAILURE
// argc: number of command-line arguments
// argv: array of command-line argument strings
//*******************************************************************************
int comparse(int argc, char **argv) {
int argnum, i, xmitnum;
if (argc < 2) { // must have at least program name and len arguments
// or -ie (I2C_END) or -ib (I2C_BEGIN)
fprintf(stderr, "Insufficient command line arguments\n");
return EXIT_FAILURE;
}
argnum = 1;
while (argnum < argc && argv[argnum][0] == '-') {
switch (argv[argnum][1]) {
case 'i': // I2C init
switch (argv[argnum][2]) {
case 'b': init = I2C_BEGIN; break;
case 'e': init = I2C_END; break;
default:
fprintf(stderr, "%c is not a valid init option\n", argv[argnum][2]);
return EXIT_FAILURE;
}
break;
case 'd': // Read/Write Mode
switch (argv[argnum][2]) {
case 'r': mode = MODE_READ; break;
case 'w': mode = MODE_WRITE; break;
default:
fprintf(stderr, "%c is not a valid init option\n", argv[argnum][2]);
return EXIT_FAILURE;
}
break;
case 'c': // Clock divider
clk_div = atoi(argv[argnum]+2);
break;
case 's': // Slave address
slave_address = atoi(argv[argnum]+2);
break;
default:
fprintf(stderr, "%c is not a valid option\n", argv[argnum][1]);
return EXIT_FAILURE;
}
argnum++; // advance the argument number
}
// If command is used for I2C_END or I2C_BEGIN only
if (argnum == argc && init != NO_ACTION) // no further arguments are needed
return EXIT_SUCCESS;
// Get len
if (strspn(argv[argnum], "0123456789") != strlen(argv[argnum])) {
fprintf(stderr, "Invalid number of bytes specified\n");
return EXIT_FAILURE;
}
len = atoi(argv[argnum]);
if (len > MAX_LEN) {
fprintf(stderr, "Invalid number of bytes specified\n");
return EXIT_FAILURE;
}
argnum++; // advance the argument number
xmitnum = argc - argnum; // number of xmit bytes
memset(wbuf, 0, sizeof(wbuf));
for (i = 0; i < xmitnum; i++) {
if (strspn(argv[argnum + i], "0123456789abcdefABCDEFxX") != strlen(argv[argnum + i])) {
fprintf(stderr, "Invalid data: ");
fprintf(stderr, "%d \n", xmitnum);
return EXIT_FAILURE;
}
wbuf[i] = (char)strtoul(argv[argnum + i], NULL, 0);
}
return EXIT_SUCCESS;
}
//*******************************************************************************
// showusage: Print the usage statement and return errcode.
//*******************************************************************************
int showusage(int errcode) {
printf("i2c \n");
printf("Usage: \n");
printf(" i2c [options] len [rcv/xmit bytes]\n");
printf("\n");
printf(" Invoking i2c results in an I2C transfer of a specified\n");
printf(" number of bytes. Additionally, it can be used to set the appropriate\n");
printf(" GPIO pins to their respective I2C configurations or return them\n");
printf(" to GPIO input configuration. Options include the I2C clock frequency,\n");
printf(" initialization option (i2c_begin and i2c_end). i2c must be invoked\n");
printf(" with root privileges.\n");
printf("\n");
printf(" The following are the options, which must be a single letter\n");
printf(" preceded by a '-' and followed by another character.\n");
printf(" -dx where x is 'w' for write and 'r' is for read.\n");
printf(" -ix where x is the I2C init option, b[egin] or e[nd]\n");
printf(" The begin option must be executed before any transfer can happen.\n");
printf(" It may be included with a transfer.\n");
printf(" The end option will return the I2C pins to GPIO inputs.\n");
printf(" It may be included with a transfer.\n");
printf(" -cx where x is the clock divider from 250MHz. Allowed values\n");
printf(" are 150 through 2500.\n");
printf(" Corresponding frequencies are specified in bcm2835.h.\n");
printf("\n");
printf(" len: The number of bytes to be transmitted or received.\n");
printf(" The maximum number of bytes allowed is %d\n", MAX_LEN);
printf("\n");
printf("\n");
printf("\n");
return errcode;
}
char buf[MAX_LEN];
int i;
uint8_t data;
int main(int argc, char **argv) {
printf("Running ... \n");
// parse the command line
if (comparse(argc, argv) == EXIT_FAILURE) return showusage (EXIT_FAILURE);
if (!bcm2835_init()) return 1;
// I2C begin if specified
if (init == I2C_BEGIN) bcm2835_i2c_begin();
// If len is 0, no need to continue, but do I2C end if specified
if (len == 0) {
if (init == I2C_END) bcm2835_i2c_end();
printf("... done!\n");
return EXIT_SUCCESS;
}
bcm2835_i2c_setSlaveAddress(slave_address);
bcm2835_i2c_setClockDivider(clk_div);
fprintf(stderr, "Clock divider set to: %d\n", clk_div);
fprintf(stderr, "len set to: %d\n", len);
fprintf(stderr, "Slave address set to: %d\n", slave_address);
if (mode == MODE_READ) {
for (i=0; i<MAX_LEN; i++) buf[i] = 'n';
data = bcm2835_i2c_read(buf, len);
printf("Read Result = %d\n", data);
for (i=0; i<MAX_LEN; i++) {
if(buf[i] != 'n') printf("Read Buf[%d] = %x\n", i, buf[i]);
}
}
if (mode == MODE_WRITE) {
data = bcm2835_i2c_write(wbuf, len);
printf("Write Result = %d\n", data);
}
// This I2C end is done after a transfer if specified
if (init == I2C_END) bcm2835_i2c_end();
bcm2835_close();
printf("... done!\n");
return 0;
}

PWM : 
Code: Select all
// pwm.c
//
// Example program for bcm2835 library
// Shows how to use PWM to control GPIO pins
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o pwm pwm.c -l bcm2835
// sudo ./pwm
//
// Or you can test it before installing with:
// gcc -o pwm -I ../../src ../../src/bcm2835.c pwm.c
// sudo ./pwm
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
// PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18)
// in alt fun 5.
// Note that this is the _only_ PWM pin available on the RPi IO headers
#define PIN RPI_GPIO_P1_12
// and it is controlled by PWM channel 0
#define PWM_CHANNEL 0
// This controls the max range of the PWM signal
#define RANGE 1024
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
// Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5);
// Clock divider is set to 16.
// With a divider of 16 and a RANGE of 1024, in MARKSPACE mode,
// the pulse repetition frequency will be
// 1.2MHz/1024 = 1171.875Hz, suitable for driving a DC motor with PWM
bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16);
bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1);
bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);
// Vary the PWM m/s ratio between 1/RANGE and (RANGE-1)/RANGE
int direction = 1;
int data = 1;
while (1)
{
if (data == 1)
direction = 1;
else if (data == RANGE-1)
direction = -1;
data += direction;
bcm2835_pwm_set_data(PWM_CHANNEL, data);
bcm2835_delay(50);
}
bcm2835_close();
return 0;
}

SPI : 
Code: Select all
// spi.c
//
// Example program for bcm2835 library
// Shows how to interface with SPI to transfer a byte to and from an SPI device
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o spi spi.c -l bcm2835
// sudo ./spi
//
// Or you can test it before installing with:
// gcc -o spi -I ../../src ../../src/bcm2835.c spi.c
// sudo ./spi
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
// Send a byte to the slave and simultaneously read a byte back from the slave
// If you tie MISO to MOSI, you should read back what was sent
uint8_t data = bcm2835_spi_transfer(0x23);
printf("Read from SPI: %02X\n", data);
bcm2835_spi_end();
bcm2835_close();
return 0;
}

Spin : 
Code: Select all
// Shows how to use SPI interface to transfer a number of bytes to and from an SPI device
// spin.c
//
// Example program for bcm2835 library
// Shows how to interface with SPI to transfer a number of bytes to and from an SPI device
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o spin spin.c -l bcm2835
// sudo ./spin
//
// Or you can test it before installing with:
// gcc -o spin -I ../../src ../../src/bcm2835.c spin.c
// sudo ./spin
//
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
return 1;
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
// Send a some bytes to the slave and simultaneously read
// some bytes back from the slave
// Most SPI devices expect one or 2 bytes of command, after which they will send back
// some data. In such a case you will have the command bytes first in the buffer,
// followed by as many 0 bytes as you expect returned data bytes. After the transfer, you
// Can the read the reply bytes from the buffer.
// If you tie MISO to MOSI, you should read back what was sent.
char buf[] = { 0x01, 0x02, 0x11, 0x33 }; // Data to send
bcm2835_spi_transfern(buf, sizeof(buf));
// buf will now be filled with the data that was read from the slave
printf("Read from SPI: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]);
bcm2835_spi_end();
bcm2835_close();
return 0;
}
Please Promote Our Work and website through yoursocial and professional network. We would highly appreciate your help.
Find us on FaceBook,Like and Share Please
User avatar
UCERD.COM
Site Admin
Site Admin
 
Posts: 958
Joined: Wed Jun 20, 2012 3:01 pm
Location: Barcelona
Has thanked: 13 times
Been thanked: 14 times
Blog: View Blog (1)

Return to Electronics

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron