MIDI->STM32->Beeb->Music 5000

for bbc micro/electron hardware, peripherals & programming issues (NOT emulators!)
Post Reply
User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

MIDI->STM32->Beeb->Music 5000

Post by danielj » Fri May 17, 2019 10:26 pm

So,
Some might have spotted this on the facebook group, but I've been playing around with an STM32F446 Nucleo board with a view to creating a very very cheap MIDI->Music 4000 converter. I've posted a couple of videos...
1) Demonstrating that the STM32 uC can actually pretend to be a Music 4000:
https://youtu.be/QCZ0QqFA-qM

2) Demonstrating that it can deal with a MIDI input and use that to pretend to be a Music 4000:
https://youtu.be/mI7FPRjFH3A

Now, it was misbehaving slightly in that second video (sticky notes) - this was mainly due to it missing key offs in the midi serial stream if an interrupt got in the way. Anyway, I've now fixed that with an interrupt filled ring-buffer and it seems to behave (after having liberally scattered the word volatile across the source-code).

Anyway, the upshot is, the entire thing can be done in the one uC which means any hardware is only going to need that, an opto isolator and voltage regs (plus a 74HC14 if you want through). Added bonus is that USB-midi should be relatively easy to implement too :)

I've stuck the code up on my github page (GPL 3.0): https://github.com/drdpj/MidiKeyboard - feel free to have a play - you'll need an optoisolator to take the midi into the nucleo board. I'm going to now try and getting going on the STM103 which is rather cheaper (no reason why it shouldn't) with a view to pushing out a board design. They will definitely not end up being very expensive. I'm not really intending to make (m)any of these, I suspect I'll do a few simply as I'll have ordered a batch of 10 pcbs and I really don't need 10! but the board will be open source hardware so people will be free to re-jiggle as they see fit. Watch this space :D

All constructive comments welcome!

Edit: and here's a video of it finally behaving: https://youtu.be/kdLP8Cr9hn0
Last edited by danielj on Sat May 18, 2019 12:30 pm, edited 1 time in total.

User avatar
IanS
Posts: 919
Joined: Mon Aug 31, 2009 6:02 pm
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by IanS » Sat May 18, 2019 10:02 am

Looks good.

Is this like the one here - viewtopic.php?f=3&t=16893
But keeping track of each key pressed in software, rather than having a hardware latch for every key? (and Open-source)

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sat May 18, 2019 10:08 am

Yes, basically does the same, but less parts and is (and will be) an open design. The keyboard matrix is held in an array, the STM32 has hardware peripherals that can deal with the signals from the user port as it scans the keyboard matrix (it's basically SPI), and dealing with the MIDI is just a question of purposing one one of the UARTS. Interrupt on SPI to display the right byte from the keyboard array on GPIOs, interrupt on the UART to dump bytes into buffer, and the rest is gravy.

d.

User avatar
Elminster
Posts: 3541
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by Elminster » Sat May 18, 2019 11:18 am

Daniel's is 'design #4' in the hardware list, but now you made me realise I forgot to put the Hideaway Studio in the hardware list. People just have too much time, they keep inventing stuff.

Edit: Added and pushed to github
Last edited by Elminster on Sat May 18, 2019 11:26 am, edited 1 time in total.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sat May 18, 2019 3:12 pm

There are a couple of acknowledgements due here :D Chris "BooBip" M. who nudged me firmly towards the ST embedded ARM platform, answered numerous stupid questions, provided reams of expert advice and said of my logic analyser trace of the user port output "that looks exactly like SPI...", and also Simon "Domesday" I. who also has answered a number of dumb questions and musings. It's a pleasure to toy around with building things for the Acorn systems as everyone is just so darn helpful!

d.

jasonl
Posts: 35
Joined: Wed Mar 27, 2019 9:22 am
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by jasonl » Sat May 18, 2019 9:09 pm

That's really cool. =D> I'm not familiar with the Nucleo. I tried implementing something similar using a Teensy LC microcontroller a couple of weeks ago (because I've got a few) - but it isn't able to respond to the keyboard scanning fast enough on its own.

In the end I made the Midi controller using 8x 74HC595 shift registers to store the keyboard state, updated using SPI from the Teensy. The registers are then enabled in turn by a 74HCT164 shift register connected to the Beeb's user port, as in the original M4000 controller. I got a small PCB made for my DIY Hybrid system. Just added the Midi code and footswitch handling today which works quite nicely. Then I saw your post. :D

Interestingly I notice the Hybrid music software only appears to react to the 49 keys of the 4 octaves of the M4000, though the keyboard controller should allow for 62 keys. And although there are two footswitches allowed for, the Hybrid software treats them the same - as a sustain pedal in the keyboard program. I've not explored the other modules yet.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sat May 18, 2019 10:10 pm

Good work :) - I'm fairly sure that's approximately the same as Dan's interface (although I think he's using an AVR by the looks of things to do the MIDI)? It's a shame the teensy didn't work - were you using a similar arrangement with the SPI input on interrupts?

