From 1cb59504671100dd41d3072821e31b3da931f723 Mon Sep 17 00:00:00 2001 From: Aditya Naik Date: Thu, 4 Jun 2020 20:26:13 -0400 Subject: Testing micropython slave --- makefile | 2 +- src/main-master.c | 192 +++++++++++------------------------------- src/main-slave.c | 247 +++++++++++++++++++++--------------------------------- 3 files changed, 144 insertions(+), 297 deletions(-) diff --git a/makefile b/makefile index 91fca11..640d80d 100644 --- a/makefile +++ b/makefile @@ -36,7 +36,7 @@ BUILD_DIR = build ###################################### # C sources C_SOURCES = \ -src/main-slave.c \ +src/main-master.c \ src/stm32f4xx_it.c \ src/stm32f4xx_hal_msp.c \ src/system_stm32f4xx.c \ diff --git a/src/main-master.c b/src/main-master.c index a4a278e..8242539 100644 --- a/src/main-master.c +++ b/src/main-master.c @@ -33,7 +33,7 @@ #define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__))) #define I2C_ADDRESS 0x05 -#define BUS_DEVICE_LIMIT 128 +#define BUS_DEVICE_LIMIT 5 /* Macro to toggle between master and slave firmware */ #define MASTER @@ -105,7 +105,7 @@ int main(void) while (1) { if (priority_counter == 0) { hs_status_t hs_status; - for (int curr_addr=0; curr_addr < 5; curr_addr++) { + for (uint8_t curr_addr=1; curr_addr < BUS_DEVICE_LIMIT; curr_addr++) { if (todo_hs_or_not_todo_hs(curr_addr)) { hs_status = handshake(curr_addr); dev_sts[GET_IDX_FROM_ADDR(curr_addr)] = get_state_from_hs_status(curr_addr, hs_status); @@ -113,7 +113,7 @@ int main(void) } } - else if (priority_counter == 5) { + else if (priority_counter == 5 && routing_ptr > 0) { routing(); } else { @@ -137,14 +137,11 @@ hs_status_t handshake(uint32_t i2c_addr) /* Handshake variables */ uint8_t hs_sts = IDLE; - uint8_t *MDR_req_buf, *MDR_ACK_buf, *MDR_CTS_buf, *MDR_buf; + uint8_t *MDR_buf; uint32_t AF_error_counter = 0; uint32_t dev_idx = GET_IDX_FROM_ADDR(i2c_addr); - uint32_t MDR_len = 0; + uint16_t MDR_len = 0; - m2s_MDR_request MDR_req_message = m2s_MDR_request_init_default; - s2m_MDR_req_ACK MDR_ACK = s2m_MDR_req_ACK_init_default; - m2s_MDR_res_CTS MDR_CTS = m2s_MDR_res_CTS_init_default; s2m_MDR_response MDR_res_message = s2m_MDR_response_init_default; #if defined(TESTING_ENABLE) || defined(DEBUG_ENABLE) @@ -152,54 +149,32 @@ hs_status_t handshake(uint32_t i2c_addr) #endif #ifdef TESTING_ENABLE uint8_t term[] = "\r\n"; - size_t MDR_req_size, MDR_CTS_size; #endif while (hs_sts != HS_FAILED && hs_sts != HS_REGISTERED) { switch (hs_sts) { case (IDLE): { - MDR_req_buf = malloc(8); - pb_ostream_t MDR_req_stream = pb_ostream_from_buffer(MDR_req_buf, 2); - MDR_req_message.record_type = 7; /* Placeholder for default record type */ - if(!pb_encode(&MDR_req_stream, m2s_MDR_request_fields, &MDR_req_message)) { + 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) { hs_sts = HS_FAILED; #ifdef DEBUG_ENABLE - goto __MDR_REQ_ENC_FAIL; - __MDR_REQ_ENC_FAIL_END: + goto __HS_MDR_REQ_I2C_ERROR; + __HS_MDR_REQ_I2C_ERROR_END: __asm__("nop"); #endif } else { -#ifdef TESTING_ENABLE - MDR_req_size = MDR_req_stream.bytes_written; - goto __HS_IDLE_TESTING; - __HS_IDLE_TESTING_END: - __asm__("nop"); -#endif - if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)i2c_addr, (uint8_t*)MDR_req_buf, - MDR_req_buf_len, 10000) != HAL_OK) { - 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; - } - free(MDR_req_buf); - break; + hs_sts = HS_MDR_ACK; } + break; } case (HS_MDR_ACK): { HAL_Delay(MASTER_I2C_BUS_INTERVAL); - MDR_ACK_buf = malloc(8); + uint8_t MDR_ACK_buf[2] = {0x0, 0x0}; AF_error_counter = 0; - while (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)i2c_addr, (uint8_t*)MDR_ACK_buf, - s2m_MDR_req_ACK_size, 100) != HAL_OK) { + 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; } @@ -216,62 +191,32 @@ hs_status_t handshake(uint32_t i2c_addr) } } if (hs_sts != HS_FAILED) { - pb_istream_t MDR_ACK_istream = pb_istream_from_buffer(MDR_ACK_buf, 2); - if (!pb_decode(&MDR_ACK_istream, s2m_MDR_req_ACK_fields, &MDR_ACK)) { - hs_sts = HS_FAILED; -#ifdef DEBUG_ENABLE - goto __MDR_ACK_DEC_ERROR; - __MDR_ACK_DEC_ERROR_END: - __asm__("nop"); -#endif + uint8_t ACK_flag = MDR_ACK_buf[1]; + if (ACK_flag == 0xFF) { + MDR_len = MDR_ACK_buf[0]; + hs_sts = HS_MDR_CTS; } else { - MDR_len = MDR_ACK.MDR_res_length; - hs_sts = HS_MDR_CTS; -#ifdef TESTING_ENABLE - goto __HS_MDR_ACK_TESTING; - __HS_MDR_ACK_TESTING_END: - __asm__("nop"); -#endif + hs_sts = HS_FAILED; } - free(MDR_ACK_buf); } break; } case (HS_MDR_CTS): { - MDR_CTS_buf = (uint8_t*)malloc(8); - pb_ostream_t MDR_CTS_ostream = pb_ostream_from_buffer(MDR_CTS_buf, sizeof(MDR_CTS_buf)); - MDR_CTS.timeout = 100; - if (!pb_encode(&MDR_CTS_ostream, m2s_MDR_res_CTS_fields, &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) { hs_sts = HS_FAILED; #ifdef DEBUG_ENABLE - goto __MDR_CTS_ENC_ERROR; - __MDR_CTS_ENC_ERROR_END: + goto __HS_CTS_I2C_ERROR; + __HS_CTS_I2C_ERROR_END: __asm__("nop"); #endif } else { -#ifdef TESTING_ENABLE - MDR_CTS_size = MDR_CTS_ostream.bytes_written; - goto __HS_MDR_CTS_TESTING; - __HS_MDR_CTS_TESTING_END: - __asm__("nop"); -#endif - if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)i2c_addr, - (uint8_t*)MDR_CTS_buf, 2, 10000) != HAL_OK) { - 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; - free(MDR_CTS_buf); - } + hs_sts = HS_MDR_MDR; } + break; } case (HS_MDR_MDR): @@ -304,6 +249,7 @@ hs_status_t handshake(uint32_t i2c_addr) MDR_res_message.subscriptions.arg = (void*)dev_idx; pb_istream_t MDR_res_stream = pb_istream_from_buffer(MDR_buf, MDR_len); if (!pb_decode(&MDR_res_stream, s2m_MDR_response_fields, &MDR_res_message)) { + hs_sts = HS_FAILED; #ifdef DEBUG_ENABLE goto __HS_MDR_DEC_ERROR; __HS_MDR_DEC_ERROR_END: @@ -327,43 +273,11 @@ hs_status_t handshake(uint32_t i2c_addr) break; } } - } #ifdef TESTING_ENABLE { goto __TESTING_BLOCK_END; - __HS_IDLE_TESTING: - sprintf((char*)debug_buf, "MDR req length: %d\r\n", MDR_req_size); - HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100); - memset(debug_buf, 0, 128); - uint8_t bufbuf[] = "MDR req buffer: "; - HAL_UART_Transmit(&huart1, bufbuf, sizeof(bufbuf), 100); - for(int x=0; xmod_idx && !alloc; dev_sub_idx++) { @@ -845,7 +747,7 @@ bool decode_subscriptions_callback(pb_istream_t *istream, const pb_field_t *fiel } if(!pb_decode(istream, _subscriptions_fields, &subs)) - return false; + return false; /* Parse all fields if they're included */ if (subs.has_module_id) @@ -970,16 +872,16 @@ bool decode_data_callback(pb_istream_t *istream, const pb_field_t *field, void * datapoint[data_idx].data = loc_datapoint.data; if (loc_datapoint.has_channel_id) { - datapoint[data_idx].has_channel_id = true; - datapoint[data_idx].channel_id = loc_datapoint.channel_id; + datapoint[data_idx].has_channel_id = true; + datapoint[data_idx].channel_id = loc_datapoint.channel_id; } if (loc_datapoint.has_unit_id) { - datapoint[data_idx].has_unit_id = true; - datapoint[data_idx].unit_id = loc_datapoint.unit_id; + datapoint[data_idx].has_unit_id = true; + datapoint[data_idx].unit_id = loc_datapoint.unit_id; } if (loc_datapoint.has_timestamp) { - datapoint[data_idx].has_timestamp = true; - datapoint[data_idx].timestamp = loc_datapoint.timestamp; + datapoint[data_idx].has_timestamp = true; + datapoint[data_idx].timestamp = loc_datapoint.timestamp; } data_idx++; diff --git a/src/main-slave.c b/src/main-slave.c index f5979db..8218290 100644 --- a/src/main-slave.c +++ b/src/main-slave.c @@ -51,7 +51,7 @@ bool decode_subscriptions_callback(pb_istream_t *istream, const pb_field_t *fiel bool encode_subscription_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg); bool encode_datapoint_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg); bool decode_data_callback(pb_istream_t *istream, const pb_field_t *field, void **args); - +bool handshake(void); /** * @brief The application entry point. * @retval int @@ -74,157 +74,8 @@ int main(void) uint8_t reset_string[] = "\r\n\n==========SLAVE RESET=========\r\n\n"; HAL_UART_Transmit(&huart1, reset_string, sizeof(reset_string), 100); - { - uint8_t MDR_buf[128], debug_buf[128], term[]="\r\n"; - size_t MDR_enc_size; - s2m_MDR_response res; - res.MDR_version = 0.1; - res.module_id = 1; - res.module_class = 2; - res.entity_id=1; - - res.subscriptions.funcs.encode=encode_subscription_callback; - pb_ostream_t ostream = pb_ostream_from_buffer(MDR_buf, sizeof(MDR_buf)); - if(!pb_encode(&ostream, s2m_MDR_response_fields, &res)) { -#ifdef DEBUG_ENABLE - uint8_t err_buf[] = "MDR encoding error\r\n"; - HAL_UART_Transmit(&huart1, err_buf, sizeof(err_buf), 100); -#endif - while(1) { - HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin); - HAL_Delay(500); - } - } - MDR_enc_size = ostream.bytes_written; -#ifdef TESTING_ENABLE - sprintf((char*)debug_buf, "MDR Encoded size: %d\r\n", MDR_enc_size); - HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100); - memset(debug_buf, 0, 128); - - uint8_t bufbuf[] = "MDR Buffer: "; - HAL_UART_Transmit(&huart1, bufbuf, sizeof(bufbuf), 100); - for(int x=0; xtag == s2m_MDR_response_subscriptions_tag) { -- cgit v1.2.3