diff options
Diffstat (limited to 'src/slave.c')
| -rw-r--r-- | src/slave.c | 81 |
1 files changed, 62 insertions, 19 deletions
diff --git a/src/slave.c b/src/slave.c index a0091e3..7bcceac 100644 --- a/src/slave.c +++ b/src/slave.c @@ -40,6 +40,9 @@ /* Private globals */ I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart1; +uint8_t cmd_str[128]; +uint8_t str_ptr = 0; +pb_byte_t str_buf[18]; /* Function prototypes */ void SystemClock_Config(void); @@ -51,6 +54,8 @@ 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 encode_cmd_string_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg); +bool decode_cmd_string_callback(pb_istream_t *istream, const pb_field_t *field, void **args); bool handshake(void); /** * @brief The application entry point. @@ -76,11 +81,10 @@ int main(void) while (handshake() == false); - uint8_t SOR_buf[2] = {0}, debug_buf[128]; + uint8_t SOR_buf[2] = {0}, debug_buf[128] = {0}; HAL_Delay(1000); while (1) { - - if (HAL_I2C_Slave_Receive(&hi2c1, (uint8_t*)SOR_buf, 2, 500) != HAL_OK) { + if (HAL_I2C_Slave_Receive(&hi2c1, (uint8_t*)SOR_buf, 2, 1000) != HAL_OK) { sprintf((char*)debug_buf, "Failed to get SOR\r\n"); HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100); memset(debug_buf, 0, 128); @@ -180,17 +184,39 @@ int main(void) HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100); memset(debug_buf, 0, 128); } + else if (SOR_buf[0] == 3) { + uint8_t CTS_buf[] = {0x0, 0x1}, len_buf[4], *cmd_buf; + + HAL_I2C_Slave_Transmit(&hi2c1, CTS_buf, 2, 10000); + HAL_I2C_Slave_Receive(&hi2c1, len_buf, 4, 10000); + uint8_t cmd_len = len_buf[1]; + cmd_buf = malloc(cmd_len*sizeof(uint8_t)); + HAL_I2C_Slave_Transmit(&hi2c1, CTS_buf, 2, 10000); + HAL_I2C_Slave_Receive(&hi2c1, cmd_buf, cmd_len, 10000); + + command cmd_decode=command_init_zero; + pb_istream_t cmd_istream = pb_istream_from_buffer(cmd_buf, cmd_len); + cmd_decode.cmd_str.funcs.decode=decode_cmd_string_callback; + if (pb_decode(&cmd_istream, command_fields, &cmd_decode)) + sprintf((char*)debug_buf, "Got cmd, decode successful\r\n"); + else + sprintf((char*)debug_buf, "Got cmd, decode unsuccessful\r\n"); + + HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100); + memset(debug_buf, 0, 128); + } + } } bool handshake(void) { - uint8_t MDR_buf[128], debug_buf[128], term[]="\r\n"; + uint8_t MDR_buf[128], debug_buf[128] = {0}, term[]="\r\n"; size_t MDR_enc_size; s2m_MDR_response res; res.MDR_version = 0.1; - res.module_id = 4; + res.module_id = 0x4; res.module_class = 2; res.entity_id=1; @@ -280,19 +306,17 @@ bool handshake(void) bool encode_subscription_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg) { if(ostream!=NULL && field->tag == s2m_MDR_response_subscriptions_tag) { - for (int x=0; x<5; x++) { - _subscriptions subs; - subs.module_id = x+1; - subs.has_module_id=true; - subs.has_entity_id=false; - subs.has_module_class=false; - subs.has_i2c_address=false; - if(!pb_encode_tag_for_field(ostream, field)) { - return false; - } - if(!pb_encode_submessage(ostream, _subscriptions_fields, &subs)) { - return false; - } + _subscriptions subs; + subs.module_id = 0xd; + subs.has_module_id=true; + subs.has_entity_id=false; + subs.has_module_class=false; + subs.has_i2c_address=false; + if(!pb_encode_tag_for_field(ostream, field)) { + return false; + } + if(!pb_encode_submessage(ostream, _subscriptions_fields, &subs)) { + return false; } } else{ @@ -319,6 +343,25 @@ bool encode_datapoint_callback(pb_ostream_t *ostream, const pb_field_t *field, v return true; } +bool encode_cmd_string_callback(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg) +{ + if (ostream != NULL && field->tag == command_cmd_str_tag) { + if (!pb_encode_tag_for_field(ostream, field)) + return false; + return pb_encode_string(ostream, cmd_str, strlen((char*)cmd_str)); + } + return true; +} + +bool decode_cmd_string_callback(pb_istream_t *istream, const pb_field_t *field, void **args) +{ + while (istream->bytes_left) { + if (!pb_read(istream, &str_buf[str_ptr++], 1)) + return false; + } + return true; +} + /** * @brief System Clock Configuration * @retval None @@ -365,7 +408,7 @@ void SystemClock_Config(void) static void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; - hi2c1.Init.ClockSpeed = 400000; + hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = I2C_ADDRESS; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
