1
0
This commit is contained in:
2025-10-10 02:20:31 +02:00
commit 85e60bcc39
2415 changed files with 1764407 additions and 0 deletions

123
P3_SETR1/Core/Src/HTS221.c Executable file
View File

@@ -0,0 +1,123 @@
#include "stm32l4xx_hal.h"
#include "HTS221.h"
extern I2C_HandleTypeDef hi2c2;
//Estructura para almacenar la calibración
typedef struct{
//Valores de los registros
float H0_rH_x2;
float H1_rH_x2;
float T0_degC_x8;
float T1_degC_x8;
int16_t H0_T0_OUT;
int16_t H1_T0_OUT;
int16_t T0_OUT;
int16_t T1_OUT;
//Rectas de calibración para humedad y temperatura
float ha, hb;
float ta, tb;
} HTS_Calibration;
//Instancia de la calibración
HTS_Calibration hts_cal;
// Direcciones de los registros
#define I2C_TH 0xBE
#define HTS_H0_rH_x2 0x30
#define HTS_H1_rH_x2 0x31
#define HTS_T0_degC_x8 0x32
#define HTS_T1_degC_x8 0x33
#define HTS_T1_T0_MSB 0x35
#define HTS_H0_T0_OUT_LSB 0x36
#define HTS_H0_T0_OUT_MSB 0x37
#define HTS_H1_T0_OUT_LSB 0x3A
#define HTS_H1_T0_OUT_MSB 0x3B
#define HTS_T0_OUT_LSB 0x3C
#define HTS_T0_OUT_MSB 0x3D
#define HTS_T1_OUT_LSB 0x3E
#define HTS_T1_OUT_MSB 0x3F
void HTS221_UpdateCalibration(){
uint8_t buffer;
uint8_t tempMSB;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H0_rH_x2, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
hts_cal.H0_rH_x2 = buffer / 2.0f;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H1_rH_x2, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
hts_cal.H1_rH_x2 = buffer / 2.0f;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T1_T0_MSB, I2C_MEMADD_SIZE_8BIT, &tempMSB, 1, 1000);
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T0_degC_x8, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
hts_cal.T0_degC_x8 = (((tempMSB & 0x03) <<8) | buffer) / 8.0f ;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T1_degC_x8, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
hts_cal.T1_degC_x8 = (((tempMSB & 0x0C) <<6) | buffer) / 8.0f ;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H0_T0_OUT_LSB, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H0_T0_OUT_MSB, I2C_MEMADD_SIZE_8BIT, &tempMSB, 1, 1000);
hts_cal.H0_T0_OUT = (tempMSB <<8) | buffer;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H1_T0_OUT_LSB, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_H1_T0_OUT_MSB, I2C_MEMADD_SIZE_8BIT, &tempMSB, 1, 1000);
hts_cal.H1_T0_OUT = (tempMSB <<8) | buffer;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T0_OUT_LSB, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T0_OUT_MSB, I2C_MEMADD_SIZE_8BIT, &tempMSB, 1, 1000);
hts_cal.T0_OUT = (tempMSB <<8) | buffer;
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T1_OUT_LSB, I2C_MEMADD_SIZE_8BIT, &buffer, 1, 1000);
HAL_I2C_Mem_Read(&hi2c2, I2C_TH, HTS_T1_OUT_MSB, I2C_MEMADD_SIZE_8BIT, &tempMSB, 1, 1000);
hts_cal.T1_OUT = (tempMSB <<8) | buffer;
hts_cal.ha = (hts_cal.H1_rH_x2 - hts_cal.H0_rH_x2) / (hts_cal.H1_T0_OUT - hts_cal.H0_T0_OUT);
hts_cal.hb = hts_cal.H0_rH_x2 - hts_cal.ha*hts_cal.H0_T0_OUT;
hts_cal.ta = (hts_cal.T1_degC_x8 - hts_cal.T0_degC_x8) / (hts_cal.T1_OUT - hts_cal.T0_OUT);
hts_cal.tb = hts_cal.T0_degC_x8 - hts_cal.ha*hts_cal.T0_OUT;
}
void HTS221_Init() {
uint8_t buffer[1];
buffer[0] = 0x87;
HAL_I2C_Mem_Write(&hi2c2, 0xBE, 0x20, I2C_MEMADD_SIZE_8BIT, buffer, 1, 1000);
HTS221_UpdateCalibration();
}
THSample HTS221_Read() {
THSample ths;
uint8_t buffer[4];
HAL_I2C_Mem_Read(&hi2c2, 0xBE, 0x80 | 0x28, I2C_MEMADD_SIZE_8BIT, buffer, 4, 1000);
int16_t hum_raw;
int16_t temp_raw;
hum_raw = (buffer[1] << 8) | buffer[0];
temp_raw = (buffer[3] << 8) | buffer[2];
ths.hum = hts_cal.ha * hum_raw + hts_cal.hb;
ths.temp = hts_cal.ta * temp_raw + hts_cal.tb;
return ths;
return ths;
}

