/*
 * $Id: main.c,v 1.1 2010/10/19 05:18:47 bsd Exp $
 *
 */

/*
 * Author : Brian Dean, bsd@bdmicro.com
 * Date   : 2 March, 2004
 */

/*
 * Magnevation SpeakJet Speech and Sound Sythesizer Module
 * 
 * This directory contains a complete test program for demonstrating the
 * use of the Magnevation SpeakJet speach chip with the BDMICRO MAVRIC
 * and MAVRIC-II microcontroller boards.  Feel free to copy the relevent
 * function definitions into your own applications and to customize them
 * for your needs.
 * 
 * The code is written in C using the GNU GCC C Compiler.  A Makefile is
 * provided which is known to work correctly on the FreeBSD Unix
 * operating system.
 * 
 * Configuration:
 *  
 *   Hook up the SpeakJet according to its data sheet.  Attach the
 *   SpeakJet output to an amplifier.  Attach your MAVRIC or MAVRIC-II
 *   PORTD3 to SpeakJet pin 11 (Serial TTL Data).  It is important to use
 *   PORTD3 and not the MAVRIC/MAVRIC-II UART1 TX pin because the UART1
 *   TX pin is a level shifted RS232 voltage, while the SpeakJet utilizes
 *   just the TTL level.  PORTD3 on the MAVRIC/MAVRIC-II is the TTL level
 *   RS232 output pin which is the one to use with the SpeakJet.
 * 
 *   When the MAVRIC/MAVRIC-II comes out of reset, it will speak the
 *   phrase: "Welcome to the BDMICRO MAVRIC SpeakJet Test Program".
 * 
 * For more information about the MAVRIC and MAVRIC-II microcontroller
 * boards, see:
 * 
 *   http://www.bdmicro.com/
 * 
 * For more information about the Magnevation SpeakJet Speech chip, see:
 * 
 *   http://www.speakjet.com/
 * 
 *
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include <inttypes.h>

//#define MCU_FREQ 16000000L
#define MCU_FREQ 14745600L
#define BAUD     9600L

volatile uint16_t ms_count;

#define WP 5
uint8_t announce[] = {
/* welcome  */ 145, 131, 145, 4, 195, 136, 140, WP,
/* to       */ 192, 162, WP,
/* the      */ 169, 134, WP,
/* bdmicro  */ 170, 128, 0, 174, 128, 0, 140, 155, 194, 148, 137, WP,
/* mavric   */ 140, 154, 166, 148, 129, 194, WP,
/* speakjet */ 187, 198, 128, 194, 4, 165, 131, 191, WP,
/* test     */ 191, 131, 187, 191, WP,
/* program  */ 199, 148, 137, 179, 148, 132, 140
};

#define N_ANNC sizeof(announce)


void uart1_init(uint16_t baud)
{
  uint16_t brr;

  brr = MCU_FREQ / (16L*(long)baud) - 1;

  UBRR1H = brr >> 8;;
  UBRR1L = brr & 0x00ff;
  UCSR1B = _BV(TXEN);
}


void uart_putc(uint8_t uart, char c)
{
  if (uart == 0) {
    loop_until_bit_is_set(UCSR0A, UDRE);
    UDR0 = c;
  }
  else if (uart == 1) {
    while ((UCSR1A & _BV(UDRE)) == 0)
      ;
    UDR1 = c;
  }
}


int main(void)
{
  uint8_t i;

  uart1_init(BAUD);

  for (i=0; i<N_ANNC; i++)
    uart_putc(1, announce[i]);

  while (1)
    ;
}