The nucleo is pretty cheap for prototyping - around the £10 mark. It's basically an STM32something with an STLINK built in. The one I'm using runs at 86MHz. If I can get it to work on the 103 version (72MHz), I'll see if I can get it to play nicely on a bluepill board, which is also a 103. - those come in at about £4/pop. I would like to design a nice little PCB though, which is under 10x10cm for maximum cheapness at JLCPCB :)

I was surprised it didn't deal with more keys - I guess they just hard coded it :(

User avatar
Elminster
Posts: 3541
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by Elminster » Sat May 18, 2019 10:47 pm

deisgn #5 ?

Definitely going Music n000 crazy this month.

jasonl
Posts: 35
Joined: Wed Mar 27, 2019 9:22 am
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by jasonl » Sat May 18, 2019 10:48 pm

danielj wrote:
Sat May 18, 2019 10:10 pm
Good work :) - I'm fairly sure that's approximately the same as Dan's interface (although I think he's using an AVR by the looks of things to do the MIDI)? It's a shame the teensy didn't work - were you using a similar arrangement with the SPI input on interrupts?

The nucleo is pretty cheap for prototyping - around the £10 mark. It's basically an STM32something with an STLINK built in. The one I'm using runs at 86MHz. If I can get it to work on the 103 version (72MHz), I'll see if I can get it to play nicely on a bluepill board, which is also a 103. - those come in at about £4/pop. I would like to design a nice little PCB though, which is under 10x10cm for maximum cheapness at JLCPCB :)

I was surprised it didn't deal with more keys - I guess they just hard coded it :(
Thanks. The Arduino SPI library I was using can't act as a slave device, so I was using a pin interrupt to read the userport CB1 and CB2 pins. It was able to cope with that, but couldn't write the byte of data (using port manipulation) of the keyboard status fast enough (i.e. within 1uS). Reading the VIA datasheet helped understand what's going on. It's similar to SPI, but is described as a shift register which latches the userport state once the byte is clocked out.

Using the 595 shift registers frees the Teensy to deal with the decoding of Midi and other stuff, at the cost of a few more parts, and it simplified the programming a lot. I've underclocked the Teensy LC at 24Mhz. Somehow it seems wrong to use a uC that's a arguably more powerful than the host Beeb just as a keyboard controller. Hey ho. :wink: My PCB (another JLCPCB fan) is 10cm x 7cm, using all through-hole parts and the Teensy.

I understand Dan is using a PIC microcontroller, and some latched buffers. Whatever works. :)

Good to have clarification about the 4 octave limitation. I wonder if that's hackable...

BrokenARM
Posts: 33
Joined: Sat Jul 22, 2017 11:07 am
Location: UK
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by BrokenARM » Sat May 18, 2019 11:30 pm

I am indeed using a PIC clocked at 20MHz which has been my go-to controller for MIDI applications for many years now. My interface may have a few more parts (although none are expensive) but it certainly responds very spritely to even the most intense of midi thrown at it.