24
P3_SETR1/Core/Src/LPS22.c Executable file
View File

@@ -0,0 +1,24 @@
#include "stm32l4xx_hal.h"
extern I2C_HandleTypeDef hi2c2;
void LPS22_Init() {
uint8_t buffer[1];
buffer[0] = 0x42;
HAL_I2C_Mem_Write(&hi2c2, 0xBA, 0x10, I2C_MEMADD_SIZE_8BIT, buffer, 1 , 1000);
}
float LPS22_ReadPress() {
float press;
uint8_t buffer[3];
HAL_I2C_Mem_Read(&hi2c2, 0xBA, 0x28, I2C_MEMADD_SIZE_8BIT, buffer, 3, 1000);
uint32_t press_raw = (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
press = press_raw / 4096.0f;
return press;
}

224
P3_SETR1/Core/Src/hd44780.c Executable file
View File

@@ -0,0 +1,224 @@
#include "hd44780.h"
#include "main.h"
#define LCD_PORT7 D7_LCD_GPIO_Port
#define LCD_PORT4 D4_LCD_GPIO_Port
#define LCD_PORT56 D6_LCD_GPIO_Port //pines D5 y D6 en el mismpo puerto, GPIOB
#define RS_PORT RS_LCD_GPIO_Port
#define CLOCK_PORT E_LCD_GPIO_Port
#define LCD_RS RS_LCD_Pin
#define LCD_CLOCK E_LCD_Pin
#define LCD_4 D4_LCD_Pin
#define LCD_5 D5_LCD_Pin
#define LCD_6 D6_LCD_Pin
#define LCD_7 D7_LCD_Pin
// Various displays exist, don't make assumptions
uint8_t lcd_chars = 0;
uint8_t lcd_lines = 0;
uint8_t *lcd_line_addresses = 0;
// "Private" globals
uint8_t _lcd_char = 0;
uint8_t _lcd_line = 0;
void lcd_clock(void)
{
// Pulse clock
HAL_GPIO_WritePin(CLOCK_PORT, LCD_CLOCK, 1);
HAL_Delay(1);
HAL_GPIO_WritePin(CLOCK_PORT, LCD_CLOCK, 0);
HAL_Delay(1);
}
void lcd_reset(void)
{
// Resets display from any state to 4-bit mode, first nibble.
// Set everything low first
HAL_GPIO_WritePin(RS_PORT, LCD_RS, 0);
HAL_GPIO_WritePin(LCD_PORT7, LCD_7, 0);
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 0);
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 0);
HAL_GPIO_WritePin(LCD_PORT56, LCD_6, 0);
HAL_GPIO_WritePin(CLOCK_PORT, LCD_CLOCK, 0);
// Reset strategy below based on Wikipedia description, should recover
// from any setting
// Write 0b0011 three times
// (Everyday Practical Electronics says 3 times, Wikipedia says 2 times,
// 3 seems to work better).
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 1);
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 1);
lcd_clock();
lcd_clock();
lcd_clock();
// LCD now guaranteed to be in 8-bit state
// Now write 0b0010 (set to 4-bit mode, ready for first nibble)
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 0);
lcd_clock();
HAL_GPIO_WritePin(Led_LCD_GPIO_Port, Led_LCD_Pin, 1);
}
/* TODO This function should achieve the same as the lcd_write below, however
* it appears to be a little problematic.
* Rather than the LCD_4 and LCD_RS defines, direct integers have to be used
* for proper masks to be calculated.
* Aside from this, setting the RS bit seems to go wrong.
*/
void lcd_write(uint8_t byte, uint8_t rs)
{
// Writes a byte to the display (rs must be either 0 or 1)
//rs=0 comando;; rs=1 dato
// Write second nibble and set RS
if((byte >> 4 ) & 1)
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 1);
else
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 0);
if((byte >> 5 ) & 1)
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 1);
else
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 0);
if((byte >> 6 ) & 1)
HAL_GPIO_WritePin(LCD_PORT56, LCD_6, 1);
else
HAL_GPIO_WritePin(LCD_PORT56, LCD_6, 0);
if((byte >> 7 ) & 1)
HAL_GPIO_WritePin(LCD_PORT7, LCD_7, 1);
else
HAL_GPIO_WritePin(LCD_PORT7, LCD_7, 0);
if(rs)
HAL_GPIO_WritePin(RS_PORT, LCD_RS, 1);
else
HAL_GPIO_WritePin(RS_PORT, LCD_RS, 0);
lcd_clock();
// Write first nibble
if(byte & 1)
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 1);
else
HAL_GPIO_WritePin(LCD_PORT4, LCD_4, 0);
if((byte >> 1 ) & 1)
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 1);
else
HAL_GPIO_WritePin(LCD_PORT56, LCD_5, 0);
if((byte >> 2 ) & 1)
HAL_GPIO_WritePin(LCD_PORT56, LCD_6, 1);
else
HAL_GPIO_WritePin(LCD_PORT56, LCD_6, 0);
if((byte >> 3 ) & 1)
HAL_GPIO_WritePin(LCD_PORT7, LCD_7, 1);
else
HAL_GPIO_WritePin(LCD_PORT7, LCD_7, 0);
lcd_clock();
}
void lcd_clear(void)
{
// Clears display, resets cursor
lcd_write(0b00000001, 0);
_lcd_char = 0;
_lcd_line = 0;
}
void lcd_display_settings(uint8_t on, uint8_t underline, uint8_t blink)
{
// "Display On/Off & Cursor" command. All parameters must be either 0 or 1
lcd_write(0b00001000 | (on << 2) | (underline << 1) | blink, 0);
}
void lcd_display_address(uint8_t address)
{
lcd_write(0b10000000 | address, 0);
}
void lcd_cgram_address(uint8_t address)
{
lcd_write(0b01000000 | address, 0);
}
void lcd_print(char string[])
{
uint8_t i;
for(i = 0; string[i] != 0; i++) {
// If we know the display properties and a newline character is
// present, print the rest of the string on the new line.
if(lcd_lines && string[i] == '\n') {
if(_lcd_line < lcd_lines) {
lcd_display_address(lcd_line_addresses[_lcd_line++]);
_lcd_char = 0;
}
}
else {
// If we know the display properties and have reached the end of
// line, print the rest on the next line
if(lcd_chars)
if((_lcd_char == lcd_chars) && (_lcd_line < lcd_lines)) {
lcd_display_address(lcd_line_addresses[_lcd_line++]);
_lcd_char = 0;
}
lcd_write(string[i], 1);
if(lcd_chars) _lcd_char++;
}
}
}
void writeIntegerToLCD(int integer)
{
// Break down the original number into the thousands, hundreds, tens,
// and ones places and then immediately write that value to the LCD
unsigned char thousands = integer / 1000;
lcd_write( thousands + 0x30,1);
unsigned char hundreds = (integer - thousands*1000) / 100;
lcd_write( hundreds + 0x30,1);
unsigned char tens = (integer - thousands*1000 - hundreds*100 ) / 10;
lcd_write( tens + 0x30,1);
unsigned char ones = (integer - thousands*1000 - hundreds*100 - tens*10);
lcd_write( ones + 0x30,1);
}
void moveToXY(unsigned char row, unsigned char column)
{
// Determine the new position
int position = (row * 16) + column;
// Send the correct commands to the command register of the LCD
if(position < 16)
lcd_write( 0x80 | position,0);
else if(position >= 16 && position < 32)
lcd_write( 0x80 | (position % 16 + 0x40),0);
else if(position >= 41 && position < 60)
lcd_write( 0x80 | (position % 40 + 0x14),0);
else if(position >= 20 && position < 40)
lcd_write( 0x80 | (position % 60 + 0x54),0);
}

