Logic level converter “a la mano” !

Like i said in a previous post (this one), i need a way to convert power from 5.5V to 3.3V in order to connect the Arduino to the 23K256 SPI Ram chip.

Because i don’t know nearly anything in electric engineering , i decided to use a pre existing solution: the 4-Channel Bi-directional Logic Level Converter¬†.

One great things with all the product gravitating around Arduino is that most of them are Open Sourced, this mean that the schematic of the Logic Level Converter is freely available for anybody: here !

But basically here is the electronic setup for a one way bi directional logic level converter:

viewer

 

What i can see is that the setup is not very complex, just two 10k resistors and one BSS138 Mosfet transistor.

I do not know why and how it works, but the things is that it WORKS and that is enough for me ūüôā

Should i switch to Arduino Mega ?

I’m facing some troubles related with something i did not anticipate:

Program Size !

Arduino Uno Flash Memory is smaaalll !

As you may know, the ATmega328p use a “Harvard architecture”. This mean that program memory is separated from data memory.

Concerning the data memory, i already knew that the 2k bytes available on the Arduino Uno platform were not going to be enough for this project. That’s was why i anticipated that, and investigate a solution based upon the 23k256 chip (see this post).

But what i did not anticipated is that the program memory was also going to be too small. The Flash memory that contain program run by the Arduino Uno is 32k bytes:

  • 5k bytes are already used by the Arduino boot loader.
  • Linking and using the avr-libc + the Arduino Core Library also consume several kilo bytes
  • Linking and using the SPI + MIDI library + SPIRAM library + LiquidCrystal Library also consume several kilo bytes

The only thing, is that now, after writing some code for the MIDILooper itself, i only have less than 6k bytes available for all the rest of the code

I can’t be sure for ¬†now, but it may not be enough to store the entire program…

There is no way to extend the Flash memory, so i may switch from Arduino Uno to Arduino Mega, which contain 512k bytes of program memory…

Extending the Arduino SRAM

What’s the problem with memory ?

That’s a good question… The ATmega328p offer 2k bytes of SRAM which is… not a lot…So knowing that, i tried to estimate how many MIDI command i can store inside this SRAM, and the result is problematic:

After reading the MIDI specification, i can estimate that most of the MIDI messages i’m going to store during the recording of a sequence are 3 bytes long: For exemple, when a note is played, the corresponding message contain the follwing bytes:

  • 1 byte of status: containing¬†an ID representing the king of message, plus the MIDI channel ID
  • 2 bytes of data: containing the number the note (if it’s a C, a D or whatever) plus the velocity of the played note

So:
2k bytes / 3 bytes = 682 messages

The point is that for each played not there is (at least) 2 MIDI messages: one when the note start (MIDI Note on) and one when the note end (MIDI Note off).

So:
2k bytes / 3 bytes / 2 = 341 messages

But the estimation does not stop here… a MIDI message does not contain any timing informations (because they are supposed to be played in real time). If i want to playback those notes, i need to know WHEN to play them back, so i need to store an extra timing informations. To do that i’m going to follow what was already done in the MIDI file format.

A MIDI file contain a sequence of MIDI message and can be used to playback those messages/notes. In order to do that, extra data are added to each MIDI messages. These extra data are between 1 and 4 bytes long and store timing informations.

So if i assume this extra informations is always 4 bytes long, each MIDI message consume now: 3 bytes +4 bytes = 7 bytes. I will round it up to 8 bytes in case my application need to store some more extra data.

So:
2k bytes / 8 Bytes / 2 = 128 messages

An other thing to take care of, is that among those 2k SRAM available, the Arduino System itself will already use some memory. And the application itself will also use memory. Here are differents estimations:

System take 128 bytes:
(2048 bytes Р128 bytes ) / 8 bytes / 2 =  120 messages

System take 256 bytes:
(2048 bytes Р256 bytes ) / 8 bytes / 2 =  112 messages

System take 512 bytes:
(2048 bytes Р512 bytes ) / 8 bytes / 2 =  96 messages

System take 1024 bytes:
(2048 bytes Р1024 bytes ) / 8 bytes / 2 =  64 messages

So i can reasonably think that i can store between 120 and 64 messages.

One last thing to note, is that when playing, not just MIDI note start and stop are send, other messages including “pitch variation“, or “note preassure” are also send. I can’t know in advance how many of this kind of messages are send because it of course depend on the way you are playing… But let’s say that for each note played, one of this commands is send this reduce the global estimation of total note that can be played to 32… This is not a lot and this is just an approximation…

So in order to avoid any memory related problems, additional memory is required !

Once again, thanks to the Arduino and it’s community, solutions exists:

The microchip 23k256 SPI SRAM:

The 23k256 is a small chip that provide 32k bytes of extra SRAM and can be easily connected to the Arduino:

IMG_20131120_212514

Communication between this chip and the Arduino is done using SPI which is a serial communication protocol. (I will probably talk about the SPI protocol in an other post, but for now i will just talk about how to physicaly connect the chip and the Arduino ).

The tricky part here is that the 23k256 chip operate at 3.3V and the Arduino at 5V, so if i want to connect them safely, i must integrate some kind of voltage converter between the two devices…

The logic level voltage converter:

This kind of voltage converter can be done in very different ways, but, as i’m not an electronic specialist, i will use something that already exist: A 4 channel Bi-directional Logic Level Converter.

After reading the various specification for each devices, here is a first draft of how the wiring between the Arduino, the level converter and the 23k256, is going to be done…

I haven’t test it yet, but i think it should be ok ūüôā

How everything should be wired (At least i think….):

23k256 wiring

Selected technical solution

Arduino to the rescue !!

Computer sciences and programming are great, but meeting and discussing with real people is even better !!

That’s a friend of mine that talk to me the first time about the Arduino solution.

Okay, i’m making some advertising, but you really should check out the website of his team Akwariom, they are creating amazing light control and audio technical solutions for live shows. YOU RULES DUDES !

So for those who don’t know about Arduino, in short, it’s an opensource hardware solution for creating small electronic device that can control pretty much everything ranging from stepper motor’s control, sensor data extraction to MIDI processing !

There are many variation of the Arduino platform, but the most used one is the Arduino Uno.

The Arduino Uno platform is mainly two things:

  • An hardware platform build around an Atmel AVR (ATmega328p) processor which is an 8 bit Risc microcontroller
  • A software solution that simplify the development process on this platform using a simplified programming language and several wrappers library.

As i want to develop this project in pure C/C++, ¬†i’m going to use only the hardware platform, and develop my own tools for replacing the software solution, including the build toolchain and the wrappers library.

Arduino source code for the software solution and the wrapper library are available on GitHub:  https://github.com/arduino/Arduino

Get rid of the Hardware side !

As i’m not an hardware specialist, i’m always disappointed when it come to solder resistors… But thanks to the Arduino platform, lots of company have developed hardware extension slot (called shields) for the Arduino platform.

Among them, there is one, SparksFun that have developed an extension shield that provide all the necessary MIDI circuitry needed for reading MIDI data and sending MIDI data:

https://www.sparkfun.com/products/9598

It provide  3 MIDI connectors:

  • A MIDI IN which allow the Arduino to read MIDI data comming from an external device (in my case the Gaia SH01)
  • A MIDI OUT which allow the Arduino to send MIDI data to external device (again in my case, the GAIA SH01)
  • A MIDI THRU which is often present in MIDI powered device and which duplicate what come in the MIDI IN connector

Sounds like a great start no ? !

Nothing to solder and i like it !

Here is a picture of both the Arduino Uno and the MIDI Shield:

IMG_20131118_222544