Code:
/* PI TRANSMITTER
*
* 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 COLOUR Pi FUNCTION
* =============================
* 1 Black GND
* 2 Red 3.3V THREE point THREE volts !!!.!!!
* 3 Brown 22 CE
* 4 White CE0 CSN
* 5 Yellow SCLK
* 6 Green MOSI
* 7 Orange MISO
* 8 Grey 17 IRQ Pulled UP (I don't use it in TX mode)
*/
#include <bcm2835.h>
#include <stdio.h>
void config_TX(void)
{
char buf[2];
int i;
buf[0]=32; //Reg0
buf[1]=10; //PTX, Power ON, CRC 1 byte
bcm2835_spi_transfern(buf, sizeof(buf));
buf[0]=36; //Reg4
buf[1]=255;
bcm2835_spi_transfern(buf, sizeof(buf)); //Wait 4ms, 15 retries
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 send(void)
{
uint8_t buf[2];
uint8_t payload=0;
while (1)
{
buf[0]=160; // "Write TX payload" command
buf[1]=payload;
bcm2835_spi_transfern(buf, sizeof(buf)); // Write TX payload
bcm2835_gpio_write(22,HIGH); // Pulse it out
bcm2835_delayMicroseconds(10);
bcm2835_gpio_write(22,LOW);
delay(10);
printf("Sent %d \n", payload);
bcm2835_spi_transfer(225); // Flush TX
clear_nrf_Interrupts(); // Get rid of TX_Sent interrupt
if (payload < 10){
payload++;
}
else{
payload=0;
}
delay(1000); // Wait a sec
}
}
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;
}
bcm2835_gpio_fsel(22,BCM2835_GPIO_FSEL_OUTP); // CE pin is output
bcm2835_gpio_write(22,LOW); // CE low
config_TX();
clear_nrf_Interrupts();
print_nrf_regs();
send();
//bcm2835_spi_end(); With Ctrl-C we never get here.
// bcm2835_close();
return 0;
}
A, B, B+, Compute Module et Pi Zéro.
Ou le bcm2836: Premiers Pi2.
Je les ai testés sur un PiB+ (bcm2835) et un Pi2B (bcm2836).
Il n'y a pas de doc publiée pour le bcm2837 (Pi3 et dernières versions du Pi2) et je n'ai pas de Pi3 pour tester. (J'attends le Pi4 pour casser ma tirelire).