[tuxdroid-svn] r718 - in firmware/tuxaudio/branches: . new_flash_programming
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2007-11-21 15:54:04
|
Author: Paul_R Date: 2007-11-21 16:53:56 +0100 (Wed, 21 Nov 2007) New Revision: 718 Added: firmware/tuxaudio/branches/new_flash_programming/ Modified: firmware/tuxaudio/branches/new_flash_programming/AT26F004.c firmware/tuxaudio/branches/new_flash_programming/PC_communication.c firmware/tuxaudio/branches/new_flash_programming/flash.c firmware/tuxaudio/branches/new_flash_programming/flash.h firmware/tuxaudio/branches/new_flash_programming/main.c firmware/tuxaudio/branches/new_flash_programming/varis.c firmware/tuxaudio/branches/new_flash_programming/varis.h Log: * Added a branches for the new way to store the sounds into the memory Copied: firmware/tuxaudio/branches/new_flash_programming (from rev 701, firmware/tuxaudio/trunk) Modified: firmware/tuxaudio/branches/new_flash_programming/AT26F004.c =================================================================== --- firmware/tuxaudio/trunk/AT26F004.c 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/AT26F004.c 2007-11-21 15:53:56 UTC (rev 718) @@ -26,7 +26,7 @@ /** * \ingroup at26f004 - * \brief initialize a table with the sectors adresses values + * \brief initialize a table with the sector adresses values */ static uint8_t sector_adress[11][3] = { {SECTOR0}, @@ -168,7 +168,6 @@ { uint8_t data1; - flash_enable(); // Set the HOLD signal flash_select(); spiSend(READ_ARRAY_LOW_F); /* Send Read Page Command */ /* Send address */ @@ -177,7 +176,6 @@ spiSend(ad0); data1 = spiSend(NOP); /* Wait response */ flash_unselect(); - flash_onhold(); // Reset the HOLD signal return data1; } Modified: firmware/tuxaudio/branches/new_flash_programming/PC_communication.c =================================================================== --- firmware/tuxaudio/trunk/PC_communication.c 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/PC_communication.c 2007-11-21 15:53:56 UTC (rev 718) @@ -44,7 +44,7 @@ asm volatile /* Clear the SPI interrupt flag */ ("in __tmp_reg__, %0" "\n\t" "in __tmp_reg__, %1" "\n\t":: "I" (_SFR_IO_ADDR(SPSR)), "I"(_SFR_IO_ADDR(SPDR))); - + flash_onhold(); PORTB &= ~0x04; // Chip select while (1) { @@ -113,6 +113,14 @@ spi_lenght_data = 34; else spi_lenght_data = 17; + if (spi_master_config == 0x08 || spi_master_config == 0x00) + { + if (frame_without_sound) + frame_without_sound --; + } + else + frame_without_sound = frame_without_sound_timeout; + spi_master = PUT_SOUND_FIFO; // Go to the next state if (!programmingFlash && !flashPlay) // XXX code must be review it's very strange ..... @@ -169,6 +177,7 @@ } } } + flash_enable(); } } Modified: firmware/tuxaudio/branches/new_flash_programming/flash.c =================================================================== --- firmware/tuxaudio/trunk/flash.c 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/flash.c 2007-11-21 15:53:56 UTC (rev 718) @@ -27,101 +27,182 @@ #include "i2c.h" #include "flash.h" #include "AT26F004.h" -/* Declarations */ -/* Theses functions are declared with the static attribute, so they're - * accessible only by this module */ -static void erasingFlash(void); -static void programmingNumSound(void); -static void programmingToc(void); -static void initSoundProgramming(void); -static void soundProgramming(void); -static void endProgramming(void); - +/* Declarations */ +static void init_programming(uint8_t adi0, uint8_t adi1, uint8_t adi2); +static void programming_sound(void); static void playInit(uint8_t const 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; +static uint16_t index; +static uint8_t sound_stored = 0; +static uint8_t count, ad0, ad1; -/* 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. + \brief Read the number of sound in the flash memory. + This function scan the sound's indexes, and return the number of sound stored + in the flash memory. */ -void flashProgramming(void) -{ - SPCR = 0x00; - SPCR = 0X50; - PORTB |= 0x01; - if (f_state == ERASE_STATE) +uint8_t readFlashNumber(void) +{ + uint8_t i; + ad0 = 0x01; + ad1 = 0x00; + count = 0; + while(1) { - if (flash_state) - erasingFlash(); - - else if (!(read_status() & BUSY)) + if (read_data(0x00, ad1, ad0) == 0xFF) + return count - 1; + else { - send_status(STATUS_AUDIO_CMD, 0, 2, 0); - f_state ++; + for (i = 0; i < 3; i++) + { + ad0 ++; + if (ad0 == 0) + ad1++; + } } + count ++; } - else if (f_state == FIRST_PROG_STATE) + return 0; +} +/** + * \ingroup flash + \brief Store a sound in the memory. + + This function contain 5 states : + + DETECT_INDEXES : Detect if the memory is empty or not, and determine where the sound must be stored. + + PROG_INIT : Start the programmation cycle with the indexes determinated in the first state. + + PROGRAMMING : Program the memory with the sound byte received by the RF. + + PROG_TOC : If the programming sequence is finished, the indexes are written. + + PROG_END : Restore the context. + + \note Each sound starts at the first byte of a 4kB block. To do this, the + lower address byte is reset, and the medium address byte is incremented. + +*/ + +void programming(void) +{ + uint8_t static programming_state = DETECT_INDEXES; + if (programming_state == DETECT_INDEXES) { - send_status(STATUS_AUDIO_CMD, 0, 3, 0); - programmingNumSound(); - send_status(STATUS_AUDIO_CMD, 0, 4, 0); - f_state ++; - } - else if (f_state == PROG_TOC_STATE) - { - if (TOCRX) + TIMSK0 = 0x00; + numSound = readFlashNumber(); + if (numSound == 0) { - programmingToc(); + /* The flash memory is empty. The first address is 0x000400 */ + ad[0] = 0x00; + ad[1] = 0x04; + ad[2] = 0x00; } + else + { + /* One or more sounds are programmed in the flash memory. + * The next sound must be stored after the others */ + index = (numSound * 3) + 1; + ad[0] = read_data(0x00, (index>>8), (index & 0xFF)); + index ++; + ad[1] = read_data(0x00, (index>>8), (index & 0xFF)); + index ++; + ad[2] = read_data(0x00, (index>>8), (index & 0xFF)); - if (flash_state) - f_state ++; + // Goto the next 4kB block. + ad[1] ++; + if (ad[1] == 0) + ad[0] ++; + ad[2] = 0; + } + resetFifo(&PWMFifo); + + if (ad[0] > 0x07) + programming_state = PROG_END; + else + programming_state ++; + + frame_without_sound = 255; + frame_without_sound_timeout = 255; } - else if (f_state == INIT_SOUND_PROG_STATE) + + else if (programming_state == PROG_INIT) { - initSoundProgramming(); + init_programming(ad[0], ad[1], ad[2]); + programming_state ++; + flash_state = 1; + sound_stored = 0; + } + else if (programming_state == PROGRAMMING) + { if (flash_state) { - send_status(STATUS_AUDIO_CMD, 0, 5, 0); - f_state ++; + programming_sound(); } + else + { + write_disable(); + if (sound_stored) + programming_state ++; + else + programming_state = PROG_END; + } + } + else if (programming_state == PROG_TOC) + { + numSound ++; + index = (numSound * 3) + 1; + program_flash(0x00, (index>>8), (index & 0xFF), ad[0]); + index ++; + program_flash(0x00, (index>>8), (index & 0xFF), ad[1]); + index ++; + program_flash(0x00, (index>>8), (index & 0xFF), ad[2]); + programming_state ++; } - if (f_state == SOUND_PROG_STATE) + else if (programming_state == PROG_END) { - soundProgramming(); + programming_state = 0; + programmingFlash = 0; + TIMSK0 = 0x01; + } +} - if (flash_state) - f_state ++; +/** + * \ingroup flash + \brief Erase the flash memory. + + The first step is to send the command to erase the flash. + After, the status is polled while the erase process isn't finished. + When the BUSY flag is null, the erase sequence is stopped. +*/ +void erase(void) +{ + uint8_t static enter = 1; + if (enter) + { + erase_flash(); + enter = 0; } - else if (f_state == END_STATE) + else if (!(read_status() & BUSY)) { - endProgramming(); - send_status(STATUS_AUDIO_CMD, 0, 0, 0); - f_state = 0; + enter = 1; + eraseFlag = 0; + program_flash(0x00, 0x00, 0x00, 0xFE); + program_flash(0x00, 0x00, 0x01, 0x00); + program_flash(0x00, 0x00, 0x02, 0x04); + program_flash(0x00, 0x00, 0x03, 0x00); + numSound = readFlashNumber(); + TIMSK0 = 0x01; } } @@ -132,20 +213,18 @@ The first step (playInit) is to initialize the flash memory with the selected sound to play. Many tests are made to ensure that the sound to play exist, the indexes are correct, etc. - The second step (playingSound) is to fill the fifo with the sound's bytes, and to verify the adresses. - + The second step (playingSound) is to fill the fifo with the sound's bytes, and to verify the addresses. */ + + void playSound(void) { if (flash_state) - playInit(soundNum); + playInit(soundToPlay); else playingSound(); } - - - /* Static functions */ /** * \ingroup flash @@ -163,50 +242,50 @@ static void playInit(uint8_t const nsound) { - uint8_t count, i; - uint8_t adp1, adp0, sounds_stored; // Address pointer varaible - flash_enable(); - sounds_stored = read_data(0x00, 0x00, 0x00); - if (sounds_stored == 0xFF) /* if unprogrammed we have 0xFF stored in flash */ + uint8_t i; + + if (numSound == 0x00) /* if unprogrammed we have 0xFF stored in flash */ { flashPlay = 0; return; } - if (!nsound || (nsound > sounds_stored)) /* check the limits */ + if (!nsound || (nsound > numSound)) /* check the limits */ { flashPlay = 0; return; } count = 1; - adp1 = 0x00; - adp0 = 0x01; + ad1 = 0x00; + ad0 = 0x01; while (count != nsound) // Compute address { for (i = 0; i < 3; i++) { - adp0++; - if (adp0 == 0) - adp1++; + ad0++; + if (ad0 == 0) + ad1++; } count++; } - flash_select(); // Chip Select - flash_enable(); spiSend(READ_ARRAY_LOW_F); // Send Read Page Command spiSend(0x00); // Send Address - spiSend(adp1); - spiSend(adp0); + spiSend(ad1); + spiSend(ad0); for (i = 0; i < 6; i++) { ad[i] = spiSend(NOP); // Read start and stop sound address } + ad[1] ++; + if (ad[1] == 0) + ad[0] ++; + ad[2] = 0; flash_unselect(); // Chip Deselect - /* Check adresses */ + /* Check addresses */ if (ad[0] > TOP_A2) { flashPlay = 0; @@ -255,7 +334,6 @@ spiSend(ad[0]); // Send Address spiSend(ad[1]); spiSend(ad[2]); - flash_onhold(); // Reset the HOLD signal OCR0A = 250; // Normal operation for PWM if fifo adaptative is on flash_state = 0; @@ -273,7 +351,6 @@ static void playingSound(void) { uint8_t sound; - flash_enable(); while (!spi_start && !isFifoFull(&PWMFifo)) { sound = spiSend(0x00); // Wait response @@ -302,7 +379,6 @@ break; } } - flash_onhold(); } /* Static functions */ @@ -311,7 +387,6 @@ \brief Stop the play sequence. */ - static void stopPlaying(void) { flashPlay = 0; @@ -320,172 +395,89 @@ PORTB |= 0x02; // Chip Deselect } - /** * \ingroup flash - * \brief Erase the flash memory. - * - * This funtion perform a full erase of the flash memory and initiate the sound - * flash programming. - */ -static void erasingFlash(void) + \brief Init the programming sequence + + To perform a sequential programming, the first step is to send the correct OP + code, and the three address bytes where the first data byte must be stored. + The second step is to send the OP code and a data to write. + + This function perform the first step. + */ +static void init_programming(uint8_t adi0, uint8_t adi1, uint8_t adi2) { - /* 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; + write_enable(); + flash_select(); + spiSend(SEQU_PROGRAM); + spiSend(adi0); + spiSend(adi1); + spiSend(adi2); + if (!isFifoEmpty(&PWMFifo)) + spiSend(pullFifo(&PWMFifo)); + else + spiSend(0x80); + + flash_unselect(); // Chip Deselect } -/** - * \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. - */ -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; - i = 0; -} /** * \ingroup flash - * \brief Write the TOC in the memory. - * - * This function store the the indexes into the memory. - */ + \brief Program the sound's data into the flash memory -static void programmingToc(void) -{ - TOCRX = 0; - flash_state = 0; - for (j = 0; j < 3; j++) - { - program_flash(ad2, ad1, ad0, TOCadress[j]); - ad0++; // Increment new adress - if (ad0 == 0x00) - ad1++; - } - i++; + This function is executed while the SPI start command is not present. Each + cycle, one byte is popped from the PWM fifo and stored in the sound flash. - /* Store the final adress */ - if (i == numSound + 1) - { - ad[0] = TOCadress[2]; - ad[1] = TOCadress[1]; - ad[2] = TOCadress[0]; + The frame_without_sound variable is decremented each time the RF receive a + frame without sound. When this variable is null, the sound process is + stopped. - /* Init the first sound byte adress */ - ad2 = 0x00; - ad1 = 0x04; - ad0 = 0x00; + If a programming sequence starts, but no sound is received, the cycle is + stopped. - flash_state = 1; - } -} -/** - * \ingroup flash - * \brief Initiate the sound programming. - * - * This function initiate the sound flash memory for a sequential programming. + If the address is equal to 0x07FFFF (the last memory address), the programming + cycle is stopped. + + The sound_stored flag is set when at least one byte is stored in the memory. + Else, this variable is null. */ -static void initSoundProgramming (void) +static void programming_sound(void) { - flash_state = 0; - write_enable(); - flash_select(); - spiSend(SEQU_PROGRAM); - spiSend(ad2); - spiSend(ad1); - spiSend(ad0); - while (!spi_start) // Send first byte into the page flash - { - if (!isFifoEmpty(&PWMFifo)) // Fifo not empty - { - spiSend(pullFifo(&PWMFifo)); // Write data in flash - ad0++; - flash_state = 1; // End of firt command - break; - } - else - flash_state = 0; - } - flash_unselect(); // Chip Deselect -} - -/** - * \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; while (!spi_start) { - if (!isFifoEmpty(&PWMFifo)) // Fifo not empty + if (!isFifoEmpty(&PWMFifo)) { - flash_select(); // Chip Select - spiSend(SEQU_PROGRAM); // Send Sequencial Program Command - spiSend(pullFifo(&PWMFifo)); // Write data in flash - flash_unselect(); // Chip DeselecT - ad0++; // Increment address byte - if (ad0 == 0x00) + sound_stored = 1; + frame_without_sound = frame_without_sound_timeout = STOP_FRAME_NUMBER; + + flash_select(); + spiSend(SEQU_PROGRAM); + spiSend(pullFifo(&PWMFifo)); + flash_unselect(); + + ad[2] ++; + if (ad[2] == 0x00) { - ad1++; - if (ad1 == 0x00) - ad2++; + ad[1]++; + if (ad[1] == 0x00) + ad[0]++; } - while (read_status() & BUSY) ; // Wait Page Program Cycle + while (read_status() & BUSY) ; } /* Check for the last sound byte */ - if (ad2 == ad[2]) + if (!(frame_without_sound)) { - if (ad1 == ad[1]) - { - if (ad0 == ad[0]) - { - flash_state = 1; - break; // Stop programming flash - } - } + flash_state = 0; + break; } + if (ad[0] == 0x07 && ad[1] == 0xFF && ad[2] == 0xFF) + { + flash_state = 0; + break; + } } } -/** - * \ingroup flash - * \brief This function end the flash programming task. - * - * This function restore all the parameters. - */ - -static void endProgramming (void) -{ - write_disable(); // Disable wrtie flash - flash_state = 0; - TCCR0A = 0x23; // Reactivate PWM - TCCR0B = 0x09; - OCR0A = 249; /* we need TOP=250 to get a 8kHz sampling frequency */ - TIMSK0 = 0x01; - - TWCR = (TWCR & TWCR_CMD_MASK) | _BV(TWIE); // Reactivate I2C - - programmingFlash = 0; // Reset the flag to suspend the task -} - - Modified: firmware/tuxaudio/branches/new_flash_programming/flash.h =================================================================== --- firmware/tuxaudio/trunk/flash.h 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/flash.h 2007-11-21 15:53:56 UTC (rev 718) @@ -36,18 +36,25 @@ /** \name 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 +#define DETECT_INDEXES 0 +#define PROG_INIT 1 +#define PROGRAMMING 2 +#define PROG_TOC 3 +#define PROG_END 4 /* @} */ +/** \name No sound in frame timeout + @{ */ +#define STOP_FRAME_NUMBER 10 +#define START_FRAME_NUMBER 100 +/* @} */ -extern void flashProgramming(void); + +extern void programming(void); extern void playSound(void); +extern void erase(void); +extern uint8_t readFlashNumber(void); /** start / end flash states flag */ extern uint8_t flash_state; Modified: firmware/tuxaudio/branches/new_flash_programming/main.c =================================================================== --- firmware/tuxaudio/trunk/main.c 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/main.c 2007-11-21 15:53:56 UTC (rev 718) @@ -70,7 +70,7 @@ i2cSlaveReceiveService(4, buf + 8); sei(); buf[0] = SOUND_VAR_CMD; - buf[1] = read_data(0x00, 0x00, 0x00); + buf[1] = numSound; buf[2] = 0; buf[3] = 0; cli(); @@ -164,7 +164,7 @@ return; audioLevel = audioBuf[2]; //playingAudio(audioBuf[1]); /* start playing the sound */ - soundNum = audioBuf[1]; + soundToPlay = audioBuf[1]; send_status(STATUS_AUDIO_CMD, audioBuf[1], 0, 0); flashPlay = 1; flash_state = 1; @@ -202,22 +202,13 @@ if (flashPlay) flashPlay = 0; /* param: command[1] : number of sounds */ - numSound = command[1]; - f_state =0; /* First programming state */ flash_state = 1; /* Erasing flash flag */ send_status(STATUS_AUDIO_CMD, 0, 1, 0); programmingFlash = 1; /* Set the flag to enter programming sequence */ } else if (command[0] == STORE_INDEX_CMD) { - /* param: command[1] : lower address byte */ - /* command[2] : middle address byte */ - /* command[3] : higher address byte */ - TOCadress[2] = command[3]; // High address - TOCadress[1] = command[2]; // Medium address - TOCadress[0] = command[1]; // Lower address - TOCRX = 1; // Set TOC incoming flag - command[0] = 0; + eraseFlag = 1; } /* Version */ @@ -256,7 +247,7 @@ resetFifo(&PWMFifo); /* Initialise the PWM fifo */ resetFifo(&ADCFifo); /* Initialise the ADC fifo */ config_init(); /* load the configuration defaults from EEPROM */ - + numSound = readFlashNumber(); i2cCommunicationInit(); /* I2C initialization */ sei(); /* Init global interrupt */ @@ -297,11 +288,14 @@ } if (programmingFlash) // Restora all the context for flash programming - flashProgramming(); + programming(); if (flashPlay) playSound(); + if (eraseFlag) + erase(); + if (sendSensorsFlag) { sendSensorsFlag = 0; Modified: firmware/tuxaudio/branches/new_flash_programming/varis.c =================================================================== --- firmware/tuxaudio/trunk/varis.c 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/varis.c 2007-11-21 15:53:56 UTC (rev 718) @@ -51,15 +51,15 @@ fifo_t ADCFifo = { (uint8_t *) ADCbuffer, sizeof ADCbuffer - 1, 0, 0 }; // Flash programming +uint8_t eraseFlag = 0; volatile unsigned char programmingFlash = 0; -volatile unsigned char numSound; -volatile unsigned char TOCadress[3]; -volatile unsigned char TOCRX = 0; +volatile uint8_t numSound; // Flash Variables volatile unsigned char flashPlay = 0; volatile unsigned char ad[6]; volatile unsigned char audioLevel; +uint8_t soundToPlay; unsigned char sendSensorsCmpt, sendSensorsFlag; unsigned char audio_level, battery_level; @@ -70,3 +70,6 @@ volatile unsigned char lockAdaptFifo = 1; volatile unsigned char Fifoinert = 0; + +uint8_t frame_without_sound = 0; +uint8_t frame_without_sound_timeout= 0; Modified: firmware/tuxaudio/branches/new_flash_programming/varis.h =================================================================== --- firmware/tuxaudio/trunk/varis.h 2007-11-15 10:27:04 UTC (rev 701) +++ firmware/tuxaudio/branches/new_flash_programming/varis.h 2007-11-21 15:53:56 UTC (rev 718) @@ -59,15 +59,16 @@ extern fifo_t ADCFifo; // Flash programming + +extern uint8_t eraseFlag; extern volatile unsigned char programmingFlash; -extern volatile unsigned char numSound; -extern volatile unsigned char TOCadress[3]; -extern volatile unsigned char TOCRX; +extern volatile uint8_t numSound; // Flash Variables extern unsigned char flashPlay; extern volatile unsigned char ad[6]; extern volatile unsigned char audioLevel; +extern uint8_t soundToPlay; extern unsigned char sendSensorsCmpt, sendSensorsFlag; extern unsigned char audio_level, battery_level; @@ -80,5 +81,6 @@ extern volatile unsigned char lockAdaptFifo; extern volatile unsigned char Fifoinert; - +extern uint8_t frame_without_sound; +extern uint8_t frame_without_sound_timeout; #endif |