Skip to content

Commit

Permalink
Feature/22a hw init (#8)
Browse files Browse the repository at this point in the history
* inital code changes

Working: LEDs, temp sensor, IMU (need to verify numbers)
Not initing: TOF
Not tested but runs: ADCs

* add ner build env support

* fix prettier violations

* CAN is operational!

* can fixups

* changing msb for imu driver

* annoying blink on hard fault

* validated

* updated submodule

* format

---------

Co-authored-by: Caio <[email protected]>
Co-authored-by: Caio DaSilva <[email protected]>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent 1a54555 commit 1ae59ca
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 208 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Build files
build/

*.nix

# Prerequisites
*.d

Expand Down
4 changes: 3 additions & 1 deletion Core/Inc/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#define configENABLE_FPU 0
#define configENABLE_MPU 0

#define configUSE_PREEMPTION 1
#define configUSE_PREEMPTION 0
#define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configUSE_IDLE_HOOK 0
Expand All @@ -74,7 +74,9 @@
#define configUSE_16_BIT_TICKS 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 2
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
Expand Down
1 change: 1 addition & 0 deletions Core/Inc/msb.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "sht30.h"
#include "stm32f405xx.h"
#include "vl6180x_api.h"
#include "vl6180x_platform.h"
#include "msb_conf.h"

typedef enum {
Expand Down
9 changes: 6 additions & 3 deletions Core/Inc/msb_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// CONTROLLER
#define DELAY_DEBUG_LED_REFRESH 250

#define DELAY_CAN_DISPATCH 5
#define DELAY_CAN_DISPATCH 2

// CAN IDS
#define CANID_TEMP_SENSOR 0x602
Expand All @@ -26,14 +26,17 @@

// Sensors to use, comment out to disable

// internal
#define CAN_ENABLE

//on central
#define SENSOR_TEMP
#define SENSOR_SHOCKPOT
#define SENSOR_STRAIN
#define SENSOR_TOF
//#define SENSOR_TOF

// on knuckle or wheel
#define SENSOR_IMU
// on knuckle or /wheel
//#define SENSOR_WHEEL_TEMP

// VERBOSE LOGGING
Expand Down
15 changes: 0 additions & 15 deletions Core/Inc/serial_monitor.h

This file was deleted.

1 change: 0 additions & 1 deletion Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void DebugMon_Handler(void);
void CAN1_RX0_IRQHandler(void);
void TIM5_IRQHandler(void);
void DMA2_Stream0_IRQHandler(void);
/* USER CODE BEGIN EFP */
Expand Down
20 changes: 11 additions & 9 deletions Core/Src/can_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "can_handler.h"
#include "can.h"
#include "msb_conf.h"
#include "serial_monitor.h"

#include "stdio.h"
#include <assert.h>
Expand All @@ -28,12 +27,14 @@ can_t *can1;

void can1_init()
{
#ifdef CAN_ENABLE
can1 = malloc(sizeof(can_t));
assert(can1);

can1->hcan = &hcan1;

assert(!can_init(can1));
#endif

can_outbound_queue =
osMessageQueueNew(CAN_MSG_QUEUE_SIZE, sizeof(can_msg_t), NULL);
Expand All @@ -49,28 +50,29 @@ const osThreadAttr_t can_dispatch_attributes = {
void vCanDispatch(void *pv_params)
{
can_msg_t msg_from_queue;
#ifdef CAN_ENABLE
HAL_StatusTypeDef msg_status;
#endif

for (;;) {
/* Send CAN message */
if (osOK == osMessageQueueGet(can_outbound_queue,
&msg_from_queue, NULL,
osWaitForever)) {
#ifdef CAN_ENABLE
msg_status = can_send_msg(can1, &msg_from_queue);
if (msg_status == HAL_ERROR) {
serial_print("Failed to send CAN message");
if (msg_status != HAL_OK) {
printf("Failed to send CAN message");
} else if (msg_status == HAL_BUSY) {
serial_print("Outbound mailbox full!");
printf("Outbound mailbox full!");
}
#endif
#ifdef LOG_VERBOSE
else {
printf("Message sent: %lX\r\n",
msg_from_queue.id);
}
printf("Message sent att: %lX\r\n", msg_from_queue.id);
#endif
}

osDelay(DELAY_CAN_DISPATCH);
//osDelay(DELAY_CAN_DISPATCH);
}
}

Expand Down
61 changes: 42 additions & 19 deletions Core/Src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,49 @@ const osThreadAttr_t led_controller_attributes = {

void vLedController(void *pv_params)
{
switch (device_loc) {
case DEVICE_FRONT_LEFT:
debug1_write(true);
debug2_write(true);
break;
case DEVICE_FRONT_RIGHT:
debug1_write(true);
debug2_write(false);
break;
case DEVICE_BACK_LEFT:
debug1_write(false);
debug2_write(true);
break;
case DEVICE_BACK_RIGHT:
debug1_write(false);
debug2_write(false);
break;
}

uint8_t i = 0;
for (;;) {
if (i % 8 == 0) {
// occassionally oposing blink
switch (device_loc) {
case DEVICE_FRONT_LEFT:
debug1_write(false);
debug2_write(false);
break;
case DEVICE_FRONT_RIGHT:
debug1_write(false);
debug2_write(true);
break;
case DEVICE_BACK_LEFT:
debug1_write(true);
debug2_write(false);
break;
case DEVICE_BACK_RIGHT:
debug1_write(true);
debug2_write(true);
break;
}
} else {
switch (device_loc) {
case DEVICE_FRONT_LEFT:
debug1_write(true);
debug2_write(true);
break;
case DEVICE_FRONT_RIGHT:
debug1_write(true);
debug2_write(false);
break;
case DEVICE_BACK_LEFT:
debug1_write(false);
debug2_write(true);
break;
case DEVICE_BACK_RIGHT:
debug1_write(false);
debug2_write(false);
break;
}
}
i++;
osDelay(DELAY_DEBUG_LED_REFRESH);
}
}
35 changes: 34 additions & 1 deletion Core/Src/freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -52,6 +52,39 @@

/* USER CODE END FunctionPrototypes */

/* Hook prototypes */
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName);
void vApplicationMallocFailedHook(void);

/* USER CODE BEGIN 4 */
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
called if a stack overflow is detected. */
printf("ERROR: STACK OVERFLOW in ");
printf((char *)pcTaskName);
printf("\r\n");
}
/* USER CODE END 4 */

/* USER CODE BEGIN 5 */
void vApplicationMallocFailedHook(void)
{
/* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails.
pvPortMalloc() is called internally by the kernel whenever a task, queue,
timer or semaphore is created. It is also called by various parts of the
demo application. If heap_1.c or heap_2.c are used, then the size of the
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
to query the size of free heap space that remains (although it does not
provide information on how the remaining heap might be fragmented). */
printf("ERROR: MALLOC FAILED\r\n");
}
/* USER CODE END 5 */

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

Expand Down
28 changes: 18 additions & 10 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
/* USER CODE BEGIN Includes */
#include "can_handler.h"
#include "controller.h"
#include "serial_monitor.h"
#include "monitor.h"
#include "msb.h"
#include "msb_conf.h"
#include "assert.h"
#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -150,6 +150,7 @@ int main(void)
MX_SPI2_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
printf("INIT MSB... FINDING ID");
// determine MSB location
bool loc1 = HAL_GPIO_ReadPin(Addr0_GPIO_Port, Addr0_Pin);
bool loc2 = HAL_GPIO_ReadPin(Addr1_GPIO_Port, Addr1_Pin);
Expand All @@ -176,6 +177,7 @@ int main(void)
device_loc = DEVICE_FRONT_LEFT;
}


/* USER CODE END 2 */

/* Init scheduler */
Expand Down Expand Up @@ -203,8 +205,6 @@ int main(void)
/* add queues, ... */
can_dispatch_handle = osThreadNew(vCanDispatch, NULL, &can_dispatch_attributes);
assert(can_dispatch_handle);
serial_monitor_handle = osThreadNew(vSerialMonitor, NULL, &serial_monitor_attributes);
assert(serial_monitor_handle);
/* USER CODE END RTOS_QUEUES */

/* Create the thread(s) */
Expand All @@ -224,17 +224,17 @@ int main(void)
#endif

#ifdef SENSOR_TOF
tof_monitor_handle = osThreadNew(vIMUMonitor, NULL, &tof_monitor_attributes);
tof_monitor_handle = osThreadNew(vTOFMonitor, NULL, &tof_monitor_attributes);
assert(tof_monitor_handle);
#endif

#ifdef SENSOR_SHOCKPOT
shockpot_monitor_handle = osThreadNew(vIMUMonitor, NULL, &shockpot_monitor_attributes);
shockpot_monitor_handle = osThreadNew(vShockpotMonitor, NULL, &shockpot_monitor_attributes);
assert(shockpot_monitor_handle);
#endif

#ifdef SENSOR_STRAIN
strain_monitor_handle = osThreadNew(vIMUMonitor, NULL, &strain_monitor_attributes);
strain_monitor_handle = osThreadNew(vStrainMonitor, NULL, &strain_monitor_attributes);
assert(strain_monitor_handle);
#endif

Expand Down Expand Up @@ -398,7 +398,7 @@ static void MX_CAN1_Init(void)

/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 1;
hcan1.Init.Prescaler = 2;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
Expand All @@ -414,7 +414,6 @@ static void MX_CAN1_Init(void)
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
can1_init(&hcan1);

/* USER CODE END CAN1_Init 2 */

Expand Down Expand Up @@ -620,11 +619,19 @@ static void MX_GPIO_Init(void)
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN 5 */
uint8_t i = 0;
printf("Init MSB default task...\r\n");
/* Infinite loop */
for (;;)
{
/* Pet watchdog, will reset after 4ish seconds */
HAL_IWDG_Refresh(&hiwdg);
if (i % 2 == 0) {
printf("MSB TYPE: %d\r\n", device_loc);
} else {
printf("..\r\n");
}
i++;
osDelay(500);
}
/* USER CODE END 5 */
Expand Down Expand Up @@ -662,6 +669,7 @@ void Error_Handler(void)
__disable_irq();
while (1)
{
printf("Error Hanlder HIT!\r\n");
}
/* USER CODE END Error_Handler_Debug */
}
Expand All @@ -677,8 +685,8 @@ void Error_Handler(void)
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* User can add his own implementation to report the file name and line number, */
printf("Param Assert failed: file %s on line %ld\r\n", file, line);
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Loading

0 comments on commit 1ae59ca

Please sign in to comment.