Skip to content

Commit

Permalink
Refactor: Cleanup TEL FW (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
AarjavJain101 authored Jun 15, 2024
1 parent 5873aa0 commit 69b99f6
Show file tree
Hide file tree
Showing 18 changed files with 1,673 additions and 1,410 deletions.
213 changes: 108 additions & 105 deletions components/tel/Core/Inc/can.h
Original file line number Diff line number Diff line change
@@ -1,105 +1,108 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file can.h
* @brief This file contains all the function prototypes for
* the can.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_H__
#define __CAN_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

#include "cmsis_os.h"

/* USER CODE END Includes */

extern CAN_HandleTypeDef hcan;

/* USER CODE BEGIN Private defines */

#define CAN_READY (uint32_t) 0x0001
#define RTC_TIMESTAMP_MSG_ID 0x300
#define TEL_DIAGNOSTICS_ID 0x750
#define IMU_X_AXIS 0x752
#define IMU_Y_AXIS 0x753
#define IMU_Z_AXIS 0x754
#define GPS_latitude_ID 0x755
#define GPS_longitude_ID 0x756
#define GPS_altitude_hdop_ID 0x757
#define GPS_side_count_ID 0x758
#define GET_BYTE_FROM_WORD(i, word) ((word >> (i * 8)) & 0xFF);

#define CAN_DATA_LENGTH 8

extern CAN_TxHeaderTypeDef tel_diagnostics_header;
extern CAN_TxHeaderTypeDef IMU_x_axis_header;
extern CAN_TxHeaderTypeDef IMU_y_axis_header;
extern CAN_TxHeaderTypeDef IMU_z_axis_header;
extern CAN_TxHeaderTypeDef GPS_latitude_header;
extern CAN_TxHeaderTypeDef GPS_longitude_header;
extern CAN_TxHeaderTypeDef GPS_altitude_hdop_header;
extern CAN_TxHeaderTypeDef GPS_side_count_header;




extern uint32_t can_mailbox;

/* USER CODE END Private defines */

void MX_CAN_Init(void);

/* USER CODE BEGIN Prototypes */


typedef union DoubleBytes {
double double_value; /**< Double value member of the union. */
uint64_t double_as_int; /**< 64 bit in member of union. */
} DoubleBytes;

typedef struct {
CAN_RxHeaderTypeDef header;
uint8_t data[8];
union DoubleBytes timestamp;
} CAN_msg_t;

typedef struct {
CAN_TxHeaderTypeDef header;
uint8_t data[8];
union DoubleBytes timestamp;
} CAN_Radio_msg_t;

void CanFilterSetup(void);
void Can_Init(void);

extern osThreadId readCANTaskHandle;

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __CAN_H__ */

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file can.h
* @brief This file contains all the function prototypes for
* the can.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CAN_H__
#define __CAN_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

#include "cmsis_os.h"
#include "utils.h"

/* USER CODE END Includes */

extern CAN_HandleTypeDef hcan;

/* USER CODE BEGIN Private defines */

#define CAN_READY (uint32_t) 0x0001
#define RTC_TIMESTAMP_MSG_ID 0x300
#define TEL_DIAGNOSTICS_ID 0x750
#define IMU_X_AXIS 0x752
#define IMU_Y_AXIS 0x753
#define IMU_Z_AXIS 0x754
#define GPS_latitude_ID 0x755
#define GPS_longitude_ID 0x756
#define GPS_altitude_hdop_ID 0x757
#define GPS_side_count_ID 0x758

#define CAN_DATA_LENGTH 8
#define CAN_TIMESTAMP_LENGTH 8

/* INITIAL CONSTANTS */
#define INITIAL_FLAGS 0x00
#define FLAG_HIGH 1
#define FIRST_DATA_BYTE 0

extern CAN_TxHeaderTypeDef tel_diagnostics_header;
extern CAN_TxHeaderTypeDef IMU_x_axis_header;
extern CAN_TxHeaderTypeDef IMU_y_axis_header;
extern CAN_TxHeaderTypeDef IMU_z_axis_header;
extern CAN_TxHeaderTypeDef GPS_latitude_header;
extern CAN_TxHeaderTypeDef GPS_longitude_header;
extern CAN_TxHeaderTypeDef GPS_altitude_hdop_header;
extern CAN_TxHeaderTypeDef GPS_side_count_header;




extern uint32_t can_mailbox;

/* USER CODE END Private defines */

void MX_CAN_Init(void);

/* USER CODE BEGIN Prototypes */

typedef struct {
CAN_RxHeaderTypeDef header;
uint8_t data[8];
union Utils_DoubleBytes_t timestamp;
} CAN_msg_t;

typedef struct {
CAN_TxHeaderTypeDef header;
uint8_t data[8];
union Utils_DoubleBytes_t timestamp;
} CAN_Radio_msg_t;

void CanFilterSetup(void);
void CAN_Init(void);
void CAN_radio_and_bus_transmit(CAN_HandleTypeDef* hcan, CAN_Radio_msg_t* tx_CAN_msg, uint32_t* can_mailbox);
void CAN_rx_to_radio(CAN_msg_t* rx_CAN_msg, CAN_Radio_msg_t* tx_CAN_msg);
void CAN_diagnostic_msg_tx_radio_bus();

extern osThreadId readCANTaskHandle;

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __CAN_H__ */

32 changes: 32 additions & 0 deletions components/tel/Core/Inc/gps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file gps.h
* @brief header file for gps.c. Define buffer lengths
*
* @date 2023/03/18
* @author Diego Armstrong
*/

#include "radio.h"
#include "i2c.h"
#include "nmea_parse.h"
#include "rtc.h"
#include "rtos_tasks.h"

#define DOUBLE_LAST_BYTE_IDX 7
#define FLOAT_LAST_BYTE_IDX 3
#define LATSIDE_MSG_IDX 0
#define LONSIDE_MSG_IDX 1
#define GPS_RCV_BUFFER_SIZE 512 // 512 bytes to receive GPS data from I2C
#define UNINITIALIZED 0
#define INDEX_6 6
#define INDEX_7 7
#define NUM_BYTES_FLOAT 4
#define GPS_SIDE_AND_COUNT_DATA_END_INDEX 5
#define GOT_FIX 1
#define GPS_SINGLE_MSG_DELAY 2000 // 2 second delay
#define GPS_NEXT_MSG_DELAY 4000 // 4 second delay
#define GPS_SYNC_TIMEOUT 2 * 60 * 1000 // 2 minute timeout
#define SAT_COUNT_START_INDEX 2

void GPS_delayed_rx_and_tx_as_CAN();
void GPS_wait_for_fix();
4 changes: 4 additions & 0 deletions components/tel/Core/Inc/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ extern I2C_HandleTypeDef hi2c2;
#define NUM_ACCEL_BYTES 6
#define NUM_GYRO_BYTES 6

#define READ_TIMEOUT 1000
#define GPS_TRIALS 1


/* USER CODE END Private defines */

void MX_I2C1_Init(void);
Expand Down
22 changes: 17 additions & 5 deletions components/tel/Core/Inc/imu.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
#include <stdint.h>
#include <time.h>
#include "i2c.h"
#include "usart.h"
#include "sd_logger.h"
#include "rtos_tasks.h"
#include "rtc.h"
#include "can.h"

#define IMU_MESSAGE_LEN 17

#define TIMESTAMP_BYTE(i, timestamp) ((timestamp >> (i * 8)) & 0xFF);
#define IMU_MESSAGE_LEN 17
#define ACCEL_SENSITIVITY 16384.0
#define GYRO_SENSITIVITY 131.0
#define MEM_ADDRESS_SIZE 1
#define NUM_BITS_IN_BYTE_SHIFT 8
#define RAW_X_START_BYTE 0
#define RAW_Y_START_BYTE 2
#define RAW_Z_START_BYTE 4
#define GYRO_DATA_START_BYTE 4

typedef struct {
uint8_t imu_type;
uint8_t dimension;
uint8_t data[4];
} IMU_msg_t;


void transmit_imu_data(uint64_t current_timestamp, uint8_t* imu_data, char imu_type, char dimension);
void IMU_send_as_CAN_msg_single_delay();
6 changes: 6 additions & 0 deletions components/tel/Core/Inc/iwdg.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern "C" {

/* USER CODE BEGIN Includes */

#include "rtos_tasks.h"
#include "cmsis_os.h"

/* USER CODE END Includes */

extern IWDG_HandleTypeDef hiwdg;
Expand All @@ -42,6 +45,9 @@ void MX_IWDG_Init(void);

/* USER CODE BEGIN Prototypes */

void IWDG_refresh();
void IWDG_inf_refresh_with_delay();

/* USER CODE END Prototypes */

#ifdef __cplusplus
Expand Down
48 changes: 48 additions & 0 deletions components/tel/Core/Inc/radio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/**
* @file radio.h
* @brief header file for radio.c. Define buffer lengths
*
* @date 2023/03/18
* @author Aarjav Jain
*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RADIO_TRANSNIT_H__
#define __RADIO_TRANSNIT_H__

#include "can.h"
#include "usart.h"
#include "rtc.h"

/* RADIO BUFFER BYTE LENGTHS */
#define CAN_BUFFER_LEN 24
#define GPS_MESSAGE_LEN 200

/* CAN BUFFER INDECIES */
#define TIMESTAMP_INDEX_START 0
#define TIMESTAMP_INDEX_END 7
#define CAN_MESSAGE_IDENTIFIER_INDEX 8
#define CAN_ID_INDEX_END 12
#define CAN_DATA_INDEX_START 13
#define CAN_DATA_INDEX_END 20
#define CAN_DATA_LENGTH_INDEX 21
#define CAN_CARRIAGE_RETURN_INDEX CAN_BUFFER_LEN - 2
#define CAN_NEW_LINE_INDEX CAN_BUFFER_LEN - 1

/* BYTE LOGIC and LENGTHS */
#define BITS_IN_BYTE 8
#define NUM_STD_ID_BYTES 2
#define NUM_EXT_ID_BYTES 4
#define MASK_8_BITS 0xFF
#define MASK_4_BITS 0xF

/* MSG CONSTANTS */
#define CAN_MESSAGE_IDENTIFIER '#'

/* TIMING CONSTANTS */
#define CAN_TRANSMIT_TIMEOUT 1000 // 1 second timeout

void RADIO_tx_CAN_msg(CAN_Radio_msg_t *tx_CAN_msg);

#endif /* __RADIO_H__ */
Loading

0 comments on commit 69b99f6

Please sign in to comment.