[tuxdroid-svn] r505 - firmware/tuxaudio/branches/audio_cleanup
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2007-09-10 08:59:10
|
Author: Paul_R Date: 2007-09-10 10:59:09 +0200 (Mon, 10 Sep 2007) New Revision: 505 Modified: firmware/tuxaudio/branches/audio_cleanup/flash.c firmware/tuxaudio/branches/audio_cleanup/flash.h firmware/tuxaudio/branches/audio_cleanup/main.c Log: * Reorganized the flash memory's playing functions - Added three 'static' functions to init, play and stop the sound. - cleanup the main loop. Modified: firmware/tuxaudio/branches/audio_cleanup/flash.c =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/flash.c 2007-09-10 08:08:55 UTC (rev 504) +++ firmware/tuxaudio/branches/audio_cleanup/flash.c 2007-09-10 08:59:09 UTC (rev 505) @@ -20,6 +20,7 @@ /* $Id: $ */ #include <avr/io.h> +#include <avr/interrupt.h> #include "varis.h" #include "spi.h" #include "i2c.h" @@ -36,10 +37,16 @@ static void soundProgramming(void); static void endProgramming(void); +static void playInit(uint8_t nsound); +static void playingSound(void); +static void stopPlaying(void); uint8_t f_state; uint8_t flash_state; uint8_t ad0, ad1, ad2, i, j; + +uint8_t soundNum; + /* Public functions */ /** @@ -58,7 +65,6 @@ - END_STATE clear all variable used to control the programming task, and reswitch-on the I2C and the PWM timer. - Because I2C is disabled during this task, all command will be ignored. */ void flashProgramming(void) @@ -108,41 +114,22 @@ /** * \ingroup flash - * \brief Erase the flash memory. - * This funtion perform a full erase of the flash memory and initiate the sound - * flash programming. - * This functions is called by the command_Parser function when a programming - * flash command is received. - */ - -/* XXX This function is directly called by commandParser. It could be changed to - * be called by the flashProgramming function. - */ -static void erasingFlash(void) + \brief This function is used to play a sound from the flash memory. + */ +void playSound(void) { - /* Desactivate the PWM */ - TCCR0A = 0x00; - TCCR0B = 0x00; - OCR0A = 0x00; - TIMSK0 = 0x00; - - - resetFifo(&PWMFifo); /* Reinitialise the PWM fifo */ - erase_flash(); /* Erase the flash */ - flash_state = 0; + if (flash_state) + playInit(soundNum); + else + playingSound(); } -/** - * \ingroup flash - \param ad2 high address part - \param ad1 medium adress part - \param ad0 lower adress part - \return The byte read in the sound flash memory - \brief This function read a byte in the sound flash memory. - */ -void playingAudio(uint8_t nsound) + +/* Static functions */ + +void playInit(uint8_t nsound) { uint8_t count, i; uint8_t adp1, adp0, sounds_stored; // Address pointer varaible @@ -167,19 +154,19 @@ count++; } - PORTB &= ~0x02; // Chip Select + FLASH_CS_ON; // Chip Select - spiSend(0x03); // Send Read Page Command + spiSend(READ_ARRAY_LOW_F); // Send Read Page Command spiSend(0x00); // Send Address spiSend(adp1); spiSend(adp0); for (i = 0; i < 6; i++) { - ad[i] = spiSend(0x00); // Read start and stop sound address + ad[i] = spiSend(NOP); // Read start and stop sound address } - PORTB |= 0x02; // Chip Deselect + FLASH_CS_OFF; // Chip Deselect /* Check adresses */ if (ad[0] > TOP_A2) @@ -201,20 +188,50 @@ return; } - PORTB &= ~0x02; // Chip Select + FLASH_CS_ON; // Chip Select spiSend(0x03); // Send Read Page Command spiSend(ad[0]); // Send Address spiSend(ad[1]); spiSend(ad[2]); - PORTB &= ~0x01; // Reset the HOLD signal + HOLD_ON; // Reset the HOLD signal OCR0A = 250; // Normal operation for PWM if fifo adaptative is on - flashPlay = 1; // Read of sound + flash_state = 0; } +static void playingSound(void) +{ + uint8_t sound; + cli(); + if (!isFifoFull(&PWMFifo)) + { + HOLD_OFF; // Set the HOLD signal + sound = spiSend(0x00); // Wait response + HOLD_ON; // Reset the HOLD signal + sound = sound >> audioLevel; + pushFifo(&PWMFifo, sound); -void stopPlayingAudio(void) + ad[2]++; // Increment address for next play + if (ad[2] == 0) + { + ad[1]++; + if (ad[1] == 0) + { + ad[0]++; + if (ad[0] == 0x08) // Address overflow + stopPlaying(); + } + } + if (ad[0] == ad[3]) // Test end of sound + if (ad[1] == ad[4]) + if (ad[2] == ad[5]) + stopPlaying(); + } + sei(); +} + +static void stopPlaying(void) { flashPlay = 0; PORTB |= 0x01; // Set the HOLD signal @@ -222,18 +239,28 @@ } -/* Static functions - They can be accessed only by this module */ - /** * \ingroup flash + * \brief Erase the flash memory. + * This funtion perform a full erase of the flash memory and initiate the sound + * flash programming. + * This functions is called by the command_Parser function when a programming + * flash command is received. + */ +static void erasingFlash(void) +{ + /* Desactivate the PWM */ + TCCR0A = 0x00; + TCCR0B = 0x00; + OCR0A = 0x00; + TIMSK0 = 0x00; - \brief This function read the flash memory status. - */ + + resetFifo(&PWMFifo); /* Reinitialise the PWM fifo */ + erase_flash(); /* Erase the flash */ + flash_state = 0; +} - - - - /** * \ingroup flash * \brief Write the number of sound to be stored. Modified: firmware/tuxaudio/branches/audio_cleanup/flash.h =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/flash.h 2007-09-10 08:08:55 UTC (rev 504) +++ firmware/tuxaudio/branches/audio_cleanup/flash.h 2007-09-10 08:59:09 UTC (rev 505) @@ -42,12 +42,12 @@ #define SOUND_PROG_STATE 4 #define END_STATE 5 -extern void playingAudio(unsigned char nsound); -extern void stopPlayingAudio(void); extern void flashProgramming(void); +extern void playSound(void); extern uint8_t flash_state; extern uint8_t f_state; +extern uint8_t soundNum; #endif Modified: firmware/tuxaudio/branches/audio_cleanup/main.c =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/main.c 2007-09-10 08:08:55 UTC (rev 504) +++ firmware/tuxaudio/branches/audio_cleanup/main.c 2007-09-10 08:59:09 UTC (rev 505) @@ -159,8 +159,10 @@ if (flashPlay) return; audioLevel = audioBuf[2]; - //if (audioBuf[1] == 1) PORTC &= ~0x02; - playingAudio(audioBuf[1]); /* start playing the sound */ + //playingAudio(audioBuf[1]); /* start playing the sound */ + soundNum = audioBuf[1]; + flashPlay = 1; + flash_state = 1; } else if (audioBuf[0] == MUTE_CMD) { @@ -243,7 +245,7 @@ int main(void) { - unsigned char sound; + init_avr(); // Init AVR @@ -282,11 +284,11 @@ while (1) /* Inifinite main loop */ { - // asm volatile ("SPITRANSACTION: \n\t"); - sei(); // Reactivate global interrupt in case of flash programmation + if (!flashPlay) spiTransaction(); // Spi transaction from radio + if (commandRX) // commend RX from radio { commandRX = 0; // Reset flag @@ -298,86 +300,58 @@ flashProgramming(); } - // else - // { - if (flashPlay) - { - cli(); - if (!isFifoFull(&PWMFifo)) - { - PORTB |= 0x01; // Set the HOLD signal - sound = spiSend(0x00); // Wait response - PORTB &= ~0x01; // Reset the HOLD signal - sound = sound >> audioLevel; - pushFifo(&PWMFifo, sound); + if (flashPlay) + { + playSound(); + } - ad[2]++; // Increment address for next play - if (ad[2] == 0) - { - ad[1]++; - if (ad[1] == 0) - { - ad[0]++; - if (ad[0] == 0x08) // Address overflow - stopPlayingAudio(); - } - } - if (ad[0] == ad[3]) // Test end of sound - if (ad[1] == ad[4]) - if (ad[2] == ad[5]) - stopPlayingAudio(); - } - sei(); - } + if (sendSensorsFlag) + { + sendSensorsFlag = 0; + if (pre_sleep_delay > 1) + /* There's a delay before the sleep function is actually called + * otherwise other commands on the stack can't be executed */ + pre_sleep_delay--; + /* stop sending sensor status in sleep as the i2C should be + * stopped at this time */ + else + sendSensors(); + /* wait for all status to be sent before going to sleep */ + /* TODO fix these conditions for the sleep */ + //if (isFifoEmpty(statusFifo)) + /* wait for audio commands to be processed */ + //if (!audioBufIdx && !flashPlay) - if (sendSensorsFlag) + if (power_on_reset_delay) /* XXX to move to a proper loop or timer after or before main() */ { - sendSensorsFlag = 0; - if (pre_sleep_delay > 1) - /* There's a delay before the sleep function is actually called - * otherwise other commands on the stack can't be executed */ - pre_sleep_delay--; - /* stop sending sensor status in sleep as the i2C should be - * stopped at this time */ - else - sendSensors(); - /* wait for all status to be sent before going to sleep */ - /* TODO fix these conditions for the sleep */ - //if (isFifoEmpty(statusFifo)) - /* wait for audio commands to be processed */ - //if (!audioBufIdx && !flashPlay) + uint8_t volatile j; - if (power_on_reset_delay) /* XXX to move to a proper loop or timer after or before main() */ + DDRD |= 0x02; + for (j = 0; j < 0xFF; j++) ; + power_on_reset_delay--; + if (!power_on_reset_delay) { - uint8_t volatile j; - + /* Set the microphone power */ DDRD |= 0x02; - for (j = 0; j < 0xFF; j++) ; - power_on_reset_delay--; - if (!power_on_reset_delay) - { - /* Set the microphone power */ - DDRD |= 0x02; - } - else - DDRD &= ~0x02; } + else + DDRD &= ~0x02; } + } - sendCommands(); /* Send commands on I2C */ + sendCommands(); /* Send commands on I2C */ - if (audioBufIdx) - audioIntParser(); + if (audioBufIdx) + audioIntParser(); - /* Send information to the computer. */ - if (info_flg) - send_info(); + /* Send information to the computer. */ + if (info_flg) + send_info(); - /* Sleep mode */ - if (pre_sleep_delay == 1) - sleep(); - // } + /* Sleep mode */ + if (pre_sleep_delay == 1) + sleep(); } } |