diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralNames.h b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralNames.h index e56c403a12..29442b8ace 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralNames.h +++ b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralNames.h @@ -124,6 +124,16 @@ typedef enum { } ADCName; +typedef enum { +#if defined(SCU_INIT_PNSSET2_VAL) && (SCU_INIT_PNSSET2_VAL & (1 << 7)) + DAC_0_0 = (int) NU_MODNAME(DAC0_BASE + NS_OFFSET, 0, 0), + DAC_1_0 = (int) NU_MODNAME(DAC1_BASE + NS_OFFSET, 1, 0) +#else + DAC_0_0 = (int) NU_MODNAME(DAC0_BASE, 0, 0), + DAC_1_0 = (int) NU_MODNAME(DAC1_BASE, 1, 0) +#endif +} DACName; + typedef enum { #if defined(SCU_INIT_PNSSET3_VAL) && (SCU_INIT_PNSSET3_VAL & (1<<16)) diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.c b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.c index 330d14d86c..c9c3c89973 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.c +++ b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.c @@ -172,6 +172,15 @@ const PinMap PinMap_ADC[] = { {NC, NC, 0} }; +//*** DAC *** + +const PinMap PinMap_DAC[] = { + {PB_12, DAC_0_0, SYS_GPB_MFPH_PB12MFP_DAC0_OUT}, + {PB_13, DAC_1_0, SYS_GPB_MFPH_PB13MFP_DAC1_OUT}, + + {NC, NC, 0} +}; + //*** I2C *** const PinMap PinMap_I2C_SDA[] = { diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.h b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.h index 1f2eb94dee..0e6d9a30b1 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.h +++ b/targets/TARGET_NUVOTON/TARGET_M2351/PeripheralPins.h @@ -32,6 +32,10 @@ extern const PinMap PinMap_GPIO[]; extern const PinMap PinMap_ADC[]; +//*** DAC *** + +extern const PinMap PinMap_DAC[]; + //*** I2C *** extern const PinMap PinMap_I2C_SDA[]; diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/analogout_api.c b/targets/TARGET_NUVOTON/TARGET_M2351/analogout_api.c new file mode 100644 index 0000000000..f8ee5527db --- /dev/null +++ b/targets/TARGET_NUVOTON/TARGET_M2351/analogout_api.c @@ -0,0 +1,198 @@ +/* mbed Microcontroller Library + * Copyright (c) 2015-2016 Nuvoton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "analogout_api.h" + +#if DEVICE_ANALOGOUT + +#include "cmsis.h" +#include "pinmap.h" +#include "PeripheralPins.h" +#include "nu_modutil.h" + +/* Maximum DAC modules */ +#define NU_DACMOD_MAXNUM 2 +/* Maximum DAC channels per module */ +#define NU_DACCHN_MAXNUM 1 + +static uint32_t dac_modinit_mask[NU_DACMOD_MAXNUM]; + +static const struct nu_modinit_s dac_modinit_tab[] = { + {DAC_0_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL}, + {DAC_1_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL} +}; + +void analogout_init(dac_t *obj, PinName pin) +{ + obj->dac = (DACName) pinmap_peripheral(pin, PinMap_DAC); + MBED_ASSERT(obj->dac != (DACName) NC); + + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Module-level setup from here */ + + /* DAC0/DAC1 are designed to share the same RESET/clock/IRQ for group + * function. So we: + * + * 1. Go to setup flow (analogout_init()) only when none of DAC0/DAC1 + * channels are activated. + * 2. Go to windup flow (analogout_free()) only when all DAC0/DAC1 + * channels are deactivated. + */ + if ((! dac_modinit_mask[0]) && (! dac_modinit_mask[1])) { + /* Reset IP + * + * NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure. + */ + SYS_ResetModule_S(modinit->rsetidx); + + /* Select IP clock source and clock divider + * + * NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure. + */ + CLK_SetModuleClock_S(modinit->clkidx, modinit->clksrc, modinit->clkdiv); + + /* Enable IP clock + * + * NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure. + */ + CLK_EnableModuleClock_S(modinit->clkidx); + + /* The conversion settling time is 8us when 12-bit input code transition from + * lowest code (0x000) to highest code (0xFFF). */ + DAC_SetDelayTime(dac_base, 8); + + /* Configure DAT data format to left-aligned + * Effective 12-bits are aligned to left of 16-bit DAC_DAT. */ + DAC_ENABLE_LEFT_ALIGN(dac_base); + } + + /* Channel-level setup from here: */ + + /* Set the software trigger, enable DAC event trigger mode and enable D/A converter */ + DAC_Open(dac_base, chn, DAC_SOFTWARE_TRIGGER); + + /* Wire pinout */ + pinmap_pinout(pin, PinMap_DAC); + + /* Mark channel allocated */ + dac_modinit_mask[modidx] |= 1 << chn; +} + +void analogout_free(dac_t *obj) +{ + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Channel-level windup from here */ + + /* Mark channel free */ + dac_modinit_mask[modidx] &= ~(1 << modidx); + + /* Close channel */ + DAC_Close(dac_base, chn); + + /* Module-level windup from here: */ + + /* See analogout_init() for reason */ + if ((! dac_modinit_mask[0]) && (! dac_modinit_mask[1])) { + + /* Disable IP clock + * + * NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure. + */ + CLK_DisableModuleClock_S(modinit->clkidx); + } +} + +void analogout_write(dac_t *obj, float value) +{ + if (value <= 0.0f) { + analogout_write_u16(obj, 0); + } else if (value >= 1.0f) { + analogout_write_u16(obj, 0xFFFF); + } else { + analogout_write_u16(obj, (uint16_t) (value * ((float) 0xFFFF))); + } +} + +void analogout_write_u16(dac_t *obj, uint16_t value) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + DAC_WRITE_DATA(dac_base, chn, value); + + /* Clear the DAC conversion complete finish flag for safe */ + DAC_CLR_INT_FLAG(dac_base, chn); + + /* Start A/D conversion */ + DAC_START_CONV(dac_base); + + /* Wait for completed */ + while (DAC_IS_BUSY(dac_base, chn)); +} + +float analogout_read(dac_t *obj) +{ + uint32_t value = analogout_read_u16(obj); + return (float) value * (1.0f / (float) 0xFFFF); +} + +uint16_t analogout_read_u16(dac_t *obj) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + uint16_t dat12_4 = DAC_READ_DATA(dac_base, chn); + /* Just 12 bits are effective. Convert to 16 bits. + * + * dat12_4 : b11b10b9b8 b7b6b5b4 b3b2b1b0 0000 + * dat16 : b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 + */ + uint16_t dat16 = (dat12_4 & 0xFFF0) | (dat12_4 >> 12); + + return dat16; +} + +#endif diff --git a/targets/TARGET_NUVOTON/TARGET_M2351/objects.h b/targets/TARGET_NUVOTON/TARGET_M2351/objects.h index 0959ef25fa..274c107560 100644 --- a/targets/TARGET_NUVOTON/TARGET_M2351/objects.h +++ b/targets/TARGET_NUVOTON/TARGET_M2351/objects.h @@ -44,6 +44,10 @@ struct analogin_s { ADCName adc; }; +struct dac_s { + DACName dac; +}; + struct serial_s { UARTName uart; PinName pin_tx; diff --git a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralNames.h b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralNames.h index 7988f18af5..511482ea73 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralNames.h +++ b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralNames.h @@ -65,6 +65,10 @@ typedef enum { ADC_0_15 = (int) NU_MODNAME(EADC0_BASE, 0, 15) } ADCName; +typedef enum { + DAC_0_0 = (int) NU_MODNAME(DAC_BASE, 0, 0) +} DACName; + typedef enum { UART_0 = (int) NU_MODNAME(UART0_BASE, 0, 0), UART_1 = (int) NU_MODNAME(UART1_BASE, 1, 0), diff --git a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.c b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.c index e236cffb11..084e847486 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.c @@ -158,6 +158,14 @@ const PinMap PinMap_ADC[] = { {NC, NC, 0} }; +//*** DAC *** + +const PinMap PinMap_DAC[] = { + {PB_0, DAC_0_0, SYS_GPB_MFPL_PB0MFP_DAC}, + + {NC, NC, 0} +}; + //*** I2C *** const PinMap PinMap_I2C_SDA[] = { diff --git a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.h b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.h index 1f2eb94dee..0e6d9a30b1 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.h +++ b/targets/TARGET_NUVOTON/TARGET_M451/PeripheralPins.h @@ -32,6 +32,10 @@ extern const PinMap PinMap_GPIO[]; extern const PinMap PinMap_ADC[]; +//*** DAC *** + +extern const PinMap PinMap_DAC[]; + //*** I2C *** extern const PinMap PinMap_I2C_SDA[]; diff --git a/targets/TARGET_NUVOTON/TARGET_M451/analogout_api.c b/targets/TARGET_NUVOTON/TARGET_M451/analogout_api.c new file mode 100644 index 0000000000..07af71e3bc --- /dev/null +++ b/targets/TARGET_NUVOTON/TARGET_M451/analogout_api.c @@ -0,0 +1,176 @@ +/* mbed Microcontroller Library + * Copyright (c) 2015-2016 Nuvoton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "analogout_api.h" + +#if DEVICE_ANALOGOUT + +#include "cmsis.h" +#include "pinmap.h" +#include "PeripheralPins.h" +#include "nu_modutil.h" + +/* Maximum DAC modules */ +#define NU_DACMOD_MAXNUM 1 +/* Maximum DAC channels per module */ +#define NU_DACCHN_MAXNUM 1 + +static uint32_t dac_modinit_mask[NU_DACMOD_MAXNUM]; + +static const struct nu_modinit_s dac_modinit_tab[] = { + {DAC_0_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL} +}; + +void analogout_init(dac_t *obj, PinName pin) +{ + obj->dac = (DACName) pinmap_peripheral(pin, PinMap_DAC); + MBED_ASSERT(obj->dac != (DACName) NC); + + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Module-level setup from here */ + + if (! dac_modinit_mask[modidx]) { + /* Reset IP */ + SYS_ResetModule(modinit->rsetidx); + + /* Select IP clock source and clock divider */ + CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); + + /* Enable IP clock */ + CLK_EnableModuleClock(modinit->clkidx); + + /* The conversion settling time is 8us when 12-bit input code transition from + * lowest code (0x000) to highest code (0xFFF). */ + DAC_SetDelayTime(dac_base, 8); + + /* Configure DAT data format to left-aligned + * Effective 12-bits are aligned to left of 16-bit DAC_DAT. */ + DAC_ENABLE_LEFT_ALIGN(dac_base); + } + + /* Channel-level setup from here: */ + + /* Set the software trigger, enable DAC event trigger mode and enable D/A converter */ + DAC_Open(dac_base, chn, DAC_SOFTWARE_TRIGGER); + + /* Wire pinout */ + pinmap_pinout(pin, PinMap_DAC); + + /* Mark channel allocated */ + dac_modinit_mask[modidx] |= 1 << chn; +} + +void analogout_free(dac_t *obj) +{ + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Channel-level windup from here */ + + /* Mark channel free */ + dac_modinit_mask[modidx] &= ~(1 << modidx); + + /* Close channel */ + DAC_Close(dac_base, chn); + + /* Module-level windup from here: */ + + if (! dac_modinit_mask[modidx]) { + + /* Disable IP clock */ + CLK_DisableModuleClock(modinit->clkidx); + } +} + +void analogout_write(dac_t *obj, float value) +{ + if (value <= 0.0f) { + analogout_write_u16(obj, 0); + } else if (value >= 1.0f) { + analogout_write_u16(obj, 0xFFFF); + } else { + analogout_write_u16(obj, (uint16_t) (value * ((float) 0xFFFF))); + } +} + +void analogout_write_u16(dac_t *obj, uint16_t value) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + DAC_WRITE_DATA(dac_base, chn, value); + + /* Clear the DAC conversion complete finish flag for safe */ + DAC_CLR_INT_FLAG(dac_base, chn); + + /* Start A/D conversion */ + DAC_START_CONV(dac_base); + + /* Wait for completed */ + while (DAC_IS_BUSY(dac_base, chn)); +} + +float analogout_read(dac_t *obj) +{ + uint32_t value = analogout_read_u16(obj); + return (float) value * (1.0f / (float) 0xFFFF); +} + +uint16_t analogout_read_u16(dac_t *obj) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + uint16_t dat12_4 = DAC_READ_DATA(dac_base, chn); + /* Just 12 bits are effective. Convert to 16 bits. + * + * dat12_4 : b11b10b9b8 b7b6b5b4 b3b2b1b0 0000 + * dat16 : b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 + */ + uint16_t dat16 = (dat12_4 & 0xFFF0) | (dat12_4 >> 12); + + return dat16; +} + +#endif diff --git a/targets/TARGET_NUVOTON/TARGET_M451/objects.h b/targets/TARGET_NUVOTON/TARGET_M451/objects.h index 9c633835d9..7d258baa2e 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/objects.h +++ b/targets/TARGET_NUVOTON/TARGET_M451/objects.h @@ -48,6 +48,10 @@ struct analogin_s { //PinName pin; }; +struct dac_s { + DACName dac; +}; + struct serial_s { UARTName uart; PinName pin_tx; diff --git a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralNames.h b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralNames.h index 0cd1cb1d9a..67e3521200 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralNames.h +++ b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralNames.h @@ -67,6 +67,11 @@ typedef enum { ADC_0_15 = (int) NU_MODNAME(EADC_BASE, 0, 15) } ADCName; +typedef enum { + DAC_0_0 = (int) NU_MODNAME(DAC0_BASE, 0, 0), + DAC_1_0 = (int) NU_MODNAME(DAC1_BASE, 1, 0) +} DACName; + typedef enum { UART_0 = (int) NU_MODNAME(UART0_BASE, 0, 0), UART_1 = (int) NU_MODNAME(UART1_BASE, 1, 0), diff --git a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.c b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.c index ef35df7682..e9966cce40 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.c @@ -39,6 +39,15 @@ const PinMap PinMap_ADC[] = { {NC, NC, 0} }; +//*** DAC *** + +const PinMap PinMap_DAC[] = { + {PB_12, DAC_0_0, SYS_GPB_MFPH_PB12MFP_DAC0_OUT}, + {PB_13, DAC_1_0, SYS_GPB_MFPH_PB13MFP_DAC1_OUT}, + + {NC, NC, 0} +}; + //*** I2C *** const PinMap PinMap_I2C_SDA[] = { diff --git a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.h b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.h index 1f2eb94dee..0e6d9a30b1 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.h +++ b/targets/TARGET_NUVOTON/TARGET_M480/PeripheralPins.h @@ -32,6 +32,10 @@ extern const PinMap PinMap_GPIO[]; extern const PinMap PinMap_ADC[]; +//*** DAC *** + +extern const PinMap PinMap_DAC[]; + //*** I2C *** extern const PinMap PinMap_I2C_SDA[]; diff --git a/targets/TARGET_NUVOTON/TARGET_M480/analogout_api.c b/targets/TARGET_NUVOTON/TARGET_M480/analogout_api.c new file mode 100644 index 0000000000..948ea8b3ec --- /dev/null +++ b/targets/TARGET_NUVOTON/TARGET_M480/analogout_api.c @@ -0,0 +1,186 @@ +/* mbed Microcontroller Library + * Copyright (c) 2015-2016 Nuvoton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "analogout_api.h" + +#if DEVICE_ANALOGOUT + +#include "cmsis.h" +#include "pinmap.h" +#include "PeripheralPins.h" +#include "nu_modutil.h" + +/* Maximum DAC modules */ +#define NU_DACMOD_MAXNUM 2 +/* Maximum DAC channels per module */ +#define NU_DACCHN_MAXNUM 1 + +static uint32_t dac_modinit_mask[NU_DACMOD_MAXNUM]; + +static const struct nu_modinit_s dac_modinit_tab[] = { + {DAC_0_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL}, + {DAC_1_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL} +}; + +void analogout_init(dac_t *obj, PinName pin) +{ + obj->dac = (DACName) pinmap_peripheral(pin, PinMap_DAC); + MBED_ASSERT(obj->dac != (DACName) NC); + + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Module-level setup from here */ + + /* DAC0/DAC1 are designed to share the same RESET/clock/IRQ for group + * function. So we: + * + * 1. Go to setup flow (analogout_init()) only when none of DAC0/DAC1 + * channels are activated. + * 2. Go to windup flow (analogout_free()) only when all DAC0/DAC1 + * channels are deactivated. + */ + if ((! dac_modinit_mask[0]) && (! dac_modinit_mask[1])) { + /* Reset IP */ + SYS_ResetModule(modinit->rsetidx); + + /* Select IP clock source and clock divider */ + CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); + + /* Enable IP clock */ + CLK_EnableModuleClock(modinit->clkidx); + + /* The conversion settling time is 8us when 12-bit input code transition from + * lowest code (0x000) to highest code (0xFFF). */ + DAC_SetDelayTime(dac_base, 8); + + /* Configure DAT data format to left-aligned + * Effective 12-bits are aligned to left of 16-bit DAC_DAT. */ + DAC_ENABLE_LEFT_ALIGN(dac_base); + } + + /* Channel-level setup from here: */ + + /* Set the software trigger, enable DAC event trigger mode and enable D/A converter */ + DAC_Open(dac_base, chn, DAC_SOFTWARE_TRIGGER); + + /* Wire pinout */ + pinmap_pinout(pin, PinMap_DAC); + + /* Mark channel allocated */ + dac_modinit_mask[modidx] |= 1 << chn; +} + +void analogout_free(dac_t *obj) +{ + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Channel-level windup from here */ + + /* Mark channel free */ + dac_modinit_mask[modidx] &= ~(1 << modidx); + + /* Close channel */ + DAC_Close(dac_base, chn); + + /* Module-level windup from here: */ + + /* See analogout_init() for reason */ + if ((! dac_modinit_mask[0]) && (! dac_modinit_mask[1])) { + + /* Disable IP clock */ + CLK_DisableModuleClock(modinit->clkidx); + } +} + +void analogout_write(dac_t *obj, float value) +{ + if (value <= 0.0f) { + analogout_write_u16(obj, 0); + } else if (value >= 1.0f) { + analogout_write_u16(obj, 0xFFFF); + } else { + analogout_write_u16(obj, (uint16_t) (value * ((float) 0xFFFF))); + } +} + +void analogout_write_u16(dac_t *obj, uint16_t value) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + DAC_WRITE_DATA(dac_base, chn, value); + + /* Clear the DAC conversion complete finish flag for safe */ + DAC_CLR_INT_FLAG(dac_base, chn); + + /* Start A/D conversion */ + DAC_START_CONV(dac_base); + + /* Wait for completed */ + while (DAC_IS_BUSY(dac_base, chn)); +} + +float analogout_read(dac_t *obj) +{ + uint32_t value = analogout_read_u16(obj); + return (float) value * (1.0f / (float) 0xFFFF); +} + +uint16_t analogout_read_u16(dac_t *obj) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* We should have configured DAC data format to left-aligned */ + MBED_ASSERT(dac_base->CTL & DAC_CTL_LALIGN_Msk); + uint16_t dat12_4 = DAC_READ_DATA(dac_base, chn); + /* Just 12 bits are effective. Convert to 16 bits. + * + * dat12_4 : b11b10b9b8 b7b6b5b4 b3b2b1b0 0000 + * dat16 : b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 + */ + uint16_t dat16 = (dat12_4 & 0xFFF0) | (dat12_4 >> 12); + + return dat16; +} + +#endif diff --git a/targets/TARGET_NUVOTON/TARGET_M480/objects.h b/targets/TARGET_NUVOTON/TARGET_M480/objects.h index e3d24534d8..952e4c9ded 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/objects.h +++ b/targets/TARGET_NUVOTON/TARGET_M480/objects.h @@ -48,6 +48,10 @@ struct analogin_s { //PinName pin; }; +struct dac_s { + DACName dac; +}; + struct serial_s { UARTName uart; PinName pin_tx; diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralNames.h b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralNames.h index 1463866444..60f57d63b7 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralNames.h +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralNames.h @@ -61,6 +61,11 @@ typedef enum { ADC_0_11 = (int) NU_MODNAME(ADC_BASE, 0, 11), } ADCName; +typedef enum { + DAC_0_0 = (int) NU_MODNAME(DAC_BASE, 0, 0), + DAC_0_1 = (int) NU_MODNAME(DAC_BASE, 0, 1) +} DACName; + typedef enum { UART_0 = (int) NU_MODNAME(UART0_BASE, 0, 0), UART_1 = (int) NU_MODNAME(UART1_BASE, 1, 0), diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.c b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.c index e66e3e2453..b8f7caad94 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.c +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.c @@ -35,6 +35,15 @@ const PinMap PinMap_ADC[] = { {NC, NC, 0} }; +//*** DAC *** + +const PinMap PinMap_DAC[] = { + {PC_6, DAC_0_0, SYS_PC_L_MFP_PC6_MFP_DA_OUT0}, + {PC_7, DAC_0_1, SYS_PC_L_MFP_PC7_MFP_DA_OUT1}, + + {NC, NC, 0} +}; + //*** I2C *** const PinMap PinMap_I2C_SDA[] = { diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.h b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.h index a3018b5a40..f7e23d0e7b 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.h +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/PeripheralPins.h @@ -34,6 +34,10 @@ extern const PinMap PinMap_GPIO[]; extern const PinMap PinMap_ADC[]; +//*** DAC *** + +extern const PinMap PinMap_DAC[]; + //*** I2C *** extern const PinMap PinMap_I2C_SDA[]; diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/analogout_api.c b/targets/TARGET_NUVOTON/TARGET_NANO100/analogout_api.c new file mode 100644 index 0000000000..5d3a95c12d --- /dev/null +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/analogout_api.c @@ -0,0 +1,172 @@ +/* mbed Microcontroller Library + * Copyright (c) 2015-2016 Nuvoton + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "analogout_api.h" + +#if DEVICE_ANALOGOUT + +#include "cmsis.h" +#include "pinmap.h" +#include "PeripheralPins.h" +#include "nu_modutil.h" + +/* Maximum DAC modules */ +#define NU_DACMOD_MAXNUM 1 +/* Maximum DAC channels per module */ +#define NU_DACCHN_MAXNUM 2 + +static uint32_t dac_modinit_mask[NU_DACMOD_MAXNUM]; + +static const struct nu_modinit_s dac_modinit_tab[] = { + {DAC_0_0, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL}, + {DAC_0_1, DAC_MODULE, 0, 0, DAC_RST, DAC_IRQn, NULL} +}; + +void analogout_init(dac_t *obj, PinName pin) +{ + obj->dac = (DACName) pinmap_peripheral(pin, PinMap_DAC); + MBED_ASSERT(obj->dac != (DACName) NC); + + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Module-level setup from here */ + + if (! dac_modinit_mask[modidx]) { + /* Reset IP */ + SYS_ResetModule(modinit->rsetidx); + + /* Select IP clock source and clock divider */ + CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv); + + /* Enable IP clock */ + CLK_EnableModuleClock(modinit->clkidx); + + /* Configure conversion settling time + * + * DAC_Open() is per-channel, but its implementation involves per-module configuration of + * conversion settling time. Even so, we still use it for default conversion settling time + * rather than call per-module DAC_SetDelayTime(). This is to accommodate BSP driver. + * + * To configure conversion settling time separately to e.g. 8us, we would call: + * + * DAC_SetDelayTime(dac_base, CLK_GetHCLKFreq() * 8 / 1000000); + */ + } + + /* Channel-level setup from here: */ + + /* Set the software trigger, enable DAC event trigger mode and enable D/A converter */ + DAC_Open(dac_base, chn, DAC_WRITE_DAT_TRIGGER); + + /* Wire pinout */ + pinmap_pinout(pin, PinMap_DAC); + + /* Mark channel allocated */ + dac_modinit_mask[modidx] |= 1 << chn; +} + +void analogout_free(dac_t *obj) +{ + const struct nu_modinit_s *modinit = get_modinit(obj->dac, dac_modinit_tab); + MBED_ASSERT(modinit != NULL); + MBED_ASSERT(modinit->modname == obj->dac); + + /* Module index */ + uint32_t modidx = NU_MODINDEX(obj->dac); + MBED_ASSERT(modidx < NU_DACMOD_MAXNUM); + + /* Module subindex (aka channel) */ + uint32_t chn = NU_MODSUBINDEX(obj->dac); + MBED_ASSERT(chn < NU_DACCHN_MAXNUM); + + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + + /* Channel-level windup from here */ + + /* Mark channel free */ + dac_modinit_mask[modidx] &= ~(1 << modidx); + + /* Close channel */ + DAC_Close(dac_base, chn); + + /* Module-level windup from here: */ + + if (! dac_modinit_mask[modidx]) { + + /* Disable IP clock */ + CLK_DisableModuleClock(modinit->clkidx); + } +} + +void analogout_write(dac_t *obj, float value) +{ + if (value <= 0.0f) { + analogout_write_u16(obj, 0); + } else if (value >= 1.0f) { + analogout_write_u16(obj, 0xFFFF); + } else { + analogout_write_u16(obj, (uint16_t) (value * ((float) 0xFFFF))); + } +} + +void analogout_write_u16(dac_t *obj, uint16_t value) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + /* Convert 16 bits to effective 12 bits by dropping 4 LSB bits. */ + DAC_WRITE_DATA(dac_base, chn, value >> 4); + + /* Wait for completed */ + while (DAC_IS_BUSY(dac_base, chn)); +} + +float analogout_read(dac_t *obj) +{ + uint32_t value = analogout_read_u16(obj); + return (float) value * (1.0f / (float) 0xFFFF); +} + +uint16_t analogout_read_u16(dac_t *obj) +{ + DAC_T *dac_base = (DAC_T *) NU_MODBASE(obj->dac); + uint32_t chn = NU_MODSUBINDEX(obj->dac); + + uint16_t dat12 = chn ? dac_base->DATA1 : dac_base->DATA0; + dat12 = (dat12 & DAC_DATA_DACData_Msk) >> DAC_DATA_DACData_Pos; + /* Just 12 bits are effective. Convert to 16 bits. + * + * dat12 : 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0 + * dat16 : b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8 + */ + uint16_t dat16 = (dat12 << 4) | (dat12 >> 8); + + return dat16; +} + +#endif diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/objects.h b/targets/TARGET_NUVOTON/TARGET_NANO100/objects.h index 12476fd7c1..c595e1e3c1 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/objects.h +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/objects.h @@ -44,6 +44,10 @@ struct analogin_s { ADCName adc; }; +struct dac_s { + DACName dac; +}; + struct serial_s { UARTName uart; PinName pin_tx; diff --git a/targets/targets.json b/targets/targets.json index 4f47b25843..b3e39a4721 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -6746,6 +6746,7 @@ "LPTICKER", "RTC", "ANALOGIN", + "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", @@ -6873,6 +6874,7 @@ "LPTICKER", "RTC", "ANALOGIN", + "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", @@ -6938,6 +6940,7 @@ "LPTICKER", "RTC", "ANALOGIN", + "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", @@ -7239,6 +7242,7 @@ "LPTICKER", "RTC", "ANALOGIN", + "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH",