int main(int argc, char **argv) { printf("Running ... \n"); // parse the command line if (comparse(argc, argv) == EXIT_FAILURE) return showusage (EXIT_FAILURE); if (!bcm2835_init()) { printf("bcm2835_init failed. Are you running as root??\n"); return 1; } // I2C begin if specified if (init == I2C_BEGIN) { if (!bcm2835_i2c_begin()) { printf("bcm2835_i2c_begin failed. Are you running as root??\n"); return 1; } } // If len is 0, no need to continue, but do I2C end if specified if (len == 0) { if (init == I2C_END) bcm2835_i2c_end(); printf("... done!\n"); return EXIT_SUCCESS; } bcm2835_i2c_setSlaveAddress(slave_address); bcm2835_i2c_setClockDivider(clk_div); fprintf(stderr, "Clock divider set to: %d\n", clk_div); fprintf(stderr, "len set to: %d\n", len); fprintf(stderr, "Slave address set to: %d\n", slave_address); if (mode == MODE_READ) { for (i=0; i<MAX_LEN; i++) buf[i] = 'n'; data = bcm2835_i2c_read(buf, len); printf("Read Result = %d\n", data); for (i=0; i<MAX_LEN; i++) { if(buf[i] != 'n') printf("Read Buf[%d] = %x\n", i, buf[i]); } } if (mode == MODE_WRITE) { data = bcm2835_i2c_write(wbuf, len); printf("Write Result = %d\n", data); } // This I2C end is done after a transfer if specified if (init == I2C_END) bcm2835_i2c_end(); bcm2835_close(); printf("... done!\n"); return 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; }