[tuxdroid-svn] r493 - firmware/tuxaudio/branches/audio_cleanup
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2007-09-06 14:45:11
|
Author: Paul_R Date: 2007-09-06 16:45:08 +0200 (Thu, 06 Sep 2007) New Revision: 493 Modified: firmware/tuxaudio/branches/audio_cleanup/flash.c firmware/tuxaudio/branches/audio_cleanup/flash.h firmware/tuxaudio/branches/audio_cleanup/main.c Log: * Reorganize the functions of flash.c. Some have been declared 'static' to hide them of other modules. * Added doxygen comments * Moved the programmingSound state machine in flash.c Modified: firmware/tuxaudio/branches/audio_cleanup/flash.c =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/flash.c 2007-09-06 12:54:06 UTC (rev 492) +++ firmware/tuxaudio/branches/audio_cleanup/flash.c 2007-09-06 14:45:08 UTC (rev 493) @@ -17,191 +17,381 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: */ +/* $Id: $ */ #include <avr/io.h> #include "varis.h" #include "spi.h" #include "i2c.h" #include "flash.h" +/* Declarations */ +/* Theses functions are declared with the static attribute, so they're + * accessible only by this module */ -unsigned char read_status(void); -void write_enable(void); -void write_disable(void); -void write_status(unsigned char status); -void erase_flash(void); -unsigned char read_data(unsigned char ad2, unsigned char ad1, - unsigned char ad0); -//void programmingAudio(void); -void unprotect_sector(unsigned char ad2, unsigned char ad1, unsigned char ad0); -void playingAudio(unsigned char nsound); -void stopPlayingAudio(void); +static void programmingNumSound(void); +static void programmingToc(void); +static void initSoundProgramming(void); +static void soundProgramming(void); +static void endProgramming(void); +static uint8_t read_status(void); +static void write_enable(void); +static void write_disable(void); +static void write_status(uint8_t const status); +static void erase_flash(void); +static void unprotect_sector(uint8_t const ad2, uint8_t const ad1, + uint8_t const ad0); +static void program_flash(uint8_t const ad2, uint8_t const ad1, uint8_t const ad0, + uint8_t const data); + +uint8_t f_state = 0; uint8_t flash_state; -unsigned char read_status(void) +/* Public functions */ + +/** + * \ingroup flash + \brief This function is used to reprogram sound flash memory.. + + This function is structured like a state machine. The six states are : + - ERASE_STATE which perform a full chip erase. This state turn off the PWM + and disable I2C interrupt. + - FIRST_PROG_STATE write the number of sounds to be stored. + - PROG_TOC_STATE write the sound's indexes each time a INDEX COMMAND is + received. The last indexes are stored. + - INIT_SOUND_PROG_STATE initiate the memory to a sequential programming. The + first sound byte is writed in the flash. + - SOUND_PROG_STATE store all the sound data received. + - 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) +{ + SPCR = 0x00; + SPCR = 0X50; + PORTB |= 0x01; + if (f_state == ERASE_STATE) + { + if (read_status() == 0x10) + f_state ++; + } + else if (f_state == FIRST_PROG_STATE) + { + programmingNumSound(); + f_state ++; + } + else if (f_state == PROG_TOC_STATE) + { + if (TOCRX) + programmingToc(); + if (flash_state) + f_state ++; + } + else if (f_state == INIT_SOUND_PROG_STATE) + { + initSoundProgramming(); + if (flash_state) + f_state ++; + } + if (f_state == SOUND_PROG_STATE) + { + soundProgramming(); + + if (flash_state) + f_state ++; + } + else if (f_state == END_STATE) + { + endProgramming(); + f_state = 0; + } +} + +/** + * \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. + */ + + +uint8_t ad0, ad1, ad2, i, j; +void erasingFlash(void) { - unsigned char status; + TCCR0A = 0x00; // Desactivate PWM + TCCR0B = 0x00; + OCR0A = 0x00; + TIMSK0 = 0x00; - FLASH_CS_ON; /* Chip Select */ + f_state = 0; + TWCR = (TWCR & TWCR_CMD_MASK) & ~_BV(TWIE); // Desactivate I2C - spiSend(READ_STATUS_REG); /* Send Read Status Command */ + resetFifo(&PWMFifo); /* Reinitialise the PWM fifo */ + programmingFlash = 1; // Set the flag to suspend the task + erase_flash(); // Erase the flash +} +/** + * \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 - status = spiSend(NOP); /* Read status on spi */ + \brief This function read a byte in the sound flash memory. + */ +uint8_t read_data(uint8_t const ad2, uint8_t const ad1, uint8_t const ad0) +{ + uint8_t data1; - FLASH_CS_OFF; /* Chip Deselect */ + FLASH_CS_ON; // Chip Select + spiSend(READ_ARRAY_LOW_F); // Send Read Page Command + spiSend(ad2); // Send Address + spiSend(ad1); + spiSend(ad0); + data1 = spiSend(NOP); // Wait response + FLASH_CS_OFF; // Chip Deselect - return status; + return data1; } -void write_enable(void) +void playingAudio(uint8_t nsound) { - FLASH_CS_ON; /* Chip Select */ + uint8_t count, i; + uint8_t adp1, adp0, sounds_stored; // Address pointer varaible - spiSend(WRITE_EN); /* Send Write Enable Command */ + sounds_stored = read_data(0x00, 0x00, 0x00); + if (sounds_stored == 0xFF) /* if unprogrammed we have 0xFF stored in flash */ + return; + if (!nsound || (nsound > sounds_stored)) /* check the limits */ + return; - FLASH_CS_OFF; /* Chip Deselect */ + count = 1; + adp1 = 0x00; + adp0 = 0x01; + while (count != nsound) // Compute address + { + for (i = 0; i < 3; i++) + { + adp0++; + if (adp0 == 0) + adp1++; + } + count++; + } + PORTB &= ~0x02; // Chip Select + + spiSend(0x03); // 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 + } + + PORTB |= 0x02; // Chip Deselect + + /* Check adresses */ + if (ad[0] > 0x07) + return; /* don't read outside the flash */ + if (ad[3] > 0x07) + return; /* don't read outside the flash */ + if ((ad[0] == 0) && (ad[1] < 0x04)) + return; /* minimum index not respected */ + if ((ad[4] == 0) && (ad[5] < 0x04)) + return; /* minimum index not respected */ + if (ad[3] < ad[0]) + return; /* check that the stop index is greater than the start index */ + else if (ad[3] == ad[0]) + { + if (ad[4] < ad[1]) + return; + else if (ad[4] == ad[1]) + if (ad[5] <= ad[2]) + return; + } + + PORTB &= ~0x02; // 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 + + OCR0A = 250; // Normal operation for PWM if fifo adaptative is on + flashPlay = 1; // Read of sound } -void write_disable(void) + +void stopPlayingAudio(void) { - FLASH_CS_ON; /* Chip Select */ + flashPlay = 0; + PORTB |= 0x01; // Set the HOLD signal + PORTB |= 0x02; // Chip Deselect +} - spiSend(WRITE_DIS); /* Send Write Disable Command */ - FLASH_CS_OFF; /* Chip Deselect */ +/* Static functions - They can be accessed only by this module */ + +/** + * \ingroup flash + + \brief This function read the flash memory status. + */ + +static uint8_t read_status(void) +{ + uint8_t status; + + FLASH_CS_ON; /* Chip Select */ + spiSend(READ_STATUS_REG); /* Send Read Status Command */ + status = spiSend(NOP); /* Read status on spi */ + FLASH_CS_OFF; /* Chip Deselect */ + return status; } +/** + * \ingroup flash -void write_status(unsigned char status) + \brief This function set the write enable flag of the flash memory. + */ + +static void write_enable(void) { - FLASH_CS_ON; /* Chip Select */ + FLASH_CS_ON; /* Chip Select */ + spiSend(WRITE_EN); /* Send Write Enable Command */ + FLASH_CS_OFF; /* Chip Deselect */ +} +/** + * \ingroup flash + + \brief This function clear the write enable flag of the flash memory. + */ + +static void write_disable(void) +{ + FLASH_CS_ON; /* Chip Select */ + spiSend(WRITE_DIS); /* Send Write Disable Command */ + FLASH_CS_OFF; /* Chip Deselect */ +} + +/** + * \ingroup flash + + \brief This function write into the flash memory status register. + */ + +static void write_status(uint8_t const status) +{ + FLASH_CS_ON; /* Chip Select */ spiSend(WRITE_STATUS_REG); /* Send Write Status Command */ spiSend(status); /* Send status */ + FLASH_CS_OFF; /* Chip Deselect */ - FLASH_CS_OFF; /* Chip Deselect */ - } +/** + * \ingroup flash + \param ad2 high address part + \param ad1 medium adress part + \param ad0 lower adress part -void unprotect_sector(unsigned char ad2, unsigned char ad1, unsigned char ad0) + \brief This function unprotect a sector. + */ +static void unprotect_sector(uint8_t const ad2, uint8_t const ad1, uint8_t const ad0) { - FLASH_CS_ON; // Chip Select - + FLASH_CS_ON; // Chip Select spiSend(UNPROTECT_SECTOR); // Send unprotect sector command spiSend(ad2); // Send Address spiSend(ad1); spiSend(ad0); + FLASH_CS_OFF; // Chip Deselect - FLASH_CS_OFF; // Chip Deselect - } +/** + * \ingroup flash -void erase_flash(void) + \brief This function erase the entire memory. + */ + +static void erase_flash(void) { write_status(0x00); // Disable sector protection register write_enable(); // Enable the writing - unprotect_sector(SECTOR0); // Unprotected sector 0 + unprotect_sector(SECTOR0); write_enable(); // Enable the writing - unprotect_sector(SECTOR1); // Unprotected sector 1 + unprotect_sector(SECTOR1); write_enable(); // Enable the writing - unprotect_sector(SECTOR2); // Unprotected sector 2 + unprotect_sector(SECTOR2); write_enable(); // Enable the writing - unprotect_sector(SECTOR3); // Unprotected sector 3 + unprotect_sector(SECTOR3); write_enable(); // Enable the writing - unprotect_sector(SECTOR4); // Unprotected sector 4 + unprotect_sector(SECTOR4); write_enable(); // Enable the writing - unprotect_sector(SECTOR5); // Unprotected sector 5 + unprotect_sector(SECTOR5); write_enable(); // Enable the writing - unprotect_sector(SECTOR6); // Unprotected sector 6 + unprotect_sector(SECTOR6); write_enable(); // Enable the writing - unprotect_sector(SECTOR7); // Unprotected sector 7 + unprotect_sector(SECTOR7); write_enable(); // Enable the writing - unprotect_sector(SECTOR8); // Unprotected sector 8 + unprotect_sector(SECTOR8); write_enable(); // Enable the writing - unprotect_sector(SECTOR9); // Unprotected sector 9 + unprotect_sector(SECTOR9); write_enable(); // Enable the writing - unprotect_sector(SECTOR10); // Unprotected sector 10 + unprotect_sector(SECTOR10); write_enable(); // Enable the writing - FLASH_CS_ON; // Chip Select + FLASH_CS_ON; // Chip Select + spiSend(CHIP_ERASE); // Send Erase Bulk command + FLASH_CS_OFF; // Chip Deselect +} - spiSend(CHIP_ERASE); // Send Erase Bulk command +/** + * \ingroup flash + \param ad2 high address part + \param ad1 medium adress part + \param ad0 lower adress part - FLASH_CS_OFF; // Chip Deselect -} + \brief This function write a byte in the flash memory. + */ -void program_flash(unsigned char ad2, unsigned char ad1, unsigned char ad0, - unsigned char data) +static void program_flash(uint8_t const ad2, uint8_t const ad1, uint8_t const ad0, + uint8_t const data) { write_enable(); - FLASH_CS_ON; // Chip Select - - spiSend(BYTE_PROGRAM); // Send Page Byte Command - spiSend(ad2); // Send Address + spiSend(BYTE_PROGRAM); // Send Page Byte Command + spiSend(ad2); // Send Address spiSend(ad1); spiSend(ad0); - spiSend(data); // Write data in flash + spiSend(data); // Write data in flash + FLASH_CS_OFF; // Chip Deselect - FLASH_CS_OFF; // Chip Deselect - while (read_status() != 0x10) ; // Wait Page Program Cycle } -unsigned char read_data(unsigned char ad2, unsigned char ad1, unsigned char ad0) -{ - unsigned char data1; - FLASH_CS_ON; // Chip Select - spiSend(READ_ARRAY_LOW_F); // Send Read Page Command - spiSend(ad2); // Send Address - spiSend(ad1); - spiSend(ad0); - data1 = spiSend(NOP); // Wait response - FLASH_CS_OFF; // Chip Deselect - - return data1; -} - /** * \ingroup flash - * \brief Erase the flash memory. - * This funtion perform a full erase of the flash memory. - */ - - -unsigned char ad0, ad1, ad2, i, j; -void erasingFlash(void) -{ - TCCR0A = 0x00; // Desactivate PWM - TCCR0B = 0x00; - OCR0A = 0x00; - TIMSK0 = 0x00; - - TWCR = (TWCR & TWCR_CMD_MASK) & ~_BV(TWIE); // Desactivate I2C - - resetFifo(&PWMFifo); /* Reinitialise the PWM fifo */ - - programmingFlash = 1; // Set the flag to suspend the task - - erase_flash(); // Erase the flash -} -/** - * \ingroup flash * \brief Write the number of sound to be stored. - * This function store the first TOC byte (numSound), received with the flash - * program command, at the first memory adress. + This function store the first TOC byte (numSound), received with the flash + program command, at the first memory adress. */ -void programmingNumSound(void) +static void programmingNumSound(void) { program_flash(0x00, 0x00, 0x00, numSound); // Write first byte of the TOC - ad0 = 0x01; // Init TOC address ad1 = 0x00; ad2 = 0x00; @@ -213,7 +403,7 @@ * This function store the the indexes into the memory. */ -void programmingToc(void) +static void programmingToc(void) { TOCRX = 0; flash_state = 0; @@ -226,27 +416,34 @@ } i++; + /* Store the final adress */ if (i == numSound + 1) { - ad[0] = TOCadress[2]; // Save final addres + ad[0] = TOCadress[2]; ad[1] = TOCadress[1]; ad[2] = TOCadress[0]; - ad2 = 0x00; // Init sound address + /* Init the first sound byte adress */ + ad2 = 0x00; ad1 = 0x04; ad0 = 0x00; flash_state = 1; } } +/** + * \ingroup flash + * \brief Initiate the sound programming. + * This function initiate the sound flash memory for a sequential programming. + */ -void initSoundProgramming (void) +static void initSoundProgramming (void) { flash_state = 0; write_enable(); - FLASH_CS_ON; // Chip Select - spiSend(SEQU_PROGRAM); // Send Sequencial Program Command - spiSend(ad2); // Send Address + FLASH_CS_ON; + spiSend(SEQU_PROGRAM); + spiSend(ad2); spiSend(ad1); spiSend(ad0); @@ -262,21 +459,25 @@ else flash_state = 0; } - FLASH_CS_OFF; // Chip Deselect + FLASH_CS_OFF; // Chip Deselect } -void soundProgramming(void) +/** + * \ingroup flash + * \brief Program the sound in the flash memory. + * This function store the sound in the flash memory. + */ +static void soundProgramming(void) { flash_state = 0; - // Fourth step WRITE SOUND while (!spi_start) { if (!isFifoEmpty(&PWMFifo)) // Fifo not empty { - FLASH_CS_ON;// PORTB &= ~0x02; // Chip Select + FLASH_CS_ON; // Chip Select spiSend(SEQU_PROGRAM); // Send Sequencial Program Command spiSend(pullFifo(&PWMFifo)); // Write data in flash - FLASH_CS_OFF;// PORTB |= 0x02; // Chip DeselecT + FLASH_CS_OFF; // Chip DeselecT ad0++; // Increment address byte if (ad0 == 0x00) { @@ -302,7 +503,12 @@ } } -void endProgramming (void) +/** + * \ingroup flash + * \brief This function end the flash programming task. + */ + +static void endProgramming (void) { write_disable(); // Disable wrtie flash flash_state = 0; @@ -316,80 +522,4 @@ programmingFlash = 0; // Reset the flag to suspend the task } -void playingAudio(unsigned char nsound) -{ - unsigned char count, i; - unsigned char adp1, adp0, sounds_stored; // Address pointer varaible - sounds_stored = read_data(0x00, 0x00, 0x00); - if (sounds_stored == 0xFF) /* if unprogrammed we have 0xFF stored in flash */ - return; - if (!nsound || (nsound > sounds_stored)) /* check the limits */ - return; - - count = 1; - adp1 = 0x00; - adp0 = 0x01; - while (count != nsound) // Compute address - { - for (i = 0; i < 3; i++) - { - adp0++; - if (adp0 == 0) - adp1++; - } - count++; - } - - PORTB &= ~0x02; // Chip Select - - spiSend(0x03); // 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 - } - - PORTB |= 0x02; // Chip Deselect - - /* Check adresses */ - if (ad[0] > 0x07) - return; /* don't read outside the flash */ - if (ad[3] > 0x07) - return; /* don't read outside the flash */ - if ((ad[0] == 0) && (ad[1] < 0x04)) - return; /* minimum index not respected */ - if ((ad[4] == 0) && (ad[5] < 0x04)) - return; /* minimum index not respected */ - if (ad[3] < ad[0]) - return; /* check that the stop index is greater than the start index */ - else if (ad[3] == ad[0]) - { - if (ad[4] < ad[1]) - return; - else if (ad[4] == ad[1]) - if (ad[5] <= ad[2]) - return; - } - - PORTB &= ~0x02; // 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 - - OCR0A = 250; // Normal operation for PWM if fifo adaptative is on - flashPlay = 1; // Read of sound -} - -void stopPlayingAudio(void) -{ - flashPlay = 0; - PORTB |= 0x01; // Set the HOLD signal - PORTB |= 0x02; // Chip Deselect -} Modified: firmware/tuxaudio/branches/audio_cleanup/flash.h =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/flash.h 2007-09-06 12:54:06 UTC (rev 492) +++ firmware/tuxaudio/branches/audio_cleanup/flash.h 2007-09-06 14:45:08 UTC (rev 493) @@ -76,24 +76,23 @@ #define SECTOR9 0x07, 0xA0, 0X00 #define SECTOR10 0x07, 0xC0, 0X00 -extern unsigned char read_status(void); -extern void write_enable(void); -extern void write_disable(void); -extern void write_status(unsigned char status); -extern void erase_flash(void); -extern unsigned char read_data(unsigned char ad2, unsigned char ad1, - unsigned char ad0); -extern void unprotect_sector(unsigned char ad2, unsigned char ad1, - unsigned char ad0); +/* Flash programming states */ +#define ERASE_STATE 0 +#define FIRST_PROG_STATE 1 +#define PROG_TOC_STATE 2 +#define INIT_SOUND_PROG_STATE 3 +#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 erasingFlash(void); -extern void programmingNumSound(void); -extern void programmingToc(void); -extern void initSoundProgramming(void); -extern void soundProgramming(void); -extern void endProgramming(void); +extern uint8_t read_data(uint8_t const ad2, uint8_t const ad1, + uint8_t const ad0); extern uint8_t flash_state; +extern uint8_t f_state; #endif Modified: firmware/tuxaudio/branches/audio_cleanup/main.c =================================================================== --- firmware/tuxaudio/branches/audio_cleanup/main.c 2007-09-06 12:54:06 UTC (rev 492) +++ firmware/tuxaudio/branches/audio_cleanup/main.c 2007-09-06 14:45:08 UTC (rev 493) @@ -39,7 +39,6 @@ * Version number */ -uint8_t f_state = 0; #define CPU_NUMBER TUXAUDIO_CPU_NUM /* audio CPU */ const author_t author __attribute__ ((section("version.3"))) = { @@ -123,7 +122,6 @@ */ #define DBG_STACK 1 -//void SIG_INTERRUPT0(void) __attribute__ ((signal, naked)); #if (DBG_STACK) /* @@ -196,7 +194,6 @@ { /* param: command[1] : number of sounds */ numSound = command[1]; - f_state = 0; erasingFlash(); } else if (command[0] == STORE_INDEX_CMD) @@ -240,12 +237,6 @@ command[3] = battery_level; pushCommands(command); /* push the command set on the filo stack */ } -#define ERASE_STATE 0 -#define FIRST_PROG_STATE 1 -#define PROG_TOC_STATE 2 -#define INIT_SOUND_PROG_STATE 3 -#define SOUND_PROG_STATE 4 -#define END_STATE 5 int main(void) { @@ -301,47 +292,11 @@ if (programmingFlash) // Restora all the context for flash programming { - SPCR = 0x00; - SPCR = 0X50; - PORTB |= 0x01; - if (f_state == ERASE_STATE) - { - if (read_status() == 0x10) - f_state ++; - } - else if (f_state == FIRST_PROG_STATE) - { - programmingNumSound(); - f_state ++; - } - else if (f_state == PROG_TOC_STATE) - { - if (TOCRX) - programmingToc(); - if (flash_state) - f_state ++; - } - else if (f_state == INIT_SOUND_PROG_STATE) - { - initSoundProgramming(); - if (flash_state) - f_state ++; - } - if (f_state == SOUND_PROG_STATE) - { - soundProgramming(); + flashProgramming(); - if (flash_state) - f_state ++; - } - else if (f_state == END_STATE) - { - endProgramming(); - f_state = 0; - } } - else - { + // else + // { if (flashPlay) { cli(); @@ -419,7 +374,7 @@ /* Sleep mode */ if (pre_sleep_delay == 1) sleep(); - } + // } } } |