Skip to content

Commit

Permalink
print queue v0
Browse files Browse the repository at this point in the history
Print queue completed in theory

builds now hehe

Test

Saturday Changes :)

fix sim issue

build fix

some changes

updated mutex buffer

inshallah it wokrs

work in progress

work in progress

Changed printFifo. Needs >256 char string testing

the message lenght was wrong...

pushin

few logic fixes

comments

making tests

fixed retarget

tests

build

reverted append

fixed retarget again

changed copyright for 2023

applied changes from suggestions

/r

one bug fix

fixed smile?

now it compiles!! *surprised pikachu face

got rid of fifo changes - want a separate PR

comment fix

build

here you go nathan

nathan big brain fix

RENAMED STUFF
  • Loading branch information
MichaelMohn authored and tiandahuang committed Apr 1, 2024
1 parent f53a42a commit 8b6f530
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 9 deletions.
50 changes: 50 additions & 0 deletions Apps/Inc/Print_Queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
/** Print_Queue.c
* Queue that holds all characters to be printed that Task_Print needs to send to UART.
*/

#ifndef PRINT_QUEUE_H
#define PRINT_QUEUE_H

#include <stdbool.h>
#include <stdint.h>
#include "stddef.h"


/**
* @brief Initializes the print queue
* @param none
* @return none
*/
void Print_Queue_Init(void);

/**
* @brief Initializes the print queue
* @param buffer String of formatted text to be added to the buffer
* @return none
*/
bool Print_Queue_Append(char *buffer);

/**
* @brief Blocks until the queue is ready to be dumped
* @param message String to be dumped from the buffer
* @param len Length of string to be dumped
* @return none
*/
void Print_Queue_Pend(char *buffer, uint32_t *len);

/**
* @brief Performs a "non-blocking" printf by dumping into a buffer for Task_Print.c
* @param string String of formatted text to be added to the buffer
* @return none
*/
void RTOS_BPS_NonBlocking_Printf(const char *format, ...);

/**
* @brief Performs a "blocking" printf by attempting to dump into the buffer until success
* @param string String of formatted text to be added to the buffer
* @return none
*/
void RTOS_BPS_Blocking_Printf(const char *format, ...);

#endif
1 change: 1 addition & 0 deletions Apps/Inc/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#ifndef __FIFO_H
#define __FIFO_H
#include <stdbool.h>
#include <string.h>
#endif

// The type of the fifo
Expand Down
139 changes: 139 additions & 0 deletions Apps/Src/Print_Queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
/** Print_Queue.c
* Queue that holds all characters to be printed that Task_Print needs to send to UART.
*/

#include "Print_Queue.h"
#include "RTOS_BPS.h"
#include "stdio.h"
#include <stdarg.h>
#include "BSP_UART.h"

uint32_t size = 1024;

#define FIFO_TYPE char
#define FIFO_SIZE 1024
#define FIFO_NAME Print_Fifo
#include "fifo.h"

static Print_Fifo_t printFifo;
static OS_MUTEX printFifo_ready;
static OS_MUTEX printCall_Mutex;

uint32_t fifo_space();


/**
* @brief Initializes the print queue
* @param none
* @return none
*/
void Print_Queue_Init() {
Print_Fifo_renew(&printFifo);
RTOS_BPS_MutexCreate(&printFifo_ready, "readyPrint");
RTOS_BPS_MutexCreate(&printCall_Mutex, "printCall mutex");
}

/**
* @brief Initializes the print queue
* @return Returns if the string was added into the buffer
* @param buffer String of formatted text to be added to the buffer
* @return none
*/
bool Print_Queue_Append(char *buffer) {
//Check if theres room
if(strlen(buffer) > fifo_space()){
RTOS_BPS_MutexPost(&printFifo_ready, OS_OPT_POST_1);
return false;
}

while(*buffer != '\0') {

//Add characters to the fifo one by one
if(!(*buffer == '\r' || *buffer == '\n')){
Print_Fifo_put(&printFifo, *buffer);
buffer++;
}else{
//Endline check
while((*buffer == '\r' || *buffer == '\n')){
Print_Fifo_put(&printFifo, *buffer);
buffer++;
}

RTOS_BPS_MutexPost(&printFifo_ready, OS_OPT_POST_1);
}


}
return true;
}


