ヤマハのFM音源YMF825をArduino UNOで制御できましたので、ESP32に移植しました。
ESP32で使うと何がうれしいか !! —- >> タッチセンサーとBLEが使えます。
Arduino UNOの例は下記の記事を参照してください。
http://keisoku-lab.mond.jp/2018/02/09/ヤマハfm音源lsi-ymf825搭載モジュールのデモプログラム/
ESP32は3.3V系ですから、5V系のUNOで使っていたYMF825モジュールを接続するのはちょっと注意が必要です。ウダ電子の公式ページでは5Vと3.3V電源を接続し、抵抗を外し、ハンダを盛ってジャンパーする旨書かれています。3.3V単独電源になるなら頑張れますが、5Vも供給しなければいけないのは面倒ですから、YMF825モジュールは5Vのままで、SPIの信号を調整する方向で考えます。信号線はSCK, MISO, MOSI, SSとリセット信号の5本です。ESP32から出力する信号は3.3VなのでそのままでもYMF825に伝わりますので気にしないことにします。ESP32に5V信号が入ってくると壊れてしまいますが、入力はMISOだけです。MISOはレジスタの値を読み取る時に使われますが、読まないと決めればつなぐ必要がないことに気がつきました。SPIライブラリの中ではMISOも指定しないとエラーになりますのでMISOも割り当てますが、配線はしません。


