Example #1
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();	
}
Example #2
0
/*
*	Function: pud_test
* Description: be used to test input pull-down and pull-up related functions
*/
void pud_test(void)
{
	uint8_t value;
	int i;
	
	printf("--------------->Start Test Pin PUD<---------------\n");
	for(i=0; i < ARRAYLEN; i++)
  	{
  		bcm2835_gpio_fsel(bplusGpio[i], BCM2835_GPIO_FSEL_INPT); //input
  	}
	for(i=0; i< ARRAYLEN; i++)
	{
		bcm2835_gpio_set_pud(bplusGpio[i], BCM2835_GPIO_PUD_UP);
		bcm2835_delay(500);
		value = bcm2835_gpio_lev(bplusGpio[i]);
		if(1 != value)
			printf("BCM pin%d:expect value %d and actual value %d\n", bplusGpio[i], 1, value);
		
		bcm2835_gpio_set_pud(bplusGpio[i], BCM2835_GPIO_PUD_DOWN);
		bcm2835_delay(500);
		value = bcm2835_gpio_lev(bplusGpio[i]);
		if(0 != value)
			printf("BCM pin%d:expect value %d and actual value %d\n", bplusGpio[i], 0, value);
	}	
	printf("--------------->Test Over Of Pin PUD<---------------\n");
	
}
Example #3
0
void bcm2835_spi_begin(void)
{
    // Set the SPI0 pins to the Alt 0 function to enable SPI0 access on them
    bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_ALT0); // CE1
    bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_ALT0); // CE0
    bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_ALT0); // MISO
    bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0); // MOSI
    bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_ALT0); // CLK
    
    // Set the SPI CS register to the some sensible defaults
    volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
    bcm2835_peri_write(paddr, 0); // All 0s
    
    // Clear TX and RX fifos
    bcm2835_peri_write_nb(paddr, BCM2835_SPI0_CS_CLEAR);
}
Example #4
0
int main (int argc, char *argv[]) {
    //load GPIO and set the pin to output
    bcm2835_init();
    bcm2835_gpio_fsel(21, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_set_pud(21, BCM2835_GPIO_PUD_UP);

    //write low to GPIO
    bcm2835_gpio_write(21, LOW);

    //create some tracking value
    timespec ts;
    bool ready = true;

    //repeat this forever
    while(true) {
        //get the current time from the system clock
        clock_gettime(CLOCK_REALTIME, &ts);

        //if we are in the first half of the second, set ready to true
        if(ts.tv_nsec < 500000000)
            ready = true;
        //first time we hit second half of the second
        else if(ready == true) {
            ready = false;
            //write HIGH to GPIO
            bcm2835_gpio_write(21, HIGH);
            //wait for a moment
            delay(5);
            //write LOW to GPIO
            bcm2835_gpio_write(21, LOW);
        }
    }

    return 0;
}
char* record_burst(int time,int rate){
	
	
	sample_interval=(1000*1000/rate); //microseconds
	sample_size = time*rate;
	bcm2835_gpio_fsel(PIN_RX,BCM2835_GPIO_FSEL_INPT);
	
	char* placeholder=calloc(sample_size,sizeof(char));
	
	printf("Recording in 3\n");
	fflush(stdout);
	bcm2835_delay(1000);
	printf("Recording in 2\n");
	bcm2835_delay(1000);
	printf("Recording in 1\n");
	bcm2835_delay(1000);
	
	printf("Recording for %d seconds\n",time);
	printf("sample_size: %d\n",sample_size);
	fflush(stdout);
	
        for (i=0;i<sample_size;i++){
        	placeholder[i]=(char)bcm2835_gpio_lev(PIN_RX);
        	bcm2835_delayMicroseconds(sample_interval);
        }
	printf("record_burst returning\n");
	return placeholder;
}
Example #6
0
void close_sockets() {
	if (mux >= 0)
		close(mux);

	bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
	bcm2835_close();
}
Example #7
0
boolean ssd1306_init()
{
	ssd1306_lcdwidth  = 128;
	ssd1306_lcdheight = 64;
	_i2c_addr = 0x3C;
	cursor_y = cursor_x = 0;
	//textsize = 1;

	// De-Allocate memory for OLED buffer if any
	if (poledbuff)
		free(poledbuff);

	// Allocate memory for OLED buffer
	poledbuff = (uint8_t *) malloc ( (ssd1306_lcdwidth * ssd1306_lcdheight / 8 ));
	if (!poledbuff)
    		return false;

	// Init Raspberry PI GPIO
  	if (!bcm2835_init())
    		return false;

	// default OLED are using internal boost VCC converter
	vcc_type = SSD_Internal_Vcc;
	// Init & Configure Raspberry PI I2C
	if (bcm2835_i2c_begin()==0)
		return false;

	bcm2835_i2c_setSlaveAddress(_i2c_addr) ;

  	// Setup reset pin direction as output
  	bcm2835_gpio_fsel(OLED_I2C_RESET, BCM2835_GPIO_FSEL_OUTP);

	return ( true);
}
Example #8
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;

    // Set RPI pin P1-15 to be an input
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
    //  with a pullup
    bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
    // And a low detect enable
    bcm2835_gpio_len(PIN);

    while (1)
    {
	if (bcm2835_gpio_eds(PIN))
	{
	    // Now clear the eds flag by setting it to 1
	    bcm2835_gpio_set_eds(PIN);
	    printf("low event detect for pin 15\n");
	}

	// wait a bit
	delay(500);
    }

    return 0;
}
/* Initialize the PIR motion detection sensor on a given GPIO pin, 0 on success */
int pirmtn_init(uint8_t pin)
{
    if (!bcm2835_init()) {
        return 1;
    }

    // Set PIN to be an input
    bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);

    // with a pulldown
    bcm2835_gpio_set_pud(pin, BCM2835_GPIO_PUD_DOWN);

    // And a rising edge detect enable
    bcm2835_gpio_aren(pin);

    // Wait little while for the init jitter on the GPIO
    delay(500);

    // Now clear the eds flag by setting it to 1
    bcm2835_gpio_set_eds(pin);

    // Set the given pin as our static default one
    pirmtn_pin = pin;

    return 0;
}
Example #10
0
/**
 * @brief Configures the pins as input and returns the pin status. 
 * @param pin Pin to be configured, Available pin definitons are defined in the Utitlies.h
 * @return status Pin level can be HIGH or LOW.
 */	
