Posts: 1
Joined: Wed Oct 17, 2018 7:38 am


Wed Oct 17, 2018 7:48 am

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
// Author: Mike McCauley
// Copyright (C) 2012 Mike McCauley
// $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())
      printf("bcm2835_init failed. Are you running as root??\n");
      return 1;
    if (!bcm2835_spi_begin())
      printf("bcm2835_spi_begin failed. Are you running as root??\n");
      return 1;
    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 send_data = 0x23;
    uint8_t read_data = bcm2835_spi_transfer(send_data);
    printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);
    if (send_data != read_data)
      printf("Do you have the loopback from MOSI to MISO connected?\n");
    return 0;

I run this code but uint8_t read_data = bcm2835_spi_transfer(send_data); this instruction is not working. Even i didn't get clock signal at SPI_CLOCK. What's wrong with bcm2835.h library

Posts: 3075
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada


Wed Oct 17, 2018 3:14 pm

Wow respond to an 4 years old post with new request.

Good chance that the post is obsolete!

You don't need the bcm2835 library to run a mcp3008 or SPI.

There is wiringpi.

And the kernel could access the mcp3008
dtoverlay=mcp3008,<param>[=<val>] check /boot/overlays/README

This is my most recent post about the mcp3008 and I used ioctl calls to access the SPI. No extra library needed!

Last edited by danjperron on Wed Oct 17, 2018 6:12 pm, edited 1 time in total.

User avatar
Forum Moderator
Forum Moderator
Posts: 11017
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands


Wed Oct 17, 2018 5:12 pm

Split off from the obsolete (now locked) thread.

and yes, writing your own SPI driver code is so 2014.

we now have driver libraries, a newer OS, and device tree, the old ways are obsolete.

please note the age of the post you are replaying to, I'm pleading to lock all posts more than 18 months old.

oh, and code tags, I added them....

Return to “Interfacing (DSI, CSI, I2C, etc.)”