main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * DHT11/22 to I2C converter
  3. *
  4. * Copyright (c) 2018 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "main.h"
  21. #include "util.h"
  22. #include "i2c_slave.h"
  23. #include "dht.h"
  24. #include <avr/wdt.h>
  25. #include <avr/sleep.h>
  26. #include <util/crc16.h>
  27. #include <string.h>
  28. #define I2C_ADDR 0x76
  29. static struct host_interface_state {
  30. uint8_t next_data[DHT_DATA_LEN + 2u];
  31. uint8_t tx_data[DHT_DATA_LEN + 2u];
  32. uint8_t tx_index;
  33. } hostintf;
  34. static void host_comm_start(void)
  35. {
  36. /* Copy the message. */
  37. memcpy(hostintf.tx_data, hostintf.next_data, sizeof(hostintf.tx_data));
  38. /* Reset transmit byte index. */
  39. hostintf.tx_index = 0u;
  40. }
  41. static uint8_t host_transmit_callback(bool start)
  42. {
  43. uint8_t data;
  44. if (start)
  45. host_comm_start();
  46. data = hostintf.tx_data[hostintf.tx_index];
  47. if (++hostintf.tx_index >= sizeof(hostintf.tx_data))
  48. hostintf.tx_index = 0u;
  49. return data;
  50. }
  51. static bool host_receive_callback(bool start, uint8_t byte)
  52. {
  53. bool continue_transfer = false;
  54. host_comm_start();
  55. return continue_transfer;
  56. }
  57. static const struct i2c_slave_ops __flash host_interface_slave_ops = {
  58. .transmit = host_transmit_callback,
  59. .receive = host_receive_callback,
  60. };
  61. static void host_interface_init(void)
  62. {
  63. memset(hostintf.next_data, 0u, sizeof(hostintf.next_data));
  64. memset(hostintf.tx_data, 0u, sizeof(hostintf.tx_data));
  65. hostintf.tx_index = 0u;
  66. i2cs_init();
  67. i2cs_add_slave(I2C_ADDR, &host_interface_slave_ops);
  68. }
  69. static void dht_rx_callback(const uint8_t *data)
  70. {
  71. uint8_t i, crc;
  72. /* Increment message sequence counter. */
  73. hostintf.next_data[0]++;
  74. /* Copy the payload data. */
  75. memcpy(hostintf.next_data + 1u, data, DHT_DATA_LEN);
  76. /* Calculate CRC8 */
  77. crc = 0u;
  78. for (i = 0u; i < sizeof(hostintf.next_data) - 1u; i++)
  79. crc = _crc8_ccitt_update(crc, hostintf.next_data[i]);
  80. hostintf.next_data[sizeof(hostintf.next_data) - 1u] = crc;
  81. }
  82. static void basic_port_init(void)
  83. {
  84. PORTB = 0u;
  85. DDRB = (0u << DDB0) | /* Input: ISP-MOSI / I2C-SDA */
  86. (0u << DDB1) | /* Input: ISP-MISO */
  87. (0u << DDB2) | /* Input: ISP-SCK / I2C-SCL */
  88. (1u << DDB3) | /* Output: Debug pin */
  89. (0u << DDB4) | /* Input: DHT signal */
  90. (0u << DDB5); /* Reset */
  91. PORTB = (0u << PB0) | /* Input, no pullup: ISP-MOSI / I2C-SDA */
  92. (1u << PB1) | /* Input, pullup: ISP-MISO */
  93. (0u << PB2) | /* Input, no pullup: ISP-SCK / I2C-SCL */
  94. (0u << PB3) | /* Output, low level: Debug pin */
  95. (1u << PB4) | /* Input, pullup: DHT signal */
  96. (0u << PB5); /* Reset */
  97. }
  98. void early_init(void) __attribute__((naked, section(".init3"), used));
  99. void early_init(void)
  100. {
  101. MCUSR = 0;
  102. wdt_enable(WDTO_500MS);
  103. }
  104. int main(void) _mainfunc;
  105. int main(void)
  106. {
  107. basic_port_init();
  108. dht_init();
  109. host_interface_init();
  110. dht_start(dht_rx_callback);
  111. wdt_enable(WDTO_250MS);
  112. set_sleep_mode(SLEEP_MODE_IDLE);
  113. irq_enable();
  114. while (1) {
  115. sleep_mode();
  116. wdt_reset();
  117. }
  118. }