Skip to content

Commit

Permalink
Merge pull request RIOT-OS#12911 from benpicco/lpc2387-adc
Browse files Browse the repository at this point in the history
cpu/lpc2387: implement periph/adc
  • Loading branch information
benpicco authored Feb 10, 2020
2 parents f997f42 + 4342d81 commit 0b04f61
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions boards/mcb2388/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CPU = lpc2387
CPU_MODEL = lpc2388

# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
Expand Down
15 changes: 15 additions & 0 deletions boards/mcb2388/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ static const uart_conf_t uart_config[] = {
#define SPI_NUMOF (1)
/** @} */

/**
* @name ADC configuration
* @{
*/
static const adc_conf_t adc_config[] = {
{
.chan = 0,
.pinsel = 1,
.pinsel_msk = BIT14,
},
};

#define ADC_NUMOF (1)
/** @} */

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions boards/msba2/Makefile.features
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CPU = lpc2387

# Put defined MCU peripherals here (in alphabetical order)
FEATURES_PROVIDED += periph_adc
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_spi
Expand Down
26 changes: 26 additions & 0 deletions boards/msba2/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ static const uart_conf_t uart_config[] = {
#define SPI_NUMOF (1)
/** @} */

/**
* @name ADC configuration
* @{
*/
static const adc_conf_t adc_config[] = {
{ /* P0.23 */
.chan = 0,
.pinsel = 1,
.pinsel_msk = BIT14,
},
{ /* P0.24 */
.chan = 1,
.pinsel = 1,
.pinsel_msk = BIT16,
},
{ /* P0.25 */
.chan = 2,
.pinsel = 1,
.pinsel_msk = BIT18,
},
};

#define ADC_NUMOF ARRAY_SIZE(adc_config)
/** @} */


#ifdef __cplusplus
}
#endif
Expand Down
29 changes: 29 additions & 0 deletions cpu/lpc2387/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ typedef enum {
*/
#define DAC_NUMOF (1U)

/**
* @brief Possible ADC resolution settings
* @{
*/
#define HAVE_ADC_RES_T
typedef enum {
ADC_RES_16BIT = 0xff, /**< not applicable */
ADC_RES_14BIT = 0xfe, /**< not applicable */
ADC_RES_12BIT = 0xfd, /**< not applicable */
ADC_RES_10BIT = 0b000, /**< ADC resolution: 10 bit */
ADC_RES_9BIT = 0b001, /**< ADC resolution: 9 bit */
ADC_RES_8BIT = 0b010, /**< ADC resolution: 8 bit */
ADC_RES_7BIT = 0b011, /**< ADC resolution: 7 bit */
ADC_RES_6BIT = 0b100, /**< ADC resolution: 6 bit */
ADC_RES_5BIT = 0b101, /**< ADC resolution: 5 bit */
ADC_RES_4BIT = 0b110, /**< ADC resolution: 4 bit */
ADC_RES_3BIT = 0b111, /**< ADC resolution: 3 bit */
} adc_res_t;
/** @} */

/**
* @brief ADC device configuration
*/
typedef struct {
uint8_t chan; /**< which ADC to use (0…7) */
uint8_t pinsel; /**< PINSEL# of the ADC pin */
uint32_t pinsel_msk; /**< PINSEL Mask for ADC pin */
} adc_conf_t;

/* @} */
#ifdef __cplusplus
}
Expand Down
81 changes: 81 additions & 0 deletions cpu/lpc2387/periph/adc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 Beuth Hochschule für Technik Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @ingroup drivers_periph
* @{
*
* @file
* @brief Low-level ADC driver implementation for lpc23xx family
*
* @author Benjamin Valentin <[email protected]>
*
* @}
*/

#include "cpu.h"
#include "kernel_defines.h"
#include "periph/adc.h"
#include "periph_conf.h"

int adc_init(adc_t line)
{
/* check if the line is valid */
if (line >= ADC_NUMOF) {
return -1;
}

/* enable clock/power for ADC */
PCONP |= BIT12;

/* set ADC PCLK to CCLK/8 */
PCLKSEL0 |= BIT25 + BIT25;

/* configure AF for ADC pin */
*(&PINSEL0 + adc_config[line].pinsel) |= adc_config[line].pinsel_msk;

/* power down ADC */
AD0CR = 0;

return 0;
}

int32_t adc_sample(adc_t line, adc_res_t res)
{
uint32_t val;

/* check if the line is valid */
if (line >= ADC_NUMOF) {
return -1;
}

/* check if resolution is applicable */
if (res > 0xf0) {
return -1;
}

/* Enable & configure ADC.
* burst mode is used even though we only do one conversion here
* in order to being able to configure the resolution.
*/
AD0CR = (1 << adc_config[line].chan) /* select channel */
| (1 << 8) /* clocked at 4.5 MHz */
| (1 << 16) /* enable burst mode */
| (res << 17) /* select resolution */
| (1 << 21) /* enable ADC */
| (1 << 24); /* start conversion */

do {
val = *(&AD0DR0 + adc_config[line].chan);
} while (!(val & BIT31));

/* power down ADC */
AD0CR = 0;

return val & 0xFFFF;
}

0 comments on commit 0b04f61

Please sign in to comment.