Author: jaguarondi
Date: 2008-07-02 17:08:50 +0200 (Wed, 02 Jul 2008)
New Revision: 1304

Modified:
   firmware/tuxaudio/trunk/PC_communication.c
   firmware/tuxaudio/trunk/communication.c
   firmware/tuxaudio/trunk/i2c.c
   firmware/tuxaudio/trunk/i2c.h
   firmware/tuxaudio/trunk/main.c
   firmware/tuxaudio/trunk/parser.c
   firmware/tuxaudio/trunk/parser.h
   firmware/tuxaudio/trunk/varis.c
   firmware/tuxaudio/trunk/varis.h
Log:
* Replaced the (commented) audio adaptation with a much simpler scheme.

* Split the command parsing from the I2C interrupt.

* Some cleanup.



Modified: firmware/tuxaudio/trunk/PC_communication.c
===================================================================
--- firmware/tuxaudio/trunk/PC_communication.c  2008-07-02 14:03:43 UTC (rev 
1303)
+++ firmware/tuxaudio/trunk/PC_communication.c  2008-07-02 15:08:50 UTC (rev 
1304)
@@ -29,6 +29,8 @@
 #include "spi.h"
 #include "hardware.h"
 
+void adapt_audio_rate(void);
+
 void spiTransaction(void)
 {
     if (spi_start)    // Wait start
@@ -129,98 +131,7 @@
                     }
 
                     spi_master = PUT_SOUND_FIFO;        // Go to the next state
-
-                    if (!programmingFlash && !flashPlay)      // XXX code must 
be review it's very strange .....
-                    {
-                        if (!(spi_master_config & 0x02))
-                        {
-                            if (!lockAdaptFifo)
-                            {
-                                //adaptFifo(PWMFifo);    // Adaptative FIFO
-#if 0
-/** \brief 3 levels adaptative function.
- * \param p fifo pointer
- *
- * High and low thresholds are used to differentiate 3 levels of the buffer. In
- * each level, a custom code or a function call can be provided to do the
- * adaptation.
- */
-void adaptFifo(fifo_t * p)
-{
-    unsigned char fifoLevel;
-
-    fifoLevel = fifoLength(p);
-    if (!p->adpt_cycle)
-    {
-        if (p->matched == 8)
-        {
-            if (OCR0A < 254)
-                p->pwm_max = OCR0A + 2;
-            else
-                p->pwm_max = 255;
-            if (OCR0A > 231)
-                p->pwm_min = OCR0A - 2;
-            else
-                p->pwm_min = 230;
-            p->matched = 9;
-        }
-
-        if (fifoLevel >= FIFO_ADAPT_HIGH)
-        {
-            if (OCR0A > p->pwm_min)
-            {
-                OCR0A--;
-                p->pwm_adpt_sens = 1;
-            }
-        }
-        else if (fifoLevel < FIFO_ADAPT_LOW)
-        {
-            if (OCR0A < p->pwm_max)
-            {
-                OCR0A++;
-                p->pwm_adpt_sens = 2;
-            }
-        }
-        else
-        {
-            if (p->pwm_adpt_sens)
-            {
-                if ((p->pwm_adpt_sens) == 1)
-                {
-                    if (OCR0A < p->pwm_max)
-                        OCR0A++;
                 }
-                else
-                {
-                    if (OCR0A > p->pwm_min)
-                        OCR0A--;
-                }
-                p->pwm_adpt_sens = 0;
-                p->matched = 0;
-            }
-            else
-            {
-                if (p->matched != 9)
-                    p->matched++;
-            }
-        }
-    }
-    p->adpt_cycle++;
-    p->adpt_cycle &= (FIFO_ADAPT_RATE << 1) - 1;
-}
-#endif
-                                lockAdaptFifo = 1;
-                            }
-                            else
-                                FifoClear(PWMFifo);
-                        }
-                        else
-                        {
-                            lockAdaptFifo = 0;
-                        }
-                    }
-
-                }
                 else if (spi_master == PUT_SOUND_FIFO)
                 {
                     if (spi_master_config & 0x02)
@@ -229,7 +140,11 @@
                             FifoPut(PWMFifo, SPDR);       // Put into the FIFO
                     }
                     if (spi_count == (spi_lenght_data + 1))
+                    {
                         spi_master = READ_COMMAND;      // Go to the next state
+                        if (spi_master_config & 0x02)
+                            adapt_audio_rate();
+                    }
 
                 }
                 else if (spi_master == READ_COMMAND)
@@ -259,3 +174,28 @@
     }
 }
 
