Code:
/* PI RECEIVER
*
* PiB+ or Pi2 (with bcm2836)
* Power supply : Anything that gives you 5V and 1 Amp or more
* Pin numbering system : Broadcom
* Requires the bcm2835 library, available here:
* http://www.airspayce.com/mikem/bcm2835/bcm2835-1.52.tar.gz
* Follow the instructions to install and use it here:
* http://www.airspayce.com/mikem/bcm2835/
*
* NRF Pi FUNC COLOUR
* ****************************
* 1 GND Black
* 2 3.3V Red THREE point THREE volts !!!.!!!
* 3 22 CE Brown
* 4 CE0 CSN White
* 5 SCLK Yellow
* 6 MOSI Green
* 7 MISO Orange
* 8 17 IRQ Grey Pulled UP
*/
#include <bcm2835.h>
#include <stdio.h>
void config_RX(void)
{
char buf[2];
int i;
buf[0]=32; //Reg0
buf[1]=11; //PRX, Power ON, CRC 1 byte
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0]=38; //Reg6
buf[1]=8; //2Mbps, -18dBm
bcm2835_spi_transfern(buf, sizeof(buf));
for (i=49;i<55;i++) //Payload width = 1 byte for all pipes
{
buf[0]=i;
buf[1]=1;
bcm2835_spi_transfern(buf, sizeof(buf));
}
}
void clear_nrf_Interrupts(void)
{
char buf[2];
buf[0]=39; //Status reg
buf[1]=112; //Write 1 to clear bits (64+32+16)
bcm2835_spi_transfern(buf, sizeof(buf));
}
void listen(void)
{
char buf[2];
bcm2835_gpio_write(22,HIGH); // CE high
printf("Waiting for data, ctrl c to quit \n");
while (1)
{
if (bcm2835_gpio_eds(17)) // nrf interrupt
{
buf[0]=97; // command to read RX_PAYLOAD
buf[1]=0;
bcm2835_gpio_write(22,LOW); // Stop listening
bcm2835_spi_transfern(buf, sizeof(buf)); // Send command
clear_nrf_Interrupts();
bcm2835_spi_transfer(226); // Flush RX
bcm2835_gpio_set_eds(17); // Clear the Pi's eds flag
printf("Received %d \n", buf[1]);
bcm2835_gpio_write(22,HIGH); // Start listening
}
delay(50);
}
}
void print_nrf_regs(void)
{
int i;
char buf[2]; // Data to send
buf[1]=0;
for (i=0;i<10;i++)
{
buf[0]=i;
bcm2835_spi_transfern(buf, sizeof(buf));
printf("Reg N° %d contains: %02X \n", i,buf[1]);
}
char pipebuf[6];
for (i=10;i<17;i++)
{
pipebuf[0]=i;
bcm2835_spi_transfern(pipebuf, sizeof(pipebuf));
printf("Reg N° %d contains: %02X %02X %02X %02X %02X\n", i,pipebuf[1],pipebuf[2],pipebuf[3],pipebuf[4],pipebuf[5]);
}
buf[1]=0;
for (i=17;i<24;i++)
{
buf[0]=i;
bcm2835_spi_transfern(buf, sizeof(buf));
printf("Reg N° %d contains: %02X \n", i,buf[1]);
}
}
int main(int argc, char **argv)
{
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;
}
print_nrf_regs();
bcm2835_gpio_fsel(22,BCM2835_GPIO_FSEL_OUTP); // CE pin is output
bcm2835_gpio_write(22,LOW); // CE low
bcm2835_gpio_fsel(17,BCM2835_GPIO_FSEL_INPT); // IRQ pin is input
bcm2835_gpio_set_pud(17, BCM2835_GPIO_PUD_UP); // Pulled UP
bcm2835_gpio_len(17); // And a low detect enable
config_RX();
clear_nrf_Interrupts();
listen();
// bcm2835_spi_end(); With Ctrl C we never get here.
//bcm2835_close();
return 0;
}
AVERTISSEMENT
Je n'aime pas le langage C et il me le rend bien. A utiliser, donc, à vos risques et périls!
A suivre...