mirror of https://github.com/ARMmbed/mbed-os.git
Support DAC
parent
8d89854589
commit
ba2174878e
|
@ -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))
|
||||
|
|
|
@ -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[] = {
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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
|
|
@ -44,6 +44,10 @@ struct analogin_s {
|
|||
ADCName adc;
|
||||
};
|
||||
|
||||
struct dac_s {
|
||||
DACName dac;
|
||||
};
|
||||
|
||||
struct serial_s {
|
||||
UARTName uart;
|
||||
PinName pin_tx;
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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[] = {
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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
|
|
@ -48,6 +48,10 @@ struct analogin_s {
|
|||
//PinName pin;
|
||||
};
|
||||
|
||||
struct dac_s {
|
||||
DACName dac;
|
||||
};
|
||||
|
||||
struct serial_s {
|
||||
UARTName uart;
|
||||
PinName pin_tx;
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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[] = {
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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
|
|
@ -48,6 +48,10 @@ struct analogin_s {
|
|||
//PinName pin;
|
||||
};
|
||||
|
||||
struct dac_s {
|
||||
DACName dac;
|
||||
};
|
||||
|
||||
struct serial_s {
|
||||
UARTName uart;
|
||||
PinName pin_tx;
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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[] = {
|
||||
|
|
|
@ -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[];
|
||||
|
|
|
@ -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
|
|
@ -44,6 +44,10 @@ struct analogin_s {
|
|||
ADCName adc;
|
||||
};
|
||||
|
||||
struct dac_s {
|
||||
DACName dac;
|
||||
};
|
||||
|
||||
struct serial_s {
|
||||
UARTName uart;
|
||||
PinName pin_tx;
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue