updates
This commit is contained in:
153
Core/Src/main.c
153
Core/Src/main.c
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user