Convert to BCM2835 from wiringPi - Please help

84 views
Skip to first unread message

David Bemenderfer

unread,
Apr 18, 2023, 1:10:50 AM4/18/23
to bcm2835
I'm trying to convert the linked GitHub project (https://github.com/alperenguman/rpi-wiegand-reader) to use BCM2835 and am stumped.  It was originally authored for wiringPi but wiringPi has since been depreciated.

I'm completely stumped on how to convert from wiringPi.  Does anyone see what I'm doing wrong?

Below is the original code:  red is wiringPi / green is my attempt at bcm2835.

/*
* linked with -lpthread -lwiringPi -lrt
* linked with -lpthread -lbcm2835 -lrt
*/

#include <stdio.h>
#include <stdlib.h>
//#include <wiringPi.h> // Depreciated
#include <bcm2835.h> // Replacement
#include <time.h>
#include <unistd.h>
#include <memory.h>

//#define PIN_0 0 // GPIO Pin 17 | Green cable | Data0
//#define PIN_1 1 // GPIO Pin 18 | White cable | Data1
//#define PIN_SOUND 25 // GPIO Pin 26 | Yellow cable | Sound

#define PIN17 RPI_GPIO_P1_11 // GPIO Pin 17 | Green cable | Data0
#define PIN18 RPI_GPIO_P1_12 // GPIO Pin 18 | White cable | Data1
#define PIN26 RPI_GPIO_P1_26 // GPIO Pin 26 | Yellow cable | Sound

#define MAXWIEGANDBITS 32
#define READERTIMEOUT 3000000
#define LEN 256

static unsigned char __wiegandData[MAXWIEGANDBITS];
static unsigned long __wiegandBitCount;
static struct timespec __wiegandBitTime;

void getData0(void) {
if (__wiegandBitCount / 8 < MAXWIEGANDBITS) {
__wiegandData[__wiegandBitCount / 8] <<= 1;
__wiegandBitCount++;
}
clock_gettime(CLOCK_MONOTONIC, &__wiegandBitTime);
}

void getData1(void) {
if (__wiegandBitCount / 8 < MAXWIEGANDBITS) {
__wiegandData[__wiegandBitCount / 8] <<= 1;
__wiegandData[__wiegandBitCount / 8] |= 1;
__wiegandBitCount++;
}
clock_gettime(CLOCK_MONOTONIC, &__wiegandBitTime);
}

int wiegandInit(int d0pin, int d1pin) {

//Setup bcm2835
if(!bcm2835_init()){
printf("Error initializing BCM2835 library. \n");
return 1;
}
bcm_gpio_fsel(d17pin, BCM2835_GPIO_FSEL_INPT); // Set pin 17 as ibcm_gpio_set_pud(d17pin, BCM2835_GPIO_PUD_UP); // add pullup


??????????????????????????????????
I believe that the below block is my issue that needs to be converted above.
// Setup wiringPi
//wiringPiSetup() ;
//pinMode(d0pin, INPUT);
//pinMode(d1pin, INPUT);
//pinMode(PIN_SOUND, OUTPUT);

//wiringPiISR(d0pin, INT_EDGE_FALLING, getData0); 
//wiringPiISR(d1pin, INT_EDGE_FALLING, getData1);

}

void wiegandReset() {
memset((void *)__wiegandData, 0, MAXWIEGANDBITS);
__wiegandBitCount = 0;
}

int wiegandGetPendingBitCount() {
struct timespec now, delta;
clock_gettime(CLOCK_MONOTONIC, &now);
delta.tv_sec = now.tv_sec - __wiegandBitTime.tv_sec;
delta.tv_nsec = now.tv_nsec - __wiegandBitTime.tv_nsec;

if ((delta.tv_sec > 1) || (delta.tv_nsec > READERTIMEOUT))
return __wiegandBitCount;

return 0;
}

int wiegandReadData(void* data, int dataMaxLen) {
if (wiegandGetPendingBitCount() > 0) {
int bitCount = __wiegandBitCount;
int byteCount = (__wiegandBitCount / 8) + 1;
memcpy(data, (void *)__wiegandData, ((byteCount > dataMaxLen) ? dataMaxLen : byteCount));

wiegandReset();
return bitCount;
}
return 0;
}

void printCharAsBinary(unsigned char ch) {
int i;
FILE * fp;
fp = fopen("output","a");

for (i = 0; i < 8; i++) {
printf("%d", (ch & 0x80) ? 1 : 0);
fprintf(fp, "%d", (ch & 0x80) ? 1 : 0);
ch <<= 1;
}

fclose(fp);
}

void makeBeep(int millisecs, int times){
int i;
for (i = 0; i < times; i++) {
digitalWrite (PIN_SOUND, LOW);
delay(millisecs);
digitalWrite (PIN_SOUND, HIGH);
delay(millisecs/2);
}
}


void main(void) {
int i;

wiegandInit(PIN17, PIN18);
//wiegandInit(PIN_0, PIN_1);


while(1) {
int bitLen = wiegandGetPendingBitCount();
if (bitLen == 0) {
usleep(5000);
} else {
char data[100];
char string1[100];
bitLen = wiegandReadData((void *)data, 100);
int bytes = bitLen / 8 + 1;
FILE *fp;
fp = fopen("output","a");
printf("%lu ", (unsigned long)time(NULL));
fprintf(fp, "%lu ", (unsigned long)time(NULL));
printf("Read %d bits (%d bytes): ", bitLen, bytes);
fprintf(fp, "Read %d bits (%d bytes): ", bitLen, bytes);
for (i = 0; i < bytes; i++)
printf("%02X", (int)data[i]);
for (i = 0; i < bytes; i++)
fprintf(fp, "%02X", (int)data[i]);

printf(" : ");
fprintf(fp, " : ");
fclose(fp);
for (i = 0; i < bytes; i++)
printCharAsBinary(data[i]);
fp = fopen("output","a");
printf("\n");
fprintf(fp, "\n");
fclose(fp);
makeBeep(200, 1);
}
}
}

Malc

unread,
Apr 18, 2023, 5:56:33 AM4/18/23
to bcm2835
There's too much wrong with your example code to list all the errors.

If you're new to the bcm2835 library, I suggest your read and implement a few of Mike's examples before you try porting your WiringPi code.  blink.c, input.c and event.c are probably the most relevant to what you're trying to do.

bcm2835 library doesn't support interrupts, because they can't be handled in user space under Linux.  WiringPi used the sysfs interface to simulate interrupts.  To replace your WiringPiISR() calls you probably need to set falling edge detection events on the relevant pins with bcm2835_gpio_fen() or similar, and then test the event status bit(s) with bcm2835_gpio_eds().  Note the warning about "Crashing on some versions of Raspbian" on the Main page of the bcm2835 library site; edge events are one of the things that can cause this crash.
Reply all
Reply to author
Forward
0 new messages