Skip to content

Commit

Permalink
adding new apis
Browse files Browse the repository at this point in the history
  • Loading branch information
kanndil committed Jan 12, 2025
1 parent c6f9630 commit ffaf9b9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 74 deletions.
80 changes: 79 additions & 1 deletion fw/EF_UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ EF_DRIVER_STATUS EF_UART_setDataSize(EF_UART_TYPE_PTR uart, uint32_t value){
return status;
}


// todo: make this generic between 1 and 2 bits
EF_DRIVER_STATUS EF_UART_setTwoStopBitsSelect(EF_UART_TYPE_PTR uart, bool is_two_bits){

EF_DRIVER_STATUS status = EF_DRIVER_OK;
Expand Down Expand Up @@ -774,6 +774,84 @@ EF_DRIVER_STATUS EF_UART_busy(EF_UART_TYPE_PTR uart, bool* busy_flag){

// todo: document the threshold is the fifo max


// Function to initialize and configure the UART
EF_DRIVER_STATUS UART_Init(EF_UART_TYPE_PTR uart, uint32_t baud_rate, uint32_t bus_clock, uint32_t data_bits, bool two_stop_bits, enum parity_type parity, uint32_t timeout, uint32_t rx_threshold, uint32_t tx_threshold) {
EF_DRIVER_STATUS status = EF_DRIVER_OK;

if (uart == NULL) {
status = EF_DRIVER_ERROR_PARAMETER; // Return EF_DRIVER_ERROR_PARAMETER if uart is NULL
}

// Calculate and set the prescaler
uint32_t prescaler = (bus_clock / (baud_rate * 16)) - 1;
if (status == EF_DRIVER_OK) {status = EF_UART_setPrescaler(uart, prescaler);} else {}

// Configure data bits, stop bits, and parity

// Set data bits (5-9 bits)
if (status == EF_DRIVER_OK) {status = EF_UART_setDataSize(uart, data_bits);} else {}

// Set stop bits (1 or 2)
if (status == EF_DRIVER_OK) {status = EF_UART_setTwoStopBitsSelect(uart, two_stop_bits);} else {}

// Set parity type
if (status == EF_DRIVER_OK) {status = EF_UART_setParityType(uart, parity);} else {}

// Set the receiver timeout value
if (status == EF_DRIVER_OK) {status = EF_UART_setTimeoutBits(uart, timeout);} else {}

// Set RX and TX FIFO thresholds
if (status != EF_DRIVER_OK) {status = EF_UART_setRxFIFOThreshold(uart, rx_threshold);} else {}
if (status != EF_DRIVER_OK) {status = EF_UART_setTxFIFOThreshold(uart, tx_threshold);} else {}

// Enable the UART and both RX and TX
if (status != EF_DRIVER_OK) {status = EF_UART_enable(uart);} else {}
if (status != EF_DRIVER_OK) {status = EF_UART_enableRx(uart);} else {}
if (status != EF_DRIVER_OK) {status = EF_UART_enableTx(uart);} else {}

// Optionally enable glitch filter and loopback for testing
if (status != EF_DRIVER_OK) {status = EF_UART_enableGlitchFilter(uart);} else {}
if (status != EF_DRIVER_OK) {status = EF_UART_enableLoopBack(uart);} else {}

return EF_DRIVER_OK;
}


// Function to receive a string using UART
EF_DRIVER_STATUS EF_UART_readCharArr(EF_UART_TYPE_PTR uart, char *buffer, size_t buffer_size) {

EF_DRIVER_STATUS status = EF_DRIVER_OK;

if (uart == NULL) {
status = EF_DRIVER_ERROR_PARAMETER; // Return EF_DRIVER_ERROR_PARAMETER if uart is NULL
} else if (buffer == NULL) {
status = EF_DRIVER_ERROR_PARAMETER; // Return EF_DRIVER_ERROR_PARAMETER if buffer is NULL
} else if (buffer_size == 0) {
status = EF_DRIVER_ERROR_PARAMETER; // Return EF_DRIVER_ERROR_PARAMETER if buffer_size is 0
}else{
size_t index = 0;
while (index < buffer_size - 1) {
bool data_available = false;
status = EF_UART_charsAvailable(uart, &data_available);
if (status != EF_DRIVER_OK){break;} // return on error
if (!data_available) {continue;} // skip this iteration and wait for data

char received_char;
status = EF_UART_readChar(uart, &received_char);
if (status != EF_DRIVER_OK){break;} // return on error

buffer[index++] = received_char;
if (received_char == '\n') break; // Stop reading at newline
}
buffer[index] = '\0'; // Null-terminate the string
}

return EF_DRIVER_OK;
}



/******************************************************************************
* Static Function Definitions
******************************************************************************/
Expand Down
31 changes: 31 additions & 0 deletions fw/EF_UART.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,37 @@ EF_DRIVER_STATUS EF_UART_getParityMode(EF_UART_TYPE_PTR uart, uint32_t* parity_m
EF_DRIVER_STATUS EF_UART_busy(EF_UART_TYPE_PTR uart, bool* flag);


//! This function initializes the UART with the specified parameters
/*!
\param [in] uart An \ref EF_UART_TYPE_PTR , which points to the base memory address of UART registers. \ref EF_UART_TYPE is a structure that contains the UART registers.
\param [in] baud_rate The baud rate of the UART
\param [in] bus_clock The bus clock frequency
\param [in] data_bits The number of data bits
\param [in] two_stop_bits A flag indicating if two stop bits are used
\param [in] parity The parity mode
\param [in] timeout The receiver timeout
\param [in] rx_threshold The receive FIFO threshold
\param [in] tx_threshold The transmit FIFO threshold
\return status A value of type \ref EF_DRIVER_STATUS : returns a success or error code
*/
EF_DRIVER_STATUS UART_Init(EF_UART_TYPE_PTR uart, uint32_t baud_rate, uint32_t bus_clock, uint32_t data_bits, bool two_stop_bits, enum parity_type parity, uint32_t timeout, uint32_t rx_threshold, uint32_t tx_threshold);


//! This function receives a string message from the UART. The message is stored in a buffer with a specified size.
/// \note This is a blocking function and can only terminate under the following conditions:
/// 1. The buffer is full
/// 2. A "\n" character is received
/// 3. An error is detected
/*!
\param [in] uart An \ref EF_UART_TYPE_PTR , which points to the base memory address of UART registers. \ref EF_UART_TYPE is a structure that contains the UART registers.
\param [out] buffer The buffer to store the received message
\param [in] buffer_size The size of the buffer
\return status A value of type \ref EF_DRIVER_STATUS : returns a success or error code
*/
EF_DRIVER_STATUS EF_UART_readCharArr(EF_UART_TYPE_PTR uart, char *buffer, size_t buffer_size);


/******************************************************************************
* External Variables
Expand Down
73 changes: 0 additions & 73 deletions fw/example.c
Original file line number Diff line number Diff line change
@@ -1,78 +1,5 @@
#include "EF_UART.h"

// Function to initialize and configure the UART
EF_DRIVER_STATUS UART_Init(EF_UART_TYPE_PTR uart, uint32_t baud_rate, uint32_t bus_clock, uint32_t data_bits, bool two_stop_bits, enum parity_type parity, uint32_t timeout, uint32_t rx_threshold, uint32_t tx_threshold) {
EF_DRIVER_STATUS status;

// Calculate and set the prescaler
uint32_t prescaler = (bus_clock / (baud_rate * 16)) - 1;
status = EF_UART_setPrescaler(uart, prescaler);
if (status != EF_DRIVER_OK) return status;

// Configure data bits, stop bits, and parity

// Set data bits (5-9 bits)
status = EF_UART_setDataSize(uart, data_bits);
if (status != EF_DRIVER_OK) return status;

// Set stop bits (1 or 2)
status = EF_UART_setTwoStopBitsSelect(uart, two_stop_bits);
if (status != EF_DRIVER_OK) return status;

// Set parity type
status = EF_UART_setParityType(uart, parity);
if (status != EF_DRIVER_OK) return status;

// Set the receiver timeout value
status = EF_UART_setTimeoutBits(uart, timeout);
if (status != EF_DRIVER_OK) return status;

// Set RX and TX FIFO thresholds
status = EF_UART_setRxFIFOThreshold(uart, rx_threshold);
if (status != EF_DRIVER_OK) return status;
status = EF_UART_setTxFIFOThreshold(uart, tx_threshold);
if (status != EF_DRIVER_OK) return status;

// Enable the UART and both RX and TX
status = EF_UART_enable(uart);
if (status != EF_DRIVER_OK) return status;
status = EF_UART_enableRx(uart);
if (status != EF_DRIVER_OK) return status;
status = EF_UART_enableTx(uart);
if (status != EF_DRIVER_OK) return status;

// Optionally enable glitch filter and loopback for testing
status = EF_UART_enableGlitchFilter(uart);
if (status != EF_DRIVER_OK) return status;
status = EF_UART_enableLoopBack(uart);
if (status != EF_DRIVER_OK) return status;

return EF_DRIVER_OK;
}

// Function to receive a string using UART
EF_DRIVER_STATUS UART_Receive(EF_UART_TYPE_PTR uart, char *buffer, size_t buffer_size) {
EF_DRIVER_STATUS status;
size_t index = 0;

while (index < buffer_size - 1) {
bool data_available = false;
status = EF_UART_charsAvailable(uart, &data_available);
if (status != EF_DRIVER_OK){break;} // return on error
if (!data_available) {continue;} // skip this iteration and wait for data

char received_char;
status = EF_UART_readChar(uart, &received_char);
if (status != EF_DRIVER_OK){break;} // return on error

buffer[index++] = received_char;
if (received_char == '\n') break; // Stop reading at newline
}
buffer[index] = '\0'; // Null-terminate the string
return EF_DRIVER_OK;
}


#define Example_UART_BASE_ADDRESS 0x40000000
#define UART0 ((EF_UART_TYPE_PTR)Example_UART_BASE_ADDRESS)

Expand Down

0 comments on commit ffaf9b9

Please sign in to comment.