2020-07-01 23:44:57 +08:00
|
|
|
/*
|
2021-03-21 22:23:24 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2020-07-01 23:44:57 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-06-27 AHTYDHD the first version
|
|
|
|
*/
|
2020-06-24 19:54:42 +08:00
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
#include "drv_adc.h"
|
2020-06-24 19:54:42 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2020-07-01 23:44:57 +08:00
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "driverlib/adc.h"
|
|
|
|
#include "driverlib/sysctl.h"
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_ADC
|
|
|
|
#include "adc_config.h"
|
|
|
|
#include "tm4c123_config.h"
|
2020-07-01 23:44:57 +08:00
|
|
|
#include <string.h>
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
#define LOG_TAG "drv.adc"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
static struct tm4c123_adc_config adc_config[] =
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_ADC0
|
|
|
|
ADC0_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_ADC1
|
|
|
|
ADC1_CONFIG,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tm4c123_adc
|
|
|
|
{
|
|
|
|
struct tm4c123_adc_config *config;
|
|
|
|
struct rt_adc_device adc_device;
|
|
|
|
};
|
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
static struct tm4c123_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
|
|
|
|
{
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2020-07-01 23:44:57 +08:00
|
|
|
struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
2020-07-01 23:44:57 +08:00
|
|
|
ADCSequenceEnable(config->adcbase, config->sequence);
|
|
|
|
ADCIntClear(config->adcbase, config->sequence);
|
2020-06-24 19:54:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-01 23:44:57 +08:00
|
|
|
ADCSequenceDisable(config->adcbase, config->sequence);
|
2020-06-24 19:54:42 +08:00
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
|
|
|
{
|
2020-07-01 23:44:57 +08:00
|
|
|
|
2020-06-24 19:54:42 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(value != RT_NULL);
|
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
uint32_t pui32ADC0Value[4] = {0};
|
|
|
|
struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
|
|
|
|
|
|
|
|
/* Trigger the ADC conversion. */
|
|
|
|
ADCProcessorTrigger(config->adcbase, config->sequence);
|
2020-06-24 19:54:42 +08:00
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
while (!ADCIntStatus(config->adcbase, config->sequence, false))
|
2020-06-24 19:54:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
/* Clear the ADC interrupt flag. */
|
|
|
|
ADCIntClear(config->adcbase, config->sequence);
|
2020-06-24 19:54:42 +08:00
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
/* Read ADC Value. */
|
|
|
|
ADCSequenceDataGet(config->adcbase, config->sequence, pui32ADC0Value);
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
/* get ADC value */
|
|
|
|
*value = (rt_uint32_t)pui32ADC0Value[channel];
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_adc_ops tm4c123_adc_ops =
|
|
|
|
{
|
|
|
|
.enabled = tm4c123_adc_enabled,
|
|
|
|
.convert = tm4c123_get_adc_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static rt_err_t tm4c123_hw_adc_init(struct tm4c123_adc *device)
|
|
|
|
{
|
2020-07-01 23:44:57 +08:00
|
|
|
|
2020-06-24 19:54:42 +08:00
|
|
|
uint32_t adcbase = device->config->adcbase;
|
|
|
|
uint32_t sequencenum = device->config->sequence;
|
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
ADCSequenceConfigure(adcbase, sequencenum,
|
|
|
|
device->config->trigermode, 0);
|
|
|
|
|
|
|
|
ADCSequenceStepConfigure(adcbase, sequencenum, 0, ADC_CTL_CH7);
|
|
|
|
ADCSequenceStepConfigure(adcbase, sequencenum, 1, ADC_CTL_CH6 | ADC_CTL_IE);
|
|
|
|
ADCSequenceStepConfigure(adcbase, sequencenum, 2, ADC_CTL_CH5);
|
|
|
|
/*Tell the ADC logic
|
|
|
|
that this is the last conversion on sequence 3 (ADC_CTL_END). */
|
|
|
|
ADCSequenceStepConfigure(adcbase, sequencenum, 3, ADC_CTL_CH4 | ADC_CTL_IE |
|
|
|
|
ADC_CTL_END);
|
|
|
|
return RT_EOK;
|
2020-06-24 19:54:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int tm4c123_adc_init(void)
|
|
|
|
{
|
2020-07-01 23:44:57 +08:00
|
|
|
int i = 0;
|
|
|
|
rt_size_t obj_num = sizeof(adc_obj) / sizeof(struct tm4c123_adc);
|
|
|
|
rt_err_t result = RT_EOK;
|
2020-06-24 19:54:42 +08:00
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
adc_hw_config();
|
2020-06-24 19:54:42 +08:00
|
|
|
|
|
|
|
for (i = 0; i < obj_num; i++)
|
|
|
|
{
|
|
|
|
/* ADC init */
|
|
|
|
adc_obj[i].config = &adc_config[i];
|
|
|
|
|
2020-07-01 23:44:57 +08:00
|
|
|
|
|
|
|
if (tm4c123_hw_adc_init(&adc_obj[i]) != RT_EOK)
|
2020-06-24 19:54:42 +08:00
|
|
|
{
|
|
|
|
LOG_E("%s init failed", adc_obj[i].config->name);
|
|
|
|
result = -RT_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_D("%s init success", adc_obj[i].config->name);
|
2020-07-01 23:44:57 +08:00
|
|
|
|
2020-06-24 19:54:42 +08:00
|
|
|
/* register adc device */
|
2020-07-01 23:44:57 +08:00
|
|
|
if (rt_hw_adc_register(&adc_obj[i].adc_device, adc_obj[i].config->name, &tm4c123_adc_ops, &adc_config[i]) == RT_EOK)
|
2020-06-24 19:54:42 +08:00
|
|
|
{
|
|
|
|
LOG_D("%s register success", adc_obj[i].config->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("%s register failed", adc_obj[i].config->name);
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
INIT_APP_EXPORT(tm4c123_adc_init);
|
|
|
|
|
|
|
|
#endif /*RT_UING_ADC*/
|
2020-07-01 23:44:57 +08:00
|
|
|
|
2021-03-21 22:23:24 +08:00
|
|
|
/************************** end of file ******************/
|