summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAditya Naik2020-03-24 18:31:51 -0400
committerAditya Naik2020-03-24 18:31:51 -0400
commitc1195a3c45da66c85d02b7047289e51e4d38ae5b (patch)
tree3e0454a86324bf10d8254b7c76d42b99f31505d2
parentf34cd1f2387edb69f238a994787808f4444d0a71 (diff)
parentc973626653856e74926d633cd3ea3dea0cd3575e (diff)
MDR req merge
-rw-r--r--.gitignore1
-rw-r--r--Src/main.c68
2 files changed, 44 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index 554ca6b..10e9fff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,5 +10,6 @@ uctrl-firmware.txt
.mxproject
*.o
*.bin
+*~
src/
MDK-ARM/ \ No newline at end of file
diff --git a/Src/main.c b/Src/main.c
index faa4d8c..9e4cbaa 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -122,12 +122,11 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
- while (1)
- {
+ /* while (1) */
+ /* { */
#ifdef MASTER
- uint8_t MDR_req_buf[64], debug_buf[128], term[]="\r\n";
- uint8_t MDR_ACK_buf[8];
+ uint8_t MDR_req_buf[64], debug_buf[128] = {0}, term[]="\r\n";
m2s_MDR_request MDR_req_message;
pb_ostream_t MDR_req_stream = pb_ostream_from_buffer(MDR_req_buf, sizeof(MDR_req_buf));
MDR_req_message.record_type = 1; /* Placeholder for default record type */
@@ -136,17 +135,14 @@ int main(void)
uint8_t err_buf[] = "MDR reqest encoding error\r\n";
HAL_UART_Transmit(&huart1, err_buf, sizeof(err_buf), 100);
#endif /* DEBUG_ENABLE */
- while(1) {
- HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
- HAL_Delay(500);
- }
+ Error_Handler();
}
uint16_t enc_size = MDR_req_stream.bytes_written;
#ifdef TESTING_ENABLE
sprintf((char*)debug_buf, "Encoded length: %d\r\n", enc_size);
HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
-
- uint8_t bufbuf[] = "Buffer: ";
+ memset(debug_buf, 0, 128);
+ uint8_t bufbuf[] = "MDR req buffer: ";
HAL_UART_Transmit(&huart1, bufbuf, sizeof(bufbuf), 100);
for(int x=0; x<enc_size; x++) {
sprintf((char*)debug_buf+x, "%x", MDR_req_buf[x]);
@@ -171,16 +167,35 @@ int main(void)
#endif /* DEBUG_ENABLE */
}
-#ifdef TESTING_ENABLE
- sprintf((char*)debug_buf, "Got MDR message length: %ld\r\n", MDR_ACK.MDR_res_length);
+ if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t*)MDR_req_buf, 2, 10000) != HAL_OK) {
+#ifdef DEBUG_ENABLE
+ sprintf((char*)debug_buf, "Unable to send MDR request. I2C error: %ld\r\n", HAL_I2C_GetError(&hi2c1));
HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
memset(debug_buf, 0, 128);
-#endif /* TESTING_ENABLE */
- /* } */
+#endif /* DEBUG_ENABLE */
+ }
HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
+ while (HAL_I2C_Master_Receive(&hi2c1, (uint8_t)I2C_ADDRESS, (uint8_t*)MDR_req_buf, 2, 10000) != HAL_OK);
+
+ uint8_t bufbuf2[] = "MDR ACK buffer: ";
+ HAL_UART_Transmit(&huart1, bufbuf2, sizeof(bufbuf2), 100);
+ for(int x=0; x<2; x++) {
+ sprintf((char*)debug_buf+x, "%x", MDR_req_buf[x]);
+ }
+ HAL_UART_Transmit(&huart1, debug_buf, 2, 100);
+ HAL_UART_Transmit(&huart1, term, 2, 100);
+ memset(debug_buf, 0, 128);
+
+ pb_istream_t MDR_ACK_istream = pb_istream_from_buffer(MDR_req_buf, 2);
+ s2m_MDR_req_ACK MDR_ACK;
+ pb_decode(&MDR_ACK_istream, s2m_MDR_req_ACK_fields, &MDR_ACK);
+#ifdef TESTING_ENABLE
+ sprintf((char*)debug_buf, "Got MDR message length: %ld\r\n", MDR_ACK.MDR_res_length);
+ HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
+ memset(debug_buf, 0, 128);
+#endif /* TESTING_ENABLE */
-
#else /* MASTER */
uint8_t buffer[128], debug_buf[128], term[]="\r\n";
size_t MDR_enc_size;
@@ -217,7 +232,7 @@ int main(void)
memset(debug_buf, 0, 128);
#endif
- uint8_t MDR_ACK_buf[64];
+ uint8_t MDR_ACK_buf[8];
s2m_MDR_req_ACK ack;
ack.MDR_res_length = MDR_enc_size;
pb_ostream_t MDR_ack_ostream = pb_ostream_from_buffer(MDR_ACK_buf,
@@ -247,14 +262,14 @@ int main(void)
uint8_t debug_buf[] = "Failed to get MDR req\r\n";
HAL_UART_Transmit(&huart1, debug_buf, sizeof(debug_buf), 100);
#endif /* DEBUG_ENABLE */
- continue;
+ /* continue; */
}
#ifdef TESTING_ENABLE
- uint8_t bufbuf2[] = "MDR req buffer: ";
+ uint8_t bufbuf2[] = "MDR ACK buffer: ";
HAL_UART_Transmit(&huart1, bufbuf2, sizeof(bufbuf2), 100);
for(int x=0; x<2; x++) {
- sprintf((char*)debug_buf+x, "%x", MDR_req_buf[x]);
+ sprintf((char*)debug_buf+x, "%x", MDR_ACK_buf[x]);
}
HAL_UART_Transmit(&huart1, debug_buf, MDR_enc_size, 100);
HAL_UART_Transmit(&huart1, term, 2, 100);
@@ -266,8 +281,8 @@ int main(void)
#ifdef DEBUG_ENABLE
uint8_t errbuf[] = "MDR request decoding error\r\n";
HAL_UART_Transmit(&huart1, errbuf, sizeof(errbuf), 100);
-#endif /* DEBUG_ENABLE */
- continue;
+#endif /* DEBUG_ENABLE */
+ /* continue; */
}
#ifdef TESTING_ENABLE
@@ -277,7 +292,7 @@ int main(void)
#endif /* TESTING_ENABLE */
HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
-
+
if (HAL_I2C_Slave_Transmit(&hi2c1, (uint8_t*)MDR_ACK_buf, 2, 1000) != HAL_OK) {
#ifdef DEBUG_ENABLE
sprintf((char*)debug_buf, "Failed to send MDR ACK to master\r\n");
@@ -287,7 +302,7 @@ int main(void)
#endif /* MASTER */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
- }
+ /* } */
/* USER CODE END 3 */
}
@@ -427,7 +442,7 @@ static void MX_GPIO_Init(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++) {
+ for (int x=0; x<25; x++) {
_subscriptions subs;
subs.module_id = x+10*x;
subs.i2c_address = x+1;
@@ -459,7 +474,10 @@ void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
-
+ while (1) {
+ HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
+ HAL_Delay(1000);
+ }
/* USER CODE END Error_Handler_Debug */
}