PinLevel_t ReadPinStatus(PIN_t pin)
{
	bcm2835_gpio_fsel(pin,BCM2835_GPIO_FSEL_INPT);		// Set the pin to be an input
	bcm2835_gpio_set_pud(pin, BCM2835_GPIO_PUD_DOWN);		// Enable pull up.
	PinLevel_t pinLevel = (PinLevel_t)bcm2835_gpio_lev(pin);		//read voltage level on MFP pin	
	return pinLevel;
}
Example #11
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;

    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

    // Blink
    while (1)
    {
    // Turn it on
    bcm2835_gpio_write(PIN, HIGH);

    // wait a bit
    delay(500);

    // turn it off
    bcm2835_gpio_write(PIN, LOW);

    // wait a bit
    delay(500);
    }

    return 0;
}
Example #12
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;

    // Set RPI pin P1-15 to be an input
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
    //  with a pullup
    bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);

    // Blink
    while (1)
    {
	// Read some data
	uint8_t value = bcm2835_gpio_lev(PIN);
	printf("read from pin 15: %d\n", value);
	
	// wait a bit
	delay(500);
    }

    bcm2835_close();
    return 0;
}
Example #13
0
static int pwm_init(pwm_t* pwm)
{
  /* TODO: configure pwm_config (clock manager) */

  size_t off;
  int fd;

  if (init_bcm2835() == -1) return -1;

  bcm2835_gpio_fsel(CONFIG_PWM_PIN, BCM2835_GPIO_FSEL_ALT5);
  close_bcm2835();

  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd == -1)
  {
    perror("open");
    return -1;
  }

  pwm->size = PWM_MAP_SIZE;
  pwm->addr = (uintptr_t)mmap
    (NULL, PWM_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PWM_MAP_OFFSET);

  close(fd);

  if ((void*)pwm->addr == MAP_FAILED)
  {
    perror("mmap");
    return -1;
  }

  return 0;
}
int main(int argc, char **argv)
{
    uint8_t value,i;
    if (!bcm2835_init())
        return 1;
    for(i=0;i<5;i++)
    {
    bcm2835_gpio_fsel(KEY[i], BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(KEY[i], BCM2835_GPIO_PUD_UP);
    }
    while (1)
    {
        for(i=0;i<5;i++)
	{
	if(bcm2835_gpio_lev(KEY[i])==0)        
            {
              printf("press the key: %d\n", i);
              delay(500);
            }
	}
	
    }
    bcm2835_close();
    return 0;
}
Example #15
0
//  ---------------------------------------------------------------------------
//  Enables SPI function on default GPIOs.
//  ---------------------------------------------------------------------------
void bcm2835_spi_open( void )
{
    // This driver uses the default pins with GPFSEL alternative function ALT0.
    bcm2835_gpio_fsel( SPI_GPIO_CE0,  BCM2835_GPFSEL_ALT0 );
    bcm2835_gpio_fsel( SPI_GPIO_CE1,  BCM2835_GPFSEL_ALT0 );
    bcm2835_gpio_fsel( SPI_GPIO_MISO, BCM2835_GPFSEL_ALT0 );
    bcm2835_gpio_fsel( SPI_GPIO_MOSI, BCM2835_GPFSEL_ALT0 );
    bcm2835_gpio_fsel( SPI_GPIO_CLK,  BCM2835_GPFSEL_ALT0 );

    // Set SPI CS register to default values (0).
    volatile uint32_t *paddr;
    paddr = bcm2835_spi0 + BCM2835_SPI0_CS / 4;
    bcm2835_peri_write( paddr, 0 );

    // Clear FIFOs.
    bcm2835_peri_write_nb( paddr, BCM2835_SPIO_CS_CLEAR );
}
Example #16
0
void SetupGpio(uint8_t gpioIn, uint8_t gpioOut) {
    if (!bcm2835_init()) {
        exit(EXIT_FAILURE);
    }

    // Set output pin to HIGH.
    // Informs power circuit that pi is on.
    bcm2835_gpio_fsel(gpioOut, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_write(gpioOut, HIGH);

    // Set PIN_IN to be an input with a pull-down resistor
    bcm2835_gpio_fsel(gpioIn, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(gpioIn, BCM2835_GPIO_PUD_DOWN);

    // Wait a while
    bcm2835_delay(100);
}
Example #17
0
int wheelsInit(char* confPath) {
    wheelsLoadConfig(confPath);

    if (wheelsConfig == NULL) {
        fprintf(stderr,"Cannot initialise wheels module. Please load configuration first!");
        return 1;
    }
    if (!bcm2835_init()) {
        return 1;
    }
    /* Set the mode of the pins to output */
    bcm2835_gpio_fsel(wheelsConfig->leftFwd.pin,   BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(wheelsConfig->leftBack.pin,  BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(wheelsConfig->rightFwd.pin,  BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(wheelsConfig->rightBack.pin, BCM2835_GPIO_FSEL_OUTP);
    return 0;
}
Example #18
0
void bcmInitPin(gpioPinId gpio, bcm2835FunctionSelect fsel)
{
    char printBuf[100];
    sprintf(printBuf, "Initializing GPIO: GPIO%d=%d [0: input, 1:output]\n", (int)gpio, (int)fsel);
    RL_PRINT(printBuf);

    // Set GPIO pin to output
    bcm2835_gpio_fsel(gpio, fsel);
}
Example #19
0
void bcm2835_spi_begin(void)
{
    volatile uint32_t* paddr;

    /* Set the SPI0 pins to the Alt 0 function to enable SPI0 access on them */
    bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_ALT0); /* CE1 */
    bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_ALT0); /* CE0 */
    bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_ALT0); /* MISO */
    bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0); /* MOSI */
    bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_ALT0); /* CLK */
    
    /* Set the SPI CS register to the some sensible defaults */
    paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
    bcm2835_peri_write(paddr, 0); /* All 0s */
    
    /* Clear TX and RX fifos */
    bcm2835_peri_write_nb(paddr, BCM2835_SPI0_CS_CLEAR);
}
Example #20
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;
}
Example #21
0
void bcm2835_spi_begin(uint8_t cs)
{
    volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS / 4;

    DBG_MSG("IN cs=%d\n", cs);
    // Set the SPI0 pins to the Alt 0 function to enable SPI0 access on them
    // except if we need custom Chip Select Pin
    // printf("bcm2835_spi_begin -> spi_custom_cs = %d \n",cs );

    // Do we need custom chip select control or
    // drive CE1 manually (because CE1 does not work with hardware)
    if ( cs > BCM2835_SPI_CS_NONE || cs == BCM2835_SPI_CS1 ) {
        // indicate we will use a custom GPIO port
        spi_custom_cs = cs ;

        // ok hard CE1 not working, drive it manually
        if (cs == BCM2835_SPI_CS1) {
            // Dirty Hack CE1 in now custom Chip Select GPIO 26
            // the real CE1 pin
            spi_custom_cs = RPI_GPIO_P1_26;

            bcm2835_gpio_fsel(spi_custom_cs, BCM2835_GPIO_FSEL_OUTP); // BCM2835_GPIO_FSEL_OUTP=0b001
            bcm2835_gpio_write(spi_custom_cs, HIGH);
        }

        // Mask in we use custom CS (not sure it has a real effect)
        bcm2835_peri_set_bits(paddr, BCM2835_SPI_CS_NONE, BCM2835_SPI0_CS_CS);
    }
    // Ok hardware driving of chip select
    else {
        // Just in case
        spi_custom_cs = 0 ;

        // Mask in the CS bits of CS
        bcm2835_peri_set_bits(paddr, cs, BCM2835_SPI0_CS_CS);
    }

    // Now we can drive the I/O as asked
    if (spi_custom_cs == 0) {
        // Not custom CS, so hardware driven
        bcm2835_gpio_fsel(RPI_GPIO_P1_24, BCM2835_GPIO_FSEL_ALT0); // CE0
        bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_ALT0); // CE1
    } else {
        // so set custom CS as output, High level by default
        bcm2835_gpio_fsel(spi_custom_cs, BCM2835_GPIO_FSEL_OUTP); // Custom GPIO
        bcm2835_gpio_write(spi_custom_cs, HIGH);
    }

    // Classic pin, hardware driven
    bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_ALT0); // MISO
    bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0); // MOSI
    bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_ALT0); // CLK

    // Set the SPI CS register to the some sensible defaults
    bcm2835_peri_write(paddr, 0); // All 0s

    // Clear TX and RX fifos
    bcm2835_peri_write_nb(paddr, BCM2835_SPI0_CS_CLEAR);
}
Example #22
0
int main(){
time_t mytime;

 printf("Start at "); mytime = time(NULL); printf(ctime(&mytime), "\n\n"); // Чисто отладочная запись в консоль, когда была запущена программа


 bcm2835_close();		// Это надо на всякий случай ибо процесс прекращается по ctrl+c и до этой функции дело не доходит
 if (!bcm2835_init())   // Инициализация GPIO и выход с кодом "1" при неудаче
  return 1;

 bcm2835_gpio_set_pud(PIR_SIGNAL, BCM2835_GPIO_PUD_DOWN);	// Подтяжка ножки к нулю
 bcm2835_gpio_fsel(PIR_SIGNAL, BCM2835_GPIO_FSEL_INPT); 	// Установка ножки для PIR на прием

//printf("%u\n", bcm2835_gpio_lev(PIR_SIGNAL)); // Вывод значения ножки

/////////Начало цикла слежения/////////

while (1) {		// Бесконечный цикл... Надо почитать про прерывания...

 while(!bcm2835_gpio_lev(PIR_SIGNAL)) 
 	{/* Ждем первого срабатывания */}	
 	
 time_to_file_first(); // см. выше
 printf("PIR activated at "); mytime = time(NULL); printf(ctime(&mytime), "\n\n");  // То было в файл, а это в консоль
 
 
// Пауза в мс. Аналог чувствительности датчика. Нужна чтобы отсечь ложные срабатывания

 bcm2835_delay(2000);
 

// Проверяем сигнал после паузы (PIR либо потухнет, либо останется активен)
// Если сигнал остался, то в консоль выводим время и ждем пока на ножке не появится ноль

 if(bcm2835_gpio_lev(PIR_SIGNAL))
	{
	 printf("Movement detected at ");
	 mytime = time(NULL); printf(ctime(&mytime), "\n"); //Вывод даты и времени
	 time_to_file_on();		// А заодно и в файл
	 while (bcm2835_gpio_lev(PIR_SIGNAL))
	 	{/* ждем пока сигнал не прекратится */}
	 printf("Movement stopped at ");
	 mytime = time(NULL); printf(ctime(&mytime), "\n"); printf("\n\n"); //Вывод даты и времени
	 time_to_file_off();	// И опять в файл
	 }
 
			
}


// До сюда обычно дело не доходит. Но пусть пока останется.

 printf("End\n");
 return (bcm2835_close());
  
}
Example #23
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  ");

}
Example #24
0
int main()
{
    bcm2835_init();
    bcm2835_gpio_fsel(SW_g, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_fsel(SW_w1, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_fsel(SW_w2, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_fsel(SW_b1, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_fsel(SW_b2, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(SW_g, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_set_pud(SW_w1, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_set_pud(SW_w2, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_set_pud(SW_b1, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_set_pud(SW_b2, BCM2835_GPIO_PUD_UP);

    while(true)
    {
        lcd.SendData_RGB565(fb.GetFrameAddress(),128*128*2);
        usleep(50000);//50ms

        if(!bcm2835_gpio_lev(SW_b1))
        {
            //FB_Func();
            Tetris_Func();
        }
        else if(!bcm2835_gpio_lev(SW_b2))
        {
            Tetris_Func();
        }
        else if(!bcm2835_gpio_lev(SW_w1))
        {
            Tetris_Func();
        }
        else if(!bcm2835_gpio_lev(SW_w2))
        {
            Tetris_Func();
        }
        else if(!bcm2835_gpio_lev(SW_g))
        {
            shutdown();
        }
    }
}
Example #25
0
void bcm2835_uart_begin(void) {
    BCM2835_UART1->ENABLE = 0x01;
    BCM2835_UART1->CNTL = 0x00;
    BCM2835_UART1->LCR = 0x03;
    BCM2835_UART1->MCR = 0x00;
    BCM2835_UART1->IER = 0x05;
    BCM2835_UART1->IIR = 0xC6;
    BCM2835_UART1->BAUD = 270;

    // Set the GPI0 pins to the Alt 5 function to enable UART1 access on them
    bcm2835_gpio_fsel(RPI_V2_GPIO_P1_08, BCM2835_GPIO_FSEL_ALT5); // UART1_TXD
    bcm2835_gpio_fsel(RPI_V2_GPIO_P1_10, BCM2835_GPIO_FSEL_ALT5); // UART1_RXD

    // Disable pull-up/down
    bcm2835_gpio_set_pud(RPI_V2_GPIO_P1_08, BCM2835_GPIO_PUD_OFF);
    bcm2835_gpio_set_pud(RPI_V2_GPIO_P1_10, BCM2835_GPIO_PUD_OFF);

    // turn on the uart for send and receive
    BCM2835_UART1->CNTL = 3;
}
Example #26
0
void PCA9685::init(int bus, int address) {
	_i2cbus = bus;
	_i2caddr = address;
	snprintf(busfile, sizeof(busfile), "/dev/i2c-%d", bus);

	bcm2835_init(); 	//init bcm chip for power switch
	bcm2835_gpio_fsel(POWERPIN, BCM2835_GPIO_FSEL_OUTP); //set GPIO17 (pin11) as the power switch pin
	SetMainPower(true); //set power on
	reset();
	//usleep(10*1000);
}
Example #27
0
/**
*@brief this function creates pwm signal
*
*This function creates pwm signal
*@param name the name of pwm (eg: pwm_reverse or pwm_forward)
*@param gpio the gpio that will be used to produce a pwm signal
*@return
*/
void pwm_create(struct pwm_data_t *pwm, char *name, int gpio)
{
	pwm_data_init(pwm, name, gpio);
	bcm2835_gpio_fsel(gpio,BCM2835_GPIO_FSEL_OUTP);
	pthread_create(&(pwm->pt), NULL, pwm_thread, (void *) pwm);

	struct sched_param param;
	memset(&param, 0, sizeof(param));
	param.sched_priority = 40;
	pthread_setschedparam(pwm->pt, SCHED_FIFO, &param);
}
Example #28
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);

  int a[MAX_SAMPLES];
  int k;
  for (k=0;k<MAX_SAMPLES;++k)
    a[k] = 100000 + k;
 
  struct timeval tv ;
  gettimeofday (&tv, NULL) ;
  epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;

  if (!bcm2835_init()) return 1;

 // Set the pin to be an input
  bcm2835_gpio_fsel(PIN_15, BCM2835_GPIO_FSEL_INPT);  // Ausgang IR Empfänger


  // Funktioniert als IR Repeater. IR Empfangsmodul ist an IN PIN_15
  // OUT PIN_22 steuert 38kHz Oszillator
  while (1) {
    int i=0;
    int msBegin = millis();
    int msEnd = 0;
    a[i] = bcm2835_gpio_lev(PIN_15);
    if (a[i] == 0) {
      ++i;
      while (i < MAX_SAMPLES) {
        a[i] = bcm2835_gpio_lev(PIN_15);
        ++i;
        int p,q;
        for (p=0; p < 1000; ++p)
          q=p;
      }
      msEnd = millis();
      
      printf("%d - %d, %d samples, %d ms per 100 samples\n", msBegin, msEnd, MAX_SAMPLES, (msEnd-msBegin)*100/MAX_SAMPLES);
      int j;
      int nLastPrint = 0;
      for (j=0;j<MAX_SAMPLES;++j) {
        if (j>0 && a[j] != a[j-1] && a[j] == 0) {
          printf(" (%d)\n", j-nLastPrint);
          nLastPrint=j;
        }
        printf("%d", (a[j]) ? 0 : 1);
      }
      printf("\n");
    }
  }
  return 0;
}
Example #29
0
void bcm2835_i2c_begin(void)
{
#ifdef I2C_V1
    volatile uint32_t* paddr = bcm2835_bsc0 + BCM2835_BSC_DIV/4;
    // Set the I2C/BSC0 pins to the Alt 0 function to enable I2C access on them
    bcm2835_gpio_fsel(RPI_GPIO_P1_03, BCM2835_GPIO_FSEL_ALT0); // SDA
    bcm2835_gpio_fsel(RPI_GPIO_P1_05, BCM2835_GPIO_FSEL_ALT0); // SCL
#else
    volatile uint32_t* paddr = bcm2835_bsc1 + BCM2835_BSC_DIV/4;
    // Set the I2C/BSC1 pins to the Alt 0 function to enable I2C access on them
    bcm2835_gpio_fsel(RPI_V2_GPIO_P1_03, BCM2835_GPIO_FSEL_ALT0); // SDA
    bcm2835_gpio_fsel(RPI_V2_GPIO_P1_05, BCM2835_GPIO_FSEL_ALT0); // SCL
#endif    

    // Read the clock divider register
    uint16_t cdiv = bcm2835_peri_read(paddr);
    // Calculate time for transmitting one byte
    // 1000000 = micros seconds in a second
    // 9 = Clocks per byte : 8 bits + ACK
    i2c_byte_wait_us = ((float)cdiv / BCM2835_CORE_CLK_HZ) * 1000000 * 9;
}
Example #30
0
int main(int argc, char *argv[])
{ 
 
  if (!bcm2835_init())
  {
        printf("Failed to init bcm2835\n");
  	return 1;
  }
  // Set the pin to be an output
  bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

  int rep;
  char *on_or_off = NULL;
 
  on_or_off = argv[1];  

  if (!(on_or_off))
     {
    	printf("Usage %s on|off \n",argv[0]);
        exit(2);	
     }

        usleep(500);
	bcm2835_gpio_write(PIN, LOW);
        usleep(8000);
  for (rep=0; rep<10; rep++);
  //recall INVERSION out a 1 to ge a zero!
  {
     {
	bcm2835_gpio_write(PIN, LOW);
        usleep(8000);
  //above is gap between burst

        bgas_main(); 

//########################################################
	if (strcmp(on_or_off,"on")==0){	
          bgas_on();
/*        # It is on */

	} else if (strcmp(on_or_off,"off") == 0) {
          bgas_off();
/*        # It is off */
        }
// ##################################################################
} 
//   printf ("loop done\n");
}
	bcm2835_gpio_write(PIN, LOW);
        bcm2835_close();
        return 0;

} // main