Lab D Lecture Slides - UMass Amherst ECE 353

ECE 353 Lab D
General MIDI Explorer
Instructor and UGA Presentation,
and Demo
Professor Sandip Kundu
Fall 2014
Lab D
General MIDI Explorer
with Record/Playback
Getting Started
ECE353: 2
Computer Systems Lab 1
Kundu
Where Are We?
Lab A
Cache Simulator in C
Lab B
MIDI Receiver
Lab C
Pipelined Machine
• C programming, data structures
• Cache architecture and analysis
• Hardware design
• Proper Verilog Coding
• Functional Simulation, GoLogic, MIDI-Ox
• C programming, data structures
• Pipeline architecture and analysis
Lab D General MIDI Explorer with Record/Playback
• Microcontroller programming
• C and Assembly (optional)
ECE353: 3
Computer Systems Lab 1
Kundu
Perspective
• As an engineer you often have multiple choices for
implementation
• What you do will depend on performance and cost
requirements
• Use a CPLD, FPGA, ASIC/microprocessor, or
microcontroller
• Write it in Verilog (CPLD, FPGA, ASIC) => Lab B
• Write it in C or ASM => Lab D
ECE353: 4
Computer Systems Lab 1
Kundu
When to use: PLDs, FPGAs, ASICs, Structured ASICs
• Programmable logic devices (PLDs) provide low/medium density
logic.
• Field-programmable gate arrays (FPGAs) provide more logic and
multi-level logic. Advanced peripheral support.
• Application-specific integrated circuits (ASICs) are manufactured for a
single purpose. Typically built with standard cells implementing gates.
Higher performance vs. FPGAs.
• Structured ASICs are in between FPGAs and ASIC in density &
performance. Typically wire together cells that are pre-built and exist
in island configurations.
ECE353: 5
Computer Systems Lab 1
Kundu
Differences between microcontrollers, microprocessors
and FPGA systems for running software

FPGA systems often contain CPUs
in softcore (synthesized) or
hardcore (part of die) format but can
also contain logic blocks for other
hardware, e.g., state machines, etc
•

Tx
Rx
Discrete
PHY
45-65 nm FPGAs (Virtex 5-7) can
achieve high performance.
Microcontrollers are more limited in
functionality and often do not
include support for virtual memory
and caches
•

