【2014-11-23】《The Hardware/Software Interface》– Section 8 (continue)

  • Exceptions:
    • An exception is transfer of control to the operating system in response to some event.
    • exception processing by exception handler
    • image

image

image

  1. Process(进程)
  • Process abstraction provides an interface between the program and the underlying CPU + Memory.
  • Definition: A process is an instance of a running program
  • Process provides each program with two key abstractions:
    • Logical control flow
      • Each process seems to have exclusive use of the CPU
    • Private virtual address space
      • Each process seems to have exclusive use of main memory
Context Switching
  • Processes are managed by a shared chunk of OS code called the kernel
  • Control flow passes from one process to another via a context switch
  • image
Creating New Processes & Programs

image

转载于:https://www.cnblogs.com/sjtujoe/p/4116541.html

/* 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" #include "adc.h" #include "i2c.h" #include "tim.h" #include "gpio.h" #include <stdio.h> #include <math.h> /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "oled.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ #define CHARGE_PIN GPIO_PIN_1 #define CHARGE_PORT GPIOA #define ADC_THRESHOLD 2048 // VCC/2 #define MEASURE_COUNT 5 // 测量次数 #define DISCHARGE_DELAY 1 // 放电时间(ms) #define CALIBRATION_SAMPLES 32 // 寄生电容校准采样次数 #define PARASITIC_CAP_ADDR 0x0800F000 // FLASH存储地址(最后1KB) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ char disp_buf[20]; float parasitic_cap = 0.0f; // 存储寄生电容值 uint8_t calibration_flag = 0; // 校准标志位 /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ void calibrate_parasitic_capacitance(void); void save_calibration_to_flash(float value); float load_calibration_from_flash(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ float measure_raw_capacitance(void); float measure_capacitance(void); void SystemClock_Config(void); /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ /** * @brief 电容测量函数 * @retval 电容值(pF) */ float measure_capacitance(void) { uint32_t total_time = 0; const float R = 1000.0f; // 1kΩ电阻 for (uint8_t i = 0; i < MEASURE_COUNT; i++) { // 放电 HAL_GPIO_WritePin(CHARGE_PORT, CHARGE_PIN, GPIO_PIN_RESET); HAL_Delay(DISCHARGE_DELAY); // 开始充电和计时 __HAL_TIM_SET_COUNTER(&htim2, 0); HAL_TIM_Base_Start(&htim2); HAL_GPIO_WritePin(CHARGE_PORT, CHARGE_PIN, GPIO_PIN_SET); // 等待电压达到阈值 while (1) { HAL_ADC_Start(&hadc1); if (HAL_ADC_PollForConversion(&hadc1, 1) != HAL_OK) continue; uint32_t adc_val = HAL_ADC_GetValue(&hadc1); uint32_t time_cnt = __HAL_TIM_GET_COUNTER(&htim2); if (adc_val >= ADC_THRESHOLD || time_cnt > 100000) { total_time += time_cnt; break; } } HAL_TIM_Base_Stop(&htim2); HAL_Delay(1); } // 计算平均时间和电容值 float avg_time = (float)total_time / (MEASURE_COUNT * 1000000.0f); // 转换为秒 return (avg_time / (R * 0.693147f)) * 1e12f * 0.98f; // 返回pF值(含校准系数) } /** * @brief 寄生电容校准(空载时调用) */ void calibrate_parasitic_capacitance(void) { float sum = 0.0f; OLED_Clear(); OLED_ShowString(0, 0, "Calibrating..."); for (int i = 0; i < CALIBRATION_SAMPLES; i++) { float raw = measure_raw_capacitance(); sum += raw; HAL_Delay(50); } parasitic_cap = sum / CALIBRATION_SAMPLES; save_calibration_to_flash(parasitic_cap); OLED_Clear(); sprintf(disp_buf, "Cp=%.1f pF", parasitic_cap); OLED_ShowString(0, 0, disp_buf); HAL_Delay(2000); } /** * @brief 带寄生补偿的电容测量 * @retval 补偿后的电容值(pF) */ float measure_compensated_capacitance(void) { float raw_val = measure_raw_capacitance(); return raw_val - parasitic_cap; // 关键补偿步骤 } /** * @brief 保存校准值到FLASH */ void save_calibration_to_flash(float value) { HAL_FLASH_Unlock(); uint32_t data = *(uint32_t*)&value; FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_3); HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, PARASITIC_CAP_ADDR, data); HAL_FLASH_Lock(); } /** * @brief 从FLASH加载校准值 */ float load_calibration_from_flash(void) { uint32_t data = *(__IO uint32_t*)PARASITIC_CAP_ADDR; return *(float*)&data; } 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_ADC1_Init(); MX_I2C1_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ // OLED初始化 OLED_Init(); OLED_Clear(); OLED_ShowString(0, 0, "Cap Meter 1k"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { float cap_val = measure_capacitance(); // 自动切换单位显示 if (cap_val < 1000) { if (cap_val < 100) { sprintf(disp_buf, "C=%.1f pF", cap_val); } else { sprintf(disp_buf, "C=%.0f pF", cap_val); } } else { sprintf(disp_buf, "C=%.3f nF", cap_val / 1000.0f); } OLED_ShowString(0, 2, disp_buf); HAL_Delay(200); /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; 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_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* 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 */ 不用flash,修改此程序,减小寄生电容效应
07-15
语句 `if [ $(sed -n '/vendor.xiaomi.hardware.cld_aidl-V1/'p 'vendor/sprd/treble/interface/aidl/current.txt' | cut -d " " -f 1) = $(tail -1 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash') ]` 存在以下几个问题: #### 1. 空值问题 如果 `sed -n '/vendor.xiaomi.hardware.cld_aidl-V1/'p 'vendor/sprd/treble/interface/aidl/current.txt' | cut -d " " -f 1` 或者 `tail -1 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash'` 的结果为空,`[` 测试命令会出现错误。例如,当 `sed` 没有匹配到任何行时,`cut` 的输入为空,其输出也为空;当 `.hash` 文件为空时,`tail` 的输出也为空。这会导致 `[` 后面的参数不完整,引发 `unary operator expected` 错误。 #### 2. 引号问题 在进行字符串比较时,应该用双引号将命令替换的结果括起来,以防止结果中包含空格或其他特殊字符时,被 shell 错误解析。例如,如果 `sed` 或 `tail` 的结果包含空格,没有引号的情况下,shell 会将其拆分成多个参数,导致比较逻辑出错。 #### 3. 文件不存在问题 如果 `vendor/sprd/treble/interface/aidl/current.txt` 或 `vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash` 文件不存在,`sed` 和 `tail` 命令会产生错误输出,可能会影响比较结果或导致脚本崩溃。 以下是修正后的代码示例: ```bash # 保存 sed 命令的输出 sed_output=$(sed -n '/vendor.xiaomi.hardware.cld_aidl-V1/'p 'vendor/sprd/treble/interface/aidl/current.txt' | cut -d " " -f 1) # 保存 tail 命令的输出 tail_output=$(tail -1 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash') # 检查文件是否存在 if [ ! -f 'vendor/sprd/treble/interface/aidl/current.txt' ] || [ ! -f 'vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/aidl_api/vendor.xiaomi.hardware.cld_aidl/1/.hash' ]; then echo "One or both of the files do not exist." exit 1 fi # 检查输出是否为空 if [ -z "$sed_output" ] || [ -z "$tail_output" ]; then echo "One of the commands produced an empty output." exit 1 fi # 进行比较 if [ "$sed_output" = "$tail_output" ]; then touch out_vendor/soong/.intermediates/vendor/xiaomi/proprietary/memory_odm_tools/cld_aidl/vendor.xiaomi.hardware.cld_aidl-api/checkhash_1_sprd.timestamp else exit 1 fi ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值