[tuxdroid-svn] r1130 - firmware/tuxaudio/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2008-05-07 12:43:19
|
Author: jaguarondi Date: 2008-05-07 14:42:28 +0200 (Wed, 07 May 2008) New Revision: 1130 Modified: firmware/tuxaudio/trunk/communication.c firmware/tuxaudio/trunk/communication.h firmware/tuxaudio/trunk/flash.c firmware/tuxaudio/trunk/main.c firmware/tuxaudio/trunk/misc.c firmware/tuxaudio/trunk/parser.c Log: * Renamed stuff, updated documentation and improved consistency between functions, and final cleanup of the communication module. This finally looks good now. Modified: firmware/tuxaudio/trunk/communication.c =================================================================== --- firmware/tuxaudio/trunk/communication.c 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/communication.c 2008-05-07 12:42:28 UTC (rev 1130) @@ -23,6 +23,7 @@ #include <avr/interrupt.h> #include "communication.h" +#include "fifo.h" #include "i2c.h" #include "parser.h" #include "hardware.h" @@ -34,19 +35,18 @@ static uint8_t in_buf[CMD_SIZE]; static struct i2c_msg msg_in = {0, 0, in_buf}; -/* - * core_cmdout is a stack for commands to be sent to tuxcore - */ -FIFO_INSTANCE(core_cmdout_buf, COMMAND_BUF_SIZE); +/** Size of the stack buffer to tuxcore */ +#define CORE_OUT_BUF_SIZE 16 +/** Size of the stack buffer to tuxrf */ +#define RF_OUT_BUF_SIZE 32 + +FIFO_INSTANCE(core_cmdout_buf, CORE_OUT_BUF_SIZE); +/** Stack for commands to be sent to tuxcore */ fifo_t *core_cmdout = FifoPointer(core_cmdout_buf); -/* - * statusFifo is a fifo buffer for commands received from the i2c and that - * should be sent over the RF link - * - */ -FIFO_INSTANCE(statusFifo_s, INCOMING_BUF_SIZE); -fifo_t *statusFifo = FifoPointer(statusFifo_s); +FIFO_INSTANCE(rf_cmdout_buf_s, RF_OUT_BUF_SIZE); +/** Stack for commands to be sent to rf */ +fifo_t *rf_cmdout_buf = FifoPointer(rf_cmdout_buf_s); /* @@ -58,13 +58,13 @@ FifoClear(core_cmdout); } -/* - * sendCommands takes one command (from 1 to 4 bytes) out of the command stack - * and send them by i2c. +/** + * \brief Take one command out of the command stack and send it through i2c. * - * Should return 0 if nothing has to be sent. + * \return 0 if nothing has to be sent, -1 if the stack is corrupted, 1 if + * something has been sent. */ -int8_t sendCommands(void) +static int8_t send_core_cmds(void) { uint8_t i; @@ -87,10 +87,14 @@ return 1; } -void get_core_cmd(void) +/** + * \brief Start an I2C read request to fetch one command from tuxcore. + * If the rf stack is full, we return immediately. + */ +static void get_core_cmd(void) { /* First check if the stack is not full */ - if (FifoLength(statusFifo) > INCOMING_BUF_SIZE - CMD_SIZE) + if (FifoLength(rf_cmdout_buf) > RF_OUT_BUF_SIZE - CMD_SIZE) return; if (i2c_get_status() != I2C_BUSY) @@ -100,69 +104,79 @@ } -/* - * Add a command on the stack for tuxcore - * Returns 0 if the stack is full, 1 if the command has been added +/** + * \brief Add a command on the stack for tuxcore + * \return 0 if the stack is full, 1 if the command has been added * successfully. */ -int8_t send_core_cmd(uint8_t *cmd) +int8_t queue_core_cmd(uint8_t *cmd) { uint8_t i; if (FifoLength(core_cmdout) > FifoSize(core_cmdout) - CMD_SIZE) return 0; + uint8_t sreg; + sreg = SREG; cli(); for (i=0; i<CMD_SIZE; i++) FifoPut(core_cmdout, cmd[i]); - sei(); + SREG = sreg; return 1; } -/* - * Add a command on the stack for tuxcore - * Returns 0 if the stack is full, 1 if the command has been added +/** + * \brief Add a command on the stack for tuxcore + * \return 0 if the stack is full, 1 if the command has been added * successfully. */ -int8_t send_core_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ +int8_t queue_core_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ uint8_t param3) { uint8_t c[4] = {cmd , param1, param2, param3}; - return send_core_cmd(c); + return queue_core_cmd(c); } -/* Send status to RF. */ -void send_status(uint8_t const *status) +/** + * \brief Add a command on the stack for the rf + * \return 0 if the stack is full, 1 if the command has been added + * successfully. + */ +int8_t queue_rf_cmd(uint8_t const *status) { uint8_t i; - if (FifoLength(statusFifo) <= INCOMING_BUF_SIZE - CMD_SIZE) - { - uint8_t sreg; - sreg = SREG; - cli(); - for (i = 0; i < CMD_SIZE; i++) - FifoPut(statusFifo, status[i]); - SREG = sreg; - } - /* XXX what if stack is full? */ + if (FifoLength(rf_cmdout_buf) > RF_OUT_BUF_SIZE - CMD_SIZE) + return 0; + + uint8_t sreg; + sreg = SREG; + cli(); + for (i = 0; i < CMD_SIZE; i++) + FifoPut(rf_cmdout_buf, status[i]); + SREG = sreg; + return 1; } -/* Send status to RF. */ -void send_status_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ +/** + * \brief Add a command on the stack for the rf + * \return 0 if the stack is full, 1 if the command has been added + * successfully. + */ +int8_t queue_rf_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ uint8_t param3) { uint8_t c[4] = {cmd , param1, param2, param3}; - send_status(c); + return queue_rf_cmd(c); } -/* - * Slave receiver function associated with the i2c ISR - * - * Only do a backup of the buffer so any new i2c data won't overwrite the - * previous ones +/** + * \brief Callback function associated with the i2c ISR and which process the + * received command. + * \param msg I2C message received + * \return 0 if a stop should be sent, 1 for a restart. */ -bool i2cMasterReceiveService(struct i2c_msg *msg) +bool i2c_master_receive_service(struct i2c_msg *msg) { if (msg->len != CMD_SIZE) /* Error here. */ @@ -178,36 +192,33 @@ /* New pointer necessary as parse_cmd modifies the pointer. */ uint8_t *cmd = msg->buf; /* Parse the command */ - /* XXX Should be moved out of the interrupt, no? */ parse_cmd(cmd); /* and forward if it isn't dropped. */ if (cmd) - send_status(cmd); + queue_rf_cmd(cmd); /* As we got something, there's maybe more so if the stack is not full, * continue */ - if (FifoLength(statusFifo) <= INCOMING_BUF_SIZE - CMD_SIZE) + if (FifoLength(rf_cmdout_buf) <= RF_OUT_BUF_SIZE - CMD_SIZE) return 1; } return 0; } /* - * Get a set of status commands from the statusFifo buffer. The command + * Get a set of status commands from the rf_cmdout_buf buffer. The command * length will always be CMD_SIZE. Returns '1' if there's nothing to * get, '0' otherwise. - * - * The command[] parameter format starts with the command and then the parameters */ uint8_t popStatus(uint8_t *command) { uint8_t i; - if (!FifoLength(statusFifo)) + if (!FifoLength(rf_cmdout_buf)) return 1; /* nothing to do */ cli(); /* XXX try to disable I2C interrupts instead */ for (i = 0; i < CMD_SIZE; i++) - if (FifoGet(statusFifo, &command[i])) + if (FifoGet(rf_cmdout_buf, &command[i])) { sei(); return 1; /* fifo corrupted so drop data XXX add some debug @@ -217,13 +228,17 @@ return 0; } +/** + * \brief Service routine that handles the regular communication tasks like sending + * and fetching commands. + */ void communication_task(void) { /* Clear stack when the RF is disconnected. This makes sense but is * necessary otherwise the standalone commands won't be fetched anymore * when the RF is off. */ if (!(RF_ONLINE_PIN & RF_ONLINE_MK)) - FifoClear(statusFifo); + FifoClear(rf_cmdout_buf); /* If busy, pass. */ if (i2c_get_status() == I2C_BUSY) @@ -236,25 +251,22 @@ return; } - if (!sendCommands()) + /* Send otherwise get commands. */ + if (!send_core_cmds()) { get_core_cmd(); } } -/* - * I2C communication initalisation - * - * Start the twi interface in the mode defined in i2c.h, set the receiver function and - * the behaviour CPU address. +/** + * \brief Initialize the I2C communication and the in and out buffers. */ -void i2cCommunicationInit(void) +void communication_init(void) { i2c_init(); msg_out.addr = TUXCORE_ADDR; msg_out.len = CMD_SIZE; - //msg_out.buf = out_buf; msg_in.addr = TUXCORE_ADDR; msg_in.len = CMD_SIZE; - i2c_master_receive_handler(i2cMasterReceiveService); + i2c_master_receive_handler(i2c_master_receive_service); } Modified: firmware/tuxaudio/trunk/communication.h =================================================================== --- firmware/tuxaudio/trunk/communication.h 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/communication.h 2008-05-07 12:42:28 UTC (rev 1130) @@ -22,45 +22,23 @@ #ifndef COMMUNICATION_H #define COMMUNICATION_H -#include <stdbool.h> - #include "common/commands.h" #include "common/api.h" #include "common/defines.h" -#include "varis.h" -#include "fifo.h" -#define COMMAND_BUF_SIZE 16 -#define COMMAND_FIFO_SIZE (COMMAND_BUF_SIZE - 1) /* due to the fifo construction */ -#define CMD_SIZE 4 -#define INCOMING_BUF_SIZE 32 -#define INCOMING_FIFO_SIZE (INCOMING_BUF_SIZE - 1) /* due to the fifo construction */ +void communication_init(void); +void communication_task(void); -extern uint8_t audioBuf[CMD_SIZE]; /* single buffer for commands received over i2c to control the audio interface */ -extern uint8_t audioBufIdx; /* index indicating the number of valid bytes in the buffer */ - -extern fifo_t *statusFifo; /* incomingBuf is used for commands received from the i2c and should be sent over the RF link */ - -/* /< XXX New clean commands */ -int8_t send_core_cmd(uint8_t *command); -int8_t send_core_cmd_p(uint8_t command, uint8_t param1, uint8_t param2, \ +int8_t queue_core_cmd(uint8_t *command); +int8_t queue_core_cmd_p(uint8_t command, uint8_t param1, uint8_t param2, \ uint8_t param3); -/* XXX /> */ +int8_t queue_rf_cmd(uint8_t const *status); +int8_t queue_rf_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ + uint8_t param3); -void i2cCommunicationInit(void); +/* XXX to remove */ void initCommunicationBuffers(void); - -/* From RF to i2c */ -int8_t sendCommands(void); - - -/* From i2c to RF */ -void acceptData(void); -void send_status(uint8_t const *status); -void send_status_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ - uint8_t param3); +/* XXX change this when reviewing the rf communication. */ uint8_t popStatus(uint8_t * command); -void get_core_cmd(void); -void communication_task(void); #endif /* COMMUNICATION_H */ Modified: firmware/tuxaudio/trunk/flash.c =================================================================== --- firmware/tuxaudio/trunk/flash.c 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/flash.c 2008-05-07 12:42:28 UTC (rev 1130) @@ -27,6 +27,7 @@ #include "i2c.h" #include "flash.h" #include "AT26F004.h" +#include "common/commands.h" #include "common/api.h" /* Declarations */ @@ -149,14 +150,14 @@ if (ad[0] > 0x07) { programming_state = PROG_END; - send_status_p(STATUS_FLASH_PROG_CMD, FLASH_FULL, 0, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, FLASH_FULL, 0, 0); } else programming_state ++; frame_without_sound = 5000; frame_without_sound_timeout = 5000; - send_status_p(STATUS_FLASH_PROG_CMD, IN_PROGRESS, ad[0], ad[1]); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, IN_PROGRESS, ad[0], ad[1]); } else if (programming_state == PROG_INIT) @@ -177,12 +178,12 @@ if (sound_stored) { last_block = (ad[0] << 4) + (ad[1] >> 4); - send_status_p(STATUS_FLASH_PROG_CMD, WAITING_FOR_CONFIRMATION, last_block - first_block, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, WAITING_FOR_CONFIRMATION, last_block - first_block, 0); programming_state ++; } else { - send_status_p(STATUS_FLASH_PROG_CMD, NO_SOUND, 0, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, NO_SOUND, 0, 0); programming_state = PROG_END; } } @@ -195,7 +196,7 @@ } else if (write_toc == 2) { - send_status_p(STATUS_FLASH_PROG_CMD, ERASING_LAST_SOUND, 0, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, ERASING_LAST_SOUND, 0, 0); if (first_block == 0) { eraseFlag = 1; @@ -214,7 +215,7 @@ } else if (programming_state == PROG_TOC) { - send_status_p(STATUS_FLASH_PROG_CMD, WRITE_TOC, 0, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, WRITE_TOC, 0, 0); numSound ++; index = (numSound * 3) + 1; program_flash(0x00, (index>>8), (index & 0xFF), ad[0]); @@ -232,8 +233,8 @@ programming_state = 0; programmingFlash = 0; TIMSK0 = 0x01; - send_status_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); - send_status_p(SOUND_VAR_CMD, numSound, last_block, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); + queue_rf_cmd_p(SOUND_VAR_CMD, numSound, last_block, 0); } } @@ -284,7 +285,7 @@ } else if (!(read_status() & BUSY)) { - //send_status_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); + //queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); enter = 1; eraseFlag = 0; program_flash(0x00, 0x00, 0x00, 0xFE); @@ -293,8 +294,8 @@ program_flash(0x00, 0x00, 0x03, 0x00); numSound = 0; last_block = 0; - send_status_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); - send_status_p(SOUND_VAR_CMD, numSound, last_block, 0); + queue_rf_cmd_p(STATUS_FLASH_PROG_CMD, STANDBY, 0, 0); + queue_rf_cmd_p(SOUND_VAR_CMD, numSound, last_block, 0); TIMSK0 = 0x01; } } @@ -488,7 +489,7 @@ { soundToPlay = 0; flashPlay = 0; - send_status_p(STATUS_AUDIO_CMD, 0, 0, 0); + queue_rf_cmd_p(STATUS_AUDIO_CMD, 0, 0, 0); PORTB |= 0x01; // Set the HOLD signal PORTB |= 0x02; // Chip Deselect } Modified: firmware/tuxaudio/trunk/main.c =================================================================== --- firmware/tuxaudio/trunk/main.c 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/main.c 2008-05-07 12:42:28 UTC (rev 1130) @@ -90,7 +90,7 @@ PRR = PRR_bak; init_avr(); initCommunicationBuffers(); - i2cCommunicationInit(); + communication_init(); leave_deep_sleep(); } @@ -137,7 +137,7 @@ command[3] = 1; else command[3] = 0; - send_core_cmd(command); /* push the command set on the filo stack */ + queue_core_cmd(command); /* push the command set on the filo stack */ } int main(void) @@ -149,7 +149,7 @@ config_init(); /* load the configuration defaults from EEPROM */ numSound = readFlashNumber(); last_block = readLastBlock(numSound); - i2cCommunicationInit(); /* I2C initialization */ + communication_init(); /* I2C initialization */ frame_without_sound_timeout = TTS_TIMEOUT; sei(); /* Init global interrupt */ @@ -178,7 +178,7 @@ /* XXX there are some empty commands getting through, removed from * here for now. */ if (spi_commandRX && spi_commandRX[0]) - send_core_cmd(spi_commandRX); + queue_core_cmd(spi_commandRX); } if (programmingFlash) // Restora all the context for flash programming @@ -211,7 +211,7 @@ sendSensors(); /* wait for all status to be sent before going to sleep */ /* TODO fix these conditions for the sleep */ - //if (isFifoEmpty(statusFifo)) + //if (isFifoEmpty(rf_cmdout_buf)) /* wait for audio commands to be processed */ //if (!audioBufIdx && !flashPlay) Modified: firmware/tuxaudio/trunk/misc.c =================================================================== --- firmware/tuxaudio/trunk/misc.c 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/misc.c 2008-05-07 12:42:28 UTC (rev 1130) @@ -25,6 +25,7 @@ #include "misc.h" #include "communication.h" #include "version.h" +#include "varis.h" /* * Version number @@ -48,9 +49,9 @@ /* Retrieve version information */ for (i = 0; i < 12; i++) buf[i] = pgm_read_byte(data++); - send_status((uint8_t const *) buf); - send_status((uint8_t const *) buf+4); - send_status((uint8_t const *) buf+8); + queue_rf_cmd((uint8_t const *) buf); + queue_rf_cmd((uint8_t const *) buf+4); + queue_rf_cmd((uint8_t const *) buf+8); /* Send extra information */ - send_status_p(SOUND_VAR_CMD, numSound, last_block, 0); + queue_rf_cmd_p(SOUND_VAR_CMD, numSound, last_block, 0); } Modified: firmware/tuxaudio/trunk/parser.c =================================================================== --- firmware/tuxaudio/trunk/parser.c 2008-05-07 11:12:02 UTC (rev 1129) +++ firmware/tuxaudio/trunk/parser.c 2008-05-07 12:42:28 UTC (rev 1130) @@ -91,7 +91,7 @@ audioLevel = cmd[2]; //playingAudio(cmd[1]); /* start playing the sound */ soundToPlay = cmd[1]; - send_status_p(STATUS_AUDIO_CMD, cmd[1], 0, 0); + queue_rf_cmd_p(STATUS_AUDIO_CMD, cmd[1], 0, 0); flashPlay = 1; flash_state = 1; } |