diff options
| author | Aditya Naik | 2020-03-24 18:26:52 -0400 |
|---|---|---|
| committer | Aditya Naik | 2020-03-24 18:26:52 -0400 |
| commit | 49b638a6ae3c1afcff640809c0a34d9021ae844b (patch) | |
| tree | 665086921ad1c7615bf30a67480dc2a98153a4fe | |
| parent | ba94542d42014b8b8929b2b4ea9a17a213cd6230 (diff) | |
Working MDR req and MDR ACK
| -rw-r--r-- | Src/main.c | 39 |
1 files changed, 22 insertions, 17 deletions
@@ -51,7 +51,7 @@ #define I2C_ADDRESS 0x30F
/* Macro to toggle between master and slave firmware */
-#define MASTER
+/* #define MASTER */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
@@ -122,11 +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_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 */
@@ -135,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]);
@@ -155,7 +152,13 @@ int main(void) memset(debug_buf, 0, 128);
#endif /* TESTING_ENABLE */
- while (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)I2C_ADDRESS, (uint8_t*)MDR_req_buf, 2, 10000) != HAL_OK);
+ 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 /* 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);
@@ -168,7 +171,6 @@ int main(void) 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;
@@ -245,7 +247,7 @@ 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
@@ -265,7 +267,7 @@ int main(void) uint8_t errbuf[] = "MDR request decoding error\r\n";
HAL_UART_Transmit(&huart1, errbuf, sizeof(errbuf), 100);
#endif /* DEBUG_ENABLE */
- continue;
+ /* continue; */
}
#ifdef TESTING_ENABLE
@@ -280,7 +282,7 @@ int main(void) #endif /* MASTER */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
- }
+ /* } */
/* USER CODE END 3 */
}
@@ -420,7 +422,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;
@@ -452,7 +454,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 */
}
|
