poppy_com  0.1
Poppy2.0 communication library
All Classes Files Functions Variables Enumerations Enumerator Pages
i2c_slave.c
1 #include "poppy-com/inc/i2c_slave.h"
2 #include HAL
3 
4 // Global variables
5 extern context_t ctx;
6 /*
7  * idle function is called when we are ready to receive or send a new message.
8  */
9 void idle(msg_dir_t dir, volatile unsigned char *data) {
10  static unsigned char *data_to_send;
11  static unsigned char msg_size = 0;
12  switch (dir) {
13  case TX:
14  /*
15  * At this point we should have something ready to send.
16  */
17  if (msg_size) {
18  /*
19  *This case is dedicated to protocol messages
20  */
21  msg_size--;
22  *data = *data_to_send++;
23  } else {
24  ctx.tx_cb(&ctx.msg);
25  }
26  break;
27  case RX:
28  case RXGC:
29  /*
30  * That should be a new message to receive.
31  */
32  ctx.msg.reg = *data;
33  switch (ctx.msg.reg) {
34  case GET_ID:
35  // Reply with ID
36  msg_size = 1;
37  data_to_send = &ctx.id;
38  break;
39  case WRITE_ID:
40  // Get and save a new given ID
41  ctx.msg.size = 1;
42  ctx.data_cb = get_data;
43  break;
44  case GET_MODULE_TYPE:
45  // Reply with module_type number
46  msg_size = 1;
47  data_to_send = &ctx.type;
48  break;
49  case GET_STATUS:
50  // Reply with a status register
51  msg_size = 1;
52  data_to_send = (unsigned char*)&ctx.status;
53  // TODO(NR) ca devrais reset le status...
54  break;
55  case GET_FIRM_REVISION:
56  // Reply with the actual firmware revision number
57  // TODO(NR)
58  break;
59  default:
60  ctx.data_cb = get_size;
61  break;
62  }
63  break;
64  case END:
65  if (msg_size > 0)
66  ctx.status.rx_error = TRUE;
67  msg_size = 0;
68  break;
69  }
70 }
71 
72 void get_size(msg_dir_t dir, volatile unsigned char *data) {
73  ctx.msg.size = *data;
74  ctx.data_cb = get_data;
75 }
76 
77 void get_data(msg_dir_t dir, volatile unsigned char *data) {
78  static unsigned char data_count = 0;
79  if (data_count < ctx.msg.size) {
80  ctx.msg.data[data_count++] = *data;
81  } else {
82  ctx.data_cb = idle;
83  data_count = 0;
84  if (*data == crc(&ctx.msg.data[0], ctx.msg.size))
85  msg_complete(dir);
86  else
87  ctx.status.rx_error = TRUE;
88  }
89 }
90 
91 void msg_complete(msg_dir_t dir) {
92  switch (ctx.msg.reg) {
93  case WRITE_ID:
94  // Get and save a new given ID
95  id_update(ctx.msg.data[0]);
96  break;
97  case GET_ID:
98  case GET_MODULE_TYPE:
99  case GET_STATUS:
100  case GET_FIRM_REVISION:
101  // ERROR
102  ctx.status.rx_error = TRUE;
103  break;
104  default:
105  if (dir == RX)
106  ctx.rx_cb(dir, &ctx.msg);
107  else
108  ctx.rxgc_cb(dir, &ctx.msg);
109  break;
110  }
111 }
RX_CB rxgc_cb
Definition: context.h:39
unsigned char id
Definition: context.h:42
msg_t msg
Definition: context.h:47
RX_CB rx_cb
Definition: context.h:38
unsigned char size
Definition: poppyNetwork.h:40
unsigned char type
Definition: context.h:43
unsigned char data[512]
Definition: poppyNetwork.h:41
TX_CB tx_cb
Definition: context.h:37
status_t status
Definition: context.h:46
DATA_CB data_cb
Definition: context.h:36
unsigned char reg
Definition: poppyNetwork.h:39
msg_dir_t
Message direction enum.
Definition: poppyNetwork.h:22