/**
******************************************************************************
* @file stm8s_tim1.c
* @author MCD Application Team
* @version V2.1.0
* @date 18-November-2011
* @brief This file contains all the functions for the TIM1 peripheral.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_tim1.h"
/** @addtogroup STM8S_StdPeriph_Driver
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(uint8_t TIM1_ICPolarity, uint8_t TIM1_ICSelection,
uint8_t TIM1_ICFilter);
static void TI2_Config(uint8_t TIM1_ICPolarity, uint8_t TIM1_ICSelection,
uint8_t TIM1_ICFilter);
static void TI3_Config(uint8_t TIM1_ICPolarity, uint8_t TIM1_ICSelection,
uint8_t TIM1_ICFilter);
static void TI4_Config(uint8_t TIM1_ICPolarity, uint8_t TIM1_ICSelection,
uint8_t TIM1_ICFilter);
/**
* @addtogroup TIM1_Public_Functions
* @{
*/
/**
* @brief Deinitializes the TIM1 peripheral registers to their default reset values.
* @param None
* @retval None
*/
void TIM1_DeInit(void)
{
TIM1->CR1 = TIM1_CR1_RESET_VALUE;
TIM1->CR2 = TIM1_CR2_RESET_VALUE;
TIM1->SMCR = TIM1_SMCR_RESET_VALUE;
TIM1->ETR = TIM1_ETR_RESET_VALUE;
TIM1->IER = TIM1_IER_RESET_VALUE;
TIM1->SR2 = TIM1_SR2_RESET_VALUE;
/* Disable channels */
TIM1->CCER1 = TIM1_CCER1_RESET_VALUE;
TIM1->CCER2 = TIM1_CCER2_RESET_VALUE;
/* Configure channels as inputs: it is necessary if lock level is equal to 2 or 3 */
TIM1->CCMR1 = 0x01;
TIM1->CCMR2 = 0x01;
TIM1->CCMR3 = 0x01;
TIM1->CCMR4 = 0x01;
/* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
TIM1->CCER1 = TIM1_CCER1_RESET_VALUE;
TIM1->CCER2 = TIM1_CCER2_RESET_VALUE;
TIM1->CCMR1 = TIM1_CCMR1_RESET_VALUE;
TIM1->CCMR2 = TIM1_CCMR2_RESET_VALUE;
TIM1->CCMR3 = TIM1_CCMR3_RESET_VALUE;
TIM1->CCMR4 = TIM1_CCMR4_RESET_VALUE;
TIM1->CNTRH = TIM1_CNTRH_RESET_VALUE;
TIM1->CNTRL = TIM1_CNTRL_RESET_VALUE;
TIM1->PSCRH = TIM1_PSCRH_RESET_VALUE;
TIM1->PSCRL = TIM1_PSCRL_RESET_VALUE;
TIM1->ARRH = TIM1_ARRH_RESET_VALUE;
TIM1->ARRL = TIM1_ARRL_RESET_VALUE;
TIM1->CCR1H = TIM1_CCR1H_RESET_VALUE;
TIM1->CCR1L = TIM1_CCR1L_RESET_VALUE;
TIM1->CCR2H = TIM1_CCR2H_RESET_VALUE;
TIM1->CCR2L = TIM1_CCR2L_RESET_VALUE;
TIM1->CCR3H = TIM1_CCR3H_RESET_VALUE;
TIM1->CCR3L = TIM1_CCR3L_RESET_VALUE;
TIM1->CCR4H = TIM1_CCR4H_RESET_VALUE;
TIM1->CCR4L = TIM1_CCR4L_RESET_VALUE;
TIM1->OISR = TIM1_OISR_RESET_VALUE;
TIM1->EGR = 0x01; /* TIM1_EGR_UG */
TIM1->DTR = TIM1_DTR_RESET_VALUE;
TIM1->BKR = TIM1_BKR_RESET_VALUE;
TIM1->RCR = TIM1_RCR_RESET_VALUE;
TIM1->SR1 = TIM1_SR1_RESET_VALUE;
}
/**
* @brief Initializes the TIM1 Time Base Unit according to the specified parameters.
* @param TIM1_Prescaler specifies the Prescaler value.
* @param TIM1_CounterMode specifies the counter mode from @ref TIM1_CounterMode_TypeDef .
* @param TIM1_Period specifies the Period value.
* @param TIM1_RepetitionCounter specifies the Repetition counter value
* @retval None
*/
void TIM1_TimeBaseInit(uint16_t TIM1_Prescaler,
TIM1_CounterMode_TypeDef TIM1_CounterMode,
uint16_t TIM1_Period,
uint8_t TIM1_RepetitionCounter)
{
/* Check parameters */
assert_param(IS_TIM1_COUNTER_MODE_OK(TIM1_CounterMode));
/* Set the Autoreload value */
TIM1->ARRH = (uint8_t)(TIM1_Period >> 8);
TIM1->ARRL = (uint8_t)(TIM1_Period);
/* Set the Prescaler value */
TIM1->PSCRH = (uint8_t)(TIM1_Prescaler >> 8);
TIM1->PSCRL = (uint8_t)(TIM1_Prescaler);
/* Select the Counter Mode */
TIM1->CR1 = (uint8_t)((uint8_t)(TIM1->CR1 & (uint8_t)(~(TIM1_CR1_CMS | TIM1_CR1_DIR)))
| (uint8_t)(TIM1_CounterMode));
/* Set the Repetition Counter value */
TIM1->RCR = TIM1_RepetitionCounter;
}
/**
* @brief Initializes the TIM1 Channel1 according to the specified parameters.
* @param TIM1_OCMode specifies the Output Compare mode from
* @ref TIM1_OCMode_TypeDef.
* @param TIM1_OutputState specifies the Output State from
* @ref TIM1_OutputState_TypeDef.
* @param TIM1_OutputNState specifies the Complementary Output State
* from @ref TIM1_OutputNState_TypeDef.
* @param TIM1_Pulse specifies the Pulse width value.
* @param TIM1_OCPolarity specifies the Output Compare Polarity from
* @ref TIM1_OCPolarity_TypeDef.
* @param TIM1_OCNPolarity specifies the Complementary Output Compare Polarity
* from @ref TIM1_OCNPolarity_TypeDef.
* @param TIM1_OCIdleState specifies the Output Compare Idle State from
* @ref TIM1_OCIdleState_TypeDef.
* @param TIM1_OCNIdleState specifies the Complementary Output Compare Idle
* State from @ref TIM1_OCIdleState_TypeDef.
* @retval None
*/
void TIM1_OC1Init(TIM1_OCMode_TypeDef TIM1_OCMode,
TIM1_OutputState_TypeDef TIM1_OutputState,
TIM1_OutputNState_TypeDef TIM1_OutputNState,
uint16_t TIM1_Pulse,
TIM1_OCPolarity_TypeDef TIM1_OCPolarity,
TIM1_OCNPolarity_TypeDef TIM1_OCNPolarity,
TIM1_OCIdleState_TypeDef TIM1_OCIdleState,
TIM1_OCNIdleState_TypeDef TIM1_OCNIdleState)
{
/* Check the parameters */
assert_param(IS_TIM1_OC_MODE_OK(TIM1_OCMode));
assert_param(IS_TIM1_OUTPUT_STATE_OK(TIM1_OutputState));
assert_param(IS_TIM1_OUTPUTN_STATE_OK(TIM1_OutputNState));
assert_param(IS_TIM1_OC_POLARITY_OK(TIM1_OCPolarity));
assert_param(IS_TIM1_OCN_POLARITY_OK(TIM1_OCNPolarity));
assert_param(IS_TIM1_OCIDLE_STATE_OK(TIM1_OCIdleState));
assert_param(IS_TIM1_OCNIDLE_STATE_OK(TIM1_OCNIdleState));
/* Disable the Channel 1: Reset the CCE Bit, Set the Output State ,
the Output N State, the Output Polarity & the Output N Polarity*/
TIM1->CCER1 &= (uint8_t)(~( TIM1_CCER1_CC1E | TIM1_CCER1_CC1NE
| TIM1_CCER1_CC1P | TIM1_CCER1_CC1NP));
/* Set the Output State & Set the Output N State & Set the Output Polarity &
Set the Output N Polarity */
TIM1->CCER1 |= (uint8_t)((uint8_t)((uint8_t)(TIM1_OutputState & TIM1_CCER1_CC1E)
| (uint8_t)(TIM1_OutputNState & TIM1_CCER1_CC1NE))
| (uint8_t)( (uint8_t)(TIM1_OCPolarity & TIM1_CCER1_CC1P)
| (uint8_t)(TIM1_OCNPolarity & TIM1_CCER1_CC1NP))
STM8 软件USB HID
需积分: 0 53 浏览量
更新于2023-11-29
收藏 208KB ZIP 举报
STM8 软件USB HID 是一种在STM8微控制器上实现USB Human Interface Device (HID)类协议的技术。STM8是意法半导体(STMicroelectronics)推出的一种8位微控制器系列,以其高效能和低功耗而受到广泛应用。USB HID主要用于创建与计算机交互的人机接口设备,如键盘、鼠标、游戏控制器等。这些设备可以直接通过USB接口与主机通信,无需驱动程序,因为Windows、Mac OS X以及Linux等主流操作系统都内置了HID类驱动。
在STM8S-USB项目中,开发者可以利用STM8微控制器的资源来构建一个USB设备,该设备能够模拟HID设备的行为。这通常涉及到以下关键知识点:
1. **USB协议**:USB是一种通用串行总线,用于连接各种外部设备到计算机系统。它提供了数据传输、电源管理和设备识别等功能。USB协议定义了不同类别的设备,HID类就是其中之一。
2. **HID类协议**:HID类定义了一套标准的数据格式和报告描述符,使得设备能够向主机发送输入数据(如鼠标移动或按键)和接收输出数据(如LED状态)。HID设备的固件必须遵循这些协议来确保兼容性。
3. **STM8微控制器**:STM8系列微控制器具有集成的USB接口,使得在低成本、低功耗的8位设备上实现USB功能成为可能。STM8S-USB项目通常会利用STM8的内部定时器、中断和I/O端口来处理USB通信。
4. **固件开发**:为了实现HID功能,需要编写固件代码。这包括设置USB控制器的配置寄存器、处理USB事务、解析主机命令以及生成HID报告。通常使用ST提供的库函数和示例代码作为起点。
5. **报告描述符**:每个HID设备都需要一个报告描述符,它定义了设备能发送和接收的数据类型。编写和正确配置报告描述符是实现HID设备的关键步骤。
6. **中断驱动**:由于USB通信是实时的,因此通常使用中断驱动模式。当USB事件发生时,中断服务程序会处理这些事件,如数据传输、枚举过程或错误处理。
7. **枚举过程**:USB设备连接到主机后,会经历一个枚举过程,主机通过这个过程识别设备并加载适当的驱动。在STM8HID设备中,固件必须正确响应主机的枚举请求,提供设备信息和配置。
8. **调试工具**:开发过程中,USB协议分析仪和逻辑分析仪等工具可以帮助调试USB通信,确保设备与主机之间的交互符合HID规范。
9. **应用实例**:除了常见的键盘和鼠标,STM8软件USB HID还可以应用于其他创新项目,比如自定义游戏控制器、工业控制设备、医疗设备的用户界面等。
10. **软件框架**:ST通常提供完整的软件框架,包括USB库、HAL(硬件抽象层)和LL(低层)驱动,帮助开发者快速构建基于STM8的HID设备。
STM8软件USB HID涉及到了嵌入式系统、USB通信、HID协议以及微控制器编程等多个领域的知识,开发者需要对这些技术有深入理解才能成功实现一个功能完备的HID设备。
jianqiang.xue
- 粉丝: 3w+
- 资源: 191
最新资源
- 毕业设计Jupyter Notebook基于深度网络的垃圾识别与分类算法研究项目源代码,用PyTorch框架中的transforms方法对数据进行预处理操作,后经过多次调参实验,对比不同模型分类效果
- 1221额的2的2的2额
- 基于python第三方库pybloom-live实现的redis布隆过滤器类
- 快速排序算法在Rust语言的实现及其优化
- 微藻检测10-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 人工智能领域计算断层成像技术研究最新进展综述
- 基于java的公司固定资产管理系统.doc
- 柑橘多种疾病类型图像分类数据集【已标注,约1,000张数据】
- 2025年 UiPath AI和自动化趋势:代理型AI的崛起及企业影响
- 基于Java的环境保护与宣传网站的设计与实现毕业论文.doc