static void handle_outbound(struct aura_node *node, struct aura_object *o, struct aura_buffer *buf) { int ret = -EIO; if (OPCODE("export")) { int gpio = aura_buffer_get_u32(buf); slog(4, SLOG_DEBUG, "gpio: export %d", gpio); ret = gpio_export(gpio); } else if (OPCODE("write")) { int gpio = aura_buffer_get_u32(buf); int value = aura_buffer_get_u32(buf); slog(4, SLOG_DEBUG, "gpio: write gpio %d value %d", gpio, value); ret = gpio_write(gpio, value); } else if (OPCODE("in")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_in(gpio); } else if (OPCODE("out")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_out(gpio); } else if (OPCODE("read")) { int gpio = aura_buffer_get_u32(buf); ret = gpio_read(gpio, &gpio); aura_buffer_rewind(buf); aura_buffer_put_u32(buf, gpio); } slog(0, SLOG_DEBUG, "gpio ret = %d", ret); if (ret) { aura_call_fail(node, o); return; } aura_queue_buffer(&node->inbound_buffers, buf); }
static void adm_read(int cs, char *buf, unsigned int bits) { int i, len = (bits + 7) / 8; __u8 mask; gpio_out(eecs, (cs ? eecs : 0)); udelay(EECK_EDGE_TIME); /* Byte assemble from MSB to LSB */ for (i = 0; i < len; i++) { __u8 byte; /* Bit bang from MSB to LSB */ for (mask = 0x80, byte = 0; mask && bits > 0; mask >>= 1, bits --) { __u8 gp; /* Clock low */ gpio_out(eesk, 0); udelay(EECK_EDGE_TIME); /* Input on rising edge */ gp = gpio_in(); if (gp & eedi) byte |= mask; /* Clock high */ gpio_out(eesk, eesk); udelay(EECK_EDGE_TIME); } *buf++ = byte; } /* Clock low */ gpio_out(eesk, 0); udelay(EECK_EDGE_TIME); if (cs) gpio_out(eecs, 0); }
bool sht11_write(SHT11_t *structure, unsigned char cmd, unsigned char *status_reg) { /* * If flag busy is true, signify that a command has been already send * and now we will wait to read data. * If flag busy is true will return true, to return signal that the command is send * and now we will wait for response. */ if(structure->busy) return true; /* * This is the single case that this function run forward. * This will go on only if a is called for first time, response is read * nack is received or timeout is occurred. * Now will set the timeout in millisecond and send the command. */ timer_interval(&structure->Timeout_Timer, 500); structure->busy = true; gpio_function_set(structure->Scl, GPIO_OUT_OPEN_DRAIN); gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); /* Send transmission start. * ___ ___ * data = \_____/ * ___ ___ * clk = _/ \_/ \__ */ gpio_out(structure->Scl, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Sda, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); /* * Send command. */ unsigned char data_cnt = 0; unsigned char _cmd = cmd; for(; data_cnt < 8; data_cnt++) { if(_cmd & 0x80) gpio_out(structure->Sda, 1); else gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_out(structure->Sda, 1); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); } sht11_delay(structure); _cmd = _cmd << 1; } gpio_out(structure->Scl, 1); sht11_delay(structure); signed int ack = gpio_in(structure->Sda); gpio_out(structure->Scl, 0); sht11_delay(structure); if(ack) { structure->busy = false; return false; } structure->reg_inst = cmd; if(cmd == SHT11_WRITE_STATUS_REG) { /* * Write status register */ gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); unsigned char st_reg = *status_reg; for(data_cnt = 0; data_cnt < 8; data_cnt++) { if(st_reg & 0x80) gpio_out(structure->Sda, 1); else gpio_out(structure->Sda, 0); sht11_delay(structure); gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_out(structure->Sda, 1); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); } sht11_delay(structure); st_reg = st_reg << 1; } gpio_out(structure->Scl, 1); sht11_delay(structure); signed int ack = gpio_in(structure->Sda); gpio_out(structure->Scl, 0); sht11_delay(structure); if(ack) { structure->busy = false; return false; } structure->busy = false; } else if(cmd == SHT11_READ_STATUS_REG) { /* * Read status register. */ unsigned char st_reg = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { st_reg = st_reg << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) st_reg |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); *status_reg = st_reg; /* * Read checksum. */ unsigned char chk = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { chk = chk << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) chk |= 1; gpio_out(structure->Scl, 0); sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); structure->busy = false; } return true; }
bool sht11_read(SHT11_t *structure) { /* * Check if sht11 is not responding. * If sht11 will not respond until timeout will occurred, * the busy flag will be set as false to permit to sht11_write function * to send another command. */ if(timer_tick(&structure->Timeout_Timer)) { structure->busy = false; return false; } /* * If busy flag is not busy this function will don't have anything to read from sh11. */ if(!structure->busy) return false; /* * If flag busy is true w will wait to read data. * Now will wait the sda pin to be '0', that signify data ready to be read. * If sda pin is '1' will return false, this signify that the sht11 is busy. */ if(gpio_in(structure->Sda)) return false; /* * Now the data is available on the buss. * Will start to read data and return true, this is the single case that * this function return true. */ unsigned char data_cnt; sht11_delay(structure); unsigned short data = 0; /* * Read MSB data */ for(data_cnt = 0; data_cnt < 8; data_cnt++) { data = data << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) data |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); /* * Read LSB data. */ for(data_cnt = 0; data_cnt < 8; data_cnt++) { data = data << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) data |= 1; gpio_out(structure->Scl, 0); if(data_cnt == 7) { gpio_function_set(structure->Sda, GPIO_OUT_OPEN_DRAIN); gpio_out(structure->Sda, 0); } sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); gpio_function_set(structure->Sda, GPIO_IN_PULL_UP); sht11_delay(structure); /* * Read checksum. */ unsigned char chk = 0; for(data_cnt = 0; data_cnt < 8; data_cnt++) { chk = chk << 1; gpio_out(structure->Scl, 1); sht11_delay(structure); if(gpio_in(structure->Sda)) chk |= 1; gpio_out(structure->Scl, 0); sht11_delay(structure); } gpio_out(structure->Scl, 1); sht11_delay(structure); gpio_out(structure->Scl, 0); sht11_delay(structure); structure->busy = false; if(structure->reg_inst == SHT11_START_MEASURE_TEMPERATURE) { structure->temperature = structure->vdd_comp + 0.01 * (float)data; } else if(structure->reg_inst == SHT11_START_MEASURE_HUMIDITY) { structure->humidity = -4 + 0.0405 * (float)data + -0.0000028 * ((float)data * (float)data); } return true; }