diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.h | 22 | ||||
| -rw-r--r-- | src/data.pb.h | 154 | ||||
| -rw-r--r-- | src/dataflow.h | 47 | ||||
| -rw-r--r-- | src/devices.h | 43 | ||||
| -rw-r--r-- | src/handshake.pb.h | 133 | ||||
| -rw-r--r-- | src/main.h | 73 | ||||
| -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 |
10 files changed, 102 insertions, 546 deletions
diff --git a/src/config.h b/src/config.h deleted file mode 100644 index e4a6590..0000000 --- a/src/config.h +++ /dev/null @@ -1,22 +0,0 @@ - -/* Enable debug mode */ -/* #define DEBUG_ENABLE 1 */ - -/* Enable testing mode */ -/* #define TESTING_ENABLE 1 */ - -/* Buffer size for routing */ -#define ROUTING_BUFSIZE 128 - -/** - * \def MASTER_I2C_BUS_INTERVAL - * \brief General interval value used by master in milliseconds - * - * Master uses this value to introduce delays on the I2C bus in order to reduce I2C timeout values. - * The optimum interval is usually dependent on I2C bus configuration on the hardware. Testing and debug UART - * operations will also affect this value. - * If unforeseen I2C timeout issues are encountered on master side, this value should be increased to suit the - * hardware. - * -*/ -#define MASTER_I2C_BUS_INTERVAL 5 diff --git a/src/data.pb.h b/src/data.pb.h deleted file mode 100644 index ce1395d..0000000 --- a/src/data.pb.h +++ /dev/null @@ -1,154 +0,0 @@ -/* Automatically generated nanopb header */ -/* Generated by nanopb-0.4.2-dev */ - -#ifndef PB_PROTO_DATA_PB_H_INCLUDED -#define PB_PROTO_DATA_PB_H_INCLUDED -#include <pb.h> - -#if PB_PROTO_HEADER_VERSION != 40 -#error Regenerate this file with the current version of nanopb generator. -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* Struct definitions */ -typedef struct _s2m_data { - pb_callback_t datapoints; -} s2m_data; - -typedef struct __datapoint { - uint32_t entity_id; - double data; - bool has_channel_id; - uint32_t channel_id; - bool has_unit_id; - uint32_t unit_id; - bool has_timestamp; - int32_t timestamp; -} _datapoint; - -typedef struct _command { - uint32_t source_module_id; - uint32_t dest_module_id; - pb_callback_t cmd_bytes; - pb_callback_t cmd_str; -} command; - -typedef struct _m2s_CTS { - uint32_t timeout; -} m2s_CTS; - -typedef struct _m2s_SOR { - uint32_t SOR_code; - bool has_rx_length; - uint32_t rx_length; -} m2s_SOR; - -typedef struct _s2m_DOC { - uint32_t DOC_code; - uint32_t tx_length; -} s2m_DOC; - - -/* Initializer values for message structs */ -#define m2s_SOR_init_default {1u, false, 0} -#define s2m_DOC_init_default {1u, 0u} -#define m2s_CTS_init_default {0} -#define command_init_default {0, 0, {{NULL}, NULL}, {{NULL}, NULL}} -#define _datapoint_init_default {0u, 0, false, 1u, false, 1u, false, 1} -#define s2m_data_init_default {{{NULL}, NULL}} -#define m2s_SOR_init_zero {0, false, 0} -#define s2m_DOC_init_zero {0, 0} -#define m2s_CTS_init_zero {0} -#define command_init_zero {0, 0, {{NULL}, NULL}, {{NULL}, NULL}} -#define _datapoint_init_zero {0, 0, false, 0, false, 0, false, 0} -#define s2m_data_init_zero {{{NULL}, NULL}} - -/* Field tags (for use in manual encoding/decoding) */ -#define s2m_data_datapoints_tag 1 -#define _datapoint_entity_id_tag 1 -#define _datapoint_data_tag 2 -#define _datapoint_channel_id_tag 3 -#define _datapoint_unit_id_tag 4 -#define _datapoint_timestamp_tag 5 -#define command_source_module_id_tag 1 -#define command_dest_module_id_tag 2 -#define command_cmd_bytes_tag 3 -#define command_cmd_str_tag 4 -#define m2s_CTS_timeout_tag 1 -#define m2s_SOR_SOR_code_tag 1 -#define m2s_SOR_rx_length_tag 2 -#define s2m_DOC_DOC_code_tag 1 -#define s2m_DOC_tx_length_tag 2 - -/* Struct field encoding specification for nanopb */ -#define m2s_SOR_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, SOR_code, 1) \ -X(a, STATIC, OPTIONAL, UINT32, rx_length, 2) -#define m2s_SOR_CALLBACK NULL -#define m2s_SOR_DEFAULT (const pb_byte_t*)"\x08\x01\x00" - -#define s2m_DOC_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, DOC_code, 1) \ -X(a, STATIC, REQUIRED, UINT32, tx_length, 2) -#define s2m_DOC_CALLBACK NULL -#define s2m_DOC_DEFAULT (const pb_byte_t*)"\x08\x01\x10\x00\x00" - -#define m2s_CTS_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, timeout, 1) -#define m2s_CTS_CALLBACK NULL -#define m2s_CTS_DEFAULT NULL - -#define command_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, source_module_id, 1) \ -X(a, STATIC, REQUIRED, UINT32, dest_module_id, 2) \ -X(a, CALLBACK, OPTIONAL, BYTES, cmd_bytes, 3) \ -X(a, CALLBACK, OPTIONAL, STRING, cmd_str, 4) -#define command_CALLBACK pb_default_field_callback -#define command_DEFAULT NULL - -#define _datapoint_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, entity_id, 1) \ -X(a, STATIC, REQUIRED, DOUBLE, data, 2) \ -X(a, STATIC, OPTIONAL, UINT32, channel_id, 3) \ -X(a, STATIC, OPTIONAL, UINT32, unit_id, 4) \ -X(a, STATIC, OPTIONAL, INT32, timestamp, 5) -#define _datapoint_CALLBACK NULL -#define _datapoint_DEFAULT (const pb_byte_t*)"\x08\x00\x11\x00\x00\x00\x00\x00\x00\x00\x00\x18\x01\x20\x01\x28\x01\x00" - -#define s2m_data_FIELDLIST(X, a) \ -X(a, CALLBACK, REPEATED, MESSAGE, datapoints, 1) -#define s2m_data_CALLBACK pb_default_field_callback -#define s2m_data_DEFAULT NULL -#define s2m_data_datapoints_MSGTYPE _datapoint - -extern const pb_msgdesc_t m2s_SOR_msg; -extern const pb_msgdesc_t s2m_DOC_msg; -extern const pb_msgdesc_t m2s_CTS_msg; -extern const pb_msgdesc_t command_msg; -extern const pb_msgdesc_t _datapoint_msg; -extern const pb_msgdesc_t s2m_data_msg; - -/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ -#define m2s_SOR_fields &m2s_SOR_msg -#define s2m_DOC_fields &s2m_DOC_msg -#define m2s_CTS_fields &m2s_CTS_msg -#define command_fields &command_msg -#define _datapoint_fields &_datapoint_msg -#define s2m_data_fields &s2m_data_msg - -/* Maximum encoded size of messages (where known) */ -#define m2s_SOR_size 12 -#define s2m_DOC_size 12 -#define m2s_CTS_size 6 -/* command_size depends on runtime parameters */ -#define _datapoint_size 38 -/* s2m_data_size depends on runtime parameters */ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif diff --git a/src/dataflow.h b/src/dataflow.h deleted file mode 100644 index 2aed40a..0000000 --- a/src/dataflow.h +++ /dev/null @@ -1,47 +0,0 @@ - -/* - * - * Dataflow Status Enumeration - * - * This typedef governs the dataflow state machine. Enums should be self explanatory - * - * TODO Handle other DF pathways for slave RX datapoint and slave RX command - * - * |------------+-----+----------------------------------------+---------------------+-------------------------| - * | Status | Int | Status Invariant | Action | Next Status | - * |------------+-----+----------------------------------------+---------------------+-------------------------| - * | DF_IDLE | 0 | Ready to send m2s_SOR | Send m2s_SOR | DF_RX_DOC or ??? | - * | DF_RX_DOC | 1 | Sent m2s_SOR; ready to receive s2m_DOC | Receive s2m_DOC | DF_CTS | - * | DF_CTS | 2 | Received m2s_SOR; ready to send DF_CTS | Send m2s_CTS | DF_RX_DATA or DF_RX_CMD | - * | DF_RX_DATA | 3 | Sent m2s_CTS; receive s2m_data | Receive s2m_data | DF_SUCCESS | - * | DF_RX_CMD | 4 | sent m2s_CTS receive s2m_command | Receive s2m_command | DF_SUCCESS | - * |------------+-----+----------------------------------------+---------------------+-------------------------| - * - * -*/ - -typedef enum dataflow_status { - DF_IDLE = 0, - DF_RX_DOC = 1, - DF_CTS = 2, - DF_RX_DATA = 3, - DF_RX_CMD = 4, - DF_SUCCESS = 5, - DF_FAIL = 6, - DF_TX_DATA = 7, - DF_TX_CMD = 8 -} dataflow_status_t; - -typedef enum SOR_codes { - SLAVE_TX = 1, - SLAVE_RX_DATAPOINT = 2, - SLAVE_RX_COMMAND = 3 -} SOR_codes_t; - -typedef enum DOC_codes { - CMD_UNICAST = 0x1, - CMD_MULTICAST = 0x2, - CMD_BROADCAST = 0x3, - RESERVED = 0x4, - DATA = 0x5 -} DOC_codes_t; diff --git a/src/devices.h b/src/devices.h deleted file mode 100644 index b2cb825..0000000 --- a/src/devices.h +++ /dev/null @@ -1,43 +0,0 @@ - -#include "handshake.pb.h" - -#define _MDR s2m_MDR_response - -typedef struct _device_info { - uint8_t device_id; - uint8_t i2c_addr; - _MDR MDR; - - uint32_t subscription_requests[4]; /* Subscriptions to this device */ - uint32_t subscriptions[4]; /* Subscriptions by this device */ -} device_info_t; - -/* TODO These IDs could be represented as a single 8-bit integer */ -typedef struct _subscription_info { - uint8_t module_ids[128]; - uint8_t entity_ids[128]; - uint8_t module_class[3]; - uint8_t i2c_address[128]; - uint8_t mod_idx, entity_idx, class_idx, i2c_idx; -} subscription_info_t; - -typedef enum hs_status { - IDLE = 0, // send mdr request. success: mdr_ack. fail: hs_fail - HS_MDR_ACK = 1, // get mdr ack. success: mdr_cts. fail: hs_fail - HS_MDR_CTS = 2, // send mdr cts. success: mdr_mdr. fail: hs_fail - HS_MDR_MDR = 3, // get mdr. success: registered. fail: hs_fail - HS_REGISTERED = 4, // send registered. success: exit. fail: hs_fail - HS_FAILED = 5 -} hs_status_t; - -typedef enum state { - NO_HS = 0, /* Initial state: handshake not attempted yet */ - OFFLINE = 1, /* Device not found on bus */ - CONNECTED = 2, /* Device found on bus but handshake not completed */ - REGISTERED = 3, /* Completed handshake; ready for action */ - FAILED = 4, /* Handshake failed */ - NO_DATA = 5 /* ??? */ -} state_t; - -/* Handshake message size definitions */ -#define MDR_req_buf_len 2 diff --git a/src/handshake.pb.h b/src/handshake.pb.h deleted file mode 100644 index 67f10e6..0000000 --- a/src/handshake.pb.h +++ /dev/null @@ -1,133 +0,0 @@ -/* Automatically generated nanopb header */ -/* Generated by nanopb-0.4.2-dev */ - -#ifndef PB_HANDSHAKE_PB_H_INCLUDED -#define PB_HANDSHAKE_PB_H_INCLUDED -#include <pb.h> - -#if PB_PROTO_HEADER_VERSION != 40 -#error Regenerate this file with the current version of nanopb generator. -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* Struct definitions */ -typedef struct __subscriptions { - bool has_module_id; - uint32_t module_id; - bool has_entity_id; - uint32_t entity_id; - bool has_module_class; - uint32_t module_class; - bool has_i2c_address; - uint32_t i2c_address; -} _subscriptions; - -typedef struct _m2s_MDR_request { - uint32_t record_type; -} m2s_MDR_request; - -typedef struct _m2s_MDR_res_CTS { - uint32_t timeout; -} m2s_MDR_res_CTS; - -typedef struct _s2m_MDR_req_ACK { - uint32_t MDR_res_length; -} s2m_MDR_req_ACK; - -typedef struct _s2m_MDR_response { - float MDR_version; - uint32_t module_id; - uint32_t module_class; - uint32_t entity_id; - pb_callback_t subscriptions; -} s2m_MDR_response; - - -/* Initializer values for message structs */ -#define m2s_MDR_request_init_default {0} -#define s2m_MDR_req_ACK_init_default {0} -#define m2s_MDR_res_CTS_init_default {0} -#define _subscriptions_init_default {false, 0u, false, 0u, false, 0u, false, 0u} -#define s2m_MDR_response_init_default {0, 0, 0, 0, {{NULL}, NULL}} -#define m2s_MDR_request_init_zero {0} -#define s2m_MDR_req_ACK_init_zero {0} -#define m2s_MDR_res_CTS_init_zero {0} -#define _subscriptions_init_zero {false, 0, false, 0, false, 0, false, 0} -#define s2m_MDR_response_init_zero {0, 0, 0, 0, {{NULL}, NULL}} - -/* Field tags (for use in manual encoding/decoding) */ -#define _subscriptions_module_id_tag 1 -#define _subscriptions_entity_id_tag 2 -#define _subscriptions_module_class_tag 3 -#define _subscriptions_i2c_address_tag 4 -#define m2s_MDR_request_record_type_tag 1 -#define m2s_MDR_res_CTS_timeout_tag 1 -#define s2m_MDR_req_ACK_MDR_res_length_tag 1 -#define s2m_MDR_response_MDR_version_tag 1 -#define s2m_MDR_response_module_id_tag 2 -#define s2m_MDR_response_module_class_tag 3 -#define s2m_MDR_response_entity_id_tag 4 -#define s2m_MDR_response_subscriptions_tag 5 - -/* Struct field encoding specification for nanopb */ -#define m2s_MDR_request_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, record_type, 1) -#define m2s_MDR_request_CALLBACK NULL -#define m2s_MDR_request_DEFAULT NULL - -#define s2m_MDR_req_ACK_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, MDR_res_length, 1) -#define s2m_MDR_req_ACK_CALLBACK NULL -#define s2m_MDR_req_ACK_DEFAULT NULL - -#define m2s_MDR_res_CTS_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, UINT32, timeout, 1) -#define m2s_MDR_res_CTS_CALLBACK NULL -#define m2s_MDR_res_CTS_DEFAULT NULL - -#define _subscriptions_FIELDLIST(X, a) \ -X(a, STATIC, OPTIONAL, UINT32, module_id, 1) \ -X(a, STATIC, OPTIONAL, UINT32, entity_id, 2) \ -X(a, STATIC, OPTIONAL, UINT32, module_class, 3) \ -X(a, STATIC, OPTIONAL, UINT32, i2c_address, 4) -#define _subscriptions_CALLBACK NULL -#define _subscriptions_DEFAULT (const pb_byte_t*)"\x08\x00\x10\x00\x18\x00\x20\x00\x00" - -#define s2m_MDR_response_FIELDLIST(X, a) \ -X(a, STATIC, REQUIRED, FLOAT, MDR_version, 1) \ -X(a, STATIC, REQUIRED, UINT32, module_id, 2) \ -X(a, STATIC, REQUIRED, UINT32, module_class, 3) \ -X(a, STATIC, REQUIRED, UINT32, entity_id, 4) \ -X(a, CALLBACK, REPEATED, MESSAGE, subscriptions, 5) -#define s2m_MDR_response_CALLBACK pb_default_field_callback -#define s2m_MDR_response_DEFAULT NULL -#define s2m_MDR_response_subscriptions_MSGTYPE _subscriptions - -extern const pb_msgdesc_t m2s_MDR_request_msg; -extern const pb_msgdesc_t s2m_MDR_req_ACK_msg; -extern const pb_msgdesc_t m2s_MDR_res_CTS_msg; -extern const pb_msgdesc_t _subscriptions_msg; -extern const pb_msgdesc_t s2m_MDR_response_msg; - -/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ -#define m2s_MDR_request_fields &m2s_MDR_request_msg -#define s2m_MDR_req_ACK_fields &s2m_MDR_req_ACK_msg -#define m2s_MDR_res_CTS_fields &m2s_MDR_res_CTS_msg -#define _subscriptions_fields &_subscriptions_msg -#define s2m_MDR_response_fields &s2m_MDR_response_msg - -/* Maximum encoded size of messages (where known) */ -#define m2s_MDR_request_size 6 -#define s2m_MDR_req_ACK_size 6 -#define m2s_MDR_res_CTS_size 6 -#define _subscriptions_size 24 -/* s2m_MDR_response_size depends on runtime parameters */ - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 6579484..0000000 --- a/src/main.h +++ /dev/null @@ -1,73 +0,0 @@ -/* USER CODE BEGIN Header */
-/**
- ******************************************************************************
- * @file : main.h
- * @brief : Header for main.c file.
- * This file contains the common defines of the application.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2020 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
-/* USER CODE END Header */
-
-/* Define to prevent recursive inclusion -------------------------------------*/
-#ifndef __MAIN_H
-#define __MAIN_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Includes ------------------------------------------------------------------*/
-#include "port.h"
-
-/* Private includes ----------------------------------------------------------*/
-/* USER CODE BEGIN Includes */
-
-/* USER CODE END Includes */
-
-/* Exported types ------------------------------------------------------------*/
-/* USER CODE BEGIN ET */
-
-/* USER CODE END ET */
-
-/* Exported constants --------------------------------------------------------*/
-/* USER CODE BEGIN EC */
-
-/* USER CODE END EC */
-
-/* Exported macro ------------------------------------------------------------*/
-/* USER CODE BEGIN EM */
-
-/* USER CODE END EM */
-
-/* Exported functions prototypes ---------------------------------------------*/
-void Error_Handler(void);
-
-/* USER CODE BEGIN EFP */
-
-/* USER CODE END EFP */
-
-/* Private defines -----------------------------------------------------------*/
-#define led_Pin GPIO_PIN_13
-#define led_GPIO_Port GPIOC
-/* USER CODE BEGIN Private defines */
-
-/* USER CODE END Private defines */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __MAIN_H */
-
-/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
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; +} |
