Busybox init启动详解

需积分: 10 1 下载量 8 浏览量 更新于2024-09-11 收藏 8KB TXT 举报
" Busybox init启动详解" Busybox是一款集成了大量命令工具的轻量级Linux发行版,主要用于嵌入式系统。它遵循GPLv2许可证,由Alain Williams创建并维护,旨在提供一个小型但功能丰富的环境,适用于有限存储空间的设备。Busybox将众多常用的Linux工具集成到一个单一可执行文件中,大大减少了体积,使其通常小于1MB,甚至在某些情况下只有几百KB。因此,Busybox成为了许多小型Linux系统的核心组件。 在Linux系统中,`init`是第一个运行的进程,它的主要任务是初始化系统并启动各种服务。在传统的Linux系统中,`init`通常是由System V或Upstart等初始化系统来执行。而在 Busybox 中,`init`也被用作这个目的,但它具有更简洁的实现方式。 Busybox的`init`功能在`kernel/init/main.c`文件中的`init`函数中体现。当系统启动时,如果存在`execute_command`变量(例如,通过`bootloader`传递的`init=`参数),则会尝试执行这个命令。若`execute_command`不存在,系统会默认执行`/sbin/init`。在某些情况下,`execute_command`可能会被设置为像`linuxrc`这样的脚本,这意味着Busybox会安装到对应的目录下,并用`/bin/busybox`执行该脚本,如果`linuxrc`未定义或无法执行,`/sbin/init`(即Busybox的`init`)将作为最后的备选方案启动。 Busybox的`init`不仅执行简单的启动任务,它还可以作为一个完整的初始化系统,处理系统的各个阶段。这包括读取配置文件(如`/etc/inittab`),根据配置启动不同的运行级别(Runlevel)服务,以及管理终端和后台进程。`inittab`是System V风格的初始化系统配置文件,用于定义不同运行级别的行为,例如启动哪些守护进程和服务。 在 Busybox 中,`inittab`的解析和处理位于`init/init.c`文件中,它会按照配置文件的指令启动相应的程序和服务。这使得Busybox能够根据不同的应用场景进行定制,适应各种嵌入式设备的需求。 Busybox的`init`启动机制是一种高效且灵活的方法,它简化了传统Linux初始化过程,同时保持了足够的功能来满足基本的系统初始化和管理需求。通过这种紧凑的实现,Busybox可以在资源受限的环境中提供强大的功能,成为许多嵌入式和物联网设备的理想选择。

怎么使用这个函数初始化串口3HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef huart) { / Check the UART handle allocation / if (huart == NULL) { return HAL_ERROR; } / Check the parameters / if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) { / The hardware flow control is available only for USART1, USART2, USART3 and USART6. Except for STM32F446xx devices, that is available for USART1, USART2, USART3, USART6, UART4 and UART5. / assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); } else { assert_param(IS_UART_INSTANCE(huart->Instance)); } assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); if (huart->gState == HAL_UART_STATE_RESET) { / Allocate lock resource and initialize it / huart->Lock = HAL_UNLOCKED; #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) UART_InitCallbacksToDefault(huart); if (huart->MspInitCallback == NULL) { huart->MspInitCallback = HAL_UART_MspInit; } / Init the low level hardware / huart->MspInitCallback(huart); #else / Init the low level hardware : GPIO, CLOCK / HAL_UART_MspInit(huart); #endif / (USE_HAL_UART_REGISTER_CALLBACKS) / } huart->gState = HAL_UART_STATE_BUSY; / Disable the peripheral / __HAL_UART_DISABLE(huart); / Set the UART Communication parameters / UART_SetConfig(huart); / In asynchronous mode, the following bits must be kept cleared: - LINEN and CLKEN bits in the USART_CR2 register, - SCEN, HDSEL and IREN bits in the USART_CR3 register./ CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); / Enable the peripheral / __HAL_UART_ENABLE(huart); / Initialize the UART state */ huart->ErrorCode = HAL_UART_ERROR_NONE; huart->gState = HAL_UART_STATE_READY; huart->RxState = HAL_UART_STATE_READY; return HAL_OK; }

2023-05-24 上传

HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) { /* Check the UART handle allocation */ if (huart == NULL) { return HAL_ERROR; } /* Check the parameters */ if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) { /* The hardware flow control is available only for USART1, USART2, USART3 and USART6. Except for STM32F446xx devices, that is available for USART1, USART2, USART3, USART6, UART4 and UART5. */ assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); } else { assert_param(IS_UART_INSTANCE(huart->Instance)); } assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); if (huart->gState == HAL_UART_STATE_RESET) { /* Allocate lock resource and initialize it */ huart->Lock = HAL_UNLOCKED; #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) UART_InitCallbacksToDefault(huart); if (huart->MspInitCallback == NULL) { huart->MspInitCallback = HAL_UART_MspInit; } /* Init the low level hardware */ huart->MspInitCallback(huart); #else /* Init the low level hardware : GPIO, CLOCK */ HAL_UART_MspInit(huart); #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ } huart->gState = HAL_UART_STATE_BUSY; /* Disable the peripheral */ __HAL_UART_DISABLE(huart); /* Set the UART Communication parameters */ UART_SetConfig(huart); /* In asynchronous mode, the following bits must be kept cleared: - LINEN and CLKEN bits in the USART_CR2 register, - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); /* Enable the peripheral */ __HAL_UART_ENABLE(huart); /* Initialize the UART state */ huart->ErrorCode = HAL_UART_ERROR_NONE; huart->gState = HAL_UART_STATE_READY; huart->RxState = HAL_UART_STATE_READY; return HAL_OK; }

2023-05-24 上传
2023-07-14 上传