Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usart driver #64

Merged
merged 13 commits into from
Dec 1, 2023
3 changes: 2 additions & 1 deletion Q24ECU/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"${workspaceFolder}/core/include",
"${workspaceFolder}/FreeRTOS-Kernel/include",
"${workspaceFolder}/FreeRTOS-Kernel/portable/GCC/ARM_CM4F",
"${workspaceFolder}/core/config"
"${workspaceFolder}/core/config",
"${workspaceFolder}/core/include/hal"
],
"defines": [
"_DEBUG",
Expand Down
22 changes: 22 additions & 0 deletions Q24ECU/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "ST-Flash",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/Tools/ST-LINK_CLI.exe",
"args": [
"-P",
"${workspaceFolder}/build/firmware.bin",
"0x08000000",
"-V",
"-RST"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "integratedTerminal"
}
]
}
9 changes: 8 additions & 1 deletion Q24ECU/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
"hal_gpio.h": "c",
"hal_uart.h": "c",
"hal_clock.h": "c",
"float.h": "c"
"float.h": "c",
"queue.h": "c",
"taskhandlers.h": "c",
"freertos.h": "c",
"limits.h": "c",
"main.h": "c",
"interface_uart.h": "c",
"task.h": "c"
}
}
25 changes: 25 additions & 0 deletions Q24ECU/core/config/nvicConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file nvicConfig.h
* @author Jacob Chisholm (Jchisholm204.github.io)
* @brief
* @version 0.1
* @date 2023-11-20
*
* @copyright Copyright (c) 2023
*
* This file exists because of some odd errors surrounding FreeRTOS and the NVIC.
*
* In order to avoid deadlocks within an interrupt: Do NOT exceed these parameters.
* EX:
* NVIC_SetPriority(USART2_IRQn, NVIC_Priority_MAX);
* NVIC_SetPriority(USART2_IRQn, NVIC_Priority_MAX+5);
* NVIC_SetPriority(USART2_IRQn, NVIC_Priority_MIN-10);
*
*/

#pragma once

// Highest Priority that can be given to an NVIC Interrupt
#define NVIC_Priority_MAX 21
// Lowest Priority that can be given to an NVIC Interrupt
#define NVIC_Priority_MIN 79
8 changes: 4 additions & 4 deletions Q24ECU/core/include/hal/hal_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void hal_FLASH_Lock(void){
};

