summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAditya Naik2020-06-24 13:17:15 -0400
committerAditya Naik2020-06-24 13:17:15 -0400
commit78a70b96dbf37cd340e1f5213befbad3bdd60a44 (patch)
treebee5c535bb0335b2f554f33c452a53e2aab6e5ef
parent0915882d1ee834d2cc7c8d4ca9bcdb7495dbe1a1 (diff)
Module DC handler
-rw-r--r--src/config.h4
-rw-r--r--src/master.c59
2 files changed, 50 insertions, 13 deletions
diff --git a/src/config.h b/src/config.h
index e4a6590..98fb8da 100644
--- a/src/config.h
+++ b/src/config.h
@@ -1,9 +1,9 @@
/* Enable debug mode */
-/* #define DEBUG_ENABLE 1 */
+#define DEBUG_ENABLE 1
/* Enable testing mode */
-/* #define TESTING_ENABLE 1 */
+#define TESTING_ENABLE 1
/* Buffer size for routing */
#define ROUTING_BUFSIZE 128
diff --git a/src/master.c b/src/master.c
index 28ff4bb..1322273 100644
--- a/src/master.c
+++ b/src/master.c
@@ -47,7 +47,7 @@ UART_HandleTypeDef huart1;
device_info_t *device_info[BUS_DEVICE_LIMIT] = {NULL};
subscription_info_t* subs_info[BUS_DEVICE_LIMIT];
uint32_t allocated[4]={0};
-uint8_t dev_sts[BUS_DEVICE_LIMIT] = {OFFLINE};
+state_t dev_sts[BUS_DEVICE_LIMIT] = {OFFLINE};
uint8_t data_idx;
uint8_t *data_rbuf[ROUTING_BUFSIZE]; /*< Buffer to store data to be routed */
@@ -61,7 +61,9 @@ uint32_t data_len_buf[ROUTING_BUFSIZE];
uint32_t data_routing_ptr = 0; /*< Pointer to tail of both data and data index buffers */
uint32_t cmd_routing_ptr = 0; /*< Pointer to tail of cmd and cmd index buffers */
-
+
+uint8_t device_fail_counter[BUS_DEVICE_LIMIT] = {0};
+
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
@@ -104,6 +106,7 @@ int main(void)
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
MX_USART1_UART_Init();
+ uint8_t debug_buf[128];
#endif
#ifdef TESTING_ENABLE
@@ -116,28 +119,62 @@ int main(void)
#endif /* MASTER */
#endif /* TESTING_ENABLE */
- uint8_t priority_counter = 0;
+ uint8_t priority_counter = 0, bus_fail_counter = 0, online_dev_count = 0;
/* Handshake */
+
while (1) {
if (priority_counter == 0) {
hs_status_t hs_status;
for (int dev_idx = 0; dev_idx < BUS_DEVICE_LIMIT-1; dev_idx++) {
+ if (device_fail_counter[dev_idx] > 20) {
+#ifdef TESTING_ENABLE
+ sprintf((char*)debug_buf, "Device at address %x set offline\r\n",
+ GET_ADDR_FROM_IDX(dev_idx));
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ memset(debug_buf, 0, 128);
+#endif
+ dev_sts[dev_idx] = OFFLINE;
+ device_fail_counter[dev_idx] = 0;
+ online_dev_count -= 1;
+ }
if (todo_hs_or_not_todo_hs(GET_ADDR_FROM_IDX(dev_idx))) {
hs_status = handshake(GET_ADDR_FROM_IDX(dev_idx));
- dev_sts[dev_idx] = get_state_from_hs_status(GET_ADDR_FROM_IDX(dev_idx), hs_status);
+ dev_sts[dev_idx] = get_state_from_hs_status(GET_ADDR_FROM_IDX(dev_idx),
+ hs_status);
+ if (dev_sts[dev_idx] == REGISTERED)
+ online_dev_count += 1;
}
}
}
- /* else if (priority_counter == 5 && routing_ptr > 0) { */
- /* routing(); */
- /* } */
else {
- for (int device_idx = 0; device_idx < BUS_DEVICE_LIMIT-1; device_idx++) {
- if (dev_sts[device_idx] == REGISTERED) {
- device_dataflow(GET_ADDR_FROM_IDX(device_idx), SLAVE_TX, 0);
+ dataflow_status_t DF_status;
+ for (int dev_idx = 0; dev_idx < BUS_DEVICE_LIMIT-1; dev_idx++) {
+ if (dev_sts[dev_idx] == REGISTERED) {
+ DF_status = device_dataflow(GET_ADDR_FROM_IDX(dev_idx), SLAVE_TX, 0);
+ if (DF_status != DF_SUCCESS) {
+ device_fail_counter[dev_idx] += 1;
+ if (device_fail_counter[dev_idx] == 1) {
+ bus_fail_counter += 1;
+ }
+ }
+ else {
+ if (device_fail_counter[dev_idx] > 0) {
+ device_fail_counter[dev_idx] = 0;
+ bus_fail_counter -= 1;
+ }
+ }
+
}
}
+ if (bus_fail_counter >= online_dev_count) {
+ /**
+ * Do something about bus failures:
+ * - send 9 CLK pulses to the slaves
+ * - hard reset the bus by resetting all modules
+ */
+ /* continue; */
+ }
routing();
cmd_routing();
}
@@ -688,7 +725,7 @@ uint8_t get_CTS(uint8_t i2c_addr)
uint8_t CTS_buf[2], AF_error_counter = 0;
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
- debug_buf[20] = {0};
+ uint8_t debug_buf[20] = {0};
#endif
uint8_t status = 1;