I'm not convinced extending the range beyond 4 octaves is worthwhile - especially in the upper registers where I have found the Music 5000 seems to produce all manner of weird intermodulation effects causing a whole raft of nasty harmonics (which leads me to a theory on why the anti-aliasing filter was rather heavy handed on these little wonders).
Last edited by BrokenARM on Sat May 18, 2019 11:32 pm, edited 2 times in total.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sat May 25, 2019 6:21 pm

Just a wee update. The github repository now holds a version of the code for the STM32F446 that seems to hold up perfectly. It's been written using the HAL, so it's rejiggleable for other STM32F446 dev boards besides the nucleo.

I'm just finalising a version that runs on the STM32F103, so can be used with the bluepill boards (it works, just shifting the GPIOs about now):
DSC_0338.JPG
I should make that repo available tomorrow :) After that I'll get a bit of hardware designed that brings all the elements together :D.
d.
Last edited by danielj on Sat May 25, 2019 6:21 pm, edited 1 time in total.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun May 26, 2019 11:28 am

Right, this is ripe for people to fiddle with if they see fit:
https://github.com/drdpj/MidiKeyboardBluePill

By far the cheapest way to do it is to get hold of one of the bluepill boards and download the software from the repository onto it. Pinouts are described in the code, but for completeness:

Code: Select all

User Port : STM32F103

2     :    PB3
4     :    PB5
6     :    PB6
8     :    PB7
10    :    PB8
12    :    PB9
14    :    PB10
16    :    PB11
18    :    PB12
20    :    PB13

Connect the MIDI serial (via optocoupler at 3.3V) to PA3
Last edited by danielj on Sun May 26, 2019 11:29 am, edited 1 time in total.

RobC
Posts: 2624
Joined: Sat Sep 01, 2007 9:41 pm
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by RobC » Sun May 26, 2019 12:37 pm

This is fantastic - will definitely be building one when I've got a bit more time.

Many thanks Daniel :D

User avatar
fordp
Posts: 1016
Joined: Sun Feb 12, 2012 9:08 pm
Location: Kent, England
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by fordp » Sun May 26, 2019 6:28 pm

danielj wrote:
Sun May 26, 2019 11:28 am
Right, this is ripe for people to fiddle with if they see fit:
https://github.com/drdpj/MidiKeyboardBluePill
[/code]
Bravo. I just had a look at the code it looks interesting.
Some constructive feedback however is there seems to be mixture of tabs and spaces used for indenting, which is not a big deal in itself but the tabs do not show up correctly in github if you do not add a .editorconfig file to the root of the repository.

I am a bit of a Midi nut and write software for STM32's for a a living so this is great project for me!

Thanks for sharing!
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun May 26, 2019 7:45 pm

fordp wrote:
Sun May 26, 2019 6:28 pm
Bravo. I just had a look at the code it looks interesting.
Some constructive feedback however is there seems to be mixture of tabs and spaces used for indenting, which is not a big deal in itself but the tabs do not show up correctly in github if you do not add a .editorconfig file to the root of the repository.

I am a bit of a Midi nut and write software for STM32's for a a living so this is great project for me!

Thanks for sharing!
Thanks Simon - I'll see if I can tidy that up (it's probably because I was using VS2017 to edit the files that were produced by CubeMX). Just out of interest, it *sometimes* seems to glitch, in that the SPI interrupt (the one that has the switch based on the SPI from the Beeb and sets up the ODA on port B) ends up offset on the keyboard array by 1 byte (and reads beyond the end of the array). It happens spontaneously, and seemingly randomly (albeit very rarely) and I honestly can't see any reason for it!? As an ARM embedded novice, any pointers are gratefully received!

d.

jasonl
Posts: 35
Joined: Wed Mar 27, 2019 9:22 am
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by jasonl » Sun May 26, 2019 9:22 pm