/**
* @brief Blocks until the queue is ready to be dumped
* @param message String to be dumped from the buffer
* @param len Length of string to be dumped
* @return none
*/
void Print_Queue_Pend(char *message, uint32_t *len) {
uint32_t max_Size = 1024;
(*len) = 0;
RTOS_BPS_MutexPend(&printFifo_ready, OS_OPT_PEND_BLOCKING);
while(!Print_Fifo_is_empty(&printFifo)){
Print_Fifo_get(&printFifo, message);
(*len)++;
message++;
if(*len >= max_Size){
return;
}
}
}

/**
* @brief Performs a "non-blocking" printf by dumping into a buffer for Task_Print.c
* @param string String of formatted text to be added to the buffer
* @return none
*/
void RTOS_BPS_NonBlocking_Printf(const char *format, ...){

RTOS_BPS_MutexPend(&printCall_Mutex, OS_OPT_PEND_BLOCKING);
va_list args;
va_start(args, format);
char buffer[size];

int printLen = strlen(format);
vsnprintf(buffer, printLen, format, args);
Print_Queue_Append(buffer);

va_end(args);
RTOS_BPS_MutexPost(&printCall_Mutex, OS_OPT_POST_1);
}

/**
* @brief Performs a "blocking" printf by dumping into a buffer for Task_Print.c until success
* @param string String of formatted text to be added to the buffer
* @return none
*/
void RTOS_BPS_Blocking_Printf(const char *format, ...){

RTOS_BPS_MutexPend(&printCall_Mutex, OS_OPT_PEND_BLOCKING);
va_list args;
va_start(args, format);
char buffer[size];

int printLen = strlen(format);
vsnprintf(buffer, printLen, format, args);
while(!Print_Queue_Append(buffer));

va_end(args);
RTOS_BPS_MutexPost(&printCall_Mutex, OS_OPT_POST_1);
}

//Function will get removed when merged with my FIFO PR
uint32_t fifo_space(){
if(!Print_Fifo_is_empty(&printFifo)){
return ((printFifo.get - printFifo.put) + size) % size;
}

return size;
}
11 changes: 6 additions & 5 deletions BSP/STM32F413/Src/retarget.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */

#include "common.h"
#include "sys/stat.h"
#include "BSP_UART.h"
#include "Print_Queue.h"

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2

int _write(int fd, char *buffer, unsigned int len) {
if(buffer != NULL) {
BSP_UART_Write(buffer, len, UART_USB);
}
//Add stuff to thread-safe OS level queue instead of BSP callsS

Print_Queue_Append(buffer);

return len;
}

Expand Down
10 changes: 8 additions & 2 deletions Tasks/Inc/Tasks.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
#ifndef TASKS_H
#define TASKS_H

Expand All @@ -14,7 +14,8 @@
#define TASK_BATTERY_BALANCE_PRIO 7
#define TASK_LOG_INFO_PRIO 8
#define TASK_CLI_PRIO 9
#define TASK_IDLE_PRIO 10
#define TASK_PRINT_PRIO 10
#define TASK_IDLE_PRIO 11

#define DEFAULT_STACK_SIZE 256
#define WATERMARK_STACK_LIMIT DEFAULT_STACK_SIZE/2
Expand All @@ -29,6 +30,7 @@
#define TASK_BATTERY_BALANCE_STACK_SIZE 512
#define TASK_CLI_STACK_SIZE DEFAULT_STACK_SIZE
#define TASK_BLE_STACK_SIZE DEFAULT_STACK_SIZE
#define TASK_PRINT_STACK_SIZE DEFAULT_STACK_SIZE
#define TASK_IDLE_STACK_SIZE DEFAULT_STACK_SIZE
#define TASK_INIT_STACK_SIZE DEFAULT_STACK_SIZE

Expand Down Expand Up @@ -64,6 +66,8 @@ void Task_CLI(void *p_arg);

void Task_BLE(void *p_arg);

void Task_Print(void *p_arg);

void Task_Idle(void *p_arg);

