Skip to content

Commit

Permalink
works
Browse files Browse the repository at this point in the history
issue with nvic priorities
  • Loading branch information
Jchisholm204 committed Nov 21, 2023
1 parent a3e12cd commit d3127ed
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
6 changes: 5 additions & 1 deletion Q24ECU/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"float.h": "c",
"queue.h": "c",
"taskhandlers.h": "c",
"freertos.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
// Highest Priority that can be given to an NVIC Interrupt
#define NVIC_Priority_MIN 79
1 change: 1 addition & 0 deletions Q24ECU/core/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "hal/hal_tim_basic.h"
#include "hal/hal_flash.h"
#include "taskHandlers.h"
#include "nvicConfig.h"

extern void SystemInit(void);

Expand Down
3 changes: 3 additions & 0 deletions Q24ECU/core/include/taskHandlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#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;
13 changes: 4 additions & 9 deletions Q24ECU/core/src/interfaces/interface_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "FreeRTOS.h"
#include "task.h"
#include "taskHandlers.h"
#include "nvicConfig.h"


uart_t debug;
Expand All @@ -21,22 +22,16 @@ void uart_setup(){
// Enable the debug usart and setup its IQR handler
uart_send_init(&debug, UART_DEBUG, 250000);
hal_uart_enable_rxne(debug.interface, true);
NVIC_SetPriority(USART2_IRQn, 0x03);
NVIC_SetPriority(USART2_IRQn, (NVIC_Priority_MIN-10));
NVIC_EnableIRQ(USART2_IRQn);
}

void USART2_IRQHandler(){
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
uint8_t receivedData = 0;
// Clear the interrupt bit (enables exiting of fn)
// xSemaphoreTakeFromISR(debug.handle, &xHigherPriorityTaskWoken);
receivedData = hal_uart_read_byte(UART_DEBUG);
// xSemaphoreGiveFromISR(debug.handle, &xHigherPriorityTaskWoken);

xStreamBufferSendFromISR(debug.stream, &receivedData, sizeof(receivedData), NULL);
xStreamBufferSendFromISR(debug.stream, &receivedData, sizeof(receivedData), &xHigherPriorityTaskWoken);

// portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
if(receivedData == '1'){
gpio_toggle_pin(debug_led2);
}
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
46 changes: 16 additions & 30 deletions Q24ECU/core/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "main.h"
#include "limits.h"
#include "taskHandlers.h"

// Init global handles for tasks

TaskHandle_t tskh_Test1 = NULL;
TaskHandle_t tskh_BlinkLED = NULL;
Expand All @@ -17,8 +20,7 @@ TaskHandle_t tskh_USART2_Handler = NULL;
void tsk_Test1(void *param){
(void)(param); // Cast unused variable to void
for(;;){
// Toggle LED Pin
// gpio_toggle_pin(debug_led2);
// Print out the systemtick timer once a second
printf("%d\n", xTaskGetTickCount());
vTaskDelay(1000);
}
Expand All @@ -28,15 +30,7 @@ void tsk_Test1(void *param){
void tsk_BlinkLED(void *param){
(void)param;
for(;;){
// if(hal_uart_read_ready(debug.interface)){
// uint8_t receivedData;

// // Read data from the USART (replace with your actual read operation)
// receivedData = hal_uart_read_byte(debug.interface);

// // Send the received data to the task
// xQueueSend(debug.rxQue, (void*) &receivedData, portMAX_DELAY);
// }
// This task is currently not used so perma suspend it
vTaskSuspend(NULL);
}
}
Expand All @@ -45,19 +39,14 @@ void tsk_USART2_Handler(void *param){
(void)param;
for(;;){
uint8_t buf[64];
uint32_t pendingBit;
// if(xStreamBufferReceive(debug.stream, (void*) &buf, sizeof(buf), 100) > 0){
// // gpio_toggle_pin(debug_led2);
// }
//printf("%d\n", xStreamBufferBytesAvailable(debug.stream));
// if(xStreamBufferBytesAvailable(debug.stream)){
int bytes = xStreamBufferReceive(debug.stream, (void*) &buf, sizeof(uint8_t), 0);
if(bytes > 0){
printf("%c\n", buf[0]);
}

// }
// printf("%c", pendingBit);
size_t bytes = xStreamBufferReceive(debug.stream, (void*) &buf, 64, portMAX_DELAY);
printf("%s (%d)\n", buf, bytes);
// reset the stream buffer
for (int i = 0; i < 64; i++)
{
buf[i] = 0;
}

}
}

Expand All @@ -78,7 +67,7 @@ int main(void){

// Enable Timer 6 (Basic Timer) 1Hz (APB2/45000, count to 2000)
TIM_basic_Init(TIM6, 45000U, 2000U);
NVIC_SetPriority(TIM6_DAC_IRQn, 0x03); // Enable Timer IRQ (lowest priority)
NVIC_SetPriority(TIM6_DAC_IRQn, NVIC_Priority_MIN); // Enable Timer IRQ (lowest priority)
NVIC_EnableIRQ(TIM6_DAC_IRQn);

// hal_uart_init(UART_DEBUG, 9600);
Expand Down Expand Up @@ -106,18 +95,15 @@ int main(void){
&tskh_Test1
);


// Create UART2 (debug) receive task handler
xTaskCreate(
tsk_USART2_Handler,
"u2",
1024,
NULL,
tskIDLE_PRIORITY + 2,
tskIDLE_PRIORITY+4,
&tskh_USART2_Handler
);
printf("u2 created: %d\n", tskh_USART2_Handler != NULL);

// xTaskGetHandle()

// Start Scheduler
vTaskStartScheduler();
Expand Down

0 comments on commit d3127ed

Please sign in to comment.