From 378202ca397dbdeba1f7071e1a4707483765f408 Mon Sep 17 00:00:00 2001 From: asterix Date: Mon, 28 Mar 2011 09:57:47 +0000 Subject: [PATCH] First implementation of dac api (beta version). git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4815 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/drv/dac.h | 89 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/bertos/drv/dac.h b/bertos/drv/dac.h index e745ec11..eacab989 100644 --- a/bertos/drv/dac.h +++ b/bertos/drv/dac.h @@ -50,20 +50,99 @@ #include #include +#include #include +#include CPU_HEADER(dac) -int dac_write(int ch, void *buf, size_t len); +struct DacContext; -INLINE int dac_putHalfWord(int ch, uint16_t sample) +typedef int (*DacWriteFunc_t) (struct DacContext *ctx, unsigned channel, uint16_t sample); +typedef void (*SetChannelMaskFunc_t) (struct DacContext *ctx, uint32_t mask); +typedef void (*SetSamplingRate_t) (struct DacContext *ctx, uint32_t rate); +typedef void (*DmaConversionBufFunc_t) (struct DacContext *ctx, void *buf, size_t len); +typedef bool (*DmaConversionIsFinished_t) (struct DacContext *ctx); +typedef void (*DmaStartStreamingFunc_t) (struct DacContext *ctx, void *buf, size_t len, size_t slicelen); +typedef void *(*DmaWaitConversionFunc_t) (struct DacContext *ctx); +typedef void (*DmaStopFunc_t) (struct DacContext *ctx); + +typedef struct DacContext +{ + DacWriteFunc_t write; + SetChannelMaskFunc_t setCh; + SetSamplingRate_t setSampleRate; + DmaConversionBufFunc_t conversion; + DmaConversionIsFinished_t isFinished; + DmaStartStreamingFunc_t start; + DmaWaitConversionFunc_t wait; + DmaStopFunc_t stop; + size_t slicelen; + + DB(id_t _type); +} DacContext; + +INLINE int dac_write(DacContext *ctx, unsigned channel, uint16_t sample) +{ + ASSERT(ctx->write); + return ctx->write(ctx, channel, sample); +} + +INLINE void dac_setChannelMask(struct DacContext *ctx, uint32_t mask) +{ + ASSERT(ctx->setCh); + ctx->setCh(ctx, mask); +} + +INLINE void dac_setSamplingRate(struct DacContext *ctx, uint32_t rate) +{ + ASSERT(ctx->setSampleRate); + ctx->setSampleRate(ctx, rate); +} + +/** + * Convert \param len samples stored into \param buf. + */ +INLINE void dac_dmaConversionBuffer(struct DacContext *ctx, void *buf, size_t len) { - return dac_write(ch, &sample, sizeof(uint16_t)); + ASSERT(ctx->conversion); + ctx->conversion(ctx, buf, len); } -INLINE int dac_putWord(int ch, uint32_t sample) +/** + * Check if a dma transfer is finished. + * + * Useful for kernel-less applications. + */ +INLINE bool dac_dmaIsFinished(struct DacContext *ctx) +{ + ASSERT(ctx->isFinished); + return ctx->isFinished(ctx); +} + +/** + * \param slicelen Must be a divisor of len, ie. len % slicelen == 0. + */ +INLINE void dac_dmaStartStreaming(struct DacContext *ctx, void *buf, size_t len, size_t slicelen) { - return dac_write(ch, &sample, sizeof(uint32_t)); + ASSERT(ctx->start); + ASSERT(len % slicelen == 0); + ctx->slicelen = slicelen; + ctx->start(ctx, buf, len, slicelen); } +INLINE void *dac_dmaWaitConversion(struct DacContext *ctx) +{ + ASSERT(ctx->wait); + return ctx->wait(ctx); +} + +INLINE void dac_dmaStop(struct DacContext *ctx) +{ + ASSERT(ctx->stop); + ctx->stop(ctx); +} + +#define dac_bits() DAC_BITS + void dac_init(void); /** \} */ //defgroup dac -- 2.25.1