Eva Deltor
Eva Deltor

Reputation: 41

BCM2835 Raspberry Pi 4

I'm using the library bcm2835 1, it works, I can see on the terminal how plots 126 data information from a sensor but from that point on I recieve this:

bcm2835_init: gpio mmap failed: Cannot allocate memory

Does anyone know what is the problem or how to solve it?I think it's due to the library but not sure how to solve it. I'm using SPI for reciving the data

#include <bcm2835.h>
#include <stdio.h>
char n = 0;

void escriure_csv(char valor){

}
int main()
{   
    FILE * fp;
    fp = fopen ("file.csv", "w+");
    
    for(int i=0;i<130;i++){
    
    bcm2835_init();
    bcm2835_spi_begin();
    
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
    
    
    uint8_t send_data = 0xB3; //ens dona igual perque nomès ens interesa el que torna
    uint8_t read_data = bcm2835_spi_transfer(send_data);
    //printf("Read back from SPI: 0x%02X.\n", read_data);
 
    
    //transformem al que volem 
    //els canvis seran mV per lo tant enlloc de *2.5 hi fem fer 2
    uint8_t mV = (read_data*250)/1024; //en cas que tornes un 0 es perque el resultat es - o 0.
    printf("mV: %u\n", mV);
    n+=1;
    fprintf(fp,"%d",mV);
    fprintf(fp,"\n");
    //escriure_csv(mV);
}
    fclose(fp);
    return 0;
}

enter image description here

Upvotes: 0

Views: 2086

Answers (1)

Eva Deltor
Eva Deltor

Reputation: 41

As one of the comments said the problem here was that I was initializinf the SPI every iteration, I had to put it outside the for loop.

Upvotes: 1

Related Questions