Exemple #1
0
void Rf24RaspberryPiIo::select() {
    bcm2835_spi_setBitOrder(RF24_BIT_ORDER);
    bcm2835_spi_setDataMode(RF24_DATA_MODE);
    bcm2835_spi_setClockDivider(spi_speed ? spi_speed : RF24_CLOCK_DIVIDER);
    bcm2835_spi_chipSelect(csn_pin);
    delayMicroseconds(5);
}
SpiPlateformImplementation::SpiPlateformImplementation( SpiChipSelect cs):
_cs( cs )
{
    if( s_count++ == 0)
    {
        printf("\n SPI INIT");
#ifdef TARGET_RASPBERRY_PI
        bcm2835_spi_begin();
        
        bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
        bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
        bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4096);
        bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
        bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
#endif
        /*
        setBitOrder( SpiMSB );
        setDataMode( SpiMode_0);
        setClockDivider( SPI_CLOCK_DIVIDER_4096);
        setChipSelect( _cs );
        setCsPolarity( low );
         */
    }


}
Exemple #3
0
	void Rfid::init_rfid(void)
	{
		if(geteuid()!=0 || getenv("FAKEROOTKEY"))
		{
			(*_pLinkToConsole)->printOut("You need to be root to properly run this program");
			throw "You need to be root to properly run this program";
		}
		if(!bcm2835_init())
		{
			(*_pLinkToConsole)->printOut("Not able to initialize BCM2835");
			throw "Not able to initialize BCM2835";
		}

		bcm2835_spi_begin();
		bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);	// default
		bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);					// default
		bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32);	// default
		bcm2835_spi_chipSelect(BCM2835_SPI_CS0);					// default
		bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);	// default

		/*
		uid_t uid=500;
		setuid(uid);
		*/

		InitRc522();
	}
Exemple #4
0
int main(int argc, char **argv) {
	if (!bcm2835_init())
		return 1;
	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE1);
	bcm2835_spi_setClockDivider(2170-200);
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
	
	volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
	bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_LEN, BCM2835_SPI0_CS_LEN); //make it 9 bit
	
	volatile uint32_t* ltoh = bcm2835_spi0+BCM2835_SPI0_LTOH/4;
	bcm2835_peri_set_bits(ltoh,0b1111,0);
	
	unsigned int i='a';
	
	while(1) {
		for(char c='a';c<='z';c++) {
			spi_uart_tx(c);
			spi_uart_tx(' ');
		}
		spi_uart_tx('\n');
		
// 		bcm2835_delay(10);
// 		printf(" ");
	}
	
	bcm2835_spi_end();
	bcm2835_close();
	
	return 0;
}
Exemple #5
0
int main(int argc, char **argv)
{
    // If you call this, it will not actually access the GPIO
    // Use for testing
    //        bcm2835_set_debug(1);

    if (!bcm2835_init())
    {
        return 1;
    }

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

    int i;
    for( i = 0; i < 100; i++ )
    {
        // Send a byte to the slave and simultaneously read a byte back from the slave
        // If you tie MISO to MOSI, you should read back what was sent
        uint8_t data = bcm2835_spi_transfer(i);
        printf("Wrote: %02X Read from SPI: %02X\n", i, data);
    }

    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
Exemple #6
0
void VM205::connect() {
	bcm2835_spi_begin();
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE2);
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	bcm2835_spi_setClockDivider(500);
}
Exemple #7
0
/*
*	Function: spi_test
* Description: be used to test SPI related functions by using the AT450BXX module
*
*   host             slave
*   MISO  < - >   MISO
*   MOSI  < - >   MOSI
*   CLK    < - >   CLK
*   CE0/1 < - >   CS
*/
void spi_test(void)
{
	int num;
	unsigned char wBuf = 0x3f;
	unsigned char rBuf;
	printf("--------------->Test SPI With AT450BXX<--------------\n");
	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);	  // The default
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); 				  // The default
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); 
	//bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
	//bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH);

	bcm2835_gpio_fsel(PIN_CS, BCM2835_GPIO_FSEL_OUTP);    /*set CS, and must be called*/

	printf("SPI write 0x%x\n", wBuf);
	ee_write(0,wBuf);
	bcm2835_delay(50);

	rBuf = ee_read(0);
	printf("SPI read 0x%x\n", rBuf);
	if(wBuf == rBuf)
	{
		printf("SPI interface work well !...\n");
	}
	else
	{
		printf("SPI interface work bad !...\n");
	}
	 bcm2835_spi_end();	
}
// initializer for SPI - we indicate the pins used and OLED type
//
boolean ArduiPi_OLED::init(int8_t DC, int8_t RST, int8_t CS, uint8_t OLED_TYPE) 
{
  rst = RST==OLED_PIN_DEFAULT ? DEF_SPI_RESET : RST;  // Reset Pin
  dc  = DC ==OLED_PIN_DEFAULT ? DEF_SPI_DC    : DC;   // Data / command Pin
  cs  = CS ==OLED_PIN_DEFAULT ? DEF_SPI_CS    : (
        CS ==OLED_CS_ALTERNATE ? ALT_SPI_CS   : CS);  // Raspberry SPI chip Enable (may be CE0 or CE1)
  
  // Select OLED parameters
  if (!select_oled(OLED_TYPE))
    return false;

  // Init & Configure Raspberry PI SPI
  bcm2835_spi_begin(cs);
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                
  
  // 16 MHz SPI bus, but Worked at 62 MHz also  
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); 

  // Set the pin that will control DC as output
  bcm2835_gpio_fsel(dc, BCM2835_GPIO_FSEL_OUTP);

  // Setup reset pin direction as output
  if (rst >= 0)
    bcm2835_gpio_fsel(rst, BCM2835_GPIO_FSEL_OUTP);

  return ( true);
}
Exemple #9
0
void init_touch_spi(void)
{
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default

}
Exemple #10
0
 //
