From 8da7c42d986832eb12b15eb5b2e6e9fd326795bc Mon Sep 17 00:00:00 2001 From: Steve Lascos Date: Sun, 17 Sep 2017 19:33:51 -0400 Subject: [PATCH] Initial Checkin --- README.md | 2 +- .../TGA_Pro_demo/SerialMonitorResults.txt | 51 ++++ examples/TGA_Pro_demo/TGA_Pro_demo.ino | 153 +++++++++++ keywords.txt | 24 ++ library.properties | 10 + src/BAAudioControlWM8731.cpp | 250 ++++++++++++++++++ src/BAAudioControlWM8731.h | 118 +++++++++ src/BAGpio.cpp | 77 ++++++ src/BAGpio.h | 82 ++++++ src/BAGuitar.h | 40 +++ src/BASpiMemory.cpp | 170 ++++++++++++ src/BASpiMemory.h | 54 ++++ 12 files changed, 1030 insertions(+), 1 deletion(-) create mode 100644 examples/TGA_Pro_demo/SerialMonitorResults.txt create mode 100644 examples/TGA_Pro_demo/TGA_Pro_demo.ino create mode 100644 keywords.txt create mode 100644 library.properties create mode 100644 src/BAAudioControlWM8731.cpp create mode 100644 src/BAAudioControlWM8731.h create mode 100644 src/BAGpio.cpp create mode 100644 src/BAGpio.h create mode 100644 src/BAGuitar.h create mode 100644 src/BASpiMemory.cpp create mode 100644 src/BASpiMemory.h diff --git a/README.md b/README.md index 673c547..099a022 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# BAGuitar +# libBAT Blackaddr Audio Teensy Library diff --git a/examples/TGA_Pro_demo/SerialMonitorResults.txt b/examples/TGA_Pro_demo/SerialMonitorResults.txt new file mode 100644 index 0000000..dea1bd7 --- /dev/null +++ b/examples/TGA_Pro_demo/SerialMonitorResults.txt @@ -0,0 +1,51 @@ +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Message, type=176, data = 32 10 +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Note On: ch=2, note=10, velocity=100 +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Message, type=176, data = 33 11 +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Note On: ch=3, note=10, velocity=100 +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Message, type=176, data = 34 12 +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +SPI writing DONE! +SPI TEST PASSED!!! +MIDI IS WORKING!!! +Note On: ch=4, note=10, velocity=100 +SPI writing DONE! +SPI TEST PASSED!!! + diff --git a/examples/TGA_Pro_demo/TGA_Pro_demo.ino b/examples/TGA_Pro_demo/TGA_Pro_demo.ino new file mode 100644 index 0000000..fb10b74 --- /dev/null +++ b/examples/TGA_Pro_demo/TGA_Pro_demo.ino @@ -0,0 +1,153 @@ +#include +#include +#include +#include "BAGuitar.h" + +using namespace BAGuitar; + +AudioInputI2S i2sIn; +AudioOutputI2S i2sOut; + +// Audio Thru Connection +AudioConnection patch0(i2sIn,0, i2sOut, 0); +AudioConnection patch1(i2sIn,1, i2sOut, 1); + +BAAudioControlWM8731 codecControl; +BAGpio gpio; // access to User LED +BASpiMemory spiMem0(SpiDeviceId::SPI_DEVICE0); + +unsigned long t=0; + +// SPI stuff +int spiAddress = 0; +int spiData = 0xff; +int spiErrorCount = 0; + + +void setup() { + + MIDI.begin(MIDI_CHANNEL_OMNI); + Serial.begin(57600); + delay(5); + + // If the codec was already powered up (due to reboot) power itd own first + codecControl.disable(); + delay(100); + AudioMemory(24); + + Serial.println("Enabling codec...\n"); + codecControl.enable(); + delay(100); + + +} + +void loop() { + + ////////////////////////////////////////////////////////////////// + // Write test data to the SPI Memory + ////////////////////////////////////////////////////////////////// + for (spiAddress=0; spiAddress <= SPI_MAX_ADDR; spiAddress++) { + if ((spiAddress % 32768) == 0) { + //Serial.print("Writing to "); + //Serial.println(spiAddress, HEX); + } + + //mem0Write(spiAddress, spiData); + spiMem0.write(spiAddress, spiData); + spiData = (spiData-1) & 0xff; + } + Serial.println("SPI writing DONE!"); + + /////////////////////////////////////////////////////////////////// + // Read back from the SPI memory + /////////////////////////////////////////////////////////////////// + spiErrorCount = 0; + spiAddress = 0; + spiData = 0xff; + + for (spiAddress=0; spiAddress <= SPI_MAX_ADDR; spiAddress++) { + if ((spiAddress % 32768) == 0) { + //Serial.print("Reading "); + //Serial.print(spiAddress, HEX); + } + + //int data = mem0Read(spiAddress); + int data = spiMem0.read(spiAddress); + if (data != spiData) { + spiErrorCount++; + Serial.println(""); + Serial.print("ERROR MEM0: (expected) (actual):"); + Serial.print(spiData, HEX); Serial.print(":"); + Serial.println(data, HEX); + delay(100); + } + + if ((spiAddress % 32768) == 0) { + //Serial.print(", data = "); + //Serial.println(data, HEX); + } + + spiData = (spiData-1) & 0xff; + + // Break out of test once the error count reaches 10 + if (spiErrorCount > 10) { break; } + + } + + if (spiErrorCount == 0) { Serial.println("SPI TEST PASSED!!!"); } + + + + /////////////////////////////////////////////////////////////////////// + // MIDI TESTING + /////////////////////////////////////////////////////////////////////// + int type, note, velocity, channel, d1, d2; + + // Send MIDI OUT + int cc, val=0xA, channelSend = 1; + for (cc=32; cc<40; cc++) { + MIDI.sendControlChange(cc, val, channelSend); val++; channelSend++; + delay(100); + MIDI.sendNoteOn(10, 100, channelSend); + delay(100); + } + + if (MIDI.read()) { // Is there a MIDI message incoming ? + byte type = MIDI.getType(); + Serial.println(String("MIDI IS WORKING!!!")); + switch (type) { + case NoteOn: + note = MIDI.getData1(); + velocity = MIDI.getData2(); + channel = MIDI.getChannel(); + if (velocity > 0) { + Serial.println(String("Note On: ch=") + channel + ", note=" + note + ", velocity=" + velocity); + } else { + Serial.println(String("Note Off: ch=") + channel + ", note=" + note); + } + break; + case NoteOff: + note = MIDI.getData1(); + velocity = MIDI.getData2(); + channel = MIDI.getChannel(); + Serial.println(String("Note Off: ch=") + channel + ", note=" + note + ", velocity=" + velocity); + break; + default: + d1 = MIDI.getData1(); + d2 = MIDI.getData2(); + Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2); + } + t = millis(); + } + + if (millis() - t > 10000) { + t += 10000; + Serial.println("(no MIDI activity, check cables)"); + } + + // Toggle the USR LED state + gpio.toggleLed(); + +} + diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..3b1044b --- /dev/null +++ b/keywords.txt @@ -0,0 +1,24 @@ +####################################### +# Syntax Coloring Map For ExampleLibrary +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +BAAudioControlWM8731 KEYWORD1 +BASpiMemory KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +doSomething KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### \ No newline at end of file diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..4dd2d8b --- /dev/null +++ b/library.properties @@ -0,0 +1,10 @@ +name=BAGuitar +version=0.1 +author=Steve Lascos +maintainer=Steve Lascos +sentence=A library for using the Blackaddr Audio Teensy Guitar Board. +paragraph=Extends the Teensy Audio Library. +category=Communication +url=www.blackaddr.com +architectures=avr +includes=BAGuitar.h \ No newline at end of file diff --git a/src/BAAudioControlWM8731.cpp b/src/BAAudioControlWM8731.cpp new file mode 100644 index 0000000..2a84ed8 --- /dev/null +++ b/src/BAAudioControlWM8731.cpp @@ -0,0 +1,250 @@ +/* + * BAAudioControlWM8731.h + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +#include +#include "BAAudioControlWM8731.h" + +namespace BAGuitar { + +void BAAudioControlWM8731::resetInternalReg(void) { + // Set to reset state + regArray[0] = 0x97; + regArray[1] = 0x97; + regArray[2] = 0x79; + regArray[3] = 0x79; + regArray[4] = 0x0a; + regArray[5] = 0x8; + regArray[6] = 0x9f; + regArray[7] = 0xa; + regArray[8] = 0; + regArray[9] = 0; +} + +BAAudioControlWM8731::BAAudioControlWM8731() +{ + resetInternalReg(); + Wire.begin(); // start the I2C service +} + +BAAudioControlWM8731::~BAAudioControlWM8731() +{ +} + +void BAAudioControlWM8731::disable(void) +{ + + // set OUTPD to '1' (powerdown), which is bit 4 + regArray[WM8731_REG_POWERDOWN] |= 0x10; + write(WM8731_REG_POWERDOWN, regArray[WM8731_REG_POWERDOWN]); + delay(100); // wait for power down + + // power down the rest of the supplies + write(WM8731_REG_POWERDOWN, 0x9f); // complete codec powerdown + delay(100); + + resetCodec(); +} + + +bool BAAudioControlWM8731::enable(void) +{ + + // Sequence from WAN0111.pdf + + // Begin configuring the codec + resetCodec(); + delay(100); // wait for reset + + // Power up all domains except OUTPD and microphone + regArray[WM8731_REG_POWERDOWN] = 0x12; + write(WM8731_REG_POWERDOWN, regArray[WM8731_REG_POWERDOWN]); + delay(100); // wait for codec powerup + + + setAdcBypass(false); // causes a slight click + setDacSelect(true); + setHPFDisable(true); + setLeftInputGain(0x17); // default input gain + setRightInputGain(0x17); + setLeftInMute(false); // no input mute + setRightInMute(false); + setDacMute(false); // unmute the DAC + + // mute the headphone outputs + write(WM8731_REG_LHEADOUT, 0x00); // volume off + regArray[WM8731_REG_LHEADOUT] = 0x00; + write(WM8731_REG_RHEADOUT, 0x00); + regArray[WM8731_REG_RHEADOUT] = 0x00; + + /// Configure the audio interface + write(WM8731_REG_INTERFACE, 0x02); // I2S, 16 bit, MCLK slave + regArray[WM8731_REG_INTERFACE] = 0x2; + + write(WM8731_REG_SAMPLING, 0x20); // 256*Fs, 44.1 kHz, MCLK/1 + regArray[WM8731_REG_SAMPLING] = 0x20; + delay(100); // wait for interface config + + // Activate the audio interface + setActivate(true); + delay(100); + + write(WM8731_REG_POWERDOWN, 0x02); // power up outputs + regArray[WM8731_REG_POWERDOWN] = 0x02; + delay(500); // wait for output to power up + +// // Activate the audio interface +// setActivate(true); +// delay(10); + + //setDacMute(false); + delay(100); // wait for mute ramp + + return true; + +} + + +void BAAudioControlWM8731::setLeftInputGain(int val) +{ + regArray[WM8731_LEFT_INPUT_GAIN_ADDR] &= ~WM8731_LEFT_INPUT_GAIN_MASK; + regArray[WM8731_LEFT_INPUT_GAIN_ADDR] |= + ((val << WM8731_LEFT_INPUT_GAIN_SHIFT) & WM8731_LEFT_INPUT_GAIN_MASK); + write(WM8731_LEFT_INPUT_GAIN_ADDR, regArray[WM8731_LEFT_INPUT_GAIN_ADDR]); +} + +void BAAudioControlWM8731::setLeftInMute(bool val) +{ + if (val) { + regArray[WM8731_LEFT_INPUT_MUTE_ADDR] |= WM8731_LEFT_INPUT_MUTE_MASK; + } else { + regArray[WM8731_LEFT_INPUT_MUTE_ADDR] &= ~WM8731_LEFT_INPUT_MUTE_MASK; + } + write(WM8731_LEFT_INPUT_MUTE_ADDR, regArray[WM8731_LEFT_INPUT_MUTE_ADDR]); +} + +void BAAudioControlWM8731::setLinkLeftRightIn(bool val) +{ + if (val) { + regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR] |= WM8731_LINK_LEFT_RIGHT_IN_MASK; + regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR] |= WM8731_LINK_RIGHT_LEFT_IN_MASK; + } else { + regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR] &= ~WM8731_LINK_LEFT_RIGHT_IN_MASK; + regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR] &= ~WM8731_LINK_RIGHT_LEFT_IN_MASK; + } + write(WM8731_LINK_LEFT_RIGHT_IN_ADDR, regArray[WM8731_LINK_LEFT_RIGHT_IN_ADDR]); + write(WM8731_LINK_RIGHT_LEFT_IN_ADDR, regArray[WM8731_LINK_RIGHT_LEFT_IN_ADDR]); +} + +void BAAudioControlWM8731::setRightInputGain(int val) +{ + regArray[WM8731_RIGHT_INPUT_GAIN_ADDR] &= ~WM8731_RIGHT_INPUT_GAIN_MASK; + regArray[WM8731_RIGHT_INPUT_GAIN_ADDR] |= + ((val << WM8731_RIGHT_INPUT_GAIN_SHIFT) & WM8731_RIGHT_INPUT_GAIN_MASK); + write(WM8731_RIGHT_INPUT_GAIN_ADDR, regArray[WM8731_RIGHT_INPUT_GAIN_ADDR]); +} + +void BAAudioControlWM8731::setRightInMute(bool val) +{ + if (val) { + regArray[WM8731_RIGHT_INPUT_MUTE_ADDR] |= WM8731_RIGHT_INPUT_MUTE_MASK; + } else { + regArray[WM8731_RIGHT_INPUT_MUTE_ADDR] &= ~WM8731_RIGHT_INPUT_MUTE_MASK; + } + write(WM8731_RIGHT_INPUT_MUTE_ADDR, regArray[WM8731_RIGHT_INPUT_MUTE_ADDR]); +} + +void BAAudioControlWM8731::setDacMute(bool val) +{ + if (val) { + regArray[WM8731_DAC_MUTE_ADDR] |= WM8731_DAC_MUTE_MASK; + } else { + regArray[WM8731_DAC_MUTE_ADDR] &= ~WM8731_DAC_MUTE_MASK; + } + write(WM8731_DAC_MUTE_ADDR, regArray[WM8731_DAC_MUTE_ADDR]); +} + +void BAAudioControlWM8731::setDacSelect(bool val) +{ + if (val) { + regArray[WM8731_DAC_SELECT_ADDR] |= WM8731_DAC_SELECT_MASK; + } else { + regArray[WM8731_DAC_SELECT_ADDR] &= ~WM8731_DAC_SELECT_MASK; + } + write(WM8731_DAC_SELECT_ADDR, regArray[WM8731_DAC_SELECT_ADDR]); +} + +void BAAudioControlWM8731::setAdcBypass(bool val) +{ + if (val) { + regArray[WM8731_ADC_BYPASS_ADDR] |= WM8731_ADC_BYPASS_MASK; + } else { + regArray[WM8731_ADC_BYPASS_ADDR] &= ~WM8731_ADC_BYPASS_MASK; + } + write(WM8731_ADC_BYPASS_ADDR, regArray[WM8731_ADC_BYPASS_ADDR]); +} + +void BAAudioControlWM8731::setHPFDisable(bool val) +{ + if (val) { + regArray[WM8731_HPF_DISABLE_ADDR] |= WM8731_HPF_DISABLE_MASK; + } else { + regArray[WM8731_HPF_DISABLE_ADDR] &= ~WM8731_HPF_DISABLE_MASK; + } + write(WM8731_HPF_DISABLE_ADDR, regArray[WM8731_HPF_DISABLE_ADDR]); +} + +void BAAudioControlWM8731::setActivate(bool val) +{ + if (val) { + write(WM8731_ACTIVATE_ADDR, WM8731_ACTIVATE_MASK); + } else { + write(WM8731_ACTIVATE_ADDR, 0); + } + +} + + +void BAAudioControlWM8731::resetCodec(void) +{ + write(WM8731_REG_RESET, 0x0); + resetInternalReg(); +} + +void BAAudioControlWM8731::writeI2C(unsigned int addr, unsigned int val) +{ + write(addr, val); +} + +bool BAAudioControlWM8731::write(unsigned int reg, unsigned int val) +{ + Wire.beginTransmission(WM8731_I2C_ADDR); + Wire.write((reg << 1) | ((val >> 8) & 1)); + Wire.write(val & 0xFF); + Wire.endTransmission(); +// Serial.print(String("Wrote ")); +// Serial.print((reg << 1) | ((val >> 8) & 1), HEX); +// Serial.print(" "); +// Serial.print(val & 0xFF, HEX); +// Serial.print("\n"); + + return true; +} + +} /* namespace BAGuitar */ diff --git a/src/BAAudioControlWM8731.h b/src/BAAudioControlWM8731.h new file mode 100644 index 0000000..975757c --- /dev/null +++ b/src/BAAudioControlWM8731.h @@ -0,0 +1,118 @@ +/* + * BAAudioControlWM8731.h + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +#ifndef INC_BAAUDIOCONTROLWM8731_H_ +#define INC_BAAUDIOCONTROLWM8731_H_ + +#include + +namespace BAGuitar { + +// use const instead of define for proper scoping +constexpr int WM8731_I2C_ADDR = 0x1A; +constexpr int WM8731_NUM_REGS = 10; + +constexpr int WM8731_REG_LLINEIN = 0; +constexpr int WM8731_REG_RLINEIN = 1; +constexpr int WM8731_REG_LHEADOUT = 2; +constexpr int WM8731_REG_RHEADOUT = 3; +constexpr int WM8731_REG_ANALOG =4; +constexpr int WM8731_REG_DIGITAL = 5; +constexpr int WM8731_REG_POWERDOWN = 6; +constexpr int WM8731_REG_INTERFACE = 7; +constexpr int WM8731_REG_SAMPLING = 8; +constexpr int WM8731_REG_ACTIVE = 9; +constexpr int WM8731_REG_RESET = 15; + +// Register Masks and Shifts +// Register 0 +constexpr int WM8731_LEFT_INPUT_GAIN_ADDR = 0; +constexpr int WM8731_LEFT_INPUT_GAIN_MASK = 0x1F; +constexpr int WM8731_LEFT_INPUT_GAIN_SHIFT = 0; +constexpr int WM8731_LEFT_INPUT_MUTE_ADDR = 0; +constexpr int WM8731_LEFT_INPUT_MUTE_MASK = 0x80; +constexpr int WM8731_LEFT_INPUT_MUTE_SHIFT = 7; +constexpr int WM8731_LINK_LEFT_RIGHT_IN_ADDR = 0; +constexpr int WM8731_LINK_LEFT_RIGHT_IN_MASK = 0x100; +constexpr int WM8731_LINK_LEFT_RIGHT_IN_SHIFT = 8; +// Register 1 +constexpr int WM8731_RIGHT_INPUT_GAIN_ADDR = 1; +constexpr int WM8731_RIGHT_INPUT_GAIN_MASK = 0x1F; +constexpr int WM8731_RIGHT_INPUT_GAIN_SHIFT = 0; +constexpr int WM8731_RIGHT_INPUT_MUTE_ADDR = 1; +constexpr int WM8731_RIGHT_INPUT_MUTE_MASK = 0x80; +constexpr int WM8731_RIGHT_INPUT_MUTE_SHIFT = 7; +constexpr int WM8731_LINK_RIGHT_LEFT_IN_ADDR = 1; +constexpr int WM8731_LINK_RIGHT_LEFT_IN_MASK = 0x100; +constexpr int WM8731_LINK_RIGHT_LEFT_IN_SHIFT = 8; +// Register 4 +constexpr int WM8731_ADC_BYPASS_ADDR = 4; +constexpr int WM8731_ADC_BYPASS_MASK = 0x8; +constexpr int WM8731_ADC_BYPASS_SHIFT = 3; +constexpr int WM8731_DAC_SELECT_ADDR = 4; +constexpr int WM8731_DAC_SELECT_MASK = 0x10; +constexpr int WM8731_DAC_SELECT_SHIFT = 4; +// Register 5 +constexpr int WM8731_DAC_MUTE_ADDR = 5; +constexpr int WM8731_DAC_MUTE_MASK = 0x8; +constexpr int WM8731_DAC_MUTE_SHIFT = 3; +constexpr int WM8731_HPF_DISABLE_ADDR = 5; +constexpr int WM8731_HPF_DISABLE_MASK = 0x1; +constexpr int WM8731_HPF_DISABLE_SHIFT = 0; + +// Register 9 +constexpr int WM8731_ACTIVATE_ADDR = 9; +constexpr int WM8731_ACTIVATE_MASK = 0x1; + + +class BAAudioControlWM8731 { +public: + BAAudioControlWM8731(); + virtual ~BAAudioControlWM8731(); + + void disable(void); + bool enable(void); + void setLeftInputGain(int val); + void setRightInputGain(int val); + void setLeftInMute(bool val); + void setRightInMute(bool val); + void setLinkLeftRightIn(bool val); + void setDacMute(bool val); + void setDacSelect(bool val); + void setAdcBypass(bool val); + void setHPFDisable(bool val); + + + void setActivate(bool val); + void resetCodec(void); + + void writeI2C(unsigned int addr, unsigned int val); +protected: + int regArray[WM8731_NUM_REGS]; + +private: + bool write(unsigned int reg, unsigned int val); + void resetInternalReg(void); + +}; + +} /* namespace BAGuitar */ + +#endif /* INC_BAAUDIOCONTROLWM8731_H_ */ diff --git a/src/BAGpio.cpp b/src/BAGpio.cpp new file mode 100644 index 0000000..1d3bcda --- /dev/null +++ b/src/BAGpio.cpp @@ -0,0 +1,77 @@ +/* + * BAGpio.cpp + * + * Created on: Sep 17, 2017 + * Author: slascos + */ + +#include "Arduino.h" +#include "BAGpio.h" + +namespace BAGuitar { + +BAGpio::BAGpio() +{ + // Set all GPIOs to input +// for (auto i=0; i(GPIO::GPIO0), INPUT); + pinMode(static_cast(GPIO::GPIO1), INPUT); + pinMode(static_cast(GPIO::GPIO2), INPUT); + pinMode(static_cast(GPIO::GPIO3), INPUT); + pinMode(static_cast(GPIO::GPIO4), INPUT); + pinMode(static_cast(GPIO::GPIO5), INPUT); + pinMode(static_cast(GPIO::GPIO6), INPUT); + pinMode(static_cast(GPIO::GPIO7), INPUT); + pinMode(static_cast(GPIO::TP1), INPUT); + pinMode(static_cast(GPIO::TP2), INPUT); + + pinMode(USR_LED_ID, OUTPUT); + m_ledState = 0; + + +} + +BAGpio::~BAGpio() +{ +} + +void BAGpio::setGPIODirection(GPIO gpioId, int direction) +{ + pinMode(static_cast(gpioId), direction); +} +void BAGpio::setGPIO(GPIO gpioId) +{ + digitalWrite(static_cast(gpioId), 0x1); +} +void BAGpio::clearGPIO(GPIO gpioId) +{ + digitalWrite(static_cast(gpioId), 0); + +} +void BAGpio::toggleGPIO(GPIO gpioId) +{ + int data = digitalRead(static_cast(gpioId)); + digitalWrite(static_cast(gpioId), ~data); +} + +void BAGpio::setLed() +{ + digitalWrite(USR_LED_ID, 0x1); + m_ledState = 1; +} +void BAGpio::clearLed() +{ + digitalWrite(USR_LED_ID, 0); + m_ledState = 0; +} +void BAGpio::toggleLed() +{ + m_ledState = ~m_ledState; + digitalWrite(USR_LED_ID, m_ledState); +} + + +} /* namespace BAGuitar */ diff --git a/src/BAGpio.h b/src/BAGpio.h new file mode 100644 index 0000000..cb2d451 --- /dev/null +++ b/src/BAGpio.h @@ -0,0 +1,82 @@ +/* + * BAGpio.h + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +#ifndef SRC_BAGPIO_H_ +#define SRC_BAGPIO_H_ + +namespace BAGuitar { + +//constexpr int NUM_GPIO = 10; + +constexpr uint8_t USR_LED_ID = 16; + +enum class GPIO : uint8_t { + GPIO0 = 2, + GPIO1 = 3, + GPIO2 = 4, + GPIO3 = 6, + + GPIO4 = 12, + GPIO5 = 32, + GPIO6 = 27, + GPIO7 = 28, + + TP1 = 34, + TP2 = 33 +}; +//// J3 - GPIO Header +//constexpr int GPIO0 = 2; +//constexpr int GPIO1 = 3; +//constexpr int GPIO2 = 4; +//constexpr int GPIO3 = 6; +// +//// J6 - GPIO Header +//constexpr int GPIO4 = 12; +//constexpr int GPIO5 = 32; +//constexpr int GPIO6 = 27; +//constexpr int GPIO7 = 28; + + +// Test points +//constexpr int TP1 = 34; +//constexpr int TP2 = 33; + +class BAGpio { +public: + BAGpio(); + virtual ~BAGpio(); + + void setGPIODirection(GPIO gpioId, int direction); + void setGPIO(GPIO gpioId); + void clearGPIO(GPIO gpioId); + void toggleGPIO(GPIO gpioId); + + void setLed(); + void clearLed(); + void toggleLed(); + +private: + //bool m_gpioState[NUM_GPIO]; + uint8_t m_ledState; +}; + +} /* namespace BAGuitar */ + +#endif /* SRC_BAGPIO_H_ */ diff --git a/src/BAGuitar.h b/src/BAGuitar.h new file mode 100644 index 0000000..3366aba --- /dev/null +++ b/src/BAGuitar.h @@ -0,0 +1,40 @@ +/* + * BAGuitar.h + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +#ifndef SRC_BATGUITAR_H_ +#define SRC_BATGUITAR_H_ + +#include "BAAudioControlWM8731.h" // Codec Control +#include "BASpiMemory.h" +#include "BAGpio.h" + +namespace BAGuitar { + +#define AUDIO_BLOCK_SIZE 128 + +// Spi Memory +constexpr int SPI_MAX_ADDR = 131071; + +// LED +constexpr int usrLED = 16; + +} + +#endif /* SRC_BATGUITAR_H_ */ diff --git a/src/BASpiMemory.cpp b/src/BASpiMemory.cpp new file mode 100644 index 0000000..b2e559c --- /dev/null +++ b/src/BASpiMemory.cpp @@ -0,0 +1,170 @@ +/* + * BASpiMemory.cpp + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +#include "Arduino.h" +#include "BASpiMemory.h" + +namespace BAGuitar { + +// MEM0 Settings +constexpr int SPI_CS_MEM0 = 15; +constexpr int SPI_MOSI_MEM0 = 7; +constexpr int SPI_MISO_MEM0 = 8; +constexpr int SPI_SCK_MEM0 = 14; + +// MEM1 Settings +constexpr int SPI_CS_MEM1 = 31; +constexpr int SPI_MOSI_MEM1 = 21; +constexpr int SPI_MISO_MEM1 = 5; +constexpr int SPI_SCK_MEM1 = 20; + +// SPI Constants +constexpr int SPI_WRITE_CMD = 0x2; +constexpr int SPI_READ_CMD = 0x3; +constexpr int SPI_ADDR_2_MASK = 0xFF0000; +constexpr int SPI_ADDR_2_SHIFT = 16; +constexpr int SPI_ADDR_1_MASK = 0x00FF00; +constexpr int SPI_ADDR_1_SHIFT = 8; +constexpr int SPI_ADDR_0_MASK = 0x0000FF; + + + + +BASpiMemory::BASpiMemory(SpiDeviceId memDeviceId) +{ + m_memDeviceId = memDeviceId; + m_settings = {20000000, MSBFIRST, SPI_MODE0}; + m_Init(); +} + +BASpiMemory::BASpiMemory(SpiDeviceId memDeviceId, uint32_t speedHz) +{ + m_memDeviceId = memDeviceId; + m_settings = {speedHz, MSBFIRST, SPI_MODE0}; + m_Init(); +} + +void BASpiMemory::m_Init() +{ + + switch (m_memDeviceId) { + case SpiDeviceId::SPI_DEVICE0 : + m_csPin = SPI_CS_MEM0; + SPI.setMOSI(SPI_MOSI_MEM0); + SPI.setMISO(SPI_MISO_MEM0); + SPI.setSCK(SPI_SCK_MEM0); + break; + +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + case SpiDeviceId::SPI_DEVICE1 : + m_csPin = SPI_CS_MEM1; + SPI1.setMOSI(SPI_MOSI_MEM1); + SPI1.setMISO(SPI_MISO_MEM1); + SPI1.setSCK(SPI_SCK_MEM1); + break; +#endif + + default : + // unreachable since memDeviceId is an enumerated class + return; + } + + SPI.begin(); + pinMode(m_csPin, OUTPUT); + digitalWrite(m_csPin, HIGH); + +} + +BASpiMemory::~BASpiMemory() { + // TODO Auto-generated destructor stub +} + +void BASpiMemory::write(int address, int data) +{ + + SPI.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + + switch (m_memDeviceId) { + case SpiDeviceId::SPI_DEVICE0 : + SPI.transfer(SPI_WRITE_CMD); + SPI.transfer((address & SPI_ADDR_2_MASK) >> SPI_ADDR_2_SHIFT); + SPI.transfer((address & SPI_ADDR_1_MASK) >> SPI_ADDR_1_SHIFT); + SPI.transfer((address & SPI_ADDR_0_MASK)); + SPI.transfer(data); + SPI.endTransaction(); + break; + +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + case SpiDeviceId::SPI_DEVICE1 : + digitalWrite(m_csPin, HIGH); + SPI1.transfer(SPI_WRITE_CMD); + SPI1.transfer((address & SPI_ADDR_2_MASK) >> SPI_ADDR_2_SHIFT); + SPI1.transfer((address & SPI_ADDR_1_MASK) >> SPI_ADDR_1_SHIFT); + SPI1.transfer((address & SPI_ADDR_0_MASK)); + SPI1.transfer(data); + SPI1.endTransaction(); + break; +#endif + + default : + break; + // unreachable + } + digitalWrite(m_csPin, HIGH); +} + +int BASpiMemory::read(int address) +{ + + int data = -1; + SPI.beginTransaction(m_settings); + digitalWrite(m_csPin, LOW); + switch (m_memDeviceId) { + case SpiDeviceId::SPI_DEVICE0 : + SPI.transfer(SPI_READ_CMD); + SPI.transfer((address & SPI_ADDR_2_MASK) >> SPI_ADDR_2_SHIFT); + SPI.transfer((address & SPI_ADDR_1_MASK) >> SPI_ADDR_1_SHIFT); + SPI.transfer((address & SPI_ADDR_0_MASK)); + data = SPI.transfer(0); + SPI.endTransaction(); + break; + +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) + case SpiDeviceId::SPI_DEVICE1 : + SPI1.transfer(SPI_READ_CMD); + SPI1.transfer((address & SPI_ADDR_2_MASK) >> SPI_ADDR_2_SHIFT); + SPI1.transfer((address & SPI_ADDR_1_MASK) >> SPI_ADDR_1_SHIFT); + SPI1.transfer((address & SPI_ADDR_0_MASK)); + data = SPI.transfer(0); + SPI1.endTransaction(); + break; +#endif + + default: + break; + // unreachable + } + + digitalWrite(m_csPin, HIGH); + return data; +} + +} /* namespace BAGuitar */ diff --git a/src/BASpiMemory.h b/src/BASpiMemory.h new file mode 100644 index 0000000..922359f --- /dev/null +++ b/src/BASpiMemory.h @@ -0,0 +1,54 @@ +/* + * BASpiMemory.h + * + * Created on: May 22, 2017 + * Author: slascos + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version.* + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ +#ifndef SRC_BASPIMEMORY_H_ +#define SRC_BASPIMEMORY_H_ + +#include + +namespace BAGuitar { + +enum class SpiDeviceId { + SPI_DEVICE0, + SPI_DEVICE1 +}; + +class BASpiMemory { +public: + BASpiMemory() = delete; + BASpiMemory(SpiDeviceId memDeviceId); + BASpiMemory(SpiDeviceId memDeviceId, uint32_t speedHz); + virtual ~BASpiMemory(); + + void write(int address, int data); + int read(int address); + +private: + SpiDeviceId m_memDeviceId; + uint8_t m_csPin; + SPISettings m_settings; + void m_Init(); + +}; + +class BASpiMemoryException {}; + +} /* namespace BAGuitar */ + +#endif /* SRC_BASPIMEMORY_H_ */