void Write_Max7219_byte(uchar DATA) { bcm2835_gpio_write(Max7219_pinCS,LOW); bcm2835_spi_transfer(DATA); }
void drvGPIO_setOutput(BOOL state) { bcm2835_gpio_write(PIN, state ? HIGH : LOW); }
void ArduiPi_OLED::begin( void ) { uint8_t multiplex; uint8_t chargepump; uint8_t compins; uint8_t contrast; uint8_t precharge; constructor(oled_width, oled_height); // Setup reset pin direction (used by both SPI and I2C) if (rst >= 0) { bcm2835_gpio_fsel(rst, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(rst, HIGH); // VDD (3.3V) goes high at start, lets just chill for a ms usleep(1000); // bring reset low bcm2835_gpio_write(rst, LOW); // wait 10ms usleep(10000); // bring out of reset bcm2835_gpio_write(rst, HIGH); } // depends on OLED type configuration if (oled_height == 32) { multiplex = 0x1F; compins = 0x02; contrast = 0x8F; } else { #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) { multiplex = 0x5F; compins = 0x12; contrast = 0x53; } else // So 128x64 #endif { multiplex = 0x3F; compins = 0x12; if (oled_type == OLED_SH1106_I2C_128x64) contrast = 0x80; else contrast = (vcc_type==SSD_External_Vcc?0x9F:0xCF); } } if (vcc_type == SSD_External_Vcc) { chargepump = 0x10; precharge = 0x22; } else { chargepump = 0x14; precharge = 0xF1; } #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) sendCommand(SSD1327_Set_Command_Lock, 0x12); // Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands #endif sendCommand(SSD_Display_Off); sendCommand(SSD_Set_Muliplex_Ratio, multiplex); #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) { sendCommand(SSD1327_Set_Display_Clock_Div, 0x01); sendCommand(SSD1327_Set_Display_Start_Line , 0 ); sendCommand(SSD1327_Set_Display_Offset, 96 ); sendCommand(SSD_Set_Segment_Remap , 0x46); sendCommand(0xAB); // set vdd internal sendCommand(0x01); // sendCommand(0xB1); // Set Phase Length sendCommand(0X51); // sendCommand(0xB9); // sendCommand(0xBC); // set pre_charge voltage/VCOMH sendCommand(0x08); // (0x08); sendCommand(0xBE); // set VCOMH sendCommand(0X07); // (0x07); sendCommand(0xB6); // Set second pre-charge period sendCommand(0x01); // sendCommand(0xD5); // enable second precharge and enternal vsl sendCommand(0X62); // (0x62); // Set Normal Display Mode sendCommand(SSD1327_Normal_Display); // Row Address // Start 0 End 95 sendCommand(SSD1327_Set_Row_Address, 0, 95); // Column Address // Start from 8th Column of driver IC. This is 0th Column for OLED // End at (8 + 47)th column. Each Column has 2 pixels(segments) sendCommand(SSD1327_Set_Column_Address, 8, 0x37 ); // Map to horizontal mode sendCommand(0xA0); // remap to sendCommand(0x46); // Vertical mode // Init gray level for text. Default:Brightest White grayH= 0xF0; grayL= 0x0F; } else #endif // SEEED if (oled_type == OLED_SH1106_I2C_128x64) { sendCommand(SSD1306_Set_Lower_Column_Start_Address|0x02); /*set lower column address*/ sendCommand(SSD1306_Set_Higher_Column_Start_Address); /*set higher column address*/ sendCommand(SSD1306_Set_Start_Line); /*set display start line*/ sendCommand(SH1106_Set_Page_Address); /*set page address*/ sendCommand(SSD_Set_Segment_Remap|0x01); /*set segment remap*/ sendCommand(SSD1306_Normal_Display); /*normal / reverse*/ sendCommand(0xad); /*set charge pump enable*/ sendCommand(0x8b); /*external VCC */ sendCommand(0x30); /*0X30---0X33 set VPP 9V liangdu!!!!*/ sendCommand(SSD1306_Set_Com_Output_Scan_Direction_Remap); /*Com scan direction*/ sendCommand(SSD1306_Set_Display_Offset); /*set display offset*/ sendCommand(0x00); /* 0x20 */ sendCommand(SSD1306_Set_Display_Clock_Div); /*set osc division*/ sendCommand(0x80); sendCommand(SSD1306_Set_Precharge_Period); /*set pre-charge period*/ sendCommand(0x1f); /*0x22*/ sendCommand(SSD1306_Set_Com_Pins); /*set COM pins*/ sendCommand(0x12); sendCommand(SSD1306_Set_Vcomh_Deselect_Level); /*set vcomh*/ sendCommand(0x40); } else { sendCommand(SSD1306_Charge_Pump_Setting, chargepump); sendCommand(SSD1306_Set_Memory_Mode, 0x00); // 0x20 0x0 act like ks0108 sendCommand(SSD1306_Set_Display_Clock_Div, 0x80); // 0xD5 + the suggested ratio 0x80 sendCommand(SSD1306_Set_Display_Offset, 0x00); // no offset sendCommand(SSD1306_Set_Start_Line | 0x0); // line #0 // use this two commands to flip display sendCommand(SSD_Set_Segment_Remap | 0x1); sendCommand(SSD1306_Set_Com_Output_Scan_Direction_Remap); sendCommand(SSD1306_Set_Com_Pins, compins); sendCommand(SSD1306_Set_Precharge_Period, precharge); sendCommand(SSD1306_Set_Vcomh_Deselect_Level, 0x40); // 0x40 -> unknown value in datasheet sendCommand(SSD1306_Entire_Display_Resume); sendCommand(SSD1306_Normal_Display); // 0xA6 // Reset to default value in case of // no reset pin available on OLED, sendCommand( SSD_Set_Column_Address, 0, 127 ); sendCommand( SSD_Set_Page_Address, 0, 7 ); } sendCommand(SSD_Set_ContrastLevel, contrast); stopscroll(); // Empty uninitialized buffer clearDisplay(); // turn on oled panel sendCommand(SSD_Display_On); // wait 100ms usleep(100000); }
void hab_spi_raise_cs(void) { bcm2835_gpio_write(aux.pinA, LOW); bcm2835_gpio_write(aux.pinB, LOW); }
/** * @brief Write a logic level to the pin. The pin should be confgured as output. * @param pin Pin to be configured, Available pin definitons are defined in the Utitlies.h * @param level Logic level can be HIGH or LOW. * @return none */ void digitalWrite(PIN_t pin, unsigned char level) { bcm2835_gpio_write(pin, level); // set to level }
int main(int argc, char *argv[]) { FILE *file; //input spidev0_0 FILE *file2; //input spidev1_1 file = fopen("input0.txt", "a+"); //apend file file2 = fopen("input1.txt", "a+"); //initialize led gpio interface if (!bcm2835_init()) return 1; bcm2835_gpio_fsel(LED_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(NEURON_SIGNAL, BCM2835_GPIO_FSEL_OUTP); int fd; int fd2; parse_opts(argc, argv); fd = open(device, O_RDWR); fd2 = open(device2, O_RDWR); /* * spi mode */ ioctl(fd, SPI_IOC_WR_MODE, &mode); ioctl(fd, SPI_IOC_RD_MODE, &mode); ioctl(fd2, SPI_IOC_WR_MODE, &mode); ioctl(fd2, SPI_IOC_RD_MODE, &mode); /* * bits per word */ ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); ioctl(fd2, SPI_IOC_WR_BITS_PER_WORD, &bits); ioctl(fd2, SPI_IOC_RD_BITS_PER_WORD, &bits); /* * max speed hz */ ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); ioctl(fd2, SPI_IOC_WR_MAX_SPEED_HZ, &speed); ioctl(fd2, SPI_IOC_RD_MAX_SPEED_HZ, &speed); printf("spi mode: %d\n", mode); printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); //Begin Transfer Function int counter = 0; uint16_t rx_buffer[1000]; uint16_t rx_buffer2[1000]; char str[16]; //transmit data uint8_t tx[] = { 0xFF, 0xFF, }; //initialize receive array uint8_t rx[ARRAY_SIZE(tx)] = {0, }; uint8_t rx2[ARRAY_SIZE(tx)] = {0, }; //setup transfer object struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx, .len = ARRAY_SIZE(tx), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; struct spi_ioc_transfer tr2 = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx2, .len = ARRAY_SIZE(tx), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; //sets timer for the LED int timer_flag = 0; //sets timer between input signal pulses int input_flag = 0; //tells whether or not a peak has been detected on one of the channels int peak_flag1 = 0; int peak_flag2 = 0; //counts peaks when both flags are not high int peak_count1 = 0; int peak_count2 = 0; int nSig = 1; //Corresponds to NEURON_SIGNAL time_t startTime = time(NULL); bcm2835_gpio_write(NEURON_SIGNAL, HIGH); char timebuffer[30]; struct timeval tv; time_t curtime; while(1) { gettimeofday(&tv, NULL); curtime=tv.tv_sec; strftime(timebuffer,30,"%m-%d-%Y %T.",localtime(&curtime)); printf("%s%ld\n",timebuffer,tv.tv_usec); //send the voltage source to neurons if((nSig != 1) && (input_flag == 1) && ((time(NULL) - startTime) > 500)) bcm2835_gpio_write(NEURON_SIGNAL, HIGH); nSig = 1; //acquire data from first input 0_0 ioctl(fd, SPI_IOC_MESSAGE(1), &tr); /*incoming stream consists of 14 bits, MSB first. 2 setup bits followed by the 12 bit digital input. Following bits are garbage.*/ uint16_t received = ((uint16_t)rx[0] << 8) | (uint16_t)rx[1]; received = received & 0011111111111111; received = received >> 2; rx_buffer[counter] = received; //acquire data from second input 1_1 ioctl(fd2, SPI_IOC_MESSAGE(1), &tr2); /*incoming stream consists of 14 bits, MSB first. 2 setup bits followed by the 12 bit digital input. Following bits are garbage.*/ uint16_t received2 = ((uint16_t)rx2[0] << 8) | (uint16_t)rx2[1]; received2 = received & 0011111111111111; received2 = received >> 2; rx_buffer2[counter] = received2; if((nSig == 1) && (timer_flag == 0)){ if((rx_buffer[counter] > 100) && (peak_flag1 == 0)) peak_flag1 = 1; else if((rx_buffer[counter] < 100) && (peak_flag1 == 1)){ peak_count1 = peak_count1 + 1; peak_flag1 = 0; } if((rx_buffer2[counter] > 100) && (peak_flag2 == 0)) peak_flag2 = 1; else if(rx_buffer2[counter] < 100 && peak_flag2 == 1){ peak_count2 = peak_count2 + 1; peak_flag2 = 0; } rx_buffer[counter+1] = 9000 + peak_count1; counter = counter + 1; } if(((peak_count1 == 3) /*|| (peak_count2 == 3)*/) && (timer_flag == 0)){ bcm2835_gpio_write(LED_PIN, HIGH); //fprintf(file, "%s\n", "OH HERRO!!!!!!!"); startTime = time(NULL); //return current time in seconds timer_flag = 1; } if((timer_flag == 1) && (time(NULL)-startTime > 3)) { bcm2835_gpio_write(LED_PIN, LOW); timer_flag = 0; peak_count1 = 0; peak_count2 = 0; printf("HERROOOOOO!!!!!!!!!!\n"); bcm2835_gpio_write(NEURON_SIGNAL, LOW); nSig = 0; startTime = time(NULL); input_flag = 1; } counter = counter + 1; //dump data from buffer into file if (counter > 1000){ int i; i = 0; while(i++ < 1000){ //fprintf(file, "Count: %d\n", peak_count1); //must move this to be inputted into the array to get realtime data //print 0_0 data into the first file fprintf(file, "%d\n", rx_buffer[i]); //print 1_1 data into the second file fprintf(file2, "%d\n", rx_buffer2[i]); //fwrite(rx_buffer, 16, 1000, file); //for future implementations... slightly faster } counter = 0; } } fclose(file); return 1; }
void blink_set_high(void){ bcm2835_gpio_write(PIN, HIGH); }
int readDHT(int type, int pin) { int counter = 0; int laststate = HIGH; int j=0; // Set GPIO pin to output bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(pin, HIGH); usleep(500000); // 500 ms bcm2835_gpio_write(pin, LOW); usleep(20000); bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT); data[0] = data[1] = data[2] = data[3] = data[4] = 0; // wait for pin to drop? while (bcm2835_gpio_lev(pin) == 1) { usleep(1); } // read data! for (int i=0; i< MAXTIMINGS; i++) { counter = 0; while ( bcm2835_gpio_lev(pin) == laststate) { counter++; //nanosleep(1); // overclocking might change this? if (counter == 1000) break; } laststate = bcm2835_gpio_lev(pin); if (counter == 1000) break; bits[bitidx++] = counter; if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > 200) data[j/8] |= 1; j++; } } #ifdef DEBUG for (int i=3; i<bitidx; i+=2) { printf("bit %d: %d\n", i-3, bits[i]); printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200); } #endif printf("data:\n"); printf(" j: %d\n", j); printf(" d:\n"); for (int i = 0; i < 5; i++) { printf(" - %x\n", data[i]); } if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { // yay! float therm, humid; switch (type) { case DHT11: therm = (float) data[2]; humid = (float) data[0]; break; case DHT22: humid = (data[0] * 256 + data[1]) / 10.0; therm = ((data[2] & 0x7F)* 256 + data[3]) / 10.0; if (data[2] & 0x80) { therm *= -1; } break; default: fprintf(stderr, "Unknown device type 0x%x.\n", type); return(1); } printf("temperature:\n"); printf(" units: C\n"); printf(" value: %.1f\n", therm); printf("humidity:\n"); printf(" units: pct\n"); printf(" value: %.1f\n", humid); } else { return(1); } return 0; }
int main(int argc, char *argv[]) { int opt; bool daemon = true; atexit(close_sockets); while ((opt = getopt(argc, argv, "m:vf")) != -1) switch (opt) { case 'm': mux = connect_block(optarg, 5678); break; case 'v': verbose = true; daemon = false; break; case 'f': daemon = false; break; default: fatal("Usage: %s: -m mux:port [-v] [-f]\n", argv[0]); } if (mux < 0) mux = connect_block("localhost", 5678); if (daemon) daemon_mode(); if (!bcm2835_init()) fatal("initialising bcm2835\n"); bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); signal(SIGINT, signal_handler); signal(SIGPIPE, SIG_IGN); struct timeval last, now; last.tv_sec = 0; char buf[256]; for (;;) { fd_set rd; FD_ZERO(&rd); FD_SET(mux, &rd); gettimeofday(&now, 0); if (now.tv_sec - last.tv_sec > idle_secs) bcm2835_gpio_write(pin, LOW); struct timeval dt; dt.tv_sec = idle_secs; dt.tv_usec = 0; if (0 > select(mux+1, &rd, 0, 0, &dt)) fatal("select: %s\n", strerror(errno)); if (FD_ISSET(mux, &rd)) { int n = sock_read_line(mux, buf, sizeof(buf)); if (verbose) printf("%d: %d [%s]\n", mux, n, buf); if (n > 0) { sensor s; s.from_csv(buf); if (s.is_wireless()) { last.tv_sec = now.tv_sec; bcm2835_gpio_write(pin, LOW); bcm2835_delay(20); bcm2835_gpio_write(pin, HIGH); } } else if (n == 0) fatal("Mux died\n"); } } }
void magnet_thread(void) { printf("Magnet thread started.\n"); bcm2835_spi_begin(); bcm2835_gpio_fsel(D6, BCM2835_GPIO_FSEL_INPT); // ATTENTION: If there's a problem in reading encoder data (the D6 and/or D5 Pins), this is the issue. Can be solved by changing the library. bcm2835_gpio_fsel(D5, BCM2835_GPIO_FSEL_INPT); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32); // 32 = 7.8125 MHz, 128 = 1.95 Mhz //bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default //bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default /* // the sensor can be configured without the eeprom by writing data into it's registers using the following code char write_buf[] = {0xD2, 0x0B, 0x00}; // Write data: first byte is op code (D2 for Register write), second is address third is data bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfern(write_buf, sizeof(write_buf)); bcm2835_gpio_write(PA0, HIGH); // repeat the above code for settting up values of all the registers */ while(start == 0) {} bcm2835_gpio_fsel(D6, BCM2835_GPIO_FSEL_INPT); // ATTENTION: If there's a problem in reading encoder data (the D6 and/or D5 Pins), this is the issue. Can be solved by changing the library. bcm2835_gpio_fsel(D5, BCM2835_GPIO_FSEL_INPT); while(1) { if(sample_magnet) { //code for obtaining value from iC-MU char mag_buf[] = {0xF5, 0x00}; // Data to send: first byte is op code, rest depends on the opcode char mag_in[] = {0x00, 0x00}; bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfernb(mag_buf, mag_in, sizeof(mag_in)); bcm2835_gpio_write(PA0, HIGH); // printf("OUT at %X is %X\n", mag_buf[0], mag_buf[1]); // printf("IN at %X is %X\n", mag_in[0], mag_in[1]); if(mag_in[1] == 0x80) printf(""); else printf("DATA NOT VALID\n"); char status[] = {0xA6}; char status_in[] = {0x00, 0x00, 0x00}; bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfernb(status, status_in, sizeof(status_in)); bcm2835_gpio_write(PA0, HIGH); // printf("OUT at %X is %X\n", status[0], status[1]); // printf("status is %X and data is %X, %X, %X, %X, %X, %X\n", status_in[0], status_in[1], status_in[2], status_in[3], status_in[4], status_in[5], status_in[6]); float mag_reading = (256.0 * status_in[1]) + status_in[2]; // code for calculating xd float mag_position = (360.0 * mag_reading) / 65536.0; // printf("mag: Reading: %f, angle: %f, read: %X, %X\n", mag_reading, mag_position, status_in[1], status_in[2]); // xd = pow(sin(mag_reading),-0.5); xd = mag_position / 2.0; // xd = 100.0; //bcm2835_gpio_write(MAG_PIN, LOW); // reset sampling flag sample_magnet = 0; //set magnet flag high magnet_flag = 1; } } // bcm2835_spi_end(); }
void digitalWrite(unsigned char pin, unsigned char value) { bcm2835_gpio_write(pin,value); }
/*static*/ void GpioPlateformImplementation::internalSetOutput( const int pin , const GpioState state ) { bcm2835_gpio_write( pin, state); }
void Motor::off() { bcm2835_gpio_write(f, LOW); bcm2835_gpio_write(b, LOW); }
void Motor::brake() { bcm2835_gpio_write(f, HIGH); bcm2835_gpio_write(b, HIGH); }
void encoder_thread(void) { bcm2835_gpio_fsel(ENC_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(RST_COUNT, BCM2835_GPIO_FSEL_OUTP); // reset count bcm2835_gpio_write(RST_COUNT, LOW); // setting modes of counter pins bcm2835_gpio_fsel(OE_COUNT, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_set_pud(OE_COUNT, BCM2835_GPIO_PUD_UP); //pull-up for eoutput enable bcm2835_gpio_fsel(SEL1, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(SEL2, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(D0, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D1, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D2, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D3, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D4, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D5, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D6, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(D7, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(D0, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D1, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D2, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D3, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D4, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D5, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D6, BCM2835_GPIO_PUD_DOWN); bcm2835_gpio_set_pud(D7, BCM2835_GPIO_PUD_DOWN); printf("Encoder Thread started.\n"); while(start == 0) {} bcm2835_gpio_write(RST_COUNT, HIGH); // now start counting while(1) { if(sample_encoder) { //bcm2835_gpio_write(ENC_PIN, HIGH); // for the oscilloscope // code for obtaining value from encoder IC x = calculate_encoder(); // no. of rotations // x = (x / 4.81) * 0.002; // distance traveled by slider in metres // code for calculating xf and dx // printf("enc_th: freq are : %f, %f\n", freq_diff, freq_filt); discrete_diff(); // updating value of dx by calling practical differentiator low_pass_filter(); // update xf, the filtered value of x // printf("enc_th: DX in encoder_thread = %f\n", dx); // printf("enc_th: xf in encoder_thread = %f\n", xf); // bcm2835_gpio_write(ENC_PIN, LOW); // reset time flag sample_encoder = 0; //reset sampling flag //set encoder flag high encoder_flag = 1; // means encoder calculations done } } }
static uint8_t[] transfer(int fd, uint8_t tx[]) { int ret; uint8_t rx[ARRAY_SIZE(tx)] = {0, }; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx, .rx_buf = (unsigned long)rx, .len = ARRAY_SIZE(tx), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) pabort("can't send spi message"); for (ret = 0; ret < ARRAY_SIZE(tx); ret++) { if (!(ret % 6)) puts(""); printf("%.2X ", rx[ret]); } puts(""); return rx; } int main(int argc, char *argv[]) { int ret = 0; int fd; uint8_t tx[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x40, 0x00, 0x00, 0x00, 0x00, 0x95, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD, 0xF0, 0x0D, }; uint8_t *rx; int i = 0; // init gpio if (!bcm2835_init()) return 1; // Set gpio pin directions bcm2835_gpio_fsel(IRQ_N, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(GPIO0, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(GPIO1, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(GPIO2, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_fsel(RXANT, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(TXANT, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(EN_N, BCM2835_GPIO_FSEL_OUTP); // set gpio initial output values bcm2835_gpio_write(RXANT, HIGH); bcm2835_gpio_write(TXANT, LOW); bcm2835_gpio_write(EN_N, LOW); trxSPI(tx, rx); return ret; }
float calculate_encoder(void) { int encoder_array[32]; // setting OE for counter bcm2835_gpio_write(OE_COUNT, LOW); // reading MSB (24-31) bcm2835_gpio_write(SEL1, LOW); bcm2835_gpio_write(SEL2, HIGH); encoder_array[24] = bcm2835_gpio_lev(D0); encoder_array[25] = bcm2835_gpio_lev(D1); encoder_array[26] = bcm2835_gpio_lev(D2); encoder_array[27] = bcm2835_gpio_lev(D3); encoder_array[28] = bcm2835_gpio_lev(D4); encoder_array[29] = bcm2835_gpio_lev(D5); encoder_array[30] = bcm2835_gpio_lev(D6); encoder_array[31] = bcm2835_gpio_lev(D7); // reading 3rd Byte (16-23) bcm2835_gpio_write(SEL1, HIGH); bcm2835_gpio_write(SEL2, HIGH); encoder_array[16] = bcm2835_gpio_lev(D0); encoder_array[17] = bcm2835_gpio_lev(D1); encoder_array[18] = bcm2835_gpio_lev(D2); encoder_array[19] = bcm2835_gpio_lev(D3); encoder_array[20] = bcm2835_gpio_lev(D4); encoder_array[21] = bcm2835_gpio_lev(D5); encoder_array[22] = bcm2835_gpio_lev(D6); encoder_array[23] = bcm2835_gpio_lev(D7); // reading 2nd byte (8-15) bcm2835_gpio_write(SEL1, LOW); bcm2835_gpio_write(SEL2, LOW); encoder_array[8] = bcm2835_gpio_lev(D0); encoder_array[9] = bcm2835_gpio_lev(D1); encoder_array[10] = bcm2835_gpio_lev(D2); encoder_array[11] = bcm2835_gpio_lev(D3); encoder_array[12] = bcm2835_gpio_lev(D4); encoder_array[13] = bcm2835_gpio_lev(D5); encoder_array[14] = bcm2835_gpio_lev(D6); encoder_array[15] = bcm2835_gpio_lev(D7); // reading LSB (0-7) bcm2835_gpio_write(SEL1, HIGH); bcm2835_gpio_write(SEL2, LOW); encoder_array[0] = bcm2835_gpio_lev(D0); encoder_array[1] = bcm2835_gpio_lev(D1); encoder_array[2] = bcm2835_gpio_lev(D2); encoder_array[3] = bcm2835_gpio_lev(D3); encoder_array[4] = bcm2835_gpio_lev(D4); encoder_array[5] = bcm2835_gpio_lev(D5); encoder_array[6] = bcm2835_gpio_lev(D6); encoder_array[7] = bcm2835_gpio_lev(D7); // reset OE value bcm2835_gpio_write(OE_COUNT, HIGH); // convert data to decimal int count = 0; int i; for (i = 0; i < 32; i++) { // printf("for loop entered, Di = %d \n",encoder_array[i]); count = count + (encoder_array[i] * pow(2,i)); //printf("Bit %d is = %d \n",i,encoder_array[i]); } if(count > 1073741824) // because when motor moves in opposite direction then count becomes 2^31. count = 0; // -1 * count; float rotation = (float)count / 4096.0; // printf("Decimal value of x = %d \n",count); // printf("Rotations = %f \n",rotation); return rotation; }
void GPIO::write(uint16_t pin, GPIO::Level_e level) { bcm2835_gpio_write((uint8_t)pin, level == LEVEL_LOW ? LOW : HIGH); }
int main(int argc, char** argv) { int one = 1, client_fd; int rdlen; struct sockaddr_in svr_addr, cli_addr; socklen_t sin_len = sizeof (cli_addr); struct tm *u; char *f; time_t timer; // Init DAC and SPI int ret = bcm2835_init(); if (ret != 1) { fprintf(stderr, "Error in bcm2835_init()\n"); return -1; } fprintf(stderr, "bcm2835_init()\n"); ret = bcm2835_spi_begin(); if (ret != 1) { fprintf(stderr, "Error in spi begin! ret code = %d\n", ret); return -1; } //setting SPI and CS bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE1); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); bcm2835_gpio_fsel(GPIO12, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(GPIO12, HIGH); if ((argc >1) && (strncmp(argv[1], "init", 4) == 0)) { fprintf(stderr, "init 5790\n" ); init_DAC(); } else { fprintf(stderr, "start without init 5790\n" ); } int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) err(1, "can't open socket"); setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int)); svr_addr.sin_family = AF_INET; svr_addr.sin_addr.s_addr = INADDR_ANY; svr_addr.sin_port = htons(port); if (bind(sock, (struct sockaddr *) &svr_addr, sizeof (svr_addr)) == -1) { close(sock); err(1, "Can't bind"); return 0; } double setval; uint16_t dsetval; listen(sock, 5); while (1) { client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len); // printf("got connection\n"); if (client_fd == -1) { // perror("Can't accept"); continue; } timer = time(NULL); u = localtime(&timer); f = settime(u); printf("\n####################################\n %s", f); rdlen = read(client_fd, answer, 2000); printf(" request: %s \n", answer); strstart = 0; strpos = 0; /* nword = ""; */ getword(); strcpy(metod, nword); getword(); strcpy(uri, nword); getword(); strcpy(protocol, nword); printf("metod='%s' uri='%s' protocol='%s'\n", metod, uri, protocol); memset(resp1, 0, 1100); strcpy(resp1, response); if (strncmp(uri, "/get", 4) == 0) { val = 123.876; sprintf(nword, "%f}", val); strcat(resp1, nword); printf("get=%s\n", nword); } if (strncmp(uri, "/setabs=", 7) == 0) { memset(nword, 0, 100); strncpy(nword, uri + 8, 20); strcat(resp1, nword); printf("setabs=%s\n", nword); long int dsetval; dsetval=0; sscanf(nword,"%d",&dsetval); printf("setabs u = %d \n", dsetval); // double Vout = atof(argv[1]) - 1e-6; // printf("1 u = \n"); // long int DAC_code = 1048576.0*(Vout + VREFN) / (VREFP - VREFN); // printf("2 u = %d \n", DAC_code); long int DAC_code = dsetval; printf("3 dac code = %d \n", DAC_code); char msb[3] = {0}; printf("4 u = %d \n", DAC_code); msb[0] = DAC_code / (256*256); msb[1] = (DAC_code - 256*256*msb[0])/256; msb[2] = (DAC_code - 256*256*msb[0] - 256*msb[1]); printf("5 u = %d \n", DAC_code); printf("msb = {%d %d %d}\n", msb[0], msb[1], msb[2]); set_V(msb); } write(client_fd, resp1, sizeof (resp1) - 1); /*-1:'\0'*/ close(client_fd); } }
int main(int argc, char **argv) { sem_init(&empty, 0, MAX); sem_init(&full, 0, 0); sem_init(&mutex, 0, 1); //mutal exlusion sem_init(&tx_empty, 0, TX_MAX); sem_init(&tx_full, 0, 0); if (!bcm2835_init()){ printf("init done failed \n"); return 1; } bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40 , BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_40, LOW); bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_16); bcm2835_spi_chipSelect(BCM2835_SPI_CS0); bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); printf("init done \n"); //audio_init(); pthread_t pid, pid2; pthread_create(&pid, NULL, spiReader, NULL); pthread_create(&pid2, NULL, packetreader, NULL); pthread_create(&pid, NULL, spiWriter, NULL); /* create a UDP socket */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("cannot create socket\n"); return 0; } struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = TIMEOUT_MS; if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(timeout)) < 0) perror("setsockopt failed\n"); /* bind the socket to any valid IP address and a specific port */ memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(SERVICE_PORT); if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); return 0; } runHermesLite(); bcm2835_spi_end(); bcm2835_close(); }
void blink_set_low(void){ bcm2835_gpio_write(PIN, LOW); }
//void bcm2835_gpio_write(uint8_t pin, uint8_t on); /// Call bcm2835_gpio_write with 1 parameter /// \par Refer /// \par Modify void ope_gpio_write(void) { get_byte_code(); get_byte_code(); bcm2835_gpio_write( *((uint8_t *)(buff+1)), *((uint8_t *)(buff+2)) ); }
void ControlPin(int pin, int state) { bcm2835_gpio_write(pin, state); }
Rfid::RfidStatusCode Rfid::listen(void) { (*_pLinkToConsole)->printLog("start void Rfid::listen(void)"); uint8_t SN[10]; uint8_t SN_len=0; uint8_t use_gpio=1; uint8_t gpio=18; uint16_t CType=0; int tmp, i; char status; char str[255]; char *p; char sn_str[23]; while(_statusCode == RfidStatusCode::NONE) { status = find_tag(&CType); if(status==TAG_NOTAG) { usleep(200000); continue; } else if((status!=TAG_OK) && (status!=TAG_COLLISION)) { continue; } if(select_tag_sn(SN, &SN_len)!=TAG_OK) { continue; } p=sn_str; for(tmp=0; tmp<SN_len; tmp++) { sprintf(p, "%02x", SN[tmp]); p+=2; } *(p++)=0; /* if(use_gpio){ bcm2835_gpio_write(gpio, HIGH); } */ _outStream << "Tag found "; _outStream << "[type: " << setfill('0') << setw(4) << setbase(16) << CType << ", "; _outStream << "SNlen: " << setbase(10) << int(SN_len) << ", "; _outStream << "SN: " << int(SN) << "] "; _outStream << "SN: " << sn_str; (*_pLinkToConsole)->printOut(&_outStream); string serial(sn_str); rfidSignal(serial); p=str; PcdHalt(); if (use_gpio){ bcm2835_gpio_write(gpio, LOW); } } (*_pLinkToConsole)->printLog("end void Rfid::listen(void)"); }
int readDHT(int type, int pin, float* t, float* h) { int bits[250], data[100]; int bitidx = 0; int counter = 0; int laststate = HIGH; int j=0; // Set GPIO pin to output bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(pin, HIGH); usleep(500000); // 500 ms bcm2835_gpio_write(pin, LOW); usleep(20000); bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT); data[0] = data[1] = data[2] = data[3] = data[4] = 0; // wait for pin to drop? while (bcm2835_gpio_lev(pin) == 1) { usleep(1); } // read data! for (int i=0; i< MAXTIMINGS; i++) { counter = 0; while ( bcm2835_gpio_lev(pin) == laststate) { counter++; //nanosleep(1); // overclocking might change this? if (counter == 1000) break; } laststate = bcm2835_gpio_lev(pin); if (counter == 1000) break; bits[bitidx++] = counter; if ((i>3) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > 200) data[j/8] |= 1; j++; } } #ifdef DEBUG for (int i=3; i<bitidx; i+=2) { printf("bit %d: %d\n", i-3, bits[i]); printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200); } #endif if ((j >= 39) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { // yay! if (type == DHT11) { *t = (float)data[2]; *h = (float)data[0]; #ifdef DEBUG printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]); #endif } if (type == DHT22) { *h = data[0] * 256 + data[1]; *h /= 10; *t = (data[2] & 0x7F)* 256 + data[3]; *t /= 10.0; if (data[2] & 0x80) *t *= -1; #ifdef DEBUG printf("Temp = %.1f *C, Hum = %.1f \%\n", *t, *h); #endif }
void setFanSpeed(unsigned char speed) { if (speed == 0) { bcm2835_gpio_write(FAN_SPEED_1_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_2_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_3_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_4_PIN, LOW); fanSpeedChanged(speed); }else if (speed == 1) { bcm2835_gpio_write(FAN_SPEED_1_PIN, HIGH); bcm2835_gpio_write(FAN_SPEED_2_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_3_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_4_PIN, LOW); fanSpeedChanged(speed); } else if (speed == 2) { bcm2835_gpio_write(FAN_SPEED_1_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_2_PIN, HIGH); bcm2835_gpio_write(FAN_SPEED_3_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_4_PIN, LOW); fanSpeedChanged(speed); } else if (speed == 3) { bcm2835_gpio_write(FAN_SPEED_1_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_2_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_3_PIN, HIGH); bcm2835_gpio_write(FAN_SPEED_4_PIN, LOW); fanSpeedChanged(speed); } else if (speed == 4) { bcm2835_gpio_write(FAN_SPEED_1_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_2_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_3_PIN, LOW); bcm2835_gpio_write(FAN_SPEED_4_PIN, HIGH); fanSpeedChanged(speed); } }
// this is a simple test program that prints out what it will do rather than // actually doing it int main(int argc, char **argv) { // Be non-destructive bcm2835_set_debug(1); if (!bcm2835_init()) return 1; // Configure some GPIO pins fo some testing // Set RPI pin P1-11 to be an output bcm2835_gpio_fsel(RPI_GPIO_P1_11, BCM2835_GPIO_FSEL_OUTP); // Set RPI pin P1-15 to be an input bcm2835_gpio_fsel(RPI_GPIO_P1_15, BCM2835_GPIO_FSEL_INPT); // with a pullup bcm2835_gpio_set_pud(RPI_GPIO_P1_15, BCM2835_GPIO_PUD_UP); // And a low detect enable bcm2835_gpio_len(RPI_GPIO_P1_15); // and input hysteresis disabled on GPIOs 0 to 27 bcm2835_gpio_set_pad(BCM2835_PAD_GROUP_GPIO_0_27, BCM2835_PAD_SLEW_RATE_UNLIMITED|BCM2835_PAD_DRIVE_8mA); #if 1 // Blink while (1) { // Turn it on bcm2835_gpio_write(RPI_GPIO_P1_11, HIGH); // wait a bit bcm2835_delay(500); // turn it off bcm2835_gpio_write(RPI_GPIO_P1_11, LOW); // wait a bit bcm2835_delay(500); } #endif #if 0 // Read input while (1) { // Read some data uint8_t value = bcm2835_gpio_lev(RPI_GPIO_P1_15); printf("read from pin 15: %d\n", value); // wait a bit bcm2835_delay(500); } #endif #if 0 // Look for a low event detection // eds will be set whenever pin 15 goes low while (1) { if (bcm2835_gpio_eds(RPI_GPIO_P1_15)) { // Now clear the eds flag by setting it to 1 bcm2835_gpio_set_eds(RPI_GPIO_P1_15); printf("low event detect for pin 15\n"); } // wait a bit bcm2835_delay(500); } #endif if (!bcm2835_close()) return 1; return 0; }
/* // ******************************************************** * */ void chipSelect(uint8_t DIS_num) { switch (DIS_num) { case (DIS1): bcm2835_gpio_write(DIS_CS1, LOW); bcm2835_gpio_write(DIS_CS2, HIGH); bcm2835_gpio_write(DIS_CS3, HIGH); bcm2835_gpio_write(DIS_CS4, HIGH); break; case (DIS2): bcm2835_gpio_write(DIS_CS1, HIGH); bcm2835_gpio_write(DIS_CS2, LOW); bcm2835_gpio_write(DIS_CS3, HIGH); bcm2835_gpio_write(DIS_CS4, HIGH); break; case (DIS3): bcm2835_gpio_write(DIS_CS1, HIGH); bcm2835_gpio_write(DIS_CS2, HIGH); bcm2835_gpio_write(DIS_CS3, LOW); bcm2835_gpio_write(DIS_CS4, HIGH); break; case (DIS4): bcm2835_gpio_write(DIS_CS1, HIGH); bcm2835_gpio_write(DIS_CS2, HIGH); bcm2835_gpio_write(DIS_CS3, HIGH); bcm2835_gpio_write(DIS_CS4, LOW); break; case (DIS_NONE): default: bcm2835_gpio_write(DIS_CS1, HIGH); bcm2835_gpio_write(DIS_CS2, HIGH); bcm2835_gpio_write(DIS_CS3, HIGH); bcm2835_gpio_write(DIS_CS4, HIGH); break; } }
void ArduiPi_OLED::display(void) { #ifdef SEEED_I2C if (oled_type == OLED_SEEED_I2C_96x96 ) { sendCommand(SSD1327_Set_Row_Address , 0x00, 0x5F); sendCommand(SSD1327_Set_Column_Address, 0x08, 0x37); } else #endif { sendCommand(SSD1306_Set_Lower_Column_Start_Address | 0x0); // low col = 0 sendCommand(SSD1306_Set_Higher_Column_Start_Address | 0x0); // hi col = 0 sendCommand(SSD1306_Set_Start_Line | 0x0); // line #0 } uint16_t i=0 ; // pointer to OLED data buffer uint8_t * p = poledbuff; // SPI if ( isSPI()) { // Setup D/C line to high to switch to data mode bcm2835_gpio_write(dc, HIGH); // Send all data to OLED for ( i=0; i<oled_buff_size; i++) { fastSPIwrite(*p++); } // I wonder why we have to do this (check datasheet) if (oled_height == 32) { for (uint16_t i=0; i<oled_buff_size; i++) { fastSPIwrite(0); } } } // I2C else { char buff[17] ; uint8_t x ; // Setup D/C to switch to data mode buff[0] = SSD_Data_Mode; if (oled_type == OLED_SH1106_I2C_128x64) { for (uint8_t k=0; k<8; k++) { sendCommand(0xB0+k);//set page addressSSD_Data_Mode; sendCommand(0x02) ;//set lower column address sendCommand(0x10) ;//set higher column address for( i=0; i<8; i++) { for (x=1; x<=16; x++) buff[x] = *p++; fastI2Cwrite(buff, 17); } } } else { // loop trough all OLED buffer and // send a bunch of 16 data byte in one xmission for ( i=0; i<oled_buff_size; i+=16 ) { for (x=1; x<=16; x++) buff[x] = *p++; fastI2Cwrite(buff, 17); } } } }
/** * Turns Sensorian LED off. */ void LED_off(void) { bcm2835_gpio_write(LED_PIN, 0); }