Non blocking GETCHAR() or equivalent

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Non blocking GETCHAR() or equivalent

367件の閲覧回数
Danielb
Contributor I

Dear support team,

I am programming a FRDM-MCXN947 dev board using MCUXpresso VS code extension. I need a way to look for any input from the serial monitor in a non blocking way. Looking through the drivers I was unable to find this and I was unsuccessful in implementing this myself.

Below you find my main.c to give a better idea of what I need to implement. Whenever there is input from a python GUI that communicates with the microcontroller I need to read the parameter and then restart the ADC and DAC with these parameters. Now I check for the parameters once and that works. I now need a now blocking way to check for new parameters.

If someone could tell me if there is a driver I am completely missing or help me implement this it would be much appreciated.

Thank you in advance

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "global.h"
#include "DAC_files/DAC_micro_hot_plate.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "source/drivers/pin_mux.h"
#include "source/ADC_files/adc_16_strip.h"

// Handler for the SysTick interrupt
void SysTick_Handler(void) {
    // Increment the timestamp
    g_timestamp_ms++;
}

// Function to parse the input string and update parameters
int updateParameters(char* input) {
    char* token;

    // Parse waveType
    token = strtok(input, ",");
    if (token != NULL) {
        waveType = atoi(token);
        PRINTF("waveType: %d\n", waveType);
    } else {
        PRINTF("Error parsing waveType\n");
        return -1;
    }

    // Parse dutyCycle
    token = strtok(NULL, ",");
    if (token != NULL) {
        dutyCycle = atoi(token);
        PRINTF("dutyCycle: %d\n", dutyCycle);
    } else {
        PRINTF("Error parsing dutyCycle\n");
        return -1;
    }

    // Parse desiredFrequency
    token = strtok(NULL, ",");
    if (token != NULL) {
        desiredFrequency = atof(token);
        PRINTF("desiredFrequency: %f\n", desiredFrequency);
    } else {
        PRINTF("Error parsing desiredFrequency\n");
        return -1;
    }

    // Parse desiredAmplitude
    token = strtok(NULL, ",");
    if (token != NULL) {
        desiredAmplitude = atof(token);
        PRINTF("desiredAmplitude: %f\n", desiredAmplitude);
    } else {
        PRINTF("Error parsing desiredAmplitude\n");
        return -1;
    }

    // Parse excitationVoltagePerStrip
    token = strtok(NULL, ",");
    if (token != NULL) {
        excitationVoltagePerStrip = atof(token);
        PRINTF("excitationVoltagePerStrip: %f\n", excitationVoltagePerStrip);
    } else {
        PRINTF("Error parsing excitationVoltagePerStrip\n");
        return -1;
    }

    // Parse mux_freq
    token = strtok(NULL, ",");
    if (token != NULL) {
        mux_freq = atoi(token);
        PRINTF("mux_freq: %d\n", mux_freq);
    } else {
        PRINTF("Error parsing mux_freq\n");
        return -1;
    }

    return 0; // Success
}

int initialize_dac_hot_plate(void) {
    // Initialize the DAC
    Dac_and_other_setup();

    // Proceed with DAC setup and wave generation only after receiving parameters
    g_Dac14ValueArraySize = (uint32_t)(SAMPLE_RATE / desiredFrequency);
    if (g_Dac14ValueArraySize > MAX_ARRAY_SIZE) {
        g_Dac14ValueArraySize = MAX_ARRAY_SIZE;
    }

    if (desiredFrequency < LOWEST_FREQ_POSSIBLE) {
        PRINTF("Desired frequency is too low, please enter a frequency above 0.002Hz\n");
        return -1;
    }

    switch (waveType) {
        case 0:
            generateSineWave(g_Dac14Values, g_Dac14ValueArraySize);
            break;
        case 1:
            generateSquareWave(g_Dac14Values, g_Dac14ValueArraySize);
            break;
        case 2:
            generateTriangleWave(g_Dac14Values, g_Dac14ValueArraySize);
            break;
        default:
            PRINTF("Invalid wave type\n");
            return -1;
    }

    // Initialize the timer
    SetupTimer();

    return 0; // Success
}

int initialize_voltage_adc(void) {
    // Initialize the ADC
    ADC_Initialize();

    ADC_timer_setup(mux_freq);

    return 0; // Success
}



int main(void) {
    char buffer[256] = {0};
    int index = 0;
    char ch;
    int paramsReceived = 0;

    BOARD_InitPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    // Dac_and_other_setup();
    // ADC_Initialize();

    // Configure Systick for 1ms for the timestamps
    SysTick_Config(SystemCoreClock / 1000U);

    // Enable interrupt and set priority for systick interrupt
    NVIC_SetPriority(SysTick_IRQn, 1U);

    PRINTF("Waiting for parameters...\n");

    while (!paramsReceived) {
        ch = GETCHAR();  // Wait and read a character from UART
        buffer[index++] = ch;  // Store the received character in the buffer
        if (ch == '\n' || ch == '\r') {
            buffer[index] = '\0';  // Null-terminate the string
            PRINTF("Received string: %s\n", buffer);  // Print the received string for debugging
            if (updateParameters(buffer) == 0) {
                PRINTF("Parameters received and set.\n");
                paramsReceived = 1;
            } else {
                PRINTF("Error receiving parameters. Please resend.\n");
            }
            index = 0;
            buffer[0] = '\0';
        }
    }

    // Initialize the DAC and timer
    if (initialize_dac_hot_plate() != 0) {
    PRINTF("Failed to initialize DAC and hot plate\n");
    // Handle error
    }

    // Initialize the ADC and timer
    if (initialize_voltage_adc() != 0) {
    PRINTF("Failed to initialize ADC and timer\n");
    // Handle error
    }

    // Start the ADC timer
    // ADC_timer_setup(mux_freq);

    while (1) {
        
        // Main loop - could be used for further processing
        //print the array containing adc values to the console
        for (int i = 0; i < g_strip_count; i++) {
           PRINTF("Strip %d: %d\n", g_strip_values[i][0], g_strip_values[i][1]);
        }
    }


    return 0;
}

 

ラベル(1)
0 件の賞賛
返信
1 返信

290件の閲覧回数
EdwinHz
NXP TechSupport
NXP TechSupport

Hi @Danielb,

The SDK's UART API a function called "LPUART_TransferReceiveNonBlocking()", which receives data through the UART peripheral based on an interruption, rather than being blocking. Please look into it and let me know if you have further inquiries about this topic.

BR,
Edwin.

0 件の賞賛
返信