427
P3_SETR1/Core/Src/main.c Executable file
View File

@@ -0,0 +1,427 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "hd44780.h"
#include "LPS22.h"
#include "HTS221.h"
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c2;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
uint8_t rxByte;
char displayMode = ' '; // para los comandos
int useFahrenheit = 0; // 0 -> ºC, 1 -> ºF
float maxTemp = -100.0, minTemp = 100.0;
float maxHum = 0.0, minHum = 100.0;
float maxPress = 0.0, minPress = 2000.0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C2_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void __io_putchar(int ch)
{
uint8_t c[1];
c[0] = ch & 0x00ff;
HAL_UART_Transmit(&huart1, &*c, 1, 10);
return ch;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
switch (rxByte) {
case 'm':
case 'n':
displayMode = rxByte;
break;
case 'c':
maxTemp = -100.0; minTemp = 100.0;
maxHum = 0.0; minHum = 100.0;
maxPress = 0.0; minPress = 2000.0;
displayMode = ' ';
break;
case 'u':
useFahrenheit = !useFahrenheit;
break;
default:
displayMode = ' ';
break;
}
HAL_UART_Receive_IT(&huart1, &rxByte, 1);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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_I2C2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
// LCD44780 INIT
lcd_reset();
lcd_display_settings(1,0,0);
lcd_clear();
// LPS22 INIT
LPS22_Init();
// HTS221 INIT
HTS221_Init();
// USART INTERRUPTIONS
HAL_UART_Receive_IT(&huart1, &rxByte, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
uint8_t str[20];
float pressure = LPS22_ReadPress();
THSample ths = HTS221_Read();
if (ths.temp > maxTemp) maxTemp = ths.temp;
if (ths.temp < minTemp) minTemp = ths.temp;
if (ths.hum > maxHum) maxHum = ths.hum;
if (ths.hum < minHum) minHum = ths.hum;
if (pressure > maxPress) maxPress = pressure;
if (pressure < minPress) minPress = pressure;
float tempToShow = useFahrenheit ? (ths.temp * 9.0 / 5.0) + 32 : ths.temp; // convertir a ºF si es necesario
float pressureToShow = useFahrenheit ? pressure * 0.75006 : pressure; // convertir a mmHg si es necesario
char tempUnit = useFahrenheit ? 'F' : 'C';
const char *pressUnit = useFahrenheit ? "mmHg" : "hPa";
switch (displayMode) {
case 'm':
sprintf(str, "Max: %.1f%c %.1f%%", maxTemp, tempUnit, maxHum);
moveToXY(0, 0);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
sprintf(str, "Pres: %.1f%s", maxPress, pressUnit);
moveToXY(1, 1);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
break;
case 'n':
sprintf(str, "Min: %.1f%c %.1f%%", minTemp, tempUnit, minHum);
moveToXY(0, 0);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
sprintf(str, "Pres: %.1f%s", minPress, pressUnit);
moveToXY(1, 1);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
break;
default:
sprintf(str, "T:%.1f%c H:%.1f%%", tempToShow, tempUnit, ths.hum);
moveToXY(0, 0);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
sprintf(str, "Pres: %.1f%s", pressureToShow, pressUnit);
moveToXY(1, 1);
lcd_print(str);
printf("%s\n\r", str); // Enviar a consola UART
break;
}
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @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
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C2 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C2_Init(void)
{
/* USER CODE BEGIN I2C2_Init 0 */
/* USER CODE END I2C2_Init 0 */
/* USER CODE BEGIN I2C2_Init 1 */
/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.Timing = 0x10D19CE4;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, Led_LCD_Pin|D4_LCD_Pin|D7_LCD_Pin|E_LCD_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, D6_LCD_Pin|RS_LCD_Pin|D5_LCD_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : Led_LCD_Pin D4_LCD_Pin D7_LCD_Pin E_LCD_Pin */
GPIO_InitStruct.Pin = Led_LCD_Pin|D4_LCD_Pin|D7_LCD_Pin|E_LCD_Pin;
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 pins : D6_LCD_Pin RS_LCD_Pin D5_LCD_Pin */
GPIO_InitStruct.Pin = D6_LCD_Pin|RS_LCD_Pin|D5_LCD_Pin;
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);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @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 */

View File

@@ -0,0 +1,242 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32l4xx_hal_msp.c
* @brief This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* Copyright (c) 2025 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*/
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspInit 0 */
/* USER CODE END I2C2_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C2;
PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB11 ------> I2C2_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C2_CLK_ENABLE();
/* USER CODE BEGIN I2C2_MspInit 1 */
/* USER CODE END I2C2_MspInit 1 */
}
}
/**
* @brief I2C MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspDeInit 0 */
/* USER CODE END I2C2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C2_CLK_DISABLE();
/**I2C2 GPIO Configuration
PB10 ------> I2C2_SCL
PB11 ------> I2C2_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11);
/* USER CODE BEGIN I2C2_MspDeInit 1 */
/* USER CODE END I2C2_MspDeInit 1 */
}
}
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**USART1 GPIO Configuration
PB6 ------> USART1_TX
PB7 ------> USART1_RX
*/
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_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
/**
* @brief UART MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PB6 ------> USART1_TX
PB7 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
/* USART1 interrupt DeInit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

217
P3_SETR1/Core/Src/stm32l4xx_it.c Executable file
View File

@@ -0,0 +1,217 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32l4xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* Copyright (c) 2025 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 "stm32l4xx_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 UART_HandleTypeDef huart1;
/* 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 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32L4xx 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_stm32l4xx.s). */
/******************************************************************************/
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

