/* Name: i2c_write * Description: Write buff_len number of bytes to the addressed slave device. */ void i2c_write(uint8_t address, uint8_t *buffer, int buff_len) { int state = 0; int i = 0; address = (address << 1)| WRITE; //7 bits address and one 0 to indicate we want to write on de CODEC i2c_start(); while(state != 0x08){ //Wait until the start signal have been recived state = LPC_I2C0->STAT ; } i2c_tx(address); LPC_I2C0->CONCLR = 0x28; //Clear SI and start flag while(state != 0x18){ //Wait until the addres has been recived state = LPC_I2C0->STAT; } i2c_tx(buffer[0]); LPC_I2C0->CONCLR = 0x18; //Clear SI and start flag while(state != 0x28){ //Wait until the register address has been recived state = LPC_I2C0->STAT; } for (i = 1; i < buff_len; i++) { i2c_tx(buffer[i]); i2c_ack(); LPC_I2C0->CONCLR = 0x18; //Clear SI and start flag state = 0; while(state != 0x28){ //Wait until the data has been transmited state = LPC_I2C0->STAT; } } i2c_stop(); // send Stop signal delay_ms(10); //wait }
/** * @brief Håndter kommando med tilhørende værdi * @details Fortager en defineret handling ud fra den modtaget kommando med den tilhørende værdi. * @param[in] cmd Er den modtaget kommando. * @param[in] val Er den tilhørende værdi. * @public * @memberof Handler * @author Casper Dieu Le ([email protected]) * @author Kasper Hinkler Uldbjerg ([email protected]) * @author Jeppe Stærk Antonsen ([email protected]) */ void handler(uint8 cmd, uint8 val) { switch (cmd) { case CMD_SET_X_POS : setXPos(val); break; case CMD_SET_Y_POS: setYPos(val); break; case CMD_GET_X_POS : i2cTxBuffer[I2C_PACKET_CMD_POS] = cmd; i2cTxBuffer[I2C_PACKET_VAL_POS] = (uint8)((resolution * dataXY.xPos) / dataXY.xMax + 1); i2c_tx(); break; case CMD_GET_Y_POS : i2cTxBuffer[I2C_PACKET_CMD_POS] = cmd; i2cTxBuffer[I2C_PACKET_VAL_POS] = (uint8)((resolution * dataXY.yPos) / dataXY.yMax + 1); i2c_tx(); break; case CMD_X_STP: dataXY.isrStopX = 1; break; case CMD_Y_STP: dataXY.isrStopY = 1; break; case CMD_X_CAL: calibrateX(); break; case CMD_Y_CAL: calibrateY(); break; default : break; } }
void i2c_lcd_hex(uint8_t x) { // sends one byte in hex (without 0x prefix) to the lcd char nibble; /* extract high nibble */ nibble = x & 0xf0; // (resets lower nibble to all zero's) nibble = x >> 4; // (moves high nibble to lower nibble) // output the high nibble if(nibble < 10) { i2c_tx(48 + nibble); } else { i2c_tx(65 + nibble - 10); } /* do the same on the low nibble */ nibble = x & 0x0f; if(nibble < 10) { i2c_tx(48 + nibble); } else { i2c_tx(65 + nibble - 10); } }
void i2c_lcd_number(int number) { int digits; if (number == 0) { digits = 0; int array[0]; array[0]=0; i2c_tx(array[0]+48); } else { // determine the number of digits digits = (int) (log(number)/log(10))+1; // split up the number's digits into an array int i = digits -1; int array[digits]; while (number > 0) { array[i--] = number % 10; number /= 10; } // send array over i2c for (i =0; i <= digits-1; i++) { i2c_tx(array[i]+48); } } }
void MP3Player::I2C_Write(byte Address, byte data) { i2c_start(); if(i2c_tx(0x86)) while(1); if(i2c_tx(Address)) while(1); if(i2c_tx(data)) while(1); i2c_stop(); }
void write_219_reg(uint8_t reg, uint16_t val) { i2c_start(); //This may need MSB high. I am unsure from the DS. i2c_tx(ADDR | I2C_WRITE); i2c_tx(reg); i2c_tx((uint8_t)(val >> 8)); i2c_tx((uint8_t)(val & 0xFF)); i2c_stop(); }
void main() { unsigned short i=0; io_init(); //timer0_init(); //pwm timer1_init(); //1ms irqon(); //enable global interrupt sleep(0); // just refrence #if 0 for(i=0;1;i++){ setdot(i%4); segprint10(i); mdelay(222); if(i>9999) i=0; } #endif #define DL 1 /*sent start ADC to attiny13, address A2, cmd 12*/ again: segprint10(0); //mdelay(DL); i2c_start(); // send start sequence segprint10(1); //mdelay(DL); i2c_tx(0x55); // SRF08 I2C address with R/W bit clear segprint10(2); //mdelay(DL); i2c_tx(0xAA); // SRF08 command register address segprint10(3); //mdelay(DL); i2c_stop(); // send stop sequence segprint10(4); //mdelay(DL); goto again; while(1){ // KEY_FUNC P3_3 KEY_DISCHARGER P3_2 KEY_RESUME P3_1 charging_update_lcd(); } }
void show_nodev_msg() { i2c_start(LCD_I2C_DEVICE_ID); i2c_tx(12); // clear lcd i=0; while (pgm_read_byte(&msg_nodev[i]) != '\0') i2c_tx(pgm_read_byte(&msg_nodev[i++])); i2c_stop(); }
void show_title_msg() { // show welcome message i2c_start(LCD_I2C_DEVICE_ID); i2c_tx(12); // clear display i=0; while (pgm_read_byte(&message1[i]) != '\0') i2c_tx(pgm_read_byte(&message1[i++])); i2c_stop(); }
int writeFuses() { target_device=0; if (enableProgramming()) { // successfully found target programming device target_device=1; if(i2c_available) { i2c_start(LCD_I2C_DEVICE_ID); i2c_tx(12); i=0; while (pgm_read_byte(&writing[i]) != '\0') i2c_tx(pgm_read_byte(&writing[i++])); i2c_stop(); } _delay_ms(500); //Write hfuse shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, HFUSE, 0x2C); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x74); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x7C); //Write lfuse shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x40, 0x4C); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, LFUSE, 0x2C); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x64); shiftOut2(DATAOUT, INSTOUT, CLKOUT, MSBFIRST, 0x00, 0x6C); } else { // show no device found if(i2c_available) { show_nodev_msg(); } } disableProgramming(); return target_device; }
byte MP3Player::I2C_Read(byte Address) { byte data=0; i2c_start(); if(i2c_tx(0x86)) while(1); if(i2c_tx(Address)) while(1); i2c_start(); if(i2c_tx(0x87)) while(1); data=i2c_rx(0); i2c_stop(); return(data); }
void show_version_msg() { // show version message i2c_start(LCD_I2C_DEVICE_ID); i2c_tx(10); i2c_tx(2); i2c_tx(6); i=0; while (pgm_read_byte(&msg_version[i]) != '\0') i2c_tx(pgm_read_byte(&msg_version[i++])); i2c_stop(); }
void l3g4200d_set_datarate(l3g4200d_datarate_t datarate) { uint8_t buf[] = {CTRL_REG1, datarate | 0x3F}; i2c_tx(l3g4200d.i2c, L3G4200D_ADDRESS, buf, 2); }
uint16_t read_219_reg(uint8_t reg) { uint8_t low, uint8_t high; i2c_start(); //This may need MSB high. I am unsure from the DS. i2c_tx(ADDR | I2C_WRITE); i2c_tx(reg); i2c_start(); i2c_tx(ADDR | I2C_READ); high = i2c_rx_ack(); low = i2c_rx_nack(); i2c_stop(); return (uint16_t)((high << 8) | low); }
void pmu_write_oocshdwn(int data) { uint8_t poweroffData[] = {0xC0, 0xFF, 0xBF, 0xFF, 0xAE, 0xFF}; uint8_t buffer[sizeof(poweroffData) + 1]; pmu_get_reg(PMU_UNK2_REG); buffer[0] = PMU_UNK1_REG; memcpy(&buffer[1], poweroffData, sizeof(poweroffData)); i2c_tx(PMU_I2C_BUS, PMU_SETADDR, buffer, sizeof(buffer)); if (data == 1) { uint8_t result, reg; // sub_5FF0D99C(); // Something to do with gas gauge. for (reg = PMU_UNKREG_START; reg <= PMU_UNKREG_END; reg++) { result = pmu_get_reg(reg); if (!((result & 0xE0) <= 0x5F || (result & 2) == 0)) pmu_write_reg(reg, result & 0xFD, FALSE); } } pmu_write_reg(PMU_OOCSHDWN_REG, data, FALSE); while(TRUE) { udelay(100000); } }
void i2c_lcd_text(char *StrData) { int p = 0; int q = strlen(StrData); int temp = 0; for (p = 0; p < q; p++) { temp = StrData[p]; i2c_tx(temp); } }
bool lps331_config(i2c_t i2c, exti_line_t data_ready_line) { uint8_t buf[2]; lps331.i2c = i2c; lps331.data_ready_line = data_ready_line; // Reboot memory content buf[0] = CTRL_REG2; buf[1] = 0x80; return i2c_tx(lps331.i2c, LPS331_ADDRESS, buf, 2) || lps331_powerdown(); }
void cmd_iic_write(int argc, char** argv) { if(argc < 5) { bufferPrintf("usage: %s <bus> <address> <register> <value>\r\n", argv[0]); return; } uint8_t buffer[2]; int bus = parseNumber(argv[1]); int address = parseNumber(argv[2]); buffer[0] = parseNumber(argv[3]); buffer[1] = parseNumber(argv[4]); int error = i2c_tx(bus, address, buffer, 2); bufferPrintf("result: %d\r\n", error); }
void pmu_write_oocshdwn(int data) { uint8_t registers[1]; uint8_t discardData[5]; uint8_t poweroffData[] = {7, 0xD1, 0xFF, 0xF0}; registers[0] = 1; i2c_rx(PMU_I2C_BUS, PMU_GETADDR, registers, sizeof(registers), discardData, 3); i2c_tx(PMU_I2C_BUS, PMU_SETADDR, poweroffData, sizeof(poweroffData)); pmu_write_reg(PMU_OOCSHDWN, data, FALSE); // Wait for the hardware to shut down EnterCriticalSection(); while(TRUE) { udelay(100000); } }
int pmu_write_reg(int reg, int data, int verify) { uint8_t command[2]; command[0] = reg; command[1] = data; i2c_tx(PMU_I2C_BUS, PMU_SETADDR, command, sizeof(command)); if (!verify) return 0; uint8_t pmuReg = reg; uint8_t buffer = 0; i2c_rx(PMU_I2C_BUS, PMU_GETADDR, &pmuReg, 1, &buffer, 1); if (buffer == data) return 0; else return -1; }
int accel_write_reg(int reg, int data, int verify) { uint8_t command[2]; command[0] = reg; command[1] = data; i2c_tx(ACCEL_I2C_BUS, ACCEL_SETADDR, command, sizeof(command)); if(!verify) return 0; uint8_t accelReg = reg; uint8_t buffer = 0; i2c_rx(ACCEL_I2C_BUS, ACCEL_GETADDR, &accelReg, 1, &buffer, 1); if(buffer == data) return 0; else return -1; }
void fst_nunchuk_xmit_cmd (int reg, int cmd){ int data[2] = {0, 0}; int count = 0; if (reg != 0x00) { data[0] = reg; data[1] = cmd; count = 2; } else{ data[0] = cmd; count = 1; } i2c_tx(0x52, count, data, 150); //use DMA timer 3 to busy-delay for 2x FST_NUNCHUK_DELAY_US microseconds dtim0_delay(2*150); }
void Temp_I2C_sensor(int *array) { int i; short check_value_tx; short check_value_rx; unsigned char tmp[2]; const unsigned char temparray[1]= {REG_TEMP_VALUE}; // const due to not changing the value, char its a one byte element // this is so I can have my address in memory and create a pointer to point to it for the function const unsigned char all_temp_sensors[TEMP_SENSORS]= {TEMP_X_PLUS, TEMP_X_MINUS,TEMP_Y_PLUS, TEMP_Y_MINUS, TEMP_Z_PLUS, TEMP_Z_MINUS, TEMP_L_BOARD}; // Measure all temperature sensors at once. for (i=0; i<TEMP_SENSORS; i++) { check_value_tx = i2c_tx(all_temp_sensors[i],temparray,1);//place the address I want to talk to, //place the pointer to the array that stores my address, specify the number of bytes being sent //printf("\r\nSensor %i %s",i,I2C_error_str(check_value_tx)); if (check_value_tx==1) { check_value_rx = i2c_rx(all_temp_sensors[i],tmp,2);//place the address I want to talk to, //the array that stores my data, number of bytes being sent array[i]=tmp[0]; //printf("\r\nT%i = %i\r\n",i,tmp[0]); ctl_timeout_wait(ctl_get_current_time()+1000); } else if (check_value_tx==-1)//returns a -1 if there is no connection to the I2C device { array[i]=500;// printf("\r\nT%i = %i\r\n",i,tmp[0]); } else if (check_value_tx==0)//returns a zero if { array[i]=1111;// } } }
void i2c_start(char i2c_id) { // SDA goes from high to low while SCL is high i2cpin(SDA,HIGH); _delay_us(10); // give bus some time to bring SDA high i2cpin(SCL,HIGH); _delay_us(10); i2cpin(SDA,LOW); _delay_us(10); // send the I2C Device ID // send as Write (last bit is 0) i2c_tx(i2c_id<<1); }
void l3g4200d_powerdown() { uint8_t buf[] = {CTRL_REG1, 0x00}; i2c_tx(l3g4200d.i2c, L3G4200D_ADDRESS, buf, 2); }
//read write cmd for CLYDE int clyde_take_data(int *array) { int res,i,found=0; unsigned char tx[2],rx[2]; unsigned short rez; //prevent other tasks from sending commands to the EPS if(!ctl_mutex_lock(&EPS_mutex,CTL_TIMEOUT_DELAY,200)) { return -1; } //Set //printf("sent cmd \r\n"); //send cmd\ //take first 21 adc measurements for beacon for(i=0; i<20; i++) { tx[0]=EPS_ADC_COMMAND; tx[1]= name_addrs_clyde[i]; res=i2c_tx(EPS_I2C_ADDRESS,tx,2); //error msg or success if(res<0) { //printf("tx returned = %s\r\n",I2C_error_str(res)); //set invalid value array[i]=0xF800; //skip to the next channel continue; } //wait 1.2 ms (300) ctl_timeout_wait(ctl_get_current_time()+5); //read cmd res=i2c_rx(EPS_I2C_ADDRESS,rx,2); //error msg or success if (res<0) { //printf("rx returned = %s\r\n",I2C_error_str(res)); //set invalid value array[i]=0xF800; //skip to the next channel continue; } rez=rx[1]; rez|=rx[0]<<8; rez&=0x3FF; //printf("rez = %i\r\n",rez); array[i]=(int)rez; //printf("EPS data = %i, array # = %i\r\n",array[i],i); } //take status packet for beacon tx[0]=EPS_STATUS_COMMAND; tx[1]=0;//THIS DOESNT MATTER WHAT IT IS res=i2c_tx(EPS_I2C_ADDRESS,tx,2); //error msg or success //printf("%s\r\n",I2C_error_str(res)); //wait 1.2 ms (300) ctl_timeout_wait(ctl_get_current_time()+5); //read cmd res=i2c_rx(EPS_I2C_ADDRESS,rx,2); //error msg or success if(res!=0) { //printf("%s\r\n",I2C_error_str(res)); //invalid value array[i]=0xFFFF; } else { rez=rx[1]; rez|=rx[0]<<8; printf("rez = 0x%04X\r\n",rez); array[i]=(int)rez; } //unlock the EPS ctl_mutex_unlock(&EPS_mutex); //return success return RET_SUCCESS; }
bool lps331_powerdown() { uint8_t buf[] = {CTRL_REG1, 0x00}; return i2c_tx(lps331.i2c, LPS331_ADDRESS, buf, 2); }
bool lps331_set_datarate(lps331_datarate_t datarate) { uint8_t buf[] = {CTRL_REG1, datarate | 0x84}; return i2c_tx(lps331.i2c, LPS331_ADDRESS, buf, 2); }
void l3g4200d_set_scale(l3g4200d_scale_t scale, bool bdu) { uint8_t buf[] = {CTRL_REG4, scale | (bdu ? 0x80 : 0x00)}; i2c_tx(l3g4200d.i2c, L3G4200D_ADDRESS, buf, 2); }
void l3g4200d_enable_drdy(exti_line_t data_ready_line) { l3g4200d.data_ready_line = data_ready_line; uint8_t buf[] = {CTRL_REG3, 0x08}; i2c_tx(l3g4200d.i2c, L3G4200D_ADDRESS, buf, 2); }