不韋 呂 / UITDSP_ADDA2

Dependents:   UITDSP_ADDA_Example2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DAC_MCP4921.cpp Source File

DAC_MCP4921.cpp

00001 //--------------------------------------------------------------
00002 //  Class for DAC MCP4921
00003 //
00004 //  2015/02/18, Copyright (c) 2015 MIKAMI, Naoki
00005 //--------------------------------------------------------------
00006 
00007 #include "InterruptManager.h"
00008 #include "DAC_MCP4921.hpp"
00009 
00010 namespace Mikami
00011 {
00012     DAC_MCP4921::DAC_MCP4921(PinName mosi, PinName sclk,
00013                              PinName cs, PinName ldac)
00014         : spi_(mosi, NC, sclk), ld_(ldac, 0)
00015     {
00016         if ( (mosi == PA_7) || (mosi == PB_5) )  mySpi_ = SPI1;
00017         if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2;
00018         if ( mosi == PC_12 )                     mySpi_ = SPI3;
00019 
00020         // Set SPI format
00021         spi_.format(16, 0);
00022         // Clock source of F401 for SPI1    : 84 MHz,
00023         //                      SPI2, SPI3  : 42 MHz
00024         mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR);
00025         if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0;
00026 #ifdef __STM32F411xE_H
00027         mySpi_->CR1 += SPI_CR1_BR_0;
00028 #endif  // __STM32F411xE_H
00029         
00030         cs_ = new DigitalOut(cs, 1);
00031 
00032         mySpi_->CR2 |= SPI_CR2_RXNEIE;   // Enable SPI RX buffer not empty interrupt
00033 
00034         IRQn_Type irq = SPI1_IRQn;
00035         if (mySpi_ == SPI2) irq = SPI2_IRQn;
00036         if (mySpi_ == SPI3) irq = SPI3_IRQn;
00037         
00038         NVIC_SetVector(irq, (uint32_t)Isr); // See "cmsis_nvic.h"
00039         NVIC_EnableIRQ(irq);                // See "core_cm4.h"
00040     }
00041 
00042     void DAC_MCP4921::ScfClockTim3(uint32_t clock, PinName pin)
00043     {
00044         if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) &&
00045              (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) )
00046         {
00047             fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4922::ScfClockTim3()\r\n");
00048             while (true) {}
00049         }
00050 
00051         PwmOut clockSCF(pin);
00052         
00053         TIM3->ARR =  SystemCoreClock/clock - 1;
00054         TIM3->PSC = 0;
00055         // Set capture/compare register 2
00056         if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) )
00057             TIM3->CCR1 = (TIM3->ARR + 1)/2;    
00058         if ( (pin == PB_5) || (pin == PC_7) )
00059             TIM3->CCR2 = (TIM3->ARR + 1)/2;    
00060         if (pin == PC_8)
00061             TIM3->CCR3 = (TIM3->ARR + 1)/2;    
00062         if (pin == PC_9)
00063             TIM3->CCR4 = (TIM3->ARR + 1)/2;    
00064     }
00065     
00066     DigitalOut* DAC_MCP4921::cs_;
00067     SPI_TypeDef* DAC_MCP4921::mySpi_;
00068 }
00069