KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Revision:
17:a5d9908bd456
Parent:
16:30cb822e072f
Child:
18:1bf4abf6895b
--- a/main.cpp	Thu Jul 06 07:20:41 2017 +0000
+++ b/main.cpp	Fri Sep 15 23:33:27 2017 +0000
@@ -2,6 +2,7 @@
  * KIK01
  * Kick Machine
  *
+ * 2017.09.16 Proto04: SPI1 for AD8402 Wein Bridge DCO & Internal DAC for Dual-OTA-VCA
  * 2017.07.04 Proto03: MCP4922 DCA
  * 2017.06.19 Proto02
  * 2017.06.04 created.
@@ -12,36 +13,34 @@
 #include "rtos.h"
 #include "mcp3008.h"
 
-#include "SpiAmpController.h"
-
-#define UART_TRACE      (0)
+#define UART_TRACE      (1)
 #define PIN_CHECK       (1)
 #define TITLE_STR1      ("KIK01 Kick Machine")
-#define TITLE_STR2      ("20170704")
+#define TITLE_STR2      ("20170916")
 
 #define PI_F            (3.1415926f)
-#define SAMPLING_RATE   (48000)
-#define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
-#define ENVELOPE_UPDATE_RATE    SAMPLING_RATE
 
-#define AMP_VREF    0x0fff
+#define MCP3008_SPI_SPEED       (1000000)
+#define AD8402_SPI_SPEED        (4000000)
+#define ENVELOPE_UPDATE_RATE    (1000)  //  Hz
+
+AnalogOut Dac1(A2);
 
-AnalogOut Dac1(PA_4);
-
+/*
 SPI SpiM1(SPI_MOSI, SPI_MISO, SPI_SCK);
-MCP3008 Adc0(&SpiM1, D10);
-MCP3008 Adc1(&SpiM1, D9);
+*/
 
-SPI SpiM2(PB_15, PB_14, PB_13);
-SpiAmpController ampController(&SpiM2, PB_1, PC_4);
+SPI SpiM3(D4, D5, D3);
+MCP3008 Adc0(&SpiM3, D6);
+MCP3008 Adc1(&SpiM3, D7);
 
 // Check pins
-DigitalOut Dout0(D2);
-DigitalOut Dout1(D3);
-DigitalOut Dout2(D4);
+DigitalOut Dout0(D8);
+DigitalOut Dout1(D9);
+DigitalOut Dout2(D14);
 
 // Sync
-DigitalOut SyncPin(D7);
+DigitalOut SyncPin(D2);
 
 class EnvelopeAR
 {
@@ -166,16 +165,14 @@
 volatile EnvelopeParam amplitudeParam;
 
 volatile int ticks;
-volatile int envelopeTicks;
 volatile float frequency;
-volatile float phi;
-volatile float phiDelta;
 volatile float amplitude;
 
 volatile float bpm;
 volatile int envelopeLength;
 volatile int stepLength;
 
+#if 0
 //-----------------------------------------------------------------------------//
 // Internal DCA
 //
@@ -238,6 +235,40 @@
         phi = PI_F / 2.0f;
     }
 }
+#endif
+
+void AD8402Write(uint8_t address, uint8_t value)
+{
+    wait_us(10);
+    /*
+    SpiCs = 0;
+    SpiM.write(address);
+    SpiM.write(value);
+    SpiCs = 1;
+    wait_us(1);
+    */
+}
+
+void DcoSetFrequency()
+{
+#if (PIN_CHECK)
+    Dout1 = 1;
+#endif
+
+    frequency = envelopeFrequency.getAmplitude(ticks);
+    const float c = 0.00000047;
+    float r = 1.0f / (2.0f * PI_F * frequency * c);
+    uint8_t v = 256.0f * (r - 50.0f) / 10000.0f;
+    
+    //printf("%d\r\n", v);
+    
+    AD8402Write(0, v);
+    AD8402Write(1, v);
+
+#if (PIN_CHECK)
+    Dout1 = 0;
+#endif
+}
 
 void update()
 {
@@ -246,40 +277,33 @@
 #endif
 
     // Output Sync Signal per steps
-    if (envelopeTicks % stepLength == 0) {
+    if (ticks % stepLength == 0) {
         SyncPin = 1;
     }
 
-    ticks++;
-    if (ticks >= SAMPLING_RATE / ENVELOPE_UPDATE_RATE) {
-#if (PIN_CHECK)
-        Dout1 = 1;
-#endif
-        ticks = 0;
+    // set envelope parameters
+    envelopeAmplitude.setAttack(amplitudeParam.attack);
+    envelopeAmplitude.setRelease(amplitudeParam.release);
+    envelopeAmplitude.setV0(amplitudeParam.v0);
+    envelopeAmplitude.setV1(amplitudeParam.v1);
+    envelopeAmplitude.setV2(amplitudeParam.v2);
+    envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
+    envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
 
-        // set envelope parameters
-        envelopeAmplitude.setAttack(amplitudeParam.attack);
-        envelopeAmplitude.setRelease(amplitudeParam.release);
-        envelopeAmplitude.setV0(amplitudeParam.v0);
-        envelopeAmplitude.setV1(amplitudeParam.v1);
-        envelopeAmplitude.setV2(amplitudeParam.v2);
-        envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
-        envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
+    envelopeFrequency.setAttack(frequencyParam.attack);
+    envelopeFrequency.setRelease(frequencyParam.release);
+    envelopeFrequency.setV0(frequencyParam.v0);
+    envelopeFrequency.setV1(frequencyParam.v1);
+    envelopeFrequency.setV2(frequencyParam.v2);
+    envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
+    envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
 
-        envelopeFrequency.setAttack(frequencyParam.attack);
-        envelopeFrequency.setRelease(frequencyParam.release);
-        envelopeFrequency.setV0(frequencyParam.v0);
-        envelopeFrequency.setV1(frequencyParam.v1);
-        envelopeFrequency.setV2(frequencyParam.v2);
-        envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
-        envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
+    DcoSetFrequency();
 
-        generateEnvelope_DcaExternal();
-#if (PIN_CHECK)
-        Dout1 = 0;
-#endif
+    ticks++;
+    if (ticks >= envelopeLength) {
+        ticks = 0;
     }
-    generateWave_DcaExternal();
 
     // Output SyncSignal
     SyncPin = 0;
@@ -316,24 +340,23 @@
 {
     printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
 
+    /*
     SpiM1.format(0, 0);
     SpiM1.frequency(2000000);
-    
-    SpiM2.format(0, 0);
-    SpiM2.frequency(20000000);
+    */
+
+    SpiM3.format(8, 0);
+    SpiM3.frequency(MCP3008_SPI_SPEED);
 
-    frequency = 1000.0f;
-    phiDelta = 2.0f * frequency / SAMPLING_RATE;
+    frequency = 100.0f;
     amplitude = 1.0f;
+    bpm = 120.0f;
+    
+    setParams();
 
     ticks = 0;
-    envelopeTicks = 0;
-
-    bpm = 120.0f;
-    setParams();
-
     Ticker samplingTicker;
-    samplingTicker.attach(&update, SAMPLING_PERIOD);
+    samplingTicker.attach(&update, (1.0f/ENVELOPE_UPDATE_RATE));
 
     for (;;) {
 #if (PIN_CHECK)