Soft core
Up to 50MHz
Microprocessors are higher
performance capable and have
typically virtual memory support
•
ECE353: 6
From 50MHz to GHz
Computer Systems Lab 1
Tx
Rx
Hard core with builtin Transceivers
Kundu
Lab D Objectives
• Exposure to microcontroller programming vs.
design of hardware
• Compiled Language (C)
• Data Structure in Memory
• Talking to UART, Timer
• Assembly Language (ASM) - optional
• Talking to A/D converter, Timer, UART
• Continuation of MIDI theme
• Serial communication
• Notes and instruments
• Complete system with MIDI – FUN! with input, output,
storage
ECE353: 7
Computer Systems Lab 1
Kundu
Lab D Requirements
• Part 1: C Program
• Send & Receive MIDI Messages
• Play and Recording Modes
• Store and Replay MIDI Messages
• Part 2: ASM Program (optional)
• Vary speed of messages via hex switch
• Instrument / Note / Velocity via photo cells
• Change channels between Percussion & Instrument
ECE353: 8
Computer Systems Lab 1
Kundu
AVR Components Used
• In Part 1 (C Program)
• Data EEPROM (Storage)
• USART (Communication)
• Timer1 (Counter)
• In Part 2 (ASM Program)
•
•
•
•
ECE353: 9
Timer1 (Counter)
USART (Communication)
ADC (Analog to Digital Converter)
Will manipulate these components using AVR assembly.
Computer Systems Lab 1
Kundu
ATmega32 AVR
•
•
•
•
•
•
•
•
•
•
16MHz CMOS 8-bit microcontroller
with AVR RISC instruction set
32 general purpose registers
32KB of Flash
2Kb internal SRAM
1KB E2PROM – 100,000 erase
cycles
8 and 16 bit counters
USART
8-bit ADC
JTAG
….
ECE353: 10
Computer Systems Lab 1
Kundu
Board Demonstration
ECE353: 11
Computer Systems Lab 1
Kundu
Wired Board
ECE353: 12
Computer Systems Lab 1
Kundu
Part 1 – C Program
•
Develop, program AVR through JTAG, for:
•
Getting MIDI messages through the Midi OX
from the PC serially
•
Similar to Lab B but using AVR Studio
and AVR gcc compiler – to receive
Input
Play Switch
Input
Record Switch
ATMega32
EEPROM
DATA
USART
To PC
Transmit
From PC
Receive
C Program Block Diagram
ECE353: 13
Computer Systems Lab 1
Kundu
Part 1 – C Program contd.
•
Once received in AVR, store in E2PROM if switch
set to Record
•
Compress/decompress before/after
storing - you can use any algorithm you
find suitable
•
Record timing also so that you can replay
exactly the same way
•
If switch set to Playback, play back
Input
Play Switch
Input
Record Switch
•
Incoming signal optically isolated from PC
ATMega32
EEPROM
DATA
USART
To PC
Transmit
From PC
Receive
C Program Block Diagram
ECE353: 14
Computer Systems Lab 1
Kundu
Compression
• Why compress?
• Store more data in same persistent memory
• Persistent data limited in # of erase cycles
• Save on bandwidth when communicating in some apps
• Lossless compression (LZ, Huffman, RLE):
• Decompress and get the same data, bit - for - bit
• Lossy compression (JPEG,…):
• Decompress and get something *similar*.
• Any amount of compression is possible.
• Tradeoff between quality and compression.
• Domain specific
• You may understand the pattern, e.g., in the case of MIDI
messages there is a repetition that could be exploited.
ECE353: 15
Computer Systems Lab 1
Kundu
Example: Run Length Encoding (RLE)
•
•
Very simple lossless encoding of repeating patterns
Example: Imagine a stream of bits with many 0s and 1s
•
•
Source: 000000000111111110001111111
RLE Version: 9Zeros8Ones3Zeros7Ones
• Can be transformed into a sequence of 1s and 0s but we often limit the encoding of
the maximum sequence value to avoid too much overhead for very random
sequences.
• Say we limit to maximum 16 repetition of 1s or 0s: represent with 4 bits
• 1001(0)1000(1)0011(0)0111(1) for the above sequence
• Best we get is minimum 5 bits needed for a sequence of length 16, or a
compression ratio of 16/5
• 111111111111111 => 1111(1)
• Worst we get an overhead of how much?
• To avoid, change algorithm to have a first bit encode WHEN RLE is used
•
RLE can be applied at various granularities
ECE353: 16
Computer Systems Lab 1
Kundu
Example of lossy compression - JPEG compression
Very high quality:
compression = 2.33
Photoshop Image
ECE353: 17
Computer Systems Lab 1
Very low quality:
compression = 115
Produced by MATLAB
with Quality = 0
Kundu
For Part 1 you need to figure out how to
• Initialize the USART
• Set baud rate for midi
• Enable the receiver and transmitter
• Initialize to 8 data bits, 1 stop, and 1 stop bit
• Create functions for receive, transmit, and flush USART
• Create functions to read and write E2PROM
• Work with timers and interrupts
• Use 16-bit counter and generate interrupts for overflows; need to setup
ISR (interrupt service routine)
• Main function
• Use LEDs to debug, try something simple first
• All information is in AVR datasheet; sample codes
ECE353: 18
Computer Systems Lab 1
Kundu
Part 2 – AVR ASM Program
•
Develop, program AVR through JTAG
•
Use the three optical sensors that are
connected to three ADC inputs on AVR to
generate instrument, note, and velocity MIDI
bytes
•
Use the internal AVR counter to synchronize
when sending back MIDI messages to PC
•
•
Sets 1 bit into register, your code will
be polling
•
Speed-dial hex switch on board
Input
ADC
Analog Signal (Photocells)
Input
ATMega32
ADC
Converter
Timer1
To PC
Transmit
USART
ASM Program Block Diagram
Use GPIO connected switch
•
•
Hex Switch
Instrument Switch
To program channel select
(percussion, instrument)
Note that you will see 2 byte message for
program channel selection and the typical 3
bytes for the message that contains note and
velocity
ECE353: 19
Computer Systems Lab 1
Kundu
Build Process
 Board Assembly
 C & Assembly Programming
• AVR Studio (IDE)
• Olimex JTAG Programmer
 Testing
• MIDI-OX
• GoLogic Logic Analyzer
• Oscilloscope
ECE353: 20
Computer Systems Lab 1
Kundu
Board Assembly

