Commit bd359ed2 authored by Reto Da Forno's avatar Reto Da Forno
Browse files

uart_init() removed

parent c4b66b95
......@@ -42,12 +42,12 @@ void gpio_check_baseboard(void)
uint32_t prev_pull = BASEBOARD_ENABLE_GPIO_Port->PUPDR;
/* to check whether the comboard is indeed not on a baseboard: read state of enable pin (has external pullup) */
GPIO_InitStruct.Pin = BASEBOARD_ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN; // first init with pull to remove potential charge on the pin
GPIO_InitStruct.Pin = BASEBOARD_ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN; // first init with pull to remove potential charge on the pin
HAL_GPIO_Init(BASEBOARD_ENABLE_GPIO_Port, &GPIO_InitStruct);
delay_us(100);
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(BASEBOARD_ENABLE_GPIO_Port, &GPIO_InitStruct);
/* read pin value */
uint32_t c = 10;
......@@ -68,8 +68,8 @@ void gpio_check_baseboard(void)
}
/* restore previous pin config */
BASEBOARD_ENABLE_GPIO_Port->PUPDR = prev_pull;
BASEBOARD_ENABLE_GPIO_Port->MODER = prev_mode;
BASEBOARD_ENABLE_GPIO_Port->PUPDR = prev_pull;
BASEBOARD_ENABLE_GPIO_Port->MODER = prev_mode;
/* make sure no interrupt is triggered by reconfiguring the pin */
__HAL_GPIO_EXTI_CLEAR_IT(BASEBOARD_ENABLE_Pin);
......
......@@ -60,7 +60,7 @@ void system_init(void)
HAL_PWREx_EnablePullUpPullDownConfig();
/* init peripherals / drivers */
uart_init();
uart_init_rx();
#if BOLT_ENABLE
bolt_init();
#endif /* BOLT_ENABLE */
......
......@@ -49,19 +49,12 @@ static uart_fifo_t tx_fifo = {0};
static uart_fifo_t rx_fifo = {0};
#endif /* UART_RX_ENABLE */
volatile static bool uart_initialized = false;
void uart_tx_fifo_send();
void uart_init()
void uart_init_rx()
{
#if UART_RX_ENABLE
HAL_UART_Receive_DMA(&UART, (uint8_t*) rx_fifo.buffer, (uint16_t) UART_FIFO_BUFFER_SIZE);
#endif /* UART_RX_ENABLE */
uart_initialized = true;
}
......@@ -69,21 +62,37 @@ void uart_init()
uart_fifo_t* uart_rx()
{
if (uart_initialized)
{
rx_fifo.set_pointer = UART_FIFO_BUFFER_SIZE - (uint16_t) __HAL_DMA_GET_COUNTER(&DMA_UART_RX);
rx_fifo.item_count = ((unsigned) rx_fifo.set_pointer - (unsigned) rx_fifo.get_pointer) % (unsigned) UART_FIFO_BUFFER_SIZE;
}
rx_fifo.set_pointer = UART_FIFO_BUFFER_SIZE - (uint16_t) __HAL_DMA_GET_COUNTER(&DMA_UART_RX);
rx_fifo.item_count = ((unsigned) rx_fifo.set_pointer - (unsigned) rx_fifo.get_pointer) % (unsigned) UART_FIFO_BUFFER_SIZE;
return &rx_fifo;
}
#endif /* UART_RX_ENABLE */
void uart_tx_fifo_send(void)
{
if (tx_fifo.item_count && !tx_fifo.dma_transfer_count) {
uint16_t processable_size;
uint32_t next_item = tx_fifo.get_pointer;
if ((next_item + tx_fifo.item_count) >= UART_FIFO_BUFFER_SIZE) {
processable_size = UART_FIFO_BUFFER_SIZE - next_item;
} else {
processable_size = tx_fifo.item_count;
}
if (HAL_OK == HAL_UART_Transmit_DMA(&UART, (uint8_t*) (tx_fifo.buffer + next_item), processable_size)) {
tx_fifo.dma_transfer_count = processable_size;
}
}
}
bool uart_tx(char* buffer, uint32_t size)
{
uint64_t aborttime = lptimer_now() + LPTIMER_MS_TO_TICKS(UART_TX_TIMEOUT_MS);
if (uart_initialized && size) {
if (size) {
while (size && (lptimer_now() < aborttime)) {
uint16_t processable_size = (UART_FIFO_BUFFER_SIZE - tx_fifo.item_count);
if (processable_size > size) {
......@@ -117,24 +126,6 @@ bool uart_tx_direct(char* buffer, uint32_t size)
}
void uart_tx_fifo_send(void)
{
if (tx_fifo.item_count && !tx_fifo.dma_transfer_count) {
uint16_t processable_size;
uint32_t next_item = tx_fifo.get_pointer;
if ((next_item + tx_fifo.item_count) >= UART_FIFO_BUFFER_SIZE) {
processable_size = UART_FIFO_BUFFER_SIZE - next_item;
} else {
processable_size = tx_fifo.item_count;
}
if (HAL_OK == HAL_UART_Transmit_DMA(&UART, (uint8_t*) (tx_fifo.buffer + next_item), processable_size)) {
tx_fifo.dma_transfer_count = processable_size;
}
}
}
bool uart_tx_fifo_empty(void)
{
return (tx_fifo.item_count == 0);
......@@ -145,16 +136,14 @@ bool uart_tx_fifo_empty(void)
bool uart_read(char* chr)
{
if(uart_initialized) {
uart_rx();
if (rx_fifo.item_count > 0) {
*chr = rx_fifo.buffer[rx_fifo.get_pointer];
rx_fifo.get_pointer = ((unsigned) rx_fifo.get_pointer + 1U) % (unsigned) UART_FIFO_BUFFER_SIZE;
rx_fifo.item_count--;
return true;
} else {
return false;
}
uart_rx();
if (rx_fifo.item_count > 0) {
*chr = rx_fifo.buffer[rx_fifo.get_pointer];
rx_fifo.get_pointer = ((unsigned) rx_fifo.get_pointer + 1U) % (unsigned) UART_FIFO_BUFFER_SIZE;
rx_fifo.item_count--;
return true;
} else {
return false;
}
return false;
}
......@@ -164,7 +153,7 @@ bool uart_read(char* chr)
void uart_wait_tx_complete(uint32_t timeout_ms)
{
if (uart_initialized && tx_fifo.item_count) {
if (tx_fifo.item_count) {
uart_tx_fifo_send(); // make sure a DMA transfer is active
uint64_t aborttime = lptimer_now() + LPTIMER_MS_TO_TICKS(timeout_ms);
while (tx_fifo.item_count && (lptimer_now() < aborttime));
......@@ -174,11 +163,9 @@ void uart_wait_tx_complete(uint32_t timeout_ms)
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (uart_initialized) {
tx_fifo.get_pointer = (tx_fifo.get_pointer + tx_fifo.dma_transfer_count) % UART_FIFO_BUFFER_SIZE;
tx_fifo.item_count -= tx_fifo.dma_transfer_count;
tx_fifo.dma_transfer_count = 0;
uart_tx_fifo_send();
}
tx_fifo.get_pointer = (tx_fifo.get_pointer + tx_fifo.dma_transfer_count) % UART_FIFO_BUFFER_SIZE;
tx_fifo.item_count -= tx_fifo.dma_transfer_count;
tx_fifo.dma_transfer_count = 0;
uart_tx_fifo_send();
}
......@@ -55,7 +55,7 @@ typedef struct
} uart_fifo_t;
void uart_init();
void uart_init_rx();
uart_fifo_t* uart_rx(void);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment