adc.c 3.1 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-03-03     lgnq
 */
mysterywolf's avatar
mysterywolf 已提交
10

M
Ming, Bai 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <rtthread.h>
#include <rthw.h>
#include "mb9bf506r.h"
#include "adc.h"
#include "led.h"
#include "lcd.h"

#ifdef RT_USING_RTGUI
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>
#endif

static struct rt_device adc;

static rt_err_t rt_adc_init(rt_device_t dev)
{
    RT_ASSERT(dev != RT_NULL);

    if(!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
    {
mysterywolf's avatar
mysterywolf 已提交
32
        /* I/O setting AN08 - P18 */
M
Ming, Bai 已提交
33
        FM3_GPIO->ADE |= 0x100;
mysterywolf's avatar
mysterywolf 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
        FM3_GPIO->PFR1 = 0x100;

        /* A/DC setting */
        FM3_ADC0->SCIS1 = 0x01;
        FM3_ADC0->ADSS1 = 0x00;             /* sampling timming ADST0 */
        FM3_ADC0->ADST1 = 0x43;
        FM3_ADC0->ADCT  = 0x02;
        FM3_ADC0->SCCR  = 0x10;             /* FIFO clear,single mode */
        FM3_ADC0->CMPCR = 0x00;             /* disable comparator */

        /* starting A/DC */
        FM3_ADC0->SCCR |= 0x01;             /* A/DC start */

M
Ming, Bai 已提交
47
        dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
mysterywolf's avatar
mysterywolf 已提交
48 49
    }
    return RT_EOK;
M
Ming, Bai 已提交
50 51
}

B
bernard 已提交
52
static rt_err_t rt_adc_control(rt_device_t dev, int cmd, void *args)
M
Ming, Bai 已提交
53
{
mysterywolf's avatar
mysterywolf 已提交
54
    RT_ASSERT(dev != RT_NULL);
M
Ming, Bai 已提交
55

mysterywolf's avatar
mysterywolf 已提交
56 57 58
    switch (cmd)
    {
        case RT_DEVICE_CTRL_ADC_START:
M
Ming, Bai 已提交
59
            FM3_ADC0->SCCR |= 0x1;
mysterywolf's avatar
mysterywolf 已提交
60 61 62
        break;

        case RT_DEVICE_CTRL_ADC_RESULT:
M
Ming, Bai 已提交
63 64 65 66 67
            while(FM3_ADC0->ADSR & 0x1)
                ;
            *((rt_uint16_t*)args) = FM3_ADC0->SCFD;
            *((rt_uint16_t*)args) = *((rt_uint16_t*)args) >> 6;
            *((rt_uint16_t*)args) = (*((rt_uint16_t*)args)*3300)/1024;
mysterywolf's avatar
mysterywolf 已提交
68 69 70
        break;
    }
    return RT_EOK;
M
Ming, Bai 已提交
71 72 73 74 75 76 77 78
}

extern struct rt_messagequeue mq;
extern rt_thread_t info_tid;
rt_uint16_t adc_value;
static void adc_thread_entry(void *parameter)
{
    rt_device_t device;
mysterywolf's avatar
mysterywolf 已提交
79

M
Ming, Bai 已提交
80 81
#ifdef RT_USING_RTGUI
    struct rtgui_event_command ecmd;
mysterywolf's avatar
mysterywolf 已提交
82

M
Ming, Bai 已提交
83 84 85 86 87
    RTGUI_EVENT_COMMAND_INIT(&ecmd);
    ecmd.type = RTGUI_CMD_USER_INT;
    ecmd.command_id = ADC_UPDATE;
#else
    struct lcd_msg msg;
mysterywolf's avatar
mysterywolf 已提交
88
#endif
M
Ming, Bai 已提交
89 90 91 92 93

    device = rt_device_find("adc");

    while(1)
    {
mysterywolf's avatar
mysterywolf 已提交
94
        rt_device_control(device, RT_DEVICE_CTRL_ADC_START, RT_NULL);
M
Ming, Bai 已提交
95 96 97 98 99 100
        rt_device_control(device, RT_DEVICE_CTRL_ADC_RESULT, &adc_value);
        pwm_update(adc_value/3);
#ifdef RT_USING_RTGUI
        rtgui_thread_send(info_tid, &ecmd.parent, sizeof(ecmd));
#else
        msg.type = ADC_MSG;
mysterywolf's avatar
mysterywolf 已提交
101
        msg.adc_value = adc_value;
M
Ming, Bai 已提交
102 103 104 105 106 107 108 109 110
        rt_mq_send(&mq, &msg, sizeof(msg));
#endif
        rt_thread_delay(20);
    }
}

static rt_thread_t adc_thread;
void rt_hw_adc_init(void)
{
mysterywolf's avatar
mysterywolf 已提交
111 112 113 114 115 116 117 118 119 120
    adc.type        = RT_Device_Class_Char;
    adc.rx_indicate = RT_NULL;
    adc.tx_complete = RT_NULL;
    adc.init        = rt_adc_init;
    adc.open        = RT_NULL;
    adc.close       = RT_NULL;
    adc.read        = RT_NULL;
    adc.write       = RT_NULL;
    adc.control     = rt_adc_control;
    adc.user_data   = RT_NULL;
M
Ming, Bai 已提交
121 122

    adc_thread = rt_thread_create("adc", adc_thread_entry, RT_NULL, 384, 26, 5);
mysterywolf's avatar
mysterywolf 已提交
123
    if(adc_thread != RT_NULL)
M
Ming, Bai 已提交
124
        rt_thread_startup(adc_thread);
mysterywolf's avatar
mysterywolf 已提交
125 126 127

    /* register a character device */
    rt_device_register(&adc, "adc", RT_DEVICE_FLAG_RDWR);
M
Ming, Bai 已提交
128 129
}