不韋 呂 / UIT_ADDA

Dependents:   UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more

Files at this revision

API Documentation at this revision

Comitter:
MikamiUitOpen
Date:
Mon Oct 20 03:01:11 2014 +0000
Child:
1:b59e98443a6e
Commit message:
1

Changed in this revision

ADC_Base.cpp Show annotated file Show diff for this revision Revisions of this file
ADC_Base.hpp Show annotated file Show diff for this revision Revisions of this file
ADC_Interrupt.hpp Show annotated file Show diff for this revision Revisions of this file
DAC_MCP4922.cpp Show annotated file Show diff for this revision Revisions of this file
DAC_MCP4922.hpp Show annotated file Show diff for this revision Revisions of this file
ScfClockTim3.hpp Show annotated file Show diff for this revision Revisions of this file
tim4_slaveSelect.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC_Base.cpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,41 @@
+//------------------------------------------------------
+// Class for ADC using TIM2 trigger
+//      To get bit definition for register in
+//      peripheral, see "stm32f401xe.h"
+//
+// 2014/10/04, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#include "ADC_Base.hpp"
+
+namespace Mikami
+{
+    ADC_Base::ADC_Base(PinName pin1, int frequency,
+                       PinName pin2, PinName pin3)
+        : adc_(pin1), PIN1_(pin1), PIN2_(pin2),
+          PIN3_(pin3), myAdc_(ADC1)
+    {
+        myAdc_->CR2 = ADC_EXTERNALTRIGCONVEDGE_RISING   // External Trigger on the rising edge
+                    | ADC_EXTERNALTRIGCONV_T2_TRGO      // Use Timer2 TRGO event
+                    | ADC_CR2_ADON;                     // Enable ADC
+
+        if (pin2 != NC) adc2_ = new AnalogIn(pin2);
+        if (pin3 != NC) adc3_ = new AnalogIn(pin3);
+        SetTim2(frequency);
+        Select1stChannel();
+    }
+
+    void ADC_Base::SetTim2(int frequency)
+    {
+        __TIM2_CLK_ENABLE();    // Supply clock, See "stm32f4xx_hal_rcc.h"
+        
+        TIM_TypeDef* myTim = TIM2;
+
+        myTim->CR2 = TIM_CR2_MMS_1; // Update event: as trigger out
+        myTim->ARR = ((SystemCoreClock)/frequency) - 1;  // Auto-reload
+        myTim->PSC = 0;             // Prescaler
+        myTim->CR1 = TIM_CR1_CEN;   // Enable TIM2
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC_Base.hpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,83 @@
+//------------------------------------------------------
+// Class for ADC using TIM2 trigger -- Header
+//
+// 2014/10/04, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#ifndef ADC_BASE_HPP
+#define ADC_BASE_HPP
+
+#include "mbed.h"
+
+namespace Mikami
+{
+    class ADC_Base
+    {
+    private:
+        // Using mbed library for ADC
+        AnalogIn adc_;
+        // Default constructor is not defined in AnalogIn class
+        AnalogIn* adc2_;
+        AnalogIn* adc3_;
+        
+        // Pins for Analog input
+        const PinName PIN1_, PIN2_, PIN3_;
+
+        // Set timer to generate sampling pulse for ADC
+        void SetTim2(int frequency);
+        
+        // for inhibition of copy constructor
+        ADC_Base(const ADC_Base&);
+        // for inhibition of substitute operator
+        ADC_Base& operator=(const ADC_Base&);
+
+    protected:
+        // for normalize   
+        static const float AMP_ = 1.0f/2048.0f;
+
+        ADC_TypeDef* const myAdc_;     
+        
+        // Wait until completion of AD conversion
+        void WaitDone()
+        { while((myAdc_->SR & ADC_SR_EOC) == RESET); }
+
+    public:
+        // Constructor
+        //      pin1:       Pin Name for input as A0, A1, etc.
+        //      frequency:  Sampling frequency
+        //      pin2:       If use 2nd channel set this parameter
+        //      pin3:       If use 3rd channel set this parameter
+        ADC_Base(PinName pin1, int frequency,
+                 PinName pin2 = NC, PinName pin3 = NC);  
+        
+        // Read ADC with waiting, range: [0, 0x0FFF]
+        virtual uint16_t Read_u16()
+        {
+            WaitDone();
+            return myAdc_->DR;   
+        }
+
+        // Read ADC with waiting, range: [-1.0f, 1.0f]
+        virtual float Read()
+        {
+            WaitDone();
+            return AMP_*((int16_t)myAdc_->DR - 2048);
+        }
+
+        // Select channel
+        void Select1stChannel() { myAdc_->SQR3 = PIN1_; }
+        void Select2ndChannel() { myAdc_->SQR3 = PIN2_; }
+        void Select3rdChannel() { myAdc_->SQR3 = PIN3_; }
+        
+        // Software start
+        virtual void SoftStart()
+        { myAdc_->CR2 |= ADC_CR2_SWSTART; }
+        
+        uint32_t ReadSQR3() { return myAdc_->SQR3; }
+    };
+}
+#endif  // ADC_BASE_HPP
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADC_Interrupt.hpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,65 @@
+//------------------------------------------------------
+// Derived class of ADC_Base for use interrupt
+//
+// 2014/10/04, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#ifndef ADC_INTERRUP_HPP
+#define ADC_INTERRUP_HPP
+
+#include "ADC_Base.hpp"
+
+namespace Mikami
+{
+    class ADC_Intr : public ADC_Base
+    {
+    private:       
+        // for inhibition of copy constructor
+        ADC_Intr(const ADC_Intr&);
+        // for inhibition of substitute operator
+        ADC_Intr& operator=(const ADC_Intr&);
+
+    public:
+        ADC_Intr(PinName pin1, int frequency,
+                 PinName pin2 = NC, PinName pin3 = NC)
+            : ADC_Base(pin1, frequency, pin2, pin3)
+        { myAdc_->CR1 |= ADC_CR1_EOCIE; }   // Interrupt enable
+
+        // Set interrupt vector and enable IRQ of ADC
+        void SetIntrVec(void (*Func)())
+        {
+            NVIC_SetVector(ADC_IRQn, (uint32_t)Func);   // See "cmsis_nvic.h"
+            NVIC_EnableIRQ(ADC_IRQn);                   // See "core_cm4.h"
+        }       
+
+        // Read ADC, range: [0, 0x0FFF]
+        virtual uint16_t Read_u16()
+        { return myAdc_->DR; }
+
+        // Read ADC, range: [0, 0x0FFF]
+        virtual uint16_t ReadWait_u16()
+        {
+            WaitDone();
+            return myAdc_->DR;
+        }
+
+        // Read ADC, range: [-1.0f, 1.0f]
+        virtual float Read()
+        { return AMP_*((int16_t)myAdc_->DR - 2048); }
+        
+        // Clear pending IRQ and enable IRQ
+        void ClearPending_EnableIRQ()
+        {
+            NVIC_ClearPendingIRQ(ADC_IRQn);
+            NVIC_EnableIRQ(ADC_IRQn);
+        }
+
+        // Software start with disable IRQ
+        virtual void SoftStart()
+        {
+            NVIC_DisableIRQ(ADC_IRQn);
+            myAdc_->CR2 |= ADC_CR2_SWSTART;
+        }        
+    };
+}
+#endif  // ADC_INTERRUP_HPP
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_MCP4922.cpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,63 @@
+//------------------------------------------------------
+// Class for single DAC in MCP4922
+//
+// 2014/09/29, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#include "DAC_MCP4922.hpp"
+
+namespace Mikami
+{
+    DAC_MCP4922::DAC_MCP4922(DAC dac, PinName mosi, PinName sclk,
+                             PinName cs, PinName ldac, int hz)
+        : wcr_(dac | 0x3000), 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 and bus frequency
+        spi_.format(16, 0);
+        spi_.frequency(hz);
+            
+        // timer prescaler is set same value of boud rate for SPI
+        uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1;
+        ss_ = new Tim4_ss(psc, 18, cs);
+        
+        // Set DAC to 0
+        WriteDac(0);
+        while (IsBusy()) {}
+    }
+
+    void DAC_MCP4922::Write(float value)
+    {
+        if (value < -1.0f) value = -1.0f;
+        if (value >  1.0f) value =  1.0f;
+
+        WriteDac((uint16_t)((value + 1.0f)*2047));
+    }
+
+    void DAC_MCP4922::Write(uint16_t value)
+    {
+        WriteDac((value > 4095) ? 4095 : value);
+    }
+        
+    void DAC_MCP4922::Ldac()
+    {
+        ld_.write(0);
+        ld_.write(0);   // ensure width of "L" pulse
+        ld_.write(1);
+    }
+
+    void DAC_MCP4922::WriteDac(uint16_t value)
+    {
+        ss_->SlaveSelect();
+        mySpi_->DR = value | wcr_;
+    }
+}
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_MCP4922.hpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,72 @@
+//------------------------------------------------------
+// Class for single DAC in MCP4922 -- Header
+//      Fast version
+//
+// Default pin assign
+//      D11  SPI Master Out Slave In
+//      D13  SPI Serial Clock
+//      D12  SPI Slave Select
+//      D10  to MCP4922 LDAC pin
+//
+// 2014/09/29, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#ifndef DAC_MCP4922_HPP
+#define DAC_MCP4922_HPP
+
+#include "mbed.h"
+#include "tim4_slaveSelect.hpp"
+
+namespace Mikami
+{
+    class DAC_MCP4922
+    {
+    public:
+        enum DAC { DAC_A = 0, DAC_B = 0x8000 };
+
+        // Constructor
+        DAC_MCP4922(
+            DAC dac,
+            PinName mosi = SPI_MOSI,    // D11
+            PinName sclk = SPI_SCK,     // D13
+            PinName cs   = SPI_CS,      // D10
+            PinName ldac = SPI_MISO,    // D12
+            int hz = 21000000);         // 21 MHz
+
+        // -1.0f <= value <= 1.0f
+        void Write(float value);
+        // 0 <= value <= 4095
+        void Write(uint16_t value);
+
+        // generate LDAC negative-going pulse
+        void Ldac();
+
+        // LDAC to H
+        void LdacH() { ld_.write(1); }
+        // LDAC to L
+        void LdacL() { ld_.write(0); }
+
+        // Check busy
+        bool IsBusy()
+        { return (mySpi_->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY; }
+
+    private:
+        uint16_t wcr_;  // write command register
+        SPI spi_;       // SPI object of mbed
+        Tim4_ss* ss_;   // for slave select
+        DigitalOut ld_; // for LDAC
+
+        // Pointer of I2C
+        SPI_TypeDef* mySpi_;
+
+        // for inhibition of copy constructor
+        DAC_MCP4922(const DAC_MCP4922&);
+        // for inhibition of substitute operator
+        DAC_MCP4922& operator=(const DAC_MCP4922&);
+
+        // for internal use
+        void WriteDac(uint16_t value);
+    };
+}
+#endif  // DAC_MCP4922_HPP
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ScfClockTim3.hpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,24 @@
+//--------------------------------------------------------
+// Clock generator for SCF using PwmOut class
+//      Output pin: D9 (PC7) ------ TIM3 CH2
+// 2014/09/30, Copyright (c) 2014 MIKAMI, Naoki
+//--------------------------------------------------------
+
+#ifndef SCF_CLOCK_TIM3_HPP
+#define SCF_CLOCK_TIM3_HPP
+
+#include "mbed.h"
+ 
+namespace Mikami
+{
+    void ScfClockTim3(uint32_t clock)
+    {
+        PwmOut clockSCF_(D9);
+        
+        TIM3->ARR =  SystemCoreClock/clock - 1;
+        TIM3->PSC = 0;
+        // Set capture/compare register 2
+        TIM3->CCR2 = (TIM3->ARR + 1)/2;    
+    }
+}
+#endif  // SCF_CLOCK_TIM3_HPP
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tim4_slaveSelect.hpp	Mon Oct 20 03:01:11 2014 +0000
@@ -0,0 +1,54 @@
+//------------------------------------------------------
+//  Class for generate SPI slave select using TIM4
+//
+//  Default pin assignments: PB_6 (D10)
+//  PB_7, PB_8 (D15), and PB_9 (D14) also can be used
+//  2014/09/28, Copyright (c) 2014 MIKAMI, Naoki
+//------------------------------------------------------
+
+#ifndef TIM4_SLAVESELECT_HPP
+#define TIM4_SLAVESELECT_HPP
+
+#include "mbed.h"
+
+namespace Mikami
+{
+    class Tim4_ss
+    {
+    public:
+        Tim4_ss(uint16_t psc,       // prescaler
+                uint16_t arr = 18,  // auto-reload register
+                PinName pin = PB_6) // pin name
+                : myTim_(TIM4)
+        {
+            PwmOut css(pin);
+            myTim_->CR1 |= TIM_CR1_OPM; // one-pulse mode
+            myTim_->PSC = psc;          // prescaler
+            myTim_->ARR = arr;          // pulse width
+            if (pin == PB_6) myTim_->CCR1 = 1;
+            if (pin == PB_7) myTim_->CCR2 = 1;
+            if (pin == PB_8) myTim_->CCR3 = 1;
+            if (pin == PB_9) myTim_->CCR4 = 1;
+            if ( (pin != PB_6) && (pin != PB_7)
+               &&(pin != PB_8) && (pin != PB_9) )
+            {
+                fprintf(stderr, "\r\nIllegal pin name\r\n");
+                while (true) {}
+            }
+        }
+        // Generate slave select
+        void SlaveSelect()
+        {
+            myTim_->CNT = 1;            // Set counter 1
+            myTim_->CR1 |= TIM_CR1_CEN; // Enable TIM4
+        }
+    private:
+        TIM_TypeDef* myTim_;
+
+        // Forbid to use copy constructor
+        Tim4_ss(const Tim4_ss&);
+        // Forbid to use substitution operator
+        Tim4_ss operator=(const Tim4_ss&);
+    };
+}
+#endif  // TIM4_SLAVESELECT_HPP