[tuxdroid-svn] r1124 - firmware/tuxaudio/trunk
Status: Beta
Brought to you by:
ks156
From: jaguarondi <c2m...@c2...> - 2008-05-06 17:13:54
|
Author: jaguarondi Date: 2008-05-06 19:13:30 +0200 (Tue, 06 May 2008) New Revision: 1124 Modified: firmware/tuxaudio/trunk/communication.c firmware/tuxaudio/trunk/communication.h firmware/tuxaudio/trunk/i2c.c firmware/tuxaudio/trunk/i2c.h firmware/tuxaudio/trunk/main.c Log: * I now use an I2C restart between 2 read from tuxcore. * When nacked, the new core_comunications() restart the request. I should still get rid of statusFlag. Modified: firmware/tuxaudio/trunk/communication.c =================================================================== --- firmware/tuxaudio/trunk/communication.c 2008-05-06 15:56:07 UTC (rev 1123) +++ firmware/tuxaudio/trunk/communication.c 2008-05-06 17:13:30 UTC (rev 1124) @@ -73,20 +73,6 @@ } /* - * I2C communication initalisation - * - * Start the twi interface in the mode defined in i2c.h, set the receiver function and - * the behaviour CPU address. - */ -void i2cCommunicationInit(void) -{ - i2c_init(); - msg_out.addr = TUXCORE_ADDR; - msg_out.buf = out_buf; - i2c_master_receive_handler(i2cMasterReceiveService); -} - -/* * sendCommands takes one command (from 1 to 4 bytes) out of the command stack * and send them by i2c. * @@ -95,50 +81,25 @@ int8_t sendCommands(void) { uint8_t i; - static uint8_t nack_cnt = 0; - if (i2c_get_status() == I2C_BUSY) - return 1; - if (msg_out.state == I2C_NACK) + if (FifoLength(core_cmdout)) + /* Send commands received from RF or testers to tuxcore only. */ { - /* If NACK, try a couple more time. */ - if (++nack_cnt == 4) + msg_out.len = CMD_SIZE; + for (i = 0; i < CMD_SIZE; i++) { - nack_cnt = 0; - switch (msg_out.addr) - { - case TUXCORE_ADDR: - /* Return 0 as if there were nothing to do so we can do - something else. Returning here doesn't drop the message. */ - return 0; - } + if (FifoGet(core_cmdout, &out_buf[i])) + /* Drop the data if the fifo is corrupted (not enough + * bytes) XXX add an error feedback on this */ + return -1; } - else - i2c_send_bytes(&msg_out); - return 2; + msg_out.addr = TUXCORE_ADDR; + i2c_send_bytes(&msg_out); } else - /* Send something else. */ - { - nack_cnt = 0; - if (FifoLength(core_cmdout)) - /* Send commands received from RF or testers to tuxcore only. */ - { - msg_out.len = CMD_SIZE; - for (i = 0; i < CMD_SIZE; i++) - { - if (FifoGet(core_cmdout, &out_buf[i])) - /* Drop the data if the fifo is corrupted (not enough - * bytes) XXX add an error feedback on this */ - return -1; - } - msg_out.addr = TUXCORE_ADDR; - i2c_send_bytes(&msg_out); - } - else - /* Nothing to do anymore */ - return 0; - } + /* Nothing to do anymore */ + return 0; + return 1; } @@ -215,28 +176,30 @@ * Only do a backup of the buffer so any new i2c data won't overwrite the * previous ones */ -void i2cMasterReceiveService(uint8_t receiveDataLength, uint8_t * receiveData) +bool i2cMasterReceiveService(struct i2c_msg *msg) { - if (receiveDataLength != CMD_SIZE) + if (msg->len != CMD_SIZE) /* Error here. */ - return; - if (*receiveData == 0) + return 0; + if (*(msg->buf) == 0) { /* Got nothing so stop reading. */ - statusFlag = 0; - return; + return 0; } - if (msg_in.addr == TUXCORE_ADDR) + 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(receiveData); + parse_cmd(cmd); /* and forward if it isn't dropped. */ - if (receiveData) - send_status(receiveData); - /* As we got something, there's maybe more so continue. */ - statusFlag = 1; + if (cmd) + send_status(cmd); + /* As we got something, there's maybe more so continue */ + return 1; } + return 0; } /* @@ -267,9 +230,43 @@ void core_communications(bool tick) { + /* If busy, pass. */ + if (i2c_get_status() == I2C_BUSY) + return; + + /* If nacked, restart. */ + if (msg_out.state == I2C_NACK) + { + i2c_send_bytes(&msg_out); + return; + } + else if (msg_in.state == I2C_NACK) + { + get_core_cmd(); + return; + } + if (!sendCommands()) { - if (statusFlag) + if (tick) + { get_core_cmd(); + statusFlag = 0; + } } } + +/* + * I2C communication initalisation + * + * Start the twi interface in the mode defined in i2c.h, set the receiver function and + * the behaviour CPU address. + */ +void i2cCommunicationInit(void) +{ + i2c_init(); + msg_out.addr = TUXCORE_ADDR; + msg_out.buf = out_buf; + i2c_master_receive_handler(i2cMasterReceiveService); +} + Modified: firmware/tuxaudio/trunk/communication.h =================================================================== --- firmware/tuxaudio/trunk/communication.h 2008-05-06 15:56:07 UTC (rev 1123) +++ firmware/tuxaudio/trunk/communication.h 2008-05-06 17:13:30 UTC (rev 1124) @@ -58,7 +58,6 @@ /* From i2c to RF */ void acceptData(void); -void i2cMasterReceiveService(uint8_t receiveDataLength, uint8_t * receiveData); void send_status(uint8_t const *status); void send_status_p(uint8_t cmd, uint8_t param1, uint8_t param2, \ uint8_t param3); Modified: firmware/tuxaudio/trunk/i2c.c =================================================================== --- firmware/tuxaudio/trunk/i2c.c 2008-05-06 15:56:07 UTC (rev 1123) +++ firmware/tuxaudio/trunk/i2c.c 2008-05-06 17:13:30 UTC (rev 1124) @@ -84,8 +84,7 @@ /* function pointer to i2c receive routine */ /* I2cSlaveReceive is called when this processor is addressed as a slave for * writing */ -static void (*i2c_master_receive) (uint8_t receiveDataLength, - uint8_t * recieveData); +static bool (*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, @@ -100,9 +99,7 @@ //i2c_resume(); /* Set the user function which handles receiving (incoming) data as a slave */ -void -i2c_master_receive_handler(void (*i2cMasterRx_func) - (uint8_t receiveDataLength, uint8_t * recieveData)) +void i2c_master_receive_handler(bool (*i2cMasterRx_func) (struct i2c_msg *msg)) { i2c_master_receive = i2cMasterRx_func; } @@ -364,8 +361,15 @@ i2c_state = I2C_FULL; m_msg->state = i2c_state; if (i2c_master_receive) - i2c_master_receive(m_msg->len, m_msg->buf); - twi_send_stop(); /* end of data stream */ + { + if (i2c_master_receive(m_msg)) + { + buf_idx = 0; + twi_send_start(); /* restart a read */ + } + else + twi_send_stop(); /* end of data stream */ + } break; case TW_MT_SLA_NACK: /* 0x20 */ /* SLA+W has been transmitted; NOT ACK has been received. */ Modified: firmware/tuxaudio/trunk/i2c.h =================================================================== --- firmware/tuxaudio/trunk/i2c.h 2008-05-06 15:56:07 UTC (rev 1123) +++ firmware/tuxaudio/trunk/i2c.h 2008-05-06 17:13:30 UTC (rev 1124) @@ -22,6 +22,7 @@ #ifndef _I2C_H_ #define _I2C_H_ +#include <stdbool.h> #include <util/twi.h> enum i2c_state @@ -79,10 +80,8 @@ /* Functions */ void i2cInit(void); +void i2c_master_receive_handler(bool (*i2cMasterRx_func) (struct i2c_msg *msg)); void -i2c_master_receive_handler(void (*i2cSlaveRx_func) - (uint8_t receiveDataLength, uint8_t * recieveData)); -void i2cSetSlaveTransmitHandler(uint8_t(*i2cSlaveTx_func) (uint8_t transmitDataLengthMax, uint8_t * transmitData)); Modified: firmware/tuxaudio/trunk/main.c =================================================================== --- firmware/tuxaudio/trunk/main.c 2008-05-06 15:56:07 UTC (rev 1123) +++ firmware/tuxaudio/trunk/main.c 2008-05-06 17:13:30 UTC (rev 1124) @@ -191,7 +191,7 @@ erase(); /* Send commands to I2C, otherwise get new status from tuxcore */ - core_communications(0); + core_communications(statusFlag); /*else if (sleep_f)*/ /*{*/ /*sleep_f = 0;*/ |