test_dht11.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. enum
  23. {
  24. EV_USER_DHT11_TEST_DONE = USER_EVENT_ID_START + 1,
  25. };
  26. typedef struct
  27. {
  28. uint32_t LastValue;
  29. uint8_t Data[6];
  30. uint8_t Pos;
  31. uint8_t BitPos;
  32. uint8_t BytePos;
  33. }DHT11_CtrlStruct;
  34. static int32_t DHT11_ReadBit(void *pData, void *pParam)
  35. {
  36. OPQueue_CmdStruct *Cmd = (OPQueue_CmdStruct *)pData;
  37. DHT11_CtrlStruct *DHT11 = (DHT11_CtrlStruct *)pParam;
  38. uint32_t diff;
  39. switch(DHT11->Pos)
  40. {
  41. case 0:
  42. // DBG("%u", Cmd->uParam.MaxCnt);
  43. break;
  44. case 1:
  45. DHT11->LastValue = Cmd->uParam.MaxCnt;
  46. DHT11->BitPos = 7;
  47. DHT11->BytePos = 0;
  48. DHT11->Data[DHT11->BytePos] = 0;
  49. break;
  50. default:
  51. diff = (Cmd->uParam.MaxCnt - DHT11->LastValue);
  52. DHT11->LastValue = Cmd->uParam.MaxCnt;
  53. // DBG("%u,%u,%u,%u,%u", DHT11->BytePos, DHT11->BitPos, diff, Cmd->uParam.MaxCnt,Cmd->PinOrDelay);
  54. if (diff > (100 * SYS_TIMER_1US))
  55. {
  56. DHT11->Data[DHT11->BytePos] |= (1 << DHT11->BitPos);
  57. }
  58. if (DHT11->BitPos)
  59. {
  60. DHT11->BitPos--;
  61. }
  62. else
  63. {
  64. DHT11->BitPos = 7;
  65. DHT11->BytePos++;
  66. DHT11->Data[DHT11->BytePos] = 0;
  67. }
  68. }
  69. DHT11->Pos++;
  70. }
  71. static int32_t prvDHT11_TestCB(void *pData, void *pParam)
  72. {
  73. Task_SendEvent(pParam, EV_USER_DHT11_TEST_DONE, 0, 0, 0);
  74. }
  75. void DHT11_TestOnce(uint8_t Pin)
  76. {
  77. uint8_t HWTimerID = HW_TIMER0;
  78. uint32_t i;
  79. OPQueue_CmdStruct OPCmd;
  80. HANDLE TaskHandle = Task_GetCurrent();
  81. HWTimer_InitOperationQueue(HWTimerID, 100, 1, prvDHT11_TestCB, TaskHandle);
  82. OPCmd.Operation = OP_QUEUE_CMD_SET_GPIO_DIR_IN;
  83. OPCmd.PinOrDelay = Pin;
  84. OPCmd.uArg.IOArg.Level = 0;
  85. OPCmd.uArg.IOArg.PullMode = OP_QUEUE_CMD_IO_PULL_UP;
  86. HWTimer_AddOperation(HWTimerID, &OPCmd);
  87. OPCmd.Operation = OP_QUEUE_CMD_ONE_TIME_DELAY;
  88. OPCmd.PinOrDelay = 0;
  89. OPCmd.uArg.Time = 60000;
  90. HWTimer_AddOperation(HWTimerID, &OPCmd);
  91. OPCmd.Operation = OP_QUEUE_CMD_SET_GPIO_DIR_OUT;
  92. OPCmd.PinOrDelay = Pin;
  93. OPCmd.uArg.IOArg.Level = 0;
  94. OPCmd.uArg.IOArg.PullMode = OP_QUEUE_CMD_IO_PULL_NONE;
  95. HWTimer_AddOperation(HWTimerID, &OPCmd);
  96. OPCmd.Operation = OP_QUEUE_CMD_ONE_TIME_DELAY;
  97. OPCmd.PinOrDelay = 0;
  98. OPCmd.uArg.Time = 18000;
  99. HWTimer_AddOperation(HWTimerID, &OPCmd);
  100. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE_SET;
  101. OPCmd.PinOrDelay = Pin;
  102. OPCmd.uArg.ExitArg.ExtiMode = OP_QUEUE_CMD_IO_EXTI_DOWN;
  103. OPCmd.uArg.ExitArg.PullMode = OP_QUEUE_CMD_IO_PULL_UP;
  104. OPCmd.uParam.MaxCnt = 100 * SYS_TIMER_1MS;
  105. HWTimer_AddOperation(HWTimerID, &OPCmd);
  106. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE;
  107. OPCmd.PinOrDelay = 0xff;
  108. OPCmd.uArg.IOArg.Level = 0xff;
  109. OPCmd.uParam.MaxCnt = 0;
  110. for(i = 0; i < 42; i++) //40bit data+2bit start+1bit end
  111. {
  112. HWTimer_AddOperation(HWTimerID, &OPCmd);
  113. }
  114. OPCmd.Operation = OP_QUEUE_CMD_CAPTURE_END;
  115. OPCmd.PinOrDelay = Pin;
  116. HWTimer_AddOperation(HWTimerID, &OPCmd);
  117. OPCmd.Operation = OP_QUEUE_CMD_SET_GPIO_DIR_IN;
  118. OPCmd.PinOrDelay = Pin;
  119. OPCmd.uArg.IOArg.Level = 0;
  120. OPCmd.uArg.IOArg.PullMode = OP_QUEUE_CMD_IO_PULL_UP;
  121. HWTimer_AddOperation(HWTimerID, &OPCmd);
  122. HWTimer_StartOperationQueue(HWTimerID);
  123. }
  124. void DHT11_TestResult(void)
  125. {
  126. uint8_t HWTimerID = HW_TIMER0;
  127. DHT11_CtrlStruct DHT11;
  128. DHT11.Pos = 0;
  129. HWTimer_GetOperationQueueCaptureResult(HWTimerID, DHT11_ReadBit, &DHT11);
  130. DHT11.Data[5] = SumCheck(DHT11.Data, 4);
  131. if (DHT11.Data[4] == DHT11.Data[5])
  132. {
  133. DBG("湿度 %d.%d, 温度 %d.%d", DHT11.Data[0], DHT11.Data[1], DHT11.Data[2], DHT11.Data[3]);
  134. }
  135. else
  136. {
  137. DBG("check error %x,%x", DHT11.Data[4], DHT11.Data[5]);
  138. DBG_HexPrintf(DHT11.Data, 4);
  139. }
  140. HWTimer_Stop(HWTimerID);
  141. }
  142. static void prvDHT11_Test(void *p)
  143. {
  144. OS_EVENT Event;
  145. while(1)
  146. {
  147. DBG("test start");
  148. DHT11_TestOnce(GPIOD_06);
  149. Task_GetEvent(Task_GetCurrent(), EV_USER_DHT11_TEST_DONE, &Event, NULL, 200 * SYS_TIMER_1MS);
  150. DHT11_TestResult();
  151. Task_DelayMS(1000);
  152. }
  153. }
  154. void DHT11_TestInit(void)
  155. {
  156. Task_Create(prvDHT11_Test, NULL, 1024, SERVICE_TASK_PRO, "dht11 task");
  157. }
  158. //INIT_TASK_EXPORT(DHT11_TestInit, "3");