void Task_Init(void *p_arg);
Expand Down Expand Up @@ -97,6 +101,7 @@ extern OS_TCB CheckContactor_TCB;
extern OS_TCB BatteryBalance_TCB;
extern OS_TCB CLI_TCB;
extern OS_TCB BLE_TCB;
extern OS_TCB Print_TCB;
extern OS_TCB Idle_TCB;
extern OS_TCB Init_TCB;
/**
Expand All @@ -112,6 +117,7 @@ extern CPU_STK CheckContactor_Stk[TASK_CHECK_CONTACTOR_STACK_SIZE];
extern CPU_STK BatteryBalance_Stk[TASK_BATTERY_BALANCE_STACK_SIZE];
extern CPU_STK CLI_Stk[TASK_CLI_STACK_SIZE];
extern CPU_STK BLE_Stk[TASK_BLE_STACK_SIZE];
extern CPU_STK Print_Stk[TASK_PRINT_STACK_SIZE];
extern CPU_STK Idle_Stk[TASK_IDLE_STACK_SIZE];
extern CPU_STK Init_Stk[TASK_INIT_STACK_SIZE];

Expand Down
12 changes: 11 additions & 1 deletion Tasks/Src/Task_Init.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
#include "config.h"
#include "Tasks.h"
#include "CAN_Queue.h"
#include "Contactor.h"
#include "RTOS_BPS.h"
#include "Print_Queue.h"
#ifndef SIMULATION
#include "stm32f4xx.h"
#endif
Expand Down Expand Up @@ -94,6 +95,14 @@ void Task_Init(void *p_arg) {
TASK_CLI_STACK_SIZE, // Stack size
);
*/
RTOS_BPS_TaskCreate(&Print_TCB,
"TASK_PRINT_QUEUE_OUT",
Task_Print,
(void *) 0,
TASK_PRINT_PRIO,
Print_Stk,
TASK_PRINT_STACK_SIZE);

RTOS_BPS_TaskCreate(&Idle_TCB, // TCB
"TASK_IDLE", // Task Name (String)
Task_Idle, // Task function pointer
Expand All @@ -103,6 +112,7 @@ void Task_Init(void *p_arg) {
TASK_IDLE_STACK_SIZE);

CAN_Queue_Init();
Print_Queue_Init();

//delete task
OSTaskDel(NULL, &err); // Delete task
Expand Down
23 changes: 23 additions & 0 deletions Tasks/Src/Task_Print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
#include "Tasks.h"
#include "Print_Queue.h"
#include "BSP_UART.h"
//#include "CANbus.h"
//#include "CAN_Queue.h"


void Task_Print(void *p_arg) {
(void)p_arg;

char message[1024] = {0};
uint32_t len = 0;

while(1) {
// BLOCKING =====================
// Wait for Print Queue to Dump
Print_Queue_Pend(message, &len);

//Port 2 for now
BSP_UART_Write(message, len, UART_USB);
}
}
4 changes: 3 additions & 1 deletion Tasks/Src/Tasks.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2018-2022 UT Longhorn Racing Solar */
/* Copyright (c) 2018-2023 UT Longhorn Racing Solar */
#include "common.h"
#include "config.h"
#include "Tasks.h"
Expand All @@ -23,6 +23,7 @@ BPS_OS_TCB BatteryBalance_TCB;
BPS_OS_TCB CheckContactor_TCB;
BPS_OS_TCB CLI_TCB;
BPS_OS_TCB BLE_TCB;
BPS_OS_TCB Print_TCB;
BPS_OS_TCB Idle_TCB;
BPS_OS_TCB Init_TCB;

Expand All @@ -39,6 +40,7 @@ BPS_CPU_STK BatteryBalance_Stk[TASK_BATTERY_BALANCE_STACK_SIZE];
BPS_CPU_STK CheckContactor_Stk[TASK_CHECK_CONTACTOR_STACK_SIZE];
BPS_CPU_STK CLI_Stk[TASK_CLI_STACK_SIZE];
BPS_CPU_STK BLE_Stk[TASK_BLE_STACK_SIZE];
BPS_CPU_STK Print_Stk[TASK_PRINT_STACK_SIZE];
BPS_CPU_STK Idle_Stk[TASK_IDLE_STACK_SIZE];
BPS_CPU_STK Init_Stk[TASK_INIT_STACK_SIZE];

Expand Down
Loading

0 comments on commit 8b6f530

Please sign in to comment.