六、MCU信息

1.mcu_info.h

/**
 * @file mcu_info.h
 * @author JohnWay (1973372075@qq.com)
 * @brief
 * @version 0.1
 * @date 2024-05-26
 *
 * @copyright Copyright (c) 2024 JohnWay
 * SPDX-License-Identifier: Apache-2.0
 *
 * @encoding UTF-8
 *
 * @changes	author		date				description
 * 			JohnWay		2024-05-26			fitst version
 *
 */

#ifndef MCU_INFO_H
#define MCU_INFO_H

#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

#define SRAM_SIZE ((uint32_t)(20 * 1024))
#define SRAM_START ((uint32_t)(0x20000000))
#define SRAM_END (SRAM_START + SRAM_START)

#define FLASH_PAGE_SIZE ((uint32_t)(1024))
#define FLASH_SIZE ((uint32_t)(64 * 1024))
#define FLASH_START ((uint32_t)(0x08000000))
#define FLASH_END (FLASH_START + FLASH_SIZE)

#define FLASH_SIZE_REGISTER_BASE_ADDRESS ((uint32_t)(0x1FFFF7E0))
#define UID_REGISTER_BASE_ADDRESS ((uint32_t)(0x1FFFF7E8))

    typedef struct
    {
        uint16_t uid_15_0;
        uint16_t uid_31_16;
        uint32_t uid_63_32;
        uint32_t uid_95_64;
    } mcu_uid_t;

    typedef struct
    {
        mcu_uid_t uid;
		uint32_t sram_size;
        uint32_t flash_size;
		uint16_t sram_usage;
		uint16_t flash_usage;
    } mcu_info_t;

    uint16_t mcu_get_flash_size(void);
    void mcu_get_uid(mcu_uid_t *uid);
	uint16_t mcu_get_sram_usage(void);
	uint16_t mcu_get_flash_usage(void);
	void mcu_get_info(mcu_info_t *info);
	void mcu_info_print(mcu_info_t *info);

#ifdef __cplusplus
}
#endif

#endif

2.mcu_info.c

/**
 * @file mcu_info.c
 * @author JohnWay (1973372075@qq.com)
 * @brief
 * @version 0.1
 * @date 2024-05-26
 *
 * @copyright Copyright (c) 2024 JohnWay
 * SPDX-License-Identifier: Apache-2.0
 *
 * @encoding UTF-8
 *
 * @changes	author		date				description
 * 			JohnWay		2024-05-26			fitst version
 *
 */

#include "mcu_info.h"
#include "log.h"

uint16_t mcu_get_flash_size(void)
{
    return (*(uint32_t *)FLASH_SIZE_REGISTER_BASE_ADDRESS);
}

void mcu_get_uid(mcu_uid_t *uid)
{
    uid->uid_15_0 = *(uint16_t *)(UID_REGISTER_BASE_ADDRESS);
    uid->uid_31_16 = *(uint16_t *)(UID_REGISTER_BASE_ADDRESS + 2);
    uid->uid_63_32 = *(uint32_t *)(UID_REGISTER_BASE_ADDRESS + 4);
    uid->uid_95_64 = *(uint32_t *)(UID_REGISTER_BASE_ADDRESS + 8);
}

uint16_t mcu_get_sram_usage(void)
{
	uint32_t used_sram = 0;
	uint16_t ret = 0;
	
	used_sram = *(uint32_t *)FLASH_START - SRAM_START;
	
	ret = ((float)used_sram / (float)SRAM_SIZE) * 10000;
	
	return ret;
}

uint16_t mcu_get_flash_usage(void)
{
	uint32_t page_address = 0, word = 0;
	uint16_t ret = 0;
	
	page_address = FLASH_START;
	
	for (uint32_t i = FLASH_START; i < FLASH_END; i += 4)
	{
		word = *(uint32_t *)page_address;
		if (word != 0xFFFFFFFF)
		{
			page_address += 4;
		}
		else
		{
			break;
		}
	}
	
	ret = ((float)(page_address - FLASH_START) / (float)FLASH_SIZE) * 10000;
	
	return ret;
}

void mcu_get_info(mcu_info_t *info)
{
    mcu_get_uid(&info->uid);
	info->sram_size = SRAM_SIZE;
	info->flash_size = FLASH_SIZE; // mcu_get_flash_size();
	info->sram_usage = mcu_get_sram_usage();
	info->flash_usage = mcu_get_flash_usage();
}

void mcu_info_print(mcu_info_t *info)
{
	LOG_I("==============================");
	LOG_I("MCU INFO");
	LOG_I("UID: 0x%x%x%x%x", info->uid.uid_95_64, info->uid.uid_63_32, info->uid.uid_31_16, info->uid.uid_15_0);
	LOG_I("SRAM SIZE: %d KBytes", info->sram_size);
	LOG_I("FLASH SIZE: %d KBytes", info->flash_size);
	LOG_I("SRAM Usage: %d.%d%%", info->sram_usage / 100, info->sram_usage % 100);
	LOG_I("FLASH Usage: %d.%d%%", info->flash_usage / 100, info->flash_usage % 100);
	LOG_I("==============================");
}

3.main.c

/* 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 "gpio.h"
#include "i2c.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "blink.h"
#include "log.h"
#include "mcu_info.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 ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
uint32_t sys_tick = 0;
mcu_info_t mcu_info;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void blink_ioctl(uint8_t cmd);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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. */
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

    /* System interrupt init*/
    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

    /* SysTick_IRQn interrupt configuration */
    NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 15, 0));

    /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
     */
    LL_GPIO_AF_Remap_SWJ_NOJTAG();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */
    LL_SYSTICK_EnableIT();
    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();
    /* USER CODE BEGIN 2 */
    blink_t blink = {
        .on_time = 100,
        .off_time = 900,
        .ioctl = blink_ioctl,
    };
	mcu_get_info(&mcu_info);
	mcu_info_print(&mcu_info);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
		blink_update(&blink, sys_tick);
    }
    /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
    LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
    while (LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
    {
    }
    LL_RCC_HSE_Enable();

    /* Wait till HSE is ready */
    while (LL_RCC_HSE_IsReady() != 1)
    {
    }
    LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
    LL_RCC_PLL_Enable();

    /* Wait till PLL is ready */
    while (LL_RCC_PLL_IsReady() != 1)
    {
    }
    LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
    LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
    LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
    LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);

    /* Wait till System clock is ready */
    while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
    {
    }
    LL_Init1msTick(72000000);
    LL_SetSystemCoreClock(72000000);
}

/* USER CODE BEGIN 4 */
void blink_ioctl(uint8_t cmd)
{
    if (cmd == BLINK_CMD_OFF)
    {
        LL_GPIO_SetOutputPin(SYS_LED_GPIO_Port, SYS_LED_Pin);
    }
    else
    {
        LL_GPIO_ResetOutputPin(SYS_LED_GPIO_Port, SYS_LED_Pin);
    }
}
/* 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 */

4.打印信息

 5.RM0008.pdf

因为 Flash 分 low, medium, high, 读取该地址的数值是 0x0080,而 STM32F103C8T6 是 64K

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值