This is cool. I've ordered a couple of the Blue Pill boards and look forward to setting up a development environment on my Linux system to try it out... I've only used the Arduino environment until now.
Does your code deal with Midi running status? Had to deal with this with my own midi decoding code otherwise it would miss notes and sometimes them hanging. :wink:

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun May 26, 2019 9:42 pm

Yes, it should deal with running status. If it gets a command byte, it sets up the data structure to expect a given number of bytes of data depending on what the command was. Once the block has all its data, it's marked as completed and processed and then it resets and can refill with data. The main loop will keep doing this until it sees another command byte.

User avatar
fordp
Posts: 1016
Joined: Sun Feb 12, 2012 9:08 pm
Location: Kent, England
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by fordp » Sun May 26, 2019 10:50 pm

danielj wrote:
Sun May 26, 2019 7:45 pm

Thanks Simon - I'll see if I can tidy that up (it's probably because I was using VS2017 to edit the files that were produced by CubeMX). Just out of interest, it *sometimes* seems to glitch, in that the SPI interrupt (the one that has the switch based on the SPI from the Beeb and sets up the ODA on port B) ends up offset on the keyboard array by 1 byte (and reads beyond the end of the array). It happens spontaneously, and seemingly randomly (albeit very rarely) and I honestly can't see any reason for it!? As an ARM embedded novice, any pointers are gratefully received!

d.
I will have a go at helping in due course when I have some hardware.
Off the top of my head the one off issues sounds like a "threading/timing" issue.
FordP (Simon Ellwood)
Time is an illusion. Lunchtime, doubly so!

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun May 26, 2019 10:58 pm

fordp wrote:
Sun May 26, 2019 10:50 pm
I will have a go at helping in due course when I have some hardware.
Off the top of my head the one off issues sounds like a "threading/timing" issue.
Thanks Simon - I wonder if I should just whack that interrupt up to the highest priority and demote some other things.... The strange thing is that it results in a "permanent" offset on the keyboard...

jasonl
Posts: 35
Joined: Wed Mar 27, 2019 9:22 am
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by jasonl » Mon May 27, 2019 7:14 am

danielj wrote:
Sun May 26, 2019 9:42 pm
Yes, it should deal with running status. If it gets a command byte, it sets up the data structure to expect a given number of bytes of data depending on what the command was. Once the block has all its data, it's marked as completed and processed and then it resets and can refill with data. The main loop will keep doing this until it sees another command byte.
:D Great! Thanks for the description. I used a state machine to do the same thing.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Mon May 27, 2019 7:39 am

jasonl wrote:
Mon May 27, 2019 7:14 am
:D Great! Thanks for the description. I used a state machine to do the same thing.
And thus my lack of computer science training shows up :D - I had to go and look that up!

Hopefully there're enough comments in the code to work out what it's doing. It was a bit of a learning curve, and I've probably not organised things very well simply as I've never done any C development in anger, let alone professional capacity, only twiddled with other people's code (again, all comments as to preferable ways of doing things are very welcome).

In the bells-and-whistles department, it currently just responds to all channels. It could either be hard-coded to a single channel, or you could even make it selectable with a button to cycle through channels and display which one's selected on a nice little 7-segment display :D. I'm just in the process of putting a board together, so I'll leave some headers on for things like that.

Just out of interest, I notice both you and Dan seem to have put a remote foot-switch(?) socket on your boards? Is there a good reason for that?

d.

User avatar
Elminster
Posts: 3541
Joined: Wed Jun 20, 2012 8:09 am
Location: Essex, UK
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by Elminster » Mon May 27, 2019 8:28 am

Good job as usual. I need some one to invent something that gives me music talent now. :)

jasonl
Posts: 35
Joined: Wed Mar 27, 2019 9:22 am
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by jasonl » Mon May 27, 2019 8:02 pm

danielj wrote:
Mon May 27, 2019 7:39 am
jasonl wrote:
Mon May 27, 2019 7:14 am
:D Great! Thanks for the description. I used a state machine to do the same thing.
And thus my lack of computer science training shows up :D - I had to go and look that up!