Reuse parts of Lab B
• MIDI input (Optoisolator)

Plan for both programs if
you decide to do both C
and ASM versions
• Layout should be
compatible with both

Use resistors as indicated
in schematic
• Otherwise photo-senors
may not work correctly
ECE353: 21
Computer Systems Lab 1
Kundu
AVR Studio




Atmel IDE
Tool chains for building C and assembly code
Programmer
Debug
 Easier to use than Quartus!
ECE353: 22
Computer Systems Lab 1
Kundu
AVR Studio Demonstration
• Open up a new instance of AVR Studio 4
• When the welcome screen pops up, click New Project
ECE353: 23
Computer Systems Lab 1
Kundu
AVR Studio Demonstration
• Select AVR GCC for the project type
• Type in project title and choose project directory if necessary
• Click next and choose the debugging platform used to program
the MCU as well as the MCU that you are using
• Now go to Project --> Configurations and choose the MCUs
clock frequency.
ECE353: 24
Computer Systems Lab 1
Kundu
AVR Studio Demonstration
• Start Coding!
• When finished, 1.build your project and then 2.connect to the
MCU.
ECE353: 25
Computer Systems Lab 1
Kundu
AVR Studio Demonstration
• Once you connect to your MCU, erase the device and then load
up the .hex file created in your project directory on to the Flash
Memory.
ECE353: 26
Computer Systems Lab 1
Kundu
AVR Studio Demonstration
• Click on program to load your .hex file on to the board.
• Now open up MIDI OX and set it up like you did in Lab B.
• Start sending notes to your board!
ECE353: 27
Computer Systems Lab 1
Kundu
Testing
 Familiar tools
• MIDI-OX
• GoLogic Logic Analyzer
• Analyzer individual MIDI frames
• Oscilloscope
• Demonstrate frequency of sent MIDI
messages
ECE353: 28
Computer Systems Lab 1
Kundu
General Advice
 Consult the Atmega32 datasheet
• Pin layouts
• Code examples in C and assembly
• All the info you need to manipulate the microcontroller
for this project is here!
 Use the AVR studio debugger
• Check the value of pins and registers
• Single step through code to find source of problems
 Use internet resources
• avrfreaks.net is a good resource for examples
ECE353: 29
Computer Systems Lab 1
Kundu
DEMO
ECE353: 30
Computer Systems Lab 1
Kundu
Backup
ECE353: 31
Computer Systems Lab 1
Kundu
AVR Assembly
Commands similar to MIPS
ldi, breq, andi, lsr, rsr, etc.
‘in’/’out’ used when working with I/O ports
Can ‘rjmp’ using tags (no offsets to worry about)
There are some differences though…
sbic, neg, cbi, etc.
ECE353: 32
Computer Systems Lab 1
Kundu
Setup Example (AVR)
Setup Example
.include "m32def.inc“ (Include system
file)
.def Temp=R16
(Define R16 register)
.org 0x0000
(Where to begin)
rjmp RESET
RESET:
…
(Jump to RESET)
ECE353: 33
Computer Systems Lab 1
Kundu
Stack Pointer
You must initialize the stack pointer if you wish to
return from jumps…
Set stack pointer
ldi
ECE353: 34
Temp, 0x65
out
SPL, Temp
Computer Systems Lab 1
Kundu
Setup Example (C)
Setup Example
#include <avr/io.h>
(Include system file)
int main() {
int data;
DDRA = 0xFF;
(PORTA output)
DDRB = 0x00;
(PORTB input)
PORTA = 0x55;
(Output 0x55 or
0b01010101)
data = PINB;
(Store input values)
}
…
ECE353: 35
Computer Systems Lab 1
Kundu
Timer1
Timer1 is the 16-bit counter on the AVR
Will count overflows of Timer1 in order to
gauge transmission time.
Good to set counter prescalar to 1024
Choose 1024 prescalar for Timer1
ldi
out
ECE353: 36
Temp, 0x05
TCCR1B, Temp
Computer Systems Lab 1
Kundu
USART
Must initialize with desired baud rate and frame
format
Must be initialized in both C and ASM
MIDI Baud rate : 31.25 kbps
Want start/stop bits and 8 data bits
Read data sheet
ECE353: 37
Computer Systems Lab 1
Kundu