// Set up a memory regions to access GPIO
//
void setup_io()
{
   /* open /dev/mem */
   if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
      printf("can't open /dev/mem \n");
      exit(-1);
   }
 
   /* mmap GPIO */
   gpio_map = mmap(
      NULL,             //Any adddress in our space will do
      BLOCK_SIZE,       //Map length
      PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
      MAP_SHARED,       //Shared with other processes
      mem_fd,           //File to map
      GPIO_BASE         //Offset to GPIO peripheral
   );
   if (gpio_map == MAP_FAILED) {
      printf("mmap error %d\n", (int)gpio_map);//errno also set!
      exit(-1);
   }
   // Always use volatile pointer!
   gpio = (volatile unsigned *)gpio_map;

#define CLK_LEN   0xA8   
   gpio_map = mmap(
      NULL,             //Any adddress in our space will do
      CLK_LEN,       //Map length
      PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
      MAP_SHARED,       //Shared with other processes
      mem_fd,           //File to map
      CLOCK_BASE         //Offset to GPIO peripheral
   );
    if (gpio_map == MAP_FAILED) {
      printf("mmap error %d\n", (int)gpio_map);//errno also set!
      exit(-1);
   }
   // Always use volatile pointer!
   clkReg = (volatile unsigned *)gpio_map;
   
   close(mem_fd); //No need to keep mem_fd open after mmap

   if (!bcm2835_init())
   {
		printf("bcm2835_init error\n");
		exit(-1);   
   }
#if 0   
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4); 
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default   
#endif
} // setup_io
void SpiPlateformImplementation::setBitOrder     ( SpiBitOrder order)
{
//    if (order != s_order)
    {
        s_order = order;
#ifdef TARGET_RASPBERRY_PI
        bcm2835_spi_setBitOrder( s_order);
#endif
    }
}
int spi_init() {
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    //bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

    return 0;
}
Exemple #13
0
/**
 *@brief Initializes the SPI peripheral
 *@return none
 */
