Example #1
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 #2
0
File: utils.c Project: olzhas/dcg
//==============================================================================
// TODO add error messages
void PWM_init()
{
    // setting PWM_PIN as pwm from channel 0 in markspace mode with range = RANGE
    bcm2835_gpio_fsel(PWM_PIN, BCM2835_GPIO_FSEL_ALT5); // ALT5 is pwm mode
    bcm2835_pwm_set_clock(
        BCM2835_PWM_CLOCK_DIVIDER_16); // pwm freq = 19.2 / 16 MHz
    bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); // markspace mode
    bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);

    bcm2835_gpio_fsel(OE_SHIFTER, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_set_pud(
        OE_SHIFTER,
        BCM2835_GPIO_PUD_DOWN); // pull-down for output enable of logic shifters

    bcm2835_gpio_fsel(MOTOR_D3, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_set_pud(MOTOR_D3,
        BCM2835_GPIO_PUD_DOWN); // pull-down for motor enable

    bcm2835_gpio_fsel(PA0, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_set_pud(PA0, BCM2835_GPIO_PUD_UP);
    bcm2835_gpio_write(PA0, HIGH);

    bcm2835_gpio_write(OE_SHIFTER, HIGH);
    bcm2835_gpio_write(MOTOR_D3, LOW);
}
Example #3
0
int main(int argc, char **argv)
{
  if (!bcm2835_init()) return 1;

  bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_INPT);
  bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_INPT);

  bcm2835_gpio_set_pud(PIN0, BCM2835_GPIO_PUD_UP);
  bcm2835_gpio_set_pud(PIN1, BCM2835_GPIO_PUD_UP);
    
  bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_ALT5);

  bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_256);
  bcm2835_pwm_set_mode(0, 1, 1);
  bcm2835_pwm_set_range(0, 1024);

  int data=0;
  while (1)
    {
    uint8_t in0 = bcm2835_gpio_lev(PIN0);
    uint8_t in1 = bcm2835_gpio_lev(PIN1);
    if (in1 == 0) break;
    data ++;
    if(data > 1023) data=0;
    bcm2835_pwm_set_data(0, data);
    bcm2835_delay(2);
    }

  bcm2835_pwm_set_data(0, 0);
  bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_INPT);
  bcm2835_close();
  return 0;
}
Example #4
0
File: hw.c Project: muscoder/RMIUSV
void initButtons( void ){
  // Set RPI pin to be an input
  bcm2835_gpio_fsel ( BUTTON_0, BCM2835_GPIO_FSEL_INPT );
  bcm2835_gpio_fsel ( BUTTON_1, BCM2835_GPIO_FSEL_INPT );
  // with a pullup
  bcm2835_gpio_set_pud ( BUTTON_0, BCM2835_GPIO_PUD_OFF );
  bcm2835_gpio_set_pud ( BUTTON_1, BCM2835_GPIO_PUD_OFF );
}
Example #5
0
int main(int argc, char *argv[]) 
{
	bcm2835_init();
	 
	if(comparse(argc, argv) == EXIT_FAILURE) return 0;
	 
	// setting PWM_PIN as pwm from channel 0 in markspace mode with range = RANGE
	bcm2835_gpio_fsel(PWM_PIN, BCM2835_GPIO_FSEL_ALT5);  //ALT5 is pwm mode
	bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); // pwm freq = 19.2 / 16 MHz
	bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1);		     // markspace mode
	bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);
	
	
	bcm2835_gpio_fsel(OE_SHIFTER, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_set_pud(OE_SHIFTER, BCM2835_GPIO_PUD_DOWN); //pull-down for output enable of logic shifters

	bcm2835_gpio_fsel(MOTOR_D3, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_set_pud(MOTOR_D3, BCM2835_GPIO_PUD_DOWN); //pull-down for motor enable
	
	bcm2835_gpio_fsel(PA0, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_set_pud(PA0, BCM2835_GPIO_PUD_UP);
	bcm2835_gpio_write(PA0, HIGH);
	
	bcm2835_gpio_write(OE_SHIFTER, HIGH);
	bcm2835_gpio_write(MOTOR_D3, LOW);
	
	// creating and running threads
	pthread_t th1, th2, th3, th4, th5, th6, th7;
	pthread_create(&th1, NULL, (void*)encoder_time_thread, NULL);
	pthread_create(&th2, NULL, (void*)magnet_time_thread, NULL);
	pthread_create(&th3, NULL, (void*)encoder_thread, NULL);
	pthread_create(&th4, NULL, (void*)magnet_thread, NULL);
	pthread_create(&th5, NULL, (void*)energy_time_thread, NULL);
	pthread_create(&th6, NULL, (void*)calculate_energy, NULL);
	pthread_create(&th7, NULL, (void*)calculate_I_ref, NULL);
	
	bcm2835_delay(100); // delay to make sure that all threads are initialised and iC-MU is conofigured
	printf("\nPress enter to start the motor.");
	getchar(); 
	bcm2835_gpio_write(MOTOR_D3, HIGH);
	
	start = 1;
	printf("Started.\n");

	printf("\nPress enter to stop the motor.\n");
	getchar();
	bcm2835_gpio_write(MOTOR_D3, LOW);
	
	bcm2835_spi_end();
	bcm2835_close();
	
	
	return 0;
}
Example #6
0
void GPIO::setPullupMode(uint16_t pin, PullupMode_e mode)
{
    if(mode == PULLUP_ENABLED)
    {
        bcm2835_gpio_set_pud((uint8_t)pin, BCM2835_GPIO_PUD_UP);
    }
    else
    {
        bcm2835_gpio_set_pud((uint8_t)pin, BCM2835_GPIO_PUD_OFF);
    }
}
// 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 );    
}
Example #8
0
void UartInit ()
{
	/* Set pins 14 and 15 to alt function 0 */
	bcm2835_gpio_fsel(14, BCM2835_GPIO_FSEL_ALT0);
	bcm2835_gpio_fsel(15, BCM2835_GPIO_FSEL_ALT0);

	/* Disable the uart for now */
	*uart0_cr = 0;

	/* Disable pull up/pull down resistors on pins 14,15 */
	bcm2835_gpio_set_pud(14, BCM2835_GPIO_PUD_OFF);
	bcm2835_gpio_set_pud(15, BCM2835_GPIO_PUD_OFF);

	/* Clear any interrupts */
	*uart0_icr = UART_ICR_ALL;

	/* Set up the integer and fractional clock settings */
	/*
		From the BCM2835 Peripherals Guide
		http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

		(Note : this is for uart 0)
		GPIO pin 14 is TXD0 using alt function 0
		GPIO pin 15 is RXD0 using alt function 0

		"The baud rate divisor is calculated as follows: 
		Baud rate divisor BAUDDIV = (FUARTCLK/(16 Baud rate))
		where FUARTCLK is the UART reference clock frequency. 
		The BAUDDIV is comprised of the integer value IBRD and the fractional value FBRD. 
		NOTE: The contents of the IBRD and FBRD registers are not updated until 
		transmission or reception of the current character is complete."

		FBRD is a 6 bit number (0-63) to represent the fractional divisor.

		The uart clock is 3MHz, and we are going to use a fixed, 115,200 baud rate. So...
		BAUDDIV = 3,000,000 / (16 * 115,200) = 1.628
		IBRD = floor(1.628) = 1
		FBRD = 0.628 * 64   = 40
	*/
	*uart0_ibrd = 1;
	*uart0_fbrd = 40;

	/* Set the uart to 8 bits, no parity, 1 stop bit (8N1) and enable the fifos */
	*uart0_lcrh = UART_LCRH_FIFO_EN | UART_LCRH_8_BITS;

	/* Finally, enable the uart for send and receive */
	*uart0_cr = UART_CR_UART_EN | UART_CR_TX_EN | UART_CR_RX_EN;

	/* Flush the rx fifo in case there is anything in there */
	//UartFlushRxFifo();
}
Example #9
0
void SHT1x_InitPins( void ) 
{
	// SCK line as output but set to low first
		bcm2835_gpio_write(RPI_GPIO_SHT1x_SCK, LOW);
		bcm2835_gpio_fsel(RPI_GPIO_SHT1x_SCK, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_write(RPI_GPIO_SHT1x_SCK, LOW);

	// DATA to input. External pull up.
	// Set PORT to 0 => pull data line low by setting port as output
		bcm2835_gpio_set_pud(RPI_GPIO_SHT1x_DATA, BCM2835_GPIO_PUD_OFF);
	bcm2835_gpio_write(RPI_GPIO_SHT1x_DATA,LOW);
		bcm2835_gpio_fsel(RPI_GPIO_SHT1x_DATA, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_set_pud(RPI_GPIO_SHT1x_DATA, BCM2835_GPIO_PUD_OFF);
		bcm2835_gpio_write(RPI_GPIO_SHT1x_DATA,LOW);
}
Example #10
0
int main(int argc, char **argv)
{
  if (!bcm2835_init()) return 1;

  bcm2835_gpio_fsel(PIN0, BCM2835_GPIO_FSEL_INPT);
  bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_INPT);

  bcm2835_gpio_set_pud(PIN0, BCM2835_GPIO_PUD_UP);
  bcm2835_gpio_set_pud(PIN1, BCM2835_GPIO_PUD_UP);
    
  bcm2835_gpio_fsel(POUT0, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(POUT1, BCM2835_GPIO_FSEL_OUTP);

  while (1)
    {
    uint8_t in0 = bcm2835_gpio_lev(PIN0);
    uint8_t in1 = bcm2835_gpio_lev(PIN1);

    if (in0 == 0)
      {
      bcm2835_gpio_write(POUT0, HIGH);
      }
    else
      {
      bcm2835_gpio_write(POUT0, LOW);
      }
    if (in1 == 0) break;
    }

  for (int i=0; i<5; i++)
    {
    bcm2835_gpio_write(POUT0, HIGH);
    bcm2835_delay(500);

    bcm2835_gpio_write(POUT0, LOW);
    bcm2835_gpio_write(POUT1, HIGH);
    bcm2835_delay(500);

    bcm2835_gpio_write(POUT0, HIGH);
    bcm2835_delay(500);

    bcm2835_gpio_write(POUT0, LOW);
    bcm2835_gpio_write(POUT1, LOW);
    bcm2835_delay(500);
    }
  bcm2835_close();
  return 0;
}
Example #11
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 #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;
}
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 #14
0
int main(int argc, char** argv){

	if (!bcm2835_init())
        return 1;

	//Set PIN_OUT to be output
	bcm2835_gpio_fsel(PIN_OUT, BCM2835_GPIO_FSEL_OUTP);
	bcm2835_gpio_write(PIN_OUT, HIGH);
	
	// Set PIN_IN to be input with pulldown
	bcm2835_gpio_fsel(PIN_IN, BCM2835_GPIO_FSEL_INPT);
	bcm2835_gpio_set_pud(PIN_IN, BCM2835_GPIO_PUD_DOWN);
    
	run = 1;
	counter = 0;
	status = LOW;
	while(run){
		//Check if door is open or closed
        	uint8_t value = bcm2835_gpio_lev(PIN_IN);
        	if(value == HIGH)
			on_high();
		else
			on_low();
        
		//Sleep
		counter++;
        	delay(SLEEP_TIME);
	}
	return 1;
}
Example #15
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;
}
Example #16
0
int main(int argc, char **argv)
{
  // If you call this, it will not actually access the GPIO
  if (!bcm2835_init())
    return 1;
  
  // Set RPI pin to be an input
  bcm2835_gpio_fsel(RF_IRQ_PIN, BCM2835_GPIO_FSEL_INPT);
  //  with a puldown
  bcm2835_gpio_set_pud(RF_IRQ_PIN, BCM2835_GPIO_PUD_DOWN);
  // And a rising edge detect enable
  bcm2835_gpio_ren(RF_IRQ_PIN);
  
  while (1) {
    // we got it ?
    if (bcm2835_gpio_eds(RF_IRQ_PIN)) {
      // Now clear the eds flag by setting it to 1
      bcm2835_gpio_set_eds(RF_IRQ_PIN);
      printf("Rising event detect for pin GPIO%d\n", RF_IRQ_PIN);
    }
    // wait a bit
    bcm2835_delay(5); 
  }
  
  bcm2835_close();
  return 0;
}
Example #17
0
File: beep.c Project: deweyjose/rpi
int main(int argc, char **argv) {
    if (!bcm2835_init()) {
	return 1;
    }

    bcm2835_gpio_fsel(LED_PIN, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(BUZ_PIN, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(TLT_PIN, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(TLT_PIN, BCM2835_GPIO_PUD_DOWN);

    for (int i = 0; i < 100; i++) {
	uint8_t value = bcm2835_gpio_lev(TLT_PIN);
	printf("read from pin %d : %d\n", TLT_PIN, value);
	if (value == 0) {
	    bcm2835_gpio_write(LED_PIN, HIGH);
	    bcm2835_gpio_write(BUZ_PIN, LOW);
	} else {
	    bcm2835_gpio_write(LED_PIN, LOW);
	    bcm2835_gpio_write(BUZ_PIN, HIGH);
	}
	bcm2835_delay(500);
    }

    bcm2835_gpio_write(LED_PIN, LOW);
    
    bcm2835_close();
    return 0;
}
Example #18
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);
    }

    bcm2835_close();
    return 0;
}
Example #19
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 #20
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 #21
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 #22
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 #23
0
/*
*	Function: edge_test
* Description: be used to test edge detect related functions
*/
void edge_test(void)
{
	printf("--------------->Start Test dege detect<---------------\n");
	bcm2835_gpio_set_pud(EDGE_PIN, BCM2835_GPIO_PUD_UP); //enable pull-up
	bcm2835_gpio_fsel(EDGE_PIN, BCM2835_GPIO_FSEL_INPT);   //set input

	edge_detect_test(EDGE_PIN, LOW_MODE);
	edge_detect_test(EDGE_PIN, HIGH_MODE);
	edge_detect_test(EDGE_PIN, FALLING_MODE);
	edge_detect_test(EDGE_PIN, RISING_MODE);	
	printf("--------------->Test Over Of Edge Detect<---------------\n");
}
Example #24
0
int initTurntable(Turntable_t *t)
{
  printf("initTurntable start\r\n");
  if(!bcm2835_init())
    return 1;
  // Set the pin modes
  bcm2835_gpio_fsel(MOTOR_A, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(MOTOR_B, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(ENCODER, BCM2835_GPIO_FSEL_INPT);
  bcm2835_gpio_set_pud(ENCODER, BCM2835_GPIO_PUD_UP);
  bcm2835_gpio_fsel(SWITCH, BCM2835_GPIO_FSEL_INPT);
  bcm2835_gpio_set_pud(SWITCH, BCM2835_GPIO_PUD_UP);
  // Initialise members
  t->maxEncoder = 0;
  t->currentEncoder = 0;
  t->encoder = 0;
  t->limitSW = 0;
  t->ttdriver = 0;
  printf("initTurntable end\r\n");
  return 0;
}
Example #25
0
//--------------------------------------------------------------------------------------------------
// Name:      HalInit
// Function:  Setup GPIO for Raspi_LCD
//            
// Parameter: -
// Return:    -
//--------------------------------------------------------------------------------------------------
int RaspiLcdHwInit(void)
{
	int HwRev;
	
	HwRev = GetRaspberryHwRevision();
	
	if (!bcm2835_init()) return 0;
	
	// Buttons 	
	PinButton[0] = 	17;
	PinButton[1] = (HwRev < 2) ? 21 : 27;
	PinButton[2] = 22;
	PinButton[3] = 23;
	PinButton[4] = 24;
	
	bcm2835_gpio_fsel(PinButton[0],BCM2835_GPIO_FSEL_INPT)	;	// Set GPIO Pin to Input 
	bcm2835_gpio_fsel(PinButton[1],BCM2835_GPIO_FSEL_INPT)	;	// Set GPIO Pin to Input 
	bcm2835_gpio_fsel(PinButton[2],BCM2835_GPIO_FSEL_INPT)	;	// Set GPIO Pin to Input 
	bcm2835_gpio_fsel(PinButton[3],BCM2835_GPIO_FSEL_INPT)	;	// Set GPIO Pin to Input 
	bcm2835_gpio_fsel(PinButton[4],BCM2835_GPIO_FSEL_INPT)	;	// Set GPIO Pin to Input 

	bcm2835_gpio_set_pud(PinButton[0],BCM2835_GPIO_PUD_UP); 	// Enable Pullup
	bcm2835_gpio_set_pud(PinButton[1],BCM2835_GPIO_PUD_UP); 	// Enable Pullup
	bcm2835_gpio_set_pud(PinButton[2],BCM2835_GPIO_PUD_UP); 	// Enable Pullup
	bcm2835_gpio_set_pud(PinButton[3],BCM2835_GPIO_PUD_UP); 	// Enable Pullup
	bcm2835_gpio_set_pud(PinButton[4],BCM2835_GPIO_PUD_UP); 	// Enable Pullup	
	
	// LCD Display
	bcm2835_gpio_fsel(PIN_LCD_MOSI,     BCM2835_GPIO_FSEL_OUTP);	// GPIO10 Output: MOSI
	bcm2835_gpio_fsel(PIN_LCD_SCLK,     BCM2835_GPIO_FSEL_OUTP);	// GPIO11 Output: SCLK
	bcm2835_gpio_fsel(PIN_LCD_RST,      BCM2835_GPIO_FSEL_OUTP);	// GPIO25 Output: RST
	bcm2835_gpio_fsel(PIN_LCD_CS ,      BCM2835_GPIO_FSEL_OUTP);	// GPIO8  Output: CS
	bcm2835_gpio_fsel(PIN_LCD_RS,       BCM2835_GPIO_FSEL_OUTP);	// GPIO7  Output: RS
	bcm2835_gpio_fsel(PIN_LCD_BACKLIGHT,BCM2835_GPIO_FSEL_OUTP);	// GPIO18 Output: Backlight
	
	Button = ButtonMem = ButtonPressed = 0;
	
	return 1;
}
Example #26
0
void 
i2c_pins_setup(void) 
{
bcm2835_init();
bcm2835_gpio_fsel(0,BCM2835_FSEL_INPT);
bcm2835_gpio_fsel(1,BCM2835_FSEL_INPT);
bcm2835_gpio_fsel(2,BCM2835_FSEL_INPT);
bcm2835_gpio_fsel(3,BCM2835_FSEL_INPT);
bcm2835_gpio_fsel(28,BCM2835_FSEL_INPT);
bcm2835_gpio_fsel(29,BCM2835_FSEL_INPT);
// Set bus i2c 0
bcm2835_gpio_fsel(2,BCM2835_FSEL_ALT0);
bcm2835_gpio_set_pud(2,BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(3,BCM2835_FSEL_ALT0);
bcm2835_gpio_set_pud(3,BCM2835_GPIO_PUD_UP);
// Set bus i2c 1
bcm2835_gpio_fsel(28,BCM2835_FSEL_ALT0);
bcm2835_gpio_set_pud(28,BCM2835_GPIO_PUD_UP);
bcm2835_gpio_fsel(29,BCM2835_FSEL_ALT0);
bcm2835_gpio_set_pud(29,BCM2835_GPIO_PUD_UP);

}
Example #27
0
static PyObject *
PyBCM2835_gpio_set_pud(PyObject *self, PyObject *args)
{
	uint8_t pin;
	uint8_t pud;

	if (!PyArg_ParseTuple(args,"ii",&pin,&pud)) {
		return NULL;
	}

	bcm2835_gpio_set_pud(pin,pud);

	Py_RETURN_NONE;
}
Example #28
0
int main(int argc, char **argv)
{
    if (!bcm2835_init())
        return 1;
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
printf("irm test start: \n");
char turn;    
char n,p,result;
unsigned long i;
unsigned long buff[33];
while (1)
{	
	i++;delayMicroseconds(60);	
	if(bcm2835_gpio_lev(PIN)==1)
	{
	turn=1;
	}	
	else
	{
	if(turn==1)
		{
		if((n>0)&&(n<=33)) 
			{
			buff[n-1]=i;
			
			}
		n++;i=0;

		if(n==34)
			{
			n=0;
			//for(p=0;p<33;p++){printf("%d-%d;",p,buff[p]);}

			if(buff[0]>180 && buff[0]<250 && buff[1]<25 && buff[2]<25 && buff[3]<25 && buff[4]<25 && buff[5]<25 && buff[6]<25 && buff[7]<25 && buff[8]<25 && buff[9]>25 && buff[10]>25 && buff[11]>25 && buff[12]>25 && buff[13]>25) 
				{
				for(p=0;p<8;p++)
					{
					result>>=1;
					if(buff[25+p]>25) {result|=0x80;}
					}	
				printf("get the irm key code-hex ox%x \n",result);
				}
			bcm2835_delay(200);
			}
		}
	turn=0;
	}
}
void GpioPlateformImplementation::internalPinSetupInput( GPioInputType type )
{
    
    if ( _inputType == InputPullUp )
        
        bcm2835_gpio_set_pud( _pin, BCM2835_GPIO_PUD_UP);
    
    else if ( _inputType == InputPullDown )
        bcm2835_gpio_fsel( _pin, BCM2835_GPIO_PUD_DOWN );
    
    else if ( _inputType == InputDirect )
        bcm2835_gpio_fsel( _pin, BCM2835_GPIO_FSEL_INPT);

    
}
Example #30
0
// Writes an number of bytes to SPI
void spi_uart_tx(char c) {
	volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4;
	volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4;
	
	bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0);
	
	//BUG: The start bit is always 1.5 periods long, probably unfixable
	
	// This is Polled transfer as per section 10.6.1
	// BUG ALERT: what happens if we get interupted in this section, and someone else
	// accesses a different peripheral?
	
	// Clear TX and RX fifos
	bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR);
	
	// Set TA = 1
	bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA);
	
		// Maybe wait for TXD
		while (!(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))
			;
		
		// Write to FIFO, no barrier
		bcm2835_peri_write_nb(fifo, reverse_bits(c));
		
		// Read from FIFO to prevent stalling
		while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
			(void) bcm2835_peri_read_nb(fifo);
// 		bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_ALT0);
	
	// Wait for DONE to be set
	while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) {
// 		while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD)
// 			(void) bcm2835_peri_read_nb(fifo);
	}
	
// 	bcm2835_delayMicroseconds(10);
	
	// Set TA = 0, and also set the barrier
	bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA);
	
	//TODO: THe program might be interrupted in here, corrupting the character.
	
	bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_OUTP); // MOSI
	bcm2835_gpio_set(RPI_GPIO_P1_19); //idle high
	bcm2835_gpio_set_pud(RPI_GPIO_P1_19,BCM2835_GPIO_PUD_UP);
	bcm2835_delayMicroseconds(40);
}