summaryrefslogtreecommitdiff
path: root/digital/beacon/src
diff options
context:
space:
mode:
authorFlorent Duchon2012-05-16 17:08:48 +0200
committerFlorent Duchon2012-05-16 17:43:09 +0200
commit1d36a77e506bb5beb5ac4d0a33d72be7faef7d47 (patch)
tree015e6604854078ef5c8e41f02edfb9400ba4c330 /digital/beacon/src
parent63cb73a6ee91deb2ca0f1458595eb45bc172632e (diff)
digital/beacon: add twi specific
Diffstat (limited to 'digital/beacon/src')
-rw-r--r--digital/beacon/src/main_avr.c26
-rw-r--r--digital/beacon/src/twi_specific.c96
-rw-r--r--digital/beacon/src/twi_specific.h68
3 files changed, 166 insertions, 24 deletions
diff --git a/digital/beacon/src/main_avr.c b/digital/beacon/src/main_avr.c
index 4f5cc5eb..180c2060 100644
--- a/digital/beacon/src/main_avr.c
+++ b/digital/beacon/src/main_avr.c
@@ -26,6 +26,7 @@
#include <types.h>
#include <util/delay.h>
#include "configuration.h"
+#include "twi_specific.h"
#include "calibration.h"
#include "codewheel.h"
#include "debug_avr.h"
@@ -45,32 +46,9 @@ AppState_t appState = APP_INITIAL_STATE; // application state
#else
DeviceType_t deviceType = DEVICE_TYPE_END_DEVICE;
#endif
-
-
-void twi_RXTX_update(void)
-{
- uint8_t TXbuffer[AC_TWI_SLAVE_SEND_BUFFER_SIZE];
- uint8_t RXbuffer[AC_TWI_SLAVE_RECV_BUFFER_SIZE];
- uint8_t RXlen;
-
- /* data to be communicated to the master */
- twi_slave_update (TXbuffer, sizeof (TXbuffer));
-
- /* Check for data. */
- RXlen = twi_slave_poll (RXbuffer, AC_TWI_SLAVE_RECV_BUFFER_SIZE);
-
- /* data availlable */
- if(RXlen != 0)
- {
- }
-}
void APL_TaskHandler(void)
{
- if(deviceType == DEVICE_TYPE_COORDINATOR)
- {
- twi_RXTX_update();
- }
switch (appState)
{
case APP_INITIAL_STATE:
@@ -85,7 +63,7 @@ void APL_TaskHandler(void)
{
case DEVICE_TYPE_COORDINATOR:
network_init();
- twi_init(AC_BEACON_TWI_ADDRESS);
+ twi_init_specific();
uprintf("COORDINATOR initialisation OK !\n\r");
break;
case DEVICE_TYPE_END_DEVICE:
diff --git a/digital/beacon/src/twi_specific.c b/digital/beacon/src/twi_specific.c
new file mode 100644
index 00000000..5def1a2b
--- /dev/null
+++ b/digital/beacon/src/twi_specific.c
@@ -0,0 +1,96 @@
+/* twi_specific.c */
+/* twi specific funtion. {{{
+ *
+ * Copyright (C) 2012 Florent Duchon
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+#include "modules/utils/byte.h"
+#include "modules/utils/crc.h"
+#include "twi_specific.h"
+#include "configuration.h"
+#include "position.h"
+#include "misc.h"
+#include "twi.h"
+#include "network.h"
+
+
+static HAL_AppTimer_t twiTimer;
+
+/* This function manages the TWI RX/RX transferts */
+void twi_task(void)
+{
+ uint8_t TXbuffer[AC_TWI_SLAVE_SEND_BUFFER_SIZE];
+ uint8_t RXbuffer[AC_TWI_SLAVE_RECV_BUFFER_SIZE];
+ uint8_t RXlen;
+ static uint8_t seq = 0;
+
+ TXbuffer[TWI_TX_RFU1_FIELD] = 0;
+ TXbuffer[TWI_TX_RFU2_FIELD] = 0;
+ TXbuffer[TWI_TX_SEQ_FIELD] = seq;
+
+ /* Opponent 1 */
+ TXbuffer[TWI_TX_X1_MSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_1,X), 1);
+ TXbuffer[TWI_TX_X1_LSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_1,X), 0);
+ TXbuffer[TWI_TX_Y1_MSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_1,Y), 1);
+ TXbuffer[TWI_TX_Y1_LSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_1,Y), 0);
+ TXbuffer[TWI_TX_TRUST1_FIELD] = position_get_trust(OPPONENT_1);
+
+ /* Opponent 2 */
+ TXbuffer[TWI_TX_X2_MSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_2,X), 1);
+ TXbuffer[TWI_TX_X2_LSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_2,X), 0);
+ TXbuffer[TWI_TX_Y2_MSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_2,Y), 1);
+ TXbuffer[TWI_TX_Y2_LSB_FIELD] = v16_to_v8 (position_get_coord(OPPONENT_2,Y), 0);
+ TXbuffer[TWI_TX_TRUST2_FIELD] = position_get_trust(OPPONENT_2);
+
+ /* Compute CRC */
+ TXbuffer[TWI_TX_CRC_FIELD] = crc_compute (&TXbuffer[1], sizeof (TXbuffer) - 1);
+
+ /* data to be communicated to the master */
+ twi_slave_update (TXbuffer, sizeof (TXbuffer));
+
+ /* Check for data. */
+ RXlen = twi_slave_poll (RXbuffer, AC_TWI_SLAVE_RECV_BUFFER_SIZE);
+
+ /* data availlable */
+ if(RXlen != 0)
+ {
+ if (crc_compute (RXbuffer + 1, RXlen - 1) == RXbuffer[0])
+ {
+ jack_update_status(RXbuffer[TWI_RX_JACK_FIELD]);
+ }
+ else
+ {
+ /* CRC not valid, dump values */
+ }
+ }
+}
+
+/* Initialisze specific TWI */
+void twi_init_specific(void)
+{
+ twi_init(AC_BEACON_TWI_ADDRESS);
+ twiTimer.interval = TWI_TASK_PERIOD;
+ twiTimer.mode = TIMER_REPEAT_MODE;
+ twiTimer.callback = twi_task;
+ HAL_StartAppTimer(&twiTimer);
+}
+
diff --git a/digital/beacon/src/twi_specific.h b/digital/beacon/src/twi_specific.h
new file mode 100644
index 00000000..ee244cb7
--- /dev/null
+++ b/digital/beacon/src/twi_specific.h
@@ -0,0 +1,68 @@
+/* twi_specific.h */
+/* twi specific funtion. {{{
+ *
+ * Copyright (C) 2012 Florent Duchon
+ *
+ * APBTeam:
+ * Web: http://apbteam.org/
+ * Email: team AT apbteam DOT org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+#ifndef _TWI_SPECIFIC_H
+#define _TWI_SPECIFIC_H
+
+#define TWI_TASK_PERIOD 4L
+
+
+typedef enum
+{
+ TWI_RX_CRC_FIELD,
+ TWI_RX_SEQ_FIELD,
+ TWI_RX_JACK_FIELD,
+ TWI_RX_NB_ADV_FIELD,
+ TWI_RX_X_MSB_FIELD,
+ TWI_RX_X_LSB_FIELD,
+ TWI_RX_Y_MSB_FIELD,
+ TWI_RX_Y_LSB_FIELD
+} TTWI_RX_field;
+
+typedef enum
+{
+ TWI_TX_CRC_FIELD,
+ TWI_TX_RFU1_FIELD,
+ TWI_TX_RFU2_FIELD,
+ TWI_TX_SEQ_FIELD,
+ TWI_TX_X1_MSB_FIELD,
+ TWI_TX_X1_LSB_FIELD,
+ TWI_TX_Y1_MSB_FIELD,
+ TWI_TX_Y1_LSB_FIELD,
+ TWI_TX_TRUST1_FIELD,
+ TWI_TX_X2_MSB_FIELD,
+ TWI_TX_X2_LSB_FIELD,
+ TWI_TX_Y2_MSB_FIELD,
+ TWI_TX_Y2_LSB_FIELD,
+ TWI_TX_TRUST2_FIELD
+} TTWI_TX_field;
+
+/* This function manages the TWI RX/RX transferts */
+void twi_task(void);
+
+/* Initialisze specific TWI */
+void twi_init_specific(void);
+
+#endif \ No newline at end of file