void SPI_Initialize(void)
{	if (!bcm2835_init())
	{
		printf("BCM libray error.\n");			//Should be run with the sudo cmd
	}
	bcm2835_spi_begin();						//Configure SPI pins
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      	//Configure bit order
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);  			//Set clock polarity and phase CPOL=0, CPHA=0
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_4);	//SPI baud rate at 244 Khz
	bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);			//Control CE0 in software
	printf("SPI initialized...\n");
}
// initialization of GPIO and SPI
// ----------------------------------------------------------
void TFT_init_board ( void )
{
	// *************** set the pins to be an output and turn them on
	
	bcm2835_gpio_fsel( OE, BCM2835_GPIO_FSEL_OUTP );
	bcm2835_gpio_write( OE, HIGH );
	
	bcm2835_gpio_fsel( RAIO_RST, BCM2835_GPIO_FSEL_OUTP );
	bcm2835_gpio_write( RAIO_RST, HIGH );

    bcm2835_gpio_fsel( RAIO_CS, BCM2835_GPIO_FSEL_OUTP );
	bcm2835_gpio_write( RAIO_CS, HIGH );
		
	bcm2835_gpio_fsel( RAIO_RS, BCM2835_GPIO_FSEL_OUTP );
	bcm2835_gpio_write( RAIO_RS, HIGH );

    bcm2835_gpio_fsel( RAIO_WR, BCM2835_GPIO_FSEL_OUTP );
    bcm2835_gpio_write( RAIO_WR, HIGH );
	
	bcm2835_gpio_fsel( RAIO_RD, BCM2835_GPIO_FSEL_OUTP );
	bcm2835_gpio_write( RAIO_RD, HIGH );
	
	
	// *************** now the inputs
	
	bcm2835_gpio_fsel( RAIO_WAIT, BCM2835_GPIO_FSEL_INPT );
	bcm2835_gpio_set_pud( RAIO_WAIT, BCM2835_GPIO_PUD_UP);
	
	bcm2835_gpio_fsel( RAIO_INT, BCM2835_GPIO_FSEL_INPT );
	bcm2835_gpio_set_pud( RAIO_INT, BCM2835_GPIO_PUD_UP);
	
		
	// *************** set pins for SPI
	
    bcm2835_gpio_fsel(MISO, BCM2835_GPIO_FSEL_ALT0); 
    bcm2835_gpio_fsel(MOSI, BCM2835_GPIO_FSEL_ALT0); 
    bcm2835_gpio_fsel(SCLK, BCM2835_GPIO_FSEL_ALT0);
    bcm2835_gpio_fsel(SPI_CE1, BCM2835_GPIO_FSEL_ALT0);
        
    // set the SPI CS register to the some sensible defaults
    volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/8;
    bcm2835_peri_write( paddr, 0 ); // All 0s
    
    // clear TX and RX fifos
    bcm2835_peri_write_nb( paddr, BCM2835_SPI0_CS_CLEAR );
    
	bcm2835_spi_setBitOrder( BCM2835_SPI_BIT_ORDER_MSBFIRST );      
    bcm2835_spi_setDataMode( BCM2835_SPI_MODE0 );                 
    bcm2835_spi_setClockDivider( BCM2835_SPI_CLOCK_DIVIDER_2 ); 
    bcm2835_spi_chipSelect( BCM2835_SPI_CS1 );                      
    bcm2835_spi_setChipSelectPolarity( BCM2835_SPI_CS1, LOW );    
}
Exemple #15
0
/**************************************************************************
    Function:	spi_lcd_init
    Purpose:	Initialize SPI device	
    Returns:	
    Note:	Sets defaults	
 *************************************************************************/
int spi_lcd_init(struct spi_lcd_dev *dev)
{
	// wait for 100ms for power up delay - could probably skip this.
	bcm2835_delay(100);

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536);
	bcm2835_spi_chipSeclect(BCM2835_SPI_CS0);
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0,LOW);
	
	return 0;
}
/*!
 * Start the SPI communication
 */