Hopefully there're enough comments in the code to work out what it's doing. It was a bit of a learning curve, and I've probably not organised things very well simply as I've never done any C development in anger, let alone professional capacity, only twiddled with other people's code (again, all comments as to preferable ways of doing things are very welcome).

In the bells-and-whistles department, it currently just responds to all channels. It could either be hard-coded to a single channel, or you could even make it selectable with a button to cycle through channels and display which one's selected on a nice little 7-segment display :D. I'm just in the process of putting a board together, so I'll leave some headers on for things like that.

Just out of interest, I notice both you and Dan seem to have put a remote foot-switch(?) socket on your boards? Is there a good reason for that?

d.
State machines are cool. 8) I repurposed some code I wrote a while ago for another Midi adapter for a synth with a similar on-off note style, and the Arduino Midi library at the time was giving me problems.

To solve the channel problem, I use a pushbutton that's held down for 5 seconds to put it into a learning mode - then the next note played sets the channel. Saves having to have a display. Though I do use an activity LED that lights up when a key is pressed. :)

As for the footswitch - that's used to trigger sustain in the M5000 software, so I put a jack socket in for one as the M4000 schematic. Athough it allows for 2 switches, but they both appear to do the same thing in the application. It was also fun to use the sustain while playing a track via the PC, and allows a limited form of expressive control. Having the switch connected to the adapter allows for that... Though I think it makes sense also to have it respond via Midi as your code does.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Mon May 27, 2019 8:27 pm

Gotcha :)

I've just noted a bit of a clanger - the MOSI pin it's using isn't 5V tolerant... :'(

Some form of shiftyness needs to be stuck in there as the other port isn't really usable in a sensible fashion...

d.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Wed May 29, 2019 7:41 pm

RIGHT, the repository has been updated: https://github.com/drdpj/MidiKeyboardBl ... g/0.2-beta

Not all pins from the beeb were 5V tolerant (argh), but they are now. And I've pinned the glitchyness down to a missing pullup on CB1. That's there now, the clock behaves properly, and it all works reliably - hooray! :)

Big thanks to Chris M again for sage council :D

d.

edit: and some silly: https://www.youtube.com/watch?v=mmdlBsHJXlo

:)
Last edited by danielj on Wed May 29, 2019 8:06 pm, edited 1 time in total.

User avatar
pixelblip
Posts: 1246
Joined: Wed Feb 04, 2015 7:19 pm
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by pixelblip » Fri May 31, 2019 10:59 am

It's amazing the way you all have got this working.

cmorley
Posts: 886
Joined: Sat Jul 30, 2016 7:11 pm
Location: Oxford
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by cmorley » Fri May 31, 2019 4:18 pm

danielj wrote:
Wed May 29, 2019 7:41 pm
Big thanks to Chris M again for sage council :D
:lol:

... later. much later ...

:lol:

I'm glad you've got it working :D
Last edited by cmorley on Fri May 31, 2019 4:21 pm, edited 1 time in total.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun Jun 09, 2019 7:35 am

Another wee update:
https://github.com/drdpj/MidiKeyboardBl ... /v0.3-beta

This is probably going to be the final version until it gets tested by more people with more things. I haven't been able to break it myself.

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Sun Jun 23, 2019 3:37 pm

Righty, I've schematerised this and have a PCB layout done that'll fit into a £2.50 enclosure. It passes the ERC, but I'm posting it here just to check that I've not done anything completely daft before I ask JLC to make some :)


d.
Attachments
Music4000-2-midi.png

User avatar
danielj
Posts: 7304
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: MIDI->STM32->Beeb->Music 5000

Post by danielj » Wed Jul 17, 2019 9:41 pm

Github updated this evening, I've resized the PCB to fit a neat little box:
https://github.com/drdpj/MidiKeyboardBluePill

PCBs ordered. :D

d.

Post Reply