adc.c 3.2 KB

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