void EcgCapture::spiInit()
{
    bcm2835_gpio_fsel(PIN18, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_write(PIN18, LOW);
    delay(100);
    bcm2835_gpio_write(PIN18, HIGH);
    delay(100);
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
    bcm2835_gpio_fsel(PIN16, BCM2835_GPIO_FSEL_INPT);
}
MPU9250::MPU9250()
{
  if (!bcm2835_init())
    {
      printf("bcm2835_init failed. Are you running as root??\n");
    }
  if (!bcm2835_spi_begin())
    {
      printf("bcm2835_spi_begin failedg. Are you running as root??\n");
    }
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); // The default
  bcm2835_spi_setChipSelectPolarity(MPU9250_PIN, LOW);      // the default
  bcm2835_spi_chipSelect(MPU9250_PIN);                      // The default    
}
Exemple #18
0
void main(int argc, char* argv)
{
   /* Pour le debugging
      1 == Print sur la console les operations des fonctions 
      0 == Comportement normal du programme */
   bcm2835_set_debug(0);

   /* Initialisation de la librairie */
   if(bcm2835_close())
   {
      /* code d'initialisation bcm */
      printf("Version de la librairie : %d", bcm2835_version());

      if(bcm2835_spi_begin())
      {
         /* Initialisation de la SPI
            Voir header pour valeurs */
         bcm2835_spi_setClockDivider(CLK_SPEED);
         bcm2835_spi_setDataMode(PI_MODE);
         bcm2835_spi_setBitOrder(BIT_ORDER);
         /* Selecte un CS pour le transfert de donnees */
         bcm2835_spi_chipSelect(CS_SLAVE);

         /* Test de transfert avec retour */
         uint8_t ret;
         ret = bcm2835_spi_transfer(5);
         printf("Retour de l'Arduino : %d", ret);

         /* Test de transfert sans retour */
         bcm2835_spi_transfern("A", 1);

         bcm2835_spi_end();
      }
      else
      {
         printf("Erreur de l'init de SPI\n");
      }

      bcm2835_close(void); /* Libere la memoire et ferme la librairie */
   }
   else
   {
      printf("Echec de l'ouverture de la librairie\n");
   }
}
Exemple #19
0
int DIS_init() {
	if (!bcm2835_init())
		return 1;

	bcm2835_gpio_fsel(DIS_CS1, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_fsel(DIS_CS2, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_fsel(DIS_CS3, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_fsel(DIS_CS4, BCM2835_GPIO_FSEL_OUTP);

	chipSelect(DIS_NONE);

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
	bcm2835_spi_setClockDivider(DIS_CLOCK_DIVIDER);
	bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);
	return 0;
}
Exemple #20
0
void RF24::csn(bool mode)
{
  // Minimum ideal SPI bus speed is 2x data rate
  // If we assume 2Mbs data rate and 16Mhz clock, a
  // divider of 4 is the minimum we want.
  // CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#ifdef ARDUINO
	#if  ( !defined(RF24_TINY) && !defined (__arm__)  && !defined (SOFTSPI)) || defined (CORE_TEENSY)
 			_SPI.setBitOrder(MSBFIRST);
  			_SPI.setDataMode(SPI_MODE0);
			_SPI.setClockDivider(SPI_CLOCK_DIV2);
	#endif
#endif

#if defined (RF24_LINUX)
    #if !defined (RF24_SPIDEV)
    //if(!mode){
	  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
	  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
	  bcm2835_spi_setClockDivider(spi_speed);
      bcm2835_spi_chipSelect(csn_pin);
      delayMicroseconds(5);
    #endif

#elif defined (RF24_TINY)
	if (ce_pin != csn_pin) {
		digitalWrite(csn_pin,mode);
	} 
	else {
		if (mode == HIGH) {
			PORTB |= (1<<PINB2);  	// SCK->CSN HIGH
			delayMicroseconds(100); // allow csn to settle.
		} 
		else {
			PORTB &= ~(1<<PINB2);	// SCK->CSN LOW
			delayMicroseconds(11);  // allow csn to settle
		}
	}		
#elif !defined  (__arm__) || defined (CORE_TEENSY)
	digitalWrite(csn_pin,mode);	
    delayMicroseconds(5);	
#endif

}
Exemple #21
0
void init_lcd_spi(void)
{    
    bcm2835_init();

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default

    bcm2835_gpio_fsel(RPI_GPIO_P1_16, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(RPI_GPIO_P1_22, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(PENIRQ, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(PENIRQ, BCM2835_GPIO_PUD_UP);///////////////////////

   printf("\n spi-lcd22 test :\n Start .......\n  ");

}
int main(int argc, char **argv)
{
    if (!bcm2835_init())
        return 1;

    char buffer[4];
    buffer[0] = 50;
    int i,temp;

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);                    //SCLK rising edge - clock idle state 1
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536);  //set clock frequency
    bcm2835_spi_chipSelect(BCM2835_SPI_CS1);                       //use chip select 1
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW);       //chip select 0 to activate


    buffer[0]=buffer[1]=buffer[2]=buffer[3]=0;
    //bcm2835_spi_transfern(buffer,4);

    buffer[0] = 0x58;       //read the id
    bcm2835_spi_transfern(buffer,2);
    printf("id:%02X\n",buffer[1]);
    
    while(1)
    {
        buffer[0] = 0x50;       //read the temp
        bcm2835_spi_transfern(buffer,3);
        printf("status %02X %02X\n",buffer[1],buffer[2]);
        temp = buffer[1]; 
        temp = temp<<8;
        temp = temp + ( buffer[2] & 0xF8);
        printf("status %08x\n",temp);
        temp = temp>>3;
        temp = temp/16;
        printf("temp:%d\n",temp);
        sleep(1);
    }

    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
Exemple #23
0
int main(int argc, char **argv) {
	if (!bcm2835_init()) {
		printf("oops, could not init bcm2835\n");
		return 1;
	} else {
		printf("Initialized");
	}

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024);    // ~ 4 MHz
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

	uint8_t mosi[12] = { 0x60, 0x00 };
	uint8_t miso[12] = { 0 };

	printf("Starting");
	int data[30000];
	for (int i = 0; i < 30000; i++) {
		bcm2835_spi_transfernb(mosi, miso, 2);
//
//		printf("%d\n", miso[1] + ((miso[0] & 3) << 8));
//		data[i] = miso[1] + ((miso[0] & 3) << 10);
		data[i] = miso[1] + miso[0];
	}

	bcm2835_spi_end();
	bcm2835_close();

	FILE *fp=fopen("output2.csv","wr");

        for(int i=0;i<30000;i++) {
 	      fprintf(fp,"%d,\n",data[i]);
     	}

 

     fclose(fp);
	return 0;
}
Exemple #24
0
/**
 * Constructor of an SPICom instance. Initializes the CS0 settings by default
 */
SPICom::SPICom(bcm2835SPIClockDivider clockCS0, bcm2835SPIClockDivider clockCS1) {

    cout << "Initialising SPI object" << endl;
    this->clockCS0 = clockCS0;
    this->clockCS1 = clockCS1;
    this->chipSelect = BCM2835_SPI_CS0;

    if (!bcm2835_init())
        cerr << "Error during initialization of bcm2835 library!!";


    if (!bcm2835Initialized)
        bcm2835_spi_begin();

    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
    bcm2835_spi_setClockDivider(clockCS0);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW);

    uint8_t readData = CS0_transfer('H'); //Send RPi heartbeat char 'H'
    if (readData != 'h') {
        cout << "Arduino doesnt have 'h' in SPDR .. waiting 3ms" << "\n";
        cerr << "instead it has: " << readData << "\n";
        usleep(3000); // Wait 3ms if no comm with Arduino

        readData = CS0_transfer('H'); //Send RPI heartbeat char 'H'
        if (readData != 'h') {
            cerr << "Arduino again doesnt have 'h' in SPDR .. Aborting communication" << "\n";
            cerr << "instead it has: " << readData << "\n";
            throw "SPI initializazion error";
        }
    }


    readData = CS0_transfer('E'); //Send RPi EOT
    cout << "Arduino communication initialized\n";

    time = micros();
}
uint8_t HW_init(uint32_t spi_speed, uint8_t gpio) {
	uint16_t sp;

	sp=(uint16_t)(250000L/spi_speed);
	if (!bcm2835_init()) {
		fprintf(stderr, "Can't init bcm2835!\n");
		return 1;
	}
	if (gpio<28) {
		bcm2835_gpio_fsel(gpio, BCM2835_GPIO_FSEL_OUTP);
		bcm2835_gpio_write(gpio, LOW);
	}

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
	bcm2835_spi_setClockDivider(sp); // The default
	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default
	return 0;
}
Exemple #26
0
int mcp3008_adc_read(uint8_t single_diff, uint8_t channel, uint16_t *adcout) {

  if(single_diff != MCP3008_ADC_SINGLE && single_diff != MCP3008_ADC_DIFF )
    return -1;
  
  if(channel > ADC_7 )
    return -1;
 
bcm2835_spi_begin();
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); // The default
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); // ~ 4 MHz
  bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); // 

