From c0185e5835778cbbf4080dfbcbef59768c01aca8 Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Sat, 16 May 2009 01:22:59 +0200 Subject: * digital/avr/modules/twi: - return length of read data when using twi_sl_poll. --- digital/avr/modules/twi/twi.avr.c | 17 +++++++++++++---- digital/avr/modules/twi/twi.h | 4 +++- digital/avr/modules/twi/twi.host.c | 12 +++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) (limited to 'digital') diff --git a/digital/avr/modules/twi/twi.avr.c b/digital/avr/modules/twi/twi.avr.c index 3452b36f..dc07ede2 100644 --- a/digital/avr/modules/twi/twi.avr.c +++ b/digital/avr/modules/twi/twi.avr.c @@ -60,6 +60,7 @@ static volatile uint8_t send_buf_sl2[AC_TWI_SL_SEND_BUFFER_SIZE]; static volatile uint8_t *send_sys_sl, *send_user_sl; static volatile uint8_t update_sl; /* lock pour savoir si on peut switcher les buffers sans risque */ static volatile uint8_t send_switch_sl; +static volatile uint8_t rcpt_idx_sl = 0; #endif /* AC_TWI_SLAVE_ENABLE */ #if AC_TWI_MASTER_ENABLE static volatile int8_t state_ms; @@ -101,11 +102,20 @@ twi_sl_poll (uint8_t *buffer, uint8_t size) if (data_ready_sl) { data_ready_sl = 0; - while (size --) - buffer[size] = rcpt_buf_sl[size]; + uint8_t to_read, read; + /* Get how many data we can really read. */ + if (size < rcpt_idx_sl) + /* FIXME: this case sucks. */ + to_read = size; + else + to_read = rcpt_idx_sl; + /* Copy amount of data read. */ + read = to_read; + while (to_read --) + buffer[to_read] = rcpt_buf_sl[to_read]; /* de nouveau dispo pour traiter de nouvelles requetes */ TWCR |= _BV (TWEA); - return 1; + return read; } else return 0; @@ -193,7 +203,6 @@ SIGNAL (SIG_2WIRE_SERIAL) { #if AC_TWI_SLAVE_ENABLE static uint8_t send_idx_sl = 0; - static uint8_t rcpt_idx_sl = 0; #endif /* AC_TWI_SLAVE_ENABLE */ #if AC_TWI_MASTER_ENABLE static uint8_t idx_ms; diff --git a/digital/avr/modules/twi/twi.h b/digital/avr/modules/twi/twi.h index 6944daa3..a96986bf 100644 --- a/digital/avr/modules/twi/twi.h +++ b/digital/avr/modules/twi/twi.h @@ -30,7 +30,9 @@ void twi_init (uint8_t addr); #if AC_TWI_SLAVE_ENABLE -/** Récupère dans buffer les données recues en tant qu'esclave */ +/** Récupère dans buffer les données recues en tant qu'esclave. + * @return the amout of read data. + */ uint8_t twi_sl_poll (uint8_t *buffer, uint8_t size); /** Met à jour le buffer de donnée à envoyer */ diff --git a/digital/avr/modules/twi/twi.host.c b/digital/avr/modules/twi/twi.host.c index bec83892..69c4e0a8 100644 --- a/digital/avr/modules/twi/twi.host.c +++ b/digital/avr/modules/twi/twi.host.c @@ -46,6 +46,8 @@ static uint8_t twi_address; /** Received data. */ static uint8_t rcpt_buf_sl[AC_TWI_SL_RECV_BUFFER_SIZE]; +/** Received data size. */ +static uint8_t rcpt_size_sl; /** Whether new received data are ready. */ static uint8_t data_ready_sl; /** Data sent on master request. */ @@ -79,12 +81,15 @@ twi_init (uint8_t addr) uint8_t twi_sl_poll (uint8_t *buffer, uint8_t size) { + uint8_t i; if (data_ready_sl) { data_ready_sl = 0; - while (size--) - buffer[size] = rcpt_buf_sl[size]; - return 1; + if (size > rcpt_size_sl) + size = rcpt_size_sl; + for (i = 0; i < size; i++) + buffer[i] = rcpt_buf_sl[i]; + return size; } else return 0; @@ -124,6 +129,7 @@ twi_handle_WRITE (void *user, mex_msg_t *msg) size = mex_msg_len (msg); assert (size <= AC_TWI_SL_RECV_BUFFER_SIZE); memcpy (rcpt_buf_sl, mex_msg_pop_buffer (msg), size); + rcpt_size_sl = size; data_ready_sl = 1; } } -- cgit v1.2.3