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