Function generator using the DAC output, using DMA alone.

Dependents:   DACDMAfuncgenlib

This library has been inspired by MODDMA example4.h.

Some differences compared with the MODDMA example: 1) this DMAFuncGen class can work using dma alone (no ISR required, although there is a callback routine included if you would like to synchronize something to the function generator), and 2) there is only one buffer (not two).

It is intended for the LPC1768. It has been tested and seems to work fine.

For a demo program, see

Import programDACDMAfuncgenlib

Generate a sine wave on the Analog Output using DMA alone.

Committer:
Mischa
Date:
Sun Dec 29 01:00:30 2013 +0000
Revision:
0:337ad0fe7734
First version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mischa 0:337ad0fe7734 1 #ifndef MBED_DMAFUNCGEN_H
Mischa 0:337ad0fe7734 2 #define MBED_DMAFUNCGEN_H
Mischa 0:337ad0fe7734 3
Mischa 0:337ad0fe7734 4 #include "mbed.h"
Mischa 0:337ad0fe7734 5 #include "MODDMA.h"
Mischa 0:337ad0fe7734 6
Mischa 0:337ad0fe7734 7 //! DMAFuncGen class generates a waveform on Analog output using Direct Memory Access alone.
Mischa 0:337ad0fe7734 8 class DMAFuncGen {
Mischa 0:337ad0fe7734 9 public:
Mischa 0:337ad0fe7734 10 /// Create DMAFucGen instance
Mischa 0:337ad0fe7734 11 /// @param dma The dma to use.
Mischa 0:337ad0fe7734 12 /// @param channel The dma channel to use for transferring data to the D/A converter.
Mischa 0:337ad0fe7734 13 DMAFuncGen(MODDMA& dma, MODDMA::CHANNELS channel);
Mischa 0:337ad0fe7734 14
Mischa 0:337ad0fe7734 15 /// Number of data points in the waveform. Must be set by client.
Mischa 0:337ad0fe7734 16 int buffer_size;
Mischa 0:337ad0fe7734 17 /// Buffer for the binary data that will be transferred by DMA. Must be allocated by client.
Mischa 0:337ad0fe7734 18 uint32_t* buffer;
Mischa 0:337ad0fe7734 19
Mischa 0:337ad0fe7734 20 /// Set waveform value for a specified data point.
Mischa 0:337ad0fe7734 21 /// @param idx The index of the data point in #buffer, must be in range 0..#buffer_size-1.
Mischa 0:337ad0fe7734 22 /// @param x The value for the D/A, in the range 0..65535.
Mischa 0:337ad0fe7734 23 void set(int idx, uint16_t x);
Mischa 0:337ad0fe7734 24 /// Get waveform value for a specified data point
Mischa 0:337ad0fe7734 25 /// @param idx The index of the data point in the #buffer, in the range 0..#buffer_size-1.
Mischa 0:337ad0fe7734 26 /// @returns The value for the data point, in the range 0..65535.
Mischa 0:337ad0fe7734 27 uint16_t operator[](const uint16_t idx);
Mischa 0:337ad0fe7734 28
Mischa 0:337ad0fe7734 29 /// Connect the D/A converter pin (enable output)
Mischa 0:337ad0fe7734 30 void Connect();
Mischa 0:337ad0fe7734 31 /// Disconnect the D/A converter pin (pin becomes tri-state)
Mischa 0:337ad0fe7734 32 void Disconnect();
Mischa 0:337ad0fe7734 33
Mischa 0:337ad0fe7734 34 /// Reference to the DMA instance
Mischa 0:337ad0fe7734 35 MODDMA& dma;
Mischa 0:337ad0fe7734 36
Mischa 0:337ad0fe7734 37 /// Set up the dma.
Mischa 0:337ad0fe7734 38 /// Must set #buffer and #buffer_size first.
Mischa 0:337ad0fe7734 39 void Setup(void);
Mischa 0:337ad0fe7734 40
Mischa 0:337ad0fe7734 41 /// Set waveform frequency
Mischa 0:337ad0fe7734 42 /// @note Must set #buffer_size first.
Mischa 0:337ad0fe7734 43 /// @param f Frequency of a complete cycle of the waveform (in Hz).
Mischa 0:337ad0fe7734 44 void SetFrequency(float f);
Mischa 0:337ad0fe7734 45 /// Read the actual waveform frequency that will be generated.
Mischa 0:337ad0fe7734 46 float Frequency();
Mischa 0:337ad0fe7734 47
Mischa 0:337ad0fe7734 48 /// Start generating the waveform (start dma)
Mischa 0:337ad0fe7734 49 void Start();
Mischa 0:337ad0fe7734 50 /// Stop generating the waveform (but waits for the current cycle to complete)
Mischa 0:337ad0fe7734 51 void Stop();
Mischa 0:337ad0fe7734 52
Mischa 0:337ad0fe7734 53 private:
Mischa 0:337ad0fe7734 54 MODDMA_Config conf;
Mischa 0:337ad0fe7734 55 MODDMA_LLI lli;
Mischa 0:337ad0fe7734 56
Mischa 0:337ad0fe7734 57 /// ISR routine, called on Terminal Count
Mischa 0:337ad0fe7734 58 void TC_callback(void);
Mischa 0:337ad0fe7734 59 /// ISR routine, called on dma ERRor
Mischa 0:337ad0fe7734 60 void ERR_callback(void);
Mischa 0:337ad0fe7734 61 };
Mischa 0:337ad0fe7734 62
Mischa 0:337ad0fe7734 63 #endif