Library for build-in ADC and DAC in STM32F446 mounted on Nucleo F446RE. Nucleo F446RE に搭載されている STM32F446 の内蔵 ADC, DAC 用のライブラリ.
Dependents: Demo_F446_AD_DA F446_MySoundMachine F446_ADF_Nlms F446_Spectrogram
F446_DAC.hpp@5:7851fd2f875b, 2017-02-21 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Feb 21 12:56:51 2017 +0000
- Revision:
- 5:7851fd2f875b
- Parent:
- 3:d1da91aec62f
6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:c945b4fe9a48 | 1 | //-------------------------------------------------------- |
MikamiUitOpen | 0:c945b4fe9a48 | 2 | // Class for buit-in dual DAC on STM32F446 ---- Header |
MikamiUitOpen | 0:c945b4fe9a48 | 3 | // TIM3 is used for clock to external SCF |
MikamiUitOpen | 0:c945b4fe9a48 | 4 | // |
MikamiUitOpen | 0:c945b4fe9a48 | 5 | // STM32F446 内蔵の DAC 用のクラス(ヘッダ) |
MikamiUitOpen | 0:c945b4fe9a48 | 6 | // TIM3 を外付けの SCF のクロックとして使用 |
MikamiUitOpen | 5:7851fd2f875b | 7 | // A2 (PA_4): 左 |
MikamiUitOpen | 5:7851fd2f875b | 8 | // D13 (PA_5): 右 |
MikamiUitOpen | 0:c945b4fe9a48 | 9 | // |
MikamiUitOpen | 5:7851fd2f875b | 10 | // 2017/02/19, Copyright (c) 2017 MIKAMI, Naoki |
MikamiUitOpen | 0:c945b4fe9a48 | 11 | //-------------------------------------------------------- |
MikamiUitOpen | 0:c945b4fe9a48 | 12 | |
MikamiUitOpen | 0:c945b4fe9a48 | 13 | #include "mbed.h" |
MikamiUitOpen | 0:c945b4fe9a48 | 14 | |
MikamiUitOpen | 0:c945b4fe9a48 | 15 | #ifndef STM32F446xx |
MikamiUitOpen | 3:d1da91aec62f | 16 | #error Select NUCLEO-F446RE. |
MikamiUitOpen | 0:c945b4fe9a48 | 17 | #endif |
MikamiUitOpen | 0:c945b4fe9a48 | 18 | |
MikamiUitOpen | 0:c945b4fe9a48 | 19 | #ifndef F446_DAC_DUAL_HPP |
MikamiUitOpen | 0:c945b4fe9a48 | 20 | #define F446_DAC_DUAL_HPP |
MikamiUitOpen | 0:c945b4fe9a48 | 21 | |
MikamiUitOpen | 0:c945b4fe9a48 | 22 | namespace Mikami |
MikamiUitOpen | 0:c945b4fe9a48 | 23 | { |
MikamiUitOpen | 0:c945b4fe9a48 | 24 | class DacDual |
MikamiUitOpen | 0:c945b4fe9a48 | 25 | { |
MikamiUitOpen | 0:c945b4fe9a48 | 26 | public: |
MikamiUitOpen | 0:c945b4fe9a48 | 27 | // Constructor |
MikamiUitOpen | 0:c945b4fe9a48 | 28 | DacDual() : da1_(PA_4), da2_(PA_5) |
MikamiUitOpen | 0:c945b4fe9a48 | 29 | { |
MikamiUitOpen | 0:c945b4fe9a48 | 30 | DAC->CR = DAC_CR_EN1 | DAC_CR_TEN1 | DAC_CR_TSEL1 | |
MikamiUitOpen | 0:c945b4fe9a48 | 31 | DAC_CR_EN2 | DAC_CR_TEN2 | DAC_CR_TSEL2; |
MikamiUitOpen | 0:c945b4fe9a48 | 32 | } |
MikamiUitOpen | 0:c945b4fe9a48 | 33 | |
MikamiUitOpen | 0:c945b4fe9a48 | 34 | virtual ~DacDual() {} |
MikamiUitOpen | 0:c945b4fe9a48 | 35 | |
MikamiUitOpen | 0:c945b4fe9a48 | 36 | // -1.0f <= data1, data2 <= 1.0f |
MikamiUitOpen | 1:6b9f2af6613d | 37 | // data1: left, data2: right |
MikamiUitOpen | 1:6b9f2af6613d | 38 | void Write(float data1, float data2) |
MikamiUitOpen | 1:6b9f2af6613d | 39 | { WriteDac(ToUint16(data1), ToUint16(data2)); } |
MikamiUitOpen | 0:c945b4fe9a48 | 40 | |
MikamiUitOpen | 0:c945b4fe9a48 | 41 | // 0 <= data1, data2 <= 4095 |
MikamiUitOpen | 1:6b9f2af6613d | 42 | // data1: left, data2: right |
MikamiUitOpen | 0:c945b4fe9a48 | 43 | void Write(uint16_t data1, uint16_t data2) |
MikamiUitOpen | 5:7851fd2f875b | 44 | { WriteDac(__USAT(data1, BIT_WIDTH_), |
MikamiUitOpen | 5:7851fd2f875b | 45 | __USAT(data2, BIT_WIDTH_)); } |
MikamiUitOpen | 0:c945b4fe9a48 | 46 | |
MikamiUitOpen | 0:c945b4fe9a48 | 47 | // Set TIM3 for clock of switched-capacitor filter |
MikamiUitOpen | 0:c945b4fe9a48 | 48 | void ScfClock(uint32_t clock); |
MikamiUitOpen | 0:c945b4fe9a48 | 49 | |
MikamiUitOpen | 0:c945b4fe9a48 | 50 | private: |
MikamiUitOpen | 5:7851fd2f875b | 51 | static const uint32_t SWTRIG_ = DAC_SWTRIGR_SWTRIG1 |
MikamiUitOpen | 0:c945b4fe9a48 | 52 | | DAC_SWTRIGR_SWTRIG2; |
MikamiUitOpen | 5:7851fd2f875b | 53 | static const int BIT_WIDTH_ = 12; |
MikamiUitOpen | 0:c945b4fe9a48 | 54 | AnalogOut da1_, da2_; |
MikamiUitOpen | 0:c945b4fe9a48 | 55 | |
MikamiUitOpen | 0:c945b4fe9a48 | 56 | // Write dual-channel data |
MikamiUitOpen | 0:c945b4fe9a48 | 57 | void WriteDac(uint16_t val1, uint16_t val2) |
MikamiUitOpen | 0:c945b4fe9a48 | 58 | { |
MikamiUitOpen | 5:7851fd2f875b | 59 | DAC->DHR12RD = __PKHBT(val1, val2, 16); |
MikamiUitOpen | 5:7851fd2f875b | 60 | DAC->SWTRIGR = SWTRIG_; |
MikamiUitOpen | 0:c945b4fe9a48 | 61 | } |
MikamiUitOpen | 0:c945b4fe9a48 | 62 | |
MikamiUitOpen | 5:7851fd2f875b | 63 | // Saturate float to an unsigned 16-bit type |
MikamiUitOpen | 0:c945b4fe9a48 | 64 | uint16_t ToUint16(float val) |
MikamiUitOpen | 0:c945b4fe9a48 | 65 | { |
MikamiUitOpen | 5:7851fd2f875b | 66 | return __USAT((val + 1.0f)*2047.0f, BIT_WIDTH_); |
MikamiUitOpen | 0:c945b4fe9a48 | 67 | } |
MikamiUitOpen | 0:c945b4fe9a48 | 68 | |
MikamiUitOpen | 0:c945b4fe9a48 | 69 | // for inhibition of copy constructor |
MikamiUitOpen | 0:c945b4fe9a48 | 70 | DacDual(const DacDual&); |
MikamiUitOpen | 0:c945b4fe9a48 | 71 | // for inhibition of substitute operator |
MikamiUitOpen | 0:c945b4fe9a48 | 72 | DacDual& operator=(const DacDual&); |
MikamiUitOpen | 0:c945b4fe9a48 | 73 | }; |
MikamiUitOpen | 0:c945b4fe9a48 | 74 | } |
MikamiUitOpen | 1:6b9f2af6613d | 75 | #endif // F446_DAC_DUAL_HPP |