User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Don your testing hats, it's WiringPi2-Python

Thu Dec 19, 2013 11:20 pm

Hi,

Wondering if there is any help available regarding SPI. I've been having a go with it, but something doesn't quite seem to be working and I'm not sure why. Hopefully I can scope it and see the difference.

Essentially, I have spi-dev working with Python 2.7 but was trying to replicate it using WiringPi2 and Python 3.

It "almost" works...but I'm not sure what is going wrong.
Note:CS is on physical pin P1.26

import wiringpi2
SPI_CS=1
SPI_SPEED=100000
wiringpi2.wiringPiSPISetup(SPI_CS,SPI_SPEED)
buff=0x0B07
buff=buff.to_bytes(2, byteorder='big')
wiringpi2.wiringPiSPIDataRW(SPI_CS,buff)

This is supposed to be the same as:
import spidev
spi = spidev.SpiDev()
spi.open(0,1)
buff=0x0B07
spi.xfer(buff)

The spidev one works, but the wiringpi2 one seems to send glitchy data or something. The chip I am using transmits the DATA IN back out on the DATA OUT but delayed, and it seems like the data is correct, so not sure what is happening.

The chip I am using is a MAX7219C, so perhaps it is the clock speed or something? Let me know if there is something obvious I have missed...SPI clock mode perhaps?

UPDATE: There is nothing like using a scope to see what is happening. It all appears to be working this morning.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
elamperti
Posts: 1
Joined: Mon Jan 20, 2014 12:33 am
Location: Argentina
Contact: Website

Re: Don your testing hats, it's WiringPi2-Python

Mon Jan 20, 2014 12:41 am

And what were your findings? I don't have a scope, and I'm using WiringPi2-Python to drive four daisy-chained MAX7219.
I can get to drive each display for some seconds, losing them and getting garbage displayed. I think it has something to do with the clock setting, but I can't be sure (yet).
Enrico
http://blog.elamperti.com/

User avatar
meltwater
Posts: 1014
Joined: Tue Oct 18, 2011 11:38 am

Re: Don your testing hats, it's WiringPi2-Python

Mon Jan 20, 2014 6:46 pm

elamperti wrote:And what were your findings? I don't have a scope, and I'm using WiringPi2-Python to drive four daisy-chained MAX7219.
I can get to drive each display for some seconds, losing them and getting garbage displayed. I think it has something to do with the clock setting, but I can't be sure (yet).
I think the CS pins were numbered differently, 1=0 and 0=1 out something like that. But could be wrong there. Will check my notes.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

ubergeek72
Posts: 12
Joined: Tue Aug 28, 2012 2:58 am

Re: Don your testing hats, it's WiringPi2-Python

Thu Oct 30, 2014 10:33 pm

Okay. I'm trying to figure out why wiringpi2.wiringPiISR doesn't work.

To verify my hardware setup, I built isr.c with the following code, copied pretty much verbatim from the wiringpi website:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>


// What GPIO input are we using?
//	This is a wiringPi pin number

#define	BUTTON_PIN	10

// globalCounter:
//	Global variable to count interrupts
//	Should be declared volatile to make sure the compiler doesn't cache it.

static volatile int globalCounter = 0 ;


/*
 * myInterrupt:
 *********************************************************************************
 */

void myInterrupt (void)
{
  ++globalCounter ;
}


/*
 *********************************************************************************
 * main
 *********************************************************************************
 */

int main (void)
{
  int myCounter = 0 ;

  if (wiringPiSetup() < 0)
  {
    fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
  {
    fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
    return 1 ;
  }


  for (;;)
  {
    printf ("Waiting ... ") ; fflush (stdout) ;

    while (myCounter == globalCounter)
      delay (100) ;

    printf (" Done. counter: %5d\n", globalCounter) ;
    myCounter = globalCounter ;
  }

  return 0 ;
}
It works as advertised. Usually the counter increments by one before I push the button that shorts pin 10 to the 3.3v rail, but it picks up all subsequent presses and counts them as you'd expect.

Having verified that my hardware setup is correct, I've ported that same code to Python:

Code: Select all

import wiringpi2
import sys

BUTTON_PIN=10
INT_EDGE_SETUP=0
INT_EDGE_RISING=1
INT_EDGE_FALLING=2
INT_EDGE_BOTH=3

globalCounter=0

def myInterrupt():
	global globalCounter
	globalCounter+=1


wiringpi2.wiringPiSetup()

mycounter=0
if (wiringpi2.wiringPiISR(BUTTON_PIN,INT_EDGE_FALLING,(myInterrupt)())<0):
	print("Problem setting up ISR")

while True:
	print("Waiting ...")
	sys.stdout.flush()
	while (mycounter==globalCounter):
		wiringpi2.delay(100)

	print(" Done. counter: "+str(globalCounter))
	mycounter = globalCounter
This also increments the counter by 1 before anything else happens. Unlike the c version, however, pressing the button does nothing.
Am I missing something? Does wiringpi2.wiringPiISR not work? Any help would be much appreciated.

-JRS

ubergeek72
Posts: 12
Joined: Tue Aug 28, 2012 2:58 am

Re: Don your testing hats, it's WiringPi2-Python

Fri Oct 31, 2014 3:04 am

To answer my own question, according to this, wiringpi2 can't do wiringpi2.wiringPiIsr interrupts. Drat.

Thanks. -JRS

Return to “Python”