0

I am using a raspberry pi 3 with wiringPi for my project. My project is to communicate with 2 SDI-12 protocol sensors. In order for the sensors to wake up, sdi demands to send a wake up signal which is a high for at least 12ms and a low for 8.3ms. Then you need to send a command in ascii. The commands are sent via UART. The problem is that with UART you can not send the wake up signal.

So i need to use both GPIO pins to send the wake up signal and UART pins for read and write. I pass all these signals throught a bus and with the right control signals i manage to pass the right signals in the output

My code:

int main() {

    wiringPiSetupGpio();
    pinMode(TXENABLE, OUTPUT);
    pinMode(RXENABLE, OUTPUT);
    pinMode(UARTENABLE, OUTPUT);
    pinMode(GPIOENABLE, OUTPUT);
    pinMode(WAKEUP, OUTPUT);

    digitalWrite(RXENABLE, LOW);    // disable reading
    digitalWrite(TXENABLE, LOW);    // disable transmition
    digitalWrite(UARTENABLE, LOW);  // disable UART
    digitalWrite(GPIOENABLE, LOW);  // disable WAKEUP

    digitalWrite(RXENABLE, HIGH);    // disable reading
    digitalWrite(TXENABLE, HIGH);    // disable transmition
    digitalWrite(UARTENABLE, HIGH);  // disable UART
    digitalWrite(GPIOENABLE, HIGH);  // disable GPIO
    digitalWrite(WAKEUP, LOW);    

    int uart0_filestream = -1;
    uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK); //Open in non blocking read/write mode

    if (uart0_filestream == -1)
    {
        //ERROR - CAN'T OPEN SERIAL PORT
        printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
    }

    fcntl (uart0_filestream, F_SETFL, O_RDWR);


    //CONFIGURE THE UART
    struct termios options;
    tcgetattr(uart0_filestream, &options);
    options.c_cflag = B1200 | CS7 | CLOCAL | CREAD | PARENB;
    options.c_iflag = IGNPAR;
    options.c_oflag = 0;
    options.c_lflag = 0;
    tcflush(uart0_filestream, TCIFLUSH);
    tcsetattr(uart0_filestream, TCSANOW, &options);

    //----- TX BYTES -----
    unsigned char tx_buffer[20];
    unsigned char *p_tx_buffer;

    p_tx_buffer = &tx_buffer[0];
    *p_tx_buffer++ = '?';
    *p_tx_buffer++ = '!';

    //digitalWrite(RXENABLE, HIGH);    // disable reading
    digitalWrite(GPIOENABLE, LOW); // enable WAKEUP
    digitalWrite(TXENABLE, LOW);   // enable transmission

    digitalWrite(WAKEUP, HIGH);
    delayMicroseconds(13000);
    digitalWrite(WAKEUP, LOW);
    delayMicroseconds(8450); 

    digitalWrite(GPIOENABLE, HIGH);   // disable WAKEUP
    digitalWrite(UARTENABLE, LOW);  // enable UART (PINK SIGNAL)

    if (uart0_filestream != -1)
    {
        //Filestream, bytes to write, number of bytes to write
        int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
        if (count < 0) {
            printf("UART TX error\n");
        }
    }

    digitalWrite(UARTENABLE, HIGH);  // disable UART
    digitalWrite(TXENABLE, HIGH);    // disable transmition
    digitalWrite(RXENABLE, LOW);   // enable reading

    //delayMicroseconds(1000000);     // wait 2 seconds

    close(uart0_filestream);

    return 0;
}

The pins i use:

#define TXENABLE 23    // control transmition
#define RXENABLE 24    // control reading
#define UARTENABLE 12  // control UART
#define GPIOENABLE 20  // control GPIO
#define WAKEUP 21        // for wake up command

Can tou please take a look? What I need is UARTENABLE signal being low during the transmition of UART data. (around 18ms). Instead of this, the oscilloscope image happens. The pink signal is the one that does not follow the code.

Does anyone have any idea about this?what i get

Also, I changed /boot/config.txt according to the published instructions. (changed enable_UART = 1)

1

The problem is that after the

int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));

you must wait until all output has been transmitted, and then call the

digitalWrite(UARTENABLE, HIGH);  // disable UART

to change the enable signal.

You can use thetcdrain()function.

tcdrain() waits until all output written to the object referred to by fd has been transmitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.