// Unlock the flash memory
static uint8_t hal_FLASH_Unlock(void){
static inline uint8_t hal_FLASH_Unlock(void){
// Check if already unlocked
if(((FLASH->CR) & (FLASH_CR_LOCK)) != 0) {

Expand All @@ -61,7 +61,7 @@ static uint8_t hal_FLASH_Unlock(void){
return FLASH_WRITE_NOACC;
}
}
return NULL;
return FLASH_WRITE_OK;
}

/**
Expand All @@ -72,7 +72,7 @@ static uint8_t hal_FLASH_Unlock(void){
* @param data2 second half of word
* @returns Flash Write Error
*/
static uint8_t hal_FLASH_WriteFW(uint32_t Address, uint32_t data1, uint32_t data2){
static inline uint8_t hal_FLASH_WriteFW(uint32_t Address, uint32_t data1, uint32_t data2){
// Check if write address is valid
if(Address < FLASH_USER_START){
return FLASH_WRITE_ADDR_INVALID;
Expand Down Expand Up @@ -110,7 +110,7 @@ static uint8_t hal_FLASH_WriteFW(uint32_t Address, uint32_t data1, uint32_t data
* @param data The Data
* @returns Flash Write Error
*/
static uint8_t hal_FLASH_WriteHW(uint32_t Address, uint32_t data){
static inline uint8_t hal_FLASH_WriteHW(uint32_t Address, uint32_t data){
// Check if write address is valid
if(Address < FLASH_USER_START){
return FLASH_WRITE_ADDR_INVALID;
Expand Down
16 changes: 8 additions & 8 deletions Q24ECU/core/include/hal/hal_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <stm32f446xx.h>
#include "hal_gpio.h"

static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
static inline void hal_uart_init(USART_TypeDef *uart, unsigned long baud) {
// figure 19. selecting an alternate function (7=spi2/3, usart1..3, uart5, spdif-in)
uint8_t af = 7; // Alternate function
uint16_t rx = 0, tx = 0; // pins
Expand All @@ -41,7 +41,7 @@ static inline void uart_init(USART_TypeDef *uart, unsigned long baud) {
uart->CR1 |= BIT(13) | BIT(2) | BIT(3); // Set UE, RE, TE
}

static inline void uart_enable_rxne(USART_TypeDef *uart, bool enable){
static inline void hal_uart_enable_rxne(USART_TypeDef *uart, bool enable){
if(enable){
uart->CR1 |= USART_CR1_RXNEIE;
}
Expand All @@ -50,7 +50,7 @@ static inline void uart_enable_rxne(USART_TypeDef *uart, bool enable){
};
}

static inline void uart_enable_txne(USART_TypeDef *uart, bool enable){
static inline void hal_uart_enable_txne(USART_TypeDef *uart, bool enable){
if(enable){
uart->CR1 |= USART_CR1_TXEIE;
}
Expand All @@ -59,24 +59,24 @@ static inline void uart_enable_txne(USART_TypeDef *uart, bool enable){
};
}

static inline int uart_read_ready(const USART_TypeDef *uart){
static inline int hal_uart_read_ready(const USART_TypeDef *uart){
return uart->SR & USART_SR_RXNE;
}

static inline uint8_t uart_read_byte(const USART_TypeDef *uart) {
static inline uint8_t hal_uart_read_byte(const USART_TypeDef *uart) {
return ((uint8_t) (uart->DR & 255));
}

static inline void spin(volatile uint32_t count) {
while (count--) asm("nop");
}

static inline void uart_write_byte(USART_TypeDef * uart, uint8_t byte) {
static inline void hal_uart_write_byte(USART_TypeDef * uart, uint8_t byte) {
uart->DR = byte;
while ((uart->SR & USART_SR_TXE) == 0) spin(1);

}

static inline void uart_write_buf(USART_TypeDef *uart, char *buf, size_t len){
while(len-- > 0) uart_write_byte(uart, *(uint8_t *) buf++);
static inline void hal_uart_write_buf(USART_TypeDef *uart, char *buf, size_t len){
while(len-- > 0) hal_uart_write_byte(uart, *(uint8_t *) buf++);
}
109 changes: 109 additions & 0 deletions Q24ECU/core/include/interfaces/interface_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @file interface_uart.h
* @author Jacob Chisholm (Jchisholm204.github.io)
* @brief uart FreeRTOS Interface with Semaphore Handler
* @version 0.1
* @date 2023-11-15
*
* @copyright Copyright (c) 2023
*
*/
#include "hal/hal_uart.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "stream_buffer.h"

#define UART_WRITE_OK 0
#define UART_ERR_UNDEF 1
#define UART_ERR_ACC 2
Comment on lines +16 to +18
Copy link
Contributor

@JoshWAllen JoshWAllen Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could be replaced with an enum - not mandatory, more of a style preference. The biggest functional difference is scoping



/**
* @brief UART port typedef
* @param port The UART port (CMSIS UART typedef)
* @param semaphore The semaphore controlling the UART port
* @param rxbuffer The receive buffer loaded by the UART IRQ Handler
*
*/
typedef struct uart_t {
USART_TypeDef *port;
xSemaphoreHandle semaphore;
StreamBufferHandle_t rxbuffer;
} uart_t;

// UART OS Handlers
extern uart_t port_uart2;

/**
* @brief OS UART Setup handler
*
* This function should be called to initalized all onboard UART interfaces.
* This function should not be called more than once.
* All UART initialization should happen within this function
*
* Called in main
*
*/
extern void os_uart_setup();


/**
* @brief Initialize a USART device and its Semaphore
*
* @param port The USART_t typedef handle
* @param uart the USARTx define from CMSIS headers
* @param baud USART Baud Rate
*/
static void uart_send_init(uart_t *port, USART_TypeDef *uart, unsigned long baud){
port->semaphore = xSemaphoreCreateBinary();
if(port->semaphore == NULL) return;
port->rxbuffer = xStreamBufferCreate(64, 1);
if(port->rxbuffer == NULL) return;
port->port = uart;
if(port->port == NULL) return;
hal_uart_init(port->port, baud);
xSemaphoreGive(port->port);
}


// USART2 IQR Handler
extern void USART2_IRQHandler();

/**
* @brief Task Blocking command to send a byte over uart
*
* @param port The uart_t port to use (Must be initialized)
* @param byte The Byte to transmit
* @param timeout The amount of ticks to wait for the interface to become available
*/
static inline int uart_send_blocking(uart_t *port, uint8_t byte, TickType_t timeout){
if(port == NULL) return UART_ERR_UNDEF;
if(port->port == NULL) return UART_ERR_UNDEF;
if(port->semaphore == NULL) return UART_ERR_UNDEF;
if(xSemaphoreTake(port->semaphore, timeout) == pdTRUE){
hal_uart_write_byte(port->port, byte);
xSemaphoreGive(port->semaphore);
return UART_WRITE_OK;
}
return UART_ERR_ACC;
}

/**
* @brief Task Blocking command to send a buffer over uart
*
* @param port The uart_t port to use (Must be initialized)
* @param buf Data Buffer
* @param len Length of Data Buffer
* @param timeout The amount of ticks to wait for the interface to become available
*/
static inline int uart_send_buf_blocking(uart_t *port, char* buf, size_t len, TickType_t timeout){
if(port == NULL) return UART_ERR_UNDEF;
if(port->port == NULL) return UART_ERR_UNDEF;
if(port->semaphore == NULL) return UART_ERR_UNDEF;
if(xSemaphoreTake(port->semaphore, timeout) == pdTRUE){
hal_uart_write_buf(port->port, buf, len);
xSemaphoreGive(port->semaphore);
return UART_WRITE_OK;
}
return UART_ERR_ACC;
}
16 changes: 16 additions & 0 deletions Q24ECU/core/include/interrupts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file interrupts.h
* @author Jacob Chisholm (https://jchisholm204.github.io/)
* @brief Global definitions of all interrupt handler functions
* @version 0.1
* @date 2023-11-22
*
* @copyright Copyright (c) 2023
*
* This file MUST be part of the main.h includes list
*
*/

#pragma once

extern void TIM6_DAC_IRQHandler();
5 changes: 5 additions & 0 deletions Q24ECU/core/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
*
*/

#include "interfaces/interface_uart.h"
#include "hal/hal_gpio.h"
#include "hal/hal_adc.h"
#include "hal/hal_uart.h"
#include "hal/hal_tim_basic.h"
#include "hal/hal_flash.h"
#include "taskHandlers.h"
#include "nvicConfig.h"
#include "interrupts.h"

extern void SystemInit(void);

Expand Down
27 changes: 27 additions & 0 deletions Q24ECU/core/include/taskHandlers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file taskHandlers.h
* @author Jacob Chisholm
* @brief Global definitions of all task handlers defined in main.c
* @version 0.1
* @date 2023-11-19
*
* @copyright Copyright (c) 2023
*
*/

#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "task.h"

// Test Task 1
extern TaskHandle_t tskh_Test1;
// Blink LED example Task
extern TaskHandle_t tskh_BlinkLED;
// UART2 (debug) receive task handler
extern TaskHandle_t tskh_USART2_Handler;

extern void tsk_Test1(void *param);

extern void tsk_BlinkLED(void *param);

extern void tsk_USART2_Handler(void *param);
Loading