// Active the CS pin
  
  MCP3008_CS_CLR;
 // SleepMs(1);
  
  
  uint8_t tx[10] = { MCP3008_SPI_START_BYTE, (single_diff|channel)<<4 };
	uint8_t rx[10] = { 0 };
  bcm2835_spi_transfernb(tx, rx, 3);
  

  bcm2835_spi_end();

  
          
  // 0100 aaa r/w 
  
  
	
  
  MCP3008_CS_SET;
  
  *adcout = ((rx[1]&0x03) << 8) | rx[2];
          
  return (EXIT_SUCCESS);
}
Exemple #27
0
void
wiring_init() 
{

	if (!bcm2835_init()) {
		printf("bcm2835 init failed\n");
		exit(errno);
	}


	bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);    // 4Mhz clock
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

	
	bcm2835_gpio_fsel(WIRING_NRF_PROG_PIN, BCM2835_GPIO_FSEL_OUTP);	
	bcm2835_gpio_fsel(WIRING_NRF_RESET_PIN, BCM2835_GPIO_FSEL_OUTP);

}
int main(int argc, char *argv[])
{
	if (1 == argc) {
		printf("Enter text as an argument.\n");
		return 1;
	}

	if (!bcm2835_init()) {
		printf("Unable to init bcm2835.\n");
		return 2;
	}

	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); // The default
	bcm2835_gpio_fsel(Max7219_pinCS, BCM2835_GPIO_FSEL_OUTP); 
	bcm2835_gpio_write(disp1[0][0],HIGH);
	Delay_xms(50);
	Init_MAX7219();

	size_t index = 0;
	while (index < strlen(argv[1])) {
		// Retrive first letter
		char letter1 = toupper(argv[1][index]);
		index++;
		// Retrieve second letter
		// If it does not exist use space
		char letter2 = (index < strlen(argv[1])) ? toupper(argv[1][index]) : (char) 0;
		led_print(find_char(letter1), find_char(letter2));
		Delay_xms(1000);
		index++;
	}
	// Clear screen
	led_print(36, 36);
	bcm2835_spi_end();
	bcm2835_close();
	return 0;
}
Exemple #29
0
int main(int argc, char **argv)
{
  if (!bcm2835_init()) {
    printf("bcm2835_init failed. Are you running as root??\n");

  } else if (!bcm2835_spi_begin()) {
    printf("bcm2835_spi_begin failed\n");

  } else {
    // List of all CS line where module can be connected
    // GPIO6, GPIO8/CE0, GPIO7/CE1, GPIO26
    uint8_t CS_pins[] = {6, 7, 8, 26};
    uint8_t i;

    // Init SPI
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default

    // We control CS line manually don't assert CEx line!
    bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);

    // Drive all CS line as output and set them High to avoid any conflict
    for ( i=0; i<sizeof(CS_pins); i++) {
      bcm2835_gpio_fsel (CS_pins[i], BCM2835_GPIO_FSEL_OUTP );
      bcm2835_gpio_write(CS_pins[i], 1 );
    }

    // Now try to detect all modules
    for ( i=0; i<sizeof(CS_pins); i++) {
      readModuleVersion( CS_pins[i] ); 
    }

    bcm2835_spi_end();
  }

  bcm2835_close();
}
/*
	Name: spi_open
	Description: Init bcm2835 lib & config SPI . Print to stdout bcm2835 lib version
	Parameters:
	Returns:
		0 - on success, non-zero - fail
*/
int spi_open(void) {
	int r;
	uint32_t v;
	
	r = bcm2835_init(); // return 0 on fail
	if (r != 1) return -1;
	
	bcm2835_spi_begin();
	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // MSB bit goes first
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // mode 0
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_8); // SPI_CLK 31.25MHz - MAX
    //bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);
    bcm2835_spi_chipSelect(BCM2835_SPI_CS1); // CS1 - for now, with TP CS0 will be used too
	// setup both SPI.CSx
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // CS0 - active Low
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW); // CS1 - active Low
	
	v = bcm2835_version();
	fprintf(stdout, "bcm2835 library version: %u (0x%08x)\n", v,v);
	
	return 0; // OK!
	
}