Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4909

Interfacing (DSI, CSI, I2C, etc.) • Re: UART RX ttyAMA0 (PL011) sometimes interrupted

$
0
0
I'm using the Raspberry Pi Zero 2 W and I have one of the 4 cores dedicated to running the thread.
Currently the thread looks like this:

Code:

void *read_thread(void *arg) {    int fd = *(int *)arg;    unsigned char buffer[RX_BUFFER_SIZE];    int bytes_read;    cpu_set_t cpuset;                                   // Code to cause this thread to be run on    CPU_ZERO(&cpuset);                                  // a particular core.    CPU_SET(3,&cpuset);                                 // Add this to your thread functions with suitable parameter adjustments.    sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);   //    while (1) {    bytes_read = read(fd, buffer, RX_BUFFER_SIZE);        if (bytes_read > 0) {            // Lock the ring buffer            pthread_mutex_lock(&ring_buffer.lock);            // Write data to the ring buffer          for (int i = 0; i < bytes_read; i++) {                ring_buffer.buffer[ring_buffer.head] = buffer[i];                ring_buffer.head = (ring_buffer.head + 1) % RING_BUFFER_SIZE;                ring_buffer.count++;                // Check if the ring buffer is full                if (ring_buffer.count == RING_BUFFER_SIZE) {                    // Overwrite the oldest data                    ring_buffer.tail = (ring_buffer.tail + 1) % RING_BUFFER_SIZE;                }            }            if (ring_buffer.count > ringmax) ringmax = ring_buffer.count;            // Unlock the ring buffer            pthread_mutex_unlock(&ring_buffer.lock);        }    }    return NULL;}
I added some GPIO toggling and it looks like it is getting stuck on this part:
bytes_read = read(fd, buffer, RX_BUFFER_SIZE);
Normally it gets stuck here for a few ms, maybe 10 or sometimes close to 20 ms, but when I tried to run the program with this command:
sudo chrt 1 sudo -u $USER program
it got stuck at the same point routinely for like 800 ms.

I decided enough screwing around with this, I'm going to implement hardware flow-control now and hope that works for me. I've set aside a 100,000 byte buffer on the sender side, so that should give me about 40 ms of delay that it can handle. Thank you all for your help.

Statistics: Posted by KingJoffrey — Wed Apr 24, 2024 5:43 pm



Viewing all articles
Browse latest Browse all 4909

Trending Articles