From b1fd0bde589fb09247526440e2e06465349006d1 Mon Sep 17 00:00:00 2001 From: planeflight Date: Mon, 30 Mar 2026 21:13:05 -0700 Subject: [PATCH 1/7] add SAMM code from last year --- SAMM/Core/Extras/CircBuF/circularBuffer.c | 64 ++ SAMM/Core/Extras/CircBuF/circularBuffer.h | 22 + SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c | 699 ++++++++++++++++++ SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h | 405 ++++++++++ .../Extras/VL53L4ED/VL53L4ED_calibration.c | 242 ++++++ .../Extras/VL53L4ED/VL53L4ED_calibration.h | 73 ++ SAMM/Core/Extras/VL53L4ED/platform.c | 113 +++ SAMM/Core/Extras/VL53L4ED/platform.h | 80 ++ SAMM/Core/Extras/extra.c | 58 ++ SAMM/Core/Extras/extra.h | 20 + SAMM/Core/Inc/adc.h | 52 ++ SAMM/Core/Inc/crc.h | 52 ++ SAMM/Core/Inc/dma.h | 52 ++ SAMM/Core/Inc/fdcan.h | 55 ++ SAMM/Core/Inc/gpio.h | 49 ++ SAMM/Core/Inc/i2c.h | 52 ++ SAMM/Core/Inc/main.h | 80 ++ SAMM/Core/Inc/spi.h | 52 ++ SAMM/Core/Inc/stm32_assert.h | 53 ++ SAMM/Core/Inc/stm32g4xx_hal_conf.h | 380 ++++++++++ SAMM/Core/Inc/stm32g4xx_it.h | 67 ++ SAMM/Core/Src/adc.c | 154 ++++ SAMM/Core/Src/crc.c | 90 +++ SAMM/Core/Src/dma.c | 59 ++ SAMM/Core/Src/fdcan.c | 223 ++++++ SAMM/Core/Src/gpio.c | 99 +++ SAMM/Core/Src/i2c.c | 135 ++++ SAMM/Core/Src/main.c | 300 ++++++++ SAMM/Core/Src/spi.c | 121 +++ SAMM/Core/Src/stm32g4xx_hal_msp.c | 86 +++ SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c | 127 ++++ SAMM/Core/Src/stm32g4xx_it.c | 218 ++++++ SAMM/Core/Src/syscalls.c | 176 +++++ SAMM/Core/Src/sysmem.c | 79 ++ SAMM/Core/Src/system_stm32g4xx.c | 285 +++++++ 35 files changed, 4872 insertions(+) create mode 100644 SAMM/Core/Extras/CircBuF/circularBuffer.c create mode 100644 SAMM/Core/Extras/CircBuF/circularBuffer.h create mode 100644 SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c create mode 100644 SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h create mode 100644 SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c create mode 100644 SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h create mode 100644 SAMM/Core/Extras/VL53L4ED/platform.c create mode 100644 SAMM/Core/Extras/VL53L4ED/platform.h create mode 100644 SAMM/Core/Extras/extra.c create mode 100644 SAMM/Core/Extras/extra.h create mode 100644 SAMM/Core/Inc/adc.h create mode 100644 SAMM/Core/Inc/crc.h create mode 100644 SAMM/Core/Inc/dma.h create mode 100644 SAMM/Core/Inc/fdcan.h create mode 100644 SAMM/Core/Inc/gpio.h create mode 100644 SAMM/Core/Inc/i2c.h create mode 100644 SAMM/Core/Inc/main.h create mode 100644 SAMM/Core/Inc/spi.h create mode 100644 SAMM/Core/Inc/stm32_assert.h create mode 100644 SAMM/Core/Inc/stm32g4xx_hal_conf.h create mode 100644 SAMM/Core/Inc/stm32g4xx_it.h create mode 100644 SAMM/Core/Src/adc.c create mode 100644 SAMM/Core/Src/crc.c create mode 100644 SAMM/Core/Src/dma.c create mode 100644 SAMM/Core/Src/fdcan.c create mode 100644 SAMM/Core/Src/gpio.c create mode 100644 SAMM/Core/Src/i2c.c create mode 100644 SAMM/Core/Src/main.c create mode 100644 SAMM/Core/Src/spi.c create mode 100644 SAMM/Core/Src/stm32g4xx_hal_msp.c create mode 100644 SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c create mode 100644 SAMM/Core/Src/stm32g4xx_it.c create mode 100644 SAMM/Core/Src/syscalls.c create mode 100644 SAMM/Core/Src/sysmem.c create mode 100644 SAMM/Core/Src/system_stm32g4xx.c diff --git a/SAMM/Core/Extras/CircBuF/circularBuffer.c b/SAMM/Core/Extras/CircBuF/circularBuffer.c new file mode 100644 index 000000000..e7f59de79 --- /dev/null +++ b/SAMM/Core/Extras/CircBuF/circularBuffer.c @@ -0,0 +1,64 @@ +#include "circularBuffer.h" +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) { + CircularBuffer *cb = malloc(sizeof(CircularBuffer)); + if (!cb) { + return NULL; + } + + cb->buffer = malloc(capacity * sizeof(void *)); + if (!cb->buffer) { + free(cb); + return NULL; + } + + for (size_t i = 0; i < capacity; i++) { + cb->buffer[i] = malloc(max_array_size); + if (!cb->buffer[i]) { + for (size_t j = 0; j < i; j++) { + free(cb->buffer[j]); + } + free(cb->buffer); + free(cb); + return NULL; + } + } + cb->capacity = capacity; + cb->max_arr_size = max_array_size; + cb->head = 0; + cb->tail = 0; + cb->size = 0; + return cb; +} + +void circularBufferDestroy(CircularBuffer *cb) { + if (!cb) return; + + for (size_t i = 0; i < cb->capacity; i++) { + free(cb->buffer[i]); + } + free(cb->buffer); + free(cb); +} + +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) { + if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { + return -1; // Buffer is full or array is too large + } + + // Copy the array into the buffer + memcpy(cb->buffer[cb->head], array, array_size); + cb->head = (cb->head + 1) % cb->capacity; + cb->size++; + return 0; +} + +void *circularBufferPop(CircularBuffer *cb) { + if (!cb || cb->size == 0) { + return NULL; // Buffer is empty + } + + void *array = cb->buffer[cb->tail]; + cb->tail = (cb->tail + 1) % cb->capacity; + cb->size--; + return array; +} diff --git a/SAMM/Core/Extras/CircBuF/circularBuffer.h b/SAMM/Core/Extras/CircBuF/circularBuffer.h new file mode 100644 index 000000000..ae7ce9220 --- /dev/null +++ b/SAMM/Core/Extras/CircBuF/circularBuffer.h @@ -0,0 +1,22 @@ +#ifndef __circularBuffer_h__ +#define __circularBuffer_h__ + +#include +#include +#include +#include + +typedef struct { + void **buffer; // Array of void pointers + uint8_t capacity; // Maximum number of elements in the buffer + uint8_t head; // Index of the next element to write + uint8_t tail; // Index of the next element to read + uint8_t size; // Current number of elements in the buffer + uint8_t max_arr_size; // Maximum size of the array +} CircularBuffer; + +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size); +void circularBufferDestroy(CircularBuffer *cb); +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size); +void *circularBufferPop(CircularBuffer *cb); +#endif \ No newline at end of file diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c new file mode 100644 index 000000000..323148da4 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c @@ -0,0 +1,699 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_api.c + * @brief Functions implementation + */ + +#include +#include +#include "VL53L4ED_api.h" + +static const uint8_t VL53L4ED_DEFAULT_CONFIGURATION[] = { + #ifdef VL53L4ED_I2C_FAST_MODE_PLUS + 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ + #else + 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ + #endif + 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low + (bits 3:0 must be 0x1), use SetInterruptPolarity() */ + 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, + use CheckForDataReady() */ + 0x00, /* 0x32 : not user-modifiable */ + 0x02, /* 0x33 : not user-modifiable */ + 0x08, /* 0x34 : not user-modifiable */ + 0x00, /* 0x35 : not user-modifiable */ + 0x08, /* 0x36 : not user-modifiable */ + 0x10, /* 0x37 : not user-modifiable */ + 0x01, /* 0x38 : not user-modifiable */ + 0x01, /* 0x39 : not user-modifiable */ + 0x00, /* 0x3a : not user-modifiable */ + 0x00, /* 0x3b : not user-modifiable */ + 0x00, /* 0x3c : not user-modifiable */ + 0x00, /* 0x3d : not user-modifiable */ + 0xff, /* 0x3e : not user-modifiable */ + 0x00, /* 0x3f : not user-modifiable */ + 0x0F, /* 0x40 : not user-modifiable */ + 0x00, /* 0x41 : not user-modifiable */ + 0x00, /* 0x42 : not user-modifiable */ + 0x00, /* 0x43 : not user-modifiable */ + 0x00, /* 0x44 : not user-modifiable */ + 0x00, /* 0x45 : not user-modifiable */ + 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, + 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ + 0x0b, /* 0x47 : not user-modifiable */ + 0x00, /* 0x48 : not user-modifiable */ + 0x00, /* 0x49 : not user-modifiable */ + 0x02, /* 0x4a : not user-modifiable */ + 0x14, /* 0x4b : not user-modifiable */ + 0x21, /* 0x4c : not user-modifiable */ + 0x00, /* 0x4d : not user-modifiable */ + 0x00, /* 0x4e : not user-modifiable */ + 0x05, /* 0x4f : not user-modifiable */ + 0x00, /* 0x50 : not user-modifiable */ + 0x00, /* 0x51 : not user-modifiable */ + 0x00, /* 0x52 : not user-modifiable */ + 0x00, /* 0x53 : not user-modifiable */ + 0xc8, /* 0x54 : not user-modifiable */ + 0x00, /* 0x55 : not user-modifiable */ + 0x00, /* 0x56 : not user-modifiable */ + 0x38, /* 0x57 : not user-modifiable */ + 0xff, /* 0x58 : not user-modifiable */ + 0x01, /* 0x59 : not user-modifiable */ + 0x00, /* 0x5a : not user-modifiable */ + 0x08, /* 0x5b : not user-modifiable */ + 0x00, /* 0x5c : not user-modifiable */ + 0x00, /* 0x5d : not user-modifiable */ + 0x01, /* 0x5e : not user-modifiable */ + 0xcc, /* 0x5f : not user-modifiable */ + 0x07, /* 0x60 : not user-modifiable */ + 0x01, /* 0x61 : not user-modifiable */ + 0xf1, /* 0x62 : not user-modifiable */ + 0x05, /* 0x63 : not user-modifiable */ + 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), + use SetSigmaThreshold(), default value 90 mm */ + 0xa0, /* 0x65 : Sigma threshold LSB */ + 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), + use SetSignalThreshold() */ + 0x80, /* 0x67 : Min count Rate LSB */ + 0x08, /* 0x68 : not user-modifiable */ + 0x38, /* 0x69 : not user-modifiable */ + 0x00, /* 0x6a : not user-modifiable */ + 0x00, /* 0x6b : not user-modifiable */ + 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, + use SetIntermeasurementInMs() */ + 0x00, /* 0x6d : Intermeasurement period */ + 0x0f, /* 0x6e : Intermeasurement period */ + 0x89, /* 0x6f : Intermeasurement period LSB */ + 0x00, /* 0x70 : not user-modifiable */ + 0x00, /* 0x71 : not user-modifiable */ + 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x73 : distance threshold high LSB */ + 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x75 : distance threshold low LSB */ + 0x00, /* 0x76 : not user-modifiable */ + 0x01, /* 0x77 : not user-modifiable */ + 0x07, /* 0x78 : not user-modifiable */ + 0x05, /* 0x79 : not user-modifiable */ + 0x06, /* 0x7a : not user-modifiable */ + 0x06, /* 0x7b : not user-modifiable */ + 0x00, /* 0x7c : not user-modifiable */ + 0x00, /* 0x7d : not user-modifiable */ + 0x02, /* 0x7e : not user-modifiable */ + 0xc7, /* 0x7f : not user-modifiable */ + 0xff, /* 0x80 : not user-modifiable */ + 0x9B, /* 0x81 : not user-modifiable */ + 0x00, /* 0x82 : not user-modifiable */ + 0x00, /* 0x83 : not user-modifiable */ + 0x00, /* 0x84 : not user-modifiable */ + 0x01, /* 0x85 : not user-modifiable */ + 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ + 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), + If you want an automatic start after VL53L4ED_init() call, + put 0x40 in location 0x87 */ +}; + +VL53L4ED_Error VL53L4ED_GetSWVersion( + VL53L4ED_Version_t *p_Version) +{ + VL53L4ED_Error Status = VL53L4ED_ERROR_NONE; + + p_Version->major = VL53L4ED_IMPLEMENTATION_VER_MAJOR; + p_Version->minor = VL53L4ED_IMPLEMENTATION_VER_MINOR; + p_Version->build = VL53L4ED_IMPLEMENTATION_VER_BUILD; + p_Version->revision = VL53L4ED_IMPLEMENTATION_VER_REVISION; + return Status; +} + +VL53L4ED_Error VL53L4ED_SetI2CAddress( + Dev_t dev, + uint8_t new_address) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, + (uint8_t)(new_address >> (uint8_t)1)); + return status; +} + +VL53L4ED_Error VL53L4ED_GetSensorId( + Dev_t dev, + uint16_t *p_id) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_IDENTIFICATION__MODEL_ID, p_id); + return status; +} + +VL53L4ED_Error VL53L4ED_SensorInit( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t Addr, tmp; + uint8_t continue_loop = 1; + uint16_t i = 0; + + do{ + status |= VL53L4ED_RdByte(dev, + VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); + + if(tmp == (uint8_t)0x3) /* Sensor booted */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for boot */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + /* Load default configuration */ + for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) + { + status |= VL53L4ED_WrByte(dev, Addr, + VL53L4ED_DEFAULT_CONFIGURATION[ + Addr - (uint8_t)0x2D]); + } + + /* Start VHV */ + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, (uint8_t)0x40); + i = (uint8_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_ClearInterrupt(dev); + status |= VL53L4ED_StopRanging(dev); + status |= VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, + (uint8_t)0x09); + status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0); + status |= VL53L4ED_WrWord(dev, 0x0024, 0x500); + + status |= VL53L4ED_SetRangeTiming(dev, 100, 0); + + return status; +} + +VL53L4ED_Error VL53L4ED_ClearInterrupt( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM__INTERRUPT_CLEAR, 0x01); + return status; +} + +VL53L4ED_Error VL53L4ED_StartRanging( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint32_t tmp; + + status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); + + /* Sensor runs in continuous mode */ + if(tmp == (uint32_t)0) + { + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x21); + } + /* Sensor runs in autonomous mode */ + else + { + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x40); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_StopRanging( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x80); + return status; +} + +VL53L4ED_Error VL53L4ED_CheckForDataReady( + Dev_t dev, + uint8_t *p_is_data_ready) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t temp; + uint8_t int_pol; + + status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO_HV_MUX__CTRL, &temp); + temp = temp & (uint8_t)0x10; + temp = temp >> 4; + + if (temp == (uint8_t)1) + { + int_pol = (uint8_t)0; + } + else + { + int_pol = (uint8_t)1; + } + + status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO__TIO_HV_STATUS, &temp); + + if ((temp & (uint8_t)1) == int_pol) + { + *p_is_data_ready = (uint8_t)1; + } + else + { + *p_is_data_ready = (uint8_t)0; + } + + return status; +} + +VL53L4ED_Error VL53L4ED_SetRangeTiming( + Dev_t dev, + uint32_t timing_budget_ms, + uint32_t inter_measurement_ms) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t clock_pll, osc_frequency, ms_byte; + uint32_t macro_period_us = 0, timing_budget_us = 0, ls_byte, tmp; + float_t inter_measurement_factor = (float_t)1.055; + + status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); + if(osc_frequency != (uint16_t)0) + { + timing_budget_us = timing_budget_ms*(uint32_t)1000; + macro_period_us = (uint32_t)((uint32_t)2304 * + ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; + } + else + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + + /* Timing budget check validity */ + if ((timing_budget_ms < (uint32_t)10) + || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) + { + status |= VL53L4ED_ERROR_INVALID_ARGUMENT; + } + /* Sensor runs in continuous mode */ + else if(inter_measurement_ms == (uint32_t)0) + { + status |= VL53L4ED_WrDWord(dev,VL53L4ED_INTERMEASUREMENT_MS, 0); + timing_budget_us -= (uint32_t)2500; + } + /* Sensor runs in autonomous low power mode */ + else if(inter_measurement_ms > timing_budget_ms) + { + status |= VL53L4ED_RdWord(dev, + VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + clock_pll = clock_pll & (uint16_t)0x3FF; + inter_measurement_factor = inter_measurement_factor + * (float_t)inter_measurement_ms + * (float_t)clock_pll; + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, + (uint32_t)inter_measurement_factor); + + timing_budget_us -= (uint32_t)4300; + timing_budget_us /= (uint32_t)2; + + } + /* Invalid case */ + else + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + + if(status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) + { + ms_byte = 0; + timing_budget_us = timing_budget_us << 12; + tmp = macro_period_us*(uint32_t)16; + ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) + - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + + (uint16_t) (ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A,ms_byte); + + ms_byte = 0; + tmp = macro_period_us*(uint32_t)12; + ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) + - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + + (uint16_t) (ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B,ms_byte); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_GetRangeTiming( + Dev_t dev, + uint32_t *p_timing_budget_ms, + uint32_t *p_inter_measurement_ms) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t osc_frequency = 1, range_config_macrop_high, clock_pll = 1; + uint32_t tmp, ls_byte, ms_byte, macro_period_us; + float_t clock_pll_factor = (float_t)1.065; + + /* Get InterMeasurement */ + status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); + status |= VL53L4ED_RdWord(dev, + VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + clock_pll = clock_pll & (uint16_t)0x3FF; + clock_pll_factor = clock_pll_factor * (float_t)clock_pll; + clock_pll = (uint16_t)clock_pll_factor; + *p_inter_measurement_ms = (uint16_t)(tmp/(uint32_t)clock_pll); + + /* Get TimingBudget */ + status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, + &range_config_macrop_high); + + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 + / (uint32_t)osc_frequency)) >> 6; + ls_byte = (range_config_macrop_high & (uint32_t)0x00FF) << 4; + ms_byte = (range_config_macrop_high & (uint32_t)0xFF00) >> 8; + ms_byte = (uint32_t)0x04 - (ms_byte - (uint32_t)1) - (uint32_t)1; + + macro_period_us = macro_period_us * (uint32_t)16; + *p_timing_budget_ms = (((ls_byte + (uint32_t)1)*(macro_period_us>> 6)) + - ((macro_period_us>> 6)>>1)) >> 12; + + if(ms_byte < (uint8_t)12) + { + *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms + >> (uint8_t)ms_byte); + } + + /* Mode continuous */ + if(tmp == (uint32_t)0) + { + *p_timing_budget_ms += (uint32_t)2500; + } + /* Mode autonomous */ + else + { + *p_timing_budget_ms *= (uint32_t)2; + *p_timing_budget_ms += (uint32_t)4300; + } + + *p_timing_budget_ms = *p_timing_budget_ms/(uint32_t)1000; + + return status; +} + +VL53L4ED_Error VL53L4ED_GetResult( + Dev_t dev, + VL53L4ED_ResultsData_t *p_result) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp_16; + uint8_t temp_8; + uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, + 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, + 255, 255, 11, 12 }; + + status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, + &temp_8); + temp_8 = temp_8 & (uint8_t)0x1F; + if (temp_8 < (uint8_t)24) + { + temp_8 = status_rtn[temp_8]; + } + p_result->range_status = temp_8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, + &temp_16); + p_result->number_of_spad = temp_16 / (uint16_t) 256; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, + &temp_16); + p_result->signal_rate_kcps = temp_16 * (uint16_t) 8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, + &temp_16); + p_result->ambient_rate_kcps = temp_16 * (uint16_t) 8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, + &temp_16); + p_result->sigma_mm = temp_16 / (uint16_t) 4; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, + &temp_16); + p_result->distance_mm = temp_16; + + p_result->signal_per_spad_kcps = p_result->signal_rate_kcps + /p_result->number_of_spad; + p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps + /p_result->number_of_spad; + + return status; +} + +VL53L4ED_Error VL53L4ED_SetOffset( + Dev_t dev, + int16_t OffsetValueInMm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp; + + temp = (uint16_t)((uint16_t)OffsetValueInMm*(uint16_t)4); + + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, temp); + status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, (uint8_t)0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, (uint8_t)0x0); + return status; +} + +VL53L4ED_Error VL53L4ED_GetOffset( + Dev_t dev, + int16_t *p_offset) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp; + + status |= VL53L4ED_RdWord(dev,VL53L4ED_RANGE_OFFSET_MM, &temp); + + temp = temp<<3; + temp = temp>>5; + *p_offset = (int16_t)(temp); + + if(*p_offset > 1024) + { + *p_offset = *p_offset - 2048; + } + + return status; +} + +VL53L4ED_Error VL53L4ED_SetXtalk( + Dev_t dev, + uint16_t XtalkValueKcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, + (XtalkValueKcps<<9)); + + return status; +} + +VL53L4ED_Error VL53L4ED_GetXtalk( + Dev_t dev, + uint16_t *p_xtalk_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + float_t tmp_xtalk; + + status |= VL53L4ED_RdWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); + + tmp_xtalk = (float_t)*p_xtalk_kcps / (float_t)512.0; + *p_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); + + return status; +} + +VL53L4ED_Error VL53L4ED_SetDetectionThresholds( + Dev_t dev, + uint16_t distance_low_mm, + uint16_t distance_high_mm, + uint8_t window) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM__INTERRUPT, window); + status |= VL53L4ED_WrWord(dev, VL53L4ED_THRESH_HIGH, distance_high_mm); + status |= VL53L4ED_WrWord(dev, VL53L4ED_THRESH_LOW, distance_low_mm); + return status; +} + +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, + uint16_t *p_distance_low_mm, + uint16_t *p_distance_high_mm, + uint8_t *p_window) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH,p_distance_high_mm); + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_LOW, p_distance_low_mm); + status |= VL53L4ED_RdByte(dev, VL53L4ED_SYSTEM__INTERRUPT, p_window); + *p_window = (*p_window & (uint8_t)0x7); + + return status; +} + +VL53L4ED_Error VL53L4ED_SetSignalThreshold( + Dev_t dev, + uint16_t signal_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrWord(dev, + VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS,signal_kcps>>3); + return status; +} + +VL53L4ED_Error VL53L4ED_GetSignalThreshold( + Dev_t dev, + uint16_t *p_signal_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t tmp = 0; + + status |= VL53L4ED_RdWord(dev, + VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); + *p_signal_kcps = tmp <<3; + + return status; +} + +VL53L4ED_Error VL53L4ED_SetSigmaThreshold( + Dev_t dev, + uint16_t sigma_mm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + if(sigma_mm>(uint16_t)((uint16_t)0xFFFF>>2)) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + status |= VL53L4ED_WrWord(dev, + VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm<<2); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_GetSigmaThreshold( + Dev_t dev, + uint16_t *p_sigma_mm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status += VL53L4ED_RdWord(dev, + VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); + *p_sigma_mm = *p_sigma_mm >> 2; + + return status; +} + +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t tmp = 0, continue_loop = 1; + uint16_t i = 0; + + status |= VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); + status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0x92); + status |= VL53L4ED_StartRanging(dev); + + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_ClearInterrupt(dev); + status |= VL53L4ED_StopRanging(dev); + + status += VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); + status += VL53L4ED_WrByte(dev, 0x0B, 0); + return status; +} diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h new file mode 100644 index 000000000..df55ef3d0 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h @@ -0,0 +1,405 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#ifndef VL53L4ED_API_H_ +#define VL53L4ED_API_H_ + +#include "platform.h" + +/** + * @brief Driver version + */ + +#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 +#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 + +/** + * @brief Driver error type + */ + +typedef uint8_t VL53L4ED_Error; + +#define VL53L4ED_ERROR_NONE ((uint8_t)0U) +#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) +#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) +#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) + + +/** + * @brief Inner Macro for API. Not for user, only for development. + */ + +#define VL53L4ED_SOFT_RESET ((uint16_t)0x0000)) +#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) +#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) +#define VL53L4ED_XTALK_PLANE_OFFSET_KCPS ((uint16_t)0x0016) +#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) +#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) +#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) +#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) +#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) +#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) +#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) +#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) +#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) +#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) +#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) +#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) +#define VL53L4ED_INTERMEASUREMENT_MS ((uint16_t)0x006C) +#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) +#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) +#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) +#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) +#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) +#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) +#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) +#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) +#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) +#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) + + +#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) +#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) +#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) + +/** + * @brief defines Software Version + */ + +typedef struct { + uint8_t major; /*!< major number */ + uint8_t minor; /*!< minor number */ + uint8_t build; /*!< build number */ + uint32_t revision; /*!< revision number */ +} VL53L4ED_Version_t; + +/** + * @brief Packed reading results type + */ + +typedef struct { + + /* Status of measurements. If the status is equal to 0, the data are valid*/ + uint8_t range_status; + /* Measured distance in millimeters */ + uint16_t distance_mm; + /* Ambient noise in kcps */ + uint16_t ambient_rate_kcps; + /* Ambient noise in kcps/SPAD */ + uint16_t ambient_per_spad_kcps; + /* Measured signal of the target in kcps */ + uint16_t signal_rate_kcps; + /* Measured signal of the target in kcps/SPAD */ + uint16_t signal_per_spad_kcps; + /* Number of SPADs enabled */ + uint16_t number_of_spad; + /* Estimated measurements std deviation in mm */ + uint16_t sigma_mm; +} VL53L4ED_ResultsData_t; + +/** + * @brief This function programs the software driver version. + * @param (VL53L4ED_Version_t) pVersion : Pointer of structure, containing the + * software version. + * @return (VL53L4ED_ERROR) status : 0 if SW version is OK. + */ + +VL53L4ED_Error VL53L4ED_GetSWVersion( + VL53L4ED_Version_t *pVersion); + + +/** + * @brief This function sets a new I2C address to a sensor. It can be used for + * example when multiple sensors share the same I2C bus. + * @param (Dev_t) dev : Device instance to update. + * @param (uint8_t) new_address : New I2C address. + * @return (VL53L4ED_ERROR) status : 0 if I2C address has been correctly + * programmed. + */ + +VL53L4ED_Error VL53L4ED_SetI2CAddress( + Dev_t dev, + uint8_t new_address); + +/** + * @brief This function is used to get the sensor id of VL53L4ED. The sensor id + * should be 0xEBAA. + * @param (Dev_t) dev : Device instance. + * @param (uint16_t) *p_id : Sensor id. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetSensorId( + Dev_t dev, + uint16_t *p_id); + +/** + * @brief This function is used to initialize the sensor. + * @param (Dev_t) dev : Device instance to initialize. + * @return (VL53L4ED_ERROR) status : 0 if init is OK. + */ + +VL53L4ED_Error VL53L4ED_SensorInit( + Dev_t dev); + +/** + * @brief This function clears the interrupt. It needs to be called after a + * ranging data reading to arm the interrupt for the next data ready event. + * @param (Dev_t) dev : Device instance. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_ClearInterrupt( + Dev_t dev); + +/** + * @brief This function starts a ranging session. The ranging operation is + * continuous. The clear interrupt has to be done after each get data to allow + * the interrupt to raise when the next data is ready. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_StartRanging( + Dev_t dev); + +/** + * @brief This function stops the ranging in progress. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_StopRanging( + Dev_t dev); + +/** + * @brief This function check if a new data is available by polling a dedicated + * register. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint8_t) *p_is_data_ready : Pointer containing a flag to know if a + * data is ready : 0 = no data ready, 1 = data ready. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_CheckForDataReady( + Dev_t dev, + uint8_t *p_is_data_ready); + +/** + * @brief This function sets new range timing. Timing are composed of + * TimingBudget and InterMeasurement. TimingBudget represents the timing during + * VCSEL enabled, and InterMeasurement the time between two measurements. + * The sensor can have different ranging mode depending of the configuration, + * please refer to the user manual for more information. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint32_t) timing_budget_ms : New timing budget in ms. Value can be + * between 10ms and 200ms. Default is 50ms. + * @param (uint32_t) inter_measurement_ms : New inter-measurement in ms. If the + * value is equal to 0, the ranging period is defined by the timing budget. + * Otherwise, inter-measurement must be > timing budget. When all the timing + * budget is consumed, the device goes in low power mode until inter-measurement + * is done. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetRangeTiming( + Dev_t dev, + uint32_t timing_budget_ms, + uint32_t inter_measurement_ms); + +/** + * @brief This function gets the current range timing. Timing are composed of + * TimingBudget and InterMeasurement. TimingBudget represents the timing during + * VCSEL enabled, and InterMeasurement the time between two measurements. + * The sensor can have different ranging mode depending of the configuration, + * please refer to the user manual for more information. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint32_t) *p_timing_budget_ms : Pointer containing the current + * timing budget in ms. + * @param (uint32_t) *p_inter_measurement_ms : Pointer containing the current + * inter-measurement in ms. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetRangeTiming( + Dev_t dev, + uint32_t *p_timing_budget_ms, + uint32_t *p_inter_measurement_ms); + +/** + * @brief This function gets the results reported by the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (VL53L4ED_ResultsData_t) *pResult : Pointer of structure, filled with the + * ranging results. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetResult(Dev_t dev, VL53L4ED_ResultsData_t *pResult); + +/** + * @brief This function sets a new offset correction in mm. Offset corresponds + * to the difference in millimeters between real distance and measured distance. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) OffsetValueInMm : Offset value in millimeters. The minimum + * value is -1024mm and maximum is 1023mm. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetOffset(Dev_t dev, int16_t OffsetValueInMm); + +/** + * @brief This function gets the current offset correction in mm. Offset + * corresponds to the difference in millimeters between real distance and + * measured distance. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) OffsetValueInMm : Offset value in millimeters. The minimum + * value is -1024mm and maximum is 1023mm. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetOffset(Dev_t dev, int16_t *Offset); + +/** + * @brief This function sets a new Xtalk value in kcps. Xtalk represents the + * correction to apply to the sensor when a protective coverglass is placed + * at the top of the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) XtalkValueKcps : New xtalk value in kcps. The default + * value is 0 kcps (no coverglass). Minimum is 0 kcps , and maximum is 128 + * kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetXtalk(Dev_t dev, uint16_t XtalkValueKcps); + +/** + * @brief This function gets the current Xtalk value in kcps. Xtalk represents + * the correction to apply to the sensor when a protective coverglass is placed + * at the top of the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) p_xtalk_kcps : Pointer of current xtalk value in kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps); + +/** + * @brief This function sets new detection thresholds. The detection + * thresholds can be programmed to generate an interrupt on pin 7 (GPIO1), only + * when a condition on distance is reach. Example: + * VL53L4ED_SetDistanceThreshold(dev,100,300,0): Below 100 mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,1): Above 300 mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,2): Below 100mm or above 300mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,3): Above 100mm or below 300mm + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) distance_low_mm : Low distance threshold in millimeters. + * @param (uint16_t) distance_high_mm : High distance threshold in millimeters. + * @param (uint8_t) window : Interrupt windows (0=below low threshold; + * 1=above high threshold; 2=out of low/high windows; 3=in low/high windows) + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, + uint16_t distance_low_mm, + uint16_t distance_high_mm, + uint8_t window); + + +/** + * @brief This function gets the current detection thresholds. The detection + * thresholds can be programmed to generate an interrupt on pin 7 (GPIO1), only + * when a condition on distance is reach. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_distance_low_mm : Pointer of low distance threshold in + * millimeters. + * @param (uint16_t) *p_distance_high_mm : Pointer of high distance threshold in + * millimeters. + * @param (uint8_t) *p_window : Interrupt windows (0=below low threshold; + * 1=above high threshold; 2=out of low/high windows; 3=in low/high windows) + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, + uint16_t *p_distance_low_mm, + uint16_t *p_distance_high_mm, + uint8_t *p_window); + +/** + * @brief This function sets a new signal threshold in kcps. If a + * target has a lower signal as the programmed value, the result status in + * structure 'VL53L4ED_ResultsData_t' will be equal to 2. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) signal_kcps : New signal threshold in kcps. The default + * value is 1024 kcps. Minimum is 0 kcps (no threshold), and maximum is 16384 + * kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps); + +/** + * @brief This function returns the current signal threshold in kcps. If a + * target has a lower signal as the programmed value, the result status in + * structure 'VL53L4ED_ResultsData_t' will be equal to 2. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_signal_kcps : Pointer of signal threshold in kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, + uint16_t *p_signal_kcps); + +/** + * @brief This function programs a new sigma threshold. The sigma corresponds to + * the standard deviation of the returned pulse. If the computed sigma is above + * the programmed value, the result status in structure 'VL53L4ED_ResultsData_t' + * will be equal to 1. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) sigma_mm : New sigma threshold in mm. The default value is + * 15mm. Minimum is 0mm (not threshold), and maximum is 16383mm. + * @return (VL53L4ED_ERROR) status : 0 if programming is or 255 if value is too + * high. + */ + +VL53L4ED_Error VL53L4ED_SetSigmaThreshold( + Dev_t dev, + uint16_t sigma_mm); + +/** + * @brief This function gets the current sigma threshold. The sigma corresponds + * to the standard deviation of the returned pulse. If the computed sigma is + * above the programmed value, the result status in structure + * 'VL53L4ED_ResultsData_t' will be equal to 1. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_sigma_mm : Current sigma threshold in mm. + * @return (VL53L4ED_ERROR) status : 0 if programming is OK. + */ + +VL53L4ED_Error VL53L4ED_GetSigmaThreshold( + Dev_t dev, + uint16_t *p_sigma_mm); + +/** + * @brief This function can be called when the temperature might have changed by + * more than 8 degrees Celsius. The function can only be used if the sensor is + * not ranging, otherwise, the ranging needs to be stopped using function + * 'VL53L4ED_StopRanging()'. After calling this function, the ranging can + * restart normally. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if update is OK. + */ + +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev); + +#endif //VL53L4ED_API_H_ diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c new file mode 100644 index 000000000..e3d95c223 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c @@ -0,0 +1,242 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_calibration.c + * @brief Calibration functions implementation + */ + +#include +#include "VL53L4ED_api.h" +#include "VL53L4ED_calibration.h" + +VL53L4ED_Error VL53L4ED_CalibrateOffset( + Dev_t dev, + int16_t TargetDistInMm, + int16_t *p_measured_offset_mm, + int16_t nb_samples) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t i, tmp, continue_loop; + uint16_t j, tmpOff; + int16_t AvgDistance = 0; + VL53L4ED_ResultsData_t results; + + if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) + || ((TargetDistInMm < (int16_t)10) + || (TargetDistInMm > (int16_t)1000))) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, 0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, 0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, 0x0); + + /* Device heat loop (10 samples) */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)10; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + } + status |= VL53L4ED_StopRanging(dev); + + /* Device ranging */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)nb_samples; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + AvgDistance += (int16_t)results.distance_mm; + } + + status |= VL53L4ED_StopRanging(dev); + AvgDistance = AvgDistance / nb_samples; + *p_measured_offset_mm = (int16_t)TargetDistInMm - AvgDistance; + tmpOff = (uint16_t) *p_measured_offset_mm * (uint16_t)4; + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, tmpOff); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_CalibrateXtalk( + Dev_t dev, + int16_t TargetDistInMm, + uint16_t *p_measured_xtalk_kcps, + int16_t nb_samples) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t i, tmp, continue_loop; + float_t AverageSignal = (float_t)0.0; + float_t AvgDistance = (float_t)0.0; + float_t AverageSpadNb = (float_t)0.0; + float_t TargetDistance = (float_t)TargetDistInMm; + float_t tmp_xtalk, CounterNbSamples = (float_t)0.0; + VL53L4ED_ResultsData_t results; + + uint16_t calXtalk, j; + + *p_measured_xtalk_kcps = 0; + if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) + || ((TargetDistInMm < (int16_t)10) + || (TargetDistInMm > (int16_t)5000))) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + /* Disable Xtalk compensation */ + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); + + /* Device heat loop (10 samples) */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)10; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + } + status |= VL53L4ED_StopRanging(dev); + + /* Device ranging loop */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)nb_samples; i++) + { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + + /* Discard invalid measurements and first frame */ + if (results.range_status == (uint8_t)0 + && i > (uint8_t)0) + { + AvgDistance += (float_t)results.distance_mm; + AverageSpadNb += (float_t)results.number_of_spad; + AverageSignal += (float_t)results.signal_rate_kcps; + CounterNbSamples++; + } + } + status |= VL53L4ED_StopRanging(dev); + + if (CounterNbSamples == 0) + { + status = VL53L4ED_ERROR_XTALK_FAILED; + } + else + { + AvgDistance /= CounterNbSamples; + AverageSpadNb /= CounterNbSamples; + AverageSignal /= CounterNbSamples; + + tmp_xtalk = (float_t)1.0 - (AvgDistance/TargetDistance); + tmp_xtalk *= (AverageSignal/AverageSpadNb); + + /* 127kcps is the max Xtalk value (65536/512) */ + if(tmp_xtalk > (uint16_t)127) + { + status = VL53L4ED_ERROR_XTALK_FAILED; + } + else + { + *p_measured_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); + + /* Send data to firmware */ + calXtalk = (uint16_t)(tmp_xtalk * (float_t)512.0); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); + } + } + } + + return status; +} diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h new file mode 100644 index 000000000..c953d0239 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h @@ -0,0 +1,73 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_calibration.h + * @brief Calibration Functions definition + */ + +#ifndef VL53L4ED_CALIBRATION_H_ +#define VL53L4ED_CALIBRATION_H_ + +#include "platform.h" + +/** + * @brief This function can be used to perform an offset calibration. Offset + * corresponds to the difference in millimeters between real distance and + * measured distance. ST recommend to perform offset at 100m, on a grey17% + * reflective target, but any other distance and reflectance can be used. + * The function returns the offset value found and programs the offset + * compensation into the device. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) TargetDistInMm : Real distance between the sensor and the + * target in millimeters. ST recommend 100mm. Min distance is 10mm and max is + * 1000mm. + * @param (int16_t) nb_samples : Number of samples (between 5 and 255). A higher + * number of samples increases the accuracy, but it also takes more time. ST + * recommend to use at least 10 samples. + * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g + * invalid nb of samples). + */ + +VL53L4ED_Error VL53L4ED_CalibrateOffset( + Dev_t dev, + int16_t TargetDistInMm, + int16_t *p_measured_offset_mm, + int16_t nb_samples); + + +/** + * @brief This function can be used to perform a Xtalk calibration. Xtalk + * represents the correction to apply to the sensor when a protective coverglass + * is placed at the top of the sensor. The distance for calibration depends of + * the coverglass, it needs to be characterized. Please refer to the User Manual + * for more information. + * The function returns the Xtalk value found and programs the Xtalk + * compensation into the device. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param uint16_t) TargetDistInMm : Real distance between the sensor and the + * target in millimeters. This distance needs to be characterized, as described + * into the User Manual. + * @param (int16_t) nb_samples : Number of samples (between 5 and 255). A higher + * number of samples increases the accuracy, but it also takes more time. ST + * recommend to use at least 10 samples. + * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g + * invalid nb of samples). + */ + +VL53L4ED_Error VL53L4ED_CalibrateXtalk( + Dev_t dev, + int16_t TargetDistInMm, + uint16_t *p_measured_xtalk_kcps, + int16_t nb_samples); + +#endif //VL53L4ED_CALIBRATION_H_ diff --git a/SAMM/Core/Extras/VL53L4ED/platform.c b/SAMM/Core/Extras/VL53L4ED/platform.c new file mode 100644 index 000000000..ba3d6f1a6 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/platform.c @@ -0,0 +1,113 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#include "platform.h" +#include "main.h" +#include "i2c.h" + +extern I2C_HandleTypeDef hi2c1; + + +/* +Im legit just using the example stm provides but instead of filling everything out im only copying the platform.c implemintation +for the vl53l4ed sensor. + + +*/ + +uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[4]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 4, 100); + *value = ((data_read[0] << 24) | (data_read[1]<<16) | + (data_read[2]<<8)| (data_read[3])); + return status; +} + +uint8_t VL53L4ED_RdWord(uint16_t dev, uint16_t RegisterAdress, uint16_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[2]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 2, 100); + *value = (data_read[0] << 8) | (data_read[1]); + return status; +} + +uint8_t VL53L4ED_RdByte(uint16_t dev, uint16_t RegisterAdress, uint8_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[1]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 1, 100); + *value = data_read[0]; + return status; +} + +uint8_t VL53L4ED_WrByte(uint16_t dev, uint16_t RegisterAdress, uint8_t value) +{ + uint8_t data_write[3]; + uint8_t status = 0; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 3, 100); + return status; +} + +uint8_t VL53L4ED_WrWord(uint16_t dev, uint16_t RegisterAdress, uint16_t value) +{ + uint8_t data_write[4]; + uint8_t status = 0; + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = (value >> 8) & 0xFF; + data_write[3] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 4, 100); + return status; +} + +uint8_t VL53L4ED_WrDWord(uint16_t dev, uint16_t RegisterAdress, uint32_t value) +{ + uint8_t data_write[6]; + uint8_t status = 0; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = (value >> 24) & 0xFF; + data_write[3] = (value >> 16) & 0xFF; + data_write[4] = (value >> 8) & 0xFF; + data_write[5] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 6, 100); + return status; +} + +uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t time_ms) +{ + HAL_Delay(time_ms); + return 0; +} diff --git a/SAMM/Core/Extras/VL53L4ED/platform.h b/SAMM/Core/Extras/VL53L4ED/platform.h new file mode 100644 index 000000000..ee27e91f2 --- /dev/null +++ b/SAMM/Core/Extras/VL53L4ED/platform.h @@ -0,0 +1,80 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#ifndef _PLATFORM_H_ +#define _PLATFORM_H_ +#pragma once + +#include +#include + +/** +* VL53L4ED device instance. +*/ + +typedef uint16_t Dev_t; + +/** + * @brief Error instance. + */ +typedef uint8_t VL53L4ED_Error; + +/** + * @brief If the macro below is defined, the device will be programmed to run + * with I2C Fast Mode Plus (up to 1MHz). Otherwise, default max value is 400kHz. + */ + +//#define VL53L4ED_I2C_FAST_MODE_PLUS + + +/** + * @brief Read 32 bits through I2C. + */ + +uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t registerAddr, uint32_t *value); +/** + * @brief Read 16 bits through I2C. + */ + +uint8_t VL53L4ED_RdWord(Dev_t dev, uint16_t registerAddr, uint16_t *value); + +/** + * @brief Read 8 bits through I2C. + */ + +uint8_t VL53L4ED_RdByte(Dev_t dev, uint16_t registerAddr, uint8_t *value); + +/** + * @brief Write 8 bits through I2C. + */ + +uint8_t VL53L4ED_WrByte(Dev_t dev, uint16_t registerAddr, uint8_t value); + +/** + * @brief Write 16 bits through I2C. + */ + +uint8_t VL53L4ED_WrWord(Dev_t dev, uint16_t RegisterAdress, uint16_t value); + +/** + * @brief Write 32 bits through I2C. + */ + +uint8_t VL53L4ED_WrDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t value); + +/** + * @brief Wait during N milliseconds. + */ + +uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t TimeMs); + +#endif // _PLATFORM_H_ \ No newline at end of file diff --git a/SAMM/Core/Extras/extra.c b/SAMM/Core/Extras/extra.c new file mode 100644 index 000000000..3611c8904 --- /dev/null +++ b/SAMM/Core/Extras/extra.c @@ -0,0 +1,58 @@ +#include "extra.h" +#include "main.h" +#include "i2c.h" +#include + + +void MLX90640_I2CInit(void) +{ + MX_I2C2_Init(); + return; +} + +int MLX90640_I2CGeneralReset(void) +{ + uint8_t data = 0x06; + return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data , 1, 1000); +} + + +int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +{ + //HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); + //HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); + /* + HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size) + */ + //return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 10000); +} + +int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) +{ + // union + // { + // uint8_t bytes[2]; + // uint16_t data; + // }extra = {.data = data}; + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); + //return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); +} + +void MLX90640_I2CFreqSet(int freq) +{ + return; +} + +uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +{ + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 500); +} + +uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data) +{ + + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *) &data, 2, 500); +} + diff --git a/SAMM/Core/Extras/extra.h b/SAMM/Core/Extras/extra.h new file mode 100644 index 000000000..2b820f432 --- /dev/null +++ b/SAMM/Core/Extras/extra.h @@ -0,0 +1,20 @@ +#ifndef __EXTRA_H__ +#define __EXTRA_H__ +#include +/* +This is for some xtra fucntions that would be needed later on + + +*/ + +void MLX90640_I2CInit(void); +int MLX90640_I2CGeneralReset(void); +int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +int MLX90640_I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +void MLX90640_I2CFreqSet(int freq); + + +uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); + +#endif /* __EXTRA_H__ */ \ No newline at end of file diff --git a/SAMM/Core/Inc/adc.h b/SAMM/Core/Inc/adc.h new file mode 100644 index 000000000..5692f3846 --- /dev/null +++ b/SAMM/Core/Inc/adc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.h + * @brief This file contains all the function prototypes for + * the adc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __ADC_H__ +#define __ADC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern ADC_HandleTypeDef hadc1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_ADC1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __ADC_H__ */ + diff --git a/SAMM/Core/Inc/crc.h b/SAMM/Core/Inc/crc.h new file mode 100644 index 000000000..836835d02 --- /dev/null +++ b/SAMM/Core/Inc/crc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file crc.h + * @brief This file contains all the function prototypes for + * the crc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __CRC_H__ +#define __CRC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern CRC_HandleTypeDef hcrc; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_CRC_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __CRC_H__ */ + diff --git a/SAMM/Core/Inc/dma.h b/SAMM/Core/Inc/dma.h new file mode 100644 index 000000000..f3b1a09f9 --- /dev/null +++ b/SAMM/Core/Inc/dma.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.h + * @brief This file contains all the function prototypes for + * the dma.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __DMA_H__ +#define __DMA_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* DMA memory to memory transfer handles -------------------------------------*/ + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_DMA_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __DMA_H__ */ + diff --git a/SAMM/Core/Inc/fdcan.h b/SAMM/Core/Inc/fdcan.h new file mode 100644 index 000000000..9289ea3a6 --- /dev/null +++ b/SAMM/Core/Inc/fdcan.h @@ -0,0 +1,55 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file fdcan.h + * @brief This file contains all the function prototypes for + * the fdcan.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __FDCAN_H__ +#define __FDCAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern FDCAN_HandleTypeDef hfdcan1; + +extern FDCAN_HandleTypeDef hfdcan2; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_FDCAN1_Init(void); +void MX_FDCAN2_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __FDCAN_H__ */ + diff --git a/SAMM/Core/Inc/gpio.h b/SAMM/Core/Inc/gpio.h new file mode 100644 index 000000000..708bac71d --- /dev/null +++ b/SAMM/Core/Inc/gpio.h @@ -0,0 +1,49 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __GPIO_H__ +#define __GPIO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_GPIO_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ GPIO_H__ */ + diff --git a/SAMM/Core/Inc/i2c.h b/SAMM/Core/Inc/i2c.h new file mode 100644 index 000000000..84ed75d2e --- /dev/null +++ b/SAMM/Core/Inc/i2c.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.h + * @brief This file contains all the function prototypes for + * the i2c.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __I2C_H__ +#define __I2C_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern I2C_HandleTypeDef hi2c1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_I2C1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __I2C_H__ */ + diff --git a/SAMM/Core/Inc/main.h b/SAMM/Core/Inc/main.h new file mode 100644 index 000000000..17a2fe760 --- /dev/null +++ b/SAMM/Core/Inc/main.h @@ -0,0 +1,80 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __MAIN_H +#define __MAIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32g4xx_hal.h" + +#include "stm32g4xx_ll_rcc.h" +#include "stm32g4xx_ll_bus.h" +#include "stm32g4xx_ll_crs.h" +#include "stm32g4xx_ll_system.h" +#include "stm32g4xx_ll_exti.h" +#include "stm32g4xx_ll_cortex.h" +#include "stm32g4xx_ll_utils.h" +#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_dma.h" +#include "stm32g4xx_ll_gpio.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +/* Private defines -----------------------------------------------------------*/ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MAIN_H */ diff --git a/SAMM/Core/Inc/spi.h b/SAMM/Core/Inc/spi.h new file mode 100644 index 000000000..bb37a07dc --- /dev/null +++ b/SAMM/Core/Inc/spi.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file spi.h + * @brief This file contains all the function prototypes for + * the spi.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __SPI_H__ +#define __SPI_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern SPI_HandleTypeDef hspi3; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_SPI3_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __SPI_H__ */ + diff --git a/SAMM/Core/Inc/stm32_assert.h b/SAMM/Core/Inc/stm32_assert.h new file mode 100644 index 000000000..61631c41e --- /dev/null +++ b/SAMM/Core/Inc/stm32_assert.h @@ -0,0 +1,53 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32_assert.h + * @author MCD Application Team + * @brief STM32 assert file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 __STM32_ASSERT_H +#define __STM32_ASSERT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_ASSERT_H */ + diff --git a/SAMM/Core/Inc/stm32g4xx_hal_conf.h b/SAMM/Core/Inc/stm32g4xx_hal_conf.h new file mode 100644 index 000000000..4087e183c --- /dev/null +++ b/SAMM/Core/Inc/stm32g4xx_hal_conf.h @@ -0,0 +1,380 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_conf.h + * @author MCD Application Team + * @brief HAL configuration file + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 STM32G4xx_HAL_CONF_H +#define STM32G4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ + +#define HAL_MODULE_ENABLED + + /*#define HAL_ADC_MODULE_ENABLED */ +/*#define HAL_COMP_MODULE_ENABLED */ +/*#define HAL_CORDIC_MODULE_ENABLED */ +#define HAL_CRC_MODULE_ENABLED +/*#define HAL_CRYP_MODULE_ENABLED */ +/*#define HAL_DAC_MODULE_ENABLED */ +#define HAL_FDCAN_MODULE_ENABLED +/*#define HAL_FMAC_MODULE_ENABLED */ +/*#define HAL_HRTIM_MODULE_ENABLED */ +/*#define HAL_IRDA_MODULE_ENABLED */ +/*#define HAL_IWDG_MODULE_ENABLED */ +#define HAL_I2C_MODULE_ENABLED +/*#define HAL_I2S_MODULE_ENABLED */ +/*#define HAL_LPTIM_MODULE_ENABLED */ +/*#define HAL_NAND_MODULE_ENABLED */ +/*#define HAL_NOR_MODULE_ENABLED */ +/*#define HAL_OPAMP_MODULE_ENABLED */ +/*#define HAL_PCD_MODULE_ENABLED */ +/*#define HAL_QSPI_MODULE_ENABLED */ +/*#define HAL_RNG_MODULE_ENABLED */ +/*#define HAL_RTC_MODULE_ENABLED */ +/*#define HAL_SAI_MODULE_ENABLED */ +/*#define HAL_SMARTCARD_MODULE_ENABLED */ +/*#define HAL_SMBUS_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED +/*#define HAL_SRAM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ +/*#define HAL_USART_MODULE_ENABLED */ +/*#define HAL_WWDG_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED + +/* ########################## Register Callbacks selection ############################## */ +/** + * @brief This is the list of modules where register callback can be used + */ +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U +#define USE_HAL_COMP_REGISTER_CALLBACKS 0U +#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U +#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U +#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U +#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U +#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U +#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U +#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_NAND_REGISTER_CALLBACKS 0U +#define USE_HAL_NOR_REGISTER_CALLBACKS 0U +#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U +#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U +#define USE_HAL_RNG_REGISTER_CALLBACKS 0U +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U +#define USE_HAL_SAI_REGISTER_CALLBACKS 0U +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U +#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U +#define USE_HAL_UART_REGISTER_CALLBACKS 0U +#define USE_HAL_USART_REGISTER_CALLBACKS 0U +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U + +/* ########################## Oscillator Values adaptation ####################*/ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. + * This internal oscillator is mainly dedicated to provide a high precision clock to + * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. + * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency + * which is subject to manufacturing process variations. + */ +#if !defined (HSI48_VALUE) + #define HSI48_VALUE (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. + The real value my vary depending on manufacturing process variations.*/ +#endif /* HSI48_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) +/*!< Value of the Internal Low Speed oscillator in Hz +The real value may vary depending on the variations in voltage and temperature.*/ +#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ +/** + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined (LSE_VALUE) +#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S and SAI peripherals + * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ + +#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 0U +#define INSTRUCTION_CACHE_ENABLE 1U +#define DATA_CACHE_ENABLE 1U + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1U */ + +/* ################## SPI peripheral configuration ########################## */ + +/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver + * Activated: CRC code is present inside driver + * Deactivated: CRC code cleaned from driver + */ + +#define USE_SPI_CRC 0U + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED +#include "stm32g4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED +#include "stm32g4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED +#include "stm32g4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED +#include "stm32g4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED +#include "stm32g4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED +#include "stm32g4xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CORDIC_MODULE_ENABLED +#include "stm32g4xx_hal_cordic.h" +#endif /* HAL_CORDIC_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED +#include "stm32g4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED +#include "stm32g4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED +#include "stm32g4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_EXTI_MODULE_ENABLED +#include "stm32g4xx_hal_exti.h" +#endif /* HAL_EXTI_MODULE_ENABLED */ + +#ifdef HAL_FDCAN_MODULE_ENABLED +#include "stm32g4xx_hal_fdcan.h" +#endif /* HAL_FDCAN_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED +#include "stm32g4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_FMAC_MODULE_ENABLED +#include "stm32g4xx_hal_fmac.h" +#endif /* HAL_FMAC_MODULE_ENABLED */ + +#ifdef HAL_HRTIM_MODULE_ENABLED +#include "stm32g4xx_hal_hrtim.h" +#endif /* HAL_HRTIM_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED +#include "stm32g4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED +#include "stm32g4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED +#include "stm32g4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED +#include "stm32g4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_LPTIM_MODULE_ENABLED +#include "stm32g4xx_hal_lptim.h" +#endif /* HAL_LPTIM_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED +#include "stm32g4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED +#include "stm32g4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_OPAMP_MODULE_ENABLED +#include "stm32g4xx_hal_opamp.h" +#endif /* HAL_OPAMP_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED +#include "stm32g4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED +#include "stm32g4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_QSPI_MODULE_ENABLED +#include "stm32g4xx_hal_qspi.h" +#endif /* HAL_QSPI_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED +#include "stm32g4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED +#include "stm32g4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED +#include "stm32g4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED +#include "stm32g4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED +#include "stm32g4xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED +#include "stm32g4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED +#include "stm32g4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED +#include "stm32g4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED +#include "stm32g4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED +#include "stm32g4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED +#include "stm32g4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* STM32G4xx_HAL_CONF_H */ diff --git a/SAMM/Core/Inc/stm32g4xx_it.h b/SAMM/Core/Inc/stm32g4xx_it.h new file mode 100644 index 000000000..d5d87eab3 --- /dev/null +++ b/SAMM/Core/Inc/stm32g4xx_it.h @@ -0,0 +1,67 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __STM32G4xx_IT_H +#define __STM32G4xx_IT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void NMI_Handler(void); +void HardFault_Handler(void); +void MemManage_Handler(void); +void BusFault_Handler(void); +void UsageFault_Handler(void); +void SVC_Handler(void); +void DebugMon_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +void TIM1_UP_TIM16_IRQHandler(void); +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32G4xx_IT_H */ diff --git a/SAMM/Core/Src/adc.c b/SAMM/Core/Src/adc.c new file mode 100644 index 000000000..15733df47 --- /dev/null +++ b/SAMM/Core/Src/adc.c @@ -0,0 +1,154 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.c + * @brief This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "adc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +ADC_HandleTypeDef hadc1; + +/* ADC1 init function */ +void MX_ADC1_Init(void) +{ + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.GainCompensation = 0; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.OversamplingMode = ENABLE; + hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; + hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; + hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; + hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; + sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ + +} + +void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); + + /* ADC1 clock enable */ + __HAL_RCC_ADC12_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } +} + +void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +{ + + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ + + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC12_CLK_DISABLE(); + + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2); + + /* USER CODE BEGIN ADC1_MspDeInit 1 */ + + /* USER CODE END ADC1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/crc.c b/SAMM/Core/Src/crc.c new file mode 100644 index 000000000..0a8907607 --- /dev/null +++ b/SAMM/Core/Src/crc.c @@ -0,0 +1,90 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file crc.c + * @brief This file provides code for the configuration + * of the CRC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "crc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +CRC_HandleTypeDef hcrc; + +/* CRC init function */ +void MX_CRC_Init(void) +{ + + /* USER CODE BEGIN CRC_Init 0 */ + + /* USER CODE END CRC_Init 0 */ + + /* USER CODE BEGIN CRC_Init 1 */ + + /* USER CODE END CRC_Init 1 */ + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; + if (HAL_CRC_Init(&hcrc) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN CRC_Init 2 */ + + /* USER CODE END CRC_Init 2 */ + +} + +void HAL_CRC_MspInit(CRC_HandleTypeDef* crcHandle) +{ + + if(crcHandle->Instance==CRC) + { + /* USER CODE BEGIN CRC_MspInit 0 */ + + /* USER CODE END CRC_MspInit 0 */ + /* CRC clock enable */ + __HAL_RCC_CRC_CLK_ENABLE(); + /* USER CODE BEGIN CRC_MspInit 1 */ + + /* USER CODE END CRC_MspInit 1 */ + } +} + +void HAL_CRC_MspDeInit(CRC_HandleTypeDef* crcHandle) +{ + + if(crcHandle->Instance==CRC) + { + /* USER CODE BEGIN CRC_MspDeInit 0 */ + + /* USER CODE END CRC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_CRC_CLK_DISABLE(); + /* USER CODE BEGIN CRC_MspDeInit 1 */ + + /* USER CODE END CRC_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/dma.c b/SAMM/Core/Src/dma.c new file mode 100644 index 000000000..372b7e92c --- /dev/null +++ b/SAMM/Core/Src/dma.c @@ -0,0 +1,59 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.c + * @brief This file provides code for the configuration + * of all the requested memory to memory DMA transfers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "dma.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure DMA */ +/*----------------------------------------------------------------------------*/ + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** + * Enable DMA controller clock + */ +void MX_DMA_Init(void) +{ + + /* DMA controller clock enable */ + __HAL_RCC_DMAMUX1_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Channel1_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + /* DMA1_Channel2_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + diff --git a/SAMM/Core/Src/fdcan.c b/SAMM/Core/Src/fdcan.c new file mode 100644 index 000000000..59cf13f59 --- /dev/null +++ b/SAMM/Core/Src/fdcan.c @@ -0,0 +1,223 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file fdcan.c + * @brief This file provides code for the configuration + * of the FDCAN instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "fdcan.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +FDCAN_HandleTypeDef hfdcan1; +FDCAN_HandleTypeDef hfdcan2; + +/* FDCAN1 init function */ +void MX_FDCAN1_Init(void) +{ + + /* USER CODE BEGIN FDCAN1_Init 0 */ + + /* USER CODE END FDCAN1_Init 0 */ + + /* USER CODE BEGIN FDCAN1_Init 1 */ + + /* USER CODE END FDCAN1_Init 1 */ + hfdcan1.Instance = FDCAN1; + hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan1.Init.AutoRetransmission = DISABLE; + hfdcan1.Init.TransmitPause = DISABLE; + hfdcan1.Init.ProtocolException = DISABLE; + hfdcan1.Init.NominalPrescaler = 16; + hfdcan1.Init.NominalSyncJumpWidth = 1; + hfdcan1.Init.NominalTimeSeg1 = 1; + hfdcan1.Init.NominalTimeSeg2 = 1; + hfdcan1.Init.DataPrescaler = 1; + hfdcan1.Init.DataSyncJumpWidth = 1; + hfdcan1.Init.DataTimeSeg1 = 1; + hfdcan1.Init.DataTimeSeg2 = 1; + hfdcan1.Init.StdFiltersNbr = 0; + hfdcan1.Init.ExtFiltersNbr = 0; + hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN1_Init 2 */ + + /* USER CODE END FDCAN1_Init 2 */ + +} +/* FDCAN2 init function */ +void MX_FDCAN2_Init(void) +{ + + /* USER CODE BEGIN FDCAN2_Init 0 */ + + /* USER CODE END FDCAN2_Init 0 */ + + /* USER CODE BEGIN FDCAN2_Init 1 */ + + /* USER CODE END FDCAN2_Init 1 */ + hfdcan2.Instance = FDCAN2; + hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan2.Init.AutoRetransmission = DISABLE; + hfdcan2.Init.TransmitPause = DISABLE; + hfdcan2.Init.ProtocolException = DISABLE; + hfdcan2.Init.NominalPrescaler = 16; + hfdcan2.Init.NominalSyncJumpWidth = 1; + hfdcan2.Init.NominalTimeSeg1 = 1; + hfdcan2.Init.NominalTimeSeg2 = 1; + hfdcan2.Init.DataPrescaler = 1; + hfdcan2.Init.DataSyncJumpWidth = 1; + hfdcan2.Init.DataTimeSeg1 = 1; + hfdcan2.Init.DataTimeSeg2 = 1; + hfdcan2.Init.StdFiltersNbr = 0; + hfdcan2.Init.ExtFiltersNbr = 0; + hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN2_Init 2 */ + + /* USER CODE END FDCAN2_Init 2 */ + +} + +static uint32_t HAL_RCC_FDCAN_CLK_ENABLED=0; + +void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(fdcanHandle->Instance==FDCAN1) + { + /* USER CODE BEGIN FDCAN1_MspInit 0 */ + + /* USER CODE END FDCAN1_MspInit 0 */ + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN1 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if(HAL_RCC_FDCAN_CLK_ENABLED==1){ + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN1_MspInit 1 */ + + /* USER CODE END FDCAN1_MspInit 1 */ + } + else if(fdcanHandle->Instance==FDCAN2) + { + /* USER CODE BEGIN FDCAN2_MspInit 0 */ + + /* USER CODE END FDCAN2_MspInit 0 */ + + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN2 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if(HAL_RCC_FDCAN_CLK_ENABLED==1){ + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN2_MspInit 1 */ + + /* USER CODE END FDCAN2_MspInit 1 */ + } +} + +void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle) +{ + + if(fdcanHandle->Instance==FDCAN1) + { + /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ + + /* USER CODE END FDCAN1_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if(HAL_RCC_FDCAN_CLK_ENABLED==0){ + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + + /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ + + /* USER CODE END FDCAN1_MspDeInit 1 */ + } + else if(fdcanHandle->Instance==FDCAN2) + { + /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ + + /* USER CODE END FDCAN2_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if(HAL_RCC_FDCAN_CLK_ENABLED==0){ + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_5); + + /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ + + /* USER CODE END FDCAN2_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/gpio.c b/SAMM/Core/Src/gpio.c new file mode 100644 index 000000000..40651b0f5 --- /dev/null +++ b/SAMM/Core/Src/gpio.c @@ -0,0 +1,99 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "gpio.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure GPIO */ +/*----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** Configure pins as + * Analog + * Input + * Output + * EVENT_OUT + * EXTI + PC8 ------> I2C3_SCL + PC9 ------> I2C3_SDA +*/ +void MX_GPIO_Init(void) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin : PF1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + + /*Configure GPIO pin : PB1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PB12 */ + GPIO_InitStruct.Pin = GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : PC8 PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /*Configure GPIO pins : PB4 PB6 */ + GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ diff --git a/SAMM/Core/Src/i2c.c b/SAMM/Core/Src/i2c.c new file mode 100644 index 000000000..0cab1143b --- /dev/null +++ b/SAMM/Core/Src/i2c.c @@ -0,0 +1,135 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.c + * @brief This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "i2c.h" + +/* USER CODE BEGIN 0 */ +extern uint32_t I2C_Freq; +/* USER CODE END 0 */ + +I2C_HandleTypeDef hi2c1; + +/* I2C1 init function */ +void MX_I2C1_Init(void) +{ + + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.Timing = 0x10B30F25; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) + { + Error_Handler(); + } + + /** I2C Fast mode Plus enable + */ + HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ + +} + +void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspInit 0 */ + + /* USER CODE END I2C1_MspInit 0 */ + LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C1 clock enable */ + __HAL_RCC_I2C1_CLK_ENABLE(); + /* USER CODE BEGIN I2C1_MspInit 1 */ + + /* USER CODE END I2C1_MspInit 1 */ + } +} + +void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +{ + + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspDeInit 0 */ + + /* USER CODE END I2C1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C1_CLK_DISABLE(); + + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); + + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); + + /* USER CODE BEGIN I2C1_MspDeInit 1 */ + + /* USER CODE END I2C1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/main.c b/SAMM/Core/Src/main.c new file mode 100644 index 000000000..aec82ca28 --- /dev/null +++ b/SAMM/Core/Src/main.c @@ -0,0 +1,300 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "crc.h" +#include "fdcan.h" +#include "i2c.h" +#include "spi.h" +#include "gpio.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include +#include "VL53L4ED_api.h" +//#include "circularBuffer.h" +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN PTD */ + +/* USER CODE END PTD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#endif +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ +PUTCHAR_PROTOTYPE +{ + ITM_SendChar(ch); + return ch; +} +//CircularBuffer *cb; +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ + +/* USER CODE BEGIN PV */ +// FDCAN_RxHeaderTypeDef RxHeader_FDCAN2; +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ +/* +I2C2 - is meant for thermal sensor MLX90640 and is ran using DMA to offload CPU for calculations + - still need to fully test but ideally implmentation is done + + + */ +/* USER CODE END 0 */ + +/** + * @brief The application entry point. + * @retval int + */ +int main(void) +{ + + /* USER CODE BEGIN 1 */ + //cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_CRC_Init(); + MX_FDCAN1_Init(); + MX_FDCAN2_Init(); + MX_I2C1_Init(); + MX_SPI3_Init(); + /* USER CODE BEGIN 2 */ + + // HAL_FDCAN_Start(&hfdcan1); + // HAL_FDCAN_Start(&hfdcan2); + // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + + + // static uint16_t eeMLX90640[832]; + // static paramsMLX90640 mlx90640; + // #define MLX90640_ADDRESS 0x33<<1 + // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); + + // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + + // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); + + // MLX90640_SynchFrame(MLX90640_ADDRESS); + // MLX90640_SetRefreshRate(0x33, 0x05); + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin + HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to reset the device + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin + HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to power up the device + + uint16_t status = 0; + + uint16_t sensor_id = 0; + VL53L4ED_ResultsData_t results; + uint8_t p_data_ready; + + int TOF_ID = 0x52; + HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_1); + status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); + printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); + status = VL53L4ED_StartRanging(TOF_ID); + status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); + status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing + + while (1) + { + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); + if(p_data_ready){ + + /* (Mandatory) Clear HW interrupt to restart measurements */ + VL53L4ED_ClearInterrupt(TOF_ID); + + /* Read measured distance. RangeStatus = 0 means valid data */ + VL53L4ED_GetResult(TOF_ID, &results); + printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", + results.range_status, + results.distance_mm- 67, + results.signal_per_spad_kcps); + }else{ + HAL_Delay(10); + __disable_irq(); + __enable_irq(); + } + + + } + /* USER CODE END 3 */ +} + +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) +{ + LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); + while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) + { + } + LL_PWR_EnableRange1BoostMode(); + LL_RCC_HSE_EnableBypass(); + LL_RCC_HSE_Enable(); + /* Wait till HSE is ready */ + while(LL_RCC_HSE_IsReady() != 1) + { + } + + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 40, LL_RCC_PLLR_DIV_2); + LL_RCC_PLL_EnableDomain_SYS(); + LL_RCC_PLL_Enable(); + /* Wait till PLL is ready */ + while(LL_RCC_PLL_IsReady() != 1) + { + } + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) + { + } + + /* Insure 1us transition state at intermediate medium speed clock*/ + for (__IO uint32_t i = (170 >> 1); i !=0; i--); + + /* Set AHB prescaler*/ + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); + LL_SetSystemCoreClock(160000000); + + /* Update the time base */ + if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK) + { + Error_Handler(); + } +} + +/* USER CODE BEGIN 4 */ +// void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) +// { +// uint8_t RxData[64]; +// HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader_FDCAN2, RxData); +// printf("got messgae\n"); +// //circularBufferPush(cb, RxData, sizeof(RxData)); + + +// } +/* USER CODE END 4 */ + +/** + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM1 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + /* USER CODE BEGIN Callback 0 */ + + /* USER CODE END Callback 0 */ + if (htim->Instance == TIM1) { + HAL_IncTick(); + } + /* USER CODE BEGIN Callback 1 */ + + /* USER CODE END Callback 1 */ +} + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } + /* USER CODE END Error_Handler_Debug */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +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 CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ diff --git a/SAMM/Core/Src/spi.c b/SAMM/Core/Src/spi.c new file mode 100644 index 000000000..22f78c561 --- /dev/null +++ b/SAMM/Core/Src/spi.c @@ -0,0 +1,121 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file spi.c + * @brief This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "spi.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +SPI_HandleTypeDef hspi3; + +/* SPI3 init function */ +void MX_SPI3_Init(void) +{ + + /* USER CODE BEGIN SPI3_Init 0 */ + + /* USER CODE END SPI3_Init 0 */ + + /* USER CODE BEGIN SPI3_Init 1 */ + + /* USER CODE END SPI3_Init 1 */ + hspi3.Instance = SPI3; + hspi3.Init.Mode = SPI_MODE_MASTER; + hspi3.Init.Direction = SPI_DIRECTION_2LINES; + hspi3.Init.DataSize = SPI_DATASIZE_8BIT; + hspi3.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi3.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi3.Init.NSS = SPI_NSS_SOFT; + hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; + hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi3.Init.TIMode = SPI_TIMODE_DISABLE; + hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi3.Init.CRCPolynomial = 7; + hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + if (HAL_SPI_Init(&hspi3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN SPI3_Init 2 */ + + /* USER CODE END SPI3_Init 2 */ + +} + +void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(spiHandle->Instance==SPI3) + { + /* USER CODE BEGIN SPI3_MspInit 0 */ + + /* USER CODE END SPI3_MspInit 0 */ + /* SPI3 clock enable */ + __HAL_RCC_SPI3_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**SPI3 GPIO Configuration + PC10 ------> SPI3_SCK + PC11 ------> SPI3_MISO + PC12 ------> SPI3_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USER CODE BEGIN SPI3_MspInit 1 */ + + /* USER CODE END SPI3_MspInit 1 */ + } +} + +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) +{ + + if(spiHandle->Instance==SPI3) + { + /* USER CODE BEGIN SPI3_MspDeInit 0 */ + + /* USER CODE END SPI3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI3_CLK_DISABLE(); + + /**SPI3 GPIO Configuration + PC10 ------> SPI3_SCK + PC11 ------> SPI3_MISO + PC12 ------> SPI3_MOSI + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12); + + /* USER CODE BEGIN SPI3_MspDeInit 1 */ + + /* USER CODE END SPI3_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/stm32g4xx_hal_msp.c b/SAMM/Core/Src/stm32g4xx_hal_msp.c new file mode 100644 index 000000000..3fc223b63 --- /dev/null +++ b/SAMM/Core/Src/stm32g4xx_hal_msp.c @@ -0,0 +1,86 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN Define */ + +/* USER CODE END Define */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN Macro */ + +/* USER CODE END Macro */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* External functions --------------------------------------------------------*/ +/* USER CODE BEGIN ExternalFunctions */ + +/* USER CODE END ExternalFunctions */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + + /* System interrupt init*/ + + /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral + */ + HAL_PWREx_DisableUCPDDeadBattery(); + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c b/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c new file mode 100644 index 000000000..7ac4d76ff --- /dev/null +++ b/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c @@ -0,0 +1,127 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_timebase_tim.c + * @brief HAL time base based on the hardware TIM. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32g4xx_hal.h" +#include "stm32g4xx_hal_tim.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +TIM_HandleTypeDef htim1; +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief This function configures the TIM1 as a time base source. + * The time source is configured to have 1ms time base with a dedicated + * Tick interrupt priority. + * @note This function is called automatically at the beginning of program after + * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). + * @param TickPriority: Tick interrupt priority. + * @retval HAL status + */ +HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) +{ + RCC_ClkInitTypeDef clkconfig; + uint32_t uwTimclock = 0; + uint32_t uwPrescalerValue = 0; + uint32_t pFLatency; + + HAL_StatusTypeDef status; + + /* Enable TIM1 clock */ + __HAL_RCC_TIM1_CLK_ENABLE(); + +/* Get clock configuration */ + HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); + + /* Compute TIM1 clock */ + uwTimclock = HAL_RCC_GetPCLK2Freq(); + + /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ + uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); + + /* Initialize TIM1 */ + htim1.Instance = TIM1; + + /* Initialize TIMx peripheral as follow: + + + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. + + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + + ClockDivision = 0 + + Counter direction = Up + */ + htim1.Init.Period = (1000000U / 1000U) - 1U; + htim1.Init.Prescaler = uwPrescalerValue; + htim1.Init.ClockDivision = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + + status = HAL_TIM_Base_Init(&htim1); + if (status == HAL_OK) + { + /* Start the TIM time Base generation in interrupt mode */ + status = HAL_TIM_Base_Start_IT(&htim1); + if (status == HAL_OK) + { + /* Enable the TIM1 global Interrupt */ + HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + /* Configure the SysTick IRQ priority */ + if (TickPriority < (1UL << __NVIC_PRIO_BITS)) + { + /* Configure the TIM IRQ priority */ + HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); + uwTickPrio = TickPriority; + } + else + { + status = HAL_ERROR; + } + } + } + + /* Return function status */ + return status; +} + +/** + * @brief Suspend Tick increment. + * @note Disable the tick increment by disabling TIM1 update interrupt. + * @param None + * @retval None + */ +void HAL_SuspendTick(void) +{ + /* Disable TIM1 update Interrupt */ + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); +} + +/** + * @brief Resume Tick increment. + * @note Enable the tick increment by Enabling TIM1 update interrupt. + * @param None + * @retval None + */ +void HAL_ResumeTick(void) +{ + /* Enable TIM1 Update interrupt */ + __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); +} + diff --git a/SAMM/Core/Src/stm32g4xx_it.c b/SAMM/Core/Src/stm32g4xx_it.c new file mode 100644 index 000000000..ef94b5f52 --- /dev/null +++ b/SAMM/Core/Src/stm32g4xx_it.c @@ -0,0 +1,218 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32g4xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern TIM_HandleTypeDef htim1; + +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M4 Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) + { + } + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Memory management fault. + */ +void MemManage_Handler(void) +{ + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ + /* USER CODE END W1_MemoryManagement_IRQn 0 */ + } +} + +/** + * @brief This function handles Prefetch fault, memory access fault. + */ +void BusFault_Handler(void) +{ + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_BusFault_IRQn 0 */ + /* USER CODE END W1_BusFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Undefined instruction or illegal state. + */ +void UsageFault_Handler(void) +{ + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ + /* USER CODE END W1_UsageFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVCall_IRQn 0 */ + + /* USER CODE END SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 1 */ + + /* USER CODE END SVCall_IRQn 1 */ +} + +/** + * @brief This function handles Debug monitor. + */ +void DebugMon_Handler(void) +{ + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + + /* USER CODE END DebugMonitor_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32G4xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32g4xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. + */ +void TIM1_UP_TIM16_IRQHandler(void) +{ + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ + + /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ + HAL_TIM_IRQHandler(&htim1); + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ + + /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/Core/Src/syscalls.c b/SAMM/Core/Src/syscalls.c new file mode 100644 index 000000000..e33a8492c --- /dev/null +++ b/SAMM/Core/Src/syscalls.c @@ -0,0 +1,176 @@ +/** + ****************************************************************************** + * @file syscalls.c + * @author Auto-generated by STM32CubeMX + * @brief Minimal System calls file + * + * For more information about which c-functions + * need which of these lowlevel functions + * please consult the Newlib libc-manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2020-2024 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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include +#include +#include +#include +#include +#include +#include + + +/* Variables */ +extern int __io_putchar(int ch) __attribute__((weak)); +extern int __io_getchar(void) __attribute__((weak)); + + +char *__env[1] = { 0 }; +char **environ = __env; + + +/* Functions */ +void initialise_monitor_handles() +{ +} + +int _getpid(void) +{ + return 1; +} + +int _kill(int pid, int sig) +{ + (void)pid; + (void)sig; + errno = EINVAL; + return -1; +} + +void _exit (int status) +{ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ +} + +__attribute__((weak)) int _read(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + *ptr++ = __io_getchar(); + } + + return len; +} + +__attribute__((weak)) int _write(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + __io_putchar(*ptr++); + } + return len; +} + +int _close(int file) +{ + (void)file; + return -1; +} + + +int _fstat(int file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _isatty(int file) +{ + (void)file; + return 1; +} + +int _lseek(int file, int ptr, int dir) +{ + (void)file; + (void)ptr; + (void)dir; + return 0; +} + +int _open(char *path, int flags, ...) +{ + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; +} + +int _wait(int *status) +{ + (void)status; + errno = ECHILD; + return -1; +} + +int _unlink(char *name) +{ + (void)name; + errno = ENOENT; + return -1; +} + +int _times(struct tms *buf) +{ + (void)buf; + return -1; +} + +int _stat(char *file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _link(char *old, char *new) +{ + (void)old; + (void)new; + errno = EMLINK; + return -1; +} + +int _fork(void) +{ + errno = EAGAIN; + return -1; +} + +int _execve(char *name, char **argv, char **env) +{ + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; +} diff --git a/SAMM/Core/Src/sysmem.c b/SAMM/Core/Src/sysmem.c new file mode 100644 index 000000000..246470ee8 --- /dev/null +++ b/SAMM/Core/Src/sysmem.c @@ -0,0 +1,79 @@ +/** + ****************************************************************************** + * @file sysmem.c + * @author Generated by STM32CubeMX + * @brief System Memory calls file + * + * For more information about which C functions + * need which of these lowlevel functions + * please consult the newlib libc manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include + +/** + * Pointer to the current high watermark of the heap usage + */ +static uint8_t *__sbrk_heap_end = NULL; + +/** + * @brief _sbrk() allocates memory to the newlib heap and is used by malloc + * and others from the C library + * + * @verbatim + * ############################################################################ + * # .data # .bss # newlib heap # MSP stack # + * # # # # Reserved by _Min_Stack_Size # + * ############################################################################ + * ^-- RAM start ^-- _end _estack, RAM end --^ + * @endverbatim + * + * This implementation starts allocating at the '_end' linker symbol + * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack + * The implementation considers '_estack' linker symbol to be RAM end + * NOTE: If the MSP stack, at any point during execution, grows larger than the + * reserved size, please increase the '_Min_Stack_Size'. + * + * @param incr Memory size + * @return Pointer to allocated memory + */ +void *_sbrk(ptrdiff_t incr) +{ + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; + + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) + { + __sbrk_heap_end = &_end; + } + + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) + { + errno = ENOMEM; + return (void *)-1; + } + + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; + + return (void *)prev_heap_end; +} diff --git a/SAMM/Core/Src/system_stm32g4xx.c b/SAMM/Core/Src/system_stm32g4xx.c new file mode 100644 index 000000000..8d35a0aa0 --- /dev/null +++ b/SAMM/Core/Src/system_stm32g4xx.c @@ -0,0 +1,285 @@ +/** + ****************************************************************************** + * @file system_stm32g4xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32g4xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * After each device reset the HSI (16 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to + * configure the system clock before to branch to main program. + * + * This file configures the system clock as follows: + *============================================================================= + *----------------------------------------------------------------------------- + * System Clock source | HSI + *----------------------------------------------------------------------------- + * SYSCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * HCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * AHB Prescaler | 1 + *----------------------------------------------------------------------------- + * APB1 Prescaler | 1 + *----------------------------------------------------------------------------- + * APB2 Prescaler | 1 + *----------------------------------------------------------------------------- + * PLL_M | 1 + *----------------------------------------------------------------------------- + * PLL_N | 16 + *----------------------------------------------------------------------------- + * PLL_P | 7 + *----------------------------------------------------------------------------- + * PLL_Q | 2 + *----------------------------------------------------------------------------- + * PLL_R | 2 + *----------------------------------------------------------------------------- + * Require 48MHz for RNG | Disabled + *----------------------------------------------------------------------------- + *============================================================================= + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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. + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32g4xx_system + * @{ + */ + +/** @addtogroup STM32G4xx_System_Private_Includes + * @{ + */ + +#include "stm32g4xx.h" + +#if !defined (HSE_VALUE) + #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Defines + * @{ + */ + +/************************* Miscellaneous Configuration ************************/ +/* Note: Following vector table addresses must be defined in line with linker + configuration. */ +/*!< Uncomment the following line if you need to relocate the vector table + anywhere in Flash or Sram, else the vector table is kept at the automatic + remap of boot address selected */ +#define USER_VECT_TAB_ADDRESS + +#if defined(USER_VECT_TAB_ADDRESS) +/*!< Uncomment the following line if you need to relocate your vector Table + in Sram else user remap will be done in Flash. */ +/* #define VECT_TAB_SRAM */ +#if defined(VECT_TAB_SRAM) +#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#else +#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +/******************************************************************************/ +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Variables + * @{ + */ + /* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ + uint32_t SystemCoreClock = HSI_VALUE; + + const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; + const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ + +void SystemInit(void) +{ + /* FPU settings ------------------------------------------------------------*/ + #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ + #endif + + /* Configure the Vector Table location add offset address ------------------*/ +#if defined(USER_VECT_TAB_ADDRESS) + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 24 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ +void SystemCoreClockUpdate(void) +{ + uint32_t tmp, pllvco, pllr, pllsource, pllm; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) + { + case 0x04: /* HSI used as system clock source */ + SystemCoreClock = HSI_VALUE; + break; + + case 0x08: /* HSE used as system clock source */ + SystemCoreClock = HSE_VALUE; + break; + + case 0x0C: /* PLL used as system clock source */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; + if (pllsource == 0x02UL) /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } + else /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; + SystemCoreClock = pllvco/pllr; + break; + + default: + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + From effa315c2ed6151e2e15243614e801ef3e43292f Mon Sep 17 00:00:00 2001 From: planeflight Date: Mon, 30 Mar 2026 21:26:43 -0700 Subject: [PATCH 2/7] change pinouts in main.c --- SAMM/Core/Src/main.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/SAMM/Core/Src/main.c b/SAMM/Core/Src/main.c index aec82ca28..7ca2c8382 100644 --- a/SAMM/Core/Src/main.c +++ b/SAMM/Core/Src/main.c @@ -112,20 +112,20 @@ int main(void) MX_I2C1_Init(); MX_SPI3_Init(); /* USER CODE BEGIN 2 */ - + // HAL_FDCAN_Start(&hfdcan1); // HAL_FDCAN_Start(&hfdcan2); // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); - + // static uint16_t eeMLX90640[832]; // static paramsMLX90640 mlx90640; // #define MLX90640_ADDRESS 0x33<<1 // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); - + // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); - + // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); // MLX90640_SynchFrame(MLX90640_ADDRESS); @@ -134,21 +134,21 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin - HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin HAL_Delay(100); // wait for 5ms to reset the device - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin - HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin HAL_Delay(100); // wait for 5ms to power up the device uint16_t status = 0; - + uint16_t sensor_id = 0; VL53L4ED_ResultsData_t results; uint8_t p_data_ready; int TOF_ID = 0x52; - HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_1); + HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); status = VL53L4ED_StartRanging(TOF_ID); @@ -156,7 +156,7 @@ int main(void) status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing while (1) - { + { // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); /* USER CODE END WHILE */ @@ -179,8 +179,6 @@ int main(void) __disable_irq(); __enable_irq(); } - - } /* USER CODE END 3 */ } From 4899164e2ae5c1118fe12fc041c6bbe4b7b2e27c Mon Sep 17 00:00:00 2001 From: planeflight Date: Mon, 30 Mar 2026 21:36:02 -0700 Subject: [PATCH 3/7] changed gpio.c and removed the extra pinouts --- SAMM/Core/Src/gpio.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/SAMM/Core/Src/gpio.c b/SAMM/Core/Src/gpio.c index 40651b0f5..5efdf6ec1 100644 --- a/SAMM/Core/Src/gpio.c +++ b/SAMM/Core/Src/gpio.c @@ -53,41 +53,47 @@ void MX_GPIO_Init(void) __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); + //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); /*Configure GPIO pin : PF1 */ + /* GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + */ /*Configure GPIO pin : PB1 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pin : PB12 */ + /* corresponds to SPI GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + */ /*Configure GPIO pins : PC8 PC9 */ + /* corresponds to tiretemp i2c GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + */ /*Configure GPIO pins : PB4 PB6 */ - GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_6; + GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); From f46c2ec093bccf5b5948ca36bd2495cb64b5fd57 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 04:39:45 +0000 Subject: [PATCH 4/7] Automatic Clang-Format: Standardized formatting automatically --- SAMM/Core/Extras/CircBuF/circularBuffer.c | 108 +-- SAMM/Core/Extras/CircBuF/circularBuffer.h | 14 +- SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c | 651 +++++++----------- SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h | 164 ++--- .../Extras/VL53L4ED/VL53L4ED_calibration.c | 139 ++-- .../Extras/VL53L4ED/VL53L4ED_calibration.h | 37 +- SAMM/Core/Extras/VL53L4ED/platform.c | 29 +- SAMM/Core/Extras/VL53L4ED/platform.h | 29 +- SAMM/Core/Extras/extra.c | 57 +- SAMM/Core/Extras/extra.h | 9 +- SAMM/Core/Inc/adc.h | 33 +- SAMM/Core/Inc/crc.h | 33 +- SAMM/Core/Inc/dma.h | 33 +- SAMM/Core/Inc/fdcan.h | 33 +- SAMM/Core/Inc/gpio.h | 33 +- SAMM/Core/Inc/i2c.h | 33 +- SAMM/Core/Inc/main.h | 45 +- SAMM/Core/Inc/spi.h | 33 +- SAMM/Core/Inc/stm32_assert.h | 49 +- SAMM/Core/Inc/stm32g4xx_hal_conf.h | 243 +++---- SAMM/Core/Inc/stm32g4xx_it.h | 32 +- SAMM/Core/Src/adc.c | 226 +++--- SAMM/Core/Src/crc.c | 102 ++- SAMM/Core/Src/dma.c | 60 +- SAMM/Core/Src/fdcan.c | 364 +++++----- SAMM/Core/Src/gpio.c | 149 ++-- SAMM/Core/Src/i2c.c | 196 +++--- SAMM/Core/Src/main.c | 336 +++++---- SAMM/Core/Src/spi.c | 172 +++-- SAMM/Core/Src/stm32g4xx_hal_msp.c | 56 +- SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c | 190 +++-- SAMM/Core/Src/stm32g4xx_it.c | 189 +++-- SAMM/Core/Src/syscalls.c | 134 ++-- SAMM/Core/Src/sysmem.c | 38 +- SAMM/Core/Src/system_stm32g4xx.c | 437 ++++++------ 35 files changed, 2110 insertions(+), 2376 deletions(-) diff --git a/SAMM/Core/Extras/CircBuF/circularBuffer.c b/SAMM/Core/Extras/CircBuF/circularBuffer.c index e7f59de79..4b0eaeaca 100644 --- a/SAMM/Core/Extras/CircBuF/circularBuffer.c +++ b/SAMM/Core/Extras/CircBuF/circularBuffer.c @@ -1,64 +1,70 @@ #include "circularBuffer.h" -CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) { - CircularBuffer *cb = malloc(sizeof(CircularBuffer)); - if (!cb) { - return NULL; - } +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) +{ + CircularBuffer *cb = malloc(sizeof(CircularBuffer)); + if (!cb) { + return NULL; + } - cb->buffer = malloc(capacity * sizeof(void *)); - if (!cb->buffer) { - free(cb); - return NULL; - } + cb->buffer = malloc(capacity * sizeof(void *)); + if (!cb->buffer) { + free(cb); + return NULL; + } - for (size_t i = 0; i < capacity; i++) { - cb->buffer[i] = malloc(max_array_size); - if (!cb->buffer[i]) { - for (size_t j = 0; j < i; j++) { - free(cb->buffer[j]); - } - free(cb->buffer); - free(cb); - return NULL; - } - } - cb->capacity = capacity; - cb->max_arr_size = max_array_size; - cb->head = 0; - cb->tail = 0; - cb->size = 0; - return cb; + for (size_t i = 0; i < capacity; i++) { + cb->buffer[i] = malloc(max_array_size); + if (!cb->buffer[i]) { + for (size_t j = 0; j < i; j++) { + free(cb->buffer[j]); + } + free(cb->buffer); + free(cb); + return NULL; + } + } + cb->capacity = capacity; + cb->max_arr_size = max_array_size; + cb->head = 0; + cb->tail = 0; + cb->size = 0; + return cb; } -void circularBufferDestroy(CircularBuffer *cb) { - if (!cb) return; +void circularBufferDestroy(CircularBuffer *cb) +{ + if (!cb) { + return; + } - for (size_t i = 0; i < cb->capacity; i++) { - free(cb->buffer[i]); - } - free(cb->buffer); - free(cb); + for (size_t i = 0; i < cb->capacity; i++) { + free(cb->buffer[i]); + } + free(cb->buffer); + free(cb); } -int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) { - if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { - return -1; // Buffer is full or array is too large - } +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) +{ + if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { + return -1; // Buffer is full or array is too large + } - // Copy the array into the buffer - memcpy(cb->buffer[cb->head], array, array_size); - cb->head = (cb->head + 1) % cb->capacity; - cb->size++; - return 0; + // Copy the array into the buffer + memcpy(cb->buffer[cb->head], array, array_size); + cb->head = (cb->head + 1) % cb->capacity; + cb->size++; + return 0; } -void *circularBufferPop(CircularBuffer *cb) { - if (!cb || cb->size == 0) { - return NULL; // Buffer is empty - } +void *circularBufferPop(CircularBuffer *cb) +{ + if (!cb || cb->size == 0) { + return NULL; // Buffer is empty + } - void *array = cb->buffer[cb->tail]; - cb->tail = (cb->tail + 1) % cb->capacity; - cb->size--; - return array; + void *array = cb->buffer[cb->tail]; + cb->tail = (cb->tail + 1) % cb->capacity; + cb->size--; + return array; } diff --git a/SAMM/Core/Extras/CircBuF/circularBuffer.h b/SAMM/Core/Extras/CircBuF/circularBuffer.h index ae7ce9220..861792535 100644 --- a/SAMM/Core/Extras/CircBuF/circularBuffer.h +++ b/SAMM/Core/Extras/CircBuF/circularBuffer.h @@ -1,18 +1,18 @@ #ifndef __circularBuffer_h__ #define __circularBuffer_h__ +#include #include #include -#include #include typedef struct { - void **buffer; // Array of void pointers - uint8_t capacity; // Maximum number of elements in the buffer - uint8_t head; // Index of the next element to write - uint8_t tail; // Index of the next element to read - uint8_t size; // Current number of elements in the buffer - uint8_t max_arr_size; // Maximum size of the array + void **buffer; // Array of void pointers + uint8_t capacity; // Maximum number of elements in the buffer + uint8_t head; // Index of the next element to write + uint8_t tail; // Index of the next element to read + uint8_t size; // Current number of elements in the buffer + uint8_t max_arr_size; // Maximum size of the array } CircularBuffer; CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size); diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c index 323148da4..7c844100e 100644 --- a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.c @@ -1,138 +1,138 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_api.c * @brief Functions implementation */ -#include -#include #include "VL53L4ED_api.h" +#include +#include + static const uint8_t VL53L4ED_DEFAULT_CONFIGURATION[] = { - #ifdef VL53L4ED_I2C_FAST_MODE_PLUS - 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), - else don't touch */ - #else - 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), - else don't touch */ - #endif - 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 - (pull up at AVDD) */ - 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 - (pull up at AVDD) */ - 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low - (bits 3:0 must be 0x1), use SetInterruptPolarity() */ - 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, - use CheckForDataReady() */ - 0x00, /* 0x32 : not user-modifiable */ - 0x02, /* 0x33 : not user-modifiable */ - 0x08, /* 0x34 : not user-modifiable */ - 0x00, /* 0x35 : not user-modifiable */ - 0x08, /* 0x36 : not user-modifiable */ - 0x10, /* 0x37 : not user-modifiable */ - 0x01, /* 0x38 : not user-modifiable */ - 0x01, /* 0x39 : not user-modifiable */ - 0x00, /* 0x3a : not user-modifiable */ - 0x00, /* 0x3b : not user-modifiable */ - 0x00, /* 0x3c : not user-modifiable */ - 0x00, /* 0x3d : not user-modifiable */ - 0xff, /* 0x3e : not user-modifiable */ - 0x00, /* 0x3f : not user-modifiable */ - 0x0F, /* 0x40 : not user-modifiable */ - 0x00, /* 0x41 : not user-modifiable */ - 0x00, /* 0x42 : not user-modifiable */ - 0x00, /* 0x43 : not user-modifiable */ - 0x00, /* 0x44 : not user-modifiable */ - 0x00, /* 0x45 : not user-modifiable */ - 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, - 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ - 0x0b, /* 0x47 : not user-modifiable */ - 0x00, /* 0x48 : not user-modifiable */ - 0x00, /* 0x49 : not user-modifiable */ - 0x02, /* 0x4a : not user-modifiable */ - 0x14, /* 0x4b : not user-modifiable */ - 0x21, /* 0x4c : not user-modifiable */ - 0x00, /* 0x4d : not user-modifiable */ - 0x00, /* 0x4e : not user-modifiable */ - 0x05, /* 0x4f : not user-modifiable */ - 0x00, /* 0x50 : not user-modifiable */ - 0x00, /* 0x51 : not user-modifiable */ - 0x00, /* 0x52 : not user-modifiable */ - 0x00, /* 0x53 : not user-modifiable */ - 0xc8, /* 0x54 : not user-modifiable */ - 0x00, /* 0x55 : not user-modifiable */ - 0x00, /* 0x56 : not user-modifiable */ - 0x38, /* 0x57 : not user-modifiable */ - 0xff, /* 0x58 : not user-modifiable */ - 0x01, /* 0x59 : not user-modifiable */ - 0x00, /* 0x5a : not user-modifiable */ - 0x08, /* 0x5b : not user-modifiable */ - 0x00, /* 0x5c : not user-modifiable */ - 0x00, /* 0x5d : not user-modifiable */ - 0x01, /* 0x5e : not user-modifiable */ - 0xcc, /* 0x5f : not user-modifiable */ - 0x07, /* 0x60 : not user-modifiable */ - 0x01, /* 0x61 : not user-modifiable */ - 0xf1, /* 0x62 : not user-modifiable */ - 0x05, /* 0x63 : not user-modifiable */ - 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), - use SetSigmaThreshold(), default value 90 mm */ - 0xa0, /* 0x65 : Sigma threshold LSB */ - 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), - use SetSignalThreshold() */ - 0x80, /* 0x67 : Min count Rate LSB */ - 0x08, /* 0x68 : not user-modifiable */ - 0x38, /* 0x69 : not user-modifiable */ - 0x00, /* 0x6a : not user-modifiable */ - 0x00, /* 0x6b : not user-modifiable */ - 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, - use SetIntermeasurementInMs() */ - 0x00, /* 0x6d : Intermeasurement period */ - 0x0f, /* 0x6e : Intermeasurement period */ - 0x89, /* 0x6f : Intermeasurement period LSB */ - 0x00, /* 0x70 : not user-modifiable */ - 0x00, /* 0x71 : not user-modifiable */ - 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), - use SetD:tanceThreshold() */ - 0x00, /* 0x73 : distance threshold high LSB */ - 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), - use SetD:tanceThreshold() */ - 0x00, /* 0x75 : distance threshold low LSB */ - 0x00, /* 0x76 : not user-modifiable */ - 0x01, /* 0x77 : not user-modifiable */ - 0x07, /* 0x78 : not user-modifiable */ - 0x05, /* 0x79 : not user-modifiable */ - 0x06, /* 0x7a : not user-modifiable */ - 0x06, /* 0x7b : not user-modifiable */ - 0x00, /* 0x7c : not user-modifiable */ - 0x00, /* 0x7d : not user-modifiable */ - 0x02, /* 0x7e : not user-modifiable */ - 0xc7, /* 0x7f : not user-modifiable */ - 0xff, /* 0x80 : not user-modifiable */ - 0x9B, /* 0x81 : not user-modifiable */ - 0x00, /* 0x82 : not user-modifiable */ - 0x00, /* 0x83 : not user-modifiable */ - 0x00, /* 0x84 : not user-modifiable */ - 0x01, /* 0x85 : not user-modifiable */ - 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ - 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), - If you want an automatic start after VL53L4ED_init() call, - put 0x40 in location 0x87 */ +#ifdef VL53L4ED_I2C_FAST_MODE_PLUS + 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ +#else + 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ +#endif + 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low + (bits 3:0 must be 0x1), use SetInterruptPolarity() */ + 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, + use CheckForDataReady() */ + 0x00, /* 0x32 : not user-modifiable */ + 0x02, /* 0x33 : not user-modifiable */ + 0x08, /* 0x34 : not user-modifiable */ + 0x00, /* 0x35 : not user-modifiable */ + 0x08, /* 0x36 : not user-modifiable */ + 0x10, /* 0x37 : not user-modifiable */ + 0x01, /* 0x38 : not user-modifiable */ + 0x01, /* 0x39 : not user-modifiable */ + 0x00, /* 0x3a : not user-modifiable */ + 0x00, /* 0x3b : not user-modifiable */ + 0x00, /* 0x3c : not user-modifiable */ + 0x00, /* 0x3d : not user-modifiable */ + 0xff, /* 0x3e : not user-modifiable */ + 0x00, /* 0x3f : not user-modifiable */ + 0x0F, /* 0x40 : not user-modifiable */ + 0x00, /* 0x41 : not user-modifiable */ + 0x00, /* 0x42 : not user-modifiable */ + 0x00, /* 0x43 : not user-modifiable */ + 0x00, /* 0x44 : not user-modifiable */ + 0x00, /* 0x45 : not user-modifiable */ + 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, + 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ + 0x0b, /* 0x47 : not user-modifiable */ + 0x00, /* 0x48 : not user-modifiable */ + 0x00, /* 0x49 : not user-modifiable */ + 0x02, /* 0x4a : not user-modifiable */ + 0x14, /* 0x4b : not user-modifiable */ + 0x21, /* 0x4c : not user-modifiable */ + 0x00, /* 0x4d : not user-modifiable */ + 0x00, /* 0x4e : not user-modifiable */ + 0x05, /* 0x4f : not user-modifiable */ + 0x00, /* 0x50 : not user-modifiable */ + 0x00, /* 0x51 : not user-modifiable */ + 0x00, /* 0x52 : not user-modifiable */ + 0x00, /* 0x53 : not user-modifiable */ + 0xc8, /* 0x54 : not user-modifiable */ + 0x00, /* 0x55 : not user-modifiable */ + 0x00, /* 0x56 : not user-modifiable */ + 0x38, /* 0x57 : not user-modifiable */ + 0xff, /* 0x58 : not user-modifiable */ + 0x01, /* 0x59 : not user-modifiable */ + 0x00, /* 0x5a : not user-modifiable */ + 0x08, /* 0x5b : not user-modifiable */ + 0x00, /* 0x5c : not user-modifiable */ + 0x00, /* 0x5d : not user-modifiable */ + 0x01, /* 0x5e : not user-modifiable */ + 0xcc, /* 0x5f : not user-modifiable */ + 0x07, /* 0x60 : not user-modifiable */ + 0x01, /* 0x61 : not user-modifiable */ + 0xf1, /* 0x62 : not user-modifiable */ + 0x05, /* 0x63 : not user-modifiable */ + 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), + use SetSigmaThreshold(), default value 90 mm */ + 0xa0, /* 0x65 : Sigma threshold LSB */ + 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), + use SetSignalThreshold() */ + 0x80, /* 0x67 : Min count Rate LSB */ + 0x08, /* 0x68 : not user-modifiable */ + 0x38, /* 0x69 : not user-modifiable */ + 0x00, /* 0x6a : not user-modifiable */ + 0x00, /* 0x6b : not user-modifiable */ + 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, + use SetIntermeasurementInMs() */ + 0x00, /* 0x6d : Intermeasurement period */ + 0x0f, /* 0x6e : Intermeasurement period */ + 0x89, /* 0x6f : Intermeasurement period LSB */ + 0x00, /* 0x70 : not user-modifiable */ + 0x00, /* 0x71 : not user-modifiable */ + 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x73 : distance threshold high LSB */ + 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x75 : distance threshold low LSB */ + 0x00, /* 0x76 : not user-modifiable */ + 0x01, /* 0x77 : not user-modifiable */ + 0x07, /* 0x78 : not user-modifiable */ + 0x05, /* 0x79 : not user-modifiable */ + 0x06, /* 0x7a : not user-modifiable */ + 0x06, /* 0x7b : not user-modifiable */ + 0x00, /* 0x7c : not user-modifiable */ + 0x00, /* 0x7d : not user-modifiable */ + 0x02, /* 0x7e : not user-modifiable */ + 0xc7, /* 0x7f : not user-modifiable */ + 0xff, /* 0x80 : not user-modifiable */ + 0x9B, /* 0x81 : not user-modifiable */ + 0x00, /* 0x82 : not user-modifiable */ + 0x00, /* 0x83 : not user-modifiable */ + 0x00, /* 0x84 : not user-modifiable */ + 0x01, /* 0x85 : not user-modifiable */ + 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ + 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), + If you want an automatic start after VL53L4ED_init() call, + put 0x40 in location 0x87 */ }; -VL53L4ED_Error VL53L4ED_GetSWVersion( - VL53L4ED_Version_t *p_Version) +VL53L4ED_Error VL53L4ED_GetSWVersion(VL53L4ED_Version_t *p_Version) { VL53L4ED_Error Status = VL53L4ED_ERROR_NONE; @@ -143,20 +143,15 @@ VL53L4ED_Error VL53L4ED_GetSWVersion( return Status; } -VL53L4ED_Error VL53L4ED_SetI2CAddress( - Dev_t dev, - uint8_t new_address) +VL53L4ED_Error VL53L4ED_SetI2CAddress(Dev_t dev, uint8_t new_address) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, - (uint8_t)(new_address >> (uint8_t)1)); + status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, (uint8_t)(new_address >> (uint8_t)1)); return status; } -VL53L4ED_Error VL53L4ED_GetSensorId( - Dev_t dev, - uint16_t *p_id) +VL53L4ED_Error VL53L4ED_GetSensorId(Dev_t dev, uint16_t *p_id) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -164,69 +159,58 @@ VL53L4ED_Error VL53L4ED_GetSensorId( return status; } -VL53L4ED_Error VL53L4ED_SensorInit( - Dev_t dev) +VL53L4ED_Error VL53L4ED_SensorInit(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t Addr, tmp; uint8_t continue_loop = 1; uint16_t i = 0; - do{ - status |= VL53L4ED_RdByte(dev, - VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); + do { + status |= VL53L4ED_RdByte(dev, VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); - if(tmp == (uint8_t)0x3) /* Sensor booted */ + if (tmp == (uint8_t)0x3) /* Sensor booted */ { continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for boot */ + } else if (i < (uint16_t)1000) /* Wait for boot */ { i++; - } - else /* Timeout 1000ms reached */ + } else /* Timeout 1000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); /* Load default configuration */ - for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) - { - status |= VL53L4ED_WrByte(dev, Addr, - VL53L4ED_DEFAULT_CONFIGURATION[ - Addr - (uint8_t)0x2D]); + for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) { + status |= VL53L4ED_WrByte(dev, Addr, VL53L4ED_DEFAULT_CONFIGURATION[Addr - (uint8_t)0x2D]); } /* Start VHV */ status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, (uint8_t)0x40); - i = (uint8_t)0; + i = (uint8_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for answer */ + } else if (i < (uint16_t)1000) /* Wait for answer */ { i++; - } - else /* Timeout 1000ms reached */ + } else /* Timeout 1000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_ClearInterrupt(dev); status |= VL53L4ED_StopRanging(dev); - status |= VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, - (uint8_t)0x09); + status |= VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x09); status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0); status |= VL53L4ED_WrWord(dev, 0x0024, 0x500); @@ -235,8 +219,7 @@ VL53L4ED_Error VL53L4ED_SensorInit( return status; } -VL53L4ED_Error VL53L4ED_ClearInterrupt( - Dev_t dev) +VL53L4ED_Error VL53L4ED_ClearInterrupt(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -244,8 +227,7 @@ VL53L4ED_Error VL53L4ED_ClearInterrupt( return status; } -VL53L4ED_Error VL53L4ED_StartRanging( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StartRanging(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint32_t tmp; @@ -253,21 +235,18 @@ VL53L4ED_Error VL53L4ED_StartRanging( status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); /* Sensor runs in continuous mode */ - if(tmp == (uint32_t)0) - { + if (tmp == (uint32_t)0) { status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x21); } /* Sensor runs in autonomous mode */ - else - { + else { status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x40); } return status; } -VL53L4ED_Error VL53L4ED_StopRanging( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StopRanging(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -275,9 +254,7 @@ VL53L4ED_Error VL53L4ED_StopRanging( return status; } -VL53L4ED_Error VL53L4ED_CheckForDataReady( - Dev_t dev, - uint8_t *p_is_data_ready) +VL53L4ED_Error VL53L4ED_CheckForDataReady(Dev_t dev, uint8_t *p_is_data_ready) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t temp; @@ -287,33 +264,24 @@ VL53L4ED_Error VL53L4ED_CheckForDataReady( temp = temp & (uint8_t)0x10; temp = temp >> 4; - if (temp == (uint8_t)1) - { + if (temp == (uint8_t)1) { int_pol = (uint8_t)0; - } - else - { + } else { int_pol = (uint8_t)1; } status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO__TIO_HV_STATUS, &temp); - if ((temp & (uint8_t)1) == int_pol) - { + if ((temp & (uint8_t)1) == int_pol) { *p_is_data_ready = (uint8_t)1; - } - else - { + } else { *p_is_data_ready = (uint8_t)0; } return status; } -VL53L4ED_Error VL53L4ED_SetRangeTiming( - Dev_t dev, - uint32_t timing_budget_ms, - uint32_t inter_measurement_ms) +VL53L4ED_Error VL53L4ED_SetRangeTiming(Dev_t dev, uint32_t timing_budget_ms, uint32_t inter_measurement_ms) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t clock_pll, osc_frequency, ms_byte; @@ -321,88 +289,67 @@ VL53L4ED_Error VL53L4ED_SetRangeTiming( float_t inter_measurement_factor = (float_t)1.055; status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); - if(osc_frequency != (uint16_t)0) - { - timing_budget_us = timing_budget_ms*(uint32_t)1000; - macro_period_us = (uint32_t)((uint32_t)2304 * - ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; - } - else - { + if (osc_frequency != (uint16_t)0) { + timing_budget_us = timing_budget_ms * (uint32_t)1000; + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; + } else { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; } /* Timing budget check validity */ - if ((timing_budget_ms < (uint32_t)10) - || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) - { + if ((timing_budget_ms < (uint32_t)10) || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) { status |= VL53L4ED_ERROR_INVALID_ARGUMENT; } /* Sensor runs in continuous mode */ - else if(inter_measurement_ms == (uint32_t)0) - { - status |= VL53L4ED_WrDWord(dev,VL53L4ED_INTERMEASUREMENT_MS, 0); + else if (inter_measurement_ms == (uint32_t)0) { + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, 0); timing_budget_us -= (uint32_t)2500; } /* Sensor runs in autonomous low power mode */ - else if(inter_measurement_ms > timing_budget_ms) - { - status |= VL53L4ED_RdWord(dev, - VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + else if (inter_measurement_ms > timing_budget_ms) { + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); clock_pll = clock_pll & (uint16_t)0x3FF; - inter_measurement_factor = inter_measurement_factor - * (float_t)inter_measurement_ms - * (float_t)clock_pll; - status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, - (uint32_t)inter_measurement_factor); + inter_measurement_factor = inter_measurement_factor * (float_t)inter_measurement_ms * (float_t)clock_pll; + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, (uint32_t)inter_measurement_factor); timing_budget_us -= (uint32_t)4300; timing_budget_us /= (uint32_t)2; } /* Invalid case */ - else - { + else { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; } - if(status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) - { - ms_byte = 0; - timing_budget_us = timing_budget_us << 12; - tmp = macro_period_us*(uint32_t)16; - ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) - - (uint32_t)1; - - while ((ls_byte & 0xFFFFFF00U) > 0U) { - ls_byte = ls_byte >> 1; - ms_byte++; - } - ms_byte = (uint16_t)(ms_byte << 8) - + (uint16_t) (ls_byte & (uint32_t)0xFF); - status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A,ms_byte); - - ms_byte = 0; - tmp = macro_period_us*(uint32_t)12; - ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) - - (uint32_t)1; - - while ((ls_byte & 0xFFFFFF00U) > 0U) { - ls_byte = ls_byte >> 1; - ms_byte++; - } - ms_byte = (uint16_t)(ms_byte << 8) - + (uint16_t) (ls_byte & (uint32_t)0xFF); - status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B,ms_byte); + if (status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) { + ms_byte = 0; + timing_budget_us = timing_budget_us << 12; + tmp = macro_period_us * (uint32_t)16; + ls_byte = ((timing_budget_us + ((tmp >> 6) >> 1)) / (tmp >> 6)) - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + (uint16_t)(ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A, ms_byte); + + ms_byte = 0; + tmp = macro_period_us * (uint32_t)12; + ls_byte = ((timing_budget_us + ((tmp >> 6) >> 1)) / (tmp >> 6)) - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + (uint16_t)(ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B, ms_byte); } return status; } -VL53L4ED_Error VL53L4ED_GetRangeTiming( - Dev_t dev, - uint32_t *p_timing_budget_ms, - uint32_t *p_inter_measurement_ms) +VL53L4ED_Error VL53L4ED_GetRangeTiming(Dev_t dev, uint32_t *p_timing_budget_ms, uint32_t *p_inter_measurement_ms) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t osc_frequency = 1, range_config_macrop_high, clock_pll = 1; @@ -411,107 +358,84 @@ VL53L4ED_Error VL53L4ED_GetRangeTiming( /* Get InterMeasurement */ status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); - status |= VL53L4ED_RdWord(dev, - VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); clock_pll = clock_pll & (uint16_t)0x3FF; clock_pll_factor = clock_pll_factor * (float_t)clock_pll; clock_pll = (uint16_t)clock_pll_factor; - *p_inter_measurement_ms = (uint16_t)(tmp/(uint32_t)clock_pll); + *p_inter_measurement_ms = (uint16_t)(tmp / (uint32_t)clock_pll); /* Get TimingBudget */ status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); - status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, - &range_config_macrop_high); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, &range_config_macrop_high); - macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 - / (uint32_t)osc_frequency)) >> 6; + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; ls_byte = (range_config_macrop_high & (uint32_t)0x00FF) << 4; ms_byte = (range_config_macrop_high & (uint32_t)0xFF00) >> 8; ms_byte = (uint32_t)0x04 - (ms_byte - (uint32_t)1) - (uint32_t)1; macro_period_us = macro_period_us * (uint32_t)16; - *p_timing_budget_ms = (((ls_byte + (uint32_t)1)*(macro_period_us>> 6)) - - ((macro_period_us>> 6)>>1)) >> 12; + *p_timing_budget_ms = (((ls_byte + (uint32_t)1) * (macro_period_us >> 6)) - ((macro_period_us >> 6) >> 1)) >> 12; - if(ms_byte < (uint8_t)12) - { - *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms - >> (uint8_t)ms_byte); + if (ms_byte < (uint8_t)12) { + *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms >> (uint8_t)ms_byte); } - - /* Mode continuous */ - if(tmp == (uint32_t)0) - { + + /* Mode continuous */ + if (tmp == (uint32_t)0) { *p_timing_budget_ms += (uint32_t)2500; - } - /* Mode autonomous */ - else - { - *p_timing_budget_ms *= (uint32_t)2; - *p_timing_budget_ms += (uint32_t)4300; - } + } + /* Mode autonomous */ + else { + *p_timing_budget_ms *= (uint32_t)2; + *p_timing_budget_ms += (uint32_t)4300; + } - *p_timing_budget_ms = *p_timing_budget_ms/(uint32_t)1000; + *p_timing_budget_ms = *p_timing_budget_ms / (uint32_t)1000; return status; } -VL53L4ED_Error VL53L4ED_GetResult( - Dev_t dev, - VL53L4ED_ResultsData_t *p_result) +VL53L4ED_Error VL53L4ED_GetResult(Dev_t dev, VL53L4ED_ResultsData_t *p_result) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp_16; uint8_t temp_8; - uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, - 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, - 255, 255, 11, 12 }; + uint8_t status_rtn[24] = {255, 255, 255, 5, 2, 4, 1, 7, 3, 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, 255, 255, 11, 12}; - status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, - &temp_8); + status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, &temp_8); temp_8 = temp_8 & (uint8_t)0x1F; - if (temp_8 < (uint8_t)24) - { + if (temp_8 < (uint8_t)24) { temp_8 = status_rtn[temp_8]; } p_result->range_status = temp_8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, - &temp_16); - p_result->number_of_spad = temp_16 / (uint16_t) 256; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, &temp_16); + p_result->number_of_spad = temp_16 / (uint16_t)256; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, - &temp_16); - p_result->signal_rate_kcps = temp_16 * (uint16_t) 8; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, &temp_16); + p_result->signal_rate_kcps = temp_16 * (uint16_t)8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, - &temp_16); - p_result->ambient_rate_kcps = temp_16 * (uint16_t) 8; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, &temp_16); + p_result->ambient_rate_kcps = temp_16 * (uint16_t)8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, - &temp_16); - p_result->sigma_mm = temp_16 / (uint16_t) 4; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, &temp_16); + p_result->sigma_mm = temp_16 / (uint16_t)4; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, - &temp_16); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, &temp_16); p_result->distance_mm = temp_16; - p_result->signal_per_spad_kcps = p_result->signal_rate_kcps - /p_result->number_of_spad; - p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps - /p_result->number_of_spad; + p_result->signal_per_spad_kcps = p_result->signal_rate_kcps / p_result->number_of_spad; + p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps / p_result->number_of_spad; return status; } -VL53L4ED_Error VL53L4ED_SetOffset( - Dev_t dev, - int16_t OffsetValueInMm) +VL53L4ED_Error VL53L4ED_SetOffset(Dev_t dev, int16_t OffsetValueInMm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp; - temp = (uint16_t)((uint16_t)OffsetValueInMm*(uint16_t)4); + temp = (uint16_t)((uint16_t)OffsetValueInMm * (uint16_t)4); status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, temp); status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, (uint8_t)0x0); @@ -519,65 +443,49 @@ VL53L4ED_Error VL53L4ED_SetOffset( return status; } -VL53L4ED_Error VL53L4ED_GetOffset( - Dev_t dev, - int16_t *p_offset) +VL53L4ED_Error VL53L4ED_GetOffset(Dev_t dev, int16_t *p_offset) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp; - status |= VL53L4ED_RdWord(dev,VL53L4ED_RANGE_OFFSET_MM, &temp); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_OFFSET_MM, &temp); - temp = temp<<3; - temp = temp>>5; + temp = temp << 3; + temp = temp >> 5; *p_offset = (int16_t)(temp); - if(*p_offset > 1024) - { + if (*p_offset > 1024) { *p_offset = *p_offset - 2048; } return status; } -VL53L4ED_Error VL53L4ED_SetXtalk( - Dev_t dev, - uint16_t XtalkValueKcps) +VL53L4ED_Error VL53L4ED_SetXtalk(Dev_t dev, uint16_t XtalkValueKcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, - (XtalkValueKcps<<9)); - + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, (XtalkValueKcps << 9)); + return status; } -VL53L4ED_Error VL53L4ED_GetXtalk( - Dev_t dev, - uint16_t *p_xtalk_kcps) +VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; float_t tmp_xtalk; - status |= VL53L4ED_RdWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); - + status |= VL53L4ED_RdWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); + tmp_xtalk = (float_t)*p_xtalk_kcps / (float_t)512.0; *p_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); return status; } -VL53L4ED_Error VL53L4ED_SetDetectionThresholds( - Dev_t dev, - uint16_t distance_low_mm, - uint16_t distance_high_mm, - uint8_t window) +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, uint16_t distance_low_mm, uint16_t distance_high_mm, uint8_t window) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -587,14 +495,11 @@ VL53L4ED_Error VL53L4ED_SetDetectionThresholds( return status; } -VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, - uint16_t *p_distance_low_mm, - uint16_t *p_distance_high_mm, - uint8_t *p_window) +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, uint16_t *p_distance_low_mm, uint16_t *p_distance_high_mm, uint8_t *p_window) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH,p_distance_high_mm); + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH, p_distance_high_mm); status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_LOW, p_distance_low_mm); status |= VL53L4ED_RdByte(dev, VL53L4ED_SYSTEM__INTERRUPT, p_window); *p_window = (*p_window & (uint8_t)0x7); @@ -602,98 +507,78 @@ VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, return status; } -VL53L4ED_Error VL53L4ED_SetSignalThreshold( - Dev_t dev, - uint16_t signal_kcps) +VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrWord(dev, - VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS,signal_kcps>>3); + status |= VL53L4ED_WrWord(dev, VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, signal_kcps >> 3); return status; } -VL53L4ED_Error VL53L4ED_GetSignalThreshold( - Dev_t dev, - uint16_t *p_signal_kcps) +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, uint16_t *p_signal_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t tmp = 0; - status |= VL53L4ED_RdWord(dev, - VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); - *p_signal_kcps = tmp <<3; + status |= VL53L4ED_RdWord(dev, VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); + *p_signal_kcps = tmp << 3; return status; } -VL53L4ED_Error VL53L4ED_SetSigmaThreshold( - Dev_t dev, - uint16_t sigma_mm) +VL53L4ED_Error VL53L4ED_SetSigmaThreshold(Dev_t dev, uint16_t sigma_mm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - if(sigma_mm>(uint16_t)((uint16_t)0xFFFF>>2)) - { + if (sigma_mm > (uint16_t)((uint16_t)0xFFFF >> 2)) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { - status |= VL53L4ED_WrWord(dev, - VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm<<2); + } else { + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm << 2); } return status; } -VL53L4ED_Error VL53L4ED_GetSigmaThreshold( - Dev_t dev, - uint16_t *p_sigma_mm) +VL53L4ED_Error VL53L4ED_GetSigmaThreshold(Dev_t dev, uint16_t *p_sigma_mm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status += VL53L4ED_RdWord(dev, - VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); + status += VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); *p_sigma_mm = *p_sigma_mm >> 2; return status; } -VL53L4ED_Error VL53L4ED_StartTemperatureUpdate( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t tmp = 0, continue_loop = 1; uint16_t i = 0; - status |= VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); + status |= VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0x92); status |= VL53L4ED_StartRanging(dev); - do{ - status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ - { - continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for answer */ - { - i++; - } - else /* Timeout 1000ms reached */ - { - continue_loop = (uint8_t)0; - status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; - } - VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + do { + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if (tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } else if (i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_ClearInterrupt(dev); status |= VL53L4ED_StopRanging(dev); - status += VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); + status += VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); status += VL53L4ED_WrByte(dev, 0x0B, 0); return status; } diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h index df55ef3d0..e5d7acf2b 100644 --- a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_api.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #ifndef VL53L4ED_API_H_ #define VL53L4ED_API_H_ @@ -19,10 +19,10 @@ * @brief Driver version */ -#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 -#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 -#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 -#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 +#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 +#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 /** * @brief Driver error type @@ -30,58 +30,56 @@ typedef uint8_t VL53L4ED_Error; -#define VL53L4ED_ERROR_NONE ((uint8_t)0U) -#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) -#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) -#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) - +#define VL53L4ED_ERROR_NONE ((uint8_t)0U) +#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) +#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) +#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) /** * @brief Inner Macro for API. Not for user, only for development. */ #define VL53L4ED_SOFT_RESET ((uint16_t)0x0000)) -#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) -#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) +#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) +#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) #define VL53L4ED_XTALK_PLANE_OFFSET_KCPS ((uint16_t)0x0016) -#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) -#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) -#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) -#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) -#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) -#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) -#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) -#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) -#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) -#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) -#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) -#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) +#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) +#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) +#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) +#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) +#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) +#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) +#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) +#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) +#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) +#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) +#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) +#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) #define VL53L4ED_INTERMEASUREMENT_MS ((uint16_t)0x006C) -#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) -#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) -#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) -#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) -#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) -#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) -#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) -#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) -#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) -#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) - - -#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) -#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) -#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) +#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) +#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) +#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) +#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) +#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) +#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) +#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) +#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) +#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) +#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) + +#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) +#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) +#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) /** * @brief defines Software Version */ typedef struct { - uint8_t major; /*!< major number */ - uint8_t minor; /*!< minor number */ - uint8_t build; /*!< build number */ - uint32_t revision; /*!< revision number */ + uint8_t major; /*!< major number */ + uint8_t minor; /*!< minor number */ + uint8_t build; /*!< build number */ + uint32_t revision; /*!< revision number */ } VL53L4ED_Version_t; /** @@ -115,9 +113,7 @@ typedef struct { * @return (VL53L4ED_ERROR) status : 0 if SW version is OK. */ -VL53L4ED_Error VL53L4ED_GetSWVersion( - VL53L4ED_Version_t *pVersion); - +VL53L4ED_Error VL53L4ED_GetSWVersion(VL53L4ED_Version_t *pVersion); /** * @brief This function sets a new I2C address to a sensor. It can be used for @@ -128,9 +124,7 @@ VL53L4ED_Error VL53L4ED_GetSWVersion( * programmed. */ -VL53L4ED_Error VL53L4ED_SetI2CAddress( - Dev_t dev, - uint8_t new_address); +VL53L4ED_Error VL53L4ED_SetI2CAddress(Dev_t dev, uint8_t new_address); /** * @brief This function is used to get the sensor id of VL53L4ED. The sensor id @@ -140,9 +134,7 @@ VL53L4ED_Error VL53L4ED_SetI2CAddress( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetSensorId( - Dev_t dev, - uint16_t *p_id); +VL53L4ED_Error VL53L4ED_GetSensorId(Dev_t dev, uint16_t *p_id); /** * @brief This function is used to initialize the sensor. @@ -150,8 +142,7 @@ VL53L4ED_Error VL53L4ED_GetSensorId( * @return (VL53L4ED_ERROR) status : 0 if init is OK. */ -VL53L4ED_Error VL53L4ED_SensorInit( - Dev_t dev); +VL53L4ED_Error VL53L4ED_SensorInit(Dev_t dev); /** * @brief This function clears the interrupt. It needs to be called after a @@ -160,8 +151,7 @@ VL53L4ED_Error VL53L4ED_SensorInit( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_ClearInterrupt( - Dev_t dev); +VL53L4ED_Error VL53L4ED_ClearInterrupt(Dev_t dev); /** * @brief This function starts a ranging session. The ranging operation is @@ -171,8 +161,7 @@ VL53L4ED_Error VL53L4ED_ClearInterrupt( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_StartRanging( - Dev_t dev); +VL53L4ED_Error VL53L4ED_StartRanging(Dev_t dev); /** * @brief This function stops the ranging in progress. @@ -180,8 +169,7 @@ VL53L4ED_Error VL53L4ED_StartRanging( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_StopRanging( - Dev_t dev); +VL53L4ED_Error VL53L4ED_StopRanging(Dev_t dev); /** * @brief This function check if a new data is available by polling a dedicated @@ -192,9 +180,7 @@ VL53L4ED_Error VL53L4ED_StopRanging( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_CheckForDataReady( - Dev_t dev, - uint8_t *p_is_data_ready); +VL53L4ED_Error VL53L4ED_CheckForDataReady(Dev_t dev, uint8_t *p_is_data_ready); /** * @brief This function sets new range timing. Timing are composed of @@ -213,10 +199,7 @@ VL53L4ED_Error VL53L4ED_CheckForDataReady( * @return (uint8_t) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_SetRangeTiming( - Dev_t dev, - uint32_t timing_budget_ms, - uint32_t inter_measurement_ms); +VL53L4ED_Error VL53L4ED_SetRangeTiming(Dev_t dev, uint32_t timing_budget_ms, uint32_t inter_measurement_ms); /** * @brief This function gets the current range timing. Timing are composed of @@ -232,10 +215,7 @@ VL53L4ED_Error VL53L4ED_SetRangeTiming( * @return (uint8_t) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetRangeTiming( - Dev_t dev, - uint32_t *p_timing_budget_ms, - uint32_t *p_inter_measurement_ms); +VL53L4ED_Error VL53L4ED_GetRangeTiming(Dev_t dev, uint32_t *p_timing_budget_ms, uint32_t *p_inter_measurement_ms); /** * @brief This function gets the results reported by the sensor. @@ -310,11 +290,7 @@ VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps); * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, - uint16_t distance_low_mm, - uint16_t distance_high_mm, - uint8_t window); - +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, uint16_t distance_low_mm, uint16_t distance_high_mm, uint8_t window); /** * @brief This function gets the current detection thresholds. The detection @@ -330,10 +306,7 @@ VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, - uint16_t *p_distance_low_mm, - uint16_t *p_distance_high_mm, - uint8_t *p_window); +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, uint16_t *p_distance_low_mm, uint16_t *p_distance_high_mm, uint8_t *p_window); /** * @brief This function sets a new signal threshold in kcps. If a @@ -357,8 +330,7 @@ VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps); * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, - uint16_t *p_signal_kcps); +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, uint16_t *p_signal_kcps); /** * @brief This function programs a new sigma threshold. The sigma corresponds to @@ -372,9 +344,7 @@ VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, * high. */ -VL53L4ED_Error VL53L4ED_SetSigmaThreshold( - Dev_t dev, - uint16_t sigma_mm); +VL53L4ED_Error VL53L4ED_SetSigmaThreshold(Dev_t dev, uint16_t sigma_mm); /** * @brief This function gets the current sigma threshold. The sigma corresponds @@ -386,9 +356,7 @@ VL53L4ED_Error VL53L4ED_SetSigmaThreshold( * @return (VL53L4ED_ERROR) status : 0 if programming is OK. */ -VL53L4ED_Error VL53L4ED_GetSigmaThreshold( - Dev_t dev, - uint16_t *p_sigma_mm); +VL53L4ED_Error VL53L4ED_GetSigmaThreshold(Dev_t dev, uint16_t *p_sigma_mm); /** * @brief This function can be called when the temperature might have changed by @@ -402,4 +370,4 @@ VL53L4ED_Error VL53L4ED_GetSigmaThreshold( VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev); -#endif //VL53L4ED_API_H_ +#endif // VL53L4ED_API_H_ diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c index e3d95c223..6a0d96eb1 100644 --- a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.c @@ -1,29 +1,27 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_calibration.c * @brief Calibration functions implementation */ +#include "VL53L4ED_calibration.h" + #include + #include "VL53L4ED_api.h" -#include "VL53L4ED_calibration.h" -VL53L4ED_Error VL53L4ED_CalibrateOffset( - Dev_t dev, - int16_t TargetDistInMm, - int16_t *p_measured_offset_mm, - int16_t nb_samples) +VL53L4ED_Error VL53L4ED_CalibrateOffset(Dev_t dev, int16_t TargetDistInMm, int16_t *p_measured_offset_mm, int16_t nb_samples) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t i, tmp, continue_loop; @@ -31,14 +29,9 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( int16_t AvgDistance = 0; VL53L4ED_ResultsData_t results; - if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) - || ((TargetDistInMm < (int16_t)10) - || (TargetDistInMm > (int16_t)1000))) - { + if (((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) || ((TargetDistInMm < (int16_t)10) || (TargetDistInMm > (int16_t)1000))) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { + } else { status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, 0x0); status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, 0x0); status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, 0x0); @@ -49,23 +42,21 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); } @@ -77,23 +68,21 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { - continue_loop = (uint8_t)0; + continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); @@ -103,18 +92,14 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( status |= VL53L4ED_StopRanging(dev); AvgDistance = AvgDistance / nb_samples; *p_measured_offset_mm = (int16_t)TargetDistInMm - AvgDistance; - tmpOff = (uint16_t) *p_measured_offset_mm * (uint16_t)4; + tmpOff = (uint16_t)*p_measured_offset_mm * (uint16_t)4; status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, tmpOff); } return status; } -VL53L4ED_Error VL53L4ED_CalibrateXtalk( - Dev_t dev, - int16_t TargetDistInMm, - uint16_t *p_measured_xtalk_kcps, - int16_t nb_samples) +VL53L4ED_Error VL53L4ED_CalibrateXtalk(Dev_t dev, int16_t TargetDistInMm, uint16_t *p_measured_xtalk_kcps, int16_t nb_samples) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t i, tmp, continue_loop; @@ -128,17 +113,11 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( uint16_t calXtalk, j; *p_measured_xtalk_kcps = 0; - if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) - || ((TargetDistInMm < (int16_t)10) - || (TargetDistInMm > (int16_t)5000))) - { + if (((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) || ((TargetDistInMm < (int16_t)10) || (TargetDistInMm > (int16_t)5000))) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { + } else { /* Disable Xtalk compensation */ - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); /* Device heat loop (10 samples) */ status |= VL53L4ED_StartRanging(dev); @@ -146,23 +125,21 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); } @@ -170,36 +147,31 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( /* Device ranging loop */ status |= VL53L4ED_StartRanging(dev); - for (i = 0; i < (uint8_t)nb_samples; i++) - { + for (i = 0; i < (uint8_t)nb_samples; i++) { tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { - continue_loop = (uint8_t)0; + continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); /* Discard invalid measurements and first frame */ - if (results.range_status == (uint8_t)0 - && i > (uint8_t)0) - { + if (results.range_status == (uint8_t)0 && i > (uint8_t)0) { AvgDistance += (float_t)results.distance_mm; AverageSpadNb += (float_t)results.number_of_spad; AverageSignal += (float_t)results.signal_rate_kcps; @@ -208,32 +180,25 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( } status |= VL53L4ED_StopRanging(dev); - if (CounterNbSamples == 0) - { + if (CounterNbSamples == 0) { status = VL53L4ED_ERROR_XTALK_FAILED; - } - else - { + } else { AvgDistance /= CounterNbSamples; AverageSpadNb /= CounterNbSamples; AverageSignal /= CounterNbSamples; - tmp_xtalk = (float_t)1.0 - (AvgDistance/TargetDistance); - tmp_xtalk *= (AverageSignal/AverageSpadNb); + tmp_xtalk = (float_t)1.0 - (AvgDistance / TargetDistance); + tmp_xtalk *= (AverageSignal / AverageSpadNb); /* 127kcps is the max Xtalk value (65536/512) */ - if(tmp_xtalk > (uint16_t)127) - { + if (tmp_xtalk > (uint16_t)127) { status = VL53L4ED_ERROR_XTALK_FAILED; - } - else - { + } else { *p_measured_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); /* Send data to firmware */ calXtalk = (uint16_t)(tmp_xtalk * (float_t)512.0); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); } } } diff --git a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h index c953d0239..97daa1d3c 100644 --- a/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h +++ b/SAMM/Core/Extras/VL53L4ED/VL53L4ED_calibration.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_calibration.h @@ -38,12 +38,7 @@ * invalid nb of samples). */ -VL53L4ED_Error VL53L4ED_CalibrateOffset( - Dev_t dev, - int16_t TargetDistInMm, - int16_t *p_measured_offset_mm, - int16_t nb_samples); - +VL53L4ED_Error VL53L4ED_CalibrateOffset(Dev_t dev, int16_t TargetDistInMm, int16_t *p_measured_offset_mm, int16_t nb_samples); /** * @brief This function can be used to perform a Xtalk calibration. Xtalk @@ -63,11 +58,7 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g * invalid nb of samples). */ - -VL53L4ED_Error VL53L4ED_CalibrateXtalk( - Dev_t dev, - int16_t TargetDistInMm, - uint16_t *p_measured_xtalk_kcps, - int16_t nb_samples); -#endif //VL53L4ED_CALIBRATION_H_ +VL53L4ED_Error VL53L4ED_CalibrateXtalk(Dev_t dev, int16_t TargetDistInMm, uint16_t *p_measured_xtalk_kcps, int16_t nb_samples); + +#endif // VL53L4ED_CALIBRATION_H_ diff --git a/SAMM/Core/Extras/VL53L4ED/platform.c b/SAMM/Core/Extras/VL53L4ED/platform.c index ba3d6f1a6..cf09e50e7 100644 --- a/SAMM/Core/Extras/VL53L4ED/platform.c +++ b/SAMM/Core/Extras/VL53L4ED/platform.c @@ -1,25 +1,25 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #include "platform.h" -#include "main.h" + #include "i2c.h" +#include "main.h" extern I2C_HandleTypeDef hi2c1; - /* Im legit just using the example stm provides but instead of filling everything out im only copying the platform.c implemintation -for the vl53l4ed sensor. +for the vl53l4ed sensor. */ @@ -34,8 +34,7 @@ uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t *value) data_write[1] = RegisterAdress & 0xFF; status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 4, 100); - *value = ((data_read[0] << 24) | (data_read[1]<<16) | - (data_read[2]<<8)| (data_read[3])); + *value = ((data_read[0] << 24) | (data_read[1] << 16) | (data_read[2] << 8) | (data_read[3])); return status; } diff --git a/SAMM/Core/Extras/VL53L4ED/platform.h b/SAMM/Core/Extras/VL53L4ED/platform.h index ee27e91f2..9c7754970 100644 --- a/SAMM/Core/Extras/VL53L4ED/platform.h +++ b/SAMM/Core/Extras/VL53L4ED/platform.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #ifndef _PLATFORM_H_ #define _PLATFORM_H_ @@ -18,8 +18,8 @@ #include /** -* VL53L4ED device instance. -*/ + * VL53L4ED device instance. + */ typedef uint16_t Dev_t; @@ -33,8 +33,7 @@ typedef uint8_t VL53L4ED_Error; * with I2C Fast Mode Plus (up to 1MHz). Otherwise, default max value is 400kHz. */ -//#define VL53L4ED_I2C_FAST_MODE_PLUS - +// #define VL53L4ED_I2C_FAST_MODE_PLUS /** * @brief Read 32 bits through I2C. @@ -77,4 +76,4 @@ uint8_t VL53L4ED_WrDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t value); uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t TimeMs); -#endif // _PLATFORM_H_ \ No newline at end of file +#endif // _PLATFORM_H_ \ No newline at end of file diff --git a/SAMM/Core/Extras/extra.c b/SAMM/Core/Extras/extra.c index 3611c8904..25519615c 100644 --- a/SAMM/Core/Extras/extra.c +++ b/SAMM/Core/Extras/extra.c @@ -1,58 +1,57 @@ #include "extra.h" -#include "main.h" -#include "i2c.h" + #include +#include "i2c.h" +#include "main.h" void MLX90640_I2CInit(void) { - MX_I2C2_Init(); - return; + MX_I2C2_Init(); + return; } int MLX90640_I2CGeneralReset(void) { - uint8_t data = 0x06; - return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data , 1, 1000); + uint8_t data = 0x06; + return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data, 1, 1000); } - -int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) { - //HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); - //HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); - /* - HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, - uint16_t MemAddSize, uint8_t *pData, uint16_t Size) - */ - //return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); - return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 10000); + // HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); + // HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); + /* + HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size) + */ + // return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2 * nMemAddressRead, 10000); } int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) { - // union - // { - // uint8_t bytes[2]; - // uint16_t data; - // }extra = {.data = data}; - return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); - //return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); + // union + // { + // uint8_t bytes[2]; + // uint16_t data; + // }extra = {.data = data}; + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); + // return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); } void MLX90640_I2CFreqSet(int freq) { - return; + return; } -uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +uint8_t I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) { - return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 500); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2 * nMemAddressRead, 500); } -uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data) +uint8_t I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) { - return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *) &data, 2, 500); + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 500); } - diff --git a/SAMM/Core/Extras/extra.h b/SAMM/Core/Extras/extra.h index 2b820f432..f032f86ab 100644 --- a/SAMM/Core/Extras/extra.h +++ b/SAMM/Core/Extras/extra.h @@ -9,12 +9,11 @@ This is for some xtra fucntions that would be needed later on void MLX90640_I2CInit(void); int MLX90640_I2CGeneralReset(void); -int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); -int MLX90640_I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data); void MLX90640_I2CFreqSet(int freq); - -uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); -uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +uint8_t I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +uint8_t I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data); #endif /* __EXTRA_H__ */ \ No newline at end of file diff --git a/SAMM/Core/Inc/adc.h b/SAMM/Core/Inc/adc.h index 5692f3846..e65b1f6f8 100644 --- a/SAMM/Core/Inc/adc.h +++ b/SAMM/Core/Inc/adc.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file adc.h - * @brief This file contains all the function prototypes for - * the adc.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file adc.h + * @brief This file contains all the function prototypes for + * the adc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __ADC_H__ @@ -49,4 +49,3 @@ void MX_ADC1_Init(void); #endif #endif /* __ADC_H__ */ - diff --git a/SAMM/Core/Inc/crc.h b/SAMM/Core/Inc/crc.h index 836835d02..133957e92 100644 --- a/SAMM/Core/Inc/crc.h +++ b/SAMM/Core/Inc/crc.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file crc.h - * @brief This file contains all the function prototypes for - * the crc.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file crc.h + * @brief This file contains all the function prototypes for + * the crc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __CRC_H__ @@ -49,4 +49,3 @@ void MX_CRC_Init(void); #endif #endif /* __CRC_H__ */ - diff --git a/SAMM/Core/Inc/dma.h b/SAMM/Core/Inc/dma.h index f3b1a09f9..7774831e0 100644 --- a/SAMM/Core/Inc/dma.h +++ b/SAMM/Core/Inc/dma.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file dma.h - * @brief This file contains all the function prototypes for - * the dma.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file dma.h + * @brief This file contains all the function prototypes for + * the dma.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __DMA_H__ @@ -49,4 +49,3 @@ void MX_DMA_Init(void); #endif #endif /* __DMA_H__ */ - diff --git a/SAMM/Core/Inc/fdcan.h b/SAMM/Core/Inc/fdcan.h index 9289ea3a6..a5cc22f04 100644 --- a/SAMM/Core/Inc/fdcan.h +++ b/SAMM/Core/Inc/fdcan.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file fdcan.h - * @brief This file contains all the function prototypes for - * the fdcan.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file fdcan.h + * @brief This file contains all the function prototypes for + * the fdcan.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __FDCAN_H__ @@ -52,4 +52,3 @@ void MX_FDCAN2_Init(void); #endif #endif /* __FDCAN_H__ */ - diff --git a/SAMM/Core/Inc/gpio.h b/SAMM/Core/Inc/gpio.h index 708bac71d..843d4e9e7 100644 --- a/SAMM/Core/Inc/gpio.h +++ b/SAMM/Core/Inc/gpio.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file gpio.h - * @brief This file contains all the function prototypes for - * the gpio.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __GPIO_H__ @@ -46,4 +46,3 @@ void MX_GPIO_Init(void); } #endif #endif /*__ GPIO_H__ */ - diff --git a/SAMM/Core/Inc/i2c.h b/SAMM/Core/Inc/i2c.h index 84ed75d2e..64f5543e6 100644 --- a/SAMM/Core/Inc/i2c.h +++ b/SAMM/Core/Inc/i2c.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file i2c.h - * @brief This file contains all the function prototypes for - * the i2c.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file i2c.h + * @brief This file contains all the function prototypes for + * the i2c.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __I2C_H__ @@ -49,4 +49,3 @@ void MX_I2C1_Init(void); #endif #endif /* __I2C_H__ */ - diff --git a/SAMM/Core/Inc/main.h b/SAMM/Core/Inc/main.h index 17a2fe760..32df0ac37 100644 --- a/SAMM/Core/Inc/main.h +++ b/SAMM/Core/Inc/main.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.h - * @brief : Header for main.c file. - * This file contains the common defines of the application. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 -------------------------------------*/ @@ -28,17 +28,16 @@ extern "C" { /* Includes ------------------------------------------------------------------*/ #include "stm32g4xx_hal.h" - -#include "stm32g4xx_ll_rcc.h" #include "stm32g4xx_ll_bus.h" -#include "stm32g4xx_ll_crs.h" -#include "stm32g4xx_ll_system.h" -#include "stm32g4xx_ll_exti.h" #include "stm32g4xx_ll_cortex.h" -#include "stm32g4xx_ll_utils.h" -#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_crs.h" #include "stm32g4xx_ll_dma.h" +#include "stm32g4xx_ll_exti.h" #include "stm32g4xx_ll_gpio.h" +#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_rcc.h" +#include "stm32g4xx_ll_system.h" +#include "stm32g4xx_ll_utils.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ diff --git a/SAMM/Core/Inc/spi.h b/SAMM/Core/Inc/spi.h index bb37a07dc..51a96c6e5 100644 --- a/SAMM/Core/Inc/spi.h +++ b/SAMM/Core/Inc/spi.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file spi.h - * @brief This file contains all the function prototypes for - * the spi.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file spi.h + * @brief This file contains all the function prototypes for + * the spi.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __SPI_H__ @@ -49,4 +49,3 @@ void MX_SPI3_Init(void); #endif #endif /* __SPI_H__ */ - diff --git a/SAMM/Core/Inc/stm32_assert.h b/SAMM/Core/Inc/stm32_assert.h index 61631c41e..92460620f 100644 --- a/SAMM/Core/Inc/stm32_assert.h +++ b/SAMM/Core/Inc/stm32_assert.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32_assert.h - * @author MCD Application Team - * @brief STM32 assert file. - ****************************************************************************** - * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32_assert.h + * @author MCD Application Team + * @brief STM32 assert file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 __STM32_ASSERT_H @@ -29,15 +29,15 @@ extern "C" { /* Exported constants --------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ void assert_failed(uint8_t *file, uint32_t line); @@ -50,4 +50,3 @@ void assert_failed(uint8_t *file, uint32_t line); #endif #endif /* __STM32_ASSERT_H */ - diff --git a/SAMM/Core/Inc/stm32g4xx_hal_conf.h b/SAMM/Core/Inc/stm32g4xx_hal_conf.h index 4087e183c..af665f477 100644 --- a/SAMM/Core/Inc/stm32g4xx_hal_conf.h +++ b/SAMM/Core/Inc/stm32g4xx_hal_conf.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_conf.h - * @author MCD Application Team - * @brief HAL configuration file - ****************************************************************************** + ****************************************************************************** + * @file stm32g4xx_hal_conf.h + * @author MCD Application Team + * @brief HAL configuration file + ****************************************************************************** * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + * + * Copyright (c) 2019 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 -------------------------------------*/ @@ -23,7 +23,7 @@ #define STM32G4xx_HAL_CONF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Exported types ------------------------------------------------------------*/ @@ -31,12 +31,12 @@ /* ########################## Module Selection ############################## */ /** - * @brief This is the list of modules to be used in the HAL driver - */ + * @brief This is the list of modules to be used in the HAL driver + */ #define HAL_MODULE_ENABLED - /*#define HAL_ADC_MODULE_ENABLED */ +/*#define HAL_ADC_MODULE_ENABLED */ /*#define HAL_COMP_MODULE_ENABLED */ /*#define HAL_CORDIC_MODULE_ENABLED */ #define HAL_CRC_MODULE_ENABLED @@ -76,122 +76,123 @@ /* ########################## Register Callbacks selection ############################## */ /** - * @brief This is the list of modules where register callback can be used - */ -#define USE_HAL_ADC_REGISTER_CALLBACKS 0U -#define USE_HAL_COMP_REGISTER_CALLBACKS 0U -#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U -#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U -#define USE_HAL_DAC_REGISTER_CALLBACKS 0U -#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U -#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U -#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U -#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U -#define USE_HAL_I2C_REGISTER_CALLBACKS 0U -#define USE_HAL_I2S_REGISTER_CALLBACKS 0U -#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U -#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U -#define USE_HAL_NAND_REGISTER_CALLBACKS 0U -#define USE_HAL_NOR_REGISTER_CALLBACKS 0U -#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U -#define USE_HAL_PCD_REGISTER_CALLBACKS 0U -#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U -#define USE_HAL_RNG_REGISTER_CALLBACKS 0U -#define USE_HAL_RTC_REGISTER_CALLBACKS 0U -#define USE_HAL_SAI_REGISTER_CALLBACKS 0U -#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U -#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U -#define USE_HAL_SPI_REGISTER_CALLBACKS 0U -#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U -#define USE_HAL_TIM_REGISTER_CALLBACKS 0U -#define USE_HAL_UART_REGISTER_CALLBACKS 0U -#define USE_HAL_USART_REGISTER_CALLBACKS 0U -#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U + * @brief This is the list of modules where register callback can be used + */ +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U +#define USE_HAL_COMP_REGISTER_CALLBACKS 0U +#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U +#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U +#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U +#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U +#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U +#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U +#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_NAND_REGISTER_CALLBACKS 0U +#define USE_HAL_NOR_REGISTER_CALLBACKS 0U +#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U +#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U +#define USE_HAL_RNG_REGISTER_CALLBACKS 0U +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U +#define USE_HAL_SAI_REGISTER_CALLBACKS 0U +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U +#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U +#define USE_HAL_UART_REGISTER_CALLBACKS 0U +#define USE_HAL_USART_REGISTER_CALLBACKS 0U +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* ########################## Oscillator Values adaptation ####################*/ /** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined(HSE_VALUE) +#define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined(HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ /** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined(HSI_VALUE) +#define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ /** - * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. - * This internal oscillator is mainly dedicated to provide a high precision clock to - * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. - * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency - * which is subject to manufacturing process variations. - */ -#if !defined (HSI48_VALUE) - #define HSI48_VALUE (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. - The real value my vary depending on manufacturing process variations.*/ -#endif /* HSI48_VALUE */ + * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. + * This internal oscillator is mainly dedicated to provide a high precision clock to + * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. + * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency + * which is subject to manufacturing process variations. + */ +#if !defined(HSI48_VALUE) +#define HSI48_VALUE \ + (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. \ + The real value my vary depending on manufacturing process variations.*/ +#endif /* HSI48_VALUE */ /** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined(LSI_VALUE) /*!< Value of the Internal Low Speed oscillator in Hz The real value may vary depending on the variations in voltage and temperature.*/ -#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ -#endif /* LSI_VALUE */ +#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ /** - * @brief External Low Speed oscillator (LSE) value. - * This value is used by the UART, RTC HAL module to compute the system frequency - */ -#if !defined (LSE_VALUE) -#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined(LSE_VALUE) +#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ -#if !defined (LSE_STARTUP_TIMEOUT) -#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ +#if !defined(LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ /** - * @brief External clock source for I2S and SAI peripherals - * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) -#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ + * @brief External clock source for I2S and SAI peripherals + * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined(EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ /* Tip: To avoid modifying this file each time you need to use different HSE, === you can define the HSE value in your toolchain compiler preprocessor. */ /* ########################### System Configuration ######################### */ /** - * @brief This is the HAL system configuration section - */ + * @brief This is the HAL system configuration section + */ -#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ -#define USE_RTOS 0U -#define PREFETCH_ENABLE 0U -#define INSTRUCTION_CACHE_ENABLE 1U -#define DATA_CACHE_ENABLE 1U +#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 0U +#define INSTRUCTION_CACHE_ENABLE 1U +#define DATA_CACHE_ENABLE 1U /* ########################## Assert Selection ############################## */ /** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ /* #define USE_FULL_ASSERT 1U */ /* ################## SPI peripheral configuration ########################## */ @@ -201,12 +202,12 @@ The real value may vary depending on the variations in voltage and temperature.* * Deactivated: CRC code cleaned from driver */ -#define USE_SPI_CRC 0U +#define USE_SPI_CRC 0U /* Includes ------------------------------------------------------------------*/ /** - * @brief Include module's header file - */ + * @brief Include module's header file + */ #ifdef HAL_RCC_MODULE_ENABLED #include "stm32g4xx_hal_rcc.h" @@ -357,15 +358,15 @@ The real value may vary depending on the variations in voltage and temperature.* #endif /* HAL_WWDG_MODULE_ENABLED */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ void assert_failed(uint8_t *file, uint32_t line); diff --git a/SAMM/Core/Inc/stm32g4xx_it.h b/SAMM/Core/Inc/stm32g4xx_it.h index d5d87eab3..13530cb56 100644 --- a/SAMM/Core/Inc/stm32g4xx_it.h +++ b/SAMM/Core/Inc/stm32g4xx_it.h @@ -1,20 +1,20 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_it.h - * @brief This file contains the headers of the interrupt handlers. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 -------------------------------------*/ @@ -22,7 +22,7 @@ #define __STM32G4xx_IT_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Private includes ----------------------------------------------------------*/ diff --git a/SAMM/Core/Src/adc.c b/SAMM/Core/Src/adc.c index 15733df47..fbcfbe933 100644 --- a/SAMM/Core/Src/adc.c +++ b/SAMM/Core/Src/adc.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file adc.c - * @brief This file provides code for the configuration - * of the ADC instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file adc.c + * @brief This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "adc.h" @@ -30,123 +30,117 @@ ADC_HandleTypeDef hadc1; void MX_ADC1_Init(void) { - /* USER CODE BEGIN ADC1_Init 0 */ - - /* USER CODE END ADC1_Init 0 */ - - ADC_MultiModeTypeDef multimode = {0}; - ADC_ChannelConfTypeDef sConfig = {0}; - - /* USER CODE BEGIN ADC1_Init 1 */ - - /* USER CODE END ADC1_Init 1 */ - - /** Common config - */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.GainCompensation = 0; - hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc1.Init.LowPowerAutoWait = DISABLE; - hadc1.Init.ContinuousConvMode = DISABLE; - hadc1.Init.NbrOfConversion = 1; - hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc1.Init.DMAContinuousRequests = DISABLE; - hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; - hadc1.Init.OversamplingMode = ENABLE; - hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; - hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; - hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; - hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; - if (HAL_ADC_Init(&hadc1) != HAL_OK) - { - Error_Handler(); - } - - /** Configure the ADC multi-mode - */ - multimode.Mode = ADC_MODE_INDEPENDENT; - if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Regular Channel - */ - sConfig.Channel = ADC_CHANNEL_7; - sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; - sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; - sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN ADC1_Init 2 */ - - /* USER CODE END ADC1_Init 2 */ - + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.GainCompensation = 0; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.OversamplingMode = ENABLE; + hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; + hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; + hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; + hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; + sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ } -void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspInit 0 */ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspInit 0 */ - /* USER CODE END ADC1_MspInit 0 */ - LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); + /* USER CODE END ADC1_MspInit 0 */ + LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); - /* ADC1 clock enable */ - __HAL_RCC_ADC12_CLK_ENABLE(); + /* ADC1 clock enable */ + __HAL_RCC_ADC12_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - /**ADC1 GPIO Configuration - PC0 ------> ADC1_IN6 - PC1 ------> ADC1_IN7 - PC2 ------> ADC1_IN8 - */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2; - GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - /* USER CODE BEGIN ADC1_MspInit 1 */ + /* USER CODE BEGIN ADC1_MspInit 1 */ - /* USER CODE END ADC1_MspInit 1 */ - } + /* USER CODE END ADC1_MspInit 1 */ + } } -void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +void HAL_ADC_MspDeInit(ADC_HandleTypeDef *adcHandle) { - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspDeInit 0 */ + if (adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ - /* USER CODE END ADC1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_ADC12_CLK_DISABLE(); + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC12_CLK_DISABLE(); - /**ADC1 GPIO Configuration - PC0 ------> ADC1_IN6 - PC1 ------> ADC1_IN7 - PC2 ------> ADC1_IN8 - */ - HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2); - /* USER CODE BEGIN ADC1_MspDeInit 1 */ + /* USER CODE BEGIN ADC1_MspDeInit 1 */ - /* USER CODE END ADC1_MspDeInit 1 */ - } + /* USER CODE END ADC1_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/crc.c b/SAMM/Core/Src/crc.c index 0a8907607..213510a5d 100644 --- a/SAMM/Core/Src/crc.c +++ b/SAMM/Core/Src/crc.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file crc.c - * @brief This file provides code for the configuration - * of the CRC instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file crc.c + * @brief This file provides code for the configuration + * of the CRC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "crc.h" @@ -30,59 +30,55 @@ CRC_HandleTypeDef hcrc; void MX_CRC_Init(void) { - /* USER CODE BEGIN CRC_Init 0 */ + /* USER CODE BEGIN CRC_Init 0 */ - /* USER CODE END CRC_Init 0 */ + /* USER CODE END CRC_Init 0 */ - /* USER CODE BEGIN CRC_Init 1 */ + /* USER CODE BEGIN CRC_Init 1 */ - /* USER CODE END CRC_Init 1 */ - hcrc.Instance = CRC; - hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; - hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; - hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; - hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; - hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; - if (HAL_CRC_Init(&hcrc) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN CRC_Init 2 */ - - /* USER CODE END CRC_Init 2 */ + /* USER CODE END CRC_Init 1 */ + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; + if (HAL_CRC_Init(&hcrc) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN CRC_Init 2 */ + /* USER CODE END CRC_Init 2 */ } -void HAL_CRC_MspInit(CRC_HandleTypeDef* crcHandle) +void HAL_CRC_MspInit(CRC_HandleTypeDef *crcHandle) { - if(crcHandle->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspInit 0 */ + if (crcHandle->Instance == CRC) { + /* USER CODE BEGIN CRC_MspInit 0 */ - /* USER CODE END CRC_MspInit 0 */ - /* CRC clock enable */ - __HAL_RCC_CRC_CLK_ENABLE(); - /* USER CODE BEGIN CRC_MspInit 1 */ + /* USER CODE END CRC_MspInit 0 */ + /* CRC clock enable */ + __HAL_RCC_CRC_CLK_ENABLE(); + /* USER CODE BEGIN CRC_MspInit 1 */ - /* USER CODE END CRC_MspInit 1 */ - } + /* USER CODE END CRC_MspInit 1 */ + } } -void HAL_CRC_MspDeInit(CRC_HandleTypeDef* crcHandle) +void HAL_CRC_MspDeInit(CRC_HandleTypeDef *crcHandle) { - if(crcHandle->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspDeInit 0 */ + if (crcHandle->Instance == CRC) { + /* USER CODE BEGIN CRC_MspDeInit 0 */ - /* USER CODE END CRC_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_CRC_CLK_DISABLE(); - /* USER CODE BEGIN CRC_MspDeInit 1 */ + /* USER CODE END CRC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_CRC_CLK_DISABLE(); + /* USER CODE BEGIN CRC_MspDeInit 1 */ - /* USER CODE END CRC_MspDeInit 1 */ - } + /* USER CODE END CRC_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/dma.c b/SAMM/Core/Src/dma.c index 372b7e92c..0b82d0d7c 100644 --- a/SAMM/Core/Src/dma.c +++ b/SAMM/Core/Src/dma.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file dma.c - * @brief This file provides code for the configuration - * of all the requested memory to memory DMA transfers. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file dma.c + * @brief This file provides code for the configuration + * of all the requested memory to memory DMA transfers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -34,26 +34,24 @@ /* USER CODE END 1 */ /** - * Enable DMA controller clock - */ + * Enable DMA controller clock + */ void MX_DMA_Init(void) { - /* DMA controller clock enable */ - __HAL_RCC_DMAMUX1_CLK_ENABLE(); - __HAL_RCC_DMA1_CLK_ENABLE(); - - /* DMA interrupt init */ - /* DMA1_Channel1_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); - /* DMA1_Channel2_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); - + /* DMA controller clock enable */ + __HAL_RCC_DMAMUX1_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Channel1_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + /* DMA1_Channel2_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); } /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ - diff --git a/SAMM/Core/Src/fdcan.c b/SAMM/Core/Src/fdcan.c index 59cf13f59..600df963c 100644 --- a/SAMM/Core/Src/fdcan.c +++ b/SAMM/Core/Src/fdcan.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file fdcan.c - * @brief This file provides code for the configuration - * of the FDCAN instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file fdcan.c + * @brief This file provides code for the configuration + * of the FDCAN instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "fdcan.h" @@ -31,191 +31,181 @@ FDCAN_HandleTypeDef hfdcan2; void MX_FDCAN1_Init(void) { - /* USER CODE BEGIN FDCAN1_Init 0 */ - - /* USER CODE END FDCAN1_Init 0 */ - - /* USER CODE BEGIN FDCAN1_Init 1 */ - - /* USER CODE END FDCAN1_Init 1 */ - hfdcan1.Instance = FDCAN1; - hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; - hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; - hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; - hfdcan1.Init.AutoRetransmission = DISABLE; - hfdcan1.Init.TransmitPause = DISABLE; - hfdcan1.Init.ProtocolException = DISABLE; - hfdcan1.Init.NominalPrescaler = 16; - hfdcan1.Init.NominalSyncJumpWidth = 1; - hfdcan1.Init.NominalTimeSeg1 = 1; - hfdcan1.Init.NominalTimeSeg2 = 1; - hfdcan1.Init.DataPrescaler = 1; - hfdcan1.Init.DataSyncJumpWidth = 1; - hfdcan1.Init.DataTimeSeg1 = 1; - hfdcan1.Init.DataTimeSeg2 = 1; - hfdcan1.Init.StdFiltersNbr = 0; - hfdcan1.Init.ExtFiltersNbr = 0; - hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; - if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN FDCAN1_Init 2 */ - - /* USER CODE END FDCAN1_Init 2 */ - + /* USER CODE BEGIN FDCAN1_Init 0 */ + + /* USER CODE END FDCAN1_Init 0 */ + + /* USER CODE BEGIN FDCAN1_Init 1 */ + + /* USER CODE END FDCAN1_Init 1 */ + hfdcan1.Instance = FDCAN1; + hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan1.Init.AutoRetransmission = DISABLE; + hfdcan1.Init.TransmitPause = DISABLE; + hfdcan1.Init.ProtocolException = DISABLE; + hfdcan1.Init.NominalPrescaler = 16; + hfdcan1.Init.NominalSyncJumpWidth = 1; + hfdcan1.Init.NominalTimeSeg1 = 1; + hfdcan1.Init.NominalTimeSeg2 = 1; + hfdcan1.Init.DataPrescaler = 1; + hfdcan1.Init.DataSyncJumpWidth = 1; + hfdcan1.Init.DataTimeSeg1 = 1; + hfdcan1.Init.DataTimeSeg2 = 1; + hfdcan1.Init.StdFiltersNbr = 0; + hfdcan1.Init.ExtFiltersNbr = 0; + hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN1_Init 2 */ + + /* USER CODE END FDCAN1_Init 2 */ } /* FDCAN2 init function */ void MX_FDCAN2_Init(void) { - /* USER CODE BEGIN FDCAN2_Init 0 */ - - /* USER CODE END FDCAN2_Init 0 */ - - /* USER CODE BEGIN FDCAN2_Init 1 */ - - /* USER CODE END FDCAN2_Init 1 */ - hfdcan2.Instance = FDCAN2; - hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; - hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; - hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; - hfdcan2.Init.AutoRetransmission = DISABLE; - hfdcan2.Init.TransmitPause = DISABLE; - hfdcan2.Init.ProtocolException = DISABLE; - hfdcan2.Init.NominalPrescaler = 16; - hfdcan2.Init.NominalSyncJumpWidth = 1; - hfdcan2.Init.NominalTimeSeg1 = 1; - hfdcan2.Init.NominalTimeSeg2 = 1; - hfdcan2.Init.DataPrescaler = 1; - hfdcan2.Init.DataSyncJumpWidth = 1; - hfdcan2.Init.DataTimeSeg1 = 1; - hfdcan2.Init.DataTimeSeg2 = 1; - hfdcan2.Init.StdFiltersNbr = 0; - hfdcan2.Init.ExtFiltersNbr = 0; - hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; - if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN FDCAN2_Init 2 */ - - /* USER CODE END FDCAN2_Init 2 */ - + /* USER CODE BEGIN FDCAN2_Init 0 */ + + /* USER CODE END FDCAN2_Init 0 */ + + /* USER CODE BEGIN FDCAN2_Init 1 */ + + /* USER CODE END FDCAN2_Init 1 */ + hfdcan2.Instance = FDCAN2; + hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan2.Init.AutoRetransmission = DISABLE; + hfdcan2.Init.TransmitPause = DISABLE; + hfdcan2.Init.ProtocolException = DISABLE; + hfdcan2.Init.NominalPrescaler = 16; + hfdcan2.Init.NominalSyncJumpWidth = 1; + hfdcan2.Init.NominalTimeSeg1 = 1; + hfdcan2.Init.NominalTimeSeg2 = 1; + hfdcan2.Init.DataPrescaler = 1; + hfdcan2.Init.DataSyncJumpWidth = 1; + hfdcan2.Init.DataTimeSeg1 = 1; + hfdcan2.Init.DataTimeSeg2 = 1; + hfdcan2.Init.StdFiltersNbr = 0; + hfdcan2.Init.ExtFiltersNbr = 0; + hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN2_Init 2 */ + + /* USER CODE END FDCAN2_Init 2 */ } -static uint32_t HAL_RCC_FDCAN_CLK_ENABLED=0; +static uint32_t HAL_RCC_FDCAN_CLK_ENABLED = 0; -void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle) +void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef *fdcanHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(fdcanHandle->Instance==FDCAN1) - { - /* USER CODE BEGIN FDCAN1_MspInit 0 */ - - /* USER CODE END FDCAN1_MspInit 0 */ - LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); - - /* FDCAN1 clock enable */ - HAL_RCC_FDCAN_CLK_ENABLED++; - if(HAL_RCC_FDCAN_CLK_ENABLED==1){ - __HAL_RCC_FDCAN_CLK_ENABLE(); - } - - __HAL_RCC_GPIOA_CLK_ENABLE(); - /**FDCAN1 GPIO Configuration - PA11 ------> FDCAN1_RX - PA12 ------> FDCAN1_TX - */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN FDCAN1_MspInit 1 */ - - /* USER CODE END FDCAN1_MspInit 1 */ - } - else if(fdcanHandle->Instance==FDCAN2) - { - /* USER CODE BEGIN FDCAN2_MspInit 0 */ - - /* USER CODE END FDCAN2_MspInit 0 */ - - LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); - - /* FDCAN2 clock enable */ - HAL_RCC_FDCAN_CLK_ENABLED++; - if(HAL_RCC_FDCAN_CLK_ENABLED==1){ - __HAL_RCC_FDCAN_CLK_ENABLE(); - } - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**FDCAN2 GPIO Configuration - PB13 ------> FDCAN2_TX - PB5 ------> FDCAN2_RX - */ - GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* USER CODE BEGIN FDCAN2_MspInit 1 */ - - /* USER CODE END FDCAN2_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (fdcanHandle->Instance == FDCAN1) { + /* USER CODE BEGIN FDCAN1_MspInit 0 */ + + /* USER CODE END FDCAN1_MspInit 0 */ + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN1 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if (HAL_RCC_FDCAN_CLK_ENABLED == 1) { + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN1_MspInit 1 */ + + /* USER CODE END FDCAN1_MspInit 1 */ + } else if (fdcanHandle->Instance == FDCAN2) { + /* USER CODE BEGIN FDCAN2_MspInit 0 */ + + /* USER CODE END FDCAN2_MspInit 0 */ + + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN2 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if (HAL_RCC_FDCAN_CLK_ENABLED == 1) { + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN2_MspInit 1 */ + + /* USER CODE END FDCAN2_MspInit 1 */ + } } -void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle) +void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef *fdcanHandle) { - if(fdcanHandle->Instance==FDCAN1) - { - /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ - - /* USER CODE END FDCAN1_MspDeInit 0 */ - /* Peripheral clock disable */ - HAL_RCC_FDCAN_CLK_ENABLED--; - if(HAL_RCC_FDCAN_CLK_ENABLED==0){ - __HAL_RCC_FDCAN_CLK_DISABLE(); - } - - /**FDCAN1 GPIO Configuration - PA11 ------> FDCAN1_RX - PA12 ------> FDCAN1_TX - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); - - /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ - - /* USER CODE END FDCAN1_MspDeInit 1 */ - } - else if(fdcanHandle->Instance==FDCAN2) - { - /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ - - /* USER CODE END FDCAN2_MspDeInit 0 */ - /* Peripheral clock disable */ - HAL_RCC_FDCAN_CLK_ENABLED--; - if(HAL_RCC_FDCAN_CLK_ENABLED==0){ - __HAL_RCC_FDCAN_CLK_DISABLE(); - } - - /**FDCAN2 GPIO Configuration - PB13 ------> FDCAN2_TX - PB5 ------> FDCAN2_RX - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_5); - - /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ - - /* USER CODE END FDCAN2_MspDeInit 1 */ - } + if (fdcanHandle->Instance == FDCAN1) { + /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ + + /* USER CODE END FDCAN1_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if (HAL_RCC_FDCAN_CLK_ENABLED == 0) { + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12); + + /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ + + /* USER CODE END FDCAN1_MspDeInit 1 */ + } else if (fdcanHandle->Instance == FDCAN2) { + /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ + + /* USER CODE END FDCAN2_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if (HAL_RCC_FDCAN_CLK_ENABLED == 0) { + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13 | GPIO_PIN_5); + + /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ + + /* USER CODE END FDCAN2_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/gpio.c b/SAMM/Core/Src/gpio.c index 5efdf6ec1..2711a3604 100644 --- a/SAMM/Core/Src/gpio.c +++ b/SAMM/Core/Src/gpio.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file gpio.c - * @brief This file provides code for the configuration - * of all used GPIO pins. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -33,71 +33,70 @@ /* USER CODE END 1 */ /** Configure pins as - * Analog - * Input - * Output - * EVENT_OUT - * EXTI + * Analog + * Input + * Output + * EVENT_OUT + * EXTI PC8 ------> I2C3_SCL PC9 ------> I2C3_SDA */ void MX_GPIO_Init(void) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOF_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); - - /*Configure GPIO pin Output Level */ - //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); - - /*Configure GPIO pin : PF1 */ - /* - GPIO_InitStruct.Pin = GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - */ - - /*Configure GPIO pin : PB1 */ - GPIO_InitStruct.Pin = GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /*Configure GPIO pin : PB12 */ - /* corresponds to SPI - GPIO_InitStruct.Pin = GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - */ - - /*Configure GPIO pins : PC8 PC9 */ - /* corresponds to tiretemp i2c - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - */ - - /*Configure GPIO pins : PB4 PB6 */ - GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin : PF1 */ + /* + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + */ + + /*Configure GPIO pin : PB1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /*Configure GPIO pin : PB12 */ + /* corresponds to SPI + GPIO_InitStruct.Pin = GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + */ + + /*Configure GPIO pins : PC8 PC9 */ + /* corresponds to tiretemp i2c + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + */ + + /*Configure GPIO pins : PB4 PB6 */ + GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 2 */ diff --git a/SAMM/Core/Src/i2c.c b/SAMM/Core/Src/i2c.c index 0cab1143b..7119f062b 100644 --- a/SAMM/Core/Src/i2c.c +++ b/SAMM/Core/Src/i2c.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file i2c.c - * @brief This file provides code for the configuration - * of the I2C instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file i2c.c + * @brief This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "i2c.h" @@ -30,104 +30,98 @@ I2C_HandleTypeDef hi2c1; void MX_I2C1_Init(void) { - /* USER CODE BEGIN I2C1_Init 0 */ - - /* USER CODE END I2C1_Init 0 */ - - /* USER CODE BEGIN I2C1_Init 1 */ - - /* USER CODE END I2C1_Init 1 */ - hi2c1.Instance = I2C1; - hi2c1.Init.Timing = 0x10B30F25; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Analogue filter - */ - if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Digital filter - */ - if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) - { - Error_Handler(); - } - - /** I2C Fast mode Plus enable - */ - HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); - /* USER CODE BEGIN I2C1_Init 2 */ - - /* USER CODE END I2C1_Init 2 */ - + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.Timing = 0x10B30F25; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) { + Error_Handler(); + } + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { + Error_Handler(); + } + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) { + Error_Handler(); + } + + /** I2C Fast mode Plus enable + */ + HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ } -void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +void HAL_I2C_MspInit(I2C_HandleTypeDef *i2cHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(i2cHandle->Instance==I2C1) - { - /* USER CODE BEGIN I2C1_MspInit 0 */ - - /* USER CODE END I2C1_MspInit 0 */ - LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**I2C1 GPIO Configuration - PB7 ------> I2C1_SDA - PB8-BOOT0 ------> I2C1_SCL - */ - GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* I2C1 clock enable */ - __HAL_RCC_I2C1_CLK_ENABLE(); - /* USER CODE BEGIN I2C1_MspInit 1 */ - - /* USER CODE END I2C1_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (i2cHandle->Instance == I2C1) { + /* USER CODE BEGIN I2C1_MspInit 0 */ + + /* USER CODE END I2C1_MspInit 0 */ + LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C1 clock enable */ + __HAL_RCC_I2C1_CLK_ENABLE(); + /* USER CODE BEGIN I2C1_MspInit 1 */ + + /* USER CODE END I2C1_MspInit 1 */ + } } -void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +void HAL_I2C_MspDeInit(I2C_HandleTypeDef *i2cHandle) { - if(i2cHandle->Instance==I2C1) - { - /* USER CODE BEGIN I2C1_MspDeInit 0 */ + if (i2cHandle->Instance == I2C1) { + /* USER CODE BEGIN I2C1_MspDeInit 0 */ - /* USER CODE END I2C1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_I2C1_CLK_DISABLE(); + /* USER CODE END I2C1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C1_CLK_DISABLE(); - /**I2C1 GPIO Configuration - PB7 ------> I2C1_SDA - PB8-BOOT0 ------> I2C1_SCL - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); - /* USER CODE BEGIN I2C1_MspDeInit 1 */ + /* USER CODE BEGIN I2C1_MspDeInit 1 */ - /* USER CODE END I2C1_MspDeInit 1 */ - } + /* USER CODE END I2C1_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/main.c b/SAMM/Core/Src/main.c index 7ca2c8382..64aafa64f 100644 --- a/SAMM/Core/Src/main.c +++ b/SAMM/Core/Src/main.c @@ -1,34 +1,36 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "main.h" + #include "crc.h" #include "fdcan.h" +#include "gpio.h" #include "i2c.h" #include "spi.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include + #include "VL53L4ED_api.h" -//#include "circularBuffer.h" +// #include "circularBuffer.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -49,10 +51,10 @@ /* USER CODE BEGIN PM */ PUTCHAR_PROTOTYPE { - ITM_SendChar(ch); - return ch; + ITM_SendChar(ch); + return ch; } -//CircularBuffer *cb; +// CircularBuffer *cb; /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ @@ -78,158 +80,145 @@ I2C2 - is meant for thermal sensor MLX90640 and is ran using DMA to offload CPU /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { - /* USER CODE BEGIN 1 */ - //cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); - /* USER CODE END 1 */ + /* USER CODE BEGIN 1 */ + // cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); + /* USER CODE END 1 */ - /* MCU Configuration--------------------------------------------------------*/ + /* MCU Configuration--------------------------------------------------------*/ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); - /* USER CODE BEGIN Init */ + /* USER CODE BEGIN Init */ - /* USER CODE END Init */ + /* USER CODE END Init */ - /* Configure the system clock */ - SystemClock_Config(); + /* Configure the system clock */ + SystemClock_Config(); - /* USER CODE BEGIN SysInit */ + /* USER CODE BEGIN SysInit */ - /* USER CODE END SysInit */ + /* USER CODE END SysInit */ - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_CRC_Init(); - MX_FDCAN1_Init(); - MX_FDCAN2_Init(); - MX_I2C1_Init(); - MX_SPI3_Init(); - /* USER CODE BEGIN 2 */ + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_CRC_Init(); + MX_FDCAN1_Init(); + MX_FDCAN2_Init(); + MX_I2C1_Init(); + MX_SPI3_Init(); + /* USER CODE BEGIN 2 */ - // HAL_FDCAN_Start(&hfdcan1); - // HAL_FDCAN_Start(&hfdcan2); - // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); - // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // HAL_FDCAN_Start(&hfdcan1); + // HAL_FDCAN_Start(&hfdcan2); + // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // static uint16_t eeMLX90640[832]; + // static paramsMLX90640 mlx90640; + // #define MLX90640_ADDRESS 0x33<<1 + // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); - // static uint16_t eeMLX90640[832]; - // static paramsMLX90640 mlx90640; - // #define MLX90640_ADDRESS 0x33<<1 - // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); + // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); - // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); - // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); + // MLX90640_SynchFrame(MLX90640_ADDRESS); + // MLX90640_SetRefreshRate(0x33, 0x05); + /* USER CODE END 2 */ - // MLX90640_SynchFrame(MLX90640_ADDRESS); - // MLX90640_SetRefreshRate(0x33, 0x05); - /* USER CODE END 2 */ + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to reset the device + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to power up the device - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin - // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin - HAL_Delay(100); // wait for 5ms to reset the device - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin - // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin - HAL_Delay(100); // wait for 5ms to power up the device + uint16_t status = 0; - uint16_t status = 0; + uint16_t sensor_id = 0; + VL53L4ED_ResultsData_t results; + uint8_t p_data_ready; - uint16_t sensor_id = 0; - VL53L4ED_ResultsData_t results; - uint8_t p_data_ready; + int TOF_ID = 0x52; + HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); + status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); + printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); + status = VL53L4ED_StartRanging(TOF_ID); + status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); + status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing - int TOF_ID = 0x52; - HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); - status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); - printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); - status = VL53L4ED_StartRanging(TOF_ID); - status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); - status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing + while (1) { + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); + /* USER CODE END WHILE */ - while (1) - { - // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); - // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); - if(p_data_ready){ + /* USER CODE BEGIN 3 */ + status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); + if (p_data_ready) { /* (Mandatory) Clear HW interrupt to restart measurements */ VL53L4ED_ClearInterrupt(TOF_ID); /* Read measured distance. RangeStatus = 0 means valid data */ VL53L4ED_GetResult(TOF_ID, &results); - printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", - results.range_status, - results.distance_mm- 67, - results.signal_per_spad_kcps); - }else{ - HAL_Delay(10); - __disable_irq(); - __enable_irq(); - } - } - /* USER CODE END 3 */ + printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", results.range_status, results.distance_mm - 67, results.signal_per_spad_kcps); + } else { + HAL_Delay(10); + __disable_irq(); + __enable_irq(); + } + } + /* USER CODE END 3 */ } /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { - LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); - while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) - { - } - LL_PWR_EnableRange1BoostMode(); - LL_RCC_HSE_EnableBypass(); - LL_RCC_HSE_Enable(); - /* Wait till HSE is ready */ - while(LL_RCC_HSE_IsReady() != 1) - { - } - - LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 40, LL_RCC_PLLR_DIV_2); - LL_RCC_PLL_EnableDomain_SYS(); - LL_RCC_PLL_Enable(); - /* Wait till PLL is ready */ - while(LL_RCC_PLL_IsReady() != 1) - { - } - - LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); - /* Wait till System clock is ready */ - while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) - { - } - - /* Insure 1us transition state at intermediate medium speed clock*/ - for (__IO uint32_t i = (170 >> 1); i !=0; i--); - - /* Set AHB prescaler*/ - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); - LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); - LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); - LL_SetSystemCoreClock(160000000); - - /* Update the time base */ - if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK) - { - Error_Handler(); - } + LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); + while (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) {} + LL_PWR_EnableRange1BoostMode(); + LL_RCC_HSE_EnableBypass(); + LL_RCC_HSE_Enable(); + /* Wait till HSE is ready */ + while (LL_RCC_HSE_IsReady() != 1) {} + + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 40, LL_RCC_PLLR_DIV_2); + LL_RCC_PLL_EnableDomain_SYS(); + LL_RCC_PLL_Enable(); + /* Wait till PLL is ready */ + while (LL_RCC_PLL_IsReady() != 1) {} + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); + /* Wait till System clock is ready */ + while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {} + + /* Insure 1us transition state at intermediate medium speed clock*/ + for (__IO uint32_t i = (170 >> 1); i != 0; i--) + ; + + /* Set AHB prescaler*/ + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); + LL_SetSystemCoreClock(160000000); + + /* Update the time base */ + if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK) { + Error_Handler(); + } } /* USER CODE BEGIN 4 */ @@ -240,59 +229,56 @@ void SystemClock_Config(void) // printf("got messgae\n"); // //circularBufferPush(cb, RxData, sizeof(RxData)); - // } /* USER CODE END 4 */ /** - * @brief Period elapsed callback in non blocking mode - * @note This function is called when TIM1 interrupt took place, inside - * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment - * a global variable "uwTick" used as application time base. - * @param htim : TIM handle - * @retval None - */ + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM1 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { - /* USER CODE BEGIN Callback 0 */ + /* USER CODE BEGIN Callback 0 */ - /* USER CODE END Callback 0 */ - if (htim->Instance == TIM1) { - HAL_IncTick(); - } - /* USER CODE BEGIN Callback 1 */ + /* USER CODE END Callback 0 */ + if (htim->Instance == TIM1) { + HAL_IncTick(); + } + /* USER CODE BEGIN Callback 1 */ - /* USER CODE END Callback 1 */ + /* USER CODE END Callback 1 */ } /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) {} + /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ 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 CODE END 6 */ + /* 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 CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/SAMM/Core/Src/spi.c b/SAMM/Core/Src/spi.c index 22f78c561..665ad89d5 100644 --- a/SAMM/Core/Src/spi.c +++ b/SAMM/Core/Src/spi.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file spi.c - * @brief This file provides code for the configuration - * of the SPI instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file spi.c + * @brief This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "spi.h" @@ -30,90 +30,86 @@ SPI_HandleTypeDef hspi3; void MX_SPI3_Init(void) { - /* USER CODE BEGIN SPI3_Init 0 */ - - /* USER CODE END SPI3_Init 0 */ - - /* USER CODE BEGIN SPI3_Init 1 */ - - /* USER CODE END SPI3_Init 1 */ - hspi3.Instance = SPI3; - hspi3.Init.Mode = SPI_MODE_MASTER; - hspi3.Init.Direction = SPI_DIRECTION_2LINES; - hspi3.Init.DataSize = SPI_DATASIZE_8BIT; - hspi3.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi3.Init.CLKPhase = SPI_PHASE_1EDGE; - hspi3.Init.NSS = SPI_NSS_SOFT; - hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; - hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi3.Init.TIMode = SPI_TIMODE_DISABLE; - hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi3.Init.CRCPolynomial = 7; - hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; - if (HAL_SPI_Init(&hspi3) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN SPI3_Init 2 */ - - /* USER CODE END SPI3_Init 2 */ - + /* USER CODE BEGIN SPI3_Init 0 */ + + /* USER CODE END SPI3_Init 0 */ + + /* USER CODE BEGIN SPI3_Init 1 */ + + /* USER CODE END SPI3_Init 1 */ + hspi3.Instance = SPI3; + hspi3.Init.Mode = SPI_MODE_MASTER; + hspi3.Init.Direction = SPI_DIRECTION_2LINES; + hspi3.Init.DataSize = SPI_DATASIZE_8BIT; + hspi3.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi3.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi3.Init.NSS = SPI_NSS_SOFT; + hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; + hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi3.Init.TIMode = SPI_TIMODE_DISABLE; + hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi3.Init.CRCPolynomial = 7; + hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + if (HAL_SPI_Init(&hspi3) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN SPI3_Init 2 */ + + /* USER CODE END SPI3_Init 2 */ } -void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) +void HAL_SPI_MspInit(SPI_HandleTypeDef *spiHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(spiHandle->Instance==SPI3) - { - /* USER CODE BEGIN SPI3_MspInit 0 */ - - /* USER CODE END SPI3_MspInit 0 */ - /* SPI3 clock enable */ - __HAL_RCC_SPI3_CLK_ENABLE(); - - __HAL_RCC_GPIOC_CLK_ENABLE(); - /**SPI3 GPIO Configuration - PC10 ------> SPI3_SCK - PC11 ------> SPI3_MISO - PC12 ------> SPI3_MOSI - */ - GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - /* USER CODE BEGIN SPI3_MspInit 1 */ - - /* USER CODE END SPI3_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (spiHandle->Instance == SPI3) { + /* USER CODE BEGIN SPI3_MspInit 0 */ + + /* USER CODE END SPI3_MspInit 0 */ + /* SPI3 clock enable */ + __HAL_RCC_SPI3_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**SPI3 GPIO Configuration + PC10 ------> SPI3_SCK + PC11 ------> SPI3_MISO + PC12 ------> SPI3_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USER CODE BEGIN SPI3_MspInit 1 */ + + /* USER CODE END SPI3_MspInit 1 */ + } } -void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) +void HAL_SPI_MspDeInit(SPI_HandleTypeDef *spiHandle) { - if(spiHandle->Instance==SPI3) - { - /* USER CODE BEGIN SPI3_MspDeInit 0 */ + if (spiHandle->Instance == SPI3) { + /* USER CODE BEGIN SPI3_MspDeInit 0 */ - /* USER CODE END SPI3_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_SPI3_CLK_DISABLE(); + /* USER CODE END SPI3_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI3_CLK_DISABLE(); - /**SPI3 GPIO Configuration - PC10 ------> SPI3_SCK - PC11 ------> SPI3_MISO - PC12 ------> SPI3_MOSI - */ - HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12); + /**SPI3 GPIO Configuration + PC10 ------> SPI3_SCK + PC11 ------> SPI3_MISO + PC12 ------> SPI3_MOSI + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12); - /* USER CODE BEGIN SPI3_MspDeInit 1 */ + /* USER CODE BEGIN SPI3_MspDeInit 1 */ - /* USER CODE END SPI3_MspDeInit 1 */ - } + /* USER CODE END SPI3_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/stm32g4xx_hal_msp.c b/SAMM/Core/Src/stm32g4xx_hal_msp.c index 3fc223b63..69189ef4e 100644 --- a/SAMM/Core/Src/stm32g4xx_hal_msp.c +++ b/SAMM/Core/Src/stm32g4xx_hal_msp.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_msp.c - * @brief This file provides code for the MSP Initialization - * and de-Initialization codes. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -58,27 +58,27 @@ /* USER CODE END 0 */ /** - * Initializes the Global MSP. - */ + * Initializes the Global MSP. + */ void HAL_MspInit(void) { - /* USER CODE BEGIN MspInit 0 */ + /* USER CODE BEGIN MspInit 0 */ - /* USER CODE END MspInit 0 */ + /* USER CODE END MspInit 0 */ - __HAL_RCC_SYSCFG_CLK_ENABLE(); - __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); - /* System interrupt init*/ + /* System interrupt init*/ - /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral - */ - HAL_PWREx_DisableUCPDDeadBattery(); + /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral + */ + HAL_PWREx_DisableUCPDDeadBattery(); - /* USER CODE BEGIN MspInit 1 */ + /* USER CODE BEGIN MspInit 1 */ - /* USER CODE END MspInit 1 */ + /* USER CODE END MspInit 1 */ } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c b/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c index 7ac4d76ff..8d49fd88c 100644 --- a/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c +++ b/SAMM/Core/Src/stm32g4xx_hal_timebase_tim.c @@ -1,20 +1,20 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_timebase_tim.c - * @brief HAL time base based on the hardware TIM. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_hal_timebase_tim.c + * @brief HAL time base based on the hardware TIM. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -25,103 +25,97 @@ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ -TIM_HandleTypeDef htim1; +TIM_HandleTypeDef htim1; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** - * @brief This function configures the TIM1 as a time base source. - * The time source is configured to have 1ms time base with a dedicated - * Tick interrupt priority. - * @note This function is called automatically at the beginning of program after - * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). - * @param TickPriority: Tick interrupt priority. - * @retval HAL status - */ + * @brief This function configures the TIM1 as a time base source. + * The time source is configured to have 1ms time base with a dedicated + * Tick interrupt priority. + * @note This function is called automatically at the beginning of program after + * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). + * @param TickPriority: Tick interrupt priority. + * @retval HAL status + */ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { - RCC_ClkInitTypeDef clkconfig; - uint32_t uwTimclock = 0; - uint32_t uwPrescalerValue = 0; - uint32_t pFLatency; - - HAL_StatusTypeDef status; - - /* Enable TIM1 clock */ - __HAL_RCC_TIM1_CLK_ENABLE(); - -/* Get clock configuration */ - HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); - - /* Compute TIM1 clock */ - uwTimclock = HAL_RCC_GetPCLK2Freq(); - - /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ - uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); - - /* Initialize TIM1 */ - htim1.Instance = TIM1; - - /* Initialize TIMx peripheral as follow: - - + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. - + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. - + ClockDivision = 0 - + Counter direction = Up - */ - htim1.Init.Period = (1000000U / 1000U) - 1U; - htim1.Init.Prescaler = uwPrescalerValue; - htim1.Init.ClockDivision = 0; - htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - - status = HAL_TIM_Base_Init(&htim1); - if (status == HAL_OK) - { - /* Start the TIM time Base generation in interrupt mode */ - status = HAL_TIM_Base_Start_IT(&htim1); - if (status == HAL_OK) - { - /* Enable the TIM1 global Interrupt */ - HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); - /* Configure the SysTick IRQ priority */ - if (TickPriority < (1UL << __NVIC_PRIO_BITS)) - { - /* Configure the TIM IRQ priority */ - HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); - uwTickPrio = TickPriority; - } - else - { - status = HAL_ERROR; - } - } - } - - /* Return function status */ - return status; + RCC_ClkInitTypeDef clkconfig; + uint32_t uwTimclock = 0; + uint32_t uwPrescalerValue = 0; + uint32_t pFLatency; + + HAL_StatusTypeDef status; + + /* Enable TIM1 clock */ + __HAL_RCC_TIM1_CLK_ENABLE(); + + /* Get clock configuration */ + HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); + + /* Compute TIM1 clock */ + uwTimclock = HAL_RCC_GetPCLK2Freq(); + + /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ + uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); + + /* Initialize TIM1 */ + htim1.Instance = TIM1; + + /* Initialize TIMx peripheral as follow: + + + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. + + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + + ClockDivision = 0 + + Counter direction = Up + */ + htim1.Init.Period = (1000000U / 1000U) - 1U; + htim1.Init.Prescaler = uwPrescalerValue; + htim1.Init.ClockDivision = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + + status = HAL_TIM_Base_Init(&htim1); + if (status == HAL_OK) { + /* Start the TIM time Base generation in interrupt mode */ + status = HAL_TIM_Base_Start_IT(&htim1); + if (status == HAL_OK) { + /* Enable the TIM1 global Interrupt */ + HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + /* Configure the SysTick IRQ priority */ + if (TickPriority < (1UL << __NVIC_PRIO_BITS)) { + /* Configure the TIM IRQ priority */ + HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); + uwTickPrio = TickPriority; + } else { + status = HAL_ERROR; + } + } + } + + /* Return function status */ + return status; } /** - * @brief Suspend Tick increment. - * @note Disable the tick increment by disabling TIM1 update interrupt. - * @param None - * @retval None - */ + * @brief Suspend Tick increment. + * @note Disable the tick increment by disabling TIM1 update interrupt. + * @param None + * @retval None + */ void HAL_SuspendTick(void) { - /* Disable TIM1 update Interrupt */ - __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); + /* Disable TIM1 update Interrupt */ + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); } /** - * @brief Resume Tick increment. - * @note Enable the tick increment by Enabling TIM1 update interrupt. - * @param None - * @retval None - */ + * @brief Resume Tick increment. + * @note Enable the tick increment by Enabling TIM1 update interrupt. + * @param None + * @retval None + */ void HAL_ResumeTick(void) { - /* Enable TIM1 Update interrupt */ - __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); + /* Enable TIM1 Update interrupt */ + __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); } - diff --git a/SAMM/Core/Src/stm32g4xx_it.c b/SAMM/Core/Src/stm32g4xx_it.c index ef94b5f52..2db0ff77b 100644 --- a/SAMM/Core/Src/stm32g4xx_it.c +++ b/SAMM/Core/Src/stm32g4xx_it.c @@ -1,25 +1,26 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_it.c - * @brief Interrupt Service Routines. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ -#include "main.h" #include "stm32g4xx_it.h" + +#include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ @@ -65,131 +66,125 @@ extern TIM_HandleTypeDef htim1; /* Cortex-M4 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** - * @brief This function handles Non maskable interrupt. - */ + * @brief This function handles Non maskable interrupt. + */ void NMI_Handler(void) { - /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ - - /* USER CODE END NonMaskableInt_IRQn 0 */ - /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ - while (1) - { - } - /* USER CODE END NonMaskableInt_IRQn 1 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) {} + /* USER CODE END NonMaskableInt_IRQn 1 */ } /** - * @brief This function handles Hard fault interrupt. - */ + * @brief This function handles Hard fault interrupt. + */ void HardFault_Handler(void) { - /* USER CODE BEGIN HardFault_IRQn 0 */ - - /* USER CODE END HardFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_HardFault_IRQn 0 */ - /* USER CODE END W1_HardFault_IRQn 0 */ - } + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } } /** - * @brief This function handles Memory management fault. - */ + * @brief This function handles Memory management fault. + */ void MemManage_Handler(void) { - /* USER CODE BEGIN MemoryManagement_IRQn 0 */ - - /* USER CODE END MemoryManagement_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ - /* USER CODE END W1_MemoryManagement_IRQn 0 */ - } + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ + /* USER CODE END W1_MemoryManagement_IRQn 0 */ + } } /** - * @brief This function handles Prefetch fault, memory access fault. - */ + * @brief This function handles Prefetch fault, memory access fault. + */ void BusFault_Handler(void) { - /* USER CODE BEGIN BusFault_IRQn 0 */ - - /* USER CODE END BusFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_BusFault_IRQn 0 */ - /* USER CODE END W1_BusFault_IRQn 0 */ - } + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_BusFault_IRQn 0 */ + /* USER CODE END W1_BusFault_IRQn 0 */ + } } /** - * @brief This function handles Undefined instruction or illegal state. - */ + * @brief This function handles Undefined instruction or illegal state. + */ void UsageFault_Handler(void) { - /* USER CODE BEGIN UsageFault_IRQn 0 */ - - /* USER CODE END UsageFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ - /* USER CODE END W1_UsageFault_IRQn 0 */ - } + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ + /* USER CODE END W1_UsageFault_IRQn 0 */ + } } /** - * @brief This function handles System service call via SWI instruction. - */ + * @brief This function handles System service call via SWI instruction. + */ void SVC_Handler(void) { - /* USER CODE BEGIN SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 0 */ - /* USER CODE END SVCall_IRQn 0 */ - /* USER CODE BEGIN SVCall_IRQn 1 */ + /* USER CODE END SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 1 */ - /* USER CODE END SVCall_IRQn 1 */ + /* USER CODE END SVCall_IRQn 1 */ } /** - * @brief This function handles Debug monitor. - */ + * @brief This function handles Debug monitor. + */ void DebugMon_Handler(void) { - /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ - /* USER CODE END DebugMonitor_IRQn 0 */ - /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ - /* USER CODE END DebugMonitor_IRQn 1 */ + /* USER CODE END DebugMonitor_IRQn 1 */ } /** - * @brief This function handles Pendable request for system service. - */ + * @brief This function handles Pendable request for system service. + */ void PendSV_Handler(void) { - /* USER CODE BEGIN PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 0 */ - /* USER CODE END PendSV_IRQn 0 */ - /* USER CODE BEGIN PendSV_IRQn 1 */ + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ - /* USER CODE END PendSV_IRQn 1 */ + /* USER CODE END PendSV_IRQn 1 */ } /** - * @brief This function handles System tick timer. - */ + * @brief This function handles System tick timer. + */ void SysTick_Handler(void) { - /* USER CODE BEGIN SysTick_IRQn 0 */ + /* USER CODE BEGIN SysTick_IRQn 0 */ - /* USER CODE END SysTick_IRQn 0 */ + /* USER CODE END SysTick_IRQn 0 */ - /* USER CODE BEGIN SysTick_IRQn 1 */ + /* USER CODE BEGIN SysTick_IRQn 1 */ - /* USER CODE END SysTick_IRQn 1 */ + /* USER CODE END SysTick_IRQn 1 */ } /******************************************************************************/ @@ -200,17 +195,17 @@ void SysTick_Handler(void) /******************************************************************************/ /** - * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. - */ + * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. + */ void TIM1_UP_TIM16_IRQHandler(void) { - /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ - /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ - HAL_TIM_IRQHandler(&htim1); - /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ + /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ + HAL_TIM_IRQHandler(&htim1); + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ - /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ + /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ } /* USER CODE BEGIN 1 */ diff --git a/SAMM/Core/Src/syscalls.c b/SAMM/Core/Src/syscalls.c index e33a8492c..a59a7d089 100644 --- a/SAMM/Core/Src/syscalls.c +++ b/SAMM/Core/Src/syscalls.c @@ -21,156 +21,148 @@ */ /* Includes */ -#include -#include #include -#include #include -#include +#include +#include +#include #include #include - +#include /* Variables */ extern int __io_putchar(int ch) __attribute__((weak)); extern int __io_getchar(void) __attribute__((weak)); - -char *__env[1] = { 0 }; +char *__env[1] = {0}; char **environ = __env; - /* Functions */ -void initialise_monitor_handles() -{ -} +void initialise_monitor_handles() {} int _getpid(void) { - return 1; + return 1; } int _kill(int pid, int sig) { - (void)pid; - (void)sig; - errno = EINVAL; - return -1; + (void)pid; + (void)sig; + errno = EINVAL; + return -1; } -void _exit (int status) +void _exit(int status) { - _kill(status, -1); - while (1) {} /* Make sure we hang here */ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ } __attribute__((weak)) int _read(int file, char *ptr, int len) { - (void)file; - int DataIdx; + (void)file; + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - *ptr++ = __io_getchar(); - } + for (DataIdx = 0; DataIdx < len; DataIdx++) { + *ptr++ = __io_getchar(); + } - return len; + return len; } __attribute__((weak)) int _write(int file, char *ptr, int len) { - (void)file; - int DataIdx; + (void)file; + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - __io_putchar(*ptr++); - } - return len; + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar(*ptr++); + } + return len; } int _close(int file) { - (void)file; - return -1; + (void)file; + return -1; } - int _fstat(int file, struct stat *st) { - (void)file; - st->st_mode = S_IFCHR; - return 0; + (void)file; + st->st_mode = S_IFCHR; + return 0; } int _isatty(int file) { - (void)file; - return 1; + (void)file; + return 1; } int _lseek(int file, int ptr, int dir) { - (void)file; - (void)ptr; - (void)dir; - return 0; + (void)file; + (void)ptr; + (void)dir; + return 0; } int _open(char *path, int flags, ...) { - (void)path; - (void)flags; - /* Pretend like we always fail */ - return -1; + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; } int _wait(int *status) { - (void)status; - errno = ECHILD; - return -1; + (void)status; + errno = ECHILD; + return -1; } int _unlink(char *name) { - (void)name; - errno = ENOENT; - return -1; + (void)name; + errno = ENOENT; + return -1; } int _times(struct tms *buf) { - (void)buf; - return -1; + (void)buf; + return -1; } int _stat(char *file, struct stat *st) { - (void)file; - st->st_mode = S_IFCHR; - return 0; + (void)file; + st->st_mode = S_IFCHR; + return 0; } int _link(char *old, char *new) { - (void)old; - (void)new; - errno = EMLINK; - return -1; + (void)old; + (void)new; + errno = EMLINK; + return -1; } int _fork(void) { - errno = EAGAIN; - return -1; + errno = EAGAIN; + return -1; } int _execve(char *name, char **argv, char **env) { - (void)name; - (void)argv; - (void)env; - errno = ENOMEM; - return -1; + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; } diff --git a/SAMM/Core/Src/sysmem.c b/SAMM/Core/Src/sysmem.c index 246470ee8..00c397937 100644 --- a/SAMM/Core/Src/sysmem.c +++ b/SAMM/Core/Src/sysmem.c @@ -52,28 +52,26 @@ static uint8_t *__sbrk_heap_end = NULL; */ void *_sbrk(ptrdiff_t incr) { - extern uint8_t _end; /* Symbol defined in the linker script */ - extern uint8_t _estack; /* Symbol defined in the linker script */ - extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ - const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; - const uint8_t *max_heap = (uint8_t *)stack_limit; - uint8_t *prev_heap_end; + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; - /* Initialize heap end at first call */ - if (NULL == __sbrk_heap_end) - { - __sbrk_heap_end = &_end; - } + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) { + __sbrk_heap_end = &_end; + } - /* Protect heap from growing into the reserved MSP stack */ - if (__sbrk_heap_end + incr > max_heap) - { - errno = ENOMEM; - return (void *)-1; - } + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) { + errno = ENOMEM; + return (void *)-1; + } - prev_heap_end = __sbrk_heap_end; - __sbrk_heap_end += incr; + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; - return (void *)prev_heap_end; + return (void *)prev_heap_end; } diff --git a/SAMM/Core/Src/system_stm32g4xx.c b/SAMM/Core/Src/system_stm32g4xx.c index 8d35a0aa0..14d0e1be1 100644 --- a/SAMM/Core/Src/system_stm32g4xx.c +++ b/SAMM/Core/Src/system_stm32g4xx.c @@ -1,109 +1,109 @@ /** - ****************************************************************************** - * @file system_stm32g4xx.c - * @author MCD Application Team - * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File - * - * This file provides two functions and one global variable to be called from - * user application: - * - SystemInit(): This function is called at startup just after reset and - * before branch to main program. This call is made inside - * the "startup_stm32g4xx.s" file. - * - * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used - * by the user application to setup the SysTick - * timer or configure other parameters. - * - * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must - * be called whenever the core clock is changed - * during program execution. - * - * After each device reset the HSI (16 MHz) is used as system clock source. - * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to - * configure the system clock before to branch to main program. - * - * This file configures the system clock as follows: - *============================================================================= - *----------------------------------------------------------------------------- - * System Clock source | HSI - *----------------------------------------------------------------------------- - * SYSCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * HCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * AHB Prescaler | 1 - *----------------------------------------------------------------------------- - * APB1 Prescaler | 1 - *----------------------------------------------------------------------------- - * APB2 Prescaler | 1 - *----------------------------------------------------------------------------- - * PLL_M | 1 - *----------------------------------------------------------------------------- - * PLL_N | 16 - *----------------------------------------------------------------------------- - * PLL_P | 7 - *----------------------------------------------------------------------------- - * PLL_Q | 2 - *----------------------------------------------------------------------------- - * PLL_R | 2 - *----------------------------------------------------------------------------- - * Require 48MHz for RNG | Disabled - *----------------------------------------------------------------------------- - *============================================================================= - ****************************************************************************** - * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file system_stm32g4xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32g4xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * After each device reset the HSI (16 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to + * configure the system clock before to branch to main program. + * + * This file configures the system clock as follows: + *============================================================================= + *----------------------------------------------------------------------------- + * System Clock source | HSI + *----------------------------------------------------------------------------- + * SYSCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * HCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * AHB Prescaler | 1 + *----------------------------------------------------------------------------- + * APB1 Prescaler | 1 + *----------------------------------------------------------------------------- + * APB2 Prescaler | 1 + *----------------------------------------------------------------------------- + * PLL_M | 1 + *----------------------------------------------------------------------------- + * PLL_N | 16 + *----------------------------------------------------------------------------- + * PLL_P | 7 + *----------------------------------------------------------------------------- + * PLL_Q | 2 + *----------------------------------------------------------------------------- + * PLL_R | 2 + *----------------------------------------------------------------------------- + * Require 48MHz for RNG | Disabled + *----------------------------------------------------------------------------- + *============================================================================= + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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. + * + ****************************************************************************** + */ /** @addtogroup CMSIS - * @{ - */ + * @{ + */ /** @addtogroup stm32g4xx_system - * @{ - */ + * @{ + */ /** @addtogroup STM32G4xx_System_Private_Includes - * @{ - */ + * @{ + */ #include "stm32g4xx.h" -#if !defined (HSE_VALUE) - #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ +#if !defined(HSE_VALUE) +#define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ -#if !defined (HSI_VALUE) - #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ +#if !defined(HSI_VALUE) +#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_TypesDefinitions - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Defines - * @{ - */ + * @{ + */ /************************* Miscellaneous Configuration ************************/ /* Note: Following vector table addresses must be defined in line with linker - configuration. */ + configuration. */ /*!< Uncomment the following line if you need to relocate the vector table anywhere in Flash or Sram, else the vector table is kept at the automatic remap of boot address selected */ @@ -114,172 +114,171 @@ in Sram else user remap will be done in Flash. */ /* #define VECT_TAB_SRAM */ #if defined(VECT_TAB_SRAM) -#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_BASE_ADDRESS \ + SRAM_BASE /*!< Vector Table base address field. \ + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET \ + 0x00000000U /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ #else -#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -/******************************************************************************/ -/** - * @} - */ +#define VECT_TAB_BASE_ADDRESS \ + FLASH_BASE /*!< Vector Table base address field. \ + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET \ + 0x00000000U /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ + /******************************************************************************/ + /** + * @} + */ /** @addtogroup STM32G4xx_System_Private_Macros - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Variables - * @{ - */ - /* The SystemCoreClock variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ - uint32_t SystemCoreClock = HSI_VALUE; - - const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; - const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; + * @{ + */ +/* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. +*/ +uint32_t SystemCoreClock = HSI_VALUE; + +const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; +const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_FunctionPrototypes - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Functions - * @{ - */ + * @{ + */ /** - * @brief Setup the microcontroller system. - * @param None - * @retval None - */ + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ void SystemInit(void) { - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ - #endif +/* FPU settings ------------------------------------------------------------*/ +#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2))); /* set CP10 and CP11 Full Access */ +#endif - /* Configure the Vector Table location add offset address ------------------*/ + /* Configure the Vector Table location add offset address ------------------*/ #if defined(USER_VECT_TAB_ADDRESS) - SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ } /** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 24 MHz), user has to ensure that HSE_VALUE is same as the real - * frequency of the crystal used. Otherwise, this function may - * have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 24 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ void SystemCoreClockUpdate(void) { - uint32_t tmp, pllvco, pllr, pllsource, pllm; - - /* Get SYSCLK source -------------------------------------------------------*/ - switch (RCC->CFGR & RCC_CFGR_SWS) - { - case 0x04: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - - case 0x08: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - - case 0x0C: /* PLL used as system clock source */ - /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN - SYSCLK = PLL_VCO / PLLR - */ - pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); - pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; - if (pllsource == 0x02UL) /* HSI used as PLL clock source */ - { - pllvco = (HSI_VALUE / pllm); - } - else /* HSE used as PLL clock source */ - { - pllvco = (HSE_VALUE / pllm); - } - pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); - pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; - SystemCoreClock = pllvco/pllr; - break; - - default: - break; - } - /* Compute HCLK clock frequency --------------------------------------------*/ - /* Get HCLK prescaler */ - tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; - /* HCLK clock frequency */ - SystemCoreClock >>= tmp; + uint32_t tmp, pllvco, pllr, pllsource, pllm; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) { + case 0x04: /* HSI used as system clock source */ + SystemCoreClock = HSI_VALUE; + break; + + case 0x08: /* HSE used as system clock source */ + SystemCoreClock = HSE_VALUE; + break; + + case 0x0C: /* PLL used as system clock source */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U; + if (pllsource == 0x02UL) /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } else /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; + SystemCoreClock = pllvco / pllr; + break; + + default: + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; } - /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /** - * @} - */ - - + * @} + */ From 83e55d0ea7a0578305678373b428757468fefde5 Mon Sep 17 00:00:00 2001 From: Shravya Salem Sathish Date: Thu, 2 Apr 2026 20:41:24 -0700 Subject: [PATCH 5/7] Added new folders under SAMM --- SAMM/Common/Application/empty.txt | 0 SAMM/Common/Core/empty.txt | 0 SAMM/IMUandMAG/Application/empty.txt | 0 SAMM/IMUandMAG/Core/empty.txt | 0 SAMM/IMUandTOF/Application/empty.txt | 0 SAMM/IMUandTOF/Core/empty.txt | 0 6 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 SAMM/Common/Application/empty.txt create mode 100644 SAMM/Common/Core/empty.txt create mode 100644 SAMM/IMUandMAG/Application/empty.txt create mode 100644 SAMM/IMUandMAG/Core/empty.txt create mode 100644 SAMM/IMUandTOF/Application/empty.txt create mode 100644 SAMM/IMUandTOF/Core/empty.txt diff --git a/SAMM/Common/Application/empty.txt b/SAMM/Common/Application/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/SAMM/Common/Core/empty.txt b/SAMM/Common/Core/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/SAMM/IMUandMAG/Application/empty.txt b/SAMM/IMUandMAG/Application/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/SAMM/IMUandMAG/Core/empty.txt b/SAMM/IMUandMAG/Core/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/SAMM/IMUandTOF/Application/empty.txt b/SAMM/IMUandTOF/Application/empty.txt new file mode 100644 index 000000000..e69de29bb diff --git a/SAMM/IMUandTOF/Core/empty.txt b/SAMM/IMUandTOF/Core/empty.txt new file mode 100644 index 000000000..e69de29bb From a87ae1580f1a7ef5354766a15155ed519d4b1716 Mon Sep 17 00:00:00 2001 From: Cuteturtle <93956770+TurtleTest2@users.noreply.github.com> Date: Thu, 2 Apr 2026 21:38:23 -0700 Subject: [PATCH 6/7] added and configured IMU --- .../Core/Extras/CircBuF/circularBuffer.c | 64 ++ .../Core/Extras/CircBuF/circularBuffer.h | 22 + .../Core/Extras/VL53L4ED/VL53L4ED_api.c | 699 ++++++++++++++++++ .../Core/Extras/VL53L4ED/VL53L4ED_api.h | 405 ++++++++++ .../Extras/VL53L4ED/VL53L4ED_calibration.c | 242 ++++++ .../Extras/VL53L4ED/VL53L4ED_calibration.h | 73 ++ .../IMUandTOF/Core/Extras/VL53L4ED/platform.c | 113 +++ .../IMUandTOF/Core/Extras/VL53L4ED/platform.h | 80 ++ SAMM/IMUandTOF/Core/Extras/extra.c | 58 ++ SAMM/IMUandTOF/Core/Extras/extra.h | 20 + SAMM/IMUandTOF/Core/Inc/adc.h | 52 ++ SAMM/IMUandTOF/Core/Inc/bmi323.h | 200 +++++ SAMM/IMUandTOF/Core/Inc/crc.h | 52 ++ SAMM/IMUandTOF/Core/Inc/dma.h | 52 ++ SAMM/IMUandTOF/Core/Inc/fdcan.h | 55 ++ SAMM/IMUandTOF/Core/Inc/gpio.h | 49 ++ SAMM/IMUandTOF/Core/Inc/i2c.h | 52 ++ SAMM/IMUandTOF/Core/Inc/main.h | 80 ++ SAMM/IMUandTOF/Core/Inc/spi.h | 51 ++ SAMM/IMUandTOF/Core/Inc/stm32_assert.h | 53 ++ SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h | 380 ++++++++++ SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h | 67 ++ SAMM/IMUandTOF/Core/Src/adc.c | 154 ++++ SAMM/IMUandTOF/Core/Src/bmi323.c | 283 +++++++ SAMM/IMUandTOF/Core/Src/crc.c | 90 +++ SAMM/IMUandTOF/Core/Src/dma.c | 59 ++ SAMM/IMUandTOF/Core/Src/fdcan.c | 223 ++++++ SAMM/IMUandTOF/Core/Src/gpio.c | 94 +++ SAMM/IMUandTOF/Core/Src/i2c.c | 135 ++++ SAMM/IMUandTOF/Core/Src/main.c | 325 ++++++++ SAMM/IMUandTOF/Core/Src/spi.c | 128 ++++ SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c | 86 +++ .../Core/Src/stm32g4xx_hal_timebase_tim.c | 126 ++++ SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c | 218 ++++++ SAMM/IMUandTOF/Core/Src/syscalls.c | 176 +++++ SAMM/IMUandTOF/Core/Src/sysmem.c | 79 ++ SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c | 285 +++++++ SAMM/IMUandTOF/Core/empty.txt | 0 38 files changed, 5380 insertions(+) create mode 100644 SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c create mode 100644 SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c create mode 100644 SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h create mode 100644 SAMM/IMUandTOF/Core/Extras/extra.c create mode 100644 SAMM/IMUandTOF/Core/Extras/extra.h create mode 100644 SAMM/IMUandTOF/Core/Inc/adc.h create mode 100644 SAMM/IMUandTOF/Core/Inc/bmi323.h create mode 100644 SAMM/IMUandTOF/Core/Inc/crc.h create mode 100644 SAMM/IMUandTOF/Core/Inc/dma.h create mode 100644 SAMM/IMUandTOF/Core/Inc/fdcan.h create mode 100644 SAMM/IMUandTOF/Core/Inc/gpio.h create mode 100644 SAMM/IMUandTOF/Core/Inc/i2c.h create mode 100644 SAMM/IMUandTOF/Core/Inc/main.h create mode 100644 SAMM/IMUandTOF/Core/Inc/spi.h create mode 100644 SAMM/IMUandTOF/Core/Inc/stm32_assert.h create mode 100644 SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h create mode 100644 SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h create mode 100644 SAMM/IMUandTOF/Core/Src/adc.c create mode 100644 SAMM/IMUandTOF/Core/Src/bmi323.c create mode 100644 SAMM/IMUandTOF/Core/Src/crc.c create mode 100644 SAMM/IMUandTOF/Core/Src/dma.c create mode 100644 SAMM/IMUandTOF/Core/Src/fdcan.c create mode 100644 SAMM/IMUandTOF/Core/Src/gpio.c create mode 100644 SAMM/IMUandTOF/Core/Src/i2c.c create mode 100644 SAMM/IMUandTOF/Core/Src/main.c create mode 100644 SAMM/IMUandTOF/Core/Src/spi.c create mode 100644 SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c create mode 100644 SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c create mode 100644 SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c create mode 100644 SAMM/IMUandTOF/Core/Src/syscalls.c create mode 100644 SAMM/IMUandTOF/Core/Src/sysmem.c create mode 100644 SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c delete mode 100644 SAMM/IMUandTOF/Core/empty.txt diff --git a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c new file mode 100644 index 000000000..e7f59de79 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c @@ -0,0 +1,64 @@ +#include "circularBuffer.h" +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) { + CircularBuffer *cb = malloc(sizeof(CircularBuffer)); + if (!cb) { + return NULL; + } + + cb->buffer = malloc(capacity * sizeof(void *)); + if (!cb->buffer) { + free(cb); + return NULL; + } + + for (size_t i = 0; i < capacity; i++) { + cb->buffer[i] = malloc(max_array_size); + if (!cb->buffer[i]) { + for (size_t j = 0; j < i; j++) { + free(cb->buffer[j]); + } + free(cb->buffer); + free(cb); + return NULL; + } + } + cb->capacity = capacity; + cb->max_arr_size = max_array_size; + cb->head = 0; + cb->tail = 0; + cb->size = 0; + return cb; +} + +void circularBufferDestroy(CircularBuffer *cb) { + if (!cb) return; + + for (size_t i = 0; i < cb->capacity; i++) { + free(cb->buffer[i]); + } + free(cb->buffer); + free(cb); +} + +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) { + if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { + return -1; // Buffer is full or array is too large + } + + // Copy the array into the buffer + memcpy(cb->buffer[cb->head], array, array_size); + cb->head = (cb->head + 1) % cb->capacity; + cb->size++; + return 0; +} + +void *circularBufferPop(CircularBuffer *cb) { + if (!cb || cb->size == 0) { + return NULL; // Buffer is empty + } + + void *array = cb->buffer[cb->tail]; + cb->tail = (cb->tail + 1) % cb->capacity; + cb->size--; + return array; +} diff --git a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h new file mode 100644 index 000000000..ae7ce9220 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h @@ -0,0 +1,22 @@ +#ifndef __circularBuffer_h__ +#define __circularBuffer_h__ + +#include +#include +#include +#include + +typedef struct { + void **buffer; // Array of void pointers + uint8_t capacity; // Maximum number of elements in the buffer + uint8_t head; // Index of the next element to write + uint8_t tail; // Index of the next element to read + uint8_t size; // Current number of elements in the buffer + uint8_t max_arr_size; // Maximum size of the array +} CircularBuffer; + +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size); +void circularBufferDestroy(CircularBuffer *cb); +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size); +void *circularBufferPop(CircularBuffer *cb); +#endif \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c new file mode 100644 index 000000000..323148da4 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c @@ -0,0 +1,699 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_api.c + * @brief Functions implementation + */ + +#include +#include +#include "VL53L4ED_api.h" + +static const uint8_t VL53L4ED_DEFAULT_CONFIGURATION[] = { + #ifdef VL53L4ED_I2C_FAST_MODE_PLUS + 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ + #else + 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ + #endif + 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low + (bits 3:0 must be 0x1), use SetInterruptPolarity() */ + 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, + use CheckForDataReady() */ + 0x00, /* 0x32 : not user-modifiable */ + 0x02, /* 0x33 : not user-modifiable */ + 0x08, /* 0x34 : not user-modifiable */ + 0x00, /* 0x35 : not user-modifiable */ + 0x08, /* 0x36 : not user-modifiable */ + 0x10, /* 0x37 : not user-modifiable */ + 0x01, /* 0x38 : not user-modifiable */ + 0x01, /* 0x39 : not user-modifiable */ + 0x00, /* 0x3a : not user-modifiable */ + 0x00, /* 0x3b : not user-modifiable */ + 0x00, /* 0x3c : not user-modifiable */ + 0x00, /* 0x3d : not user-modifiable */ + 0xff, /* 0x3e : not user-modifiable */ + 0x00, /* 0x3f : not user-modifiable */ + 0x0F, /* 0x40 : not user-modifiable */ + 0x00, /* 0x41 : not user-modifiable */ + 0x00, /* 0x42 : not user-modifiable */ + 0x00, /* 0x43 : not user-modifiable */ + 0x00, /* 0x44 : not user-modifiable */ + 0x00, /* 0x45 : not user-modifiable */ + 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, + 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ + 0x0b, /* 0x47 : not user-modifiable */ + 0x00, /* 0x48 : not user-modifiable */ + 0x00, /* 0x49 : not user-modifiable */ + 0x02, /* 0x4a : not user-modifiable */ + 0x14, /* 0x4b : not user-modifiable */ + 0x21, /* 0x4c : not user-modifiable */ + 0x00, /* 0x4d : not user-modifiable */ + 0x00, /* 0x4e : not user-modifiable */ + 0x05, /* 0x4f : not user-modifiable */ + 0x00, /* 0x50 : not user-modifiable */ + 0x00, /* 0x51 : not user-modifiable */ + 0x00, /* 0x52 : not user-modifiable */ + 0x00, /* 0x53 : not user-modifiable */ + 0xc8, /* 0x54 : not user-modifiable */ + 0x00, /* 0x55 : not user-modifiable */ + 0x00, /* 0x56 : not user-modifiable */ + 0x38, /* 0x57 : not user-modifiable */ + 0xff, /* 0x58 : not user-modifiable */ + 0x01, /* 0x59 : not user-modifiable */ + 0x00, /* 0x5a : not user-modifiable */ + 0x08, /* 0x5b : not user-modifiable */ + 0x00, /* 0x5c : not user-modifiable */ + 0x00, /* 0x5d : not user-modifiable */ + 0x01, /* 0x5e : not user-modifiable */ + 0xcc, /* 0x5f : not user-modifiable */ + 0x07, /* 0x60 : not user-modifiable */ + 0x01, /* 0x61 : not user-modifiable */ + 0xf1, /* 0x62 : not user-modifiable */ + 0x05, /* 0x63 : not user-modifiable */ + 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), + use SetSigmaThreshold(), default value 90 mm */ + 0xa0, /* 0x65 : Sigma threshold LSB */ + 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), + use SetSignalThreshold() */ + 0x80, /* 0x67 : Min count Rate LSB */ + 0x08, /* 0x68 : not user-modifiable */ + 0x38, /* 0x69 : not user-modifiable */ + 0x00, /* 0x6a : not user-modifiable */ + 0x00, /* 0x6b : not user-modifiable */ + 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, + use SetIntermeasurementInMs() */ + 0x00, /* 0x6d : Intermeasurement period */ + 0x0f, /* 0x6e : Intermeasurement period */ + 0x89, /* 0x6f : Intermeasurement period LSB */ + 0x00, /* 0x70 : not user-modifiable */ + 0x00, /* 0x71 : not user-modifiable */ + 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x73 : distance threshold high LSB */ + 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x75 : distance threshold low LSB */ + 0x00, /* 0x76 : not user-modifiable */ + 0x01, /* 0x77 : not user-modifiable */ + 0x07, /* 0x78 : not user-modifiable */ + 0x05, /* 0x79 : not user-modifiable */ + 0x06, /* 0x7a : not user-modifiable */ + 0x06, /* 0x7b : not user-modifiable */ + 0x00, /* 0x7c : not user-modifiable */ + 0x00, /* 0x7d : not user-modifiable */ + 0x02, /* 0x7e : not user-modifiable */ + 0xc7, /* 0x7f : not user-modifiable */ + 0xff, /* 0x80 : not user-modifiable */ + 0x9B, /* 0x81 : not user-modifiable */ + 0x00, /* 0x82 : not user-modifiable */ + 0x00, /* 0x83 : not user-modifiable */ + 0x00, /* 0x84 : not user-modifiable */ + 0x01, /* 0x85 : not user-modifiable */ + 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ + 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), + If you want an automatic start after VL53L4ED_init() call, + put 0x40 in location 0x87 */ +}; + +VL53L4ED_Error VL53L4ED_GetSWVersion( + VL53L4ED_Version_t *p_Version) +{ + VL53L4ED_Error Status = VL53L4ED_ERROR_NONE; + + p_Version->major = VL53L4ED_IMPLEMENTATION_VER_MAJOR; + p_Version->minor = VL53L4ED_IMPLEMENTATION_VER_MINOR; + p_Version->build = VL53L4ED_IMPLEMENTATION_VER_BUILD; + p_Version->revision = VL53L4ED_IMPLEMENTATION_VER_REVISION; + return Status; +} + +VL53L4ED_Error VL53L4ED_SetI2CAddress( + Dev_t dev, + uint8_t new_address) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, + (uint8_t)(new_address >> (uint8_t)1)); + return status; +} + +VL53L4ED_Error VL53L4ED_GetSensorId( + Dev_t dev, + uint16_t *p_id) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_IDENTIFICATION__MODEL_ID, p_id); + return status; +} + +VL53L4ED_Error VL53L4ED_SensorInit( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t Addr, tmp; + uint8_t continue_loop = 1; + uint16_t i = 0; + + do{ + status |= VL53L4ED_RdByte(dev, + VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); + + if(tmp == (uint8_t)0x3) /* Sensor booted */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for boot */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + /* Load default configuration */ + for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) + { + status |= VL53L4ED_WrByte(dev, Addr, + VL53L4ED_DEFAULT_CONFIGURATION[ + Addr - (uint8_t)0x2D]); + } + + /* Start VHV */ + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, (uint8_t)0x40); + i = (uint8_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_ClearInterrupt(dev); + status |= VL53L4ED_StopRanging(dev); + status |= VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, + (uint8_t)0x09); + status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0); + status |= VL53L4ED_WrWord(dev, 0x0024, 0x500); + + status |= VL53L4ED_SetRangeTiming(dev, 100, 0); + + return status; +} + +VL53L4ED_Error VL53L4ED_ClearInterrupt( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM__INTERRUPT_CLEAR, 0x01); + return status; +} + +VL53L4ED_Error VL53L4ED_StartRanging( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint32_t tmp; + + status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); + + /* Sensor runs in continuous mode */ + if(tmp == (uint32_t)0) + { + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x21); + } + /* Sensor runs in autonomous mode */ + else + { + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x40); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_StopRanging( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x80); + return status; +} + +VL53L4ED_Error VL53L4ED_CheckForDataReady( + Dev_t dev, + uint8_t *p_is_data_ready) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t temp; + uint8_t int_pol; + + status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO_HV_MUX__CTRL, &temp); + temp = temp & (uint8_t)0x10; + temp = temp >> 4; + + if (temp == (uint8_t)1) + { + int_pol = (uint8_t)0; + } + else + { + int_pol = (uint8_t)1; + } + + status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO__TIO_HV_STATUS, &temp); + + if ((temp & (uint8_t)1) == int_pol) + { + *p_is_data_ready = (uint8_t)1; + } + else + { + *p_is_data_ready = (uint8_t)0; + } + + return status; +} + +VL53L4ED_Error VL53L4ED_SetRangeTiming( + Dev_t dev, + uint32_t timing_budget_ms, + uint32_t inter_measurement_ms) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t clock_pll, osc_frequency, ms_byte; + uint32_t macro_period_us = 0, timing_budget_us = 0, ls_byte, tmp; + float_t inter_measurement_factor = (float_t)1.055; + + status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); + if(osc_frequency != (uint16_t)0) + { + timing_budget_us = timing_budget_ms*(uint32_t)1000; + macro_period_us = (uint32_t)((uint32_t)2304 * + ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; + } + else + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + + /* Timing budget check validity */ + if ((timing_budget_ms < (uint32_t)10) + || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) + { + status |= VL53L4ED_ERROR_INVALID_ARGUMENT; + } + /* Sensor runs in continuous mode */ + else if(inter_measurement_ms == (uint32_t)0) + { + status |= VL53L4ED_WrDWord(dev,VL53L4ED_INTERMEASUREMENT_MS, 0); + timing_budget_us -= (uint32_t)2500; + } + /* Sensor runs in autonomous low power mode */ + else if(inter_measurement_ms > timing_budget_ms) + { + status |= VL53L4ED_RdWord(dev, + VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + clock_pll = clock_pll & (uint16_t)0x3FF; + inter_measurement_factor = inter_measurement_factor + * (float_t)inter_measurement_ms + * (float_t)clock_pll; + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, + (uint32_t)inter_measurement_factor); + + timing_budget_us -= (uint32_t)4300; + timing_budget_us /= (uint32_t)2; + + } + /* Invalid case */ + else + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + + if(status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) + { + ms_byte = 0; + timing_budget_us = timing_budget_us << 12; + tmp = macro_period_us*(uint32_t)16; + ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) + - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + + (uint16_t) (ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A,ms_byte); + + ms_byte = 0; + tmp = macro_period_us*(uint32_t)12; + ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) + - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + + (uint16_t) (ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B,ms_byte); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_GetRangeTiming( + Dev_t dev, + uint32_t *p_timing_budget_ms, + uint32_t *p_inter_measurement_ms) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t osc_frequency = 1, range_config_macrop_high, clock_pll = 1; + uint32_t tmp, ls_byte, ms_byte, macro_period_us; + float_t clock_pll_factor = (float_t)1.065; + + /* Get InterMeasurement */ + status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); + status |= VL53L4ED_RdWord(dev, + VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + clock_pll = clock_pll & (uint16_t)0x3FF; + clock_pll_factor = clock_pll_factor * (float_t)clock_pll; + clock_pll = (uint16_t)clock_pll_factor; + *p_inter_measurement_ms = (uint16_t)(tmp/(uint32_t)clock_pll); + + /* Get TimingBudget */ + status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, + &range_config_macrop_high); + + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 + / (uint32_t)osc_frequency)) >> 6; + ls_byte = (range_config_macrop_high & (uint32_t)0x00FF) << 4; + ms_byte = (range_config_macrop_high & (uint32_t)0xFF00) >> 8; + ms_byte = (uint32_t)0x04 - (ms_byte - (uint32_t)1) - (uint32_t)1; + + macro_period_us = macro_period_us * (uint32_t)16; + *p_timing_budget_ms = (((ls_byte + (uint32_t)1)*(macro_period_us>> 6)) + - ((macro_period_us>> 6)>>1)) >> 12; + + if(ms_byte < (uint8_t)12) + { + *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms + >> (uint8_t)ms_byte); + } + + /* Mode continuous */ + if(tmp == (uint32_t)0) + { + *p_timing_budget_ms += (uint32_t)2500; + } + /* Mode autonomous */ + else + { + *p_timing_budget_ms *= (uint32_t)2; + *p_timing_budget_ms += (uint32_t)4300; + } + + *p_timing_budget_ms = *p_timing_budget_ms/(uint32_t)1000; + + return status; +} + +VL53L4ED_Error VL53L4ED_GetResult( + Dev_t dev, + VL53L4ED_ResultsData_t *p_result) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp_16; + uint8_t temp_8; + uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, + 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, + 255, 255, 11, 12 }; + + status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, + &temp_8); + temp_8 = temp_8 & (uint8_t)0x1F; + if (temp_8 < (uint8_t)24) + { + temp_8 = status_rtn[temp_8]; + } + p_result->range_status = temp_8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, + &temp_16); + p_result->number_of_spad = temp_16 / (uint16_t) 256; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, + &temp_16); + p_result->signal_rate_kcps = temp_16 * (uint16_t) 8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, + &temp_16); + p_result->ambient_rate_kcps = temp_16 * (uint16_t) 8; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, + &temp_16); + p_result->sigma_mm = temp_16 / (uint16_t) 4; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, + &temp_16); + p_result->distance_mm = temp_16; + + p_result->signal_per_spad_kcps = p_result->signal_rate_kcps + /p_result->number_of_spad; + p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps + /p_result->number_of_spad; + + return status; +} + +VL53L4ED_Error VL53L4ED_SetOffset( + Dev_t dev, + int16_t OffsetValueInMm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp; + + temp = (uint16_t)((uint16_t)OffsetValueInMm*(uint16_t)4); + + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, temp); + status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, (uint8_t)0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, (uint8_t)0x0); + return status; +} + +VL53L4ED_Error VL53L4ED_GetOffset( + Dev_t dev, + int16_t *p_offset) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t temp; + + status |= VL53L4ED_RdWord(dev,VL53L4ED_RANGE_OFFSET_MM, &temp); + + temp = temp<<3; + temp = temp>>5; + *p_offset = (int16_t)(temp); + + if(*p_offset > 1024) + { + *p_offset = *p_offset - 2048; + } + + return status; +} + +VL53L4ED_Error VL53L4ED_SetXtalk( + Dev_t dev, + uint16_t XtalkValueKcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, + (XtalkValueKcps<<9)); + + return status; +} + +VL53L4ED_Error VL53L4ED_GetXtalk( + Dev_t dev, + uint16_t *p_xtalk_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + float_t tmp_xtalk; + + status |= VL53L4ED_RdWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); + + tmp_xtalk = (float_t)*p_xtalk_kcps / (float_t)512.0; + *p_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); + + return status; +} + +VL53L4ED_Error VL53L4ED_SetDetectionThresholds( + Dev_t dev, + uint16_t distance_low_mm, + uint16_t distance_high_mm, + uint8_t window) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM__INTERRUPT, window); + status |= VL53L4ED_WrWord(dev, VL53L4ED_THRESH_HIGH, distance_high_mm); + status |= VL53L4ED_WrWord(dev, VL53L4ED_THRESH_LOW, distance_low_mm); + return status; +} + +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, + uint16_t *p_distance_low_mm, + uint16_t *p_distance_high_mm, + uint8_t *p_window) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH,p_distance_high_mm); + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_LOW, p_distance_low_mm); + status |= VL53L4ED_RdByte(dev, VL53L4ED_SYSTEM__INTERRUPT, p_window); + *p_window = (*p_window & (uint8_t)0x7); + + return status; +} + +VL53L4ED_Error VL53L4ED_SetSignalThreshold( + Dev_t dev, + uint16_t signal_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status |= VL53L4ED_WrWord(dev, + VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS,signal_kcps>>3); + return status; +} + +VL53L4ED_Error VL53L4ED_GetSignalThreshold( + Dev_t dev, + uint16_t *p_signal_kcps) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint16_t tmp = 0; + + status |= VL53L4ED_RdWord(dev, + VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); + *p_signal_kcps = tmp <<3; + + return status; +} + +VL53L4ED_Error VL53L4ED_SetSigmaThreshold( + Dev_t dev, + uint16_t sigma_mm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + if(sigma_mm>(uint16_t)((uint16_t)0xFFFF>>2)) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + status |= VL53L4ED_WrWord(dev, + VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm<<2); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_GetSigmaThreshold( + Dev_t dev, + uint16_t *p_sigma_mm) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + + status += VL53L4ED_RdWord(dev, + VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); + *p_sigma_mm = *p_sigma_mm >> 2; + + return status; +} + +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate( + Dev_t dev) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t tmp = 0, continue_loop = 1; + uint16_t i = 0; + + status |= VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); + status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0x92); + status |= VL53L4ED_StartRanging(dev); + + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } + else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_ClearInterrupt(dev); + status |= VL53L4ED_StopRanging(dev); + + status += VL53L4ED_WrByte(dev, + VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); + status += VL53L4ED_WrByte(dev, 0x0B, 0); + return status; +} diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h new file mode 100644 index 000000000..df55ef3d0 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h @@ -0,0 +1,405 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#ifndef VL53L4ED_API_H_ +#define VL53L4ED_API_H_ + +#include "platform.h" + +/** + * @brief Driver version + */ + +#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 +#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 + +/** + * @brief Driver error type + */ + +typedef uint8_t VL53L4ED_Error; + +#define VL53L4ED_ERROR_NONE ((uint8_t)0U) +#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) +#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) +#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) + + +/** + * @brief Inner Macro for API. Not for user, only for development. + */ + +#define VL53L4ED_SOFT_RESET ((uint16_t)0x0000)) +#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) +#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) +#define VL53L4ED_XTALK_PLANE_OFFSET_KCPS ((uint16_t)0x0016) +#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) +#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) +#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) +#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) +#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) +#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) +#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) +#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) +#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) +#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) +#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) +#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) +#define VL53L4ED_INTERMEASUREMENT_MS ((uint16_t)0x006C) +#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) +#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) +#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) +#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) +#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) +#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) +#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) +#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) +#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) +#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) + + +#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) +#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) +#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) + +/** + * @brief defines Software Version + */ + +typedef struct { + uint8_t major; /*!< major number */ + uint8_t minor; /*!< minor number */ + uint8_t build; /*!< build number */ + uint32_t revision; /*!< revision number */ +} VL53L4ED_Version_t; + +/** + * @brief Packed reading results type + */ + +typedef struct { + + /* Status of measurements. If the status is equal to 0, the data are valid*/ + uint8_t range_status; + /* Measured distance in millimeters */ + uint16_t distance_mm; + /* Ambient noise in kcps */ + uint16_t ambient_rate_kcps; + /* Ambient noise in kcps/SPAD */ + uint16_t ambient_per_spad_kcps; + /* Measured signal of the target in kcps */ + uint16_t signal_rate_kcps; + /* Measured signal of the target in kcps/SPAD */ + uint16_t signal_per_spad_kcps; + /* Number of SPADs enabled */ + uint16_t number_of_spad; + /* Estimated measurements std deviation in mm */ + uint16_t sigma_mm; +} VL53L4ED_ResultsData_t; + +/** + * @brief This function programs the software driver version. + * @param (VL53L4ED_Version_t) pVersion : Pointer of structure, containing the + * software version. + * @return (VL53L4ED_ERROR) status : 0 if SW version is OK. + */ + +VL53L4ED_Error VL53L4ED_GetSWVersion( + VL53L4ED_Version_t *pVersion); + + +/** + * @brief This function sets a new I2C address to a sensor. It can be used for + * example when multiple sensors share the same I2C bus. + * @param (Dev_t) dev : Device instance to update. + * @param (uint8_t) new_address : New I2C address. + * @return (VL53L4ED_ERROR) status : 0 if I2C address has been correctly + * programmed. + */ + +VL53L4ED_Error VL53L4ED_SetI2CAddress( + Dev_t dev, + uint8_t new_address); + +/** + * @brief This function is used to get the sensor id of VL53L4ED. The sensor id + * should be 0xEBAA. + * @param (Dev_t) dev : Device instance. + * @param (uint16_t) *p_id : Sensor id. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetSensorId( + Dev_t dev, + uint16_t *p_id); + +/** + * @brief This function is used to initialize the sensor. + * @param (Dev_t) dev : Device instance to initialize. + * @return (VL53L4ED_ERROR) status : 0 if init is OK. + */ + +VL53L4ED_Error VL53L4ED_SensorInit( + Dev_t dev); + +/** + * @brief This function clears the interrupt. It needs to be called after a + * ranging data reading to arm the interrupt for the next data ready event. + * @param (Dev_t) dev : Device instance. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_ClearInterrupt( + Dev_t dev); + +/** + * @brief This function starts a ranging session. The ranging operation is + * continuous. The clear interrupt has to be done after each get data to allow + * the interrupt to raise when the next data is ready. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_StartRanging( + Dev_t dev); + +/** + * @brief This function stops the ranging in progress. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_StopRanging( + Dev_t dev); + +/** + * @brief This function check if a new data is available by polling a dedicated + * register. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint8_t) *p_is_data_ready : Pointer containing a flag to know if a + * data is ready : 0 = no data ready, 1 = data ready. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_CheckForDataReady( + Dev_t dev, + uint8_t *p_is_data_ready); + +/** + * @brief This function sets new range timing. Timing are composed of + * TimingBudget and InterMeasurement. TimingBudget represents the timing during + * VCSEL enabled, and InterMeasurement the time between two measurements. + * The sensor can have different ranging mode depending of the configuration, + * please refer to the user manual for more information. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint32_t) timing_budget_ms : New timing budget in ms. Value can be + * between 10ms and 200ms. Default is 50ms. + * @param (uint32_t) inter_measurement_ms : New inter-measurement in ms. If the + * value is equal to 0, the ranging period is defined by the timing budget. + * Otherwise, inter-measurement must be > timing budget. When all the timing + * budget is consumed, the device goes in low power mode until inter-measurement + * is done. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetRangeTiming( + Dev_t dev, + uint32_t timing_budget_ms, + uint32_t inter_measurement_ms); + +/** + * @brief This function gets the current range timing. Timing are composed of + * TimingBudget and InterMeasurement. TimingBudget represents the timing during + * VCSEL enabled, and InterMeasurement the time between two measurements. + * The sensor can have different ranging mode depending of the configuration, + * please refer to the user manual for more information. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint32_t) *p_timing_budget_ms : Pointer containing the current + * timing budget in ms. + * @param (uint32_t) *p_inter_measurement_ms : Pointer containing the current + * inter-measurement in ms. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetRangeTiming( + Dev_t dev, + uint32_t *p_timing_budget_ms, + uint32_t *p_inter_measurement_ms); + +/** + * @brief This function gets the results reported by the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (VL53L4ED_ResultsData_t) *pResult : Pointer of structure, filled with the + * ranging results. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetResult(Dev_t dev, VL53L4ED_ResultsData_t *pResult); + +/** + * @brief This function sets a new offset correction in mm. Offset corresponds + * to the difference in millimeters between real distance and measured distance. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) OffsetValueInMm : Offset value in millimeters. The minimum + * value is -1024mm and maximum is 1023mm. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetOffset(Dev_t dev, int16_t OffsetValueInMm); + +/** + * @brief This function gets the current offset correction in mm. Offset + * corresponds to the difference in millimeters between real distance and + * measured distance. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) OffsetValueInMm : Offset value in millimeters. The minimum + * value is -1024mm and maximum is 1023mm. + * @return (uint8_t) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetOffset(Dev_t dev, int16_t *Offset); + +/** + * @brief This function sets a new Xtalk value in kcps. Xtalk represents the + * correction to apply to the sensor when a protective coverglass is placed + * at the top of the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) XtalkValueKcps : New xtalk value in kcps. The default + * value is 0 kcps (no coverglass). Minimum is 0 kcps , and maximum is 128 + * kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetXtalk(Dev_t dev, uint16_t XtalkValueKcps); + +/** + * @brief This function gets the current Xtalk value in kcps. Xtalk represents + * the correction to apply to the sensor when a protective coverglass is placed + * at the top of the sensor. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) p_xtalk_kcps : Pointer of current xtalk value in kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps); + +/** + * @brief This function sets new detection thresholds. The detection + * thresholds can be programmed to generate an interrupt on pin 7 (GPIO1), only + * when a condition on distance is reach. Example: + * VL53L4ED_SetDistanceThreshold(dev,100,300,0): Below 100 mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,1): Above 300 mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,2): Below 100mm or above 300mm + * VL53L4ED_SetDistanceThreshold(dev,100,300,3): Above 100mm or below 300mm + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) distance_low_mm : Low distance threshold in millimeters. + * @param (uint16_t) distance_high_mm : High distance threshold in millimeters. + * @param (uint8_t) window : Interrupt windows (0=below low threshold; + * 1=above high threshold; 2=out of low/high windows; 3=in low/high windows) + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, + uint16_t distance_low_mm, + uint16_t distance_high_mm, + uint8_t window); + + +/** + * @brief This function gets the current detection thresholds. The detection + * thresholds can be programmed to generate an interrupt on pin 7 (GPIO1), only + * when a condition on distance is reach. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_distance_low_mm : Pointer of low distance threshold in + * millimeters. + * @param (uint16_t) *p_distance_high_mm : Pointer of high distance threshold in + * millimeters. + * @param (uint8_t) *p_window : Interrupt windows (0=below low threshold; + * 1=above high threshold; 2=out of low/high windows; 3=in low/high windows) + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, + uint16_t *p_distance_low_mm, + uint16_t *p_distance_high_mm, + uint8_t *p_window); + +/** + * @brief This function sets a new signal threshold in kcps. If a + * target has a lower signal as the programmed value, the result status in + * structure 'VL53L4ED_ResultsData_t' will be equal to 2. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) signal_kcps : New signal threshold in kcps. The default + * value is 1024 kcps. Minimum is 0 kcps (no threshold), and maximum is 16384 + * kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps); + +/** + * @brief This function returns the current signal threshold in kcps. If a + * target has a lower signal as the programmed value, the result status in + * structure 'VL53L4ED_ResultsData_t' will be equal to 2. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_signal_kcps : Pointer of signal threshold in kcps. + * @return (VL53L4ED_ERROR) status : 0 if OK. + */ + +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, + uint16_t *p_signal_kcps); + +/** + * @brief This function programs a new sigma threshold. The sigma corresponds to + * the standard deviation of the returned pulse. If the computed sigma is above + * the programmed value, the result status in structure 'VL53L4ED_ResultsData_t' + * will be equal to 1. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) sigma_mm : New sigma threshold in mm. The default value is + * 15mm. Minimum is 0mm (not threshold), and maximum is 16383mm. + * @return (VL53L4ED_ERROR) status : 0 if programming is or 255 if value is too + * high. + */ + +VL53L4ED_Error VL53L4ED_SetSigmaThreshold( + Dev_t dev, + uint16_t sigma_mm); + +/** + * @brief This function gets the current sigma threshold. The sigma corresponds + * to the standard deviation of the returned pulse. If the computed sigma is + * above the programmed value, the result status in structure + * 'VL53L4ED_ResultsData_t' will be equal to 1. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (uint16_t) *p_sigma_mm : Current sigma threshold in mm. + * @return (VL53L4ED_ERROR) status : 0 if programming is OK. + */ + +VL53L4ED_Error VL53L4ED_GetSigmaThreshold( + Dev_t dev, + uint16_t *p_sigma_mm); + +/** + * @brief This function can be called when the temperature might have changed by + * more than 8 degrees Celsius. The function can only be used if the sensor is + * not ranging, otherwise, the ranging needs to be stopped using function + * 'VL53L4ED_StopRanging()'. After calling this function, the ranging can + * restart normally. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @return (VL53L4ED_ERROR) status : 0 if update is OK. + */ + +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev); + +#endif //VL53L4ED_API_H_ diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c new file mode 100644 index 000000000..e3d95c223 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c @@ -0,0 +1,242 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_calibration.c + * @brief Calibration functions implementation + */ + +#include +#include "VL53L4ED_api.h" +#include "VL53L4ED_calibration.h" + +VL53L4ED_Error VL53L4ED_CalibrateOffset( + Dev_t dev, + int16_t TargetDistInMm, + int16_t *p_measured_offset_mm, + int16_t nb_samples) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t i, tmp, continue_loop; + uint16_t j, tmpOff; + int16_t AvgDistance = 0; + VL53L4ED_ResultsData_t results; + + if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) + || ((TargetDistInMm < (int16_t)10) + || (TargetDistInMm > (int16_t)1000))) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, 0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, 0x0); + status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, 0x0); + + /* Device heat loop (10 samples) */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)10; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + } + status |= VL53L4ED_StopRanging(dev); + + /* Device ranging */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)nb_samples; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + AvgDistance += (int16_t)results.distance_mm; + } + + status |= VL53L4ED_StopRanging(dev); + AvgDistance = AvgDistance / nb_samples; + *p_measured_offset_mm = (int16_t)TargetDistInMm - AvgDistance; + tmpOff = (uint16_t) *p_measured_offset_mm * (uint16_t)4; + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, tmpOff); + } + + return status; +} + +VL53L4ED_Error VL53L4ED_CalibrateXtalk( + Dev_t dev, + int16_t TargetDistInMm, + uint16_t *p_measured_xtalk_kcps, + int16_t nb_samples) +{ + VL53L4ED_Error status = VL53L4ED_ERROR_NONE; + uint8_t i, tmp, continue_loop; + float_t AverageSignal = (float_t)0.0; + float_t AvgDistance = (float_t)0.0; + float_t AverageSpadNb = (float_t)0.0; + float_t TargetDistance = (float_t)TargetDistInMm; + float_t tmp_xtalk, CounterNbSamples = (float_t)0.0; + VL53L4ED_ResultsData_t results; + + uint16_t calXtalk, j; + + *p_measured_xtalk_kcps = 0; + if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) + || ((TargetDistInMm < (int16_t)10) + || (TargetDistInMm > (int16_t)5000))) + { + status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; + } + else + { + /* Disable Xtalk compensation */ + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); + + /* Device heat loop (10 samples) */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)10; i++) { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + } + status |= VL53L4ED_StopRanging(dev); + + /* Device ranging loop */ + status |= VL53L4ED_StartRanging(dev); + for (i = 0; i < (uint8_t)nb_samples; i++) + { + tmp = (uint8_t)0; + j = (uint16_t)0; + continue_loop = (uint8_t)1; + do{ + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if(tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } + else if(j < (uint16_t)5000) /* Wait for answer*/ + { + j++; + } + else /* Timeout 5000ms reached */ + { + continue_loop = (uint8_t)0; + status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + }while(continue_loop == (uint8_t)1); + + status |= VL53L4ED_GetResult(dev, &results); + status |= VL53L4ED_ClearInterrupt(dev); + + /* Discard invalid measurements and first frame */ + if (results.range_status == (uint8_t)0 + && i > (uint8_t)0) + { + AvgDistance += (float_t)results.distance_mm; + AverageSpadNb += (float_t)results.number_of_spad; + AverageSignal += (float_t)results.signal_rate_kcps; + CounterNbSamples++; + } + } + status |= VL53L4ED_StopRanging(dev); + + if (CounterNbSamples == 0) + { + status = VL53L4ED_ERROR_XTALK_FAILED; + } + else + { + AvgDistance /= CounterNbSamples; + AverageSpadNb /= CounterNbSamples; + AverageSignal /= CounterNbSamples; + + tmp_xtalk = (float_t)1.0 - (AvgDistance/TargetDistance); + tmp_xtalk *= (AverageSignal/AverageSpadNb); + + /* 127kcps is the max Xtalk value (65536/512) */ + if(tmp_xtalk > (uint16_t)127) + { + status = VL53L4ED_ERROR_XTALK_FAILED; + } + else + { + *p_measured_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); + + /* Send data to firmware */ + calXtalk = (uint16_t)(tmp_xtalk * (float_t)512.0); + status |= VL53L4ED_WrWord(dev, + VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); + } + } + } + + return status; +} diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h new file mode 100644 index 000000000..c953d0239 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h @@ -0,0 +1,73 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +/** + * @file vl53l4ed_calibration.h + * @brief Calibration Functions definition + */ + +#ifndef VL53L4ED_CALIBRATION_H_ +#define VL53L4ED_CALIBRATION_H_ + +#include "platform.h" + +/** + * @brief This function can be used to perform an offset calibration. Offset + * corresponds to the difference in millimeters between real distance and + * measured distance. ST recommend to perform offset at 100m, on a grey17% + * reflective target, but any other distance and reflectance can be used. + * The function returns the offset value found and programs the offset + * compensation into the device. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param (int16_t) TargetDistInMm : Real distance between the sensor and the + * target in millimeters. ST recommend 100mm. Min distance is 10mm and max is + * 1000mm. + * @param (int16_t) nb_samples : Number of samples (between 5 and 255). A higher + * number of samples increases the accuracy, but it also takes more time. ST + * recommend to use at least 10 samples. + * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g + * invalid nb of samples). + */ + +VL53L4ED_Error VL53L4ED_CalibrateOffset( + Dev_t dev, + int16_t TargetDistInMm, + int16_t *p_measured_offset_mm, + int16_t nb_samples); + + +/** + * @brief This function can be used to perform a Xtalk calibration. Xtalk + * represents the correction to apply to the sensor when a protective coverglass + * is placed at the top of the sensor. The distance for calibration depends of + * the coverglass, it needs to be characterized. Please refer to the User Manual + * for more information. + * The function returns the Xtalk value found and programs the Xtalk + * compensation into the device. + * @param (Dev_t) dev : instance of selected VL53L4ED sensor. + * @param uint16_t) TargetDistInMm : Real distance between the sensor and the + * target in millimeters. This distance needs to be characterized, as described + * into the User Manual. + * @param (int16_t) nb_samples : Number of samples (between 5 and 255). A higher + * number of samples increases the accuracy, but it also takes more time. ST + * recommend to use at least 10 samples. + * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g + * invalid nb of samples). + */ + +VL53L4ED_Error VL53L4ED_CalibrateXtalk( + Dev_t dev, + int16_t TargetDistInMm, + uint16_t *p_measured_xtalk_kcps, + int16_t nb_samples); + +#endif //VL53L4ED_CALIBRATION_H_ diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c new file mode 100644 index 000000000..ba3d6f1a6 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c @@ -0,0 +1,113 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#include "platform.h" +#include "main.h" +#include "i2c.h" + +extern I2C_HandleTypeDef hi2c1; + + +/* +Im legit just using the example stm provides but instead of filling everything out im only copying the platform.c implemintation +for the vl53l4ed sensor. + + +*/ + +uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[4]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 4, 100); + *value = ((data_read[0] << 24) | (data_read[1]<<16) | + (data_read[2]<<8)| (data_read[3])); + return status; +} + +uint8_t VL53L4ED_RdWord(uint16_t dev, uint16_t RegisterAdress, uint16_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[2]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 2, 100); + *value = (data_read[0] << 8) | (data_read[1]); + return status; +} + +uint8_t VL53L4ED_RdByte(uint16_t dev, uint16_t RegisterAdress, uint8_t *value) +{ + uint8_t status = 0; + uint8_t data_write[2]; + uint8_t data_read[1]; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); + status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 1, 100); + *value = data_read[0]; + return status; +} + +uint8_t VL53L4ED_WrByte(uint16_t dev, uint16_t RegisterAdress, uint8_t value) +{ + uint8_t data_write[3]; + uint8_t status = 0; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 3, 100); + return status; +} + +uint8_t VL53L4ED_WrWord(uint16_t dev, uint16_t RegisterAdress, uint16_t value) +{ + uint8_t data_write[4]; + uint8_t status = 0; + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = (value >> 8) & 0xFF; + data_write[3] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 4, 100); + return status; +} + +uint8_t VL53L4ED_WrDWord(uint16_t dev, uint16_t RegisterAdress, uint32_t value) +{ + uint8_t data_write[6]; + uint8_t status = 0; + + data_write[0] = (RegisterAdress >> 8) & 0xFF; + data_write[1] = RegisterAdress & 0xFF; + data_write[2] = (value >> 24) & 0xFF; + data_write[3] = (value >> 16) & 0xFF; + data_write[4] = (value >> 8) & 0xFF; + data_write[5] = value & 0xFF; + status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 6, 100); + return status; +} + +uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t time_ms) +{ + HAL_Delay(time_ms); + return 0; +} diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h new file mode 100644 index 000000000..ee27e91f2 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h @@ -0,0 +1,80 @@ +/** + * + * 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. + * + ****************************************************************************** + */ + +#ifndef _PLATFORM_H_ +#define _PLATFORM_H_ +#pragma once + +#include +#include + +/** +* VL53L4ED device instance. +*/ + +typedef uint16_t Dev_t; + +/** + * @brief Error instance. + */ +typedef uint8_t VL53L4ED_Error; + +/** + * @brief If the macro below is defined, the device will be programmed to run + * with I2C Fast Mode Plus (up to 1MHz). Otherwise, default max value is 400kHz. + */ + +//#define VL53L4ED_I2C_FAST_MODE_PLUS + + +/** + * @brief Read 32 bits through I2C. + */ + +uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t registerAddr, uint32_t *value); +/** + * @brief Read 16 bits through I2C. + */ + +uint8_t VL53L4ED_RdWord(Dev_t dev, uint16_t registerAddr, uint16_t *value); + +/** + * @brief Read 8 bits through I2C. + */ + +uint8_t VL53L4ED_RdByte(Dev_t dev, uint16_t registerAddr, uint8_t *value); + +/** + * @brief Write 8 bits through I2C. + */ + +uint8_t VL53L4ED_WrByte(Dev_t dev, uint16_t registerAddr, uint8_t value); + +/** + * @brief Write 16 bits through I2C. + */ + +uint8_t VL53L4ED_WrWord(Dev_t dev, uint16_t RegisterAdress, uint16_t value); + +/** + * @brief Write 32 bits through I2C. + */ + +uint8_t VL53L4ED_WrDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t value); + +/** + * @brief Wait during N milliseconds. + */ + +uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t TimeMs); + +#endif // _PLATFORM_H_ \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Extras/extra.c b/SAMM/IMUandTOF/Core/Extras/extra.c new file mode 100644 index 000000000..3611c8904 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/extra.c @@ -0,0 +1,58 @@ +#include "extra.h" +#include "main.h" +#include "i2c.h" +#include + + +void MLX90640_I2CInit(void) +{ + MX_I2C2_Init(); + return; +} + +int MLX90640_I2CGeneralReset(void) +{ + uint8_t data = 0x06; + return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data , 1, 1000); +} + + +int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +{ + //HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); + //HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); + /* + HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size) + */ + //return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 10000); +} + +int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) +{ + // union + // { + // uint8_t bytes[2]; + // uint16_t data; + // }extra = {.data = data}; + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); + //return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); +} + +void MLX90640_I2CFreqSet(int freq) +{ + return; +} + +uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +{ + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 500); +} + +uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data) +{ + + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *) &data, 2, 500); +} + diff --git a/SAMM/IMUandTOF/Core/Extras/extra.h b/SAMM/IMUandTOF/Core/Extras/extra.h new file mode 100644 index 000000000..2b820f432 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Extras/extra.h @@ -0,0 +1,20 @@ +#ifndef __EXTRA_H__ +#define __EXTRA_H__ +#include +/* +This is for some xtra fucntions that would be needed later on + + +*/ + +void MLX90640_I2CInit(void); +int MLX90640_I2CGeneralReset(void); +int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +int MLX90640_I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +void MLX90640_I2CFreqSet(int freq); + + +uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); + +#endif /* __EXTRA_H__ */ \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Inc/adc.h b/SAMM/IMUandTOF/Core/Inc/adc.h new file mode 100644 index 000000000..5692f3846 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/adc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.h + * @brief This file contains all the function prototypes for + * the adc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __ADC_H__ +#define __ADC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern ADC_HandleTypeDef hadc1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_ADC1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __ADC_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/bmi323.h b/SAMM/IMUandTOF/Core/Inc/bmi323.h new file mode 100644 index 000000000..f434cb8dd --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/bmi323.h @@ -0,0 +1,200 @@ +#ifndef BMI323_H +#define BMI323_H + +//includes + + +#include "main.h" +#include "spi.h" +#include "stm32g474xx.h" + +//BMI323 register defines + +#define BMI323_CHIP_ID 0x00 +#define BMI323_ERR_REG 0x01 +#define BMI323_STATUS 0x02 +#define BMI323_ACC_X 0x03 +#define BMI323_ACC_Y 0x04 +#define BMI323_ACC_Z 0x05 +#define BMI323_GYR_X 0x06 +#define BMI323_GYR_Y 0x07 +#define BMI323_GYR_Z 0x08 +#define BMI323_TEMP_DATA 0x09 + +#define BMI323_FEATURE_IO0 0x10 +#define BMI323_FEATURE_I01 0x11 +#define BMI323_FEATURE_IO2 0x12 +#define BMI323_FEATURE_IO3 0x13 +#define BMI323_FEATURE_IO_STATUS 0x14 + +#define BMI323_ACC_CONF 0x20 +#define BMI323_GYR_CONF 0x21 +#define BMI323_ALT_ACC_CONF 0x28 +#define BMI323_ALT_GYR_CONF 0x29 +#define BMI323_ALT_CONF 0x2A + +#define BMI323_FIFO_CONF 0x36 +#define BMI323_FIFO_CTRL 0x38 + +#define BMI323_FEATURE_CTRL 0x40 + +#define BMI323_ACC_DP_OFF_X 0x60 +#define BMI323_ACC_DP_DGAIN_X 0x61 +#define BMI323_ACC_DP_OFF_Y 0x62 +#define BMI323_ACC_DP_DGAIN_Y 0x63 +#define BMI323_ACC_DP_OFF_Z 0x64 +#define BMI323_ACC_DP_DGAIN_Z 0x65 +#define BMI323_GYR_DP_OFF_X 0x66 +#define BMI323_GYR_DP_DGAIN_X 0x67 +#define BMI323_GYR_DP_OFF_Y 0x68 +#define BMI323_GYR_DP_DGAIN_Y 0x69 +#define BMI323_GYR_DP_OFF_Z 0x6A +#define BMI323_GYR_DP_DGAIN_Z 0x6B + +#define BMI323_CMD 0x7E +#define BMI323_CFG_RES 0x7F + +//BMI323 potential spi address +#define BMI323_I2C_ADDR 0x68 +#define BMI323_I2C_ADDR_ALT 0x69 + +//BMI323 sub-defines for registers +#define BMI323_CHIP_ID_RESET_VAL 0x0043 +#define BMI323_ERR_REG_RESET_VAL 0x0000 +#define BMI323_STATUS_RESET_VAL 0x0001 +#define BMI323_ACC_X_RESET_VAL 0x8000 +#define BMI323_ACC_Y_RESET_VAL 0x8000 +#define BMI323_ACC_Z_RESET_VAL 0x8000 +#define BMI323_GYR_X_RESET_VAL 0x8000 +#define BMI323_GYR_Y_RESET_VAL 0x8000 +#define BMI323_GYR_Z_RESET_VAL 0x8000 +#define BMI323_TEMP_DATA_RESET_VAL 0x8000 + +#define BMI323_FEATURE_IO0_RESET_VAL 0x0000 +#define BMI323_FEATURE_I01_RESET_VAL 0x0000 + +#define BMI323_ACC_CONF_RESET_VAL 0x0028 +#define BMI323_GYR_CONF_RESET_VAL 0x0048 + +#define BMI323_FIFO_CONF_RESET_VAL 0x0000 +#define BMI323_FIFO_CTRL_RESET_VAL 0x0000 + +#define BMI323_ACC_DP_OFF_X_RESET_VAL 0x0000 +#define BMI323_ACC_DP_DGAIN_X_RESET_VAL 0x0000 +#define BMI323_ACC_DP_OFF_Y_RESET_VAL 0x0000 +#define BMI323_ACC_DP_DGAIN_Y_RESET_VAL 0x0000 +#define BMI323_ACC_DP_OFF_Z_RESET_VAL 0x0000 +#define BMI323_ACC_DP_DGAIN_Z_RESET_VAL 0x0000 +#define BMI323_GYR_DP_OFF_X_RESET_VAL 0x0000 +#define BMI323_GYR_DP_DGAIN_X_RESET_VAL 0x0000 +#define BMI323_GYR_DP_OFF_Y_RESET_VAL 0x0000 +#define BMI323_GYR_DP_DGAIN_Y_RESET_VAL 0x0000 +#define BMI323_GYR_DP_OFF_Z_RESET_VAL 0x0000 +#define BMI323_GYR_DP_DGAIN_Z_RESET_VAL 0x0000 + +#define BMI323_CMD_RESET_VAL 0x0000 +#define BMI323_CFG_RES_RESET_VAL 0x0000 + +//defines for commands +#define BMI323_CMD_SOFT_RESET 0xDEAF +#define BMI323_CMD_CALIB 0x0101 +#define BMI323_CMD_CALIB_ABORT 0x0200 + +//defines for features +#define BMI323_FEATURE_IO2_EN 0x012C +#define BMI323_FEATURE_IO_STS 0x0001 +#define BMI323_FEATURE_CTRL_ENGINE_EN 0x0001 +#define BMI323_FEATURE_IO1_STATUS 0x0001 + + +//defines for acc and gyro conf +#define SUSPEND 0b000 +#define LOW_POWER 0b011 +#define HIGH_PERF 0b111 +#define NORMAL 0b100 + +//these are the values that are used for the polling frequency +#define ODR_0_78 0x1 +#define ODR_1_56 0x2 +#define ODR_3_12 0x3 +#define ODR_6_25 0x4 +#define ODR_12_5 0x5 +#define ODR_25 0x6 +#define ODR_50 0x7 +#define ODR_100 0x8 +#define ODR_200 0x9 +#define ODR_400 0xA +#define ODR_800 0xB +#define ODR_1600 0xC +#define ODR_3200 0xD +#define ODR_6400 0xE + +//these are the values that are used for the -3dB bandwidth +#define ODR_DIV_2 0x0 +#define ODR_DIV_4 0x1 + +//these are defines for the sample averaging +#define AVG_0 0x0 +#define AVG_2 0x1 +#define AVG_4 0x2 +#define AVG_8 0x3 +#define AVG_16 0x4 +#define AVG_32 0x5 +#define AVG_64 0x6 + +//acc specific defines for range +#define ACC_RANGE_2G 0x0 +#define ACC_RANGE_4G 0x1 +#define ACC_RANGE_8G 0x2 +#define ACC_RANGE_16G 0x3 + +//gyro specific defines for range +//WRONG VALUES +#define GYR_RANGE_2000 0x0 +#define GYR_RANGE_1000 0x1 +#define GYR_RANGE_500 0x2 +#define GYR_RANGE_250 0x3 + +#define BMI323_TIMEOUT 1000 + +typedef struct +{ + SPI_HandleTypeDef *spi_port; + uint16_t chip_id; + GPIO_TypeDef *port; + uint16_t pin; + /* data */ +} bmi323 ; + +uint8_t bmi323_init(bmi323 *bmi323_dev, SPI_HandleTypeDef *spi_port, GPIO_TypeDef *port, uint16_t pin); +uint16_t bmi323_read(bmi323 *bmi323_dev, uint8_t reg); +uint8_t bmi323_write(bmi323 *bmi323_dev, uint8_t reg, uint16_t data); +uint8_t bmi323_soft_reset(bmi323 *bmi323_dev); +uint8_t bmi323_calib(bmi323 *bmi323_dev); +uint8_t bmi323_calib_abort(bmi323 *bmi323_dev); +uint16_t bmi323_read_acc_x(bmi323 *bmi323_dev); +uint16_t bmi323_read_acc_y(bmi323 *bmi323_dev); +uint16_t bmi323_read_acc_z(bmi323 *bmi323_dev); +uint16_t bmi323_read_gyr_x(bmi323 *bmi323_dev); +uint16_t bmi323_read_gyr_y(bmi323 *bmi323_dev); +uint16_t bmi323_read_gyr_z(bmi323 *bmi323_dev); +uint16_t bmi323_read_temp_data(bmi323 *bmi323_dev); +uint16_t bmi323_read_status(bmi323 *bmi323_dev); +uint16_t bmi323_read_err_reg(bmi323 *bmi323_dev); +uint16_t bmi323_read_chip_id(bmi323 *bmi323_dev); +uint16_t bmi323_read_acc_conf(bmi323 *bmi323_dev); +uint16_t bmi323_read_gyr_conf(bmi323 *bmi323_dev); +uint16_t bmi323_read_acc_alt_conf(bmi323 *bmi323_dev); +uint16_t bmi323_read_gyr_alt_conf(bmi323 *bmi323_dev); +// uint8_t bmi323_enable_acc(bmi323 *bmi323_dev); +// uint8_t bmi323_enable_gyr(bmi323 *bmi323_dev); +uint8_t bmi323_disable_acc(bmi323 *bmi323_dev); +uint8_t bmi323_enable_gyro(bmi323 *bmi323_dev, uint8_t gyr_mode, uint8_t gyr_avg_num, uint8_t gyr_bw, uint8_t gyr_range, uint8_t gyr_odr); +uint8_t bmi323_enable_acc(bmi323 *bmi323_dev, uint8_t acc_mode, uint8_t acc_avg_num, uint8_t acc_bw, uint8_t acc_range, uint8_t acc_odr); +uint8_t bmi323_read_acc(bmi323 *bmi323_dev, int16_t *acc_data); +uint8_t bmi323_read_gyr(bmi323 *bmi323_dev, int16_t *gyr_data); +uint8_t bmi323_read_all(bmi323 *bmi323_dev, int16_t *temp_data); + + + +#endif // BMI323_H \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Inc/crc.h b/SAMM/IMUandTOF/Core/Inc/crc.h new file mode 100644 index 000000000..836835d02 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/crc.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file crc.h + * @brief This file contains all the function prototypes for + * the crc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __CRC_H__ +#define __CRC_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern CRC_HandleTypeDef hcrc; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_CRC_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __CRC_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/dma.h b/SAMM/IMUandTOF/Core/Inc/dma.h new file mode 100644 index 000000000..f3b1a09f9 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/dma.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.h + * @brief This file contains all the function prototypes for + * the dma.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __DMA_H__ +#define __DMA_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* DMA memory to memory transfer handles -------------------------------------*/ + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_DMA_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __DMA_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/fdcan.h b/SAMM/IMUandTOF/Core/Inc/fdcan.h new file mode 100644 index 000000000..9289ea3a6 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/fdcan.h @@ -0,0 +1,55 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file fdcan.h + * @brief This file contains all the function prototypes for + * the fdcan.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __FDCAN_H__ +#define __FDCAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern FDCAN_HandleTypeDef hfdcan1; + +extern FDCAN_HandleTypeDef hfdcan2; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_FDCAN1_Init(void); +void MX_FDCAN2_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __FDCAN_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/gpio.h b/SAMM/IMUandTOF/Core/Inc/gpio.h new file mode 100644 index 000000000..708bac71d --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/gpio.h @@ -0,0 +1,49 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __GPIO_H__ +#define __GPIO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_GPIO_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ GPIO_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/i2c.h b/SAMM/IMUandTOF/Core/Inc/i2c.h new file mode 100644 index 000000000..84ed75d2e --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/i2c.h @@ -0,0 +1,52 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.h + * @brief This file contains all the function prototypes for + * the i2c.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __I2C_H__ +#define __I2C_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern I2C_HandleTypeDef hi2c1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_I2C1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __I2C_H__ */ + diff --git a/SAMM/IMUandTOF/Core/Inc/main.h b/SAMM/IMUandTOF/Core/Inc/main.h new file mode 100644 index 000000000..17a2fe760 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/main.h @@ -0,0 +1,80 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __MAIN_H +#define __MAIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32g4xx_hal.h" + +#include "stm32g4xx_ll_rcc.h" +#include "stm32g4xx_ll_bus.h" +#include "stm32g4xx_ll_crs.h" +#include "stm32g4xx_ll_system.h" +#include "stm32g4xx_ll_exti.h" +#include "stm32g4xx_ll_cortex.h" +#include "stm32g4xx_ll_utils.h" +#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_dma.h" +#include "stm32g4xx_ll_gpio.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +/* Private defines -----------------------------------------------------------*/ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MAIN_H */ diff --git a/SAMM/IMUandTOF/Core/Inc/spi.h b/SAMM/IMUandTOF/Core/Inc/spi.h new file mode 100644 index 000000000..434f0eeb8 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/spi.h @@ -0,0 +1,51 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file spi.h + * @brief This file contains all the function prototypes for + * the spi.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __SPI_H__ +#define __SPI_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern SPI_HandleTypeDef hspi1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_SPI1_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __SPI_H__ */ diff --git a/SAMM/IMUandTOF/Core/Inc/stm32_assert.h b/SAMM/IMUandTOF/Core/Inc/stm32_assert.h new file mode 100644 index 000000000..61631c41e --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/stm32_assert.h @@ -0,0 +1,53 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32_assert.h + * @author MCD Application Team + * @brief STM32 assert file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 __STM32_ASSERT_H +#define __STM32_ASSERT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_ASSERT_H */ + diff --git a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h new file mode 100644 index 000000000..4087e183c --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h @@ -0,0 +1,380 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_conf.h + * @author MCD Application Team + * @brief HAL configuration file + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 STM32G4xx_HAL_CONF_H +#define STM32G4xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ + +#define HAL_MODULE_ENABLED + + /*#define HAL_ADC_MODULE_ENABLED */ +/*#define HAL_COMP_MODULE_ENABLED */ +/*#define HAL_CORDIC_MODULE_ENABLED */ +#define HAL_CRC_MODULE_ENABLED +/*#define HAL_CRYP_MODULE_ENABLED */ +/*#define HAL_DAC_MODULE_ENABLED */ +#define HAL_FDCAN_MODULE_ENABLED +/*#define HAL_FMAC_MODULE_ENABLED */ +/*#define HAL_HRTIM_MODULE_ENABLED */ +/*#define HAL_IRDA_MODULE_ENABLED */ +/*#define HAL_IWDG_MODULE_ENABLED */ +#define HAL_I2C_MODULE_ENABLED +/*#define HAL_I2S_MODULE_ENABLED */ +/*#define HAL_LPTIM_MODULE_ENABLED */ +/*#define HAL_NAND_MODULE_ENABLED */ +/*#define HAL_NOR_MODULE_ENABLED */ +/*#define HAL_OPAMP_MODULE_ENABLED */ +/*#define HAL_PCD_MODULE_ENABLED */ +/*#define HAL_QSPI_MODULE_ENABLED */ +/*#define HAL_RNG_MODULE_ENABLED */ +/*#define HAL_RTC_MODULE_ENABLED */ +/*#define HAL_SAI_MODULE_ENABLED */ +/*#define HAL_SMARTCARD_MODULE_ENABLED */ +/*#define HAL_SMBUS_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED +/*#define HAL_SRAM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ +/*#define HAL_USART_MODULE_ENABLED */ +/*#define HAL_WWDG_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED + +/* ########################## Register Callbacks selection ############################## */ +/** + * @brief This is the list of modules where register callback can be used + */ +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U +#define USE_HAL_COMP_REGISTER_CALLBACKS 0U +#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U +#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U +#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U +#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U +#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U +#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U +#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_NAND_REGISTER_CALLBACKS 0U +#define USE_HAL_NOR_REGISTER_CALLBACKS 0U +#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U +#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U +#define USE_HAL_RNG_REGISTER_CALLBACKS 0U +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U +#define USE_HAL_SAI_REGISTER_CALLBACKS 0U +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U +#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U +#define USE_HAL_UART_REGISTER_CALLBACKS 0U +#define USE_HAL_USART_REGISTER_CALLBACKS 0U +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U + +/* ########################## Oscillator Values adaptation ####################*/ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. + * This internal oscillator is mainly dedicated to provide a high precision clock to + * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. + * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency + * which is subject to manufacturing process variations. + */ +#if !defined (HSI48_VALUE) + #define HSI48_VALUE (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. + The real value my vary depending on manufacturing process variations.*/ +#endif /* HSI48_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) +/*!< Value of the Internal Low Speed oscillator in Hz +The real value may vary depending on the variations in voltage and temperature.*/ +#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ +/** + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined (LSE_VALUE) +#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S and SAI peripherals + * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined (EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ + +#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 0U +#define INSTRUCTION_CACHE_ENABLE 1U +#define DATA_CACHE_ENABLE 1U + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1U */ + +/* ################## SPI peripheral configuration ########################## */ + +/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver + * Activated: CRC code is present inside driver + * Deactivated: CRC code cleaned from driver + */ + +#define USE_SPI_CRC 0U + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED +#include "stm32g4xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED +#include "stm32g4xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED +#include "stm32g4xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED +#include "stm32g4xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED +#include "stm32g4xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED +#include "stm32g4xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CORDIC_MODULE_ENABLED +#include "stm32g4xx_hal_cordic.h" +#endif /* HAL_CORDIC_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED +#include "stm32g4xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_CRYP_MODULE_ENABLED +#include "stm32g4xx_hal_cryp.h" +#endif /* HAL_CRYP_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED +#include "stm32g4xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_EXTI_MODULE_ENABLED +#include "stm32g4xx_hal_exti.h" +#endif /* HAL_EXTI_MODULE_ENABLED */ + +#ifdef HAL_FDCAN_MODULE_ENABLED +#include "stm32g4xx_hal_fdcan.h" +#endif /* HAL_FDCAN_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED +#include "stm32g4xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_FMAC_MODULE_ENABLED +#include "stm32g4xx_hal_fmac.h" +#endif /* HAL_FMAC_MODULE_ENABLED */ + +#ifdef HAL_HRTIM_MODULE_ENABLED +#include "stm32g4xx_hal_hrtim.h" +#endif /* HAL_HRTIM_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED +#include "stm32g4xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED +#include "stm32g4xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED +#include "stm32g4xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED +#include "stm32g4xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_LPTIM_MODULE_ENABLED +#include "stm32g4xx_hal_lptim.h" +#endif /* HAL_LPTIM_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED +#include "stm32g4xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED +#include "stm32g4xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_OPAMP_MODULE_ENABLED +#include "stm32g4xx_hal_opamp.h" +#endif /* HAL_OPAMP_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED +#include "stm32g4xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED +#include "stm32g4xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_QSPI_MODULE_ENABLED +#include "stm32g4xx_hal_qspi.h" +#endif /* HAL_QSPI_MODULE_ENABLED */ + +#ifdef HAL_RNG_MODULE_ENABLED +#include "stm32g4xx_hal_rng.h" +#endif /* HAL_RNG_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED +#include "stm32g4xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SAI_MODULE_ENABLED +#include "stm32g4xx_hal_sai.h" +#endif /* HAL_SAI_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED +#include "stm32g4xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED +#include "stm32g4xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED +#include "stm32g4xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED +#include "stm32g4xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED +#include "stm32g4xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED +#include "stm32g4xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED +#include "stm32g4xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED +#include "stm32g4xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* STM32G4xx_HAL_CONF_H */ diff --git a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h new file mode 100644 index 000000000..e12321bcf --- /dev/null +++ b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h @@ -0,0 +1,67 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __STM32G4xx_IT_H +#define __STM32G4xx_IT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void NMI_Handler(void); +void HardFault_Handler(void); +void MemManage_Handler(void); +void BusFault_Handler(void); +void UsageFault_Handler(void); +void SVC_Handler(void); +void DebugMon_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +void TIM1_UP_TIM16_IRQHandler(void); +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32G4xx_IT_H */ diff --git a/SAMM/IMUandTOF/Core/Src/adc.c b/SAMM/IMUandTOF/Core/Src/adc.c new file mode 100644 index 000000000..15733df47 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/adc.c @@ -0,0 +1,154 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file adc.c + * @brief This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "adc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +ADC_HandleTypeDef hadc1; + +/* ADC1 init function */ +void MX_ADC1_Init(void) +{ + + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.GainCompensation = 0; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.OversamplingMode = ENABLE; + hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; + hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; + hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; + hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; + sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ + +} + +void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); + + /* ADC1 clock enable */ + __HAL_RCC_ADC12_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } +} + +void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +{ + + if(adcHandle->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ + + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC12_CLK_DISABLE(); + + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2); + + /* USER CODE BEGIN ADC1_MspDeInit 1 */ + + /* USER CODE END ADC1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/bmi323.c b/SAMM/IMUandTOF/Core/Src/bmi323.c new file mode 100644 index 000000000..125342eb4 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/bmi323.c @@ -0,0 +1,283 @@ +#include "bmi323.h" +#include +#include "stm32g4xx_hal_spi.h" +#include "stm32g474xx.h" + +//init spi port before calling this function +uint8_t bmi323_init(bmi323 *bmi323_dev, SPI_HandleTypeDef *spi_port, GPIO_TypeDef *port, uint16_t pin){ + uint8_t tx_word[4]; + uint8_t rx_word[4] = {0}; + uint8_t status = 0; + bmi323_dev->spi_port = spi_port; + bmi323_dev->port = port; + bmi323_dev->pin = pin; + tx_word[1] = (BMI323_CHIP_ID << 8); + tx_word[1] |= 0x80; + tx_word[0] = 0x69; + /* + Okay so for one of these transmits we need to follow the following operation:bmi323_dev + 1. to read the register we want to: + transmit first 8 bytes, then transmit a fake 8 bytes + after we want to read 16 bytes. This should complete a single read + */ + //first we read do the dummy read to switch to spi mode + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); + status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); + // rx_word = 0; + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); + status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); + if(rx_word[3] == 0x43){ + return HAL_OK; + } + return HAL_ERROR; +} + + + +/* +TODO: VIN DO THESE FUNCYIONS +*/ +uint16_t bmi323_read(bmi323 *bmi323_dev, uint8_t reg){ + uint16_t data; + // i2c_read_single(BMI323_I2C_ADDR, reg, &data, bmi323_dev->i2c_port); + return data; +} + +uint8_t bmi323_write(bmi323 *bmi323_dev, uint8_t reg, uint16_t data){ + // i2c_write(BMI323_I2C_ADDR, reg, data, bmi323_dev->i2c_port); + return 1; +} + +uint8_t bmi323_soft_reset(bmi323 *bmi323_dev){ + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_SOFT_RESET); + return 1; +} + + +/* +CALIBRATION PROCEDURE. VERY DANGEROUS. ONLY USE IF YOU KNOW WHAT YOU ARE DOING +THIS FUNCTION IS BLOCKING, CAN TAKE UP TO 3 SECONDS TO TIMEOUT +IF EVERYTHING IS DONE PROPERLY, THE FUNCTION WILL BLOCK FOR UP TO 500ms +THE FUNCTION WILL RETURN 1 IF CALIBRATION WAS SUCCESSFUL, 0 IF NOT +STEPS: +1. Disable all sensors +2. Enable the feature engine +3. Check if a calibration is already in progress +4. Check if the accelerometer is in high performance mode and the ODR is between 25 and 200 hertz +5. Start the calibration +6. Check if the calibration was successful +7. If not, reset the accelerometer and try again +8. If successful, reset the accelerometer and gyro to their original configuration +9. Done +*/ +uint8_t bmi323_calib(bmi323 *bmi323_dev){ + //first we have to fucking start the feature engine + //to do that we have to disable all sensors + uint16_t acc_conf = bmi323_read(bmi323_dev, BMI323_ACC_CONF); + uint16_t gyro_conf = bmi323_read(bmi323_dev, BMI323_GYR_CONF); + uint32_t timeout_ref = 0; + if((acc_conf & 0x7000) != 0b000){ + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + } + if((gyro_conf & 0x7000) != 0b000){ + bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); + } + //now we need to check if the feature engine has been enabled prior + if((bmi323_read(bmi323_dev, BMI323_FEATURE_CTRL) & 0x0001) == 0b0){ + //then we write 0x012C to Feature Io 2 + bmi323_write(bmi323_dev, BMI323_FEATURE_IO2, BMI323_FEATURE_IO2_EN); + //then we write 0x0001 to Feature Io status + bmi323_write(bmi323_dev, BMI323_FEATURE_IO_STATUS, BMI323_FEATURE_IO_STS); + //now we set the feature ctrol engine enable to 1 + bmi323_write(bmi323_dev, BMI323_FEATURE_CTRL, BMI323_FEATURE_CTRL_ENGINE_EN); + //now we poll the feature engine + timeout_ref = millis(); + while((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x000F) != 0b001){ + if(millis() - timeout_ref > BMI323_TIMEOUT){ + D_println("Feature engine enable timeout"); + return 0; + } + continue; + } + D_println("enabled feature engine Hooray!"); + } + //no need for an else statement as the feature engine is already enabled and we can continue + //now we need to check if a calibration is already in progress + //polls the bmi323 FEATURE I01 state untill the state is 0b00, this means calibration can start. + timeout_ref = millis(); + while(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & (0x1800)) >> 11) != 0b00){ + if(millis() - timeout_ref > BMI323_TIMEOUT){ + D_println("Calibration state timeout"); + return 0; + } + continue; + } + //check the current configuration of the accelerometer and make sure it is in high performance adn + //ODR is between 25 and 200 hertz + calibrate: + //read the acc conf register + uint16_t data = bmi323_read(bmi323_dev, BMI323_ACC_CONF) & 0x700F; + //checks if we are in the correct configuration. if not skips if statement + if(((data >> 12) == HIGH_PERF) && (((data & 0x000F) >= ODR_25) && ((data & 0x000F) <= ODR_200))){ + D_println("Starting calibration"); + //if it is, then we can start the calibration + //first we check if the alernalte configuration acc mode is set to 0: + if(((bmi323_read(bmi323_dev, BMI323_ALT_ACC_CONF) & 0x7000)>>11) != 0b000){ + //if it is, we set it to 0 + bmi323_write(bmi323_dev, BMI323_ALT_ACC_CONF, 0x0206); + } + //next we check if the alternate configuration gyro mode is set to 0: + if(((bmi323_read(bmi323_dev, BMI323_ALT_GYR_CONF) & 0x7000)>>11) != 0b000){ + //if it is, we set it to 0 + bmi323_write(bmi323_dev, BMI323_ALT_GYR_CONF, 0x0206); + } + D_println("Alternate configurations set"); + //next we can actually send the command for calibration + //reset all of the gyro calibration values + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_X, BMI323_ACC_DP_DGAIN_X_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Y, BMI323_ACC_DP_DGAIN_Y_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Z, BMI323_ACC_DP_DGAIN_Z_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_X, BMI323_ACC_DP_OFF_X_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Y, BMI323_ACC_DP_OFF_Y_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Z, BMI323_ACC_DP_OFF_Z_RESET_VAL); + //now we poll the state of the calibration untill we get 0b1 + D_println("Starting calibration"); + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB); + + D_println("Polling calibration state"); + //check if the feature engine is enabled + timeout_ref = millis(); + while(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0010) >> 4 ) != 0b1){ + if(millis() - timeout_ref > BMI323_TIMEOUT){ + D_println("Feature engine enable timeout"); + return 0; + } + continue; + } + D_println("Calibration complete"); + if(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0020) >> 5) == 0b1){ + // D_println("Calibration successful"); + // D_println("reseting values to original configuration"); + //cycle the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_ACC_CONF, acc_conf); + //cycle the gyro + bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); + // D_println(gyro_conf); + bmi323_write(bmi323_dev, BMI323_GYR_CONF, gyro_conf); + //display the calibration values + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_X), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Y), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Z), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_X), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Y), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Z), HEX); + return 1; + } + else{ + // D_println("Calibration failed"); + return 0; + } + + } + // D_println("Calibration failed"); + // D_println("reseting values trying again"); + //turns off the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + //turn on the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL | 0x7000); + //jumps to the calibration sequence + goto calibrate; + return 1; +} + +uint8_t bmi323_calib_abort(bmi323 *bmi323_dev){ + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB_ABORT); + return 1; +} + +/* +TODO: +Check the status of the acc, gyro and temp before returning the values + if they are not ready return 0 + if they are ready return 1 + need to pass by reference the values to be returned + +*/ + +uint16_t bmi323_read_acc_x(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_ACC_X); +} + +uint16_t bmi323_read_acc_y(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_ACC_Y); +} + +uint16_t bmi323_read_acc_z(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_ACC_Z); +} + +uint16_t bmi323_read_gyr_x(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_GYR_X); +} + +uint16_t bmi323_read_gyr_y(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_GYR_Y); +} + +uint16_t bmi323_read_gyr_z(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_GYR_Z); +} + +uint16_t bmi323_read_temp_data(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_TEMP_DATA); +} + +uint16_t bmi323_read_status(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_STATUS); +} + +uint16_t bmi323_read_err_reg(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_ERR_REG); +} + +uint16_t bmi323_read_chip_id(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_CHIP_ID); +} + +uint16_t bmi323_read_acc_conf(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_ACC_CONF); +} + +uint16_t bmi323_read_gyr_conf(bmi323 *bmi323_dev){ + return bmi323_read(bmi323_dev, BMI323_GYR_CONF); +} + +uint8_t bmi323_enable_acc(bmi323 *bmi323_dev, uint8_t acc_mode, uint8_t acc_avg_num, uint8_t acc_bw, uint8_t acc_range, uint8_t acc_odr){ + //uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); + uint16_t new_conf = 0; + new_conf |= acc_mode << 12; + new_conf |= acc_avg_num << 8; + new_conf |= acc_bw << 7; + new_conf |= acc_range << 4; + new_conf |= acc_odr; + bmi323_write(bmi323_dev, BMI323_ACC_CONF, new_conf); + return 1; +} + +uint8_t bmi323_enable_gyro(bmi323 *bmi323_dev, uint8_t gyr_mode, uint8_t gyr_avg_num, uint8_t gyr_bw, uint8_t gyr_range, uint8_t gyr_odr){ + //uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); + uint16_t new_conf = 0; + new_conf |= gyr_mode << 12; + new_conf |= gyr_avg_num << 8; + new_conf |= gyr_bw << 7; + new_conf |= gyr_range << 4; + new_conf |= gyr_odr; + bmi323_write(bmi323_dev, BMI323_GYR_CONF, new_conf); + return 1; +} + + + diff --git a/SAMM/IMUandTOF/Core/Src/crc.c b/SAMM/IMUandTOF/Core/Src/crc.c new file mode 100644 index 000000000..0a8907607 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/crc.c @@ -0,0 +1,90 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file crc.c + * @brief This file provides code for the configuration + * of the CRC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "crc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +CRC_HandleTypeDef hcrc; + +/* CRC init function */ +void MX_CRC_Init(void) +{ + + /* USER CODE BEGIN CRC_Init 0 */ + + /* USER CODE END CRC_Init 0 */ + + /* USER CODE BEGIN CRC_Init 1 */ + + /* USER CODE END CRC_Init 1 */ + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; + if (HAL_CRC_Init(&hcrc) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN CRC_Init 2 */ + + /* USER CODE END CRC_Init 2 */ + +} + +void HAL_CRC_MspInit(CRC_HandleTypeDef* crcHandle) +{ + + if(crcHandle->Instance==CRC) + { + /* USER CODE BEGIN CRC_MspInit 0 */ + + /* USER CODE END CRC_MspInit 0 */ + /* CRC clock enable */ + __HAL_RCC_CRC_CLK_ENABLE(); + /* USER CODE BEGIN CRC_MspInit 1 */ + + /* USER CODE END CRC_MspInit 1 */ + } +} + +void HAL_CRC_MspDeInit(CRC_HandleTypeDef* crcHandle) +{ + + if(crcHandle->Instance==CRC) + { + /* USER CODE BEGIN CRC_MspDeInit 0 */ + + /* USER CODE END CRC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_CRC_CLK_DISABLE(); + /* USER CODE BEGIN CRC_MspDeInit 1 */ + + /* USER CODE END CRC_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/dma.c b/SAMM/IMUandTOF/Core/Src/dma.c new file mode 100644 index 000000000..372b7e92c --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/dma.c @@ -0,0 +1,59 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file dma.c + * @brief This file provides code for the configuration + * of all the requested memory to memory DMA transfers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "dma.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure DMA */ +/*----------------------------------------------------------------------------*/ + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** + * Enable DMA controller clock + */ +void MX_DMA_Init(void) +{ + + /* DMA controller clock enable */ + __HAL_RCC_DMAMUX1_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Channel1_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + /* DMA1_Channel2_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ + diff --git a/SAMM/IMUandTOF/Core/Src/fdcan.c b/SAMM/IMUandTOF/Core/Src/fdcan.c new file mode 100644 index 000000000..59cf13f59 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/fdcan.c @@ -0,0 +1,223 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file fdcan.c + * @brief This file provides code for the configuration + * of the FDCAN instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "fdcan.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +FDCAN_HandleTypeDef hfdcan1; +FDCAN_HandleTypeDef hfdcan2; + +/* FDCAN1 init function */ +void MX_FDCAN1_Init(void) +{ + + /* USER CODE BEGIN FDCAN1_Init 0 */ + + /* USER CODE END FDCAN1_Init 0 */ + + /* USER CODE BEGIN FDCAN1_Init 1 */ + + /* USER CODE END FDCAN1_Init 1 */ + hfdcan1.Instance = FDCAN1; + hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan1.Init.AutoRetransmission = DISABLE; + hfdcan1.Init.TransmitPause = DISABLE; + hfdcan1.Init.ProtocolException = DISABLE; + hfdcan1.Init.NominalPrescaler = 16; + hfdcan1.Init.NominalSyncJumpWidth = 1; + hfdcan1.Init.NominalTimeSeg1 = 1; + hfdcan1.Init.NominalTimeSeg2 = 1; + hfdcan1.Init.DataPrescaler = 1; + hfdcan1.Init.DataSyncJumpWidth = 1; + hfdcan1.Init.DataTimeSeg1 = 1; + hfdcan1.Init.DataTimeSeg2 = 1; + hfdcan1.Init.StdFiltersNbr = 0; + hfdcan1.Init.ExtFiltersNbr = 0; + hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN1_Init 2 */ + + /* USER CODE END FDCAN1_Init 2 */ + +} +/* FDCAN2 init function */ +void MX_FDCAN2_Init(void) +{ + + /* USER CODE BEGIN FDCAN2_Init 0 */ + + /* USER CODE END FDCAN2_Init 0 */ + + /* USER CODE BEGIN FDCAN2_Init 1 */ + + /* USER CODE END FDCAN2_Init 1 */ + hfdcan2.Instance = FDCAN2; + hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan2.Init.AutoRetransmission = DISABLE; + hfdcan2.Init.TransmitPause = DISABLE; + hfdcan2.Init.ProtocolException = DISABLE; + hfdcan2.Init.NominalPrescaler = 16; + hfdcan2.Init.NominalSyncJumpWidth = 1; + hfdcan2.Init.NominalTimeSeg1 = 1; + hfdcan2.Init.NominalTimeSeg2 = 1; + hfdcan2.Init.DataPrescaler = 1; + hfdcan2.Init.DataSyncJumpWidth = 1; + hfdcan2.Init.DataTimeSeg1 = 1; + hfdcan2.Init.DataTimeSeg2 = 1; + hfdcan2.Init.StdFiltersNbr = 0; + hfdcan2.Init.ExtFiltersNbr = 0; + hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN2_Init 2 */ + + /* USER CODE END FDCAN2_Init 2 */ + +} + +static uint32_t HAL_RCC_FDCAN_CLK_ENABLED=0; + +void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(fdcanHandle->Instance==FDCAN1) + { + /* USER CODE BEGIN FDCAN1_MspInit 0 */ + + /* USER CODE END FDCAN1_MspInit 0 */ + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN1 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if(HAL_RCC_FDCAN_CLK_ENABLED==1){ + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN1_MspInit 1 */ + + /* USER CODE END FDCAN1_MspInit 1 */ + } + else if(fdcanHandle->Instance==FDCAN2) + { + /* USER CODE BEGIN FDCAN2_MspInit 0 */ + + /* USER CODE END FDCAN2_MspInit 0 */ + + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN2 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if(HAL_RCC_FDCAN_CLK_ENABLED==1){ + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN2_MspInit 1 */ + + /* USER CODE END FDCAN2_MspInit 1 */ + } +} + +void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle) +{ + + if(fdcanHandle->Instance==FDCAN1) + { + /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ + + /* USER CODE END FDCAN1_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if(HAL_RCC_FDCAN_CLK_ENABLED==0){ + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + + /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ + + /* USER CODE END FDCAN1_MspDeInit 1 */ + } + else if(fdcanHandle->Instance==FDCAN2) + { + /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ + + /* USER CODE END FDCAN2_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if(HAL_RCC_FDCAN_CLK_ENABLED==0){ + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_5); + + /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ + + /* USER CODE END FDCAN2_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/gpio.c b/SAMM/IMUandTOF/Core/Src/gpio.c new file mode 100644 index 000000000..ab7f2d3bf --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/gpio.c @@ -0,0 +1,94 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "gpio.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure GPIO */ +/*----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + + +/* USER CODE END 1 */ + +/** Configure pins as + * Analog + * Input + * Output + * EVENT_OUT + * EXTI + PC8 ------> I2C3_SCL + PC9 ------> I2C3_SDA +*/ +void MX_GPIO_Init(void) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1|GPIO_PIN_4, GPIO_PIN_RESET); + + /*Configure GPIO pin : PF1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + + /*Configure GPIO pins : PB1 PB4 */ + GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : PB12 PB6 */ + GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : PC8 PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + +} + +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ diff --git a/SAMM/IMUandTOF/Core/Src/i2c.c b/SAMM/IMUandTOF/Core/Src/i2c.c new file mode 100644 index 000000000..8912dcd02 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/i2c.c @@ -0,0 +1,135 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.c + * @brief This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "i2c.h" + +/* USER CODE BEGIN 0 */ +extern uint32_t I2C_Freq; +/* USER CODE END 0 */ + +I2C_HandleTypeDef hi2c1; + +/* I2C1 init function */ +void MX_I2C1_Init(void) +{ + + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.Timing = 0x10920C1D; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) + { + Error_Handler(); + } + + /** I2C Fast mode Plus enable + */ + HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ + +} + +void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspInit 0 */ + + /* USER CODE END I2C1_MspInit 0 */ + LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C1 clock enable */ + __HAL_RCC_I2C1_CLK_ENABLE(); + /* USER CODE BEGIN I2C1_MspInit 1 */ + + /* USER CODE END I2C1_MspInit 1 */ + } +} + +void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +{ + + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspDeInit 0 */ + + /* USER CODE END I2C1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C1_CLK_DISABLE(); + + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); + + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); + + /* USER CODE BEGIN I2C1_MspDeInit 1 */ + + /* USER CODE END I2C1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/main.c b/SAMM/IMUandTOF/Core/Src/main.c new file mode 100644 index 000000000..7400c2e1f --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/main.c @@ -0,0 +1,325 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "crc.h" +#include "fdcan.h" +#include "i2c.h" +#include "spi.h" +#include "gpio.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +#include +#include "VL53L4ED_api.h" +#include "bmi323.h" +//#include "circularBuffer.h" +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN PTD */ +#define BMI323_CS_GPIO_Port GPIOA +#define BMI323_CS_Pin GPIO_PIN_4 +/* USER CODE END PTD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#endif +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ +PUTCHAR_PROTOTYPE +{ + ITM_SendChar(ch); + return ch; +} +//CircularBuffer *cb; +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ + +/* USER CODE BEGIN PV */ +// FDCAN_RxHeaderTypeDef RxHeader_FDCAN2; +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ +/* +I2C2 - is meant for thermal sensor MLX90640 and is ran using DMA to offload CPU for calculations + - still need to fully test but ideally implmentation is done + + + */ +/* USER CODE END 0 */ + +/** + * @brief The application entry point. + * @retval int + */ +int main(void) +{ + + /* USER CODE BEGIN 1 */ + //cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_CRC_Init(); + MX_FDCAN1_Init(); + MX_FDCAN2_Init(); + MX_I2C1_Init(); + MX_SPI1_Init(); // TODO: change all instances of spi1 -> SPI1 + /* USER CODE BEGIN 2 */ + + // HAL_FDCAN_Start(&hfdcan1); + // HAL_FDCAN_Start(&hfdcan2); + // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + bmi323 bmi323_dev; + HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); + bmi323_init(&bmi323_dev, &hspi1, BMI323_CS_GPIO_Port, BMI323_CS_Pin); + // Send 2 dummy bytes to switch BMI323 to SPI mode + // uint16_t dummy_byte = 0x8000; + // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_RESET); + // HAL_SPI_Transmit(&hspi1,(uint8_t*)&dummy_byte, 1, HAL_MAX_DELAY); + // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); + //HAL_Delay(1); // Short delay after mode switch + + // Initialize BMI323 sensor + + // if (BMI323_Init() != HAL_OK) { + // printf("BMI323 initialization failed!\r\n"); + // Error_Handler(); + // } + + // static uint16_t eeMLX90640[832]; + // static paramsMLX90640 mlx90640; + // #define MLX90640_ADDRESS 0x33<<1 + // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); + + // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + + // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); + + // MLX90640_SynchFrame(MLX90640_ADDRESS); + // MLX90640_SetRefreshRate(0x33, 0x05); + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + //begin VL53L4ED + HAL_Delay(100); // wait for 5ms to power up the device + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin + //HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to reset the device + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin + //HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to power up the device + + uint16_t status = 0; + + uint16_t sensor_id = 0; + VL53L4ED_ResultsData_t results; + uint8_t p_data_ready; + + int TOF_ID = 0x52; + HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); + status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); + printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); + status = VL53L4ED_StartRanging(TOF_ID); + status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); + status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing + + + while (1) + { + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + //begin VL53L4ED + status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); + if(p_data_ready){ + /* (Mandatory) Clear HW interrupt to restart measurements */ + VL53L4ED_ClearInterrupt(TOF_ID); + /* Read measured distance. RangeStatus = 0 means valid data */ + VL53L4ED_GetResult(TOF_ID, &results); + printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", + results.range_status, + results.distance_mm- 67, + results.signal_per_spad_kcps); + }else{ + HAL_Delay(10); + __disable_irq(); + __enable_irq(); + } + + //begin BMI323 + // int16_t ax, ay, az; + // if (BMI323_ReadAccel(&ax, &ay, &az) == HAL_OK) { + // printf("Accel: X=%d, Y=%d, Z=%d\r\n", ax, ay, az); + // } + HAL_Delay(100); // Read every 100ms + } + /* USER CODE END 3 */ +} + +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) +{ + LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); + while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) + { + } + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + LL_RCC_HSE_EnableBypass(); + LL_RCC_HSE_Enable(); + /* Wait till HSE is ready */ + while(LL_RCC_HSE_IsReady() != 1) + { + } + + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 32, LL_RCC_PLLR_DIV_2); + LL_RCC_PLL_EnableDomain_SYS(); + LL_RCC_PLL_Enable(); + /* Wait till PLL is ready */ + while(LL_RCC_PLL_IsReady() != 1) + { + } + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) + { + } + + /* Insure 1us transition state at intermediate medium speed clock*/ + for (__IO uint32_t i = (170 >> 1); i !=0; i--); + + /* Set AHB prescaler*/ + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); + LL_SetSystemCoreClock(128000000); + + /* Update the time base */ + if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK) + { + Error_Handler(); + } +} + +/* USER CODE BEGIN 4 */ +// void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) +// { +// uint8_t RxData[64]; +// HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &RxHeader_FDCAN2, RxData); +// printf("got messgae\n"); +// //circularBufferPush(cb, RxData, sizeof(RxData)); + + +// } +/* USER CODE END 4 */ + +/** + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM1 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + /* USER CODE BEGIN Callback 0 */ + + /* USER CODE END Callback 0 */ + if (htim->Instance == TIM1) + { + HAL_IncTick(); + } + /* USER CODE BEGIN Callback 1 */ + + /* USER CODE END Callback 1 */ +} + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } + /* USER CODE END Error_Handler_Debug */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +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 CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ diff --git a/SAMM/IMUandTOF/Core/Src/spi.c b/SAMM/IMUandTOF/Core/Src/spi.c new file mode 100644 index 000000000..2944b6ce0 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/spi.c @@ -0,0 +1,128 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file spi.c + * @brief This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ +/* Includes ------------------------------------------------------------------*/ +#include "spi.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +SPI_HandleTypeDef hspi1; + +/* spi1 init function */ +void MX_SPI1_Init(void) +{ + + /* USER CODE BEGIN spi1_Init 0 */ + + /* USER CODE END spi1_Init 0 */ + + /* USER CODE BEGIN spi1_Init 1 */ + + /* USER CODE END spi1_Init 1 */ + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_2LINES; + hspi1.Init.DataSize = SPI_DATASIZE_16BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 7; + hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; + if (HAL_SPI_Init(&hspi1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN spi1_Init 2 */ + + /* USER CODE END spi1_Init 2 */ + +} + +void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(spiHandle->Instance==spi1) + { + /* USER CODE BEGIN spi1_MspInit 0 */ + + /* USER CODE END spi1_MspInit 0 */ + /* spi1 clock enable */ + __HAL_RCC_spi1_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**spi1 GPIO Configuration + PA5 ------> spi1_SCK + PA6 ------> spi1_MISO + PA7 ------> spi1_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF5_spi1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF5_spi1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN spi1_MspInit 1 */ + + /* USER CODE END spi1_MspInit 1 */ + } +} + +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) +{ + + if(spiHandle->Instance==spi1) + { + /* USER CODE BEGIN spi1_MspDeInit 0 */ + + /* USER CODE END spi1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_spi1_CLK_DISABLE(); + + /**spi1 GPIO Configuration + PC10 ------> spi1_SCK + PC11 ------> spi1_MISO + PC12 ------> spi1_MOSI + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); + + /* USER CODE BEGIN spi1_MspDeInit 1 */ + + /* USER CODE END spi1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c new file mode 100644 index 000000000..3fc223b63 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c @@ -0,0 +1,86 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN Define */ + +/* USER CODE END Define */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN Macro */ + +/* USER CODE END Macro */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* External functions --------------------------------------------------------*/ +/* USER CODE BEGIN ExternalFunctions */ + +/* USER CODE END ExternalFunctions */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + + /* System interrupt init*/ + + /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral + */ + HAL_PWREx_DisableUCPDDeadBattery(); + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c new file mode 100644 index 000000000..f43496b92 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c @@ -0,0 +1,126 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_hal_timebase_tim.c + * @brief HAL time base based on the hardware TIM. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32g4xx_hal.h" +#include "stm32g4xx_hal_tim.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +TIM_HandleTypeDef htim1; +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief This function configures the TIM1 as a time base source. + * The time source is configured to have 1ms time base with a dedicated + * Tick interrupt priority. + * @note This function is called automatically at the beginning of program after + * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). + * @param TickPriority: Tick interrupt priority. + * @retval HAL status + */ +HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) +{ + RCC_ClkInitTypeDef clkconfig; + uint32_t uwTimclock = 0; + uint32_t uwPrescalerValue = 0; + uint32_t pFLatency; + + HAL_StatusTypeDef status; + + /* Enable TIM1 clock */ + __HAL_RCC_TIM1_CLK_ENABLE(); + + /* Get clock configuration */ + HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); + + /* Compute TIM1 clock */ + uwTimclock = HAL_RCC_GetPCLK2Freq(); + + /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ + uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); + + /* Initialize TIM1 */ + htim1.Instance = TIM1; + + /* Initialize TIMx peripheral as follow: + * Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. + * Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + * ClockDivision = 0 + * Counter direction = Up + */ + htim1.Init.Period = (1000000U / 1000U) - 1U; + htim1.Init.Prescaler = uwPrescalerValue; + htim1.Init.ClockDivision = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + + status = HAL_TIM_Base_Init(&htim1); + if (status == HAL_OK) + { + /* Start the TIM time Base generation in interrupt mode */ + status = HAL_TIM_Base_Start_IT(&htim1); + if (status == HAL_OK) + { + /* Enable the TIM1 global Interrupt */ + HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + /* Configure the SysTick IRQ priority */ + if (TickPriority < (1UL << __NVIC_PRIO_BITS)) + { + /* Configure the TIM IRQ priority */ + HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); + uwTickPrio = TickPriority; + } + else + { + status = HAL_ERROR; + } + } + } + + /* Return function status */ + return status; +} + +/** + * @brief Suspend Tick increment. + * @note Disable the tick increment by disabling TIM1 update interrupt. + * @param None + * @retval None + */ +void HAL_SuspendTick(void) +{ + /* Disable TIM1 update Interrupt */ + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); +} + +/** + * @brief Resume Tick increment. + * @note Enable the tick increment by Enabling TIM1 update interrupt. + * @param None + * @retval None + */ +void HAL_ResumeTick(void) +{ + /* Enable TIM1 Update interrupt */ + __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); +} + diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c new file mode 100644 index 000000000..ef94b5f52 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c @@ -0,0 +1,218 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32g4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32g4xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +extern TIM_HandleTypeDef htim1; + +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M4 Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) + { + } + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Memory management fault. + */ +void MemManage_Handler(void) +{ + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ + /* USER CODE END W1_MemoryManagement_IRQn 0 */ + } +} + +/** + * @brief This function handles Prefetch fault, memory access fault. + */ +void BusFault_Handler(void) +{ + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_BusFault_IRQn 0 */ + /* USER CODE END W1_BusFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Undefined instruction or illegal state. + */ +void UsageFault_Handler(void) +{ + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ + /* USER CODE END W1_UsageFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVCall_IRQn 0 */ + + /* USER CODE END SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 1 */ + + /* USER CODE END SVCall_IRQn 1 */ +} + +/** + * @brief This function handles Debug monitor. + */ +void DebugMon_Handler(void) +{ + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + + /* USER CODE END DebugMonitor_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32G4xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32g4xx.s). */ +/******************************************************************************/ + +/** + * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. + */ +void TIM1_UP_TIM16_IRQHandler(void) +{ + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ + + /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ + HAL_TIM_IRQHandler(&htim1); + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ + + /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/syscalls.c b/SAMM/IMUandTOF/Core/Src/syscalls.c new file mode 100644 index 000000000..e33a8492c --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/syscalls.c @@ -0,0 +1,176 @@ +/** + ****************************************************************************** + * @file syscalls.c + * @author Auto-generated by STM32CubeMX + * @brief Minimal System calls file + * + * For more information about which c-functions + * need which of these lowlevel functions + * please consult the Newlib libc-manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2020-2024 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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include +#include +#include +#include +#include +#include +#include + + +/* Variables */ +extern int __io_putchar(int ch) __attribute__((weak)); +extern int __io_getchar(void) __attribute__((weak)); + + +char *__env[1] = { 0 }; +char **environ = __env; + + +/* Functions */ +void initialise_monitor_handles() +{ +} + +int _getpid(void) +{ + return 1; +} + +int _kill(int pid, int sig) +{ + (void)pid; + (void)sig; + errno = EINVAL; + return -1; +} + +void _exit (int status) +{ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ +} + +__attribute__((weak)) int _read(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + *ptr++ = __io_getchar(); + } + + return len; +} + +__attribute__((weak)) int _write(int file, char *ptr, int len) +{ + (void)file; + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) + { + __io_putchar(*ptr++); + } + return len; +} + +int _close(int file) +{ + (void)file; + return -1; +} + + +int _fstat(int file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _isatty(int file) +{ + (void)file; + return 1; +} + +int _lseek(int file, int ptr, int dir) +{ + (void)file; + (void)ptr; + (void)dir; + return 0; +} + +int _open(char *path, int flags, ...) +{ + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; +} + +int _wait(int *status) +{ + (void)status; + errno = ECHILD; + return -1; +} + +int _unlink(char *name) +{ + (void)name; + errno = ENOENT; + return -1; +} + +int _times(struct tms *buf) +{ + (void)buf; + return -1; +} + +int _stat(char *file, struct stat *st) +{ + (void)file; + st->st_mode = S_IFCHR; + return 0; +} + +int _link(char *old, char *new) +{ + (void)old; + (void)new; + errno = EMLINK; + return -1; +} + +int _fork(void) +{ + errno = EAGAIN; + return -1; +} + +int _execve(char *name, char **argv, char **env) +{ + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; +} diff --git a/SAMM/IMUandTOF/Core/Src/sysmem.c b/SAMM/IMUandTOF/Core/Src/sysmem.c new file mode 100644 index 000000000..246470ee8 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/sysmem.c @@ -0,0 +1,79 @@ +/** + ****************************************************************************** + * @file sysmem.c + * @author Generated by STM32CubeMX + * @brief System Memory calls file + * + * For more information about which C functions + * need which of these lowlevel functions + * please consult the newlib libc manual + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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. + * + ****************************************************************************** + */ + +/* Includes */ +#include +#include + +/** + * Pointer to the current high watermark of the heap usage + */ +static uint8_t *__sbrk_heap_end = NULL; + +/** + * @brief _sbrk() allocates memory to the newlib heap and is used by malloc + * and others from the C library + * + * @verbatim + * ############################################################################ + * # .data # .bss # newlib heap # MSP stack # + * # # # # Reserved by _Min_Stack_Size # + * ############################################################################ + * ^-- RAM start ^-- _end _estack, RAM end --^ + * @endverbatim + * + * This implementation starts allocating at the '_end' linker symbol + * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack + * The implementation considers '_estack' linker symbol to be RAM end + * NOTE: If the MSP stack, at any point during execution, grows larger than the + * reserved size, please increase the '_Min_Stack_Size'. + * + * @param incr Memory size + * @return Pointer to allocated memory + */ +void *_sbrk(ptrdiff_t incr) +{ + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; + + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) + { + __sbrk_heap_end = &_end; + } + + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) + { + errno = ENOMEM; + return (void *)-1; + } + + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; + + return (void *)prev_heap_end; +} diff --git a/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c b/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c new file mode 100644 index 000000000..8d35a0aa0 --- /dev/null +++ b/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c @@ -0,0 +1,285 @@ +/** + ****************************************************************************** + * @file system_stm32g4xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32g4xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * After each device reset the HSI (16 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to + * configure the system clock before to branch to main program. + * + * This file configures the system clock as follows: + *============================================================================= + *----------------------------------------------------------------------------- + * System Clock source | HSI + *----------------------------------------------------------------------------- + * SYSCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * HCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * AHB Prescaler | 1 + *----------------------------------------------------------------------------- + * APB1 Prescaler | 1 + *----------------------------------------------------------------------------- + * APB2 Prescaler | 1 + *----------------------------------------------------------------------------- + * PLL_M | 1 + *----------------------------------------------------------------------------- + * PLL_N | 16 + *----------------------------------------------------------------------------- + * PLL_P | 7 + *----------------------------------------------------------------------------- + * PLL_Q | 2 + *----------------------------------------------------------------------------- + * PLL_R | 2 + *----------------------------------------------------------------------------- + * Require 48MHz for RNG | Disabled + *----------------------------------------------------------------------------- + *============================================================================= + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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. + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32g4xx_system + * @{ + */ + +/** @addtogroup STM32G4xx_System_Private_Includes + * @{ + */ + +#include "stm32g4xx.h" + +#if !defined (HSE_VALUE) + #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Defines + * @{ + */ + +/************************* Miscellaneous Configuration ************************/ +/* Note: Following vector table addresses must be defined in line with linker + configuration. */ +/*!< Uncomment the following line if you need to relocate the vector table + anywhere in Flash or Sram, else the vector table is kept at the automatic + remap of boot address selected */ +#define USER_VECT_TAB_ADDRESS + +#if defined(USER_VECT_TAB_ADDRESS) +/*!< Uncomment the following line if you need to relocate your vector Table + in Sram else user remap will be done in Flash. */ +/* #define VECT_TAB_SRAM */ +#if defined(VECT_TAB_SRAM) +#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#else +#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +/******************************************************************************/ +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Variables + * @{ + */ + /* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ + uint32_t SystemCoreClock = HSI_VALUE; + + const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; + const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32G4xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ + +void SystemInit(void) +{ + /* FPU settings ------------------------------------------------------------*/ + #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ + #endif + + /* Configure the Vector Table location add offset address ------------------*/ +#if defined(USER_VECT_TAB_ADDRESS) + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 24 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ +void SystemCoreClockUpdate(void) +{ + uint32_t tmp, pllvco, pllr, pllsource, pllm; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) + { + case 0x04: /* HSI used as system clock source */ + SystemCoreClock = HSI_VALUE; + break; + + case 0x08: /* HSE used as system clock source */ + SystemCoreClock = HSE_VALUE; + break; + + case 0x0C: /* PLL used as system clock source */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; + if (pllsource == 0x02UL) /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } + else /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; + SystemCoreClock = pllvco/pllr; + break; + + default: + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + diff --git a/SAMM/IMUandTOF/Core/empty.txt b/SAMM/IMUandTOF/Core/empty.txt deleted file mode 100644 index e69de29bb..000000000 From ce994e5103e7f4b9dfe082bf20bfdb4d3c6afc89 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 3 Apr 2026 04:39:51 +0000 Subject: [PATCH 7/7] Automatic Clang-Format: Standardized formatting automatically --- .../Core/Extras/CircBuF/circularBuffer.c | 108 +-- .../Core/Extras/CircBuF/circularBuffer.h | 14 +- .../Core/Extras/VL53L4ED/VL53L4ED_api.c | 651 +++++++----------- .../Core/Extras/VL53L4ED/VL53L4ED_api.h | 164 ++--- .../Extras/VL53L4ED/VL53L4ED_calibration.c | 139 ++-- .../Extras/VL53L4ED/VL53L4ED_calibration.h | 37 +- .../IMUandTOF/Core/Extras/VL53L4ED/platform.c | 29 +- .../IMUandTOF/Core/Extras/VL53L4ED/platform.h | 29 +- SAMM/IMUandTOF/Core/Extras/extra.c | 57 +- SAMM/IMUandTOF/Core/Extras/extra.h | 9 +- SAMM/IMUandTOF/Core/Inc/adc.h | 33 +- SAMM/IMUandTOF/Core/Inc/bmi323.h | 45 +- SAMM/IMUandTOF/Core/Inc/crc.h | 33 +- SAMM/IMUandTOF/Core/Inc/dma.h | 33 +- SAMM/IMUandTOF/Core/Inc/fdcan.h | 33 +- SAMM/IMUandTOF/Core/Inc/gpio.h | 33 +- SAMM/IMUandTOF/Core/Inc/i2c.h | 33 +- SAMM/IMUandTOF/Core/Inc/main.h | 45 +- SAMM/IMUandTOF/Core/Inc/spi.h | 32 +- SAMM/IMUandTOF/Core/Inc/stm32_assert.h | 49 +- SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h | 243 +++---- SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h | 30 +- SAMM/IMUandTOF/Core/Src/adc.c | 226 +++--- SAMM/IMUandTOF/Core/Src/bmi323.c | 436 ++++++------ SAMM/IMUandTOF/Core/Src/crc.c | 102 ++- SAMM/IMUandTOF/Core/Src/dma.c | 60 +- SAMM/IMUandTOF/Core/Src/fdcan.c | 364 +++++----- SAMM/IMUandTOF/Core/Src/gpio.c | 126 ++-- SAMM/IMUandTOF/Core/Src/i2c.c | 196 +++--- SAMM/IMUandTOF/Core/Src/main.c | 437 ++++++------ SAMM/IMUandTOF/Core/Src/spi.c | 186 +++-- SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c | 56 +- .../Core/Src/stm32g4xx_hal_timebase_tim.c | 170 +++-- SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c | 189 +++-- SAMM/IMUandTOF/Core/Src/syscalls.c | 134 ++-- SAMM/IMUandTOF/Core/Src/sysmem.c | 38 +- SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c | 437 ++++++------ 37 files changed, 2389 insertions(+), 2647 deletions(-) diff --git a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c index e7f59de79..4b0eaeaca 100644 --- a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c +++ b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.c @@ -1,64 +1,70 @@ #include "circularBuffer.h" -CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) { - CircularBuffer *cb = malloc(sizeof(CircularBuffer)); - if (!cb) { - return NULL; - } +CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size) +{ + CircularBuffer *cb = malloc(sizeof(CircularBuffer)); + if (!cb) { + return NULL; + } - cb->buffer = malloc(capacity * sizeof(void *)); - if (!cb->buffer) { - free(cb); - return NULL; - } + cb->buffer = malloc(capacity * sizeof(void *)); + if (!cb->buffer) { + free(cb); + return NULL; + } - for (size_t i = 0; i < capacity; i++) { - cb->buffer[i] = malloc(max_array_size); - if (!cb->buffer[i]) { - for (size_t j = 0; j < i; j++) { - free(cb->buffer[j]); - } - free(cb->buffer); - free(cb); - return NULL; - } - } - cb->capacity = capacity; - cb->max_arr_size = max_array_size; - cb->head = 0; - cb->tail = 0; - cb->size = 0; - return cb; + for (size_t i = 0; i < capacity; i++) { + cb->buffer[i] = malloc(max_array_size); + if (!cb->buffer[i]) { + for (size_t j = 0; j < i; j++) { + free(cb->buffer[j]); + } + free(cb->buffer); + free(cb); + return NULL; + } + } + cb->capacity = capacity; + cb->max_arr_size = max_array_size; + cb->head = 0; + cb->tail = 0; + cb->size = 0; + return cb; } -void circularBufferDestroy(CircularBuffer *cb) { - if (!cb) return; +void circularBufferDestroy(CircularBuffer *cb) +{ + if (!cb) { + return; + } - for (size_t i = 0; i < cb->capacity; i++) { - free(cb->buffer[i]); - } - free(cb->buffer); - free(cb); + for (size_t i = 0; i < cb->capacity; i++) { + free(cb->buffer[i]); + } + free(cb->buffer); + free(cb); } -int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) { - if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { - return -1; // Buffer is full or array is too large - } +int circularBufferPush(CircularBuffer *cb, void *array, size_t array_size) +{ + if (!cb || cb->size == cb->capacity || array_size > cb->max_arr_size) { + return -1; // Buffer is full or array is too large + } - // Copy the array into the buffer - memcpy(cb->buffer[cb->head], array, array_size); - cb->head = (cb->head + 1) % cb->capacity; - cb->size++; - return 0; + // Copy the array into the buffer + memcpy(cb->buffer[cb->head], array, array_size); + cb->head = (cb->head + 1) % cb->capacity; + cb->size++; + return 0; } -void *circularBufferPop(CircularBuffer *cb) { - if (!cb || cb->size == 0) { - return NULL; // Buffer is empty - } +void *circularBufferPop(CircularBuffer *cb) +{ + if (!cb || cb->size == 0) { + return NULL; // Buffer is empty + } - void *array = cb->buffer[cb->tail]; - cb->tail = (cb->tail + 1) % cb->capacity; - cb->size--; - return array; + void *array = cb->buffer[cb->tail]; + cb->tail = (cb->tail + 1) % cb->capacity; + cb->size--; + return array; } diff --git a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h index ae7ce9220..861792535 100644 --- a/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h +++ b/SAMM/IMUandTOF/Core/Extras/CircBuF/circularBuffer.h @@ -1,18 +1,18 @@ #ifndef __circularBuffer_h__ #define __circularBuffer_h__ +#include #include #include -#include #include typedef struct { - void **buffer; // Array of void pointers - uint8_t capacity; // Maximum number of elements in the buffer - uint8_t head; // Index of the next element to write - uint8_t tail; // Index of the next element to read - uint8_t size; // Current number of elements in the buffer - uint8_t max_arr_size; // Maximum size of the array + void **buffer; // Array of void pointers + uint8_t capacity; // Maximum number of elements in the buffer + uint8_t head; // Index of the next element to write + uint8_t tail; // Index of the next element to read + uint8_t size; // Current number of elements in the buffer + uint8_t max_arr_size; // Maximum size of the array } CircularBuffer; CircularBuffer *circular_buffer_init(size_t capacity, size_t max_array_size); diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c index 323148da4..7c844100e 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.c @@ -1,138 +1,138 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_api.c * @brief Functions implementation */ -#include -#include #include "VL53L4ED_api.h" +#include +#include + static const uint8_t VL53L4ED_DEFAULT_CONFIGURATION[] = { - #ifdef VL53L4ED_I2C_FAST_MODE_PLUS - 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), - else don't touch */ - #else - 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), - else don't touch */ - #endif - 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 - (pull up at AVDD) */ - 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 - (pull up at AVDD) */ - 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low - (bits 3:0 must be 0x1), use SetInterruptPolarity() */ - 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, - use CheckForDataReady() */ - 0x00, /* 0x32 : not user-modifiable */ - 0x02, /* 0x33 : not user-modifiable */ - 0x08, /* 0x34 : not user-modifiable */ - 0x00, /* 0x35 : not user-modifiable */ - 0x08, /* 0x36 : not user-modifiable */ - 0x10, /* 0x37 : not user-modifiable */ - 0x01, /* 0x38 : not user-modifiable */ - 0x01, /* 0x39 : not user-modifiable */ - 0x00, /* 0x3a : not user-modifiable */ - 0x00, /* 0x3b : not user-modifiable */ - 0x00, /* 0x3c : not user-modifiable */ - 0x00, /* 0x3d : not user-modifiable */ - 0xff, /* 0x3e : not user-modifiable */ - 0x00, /* 0x3f : not user-modifiable */ - 0x0F, /* 0x40 : not user-modifiable */ - 0x00, /* 0x41 : not user-modifiable */ - 0x00, /* 0x42 : not user-modifiable */ - 0x00, /* 0x43 : not user-modifiable */ - 0x00, /* 0x44 : not user-modifiable */ - 0x00, /* 0x45 : not user-modifiable */ - 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, - 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ - 0x0b, /* 0x47 : not user-modifiable */ - 0x00, /* 0x48 : not user-modifiable */ - 0x00, /* 0x49 : not user-modifiable */ - 0x02, /* 0x4a : not user-modifiable */ - 0x14, /* 0x4b : not user-modifiable */ - 0x21, /* 0x4c : not user-modifiable */ - 0x00, /* 0x4d : not user-modifiable */ - 0x00, /* 0x4e : not user-modifiable */ - 0x05, /* 0x4f : not user-modifiable */ - 0x00, /* 0x50 : not user-modifiable */ - 0x00, /* 0x51 : not user-modifiable */ - 0x00, /* 0x52 : not user-modifiable */ - 0x00, /* 0x53 : not user-modifiable */ - 0xc8, /* 0x54 : not user-modifiable */ - 0x00, /* 0x55 : not user-modifiable */ - 0x00, /* 0x56 : not user-modifiable */ - 0x38, /* 0x57 : not user-modifiable */ - 0xff, /* 0x58 : not user-modifiable */ - 0x01, /* 0x59 : not user-modifiable */ - 0x00, /* 0x5a : not user-modifiable */ - 0x08, /* 0x5b : not user-modifiable */ - 0x00, /* 0x5c : not user-modifiable */ - 0x00, /* 0x5d : not user-modifiable */ - 0x01, /* 0x5e : not user-modifiable */ - 0xcc, /* 0x5f : not user-modifiable */ - 0x07, /* 0x60 : not user-modifiable */ - 0x01, /* 0x61 : not user-modifiable */ - 0xf1, /* 0x62 : not user-modifiable */ - 0x05, /* 0x63 : not user-modifiable */ - 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), - use SetSigmaThreshold(), default value 90 mm */ - 0xa0, /* 0x65 : Sigma threshold LSB */ - 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), - use SetSignalThreshold() */ - 0x80, /* 0x67 : Min count Rate LSB */ - 0x08, /* 0x68 : not user-modifiable */ - 0x38, /* 0x69 : not user-modifiable */ - 0x00, /* 0x6a : not user-modifiable */ - 0x00, /* 0x6b : not user-modifiable */ - 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, - use SetIntermeasurementInMs() */ - 0x00, /* 0x6d : Intermeasurement period */ - 0x0f, /* 0x6e : Intermeasurement period */ - 0x89, /* 0x6f : Intermeasurement period LSB */ - 0x00, /* 0x70 : not user-modifiable */ - 0x00, /* 0x71 : not user-modifiable */ - 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), - use SetD:tanceThreshold() */ - 0x00, /* 0x73 : distance threshold high LSB */ - 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), - use SetD:tanceThreshold() */ - 0x00, /* 0x75 : distance threshold low LSB */ - 0x00, /* 0x76 : not user-modifiable */ - 0x01, /* 0x77 : not user-modifiable */ - 0x07, /* 0x78 : not user-modifiable */ - 0x05, /* 0x79 : not user-modifiable */ - 0x06, /* 0x7a : not user-modifiable */ - 0x06, /* 0x7b : not user-modifiable */ - 0x00, /* 0x7c : not user-modifiable */ - 0x00, /* 0x7d : not user-modifiable */ - 0x02, /* 0x7e : not user-modifiable */ - 0xc7, /* 0x7f : not user-modifiable */ - 0xff, /* 0x80 : not user-modifiable */ - 0x9B, /* 0x81 : not user-modifiable */ - 0x00, /* 0x82 : not user-modifiable */ - 0x00, /* 0x83 : not user-modifiable */ - 0x00, /* 0x84 : not user-modifiable */ - 0x01, /* 0x85 : not user-modifiable */ - 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ - 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), - If you want an automatic start after VL53L4ED_init() call, - put 0x40 in location 0x87 */ +#ifdef VL53L4ED_I2C_FAST_MODE_PLUS + 0x12, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ +#else + 0x00, /* 0x2d : set bit 2 and 5 to 1 for fast plus mode (1MHz I2C), + else don't touch */ +#endif + 0x00, /* 0x2e : bit 0 if I2C pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x00, /* 0x2f : bit 0 if GPIO pulled up at 1.8V, else set bit 0 to 1 + (pull up at AVDD) */ + 0x11, /* 0x30 : set bit 4 to 0 for active high interrupt and 1 for active low + (bits 3:0 must be 0x1), use SetInterruptPolarity() */ + 0x02, /* 0x31 : bit 1 = interrupt depending on the polarity, + use CheckForDataReady() */ + 0x00, /* 0x32 : not user-modifiable */ + 0x02, /* 0x33 : not user-modifiable */ + 0x08, /* 0x34 : not user-modifiable */ + 0x00, /* 0x35 : not user-modifiable */ + 0x08, /* 0x36 : not user-modifiable */ + 0x10, /* 0x37 : not user-modifiable */ + 0x01, /* 0x38 : not user-modifiable */ + 0x01, /* 0x39 : not user-modifiable */ + 0x00, /* 0x3a : not user-modifiable */ + 0x00, /* 0x3b : not user-modifiable */ + 0x00, /* 0x3c : not user-modifiable */ + 0x00, /* 0x3d : not user-modifiable */ + 0xff, /* 0x3e : not user-modifiable */ + 0x00, /* 0x3f : not user-modifiable */ + 0x0F, /* 0x40 : not user-modifiable */ + 0x00, /* 0x41 : not user-modifiable */ + 0x00, /* 0x42 : not user-modifiable */ + 0x00, /* 0x43 : not user-modifiable */ + 0x00, /* 0x44 : not user-modifiable */ + 0x00, /* 0x45 : not user-modifiable */ + 0x20, /* 0x46 : interrupt configuration 0->level low detection, 1-> level high, + 2-> Out of window, 3->In window, 0x20-> New sample ready , TBC */ + 0x0b, /* 0x47 : not user-modifiable */ + 0x00, /* 0x48 : not user-modifiable */ + 0x00, /* 0x49 : not user-modifiable */ + 0x02, /* 0x4a : not user-modifiable */ + 0x14, /* 0x4b : not user-modifiable */ + 0x21, /* 0x4c : not user-modifiable */ + 0x00, /* 0x4d : not user-modifiable */ + 0x00, /* 0x4e : not user-modifiable */ + 0x05, /* 0x4f : not user-modifiable */ + 0x00, /* 0x50 : not user-modifiable */ + 0x00, /* 0x51 : not user-modifiable */ + 0x00, /* 0x52 : not user-modifiable */ + 0x00, /* 0x53 : not user-modifiable */ + 0xc8, /* 0x54 : not user-modifiable */ + 0x00, /* 0x55 : not user-modifiable */ + 0x00, /* 0x56 : not user-modifiable */ + 0x38, /* 0x57 : not user-modifiable */ + 0xff, /* 0x58 : not user-modifiable */ + 0x01, /* 0x59 : not user-modifiable */ + 0x00, /* 0x5a : not user-modifiable */ + 0x08, /* 0x5b : not user-modifiable */ + 0x00, /* 0x5c : not user-modifiable */ + 0x00, /* 0x5d : not user-modifiable */ + 0x01, /* 0x5e : not user-modifiable */ + 0xcc, /* 0x5f : not user-modifiable */ + 0x07, /* 0x60 : not user-modifiable */ + 0x01, /* 0x61 : not user-modifiable */ + 0xf1, /* 0x62 : not user-modifiable */ + 0x05, /* 0x63 : not user-modifiable */ + 0x00, /* 0x64 : Sigma threshold MSB (mm in 14.2 format for MSB+LSB), + use SetSigmaThreshold(), default value 90 mm */ + 0xa0, /* 0x65 : Sigma threshold LSB */ + 0x00, /* 0x66 : Min count Rate MSB (MCPS in 9.7 format for MSB+LSB), + use SetSignalThreshold() */ + 0x80, /* 0x67 : Min count Rate LSB */ + 0x08, /* 0x68 : not user-modifiable */ + 0x38, /* 0x69 : not user-modifiable */ + 0x00, /* 0x6a : not user-modifiable */ + 0x00, /* 0x6b : not user-modifiable */ + 0x00, /* 0x6c : Intermeasurement period MSB, 32 bits register, + use SetIntermeasurementInMs() */ + 0x00, /* 0x6d : Intermeasurement period */ + 0x0f, /* 0x6e : Intermeasurement period */ + 0x89, /* 0x6f : Intermeasurement period LSB */ + 0x00, /* 0x70 : not user-modifiable */ + 0x00, /* 0x71 : not user-modifiable */ + 0x00, /* 0x72 : distance threshold high MSB (in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x73 : distance threshold high LSB */ + 0x00, /* 0x74 : distance threshold low MSB ( in mm, MSB+LSB), + use SetD:tanceThreshold() */ + 0x00, /* 0x75 : distance threshold low LSB */ + 0x00, /* 0x76 : not user-modifiable */ + 0x01, /* 0x77 : not user-modifiable */ + 0x07, /* 0x78 : not user-modifiable */ + 0x05, /* 0x79 : not user-modifiable */ + 0x06, /* 0x7a : not user-modifiable */ + 0x06, /* 0x7b : not user-modifiable */ + 0x00, /* 0x7c : not user-modifiable */ + 0x00, /* 0x7d : not user-modifiable */ + 0x02, /* 0x7e : not user-modifiable */ + 0xc7, /* 0x7f : not user-modifiable */ + 0xff, /* 0x80 : not user-modifiable */ + 0x9B, /* 0x81 : not user-modifiable */ + 0x00, /* 0x82 : not user-modifiable */ + 0x00, /* 0x83 : not user-modifiable */ + 0x00, /* 0x84 : not user-modifiable */ + 0x01, /* 0x85 : not user-modifiable */ + 0x00, /* 0x86 : clear interrupt, use ClearInterrupt() */ + 0x00 /* 0x87 : start ranging, use StartRanging() or StopRanging(), + If you want an automatic start after VL53L4ED_init() call, + put 0x40 in location 0x87 */ }; -VL53L4ED_Error VL53L4ED_GetSWVersion( - VL53L4ED_Version_t *p_Version) +VL53L4ED_Error VL53L4ED_GetSWVersion(VL53L4ED_Version_t *p_Version) { VL53L4ED_Error Status = VL53L4ED_ERROR_NONE; @@ -143,20 +143,15 @@ VL53L4ED_Error VL53L4ED_GetSWVersion( return Status; } -VL53L4ED_Error VL53L4ED_SetI2CAddress( - Dev_t dev, - uint8_t new_address) +VL53L4ED_Error VL53L4ED_SetI2CAddress(Dev_t dev, uint8_t new_address) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, - (uint8_t)(new_address >> (uint8_t)1)); + status |= VL53L4ED_WrByte(dev, VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS, (uint8_t)(new_address >> (uint8_t)1)); return status; } -VL53L4ED_Error VL53L4ED_GetSensorId( - Dev_t dev, - uint16_t *p_id) +VL53L4ED_Error VL53L4ED_GetSensorId(Dev_t dev, uint16_t *p_id) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -164,69 +159,58 @@ VL53L4ED_Error VL53L4ED_GetSensorId( return status; } -VL53L4ED_Error VL53L4ED_SensorInit( - Dev_t dev) +VL53L4ED_Error VL53L4ED_SensorInit(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t Addr, tmp; uint8_t continue_loop = 1; uint16_t i = 0; - do{ - status |= VL53L4ED_RdByte(dev, - VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); + do { + status |= VL53L4ED_RdByte(dev, VL53L4ED_FIRMWARE__SYSTEM_STATUS, &tmp); - if(tmp == (uint8_t)0x3) /* Sensor booted */ + if (tmp == (uint8_t)0x3) /* Sensor booted */ { continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for boot */ + } else if (i < (uint16_t)1000) /* Wait for boot */ { i++; - } - else /* Timeout 1000ms reached */ + } else /* Timeout 1000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); /* Load default configuration */ - for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) - { - status |= VL53L4ED_WrByte(dev, Addr, - VL53L4ED_DEFAULT_CONFIGURATION[ - Addr - (uint8_t)0x2D]); + for (Addr = (uint8_t)0x2D; Addr <= (uint8_t)0x87; Addr++) { + status |= VL53L4ED_WrByte(dev, Addr, VL53L4ED_DEFAULT_CONFIGURATION[Addr - (uint8_t)0x2D]); } /* Start VHV */ status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, (uint8_t)0x40); - i = (uint8_t)0; + i = (uint8_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for answer */ + } else if (i < (uint16_t)1000) /* Wait for answer */ { i++; - } - else /* Timeout 1000ms reached */ + } else /* Timeout 1000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_ClearInterrupt(dev); status |= VL53L4ED_StopRanging(dev); - status |= VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, - (uint8_t)0x09); + status |= VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x09); status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0); status |= VL53L4ED_WrWord(dev, 0x0024, 0x500); @@ -235,8 +219,7 @@ VL53L4ED_Error VL53L4ED_SensorInit( return status; } -VL53L4ED_Error VL53L4ED_ClearInterrupt( - Dev_t dev) +VL53L4ED_Error VL53L4ED_ClearInterrupt(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -244,8 +227,7 @@ VL53L4ED_Error VL53L4ED_ClearInterrupt( return status; } -VL53L4ED_Error VL53L4ED_StartRanging( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StartRanging(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint32_t tmp; @@ -253,21 +235,18 @@ VL53L4ED_Error VL53L4ED_StartRanging( status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); /* Sensor runs in continuous mode */ - if(tmp == (uint32_t)0) - { + if (tmp == (uint32_t)0) { status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x21); } /* Sensor runs in autonomous mode */ - else - { + else { status |= VL53L4ED_WrByte(dev, VL53L4ED_SYSTEM_START, 0x40); } return status; } -VL53L4ED_Error VL53L4ED_StopRanging( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StopRanging(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -275,9 +254,7 @@ VL53L4ED_Error VL53L4ED_StopRanging( return status; } -VL53L4ED_Error VL53L4ED_CheckForDataReady( - Dev_t dev, - uint8_t *p_is_data_ready) +VL53L4ED_Error VL53L4ED_CheckForDataReady(Dev_t dev, uint8_t *p_is_data_ready) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t temp; @@ -287,33 +264,24 @@ VL53L4ED_Error VL53L4ED_CheckForDataReady( temp = temp & (uint8_t)0x10; temp = temp >> 4; - if (temp == (uint8_t)1) - { + if (temp == (uint8_t)1) { int_pol = (uint8_t)0; - } - else - { + } else { int_pol = (uint8_t)1; } status |= VL53L4ED_RdByte(dev, VL53L4ED_GPIO__TIO_HV_STATUS, &temp); - if ((temp & (uint8_t)1) == int_pol) - { + if ((temp & (uint8_t)1) == int_pol) { *p_is_data_ready = (uint8_t)1; - } - else - { + } else { *p_is_data_ready = (uint8_t)0; } return status; } -VL53L4ED_Error VL53L4ED_SetRangeTiming( - Dev_t dev, - uint32_t timing_budget_ms, - uint32_t inter_measurement_ms) +VL53L4ED_Error VL53L4ED_SetRangeTiming(Dev_t dev, uint32_t timing_budget_ms, uint32_t inter_measurement_ms) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t clock_pll, osc_frequency, ms_byte; @@ -321,88 +289,67 @@ VL53L4ED_Error VL53L4ED_SetRangeTiming( float_t inter_measurement_factor = (float_t)1.055; status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); - if(osc_frequency != (uint16_t)0) - { - timing_budget_us = timing_budget_ms*(uint32_t)1000; - macro_period_us = (uint32_t)((uint32_t)2304 * - ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; - } - else - { + if (osc_frequency != (uint16_t)0) { + timing_budget_us = timing_budget_ms * (uint32_t)1000; + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; + } else { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; } /* Timing budget check validity */ - if ((timing_budget_ms < (uint32_t)10) - || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) - { + if ((timing_budget_ms < (uint32_t)10) || (timing_budget_ms > (uint32_t)200) || (status != (uint8_t)0)) { status |= VL53L4ED_ERROR_INVALID_ARGUMENT; } /* Sensor runs in continuous mode */ - else if(inter_measurement_ms == (uint32_t)0) - { - status |= VL53L4ED_WrDWord(dev,VL53L4ED_INTERMEASUREMENT_MS, 0); + else if (inter_measurement_ms == (uint32_t)0) { + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, 0); timing_budget_us -= (uint32_t)2500; } /* Sensor runs in autonomous low power mode */ - else if(inter_measurement_ms > timing_budget_ms) - { - status |= VL53L4ED_RdWord(dev, - VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + else if (inter_measurement_ms > timing_budget_ms) { + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); clock_pll = clock_pll & (uint16_t)0x3FF; - inter_measurement_factor = inter_measurement_factor - * (float_t)inter_measurement_ms - * (float_t)clock_pll; - status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, - (uint32_t)inter_measurement_factor); + inter_measurement_factor = inter_measurement_factor * (float_t)inter_measurement_ms * (float_t)clock_pll; + status |= VL53L4ED_WrDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, (uint32_t)inter_measurement_factor); timing_budget_us -= (uint32_t)4300; timing_budget_us /= (uint32_t)2; } /* Invalid case */ - else - { + else { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; } - if(status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) - { - ms_byte = 0; - timing_budget_us = timing_budget_us << 12; - tmp = macro_period_us*(uint32_t)16; - ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) - - (uint32_t)1; - - while ((ls_byte & 0xFFFFFF00U) > 0U) { - ls_byte = ls_byte >> 1; - ms_byte++; - } - ms_byte = (uint16_t)(ms_byte << 8) - + (uint16_t) (ls_byte & (uint32_t)0xFF); - status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A,ms_byte); - - ms_byte = 0; - tmp = macro_period_us*(uint32_t)12; - ls_byte = ((timing_budget_us + ((tmp >> 6)>>1)) /(tmp>> 6)) - - (uint32_t)1; - - while ((ls_byte & 0xFFFFFF00U) > 0U) { - ls_byte = ls_byte >> 1; - ms_byte++; - } - ms_byte = (uint16_t)(ms_byte << 8) - + (uint16_t) (ls_byte & (uint32_t)0xFF); - status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B,ms_byte); + if (status != (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT) { + ms_byte = 0; + timing_budget_us = timing_budget_us << 12; + tmp = macro_period_us * (uint32_t)16; + ls_byte = ((timing_budget_us + ((tmp >> 6) >> 1)) / (tmp >> 6)) - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + (uint16_t)(ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_A, ms_byte); + + ms_byte = 0; + tmp = macro_period_us * (uint32_t)12; + ls_byte = ((timing_budget_us + ((tmp >> 6) >> 1)) / (tmp >> 6)) - (uint32_t)1; + + while ((ls_byte & 0xFFFFFF00U) > 0U) { + ls_byte = ls_byte >> 1; + ms_byte++; + } + ms_byte = (uint16_t)(ms_byte << 8) + (uint16_t)(ls_byte & (uint32_t)0xFF); + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG_B, ms_byte); } return status; } -VL53L4ED_Error VL53L4ED_GetRangeTiming( - Dev_t dev, - uint32_t *p_timing_budget_ms, - uint32_t *p_inter_measurement_ms) +VL53L4ED_Error VL53L4ED_GetRangeTiming(Dev_t dev, uint32_t *p_timing_budget_ms, uint32_t *p_inter_measurement_ms) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t osc_frequency = 1, range_config_macrop_high, clock_pll = 1; @@ -411,107 +358,84 @@ VL53L4ED_Error VL53L4ED_GetRangeTiming( /* Get InterMeasurement */ status |= VL53L4ED_RdDWord(dev, VL53L4ED_INTERMEASUREMENT_MS, &tmp); - status |= VL53L4ED_RdWord(dev, - VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__OSC_CALIBRATE_VAL, &clock_pll); clock_pll = clock_pll & (uint16_t)0x3FF; clock_pll_factor = clock_pll_factor * (float_t)clock_pll; clock_pll = (uint16_t)clock_pll_factor; - *p_inter_measurement_ms = (uint16_t)(tmp/(uint32_t)clock_pll); + *p_inter_measurement_ms = (uint16_t)(tmp / (uint32_t)clock_pll); /* Get TimingBudget */ status |= VL53L4ED_RdWord(dev, 0x0006, &osc_frequency); - status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, - &range_config_macrop_high); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG_A, &range_config_macrop_high); - macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 - / (uint32_t)osc_frequency)) >> 6; + macro_period_us = (uint32_t)((uint32_t)2304 * ((uint32_t)0x40000000 / (uint32_t)osc_frequency)) >> 6; ls_byte = (range_config_macrop_high & (uint32_t)0x00FF) << 4; ms_byte = (range_config_macrop_high & (uint32_t)0xFF00) >> 8; ms_byte = (uint32_t)0x04 - (ms_byte - (uint32_t)1) - (uint32_t)1; macro_period_us = macro_period_us * (uint32_t)16; - *p_timing_budget_ms = (((ls_byte + (uint32_t)1)*(macro_period_us>> 6)) - - ((macro_period_us>> 6)>>1)) >> 12; + *p_timing_budget_ms = (((ls_byte + (uint32_t)1) * (macro_period_us >> 6)) - ((macro_period_us >> 6) >> 1)) >> 12; - if(ms_byte < (uint8_t)12) - { - *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms - >> (uint8_t)ms_byte); + if (ms_byte < (uint8_t)12) { + *p_timing_budget_ms = (uint32_t)(*p_timing_budget_ms >> (uint8_t)ms_byte); } - - /* Mode continuous */ - if(tmp == (uint32_t)0) - { + + /* Mode continuous */ + if (tmp == (uint32_t)0) { *p_timing_budget_ms += (uint32_t)2500; - } - /* Mode autonomous */ - else - { - *p_timing_budget_ms *= (uint32_t)2; - *p_timing_budget_ms += (uint32_t)4300; - } + } + /* Mode autonomous */ + else { + *p_timing_budget_ms *= (uint32_t)2; + *p_timing_budget_ms += (uint32_t)4300; + } - *p_timing_budget_ms = *p_timing_budget_ms/(uint32_t)1000; + *p_timing_budget_ms = *p_timing_budget_ms / (uint32_t)1000; return status; } -VL53L4ED_Error VL53L4ED_GetResult( - Dev_t dev, - VL53L4ED_ResultsData_t *p_result) +VL53L4ED_Error VL53L4ED_GetResult(Dev_t dev, VL53L4ED_ResultsData_t *p_result) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp_16; uint8_t temp_8; - uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3, - 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, - 255, 255, 11, 12 }; + uint8_t status_rtn[24] = {255, 255, 255, 5, 2, 4, 1, 7, 3, 0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6, 255, 255, 11, 12}; - status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, - &temp_8); + status |= VL53L4ED_RdByte(dev, VL53L4ED_RESULT__RANGE_STATUS, &temp_8); temp_8 = temp_8 & (uint8_t)0x1F; - if (temp_8 < (uint8_t)24) - { + if (temp_8 < (uint8_t)24) { temp_8 = status_rtn[temp_8]; } p_result->range_status = temp_8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, - &temp_16); - p_result->number_of_spad = temp_16 / (uint16_t) 256; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SPAD_NB, &temp_16); + p_result->number_of_spad = temp_16 / (uint16_t)256; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, - &temp_16); - p_result->signal_rate_kcps = temp_16 * (uint16_t) 8; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGNAL_RATE, &temp_16); + p_result->signal_rate_kcps = temp_16 * (uint16_t)8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, - &temp_16); - p_result->ambient_rate_kcps = temp_16 * (uint16_t) 8; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__AMBIENT_RATE, &temp_16); + p_result->ambient_rate_kcps = temp_16 * (uint16_t)8; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, - &temp_16); - p_result->sigma_mm = temp_16 / (uint16_t) 4; + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__SIGMA, &temp_16); + p_result->sigma_mm = temp_16 / (uint16_t)4; - status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, - &temp_16); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RESULT__DISTANCE, &temp_16); p_result->distance_mm = temp_16; - p_result->signal_per_spad_kcps = p_result->signal_rate_kcps - /p_result->number_of_spad; - p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps - /p_result->number_of_spad; + p_result->signal_per_spad_kcps = p_result->signal_rate_kcps / p_result->number_of_spad; + p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps / p_result->number_of_spad; return status; } -VL53L4ED_Error VL53L4ED_SetOffset( - Dev_t dev, - int16_t OffsetValueInMm) +VL53L4ED_Error VL53L4ED_SetOffset(Dev_t dev, int16_t OffsetValueInMm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp; - temp = (uint16_t)((uint16_t)OffsetValueInMm*(uint16_t)4); + temp = (uint16_t)((uint16_t)OffsetValueInMm * (uint16_t)4); status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, temp); status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, (uint8_t)0x0); @@ -519,65 +443,49 @@ VL53L4ED_Error VL53L4ED_SetOffset( return status; } -VL53L4ED_Error VL53L4ED_GetOffset( - Dev_t dev, - int16_t *p_offset) +VL53L4ED_Error VL53L4ED_GetOffset(Dev_t dev, int16_t *p_offset) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t temp; - status |= VL53L4ED_RdWord(dev,VL53L4ED_RANGE_OFFSET_MM, &temp); + status |= VL53L4ED_RdWord(dev, VL53L4ED_RANGE_OFFSET_MM, &temp); - temp = temp<<3; - temp = temp>>5; + temp = temp << 3; + temp = temp >> 5; *p_offset = (int16_t)(temp); - if(*p_offset > 1024) - { + if (*p_offset > 1024) { *p_offset = *p_offset - 2048; } return status; } -VL53L4ED_Error VL53L4ED_SetXtalk( - Dev_t dev, - uint16_t XtalkValueKcps) +VL53L4ED_Error VL53L4ED_SetXtalk(Dev_t dev, uint16_t XtalkValueKcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, - (XtalkValueKcps<<9)); - + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS, 0x0000); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, (XtalkValueKcps << 9)); + return status; } -VL53L4ED_Error VL53L4ED_GetXtalk( - Dev_t dev, - uint16_t *p_xtalk_kcps) +VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; float_t tmp_xtalk; - status |= VL53L4ED_RdWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); - + status |= VL53L4ED_RdWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, p_xtalk_kcps); + tmp_xtalk = (float_t)*p_xtalk_kcps / (float_t)512.0; *p_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); return status; } -VL53L4ED_Error VL53L4ED_SetDetectionThresholds( - Dev_t dev, - uint16_t distance_low_mm, - uint16_t distance_high_mm, - uint8_t window) +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, uint16_t distance_low_mm, uint16_t distance_high_mm, uint8_t window) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; @@ -587,14 +495,11 @@ VL53L4ED_Error VL53L4ED_SetDetectionThresholds( return status; } -VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, - uint16_t *p_distance_low_mm, - uint16_t *p_distance_high_mm, - uint8_t *p_window) +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, uint16_t *p_distance_low_mm, uint16_t *p_distance_high_mm, uint8_t *p_window) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH,p_distance_high_mm); + status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_HIGH, p_distance_high_mm); status |= VL53L4ED_RdWord(dev, VL53L4ED_THRESH_LOW, p_distance_low_mm); status |= VL53L4ED_RdByte(dev, VL53L4ED_SYSTEM__INTERRUPT, p_window); *p_window = (*p_window & (uint8_t)0x7); @@ -602,98 +507,78 @@ VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, return status; } -VL53L4ED_Error VL53L4ED_SetSignalThreshold( - Dev_t dev, - uint16_t signal_kcps) +VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status |= VL53L4ED_WrWord(dev, - VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS,signal_kcps>>3); + status |= VL53L4ED_WrWord(dev, VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, signal_kcps >> 3); return status; } -VL53L4ED_Error VL53L4ED_GetSignalThreshold( - Dev_t dev, - uint16_t *p_signal_kcps) +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, uint16_t *p_signal_kcps) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint16_t tmp = 0; - status |= VL53L4ED_RdWord(dev, - VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); - *p_signal_kcps = tmp <<3; + status |= VL53L4ED_RdWord(dev, VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp); + *p_signal_kcps = tmp << 3; return status; } -VL53L4ED_Error VL53L4ED_SetSigmaThreshold( - Dev_t dev, - uint16_t sigma_mm) +VL53L4ED_Error VL53L4ED_SetSigmaThreshold(Dev_t dev, uint16_t sigma_mm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - if(sigma_mm>(uint16_t)((uint16_t)0xFFFF>>2)) - { + if (sigma_mm > (uint16_t)((uint16_t)0xFFFF >> 2)) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { - status |= VL53L4ED_WrWord(dev, - VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm<<2); + } else { + status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, sigma_mm << 2); } return status; } -VL53L4ED_Error VL53L4ED_GetSigmaThreshold( - Dev_t dev, - uint16_t *p_sigma_mm) +VL53L4ED_Error VL53L4ED_GetSigmaThreshold(Dev_t dev, uint16_t *p_sigma_mm) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; - status += VL53L4ED_RdWord(dev, - VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); + status += VL53L4ED_RdWord(dev, VL53L4ED_RANGE_CONFIG__SIGMA_THRESH, p_sigma_mm); *p_sigma_mm = *p_sigma_mm >> 2; return status; } -VL53L4ED_Error VL53L4ED_StartTemperatureUpdate( - Dev_t dev) +VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t tmp = 0, continue_loop = 1; uint16_t i = 0; - status |= VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); + status |= VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, (uint8_t)0x81); status |= VL53L4ED_WrByte(dev, 0x0B, (uint8_t)0x92); status |= VL53L4ED_StartRanging(dev); - do{ - status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ - { - continue_loop = (uint8_t)0; - } - else if(i < (uint16_t)1000) /* Wait for answer */ - { - i++; - } - else /* Timeout 1000ms reached */ - { - continue_loop = (uint8_t)0; - status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; - } - VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + do { + status |= VL53L4ED_CheckForDataReady(dev, &tmp); + if (tmp == (uint8_t)1) /* Data ready */ + { + continue_loop = (uint8_t)0; + } else if (i < (uint16_t)1000) /* Wait for answer */ + { + i++; + } else /* Timeout 1000ms reached */ + { + continue_loop = (uint8_t)0; + status = (uint8_t)VL53L4ED_ERROR_TIMEOUT; + } + VL53L4ED_WaitMs(dev, 1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_ClearInterrupt(dev); status |= VL53L4ED_StopRanging(dev); - status += VL53L4ED_WrByte(dev, - VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); + status += VL53L4ED_WrByte(dev, VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); status += VL53L4ED_WrByte(dev, 0x0B, 0); return status; } diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h index df55ef3d0..e5d7acf2b 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_api.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #ifndef VL53L4ED_API_H_ #define VL53L4ED_API_H_ @@ -19,10 +19,10 @@ * @brief Driver version */ -#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 -#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 -#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 -#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 +#define VL53L4ED_IMPLEMENTATION_VER_MAJOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_MINOR 1 +#define VL53L4ED_IMPLEMENTATION_VER_BUILD 2 +#define VL53L4ED_IMPLEMENTATION_VER_REVISION 0 /** * @brief Driver error type @@ -30,58 +30,56 @@ typedef uint8_t VL53L4ED_Error; -#define VL53L4ED_ERROR_NONE ((uint8_t)0U) -#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) -#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) -#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) - +#define VL53L4ED_ERROR_NONE ((uint8_t)0U) +#define VL53L4ED_ERROR_XTALK_FAILED ((uint8_t)253U) +#define VL53L4ED_ERROR_INVALID_ARGUMENT ((uint8_t)254U) +#define VL53L4ED_ERROR_TIMEOUT ((uint8_t)255U) /** * @brief Inner Macro for API. Not for user, only for development. */ #define VL53L4ED_SOFT_RESET ((uint16_t)0x0000)) -#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) -#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) +#define VL53L4ED_I2C_SLAVE__DEVICE_ADDRESS ((uint16_t)0x0001) +#define VL53L4ED_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND ((uint16_t)0x0008) #define VL53L4ED_XTALK_PLANE_OFFSET_KCPS ((uint16_t)0x0016) -#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) -#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) -#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) -#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) -#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) -#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) -#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) -#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) -#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) -#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) -#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) -#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) +#define VL53L4ED_XTALK_X_PLANE_GRADIENT_KCPS ((uint16_t)0x0018) +#define VL53L4ED_XTALK_Y_PLANE_GRADIENT_KCPS ((uint16_t)0x001A) +#define VL53L4ED_RANGE_OFFSET_MM ((uint16_t)0x001E) +#define VL53L4ED_INNER_OFFSET_MM ((uint16_t)0x0020) +#define VL53L4ED_OUTER_OFFSET_MM ((uint16_t)0x0022) +#define VL53L4ED_GPIO_HV_MUX__CTRL ((uint16_t)0x0030) +#define VL53L4ED_GPIO__TIO_HV_STATUS ((uint16_t)0x0031) +#define VL53L4ED_SYSTEM__INTERRUPT ((uint16_t)0x0046) +#define VL53L4ED_RANGE_CONFIG_A ((uint16_t)0x005E) +#define VL53L4ED_RANGE_CONFIG_B ((uint16_t)0x0061) +#define VL53L4ED_RANGE_CONFIG__SIGMA_THRESH ((uint16_t)0x0064) +#define VL53L4ED_MIN_COUNT_RATE_RTN_LIMIT_MCPS ((uint16_t)0x0066) #define VL53L4ED_INTERMEASUREMENT_MS ((uint16_t)0x006C) -#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) -#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) -#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) -#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) -#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) -#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) -#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) -#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) -#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) -#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) - - -#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) -#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) -#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) +#define VL53L4ED_THRESH_HIGH ((uint16_t)0x0072) +#define VL53L4ED_THRESH_LOW ((uint16_t)0x0074) +#define VL53L4ED_SYSTEM__INTERRUPT_CLEAR ((uint16_t)0x0086) +#define VL53L4ED_SYSTEM_START ((uint16_t)0x0087) +#define VL53L4ED_RESULT__RANGE_STATUS ((uint16_t)0x0089) +#define VL53L4ED_RESULT__SPAD_NB ((uint16_t)0x008C) +#define VL53L4ED_RESULT__SIGNAL_RATE ((uint16_t)0x008E) +#define VL53L4ED_RESULT__AMBIENT_RATE ((uint16_t)0x0090) +#define VL53L4ED_RESULT__SIGMA ((uint16_t)0x0092) +#define VL53L4ED_RESULT__DISTANCE ((uint16_t)0x0096) + +#define VL53L4ED_RESULT__OSC_CALIBRATE_VAL ((uint16_t)0x00DE) +#define VL53L4ED_FIRMWARE__SYSTEM_STATUS ((uint16_t)0x00E5) +#define VL53L4ED_IDENTIFICATION__MODEL_ID ((uint16_t)0x010F) /** * @brief defines Software Version */ typedef struct { - uint8_t major; /*!< major number */ - uint8_t minor; /*!< minor number */ - uint8_t build; /*!< build number */ - uint32_t revision; /*!< revision number */ + uint8_t major; /*!< major number */ + uint8_t minor; /*!< minor number */ + uint8_t build; /*!< build number */ + uint32_t revision; /*!< revision number */ } VL53L4ED_Version_t; /** @@ -115,9 +113,7 @@ typedef struct { * @return (VL53L4ED_ERROR) status : 0 if SW version is OK. */ -VL53L4ED_Error VL53L4ED_GetSWVersion( - VL53L4ED_Version_t *pVersion); - +VL53L4ED_Error VL53L4ED_GetSWVersion(VL53L4ED_Version_t *pVersion); /** * @brief This function sets a new I2C address to a sensor. It can be used for @@ -128,9 +124,7 @@ VL53L4ED_Error VL53L4ED_GetSWVersion( * programmed. */ -VL53L4ED_Error VL53L4ED_SetI2CAddress( - Dev_t dev, - uint8_t new_address); +VL53L4ED_Error VL53L4ED_SetI2CAddress(Dev_t dev, uint8_t new_address); /** * @brief This function is used to get the sensor id of VL53L4ED. The sensor id @@ -140,9 +134,7 @@ VL53L4ED_Error VL53L4ED_SetI2CAddress( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetSensorId( - Dev_t dev, - uint16_t *p_id); +VL53L4ED_Error VL53L4ED_GetSensorId(Dev_t dev, uint16_t *p_id); /** * @brief This function is used to initialize the sensor. @@ -150,8 +142,7 @@ VL53L4ED_Error VL53L4ED_GetSensorId( * @return (VL53L4ED_ERROR) status : 0 if init is OK. */ -VL53L4ED_Error VL53L4ED_SensorInit( - Dev_t dev); +VL53L4ED_Error VL53L4ED_SensorInit(Dev_t dev); /** * @brief This function clears the interrupt. It needs to be called after a @@ -160,8 +151,7 @@ VL53L4ED_Error VL53L4ED_SensorInit( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_ClearInterrupt( - Dev_t dev); +VL53L4ED_Error VL53L4ED_ClearInterrupt(Dev_t dev); /** * @brief This function starts a ranging session. The ranging operation is @@ -171,8 +161,7 @@ VL53L4ED_Error VL53L4ED_ClearInterrupt( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_StartRanging( - Dev_t dev); +VL53L4ED_Error VL53L4ED_StartRanging(Dev_t dev); /** * @brief This function stops the ranging in progress. @@ -180,8 +169,7 @@ VL53L4ED_Error VL53L4ED_StartRanging( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_StopRanging( - Dev_t dev); +VL53L4ED_Error VL53L4ED_StopRanging(Dev_t dev); /** * @brief This function check if a new data is available by polling a dedicated @@ -192,9 +180,7 @@ VL53L4ED_Error VL53L4ED_StopRanging( * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_CheckForDataReady( - Dev_t dev, - uint8_t *p_is_data_ready); +VL53L4ED_Error VL53L4ED_CheckForDataReady(Dev_t dev, uint8_t *p_is_data_ready); /** * @brief This function sets new range timing. Timing are composed of @@ -213,10 +199,7 @@ VL53L4ED_Error VL53L4ED_CheckForDataReady( * @return (uint8_t) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_SetRangeTiming( - Dev_t dev, - uint32_t timing_budget_ms, - uint32_t inter_measurement_ms); +VL53L4ED_Error VL53L4ED_SetRangeTiming(Dev_t dev, uint32_t timing_budget_ms, uint32_t inter_measurement_ms); /** * @brief This function gets the current range timing. Timing are composed of @@ -232,10 +215,7 @@ VL53L4ED_Error VL53L4ED_SetRangeTiming( * @return (uint8_t) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetRangeTiming( - Dev_t dev, - uint32_t *p_timing_budget_ms, - uint32_t *p_inter_measurement_ms); +VL53L4ED_Error VL53L4ED_GetRangeTiming(Dev_t dev, uint32_t *p_timing_budget_ms, uint32_t *p_inter_measurement_ms); /** * @brief This function gets the results reported by the sensor. @@ -310,11 +290,7 @@ VL53L4ED_Error VL53L4ED_GetXtalk(Dev_t dev, uint16_t *p_xtalk_kcps); * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, - uint16_t distance_low_mm, - uint16_t distance_high_mm, - uint8_t window); - +VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, uint16_t distance_low_mm, uint16_t distance_high_mm, uint8_t window); /** * @brief This function gets the current detection thresholds. The detection @@ -330,10 +306,7 @@ VL53L4ED_Error VL53L4ED_SetDetectionThresholds(Dev_t dev, * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, - uint16_t *p_distance_low_mm, - uint16_t *p_distance_high_mm, - uint8_t *p_window); +VL53L4ED_Error VL53L4ED_GetDetectionThresholds(Dev_t dev, uint16_t *p_distance_low_mm, uint16_t *p_distance_high_mm, uint8_t *p_window); /** * @brief This function sets a new signal threshold in kcps. If a @@ -357,8 +330,7 @@ VL53L4ED_Error VL53L4ED_SetSignalThreshold(Dev_t dev, uint16_t signal_kcps); * @return (VL53L4ED_ERROR) status : 0 if OK. */ -VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, - uint16_t *p_signal_kcps); +VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, uint16_t *p_signal_kcps); /** * @brief This function programs a new sigma threshold. The sigma corresponds to @@ -372,9 +344,7 @@ VL53L4ED_Error VL53L4ED_GetSignalThreshold(Dev_t dev, * high. */ -VL53L4ED_Error VL53L4ED_SetSigmaThreshold( - Dev_t dev, - uint16_t sigma_mm); +VL53L4ED_Error VL53L4ED_SetSigmaThreshold(Dev_t dev, uint16_t sigma_mm); /** * @brief This function gets the current sigma threshold. The sigma corresponds @@ -386,9 +356,7 @@ VL53L4ED_Error VL53L4ED_SetSigmaThreshold( * @return (VL53L4ED_ERROR) status : 0 if programming is OK. */ -VL53L4ED_Error VL53L4ED_GetSigmaThreshold( - Dev_t dev, - uint16_t *p_sigma_mm); +VL53L4ED_Error VL53L4ED_GetSigmaThreshold(Dev_t dev, uint16_t *p_sigma_mm); /** * @brief This function can be called when the temperature might have changed by @@ -402,4 +370,4 @@ VL53L4ED_Error VL53L4ED_GetSigmaThreshold( VL53L4ED_Error VL53L4ED_StartTemperatureUpdate(Dev_t dev); -#endif //VL53L4ED_API_H_ +#endif // VL53L4ED_API_H_ diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c index e3d95c223..6a0d96eb1 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.c @@ -1,29 +1,27 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_calibration.c * @brief Calibration functions implementation */ +#include "VL53L4ED_calibration.h" + #include + #include "VL53L4ED_api.h" -#include "VL53L4ED_calibration.h" -VL53L4ED_Error VL53L4ED_CalibrateOffset( - Dev_t dev, - int16_t TargetDistInMm, - int16_t *p_measured_offset_mm, - int16_t nb_samples) +VL53L4ED_Error VL53L4ED_CalibrateOffset(Dev_t dev, int16_t TargetDistInMm, int16_t *p_measured_offset_mm, int16_t nb_samples) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t i, tmp, continue_loop; @@ -31,14 +29,9 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( int16_t AvgDistance = 0; VL53L4ED_ResultsData_t results; - if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) - || ((TargetDistInMm < (int16_t)10) - || (TargetDistInMm > (int16_t)1000))) - { + if (((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) || ((TargetDistInMm < (int16_t)10) || (TargetDistInMm > (int16_t)1000))) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { + } else { status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, 0x0); status |= VL53L4ED_WrWord(dev, VL53L4ED_INNER_OFFSET_MM, 0x0); status |= VL53L4ED_WrWord(dev, VL53L4ED_OUTER_OFFSET_MM, 0x0); @@ -49,23 +42,21 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); } @@ -77,23 +68,21 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { - continue_loop = (uint8_t)0; + continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); @@ -103,18 +92,14 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( status |= VL53L4ED_StopRanging(dev); AvgDistance = AvgDistance / nb_samples; *p_measured_offset_mm = (int16_t)TargetDistInMm - AvgDistance; - tmpOff = (uint16_t) *p_measured_offset_mm * (uint16_t)4; + tmpOff = (uint16_t)*p_measured_offset_mm * (uint16_t)4; status |= VL53L4ED_WrWord(dev, VL53L4ED_RANGE_OFFSET_MM, tmpOff); } return status; } -VL53L4ED_Error VL53L4ED_CalibrateXtalk( - Dev_t dev, - int16_t TargetDistInMm, - uint16_t *p_measured_xtalk_kcps, - int16_t nb_samples) +VL53L4ED_Error VL53L4ED_CalibrateXtalk(Dev_t dev, int16_t TargetDistInMm, uint16_t *p_measured_xtalk_kcps, int16_t nb_samples) { VL53L4ED_Error status = VL53L4ED_ERROR_NONE; uint8_t i, tmp, continue_loop; @@ -128,17 +113,11 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( uint16_t calXtalk, j; *p_measured_xtalk_kcps = 0; - if(((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) - || ((TargetDistInMm < (int16_t)10) - || (TargetDistInMm > (int16_t)5000))) - { + if (((nb_samples < (int16_t)5) || (nb_samples > (int16_t)255)) || ((TargetDistInMm < (int16_t)10) || (TargetDistInMm > (int16_t)5000))) { status |= (uint8_t)VL53L4ED_ERROR_INVALID_ARGUMENT; - } - else - { + } else { /* Disable Xtalk compensation */ - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, *p_measured_xtalk_kcps); /* Device heat loop (10 samples) */ status |= VL53L4ED_StartRanging(dev); @@ -146,23 +125,21 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); } @@ -170,36 +147,31 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( /* Device ranging loop */ status |= VL53L4ED_StartRanging(dev); - for (i = 0; i < (uint8_t)nb_samples; i++) - { + for (i = 0; i < (uint8_t)nb_samples; i++) { tmp = (uint8_t)0; j = (uint16_t)0; continue_loop = (uint8_t)1; - do{ + do { status |= VL53L4ED_CheckForDataReady(dev, &tmp); - if(tmp == (uint8_t)1) /* Data ready */ + if (tmp == (uint8_t)1) /* Data ready */ { continue_loop = (uint8_t)0; - } - else if(j < (uint16_t)5000) /* Wait for answer*/ + } else if (j < (uint16_t)5000) /* Wait for answer*/ { j++; - } - else /* Timeout 5000ms reached */ + } else /* Timeout 5000ms reached */ { - continue_loop = (uint8_t)0; + continue_loop = (uint8_t)0; status |= (uint8_t)VL53L4ED_ERROR_TIMEOUT; } VL53L4ED_WaitMs(dev, 1); - }while(continue_loop == (uint8_t)1); + } while (continue_loop == (uint8_t)1); status |= VL53L4ED_GetResult(dev, &results); status |= VL53L4ED_ClearInterrupt(dev); /* Discard invalid measurements and first frame */ - if (results.range_status == (uint8_t)0 - && i > (uint8_t)0) - { + if (results.range_status == (uint8_t)0 && i > (uint8_t)0) { AvgDistance += (float_t)results.distance_mm; AverageSpadNb += (float_t)results.number_of_spad; AverageSignal += (float_t)results.signal_rate_kcps; @@ -208,32 +180,25 @@ VL53L4ED_Error VL53L4ED_CalibrateXtalk( } status |= VL53L4ED_StopRanging(dev); - if (CounterNbSamples == 0) - { + if (CounterNbSamples == 0) { status = VL53L4ED_ERROR_XTALK_FAILED; - } - else - { + } else { AvgDistance /= CounterNbSamples; AverageSpadNb /= CounterNbSamples; AverageSignal /= CounterNbSamples; - tmp_xtalk = (float_t)1.0 - (AvgDistance/TargetDistance); - tmp_xtalk *= (AverageSignal/AverageSpadNb); + tmp_xtalk = (float_t)1.0 - (AvgDistance / TargetDistance); + tmp_xtalk *= (AverageSignal / AverageSpadNb); /* 127kcps is the max Xtalk value (65536/512) */ - if(tmp_xtalk > (uint16_t)127) - { + if (tmp_xtalk > (uint16_t)127) { status = VL53L4ED_ERROR_XTALK_FAILED; - } - else - { + } else { *p_measured_xtalk_kcps = (uint16_t)(round(tmp_xtalk)); /* Send data to firmware */ calXtalk = (uint16_t)(tmp_xtalk * (float_t)512.0); - status |= VL53L4ED_WrWord(dev, - VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); + status |= VL53L4ED_WrWord(dev, VL53L4ED_XTALK_PLANE_OFFSET_KCPS, calXtalk); } } } diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h index c953d0239..97daa1d3c 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/VL53L4ED_calibration.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ /** * @file vl53l4ed_calibration.h @@ -38,12 +38,7 @@ * invalid nb of samples). */ -VL53L4ED_Error VL53L4ED_CalibrateOffset( - Dev_t dev, - int16_t TargetDistInMm, - int16_t *p_measured_offset_mm, - int16_t nb_samples); - +VL53L4ED_Error VL53L4ED_CalibrateOffset(Dev_t dev, int16_t TargetDistInMm, int16_t *p_measured_offset_mm, int16_t nb_samples); /** * @brief This function can be used to perform a Xtalk calibration. Xtalk @@ -63,11 +58,7 @@ VL53L4ED_Error VL53L4ED_CalibrateOffset( * @return (VL53L4ED_ERROR) status : 0 if OK, or 255 if something occurred (e.g * invalid nb of samples). */ - -VL53L4ED_Error VL53L4ED_CalibrateXtalk( - Dev_t dev, - int16_t TargetDistInMm, - uint16_t *p_measured_xtalk_kcps, - int16_t nb_samples); -#endif //VL53L4ED_CALIBRATION_H_ +VL53L4ED_Error VL53L4ED_CalibrateXtalk(Dev_t dev, int16_t TargetDistInMm, uint16_t *p_measured_xtalk_kcps, int16_t nb_samples); + +#endif // VL53L4ED_CALIBRATION_H_ diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c index ba3d6f1a6..cf09e50e7 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.c @@ -1,25 +1,25 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #include "platform.h" -#include "main.h" + #include "i2c.h" +#include "main.h" extern I2C_HandleTypeDef hi2c1; - /* Im legit just using the example stm provides but instead of filling everything out im only copying the platform.c implemintation -for the vl53l4ed sensor. +for the vl53l4ed sensor. */ @@ -34,8 +34,7 @@ uint8_t VL53L4ED_RdDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t *value) data_write[1] = RegisterAdress & 0xFF; status = HAL_I2C_Master_Transmit(&hi2c1, dev, data_write, 2, 100); status = HAL_I2C_Master_Receive(&hi2c1, dev, data_read, 4, 100); - *value = ((data_read[0] << 24) | (data_read[1]<<16) | - (data_read[2]<<8)| (data_read[3])); + *value = ((data_read[0] << 24) | (data_read[1] << 16) | (data_read[2] << 8) | (data_read[3])); return status; } diff --git a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h index ee27e91f2..9c7754970 100644 --- a/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h +++ b/SAMM/IMUandTOF/Core/Extras/VL53L4ED/platform.h @@ -1,14 +1,14 @@ /** - * - * 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. - * - ****************************************************************************** - */ + * + * 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. + * + ****************************************************************************** + */ #ifndef _PLATFORM_H_ #define _PLATFORM_H_ @@ -18,8 +18,8 @@ #include /** -* VL53L4ED device instance. -*/ + * VL53L4ED device instance. + */ typedef uint16_t Dev_t; @@ -33,8 +33,7 @@ typedef uint8_t VL53L4ED_Error; * with I2C Fast Mode Plus (up to 1MHz). Otherwise, default max value is 400kHz. */ -//#define VL53L4ED_I2C_FAST_MODE_PLUS - +// #define VL53L4ED_I2C_FAST_MODE_PLUS /** * @brief Read 32 bits through I2C. @@ -77,4 +76,4 @@ uint8_t VL53L4ED_WrDWord(Dev_t dev, uint16_t RegisterAdress, uint32_t value); uint8_t VL53L4ED_WaitMs(Dev_t dev, uint32_t TimeMs); -#endif // _PLATFORM_H_ \ No newline at end of file +#endif // _PLATFORM_H_ \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Extras/extra.c b/SAMM/IMUandTOF/Core/Extras/extra.c index 3611c8904..25519615c 100644 --- a/SAMM/IMUandTOF/Core/Extras/extra.c +++ b/SAMM/IMUandTOF/Core/Extras/extra.c @@ -1,58 +1,57 @@ #include "extra.h" -#include "main.h" -#include "i2c.h" + #include +#include "i2c.h" +#include "main.h" void MLX90640_I2CInit(void) { - MX_I2C2_Init(); - return; + MX_I2C2_Init(); + return; } int MLX90640_I2CGeneralReset(void) { - uint8_t data = 0x06; - return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data , 1, 1000); + uint8_t data = 0x06; + return HAL_I2C_Master_Transmit(&hi2c2, 0x00, &data, 1, 1000); } - -int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) { - //HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); - //HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); - /* - HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, - uint16_t MemAddSize, uint8_t *pData, uint16_t Size) - */ - //return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); - return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 10000); + // HAL_I2C_Master_Receive(&hi2c2, slaveAddr, data, 2*nMemAddressRead, 1000); + // HAL_I2C_Master_Receive_DMA(&hi2c2, slaveAddr, data, nMemAddressRead); + /* + HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size) + */ + // return HAL_I2C_Mem_Read_DMA(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, nMemAddressRead); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2 * nMemAddressRead, 10000); } int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) { - // union - // { - // uint8_t bytes[2]; - // uint16_t data; - // }extra = {.data = data}; - return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); - //return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); + // union + // { + // uint8_t bytes[2]; + // uint16_t data; + // }extra = {.data = data}; + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 10000); + // return HAL_I2C_Master_Transmit_DMA(&hi2c2, writeAddress, extra.bytes, 2); } void MLX90640_I2CFreqSet(int freq) { - return; + return; } -uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) +uint8_t I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data) { - return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2*nMemAddressRead, 500); + return HAL_I2C_Mem_Read(&hi2c2, slaveAddr, startAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)data, 2 * nMemAddressRead, 500); } -uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data) +uint8_t I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data) { - return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *) &data, 2, 500); + return HAL_I2C_Mem_Write(&hi2c2, slaveAddr, writeAddress, I2C_MEMADD_SIZE_16BIT, (uint8_t *)&data, 2, 500); } - diff --git a/SAMM/IMUandTOF/Core/Extras/extra.h b/SAMM/IMUandTOF/Core/Extras/extra.h index 2b820f432..f032f86ab 100644 --- a/SAMM/IMUandTOF/Core/Extras/extra.h +++ b/SAMM/IMUandTOF/Core/Extras/extra.h @@ -9,12 +9,11 @@ This is for some xtra fucntions that would be needed later on void MLX90640_I2CInit(void); int MLX90640_I2CGeneralReset(void); -int MLX90640_I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); -int MLX90640_I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data); void MLX90640_I2CFreqSet(int freq); - -uint8_t I2CRead(uint8_t slaveAddr,uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); -uint8_t I2CWrite(uint8_t slaveAddr,uint16_t writeAddress, uint16_t data); +uint8_t I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data); +uint8_t I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data); #endif /* __EXTRA_H__ */ \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Inc/adc.h b/SAMM/IMUandTOF/Core/Inc/adc.h index 5692f3846..e65b1f6f8 100644 --- a/SAMM/IMUandTOF/Core/Inc/adc.h +++ b/SAMM/IMUandTOF/Core/Inc/adc.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file adc.h - * @brief This file contains all the function prototypes for - * the adc.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file adc.h + * @brief This file contains all the function prototypes for + * the adc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __ADC_H__ @@ -49,4 +49,3 @@ void MX_ADC1_Init(void); #endif #endif /* __ADC_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/bmi323.h b/SAMM/IMUandTOF/Core/Inc/bmi323.h index f434cb8dd..3299db7ab 100644 --- a/SAMM/IMUandTOF/Core/Inc/bmi323.h +++ b/SAMM/IMUandTOF/Core/Inc/bmi323.h @@ -1,14 +1,13 @@ #ifndef BMI323_H #define BMI323_H -//includes - +// includes #include "main.h" #include "spi.h" #include "stm32g474xx.h" -//BMI323 register defines +// BMI323 register defines #define BMI323_CHIP_ID 0x00 #define BMI323_ERR_REG 0x01 @@ -54,11 +53,11 @@ #define BMI323_CMD 0x7E #define BMI323_CFG_RES 0x7F -//BMI323 potential spi address +// BMI323 potential spi address #define BMI323_I2C_ADDR 0x68 #define BMI323_I2C_ADDR_ALT 0x69 -//BMI323 sub-defines for registers +// BMI323 sub-defines for registers #define BMI323_CHIP_ID_RESET_VAL 0x0043 #define BMI323_ERR_REG_RESET_VAL 0x0000 #define BMI323_STATUS_RESET_VAL 0x0001 @@ -95,25 +94,24 @@ #define BMI323_CMD_RESET_VAL 0x0000 #define BMI323_CFG_RES_RESET_VAL 0x0000 -//defines for commands +// defines for commands #define BMI323_CMD_SOFT_RESET 0xDEAF #define BMI323_CMD_CALIB 0x0101 #define BMI323_CMD_CALIB_ABORT 0x0200 -//defines for features +// defines for features #define BMI323_FEATURE_IO2_EN 0x012C #define BMI323_FEATURE_IO_STS 0x0001 #define BMI323_FEATURE_CTRL_ENGINE_EN 0x0001 #define BMI323_FEATURE_IO1_STATUS 0x0001 - -//defines for acc and gyro conf +// defines for acc and gyro conf #define SUSPEND 0b000 #define LOW_POWER 0b011 #define HIGH_PERF 0b111 #define NORMAL 0b100 -//these are the values that are used for the polling frequency +// these are the values that are used for the polling frequency #define ODR_0_78 0x1 #define ODR_1_56 0x2 #define ODR_3_12 0x3 @@ -129,11 +127,11 @@ #define ODR_3200 0xD #define ODR_6400 0xE -//these are the values that are used for the -3dB bandwidth +// these are the values that are used for the -3dB bandwidth #define ODR_DIV_2 0x0 #define ODR_DIV_4 0x1 -//these are defines for the sample averaging +// these are defines for the sample averaging #define AVG_0 0x0 #define AVG_2 0x1 #define AVG_4 0x2 @@ -142,14 +140,14 @@ #define AVG_32 0x5 #define AVG_64 0x6 -//acc specific defines for range +// acc specific defines for range #define ACC_RANGE_2G 0x0 #define ACC_RANGE_4G 0x1 #define ACC_RANGE_8G 0x2 #define ACC_RANGE_16G 0x3 -//gyro specific defines for range -//WRONG VALUES +// gyro specific defines for range +// WRONG VALUES #define GYR_RANGE_2000 0x0 #define GYR_RANGE_1000 0x1 #define GYR_RANGE_500 0x2 @@ -157,14 +155,13 @@ #define BMI323_TIMEOUT 1000 -typedef struct -{ - SPI_HandleTypeDef *spi_port; - uint16_t chip_id; - GPIO_TypeDef *port; - uint16_t pin; - /* data */ -} bmi323 ; +typedef struct { + SPI_HandleTypeDef *spi_port; + uint16_t chip_id; + GPIO_TypeDef *port; + uint16_t pin; + /* data */ +} bmi323; uint8_t bmi323_init(bmi323 *bmi323_dev, SPI_HandleTypeDef *spi_port, GPIO_TypeDef *port, uint16_t pin); uint16_t bmi323_read(bmi323 *bmi323_dev, uint8_t reg); @@ -195,6 +192,4 @@ uint8_t bmi323_read_acc(bmi323 *bmi323_dev, int16_t *acc_data); uint8_t bmi323_read_gyr(bmi323 *bmi323_dev, int16_t *gyr_data); uint8_t bmi323_read_all(bmi323 *bmi323_dev, int16_t *temp_data); - - #endif // BMI323_H \ No newline at end of file diff --git a/SAMM/IMUandTOF/Core/Inc/crc.h b/SAMM/IMUandTOF/Core/Inc/crc.h index 836835d02..133957e92 100644 --- a/SAMM/IMUandTOF/Core/Inc/crc.h +++ b/SAMM/IMUandTOF/Core/Inc/crc.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file crc.h - * @brief This file contains all the function prototypes for - * the crc.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file crc.h + * @brief This file contains all the function prototypes for + * the crc.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __CRC_H__ @@ -49,4 +49,3 @@ void MX_CRC_Init(void); #endif #endif /* __CRC_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/dma.h b/SAMM/IMUandTOF/Core/Inc/dma.h index f3b1a09f9..7774831e0 100644 --- a/SAMM/IMUandTOF/Core/Inc/dma.h +++ b/SAMM/IMUandTOF/Core/Inc/dma.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file dma.h - * @brief This file contains all the function prototypes for - * the dma.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file dma.h + * @brief This file contains all the function prototypes for + * the dma.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __DMA_H__ @@ -49,4 +49,3 @@ void MX_DMA_Init(void); #endif #endif /* __DMA_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/fdcan.h b/SAMM/IMUandTOF/Core/Inc/fdcan.h index 9289ea3a6..a5cc22f04 100644 --- a/SAMM/IMUandTOF/Core/Inc/fdcan.h +++ b/SAMM/IMUandTOF/Core/Inc/fdcan.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file fdcan.h - * @brief This file contains all the function prototypes for - * the fdcan.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file fdcan.h + * @brief This file contains all the function prototypes for + * the fdcan.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __FDCAN_H__ @@ -52,4 +52,3 @@ void MX_FDCAN2_Init(void); #endif #endif /* __FDCAN_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/gpio.h b/SAMM/IMUandTOF/Core/Inc/gpio.h index 708bac71d..843d4e9e7 100644 --- a/SAMM/IMUandTOF/Core/Inc/gpio.h +++ b/SAMM/IMUandTOF/Core/Inc/gpio.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file gpio.h - * @brief This file contains all the function prototypes for - * the gpio.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __GPIO_H__ @@ -46,4 +46,3 @@ void MX_GPIO_Init(void); } #endif #endif /*__ GPIO_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/i2c.h b/SAMM/IMUandTOF/Core/Inc/i2c.h index 84ed75d2e..64f5543e6 100644 --- a/SAMM/IMUandTOF/Core/Inc/i2c.h +++ b/SAMM/IMUandTOF/Core/Inc/i2c.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file i2c.h - * @brief This file contains all the function prototypes for - * the i2c.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file i2c.h + * @brief This file contains all the function prototypes for + * the i2c.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __I2C_H__ @@ -49,4 +49,3 @@ void MX_I2C1_Init(void); #endif #endif /* __I2C_H__ */ - diff --git a/SAMM/IMUandTOF/Core/Inc/main.h b/SAMM/IMUandTOF/Core/Inc/main.h index 17a2fe760..32df0ac37 100644 --- a/SAMM/IMUandTOF/Core/Inc/main.h +++ b/SAMM/IMUandTOF/Core/Inc/main.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.h - * @brief : Header for main.c file. - * This file contains the common defines of the application. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 -------------------------------------*/ @@ -28,17 +28,16 @@ extern "C" { /* Includes ------------------------------------------------------------------*/ #include "stm32g4xx_hal.h" - -#include "stm32g4xx_ll_rcc.h" #include "stm32g4xx_ll_bus.h" -#include "stm32g4xx_ll_crs.h" -#include "stm32g4xx_ll_system.h" -#include "stm32g4xx_ll_exti.h" #include "stm32g4xx_ll_cortex.h" -#include "stm32g4xx_ll_utils.h" -#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_crs.h" #include "stm32g4xx_ll_dma.h" +#include "stm32g4xx_ll_exti.h" #include "stm32g4xx_ll_gpio.h" +#include "stm32g4xx_ll_pwr.h" +#include "stm32g4xx_ll_rcc.h" +#include "stm32g4xx_ll_system.h" +#include "stm32g4xx_ll_utils.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ diff --git a/SAMM/IMUandTOF/Core/Inc/spi.h b/SAMM/IMUandTOF/Core/Inc/spi.h index 434f0eeb8..795f9dd81 100644 --- a/SAMM/IMUandTOF/Core/Inc/spi.h +++ b/SAMM/IMUandTOF/Core/Inc/spi.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file spi.h - * @brief This file contains all the function prototypes for - * the spi.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file spi.h + * @brief This file contains all the function prototypes for + * the spi.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 __SPI_H__ diff --git a/SAMM/IMUandTOF/Core/Inc/stm32_assert.h b/SAMM/IMUandTOF/Core/Inc/stm32_assert.h index 61631c41e..92460620f 100644 --- a/SAMM/IMUandTOF/Core/Inc/stm32_assert.h +++ b/SAMM/IMUandTOF/Core/Inc/stm32_assert.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32_assert.h - * @author MCD Application Team - * @brief STM32 assert file. - ****************************************************************************** - * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32_assert.h + * @author MCD Application Team + * @brief STM32 assert file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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 __STM32_ASSERT_H @@ -29,15 +29,15 @@ extern "C" { /* Exported constants --------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ void assert_failed(uint8_t *file, uint32_t line); @@ -50,4 +50,3 @@ void assert_failed(uint8_t *file, uint32_t line); #endif #endif /* __STM32_ASSERT_H */ - diff --git a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h index 4087e183c..af665f477 100644 --- a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h +++ b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_hal_conf.h @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_conf.h - * @author MCD Application Team - * @brief HAL configuration file - ****************************************************************************** + ****************************************************************************** + * @file stm32g4xx_hal_conf.h + * @author MCD Application Team + * @brief HAL configuration file + ****************************************************************************** * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + * + * Copyright (c) 2019 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 -------------------------------------*/ @@ -23,7 +23,7 @@ #define STM32G4xx_HAL_CONF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Exported types ------------------------------------------------------------*/ @@ -31,12 +31,12 @@ /* ########################## Module Selection ############################## */ /** - * @brief This is the list of modules to be used in the HAL driver - */ + * @brief This is the list of modules to be used in the HAL driver + */ #define HAL_MODULE_ENABLED - /*#define HAL_ADC_MODULE_ENABLED */ +/*#define HAL_ADC_MODULE_ENABLED */ /*#define HAL_COMP_MODULE_ENABLED */ /*#define HAL_CORDIC_MODULE_ENABLED */ #define HAL_CRC_MODULE_ENABLED @@ -76,122 +76,123 @@ /* ########################## Register Callbacks selection ############################## */ /** - * @brief This is the list of modules where register callback can be used - */ -#define USE_HAL_ADC_REGISTER_CALLBACKS 0U -#define USE_HAL_COMP_REGISTER_CALLBACKS 0U -#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U -#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U -#define USE_HAL_DAC_REGISTER_CALLBACKS 0U -#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U -#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U -#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U -#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U -#define USE_HAL_I2C_REGISTER_CALLBACKS 0U -#define USE_HAL_I2S_REGISTER_CALLBACKS 0U -#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U -#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U -#define USE_HAL_NAND_REGISTER_CALLBACKS 0U -#define USE_HAL_NOR_REGISTER_CALLBACKS 0U -#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U -#define USE_HAL_PCD_REGISTER_CALLBACKS 0U -#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U -#define USE_HAL_RNG_REGISTER_CALLBACKS 0U -#define USE_HAL_RTC_REGISTER_CALLBACKS 0U -#define USE_HAL_SAI_REGISTER_CALLBACKS 0U -#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U -#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U -#define USE_HAL_SPI_REGISTER_CALLBACKS 0U -#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U -#define USE_HAL_TIM_REGISTER_CALLBACKS 0U -#define USE_HAL_UART_REGISTER_CALLBACKS 0U -#define USE_HAL_USART_REGISTER_CALLBACKS 0U -#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U + * @brief This is the list of modules where register callback can be used + */ +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U +#define USE_HAL_COMP_REGISTER_CALLBACKS 0U +#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U +#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U +#define USE_HAL_EXTI_REGISTER_CALLBACKS 0U +#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U +#define USE_HAL_FMAC_REGISTER_CALLBACKS 0U +#define USE_HAL_HRTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U +#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U +#define USE_HAL_NAND_REGISTER_CALLBACKS 0U +#define USE_HAL_NOR_REGISTER_CALLBACKS 0U +#define USE_HAL_OPAMP_REGISTER_CALLBACKS 0U +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U +#define USE_HAL_QSPI_REGISTER_CALLBACKS 0U +#define USE_HAL_RNG_REGISTER_CALLBACKS 0U +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U +#define USE_HAL_SAI_REGISTER_CALLBACKS 0U +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U +#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U +#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U +#define USE_HAL_UART_REGISTER_CALLBACKS 0U +#define USE_HAL_USART_REGISTER_CALLBACKS 0U +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* ########################## Oscillator Values adaptation ####################*/ /** - * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSE is used as system clock source, directly or through the PLL). - */ -#if !defined (HSE_VALUE) - #define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ - -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ -#endif /* HSE_STARTUP_TIMEOUT */ + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined(HSE_VALUE) +#define HSE_VALUE (8000000UL) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined(HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT (100UL) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ /** - * @brief Internal High Speed oscillator (HSI) value. - * This value is used by the RCC HAL module to compute the system frequency - * (when HSI is used as system clock source, directly or through the PLL). - */ -#if !defined (HSI_VALUE) - #define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined(HSI_VALUE) +#define HSI_VALUE (16000000UL) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ /** - * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. - * This internal oscillator is mainly dedicated to provide a high precision clock to - * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. - * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency - * which is subject to manufacturing process variations. - */ -#if !defined (HSI48_VALUE) - #define HSI48_VALUE (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. - The real value my vary depending on manufacturing process variations.*/ -#endif /* HSI48_VALUE */ + * @brief Internal High Speed oscillator (HSI48) value for USB FS and RNG. + * This internal oscillator is mainly dedicated to provide a high precision clock to + * the USB peripheral by means of a special Clock Recovery System (CRS) circuitry. + * When the CRS is not used, the HSI48 RC oscillator runs on it default frequency + * which is subject to manufacturing process variations. + */ +#if !defined(HSI48_VALUE) +#define HSI48_VALUE \ + (48000000UL) /*!< Value of the Internal High Speed oscillator for USB FS/RNG in Hz. \ + The real value my vary depending on manufacturing process variations.*/ +#endif /* HSI48_VALUE */ /** - * @brief Internal Low Speed oscillator (LSI) value. - */ -#if !defined (LSI_VALUE) + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined(LSI_VALUE) /*!< Value of the Internal Low Speed oscillator in Hz The real value may vary depending on the variations in voltage and temperature.*/ -#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ -#endif /* LSI_VALUE */ +#define LSI_VALUE (32000UL) /*!< LSI Typical Value in Hz*/ +#endif /* LSI_VALUE */ /** - * @brief External Low Speed oscillator (LSE) value. - * This value is used by the UART, RTC HAL module to compute the system frequency - */ -#if !defined (LSE_VALUE) -#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined(LSE_VALUE) +#define LSE_VALUE (32768UL) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ -#if !defined (LSE_STARTUP_TIMEOUT) -#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ -#endif /* LSE_STARTUP_TIMEOUT */ +#if !defined(LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT (5000UL) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ /** - * @brief External clock source for I2S and SAI peripherals - * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source - * frequency, this source is inserted directly through I2S_CKIN pad. - */ -#if !defined (EXTERNAL_CLOCK_VALUE) -#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ -#endif /* EXTERNAL_CLOCK_VALUE */ + * @brief External clock source for I2S and SAI peripherals + * This value is used by the I2S and SAI HAL modules to compute the I2S and SAI clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + */ +#if !defined(EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE (12288000UL) /*!< Value of the External oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ /* Tip: To avoid modifying this file each time you need to use different HSE, === you can define the HSE value in your toolchain compiler preprocessor. */ /* ########################### System Configuration ######################### */ /** - * @brief This is the HAL system configuration section - */ + * @brief This is the HAL system configuration section + */ -#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ -#define USE_RTOS 0U -#define PREFETCH_ENABLE 0U -#define INSTRUCTION_CACHE_ENABLE 1U -#define DATA_CACHE_ENABLE 1U +#define VDD_VALUE (3300UL) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY (15UL) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 0U +#define INSTRUCTION_CACHE_ENABLE 1U +#define DATA_CACHE_ENABLE 1U /* ########################## Assert Selection ############################## */ /** - * @brief Uncomment the line below to expanse the "assert_param" macro in the - * HAL drivers code - */ + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ /* #define USE_FULL_ASSERT 1U */ /* ################## SPI peripheral configuration ########################## */ @@ -201,12 +202,12 @@ The real value may vary depending on the variations in voltage and temperature.* * Deactivated: CRC code cleaned from driver */ -#define USE_SPI_CRC 0U +#define USE_SPI_CRC 0U /* Includes ------------------------------------------------------------------*/ /** - * @brief Include module's header file - */ + * @brief Include module's header file + */ #ifdef HAL_RCC_MODULE_ENABLED #include "stm32g4xx_hal_rcc.h" @@ -357,15 +358,15 @@ The real value may vary depending on the variations in voltage and temperature.* #endif /* HAL_WWDG_MODULE_ENABLED */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief The assert_param macro is used for function's parameters check. - * @param expr: If expr is false, it calls assert_failed function - * which reports the name of the source file and the source - * line number of the call that failed. - * If expr is true, it returns no value. - * @retval None - */ + * @brief The assert_param macro is used for function's parameters check. + * @param expr: If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ #define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ void assert_failed(uint8_t *file, uint32_t line); diff --git a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h index e12321bcf..13530cb56 100644 --- a/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h +++ b/SAMM/IMUandTOF/Core/Inc/stm32g4xx_it.h @@ -1,20 +1,20 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_it.h - * @brief This file contains the headers of the interrupt handlers. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 -------------------------------------*/ diff --git a/SAMM/IMUandTOF/Core/Src/adc.c b/SAMM/IMUandTOF/Core/Src/adc.c index 15733df47..fbcfbe933 100644 --- a/SAMM/IMUandTOF/Core/Src/adc.c +++ b/SAMM/IMUandTOF/Core/Src/adc.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file adc.c - * @brief This file provides code for the configuration - * of the ADC instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file adc.c + * @brief This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "adc.h" @@ -30,123 +30,117 @@ ADC_HandleTypeDef hadc1; void MX_ADC1_Init(void) { - /* USER CODE BEGIN ADC1_Init 0 */ - - /* USER CODE END ADC1_Init 0 */ - - ADC_MultiModeTypeDef multimode = {0}; - ADC_ChannelConfTypeDef sConfig = {0}; - - /* USER CODE BEGIN ADC1_Init 1 */ - - /* USER CODE END ADC1_Init 1 */ - - /** Common config - */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.GainCompensation = 0; - hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc1.Init.LowPowerAutoWait = DISABLE; - hadc1.Init.ContinuousConvMode = DISABLE; - hadc1.Init.NbrOfConversion = 1; - hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - hadc1.Init.DMAContinuousRequests = DISABLE; - hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; - hadc1.Init.OversamplingMode = ENABLE; - hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; - hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; - hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; - hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; - if (HAL_ADC_Init(&hadc1) != HAL_OK) - { - Error_Handler(); - } - - /** Configure the ADC multi-mode - */ - multimode.Mode = ADC_MODE_INDEPENDENT; - if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Regular Channel - */ - sConfig.Channel = ADC_CHANNEL_7; - sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; - sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; - sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN ADC1_Init 2 */ - - /* USER CODE END ADC1_Init 2 */ - + /* USER CODE BEGIN ADC1_Init 0 */ + + /* USER CODE END ADC1_Init 0 */ + + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + + /* USER CODE BEGIN ADC1_Init 1 */ + + /* USER CODE END ADC1_Init 1 */ + + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.GainCompensation = 0; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.OversamplingMode = ENABLE; + hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16; + hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_NONE; + hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; + hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_7; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; + sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN ADC1_Init 2 */ + + /* USER CODE END ADC1_Init 2 */ } -void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) +void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspInit 0 */ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspInit 0 */ - /* USER CODE END ADC1_MspInit 0 */ - LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); + /* USER CODE END ADC1_MspInit 0 */ + LL_RCC_SetADCClockSource(LL_RCC_ADC12_CLKSOURCE_SYSCLK); - /* ADC1 clock enable */ - __HAL_RCC_ADC12_CLK_ENABLE(); + /* ADC1 clock enable */ + __HAL_RCC_ADC12_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - /**ADC1 GPIO Configuration - PC0 ------> ADC1_IN6 - PC1 ------> ADC1_IN7 - PC2 ------> ADC1_IN8 - */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2; - GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - /* USER CODE BEGIN ADC1_MspInit 1 */ + /* USER CODE BEGIN ADC1_MspInit 1 */ - /* USER CODE END ADC1_MspInit 1 */ - } + /* USER CODE END ADC1_MspInit 1 */ + } } -void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) +void HAL_ADC_MspDeInit(ADC_HandleTypeDef *adcHandle) { - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspDeInit 0 */ + if (adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ - /* USER CODE END ADC1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_ADC12_CLK_DISABLE(); + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_ADC12_CLK_DISABLE(); - /**ADC1 GPIO Configuration - PC0 ------> ADC1_IN6 - PC1 ------> ADC1_IN7 - PC2 ------> ADC1_IN8 - */ - HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2); + /**ADC1 GPIO Configuration + PC0 ------> ADC1_IN6 + PC1 ------> ADC1_IN7 + PC2 ------> ADC1_IN8 + */ + HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2); - /* USER CODE BEGIN ADC1_MspDeInit 1 */ + /* USER CODE BEGIN ADC1_MspDeInit 1 */ - /* USER CODE END ADC1_MspDeInit 1 */ - } + /* USER CODE END ADC1_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/bmi323.c b/SAMM/IMUandTOF/Core/Src/bmi323.c index 125342eb4..30d292042 100644 --- a/SAMM/IMUandTOF/Core/Src/bmi323.c +++ b/SAMM/IMUandTOF/Core/Src/bmi323.c @@ -1,61 +1,64 @@ #include "bmi323.h" + #include -#include "stm32g4xx_hal_spi.h" + #include "stm32g474xx.h" +#include "stm32g4xx_hal_spi.h" -//init spi port before calling this function -uint8_t bmi323_init(bmi323 *bmi323_dev, SPI_HandleTypeDef *spi_port, GPIO_TypeDef *port, uint16_t pin){ - uint8_t tx_word[4]; - uint8_t rx_word[4] = {0}; - uint8_t status = 0; - bmi323_dev->spi_port = spi_port; - bmi323_dev->port = port; - bmi323_dev->pin = pin; - tx_word[1] = (BMI323_CHIP_ID << 8); - tx_word[1] |= 0x80; - tx_word[0] = 0x69; - /* - Okay so for one of these transmits we need to follow the following operation:bmi323_dev - 1. to read the register we want to: - transmit first 8 bytes, then transmit a fake 8 bytes - after we want to read 16 bytes. This should complete a single read - */ - //first we read do the dummy read to switch to spi mode - HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); - status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); - HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); - // rx_word = 0; - HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); - status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); - HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); - if(rx_word[3] == 0x43){ - return HAL_OK; - } - return HAL_ERROR; +// init spi port before calling this function +uint8_t bmi323_init(bmi323 *bmi323_dev, SPI_HandleTypeDef *spi_port, GPIO_TypeDef *port, uint16_t pin) +{ + uint8_t tx_word[4]; + uint8_t rx_word[4] = {0}; + uint8_t status = 0; + bmi323_dev->spi_port = spi_port; + bmi323_dev->port = port; + bmi323_dev->pin = pin; + tx_word[1] = (BMI323_CHIP_ID << 8); + tx_word[1] |= 0x80; + tx_word[0] = 0x69; + /* + Okay so for one of these transmits we need to follow the following operation:bmi323_dev + 1. to read the register we want to: + transmit first 8 bytes, then transmit a fake 8 bytes + after we want to read 16 bytes. This should complete a single read + */ + // first we read do the dummy read to switch to spi mode + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); + status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); + // rx_word = 0; + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_RESET); + status = HAL_SPI_TransmitReceive(bmi323_dev->spi_port, tx_word, rx_word, 2, HAL_MAX_DELAY); + HAL_GPIO_WritePin(bmi323_dev->port, bmi323_dev->pin, GPIO_PIN_SET); + if (rx_word[3] == 0x43) { + return HAL_OK; + } + return HAL_ERROR; } - - /* TODO: VIN DO THESE FUNCYIONS */ -uint16_t bmi323_read(bmi323 *bmi323_dev, uint8_t reg){ - uint16_t data; - // i2c_read_single(BMI323_I2C_ADDR, reg, &data, bmi323_dev->i2c_port); - return data; +uint16_t bmi323_read(bmi323 *bmi323_dev, uint8_t reg) +{ + uint16_t data; + // i2c_read_single(BMI323_I2C_ADDR, reg, &data, bmi323_dev->i2c_port); + return data; } -uint8_t bmi323_write(bmi323 *bmi323_dev, uint8_t reg, uint16_t data){ - // i2c_write(BMI323_I2C_ADDR, reg, data, bmi323_dev->i2c_port); - return 1; +uint8_t bmi323_write(bmi323 *bmi323_dev, uint8_t reg, uint16_t data) +{ + // i2c_write(BMI323_I2C_ADDR, reg, data, bmi323_dev->i2c_port); + return 1; } -uint8_t bmi323_soft_reset(bmi323 *bmi323_dev){ - bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_SOFT_RESET); - return 1; +uint8_t bmi323_soft_reset(bmi323 *bmi323_dev) +{ + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_SOFT_RESET); + return 1; } - /* CALIBRATION PROCEDURE. VERY DANGEROUS. ONLY USE IF YOU KNOW WHAT YOU ARE DOING THIS FUNCTION IS BLOCKING, CAN TAKE UP TO 3 SECONDS TO TIMEOUT @@ -72,130 +75,130 @@ THE FUNCTION WILL RETURN 1 IF CALIBRATION WAS SUCCESSFUL, 0 IF NOT 8. If successful, reset the accelerometer and gyro to their original configuration 9. Done */ -uint8_t bmi323_calib(bmi323 *bmi323_dev){ - //first we have to fucking start the feature engine - //to do that we have to disable all sensors - uint16_t acc_conf = bmi323_read(bmi323_dev, BMI323_ACC_CONF); - uint16_t gyro_conf = bmi323_read(bmi323_dev, BMI323_GYR_CONF); - uint32_t timeout_ref = 0; - if((acc_conf & 0x7000) != 0b000){ - bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); - } - if((gyro_conf & 0x7000) != 0b000){ - bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); - } - //now we need to check if the feature engine has been enabled prior - if((bmi323_read(bmi323_dev, BMI323_FEATURE_CTRL) & 0x0001) == 0b0){ - //then we write 0x012C to Feature Io 2 - bmi323_write(bmi323_dev, BMI323_FEATURE_IO2, BMI323_FEATURE_IO2_EN); - //then we write 0x0001 to Feature Io status - bmi323_write(bmi323_dev, BMI323_FEATURE_IO_STATUS, BMI323_FEATURE_IO_STS); - //now we set the feature ctrol engine enable to 1 - bmi323_write(bmi323_dev, BMI323_FEATURE_CTRL, BMI323_FEATURE_CTRL_ENGINE_EN); - //now we poll the feature engine - timeout_ref = millis(); - while((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x000F) != 0b001){ - if(millis() - timeout_ref > BMI323_TIMEOUT){ - D_println("Feature engine enable timeout"); - return 0; - } - continue; - } - D_println("enabled feature engine Hooray!"); - } - //no need for an else statement as the feature engine is already enabled and we can continue - //now we need to check if a calibration is already in progress - //polls the bmi323 FEATURE I01 state untill the state is 0b00, this means calibration can start. - timeout_ref = millis(); - while(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & (0x1800)) >> 11) != 0b00){ - if(millis() - timeout_ref > BMI323_TIMEOUT){ - D_println("Calibration state timeout"); - return 0; - } - continue; - } - //check the current configuration of the accelerometer and make sure it is in high performance adn - //ODR is between 25 and 200 hertz - calibrate: - //read the acc conf register - uint16_t data = bmi323_read(bmi323_dev, BMI323_ACC_CONF) & 0x700F; - //checks if we are in the correct configuration. if not skips if statement - if(((data >> 12) == HIGH_PERF) && (((data & 0x000F) >= ODR_25) && ((data & 0x000F) <= ODR_200))){ - D_println("Starting calibration"); - //if it is, then we can start the calibration - //first we check if the alernalte configuration acc mode is set to 0: - if(((bmi323_read(bmi323_dev, BMI323_ALT_ACC_CONF) & 0x7000)>>11) != 0b000){ - //if it is, we set it to 0 - bmi323_write(bmi323_dev, BMI323_ALT_ACC_CONF, 0x0206); - } - //next we check if the alternate configuration gyro mode is set to 0: - if(((bmi323_read(bmi323_dev, BMI323_ALT_GYR_CONF) & 0x7000)>>11) != 0b000){ - //if it is, we set it to 0 - bmi323_write(bmi323_dev, BMI323_ALT_GYR_CONF, 0x0206); - } - D_println("Alternate configurations set"); - //next we can actually send the command for calibration - //reset all of the gyro calibration values - bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_X, BMI323_ACC_DP_DGAIN_X_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Y, BMI323_ACC_DP_DGAIN_Y_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Z, BMI323_ACC_DP_DGAIN_Z_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_X, BMI323_ACC_DP_OFF_X_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Y, BMI323_ACC_DP_OFF_Y_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Z, BMI323_ACC_DP_OFF_Z_RESET_VAL); - //now we poll the state of the calibration untill we get 0b1 - D_println("Starting calibration"); - bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB); - - D_println("Polling calibration state"); - //check if the feature engine is enabled - timeout_ref = millis(); - while(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0010) >> 4 ) != 0b1){ - if(millis() - timeout_ref > BMI323_TIMEOUT){ - D_println("Feature engine enable timeout"); - return 0; - } - continue; - } - D_println("Calibration complete"); - if(((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0020) >> 5) == 0b1){ - // D_println("Calibration successful"); - // D_println("reseting values to original configuration"); - //cycle the acc - bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); - bmi323_write(bmi323_dev, BMI323_ACC_CONF, acc_conf); - //cycle the gyro - bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); - // D_println(gyro_conf); - bmi323_write(bmi323_dev, BMI323_GYR_CONF, gyro_conf); - //display the calibration values - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_X), HEX); - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Y), HEX); - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Z), HEX); - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_X), HEX); - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Y), HEX); - // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Z), HEX); - return 1; - } - else{ - // D_println("Calibration failed"); - return 0; - } +uint8_t bmi323_calib(bmi323 *bmi323_dev) +{ + // first we have to fucking start the feature engine + // to do that we have to disable all sensors + uint16_t acc_conf = bmi323_read(bmi323_dev, BMI323_ACC_CONF); + uint16_t gyro_conf = bmi323_read(bmi323_dev, BMI323_GYR_CONF); + uint32_t timeout_ref = 0; + if ((acc_conf & 0x7000) != 0b000) { + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + } + if ((gyro_conf & 0x7000) != 0b000) { + bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); + } + // now we need to check if the feature engine has been enabled prior + if ((bmi323_read(bmi323_dev, BMI323_FEATURE_CTRL) & 0x0001) == 0b0) { + // then we write 0x012C to Feature Io 2 + bmi323_write(bmi323_dev, BMI323_FEATURE_IO2, BMI323_FEATURE_IO2_EN); + // then we write 0x0001 to Feature Io status + bmi323_write(bmi323_dev, BMI323_FEATURE_IO_STATUS, BMI323_FEATURE_IO_STS); + // now we set the feature ctrol engine enable to 1 + bmi323_write(bmi323_dev, BMI323_FEATURE_CTRL, BMI323_FEATURE_CTRL_ENGINE_EN); + // now we poll the feature engine + timeout_ref = millis(); + while ((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x000F) != 0b001) { + if (millis() - timeout_ref > BMI323_TIMEOUT) { + D_println("Feature engine enable timeout"); + return 0; + } + continue; + } + D_println("enabled feature engine Hooray!"); + } + // no need for an else statement as the feature engine is already enabled and we can continue + // now we need to check if a calibration is already in progress + // polls the bmi323 FEATURE I01 state untill the state is 0b00, this means calibration can start. + timeout_ref = millis(); + while (((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & (0x1800)) >> 11) != 0b00) { + if (millis() - timeout_ref > BMI323_TIMEOUT) { + D_println("Calibration state timeout"); + return 0; + } + continue; + } +// check the current configuration of the accelerometer and make sure it is in high performance adn +// ODR is between 25 and 200 hertz +calibrate: + // read the acc conf register + uint16_t data = bmi323_read(bmi323_dev, BMI323_ACC_CONF) & 0x700F; + // checks if we are in the correct configuration. if not skips if statement + if (((data >> 12) == HIGH_PERF) && (((data & 0x000F) >= ODR_25) && ((data & 0x000F) <= ODR_200))) { + D_println("Starting calibration"); + // if it is, then we can start the calibration + // first we check if the alernalte configuration acc mode is set to 0: + if (((bmi323_read(bmi323_dev, BMI323_ALT_ACC_CONF) & 0x7000) >> 11) != 0b000) { + // if it is, we set it to 0 + bmi323_write(bmi323_dev, BMI323_ALT_ACC_CONF, 0x0206); + } + // next we check if the alternate configuration gyro mode is set to 0: + if (((bmi323_read(bmi323_dev, BMI323_ALT_GYR_CONF) & 0x7000) >> 11) != 0b000) { + // if it is, we set it to 0 + bmi323_write(bmi323_dev, BMI323_ALT_GYR_CONF, 0x0206); + } + D_println("Alternate configurations set"); + // next we can actually send the command for calibration + // reset all of the gyro calibration values + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_X, BMI323_ACC_DP_DGAIN_X_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Y, BMI323_ACC_DP_DGAIN_Y_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_DGAIN_Z, BMI323_ACC_DP_DGAIN_Z_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_X, BMI323_ACC_DP_OFF_X_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Y, BMI323_ACC_DP_OFF_Y_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_GYR_DP_OFF_Z, BMI323_ACC_DP_OFF_Z_RESET_VAL); + // now we poll the state of the calibration untill we get 0b1 + D_println("Starting calibration"); + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB); - } - // D_println("Calibration failed"); - // D_println("reseting values trying again"); - //turns off the acc - bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); - //turn on the acc - bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL | 0x7000); - //jumps to the calibration sequence - goto calibrate; - return 1; + D_println("Polling calibration state"); + // check if the feature engine is enabled + timeout_ref = millis(); + while (((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0010) >> 4) != 0b1) { + if (millis() - timeout_ref > BMI323_TIMEOUT) { + D_println("Feature engine enable timeout"); + return 0; + } + continue; + } + D_println("Calibration complete"); + if (((bmi323_read(bmi323_dev, BMI323_FEATURE_I01) & 0x0020) >> 5) == 0b1) { + // D_println("Calibration successful"); + // D_println("reseting values to original configuration"); + // cycle the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + bmi323_write(bmi323_dev, BMI323_ACC_CONF, acc_conf); + // cycle the gyro + bmi323_write(bmi323_dev, BMI323_GYR_CONF, BMI323_GYR_CONF_RESET_VAL); + // D_println(gyro_conf); + bmi323_write(bmi323_dev, BMI323_GYR_CONF, gyro_conf); + // display the calibration values + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_X), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Y), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_DGAIN_Z), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_X), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Y), HEX); + // D_println(bmi323_read(bmi323_dev, BMI323_ACC_DP_OFF_Z), HEX); + return 1; + } else { + // D_println("Calibration failed"); + return 0; + } + } + // D_println("Calibration failed"); + // D_println("reseting values trying again"); + // turns off the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL); + // turn on the acc + bmi323_write(bmi323_dev, BMI323_ACC_CONF, BMI323_ACC_CONF_RESET_VAL | 0x7000); + // jumps to the calibration sequence + goto calibrate; + return 1; } -uint8_t bmi323_calib_abort(bmi323 *bmi323_dev){ - bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB_ABORT); - return 1; +uint8_t bmi323_calib_abort(bmi323 *bmi323_dev) +{ + bmi323_write(bmi323_dev, BMI323_CMD, BMI323_CMD_CALIB_ABORT); + return 1; } /* @@ -207,77 +210,88 @@ Check the status of the acc, gyro and temp before returning the values */ -uint16_t bmi323_read_acc_x(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_ACC_X); +uint16_t bmi323_read_acc_x(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_ACC_X); } -uint16_t bmi323_read_acc_y(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_ACC_Y); +uint16_t bmi323_read_acc_y(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_ACC_Y); } -uint16_t bmi323_read_acc_z(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_ACC_Z); +uint16_t bmi323_read_acc_z(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_ACC_Z); } -uint16_t bmi323_read_gyr_x(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_GYR_X); +uint16_t bmi323_read_gyr_x(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_GYR_X); } -uint16_t bmi323_read_gyr_y(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_GYR_Y); +uint16_t bmi323_read_gyr_y(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_GYR_Y); } -uint16_t bmi323_read_gyr_z(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_GYR_Z); +uint16_t bmi323_read_gyr_z(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_GYR_Z); } -uint16_t bmi323_read_temp_data(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_TEMP_DATA); +uint16_t bmi323_read_temp_data(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_TEMP_DATA); } -uint16_t bmi323_read_status(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_STATUS); +uint16_t bmi323_read_status(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_STATUS); } -uint16_t bmi323_read_err_reg(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_ERR_REG); +uint16_t bmi323_read_err_reg(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_ERR_REG); } -uint16_t bmi323_read_chip_id(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_CHIP_ID); +uint16_t bmi323_read_chip_id(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_CHIP_ID); } -uint16_t bmi323_read_acc_conf(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_ACC_CONF); +uint16_t bmi323_read_acc_conf(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_ACC_CONF); } -uint16_t bmi323_read_gyr_conf(bmi323 *bmi323_dev){ - return bmi323_read(bmi323_dev, BMI323_GYR_CONF); +uint16_t bmi323_read_gyr_conf(bmi323 *bmi323_dev) +{ + return bmi323_read(bmi323_dev, BMI323_GYR_CONF); } -uint8_t bmi323_enable_acc(bmi323 *bmi323_dev, uint8_t acc_mode, uint8_t acc_avg_num, uint8_t acc_bw, uint8_t acc_range, uint8_t acc_odr){ - //uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); - uint16_t new_conf = 0; - new_conf |= acc_mode << 12; - new_conf |= acc_avg_num << 8; - new_conf |= acc_bw << 7; - new_conf |= acc_range << 4; - new_conf |= acc_odr; - bmi323_write(bmi323_dev, BMI323_ACC_CONF, new_conf); - return 1; +uint8_t bmi323_enable_acc(bmi323 *bmi323_dev, uint8_t acc_mode, uint8_t acc_avg_num, uint8_t acc_bw, uint8_t acc_range, uint8_t acc_odr) +{ + // uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); + uint16_t new_conf = 0; + new_conf |= acc_mode << 12; + new_conf |= acc_avg_num << 8; + new_conf |= acc_bw << 7; + new_conf |= acc_range << 4; + new_conf |= acc_odr; + bmi323_write(bmi323_dev, BMI323_ACC_CONF, new_conf); + return 1; } -uint8_t bmi323_enable_gyro(bmi323 *bmi323_dev, uint8_t gyr_mode, uint8_t gyr_avg_num, uint8_t gyr_bw, uint8_t gyr_range, uint8_t gyr_odr){ - //uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); - uint16_t new_conf = 0; - new_conf |= gyr_mode << 12; - new_conf |= gyr_avg_num << 8; - new_conf |= gyr_bw << 7; - new_conf |= gyr_range << 4; - new_conf |= gyr_odr; - bmi323_write(bmi323_dev, BMI323_GYR_CONF, new_conf); - return 1; +uint8_t bmi323_enable_gyro(bmi323 *bmi323_dev, uint8_t gyr_mode, uint8_t gyr_avg_num, uint8_t gyr_bw, uint8_t gyr_range, uint8_t gyr_odr) +{ + // uint16_t acc_conf = bmi323_read_acc_conf(bmi323_dev); + uint16_t new_conf = 0; + new_conf |= gyr_mode << 12; + new_conf |= gyr_avg_num << 8; + new_conf |= gyr_bw << 7; + new_conf |= gyr_range << 4; + new_conf |= gyr_odr; + bmi323_write(bmi323_dev, BMI323_GYR_CONF, new_conf); + return 1; } - - - diff --git a/SAMM/IMUandTOF/Core/Src/crc.c b/SAMM/IMUandTOF/Core/Src/crc.c index 0a8907607..213510a5d 100644 --- a/SAMM/IMUandTOF/Core/Src/crc.c +++ b/SAMM/IMUandTOF/Core/Src/crc.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file crc.c - * @brief This file provides code for the configuration - * of the CRC instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file crc.c + * @brief This file provides code for the configuration + * of the CRC instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "crc.h" @@ -30,59 +30,55 @@ CRC_HandleTypeDef hcrc; void MX_CRC_Init(void) { - /* USER CODE BEGIN CRC_Init 0 */ + /* USER CODE BEGIN CRC_Init 0 */ - /* USER CODE END CRC_Init 0 */ + /* USER CODE END CRC_Init 0 */ - /* USER CODE BEGIN CRC_Init 1 */ + /* USER CODE BEGIN CRC_Init 1 */ - /* USER CODE END CRC_Init 1 */ - hcrc.Instance = CRC; - hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; - hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; - hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; - hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; - hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; - if (HAL_CRC_Init(&hcrc) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN CRC_Init 2 */ - - /* USER CODE END CRC_Init 2 */ + /* USER CODE END CRC_Init 1 */ + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; + if (HAL_CRC_Init(&hcrc) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN CRC_Init 2 */ + /* USER CODE END CRC_Init 2 */ } -void HAL_CRC_MspInit(CRC_HandleTypeDef* crcHandle) +void HAL_CRC_MspInit(CRC_HandleTypeDef *crcHandle) { - if(crcHandle->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspInit 0 */ + if (crcHandle->Instance == CRC) { + /* USER CODE BEGIN CRC_MspInit 0 */ - /* USER CODE END CRC_MspInit 0 */ - /* CRC clock enable */ - __HAL_RCC_CRC_CLK_ENABLE(); - /* USER CODE BEGIN CRC_MspInit 1 */ + /* USER CODE END CRC_MspInit 0 */ + /* CRC clock enable */ + __HAL_RCC_CRC_CLK_ENABLE(); + /* USER CODE BEGIN CRC_MspInit 1 */ - /* USER CODE END CRC_MspInit 1 */ - } + /* USER CODE END CRC_MspInit 1 */ + } } -void HAL_CRC_MspDeInit(CRC_HandleTypeDef* crcHandle) +void HAL_CRC_MspDeInit(CRC_HandleTypeDef *crcHandle) { - if(crcHandle->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspDeInit 0 */ + if (crcHandle->Instance == CRC) { + /* USER CODE BEGIN CRC_MspDeInit 0 */ - /* USER CODE END CRC_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_CRC_CLK_DISABLE(); - /* USER CODE BEGIN CRC_MspDeInit 1 */ + /* USER CODE END CRC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_CRC_CLK_DISABLE(); + /* USER CODE BEGIN CRC_MspDeInit 1 */ - /* USER CODE END CRC_MspDeInit 1 */ - } + /* USER CODE END CRC_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/dma.c b/SAMM/IMUandTOF/Core/Src/dma.c index 372b7e92c..0b82d0d7c 100644 --- a/SAMM/IMUandTOF/Core/Src/dma.c +++ b/SAMM/IMUandTOF/Core/Src/dma.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file dma.c - * @brief This file provides code for the configuration - * of all the requested memory to memory DMA transfers. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file dma.c + * @brief This file provides code for the configuration + * of all the requested memory to memory DMA transfers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -34,26 +34,24 @@ /* USER CODE END 1 */ /** - * Enable DMA controller clock - */ + * Enable DMA controller clock + */ void MX_DMA_Init(void) { - /* DMA controller clock enable */ - __HAL_RCC_DMAMUX1_CLK_ENABLE(); - __HAL_RCC_DMA1_CLK_ENABLE(); - - /* DMA interrupt init */ - /* DMA1_Channel1_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); - /* DMA1_Channel2_IRQn interrupt configuration */ - HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); - + /* DMA controller clock enable */ + __HAL_RCC_DMAMUX1_CLK_ENABLE(); + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Channel1_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + /* DMA1_Channel2_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); } /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ - diff --git a/SAMM/IMUandTOF/Core/Src/fdcan.c b/SAMM/IMUandTOF/Core/Src/fdcan.c index 59cf13f59..600df963c 100644 --- a/SAMM/IMUandTOF/Core/Src/fdcan.c +++ b/SAMM/IMUandTOF/Core/Src/fdcan.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file fdcan.c - * @brief This file provides code for the configuration - * of the FDCAN instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file fdcan.c + * @brief This file provides code for the configuration + * of the FDCAN instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "fdcan.h" @@ -31,191 +31,181 @@ FDCAN_HandleTypeDef hfdcan2; void MX_FDCAN1_Init(void) { - /* USER CODE BEGIN FDCAN1_Init 0 */ - - /* USER CODE END FDCAN1_Init 0 */ - - /* USER CODE BEGIN FDCAN1_Init 1 */ - - /* USER CODE END FDCAN1_Init 1 */ - hfdcan1.Instance = FDCAN1; - hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; - hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; - hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; - hfdcan1.Init.AutoRetransmission = DISABLE; - hfdcan1.Init.TransmitPause = DISABLE; - hfdcan1.Init.ProtocolException = DISABLE; - hfdcan1.Init.NominalPrescaler = 16; - hfdcan1.Init.NominalSyncJumpWidth = 1; - hfdcan1.Init.NominalTimeSeg1 = 1; - hfdcan1.Init.NominalTimeSeg2 = 1; - hfdcan1.Init.DataPrescaler = 1; - hfdcan1.Init.DataSyncJumpWidth = 1; - hfdcan1.Init.DataTimeSeg1 = 1; - hfdcan1.Init.DataTimeSeg2 = 1; - hfdcan1.Init.StdFiltersNbr = 0; - hfdcan1.Init.ExtFiltersNbr = 0; - hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; - if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN FDCAN1_Init 2 */ - - /* USER CODE END FDCAN1_Init 2 */ - + /* USER CODE BEGIN FDCAN1_Init 0 */ + + /* USER CODE END FDCAN1_Init 0 */ + + /* USER CODE BEGIN FDCAN1_Init 1 */ + + /* USER CODE END FDCAN1_Init 1 */ + hfdcan1.Instance = FDCAN1; + hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan1.Init.AutoRetransmission = DISABLE; + hfdcan1.Init.TransmitPause = DISABLE; + hfdcan1.Init.ProtocolException = DISABLE; + hfdcan1.Init.NominalPrescaler = 16; + hfdcan1.Init.NominalSyncJumpWidth = 1; + hfdcan1.Init.NominalTimeSeg1 = 1; + hfdcan1.Init.NominalTimeSeg2 = 1; + hfdcan1.Init.DataPrescaler = 1; + hfdcan1.Init.DataSyncJumpWidth = 1; + hfdcan1.Init.DataTimeSeg1 = 1; + hfdcan1.Init.DataTimeSeg2 = 1; + hfdcan1.Init.StdFiltersNbr = 0; + hfdcan1.Init.ExtFiltersNbr = 0; + hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN1_Init 2 */ + + /* USER CODE END FDCAN1_Init 2 */ } /* FDCAN2 init function */ void MX_FDCAN2_Init(void) { - /* USER CODE BEGIN FDCAN2_Init 0 */ - - /* USER CODE END FDCAN2_Init 0 */ - - /* USER CODE BEGIN FDCAN2_Init 1 */ - - /* USER CODE END FDCAN2_Init 1 */ - hfdcan2.Instance = FDCAN2; - hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; - hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; - hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; - hfdcan2.Init.AutoRetransmission = DISABLE; - hfdcan2.Init.TransmitPause = DISABLE; - hfdcan2.Init.ProtocolException = DISABLE; - hfdcan2.Init.NominalPrescaler = 16; - hfdcan2.Init.NominalSyncJumpWidth = 1; - hfdcan2.Init.NominalTimeSeg1 = 1; - hfdcan2.Init.NominalTimeSeg2 = 1; - hfdcan2.Init.DataPrescaler = 1; - hfdcan2.Init.DataSyncJumpWidth = 1; - hfdcan2.Init.DataTimeSeg1 = 1; - hfdcan2.Init.DataTimeSeg2 = 1; - hfdcan2.Init.StdFiltersNbr = 0; - hfdcan2.Init.ExtFiltersNbr = 0; - hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; - if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN FDCAN2_Init 2 */ - - /* USER CODE END FDCAN2_Init 2 */ - + /* USER CODE BEGIN FDCAN2_Init 0 */ + + /* USER CODE END FDCAN2_Init 0 */ + + /* USER CODE BEGIN FDCAN2_Init 1 */ + + /* USER CODE END FDCAN2_Init 1 */ + hfdcan2.Instance = FDCAN2; + hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1; + hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC; + hfdcan2.Init.Mode = FDCAN_MODE_NORMAL; + hfdcan2.Init.AutoRetransmission = DISABLE; + hfdcan2.Init.TransmitPause = DISABLE; + hfdcan2.Init.ProtocolException = DISABLE; + hfdcan2.Init.NominalPrescaler = 16; + hfdcan2.Init.NominalSyncJumpWidth = 1; + hfdcan2.Init.NominalTimeSeg1 = 1; + hfdcan2.Init.NominalTimeSeg2 = 1; + hfdcan2.Init.DataPrescaler = 1; + hfdcan2.Init.DataSyncJumpWidth = 1; + hfdcan2.Init.DataTimeSeg1 = 1; + hfdcan2.Init.DataTimeSeg2 = 1; + hfdcan2.Init.StdFiltersNbr = 0; + hfdcan2.Init.ExtFiltersNbr = 0; + hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; + if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN FDCAN2_Init 2 */ + + /* USER CODE END FDCAN2_Init 2 */ } -static uint32_t HAL_RCC_FDCAN_CLK_ENABLED=0; +static uint32_t HAL_RCC_FDCAN_CLK_ENABLED = 0; -void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle) +void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef *fdcanHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(fdcanHandle->Instance==FDCAN1) - { - /* USER CODE BEGIN FDCAN1_MspInit 0 */ - - /* USER CODE END FDCAN1_MspInit 0 */ - LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); - - /* FDCAN1 clock enable */ - HAL_RCC_FDCAN_CLK_ENABLED++; - if(HAL_RCC_FDCAN_CLK_ENABLED==1){ - __HAL_RCC_FDCAN_CLK_ENABLE(); - } - - __HAL_RCC_GPIOA_CLK_ENABLE(); - /**FDCAN1 GPIO Configuration - PA11 ------> FDCAN1_RX - PA12 ------> FDCAN1_TX - */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN FDCAN1_MspInit 1 */ - - /* USER CODE END FDCAN1_MspInit 1 */ - } - else if(fdcanHandle->Instance==FDCAN2) - { - /* USER CODE BEGIN FDCAN2_MspInit 0 */ - - /* USER CODE END FDCAN2_MspInit 0 */ - - LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); - - /* FDCAN2 clock enable */ - HAL_RCC_FDCAN_CLK_ENABLED++; - if(HAL_RCC_FDCAN_CLK_ENABLED==1){ - __HAL_RCC_FDCAN_CLK_ENABLE(); - } - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**FDCAN2 GPIO Configuration - PB13 ------> FDCAN2_TX - PB5 ------> FDCAN2_RX - */ - GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* USER CODE BEGIN FDCAN2_MspInit 1 */ - - /* USER CODE END FDCAN2_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (fdcanHandle->Instance == FDCAN1) { + /* USER CODE BEGIN FDCAN1_MspInit 0 */ + + /* USER CODE END FDCAN1_MspInit 0 */ + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN1 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if (HAL_RCC_FDCAN_CLK_ENABLED == 1) { + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOA_CLK_ENABLE(); + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN1_MspInit 1 */ + + /* USER CODE END FDCAN1_MspInit 1 */ + } else if (fdcanHandle->Instance == FDCAN2) { + /* USER CODE BEGIN FDCAN2_MspInit 0 */ + + /* USER CODE END FDCAN2_MspInit 0 */ + + LL_RCC_SetFDCANClockSource(LL_RCC_FDCAN_CLKSOURCE_PCLK1); + + /* FDCAN2 clock enable */ + HAL_RCC_FDCAN_CLK_ENABLED++; + if (HAL_RCC_FDCAN_CLK_ENABLED == 1) { + __HAL_RCC_FDCAN_CLK_ENABLE(); + } + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN FDCAN2_MspInit 1 */ + + /* USER CODE END FDCAN2_MspInit 1 */ + } } -void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle) +void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef *fdcanHandle) { - if(fdcanHandle->Instance==FDCAN1) - { - /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ - - /* USER CODE END FDCAN1_MspDeInit 0 */ - /* Peripheral clock disable */ - HAL_RCC_FDCAN_CLK_ENABLED--; - if(HAL_RCC_FDCAN_CLK_ENABLED==0){ - __HAL_RCC_FDCAN_CLK_DISABLE(); - } - - /**FDCAN1 GPIO Configuration - PA11 ------> FDCAN1_RX - PA12 ------> FDCAN1_TX - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); - - /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ - - /* USER CODE END FDCAN1_MspDeInit 1 */ - } - else if(fdcanHandle->Instance==FDCAN2) - { - /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ - - /* USER CODE END FDCAN2_MspDeInit 0 */ - /* Peripheral clock disable */ - HAL_RCC_FDCAN_CLK_ENABLED--; - if(HAL_RCC_FDCAN_CLK_ENABLED==0){ - __HAL_RCC_FDCAN_CLK_DISABLE(); - } - - /**FDCAN2 GPIO Configuration - PB13 ------> FDCAN2_TX - PB5 ------> FDCAN2_RX - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_5); - - /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ - - /* USER CODE END FDCAN2_MspDeInit 1 */ - } + if (fdcanHandle->Instance == FDCAN1) { + /* USER CODE BEGIN FDCAN1_MspDeInit 0 */ + + /* USER CODE END FDCAN1_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if (HAL_RCC_FDCAN_CLK_ENABLED == 0) { + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN1 GPIO Configuration + PA11 ------> FDCAN1_RX + PA12 ------> FDCAN1_TX + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12); + + /* USER CODE BEGIN FDCAN1_MspDeInit 1 */ + + /* USER CODE END FDCAN1_MspDeInit 1 */ + } else if (fdcanHandle->Instance == FDCAN2) { + /* USER CODE BEGIN FDCAN2_MspDeInit 0 */ + + /* USER CODE END FDCAN2_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_FDCAN_CLK_ENABLED--; + if (HAL_RCC_FDCAN_CLK_ENABLED == 0) { + __HAL_RCC_FDCAN_CLK_DISABLE(); + } + + /**FDCAN2 GPIO Configuration + PB13 ------> FDCAN2_TX + PB5 ------> FDCAN2_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13 | GPIO_PIN_5); + + /* USER CODE BEGIN FDCAN2_MspDeInit 1 */ + + /* USER CODE END FDCAN2_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/gpio.c b/SAMM/IMUandTOF/Core/Src/gpio.c index ab7f2d3bf..bd5e1df5f 100644 --- a/SAMM/IMUandTOF/Core/Src/gpio.c +++ b/SAMM/IMUandTOF/Core/Src/gpio.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file gpio.c - * @brief This file provides code for the configuration - * of all used GPIO pins. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -30,63 +30,61 @@ /*----------------------------------------------------------------------------*/ /* USER CODE BEGIN 1 */ - /* USER CODE END 1 */ /** Configure pins as - * Analog - * Input - * Output - * EVENT_OUT - * EXTI + * Analog + * Input + * Output + * EVENT_OUT + * EXTI PC8 ------> I2C3_SCL PC9 ------> I2C3_SDA */ void MX_GPIO_Init(void) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOF_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOA_CLK_ENABLE(); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); - - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1|GPIO_PIN_4, GPIO_PIN_RESET); - - /*Configure GPIO pin : PF1 */ - GPIO_InitStruct.Pin = GPIO_PIN_1; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - - /*Configure GPIO pins : PB1 PB4 */ - GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pins : PB12 PB6 */ - GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_6; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /*Configure GPIO pins : PC8 PC9 */ - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); + + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1 | GPIO_PIN_4, GPIO_PIN_RESET); + + /*Configure GPIO pin : PF1 */ + GPIO_InitStruct.Pin = GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); + + /*Configure GPIO pins : PB1 PB4 */ + GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : PB12 PB6 */ + GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pins : PC8 PC9 */ + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF8_I2C3; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); } /* USER CODE BEGIN 2 */ diff --git a/SAMM/IMUandTOF/Core/Src/i2c.c b/SAMM/IMUandTOF/Core/Src/i2c.c index 8912dcd02..ec88833f4 100644 --- a/SAMM/IMUandTOF/Core/Src/i2c.c +++ b/SAMM/IMUandTOF/Core/Src/i2c.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file i2c.c - * @brief This file provides code for the configuration - * of the I2C instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file i2c.c + * @brief This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "i2c.h" @@ -30,104 +30,98 @@ I2C_HandleTypeDef hi2c1; void MX_I2C1_Init(void) { - /* USER CODE BEGIN I2C1_Init 0 */ - - /* USER CODE END I2C1_Init 0 */ - - /* USER CODE BEGIN I2C1_Init 1 */ - - /* USER CODE END I2C1_Init 1 */ - hi2c1.Instance = I2C1; - hi2c1.Init.Timing = 0x10920C1D; - hi2c1.Init.OwnAddress1 = 0; - hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - hi2c1.Init.OwnAddress2 = 0; - hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; - hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if (HAL_I2C_Init(&hi2c1) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Analogue filter - */ - if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) - { - Error_Handler(); - } - - /** Configure Digital filter - */ - if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) - { - Error_Handler(); - } - - /** I2C Fast mode Plus enable - */ - HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); - /* USER CODE BEGIN I2C1_Init 2 */ - - /* USER CODE END I2C1_Init 2 */ - + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.Timing = 0x10920C1D; + hi2c1.Init.OwnAddress1 = 0; + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; + hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) { + Error_Handler(); + } + + /** Configure Analogue filter + */ + if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK) { + Error_Handler(); + } + + /** Configure Digital filter + */ + if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK) { + Error_Handler(); + } + + /** I2C Fast mode Plus enable + */ + HAL_I2CEx_EnableFastModePlus(I2C_FASTMODEPLUS_I2C1); + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ } -void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +void HAL_I2C_MspInit(I2C_HandleTypeDef *i2cHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(i2cHandle->Instance==I2C1) - { - /* USER CODE BEGIN I2C1_MspInit 0 */ - - /* USER CODE END I2C1_MspInit 0 */ - LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**I2C1 GPIO Configuration - PB7 ------> I2C1_SDA - PB8-BOOT0 ------> I2C1_SCL - */ - GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8; - GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* I2C1 clock enable */ - __HAL_RCC_I2C1_CLK_ENABLE(); - /* USER CODE BEGIN I2C1_MspInit 1 */ - - /* USER CODE END I2C1_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (i2cHandle->Instance == I2C1) { + /* USER CODE BEGIN I2C1_MspInit 0 */ + + /* USER CODE END I2C1_MspInit 0 */ + LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C1 clock enable */ + __HAL_RCC_I2C1_CLK_ENABLE(); + /* USER CODE BEGIN I2C1_MspInit 1 */ + + /* USER CODE END I2C1_MspInit 1 */ + } } -void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +void HAL_I2C_MspDeInit(I2C_HandleTypeDef *i2cHandle) { - if(i2cHandle->Instance==I2C1) - { - /* USER CODE BEGIN I2C1_MspDeInit 0 */ + if (i2cHandle->Instance == I2C1) { + /* USER CODE BEGIN I2C1_MspDeInit 0 */ - /* USER CODE END I2C1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_I2C1_CLK_DISABLE(); + /* USER CODE END I2C1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C1_CLK_DISABLE(); - /**I2C1 GPIO Configuration - PB7 ------> I2C1_SDA - PB8-BOOT0 ------> I2C1_SCL - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); + /**I2C1 GPIO Configuration + PB7 ------> I2C1_SDA + PB8-BOOT0 ------> I2C1_SCL + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8); - /* USER CODE BEGIN I2C1_MspDeInit 1 */ + /* USER CODE BEGIN I2C1_MspDeInit 1 */ - /* USER CODE END I2C1_MspDeInit 1 */ - } + /* USER CODE END I2C1_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/main.c b/SAMM/IMUandTOF/Core/Src/main.c index 7400c2e1f..5c1dabb9e 100644 --- a/SAMM/IMUandTOF/Core/Src/main.c +++ b/SAMM/IMUandTOF/Core/Src/main.c @@ -1,41 +1,43 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file : main.c - * @brief : Main program body - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "main.h" + #include "crc.h" #include "fdcan.h" +#include "gpio.h" #include "i2c.h" #include "spi.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include + #include "VL53L4ED_api.h" #include "bmi323.h" -//#include "circularBuffer.h" +// #include "circularBuffer.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define BMI323_CS_GPIO_Port GPIOA -#define BMI323_CS_Pin GPIO_PIN_4 +#define BMI323_CS_Pin GPIO_PIN_4 /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ @@ -51,10 +53,10 @@ /* USER CODE BEGIN PM */ PUTCHAR_PROTOTYPE { - ITM_SendChar(ch); - return ch; + ITM_SendChar(ch); + return ch; } -//CircularBuffer *cb; +// CircularBuffer *cb; /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ @@ -80,182 +82,169 @@ I2C2 - is meant for thermal sensor MLX90640 and is ran using DMA to offload CPU /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { - /* USER CODE BEGIN 1 */ - //cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); - /* USER CODE END 1 */ - - /* MCU Configuration--------------------------------------------------------*/ - - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); - - /* USER CODE BEGIN Init */ - - /* USER CODE END Init */ - - /* Configure the system clock */ - SystemClock_Config(); - - /* USER CODE BEGIN SysInit */ - - /* USER CODE END SysInit */ - - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_CRC_Init(); - MX_FDCAN1_Init(); - MX_FDCAN2_Init(); - MX_I2C1_Init(); - MX_SPI1_Init(); // TODO: change all instances of spi1 -> SPI1 - /* USER CODE BEGIN 2 */ - - // HAL_FDCAN_Start(&hfdcan1); - // HAL_FDCAN_Start(&hfdcan2); - // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); - // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); - bmi323 bmi323_dev; - HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); - bmi323_init(&bmi323_dev, &hspi1, BMI323_CS_GPIO_Port, BMI323_CS_Pin); - // Send 2 dummy bytes to switch BMI323 to SPI mode - // uint16_t dummy_byte = 0x8000; - // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_RESET); - // HAL_SPI_Transmit(&hspi1,(uint8_t*)&dummy_byte, 1, HAL_MAX_DELAY); - // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); - //HAL_Delay(1); // Short delay after mode switch - - // Initialize BMI323 sensor - - // if (BMI323_Init() != HAL_OK) { - // printf("BMI323 initialization failed!\r\n"); - // Error_Handler(); - // } - - // static uint16_t eeMLX90640[832]; - // static paramsMLX90640 mlx90640; - // #define MLX90640_ADDRESS 0x33<<1 - // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); - - // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); - - // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); - - // MLX90640_SynchFrame(MLX90640_ADDRESS); - // MLX90640_SetRefreshRate(0x33, 0x05); - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - //begin VL53L4ED - HAL_Delay(100); // wait for 5ms to power up the device - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_L_XSHUT_Pin - //HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin - HAL_Delay(100); // wait for 5ms to reset the device - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); //TOF_L_XSHUT_Pin - //HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin - HAL_Delay(100); // wait for 5ms to power up the device - - uint16_t status = 0; - - uint16_t sensor_id = 0; - VL53L4ED_ResultsData_t results; - uint8_t p_data_ready; - - int TOF_ID = 0x52; - HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); - status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); - printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); - status = VL53L4ED_StartRanging(TOF_ID); - status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); - status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing - - - while (1) - { - // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); - // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - //begin VL53L4ED - status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); - if(p_data_ready){ - /* (Mandatory) Clear HW interrupt to restart measurements */ - VL53L4ED_ClearInterrupt(TOF_ID); - /* Read measured distance. RangeStatus = 0 means valid data */ - VL53L4ED_GetResult(TOF_ID, &results); - printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", - results.range_status, - results.distance_mm- 67, - results.signal_per_spad_kcps); - }else{ - HAL_Delay(10); - __disable_irq(); - __enable_irq(); - } - - //begin BMI323 - // int16_t ax, ay, az; - // if (BMI323_ReadAccel(&ax, &ay, &az) == HAL_OK) { - // printf("Accel: X=%d, Y=%d, Z=%d\r\n", ax, ay, az); - // } - HAL_Delay(100); // Read every 100ms - } - /* USER CODE END 3 */ + /* USER CODE BEGIN 1 */ + // cb = circular_buffer_init(64, 68 * sizeof(uint8_t)); + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* USER CODE BEGIN Init */ + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_CRC_Init(); + MX_FDCAN1_Init(); + MX_FDCAN2_Init(); + MX_I2C1_Init(); + MX_SPI1_Init(); // TODO: change all instances of spi1 -> SPI1 + /* USER CODE BEGIN 2 */ + + // HAL_FDCAN_Start(&hfdcan1); + // HAL_FDCAN_Start(&hfdcan2); + // HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + // HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0); + bmi323 bmi323_dev; + HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); + bmi323_init(&bmi323_dev, &hspi1, BMI323_CS_GPIO_Port, BMI323_CS_Pin); + // Send 2 dummy bytes to switch BMI323 to SPI mode + // uint16_t dummy_byte = 0x8000; + // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_RESET); + // HAL_SPI_Transmit(&hspi1,(uint8_t*)&dummy_byte, 1, HAL_MAX_DELAY); + // HAL_GPIO_WritePin(BMI323_CS_GPIO_Port, BMI323_CS_Pin, GPIO_PIN_SET); + // HAL_Delay(1); // Short delay after mode switch + + // Initialize BMI323 sensor + + // if (BMI323_Init() != HAL_OK) { + // printf("BMI323 initialization failed!\r\n"); + // Error_Handler(); + // } + + // static uint16_t eeMLX90640[832]; + // static paramsMLX90640 mlx90640; + // #define MLX90640_ADDRESS 0x33<<1 + // MLX90640_DumpEE(MLX90640_ADDRESS, eeMLX90640); + + // MLX90640_ExtractParameters(eeMLX90640, &mlx90640); + + // MLX90640_SetRefreshRate(MLX90640_ADDRESS, 0x05); + + // MLX90640_SynchFrame(MLX90640_ADDRESS); + // MLX90640_SetRefreshRate(0x33, 0x05); + /* USER CODE END 2 */ + + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + // begin VL53L4ED + HAL_Delay(100); // wait for 5ms to power up the device + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_RESET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to reset the device + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // TOF_L_XSHUT_Pin + // HAL_GPIO_WritePin(GPIOF, GPIO_PIN_1, GPIO_PIN_SET); //TOF_C_XSHUT_Pin + HAL_Delay(100); // wait for 5ms to power up the device + + uint16_t status = 0; + + uint16_t sensor_id = 0; + VL53L4ED_ResultsData_t results; + uint8_t p_data_ready; + + int TOF_ID = 0x52; + HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1); + status = VL53L4ED_GetSensorId(TOF_ID, &sensor_id); + printf("VL53L4ED Sensor ID: 0x%04X\n", sensor_id); + status = VL53L4ED_StartRanging(TOF_ID); + status = VL53L4ED_SetRangeTiming(TOF_ID, 50, 70); + status = VL53L4ED_SetOffset(TOF_ID, 50); // Set offset to 0 for testing + + while (1) { + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData); + // HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, TxData); + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + // begin VL53L4ED + status = VL53L4ED_CheckForDataReady(TOF_ID, &p_data_ready); + if (p_data_ready) { + /* (Mandatory) Clear HW interrupt to restart measurements */ + VL53L4ED_ClearInterrupt(TOF_ID); + /* Read measured distance. RangeStatus = 0 means valid data */ + VL53L4ED_GetResult(TOF_ID, &results); + printf("Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\n", results.range_status, results.distance_mm - 67, results.signal_per_spad_kcps); + } else { + HAL_Delay(10); + __disable_irq(); + __enable_irq(); + } + + // begin BMI323 + // int16_t ax, ay, az; + // if (BMI323_ReadAccel(&ax, &ay, &az) == HAL_OK) { + // printf("Accel: X=%d, Y=%d, Z=%d\r\n", ax, ay, az); + // } + HAL_Delay(100); // Read every 100ms + } + /* USER CODE END 3 */ } /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { - LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); - while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) - { - } - LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); - LL_RCC_HSE_EnableBypass(); - LL_RCC_HSE_Enable(); - /* Wait till HSE is ready */ - while(LL_RCC_HSE_IsReady() != 1) - { - } - - LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 32, LL_RCC_PLLR_DIV_2); - LL_RCC_PLL_EnableDomain_SYS(); - LL_RCC_PLL_Enable(); - /* Wait till PLL is ready */ - while(LL_RCC_PLL_IsReady() != 1) - { - } - - LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); - /* Wait till System clock is ready */ - while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) - { - } - - /* Insure 1us transition state at intermediate medium speed clock*/ - for (__IO uint32_t i = (170 >> 1); i !=0; i--); - - /* Set AHB prescaler*/ - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); - LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); - LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); - LL_SetSystemCoreClock(128000000); - - /* Update the time base */ - if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK) - { - Error_Handler(); - } + LL_FLASH_SetLatency(LL_FLASH_LATENCY_4); + while (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4) {} + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + LL_RCC_HSE_EnableBypass(); + LL_RCC_HSE_Enable(); + /* Wait till HSE is ready */ + while (LL_RCC_HSE_IsReady() != 1) {} + + LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_1, 32, LL_RCC_PLLR_DIV_2); + LL_RCC_PLL_EnableDomain_SYS(); + LL_RCC_PLL_Enable(); + /* Wait till PLL is ready */ + while (LL_RCC_PLL_IsReady() != 1) {} + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL); + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2); + /* Wait till System clock is ready */ + while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {} + + /* Insure 1us transition state at intermediate medium speed clock*/ + for (__IO uint32_t i = (170 >> 1); i != 0; i--) + ; + + /* Set AHB prescaler*/ + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); + LL_SetSystemCoreClock(128000000); + + /* Update the time base */ + if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK) { + Error_Handler(); + } } /* USER CODE BEGIN 4 */ @@ -266,60 +255,56 @@ void SystemClock_Config(void) // printf("got messgae\n"); // //circularBufferPush(cb, RxData, sizeof(RxData)); - // } /* USER CODE END 4 */ /** - * @brief Period elapsed callback in non blocking mode - * @note This function is called when TIM1 interrupt took place, inside - * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment - * a global variable "uwTick" used as application time base. - * @param htim : TIM handle - * @retval None - */ + * @brief Period elapsed callback in non blocking mode + * @note This function is called when TIM1 interrupt took place, inside + * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment + * a global variable "uwTick" used as application time base. + * @param htim : TIM handle + * @retval None + */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { - /* USER CODE BEGIN Callback 0 */ + /* USER CODE BEGIN Callback 0 */ - /* USER CODE END Callback 0 */ - if (htim->Instance == TIM1) - { - HAL_IncTick(); - } - /* USER CODE BEGIN Callback 1 */ + /* USER CODE END Callback 0 */ + if (htim->Instance == TIM1) { + HAL_IncTick(); + } + /* USER CODE BEGIN Callback 1 */ - /* USER CODE END Callback 1 */ + /* USER CODE END Callback 1 */ } /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) {} + /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ 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 CODE END 6 */ + /* 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 CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/SAMM/IMUandTOF/Core/Src/spi.c b/SAMM/IMUandTOF/Core/Src/spi.c index 2944b6ce0..31f66776b 100644 --- a/SAMM/IMUandTOF/Core/Src/spi.c +++ b/SAMM/IMUandTOF/Core/Src/spi.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file spi.c - * @brief This file provides code for the configuration - * of the SPI instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file spi.c + * @brief This file provides code for the configuration + * of the SPI instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ #include "spi.h" @@ -30,97 +30,93 @@ SPI_HandleTypeDef hspi1; void MX_SPI1_Init(void) { - /* USER CODE BEGIN spi1_Init 0 */ - - /* USER CODE END spi1_Init 0 */ - - /* USER CODE BEGIN spi1_Init 1 */ - - /* USER CODE END spi1_Init 1 */ - hspi1.Instance = SPI1; - hspi1.Init.Mode = SPI_MODE_MASTER; - hspi1.Init.Direction = SPI_DIRECTION_2LINES; - hspi1.Init.DataSize = SPI_DATASIZE_16BIT; - hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; - hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; - hspi1.Init.NSS = SPI_NSS_SOFT; - hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; - hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; - hspi1.Init.TIMode = SPI_TIMODE_DISABLE; - hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; - hspi1.Init.CRCPolynomial = 7; - hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; - hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; - if (HAL_SPI_Init(&hspi1) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN spi1_Init 2 */ - - /* USER CODE END spi1_Init 2 */ - + /* USER CODE BEGIN spi1_Init 0 */ + + /* USER CODE END spi1_Init 0 */ + + /* USER CODE BEGIN spi1_Init 1 */ + + /* USER CODE END spi1_Init 1 */ + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_2LINES; + hspi1.Init.DataSize = SPI_DATASIZE_16BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 7; + hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; + if (HAL_SPI_Init(&hspi1) != HAL_OK) { + Error_Handler(); + } + /* USER CODE BEGIN spi1_Init 2 */ + + /* USER CODE END spi1_Init 2 */ } -void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) +void HAL_SPI_MspInit(SPI_HandleTypeDef *spiHandle) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(spiHandle->Instance==spi1) - { - /* USER CODE BEGIN spi1_MspInit 0 */ - - /* USER CODE END spi1_MspInit 0 */ - /* spi1 clock enable */ - __HAL_RCC_spi1_CLK_ENABLE(); - - __HAL_RCC_GPIOC_CLK_ENABLE(); - /**spi1 GPIO Configuration - PA5 ------> spi1_SCK - PA6 ------> spi1_MISO - PA7 ------> spi1_MOSI - */ - GPIO_InitStruct.Pin = GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLDOWN; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF5_spi1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF5_spi1; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN spi1_MspInit 1 */ - - /* USER CODE END spi1_MspInit 1 */ - } + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if (spiHandle->Instance == spi1) { + /* USER CODE BEGIN spi1_MspInit 0 */ + + /* USER CODE END spi1_MspInit 0 */ + /* spi1 clock enable */ + __HAL_RCC_spi1_CLK_ENABLE(); + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**spi1 GPIO Configuration + PA5 ------> spi1_SCK + PA6 ------> spi1_MISO + PA7 ------> spi1_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF5_spi1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF5_spi1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN spi1_MspInit 1 */ + + /* USER CODE END spi1_MspInit 1 */ + } } -void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) +void HAL_SPI_MspDeInit(SPI_HandleTypeDef *spiHandle) { - if(spiHandle->Instance==spi1) - { - /* USER CODE BEGIN spi1_MspDeInit 0 */ + if (spiHandle->Instance == spi1) { + /* USER CODE BEGIN spi1_MspDeInit 0 */ - /* USER CODE END spi1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_spi1_CLK_DISABLE(); + /* USER CODE END spi1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_spi1_CLK_DISABLE(); - /**spi1 GPIO Configuration - PC10 ------> spi1_SCK - PC11 ------> spi1_MISO - PC12 ------> spi1_MOSI - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); + /**spi1 GPIO Configuration + PC10 ------> spi1_SCK + PC11 ------> spi1_MISO + PC12 ------> spi1_MOSI + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); - /* USER CODE BEGIN spi1_MspDeInit 1 */ + /* USER CODE BEGIN spi1_MspDeInit 1 */ - /* USER CODE END spi1_MspDeInit 1 */ - } + /* USER CODE END spi1_MspDeInit 1 */ + } } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c index 3fc223b63..69189ef4e 100644 --- a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_msp.c @@ -1,21 +1,21 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_msp.c - * @brief This file provides code for the MSP Initialization - * and de-Initialization codes. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -58,27 +58,27 @@ /* USER CODE END 0 */ /** - * Initializes the Global MSP. - */ + * Initializes the Global MSP. + */ void HAL_MspInit(void) { - /* USER CODE BEGIN MspInit 0 */ + /* USER CODE BEGIN MspInit 0 */ - /* USER CODE END MspInit 0 */ + /* USER CODE END MspInit 0 */ - __HAL_RCC_SYSCFG_CLK_ENABLE(); - __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); - /* System interrupt init*/ + /* System interrupt init*/ - /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral - */ - HAL_PWREx_DisableUCPDDeadBattery(); + /** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral + */ + HAL_PWREx_DisableUCPDDeadBattery(); - /* USER CODE BEGIN MspInit 1 */ + /* USER CODE BEGIN MspInit 1 */ - /* USER CODE END MspInit 1 */ + /* USER CODE END MspInit 1 */ } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c index f43496b92..b9b75acf8 100644 --- a/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_hal_timebase_tim.c @@ -1,20 +1,20 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_hal_timebase_tim.c - * @brief HAL time base based on the hardware TIM. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_hal_timebase_tim.c + * @brief HAL time base based on the hardware TIM. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ @@ -25,102 +25,96 @@ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ -TIM_HandleTypeDef htim1; +TIM_HandleTypeDef htim1; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** - * @brief This function configures the TIM1 as a time base source. - * The time source is configured to have 1ms time base with a dedicated - * Tick interrupt priority. - * @note This function is called automatically at the beginning of program after - * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). - * @param TickPriority: Tick interrupt priority. - * @retval HAL status - */ + * @brief This function configures the TIM1 as a time base source. + * The time source is configured to have 1ms time base with a dedicated + * Tick interrupt priority. + * @note This function is called automatically at the beginning of program after + * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). + * @param TickPriority: Tick interrupt priority. + * @retval HAL status + */ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { - RCC_ClkInitTypeDef clkconfig; - uint32_t uwTimclock = 0; - uint32_t uwPrescalerValue = 0; - uint32_t pFLatency; + RCC_ClkInitTypeDef clkconfig; + uint32_t uwTimclock = 0; + uint32_t uwPrescalerValue = 0; + uint32_t pFLatency; - HAL_StatusTypeDef status; + HAL_StatusTypeDef status; - /* Enable TIM1 clock */ - __HAL_RCC_TIM1_CLK_ENABLE(); + /* Enable TIM1 clock */ + __HAL_RCC_TIM1_CLK_ENABLE(); - /* Get clock configuration */ - HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); + /* Get clock configuration */ + HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); - /* Compute TIM1 clock */ - uwTimclock = HAL_RCC_GetPCLK2Freq(); + /* Compute TIM1 clock */ + uwTimclock = HAL_RCC_GetPCLK2Freq(); - /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ - uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); + /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */ + uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); - /* Initialize TIM1 */ - htim1.Instance = TIM1; + /* Initialize TIM1 */ + htim1.Instance = TIM1; - /* Initialize TIMx peripheral as follow: - * Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. - * Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. - * ClockDivision = 0 - * Counter direction = Up - */ - htim1.Init.Period = (1000000U / 1000U) - 1U; - htim1.Init.Prescaler = uwPrescalerValue; - htim1.Init.ClockDivision = 0; - htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + /* Initialize TIMx peripheral as follow: + * Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base. + * Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + * ClockDivision = 0 + * Counter direction = Up + */ + htim1.Init.Period = (1000000U / 1000U) - 1U; + htim1.Init.Prescaler = uwPrescalerValue; + htim1.Init.ClockDivision = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - status = HAL_TIM_Base_Init(&htim1); - if (status == HAL_OK) - { - /* Start the TIM time Base generation in interrupt mode */ - status = HAL_TIM_Base_Start_IT(&htim1); - if (status == HAL_OK) - { - /* Enable the TIM1 global Interrupt */ - HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); - /* Configure the SysTick IRQ priority */ - if (TickPriority < (1UL << __NVIC_PRIO_BITS)) - { - /* Configure the TIM IRQ priority */ - HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); - uwTickPrio = TickPriority; - } - else - { - status = HAL_ERROR; - } - } - } + status = HAL_TIM_Base_Init(&htim1); + if (status == HAL_OK) { + /* Start the TIM time Base generation in interrupt mode */ + status = HAL_TIM_Base_Start_IT(&htim1); + if (status == HAL_OK) { + /* Enable the TIM1 global Interrupt */ + HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + /* Configure the SysTick IRQ priority */ + if (TickPriority < (1UL << __NVIC_PRIO_BITS)) { + /* Configure the TIM IRQ priority */ + HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, TickPriority, 0U); + uwTickPrio = TickPriority; + } else { + status = HAL_ERROR; + } + } + } - /* Return function status */ - return status; + /* Return function status */ + return status; } /** - * @brief Suspend Tick increment. - * @note Disable the tick increment by disabling TIM1 update interrupt. - * @param None - * @retval None - */ + * @brief Suspend Tick increment. + * @note Disable the tick increment by disabling TIM1 update interrupt. + * @param None + * @retval None + */ void HAL_SuspendTick(void) { - /* Disable TIM1 update Interrupt */ - __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); + /* Disable TIM1 update Interrupt */ + __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE); } /** - * @brief Resume Tick increment. - * @note Enable the tick increment by Enabling TIM1 update interrupt. - * @param None - * @retval None - */ + * @brief Resume Tick increment. + * @note Enable the tick increment by Enabling TIM1 update interrupt. + * @param None + * @retval None + */ void HAL_ResumeTick(void) { - /* Enable TIM1 Update interrupt */ - __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); + /* Enable TIM1 Update interrupt */ + __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); } - diff --git a/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c b/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c index ef94b5f52..2db0ff77b 100644 --- a/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c +++ b/SAMM/IMUandTOF/Core/Src/stm32g4xx_it.c @@ -1,25 +1,26 @@ /* USER CODE BEGIN Header */ /** - ****************************************************************************** - * @file stm32g4xx_it.c - * @brief Interrupt Service Routines. - ****************************************************************************** - * @attention - * - * Copyright (c) 2024 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file stm32g4xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2024 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 */ /* Includes ------------------------------------------------------------------*/ -#include "main.h" #include "stm32g4xx_it.h" + +#include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ @@ -65,131 +66,125 @@ extern TIM_HandleTypeDef htim1; /* Cortex-M4 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** - * @brief This function handles Non maskable interrupt. - */ + * @brief This function handles Non maskable interrupt. + */ void NMI_Handler(void) { - /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ - - /* USER CODE END NonMaskableInt_IRQn 0 */ - /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ - while (1) - { - } - /* USER CODE END NonMaskableInt_IRQn 1 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) {} + /* USER CODE END NonMaskableInt_IRQn 1 */ } /** - * @brief This function handles Hard fault interrupt. - */ + * @brief This function handles Hard fault interrupt. + */ void HardFault_Handler(void) { - /* USER CODE BEGIN HardFault_IRQn 0 */ - - /* USER CODE END HardFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_HardFault_IRQn 0 */ - /* USER CODE END W1_HardFault_IRQn 0 */ - } + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } } /** - * @brief This function handles Memory management fault. - */ + * @brief This function handles Memory management fault. + */ void MemManage_Handler(void) { - /* USER CODE BEGIN MemoryManagement_IRQn 0 */ - - /* USER CODE END MemoryManagement_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ - /* USER CODE END W1_MemoryManagement_IRQn 0 */ - } + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ + /* USER CODE END W1_MemoryManagement_IRQn 0 */ + } } /** - * @brief This function handles Prefetch fault, memory access fault. - */ + * @brief This function handles Prefetch fault, memory access fault. + */ void BusFault_Handler(void) { - /* USER CODE BEGIN BusFault_IRQn 0 */ - - /* USER CODE END BusFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_BusFault_IRQn 0 */ - /* USER CODE END W1_BusFault_IRQn 0 */ - } + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_BusFault_IRQn 0 */ + /* USER CODE END W1_BusFault_IRQn 0 */ + } } /** - * @brief This function handles Undefined instruction or illegal state. - */ + * @brief This function handles Undefined instruction or illegal state. + */ void UsageFault_Handler(void) { - /* USER CODE BEGIN UsageFault_IRQn 0 */ - - /* USER CODE END UsageFault_IRQn 0 */ - while (1) - { - /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ - /* USER CODE END W1_UsageFault_IRQn 0 */ - } + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + while (1) { + /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ + /* USER CODE END W1_UsageFault_IRQn 0 */ + } } /** - * @brief This function handles System service call via SWI instruction. - */ + * @brief This function handles System service call via SWI instruction. + */ void SVC_Handler(void) { - /* USER CODE BEGIN SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 0 */ - /* USER CODE END SVCall_IRQn 0 */ - /* USER CODE BEGIN SVCall_IRQn 1 */ + /* USER CODE END SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 1 */ - /* USER CODE END SVCall_IRQn 1 */ + /* USER CODE END SVCall_IRQn 1 */ } /** - * @brief This function handles Debug monitor. - */ + * @brief This function handles Debug monitor. + */ void DebugMon_Handler(void) { - /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ - /* USER CODE END DebugMonitor_IRQn 0 */ - /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ - /* USER CODE END DebugMonitor_IRQn 1 */ + /* USER CODE END DebugMonitor_IRQn 1 */ } /** - * @brief This function handles Pendable request for system service. - */ + * @brief This function handles Pendable request for system service. + */ void PendSV_Handler(void) { - /* USER CODE BEGIN PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 0 */ - /* USER CODE END PendSV_IRQn 0 */ - /* USER CODE BEGIN PendSV_IRQn 1 */ + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ - /* USER CODE END PendSV_IRQn 1 */ + /* USER CODE END PendSV_IRQn 1 */ } /** - * @brief This function handles System tick timer. - */ + * @brief This function handles System tick timer. + */ void SysTick_Handler(void) { - /* USER CODE BEGIN SysTick_IRQn 0 */ + /* USER CODE BEGIN SysTick_IRQn 0 */ - /* USER CODE END SysTick_IRQn 0 */ + /* USER CODE END SysTick_IRQn 0 */ - /* USER CODE BEGIN SysTick_IRQn 1 */ + /* USER CODE BEGIN SysTick_IRQn 1 */ - /* USER CODE END SysTick_IRQn 1 */ + /* USER CODE END SysTick_IRQn 1 */ } /******************************************************************************/ @@ -200,17 +195,17 @@ void SysTick_Handler(void) /******************************************************************************/ /** - * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. - */ + * @brief This function handles TIM1 update interrupt and TIM16 global interrupt. + */ void TIM1_UP_TIM16_IRQHandler(void) { - /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */ - /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ - HAL_TIM_IRQHandler(&htim1); - /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ + /* USER CODE END TIM1_UP_TIM16_IRQn 0 */ + HAL_TIM_IRQHandler(&htim1); + /* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */ - /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ + /* USER CODE END TIM1_UP_TIM16_IRQn 1 */ } /* USER CODE BEGIN 1 */ diff --git a/SAMM/IMUandTOF/Core/Src/syscalls.c b/SAMM/IMUandTOF/Core/Src/syscalls.c index e33a8492c..a59a7d089 100644 --- a/SAMM/IMUandTOF/Core/Src/syscalls.c +++ b/SAMM/IMUandTOF/Core/Src/syscalls.c @@ -21,156 +21,148 @@ */ /* Includes */ -#include -#include #include -#include #include -#include +#include +#include +#include #include #include - +#include /* Variables */ extern int __io_putchar(int ch) __attribute__((weak)); extern int __io_getchar(void) __attribute__((weak)); - -char *__env[1] = { 0 }; +char *__env[1] = {0}; char **environ = __env; - /* Functions */ -void initialise_monitor_handles() -{ -} +void initialise_monitor_handles() {} int _getpid(void) { - return 1; + return 1; } int _kill(int pid, int sig) { - (void)pid; - (void)sig; - errno = EINVAL; - return -1; + (void)pid; + (void)sig; + errno = EINVAL; + return -1; } -void _exit (int status) +void _exit(int status) { - _kill(status, -1); - while (1) {} /* Make sure we hang here */ + _kill(status, -1); + while (1) {} /* Make sure we hang here */ } __attribute__((weak)) int _read(int file, char *ptr, int len) { - (void)file; - int DataIdx; + (void)file; + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - *ptr++ = __io_getchar(); - } + for (DataIdx = 0; DataIdx < len; DataIdx++) { + *ptr++ = __io_getchar(); + } - return len; + return len; } __attribute__((weak)) int _write(int file, char *ptr, int len) { - (void)file; - int DataIdx; + (void)file; + int DataIdx; - for (DataIdx = 0; DataIdx < len; DataIdx++) - { - __io_putchar(*ptr++); - } - return len; + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar(*ptr++); + } + return len; } int _close(int file) { - (void)file; - return -1; + (void)file; + return -1; } - int _fstat(int file, struct stat *st) { - (void)file; - st->st_mode = S_IFCHR; - return 0; + (void)file; + st->st_mode = S_IFCHR; + return 0; } int _isatty(int file) { - (void)file; - return 1; + (void)file; + return 1; } int _lseek(int file, int ptr, int dir) { - (void)file; - (void)ptr; - (void)dir; - return 0; + (void)file; + (void)ptr; + (void)dir; + return 0; } int _open(char *path, int flags, ...) { - (void)path; - (void)flags; - /* Pretend like we always fail */ - return -1; + (void)path; + (void)flags; + /* Pretend like we always fail */ + return -1; } int _wait(int *status) { - (void)status; - errno = ECHILD; - return -1; + (void)status; + errno = ECHILD; + return -1; } int _unlink(char *name) { - (void)name; - errno = ENOENT; - return -1; + (void)name; + errno = ENOENT; + return -1; } int _times(struct tms *buf) { - (void)buf; - return -1; + (void)buf; + return -1; } int _stat(char *file, struct stat *st) { - (void)file; - st->st_mode = S_IFCHR; - return 0; + (void)file; + st->st_mode = S_IFCHR; + return 0; } int _link(char *old, char *new) { - (void)old; - (void)new; - errno = EMLINK; - return -1; + (void)old; + (void)new; + errno = EMLINK; + return -1; } int _fork(void) { - errno = EAGAIN; - return -1; + errno = EAGAIN; + return -1; } int _execve(char *name, char **argv, char **env) { - (void)name; - (void)argv; - (void)env; - errno = ENOMEM; - return -1; + (void)name; + (void)argv; + (void)env; + errno = ENOMEM; + return -1; } diff --git a/SAMM/IMUandTOF/Core/Src/sysmem.c b/SAMM/IMUandTOF/Core/Src/sysmem.c index 246470ee8..00c397937 100644 --- a/SAMM/IMUandTOF/Core/Src/sysmem.c +++ b/SAMM/IMUandTOF/Core/Src/sysmem.c @@ -52,28 +52,26 @@ static uint8_t *__sbrk_heap_end = NULL; */ void *_sbrk(ptrdiff_t incr) { - extern uint8_t _end; /* Symbol defined in the linker script */ - extern uint8_t _estack; /* Symbol defined in the linker script */ - extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ - const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; - const uint8_t *max_heap = (uint8_t *)stack_limit; - uint8_t *prev_heap_end; + extern uint8_t _end; /* Symbol defined in the linker script */ + extern uint8_t _estack; /* Symbol defined in the linker script */ + extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ + const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; + const uint8_t *max_heap = (uint8_t *)stack_limit; + uint8_t *prev_heap_end; - /* Initialize heap end at first call */ - if (NULL == __sbrk_heap_end) - { - __sbrk_heap_end = &_end; - } + /* Initialize heap end at first call */ + if (NULL == __sbrk_heap_end) { + __sbrk_heap_end = &_end; + } - /* Protect heap from growing into the reserved MSP stack */ - if (__sbrk_heap_end + incr > max_heap) - { - errno = ENOMEM; - return (void *)-1; - } + /* Protect heap from growing into the reserved MSP stack */ + if (__sbrk_heap_end + incr > max_heap) { + errno = ENOMEM; + return (void *)-1; + } - prev_heap_end = __sbrk_heap_end; - __sbrk_heap_end += incr; + prev_heap_end = __sbrk_heap_end; + __sbrk_heap_end += incr; - return (void *)prev_heap_end; + return (void *)prev_heap_end; } diff --git a/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c b/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c index 8d35a0aa0..14d0e1be1 100644 --- a/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c +++ b/SAMM/IMUandTOF/Core/Src/system_stm32g4xx.c @@ -1,109 +1,109 @@ /** - ****************************************************************************** - * @file system_stm32g4xx.c - * @author MCD Application Team - * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File - * - * This file provides two functions and one global variable to be called from - * user application: - * - SystemInit(): This function is called at startup just after reset and - * before branch to main program. This call is made inside - * the "startup_stm32g4xx.s" file. - * - * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used - * by the user application to setup the SysTick - * timer or configure other parameters. - * - * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must - * be called whenever the core clock is changed - * during program execution. - * - * After each device reset the HSI (16 MHz) is used as system clock source. - * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to - * configure the system clock before to branch to main program. - * - * This file configures the system clock as follows: - *============================================================================= - *----------------------------------------------------------------------------- - * System Clock source | HSI - *----------------------------------------------------------------------------- - * SYSCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * HCLK(Hz) | 16000000 - *----------------------------------------------------------------------------- - * AHB Prescaler | 1 - *----------------------------------------------------------------------------- - * APB1 Prescaler | 1 - *----------------------------------------------------------------------------- - * APB2 Prescaler | 1 - *----------------------------------------------------------------------------- - * PLL_M | 1 - *----------------------------------------------------------------------------- - * PLL_N | 16 - *----------------------------------------------------------------------------- - * PLL_P | 7 - *----------------------------------------------------------------------------- - * PLL_Q | 2 - *----------------------------------------------------------------------------- - * PLL_R | 2 - *----------------------------------------------------------------------------- - * Require 48MHz for RNG | Disabled - *----------------------------------------------------------------------------- - *============================================================================= - ****************************************************************************** - * @attention - * - * Copyright (c) 2019 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. - * - ****************************************************************************** - */ + ****************************************************************************** + * @file system_stm32g4xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File + * + * This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32g4xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * After each device reset the HSI (16 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to + * configure the system clock before to branch to main program. + * + * This file configures the system clock as follows: + *============================================================================= + *----------------------------------------------------------------------------- + * System Clock source | HSI + *----------------------------------------------------------------------------- + * SYSCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * HCLK(Hz) | 16000000 + *----------------------------------------------------------------------------- + * AHB Prescaler | 1 + *----------------------------------------------------------------------------- + * APB1 Prescaler | 1 + *----------------------------------------------------------------------------- + * APB2 Prescaler | 1 + *----------------------------------------------------------------------------- + * PLL_M | 1 + *----------------------------------------------------------------------------- + * PLL_N | 16 + *----------------------------------------------------------------------------- + * PLL_P | 7 + *----------------------------------------------------------------------------- + * PLL_Q | 2 + *----------------------------------------------------------------------------- + * PLL_R | 2 + *----------------------------------------------------------------------------- + * Require 48MHz for RNG | Disabled + *----------------------------------------------------------------------------- + *============================================================================= + ****************************************************************************** + * @attention + * + * Copyright (c) 2019 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. + * + ****************************************************************************** + */ /** @addtogroup CMSIS - * @{ - */ + * @{ + */ /** @addtogroup stm32g4xx_system - * @{ - */ + * @{ + */ /** @addtogroup STM32G4xx_System_Private_Includes - * @{ - */ + * @{ + */ #include "stm32g4xx.h" -#if !defined (HSE_VALUE) - #define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ -#endif /* HSE_VALUE */ +#if !defined(HSE_VALUE) +#define HSE_VALUE 24000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ -#if !defined (HSI_VALUE) - #define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ -#endif /* HSI_VALUE */ +#if !defined(HSI_VALUE) +#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_TypesDefinitions - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Defines - * @{ - */ + * @{ + */ /************************* Miscellaneous Configuration ************************/ /* Note: Following vector table addresses must be defined in line with linker - configuration. */ + configuration. */ /*!< Uncomment the following line if you need to relocate the vector table anywhere in Flash or Sram, else the vector table is kept at the automatic remap of boot address selected */ @@ -114,172 +114,171 @@ in Sram else user remap will be done in Flash. */ /* #define VECT_TAB_SRAM */ #if defined(VECT_TAB_SRAM) -#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_BASE_ADDRESS \ + SRAM_BASE /*!< Vector Table base address field. \ + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET \ + 0x00000000U /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ #else -#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. - This value must be a multiple of 0x200. */ -#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ -#endif /* VECT_TAB_SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ -/******************************************************************************/ -/** - * @} - */ +#define VECT_TAB_BASE_ADDRESS \ + FLASH_BASE /*!< Vector Table base address field. \ + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET \ + 0x00000000U /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ + /******************************************************************************/ + /** + * @} + */ /** @addtogroup STM32G4xx_System_Private_Macros - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Variables - * @{ - */ - /* The SystemCoreClock variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetHCLKFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ - uint32_t SystemCoreClock = HSI_VALUE; - - const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; - const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; + * @{ + */ +/* The SystemCoreClock variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. +*/ +uint32_t SystemCoreClock = HSI_VALUE; + +const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U}; +const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U}; /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_FunctionPrototypes - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32G4xx_System_Private_Functions - * @{ - */ + * @{ + */ /** - * @brief Setup the microcontroller system. - * @param None - * @retval None - */ + * @brief Setup the microcontroller system. + * @param None + * @retval None + */ void SystemInit(void) { - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */ - #endif +/* FPU settings ------------------------------------------------------------*/ +#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2))); /* set CP10 and CP11 Full Access */ +#endif - /* Configure the Vector Table location add offset address ------------------*/ + /* Configure the Vector Table location add offset address ------------------*/ #if defined(USER_VECT_TAB_ADDRESS) - SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#endif /* USER_VECT_TAB_ADDRESS */ + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ } /** - * @brief Update SystemCoreClock variable according to Clock Register Values. - * The SystemCoreClock variable contains the core clock (HCLK), it can - * be used by the user application to setup the SysTick timer or configure - * other parameters. - * - * @note Each time the core clock (HCLK) changes, this function must be called - * to update SystemCoreClock variable value. Otherwise, any configuration - * based on this variable will be incorrect. - * - * @note - The system frequency computed by this function is not the real - * frequency in the chip. It is calculated based on the predefined - * constant and the selected clock source: - * - * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) - * - * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) - * - * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) - * or HSI_VALUE(*) multiplied/divided by the PLL factors. - * - * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 16 MHz) but the real value may vary depending on the variations - * in voltage and temperature. - * - * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value - * 24 MHz), user has to ensure that HSE_VALUE is same as the real - * frequency of the crystal used. Otherwise, this function may - * have wrong result. - * - * - The result of this function could be not correct when using fractional - * value for HSE crystal. - * - * @param None - * @retval None - */ + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***) + * or HSI_VALUE(*) multiplied/divided by the PLL factors. + * + * (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 16 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value + * 24 MHz), user has to ensure that HSE_VALUE is same as the real + * frequency of the crystal used. Otherwise, this function may + * have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * + * @param None + * @retval None + */ void SystemCoreClockUpdate(void) { - uint32_t tmp, pllvco, pllr, pllsource, pllm; - - /* Get SYSCLK source -------------------------------------------------------*/ - switch (RCC->CFGR & RCC_CFGR_SWS) - { - case 0x04: /* HSI used as system clock source */ - SystemCoreClock = HSI_VALUE; - break; - - case 0x08: /* HSE used as system clock source */ - SystemCoreClock = HSE_VALUE; - break; - - case 0x0C: /* PLL used as system clock source */ - /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN - SYSCLK = PLL_VCO / PLLR - */ - pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); - pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ; - if (pllsource == 0x02UL) /* HSI used as PLL clock source */ - { - pllvco = (HSI_VALUE / pllm); - } - else /* HSE used as PLL clock source */ - { - pllvco = (HSE_VALUE / pllm); - } - pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); - pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; - SystemCoreClock = pllvco/pllr; - break; - - default: - break; - } - /* Compute HCLK clock frequency --------------------------------------------*/ - /* Get HCLK prescaler */ - tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; - /* HCLK clock frequency */ - SystemCoreClock >>= tmp; + uint32_t tmp, pllvco, pllr, pllsource, pllm; + + /* Get SYSCLK source -------------------------------------------------------*/ + switch (RCC->CFGR & RCC_CFGR_SWS) { + case 0x04: /* HSI used as system clock source */ + SystemCoreClock = HSI_VALUE; + break; + + case 0x08: /* HSE used as system clock source */ + SystemCoreClock = HSE_VALUE; + break; + + case 0x0C: /* PLL used as system clock source */ + /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN + SYSCLK = PLL_VCO / PLLR + */ + pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC); + pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U; + if (pllsource == 0x02UL) /* HSI used as PLL clock source */ + { + pllvco = (HSI_VALUE / pllm); + } else /* HSE used as PLL clock source */ + { + pllvco = (HSE_VALUE / pllm); + } + pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8); + pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U; + SystemCoreClock = pllvco / pllr; + break; + + default: + break; + } + /* Compute HCLK clock frequency --------------------------------------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; } - /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /** - * @} - */ - - + * @} + */