Hardware/software partitioning is the problem of dividing an application’s computations into a part that executes as sequential instructions on a microprocessor (the “software”) and a part that runs as parallel circuits on some IC fabric like an ASIC or FPGA (the “hardware”), such as to achieve design goals set for metrics like performance, power, size, and cost.
Introduction:
The aim of the project is to implement a system based on Hardware/Software Co-Design on a Spartan 3E FPGA board and a Microcontroller using Xilinx IDE tool. Alphabets and/or numbers are entered through keyboard (push buttons on the FPGA) and are in-turn displayed on the LCD display (on the Microcontroller), at the same time audio corresponding to the key being pressed will be played on a speaker. The system is implemented in both hardware and software. VHDL is used as hardware description language and C as software. Though this project can be implemented by use of either FPGA or microcontroller only, but that is not the aim here. We want to introduce this concept so that it can be applied later in problems where partitioning can actually make a difference in the performance.
System Architecture:
Fig. 1: Overview of FPGA based Hardware Software Co-Design
Components Required:
1. Nexys 2 Spartan 3E FPGA Board
2. Microcontroller – Atmega16 + LCD
3. 8Ohm Speaker
4. Connectors
5. 74LS245 IC
Construction:
A. FPGA
· The FPGA has to be configured to take the input from the user through its input switches (or push button switches).
· Hence the first step is to configure some switches as the input to the FPGA.
· Next, we need to make sure that each switch corresponds to a different input. Hence we will give a code (4 bit) to uniquely identify each input.
· Since, this is a prototype we will define just 4 inputs. And hence code them as:
A => “0001”
B => “0010”
C => “0011”
D => “0100”
· That is, as soon as a switch is pressed, we will identify it using the 4 bit code unique for it.
· The next thing to be done from the FPGA is to send these 4 bits to the microcontroller.
· Communication:
The communication can be either Synchronous or Asynchronous.
If you prefer Asynchronous mode, then parallel transmission will be the fastest mode to choose.
If you prefer Synchronous mode, I would recommend setting up of SPI between the FPGA (master) and microcontroller (slave).
Here I will be using Asynchronous Parallel Transmission.
· For transmission we will make use of Peripheral Connectors (PMod Connectors) on the Spartan 3E board. The figure bellow explains this in detail.
Fig. 2: Circuit Diagram of Nexys2 Pmod Connector
· We can configure the PMods as both Input and Output. Here they will be set as output.
B. Microcontroller
· This unit has mainly two tasks. First, displaying the character on the LCD. Second, playing the sound corresponding to the character on the speaker.
· Task 1: Display
Since we are using parallel communication, each of those transmission lines are connected to corresponding input pins on microcontroller. Now there can be two ways to ensure data is read correctly;
1. The pins are continuously polled to check if there is any activity. Once, there is, the value is read and then stored in a variable.
2. We set up an interrupt. This interrupt is triggered from the FPGA when transmission lines are going to send the bits. Once, the interrupt is received, the value is read and then stored in the variable.
· The 2nd method is more efficient than the 1st one. Hence we will run with it.
· Hence the value is read by using the PIN<port no> variable and stored in some variable of our choice.
· Task 2: Sound
We have to play a sound on the 8? speaker. For this we will use the internal flash of the microcontroller. We will store a sample sound (say a beep) in the memory. And we will play it according to the input. That is, ‘A’ means playing the beep once. ‘B’ means playing it twice, and so-on.
The microcontroller we are using here is the Atmega16 which has just 16K Bytes of internal flash. Hence we cannot store many different sounds in this much memory. To achieve this we need to use an external flash and interface it with the microcontroller. This is not the focus here so we will not go there.
To play PCM audio on the AVR microcontroller, we will make use of AVR’s high speed PWM (fast PWM Mode). It almost sounds fine and can be used for simple projects that require sound effects.
Step1: Create a .wav file. If you don’t have any use the following link and convert text to speech. Save it as .wav file.
http://www2.research.att.com/~ttsweb/tts/demo.php
Step2: Convert the created wav file into 8 bit unsigned 8000 samples per second wav file using the software switch from NCH. It can be found here:
http://www.nch.com.au/switch/index.html
Step3: Convert the 8 bit wav file to a sample c file using the software below.
http://darkfader.net/ngpc/files/wav2c.zip
Step4: Copy the created samples onto the header file pcm_sample and save it.
In essence, what we are doing here is taking a sound sample, and then sampling it to create finite (8000) samples each of 8 bits. These samples are stored in the flash using the PROG_MEM command (due to use of avr/pgmspace library). These samples will act as the duty cycle for the PWM that will be generated. We set the initial duty cycle (value in the OCR1A register) as 0. Thereafter, samples will be continuously read from the flash and will be written to the OCR1A. Finally the speaker will be attached to the output of the PWM (OC1A) and we will be able to hear the sounds.
To use the internal flash, we make use of the following functions:
1. const long pcm_length=6800;
const unsigned char pcm_samples[] PROGMEM ={ enter samples here };
This function is used to make the header file “pcm_sample” which will be used later.
2. pgm_read_byte(&pcm_samples[sample++]);
This function is used to read byte after byte from the header file.
Connections:
For the FPGA:
1. The 4 slide switches SW0, SW1, SW2 and SW3 will be used as input.
Fig. 3: Table listing PMod Connectors
2. Next for output from the FPGA, we will use PMod JA. The connections are:
Interrupt (O) is connected to JA1 “L15”
Transmission Line “Z0” connected to JA2 “K12”
Transmission Line “Z1” connected to JA3 “L17”
Transmission Line “Z2” connected to JA4 “M15”
Transmission Line “Z3” connected to JA7 “K13”
Note1: We cannot use JA5, JA6 since they are predefined to be used as GND, VCC.
Note2: Since the Spartan 3E works at 3.3V Logic and the Atmega16 at 5V Logic, we need to connect a Logic Shifter IC like 74LS245 to level up the transmission lines before connecting them to the PORT of the microcontroller.
For the Microcontroller:
1. First connect the interrupt to PD2 (since we are using INT0, can be anything depending on your choice). Then connect the transmission lines to pins 0-3 of any port. Here, Port B is used.
2. Connect the speaker through a Low-Pass filter (resistor (1k) + capacitor (470uf) connected to GND) to PD5 (OC1A-PWM output).
Working:
It can be observed that as soon as we press a switch on the FPGA, we can see the corresponding letter on the LCD and hear the number of beats on the speaker.
Conclusion:
This was a simple experiment to showcase the concept of Hardware-Software co-design. Though this project on its own does not really exploit the full potential of this concept but it is a good start for beginners who can learn from it and use this in projects of their own.
Pin Diagrams:
Fig. 4: Image of 4-slide switch on FPGA board
74LS245
Project Source Code
###
// Program to use External (Hardware) Interrupts of AVR Microcontroller (ATmega16)#include "lcd.h"#include#include#include#include "pcm_sample.h"#include#include#define SAMPLE_RATE 8000;volatile uint16_t sample;int sample_count;/* initialise the PWM */void pwm_init(void){lcd_init();/* use OC1A pin as output */DDRD = _BV(PD5);/** clear OC1A on compare match* set OC1A at BOTTOM, non-inverting mode* Fast PWM, 8bit*/TCCR1A = _BV(COM1A1) | _BV(WGM10);/** Fast PWM, 8bit* Prescaler: clk/1 = 8MHz* PWM frequency = 8MHz / (255 + 1) = 31.25kHz*/TCCR1B = _BV(WGM12) | _BV(CS10);/* set initial duty cycle to zero */OCR1A = 0;/* Setup Timer0 */int a;a = PINB; //since pins are connected at PB0,PB1,PB2,PB3 the codes will form integers as 1,2,3,4//lcd_print(a,10);if(a==1){_delay_ms(100);OCR1A = pgm_read_byte(&pcm_samples[sample++]); //reading the serial flash completely onceif(sample>pcm_length)sample=0; //after reading making the counter=0lcd_send_string("A");}if(a==2){lcd_send_string("B");_delay_ms(100);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(200);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;}if(a==3){lcd_send_string("C");_delay_ms(100);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(200);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(300);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;}if(a==4){lcd_send_string("D");_delay_ms(100);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(200);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(300);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;_delay_ms(400);OCR1A = pgm_read_byte(&pcm_samples[sample++]);if(sample>pcm_length)sample=0;}}ISR (INT0_vect){pwm_init();}int main(void){lcd_init();cli();GICR =(1<//Set Bit6 of GICR to unmask INT0 interrupt. MCUCR =(3< sei();while(1){}}
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.