+/**
+ * \brief Adapt the audio output rate to keep the stack at a mean level.
+ * Called only once when all 17 bytes have been received. All is here, nothing
+ * else is necessary.
+ */
+void adapt_audio_rate(void)
+{
+    static uint8_t prescaler = 0;
+    uint8_t static prev_stack_length = 0;
+    uint8_t stack_length = FifoLength(PWMFifo);
+    uint8_t slope;
+
+    if (++prescaler == 0)
+    {
+        /* Determine if the stack is growing or decreasing. */
+        slope = (stack_length >= prev_stack_length);
+        prev_stack_length = stack_length;
+
+        /* Adaptation */
+        if (slope && stack_length > 40 && OCR0A > 240)
+            OCR0A--;
+        if (!slope && stack_length < 80 && OCR0A < 254)
+            OCR0A++;
+    }
+}

Modified: firmware/tuxaudio/trunk/communication.c
===================================================================
--- firmware/tuxaudio/trunk/communication.c     2008-07-02 14:03:43 UTC (rev 
1303)
+++ firmware/tuxaudio/trunk/communication.c     2008-07-02 15:08:50 UTC (rev 
1304)
@@ -34,6 +34,8 @@
 /* I2C read message (in) */
 static uint8_t in_buf[CMD_SIZE];
 static struct i2c_msg msg_in = {0, 0, in_buf};
+/* I2C last received command */
+static uint8_t *received_cmd = 0;
 
 /** Size of the stack buffer to tuxcore */
 #define CORE_OUT_BUF_SIZE    16
@@ -176,32 +178,21 @@
  * \param msg I2C message received
  * \return 0 if a stop should be sent, 1 for a restart.
  */
