-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db22903
commit 3077ad6
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/** | ||
* @file test_battery.c | ||
* @brief Prints out battery voltage levels | ||
* | ||
* In an infinite loop the battery voltage level is retrieved then outputted | ||
* over serial. The user should check if the voltage levels are expected. When | ||
* connected to USB the voltage should be ~5V. The battery voltage level should | ||
* be checked and compared to a multimeter measurement. | ||
* | ||
* @see battery.h | ||
* | ||
* @author John Madden <jmadden173@pm.me> | ||
* @date 2023-11-17 | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "adc.h" | ||
#include "battery.h" | ||
#include "dma.h" | ||
#include "gpio.h" | ||
#include "main.h" | ||
#include "usart.h" | ||
#include "sys_app.h" | ||
#include "fram.h" | ||
|
||
/** Delay between print statements */ | ||
#ifndef DELAY | ||
#define DELAY 1000 | ||
#endif | ||
|
||
void SystemClock_Config(void); | ||
|
||
/** Global variable for all return codes */ | ||
HAL_StatusTypeDef rc; | ||
|
||
/** | ||
* @brief Entry point for battery test | ||
* @retval int | ||
*/ | ||
int main(void) { | ||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. | ||
*/ | ||
HAL_Init(); | ||
|
||
/* Configure the system clock */ | ||
SystemClock_Config(); | ||
|
||
/* Initialize all configured peripherals */ | ||
MX_GPIO_Init(); | ||
|
||
// already called Enable the GPIOB clock | ||
//__HAL_RCC_GPIOB_CLK_ENABLE(); | ||
|
||
// GPIO Pin Configuration | ||
GPIO_InitTypeDef GPIO_InitStruct = {0}; | ||
GPIO_InitStruct.Pin = GPIO_PIN_5; | ||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Set as push-pull output | ||
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down | ||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Low frequency for LED | ||
|
||
//HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); | ||
// MX_DMA_Init(); | ||
// MX_ADC_Init(); | ||
// mspm | ||
MX_I2C2_Init(); | ||
|
||
MX_USART1_UART_Init(); | ||
|
||
// User level initialization | ||
// battery_init(); | ||
|
||
// Print the compilation time at startup | ||
SystemApp_Init(); | ||
// APP_PRINTF("HELLO"); | ||
char info_str[128]; | ||
int info_len; | ||
info_len = snprintf( | ||
info_str, sizeof(info_str), | ||
"Hello "); | ||
HAL_UART_Transmit(&huart1, (const uint8_t *)info_str, info_len, 1000); | ||
|
||
|
||
|
||
const uint8_t somethingelse[] = {0x61,0x62,0x69}; | ||
uint8_t anyelse[sizeof(somethingelse)]; | ||
FramWrite(0, somethingelse, sizeof(somethingelse)); | ||
FramRead(0, sizeof(somethingelse), anyelse); | ||
HAL_UART_Transmit(&huart1, (const uint8_t *)anyelse, sizeof(anyelse), 1000); | ||
|
||
// Infinite loop | ||
while (1) { | ||
// Print voltage level | ||
char buf[32]; | ||
|
||
// int buf_len = snprintf(buf, sizeof(buf), "Battery Voltage: %d mV\n", | ||
// battery_voltage()); | ||
// HAL_UART_Transmit(&huart1, (const uint8_t *)buf, buf_len, 1000); | ||
|
||
// Sleep | ||
// HAL_Delay(DELAY); | ||
|
||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5); | ||
|
||
|
||
// Delay for 500 milliseconds | ||
HAL_Delay(500); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @brief System Clock Configuration | ||
* @retval None | ||
*/ | ||
void SystemClock_Config(void) { | ||
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; | ||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; | ||
|
||
/** Configure the main internal regulator output voltage | ||
*/ | ||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); | ||
|
||
/** Initializes the CPU, AHB and APB buses clocks | ||
*/ | ||
RCC_OscInitStruct.OscillatorType = | ||
RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_MSI; | ||
RCC_OscInitStruct.HSIState = RCC_HSI_ON; | ||
RCC_OscInitStruct.MSIState = RCC_MSI_ON; | ||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; | ||
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; | ||
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; | ||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; | ||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { | ||
Error_Handler(); | ||
} | ||
|
||
/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers | ||
*/ | ||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3 | RCC_CLOCKTYPE_HCLK | | ||
RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | | ||
RCC_CLOCKTYPE_PCLK2; | ||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; | ||
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | ||
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; | ||
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; | ||
RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; | ||
|
||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { | ||
Error_Handler(); | ||
} | ||
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1); | ||
} | ||
|
||
/* USER CODE BEGIN 4 */ | ||
|
||
/* USER CODE END 4 */ | ||
|
||
/** | ||
* @brief This function is executed in case of error occurrence. | ||
* @retval None | ||
*/ | ||
void Error_Handler(void) { | ||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET); | ||
|
||
/* USER CODE BEGIN Error_Handler_Debug */ | ||
char error[30]; | ||
int error_len = | ||
snprintf(error, sizeof(error), "Error! HAL Status: %d\n", rc); | ||
HAL_UART_Transmit(&huart1, (const uint8_t *)error, error_len, 1000); | ||
|
||
/* 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters