Hi everyone,
I'm trying to create a small project in which two different threads (send_command and received_buffer) are launched and executed simultaneously within the main function (two infinite while loops) which are used respectively to send and receive buffers from the UART (I'm using the TWRK65F180M evaluation board).
The code is as follows:
#include <stdio.h>
#include <stdint.h
> #include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "board.h"
#include "fsl_uart_driver.h"
#include "fsl_clock_manager.h"
const uint8_t buffer[] = {0x55,0x02,0x01,0x00,0x00,0x03};
const uint8_t buffer_rx[1];
int main (void)
{
uint32_t byteCountBuff = 0;
uint32_t byteCountBuff_Rx = 0;
pthread_t thread1, thread2;
int iret1, iret2;
uart_state_t uart2State;
uart_state_t uart1State;
uart_user_config_t uart2Config = {
.bitCountPerChar = kUart8BitsPerChar,
.parityMode = kUartParityDisabled,
.stopBitCount = kUartOneStopBit,
.baudRate = 115200U
};
uart_user_config_t uart1Config = {
.bitCountPerChar = kUart8BitsPerChar,
.parityMode = kUartParityDisabled,
.stopBitCount = kUartOneStopBit,
.baudRate = 2000000U //BaudRate = 2MHz
};
// Enable clock for PORTs, setup board clock source, config pin
hardware_init();
// Call OSA_Init to setup LP Timer for timeout
OSA_Init();
// Initialize the uart module with base address and config structure
UART_DRV_Init(BOARD_DEBUG_UART_INSTANCE, &uart2State, &uart2Config);
UART_DRV_Init(UART1_IDX, &uart1State, &uart1Config);
// Inform to start blocking example
byteCountBuff = sizeof(buffer);
byteCountBuff_Rx = sizeof(buffer_rx);
iret1 = pthread_create( &thread1, NULL, received_buffer, NULL);
iret2 = pthread_create( &thread2, NULL, send_command, NULL);
}
int send_command(void)
{
while(1)
{
UART_DRV_SendDataBlocking(UART1_IDX, buffer, byteCountBuff, 1000u);
delay_ms(100000);
}
}
int received_buffer(void)
{
while(true)
{
// Call received API
UART_DRV_ReceiveData(UART1_IDX, buffer_rx, byteCountBuff_Rx);
// Wait until we receive a character
while (kStatus_UART_RxBusy == UART_DRV_GetReceiveStatus(UART1_IDX, NULL)){}
// Echo received character
UART_DRV_SendData(BOARD_DEBUG_UART_INSTANCE, buffer_rx, byteCountBuff_Rx);
}
}
//funzione per attendere tot ms prima della prossima operazione.
void delay_ms(int times)
{
int x;
for (x=times; x>0; x--)
{
}
}
The problem is that when I go to compile the project, the compiler doesn't recognize the pthread_t datatype and the pthread_create function. I thought including the <pthread.h> library would fix the problem, but it didn't. Could anyone
advise what other library to include or a way to fix this recognition problem?
Thanks.