static int dht11_read(uint8_t pin, uint8_t *buf, uint8_t buflen) { if (buflen < DHT_DATA_LEN) return DHT_ENOSPACE; /* Send START signal by pulling low the Data Signal bus at least 18ms */ gpio_setup(pin, OUTPUT); gpio_write(pin, LOW); mcu_delay(20000); /* Release the Data Signal bus and wait 20~40us for DHT's response */ gpio_write(pin, HIGH); gpio_setup(pin, INPUT); mcu_delay(30); /* DHT responds by pulling the Data Signal bus for 80us */ while (gpio_read(pin) != LOW); while (gpio_read(pin) == LOW); /* DHT pulls high the Data Signal bus for 80us and prepares the sending data */ while (gpio_read(pin) != LOW); uint8_t index, checksum = 0; for (index = 0; index < DHT_DATA_LEN; index++) { uint8_t bit, value = 0; for (bit = 0x80; bit; bit >>= 1) { /* DHT pulls low the Data Signal bus for 50us to start sending each bit */ while (gpio_read(pin) == LOW); /* DHT pulls high the Data Signal bus, the length of duration is determined by value of the sending bit. 0: 26~28us, 1: 70us */ mcu_delay(40); if (gpio_read(pin) != LOW) { while (gpio_read(pin) != LOW); value |= bit; } } buf[index] = value; } /* Acquire the Data Signal bus and keep it high */ gpio_setup(pin, OUTPUT); gpio_write(pin, HIGH); for (index = 0; index < DHT_DATA_LEN-1; index++) checksum += buf[index]; return (buf[4] == checksum)? DHT_SUCCESS : DHT_ECHECKSUM; }
/* Send CMD55/ACMD41 until the card returns 0 or times out */ static int wakeup(struct sdcard_spi_dev_t *dev, uint8_t is_sdhc) { uint8_t response; uint16_t attempt_count = INIT_ATTEMPT_COUNT_MAX; do { attempt_count--; if (send_cmd(dev, &response, CMD55_APP_CMD, 0, 0x65) < 0) return -1; if (is_sdhc) { if (send_cmd(dev, &response, ACMD41_SEND_OP_COND, 0x40000000, 0x77) < 0) return -1; } else { if (send_cmd(dev, &response, ACMD41_SEND_OP_COND, 0, 0xE5) < 0) return -1; } if (response == 0) break; /* Let's try again 10 ticks later if the card is not ready */ mcu_delay(TICKS_PER_SEC / 100); } while (attempt_count > 0 && response != 0); if (attempt_count == 0 && response != 0) return -1; return 0; }
void send_high_bit() { //debug_print(DBG_INFO, "1 \n"); pwm_configure(pwm1, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); // 1 pwm_configure(pwm2, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); pwm_configure(pwm3, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); pwm_configure(pwm4, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); pwm_enable(pwm1); pwm_enable(pwm2); pwm_enable(pwm3); pwm_enable(pwm4); mcu_delay(MESSAGE_SLEEP); }
void send_low_bit() { //debug_print(DBG_INFO, "0 \n"); pwm_configure(pwm1, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); // 0 pwm_configure(pwm2, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); pwm_configure(pwm3, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); pwm_configure(pwm4, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); pwm_enable(pwm1); pwm_enable(pwm2); pwm_enable(pwm3); pwm_enable(pwm4); mcu_delay(MESSAGE_SLEEP); }
void send_preamble_sequence(int preamble_duration) { //debug_print(DBG_INFO, "Preamble\n"); pwm_configure(pwm1, PREAMBLE_DUTY, PREAMBLE_PERIOD); pwm_configure(pwm2, PREAMBLE_DUTY, PREAMBLE_PERIOD); pwm_configure(pwm3, PREAMBLE_DUTY, PREAMBLE_PERIOD); pwm_configure(pwm4, PREAMBLE_DUTY, PREAMBLE_PERIOD); pwm_enable(pwm1); pwm_enable(pwm2); pwm_enable(pwm3); pwm_enable(pwm4); mcu_delay(preamble_duration*PREAMBLE_SLEEP); }
/* Send CMD1 until the card returns 0 or times out */ static int old_wakeup(struct sdcard_spi_dev_t *dev) { uint8_t response; uint16_t attempt_count = INIT_ATTEMPT_COUNT_MAX; do { attempt_count--; if (send_cmd(dev, &response, CMD1_SEND_OP_COND, 0, 0xF9) < 0) return -1; if (response == 0) break; /* Let's try again 10 ticks later if the card is not ready */ mcu_delay(TICKS_PER_SEC / 100); } while (attempt_count > 0 && response != 0); if (attempt_count == 0 && response != 0) return -1; return 0; }
void mcu_main() { // by default, the edison has ID 0 int edisonID = 0; // if we pass in an argument, use it for the edisonID // please pass in an integer //if (argc == 2) { //edisonID = atoi(argv[1]); //} int temp; while (1) { //int host_receive(unsigned char *buf, int length) temp = host_receive((unsigned char *)host_message, BUFFER_LENGTH); if (temp > 0) { debug_print(DBG_INFO, "Received a Message!\n"); host_send((unsigned char*)"hello mcu\n", 10); preamble_length = host_message[0]; } // Preamble - Signals the Receiver Message Incoming send_preamble_sequence(preamble_length); // Sending Edison Board ID # - 2 bits, MSB then LSB switch (edisonID) { case 0: send_low_bit(); // Send lsb bit 0 = LOW send_low_bit(); // Send msb bit 1 = LOW break; case 1: send_high_bit(); // Send lsb bit 0 = HIGH send_low_bit(); // Send msb bit 1 = LOW break; case 2: send_low_bit(); // Send lsb bit 0 = LOW send_high_bit(); // Send msb bit 1 = break; case 3: send_high_bit(); // Send lsb bit 0 = HIGH send_high_bit(); // Send msb bit 1 = HIGH break; default: send_low_bit(); // Send lsb bit 0 = LOW send_low_bit(); // Send msb bit 1 = LOW } // Sending Edison IR Emitter ID # - 2 bits, MSB then LSB // pwm1 = 00 = short-long/short-long = 5-20/5-20 // pwm2 = 01 = short-long/long-short = 5-20/20-5 // pwm3 = 10 = long-short/short-long = 20-5/5-20 // pwm4 = 11 = long-short/long-short = 20-5/20-5 // First Bit pwm_configure(pwm1, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); // 0 pwm_configure(pwm2, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); // 0 pwm_configure(pwm3, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); // 1 pwm_configure(pwm4, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); // 1 pwm_enable(pwm1); pwm_enable(pwm2); pwm_enable(pwm3); pwm_enable(pwm4); mcu_delay(MESSAGE_SLEEP); // Second Bit pwm_configure(pwm1, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); // 0 pwm_configure(pwm2, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); // 1 pwm_configure(pwm3, MESSAGE_DUTY_LOW, MESSAGE_PERIOD); // 0 pwm_configure(pwm4, MESSAGE_DUTY_HIGH, MESSAGE_PERIOD); // 1 pwm_enable(pwm1); pwm_enable(pwm2); pwm_enable(pwm3); pwm_enable(pwm4); mcu_delay(MESSAGE_SLEEP); } }