//-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- int main(int argc, char **argv) { uint32 Counter; int16 Temperature; uint8 Humidity; uint8 HwRev; char Mode; Counter = 0; Mode = 0; HwRev = GetRaspberryHwRevision(); if((argc > 1) && ((argv[1][0]=='S') || (argv[1][0]=='L') || (argv[1][0]=='C'))) Mode = argv[1][0]; if(!Mode) { printf("Raspi-SHT21 V3.0.0 by Martin Steppuhn (www.emsystech.de) [" __DATE__ " " __TIME__"]\n"); printf("Options:\n"); printf(" S : [20.0 99]\n"); printf(" L : [temperature=20.0][humidity=99]\n"); printf(" C : [Temperature,20,0][Humidity,99]\n"); printf("RaspberryHwRevision=%i\r\n",HwRev); } if(HwRev < 2) I2C_Open("/dev/i2c-0"); // Hardware Revision 1.0 else I2C_Open("/dev/i2c-1"); // Hardware Revision 2.0 I2C_Setup(I2C_SLAVE, 0x40); if(I2cError) { I2C_PrintError(); exit(1); } SHT21_Read(&Temperature,&Humidity); if(Sht21Error == 0) { if( Mode == 'S') printf("%.1f\t%u\n",((float)Temperature)/10,Humidity); else if(Mode == 'L') printf("temperature=%.1f\nhumidity=%u\n",((float)Temperature)/10,Humidity); else if(Mode == 'C') printf("Temperature,%.1f\nHumidity,%u\n",((float)Temperature)/10,Humidity); else { while(1) { SHT21_Read(&Temperature,&Humidity); if(Sht21Error == 0) printf("%lu\t%.1f\t%u\n",Counter++,((float)Temperature)/10,Humidity); else { PrintSht21Error(); I2cError = 0; } DelayMs(1000); } } } else { PrintSht21Error(); } I2C_Close(); return(0); }
/* Set Factory I2C device address */ void Set_Factory(void){ int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 0; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) { /* Close I2C-BUS */ I2C_Close(&fd); printf("Found with I2c address 0x%02x \n", adrs); buff[0]=0xF0; buff[1]=0x21; /* value: * New Address to be setted * / /* Open I2C-BUS */ I2C_Open(&fd, adrs); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); printf("Reverted to 0x21\n"); } /* Close I2C-BUS */ I2C_Close(&fd); } } }
uint8_t IO2::readID() { int fd; unsigned char buff[1] = {GET_ID}, id, result; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, &id, 1); I2C_Close(&fd); return id; }
uint8_t IO2::digitalRead(uint8_t pin) { int fd; unsigned char buff[1] = {GET_PORT}, data, result; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, &data, 1); I2C_Close(&fd); return !!(data & pin); }
/* Use this function to find module i2c address */ void BusScan(void) { int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 1; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) printf("Found with I2c address 0x%02x \n", adrs); /* Close I2C-BUS */ I2C_Close(&fd); } } }
void Write_RTC(void){ /* Syncin MOD-RTC with system clock */ time_t ltime; struct tm *rtc_tm; unsigned char buff[10]; int fd; ltime = time(NULL); rtc_tm = localtime(<ime); buff[0] = 0x02; //Pointer to the first register buff[1] = InttoBCD(rtc_tm -> tm_sec); buff[2] = InttoBCD(rtc_tm -> tm_min); buff[3] = InttoBCD(rtc_tm -> tm_hour); buff[4] = InttoBCD(rtc_tm -> tm_mday); buff[5] = InttoBCD(rtc_tm -> tm_wday); buff[6] = InttoBCD(rtc_tm -> tm_mon+1); //because local time counts months 0-11 but MOD-RTC 1-12 buff[7] = InttoBCD(rtc_tm -> tm_year); I2C_Open(&fd, 0x51); I2C_Send(&fd, buff, 8); I2C_Close(&fd); }
/* Read GPIO Digital Inputs */ void ReadGPIO(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x03; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("GPIO: 0x%02x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read Board Firmware Version */ void ReadSV(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x21; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("Firmware Version: %d.0.%2d\n", data[0]>>4,data[0]&0x0f); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read Board ID */ void ReadID(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read ID */ I2C_Read(&fd, data, 1); printf("ID: 0x%x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set pin directions */ void Set_TRIS(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x01; buff[1]=value; /* value: * 0b0ddddddd * bit0 coresponding to GPIO0, * as "1" set pin as input "0" - set pin as output. * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set GPIO Pull Ups */ void Set_PU(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x04; buff[1]=value; /* value: * 0b000dxddd * bit0 coresponding to GPIO0, * as "1" enables the pullup and "0" - disables it. * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Set Relay states */ void Relay(unsigned char value){ int fd; unsigned char buff[5]; buff[0]=0x40; buff[1]=value; /* value: * should be iterpreted as binary * b0 is relay 1 * b1 is relay 2 * 0 turn of relay * 1 turn on relay * / /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); }
void I2C0_Init(void) { /* Open I2C0 and set clock to 100k */ I2C_Open(I2C0, 100000); /* Get I2C0 Bus Clock */ printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0)); }
void IO2::setAddress(uint8_t newAddress) { int fd; unsigned char buff[2] = {SET_ADDRESS, newAddress}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
uint16_t IO2::analogRead(uint8_t pin) { int fd; uint16_t result; unsigned char buff[1] = {pin}, data[2]; I2C_Open(&fd, address); I2C_Send(&fd, buff, 1); I2C_Close(&fd); I2C_Open(&fd, address); I2C_Read(&fd, data, 2); I2C_Close(&fd); result = (data[1] << 8) | data[0]; return result; }
void i2c_Init(void) //ericyang 20151120 add for oled i2c driver { CLK_EnableModuleClock(I2C0_MODULE);//ericyang 20151120 SYS_ResetModule(I2C0_RST); SYS->GPB_MFP = (SYS->GPB_MFP & (~SYS_GPB_MFP_PB2MFP_Msk) ) | SYS_GPB_MFP_PB2MFP_I2C_SCL; SYS->GPB_MFP = (SYS->GPB_MFP & (~SYS_GPB_MFP_PB3MFP_Msk) ) | SYS_GPB_MFP_PB3MFP_I2C_SDA; I2C_Open(I2C0, 400000); }
void I2C0_Init(void) { /* Open I2C0 and set clock to 100k */ I2C_Open(I2C0, 100000); /* Get I2C0 Bus Clock */ printf("I2C0 clock %d Hz\n", I2C_GetBusClockFreq(I2C0)); I2C_EnableInt(I2C0); NVIC_EnableIRQ(I2C0_IRQn); }
int I2C_Read(uint8_t addr,void *pbuffer,uint16_t len) { struct i2c_job_st i2c_job; xSemaphoreTake(xSemaphoreI2C_Mutex,portMAX_DELAY); if (I2C_isBusy()) { i2c_error_flags = I2C_SR1_SB; xSemaphoreGive(xSemaphoreI2C_Mutex); return FALSE; } i2c_cntr = 0; i2c_oper = I2C_Direction_Receiver; job = &i2c_job; i2c_addr = addr << 1; i2c_job.buf = (uint8_t *)pbuffer; i2c_job.len = len; i2c_job.dir = I2C_Direction_Receiver; i2c_job.last = TRUE; i2c_done = i2c_err = FALSE; // clear the semaphore while (xSemaphoreTake(xSemaphoreI2C_Work,0)); if ((i2c_oper == I2C_Direction_Receiver) && (len == 2)) I2C_NACKPositionConfig(I2Cx,I2C_NACKPosition_Next); else I2C_NACKPositionConfig(I2Cx,I2C_NACKPosition_Current); I2C_AcknowledgeConfig(I2Cx,ENABLE); I2C_ITConfig(I2Cx,I2C_IT_BUF | I2C_IT_ERR | I2C_IT_EVT,ENABLE); /* Send START condition */ I2C_GenerateSTART(I2Cx, ENABLE); while (!i2c_done && !i2c_err) { if (!xSemaphoreTake(xSemaphoreI2C_Work,SEMA_DELAY)) { I2C_Open(0); i2c_err = TRUE; break; } } I2C_ITConfig(I2Cx,I2C_IT_BUF | I2C_IT_ERR | I2C_IT_EVT,DISABLE); xSemaphoreGive(xSemaphoreI2C_Mutex); return !i2c_err; }
int main(int argc, char **argv) { int file; unsigned char buffer[2], data[2], address; if(argc < 3) { printf("Too few arguments.\n Type -help.\n"); exit(1); } if(!strcmp(argv[1], "-relay")) { address = strtol(argv[2], NULL, 0); buffer[0] = 0x10; buffer[1] = strtol(argv[3], NULL, 0); I2C_Open(&file, address); I2C_Send(&file, buffer, 2); I2C_Close(&file); } else if(!strcmp(argv[1], "-dig")) { int i; address = strtol(argv[2], NULL, 0); buffer[0] = 0x20; I2C_Open(&file, address); I2C_Send(&file, buffer, 1); I2C_Read(&file, data, 1); I2C_Close(&file); for(i = 0; i < 4; i++) { printf("IN[%d] = %d\n", i+1, (data[0] >> i) & 0x01); } }
// ********************************************************************** // I2C Master Restart // ********************************************************************** void I2C_MS_Master_Restart() { /* Open I2C module and set bus clock */ I2C_Open(I2C_MS_PORT, 100000); /* Enable I2C interrupt */ I2C_EnableInt(I2C_MS_PORT); #ifdef I2C_MS_PORT0 NVIC_EnableIRQ(I2C0_IRQn); #else NVIC_EnableIRQ(I2C1_IRQn); #endif }
void TwoWire::begin(uint8_t address) { IRQn_Type irq=I2C0_IRQn; NVIC_DisableIRQ(irq); NVIC_ClearPendingIRQ(irq); NVIC_SetPriority(irq, 3); NVIC_EnableIRQ(irq); I2C_Open(i2c,I2C_CLOCK); status = SLAVE_IDLE; I2C_SetSlaveAddr(i2c, 0, address, 0); /* Slave Address */ I2C_EnableInt(i2c); I2C_SET_CONTROL_REG(i2c, I2C_SI_AA); /* I2C enter no address SLV mode */ }
void IO2::setRelay(uint8_t relay, uint8_t state) { if(state) relay_status |= relay; else relay_status &= ~relay; int fd; unsigned char buff[2] = {SET_RELAY, relay_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::setPullpin(uint8_t pin, uint8_t state) { if(state) pp_status |= pin; else pp_status &= ~pin; int fd; unsigned char buff[2] = {SET_PU, pp_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::digitalWrite(uint8_t pin, uint8_t level) { if(level) lat_status |= pin; else lat_status &= ~pin; int fd; unsigned char buff[2] = {SET_LAT, lat_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void IO2::pinMode(uint8_t pin, uint8_t mode) { if(mode) tris_status |= pin; else tris_status &= ~pin; int fd; unsigned char buff[2] = {SET_TRIS, tris_status}; I2C_Open(&fd, address); I2C_Send(&fd, buff, 2); I2C_Close(&fd); }
void TwoWire::begin(void) { /* Init System, IP clock and multi-function I/O */ SYS_UnlockReg(); /* Enable IP clock */ CLK_EnableModuleClock(I2C0_MODULE); /* Select IP clock source and clock divider */ //CLK_SetModuleClock(I2C_Desc[0].module,0,0); SYS->GPF_MFP |= (SYS_GPF_MFP_PF2_I2C0_SDA | SYS_GPF_MFP_PF3_I2C0_SCL); SYS->ALT_MFP1 &= ~(SYS_ALT_MFP1_PF2_Msk | SYS_ALT_MFP1_PF3_Msk); SYS->ALT_MFP1 |= (SYS_ALT_MFP1_PF2_I2C0_SDA | SYS_ALT_MFP1_PF3_I2C0_SCL); SYS_LockReg(); I2C_Open(i2c,I2C_CLOCK); status = MASTER_IDLE; delay(1); }
void I2C0_Init(void) { /* Open I2C0 and set clock to 100k */ I2C_Open(I2C0, 100000); /* Get I2C0 Bus Clock */ printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0)); /* Set I2C0 2 Slave Addresses */ I2C_SetSlaveAddr(I2C0, 0, 0x15, I2C_GCMODE_DISABLE); /* Slave Address : 0x15 */ I2C_SetSlaveAddr(I2C0, 1, 0x35, I2C_GCMODE_DISABLE); /* Slave Address : 0x35 */ /* Enable I2C0 interrupt */ I2C_EnableInt(I2C0); NVIC_EnableIRQ(I2C0_IRQn); }
/* Read GPIO Analog Inputs */ void ReadADC(unsigned char value) { int fd; unsigned char buff[5]; unsigned char data[5]; switch(value) { case 255: ReadADC(0); ReadADC(1); ReadADC(2); ReadADC(3); ReadADC(5); break; case 0: case 1: case 2: case 3: case 5: buff[0]=0x10+value; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 2); /* Convert to Volts. Vref = 3.3V, ADC is 10 bits */ float volts = data[0] * 0.003222656 + data[1]*0.825; printf("ADC%u: %1.3fV\n",value, volts); /* Close I2C-BUS */ I2C_Close(&fd); break; default: printf("ADC%u not found\n",value); } }
void I2C3_Init(void) { /* Open I2C3 and set clock to 100k */ I2C_Open(I2C3, 100000); /* Get I2C3 Bus Clock */ printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C3)); /* Set I2C3 4 Slave Addresses */ I2C_SetSlaveAddr(I2C3, 0, 0x15, I2C_GCMODE_DISABLE); /* Slave Address : 0x15 */ I2C_SetSlaveAddr(I2C3, 1, 0x35, I2C_GCMODE_DISABLE); /* Slave Address : 0x35 */ I2C_SetSlaveAddr(I2C3, 2, 0x55, I2C_GCMODE_DISABLE); /* Slave Address : 0x55 */ I2C_SetSlaveAddr(I2C3, 3, 0x75, I2C_GCMODE_DISABLE); /* Slave Address : 0x75 */ I2C_EnableInt(I2C3); NVIC_EnableIRQ(I2C3_IRQn); }
void I2C1_Init(void) { /* Open I2C1 and set clock to 100k */ I2C_Open(I2C1, 400000); /* Get I2C1 Bus Clock */ printf("I2C1 clock %d Hz\n", I2C_GetBusClockFreq(I2C1)); /* Set I2C1 2 Slave Addresses */ I2C_SetSlaveAddr(I2C1, 0, SLAVE_ADDRESS, I2C_GCMODE_DISABLE); /* Set Slave Address */ I2C_EnableInt(I2C1); NVIC_EnableIRQ(I2C1_IRQn); /* I2C enter no address SLV mode */ I2C_SET_CONTROL_REG(I2C1, I2C_SI | I2C_AA); }