Last active
August 12, 2019 23:33
-
-
Save nanase/227f826649ca419f15d43754803c12da to your computer and use it in GitHub Desktop.
YMF825 from Arduino MKR Zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the code is based on: | |
// https://github.com/yamaha-webmusic/ymf825board/blob/master/sample1/ymf825board_sample1/ymf825board_sample1.ino | |
#include "SPI.h" | |
#define PIN_YMF825_SS 7 | |
#define PIN_YMF825_RST 6 | |
unsigned char TONE_DATA[] = { | |
0x81, // header | |
// T_ADR 0 | |
0x01, 0x85, | |
0x00, | |
0x7F, | |
0xF4, | |
0xBB, | |
0x00, | |
0x10, | |
0x40, | |
0x00, | |
0xAF, | |
0xA0, | |
0x0E, | |
0x03, | |
0x10, | |
0x40, | |
0x00, | |
0x2F, | |
0xF3, | |
0x9B, | |
0x00, | |
0x20, | |
0x41, | |
0x00, | |
0xAF, | |
0xA0, | |
0x0E, | |
0x01, | |
0x10, | |
0x40, | |
0x80, | |
0x03, | |
0x81, | |
0x80, | |
}; | |
SPISettings spiSettings = SPISettings(1000000, MSBFIRST, SPI_MODE0); | |
void ymf825Initialize() { | |
digitalWrite(PIN_YMF825_RST, HIGH); | |
digitalWrite(PIN_YMF825_RST, LOW); | |
delay(1); | |
digitalWrite(PIN_YMF825_RST, HIGH); | |
ymf825Write(0x1D, 0x01); | |
ymf825Write(0x02, 0x0E); | |
delay(1); | |
ymf825Write(0x00, 0x01); // CLKEN | |
ymf825Write(0x01, 0x00); // AKRST | |
ymf825Write(0x1A, 0xA3); | |
delay(1); | |
ymf825Write(0x1A, 0x00); | |
delay(30); | |
ymf825Write(0x02, 0x04); // AP1,AP3 | |
delay(1); | |
ymf825Write(0x02, 0x00); | |
// add | |
ymf825Write(0x19, 0xF0); // MASTER VOL | |
ymf825Write(0x1B, 0x3F); // interpolation | |
ymf825Write(0x14, 0x00); // interpolation | |
ymf825Write(0x03, 0x01); // Analog Gain | |
ymf825Write(0x08, 0xF6); | |
delay(21); | |
ymf825Write(0x08, 0x00); | |
ymf825Write(0x09, 0xF8); | |
ymf825Write(0x0A, 0x00); | |
ymf825Write(0x17, 0x40); // MS_S | |
ymf825Write(0x18, 0x00); | |
ymf825Write(0x08, 0xf6); | |
delay(1); | |
ymf825Write(0x08, 0x00); | |
} | |
void ymf825Write(byte address, byte data) { | |
SPI.beginTransaction(spiSettings); | |
enableLch(); | |
SPI.transfer(address); | |
SPI.transfer(data); | |
disableSS(); | |
SPI.endTransaction(); | |
} | |
void ymf825BurstWrite(byte address, byte *data, uint16_t size) { | |
SPI.beginTransaction(spiSettings); | |
enableLch(); | |
SPI.transfer(address); | |
SPI.transfer(data, size); | |
disableSS(); | |
SPI.endTransaction(); | |
} | |
void enableLch() { | |
digitalWrite(PIN_YMF825_SS, LOW); | |
// PORT->Group[g_APinDescription[PIN_YMF825_SS].ulPort].OUTCLR.reg = (1ul << g_APinDescription[PIN_YMF825_SS].ulPin); | |
} | |
void disableSS() { | |
digitalWrite(PIN_YMF825_SS, HIGH); | |
// PORT->Group[g_APinDescription[PIN_YMF825_SS].ulPort].OUTCLR.reg = (1ul << g_APinDescription[PIN_YMF825_SS].ulPin); | |
} | |
void setup() { | |
pinMode(PIN_YMF825_SS, OUTPUT); | |
pinMode(PIN_YMF825_RST, OUTPUT); | |
disableSS(); | |
SPI.begin(); | |
ymf825Initialize(); | |
ymf825BurstWrite(0x07, TONE_DATA, 35); | |
ymf825Write(0x0F, 0x30); // keyon = 0 | |
ymf825Write(0x10, 0x71); // chvol | |
ymf825Write(0x11, 0x00); // XVB | |
ymf825Write(0x12, 0x08); // FRAC | |
ymf825Write(0x13, 0x00); // FRAC | |
// timer_setup(); | |
} | |
void keyon(unsigned char fnumh, unsigned char fnuml) { | |
ymf825Write(0x0B, 0x00); // voice num | |
ymf825Write(0x0C, 0x54); // vovol | |
ymf825Write(0x0D, fnumh); // fnum | |
ymf825Write(0x0E, fnuml); // fnum | |
ymf825Write(0x0F, 0x40); // keyon = 1 | |
} | |
void keyoff(void) { | |
ymf825Write(0x0F, 0x00); // keyon = 0 | |
} | |
void loop() { | |
// if (timer_is_advanced()) {} | |
keyon(0x14, 0x65); | |
delay(500); | |
keyoff(); | |
delay(200); | |
keyon(0x1c, 0x11); | |
delay(500); | |
keyoff(); | |
delay(200); | |
keyon(0x1c, 0x42); | |
delay(500); | |
keyoff(); | |
delay(200); | |
keyon(0x1c, 0x5d); | |
delay(500); | |
keyoff(); | |
delay(200); | |
keyon(0x24, 0x17); | |
delay(500); | |
keyoff(); | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment