From 1bf81b80643fa21f5ba5982ebd9cf5a9bdcdc6c4 Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Sun, 14 Jun 2015 18:07:21 +0200 Subject: [PATCH 1/4] feat(lpc43xx): Basic support for DAC --- include/libopencm3/lpc43xx/dac.h | 84 ++++++++++++++++++++++++++++++++ lib/lpc43xx/dac.c | 54 ++++++++++++++++++++ lib/lpc43xx/m4/Makefile | 2 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 include/libopencm3/lpc43xx/dac.h create mode 100644 lib/lpc43xx/dac.c diff --git a/include/libopencm3/lpc43xx/dac.h b/include/libopencm3/lpc43xx/dac.h new file mode 100644 index 0000000000..2f04aff806 --- /dev/null +++ b/include/libopencm3/lpc43xx/dac.h @@ -0,0 +1,84 @@ +/** @defgroup dac_defines DAC Defines + +@brief Defined Constants and Types for the LPC43xx DAC + +@ingroup LPC43xx_defines + +@version 1.0.0 + +@author @htmlonly © @endhtmlonly 2015 + +@date 09 June 2015 + +LGPL License Terms @ref lgpl_license + */ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2015 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef LPC43XX_DAC_H +#define LPC43XX_DAC_H + +/**@{*/ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* --- DAC registers ------------------------------------------------------- */ + +/* DAC register. Holds the conversion data */ +#define DAC_CR MMIO32(DAC_BASE + 0x000) + +/* DAC control register */ +#define DAC_CTRL MMIO32(DAC_BASE + 0x004) + +/* DAC counter value register */ +#define DAC_CNTVAL MMIO32(DAC_BASE + 0x008) + +/* --- DAC_CR values -------------------------------------------------- */ + +#define DAC_CR_BIAS (1 << 16) /* Settling time */ + +/* --- DAC_CTRL values -------------------------------------------------- */ + +#define DAC_CTRL_INT_DMA_REQ (1 << 0) /* DMA request */ +#define DAC_CTRL_DBLBUF_ENA (1 << 1) /* DMA double-buffering */ +#define DAC_CTRL_CNT_ENA (1 << 2) /* DMA time-out */ +#define DAC_CTRL_DMA_ENA (1 << 3) /* Combined DAC and DMA enable */ + +/* --- DAC function prototypes --------------------------------------------- */ + +BEGIN_DECLS + +void dac_init(bool fast); + +void dac_set(uint16_t v); + +END_DECLS + +/**@}*/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/lpc43xx/dac.c b/lib/lpc43xx/dac.c new file mode 100644 index 0000000000..5780ac7f9b --- /dev/null +++ b/lib/lpc43xx/dac.c @@ -0,0 +1,54 @@ +/** @defgroup dac_file DAC + +@ingroup LPC43xx + +@brief libopencm3 LPC43xx DAC + +@version 1.0.0 + +@author @htmlonly © @endhtmlonly 2015 + +LGPL License Terms @ref lgpl_license +*/ + +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2015 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/**@{*/ + +#include +#include + +static bool dac_fast = false; + +void dac_init(bool fast) +{ + CCU1_CLK_APB3_DAC_CFG |= 1; /* Enable DAC Clock */ + DAC_CTRL = DAC_CTRL_DMA_ENA; + dac_fast = fast; +} + +void dac_set(uint16_t v) +{ + DAC_CR = (v & 0x3FF) << 6 | (dac_fast ? DAC_CR_BIAS : 0); +} + + +/**@}*/ + diff --git a/lib/lpc43xx/m4/Makefile b/lib/lpc43xx/m4/Makefile index d7abe956f1..4e0d1c5e07 100644 --- a/lib/lpc43xx/m4/Makefile +++ b/lib/lpc43xx/m4/Makefile @@ -39,7 +39,7 @@ CFLAGS = -O2 -g3 \ ARFLAGS = rcs # LPC43xx common files for M4 / M0 -OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o +OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o dac.o #LPC43xx M4 specific file + Generic LPC43xx M4/M0 files OBJS = $(OBJ_LPC43XX) ipc.o From eb7c294876077c603584ab6397c9fbdc2a39a072 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 14 Jun 2015 23:58:32 +0200 Subject: [PATCH 2/4] Add adc stuff --- include/libopencm3/lpc43xx/adc.h | 37 +++++++++++++++++++++++ include/libopencm3/lpc43xx/scu.h | 9 ++++++ lib/lpc43xx/adc.c | 51 ++++++++++++++++++++++++++++++++ lib/lpc43xx/m4/Makefile | 2 +- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 lib/lpc43xx/adc.c diff --git a/include/libopencm3/lpc43xx/adc.h b/include/libopencm3/lpc43xx/adc.h index 502c0fb27e..d22d596944 100644 --- a/include/libopencm3/lpc43xx/adc.h +++ b/include/libopencm3/lpc43xx/adc.h @@ -112,6 +112,43 @@ extern "C" { #define ADC0_STAT ADC_STAT(ADC0) #define ADC1_STAT ADC_STAT(ADC1) + +/* ADC_CR Values ----------------------------------------------------------- */ + +#define ADC_CR_CH0 (1<<0) +#define ADC_CR_CH1 (1<<1) +#define ADC_CR_CH2 (1<<2) +#define ADC_CR_CH3 (1<<3) +#define ADC_CR_CH4 (1<<4) +#define ADC_CR_CH5 (1<<5) +#define ADC_CR_CH6 (1<<6) +#define ADC_CR_CH7 (1<<7) + +#define ADC_CR_CLKDIV(x) ((x&0xff)<<8) +#define ADC_CR_BURST (1<<16) + +#define ADC_CR_10BITS (0<<17) +#define ADC_CR_9BITS (1<<17) +#define ADC_CR_8BITS (2<<17) +#define ADC_CR_7BITS (3<<17) +#define ADC_CR_6BITS (4<<17) +#define ADC_CR_5BITS (5<<17) +#define ADC_CR_4BITS (6<<17) +#define ADC_CR_3BITS (7<<17) + +#define ADC_CR_POWER (1<<21) + +#define ADC_CR_START (1<<24) +/* missing: add other start modes */ + +/* ADC_GDR and ADC_DR Values */ +#define ADC_DR_VREF(x) ((x>>6)&0b1111111111) +#define ADC_DR_CHN(x) ((x>>24)&0b111) +#define ADC_DR_OVERRUN(x) (((x&(1<<30))!=0)) +#define ADC_DR_DONE(x) (((x&(1<<31))!=0)) + +uint16_t adc_get_single(uint32_t adc, uint32_t flags); + /**@}*/ #ifdef __cplusplus diff --git a/include/libopencm3/lpc43xx/scu.h b/include/libopencm3/lpc43xx/scu.h index e057a38eb4..0ab580f91c 100644 --- a/include/libopencm3/lpc43xx/scu.h +++ b/include/libopencm3/lpc43xx/scu.h @@ -773,6 +773,15 @@ typedef enum { SCU_CONF_EZI_EN_IN_BUFFER | \ SCU_CONF_ZIF_DIS_IN_GLITCH_FILT) +/* definitions for ENAIO */ + +#define SCU_ENAIO_ADCx_0 (1<<0) +#define SCU_ENAIO_ADCx_1 (1<<1) +#define SCU_ENAIO_ADCx_2 (1<<2) +#define SCU_ENAIO_ADCx_3 (1<<3) +#define SCU_ENAIO_ADCx_4 (1<<4) +#define SCU_ENAIO_ADCx_5 (1<<5) +#define SCU_ENAIO_ADCx_6 (1<<6) BEGIN_DECLS void scu_pinmux(scu_grp_pin_t group_pin, uint32_t scu_conf); diff --git a/lib/lpc43xx/adc.c b/lib/lpc43xx/adc.c new file mode 100644 index 0000000000..08637a78f6 --- /dev/null +++ b/lib/lpc43xx/adc.c @@ -0,0 +1,51 @@ +/** @defgroup adc_file ADC + +@ingroup LPC43xx + +@brief libopencm3 LPC43xx ADC + +@version 1.0.0 + +@author @htmlonly © @endhtmlonly 2015 + +LGPL License Terms @ref lgpl_license +*/ + +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2015 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/**@{*/ + +#include +#include + +uint16_t adc_get_single(uint32_t adc, uint32_t flags) +{ + uint32_t result; + ADC_CR(adc)=flags | ADC_CR_CLKDIV((uint8_t)(208/4.5))|ADC_CR_10BITS|ADC_CR_POWER|ADC_CR_START; + + do { + result=ADC_GDR(adc); + } while( (!ADC_DR_DONE(result)) ); + + return ADC_DR_VREF(result); +}; + +/**@}*/ + diff --git a/lib/lpc43xx/m4/Makefile b/lib/lpc43xx/m4/Makefile index 4e0d1c5e07..48a204e3e2 100644 --- a/lib/lpc43xx/m4/Makefile +++ b/lib/lpc43xx/m4/Makefile @@ -39,7 +39,7 @@ CFLAGS = -O2 -g3 \ ARFLAGS = rcs # LPC43xx common files for M4 / M0 -OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o dac.o +OBJ_LPC43XX = gpio.o scu.o i2c.o ssp.o uart.o timer.o dac.o adc.o #LPC43xx M4 specific file + Generic LPC43xx M4/M0 files OBJS = $(OBJ_LPC43XX) ipc.o From b39ac295ebd1d2562ef51dc43354d012b761dd25 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 28 Jun 2015 22:45:52 +0200 Subject: [PATCH 3/4] fix irqhandlers include --- include/libopencmsis/dispatch/irqhandlers.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/libopencmsis/dispatch/irqhandlers.h b/include/libopencmsis/dispatch/irqhandlers.h index 65e071d79d..2a8105157c 100644 --- a/include/libopencmsis/dispatch/irqhandlers.h +++ b/include/libopencmsis/dispatch/irqhandlers.h @@ -14,8 +14,11 @@ #elif defined(EFM32GG) # include -#elif defined(LPC43XX) -# include +#elif defined(LPC43XX_M4) +# include + +#elif defined(LPC43XX_M0) +# include #else # warning"no chipset defined; user interrupts are not redirected" From 3b547eb4d715e251e1fd6b578345d684bca2d3ae Mon Sep 17 00:00:00 2001 From: Christian Nicolai Date: Tue, 2 Feb 2016 20:40:04 +0100 Subject: [PATCH 4/4] Fix DAC (short) settling time bit handling According to the LPC43xx User Manual (rev 2.1) a 0 in DAC_CR_BIAS bit leads to shorter settling times and higher power consumption. --- lib/lpc43xx/dac.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lpc43xx/dac.c b/lib/lpc43xx/dac.c index 5780ac7f9b..55b112bc6a 100644 --- a/lib/lpc43xx/dac.c +++ b/lib/lpc43xx/dac.c @@ -46,7 +46,7 @@ void dac_init(bool fast) void dac_set(uint16_t v) { - DAC_CR = (v & 0x3FF) << 6 | (dac_fast ? DAC_CR_BIAS : 0); + DAC_CR = (v & 0x3FF) << 6 | (dac_fast ? 0 : DAC_CR_BIAS); }