This commit is contained in:
David Rice
2026-01-08 11:31:22 +00:00
parent 0ac7882881
commit 03ab678b79
8 changed files with 13618 additions and 12294 deletions

View File

@@ -22,6 +22,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32g4xx_hal.h"
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -34,9 +35,6 @@
#define IN_SYNC_BYTE_1 'A'
#define IN_SYNC_BYTE_2 'R'
#define VREFINT_CAL_ADDR ((uint16_t*) ((uint32_t)0x1FFF75AA))
#define VREFINT_CAL_VREF 3000UL
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@@ -53,28 +51,31 @@ TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/**
* @brief Structure to hold the time and date data in binary format
*/
uint8_t rx_hold_buffer[2];
uint8_t rx_buffer[32];
uint8_t tx_buffer[32];
uint8_t tx_len = 0x00;
uint8_t tx_len_counter = 0x00;
uint8_t rx_counter = 0x00;
uint8_t rx_len = 0x00;
uint8_t rx_len_counter = 0x00;
uint16_t rx_checksum = 0x0000;
uint16_t tx_checksum = 0x0000;
uint8_t rx_checksum_hold_1 = 0x00;
uint8_t rx_checksum_hold_2 = 0x00;
uint16_t rx_checksum_hold = 0x0000;
uint8_t power_state_value = 0x00;
uint8_t command = 0x00;
uint8_t adc_task_flag = 0x00;
uint8_t uart_tx_flag = 0x00;
uint16_t vin_adc_val = 0x0000;
uint16_t vout_adc_val = 0x0000;
uint32_t vdd_ref = 0x00000000;
uint32_t vin_val = 0x00000000;
uint32_t vout_val = 0x00000000;
uint8_t serial_number_flag = 0x00;
uint8_t serial_number[19] = "ARRIVE-POWERSIM-001";
/* USER CODE END PV */
@@ -89,7 +90,9 @@ static void MX_ADC1_Init(void);
void power_switch (uint8_t state);
void adc_task(void);
uint32_t get_actual_vdda(ADC_HandleTypeDef *hadc);
uint32_t get_calibrated_value_mv(uint32_t raw_adc_value, uint32_t vdda_mv);
void voltage_conversion_task(void);
uint32_t get_divider_input_mv(uint32_t raw_adc_value, uint32_t vdda_mv);
void serial_number_task (void);
/* USER CODE END PFP */
@@ -137,26 +140,30 @@ int main(void)
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(POWER_SWITCH_GPIO_Port, POWER_SWITCH_Pin, GPIO_PIN_RESET);
/* Run ADC calibration */
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
/* Setup UART interrupts */
/* Make sure UART Rx counters and flags are reset */
rx_counter = 0x00;
rx_len = 0x00;
rx_len_counter = 0x00;
adc_task_flag = 0x00;
uart_tx_flag = 0x00;
HAL_UART_Receive_IT(&huart2, rx_hold_buffer, 1);
/* Get real VDDA value */
vdd_ref = get_actual_vdda(&hadc1);
tx_buffer[0] = vdd_ref >> 24;
tx_buffer[1] = vdd_ref >> 16;
tx_buffer[2] = vdd_ref >> 8;
tx_buffer[3] = vdd_ref;
tx_len = 4;
tx_buffer[0] = (uint8_t)((vdd_ref >> 24) & 0xFF);
tx_buffer[1] = (uint8_t)((vdd_ref >> 16) & 0xFF);
tx_buffer[2] = (uint8_t)((vdd_ref >> 8) & 0xFF);
tx_buffer[3] = (uint8_t)(vdd_ref & 0xFF);
HAL_UART_Transmit(&huart2, tx_buffer, tx_len, HAL_MAX_DELAY);
tx_len = 0x04;
HAL_UART_Transmit(&huart2, tx_buffer, tx_len, 100);
/* USER CODE END 2 */
@@ -168,13 +175,15 @@ int main(void)
{
adc_task_flag = 0x00;
adc_task();
voltage_conversion_task();
}
if (uart_tx_flag == 0xff)
if (serial_number_flag == 0xff)
{
uart_tx_flag = 0x00;
HAL_UART_Transmit(&huart2, tx_buffer, tx_len, HAL_MAX_DELAY);
serial_number_flag = 0x00;
serial_number_task ();
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
@@ -251,7 +260,7 @@ static void MX_ADC1_Init(void)
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV32;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = 0;
@@ -283,7 +292,7 @@ static void MX_ADC1_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_VREFINT;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
@@ -318,7 +327,7 @@ static void MX_ADC2_Init(void)
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV32;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
@@ -342,7 +351,7 @@ static void MX_ADC2_Init(void)
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
@@ -426,7 +435,7 @@ static void MX_USART2_UART_Init(void)
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 921600;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
@@ -506,6 +515,7 @@ uint32_t get_actual_vdda(ADC_HandleTypeDef *hadc)
/* Perform ADC reading of the VREFINT channel */
HAL_ADC_Start(hadc);
if (HAL_ADC_PollForConversion(hadc, 10) == HAL_OK) {
vrefint_raw = HAL_ADC_GetValue(hadc);
}
@@ -521,23 +531,99 @@ uint32_t get_actual_vdda(ADC_HandleTypeDef *hadc)
return vdda_mv;
}
/* Converts a raw ADC reading to real mV using the calculated VDDA */
uint32_t get_calibrated_value_mv(uint32_t raw_adc_value, uint32_t vdda_mv)
/* Calculate original input voltage from a 22k/2.2k divider in mV */
uint32_t get_divider_input_mv(uint32_t raw_adc_value, uint32_t vdda_mv)
{
/* Correct for 12-bit ADC (4095) */
return (raw_adc_value * vdda_mv) / 4095;
/* Calculate the voltage at the ADC pin (Vout of the divider) */
/* Using 64-bit for intermediate to avoid overflow: (Raw * VDDA) / 4095 */
uint64_t vout_mv = ((uint64_t)raw_adc_value * vdda_mv) / 4095;
/* Scale by the divider ratio: (22k + 2.2k) / 2.2k = 11 */
uint32_t vin_mv = (uint32_t)(vout_mv * 10.9);
return vin_mv;
}
/* Voltage Conversion Task */
void voltage_conversion_task(void)
{
/* Get Vin voltage */
vin_val = get_divider_input_mv(vin_adc_val, vdd_ref);
/* Get Vout voltage */
vout_val = get_divider_input_mv(vout_adc_val, vdd_ref);
tx_len = 0x08;
tx_buffer[0] = IN_SYNC_BYTE_1;
tx_buffer[1] = IN_SYNC_BYTE_2;
tx_buffer[2] = tx_len;
tx_buffer[3] = (uint8_t)((vin_val >> 24) & 0xFF);
tx_buffer[4] = (uint8_t)((vin_val >> 16) & 0xFF);
tx_buffer[5] = (uint8_t)((vin_val >> 8) & 0xFF);
tx_buffer[6] = (uint8_t)(vin_val & 0xFF);
tx_buffer[7] = (uint8_t)((vout_val >> 24) & 0xFF);
tx_buffer[8] = (uint8_t)((vout_val >> 16) & 0xFF);
tx_buffer[9] = (uint8_t)((vout_val >> 8) & 0xFF);
tx_buffer[10] = (uint8_t)(vout_val & 0xFF);
/* Need to apply checksum to all data bits */
for (tx_len_counter = 0x00; tx_len_counter < tx_len; tx_len_counter++)
{
tx_checksum += tx_buffer[tx_len_counter + 3];
}
tx_checksum = ~tx_checksum;
tx_buffer[11] = (uint8_t)((tx_checksum >> 8) & 0xFF);
tx_buffer[12] = (uint8_t)(tx_checksum & 0xFF);
tx_len = 0x0D;
HAL_UART_Transmit(&huart2, tx_buffer, tx_len, 100);
}
void serial_number_task (void)
{
tx_len = 0x13;
tx_buffer[0] = IN_SYNC_BYTE_1;
tx_buffer[1] = IN_SYNC_BYTE_2;
tx_buffer[2] = tx_len;
for (tx_len_counter = 0x00; tx_len_counter < tx_len; tx_len_counter++)
{
tx_buffer[tx_len_counter + 3] = serial_number[tx_len_counter];
}
tx_checksum = 0x00;
/* Need to apply checksum to all data bits */
for (tx_len_counter = 0x00; tx_len_counter < tx_len; tx_len_counter++)
{
tx_checksum += tx_buffer[tx_len_counter + 3];
}
tx_checksum = ~tx_checksum;
tx_buffer[22] = (uint8_t)((tx_checksum >> 8) & 0xFF);
tx_buffer[23] = (uint8_t)(tx_checksum & 0xFF);
tx_len = 0x18;
HAL_UART_Transmit(&huart2, tx_buffer, tx_len, 100);
}
/* ADC task */
void adc_task (void)
{
HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 100);
vin_adc_val = HAL_ADC_GetValue(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 500);
vout_adc_val = HAL_ADC_GetValue(&hadc2);
HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 100);
vout_adc_val = HAL_ADC_GetValue(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 500);
vin_adc_val = HAL_ADC_GetValue(&hadc2);
HAL_ADC_Stop(&hadc2);
}
@@ -681,6 +767,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
adc_task_flag = 0xff;
break;
/* 'I' - Get serial number information */
case 0x49:
serial_number_flag = 0xff;
break;
default:
break;
}

