diff options
| author | Aditya Naik | 2020-07-30 14:16:20 -0400 |
|---|---|---|
| committer | Aditya Naik | 2020-07-30 14:16:20 -0400 |
| commit | c8c06aea5199e555235f71acc8547ed2ef0706cf (patch) | |
| tree | e1d72f836873c9fc99dd200dd8829c6f0046b96f | |
| parent | 70f623d66439a826927b7d2fc9d64ae0cb631b92 (diff) | |
Stream abstraction
| -rw-r--r-- | include/config.h (renamed from src/config.h) | 0 | ||||
| -rw-r--r-- | include/data.pb.h (renamed from src/data.pb.h) | 0 | ||||
| -rw-r--r-- | include/dataflow.h (renamed from src/dataflow.h) | 0 | ||||
| -rw-r--r-- | include/devices.h (renamed from src/devices.h) | 0 | ||||
| -rw-r--r-- | include/handshake.pb.h (renamed from src/handshake.pb.h) | 0 | ||||
| -rw-r--r-- | include/main.h (renamed from src/main.h) | 0 | ||||
| -rw-r--r-- | include/stream.h | 10 | ||||
| -rw-r--r-- | include/stream_i2c.h | 5 | ||||
| -rw-r--r-- | include/stream_stdio.h | 5 | ||||
| -rw-r--r-- | makefile | 3 | ||||
| -rw-r--r-- | src/master.c | 110 | ||||
| -rw-r--r-- | src/stream_i2c.c | 37 | ||||
| -rw-r--r-- | src/stream_i2c.h | 11 | ||||
| -rw-r--r-- | src/stream_stdio.c | 18 |
14 files changed, 124 insertions, 75 deletions
diff --git a/src/config.h b/include/config.h index e4a6590..e4a6590 100644 --- a/src/config.h +++ b/include/config.h diff --git a/src/data.pb.h b/include/data.pb.h index ce1395d..ce1395d 100644 --- a/src/data.pb.h +++ b/include/data.pb.h diff --git a/src/dataflow.h b/include/dataflow.h index 2aed40a..2aed40a 100644 --- a/src/dataflow.h +++ b/include/dataflow.h diff --git a/src/devices.h b/include/devices.h index b2cb825..b2cb825 100644 --- a/src/devices.h +++ b/include/devices.h diff --git a/src/handshake.pb.h b/include/handshake.pb.h index 67f10e6..67f10e6 100644 --- a/src/handshake.pb.h +++ b/include/handshake.pb.h diff --git a/src/main.h b/include/main.h index 6579484..6579484 100644 --- a/src/main.h +++ b/include/main.h diff --git a/include/stream.h b/include/stream.h new file mode 100644 index 0000000..6bdc29e --- /dev/null +++ b/include/stream.h @@ -0,0 +1,10 @@ +#include <stdint.h> +#include <stddef.h> + +typedef struct p_stream_s p_stream_t; + +struct p_stream_s { + int (*read)(uint8_t *buf, size_t len, void **vptr, void *sptr); + int (*write)(uint8_t *buf, size_t len, void **vptr, void *sptr); + void **props; +}; diff --git a/include/stream_i2c.h b/include/stream_i2c.h new file mode 100644 index 0000000..ccbccde --- /dev/null +++ b/include/stream_i2c.h @@ -0,0 +1,5 @@ +#include <stdint.h> +#include <stddef.h> + +int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr); +int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr); diff --git a/include/stream_stdio.h b/include/stream_stdio.h new file mode 100644 index 0000000..c72183b --- /dev/null +++ b/include/stream_stdio.h @@ -0,0 +1,5 @@ +#include <stdint.h> +#include <stddef.h> + +int stdio_read(uint8_t* buf, size_t count, void **vptr, void *sptr); +int stdio_write(uint8_t* buf, size_t count, void **vptr, void *sptr); @@ -27,6 +27,7 @@ lib/nanopb/pb_encode.c \ lib/nanopb/pb_common.c \
src/handshake.pb.c \
src/data.pb.c \
+src/stream_i2c.c
# set the main C source based on whether we're compiling the master or slave
C_SOURCES+=$(addprefix src/, $(TARGET).c)
@@ -63,7 +64,7 @@ AS_INCLUDES = # General C includes for all ports. Since CMSIS is being included, that means this is restricted to ARM ports
C_INCLUDES = \
--Isrc \
+-Iinclude \
-Ilib/cmsis \
-Ilib/nanopb
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; } /** diff --git a/src/stream_i2c.c b/src/stream_i2c.c new file mode 100644 index 0000000..b149756 --- /dev/null +++ b/src/stream_i2c.c @@ -0,0 +1,37 @@ +#include "stream_i2c.h" +#include "port.h" +#include "stream.h" + +int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr) +{ + p_stream_t *stream = (p_stream_t*)sptr; + I2C_HandleTypeDef dev = *(I2C_HandleTypeDef*)stream->props[DEVICE]; + + uint16_t addr = *(uint16_t*)vptr[0]; + uint32_t timeout = *(uint32_t*)vptr[1]; + uint16_t AF_limit = *(uint32_t*)vptr[2]; + + int error, AF_counter = 0; + while (HAL_I2C_Master_Receive(&dev, addr, buf, count, timeout) != HAL_OK) { + if ((error = HAL_I2C_GetError(&dev)) != HAL_I2C_ERROR_AF) { + return error; + } + else if (++AF_counter > AF_limit) { + return HAL_I2C_ERROR_AF; + } + } + return 0; +} + +int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr) +{ + p_stream_t *stream = (p_stream_t*)sptr; + I2C_HandleTypeDef dev = *(I2C_HandleTypeDef*)stream->props[DEVICE]; + uint16_t addr = *(uint16_t*)vptr[1]; + uint32_t timeout = *(uint32_t*)vptr[2]; + + while (HAL_I2C_Master_Transmit(&dev, addr, buf, count, timeout) != HAL_OK) { + return HAL_I2C_GetError(&dev); + } + return 0; +} diff --git a/src/stream_i2c.h b/src/stream_i2c.h new file mode 100644 index 0000000..beba717 --- /dev/null +++ b/src/stream_i2c.h @@ -0,0 +1,11 @@ +#include <stdint.h> +#include <stddef.h> + +typedef enum { + DEVICE, + TIMEOUT, + AF_LIMIT +} I2C_PROPS; + +int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr); +int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr); diff --git a/src/stream_stdio.c b/src/stream_stdio.c new file mode 100644 index 0000000..efa4b13 --- /dev/null +++ b/src/stream_stdio.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include "stream_stdio.h" +#include "stream.h" + +int stdio_read(uint8_t* buf, size_t count, void **vptr, void *sptr) +{ + int x; + for (x = 0; x < count; x++) { + scanf("%c", &buf[x]); + } + return 0; +} + +int stdio_write(uint8_t* buf, size_t count, void **vptr, void *sptr) +{ + printf("%s\n", buf); + return 0; +} |
