-1

Is there something wrong with my code/settings? I tried using putty's terminal to view the output; however, even though I configured it correctly with the baud settings, etc., no data is appearing. I am able to see the "hello world" in the terminal though. It is on a NUCLEO-F767ZI Microprocessor. I am using STM32CubeIDE.

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include "stm32f7xx_hal.h"

/* Define your UART handle */
UART_HandleTypeDef huart3;

ADC_HandleTypeDef hadc; // Declare 'hadc' here

void Error_Handler(void) { while (1); }
void SystemClock_Config(void) { /* Configure system clock here */ }

/* Initialize UART */
void init_uart(void) {
    huart3.Instance = USART3;
    huart3.Init.BaudRate = 9600; // Baud rate: 9600 bps
    huart3.Init.WordLength = UART_WORDLENGTH_8B;
    huart3.Init.StopBits = UART_STOPBITS_1; // Stop bits: 1
    huart3.Init.Parity = UART_PARITY_NONE; // Parity: None
    huart3.Init.Mode = UART_MODE_TX_RX;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart3.Init.OverSampling = UART_OVERSAMPLING_16;
    HAL_UART_Init(&huart3);
}

/* Custom implementation of _write for printf redirection */
int _write(int file, char *ptr, int len) {
    HAL_UART_Transmit(&huart3, (uint8_t*)ptr, len, HAL_MAX_DELAY);
    return len;
}

void init_gpio(void) {
    // GPIO initialization code
}

void init_timer(void) {
    // Timer initialization code
}

void init_adc(void) {
    // ADC initialization code
}

int main(void) {
    HAL_Init();
    SystemClock_Config();
    init_uart();
    init_gpio();
    init_timer();
    init_adc();

    uint32_t start_time = 0;
    float input_voltage = 0.0f;

    while (1) {
        printf("Hello World\n");

        start_time = HAL_GetTick(); // Start time for capturing voltage data

        while (HAL_GetTick() - start_time < 5000000) // Capture data for 5 seconds
        {
            // Read data from ADC and process if needed
            // (similar to your previous ADC reading code)

            // Print voltage and time
            HAL_ADC_Start(&hadc); // Declare 'hadc' here
            HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

            uint32_t adc_value = HAL_ADC_GetValue(&hadc);
            input_voltage = (float)adc_value / 4095.0f * 3.3f;

            HAL_ADC_Stop(&hadc);

            printf("Time: %lu ms, Input Voltage: %.2f V\n", HAL_GetTick() - start_time, input_voltage);

            // No fflush(stdout) here

            HAL_Delay(1); // Delay between voltage measurements
        }

        printf("Measurement done\n"); // Output to indicate end of measurement
    }
}
Annie
  • 1
  • 1
  • 1
    Is it just me, or your link is broken? It opens stackoverflow when I click on it. Also, please, post the code properly. It takes you 30 seconds. – Ilya Aug 29 '23 at 11:32
  • Copied the link text and it worked (still fix it tho by pasting code here and removing the link altogether). Why do you have printf? Have you made your own implementation of printf? If not, it won't work. Printf doesn't exist natively the way it does on a desktop app. – Ilya Aug 29 '23 at 11:35
  • I fixed it, the formatting before wasn't working when I tried pasting the code directly. In the terminal I am able to see the "hello world" which also uses the printf, but not the data that I need. – Annie Aug 29 '23 at 20:03
  • Then remove the message structure from where you send data, send fixed data there (like "goodbye, world"). See if it works. If not, it's something before that. If it does, check what the data is. Additionally, make sure you don't violate timings. Remember that physically shifting data out takes longer than for your code to execute many many lines of code. Depending on implementation, it can cause trouble if you have new printf request while previous data (e.g. last byte) is still being shifted out. Try adding significant delays after printf like 50ms just to see if anything changes. – Ilya Aug 29 '23 at 20:08
  • `5000000` ticks is not 5 seconds, it's 5000 seconds. – pmacfarlane Aug 30 '23 at 10:41
  • You should change your HAL timeout values from `HAL_MAX_DELAY` to something more realistic (e.g. 1000 ms) and check if any of them actually time out. – pmacfarlane Aug 30 '23 at 10:46

1 Answers1

0

I'm not familiar with this series of controllers, but it looks like you're using printf and fflush to send the output to stdout; how can you double-check that the serial channel is mapped to stdout?

  • I am able to see the "hello world" now in the terminal which also uses printf, but I can't see any of the data I need. – Annie Aug 29 '23 at 20:04