[tuxdroid-svn] r1133 - firmware/tuxcore/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2008-05-08 09:46:36
|
Author: jaguarondi Date: 2008-05-08 11:46:35 +0200 (Thu, 08 May 2008) New Revision: 1133 Modified: firmware/tuxcore/trunk/communication.c firmware/tuxcore/trunk/communication.h firmware/tuxcore/trunk/i2c.c firmware/tuxcore/trunk/i2c.h firmware/tuxcore/trunk/main.c firmware/tuxcore/trunk/parser.c firmware/tuxcore/trunk/parser.h Log: * Here's the cleaning of the communication module in tuxcore. Things are much simpler and comments have been updated. I'm now done with the communication/I2C, except for bug fixes of course. Modified: firmware/tuxcore/trunk/communication.c =================================================================== --- firmware/tuxcore/trunk/communication.c 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/communication.c 2008-05-08 09:46:35 UTC (rev 1133) @@ -22,47 +22,36 @@ #include <avr/io.h> #include <avr/interrupt.h> +#include "common/defines.h" #include "global.h" #include "communication.h" #include "i2c.h" -/* - * Buffer for commands received from tuxaudio - */ -FIFO_INSTANCE(cmd_inbuf, CMD_IN_BUF_SIZE); -fifo_t *cmdin = FifoPointer(cmd_inbuf); +/** Size of the incoming stack buffer. */ +#define CMD_IN_BUF_SIZE 32 +/** Size of the outgoing stack buffer. */ +#define CMD_OUT_BUF_SIZE 32 -/* - * audioIntBuf is a single buffer for audio commands to be sent to the audio - * interface - * - * audioIntBufIdx should be set to the number of valid bytes in the buffer, - * depending on the number of parameters associated with the command. It is - * cleared when the command is sent so it can be used to check whether the - * buffer is empty or not. - */ -uint8_t audioIntBuf[CMD_SIZE]; /* single buffer for audio commands to be sent to the audio interface */ -uint8_t audioIntBufIdx = 0; /* index indicating the number of valid bytes in the buffer */ +FIFO_INSTANCE(cmdin_buf, CMD_IN_BUF_SIZE); +/** Stack for commands received from tuxaudio */ +fifo_t *cmdin = FifoPointer(cmdin_buf); -/** \brief Fifo used to buffer status datas which is to be sent over i2c to tuxaudio. - * Declare a fifo (statusFifo_s) and a pointer. - */ -FIFO_INSTANCE(statusFifo_s, CMD_OUT_BUF_SIZE); -fifo_t *statusFifo = FifoPointer(statusFifo_s); +FIFO_INSTANCE(cmdout_s, CMD_OUT_BUF_SIZE); +/** Stack for commands to tuxaudio */ +fifo_t *cmdout = FifoPointer(cmdout_s); -/* - * i2cSlaveReceiveService is called at the end of the i2c - * slave reception - * - * We basically do a transfer of the received data to the - * commandBuf here. +/** + * \brief Called at the end of the I2C slave reception to process received + * data. + * \param data_length Length of the data buffer. + * \param data Buffer of data received. */ -void i2cSlaveReceiveService(uint8_t length, uint8_t *data) +void i2c_slave_receive_service(uint8_t data_length, uint8_t *data) { uint8_t i; /* We should have the right number of bytes. */ - if (length != CMD_SIZE) + if (data_length != CMD_SIZE) { gerror = GERROR_INV_RECEIVE_LENGTH; return; @@ -77,38 +66,34 @@ FifoPut(cmdin, data[i]); } -/* - * i2cSlaveTransmitService is called when an I2C read transaction occurs. It - * fetches data to be sent to tuxaudio. +/** + * \brief Called when an I2C read transaction occurs. It fetches data to be + * sent to tuxaudio. + * \param data_length Length of the data buffer. + * \param data Buffer of data to be transmitted. + * \return 0 if nothing has to be sent, 1 otherwise. */ -uint8_t i2cSlaveTransmitService(uint8_t transmitDataLength, uint8_t* transmitData) +uint8_t i2c_slave_transmit_service(uint8_t data_length, uint8_t* data) { uint8_t i; - if (audioIntBufIdx) /* one audio command to send */ + if (!FifoLength(cmdout)) + return 0; + + for (i = 0; i < data_length; i++) { - for (i = 0; i < audioIntBufIdx; i++) - transmitData[i] = audioIntBuf[i]; - for (; i < 4; i++) - transmitData[i] = 0; - audioIntBufIdx = 0; - return 1; - } - else if (FifoLength(statusFifo)) /* if no audio and there's some status to send, get the next value */ - { - for (i = 0; i < 4; i++) /* so we loop 4 times here to get all command bytes */ + if (FifoGet(cmdout, &data[i]) != FIFO_OK) { - i2cSendData[i] = 0; - FifoGet(statusFifo, &transmitData[i]); /* TODO add a test on returned value to detect any corrupted fifo */ + gerror = GERROR_CMDOUTBUF_EMPTY; + return 0; } - return 1; } - return 0; + return 1; } -/* - * Get a command from the stack of received commands from tuxaudio. - * Returns 0 if the stack is empty, 1 if a command has been received +/** + * \brief Get a command from the stack of received commands from tuxaudio. + * \return 0 if the stack is empty, 1 if a command has been received * successfully. */ int8_t get_cmd(uint8_t *cmd) @@ -117,6 +102,7 @@ if (!FifoLength(cmdin)) return 0; + for (i=0; i<CMD_SIZE; i++) { if (FifoGet(cmdin, &cmd[i]) != FIFO_OK) @@ -128,20 +114,21 @@ return 1; }; -/* - * Add a command on the status stack to be sent to tuxaudio. - * Returns 0 if the stack is full, 1 if the command has been added +/** + * \brief Add a command on the status stack to be sent to tuxaudio. + * \param cmd Command array. + * \return 0 if the stack is full, 1 if the command has been added * successfully. */ -int8_t send_cmd(uint8_t *cmd) +int8_t queue_cmd(uint8_t *cmd) { uint8_t i; - if (FifoLength(statusFifo) > FifoSize(statusFifo) - CMD_SIZE) + if (FifoLength(cmdout) > FifoSize(cmdout) - CMD_SIZE) return 0; cli(); for (i=0; i<CMD_SIZE; i++) - if (FifoPut(statusFifo, cmd[i]) != FIFO_OK) + if (FifoPut(cmdout, cmd[i]) != FIFO_OK) { gerror = GERROR_CMDOUTBUF_FULL; return 0; @@ -150,41 +137,54 @@ return 1; } -/* - * Add a command on the status stack to be sent to tuxaudio. - * Returns 0 if the stack is full, 1 if the command has been added +/** + * \brief Add a command on the status stack to be sent to tuxaudio. + * \param cmd Command byte. + * \param param1 First parameter. + * \param param2 Second parameter. + * \param param3 Third parameter. + * \return 0 if the stack is full, 1 if the command has been added * successfully. */ -int8_t send_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ +int8_t queue_cmd_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ uint8_t param3) { uint8_t c[4] = {cmd , param1, param2, param3}; - return send_cmd(c); + return queue_cmd(c); } -/* - * Returns true if all commands are sent, false otherwise. +/** + * \brief Check if all commands are completely sent through I2C. + * \return true if all commands are sent, false otherwise. */ bool cmds_sent(void) { - return !(FifoLength(statusFifo) || audioIntBufIdx || - (i2c_get_status() == I2C_BUSY)); + return !(FifoLength(cmdout) || (i2c_get_status() == I2C_BUSY)); } +/** + * \brief Check if the command stack is empty. + * \return true if stack is empty. + * It's possible that the stack is empty but the command is in the I2C buffer. + * This doesn't ensure that the command has been received on the other side. + */ bool cmds_empty(void) { - return !FifoLength(statusFifo); + return !FifoLength(cmdout); } /** - * I2C communication initialization + * \brief I2C communication initialization * * Initialize the I2C interface in slave mode. */ void communication_init(void) { i2c_init(); - i2c_slave_receive_handler(i2cSlaveReceiveService); /* set receive function */ - i2c_slave_transmit_handler(i2cSlaveTransmitService); /* set receive function */ - FifoClear(statusFifo); + + /* Set receive and transmit handlers. */ + i2c_slave_receive_handler(i2c_slave_receive_service); + i2c_slave_transmit_handler(i2c_slave_transmit_service); + + FifoClear(cmdout); } Modified: firmware/tuxcore/trunk/communication.h =================================================================== --- firmware/tuxcore/trunk/communication.h 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/communication.h 2008-05-08 09:46:35 UTC (rev 1133) @@ -26,28 +26,14 @@ #include "common/commands.h" #include "fifo.h" -#define CMD_OUT_BUF_SIZE 32 -#define CMD_IN_BUF_SIZE 32 -#define CMD_SIZE 4 -#define AFTER_NACK_DELAY 6 -enum CMD_TYPE -{ AUDIO_CMD, STATUS_CMD }; - -extern uint8_t audioIntBuf[CMD_SIZE]; /* single buffer for commands received over i2c to control the audio interface */ -extern uint8_t audioIntBufIdx; /* index indicating the number of valid bytes in the buffer */ - -extern fifo_t *statusFifo; - -extern uint8_t errorBuf[2]; /* buffer for command errors, should contain command number and error code */ - void communication_init(void); + int8_t get_cmd(uint8_t *cmd); -int8_t send_cmd_p(uint8_t command, uint8_t param1, uint8_t param2, \ +int8_t queue_cmd_p(uint8_t command, uint8_t param1, uint8_t param2, \ uint8_t param3); -int8_t send_cmd(uint8_t *status); +int8_t queue_cmd(uint8_t *status); bool cmds_sent(void); bool cmds_empty(void); -uint8_t fetchCommands(void); #endif /* COMMUNICATION_H */ Modified: firmware/tuxcore/trunk/i2c.c =================================================================== --- firmware/tuxcore/trunk/i2c.c 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/i2c.c 2008-05-08 09:46:35 UTC (rev 1133) @@ -22,15 +22,16 @@ #include <avr/io.h> #include <avr/interrupt.h> +#include "common/defines.h" #include "i2c.h" #define TWI_TWCR (_BV(TWINT) | _BV(TWIE) | _BV(TWEA) | _BV(TWEN)) /* send/transmit buffer (outgoing data) */ -uint8_t i2cSendData[4]; +uint8_t i2cSendData[CMD_SIZE]; uint8_t i2cSendDataIndex; /* receive buffer (incoming data) */ -uint8_t i2cReceiveData[4]; +uint8_t i2cReceiveData[CMD_SIZE]; uint8_t i2cReceiveDataIndex; /* Modified: firmware/tuxcore/trunk/i2c.h =================================================================== --- firmware/tuxcore/trunk/i2c.h 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/i2c.h 2008-05-08 09:46:35 UTC (rev 1133) @@ -67,7 +67,6 @@ extern uint8_t i2cDeviceAddrRW; /* send/transmit buffer (outgoing data) */ -extern uint8_t i2cSendData[]; extern uint8_t i2cSendDataIndex; extern uint8_t i2cSendDataLength; Modified: firmware/tuxcore/trunk/main.c =================================================================== --- firmware/tuxcore/trunk/main.c 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/main.c 2008-05-08 09:46:35 UTC (rev 1133) @@ -203,7 +203,7 @@ if (pingCnt && cmds_empty()) { pingCnt--; - send_cmd_p(PONG_CMD, pingCnt, 0, 0); + queue_cmd_p(PONG_CMD, pingCnt, 0, 0); } /* Parse and execute received commands. */ parse_received_cmd(); @@ -253,16 +253,16 @@ static void updateStatus(void) { - send_cmd_p(STATUS_SENSORS1_CMD, gStatus.sw, gStatus.audio_play, + queue_cmd_p(STATUS_SENSORS1_CMD, gStatus.sw, gStatus.audio_play, gStatus.audio_status); - send_cmd_p(STATUS_PORTS_CMD, PINB, PINC, PIND); - send_cmd_p(STATUS_POSITION1_CMD, eyes_move_counter, mouth_move_counter, + queue_cmd_p(STATUS_PORTS_CMD, PINB, PINC, PIND); + queue_cmd_p(STATUS_POSITION1_CMD, eyes_move_counter, mouth_move_counter, flippers_move_counter); - send_cmd_p(STATUS_POSITION2_CMD, spin_move_counter, gStatus.pos, 0); + queue_cmd_p(STATUS_POSITION2_CMD, spin_move_counter, gStatus.pos, 0); if (led_f) { led_f = false; - send_cmd_p(STATUS_LED_CMD, left_led.status.intensity, + queue_cmd_p(STATUS_LED_CMD, left_led.status.intensity, right_led.status.intensity, left_led.status.fading | (left_led.status.pulsing << 1) | @@ -274,20 +274,20 @@ if (sensorsStatus & LIGHT_FLAG) /* send light measurement */ { sensorsStatus &= ~LIGHT_FLAG; - send_cmd_p(STATUS_LIGHT_CMD, gStatus.lightH, gStatus.lightL, gStatus.lightM); + queue_cmd_p(STATUS_LIGHT_CMD, gStatus.lightH, gStatus.lightL, gStatus.lightM); } if (sensorsStatus & BATTERY_FLAG) /* send battery measurement */ { sensorsStatus &= ~BATTERY_FLAG; - send_cmd_p(STATUS_BATTERY_CMD, gStatus.batteryH, gStatus.batteryL, gStatus.batteryS); + queue_cmd_p(STATUS_BATTERY_CMD, gStatus.batteryH, gStatus.batteryL, gStatus.batteryS); } if (ir_f) /* send received ir signals */ { ir_f--; - send_cmd_p(STATUS_IR_CMD, gStatus.ir, ir_f, gStatus.ir); + queue_cmd_p(STATUS_IR_CMD, gStatus.ir, ir_f, gStatus.ir); } if (gerror) - send_cmd_p(GERROR_CMD, TUXCORE_CPU_NUM, gerror, 0); + queue_cmd_p(GERROR_CMD, TUXCORE_CPU_NUM, gerror, 0); sensorsUpdate |= STATUS_SENT; } Modified: firmware/tuxcore/trunk/parser.c =================================================================== --- firmware/tuxcore/trunk/parser.c 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/parser.c 2008-05-08 09:46:35 UTC (rev 1133) @@ -75,29 +75,24 @@ /* Sound */ else if (cmd[0] == PLAY_SOUND_CMD) { - for (i = 0; i < 3; i++) /* forwards the cmd to the audio CPU */ - audioIntBuf[i] = cmd[i]; - audioIntBufIdx = 3; + /* Forward the cmd to the audio CPU. */ + queue_cmd(cmd); return; } else if (cmd[0] == MUTE_CMD) { - for (i = 0; i < 3; i++) /* forwards the cmd to the audio CPU */ - audioIntBuf[i] = cmd[i]; - audioIntBufIdx = 3; + /* Forward the cmd to the audio CPU. */ + queue_cmd(cmd); return; } - /* Sleep mode */ else if (cmd[0] == SLEEP_CMD) { - for (i = 0; i < 3; i++) /* forward the cmd to the audio CPU */ - audioIntBuf[i] = cmd[i]; - audioIntBufIdx = 3; + /* Forward the cmd to the audio CPU. */ + queue_cmd(cmd); cond_flags.sleep = COND_PRE_SLEEP; return; } - /* Version */ else if (cmd[0] == INFO_TUXCORE_CMD) { @@ -106,9 +101,9 @@ for (i = 0; i < 12; i++) info[i] = pgm_read_byte(p++); - send_cmd(&info[0]); - send_cmd(&info[4]); - send_cmd(&info[8]); + queue_cmd(&info[0]); + queue_cmd(&info[4]); + queue_cmd(&info[8]); return; } /* Reset condition flags */ Modified: firmware/tuxcore/trunk/parser.h =================================================================== --- firmware/tuxcore/trunk/parser.h 2008-05-07 19:54:14 UTC (rev 1132) +++ firmware/tuxcore/trunk/parser.h 2008-05-08 09:46:35 UTC (rev 1133) @@ -22,7 +22,7 @@ #ifndef PARSER_H #define PARSER_H -void parse_cmd(uint8_t cmd[CMD_SIZE]); +void parse_cmd(uint8_t cmd[]); void parse_received_cmd(void); #endif /* PARSER_H */ |