diff --git a/Core/Src/main.c b/Core/Src/main.c index 2dea81a..0cb24da 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -102,15 +102,22 @@ static void hex_dump(const uint8_t *data, uint16_t len) // Print ASCII representation pos += snprintf(line_buf + pos, sizeof(line_buf) - pos, " |"); - for (int j = 0; j < 16 && (i + j) < len; j++) { - uint8_t c = data[i + j]; - if (c >= 32 && c <= 126) { - pos += snprintf(line_buf + pos, sizeof(line_buf) - pos, "%c", c); + for (int j = 0; j < 16; j++) { + if (i + j < len) { + uint8_t c = data[i + j]; + if (c >= 32 && c <= 126) { + line_buf[pos++] = c; + } else { + line_buf[pos++] = '.'; + } } else { - pos += snprintf(line_buf + pos, sizeof(line_buf) - pos, "."); + // Pad with space if beyond data length + line_buf[pos++] = ' '; } } - pos += snprintf(line_buf + pos, sizeof(line_buf) - pos, "|\r\n"); + line_buf[pos++] = '|'; + line_buf[pos++] = '\r'; + line_buf[pos++] = '\n'; // Send line to UART1 HAL_UART_Transmit(&huart1, (uint8_t *)line_buf, (uint16_t)pos, HAL_MAX_DELAY); @@ -160,21 +167,6 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - static uint32_t last_toggle_timestamp; - if (HAL_GetTick() - last_toggle_timestamp >= 1000) { - HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); - /* Heartbeat: emit tick timestamp over UART1 and blink the PB6 LED */ - int msg_len = snprintf(heartbeat_buf, sizeof(heartbeat_buf), - "PB6 heartbeat %lu\r\n", (unsigned long)HAL_GetTick()); - if (msg_len > 0) { - if (msg_len >= (int)sizeof(heartbeat_buf)) { - msg_len = (int)sizeof(heartbeat_buf) - 1; - } - HAL_UART_Transmit(&huart1, (uint8_t *)heartbeat_buf, (uint16_t)msg_len, - HAL_MAX_DELAY); - } - last_toggle_timestamp = HAL_GetTick(); - } /* Check if data received from UART2 */ if (rx_complete) { @@ -195,6 +187,23 @@ int main(void) /* Re-arm UART2 reception */ HAL_UART_Receive_IT(&huart2, rx_buf, sizeof(rx_buf)); } + + static uint32_t last_toggle_timestamp; + if (HAL_GetTick() - last_toggle_timestamp >= 1000) { + HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); + /* Heartbeat: emit tick timestamp over UART1 and blink the PB6 LED */ + int msg_len = snprintf(heartbeat_buf, sizeof(heartbeat_buf), + "%lu PB6 heartbeat\r\n", (unsigned long)HAL_GetTick()); + if (msg_len > 0) { + if (msg_len >= (int)sizeof(heartbeat_buf)) { + msg_len = (int)sizeof(heartbeat_buf) - 1; + } + HAL_UART_Transmit(&huart1, (uint8_t *)heartbeat_buf, (uint16_t)msg_len, + HAL_MAX_DELAY); + } + last_toggle_timestamp = HAL_GetTick(); + } + /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } @@ -296,13 +305,13 @@ static void MX_USART2_UART_Init(void) /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; - huart2.Init.BaudRate = 115200; + huart2.Init.BaudRate = 14400; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart2.Init.OverSampling = UART_OVERSAMPLING_16; + huart2.Init.OverSampling = UART_OVERSAMPLING_8; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler();