summaryrefslogtreecommitdiff
path: root/src/master.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/master.c')
-rw-r--r--src/master.c110
1 files changed, 36 insertions, 74 deletions
diff --git a/src/master.c b/src/master.c
index 28ff4bb..6637ca5 100644
--- a/src/master.c
+++ b/src/master.c
@@ -23,7 +23,9 @@
#include "dataflow.h"
#include "handshake.pb.h"
#include "data.pb.h"
-
+#include "stream.h"
+#include "stream_i2c.h"
+#include "stream_stdio.h"
/* Private Macros */
#define device_MDR s2m_MDR_response
#define GET_IDX_FROM_ADDR(i2c_addr) (i2c_addr>>1)-1
@@ -37,7 +39,6 @@
/* Macro to toggle between master and slave firmware */
#define MASTER
-
/* Private globals */
I2C_HandleTypeDef hi2c1;
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
@@ -61,7 +62,10 @@ 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 */
-
+
+p_stream_t stream = {&i2c_read, &i2c_write, NULL};
+/* p_stream_t stream = {&stdio_read, &stdio_write, NULL}; */ // STDIO stream
+
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
@@ -152,10 +156,13 @@ hs_status_t handshake(uint32_t i2c_addr)
uint8_t hs_sts = IDLE;
uint8_t *MDR_buf;
- uint32_t AF_error_counter = 0;
uint32_t dev_idx = GET_IDX_FROM_ADDR(i2c_addr);
uint16_t MDR_len = 0;
+ void **vptr = malloc(sizeof(uint8_t*));
+ vptr[0] = malloc(sizeof(uint8_t*));
+ vptr[0] = &i2c_addr;
+
s2m_MDR_response MDR_res_message = s2m_MDR_response_init_default;
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
@@ -170,41 +177,22 @@ hs_status_t handshake(uint32_t i2c_addr)
case (IDLE):
{
uint8_t MDR_req_buf[2] = {0x0, 0x1};
- if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)i2c_addr, MDR_req_buf, 2, 10000) != HAL_OK) {
+ if (stream.write(MDR_req_buf, 2, vptr, &stream) != 0) {
hs_sts = HS_FAILED;
-#ifdef DEBUG_ENABLE
- goto __HS_MDR_REQ_I2C_ERROR;
- __HS_MDR_REQ_I2C_ERROR_END:
- __asm__("nop");
-#endif
}
else {
hs_sts = HS_MDR_ACK;
- }
+ }
break;
}
case (HS_MDR_ACK):
{
HAL_Delay(MASTER_I2C_BUS_INTERVAL);
uint8_t MDR_ACK_buf[2] = {0x0, 0x0};
- AF_error_counter = 0;
- while (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)i2c_addr, MDR_ACK_buf, 2, 100) != HAL_OK) {
- if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF) {
- hs_sts = HS_FAILED;
- }
- if (++AF_error_counter > 1500) {
- hs_sts = HS_FAILED;
- }
- if (hs_sts == HS_FAILED) {
-#ifdef DEBUG_ENABLE
- goto __HS_MDR_ACK_I2C_ERROR;
- __HS_MDR_ACK_I2C_ERROR_END:
- __asm__("nop");
-#endif
- break;
- }
+ if (stream.read(MDR_ACK_buf, 2, vptr, &stream) != 0) {
+ hs_sts = HS_FAILED;
}
- if (hs_sts != HS_FAILED) {
+ else {
uint8_t ACK_flag = MDR_ACK_buf[1];
if (ACK_flag == 0xFF) {
MDR_len = MDR_ACK_buf[0];
@@ -219,46 +207,21 @@ hs_status_t handshake(uint32_t i2c_addr)
case (HS_MDR_CTS):
{
uint8_t MDR_CTS_buf[2] = {0x0, 0x02};
- if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)i2c_addr, MDR_CTS_buf, 2, 10000) != HAL_OK) {
+ if (stream.write(MDR_CTS_buf, 2, vptr, &stream) != 0) {
hs_sts = HS_FAILED;
-#ifdef DEBUG_ENABLE
- goto __HS_CTS_I2C_ERROR;
- __HS_CTS_I2C_ERROR_END:
- __asm__("nop");
-#endif
}
else {
hs_sts = HS_MDR_MDR;
- }
-
+ }
break;
}
case (HS_MDR_MDR):
{
MDR_buf = (uint8_t*)malloc(MDR_len);
- AF_error_counter = 0;
- while (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)i2c_addr,
- (uint8_t*)MDR_buf, MDR_len, 1000) != HAL_OK) {
- if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF) {
- hs_sts = HS_FAILED;
-#ifdef DEBUG_ENABLE
- goto __HS_MDR_I2C_ERROR;
- __HS_MDR_I2C_ERROR_END:
- __asm__("nop");
-#endif
- break;
- }
- else if (++AF_error_counter > 1500) {
- hs_sts = HS_FAILED;
- break;
- }
+ if (stream.read(MDR_buf, MDR_len, vptr, &stream) != 0) {
+ hs_sts = HS_FAILED;
}
- if (hs_sts != HS_FAILED) {
-#ifdef TESTING_ENABLE
- goto __HS_MDR_MDR_TESTING;
- __HS_MDR_MDR_TESTING_END:
- __asm__("nop");
-#endif
+ else {
MDR_res_message.subscriptions.funcs.decode = decode_subscriptions_callback;
MDR_res_message.subscriptions.arg = (void*)dev_idx;
pb_istream_t MDR_res_stream = pb_istream_from_buffer(MDR_buf, MDR_len);
@@ -360,6 +323,10 @@ dataflow_status_t device_dataflow(uint8_t i2c_addr, uint32_t SOR_code, volatile
uint32_t AF_error_counter = 0;
uint32_t data_len = 0;
+
+ void **vptr = malloc(sizeof(uint8_t*));
+ vptr[0] = malloc(sizeof(uint8_t*));
+ vptr[0] = &i2c_addr;
/* TODO Add default values to the CTS message in proto */
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
@@ -685,27 +652,21 @@ dataflow_status_t device_dataflow(uint8_t i2c_addr, uint32_t SOR_code, volatile
uint8_t get_CTS(uint8_t i2c_addr)
{
- uint8_t CTS_buf[2], AF_error_counter = 0;
-
+ uint8_t CTS_buf[2];
+ void **vptr = malloc(sizeof(uint8_t*));
+ vptr[0] = malloc(sizeof(uint8_t*));
+ vptr[0] = &i2c_addr;
+
#if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE)
debug_buf[20] = {0};
#endif
-
- uint8_t status = 1;
- while (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)i2c_addr, CTS_buf, 2, 10000) != HAL_OK) {
- if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF) {
- status = 0;
- }
- if (++AF_error_counter > 1000) {
- status = 0;
- }
- if (status == 0) {
+
+ uint8_t status = stream.read(CTS_buf, 2, vptr, &stream);
+ if (status != 0) {
#ifdef DEBUG_ENABLE
- sprintf((char*)debug_buf, "Failed to get CTS\r\n");
- HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ sprintf((char*)debug_buf, "Failed to get CTS\r\n");
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
#endif
- break;
- }
}
return status;
}
@@ -990,6 +951,7 @@ static void MX_I2C1_Init(void)
Error_Handler();
}
+ stream.props[DEVICE] = &hi2c1;
}
/**