Class library for internal ADC and DAC connected by SPI. ADC is triggered by TIM2. This library support clock generator using TIM3 for switched-capacitor filter to smooth output signal of DAC. This library includes derivative class to support interrupt occured in end of AD conversion. Slave select of SPI for DAC is generated using TIM4. Validated for ST Nucleo F401RE, F411RE. New version. 内蔵 ADC と,SPI 接続の DAC のためのクラスライブラリ.ADC の変換開始トリガは TIM2 で発生.DAC の出力信号を平滑化するためのスイッチトキャパシタフィルタ用のクロックは TIM3 を使用.DAC の SPI 用スレーブ選択信号は TIM4 で発生.ST Nucleo F401RE,F411RE で動作を確認.新バージョン

Dependents:   UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more

DAC_MCP4921.cpp

Committer:
MikamiUitOpen
Date:
2015-02-02
Revision:
21:3731753ebf24
Parent:
20:c49f3b565a87

File content as of revision 21:3731753ebf24:

//------------------------------------------------------
// Class for single DAC in MCP4921
//
// 2015/02/02, Copyright (c) 2015 MIKAMI, Naoki
//------------------------------------------------------

#include "DAC_MCP4921.hpp"

namespace Mikami
{
    DAC_MCP4921::DAC_MCP4921(PinName mosi, PinName sclk,
                             PinName cs, PinName ldac)
        : spi_(mosi, NC, sclk),
          ld_(ldac, 0), mySpi_((SPI_TypeDef*)NULL)
    {
        if ( (mosi == PA_7) || (mosi == PB_5) )  mySpi_ = SPI1;
        if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2;
        if ( mosi == PC_12 )                     mySpi_ = SPI3;

        // Set SPI format
        spi_.format(16, 0);
        // Clock source of F401 for SPI1    : 84 MHz,
        //                      SPI2, SPI3  : 42 MHz
        mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR);
        if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0;
#ifdef __STM32F411xE_H
        mySpi_->CR1 += SPI_CR1_BR_0;
#endif  // __STM32F411xE_H
        
        // timer prescaler is set same value of boud rate for SPI
        uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1;
        if (mySpi_ != SPI1) psc = (psc + 1)*2 - 1;
        ss_ = new Tim4_ss(psc, 19, cs);
    }

    void DAC_MCP4921::ScfClockTim3(uint32_t clock, PinName pin)
    {
        if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) &&
             (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) )
        {
            fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4921::ScfClockTim3()\r\n");
            while (true) {}
        }

        PwmOut clockSCF(pin);
        
        TIM3->ARR =  SystemCoreClock/clock - 1;
        TIM3->PSC = 0;
        // Set capture/compare register 2
        if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) )
            TIM3->CCR1 = (TIM3->ARR + 1)/2;    
        if ( (pin == PB_5) || (pin == PC_7) )
            TIM3->CCR2 = (TIM3->ARR + 1)/2;    
        if (pin == PC_8)
            TIM3->CCR3 = (TIM3->ARR + 1)/2;    
        if (pin == PC_9)
            TIM3->CCR4 = (TIM3->ARR + 1)/2;    
    }
}