Skip to content

Commit

Permalink
190 debounce function, cerb_utils, fault handler task notis
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Aug 4, 2024
1 parent 322ad60 commit 39f7b27
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 201 deletions.
27 changes: 27 additions & 0 deletions Core/Inc/cerb_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file cerb_utils.h
* @author Scott Abramson
* @brief Utility functions for Cerberus.
* @version 0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/

#ifndef CERB_UTILS_H
#define CERB_UTILS_H

#include "cmsis_os.h"
#include "stdbool.h"

/**
* @brief Function to debounce an input with an RTOS timer. Debounce is started and maintained by a high signal, and it is interrupted by a low signal.
*
* @param input Input to debounce.
* @param timer RTOS timer with callback to be called at end of debounce period.
* @param period The period of time, in milliseconds, to debounce the input for.
*/
void debounce(bool input, osTimerAttr_t *timer, uint32_t period);

#endif
6 changes: 1 addition & 5 deletions Core/Inc/steeringio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ typedef enum {
MAX_STEERING_BUTTONS
} steeringio_button_t;

typedef struct {
uint8_t data[8];
} button_data_t;

typedef struct {
osMutexId_t *
button_mutex; /* Necessary to allow multiple threads to access same data */
Expand All @@ -41,6 +37,6 @@ steeringio_t *steeringio_init();

bool get_steeringio_button(steeringio_t *wheel, steeringio_button_t button);

void steeringio_update(steeringio_t *wheel, uint8_t button_data[]);
void steeringio_update(steeringio_t *wheel, uint8_t button_data);

#endif /* STEERING_H */
23 changes: 23 additions & 0 deletions Core/Src/cerb_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file cerb_utils.c
* @author Scott Abramson
* @brief Utility functions for Cerberus.
* @version 0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/

#include "cerb_utils.h"

void debounce(bool input, osTimerAttr_t *timer, uint32_t period)
{
if (input && !osTimerIsRunning(timer)) {
/* Start timer if a button has been pressed */
osTimerStart(timer, period);
} else if (!input && osTimerIsRunning(timer)) {
/* Button stopped being pressed. Kill timer. */
osTimerStop(timer);
}
}
77 changes: 40 additions & 37 deletions Core/Src/fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "c_utils.h"

#define FAULT_HANDLE_QUEUE_SIZE 16
#define NEW_FAULT_FLAG 1U

osMessageQueueId_t fault_handle_queue;

Expand All @@ -17,7 +18,10 @@ int queue_fault(fault_data_t *fault_data)
if (!fault_handle_queue)
return -1;

return osMessageQueuePut(fault_handle_queue, fault_data, 0U, 0U);
osStatus_t error =
osMessageQueuePut(fault_handle_queue, fault_data, 0U, 0U);
osThreadFlagsSet(fault_handle, NEW_FAULT_FLAG);
return error;
}

osThreadId_t fault_handle;
Expand All @@ -30,47 +34,46 @@ const osThreadAttr_t fault_handle_attributes = {
void vFaultHandler(void *pv_params)
{
fault_data_t fault_data;
osStatus_t status;
fault_handle_queue = osMessageQueueNew(FAULT_HANDLE_QUEUE_SIZE,
sizeof(fault_data_t), NULL);

for (;;) {
/* Wait until a message is in the queue, send messages when they are in the queue */
status = osMessageQueueGet(fault_handle_queue, &fault_data,
NULL, osWaitForever);
if (status == osOK) {
uint32_t fault_id = (uint32_t)fault_data.id;
endian_swap(&fault_id, sizeof(fault_id));
uint8_t defcon = (uint8_t)fault_data.severity;
osThreadFlagsWait(NEW_FAULT_FLAG, osFlagsWaitAny,
osWaitForever);

can_msg_t msg;
msg.id = CANID_FAULT_MSG;
msg.len = 8;
uint8_t msg_data[8];
memcpy(msg_data, &fault_id, sizeof(fault_id));
memcpy(msg_data + sizeof(fault_id), &defcon,
sizeof(defcon));
memcpy(msg.data, msg_data, msg.len);
queue_can_msg(msg);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n",
fault_data.diag);
switch (fault_data.severity) {
case DEFCON1: /* Highest(1st) Priority */
fault();
break;
case DEFCON2:
fault();
break;
case DEFCON3:
fault();
break;
case DEFCON4:
break;
case DEFCON5: /* Lowest Priority */
break;
default:
break;
}
osMessageQueueGet(fault_handle_queue, &fault_data, NULL,
osWaitForever);

uint32_t fault_id = (uint32_t)fault_data.id;
endian_swap(&fault_id, sizeof(fault_id));
uint8_t defcon = (uint8_t)fault_data.severity;

can_msg_t msg;
msg.id = CANID_FAULT_MSG;
msg.len = 8;
uint8_t msg_data[8];
memcpy(msg_data, &fault_id, sizeof(fault_id));
memcpy(msg_data + sizeof(fault_id), &defcon, sizeof(defcon));
memcpy(msg.data, msg_data, msg.len);
queue_can_msg(msg);
printf("\r\nFault Handler! Diagnostic Info:\t%s\r\n\r\n",
fault_data.diag);
switch (fault_data.severity) {
case DEFCON1: /* Highest(1st) Priority */
fault();
break;
case DEFCON2:
fault();
break;
case DEFCON3:
fault();
break;
case DEFCON4:
break;
case DEFCON5: /* Lowest Priority */
break;
default:
break;
}
}
}
Loading

0 comments on commit 39f7b27

Please sign in to comment.