void I2C_WriteBitSCL(I2C_ConfigStruct* conf, uint8_t data) { uint32_t scl_port_num = I2C_PORT(conf->scl); uint32_t scl_pin_index = I2C_PIN_INDEX(conf->scl); if(scl_port_num == 0) { if(data == 1) GPIO_SetBits(GPIOA, scl_pin_index); else GPIO_ResetBits(GPIOA, scl_pin_index); } else if(scl_port_num == 1) { if(data == 1) GPIO_SetBits(GPIOB, scl_pin_index); else GPIO_ResetBits(GPIOB, scl_pin_index); } else if(scl_port_num == 2) { if(data == 1) GPIO_SetBits(GPIOC, scl_pin_index); else GPIO_ResetBits(GPIOC, scl_pin_index); } else if(scl_port_num == 3) { if(data == 1) GPIO_SetBits(GPIOD, scl_pin_index); else GPIO_ResetBits(GPIOD, scl_pin_index); } }
void I2C_WriteBitSDA(I2C_ConfigStruct* conf, uint8_t data) { uint32_t sda_port_num = I2C_PORT(conf->sda); uint32_t sda_pin_index = I2C_PIN_INDEX(conf->sda); if(sda_port_num == 0) { if(data == 1) GPIOA->OUTENCLR = sda_pin_index; else GPIOA->OUTENSET = sda_pin_index; } else if(sda_port_num == 1) { if(data == 1) GPIOB->OUTENCLR = sda_pin_index; else GPIOB->OUTENSET = sda_pin_index; } else if(sda_port_num == 2) { if(data == 1) GPIOC->OUTENCLR = sda_pin_index; else GPIOC->OUTENSET = sda_pin_index; } else if(sda_port_num == 3) { if(data == 1) GPIOD->OUTENCLR = sda_pin_index; else GPIOD->OUTENSET = sda_pin_index; } }
uint8_t I2C_ReadBitSDA(I2C_ConfigStruct* conf) { uint32_t sda_port_num = I2C_PORT(conf->sda); uint32_t sda_pin_index = I2C_PIN_INDEX(conf->sda); if(sda_port_num == 0) { if(GPIOA->DATA & sda_pin_index) return 1; else return 0; } else if(sda_port_num == 1) { if(GPIOB->DATA & sda_pin_index) return 1; else return 0; } else if(sda_port_num == 2) { if(GPIOC->DATA & sda_pin_index) return 1; else return 0; } else if(sda_port_num == 3) { if(GPIOD->DATA & sda_pin_index) return 1; else return 0; } return 0; }
static int pca9556_attach(dev_info_t *dip) { pca9556_unit_t *pcap; int instance = ddi_get_instance(dip); char name[MAXNAMELEN]; char *device_name; minor_t minor; int i, num_ports; if (ddi_soft_state_zalloc(pca9556_soft_statep, instance) != 0) { cmn_err(CE_WARN, "%s%d failed to zalloc softstate", ddi_get_name(dip), instance); return (DDI_FAILURE); } pcap = ddi_get_soft_state(pca9556_soft_statep, instance); if (pcap == NULL) return (DDI_FAILURE); mutex_init(&pcap->pca9556_mutex, NULL, MUTEX_DRIVER, NULL); cv_init(&pcap->pca9556_cv, NULL, CV_DRIVER, NULL); (void) snprintf(pcap->pca9556_name, sizeof (pcap->pca9556_name), "%s_%d", ddi_driver_name(dip), instance); device_name = ddi_get_name(dip); if (strcmp(device_name, "i2c-pca9555") == 0) { num_ports = PCA9555_NUM_PORTS; pcap->pca9555_device = B_TRUE; } else { num_ports = PCA9556_NUM_PORTS; pcap->pca9555_device = B_FALSE; minor = INST_TO_MINOR(instance); } for (i = 0; i < num_ports; i++) { if (!(pcap->pca9555_device)) { (void) snprintf(pcap->pca9556_name, sizeof (pcap->pca9556_name), "%s_%d", ddi_driver_name(dip), instance); (void) snprintf(name, sizeof (name), "%s", pcap->pca9556_name); } else { (void) sprintf(name, "port_%d", i); minor = INST_TO_MINOR(instance) | PORT_TO_MINOR(I2C_PORT(i)); } if (ddi_create_minor_node(dip, name, S_IFCHR, minor, PCA9556_NODE_TYPE, NULL) == DDI_FAILURE) { cmn_err(CE_WARN, "%s: failed to create node for %s", pcap->pca9556_name, name); pca9556_detach(dip); return (DDI_FAILURE); } } pcap->pca9556_flags |= PCA9556_MINORFLAG; /* * Add a zero-length attribute to tell the world we support * kernel ioctls (for layered drivers) */ (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0); /* * preallocate a single buffer for all reads and writes */ if (i2c_transfer_alloc(pcap->pca9556_hdl, &pcap->pca9556_transfer, 2, 2, I2C_SLEEP) != I2C_SUCCESS) { cmn_err(CE_WARN, "%s i2c_transfer_alloc failed", pcap->pca9556_name); pca9556_detach(dip); return (DDI_FAILURE); } pcap->pca9556_flags |= PCA9556_TBUFFLAG; pcap->pca9556_transfer->i2c_version = I2C_XFER_REV; if (i2c_client_register(dip, &pcap->pca9556_hdl) != I2C_SUCCESS) { ddi_remove_minor_node(dip, NULL); cmn_err(CE_WARN, "%s i2c_client_register failed", pcap->pca9556_name); pca9556_detach(dip); return (DDI_FAILURE); } pcap->pca9556_flags |= PCA9556_REGFLAG; /* * Store the dip for future dip. */ pcap->pca9556_dip = dip; return (DDI_SUCCESS); }
uint32_t I2C_Init(I2C_ConfigStruct* conf) { uint32_t scl_port_num; uint32_t scl_pin_index; uint32_t sda_port_num; uint32_t sda_pin_index; scl_port_num = I2C_PORT(conf->scl); scl_pin_index = I2C_PIN_INDEX(conf->scl); sda_port_num = I2C_PORT(conf->sda); sda_pin_index = I2C_PIN_INDEX(conf->sda); //SCL setting GPIO_InitDef.GPIO_Pin = scl_pin_index; GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; if(scl_port_num == 0) { GPIO_Init(GPIOA, &GPIO_InitDef); GPIO_SetBits(GPIOA, scl_pin_index); } else if(scl_port_num == 1) { GPIO_Init(GPIOB, &GPIO_InitDef); GPIO_SetBits(GPIOB, scl_pin_index); } else if(scl_port_num == 2) { GPIO_Init(GPIOC, &GPIO_InitDef); GPIO_SetBits(GPIOC, scl_pin_index); } else if(scl_port_num == 3) { GPIO_Init(GPIOD, &GPIO_InitDef); GPIO_SetBits(GPIOD, scl_pin_index); } else { printf("SCL pin Port number error\r\n"); return 1; } //SDA setting GPIO_InitDef.GPIO_Pin = sda_pin_index; GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; if(sda_port_num == 0) { GPIO_Init(GPIOA, &GPIO_InitDef); GPIO_ResetBits(GPIOA, sda_pin_index); } else if(sda_port_num == 1) { GPIO_Init(GPIOB, &GPIO_InitDef); GPIO_ResetBits(GPIOB, sda_pin_index); } else if(sda_port_num == 2) { GPIO_Init(GPIOC, &GPIO_InitDef); GPIO_ResetBits(GPIOC, sda_pin_index); } else if(sda_port_num == 3) { GPIO_Init(GPIOD, &GPIO_InitDef); GPIO_ResetBits(GPIOD, sda_pin_index); } else { printf("SDA pin Port number error\r\n"); return 1; } PAD_AFConfig((PAD_Type) scl_port_num, scl_pin_index, (PAD_AF_TypeDef) PAD_AF1); PAD_AFConfig((PAD_Type) sda_port_num, sda_pin_index, (PAD_AF_TypeDef) PAD_AF1); return 0; }