Reading Serial1 Pins Directly

argon
Tags: #<Tag:0x00007fe21bfacd28>

#1

I am using an Argon board running OS 1.4.4 and am unable to convert the Serial1 RX/TX pins into GPIO.

In the setup function, I initialize the Serial1 port with this code:

    Serial1.begin(38400, SERIAL_8E2);

I have a wire connected directly to the RX pin that acts as a one-wire Serial communication bus. When that wire is measured with the bus active, the output is around 2.2 Volts to 2.5 Volts. My understanding is that reading from that pin should return a non-zero result, but the output of reading that pin is always 0 Volts.

In the loop, I call a function to read characters from the Serial1 port and that function works correctly. However, when I end the Serial1 communication and attempt to read an analog voltage from that pin, the result does not match the input to that pin. The code I have used to try and free those pins for reading is as below:

    Serial1.end();
    int32_t rxStatus = analogRead(RX);
    LOGINFO(("RX - %d", rxStatus));
    Serial1.begin(38400, SERIAL_8E2);

My understanding of the USART library is that when a serial port is closed, the pins return to an INPUT state, from this code in the Serial1.end() function:

        HAL_Pin_Mode(txPin_, INPUT);
        HAL_Set_Pin_Function(txPin_, PF_NONE);
        HAL_Pin_Mode(rxPin_, INPUT);
        HAL_Set_Pin_Function(rxPin_, PF_NONE);

        if (config_.config & SERIAL_FLOW_CONTROL_CTS) {
            HAL_Pin_Mode(ctsPin_, INPUT);
            HAL_Set_Pin_Function(ctsPin_, PF_NONE);
        }
        if (config_.config & SERIAL_FLOW_CONTROL_RTS) {
            HAL_Pin_Mode(rtsPin_, INPUT);
            HAL_Set_Pin_Function(rtsPin_, PF_NONE);
        }

I have also attempted to define the RX pin as the INPUT, INPUT_PULLDOWN, or INPUT_PULLUP states, but the result still does not match the input.

Is there a way to free the RX pin to be read as an analog GPIO pin and return that pin to the Serial1 port?


#2

@sunmed1991, the RX pin is always an INPUT so it will not be HIGH and most likely float. The TX pin is the NRZ (non-return-zero) output. What you are measuring is likely from the circuit connected to the RX pin.


#3

Why would you initialise this port as a UART when you want to use it for I/O? Perhaps if you shared your use case we could provide better answers?


#4

I’m also not convinced that RX supports analogRead() at all, but it will digitalRead() - using pinMode(RX, INPUT) and not Serial1.begin().

As you can see here, there is no ADC_CHANNEL assigned for RX - which would be required for analogRead() to work.


#5

Echo that - its in the data sheet here - only the pins attached to an ADC peripheral will read analogue


#6

I am working with a one-wire serial communication bus. I read from Serial1 to monitor that bus, but there is a charging state of the device I am working with where the one-wire serial bus is pulled to ground. I was attempting to read from that pin with Serial1 and monitor its voltage to see if the charging state is active. I see from a later reply that Serial1 does not have an ADC, so I will attempt another option. Thank you!


#7

There is a library for one wire - it is somewhat problematic on Gen3 devices. A better solution is to use a I2C to OneWire bridge. this will drive the end devices correctly and provide accurate data.