View File

@@ -1,14 +1,16 @@
../Core/Src/main.c:105:5:main 3
../Core/Src/main.c:190:6:SystemClock_Config 3
../Core/Src/main.c:237:13:MX_ADC1_Init 4
../Core/Src/main.c:305:13:MX_ADC2_Init 4
../Core/Src/main.c:373:13:MX_TIM2_Init 4
../Core/Src/main.c:418:13:MX_USART2_UART_Init 5
../Core/Src/main.c:466:13:MX_GPIO_Init 1
../Core/Src/main.c:503:10:get_actual_vdda 3
../Core/Src/main.c:525:10:get_calibrated_value_mv 1
../Core/Src/main.c:532:6:adc_task 1
../Core/Src/main.c:546:6:power_switch 2
../Core/Src/main.c:562:6:HAL_UART_TxCpltCallback 1
../Core/Src/main.c:568:6:HAL_UART_RxCpltCallback 16
../Core/Src/main.c:714:6:Error_Handler 1
../Core/Src/main.c:108:5:main 3
../Core/Src/main.c:199:6:SystemClock_Config 3
../Core/Src/main.c:246:13:MX_ADC1_Init 4
../Core/Src/main.c:314:13:MX_ADC2_Init 4
../Core/Src/main.c:382:13:MX_TIM2_Init 4
../Core/Src/main.c:427:13:MX_USART2_UART_Init 5
../Core/Src/main.c:475:13:MX_GPIO_Init 1
../Core/Src/main.c:512:10:get_actual_vdda 3
../Core/Src/main.c:535:10:get_divider_input_mv 1
../Core/Src/main.c:548:6:voltage_conversion_task 2
../Core/Src/main.c:586:6:serial_number_task 3
../Core/Src/main.c:618:6:adc_task 1
../Core/Src/main.c:632:6:power_switch 2
../Core/Src/main.c:648:6:HAL_UART_TxCpltCallback 1
../Core/Src/main.c:654:6:HAL_UART_RxCpltCallback 18
../Core/Src/main.c:805:6:Error_Handler 1