-bool i2c_master_receive_service(struct i2c_msg *msg)
+void i2c_master_receive_service(struct i2c_msg *msg)
 {
     if (msg->len != CMD_SIZE)
         /* Error here. */
-        return 0;
+        return;
     if (*(msg->buf) == 0)
     {
         /* Got nothing so stop reading. */
-        return 0;
+        return;
     }
     if (msg->addr == TUXCORE_ADDR)
         /* From tuxcore */
     {
-        /* New pointer necessary as parse_cmd modifies the pointer. */
-        uint8_t *cmd = msg->buf;
-        /* Parse the command */
-        parse_cmd(cmd);
-        /* and forward if it isn't dropped. */
-        if (cmd)
-            queue_rf_cmd(cmd);
-        /* As we got something, there's maybe more so if the stack is not full,
-         * continue */
-        if (FifoLength(rf_cmdout_buf) <= RF_OUT_BUF_SIZE - CMD_SIZE)
-            return 1;
+        received_cmd = msg->buf;
     }
-    return 0;
 }
 
 /*
@@ -251,6 +242,14 @@
         return;
     }
 
+    /* Parse the received command and forward if it isn't dropped. */
+    if (received_cmd)
+    {
+        if (!parse_cmd(received_cmd))
+            queue_rf_cmd(received_cmd);
+        received_cmd = 0;
+    }
+
     /* Send otherwise get commands. */
     if (!send_core_cmds())
     {

Modified: firmware/tuxaudio/trunk/i2c.c
===================================================================
--- firmware/tuxaudio/trunk/i2c.c       2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/i2c.c       2008-07-02 15:08:50 UTC (rev 1304)
@@ -84,7 +84,7 @@
 /* function pointer to i2c receive routine */
 /* I2cSlaveReceive is called when this processor is addressed as a slave for
  * writing */
-static bool (*i2c_master_receive) (struct i2c_msg *msg);
+static void (*i2c_master_receive) (struct i2c_msg *msg);
 /* I2cSlaveTransmit is called when this processor is addressed as a slave for
  * reading */
 static uint8_t(*i2cSlaveTransmit) (uint8_t transmitDataLengthMax,
@@ -99,7 +99,7 @@
     //i2c_resume();
 
 /* Set the user function which handles receiving (incoming) data as a slave */
-void i2c_master_receive_handler(bool (*i2cMasterRx_func) (struct i2c_msg *msg))
+void i2c_master_receive_handler(void (*i2cMasterRx_func) (struct i2c_msg *msg))
 {
     i2c_master_receive = i2cMasterRx_func;
 }
@@ -362,14 +362,8 @@
             m_msg->state = i2c_state;
             if (i2c_master_receive)
             {
-                if (i2c_master_receive(m_msg))
-                {
-                    buf_idx = 0;
-                    i2c_state = I2C_BUSY;
-                    twi_send_start(); /* restart a read */
-                }
-                else
-                    twi_send_stop(); /* end of data stream */
+                i2c_master_receive(m_msg);
+                twi_send_stop(); /* end of data stream */
             }
             break;
         case TW_MT_SLA_NACK: /* 0x20 */

Modified: firmware/tuxaudio/trunk/i2c.h
===================================================================
--- firmware/tuxaudio/trunk/i2c.h       2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/i2c.h       2008-07-02 15:08:50 UTC (rev 1304)
@@ -80,7 +80,7 @@
 
 /* Functions */
 void i2cInit(void);
-void i2c_master_receive_handler(bool (*i2cMasterRx_func) (struct i2c_msg 
*msg));
+void i2c_master_receive_handler(void (*i2cMasterRx_func) (struct i2c_msg 
*msg));
 void
 i2cSetSlaveTransmitHandler(uint8_t(*i2cSlaveTx_func)
                            (uint8_t transmitDataLengthMax,

Modified: firmware/tuxaudio/trunk/main.c
===================================================================
--- firmware/tuxaudio/trunk/main.c      2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/main.c      2008-07-02 15:08:50 UTC (rev 1304)
@@ -167,10 +167,7 @@
         if (commandRX)          // commend RX from radio
         {
             commandRX = 0;      // Reset flag
-            parse_cmd(spi_commandRX);
-            /* XXX there are some empty commands getting through, removed from
-             * here for now. */
-            if (spi_commandRX && spi_commandRX[0])
+            if (!parse_cmd(spi_commandRX))
                 queue_core_cmd(spi_commandRX);
         }
 
@@ -286,20 +283,13 @@
 
         if (!FifoGet(PWMFifo, (uint8_t *)&OCR0B))  /* set the sample value to 
timer pulse width */
         {
-            Fifoinert = 0;
             if (tuxaudio_config.automute)   /* XXX mute functions should not 
be called here each time a sample is placed, this is silly */
                 unmute_amp();
         }
         else
         {
-            Fifoinert++;
-            if (Fifoinert >= 30)
-            {
-                if (tuxaudio_config.automute)
-                    mute_amp();
-                Fifoinert = 30;
-                //OCR0A = 250; // Normal operation for ADC sampling if FIFO 
Adaptative is on
-            }
+            if (tuxaudio_config.automute)
+                mute_amp();
         }
         /* XXX we should move this timer away from here */
         /* send status to the behavioural CPU, 8KHz divided by 256 lead to a

Modified: firmware/tuxaudio/trunk/parser.c
===================================================================
--- firmware/tuxaudio/trunk/parser.c    2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/parser.c    2008-07-02 15:08:50 UTC (rev 1304)
@@ -32,94 +32,101 @@
 /**
  * Parse a cmd received by the computer or by tuxcore and drop it if it
  * shouldn't be forwarded.
+ * \return True if the command has been parsed and shouldn't be forwarded,
+ * false if it should be forwarded.
  */
-void parse_cmd(uint8_t *cmd)
+bool parse_cmd(uint8_t *cmd)
 {
+        /* XXX there are some empty commands getting through, removed from
+         * here for now. */
 
-    /*
-     * Commands that should be forwarded
-     */
-
-    /* Ping */
-    if (cmd[0] == PONG_CMD)
+        /*
+         * Commands that shouldn't be forwarded.
+         */
+        /* Version */
+    if (cmd[0] == NULL_CMD)
     {
-        /* Index of the pong that is supposed to be received from tuxcore */
-        static uint8_t pong_received;
-        /* Counter of the missed pongs */
-        static uint8_t pong_missed;
-        if (pong_received-- < cmd[1])       /* new ping, reset */
-        {
-            pong_received = cmd[1];
-            pong_missed = 0;
-        }
-        else /* pongs */
-        {
-            pong_missed += pong_received - cmd[1];
-            pong_received = cmd[1]; /* resync */
-        }
-        cmd[2] = pong_missed;
     }
-    else if (cmd[0] == SLEEP_CMD)
+    else if (cmd[0] == INFO_TUXAUDIO_CMD)
     {
-        /* XXX sleep commented for now */
-        /* Forwards the cmd to the rf CPU */
-        /*cmd[0] = SLEEP_ACK_CMD;*/
-        /*cmd[1] = 0;*/
-        /*cmd[2] = 0;*/
-        /*cmd[3] = 0;*/
-        /*pre_sleep_delay = 30; [> handle sleep in its own function <]*/
-        /*sleep_f = 1;*/
-        /*statusFlag = 1;*/
+        send_info();
     }
-    else
+    else if (cmd[0] == PLAY_SOUND_CMD)
+        /* param: cmd[1] : sound number */
+        /* cmd[2] : mic sound intensity  */
     {
-        /*
-         * Commands that shouldn't be forwarded.
-         */
-        /* Version */
-        if (cmd[0] == INFO_TUXAUDIO_CMD)
+        /* Drop the cmd if a sound is already playing */
+        if (!(flashPlay || programmingFlash))
         {
-            send_info();
-        }
-        else if (cmd[0] == PLAY_SOUND_CMD)
-            /* param: cmd[1] : sound number */
-            /* cmd[2] : mic sound intensity  */
-        {
-            /* postpone the cmd if a sound is already playing */
-            if (flashPlay || programmingFlash)
-                return;
             audioLevel = cmd[2];
-            //playingAudio(cmd[1]); /* start playing the sound */
             soundToPlay = cmd[1];
             flashPlay = 1;
             flash_state = 1;
         }
-        else if (cmd[0] == MUTE_CMD)
+    }
+    else if (cmd[0] == MUTE_CMD)
+    {
+        if (cmd[1])
+            mute_amp();
+        else
+            unmute_amp();
+    }
+    else if (cmd[0] == STORE_SOUND_CMD)
+    {
+        if (flashPlay)
+            flashPlay = 0;
+        flash_state = 1; /* Erasing flash flag */
+        programmingFlash = 1; /* Set the flag to enter programming sequence */
+    }
+    else if (cmd[0] == ERASE_FLASH_CMD)
+    {
+        eraseFlag = 1;
+    }
+    else if (cmd[0] == CONFIRM_STORAGE_CMD)
+    {
+        if (cmd[1])
+            write_toc = 1;
+        else
+            write_toc = 2;
+    }
+    else
+    {
+        /*
+         * Commands that should be forwarded
+         */
+
+        /* Ping */
+        if (cmd[0] == PONG_CMD)
         {
-            if (cmd[1])
-                mute_amp();
-            else
-                unmute_amp();
+            /* Index of the pong that is supposed to be received from tuxcore 
*/
+            static uint8_t pong_received;
+            /* Counter of the missed pongs */
+            static uint8_t pong_missed;
+            if (pong_received-- < cmd[1])       /* new ping, reset */
+            {
+                pong_received = cmd[1];
+                pong_missed = 0;
+            }
+            else /* pongs */
+            {
+                pong_missed += pong_received - cmd[1];
+                pong_received = cmd[1]; /* resync */
+            }
+            cmd[2] = pong_missed;
         }
-        else if (cmd[0] == STORE_SOUND_CMD)
+        else if (cmd[0] == SLEEP_CMD)
         {
-            if (flashPlay)
-                flashPlay = 0;
-            flash_state = 1; /* Erasing flash flag */
-            programmingFlash = 1; /* Set the flag to enter programming 
sequence */
+            /* XXX sleep commented for now */
+            /* Forwards the cmd to the rf CPU */
+            /*cmd[0] = SLEEP_ACK_CMD;*/
+            /*cmd[1] = 0;*/
+            /*cmd[2] = 0;*/
+            /*cmd[3] = 0;*/
+            /*pre_sleep_delay = 30; [> handle sleep in its own function <]*/
+            /*sleep_f = 1;*/
+            /*statusFlag = 1;*/
         }
-        else if (cmd[0] == ERASE_FLASH_CMD)
-        {
-            eraseFlag = 1;
-        }
-        else if (cmd[0] == CONFIRM_STORAGE_CMD)
-        {
-            if (cmd[1])
-                write_toc = 1;
-            else
-                write_toc = 2;
-        }
-        /* Drop the command */
-        cmd = NULL;
+        return false;
     }
+    return true;
 }

Modified: firmware/tuxaudio/trunk/parser.h
===================================================================
--- firmware/tuxaudio/trunk/parser.h    2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/parser.h    2008-07-02 15:08:50 UTC (rev 1304)
@@ -24,6 +24,6 @@
 
 #include <stdbool.h>
 
-void parse_cmd(uint8_t *cmd);
+bool parse_cmd(uint8_t *cmd);
 
 #endif /* PARSER_H */

Modified: firmware/tuxaudio/trunk/varis.c
===================================================================
--- firmware/tuxaudio/trunk/varis.c     2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/varis.c     2008-07-02 15:08:50 UTC (rev 1304)
@@ -67,10 +67,6 @@
 
 volatile unsigned char spiCommandFlasg = 0;
 
-volatile unsigned char lockAdaptFifo = 1;
-
-volatile unsigned char Fifoinert = 0;
-
 uint16_t frame_without_sound = 0;
 uint16_t frame_without_sound_timeout= 0;
 uint8_t sound_played = 0;

Modified: firmware/tuxaudio/trunk/varis.h
===================================================================
--- firmware/tuxaudio/trunk/varis.h     2008-07-02 14:03:43 UTC (rev 1303)
+++ firmware/tuxaudio/trunk/varis.h     2008-07-02 15:08:50 UTC (rev 1304)
@@ -78,9 +78,6 @@
 
 extern volatile unsigned char spiCommandFlasg;
 
-extern volatile unsigned char lockAdaptFifo;
-
-extern volatile unsigned char Fifoinert;
 extern uint16_t frame_without_sound;
 extern uint16_t frame_without_sound_timeout;
 extern uint8_t sound_played;


-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to