176
P3_SETR1/Core/Src/syscalls.c Executable file
View File

@@ -0,0 +1,176 @@
/**
******************************************************************************
* @file syscalls.c
* @author Auto-generated by STM32CubeIDE
* @brief STM32CubeIDE 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 <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* 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;
}

79
P3_SETR1/Core/Src/sysmem.c Executable file
View File

@@ -0,0 +1,79 @@
/**
******************************************************************************
* @file sysmem.c
* @author Generated by STM32CubeIDE
* @brief STM32CubeIDE 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 <errno.h>
#include <stdint.h>
/**
* 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;
}

View File

@@ -0,0 +1,332 @@
/**
******************************************************************************
* @file system_stm32l4xx.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_stm32l4xx.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 MSI (4 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32l4xx.s" file, to
* configure the system clock before to branch to main program.
*
* This file configures the system clock as follows:
*=============================================================================
*-----------------------------------------------------------------------------
* System Clock source | MSI
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 4000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 4000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 1
*-----------------------------------------------------------------------------
* APB2 Prescaler | 1
*-----------------------------------------------------------------------------
* PLL_M | 1
*-----------------------------------------------------------------------------
* PLL_N | 8
*-----------------------------------------------------------------------------
* PLL_P | 7
*-----------------------------------------------------------------------------
* PLL_Q | 2
*-----------------------------------------------------------------------------
* PLL_R | 2
*-----------------------------------------------------------------------------
* PLLSAI1_P | NA
*-----------------------------------------------------------------------------
* PLLSAI1_Q | NA
*-----------------------------------------------------------------------------
* PLLSAI1_R | NA
*-----------------------------------------------------------------------------
* PLLSAI2_P | NA
*-----------------------------------------------------------------------------
* PLLSAI2_Q | NA
*-----------------------------------------------------------------------------
* PLLSAI2_R | NA
*-----------------------------------------------------------------------------
* Require 48MHz for USB OTG FS, | Disabled
* SDIO and RNG clock |
*-----------------------------------------------------------------------------
*=============================================================================
******************************************************************************
* @attention
*
* Copyright (c) 2017 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 stm32l4xx_system
* @{
*/
/** @addtogroup STM32L4xx_System_Private_Includes
* @{
*/
#include "stm32l4xx.h"
/**
* @}
*/
/** @addtogroup STM32L4xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32L4xx_System_Private_Defines
* @{
*/
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (MSI_VALUE)
#define MSI_VALUE 4000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* MSI_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
/* 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 SRAM1_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 STM32L4xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32L4xx_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 = 4000000U;
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};
const uint32_t MSIRangeTable[12] = {100000U, 200000U, 400000U, 800000U, 1000000U, 2000000U, \
4000000U, 8000000U, 16000000U, 24000000U, 32000000U, 48000000U};
/**
* @}
*/
/** @addtogroup STM32L4xx_System_Private_FunctionPrototypes
* @{
*/
/**
* @}
*/
/** @addtogroup STM32L4xx_System_Private_Functions
* @{
*/
/**
* @brief Setup the microcontroller system.
* @retval None
*/
void SystemInit(void)
{
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#endif
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
#endif
}
/**
* @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 MSI, SystemCoreClock will contain the MSI_VALUE(*)
*
* - 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(*) or MSI_VALUE(*) multiplied/divided by the PLL factors.
*
* (*) MSI_VALUE is a constant defined in stm32l4xx_hal.h file (default value
* 4 MHz) but the real value may vary depending on the variations
* in voltage and temperature.
*
* (**) HSI_VALUE is a constant defined in stm32l4xx_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 stm32l4xx_hal.h file (default value
* 8 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.
*
* @retval None
*/
void SystemCoreClockUpdate(void)
{
uint32_t tmp, msirange, pllvco, pllsource, pllm, pllr;
/* Get MSI Range frequency--------------------------------------------------*/
if ((RCC->CR & RCC_CR_MSIRGSEL) == 0U)
{ /* MSISRANGE from RCC_CSR applies */
msirange = (RCC->CSR & RCC_CSR_MSISRANGE) >> 8U;
}
else
{ /* MSIRANGE from RCC_CR applies */
msirange = (RCC->CR & RCC_CR_MSIRANGE) >> 4U;
}
/*MSI frequency range in HZ*/
msirange = MSIRangeTable[msirange];
/* Get SYSCLK source -------------------------------------------------------*/
switch (RCC->CFGR & RCC_CFGR_SWS)
{
case 0x00: /* MSI used as system clock source */
SystemCoreClock = msirange;
break;
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 or MSI_VALUE/ PLLM) * PLLN
SYSCLK = PLL_VCO / PLLR
*/
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4U) + 1U ;
switch (pllsource)
{
case 0x02: /* HSI used as PLL clock source */
pllvco = (HSI_VALUE / pllm);
break;
case 0x03: /* HSE used as PLL clock source */
pllvco = (HSE_VALUE / pllm);
break;
default: /* MSI used as PLL clock source */
pllvco = (msirange / pllm);
break;
}
pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8U);
pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25U) + 1U) * 2U;
SystemCoreClock = pllvco/pllr;
break;
default:
SystemCoreClock = msirange;
break;
}
/* Compute HCLK clock frequency --------------------------------------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/