Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file removed firmware/com-modules/.gitkeep
Empty file.
27 changes: 8 additions & 19 deletions firmware/com-modules/CANFD/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,14 @@ HAL_StatusTypeDef CAN_Transmit(uint32_t Identifier, uint32_t IdType, uint32_t Da
}

/**
* @brief Copies received CAN Rx data and adds metadata into a local user-provided buffer.
* @param RxData_buffer: Pointer to buffer where received data will be copied.
* Buffer format: {ID[0], ID[1], ID[2], ID[3], Length, Data[0], ..., Data[n]}
* - Bytes 0-3: 32-bit identifier in little-endian
* - Byte 4: Data length in uint8_t
* - Bytes 5+: Actual data payload
* @return HAL_StatusTypeDef: HAL_OK if a message was received, HAL_ERROR if queue is empty.
* @note Should be called from the main application context, not from ISR.
* The queue uses a circular buffer.
* @brief Dequeues the next received CAN frame.
* @param frame: Pointer to a CAN_Frame struct that will be filled with the data.
* @return HAL_OK if a frame was dequeued, HAL_ERROR if the queue is empty.
*
* @note Called from application context (not ISR).
*/
HAL_StatusTypeDef CAN_Receive(uint8_t *RxData_buffer) {
CAN_Frame frame;
if (CAN_DequeueFrame(&frame) == 0) return HAL_ERROR;

RxData_buffer[0] = (uint8_t)(frame.RxData1_Identifier & 0xFF);
RxData_buffer[1] = (uint8_t)((frame.RxData1_Identifier >> 8) & 0xFF);
RxData_buffer[2] = (uint8_t)((frame.RxData1_Identifier >> 16) & 0xFF);
RxData_buffer[3] = (uint8_t)((frame.RxData1_Identifier >> 24) & 0xFF);
RxData_buffer[4] = frame.RxData1_BufferLength;
memcpy(RxData_buffer + 5, frame.RxData1, frame.RxData1_BufferLength);
HAL_StatusTypeDef CAN_Receive(CAN_Frame *frame) {
if (CAN_DequeueFrame(frame) == 0) return HAL_ERROR;
return HAL_OK;
}

Expand All @@ -166,6 +154,7 @@ HAL_StatusTypeDef CAN_Receive(uint8_t *RxData_buffer) {
* - When processing of urgent messages
*/


/**
* @brief Callback function for handling messages received in FIFO0.
* @param hfdcan: Pointer to FDCAN handle.
Expand Down
6 changes: 3 additions & 3 deletions firmware/com-modules/CANFD/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <stdint.h>
#include <stdlib.h>

/* CAN_frame struct for Rx buffer (standard filter). Provides Rx ID, length and buffer -*/
/* CAN_frame struct for Rx buffer (standard filter). Provides Rx ID, length and buffer --*/
typedef struct {
uint32_t RxData1_Identifier;
uint8_t RxData1_BufferLength;
Expand All @@ -28,6 +28,6 @@ typedef struct {
/* Function prototypes ------------------------------------------------------------------*/
void CAN_Init(FDCAN_HandleTypeDef *hfdcan1);
HAL_StatusTypeDef CAN_Transmit(uint32_t Identifier, uint32_t IdType, uint32_t DataLength, uint8_t* DataBuffer, FDCAN_HandleTypeDef *hfdcan1);
HAL_StatusTypeDef CAN_Receive(uint8_t *RxData_buffer);
HAL_StatusTypeDef CAN_Receive(CAN_Frame *frame);
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs);
#endif /* SRC_CAN_H_ */
#endif /* SRC_CAN_H_ */
39 changes: 39 additions & 0 deletions firmware/com-modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Firmware Base Library

This page is for the communication modules base library, and includes explanations and instructions for its users. Don't forget to also read the main README.

## High-level Design

<img width="692" alt="image" src="https://github.com/UBCSailbot/com-module-firmware/assets/144284916/c1f9b2fc-40b4-414a-8d1a-b80418eed409">

### What is the Base Library?
The base library is made up of files corresponding to the boxes in the image above. Namely, they are CAN, Board, Debug, Error, and Config. The purpose of this code is to provide the "outline" for teams to build off of. It contains functions that facilitate communication through CAN, I2C, ADC, PWM, and UART, as well as provides debug and error handling capabilities. Lastly, the configuration file provides unique customizability, and avoids including unneeded code in other teams designs, thereby optimizing performance.

Here is what you need to know about which features/functions you can find, and where:

```can.h``` - provides functionality to create CAN message frames to be sent, as well as parse and extract data from received frames (the way it does this depends on the device being communicated with, which will be specified in the config file)

```board.h``` - provides basic functions for CAN, I2C, ADC, PWM, and UART communication

```debug.h``` - provides debugging functions such as printing/storing logs

```error.h``` - provides functions that properly handle errors in operations such as CAN communication or issues with receiving sensor data

As you probably notice, the location of each feature is quite intuitive, especially once you've seen the diagram above.

To find these files, navigate through the repository as follows: ```CANFD -> can.h``` for CANFD, ```base-library inc -> xxxxx.h``` otherwise

## User Manual
Before continuing, please read the "What is the Base Library" paragraph above if you haven't already.

The three boxes at the top of the firmware design diagram illustrate the tasks of the individual teams. To reach the desired functionality for a specific COM module, such as reading data from a sensor, teams have to determine which functions are required, and what the proper inputs to those functions would be. Most important for this would be the ```board.h``` file, but the comprehensive list above covers everything available.

There are also features that you can request from the firmware team. If there is any specific functionality you would like for handling errors, such as turning off a sensor if we can no longer receive data from it, then leave your request on the [Confluence feature request page](https://ubcsailbot.atlassian.net/wiki/spaces/prjt22/pages/1994457093/Feature+Request). Additionally, any other requests for code that needs new functions/functionality that isn't included in the base library can also be left in the same place.

Lastly, you can change values in the configurations file based on what you need and don't need from the base library. This is an important consideration as it will improve the performance of your COM Module!

## Notes
You can find tutorials on using STM32CubeIDE, GitHub, and more in the tutorials folder.

Don't forget to consult the main README to learn more.

62 changes: 62 additions & 0 deletions firmware/com-modules/base-library inc/board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* board.h
*
* Description: Provides declarations for variables and function prototypes related to CAN, I2C, ADC, PWM, UART communication, and other hardware peripherals.
*
* Created on: Mar 29, 2024
* Author: Peter, Jordan, Katherine
*/

#ifndef INC_BOARD_H_
#define INC_BOARD_H_

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

/* Variables ------------------------------------------------------------------*/
/* Protocols */
extern FDCAN_HandleTypeDef hfdcan1;
extern I2C_HandleTypeDef hi2c1;
extern I2C_HandleTypeDef hi2c2;
extern I2C_HandleTypeDef hi2c3;
extern I2C_HandleTypeDef hi2c4;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern HAL_StatusTypeDef status;


/* Non-protocols */
extern ADC_HandleTypeDef hadc1;
extern DMA_NodeTypeDef Node_GPDMA1_Channel4;
extern DMA_QListTypeDef List_GPDMA1_Channel4;
extern DMA_HandleTypeDef handle_GPDMA1_Channel4;
extern DMA_QListTypeDef pQueueLinkList;
extern DMA_NodeTypeDef Node_GPDMA1_Channel1;
extern DMA_QListTypeDef List_GPDMA1_Channel1;
extern DMA_HandleTypeDef handle_GPDMA1_Channel1;
extern uint8_t UART1_rxBuffer[1];

/* Function prototypes ------------------------------------------------------------------*/
void delay(uint16_t time);

GPIO_PinState gpio_rd_e2();
GPIO_PinState gpio_rd_e4();
void gpio_wr_e3();
void gpio_rs_e3();
void gpio_wr_e5();
void gpio_rs_e5();

void pwm1_init_ch1(uint16_t dutycycle);
void pwm1_set_ch1(uint16_t dutycycle);
void pwm3_init_ch1(uint16_t dutycycle);
void pwm3_set_ch1(uint16_t dutycycle);

extern HAL_StatusTypeDef i2c_wr(I2C_HandleTypeDef handle, uint8_t device_address, uint8_t register_address, uint16_t value);
extern HAL_StatusTypeDef i2c_rd(I2C_HandleTypeDef handle, uint8_t device_address, uint8_t register_address, uint16_t* value);

void uart_rd(UART_HandleTypeDef handle, uint8_t* buffer, int size);

#endif /* INC_BOARD_H_ */
22 changes: 22 additions & 0 deletions firmware/com-modules/base-library inc/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* debug.h
*
* Description: Provides declarations for variables and function prototypes related to debugging features.
*
* Created on: Mar 29, 2024
* Author: Peter, Jordan, Katherine
*/

#ifndef INC_OPRT_H_
#define INC_OPRT_H_

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

/* Variables ------------------------------------------------------------------*/
extern uint8_t UART1_rxBuffer[1];

/* Function prototypes ------------------------------------------------------------------*/
int debug_key(void);

#endif /* INC_OPRT_H_ */
52 changes: 52 additions & 0 deletions firmware/com-modules/base-library inc/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* error.h
*
* Description: Provides declarations for variables and function prototypes related to error handling.
*
* Created on: Mar 30, 2024
* Author: Moiz
*/
#include <cstdint>

#ifndef INC_ERROR_H_
#define INC_ERROR_H_

#define ERROR_LOG_SIZE 10

typedef enum
{
ERR_LOW,
ERR_MID,
ERR_HIGH
}errlevel;

typedef enum
{
SLOW_BLINK,
FAST_BLINK,
}ledpattern;

typedef enum
{
OK,
ERROR
} errcode;

// TODO: Move both of these definitions to source code later otherwise the compiler will complain (multiple definitions)
errcode errlog[ERROR_LOG_SIZE];
uint8_t index;



//-- Internal functions
static void reportError(errcode err_code, errlevel err_level); // Sending all accumulated error codes through UART and/or through CAN
static void logError(errcode err_code); // Add error to error logs. Can be stored in memory
static void ledIndicationError(ledpattern pattern);
static void I2C_ErrorResetCycle(I2C_HandleTypeDef handle, uint16_t device_address, uint8_t register_address, int read_regs);

//-- Public Functions
#define REPORT_ERR(err_code, err_level) (reportError(err_code, err_level))
errcode getLatestError(void);
void clearAllErrors(void);

#endif /* INC_ERROR_H_ */
97 changes: 97 additions & 0 deletions firmware/com-modules/base-library inc/veml3328.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* veml3328.h
*
* Created on: Mar 29, 2024
* Author: Peter
*/

#ifndef INC_VEML3328_H_
#define INC_VEML3328_H_

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

/* Variables ------------------------------------------------------------------*/
/*Data variables*/
extern HAL_StatusTypeDef r,g,b;
extern uint16_t conf, r_data, g_data, b_data;

/* Registers */
#define veml3328_addr 0x10
#define veml3328__conf 0x00
#define veml3328_reg_deviceID 0x0C
#define C_ 0x04
#define R_ 0x05
#define G_ 0x06
#define B_ 0x07
#define IR_ 0x08

/* Sensor resolutions */
#define res1 0.003
#define res2 0.006
#define res3 0.012
#define res4 0.024
#define res5 0.048
#define res6 0.096
#define res7 0.192
#define res8 0.328
#define res9 0.768

/* Command codes for registers */
typedef enum {
SD1_on = 0,
SD1_off = 1 // default
} SD1;

typedef enum{
chnl_on = 0, // default
chnl_GCIR_on = 1
} SD_ALS;

typedef enum {
DGx1 = 0, // default
DGx2 = 1,
DGx4 = 2
} DG;

typedef enum {
GAINx1 = 0, // default
GAINx2 = 1,
GAINx4 = 2,
GAINx1_2 = 3
} GAIN;

typedef enum {
SENS_hi = 0, // default
SENS_lo = 1
} SENS;

typedef enum {
IT_50 = 0, // default
IT_100 = 1,
IT_200 = 2,
IT_400 = 3
} IT;

typedef enum {
AF_auto = 0, // default
AF_active = 1
} AF;

typedef enum{
TRIG_inf = 0, // default
TRIG_one = 1
} TRIG;

typedef enum {
SD0_on = 0,
SD0_off = 1 // default
} SD0;

/* Function prototypes ------------------------------------------------------------------*/
HAL_StatusTypeDef veml3328_init(void);
void veml3328_rd_rgb(void);
uint16_t veml3328_avg_amb(void);
int veml3328_run(uint16_t amb);

#endif /* INC_VEML3328_H_ */
Loading