poppy_com  0.1
Poppy2.0 communication library
 All Classes Files Functions Variables Enumerations Enumerator Pages
poppyNetwork.c
1 /*
2  * poppyNetwork.c
3  *
4  * Created: 16/02/2015 11:53:28
5  * Author: nico
6  * Abstract: basics functionalities of the Poppy communication protocol
7  */
9 #include "poppy-com/inc/i2c_master.h"
10 #include "poppy-com/inc/i2c_slave.h"
11 #include "poppy-com/inc/context.h"
12 #include HAL
13 
14 extern context_t ctx;
15 
16 // Startup and network configuration
18  RX_CB rx_cb,
19  RX_CB rxgc_cb) {
20  hal_init();
21 
22  // Save context
23  // User side slave TX callback
24  ctx.tx_cb = tx_cb;
25  // User side slave RX callback
26  ctx.rx_cb = rx_cb;
27  // User side slave RX general call callback
28  ctx.rxgc_cb = rxgc_cb;
29 
30  // Data callback
31  ctx.data_cb = idle;
32 
33  // Module id
34  ctx.id = DEFAULTID;
35  // Module type
36  ctx.type = MODULETYPE;
37 
38  // Status
39  ctx.status = (status_t) {.rx_error = FALSE,
40  .master_write = FALSE,
41  .unexpected_state = FALSE,
42  .warning = FALSE};
43 }
44 
45 unsigned char poppyNetwork_read(unsigned char addr, msg_t *msg,
46  unsigned char reply_size) {
47  unsigned char i = 0;
48 
49  // Write the command
50  if (i2cAddr(addr, TX)) {
51  i2c_transmit(STOP);
52  return 1;
53  }
54  if (i2cWrite(msg->reg)) {
55  i2c_transmit(STOP);
56  return 1;
57  }
58  if (i2cWrite(msg->size)) {
59  i2c_transmit(STOP);
60  return 1;
61  }
62  for (i = 0; i < msg->size; i++) {
63  if (i2cWrite(msg->data[i])) {
64  i2c_transmit(STOP);
65  return 1;
66  }
67  }
68 
69  // Read the reply
70  if (i2cAddr(addr, RX)) {
71  i2c_transmit(STOP);
72  return 1;
73  }
74  msg->size = reply_size;
75  for (i = 0; i < msg->size; i++) {
76  if (i2cRead(FALSE, &msg->data[i])) {
77  i2c_transmit(STOP);
78  return 1;
79  }
80  }
81  i2c_transmit(STOP);
82  return 0;
83 }
84 
85 unsigned char poppyNetwork_write(unsigned char addr, msg_t *msg) {
86  if (i2cAddr(addr, TX)) {
87  i2c_transmit(STOP);
88  return 1;
89  }
90  // Write DATA
91  i2cWrite(msg->reg);
92  i2cWrite(msg->size);
93  for (unsigned char i = 0; i < msg->size; i++) {
94  i2cWrite(msg->data[i]);
95  }
96  i2cWrite(crc(&msg->data[0], msg->size));
97  i2c_transmit(STOP);
98  return 0;
99 }
RX_CB rxgc_cb
Definition: context.h:39
unsigned char id
Definition: context.h:42
RX_CB rx_cb
Definition: context.h:38
unsigned char size
Definition: poppyNetwork.h:40
unsigned char poppyNetwork_write(unsigned char addr, msg_t *msg)
Master mode write function.
Definition: poppyNetwork.c:85
void tx_cb(msg_t *msg)
Callback function for Slave mode messages transmission.
Definition: template.c:73
unsigned char type
Definition: context.h:43
void poppyNetwork_init(TX_CB tx_cb, RX_CB rx_cb, RX_CB rxgc_cb)
Initialisation of the Poppy communication lib.
Definition: poppyNetwork.c:17
unsigned char data[512]
Definition: poppyNetwork.h:41
Message structure.
Definition: poppyNetwork.h:38
TX_CB tx_cb
Definition: context.h:37
status_t status
Definition: context.h:46
DATA_CB data_cb
Definition: context.h:36
void rxgc_cb(msg_dir_t dir, msg_t *msg)
Callback function for Slave mode messages reception with general call.
Definition: template.c:61
Poppy communication main include file.
unsigned char reg
Definition: poppyNetwork.h:39
void rx_cb(msg_dir_t dir, msg_t *msg)
Callback function for Slave mode messages reception.
Definition: template.c:48