void getButtons(void) { int i; while(updateState == 1) { bcm2835_gpio_write(gpioSnesLatch, HIGH); bcm2835_delayMicroseconds(12); bcm2835_gpio_write(gpioSnesLatch, LOW); bcm2835_delayMicroseconds(6); buttonStatesAct[0] = !bcm2835_gpio_lev(gpioSnesData); for(i=1; i<16; i++) { bcm2835_gpio_write(gpioSnesClock, LOW); bcm2835_delayMicroseconds(6); bcm2835_gpio_write(gpioSnesClock, HIGH); bcm2835_delayMicroseconds(6); buttonStatesAct[i] = !bcm2835_gpio_lev(gpioSnesData); } for(i=0; i<12; i++) { if(buttonStatesAct[i] && (!buttonStatesLast[i]) && (callbacks_pressed[i] != NULL)) (*(callbacks_pressed[i]))(); else if((!buttonStatesAct[i]) && buttonStatesLast[i] && (callbacks_released[i] != NULL)) (*(callbacks_released[i]))(); buttonStatesLast[i] = buttonStatesAct[i]; } bcm2835_delay(16); } }
void inline bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud) { bcm2835_gpio_pud(pud); bcm2835_delayMicroseconds(10); bcm2835_gpio_pudclk(pin, 1); bcm2835_delayMicroseconds(10); bcm2835_gpio_pud(BCM2835_GPIO_PUD_OFF); bcm2835_gpio_pudclk(pin, 0); }
void *rotateServo(void *servo_thread_stuff){ struct servo_thread_data *data; data= (struct servo_thread_data *) servo_thread_stuff; while(*(data->keepgoing)==1){ bcm2835_gpio_write(data->pin_num,HIGH); bcm2835_delayMicroseconds(*(data->hightime)); bcm2835_gpio_write(data->pin_num,LOW); bcm2835_delayMicroseconds(20000); } pthread_exit(NULL); }
void GStepper(Environment *env) { // X Axis if(env->motors[0].step) bcm2835_gpio_set(X_STEP); if(env->motors[1].step) bcm2835_gpio_set(Y_STEP); if(env->motors[2].step) bcm2835_gpio_set(Z_STEP); bcm2835_delayMicroseconds (200); bcm2835_gpio_clr(X_STEP); bcm2835_gpio_clr(Y_STEP); bcm2835_gpio_clr(Z_STEP); bcm2835_delayMicroseconds (200); }
void *drivemotors(void *arg){ struct motor_params* motor = (struct motor_params*)arg; while(1){ bcm2835_gpio_write(motor->pin,HIGH); bcm2835_delayMicroseconds(motor->duty*200); bcm2835_gpio_write(motor->pin,LOW); bcm2835_delayMicroseconds((100-motor->duty)*200); } }
/************************************************************************** Function: spi_lcd_off Purpose: Turn off lcd display Returns: Note: Assumes spi_lcd_init() has been called *************************************************************************/ void spi_lcd_off(void) { u8 tx_buff[4] = { CMD_CODE, CMD_OFF, 0, 0 }; u8 rx_buff[4]; bcm2835_spi_transfernb(tx_buff, rx_buff, 2); bcm2835_delayMicroseconds(100); }
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; }
static PyObject * PyBCM2835_delay_microseconds(PyObject *self, PyObject *args) { unsigned int micros; if (!PyArg_ParseTuple(args,"i",µs)) { return NULL; } bcm2835_delayMicroseconds(micros); Py_RETURN_NONE; }
uint8_t MPU9250::AK8963_whoami(){ char response; WriteReg(MPUREG_I2C_SLV0_ADDR,AK8963_I2C_ADDR|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read. WriteReg(MPUREG_I2C_SLV0_REG, AK8963_WIA); //I2C slave 0 register address from where to begin data transfer WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81); //Read 1 byte from the magnetometer //WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81); //Enable I2C and set bytes bcm2835_delayMicroseconds(10000); response=ReadReg(MPUREG_EXT_SENS_DATA_00); //Read I2C //ReadRegs(MPUREG_EXT_SENS_DATA_00,response,1); //response=WriteReg(MPUREG_I2C_SLV0_DO, 0x00); //Read I2C return response; }
// 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); }
void MPU9250::ReadRegs( uint8_t ReadAddr, char *ReadBuf, unsigned int Bytes ) { char buff[1+Bytes]; buff[0] = ReadAddr | READ_FLAG; for(int i = 1; i < 1+Bytes; i++) buff[i] = 0x00; bcm2835_spi_chipSelect(MPU9250_PIN); // The default bcm2835_spi_transfern(buff,1+Bytes); bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); memcpy(ReadBuf,buff+1,Bytes); bcm2835_delayMicroseconds(50); }
bool MPU9250::initialize(int sample_rate_div, int low_pass_filter) { uint8_t i = 0; char MPU_Init_Data[MPU_InitRegNum][2] = { //{0x80, MPUREG_PWR_MGMT_1}, // Reset Device - Disabled because it seems to corrupt initialisation of AK8963 {0x01, MPUREG_PWR_MGMT_1}, // Clock Source {0x00, MPUREG_PWR_MGMT_2}, // Enable Acc & Gyro {low_pass_filter, MPUREG_CONFIG}, // Use DLPF set Gyroscope bandwidth 184Hz, temperature bandwidth 188Hz {0x18, MPUREG_GYRO_CONFIG}, // +-2000dps {0x08, MPUREG_ACCEL_CONFIG}, // +-4G {0x09, MPUREG_ACCEL_CONFIG_2}, // Set Acc Data Rates, Enable Acc LPF , Bandwidth 184Hz {0x30, MPUREG_INT_PIN_CFG}, // //{0x40, MPUREG_I2C_MST_CTRL}, // I2C Speed 348 kHz //{0x20, MPUREG_USER_CTRL}, // Enable AUX {0x20, MPUREG_USER_CTRL}, // I2C Master mode {0x0D, MPUREG_I2C_MST_CTRL}, // I2C configuration multi-master IIC 400KHz {AK8963_I2C_ADDR, MPUREG_I2C_SLV0_ADDR}, //Set the I2C slave addres of AK8963 and set for write. //{0x09, MPUREG_I2C_SLV4_CTRL}, //{0x81, MPUREG_I2C_MST_DELAY_CTRL}, //Enable I2C delay {AK8963_CNTL2, MPUREG_I2C_SLV0_REG}, //I2C slave 0 register address from where to begin data transfer {0x01, MPUREG_I2C_SLV0_DO}, // Reset AK8963 {0x81, MPUREG_I2C_SLV0_CTRL}, //Enable I2C and set 1 byte {AK8963_CNTL1, MPUREG_I2C_SLV0_REG}, //I2C slave 0 register address from where to begin data transfer {0x12, MPUREG_I2C_SLV0_DO}, // Register value to continuous measurement in 16bit {0x81, MPUREG_I2C_SLV0_CTRL} //Enable I2C and set 1 byte }; //spi.format(8,0); //spi.frequency(1000000); for(i=0; i<MPU_InitRegNum; i++) { WriteReg(MPU_Init_Data[i][1], MPU_Init_Data[i][0]); bcm2835_delayMicroseconds(100000); //I2C must slow down the write speed, otherwise it won't work } set_acc_scale(BITS_FS_16G); set_gyro_scale(BITS_FS_2000DPS); calib_mag(); return 0; }
void MPU9250::read_mag(){ char response[7]; int16_t bit_data; float data; int i; WriteReg(MPUREG_I2C_SLV0_ADDR,AK8963_I2C_ADDR|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read. WriteReg(MPUREG_I2C_SLV0_REG, AK8963_HXL); //I2C slave 0 register address from where to begin data transfer WriteReg(MPUREG_I2C_SLV0_CTRL, 0x87); //Read 6 bytes from the magnetometer bcm2835_delayMicroseconds(10000); ReadRegs(MPUREG_EXT_SENS_DATA_00,response,7); //must start your read from AK8963A register 0x03 and read seven bytes so that upon read of ST2 register 0x09 the AK8963A will unlatch the data registers for the next measurement. for(i=0; i<3; i++) { bit_data=((int16_t)response[i*2+1]<<8)|response[i*2]; data=(float)bit_data; magnetometer_data[i]=data*magnetometer_ASA[i]; } }
void MPU9250::calib_mag(){ char response[3]; float data; int i; WriteReg(MPUREG_I2C_SLV0_ADDR,AK8963_I2C_ADDR|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read. WriteReg(MPUREG_I2C_SLV0_REG, AK8963_ASAX); //I2C slave 0 register address from where to begin data transfer WriteReg(MPUREG_I2C_SLV0_CTRL, 0x83); //Read 3 bytes from the magnetometer //WriteReg(MPUREG_I2C_SLV0_CTRL, 0x81); //Enable I2C and set bytes bcm2835_delayMicroseconds(10000); //response[0]=WriteReg(MPUREG_EXT_SENS_DATA_01|READ_FLAG, 0x00); //Read I2C ReadRegs(MPUREG_EXT_SENS_DATA_00,response,3); //response=WriteReg(MPUREG_I2C_SLV0_DO, 0x00); //Read I2C for(i=0; i<3; i++) { data=response[i]; magnetometer_ASA[i]=((data-128)/256+1)*Magnetometer_Sensitivity_Scale_Factor; } }
/* Read an number of bytes from I2C sending a repeated start after writing // the required register. Only works if your device supports this mode */ uint8_t bcm2835_i2c_read_register_rs(char* regaddr, char* buf, uint32_t len) { #ifdef I2C_V1 volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4; volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4; volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4; volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4; #else volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4; volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4; volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4; volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4; #endif uint32_t remaining = len; uint32_t i = 0; uint8_t reason = BCM2835_I2C_REASON_OK; /* Clear FIFO */ bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 ); /* Clear Status */ bcm2835_peri_write(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE); /* Set Data Length */ bcm2835_peri_write(dlen, 1); /* Enable device and start transfer */ bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN); bcm2835_peri_write(fifo, regaddr[0]); bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST); /* poll for transfer has started */ while ( !( bcm2835_peri_read(status) & BCM2835_BSC_S_TA ) ) { /* Linux may cause us to miss entire transfer stage */ if(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE) break; } /* Send a repeated start with read bit set in address */ bcm2835_peri_write(dlen, len); bcm2835_peri_write(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST | BCM2835_BSC_C_READ ); /* Wait for write to complete and first byte back. */ bcm2835_delayMicroseconds(i2c_byte_wait_us * 3); /* wait for transfer to complete */ while (!(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE)) { /* we must empty the FIFO as it is populated and not use any delay */ while (remaining && bcm2835_peri_read(status) & BCM2835_BSC_S_RXD) { /* Read from FIFO */ buf[i] = bcm2835_peri_read(fifo); i++; remaining--; } } /* transfer has finished - grab any remaining stuff in FIFO */ while (remaining && (bcm2835_peri_read(status) & BCM2835_BSC_S_RXD)) { /* Read from FIFO */ buf[i] = bcm2835_peri_read(fifo); i++; remaining--; } /* Received a NACK */ if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR) { reason = BCM2835_I2C_REASON_ERROR_NACK; } /* Received Clock Stretch Timeout */ else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT) { reason = BCM2835_I2C_REASON_ERROR_CLKT; } /* Not all data is sent */ else if (remaining) { reason = BCM2835_I2C_REASON_ERROR_DATA; } bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE); return reason; }
/************************************************************************** Function: lcd_on Purpose: Turn on lcd display Returns: Note: Assumes spi_lcd_init() has been called *************************************************************************/ void lcd_on(struct lcd_dev *dev) { u8 tx_buff[4] = { CMD_CODE, CMD_ON, 0, 0 }; dev->write(dev,tx_buff, 2); bcm2835_delayMicroseconds(100); }
void bsp_DelayUS(uint64_t micros) { bcm2835_delayMicroseconds(micros); }
// initialization of RAIO8870 // ---------------------------------------------------------- void RAIO_init( void ) { static uint8_t PLL_Initial_Flag = 0; // *************** PLL settings (System Clock) if ( !PLL_Initial_Flag ) // wait until PLL is ready { PLL_Initial_Flag = 1; // set Flag to avoid repeated PLL init RAIO_SetRegister( PLLC1, 0x07 ); // set sys_clk bcm2835_delayMicroseconds( 200 ); RAIO_SetRegister( PLLC2, 0x03 ); // set sys_clk bcm2835_delayMicroseconds( 200 ); RAIO_SetRegister( PWRR, 0x01 ); // Raio software reset ( bit 0 ) set RAIO_SetRegister( PWRR, 0x00 ); // Raio software reset ( bit 0 ) set to 0 delay( 100 ); // *************** color modes (color depths) #ifdef CM_65K // System Configuration Register RAIO_SetRegister( SYSR, 0x0A ); // digital TFT // parallel data out // no external memory // 8bit memory data bus // 16bpp 65K color // 16bit MCU-interface (data) RAIO_SetRegister( DPCR, 0x00 ); // one layer #elif defined(CM_4K) // System Configuration Register RAIO_SetRegister( SYSR, 0x06 ); // digital TFT // parallel data out // no external memory // 8bit memory data bus // 12bpp 4K color // 16bit MCU-interface (data) RAIO_SetRegister( DPCR, 0x80 ); // two layers RAIO_SetRegister( MWCR1, BankNo_WR ); RAIO_SetRegister( LTPR0, BankNo_RD ); #else #error "color_mode not defined" #endif } // *************** horizontal settings // 0x27+1 * 8 = 320 pixel RAIO_SetRegister( HDWR , (DISPLAY_WIDTH / 8) - 1 ); RAIO_SetRegister( HNDFTR, 0x02 ); // Horizontal Non-Display Period Fine Tuning // HNDR , Horizontal Non-Display Period Bit[4:0] // Horizontal Non-Display Period (pixels) = (HNDR + 1)*8 RAIO_SetRegister( HNDR, 0x03 ); // 0x06 RAIO_SetRegister( HSTR, 0x04 ); //HSTR , HSYNC Start Position[4:0], HSYNC Start Position(PCLK) = (HSTR + 1)*8 0x02 // HPWR , HSYNC Polarity ,The period width of HSYNC. // 1xxxxxxx activ high 0xxxxxxx activ low // HSYNC Width [4:0] HSYNC Pulse width // (PCLK) = (HPWR + 1)*8 RAIO_SetRegister( HPWR, 0x03 ); // 0x00 // ********************* vertical settings // 0x0EF +1 = 240 pixel RAIO_SetRegister( VDHR0 , ( (DISPLAY_HEIGHT-1) & 0xFF ) ); RAIO_SetRegister( VDHR1 , ( (DISPLAY_HEIGHT-1) >> 8) ); // VNDR0 , Vertical Non-Display Period Bit [7:0] // Vertical Non-Display area = (VNDR + 1) // VNDR1 , Vertical Non-Display Period Bit [8] // Vertical Non-Display area = (VNDR + 1) RAIO_SetRegister( VNDR0, 0x10 ); RAIO_SetRegister( VNDR1, 0x00 ); // VPWR , VSYNC Polarity ,VSYNC Pulse Width[6:0] // VSYNC , Pulse Width(PCLK) = (VPWR + 1) RAIO_SetRegister( VPWR, 0x00 ); // *************** miscellaneous settings // active Window Active_Window( 0, DISPLAY_WIDTH-1, 0, DISPLAY_HEIGHT-1 ); // PCLK fetch data on rising edge RAIO_SetRegister( PCLK, 0x00 ); // Backlight dimming RAIO_SetBacklightPWMValue(50); Text_Background_Color( COLOR_WHITE ); // memory clear with background color RAIO_SetRegister( MCLR, 0x81 ); TFT_wait_for_raio(); RAIO_SetRegister( IODR, 0x07 ); RAIO_SetRegister( PWRR, 0x80 ); }
int main() { if(!bcm2835_init()){return 1;} bcm2835_gpio_write(PIN,HIGH); int pin7 = 7; int pinx= 18; int result =-1; result=0; //result=softPwmCreate(pinx,0,215); if(result ==0){ while(result<600){ bcm2835_gpio_write(PIN,HIGH); //bcm2835_delay(3); bcm2835_delayMicroseconds(1400); bcm2835_gpio_write(PIN,LOW); //bcm2835_delay(20); bcm2835_delayMicroseconds(20000); //printf("In while loop"); result++; } result=0; while(result<600){ bcm2835_gpio_write(PIN,HIGH); //bcm2835_delay(3); bcm2835_delayMicroseconds(1350); bcm2835_gpio_write(PIN,LOW); //bcm2835_delay(20); bcm2835_delayMicroseconds(20000); //printf("In while loop"); result++; } result=0; while(result<600){ bcm2835_gpio_write(PIN,HIGH); //bcm2835_delay(3); bcm2835_delayMicroseconds(1275); bcm2835_gpio_write(PIN,LOW); //bcm2835_delay(20); bcm2835_delayMicroseconds(20000); //printf("In while loop"); result++; } result=0; while(result<600){ bcm2835_gpio_write(PIN,HIGH); //bcm2835_delay(3); bcm2835_delayMicroseconds(1100); bcm2835_gpio_write(PIN,LOW); //bcm2835_delay(20); bcm2835_delayMicroseconds(20000); //printf("In while loop"); result++; } } bcm2835_close(); return 0; }
void lcm_init() { bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); /* 64=250ns=4MHz | works up to 16 | unstable 8 | unusable 4 */ bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); bcm2835_gpio_fsel(A0, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(RST, BCM2835_GPIO_FSEL_OUTP); bcm2835_delayMicroseconds(1); // Reset bcm2835_gpio_write(RST, LOW); bcm2835_delayMicroseconds(10); bcm2835_gpio_write(RST, HIGH); lcm_set_command(); bcm2835_spi_transfer(0xAE); // turn off the screen bcm2835_spi_transfer(0xD5); // set display clock divide ratio/oscillator frequency bcm2835_spi_transfer(0x80); // suggested ratio: 0x80 bcm2835_spi_transfer(0xA8); // set multiplex bcm2835_spi_transfer(0x3F); // height: 64 bcm2835_spi_transfer(0xD3); // set display offset bcm2835_spi_transfer(0x00); // offset: 0 bcm2835_spi_transfer(0x40 | 0x0); // set start line bcm2835_spi_transfer(0x8D); // set charge pump bcm2835_spi_transfer(0x14); // charge pump: disable bcm2835_spi_transfer(0x20); // set memory mode bcm2835_spi_transfer(0x00); // mode: 0 bcm2835_spi_transfer(0xA0 | 0x1); // set seg/column mapping bcm2835_spi_transfer(0xC8); // set com/row scan direction bcm2835_spi_transfer(0xDA); // set com pins hardware configuration bcm2835_spi_transfer(0x12); // compins: 0x12 bcm2835_spi_transfer(0x81); // set contrast control register bcm2835_spi_transfer(0xCF); // brightness: 0xCF bcm2835_spi_transfer(0xD9); // set pre-charge period bcm2835_spi_transfer(0xF1); // pre-charge: 15 clocks / discharge: 1 clock bcm2835_spi_transfer(0XDB); // set vcom detect bcm2835_spi_transfer(0x40); // vcom deselect level: 0x40 bcm2835_spi_transfer(0xA4); // disable entire display bcm2835_spi_transfer(0xA6); // disable inverse display (normal display) bcm2835_spi_transfer(0xAF); // turn on the screen lcm_clear(); }
//---------------------------------------------------------------------------- void main() { unsigned char i,j,h; if (!bcm2835_init()) return; ini_displaypx(); bcm2835_gpio_clr(CS1_DISPLAY); setx_displaypx(0); write_displaypx(0x01); setx_displaypx(1); write_displaypx(0x02); setx_displaypx(2); write_displaypx(0x03); setx_displaypx(3); write_displaypx(0x04); setx_displaypx(4); write_displaypx(0x05); setx_displaypx(5); write_displaypx(0x06); setx_displaypx(6); write_displaypx(0x07); setx_displaypx(7); write_displaypx(0x08); for(i=0;i<8;i++) { setx_displaypx(i); for(j=0;j<64;j++) { setx_displaypx(i); sety_displaypx(j); printf("%X ", (int)read_data_display()); setx_displaypx(i); sety_displaypx(j); write_displaypx(0x5a); bcm2835_delayMicroseconds(100); setx_displaypx(i); sety_displaypx(j); printf("%X - ", (int)read_data_display()); } printf("\n"); } bcm2835_gpio_set(CS1_DISPLAY); bcm2835_gpio_clr(CS2_DISPLAY); write_displaypx(0xff); bcm2835_gpio_set(CS2_DISPLAY); for(i=0;i<10;i++) printf("%d ",(int)read_com_displaypx()); printf("\n"); bcm2835_close(); return; }
int w_delayMicroseconds(unsigned long long micros){ save("%u %u",ACT_SLEEP,micros); if(!a->dummy)bcm2835_delayMicroseconds(micros); }
static int play_song(char *filename) { int i,j,f,length_seconds; double s,n,hz,diff; int result; int frame_num=0,frames_elapsed=0; struct timeval start,next; struct pt3_song_t pt3; struct display_stats ds; char string[13]; unsigned char frame[16]; #define TIMING_DEBUG 0 #if TIMING_DEBUG==1 struct timeval before,after; double b,a; FILE *debug_file; debug_file=fopen("timing.debug","w"); #endif printf("\nPlaying song %s\n",filename); result=pt3_load_song(filename,&pt3); if (result<0) { return -1; } gettimeofday(&start,NULL); /**********************/ /* Print song summary */ /**********************/ length_seconds=60; printf("\tSong name: %s\n",pt3.name); printf("\tAuthor name: %s\n",pt3.author); sprintf(display_text,"VMW CHIPTUNE"); display_14seg_string(display_type,display_text); /******************/ /* Play the song! */ /******************/ frame_num=0; for(i=0;i < pt3.music_len;i++) { pt3_set_pattern(i,&pt3); for(j=0;j<64;j++) { /* decode line. 1 if done early */ if (pt3_decode_line(&pt3)) break; /* Dump out subframes of line */ for(f=0;f<pt3.speed;f++) { pt3_make_frame(&pt3,frame); ym_play_frame(frame,shift_size, &ds, diff_mode,play_music,mute_channel); if (visualize) { if (display_type&DISPLAY_TEXT) { printf("\033[H\033[2J"); } display_update(display_type, &ds, frame_num, 100, filename,0, current_mode); } result=handle_keypress(); if (result) return result; frames_elapsed++; frame_num++; sprintf(string,"%s %s %s", pt3_current_note('A',&pt3), pt3_current_note('B',&pt3), pt3_current_note('C',&pt3)); music_visualize(frames_elapsed,frame_num, filename, length_seconds,pt3.name,pt3.author, string); /* Calculate time it took to play/visualize */ gettimeofday(&next,NULL); s=start.tv_sec+(start.tv_usec/1000000.0); n=next.tv_sec+(next.tv_usec/1000000.0); diff=(n-s)*1000000.0; /* Delay until time for next update (often 50Hz) */ if (play_music) { if (diff>0) bcm2835_delayMicroseconds(20000-diff); /* often 50Hz = 20000 */ /* TODO: calculate correctly */ } else { if (visualize) usleep(1000000/frame_rate); } /* Calculate time it actually took, and print */ /* so we can see if things are going horribly wrong */ gettimeofday(&next,NULL); n=next.tv_sec+(next.tv_usec/1000000.0); if (frame_num%100==0) { hz=1/(n-s); printf("Done frame %d/%d, %.1lfHz\n", frame_num,1,hz); } start.tv_sec=next.tv_sec; start.tv_usec=next.tv_usec; } } } return 0; }
// Sending an arbitrary number of bytes before issuing a repeated start // (with no prior stop) and reading a response. Some devices require this behavior. uint8_t bcm2835_i2c_write_read_rs(char* cmds, uint32_t cmds_len, char* buf, uint32_t buf_len) { #ifdef I2C_V1 volatile uint32_t* dlen = bcm2835_bsc0 + BCM2835_BSC_DLEN/4; volatile uint32_t* fifo = bcm2835_bsc0 + BCM2835_BSC_FIFO/4; volatile uint32_t* status = bcm2835_bsc0 + BCM2835_BSC_S/4; volatile uint32_t* control = bcm2835_bsc0 + BCM2835_BSC_C/4; #else volatile uint32_t* dlen = bcm2835_bsc1 + BCM2835_BSC_DLEN/4; volatile uint32_t* fifo = bcm2835_bsc1 + BCM2835_BSC_FIFO/4; volatile uint32_t* status = bcm2835_bsc1 + BCM2835_BSC_S/4; volatile uint32_t* control = bcm2835_bsc1 + BCM2835_BSC_C/4; #endif uint32_t remaining = cmds_len; uint32_t i = 0; uint8_t reason = BCM2835_I2C_REASON_OK; // Clear FIFO bcm2835_peri_set_bits(control, BCM2835_BSC_C_CLEAR_1 , BCM2835_BSC_C_CLEAR_1 ); // Clear Status bcm2835_peri_write_nb(status, BCM2835_BSC_S_CLKT | BCM2835_BSC_S_ERR | BCM2835_BSC_S_DONE); // Set Data Length bcm2835_peri_write_nb(dlen, cmds_len); // pre populate FIFO with max buffer while( remaining && ( i < BCM2835_BSC_FIFO_SIZE ) ) { bcm2835_peri_write_nb(fifo, cmds[i]); i++; remaining--; } // Enable device and start transfer bcm2835_peri_write_nb(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST); // poll for transfer has started (way to do repeated start, from BCM2835 datasheet) while ( !( bcm2835_peri_read_nb(status) & BCM2835_BSC_S_TA ) ) { // Linux may cause us to miss entire transfer stage if(bcm2835_peri_read(status) & BCM2835_BSC_S_DONE) break; } remaining = buf_len; i = 0; // Send a repeated start with read bit set in address bcm2835_peri_write_nb(dlen, buf_len); bcm2835_peri_write_nb(control, BCM2835_BSC_C_I2CEN | BCM2835_BSC_C_ST | BCM2835_BSC_C_READ ); // Wait for write to complete and first byte back. bcm2835_delayMicroseconds(i2c_byte_wait_us * (cmds_len + 1)); // wait for transfer to complete while (!(bcm2835_peri_read_nb(status) & BCM2835_BSC_S_DONE)) { // we must empty the FIFO as it is populated and not use any delay while (remaining && bcm2835_peri_read_nb(status) & BCM2835_BSC_S_RXD) { // Read from FIFO, no barrier buf[i] = bcm2835_peri_read_nb(fifo); i++; remaining--; } } // transfer has finished - grab any remaining stuff in FIFO while (remaining && (bcm2835_peri_read_nb(status) & BCM2835_BSC_S_RXD)) { // Read from FIFO, no barrier buf[i] = bcm2835_peri_read_nb(fifo); i++; remaining--; } // Received a NACK if (bcm2835_peri_read(status) & BCM2835_BSC_S_ERR) { reason = BCM2835_I2C_REASON_ERROR_NACK; } // Received Clock Stretch Timeout else if (bcm2835_peri_read(status) & BCM2835_BSC_S_CLKT) { reason = BCM2835_I2C_REASON_ERROR_CLKT; } // Not all data is sent else if (remaining) { reason = BCM2835_I2C_REASON_ERROR_DATA; } bcm2835_peri_set_bits(control, BCM2835_BSC_S_DONE , BCM2835_BSC_S_DONE); return reason; }
//void bcm2835_delayMicroseconds (uint64_t micros); /// Call bcm2835_delaymicroseconds with 1 parameter /// \par Refer /// \par Modify void ope_delaymicroseconds(void) { get_long_code(); bcm2835_delayMicroseconds( *((uint64_t *)(buff+1)) ); }