From 63a0f3b710ebd6651a159aef16f2195396290aa9 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 31 Mar 2008 18:43:51 +0200 Subject: * digital/avr/modules/twi: - added host support. --- digital/avr/modules/twi/test/Makefile.master | 4 +- digital/avr/modules/twi/test/Makefile.slave | 4 +- digital/avr/modules/twi/test/avrconfig_master.h | 4 +- digital/avr/modules/twi/test/test_twi_host.py | 32 ++++++++ digital/avr/modules/twi/test/test_twi_master.c | 23 +++++- digital/avr/modules/twi/test/test_twi_sl.c | 16 +++- digital/avr/modules/twi/twi.host.c | 99 +++++++++++++++++++++++-- 7 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 digital/avr/modules/twi/test/test_twi_host.py diff --git a/digital/avr/modules/twi/test/Makefile.master b/digital/avr/modules/twi/test/Makefile.master index 72762aa2..55a762c0 100644 --- a/digital/avr/modules/twi/test/Makefile.master +++ b/digital/avr/modules/twi/test/Makefile.master @@ -1,9 +1,9 @@ BASE = ../../.. -AVR_PROGS = test_twi_master +PROGS = test_twi_master test_twi_master_SOURCES = test_twi_master.c DOC = EXTRACTDOC = -MODULES = twi uart proto math/random +MODULES = twi uart proto math/random utils CONFIGFILE = avrconfig_master.h diff --git a/digital/avr/modules/twi/test/Makefile.slave b/digital/avr/modules/twi/test/Makefile.slave index 73d3b01e..35f84bcd 100644 --- a/digital/avr/modules/twi/test/Makefile.slave +++ b/digital/avr/modules/twi/test/Makefile.slave @@ -1,9 +1,9 @@ BASE = ../../.. -AVR_PROGS = test_twi_sl +PROGS = test_twi_sl test_twi_sl_SOURCES = test_twi_sl.c DOC = EXTRACTDOC = -MODULES = twi uart proto +MODULES = twi uart proto utils CONFIGFILE = avrconfig_slave.h diff --git a/digital/avr/modules/twi/test/avrconfig_master.h b/digital/avr/modules/twi/test/avrconfig_master.h index 21bc4888..a65f872f 100644 --- a/digital/avr/modules/twi/test/avrconfig_master.h +++ b/digital/avr/modules/twi/test/avrconfig_master.h @@ -36,9 +36,9 @@ /** Activate slave part. */ #define AC_TWI_SLAVE_ENABLE 0 /** Slave recv buffer size. */ -#define AC_TWI_SL_RECV_BUFFER_SIZE 1 +#define AC_TWI_SL_RECV_BUFFER_SIZE 16 /** Slave send buffer size. */ -#define AC_TWI_SL_SEND_BUFFER_SIZE 1 +#define AC_TWI_SL_SEND_BUFFER_SIZE 16 /* proto - Protocol module. */ /** Maximum argument size. */ diff --git a/digital/avr/modules/twi/test/test_twi_host.py b/digital/avr/modules/twi/test/test_twi_host.py new file mode 100644 index 00000000..67942276 --- /dev/null +++ b/digital/avr/modules/twi/test/test_twi_host.py @@ -0,0 +1,32 @@ +import sys +sys.path.append (sys.path[0] + '/../../../../../host/mex') + +from mex.hub import Hub +from mex.msg import Msg +from mex.node import Node +from mex.forked import Forked + +import os, signal, time + +def log (x): + print x + +h = Hub (min_clients = 3, log = log) +fh = Forked (h.wait) + +n = Node () +def nf (): + while True: + time.sleep (1) + n.wait (n.date + 1) +fn = Forked (nf) + +slave = os.popen ('./test_twi_sl.host', 'w') + +try: + os.system ('./test_twi_master.host') +finally: + fn.kill () + fh.kill () + slave.close () + time.sleep (1) diff --git a/digital/avr/modules/twi/test/test_twi_master.c b/digital/avr/modules/twi/test/test_twi_master.c index d65d185d..e032f839 100644 --- a/digital/avr/modules/twi/test/test_twi_master.c +++ b/digital/avr/modules/twi/test/test_twi_master.c @@ -31,6 +31,10 @@ #include "modules/math/random/random.h" #include "io.h" +#ifdef HOST +# include "modules/host/mex.h" +#endif + void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { @@ -121,6 +125,10 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) twi_ms_send (args[0], random_bytes, byte_number); while (!twi_ms_is_finished ()) ; +#ifdef HOST + /* Give time to slave to copy data. */ + mex_node_wait_date (mex_node_date () + 2); +#endif /* Received buffer */ uint8_t received_buffer[max_byte_number]; @@ -182,8 +190,12 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) } int -main (void) +main (int argc, char **argv) { + avr_init (argc, argv); +#ifdef HOST + mex_node_connect (); +#endif /* Enable interruptions */ sei (); /* Initialize serial port */ @@ -196,8 +208,13 @@ main (void) proto_send0 ('M'); while (42) { - uint8_t c = uart0_getc (); - proto_accept (c); +#ifdef HOST + mex_node_wait_date (mex_node_date () + 1); + while (uart0_poll ()) + proto_accept (uart0_getc ()); +#else + proto_accept (uart0_getc ()); +#endif } return 0; } diff --git a/digital/avr/modules/twi/test/test_twi_sl.c b/digital/avr/modules/twi/test/test_twi_sl.c index 020c9e6b..e52d914f 100644 --- a/digital/avr/modules/twi/test/test_twi_sl.c +++ b/digital/avr/modules/twi/test/test_twi_sl.c @@ -23,7 +23,6 @@ * * }}} */ - #include "common.h" #include "modules/twi/twi.h" #include "modules/proto/proto.h" @@ -31,6 +30,10 @@ #include "modules/utils/utils.h" #include "io.h" +#ifdef HOST +# include "modules/host/mex.h" +#endif + void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { @@ -51,8 +54,12 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) } int -main (void) +main (int argc, char **argv) { + avr_init (argc, argv); +#ifdef HOST + mex_node_connect (); +#endif /* Enable interruptions */ sei (); /* Initialize serial port */ @@ -65,6 +72,9 @@ main (void) proto_send0 ('S'); while (42) { +#ifdef HOST + mex_node_wait_date (mex_node_date () + 1); +#endif uint8_t data[AC_TWI_SL_RECV_BUFFER_SIZE]; data[0] = 0; /* Check for data */ @@ -73,7 +83,7 @@ main (void) /* Receive and store them */ twi_sl_update (data, AC_TWI_SL_RECV_BUFFER_SIZE); } - if (uart0_poll ()) + while (uart0_poll ()) proto_accept (uart0_getc ()); } return 0; diff --git a/digital/avr/modules/twi/twi.host.c b/digital/avr/modules/twi/twi.host.c index 526ebe62..bec83892 100644 --- a/digital/avr/modules/twi/twi.host.c +++ b/digital/avr/modules/twi/twi.host.c @@ -24,13 +24,53 @@ * }}} */ #include "common.h" #include "twi.h" +#include "modules/host/mex.h" -/* Empty for the moment. */ +#include + +/** This implementation should cover all usual cases, and assert in other + * cases. */ + +/** Read messages are sent as request. + * In request, first byte is address, second byte is length. + * In response, whole payload is data. */ +#define TWI_READ 0x90 +/** Write messages are sent directly. + * First byte is address, rest of payload is data. */ +#define TWI_WRITE 0x91 + +/** TWI address. */ +static uint8_t twi_address; + +#if AC_TWI_SLAVE_ENABLE + +/** Received data. */ +static uint8_t rcpt_buf_sl[AC_TWI_SL_RECV_BUFFER_SIZE]; +/** Whether new received data are ready. */ +static uint8_t data_ready_sl; +/** Data sent on master request. */ +static uint8_t send_buf_sl[AC_TWI_SL_SEND_BUFFER_SIZE]; + +/** Handle READ requests from master. */ +static void +twi_handle_READ (void *user, mex_msg_t *msg); + +/** Handle WRITE requests from master. */ +static void +twi_handle_WRITE (void *user, mex_msg_t *msg); + +#endif /* AC_TWI_SLAVE_ENABLE */ /** Initialise twi. */ void twi_init (uint8_t addr) { + twi_address = addr; +#if AC_TWI_SLAVE_ENABLE + data_ready_sl = 0; + mex_node_register (TWI_READ, twi_handle_READ, NULL); + mex_node_register (TWI_WRITE, twi_handle_WRITE, NULL); +#endif /* AC_TWI_SLAVE_ENABLE */ } #if AC_TWI_SLAVE_ENABLE @@ -39,13 +79,53 @@ twi_init (uint8_t addr) uint8_t twi_sl_poll (uint8_t *buffer, uint8_t size) { - return 0; + if (data_ready_sl) + { + data_ready_sl = 0; + while (size--) + buffer[size] = rcpt_buf_sl[size]; + return 1; + } + else + return 0; } /** Met à jour le buffer de donnée à envoyer */ void twi_sl_update (uint8_t *buffer, uint8_t size) { + while (size--) + send_buf_sl[size] = buffer[size]; +} + +/** Handle READ requests from master. */ +static void +twi_handle_READ (void *user, mex_msg_t *msg) +{ + u8 addr, size; + mex_msg_pop (msg, "BB", &addr, &size); + if (addr == twi_address) + { + assert (size <= AC_TWI_SL_SEND_BUFFER_SIZE); + mex_msg_t *m = mex_msg_new (TWI_READ); + mex_msg_push_buffer (m, send_buf_sl, size); + mex_node_response (m); + } +} + +/** Handle WRITE requests from master. */ +static void +twi_handle_WRITE (void *user, mex_msg_t *msg) +{ + u8 addr, size; + mex_msg_pop (msg, "B", &addr); + if (addr == twi_address) + { + size = mex_msg_len (msg); + assert (size <= AC_TWI_SL_RECV_BUFFER_SIZE); + memcpy (rcpt_buf_sl, mex_msg_pop_buffer (msg), size); + data_ready_sl = 1; + } } #endif /* AC_TWI_SLAVE_ENABLE */ @@ -56,21 +136,30 @@ twi_sl_update (uint8_t *buffer, uint8_t size) int8_t twi_ms_is_finished (void) { - return 0; + return 1; } /** Send len bytes of data to address */ int8_t twi_ms_send (uint8_t address, uint8_t *data, uint8_t len) { - return -1; + mex_msg_t *m = mex_msg_new (TWI_WRITE); + mex_msg_push (m, "B", address); + mex_msg_push_buffer (m, data, len); + mex_node_send (m); + return 0; } /** Read len bytes at addresse en put them in data */ int8_t twi_ms_read (uint8_t address, uint8_t *data, uint8_t len) { - return -1; + mex_msg_t *m = mex_msg_new (TWI_READ); + mex_msg_push (m, "BB", address, len); + m = mex_node_request (m); + assert (mex_msg_len (m) == len); + memcpy (data, mex_msg_pop_buffer (m), len); + return 0; } #endif /* AC_TWI_MASTER_ENABLE */ -- cgit v1.2.3