Binary file not shown.

View File

@@ -1,14 +1,16 @@
../Core/Src/main.c:105:5:main 8 static
../Core/Src/main.c:190:6:SystemClock_Config 88 static
../Core/Src/main.c:237:13:MX_ADC1_Init 56 static
../Core/Src/main.c:305:13:MX_ADC2_Init 40 static
../Core/Src/main.c:373:13:MX_TIM2_Init 40 static
../Core/Src/main.c:418:13:MX_USART2_UART_Init 8 static
../Core/Src/main.c:466:13:MX_GPIO_Init 40 static
../Core/Src/main.c:503:10:get_actual_vdda 24 static
../Core/Src/main.c:525:10:get_calibrated_value_mv 16 static
../Core/Src/main.c:532:6:adc_task 8 static
../Core/Src/main.c:546:6:power_switch 16 static
../Core/Src/main.c:562:6:HAL_UART_TxCpltCallback 16 static
../Core/Src/main.c:568:6:HAL_UART_RxCpltCallback 16 static
../Core/Src/main.c:714:6:Error_Handler 4 static,ignoring_inline_asm
../Core/Src/main.c:108:5:main 8 static
../Core/Src/main.c:199:6:SystemClock_Config 88 static
../Core/Src/main.c:246:13:MX_ADC1_Init 56 static
../Core/Src/main.c:314:13:MX_ADC2_Init 40 static
../Core/Src/main.c:382:13:MX_TIM2_Init 40 static
../Core/Src/main.c:427:13:MX_USART2_UART_Init 8 static
../Core/Src/main.c:475:13:MX_GPIO_Init 40 static
../Core/Src/main.c:512:10:get_actual_vdda 24 static
../Core/Src/main.c:535:10:get_divider_input_mv 48 static
../Core/Src/main.c:548:6:voltage_conversion_task 8 static
../Core/Src/main.c:586:6:serial_number_task 8 static
../Core/Src/main.c:618:6:adc_task 8 static
../Core/Src/main.c:632:6:power_switch 16 static
../Core/Src/main.c:648:6:HAL_UART_TxCpltCallback 16 static
../Core/Src/main.c:654:6:HAL_UART_RxCpltCallback 16 static
../Core/Src/main.c:805:6:Error_Handler 4 static,ignoring_inline_asm

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,16 @@
#MicroXplorer Configuration settings - do not modify
ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_VREFINT
ADC1.ClockPrescaler=ADC_CLOCK_ASYNC_DIV16
ADC1.ClockPrescaler=ADC_CLOCK_ASYNC_DIV32
ADC1.CommonPathInternal=ADC_CHANNEL_VREFINT|null|null|null
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,OffsetNumber-0\#ChannelRegularConversion,NbrOfConversionFlag,master,ClockPrescaler,CommonPathInternal
ADC1.NbrOfConversionFlag=1
ADC1.OffsetNumber-0\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC1.Rank-0\#ChannelRegularConversion=1
ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_2CYCLES_5
ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_640CYCLES_5
ADC1.master=1
ADC2.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_3
ADC2.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_4
ADC2.ClockPrescaler=ADC_CLOCK_ASYNC_DIV16
ADC2.ClockPrescaler=ADC_CLOCK_ASYNC_DIV32
ADC2.CommonPathInternal=null|null|null|null
ADC2.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,OffsetNumber-0\#ChannelRegularConversion,NbrOfConversionFlag,ClockPrescaler,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,OffsetNumber-1\#ChannelRegularConversion,NbrOfConversion,CommonPathInternal
ADC2.NbrOfConversion=2
@@ -19,8 +19,8 @@ ADC2.OffsetNumber-0\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC2.OffsetNumber-1\#ChannelRegularConversion=ADC_OFFSET_NONE
ADC2.Rank-0\#ChannelRegularConversion=1
ADC2.Rank-1\#ChannelRegularConversion=2
ADC2.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_24CYCLES_5
ADC2.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_24CYCLES_5
ADC2.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_640CYCLES_5
ADC2.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_640CYCLES_5
BSP_IP_NAME=NUCLEO-G431KB
CAD.formats=
CAD.pinconfig=
@@ -148,7 +148,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true,4-MX_ADC2_Init-ADC2-false-HAL-true,5-MX_TIM2_Init-TIM2-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true,4-MX_ADC2_Init-ADC2-false-HAL-true,5-MX_TIM2_Init-TIM2-false-HAL-true,6-MX_ADC1_Init-ADC1-false-HAL-true
RCC.ADC12Freq_Value=128000000
RCC.AHBFreq_Value=128000000
RCC.APB1Freq_Value=128000000