但し、後日何か問題が起こった時に配線チェックをして、抜けている線を発見すると反射的に結線してしまう可能性がありますので注意が必要です。何気なくMISOを結線するとESP32を壊してしまいます。Vfが2VぐらいのLEDの+側をYMF825のMISO、-側をESP32のMISO入力端子に接続しておけば、思い出すきっかけにもなりますし、破壊する心配も無くなります。
ESP32のSPIでYMF825を使う方法は
ywabikoさんの記事がたいへん参考になりました。
YMF825モジュール <<—->> ESP32
RST_N <<—->> IO32
5V <<—->> USB 5V
GND <<—->> USB GNDとESP32 GND
SCK <<—->> IO18
MISO <<—->> 接続しない (IO19)
MOSI <<—->> IO23
SS <<—->> IO5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
// //Based on ymf825board_sample1 // 音色データの追加と和音演奏 // 音色パラメータによる音色データの生成 // 西洋音階での演奏 #include <SPI.h> //0 :5V 1:3.3V #define OUTPUT_power 0 #define PIN_RESET 32 // VSPI #define PIN_CS 5 #define PIN_SCLK 18 #define PIN_MOSI 23 #define PIN_MISO 19//not connected SPIClass spi_; void set_ss_pin(int val) { digitalWrite(PIN_CS, val); } void set_rst_pin(int val) { digitalWrite(PIN_RESET, val); } //西洋音階 int noteFnum[12]={357,378,401,425,450,477,505,535,567,601,637,674};//ピアノの白鍵黒鍵 int wholeStep[7]={0,2,4,5,7,9,11};//ドレミファソラシ //音色パラメーター(Yamahaの音色設計から抽出) int const TONES=4; //GrandPiano,TenorSax,NewAgePd,PickBass byte BO[TONES]= {0x01,0x00,0x00,0x00};//0-3 byte LFO[TONES]= {0x01,0x01,0x01,0x01};//0-3 byte ALG[TONES]= {0x03,0x05,0x05,0x03};//0-7 byte SR[TONES][4]={ {0x00,0x02,0x01,0x02}, {0x00,0x00,0x00,0x00}, {0x03,0x04,0x00,0x00}, {0x02,0x04,0x02,0x06}};//0-7 byte XOF[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}};//0-1 byte KSR[TONES][4]={ {0x00,0x01,0x00,0x01}, {0x01,0x00,0x01,0x00}, {0x01,0x00,0x00,0x00}, {0x01,0x01,0x01,0x01}};//0-1 byte RR[TONES][4]={ {0x06,0x03,0x04,0x06}, {0x00,0x09,0x00,0x09}, {0x00,0x09,0x00,0x09}, {0x03,0x06,0x06,0x08}};//0-15 byte DR[TONES][4]={ {0x07,0x03,0x01,0x02}, {0x03,0x02,0x03,0x02}, {0x0F,0x07,0x01,0x01}, {0x07,0x0b,0x09,0x02}};//0-15 byte AR[TONES][4]={ {0x0F,0x0E,0x0D,0x0D}, {0x07,0x07,0x07,0x07}, {0x0F,0x0F,0x06,0x08}, {0x0f,0x0c,0x0f,0x0b}};//0-15 byte SL[TONES][4]={ {0x0f,0x02,0x03,0x04}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x01}, {0x01,0x07,0x02,0x06}};//0-15 byte TL[TONES][4]={ {0x27,0x28,0x22,0x00}, {0x05,0x0f,0x08,0x0d}, {0x26,0x0b,0x18,0x00}, {0x13,0x15,0x17,0x00}};//0-63 byte KSL[TONES][4]={ {0x01,0x03,0x00,0x02}, {0x02,0x00,0x02,0x02}, {0x00,0x02,0x02,0x02}, {0x02,0x00,0x02,0x00}};//0-3 byte DAM[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x02,0x02,0x02,0x02}, {0x02,0x02,0x00,0x00}, {0x02,0x02,0x02,0x02}};//0-3 byte EAM[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}};//0-1 byte DVB[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x02,0x01,0x02,0x01}, {0x02,0x02,0x01,0x01}, {0x02,0x02,0x02,0x02}};//0-3 byte EVB[TONES][4]={ {0x00,0x00,0x01,0x01}, {0x00,0x01,0x00,0x01}, {0x00,0x00,0x01,0x01}, {0x00,0x00,0x01,0x01}};//0-1 byte MULTI[TONES][4]={ {0x01,0x05,0x01,0x01}, {0x01,0x01,0x01,0x01}, {0x07,0x05,0x01,0x01}, {0x01,0x07,0x02,0x01}};//0-15 byte DT[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x00,0x00,0x00,0x00}, {0x00,0x00,0x07,0x00}, {0x00,0x00,0x00,0x00}};//0-7 byte WS[TONES][4]={ {0x08,0x00,0x00,0x00}, {0x01,0x08,0x09,0x00}, {0x01,0x00,0x01,0x00}, {0x00,0x00,0x00,0x00}};//0-31 byte FB[TONES][4]={ {0x00,0x00,0x00,0x00}, {0x03,0x00,0x03,0x00}, {0x05,0x00,0x06,0x00}, {0x05,0x00,0x00,0x00}};//0-7 void setup() { Serial.begin(9600); Serial.println(""); Serial.println("ymf825_esp32_demo180213"); pinMode(PIN_RESET, OUTPUT); pinMode(PIN_CS, OUTPUT); spi_.begin(PIN_SCLK, PIN_MISO, PIN_MOSI, PIN_CS); //spi_.begin(PIN_SCLK, , PIN_MOSI, PIN_CS); spi_.setFrequency(7000000); spi_.setDataMode(SPI_MODE3); spi_.setHwCs(false); set_ss_pin(HIGH); init_825(); set_tone(); set_ch(); } void loop() { for(int t=0;t<4;t++){ //tone 0-3 for(int i=0;i<3;i++){ midiNote(t, 0, 24); midiNote(t, 1, 28); midiNote(t, 2, 31); delay(200); keyoffVn(0); keyoffVn(1); keyoffVn(2); delay(10); } for(int i=0;i<3;i++){ midiNote(t, 0, 31); midiNote(t, 1, 36); midiNote(t, 2, 40); delay(400); keyoffVn(0); keyoffVn(1); keyoffVn(2); delay(10); } for(int i=0;i<3;i++){ midiNote(t, 0, 40); midiNote(t, 1, 43); midiNote(t, 2, 48); delay(200); keyoffVn(0); keyoffVn(1); keyoffVn(2); delay(10); } delay(0); for(int i=1;i<8;i++){ for(int j=0;j<7;j++){ int note0=i*12 + wholeStep[j]; int note1=(7-i)*12+wholeStep[(6-j)]; midiNote(t, 0, note0); midiNote(t, 1, note1); delay(200); keyoffVn(0); keyoffVn(1); delay(10); } } for(int i=7;i>=1;i--){ for(int j=6;j>=0;j--){ int note=i*12 + wholeStep[j]; midiNote(t, 0, note); delay(200); keyoffVn(0); delay(10); } } delay(2000); } } void midiNote(int tnum, int voice, int note){ int fnum=noteFnum[note%12]; byte block=(byte)note/12; byte fnumh=((0x380 & fnum)>>4)|block; //(0,0,f9,f8,f7,b2,b1,b0) byte fnuml=0x7f&fnum; //(0,f6,f5,f4,f3,f2,f1,f0) keyonVn(tnum,voice,fnumh,fnuml); } void init_825(void) { set_rst_pin(LOW); delay(1); set_rst_pin(HIGH); if_s_write( 0x1D, OUTPUT_power ); if_s_write( 0x02, 0x0E ); delay(1); if_s_write( 0x00, 0x01 );//CLKEN if_s_write( 0x01, 0x00 ); //AKRST if_s_write( 0x1A, 0xA3 ); delay(1); if_s_write( 0x1A, 0x00 ); delay(30); if_s_write( 0x02, 0x04 );//AP1,AP3 delay(1); if_s_write( 0x02, 0x00 ); //add if_s_write( 0x19, 0xF0 );//MASTER VOL if_s_write( 0x1B, 0x3F );//interpolation if_s_write( 0x14, 0x00 );//interpolation if_s_write( 0x03, 0x01 );//Analog Gain if_s_write( 0x08, 0xF6 ); delay(21); if_s_write( 0x08, 0x00 ); if_s_write( 0x09, 0xF8 ); if_s_write( 0x0A, 0x00 ); if_s_write( 0x17, 0x40 );//MS_S if_s_write( 0x18, 0x00 ); } void set_ch(void){ if_s_write( 0x0F, 0x30 );// keyon = 0 if_s_write( 0x10, 0x71 );// chvol if_s_write( 0x11, 0x00 );// XVB if_s_write( 0x12, 0x08 );// FRAC if_s_write( 0x13, 0x00 );// FRAC } void keyonVn(unsigned char tnum, unsigned char voice, unsigned char fnumh, unsigned char fnuml){ byte keyTone=0x40|(tnum&0x0f); if_s_write( 0x0B, voice );//voice num if_s_write( 0x0C, 0x54 );//vovol if_s_write( 0x0D, fnumh );//fnum if_s_write( 0x0E, fnuml );//fnum if_s_write( 0x0F, keyTone );//keyon = 1 } void keyoffVn(unsigned char voice){ if_s_write( 0x0B, voice );//voice num if_s_write( 0x0F, 0x00 );//keyon = 0 } void set_tone(){ int const COUNT=TONES*30+5; byte tone_data[COUNT]; tone_data[0]=0x80+TONES;//Header for(int i=0;i<TONES;i++){ tone_data[1+i*30]=BO[i] & 0x03; tone_data[2+i*30]=(LFO[i]<<6)|(ALG[i]&0x07); tone_data[3+i*30]=(SR[i][0]<<4)|(XOF[i][0]<<3)|KSR[i][0]; tone_data[10+i*30]=(SR[i][1]<<4)|(XOF[i][1]<<3)|KSR[i][1]; tone_data[17+i*30]=(SR[i][2]<<4)|(XOF[i][2]<<3)|KSR[i][2]; tone_data[24+i*30]=(SR[i][3]<<4)|(XOF[i][3]<<3)|KSR[i][3]; tone_data[4+i*30]=(RR[i][0]<<4)|DR[i][0]; tone_data[11+i*30]=(RR[i][1]<<4)|DR[i][1]; tone_data[18+i*30]=(RR[i][2]<<4)|DR[i][2]; tone_data[25+i*30]=(RR[i][3]<<4)|DR[i][3]; tone_data[5+i*30]=(AR[i][0]<<4)|SL[i][0]; tone_data[12+i*30]=(AR[i][1]<<4)|SL[i][1]; tone_data[19+i*30]=(AR[i][2]<<4)|SL[i][2]; tone_data[26+i*30]=(AR[i][3]<<4)|SL[i][3]; tone_data[6+i*30]=(TL[i][0]<<2)|KSL[i][0]; tone_data[13+i*30]=(TL[i][1]<<2)|KSL[i][1]; tone_data[20+i*30]=(TL[i][2]<<2)|KSL[i][2]; tone_data[27+i*30]=(TL[i][3]<<2)|KSL[i][3]; tone_data[7+i*30]=(DAM[i][0]<<5)|(EAM[i][0]<<4)|(DVB[i][0]<<1)|EVB[i][0]; tone_data[14+i*30]=(DAM[i][1]<<5)|(EAM[i][1]<<4)|(DVB[i][1]<<1)|EVB[i][1]; tone_data[21+i*30]=(DAM[i][2]<<5)|(EAM[i][2]<<4)|(DVB[i][2]<<1)|EVB[i][2]; tone_data[28+i*30]=(DAM[i][3]<<5)|(EAM[i][3]<<4)|(DVB[i][3]<<1)|EVB[i][3]; tone_data[8+i*30]=(MULTI[i][0]<<4)|DT[i][0]; tone_data[15+i*30]=(MULTI[i][1]<<4)|DT[i][1]; tone_data[22+i*30]=(MULTI[i][2]<<4)|DT[i][2]; tone_data[29+i*30]=(MULTI[i][3]<<4)|DT[i][3]; tone_data[9+i*30]=(WS[i][0]<<3)|FB[i][0]; tone_data[16+i*30]=(WS[i][1]<<3)|FB[i][1]; tone_data[23+i*30]=(WS[i][2]<<3)|FB[i][2]; tone_data[30+i*30]=(WS[i][3]<<3)|FB[i][3]; } tone_data[1+TONES*30]=0x80;//Footer tone_data[2+TONES*30]=0x03; tone_data[3+TONES*30]=0x81; tone_data[4+TONES*30]=0x80; if_s_write( 0x08, 0xF6 ); delay(1); if_s_write( 0x08, 0x00 ); if_write( 0x07, &tone_data[0], TONES*30+5 );//write to FIFO } void if_write(char addr,unsigned char* data,char num){ char i; char snd; set_ss_pin(LOW); spi_.transfer(addr); for(i=0;i<num;i++){ spi_.transfer(data[i]); } set_ss_pin(HIGH); } void if_s_write(char addr,unsigned char data){ if_write(addr,&data,1); } unsigned char if_s_read(char addr){ unsigned char rcv; set_ss_pin(LOW); spi_.transfer(0x80|addr); rcv = spi_.transfer(0x00); set_ss_pin(HIGH); return rcv; } |
(180213)