adc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-03-03 lgnq First version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include "mb9bf506r.h"
  13. #include "adc.h"
  14. #include "led.h"
  15. #include "lcd.h"
  16. #ifdef RT_USING_RTGUI
  17. #include <rtgui/event.h>
  18. #include <rtgui/rtgui_server.h>
  19. #include <rtgui/rtgui_system.h>
  20. #endif
  21. static struct rt_device adc;
  22. static rt_err_t rt_adc_init(rt_device_t dev)
  23. {
  24. RT_ASSERT(dev != RT_NULL);
  25. if(!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  26. {
  27. /* I/O setting AN08 - P18 */
  28. FM3_GPIO->ADE |= 0x100;
  29. FM3_GPIO->PFR1 = 0x100;
  30. /* A/DC setting */
  31. FM3_ADC0->SCIS1 = 0x01;
  32. FM3_ADC0->ADSS1 = 0x00; /* sampling timming ADST0 */
  33. FM3_ADC0->ADST1 = 0x43;
  34. FM3_ADC0->ADCT = 0x02;
  35. FM3_ADC0->SCCR = 0x10; /* FIFO clear,single mode */
  36. FM3_ADC0->CMPCR = 0x00; /* disable comparator */
  37. /* starting A/DC */
  38. FM3_ADC0->SCCR |= 0x01; /* A/DC start */
  39. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  40. }
  41. return RT_EOK;
  42. }
  43. static rt_err_t rt_adc_control(rt_device_t dev, int cmd, void *args)
  44. {
  45. RT_ASSERT(dev != RT_NULL);
  46. switch (cmd)
  47. {
  48. case RT_DEVICE_CTRL_ADC_START:
  49. FM3_ADC0->SCCR |= 0x1;
  50. break;
  51. case RT_DEVICE_CTRL_ADC_RESULT:
  52. while(FM3_ADC0->ADSR & 0x1)
  53. ;
  54. *((rt_uint16_t*)args) = FM3_ADC0->SCFD;
  55. *((rt_uint16_t*)args) = *((rt_uint16_t*)args) >> 6;
  56. *((rt_uint16_t*)args) = (*((rt_uint16_t*)args)*3300)/1024;
  57. break;
  58. }
  59. return RT_EOK;
  60. }
  61. extern struct rt_messagequeue mq;
  62. extern rt_thread_t info_tid;
  63. rt_uint16_t adc_value;
  64. static void adc_thread_entry(void *parameter)
  65. {
  66. rt_device_t device;
  67. #ifdef RT_USING_RTGUI
  68. struct rtgui_event_command ecmd;
  69. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  70. ecmd.type = RTGUI_CMD_USER_INT;
  71. ecmd.command_id = ADC_UPDATE;
  72. #else
  73. struct lcd_msg msg;
  74. #endif
  75. device = rt_device_find("adc");
  76. while(1)
  77. {
  78. rt_device_control(device, RT_DEVICE_CTRL_ADC_START, RT_NULL);
  79. rt_device_control(device, RT_DEVICE_CTRL_ADC_RESULT, &adc_value);
  80. pwm_update(adc_value/3);
  81. #ifdef RT_USING_RTGUI
  82. rtgui_thread_send(info_tid, &ecmd.parent, sizeof(ecmd));
  83. #else
  84. msg.type = ADC_MSG;
  85. msg.adc_value = adc_value;
  86. rt_mq_send(&mq, &msg, sizeof(msg));
  87. #endif
  88. rt_thread_delay(20);
  89. }
  90. }
  91. static rt_thread_t adc_thread;
  92. void rt_hw_adc_init(void)
  93. {
  94. adc.type = RT_Device_Class_Char;
  95. adc.rx_indicate = RT_NULL;
  96. adc.tx_complete = RT_NULL;
  97. adc.init = rt_adc_init;
  98. adc.open = RT_NULL;
  99. adc.close = RT_NULL;
  100. adc.read = RT_NULL;
  101. adc.write = RT_NULL;
  102. adc.control = rt_adc_control;
  103. adc.user_data = RT_NULL;
  104. adc_thread = rt_thread_create("adc", adc_thread_entry, RT_NULL, 384, 26, 5);
  105. if(adc_thread != RT_NULL)
  106. rt_thread_startup(adc_thread);
  107. /* register a character device */
  108. rt_device_register(&adc, "adc", RT_DEVICE_FLAG_RDWR);
  109. }