int main(void) { clock_setup(); gpio_setup(); usart_setup(); i2c_setup(); /*uint8_t data[1]={(0x4 << ACC_CTRL_REG1_A_ODR_SHIFT) | ACC_CTRL_REG1_A_XEN};*/ uint8_t data[1]={0x97}; write_i2c(I2C1, I2C_ACC_ADDR, ACC_CTRL_REG1_A, 1, data); data[0]=0x08; write_i2c(I2C1, I2C_ACC_ADDR, ACC_CTRL_REG4_A, 1, data); uint16_t acc_x; while (1) { read_i2c(I2C1, I2C_ACC_ADDR, ACC_STATUS, 1, data); /*my_usart_print_int(USART2, data[0]);*/ read_i2c(I2C1, I2C_ACC_ADDR, ACC_OUT_X_L_A, 1, data); acc_x=data[0]; read_i2c(I2C1, I2C_ACC_ADDR, ACC_OUT_X_H_A, 1, data); acc_x|=(data[0] << 8); my_usart_print_int(USART2, (int16_t) acc_x); //int i; //for (i = 0; i < 800000; i++) /* Wait a bit. */ // __asm__("nop"); } return 0; }
int i2c_init() { /*open I2C 设备*/ if ((pt->fd_config = open(I2C_DEVICE, O_RDWR)) < 0) { perror(I2C_DEVICE); return 1; } /*设定 I2C 设备总线地址*/ if ((ioctl(pt->fd_config, I2C_SLAVE, 0x53)) < 0) { perror("ioctl i2c address error!"); return 1; } if (-1 == (read_i2c(pt->fd_config, i2c_add, pt->p_config, 224))) return 1;/*读取默认配置文件*/ i2c_add[1] = 228; /*开机设置默认选择为第一主菜单第一附属菜单*/ write_i2c(pt->fd_config, i2c_add, pt->p_config + 228, 12); i2c_add[1] = 240; /*开机设置默认选择为第一主菜单第一附属菜单*/ write_i2c(pt->fd_config, i2c_add, pt->p_config +240, 16); return 0; }
static int __init trust_init(void) { if(io == -1) { printk(KERN_ERR "You must set an I/O address with io=0x???\n"); return -EINVAL; } if(!request_region(io, 2, "Trust FM Radio")) { printk(KERN_ERR "trust: port 0x%x already in use\n", io); return -EBUSY; } if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1) { release_region(io, 2); return -EINVAL; } printk(KERN_INFO "Trust FM Radio card driver v1.0.\n"); write_i2c(2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */ write_i2c(2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */ write_i2c(2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */ write_i2c(2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */ write_i2c(2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */ tr_setvol(0x8000); tr_setbass(0x8000); tr_settreble(0x8000); tr_setstereo(1); /* mute card - prevents noisy bootups */ tr_setmute(1); return 0; }
void pmx::GpioBoard::setup() { if (!connected_) { logger().error() << "setup() : GpioBoard NOT CONNECTED !" << utils::end; return; } write_i2c(CONFIG_P0, 0x00); //defines all pins on Port0 are outputs write_i2c(OUT_P0, 0x00); //clears all relays write_i2c(CONFIG_P1, 0xFF); //defines all pins on Port1 are inputs write_i2c(IN_P1, 0x00); //clears all relays usleep(PAUSE); }
//Performs a software reset on all devices on the I2C bus int I2C::softwareReset(void) { char rst[] = {0x6}; write_i2c(0x0,1,rst); //Software reset command return 0; }
static int trust_s_ctrl(struct v4l2_ctrl *ctrl) { struct radio_isa_card *isa = container_of(ctrl->handler, struct radio_isa_card, hdl); struct trust *tr = container_of(isa, struct trust, isa); switch (ctrl->id) { case V4L2_CID_AUDIO_BASS: write_i2c(tr, 2, TDA7318_ADDR, 0x60 | basstreble2chip[ctrl->val]); return 0; case V4L2_CID_AUDIO_TREBLE: write_i2c(tr, 2, TDA7318_ADDR, 0x70 | basstreble2chip[ctrl->val]); return 0; } return -EINVAL; }
static int trust_initialize(struct radio_isa_card *isa) { struct trust *tr = container_of(isa, struct trust, isa); tr->ioval = 0xf; write_i2c(tr, 2, TDA7318_ADDR, 0x80); /* speaker att. LF = 0 dB */ write_i2c(tr, 2, TDA7318_ADDR, 0xa0); /* speaker att. RF = 0 dB */ write_i2c(tr, 2, TDA7318_ADDR, 0xc0); /* speaker att. LR = 0 dB */ write_i2c(tr, 2, TDA7318_ADDR, 0xe0); /* speaker att. RR = 0 dB */ write_i2c(tr, 2, TDA7318_ADDR, 0x40); /* stereo 1 input, gain = 18.75 dB */ v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops, V4L2_CID_AUDIO_BASS, 0, 15, 1, 8); v4l2_ctrl_new_std(&isa->hdl, &trust_ctrl_ops, V4L2_CID_AUDIO_TREBLE, 0, 15, 1, 8); return isa->hdl.error; }
static int trust_s_mute_volume(struct radio_isa_card *isa, bool mute, int vol) { struct trust *tr = container_of(isa, struct trust, isa); tr->ioval = (tr->ioval & 0xf7) | (mute << 3); outb(tr->ioval, isa->io); write_i2c(tr, 2, TDA7318_ADDR, vol ^ 0x1f); return 0; }
uint8_t init_speedy(void){ uint8_t data = 0x00; //Init I2C 1 if(init_i2c(I2C_1) != 0) return 1; //Pyro Sensor if(write_i2c(I2C_1, ADDR_TPA81, &data, 1) != 1) return 11; //SRF08 hinten links if(write_i2c(I2C_1, SRF_HL, &data, 1) != 1) return 12; //SRF08 vorne if(write_i2c(I2C_1, SRF_V, &data, 1) != 1) return 13; //SRF08 hinten rechts if(write_i2c(I2C_1, SRF_HR, &data, 1) != 1) return 14; //Init I2C 2 if(init_i2c(I2C_2) != 0) return 2; //HMC6343 if(write_i2c(I2C_2, ADDR_HMC, &data, 1) != 1) return 21; //MAX11613 if(write_i2c(I2C_2, ADDR_MAX11613, &data, 1) != 1) return 22; //LCD if(write_i2c(I2C_2, LCD03_ADRESS, &data, 1) != 1) return 23; //ITG3200 if(write_i2c(I2C_2, ADDR_ITG, &data, 1) != 1) return 24; //Init Motor Modul if(init_itg() != 0) return 3; //Init G Sensor ADXL345 on DE0 Nano SoC if(!ADXL345_Init()) return 4; return 0; }
uint8_t read_pixel_temp(uint8_t pixel, uint8_t data_tpa[]){ uint8_t byte = 1; uint8_t command; uint8_t bytes_read; //Festlegen des Rigsters ab dem gelesen werden soll, abhaenig vom Patam: pixel switch (pixel){ //Alle pixel case 0: byte = 8; command = 2; break; case 1: command = 2; break; case 2: command = 3; break; case 3: command = 4; break; case 4: command = 5; break; case 5: command = 6; break; case 6: command = 7; break; case 7: command = 8; break; case 8: command = 9; break; } //Registernr an modul schreiben if(write_i2c(I2C_1, ADDR_TPA81, &command, 1) != 1) return -2; //Lesen der Daten vom Modul bytes_read = read_i2c(I2C_1, ADDR_TPA81, data_tpa, byte); //Pruefen ob die geforderten Bytes gelesen wurden if(bytes_read == byte) return bytes_read; return -1; }
/** * nmk_i2c_xfer() - I2C transfer function used by kernel framework * @i2c_adap: Adapter pointer to the controller * @msgs: Pointer to data to be written. * @num_msgs: Number of messages to be executed * * This is the function called by the generic kernel i2c_transfer() * or i2c_smbus...() API calls. Note that this code is protected by the * semaphore set in the kernel i2c_transfer() function. * * NOTE: * READ TRANSFER : We impose a restriction of the first message to be the * index message for any read transaction. * - a no index is coded as '0', * - 2byte big endian index is coded as '3' * !!! msg[0].buf holds the actual index. * This is compatible with generic messages of smbus emulator * that send a one byte index. * eg. a I2C transation to read 2 bytes from index 0 * idx = 0; * msg[0].addr = client->addr; * msg[0].flags = 0x0; * msg[0].len = 1; * msg[0].buf = &idx; * * msg[1].addr = client->addr; * msg[1].flags = I2C_M_RD; * msg[1].len = 2; * msg[1].buf = rd_buff * i2c_transfer(adap, msg, 2); * * WRITE TRANSFER : The I2C standard interface interprets all data as payload. * If you want to emulate an SMBUS write transaction put the * index as first byte(or first and second) in the payload. * eg. a I2C transation to write 2 bytes from index 1 * wr_buff[0] = 0x1; * wr_buff[1] = 0x23; * wr_buff[2] = 0x46; * msg[0].flags = 0x0; * msg[0].len = 3; * msg[0].buf = wr_buff; * i2c_transfer(adap, msg, 1); * * To read or write a block of data (multiple bytes) using SMBUS emulation * please use the i2c_smbus_read_i2c_block_data() * or i2c_smbus_write_i2c_block_data() API */ static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num_msgs) { int status; int i; u32 cause; struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap); status = init_hw(dev); if (status) return status; clk_enable(dev->clk); /* setup the i2c controller */ setup_i2c_controller(dev); for (i = 0; i < num_msgs; i++) { if (unlikely(msgs[i].flags & I2C_M_TEN)) { dev_err(&dev->pdev->dev, "10 bit addressing" "not supported\n"); return -EINVAL; } dev->cli.slave_adr = msgs[i].addr; dev->cli.buffer = msgs[i].buf; dev->cli.count = msgs[i].len; dev->stop = (i < (num_msgs - 1)) ? 0 : 1; dev->result = 0; if (msgs[i].flags & I2C_M_RD) { /* it is a read operation */ dev->cli.operation = I2C_READ; status = read_i2c(dev); } else { /* write operation */ dev->cli.operation = I2C_WRITE; status = write_i2c(dev); } if (status || (dev->result)) { /* get the abort cause */ cause = (readl(dev->virtbase + I2C_SR) >> 4) & 0x7; dev_err(&dev->pdev->dev, "error during I2C" "message xfer: %d\n", cause); dev_err(&dev->pdev->dev, "%s\n", cause >= ARRAY_SIZE(abort_causes) ? "unknown reason" : abort_causes[cause]); clk_disable(dev->clk); return status; } udelay(I2C_DELAY); }
void GpioPCA9555::setValueP0(int port, int pin, int value) { if (!connected_) { logger().error() << "setValueP0() : GpioBoard NOT CONNECTED !" << logs::end; return; } int out = 0; if (value == 1) out = port0Value_ | (0x01 << pin); else if (value == 0) out = port0Value_ & (0xFE << pin); write_i2c(port, out); }
uint8_t read_umgebungs_temp(void){ uint8_t temperatur = 0; //Festlegen des zu lesenden Registers uint8_t cmd_reg_1 = {1}; //Registernr an modul schreiben if(write_i2c(I2C_1, ADDR_TPA81, &cmd_reg_1, 1) != 1) return -1; //Lesen der Umgebungstemperatur if(read_i2c(I2C_1, ADDR_TPA81, &temperatur, 1) != 1) return -1; //Rueckgabe der Umgebungstemperatur return temperatur; }
uint8_t read_version_tpa81(void){ uint8_t version = 0; //Festlegen des zu lesenden Registers uint8_t cmd_reg_0 = {0}; //Registernr an modul schreiben if(write_i2c(I2C_1, ADDR_TPA81, &cmd_reg_0, 1) != 1) return -1; //Lesen der Versionsnummer if(read_i2c(I2C_1, ADDR_TPA81, &version, 1) != 1) return -1; //Rueckgabe der Verison return version; }
void process_set_command(const char *command, int i2c_handle, char *reply, int reply_size) { uint8_t address, reg, value; int n = sscanf(command, "set %hhd %hhd %hhd", &address, ®, &value); if (n != 3) { fprintf(stderr, "ERROR => Incorrect arguments. Expected slave 2ddress, i2c register and value.\n"); strcpy(reply, "ERROR\r\n"); return; } int result = write_i2c(i2c_handle, address, reg, value, 1); if (result == -1) { char message[100]; snprintf(message, sizeof(message), "ERROR => Error writing i2c value at address=%d, register=%d. The error was", (int)address, (int)reg); perror(message); strcpy(reply, "ERROR\r\n"); return; } strncpy(reply, "OK\r\n", reply_size); }
int main (int argc, char *argv[]) { struct pci_access *pacc; struct pci_dev *dev; struct pci_filter filter; char *msg; uint16_t command; int v1_1 = 0, i2c; if (argc != 2 && ((argc != 3) || (!(v1_1 = !strcmp(argv[2], "1.1")) && strcmp(argv[2], "1.0")))) { fprintf(stderr, "VT6307 OHCI mode config\n" "Version 0.9\n" "Copyright (C) 2007 Krzysztof Halasa <*****@*****.**>\n" "\n" "Usage: vt6307ohciver <pci_device> [ 1.0 | 1.1 ]\n"); exit(1); } if (iopl(3)) { fprintf(stderr, "iopl() failed (must be root)\n"); exit(1); } pacc = pci_alloc(); pci_filter_init(pacc, &filter); if ((msg = pci_filter_parse_slot(&filter, argv[1]))) { fprintf(stderr, "Invalid pci_device\n"); exit(1); } filter.vendor = VENDOR_ID; filter.device = DEVICE_ID; pci_init(pacc); pci_scan_bus(pacc); for (dev = pacc->devices; dev; dev = dev->next) if (pci_filter_match(&filter, dev)) break; if (!dev) { fprintf(stderr, "Device %s not found\n", argv[2]); exit(1); } pci_fill_info(dev, PCI_FILL_BASES | PCI_FILL_SIZES); if (dev->size[0] != MEM_SIZE || dev->size[1] != IO_SIZE) { fprintf(stderr, "Unexpected MEM/IO region size, is it" " VT6307 chip?\n"); exit(1); } command = pci_read_word(dev, PCI_COMMAND); if ((command & PCI_COMMAND_IO) == 0) { fprintf(stderr, "Device disabled, trying to enable it\n"); pci_write_word(dev, PCI_COMMAND, command | PCI_COMMAND_IO); } io_ports = dev->base_addr[1] & PCI_BASE_ADDRESS_IO_MASK; i2c = (inl(io_ports + 0x20) & 0x80) ? 0 : 1; fprintf(stderr, "I/O region #1 is at %04X\n", io_ports); fprintf(stderr, "It seems your VT6307 chip is connected to %s " "EEPROM\n", i2c ? "I^2C (24c01 or similar)" : "93c46"); if (argc == 3) { /* enable direct access to pins */ outl_p(inl(io_ports) | 0x80, io_ports); if (i2c) write_i2c(0x22, v1_1 ? 8 : 0); else write_4w(0x11, v1_1 ? 8 : 0); /* disable direct access to pins */ outl_p(0x20, io_ports + 0x20); fprintf(stderr, "Please reboot\n"); } else display(dev->base_addr[0] & PCI_BASE_ADDRESS_MEM_MASK); if ((command & PCI_COMMAND_IO) == 0) { fprintf(stderr, "Disabling device\n"); pci_write_word(dev, PCI_COMMAND, command); } exit(0); }
void interrupt isr(void) { if (TMR1IF) { TMR1IF = 0; if(switch_dur_mult >= SWITCH_ON_DURATION_MULT) { off(); } else { switch_dur_mult++; } } #ifdef DHT22_ENABLED if (TMR2IF) { TMR2IF = 0; switch (dht22_state) { case 1: start_read_dht22_pullup(); break; default: dht22_abort(); } } #endif if (IOCIF) { asm("MOVLW 0xff"); #ifdef _12F1840 asm("banksel IOCAF"); asm("XORWF IOCAF, W"); asm("ANDWF IOCAF, F"); #else asm("banksel IOCBF"); asm("XORWF IOCBF, W"); asm("ANDWF IOCBF, F"); #endif unsigned char t = TMR2; TMR2 = 0; // reset timeout #ifdef _12F1840 if (PORTAbits.RA4 == 0) {// we care only about HI => LO transition #else if (PORTBbits.RB0 == 0) {// we care only about HI => LO transition #endif if (dht22_state < 4) { dht22_state++; } else { if (dht22_bit_index > 7) { dht22_bit_index = 0; dht22_index++; } if (t > DHT22_CUTOFF_TIME) { dht22_bits[dht22_index] |= (1 << (7 - dht22_bit_index)); } dht22_bit_index++; if (dht22_bit_index == 8 && dht22_index >= (DHT22_MAX_BYTES - 1)) { unsigned char sum = dht22_bits[0] + dht22_bits[1] + dht22_bits[2] + dht22_bits[3]; if (sum != dht22_bits[4]) {// checksum doesn't match up? // indicate error dht22_bits[0] = dht22_bits[1] = dht22_bits[2] = dht22_bits[3] = 0; } dht22_abort(); } } } } if (SSPIF) { unsigned char i2c_state = SSPSTAT & 0b00100100; // 0b00100000 = D/nA // 0b00000100 = R/nW // prevent overflow if (rx_index >= RX_SIZE) { rx_index = 0; } switch (i2c_state) { case 0b00000000: // STATE1: Maser Write, Last Byte = Address rx_index = 0; command = 0; // do a dummy read rx_buffer[0] = SSPBUF; for (int i = 0; i < RX_SIZE; i++) { rx_buffer[i] = 0; } break; case 0b00100000: // STATE2: Maser Write, Last Byte = Data rx_buffer[rx_index++] = SSPBUF; if (rx_index == 1) { command = rx_buffer[0]; // save command // input switch (command) { case 0x78: ENTER_BOOTLOADER = 1; asm("pagesel 0x000"); asm("goto 0x000"); break; case 0x12: start_read_dht22(); break; } } if (rx_index == 2) { // input switch (command) { case 0x01: counter++; on(rx_buffer[1]); break; } } break; case 0b00000100: // STATE3: Maser Read, Last Byte = Address rx_index = 0; case 0b00100100: // STATE4: Maser Read, Last Byte = Data // output switch (command) { case 0x01: if(rx_index == 0) rx_buffer[0] = counter; break; case 0x13: // read humidity rx_buffer[0] = dht22_bits[1]; rx_buffer[1] = dht22_bits[0]; break; case 0x14: // // read temperature rx_buffer[0] = dht22_bits[3]; rx_buffer[1] = dht22_bits[2]; break; default: rx_buffer[0] = rx_buffer[1] = 0; } write_i2c(rx_buffer[rx_index++]); break; } SSPCON1bits.CKP = 1; SSPIF = 0; } }
void Camera_thread_entry (void* parameter) { uint8_t ret=0; int i,fd; int ttt; //等待TIM3 初始化完毕 rt_sem_take(&rt_tim3_sem, RT_WAITING_FOREVER); rt_memset((void*)RAM_Buffer,0,JPEG_BUFFER_CNT*(JPEG_BUFFER_SIZE+1)); //RAM_Buffer[JPEG_BUFFER_CNT*(JPEG_BUFFER_SIZE+1)-1]=0xff; ret=CF5642C_init(); if(1==ret) { //Prepair for Camera to SPI LCD Cam_Stop(); Camera_key(); while(1) { CF5642C_DMA_Init(); write_i2c(0x3503, 0x0);//to enable AGC/AEC write_i2c(0x3b07 ,0x0a);//return to rolling strobe rt_memset((void*)RAM_Buffer,0,4*JPEG_BUFFER_CNT*(JPEG_BUFFER_SIZE+1)); //rt_memset((void*)RAM_Buffer,0,1); rt_kprintf("waiting for shutter...\n"); while(!cam_capture_ok){} cam_capture_ok=0; soi_index=0; eoi_index=0; tail_index=0; jpeg_soi_flag=0; jpeg_eoi_flag=0; Cam_Capture(); rt_sem_take(&rt_camera_sem, RT_WAITING_FOREVER); while(!dma_freebuf_ok){} dma_freebuf_ok = 0; for(i=0;i<JPEG_BUFFER_CNT*(JPEG_BUFFER_SIZE+1);i++) { temp1 = RAM_Buffer[i]&0xff; temp2 = RAM_Buffer[i]>>8&0xff; temp3 = RAM_Buffer[i]>>16&0xff; temp4 = RAM_Buffer[i]>>24&0xff; if((temp1 == 0xff)&&(temp2 == 0xd8)) { jpeg_soi_flag =1; soi_index = i; } else if((temp2 == 0xff)&&(temp3 == 0xd8)) { jpeg_soi_flag =1; soi_index = i; } else if((temp3 == 0xff)&&(temp4 == 0xd8)) { jpeg_soi_flag =1; soi_index = i; } if(jpeg_soi_flag == 1){ // rt_kprintf("%02x",temp1); // rt_kprintf("%02x",temp2); // rt_kprintf("%02x",temp3); // rt_kprintf("%02x",temp4); //search for eoi if((temp1 == 0xff)&&(temp2 == 0xd9))//[][][2][1] { // if(jpeg_off_flag == 1) // goto next; // else // jpeg_off_flag++; jpeg_eoi_flag =1; eoi_index = i; tail_index = 2; goto next; } else if((temp2 == 0xff)&&(temp3 == 0xd9))//[][3][2][] { jpeg_eoi_flag =1; eoi_index = i; tail_index = 1; goto next; } else if((temp3 == 0xff)&&(temp4 == 0xd9))//[4][3][][] { jpeg_eoi_flag =1; eoi_index = i; tail_index = 0; goto next; } else if((tempp == 0xff)&&(temp1 == 0xd9))//[][][][1] | [4] { jpeg_eoi_flag =1; eoi_index = i; tail_index = 3; goto next; } tempp = temp4; } } next: Cam_Stop(); rt_kprintf("start write \n"); sprintf(file_string,"%d",file_index); file_index++; mywrite_usb(file_string); // fd = open("/ud/text.text", O_WRONLY | O_CREAT,0); // if (fd >= 0) // { // //TIM_Cmd(TIM3,ENABLE); // // write(fd,(uint8_t *)&RAM_Buffer[soi_index-1], eoi_index-soi_index+2); // //rt_kprintf("%d\n", count); // // // close(fd); // } // rt_kprintf("write over\n"); /****** TM_ILI9341_Rotate(TM_ILI9341_Orientation_Landscape_2); TM_ILI9341_SetCursorPosition(0, 0, picture_x - 1, picture_y - 1); TM_ILI9341_SendCommand(ILI9341_GRAM); //Wirte GRAM ILI9341_WRX_SET; for (i = 0; i < picture_x*picture_y; i++) { ILI9341_CS_RESET; ILI9341_SPI->DR=(RAM_Buffer[i])&0xff; while (!SPI_I2S_GetFlagStatus(ILI9341_SPI, SPI_I2S_FLAG_TXE)); ILI9341_CS_SET; ILI9341_CS_RESET; ILI9341_SPI->DR=(RAM_Buffer[i]>>8)&0xff; while (!SPI_I2S_GetFlagStatus(ILI9341_SPI, SPI_I2S_FLAG_TXE)); ILI9341_CS_SET; } if(camera_status==0) { TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_1); LCD_SetCursor(0,0); } *******/ } }
int sysI2cWrite(unsigned char addr_i2c, unsigned char *data, unsigned int bufsize) { int nmax=1000; while((nmax>0)&&(write_i2c(addr_i2c,data,bufsize)==FALSE)){ nmax--;__no_operation(); } return nmax; }
static void tr_setvol(__u16 vol) { curvol = vol / 2048; write_i2c(2, TDA7318_ADDR, curvol ^ 0x1f); }
static void tr_setbass(__u16 bass) { curbass = bass / 4370; write_i2c(2, TDA7318_ADDR, 0x60 | basstreble2chip[curbass]); }
void update_x_translation(void){ write_i2c (SENSORS_I2C, MPU6050_ADDR, 0x01, 0x01); read_i2c (SENSORS_I2C, MPU6050_ADDR, 0x01); }
int main(int argc, char **argv, char **envp) { int file; const char *filename = "/dev/i2c-1"; if((file = open(filename, O_RDWR)) < 0) { perror("Failed to open the i2c bus.\n"); exit(1); } int device_addr = 0x53; if(ioctl(file, I2C_SLAVE, device_addr) < 0) { printf("Failed to acquire bus and/or talk to slave.\n"); exit(1); } write_i2c(file, 0x2c, SAMPLING_RATE); //BW_RATE -- Last 4 bits = sampling rate write_i2c(file, 0x31, 0x20 | RANGE); //DATA_FORMAT -- Last 2 bits = range write_i2c(file, 0x2e, 0x02); //INT_ENABLE write_i2c(file, 0x38, 0x41); //FIFO_CTL write_i2c(file, 0x2d, 0x08); //POWER_CTL int buttons[] = BUTTON_GPIO_PINS; int button_size = sizeof(buttons)/sizeof(buttons[0]); int button_active_edges[] = BUTTON_ACTIVE_EDGES; struct pollfd fdset[button_size]; int nfds = button_size; int gpio_fd[button_size]; int button; for(button = 0; button < button_size; button++) { gpio_export(buttons[button]); gpio_set_dir(buttons[button], "in"); gpio_set_edge(buttons[button], button_active_edges[button] ? "rising" : "falling"); gpio_fd[button] = gpio_fd_open(buttons[button], O_RDONLY); } int num_samples = 0; long int z_nooffset = 0; long int z_accumulator = 0; long int previous_z_accumulator = 0; long int z_diff = 0; long int previous_z = 0; long int z_jerk_fifo[QUEUE_LENGTH] = {0}; long int z_accel_fifo[QUEUE_LENGTH] = {0}; while(1) { memset((void*)fdset, 0, sizeof(fdset)); for(button = 0; button < button_size; button++) { fdset[button].fd = gpio_fd[button]; fdset[button].events = POLLPRI; } poll(fdset, nfds, 100); for(button = 0; button < button_size; button++) { if(fdset[button].revents & POLLPRI) { char buf[1]; read(fdset[button].fd, buf, 1); int button_state; gpio_get_value(buttons[button], &button_state); while(button_state == button_active_edges[button]) { char write_buf[1]; write_buf[0] = 0x32; if(write(file, write_buf,1) != 1) { printf("Failed to write to the i2c bus.\n"); } int reg_num; signed char read_buf[6]; if(read(file, read_buf, 6) != 6) { printf("Failed to read from the i2c bus.\n"); } else { long int z_data = ((((long int)read_buf[5]) << 8) | (0xff & read_buf[4])); num_samples++; if( num_samples > 1 ) { z_diff = (z_data - previous_z); z_nooffset += (z_diff - z_jerk_fifo[QUEUE_LENGTH - 1]); z_accumulator = (z_nooffset); } /*if( num_samples % 1 == 0 ) { printf(" \r"); printf("Current vel = %d\r", z_nooffset); fflush(stdout); }*/ if( (z_accumulator * previous_z_accumulator) < 0 && abs(z_accumulator - previous_z_accumulator) > 50 ) { // Put stuff to do on hits here! printf("HIT!\n"); printf("Acceleration = %d.\n", z_nooffset); fflush(stdout); } int index; for( index = QUEUE_LENGTH - 1; index >= 0; index-- ){ z_jerk_fifo[index] = z_jerk_fifo[index - 1]; z_accel_fifo[index] = z_accel_fifo[index - 1]; } z_jerk_fifo[0] = z_diff; z_accel_fifo[0] = z_nooffset; previous_z = z_data; previous_z_accumulator = z_accumulator; } gpio_get_value(buttons[button], &button_state); } } } } }
static void tr_settreble(__u16 treble) { curtreble = treble / 4370; write_i2c(2, TDA7318_ADDR, 0x70 | basstreble2chip[curtreble]); }