CSEP567 Final Design Project: due 11:59 p.m. PST Thursday, December 15, 2005

Last modified: Dec. 13, 4:30 p.m. Changes will be shown in red.

Note: This is an exam. Do not discuss this or compare notes. This should be your own work, not a group project.
We will be happy to answer questions relating to clarity of this document via the class mailing list. We will not answer questions about your design.

Congratulations! You’ve just been hired by the Seattle Happy Teddy Bear Company to design and develop a prototype of next year’s Talking Bear toy.

The bear is based on an Atmel AVR ATmega16 microprocessor, with several external peripherals. The bear will listen and respond to certain sounds and movements.

Inputs:

10 bit A to D converter with microphone for listening (AD0 of ATmega16)
Switch input, left ear squeeze, 0=SQUEEZE (Needs to be debounced)
Switch input, right ear squeeze, 0=SQUEEZE (Needs to be debounced)
Switch input, body position detector, 0= HORIZONTAL, 1=VERTICAL (Needs to be debounced)
Switch input, leg position detector 0= HORIZONTAL, 1=VERTICAL (Needs to be debounced)
Single Bit Digital input, greater than 2 G motion detector, 1=GREATER THAN 2 G
Photocell light level detector (AD1 OF ATmega16)
4Mbit Flash Memory, SPI interface, for audio message storage (Part # AT26F004)

Outputs:

12 bit D to A converter—TWI interface, with amp and speaker, for talking (Part #AD5321)
Two blue LEDs, for eyes, COMMON ANODE, 0=ON, 1=OFF

Schematic of the bear’s action module.

Operation:

The bear should have a reasonable user interface and should not make noises unless the user intention is clear. You need to add lots of debouncing to your design so that the system does not flicker between states and annoy parents with lots of repeated phrases.

The AD converter produces a stream of audio samples. These samples need to be analyzed for ambient sounds that will trigger bear responses. See audio analysis section for details.

The LEDs in the bear’s eyes should light up either when the bear is talking or when the ambient room sound is above minimum sound threshold. The brightness of the LEDs should correspond with the loudness of the sound. See audio analysis section for details.

The photocell should be sampled periodically for changes in ambient light. When the lights go down, the bear will say “Good night.” The bear should respond to light levels appropriately and have a reasonable user interface.

The bear has many different responses to motion. To detect these motions the inputs should be debounced and sampled periodically. The switched inputs on the ears are noisy buttons and will settle in 25ms. The body and leg position switches are made of a liquid and can take a minimum of 3 seconds to settle. The single-bit 2G Motion detector input only needs to be sampled periodically. The list of bear responses are:

  1. When both the body position detector and the leg position detector are in the horizontal position, the bear is in the sleepy state. It should periodically say “I’m sleepy.” every 15 seconds for 1 minute, then power-down until further activity is detected.
  2. When body position is vertical and leg position is horizontal, the bear is in the sitting state. The bear should say “Is it time to eat?”
  3. When both detectors are in the vertical position the bear is in the standing state. The bear should periodically say “Let’s play.”
  4. When either ear is squeezed, the bear should say a random phrase from the response list. Additional Phrases: “You’re cool.”, “Bears rule.”
  5. An active signal on the 2G Motion detector of less than 2 seconds should be interpreted as being dropped or struck, and the bear should say “Ouch, that hurt!” An active signal of greater than 2 seconds (continuous) is probably because the bear is being played with (e.g. swung in a circle, etc) so the bear should say “Wheeeeeeeeeeeeeeee!”

When no activity occurs on any of the inputs for more than 2 minutes, the bear should say “Let’s play.”. The bear should repeat this every 15 seconds for 1 minute, then enter power-down-mode1 until the next activity is detected. After 5 minutes the bear should enter power-down mode2.

Power-down Mode1

In this mode the real-time clock should be used to wake up the bear from sleep once each second, so that the bear can listen for 200 ms. If no sound activity or movement is detected, the bear should re-start the timer and sleep for a second. After 5 minutes without activity the bear should enter Power-down mode 2.

Power-down Mode2

In this mode the bear sleeps and can be awakened only by an edge on the body position detector. See the AVRmega16 datasheet section on "Power Management and Sleep Modes" for the lowest-power mode possible.

Audio analysis:

The AD converter is sampling ambient sound from the microphone, which can look like this:

The 10-bit audio samples can range from 0x0000 (bottom) to 0x03FF (top), centered around 0x01FF, or half full-scale. Silence would be a line at 0x1FF. Your job is to produce a stream of numbers represented by the black line, call the envelope of the signal. The higher the line, the louder the sound. You should create a moving average of the magnitude from the center, or half-full scale. This average should be long enough to smooth out sounds shorter than 200 msec, like the sound marked in red. You will need to select a sample rate greater than 8k samples per second. See audio lecture slides.

You should select a minimum sound threshold above which you will use the detected sound envelope to control the brightness of the bear’s blue LED eyes via PWM. The bears eyes should get brighter when the room volume goes above the minimum sound threshold. The brightness is determined by how loud the room volume is, or by the loudness of the playback audio when the bear is talking. The bear doesn’t listen while he talks.

Audio Phrases

Audio phrases are stored in the AT26F004 serial interface Flash memory device accessed by SPI interface. Eight phrases are stored in the external memory, each starting on a 64kb memory boundary. See figure 4-1 in the datasheet for address boundaries. The first 16 bit word of the block is the sample count in the phrase; the rest of the data is the audio in 16 bit values, from 0x0000 to 0x0FFF, corresponding to the 12 bit resolution of the AD5321 D to A converter. The recorded sample rate of the phrases is 16000 samples per second. Your DA sample rate should be as close to that rate as possible. The samples are padded with silence to the next 16-sample length, so that their length is a multiple of the buffer size (see below).

The phrases are:

Block 0: “Good night.” Lowest address
Block 1: “I’m sleepy.”
Block 2: “Is it time to eat?”
Block 3: “Let’s play.”
Block 4: “You’re cool.”
Block 5: “Bears rule.”
Block 6: “Ouch, that hurt!”
Block 7: “Wheeeeeeeeeeeeeeee!” Highest address

Audio buffers

Audio input from the on-board ADC and audio output to the DA converter need to be buffered in 16-sample buffers in SRAM, with two buffers in each direction, called INA, INB, OUTA, and OUTB.

Input processing:

The on-board ADC interrupt routine fills INA while INB is available for envelope processing by the foreground routine. When the ADC interrupt routine reaches the end of INA, it signals the foreground routine to process INA data and it switches to filling INB. This buffer-swapping continues with the rate controlled by the ADC interrupt rate. The main routine has 16 sample times to process the buffer.

Output processing:

The TWI interrupt routine will move data from OUTA to the DA converter while the foreground routine fills OUTB with phrase data from the external flash memory. When the TWI interrupt routine empties the OUTA buffer it signals the foreground routine to refill OUTA and it switches to moving data from OUTB. This buffer-swapping continues with the rate controlled by the TWI interrupt rate. The foreground routine must implement a routine to fill the correct buffer from the SPI interface to the external flash memory.

 

Assignment (submit all as a single file, code snippets followed by answers to questions):

I. Design and write the following code snippets:

  1. Initialization and Interrupt routine for the AD converter. Include the buffer switch signaling as described above.
  2. Initialization and Interrupt routine for the AD5321 DAC Sound Output Chip. Include the buffer switch signaling as described above.
  3. Implement the foreground sound input processing as a callable function – take the correct 16 sample buffer and process the data to get the moving average to detect ambient room loudness.
  4. Implement the foreground sound output processing as a callable function – implement the SPI routines for reading the correct buffer of audio data from the external flash memory and placing it into the correct output buffer .
  5. Implement the foreground MAIN sound playback state machine loop, including triggering the appropriate audio phrases and behavior, the handling of processing data and changing states for playback, and the sleep modes.
  6. Implement the PWM routines for controlling the eyes.


    II. Answer the following:
    (State your assumptions for questions 3 and 4 by estimating the number of cycles used in code blocks that you may or may not have written. DO NOT count cycles; only give a reasonable estimate of cycles. WE are talking ballpark here.)
  1. Map out all sources of interrupts in this system, including Power-down Mode1 and Power-down Mode2.
  2. What is the most important interrupt? Is there an interrupt that shouldn’t be interrupted or delayed, and why?
  3. How many instruction cycles does the main loop have to process an input buffer of audio? An output buffer? Show that your code meets this timing requirement.
  4. Analyse how much processing power is left over when the bear is listening, and when the bear is talking.
  5. Estimate the power savings for Power-down Mode1 and Power-down Mode2.

    III. For extra credit:

    Design a protocol for a separate program to download from a PC via RS-232 and write audio blocks to the AT26F004.

Deliverables

Your file should contain comments that include: your full name and email address. Email to: Bruce-- bruceh@cs.washington.edu AND Waylon-- wrb@cs.washington.edu by 11:59 p.m. PST Thursday, December 15, 2005.