This program generates sound by using FM tone generator YMF825 via SPI.
fmsd1_micro.cpp@0:c54d59d6fb78, 2018-01-05 (annotated)
- Committer:
- hasebems
- Date:
- Fri Jan 05 22:58:49 2018 +0000
- Revision:
- 0:c54d59d6fb78
???????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hasebems | 0:c54d59d6fb78 | 1 | #include "MicroBit.h" |
hasebems | 0:c54d59d6fb78 | 2 | |
hasebems | 0:c54d59d6fb78 | 3 | SPI ymf825(MOSI, MISO, SCK); |
hasebems | 0:c54d59d6fb78 | 4 | MicroBitPin ss(MICROBIT_ID_IO_P16,MICROBIT_PIN_P16,PIN_CAPABILITY_DIGITAL); |
hasebems | 0:c54d59d6fb78 | 5 | MicroBitPin rst(MICROBIT_ID_IO_P8,MICROBIT_PIN_P8,PIN_CAPABILITY_DIGITAL); |
hasebems | 0:c54d59d6fb78 | 6 | |
hasebems | 0:c54d59d6fb78 | 7 | //extern MicroBit uBit; |
hasebems | 0:c54d59d6fb78 | 8 | //#define ss uBit.io.P16 |
hasebems | 0:c54d59d6fb78 | 9 | //#define rst uBit.io.P8 |
hasebems | 0:c54d59d6fb78 | 10 | |
hasebems | 0:c54d59d6fb78 | 11 | void writeSingleCPP( unsigned char adrs, unsigned char data ) |
hasebems | 0:c54d59d6fb78 | 12 | { |
hasebems | 0:c54d59d6fb78 | 13 | ss.setDigitalValue(0); // select slave |
hasebems | 0:c54d59d6fb78 | 14 | ymf825.write(adrs); |
hasebems | 0:c54d59d6fb78 | 15 | ymf825.write(data); |
hasebems | 0:c54d59d6fb78 | 16 | ss.setDigitalValue(1); |
hasebems | 0:c54d59d6fb78 | 17 | wait_ms(1); |
hasebems | 0:c54d59d6fb78 | 18 | } |
hasebems | 0:c54d59d6fb78 | 19 | extern "C" void writeSingle( unsigned char adrs, unsigned char data ){ writeSingleCPP(adrs,data);} |
hasebems | 0:c54d59d6fb78 | 20 | extern "C" void writeBurst( unsigned char adrs, unsigned char* data, int count ) |
hasebems | 0:c54d59d6fb78 | 21 | { |
hasebems | 0:c54d59d6fb78 | 22 | ss.setDigitalValue(0); |
hasebems | 0:c54d59d6fb78 | 23 | ymf825.write(adrs); |
hasebems | 0:c54d59d6fb78 | 24 | for (int i = 0; i<count; i++) { |
hasebems | 0:c54d59d6fb78 | 25 | ymf825.write(*(data + i)); |
hasebems | 0:c54d59d6fb78 | 26 | } |
hasebems | 0:c54d59d6fb78 | 27 | ss.setDigitalValue(1); |
hasebems | 0:c54d59d6fb78 | 28 | wait_ms(1); |
hasebems | 0:c54d59d6fb78 | 29 | } |
hasebems | 0:c54d59d6fb78 | 30 | extern "C" void delayMs( int ms ){ wait_ms(ms);} |
hasebems | 0:c54d59d6fb78 | 31 | |
hasebems | 0:c54d59d6fb78 | 32 | void initSPI(void) |
hasebems | 0:c54d59d6fb78 | 33 | { |
hasebems | 0:c54d59d6fb78 | 34 | // init SPI |
hasebems | 0:c54d59d6fb78 | 35 | ymf825.format(8); // mode=0 |
hasebems | 0:c54d59d6fb78 | 36 | ymf825.frequency(4000000); |
hasebems | 0:c54d59d6fb78 | 37 | |
hasebems | 0:c54d59d6fb78 | 38 | } |
hasebems | 0:c54d59d6fb78 | 39 | void initSD1( void ) |
hasebems | 0:c54d59d6fb78 | 40 | { |
hasebems | 0:c54d59d6fb78 | 41 | // 1. powerOnReset( void ); |
hasebems | 0:c54d59d6fb78 | 42 | rst.setDigitalValue(0); |
hasebems | 0:c54d59d6fb78 | 43 | |
hasebems | 0:c54d59d6fb78 | 44 | // 2. wait 100usec |
hasebems | 0:c54d59d6fb78 | 45 | wait_us(100); |
hasebems | 0:c54d59d6fb78 | 46 | |
hasebems | 0:c54d59d6fb78 | 47 | // 3. RST_N : high |
hasebems | 0:c54d59d6fb78 | 48 | rst.setDigitalValue(1); |
hasebems | 0:c54d59d6fb78 | 49 | |
hasebems | 0:c54d59d6fb78 | 50 | // 4. DRV_SEL : low |
hasebems | 0:c54d59d6fb78 | 51 | writeSingleCPP(29, 1); // 3.3v |
hasebems | 0:c54d59d6fb78 | 52 | |
hasebems | 0:c54d59d6fb78 | 53 | // 5. AP0 : 0 |
hasebems | 0:c54d59d6fb78 | 54 | writeSingleCPP(2, 0x0e); |
hasebems | 0:c54d59d6fb78 | 55 | |
hasebems | 0:c54d59d6fb78 | 56 | // 6. wait for Quarz stability |
hasebems | 0:c54d59d6fb78 | 57 | wait_ms(1); |
hasebems | 0:c54d59d6fb78 | 58 | |
hasebems | 0:c54d59d6fb78 | 59 | // 7. CLKE : "1" |
hasebems | 0:c54d59d6fb78 | 60 | writeSingleCPP(0, 0x01); |
hasebems | 0:c54d59d6fb78 | 61 | |
hasebems | 0:c54d59d6fb78 | 62 | // 8. ALRST : low |
hasebems | 0:c54d59d6fb78 | 63 | writeSingleCPP(1, 0x00); |
hasebems | 0:c54d59d6fb78 | 64 | |
hasebems | 0:c54d59d6fb78 | 65 | // 9. SFTRST : 0xa3 |
hasebems | 0:c54d59d6fb78 | 66 | writeSingleCPP(26, 0xa3); |
hasebems | 0:c54d59d6fb78 | 67 | wait_ms(1); |
hasebems | 0:c54d59d6fb78 | 68 | |
hasebems | 0:c54d59d6fb78 | 69 | // 10. SFTRST : 0x00 |
hasebems | 0:c54d59d6fb78 | 70 | writeSingleCPP(26, 0x00); |
hasebems | 0:c54d59d6fb78 | 71 | |
hasebems | 0:c54d59d6fb78 | 72 | // 11. wait 30msec |
hasebems | 0:c54d59d6fb78 | 73 | wait_ms(30); |
hasebems | 0:c54d59d6fb78 | 74 | |
hasebems | 0:c54d59d6fb78 | 75 | // 12. AP1 AP3: "0" |
hasebems | 0:c54d59d6fb78 | 76 | writeSingleCPP(2, 0x04); |
hasebems | 0:c54d59d6fb78 | 77 | |
hasebems | 0:c54d59d6fb78 | 78 | // 13. wait 10usec |
hasebems | 0:c54d59d6fb78 | 79 | wait_us(10); |
hasebems | 0:c54d59d6fb78 | 80 | |
hasebems | 0:c54d59d6fb78 | 81 | // 14. AP2: "0" |
hasebems | 0:c54d59d6fb78 | 82 | writeSingleCPP(2, 0x00); |
hasebems | 0:c54d59d6fb78 | 83 | |
hasebems | 0:c54d59d6fb78 | 84 | writeSingleCPP(25, 0xcc); // -32[dB] |
hasebems | 0:c54d59d6fb78 | 85 | writeSingleCPP(27, 0x3f); // set itp max |
hasebems | 0:c54d59d6fb78 | 86 | writeSingleCPP(20, 0x00); // set itp on |
hasebems | 0:c54d59d6fb78 | 87 | writeSingleCPP(3, 0x01); // amp gain(6.5[dB]) |
hasebems | 0:c54d59d6fb78 | 88 | writeSingleCPP(9, 0xf8); // Sequencer Volume |
hasebems | 0:c54d59d6fb78 | 89 | } |