//get accelerometer samples, mask can choose which axis are requested (8b000_0zyx) void accel_get_sample(int8_t *xdat, int8_t *ydat, int8_t *zdat, uint8_t mask) { uint8_t i2cbuffer[3]; //Initialize I2C master i2c_init(I2C_SCL_RATIO(1), i2cbuffer, 3); //for(int i = 0; i< 10000; i++){} if((mask & 0x01) > 0) //send x axis *xdat = (int8_t)(((uint8_t)(I2C_BYTE_READ(LIS3DH_ADDR, LIS3DH_OUT_X_H)))); if((mask & 0x02) > 0) //send y axis *ydat = (int8_t)(((uint8_t)(I2C_BYTE_READ(LIS3DH_ADDR, LIS3DH_OUT_Y_H)))); if((mask & 0x04) > 0) //send z axis *zdat = (int8_t)(((uint8_t)(I2C_BYTE_READ(LIS3DH_ADDR, LIS3DH_OUT_Z_H)))); }
void accel_get_XY_samples(int8_t *xdat, int8_t *ydat, uint8_t samps) { uint8_t i2cbuffer[3]; int i; //Initialize I2C master i2c_init(I2C_SCL_RATIO(1), i2cbuffer, 3); for(i = 0; i< 10000; i++);//wait for i2c clock to lock for(i = 0; i< samps; i++) { xdat[i] = (int8_t)(((uint8_t)(I2C_BYTE_READ(LIS3DH_ADDR, LIS3DH_OUT_X_H)))); ydat[i] = (int8_t)(((uint8_t)(I2C_BYTE_READ(LIS3DH_ADDR, LIS3DH_OUT_Y_H)))); } }
int main (void) { uint32_t j; uint8_t buffer[100]; uint8_t rxbuffer[100]; SystemInit(); //Initialize I2C master i2c_init(I2C_SCL_RATIO(100000), i2cbuffer, 104); for (j = 0; j < 100; j++) { buffer[j] = 0x55 + j; rxbuffer[j] = 0; } while (1) /* Loop forever */ { // Write 1byte data(0x80) to register(address is 0x00 and 1byte) of device(address is 0x21) I2C_BYTE_WRITE(0x21, 0x00, 0x80); // Read 1byte data from register(address is 0x06 and 1byte) of device(address is 0x21) buffer[0] = I2C_BYTE_READ(0x21, 0x06); // Write 1byte data(0x11) to register(address is 0x00 and 2byte) of device(address is 0x50) I2C_BYTE_WRITE2(0x50, 0x00, 0x11); // Read 1byte data from register(address is 0x00 and 2byte) of device(address is 0x50) buffer[0] = I2C_BYTE_READ2(0x50, 0x00); // Write nbyte data(50byte) to register(address is 0x00 and 2byte) of device(address is 0x50) I2C_nBYTE_WRITE2(0x50, 0x00, buffer, 50); // Read nbyte data(60byte) from register(address is 0x00 and 2byte) of device(address is 0x50) I2C_nBYTE_READ2(0x50, 0x00, rxbuffer, 60); } }
int ble_i2c( const uint8_t * const pcCommandString,uint8_t* pcWriteBuffer,uint32_t commpare_length) { const int8_t *pcom; uint32_t pxParameterStringLength; uint8_t len; uint8_t i2c_addr ; uint8_t i2c_reg ; uint8_t i2c_data; if(pcCommandString[commpare_length+1] == '=') { //i2c_addr pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 1, &pxParameterStringLength); if(pxParameterStringLength > 0) { i2c_addr = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); } else { goto pram_err; } //i2c_reg pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 2, &pxParameterStringLength); if(pxParameterStringLength > 0) { i2c_reg = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); } else { goto pram_err; } // i2c R&W? pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 0, &pxParameterStringLength); if(pxParameterStringLength == 1) { syscon_SetPMCR1WithMask(QN_SYSCON,P23_MASK_PIN_CTRL | P24_MASK_PIN_CTRL,P23_I2C_SDA_PIN_CTRL | P24_I2C_SCL_PIN_CTRL); i2c_init(I2C_SCL_RATIO(100000), i2c_buff, 4); if(pcom[0] == 'R') { i2c_data = I2C_BYTE_READ(i2c_addr,i2c_reg); if(i2c_is_finish()) { len = commpare_length+1; memcpy(pcWriteBuffer, pcCommandString, len); len += sprintf((char *)pcWriteBuffer + len,":0x%02x\r\nOK\r\n",i2c_data); return len; } goto pram_err; } else if(pcom[0] == 'W') { //i2c_data pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 3, &pxParameterStringLength); if(pxParameterStringLength > 0) { i2c_data = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); } else { goto pram_err; } I2C_BYTE_WRITE(i2c_addr, i2c_reg, i2c_data); if(!i2c_is_finish()) { goto pram_err; } } } else { goto pram_err; } } else { goto pram_err; } return sprintf((char*)pcWriteBuffer,"OK\r\n"); pram_err: return sprintf((char*)pcWriteBuffer,"ERR\r\n"); }