当前位置:网站首页>Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
Pandora IOT development board learning (RT thread) - Experiment 3 button experiment (learning notes)
2022-07-04 14:26:00 【Xiaohui_ Super】
This article code reference RT-Thread official BSP
List of articles
Experimental function
Routine source code :(main.c)
Functions realized by this experiment : Press down KEY0,LED_R Lighten up , Release KEY0,LED_R Extinguish .
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-08-23 balanceTWK first implementation */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
int main(void)
{
unsigned int count = 1;
/* Set up RGB The mode of red light pin is output mode */
rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT);
/* Set up KEY0 The mode of the pin is input mode */
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT);
while (count > 0)
{
/* Read button KEY0 Pin status of */
if (rt_pin_read(PIN_KEY0) == PIN_LOW)
{
rt_thread_mdelay(50); // Eliminate jitter
if (rt_pin_read(PIN_KEY0) == PIN_LOW)
{
/* The key has been pressed , Output log, Lighten up LED The lamp */
LOG_D("KEY0 pressed!");
rt_pin_write(PIN_LED_R, PIN_LOW);
}
}
else
{
/* Key not pressed , Extinguish LED The lamp */
rt_pin_write(PIN_LED_R, PIN_HIGH);
}
rt_thread_mdelay(10);
count++;
}
return 0;
}
Code analysis
rt_pin_mode()
The function is to GPIO Pin The initialization , Defined as
/* RT-Thread Hardware PIN APIs */
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
}
Parameters pin It's a rt_base_t Variable (long), Below GET_PIN()
yes STM32 Of pin Value macro definition , Fill in capital letters for the first parameter , The second parameter is filled with numbers .
#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__STM32_PORT(PORTx) - (rt_base_t)GPIOA)/(0x0400UL) )) + PIN)
#define __STM32_PORT(port) GPIO##port // ## Is a character connector , If port by A, said GPIOA
For example, in the experiment
#define PIN_LED_R GET_PIN(E, 7)
, Express GPIOE GPIO_Pin7
at present RT-Thread Supported pin operating modes include :
#define PIN_MODE_OUTPUT 0x00 /* Output */
#define PIN_MODE_INPUT 0x01 /* Input */
#define PIN_MODE_INPUT_PULLUP 0x02 /* Pull up input */
#define PIN_MODE_INPUT_PULLDOWN 0x03 /* Drop down input */
#define PIN_MODE_OUTPUT_OD 0x04 /* Open drain output */
stay bsp Of drv_gpio.c
In file , There is a bottom layer GPIO drive , Here is STM32 Of GPIO Driver function of mode setting ( You should be familiar with , Just use HAL Library written GPIO Initialization code )
static void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
{
const struct pin_index *index;
GPIO_InitTypeDef GPIO_InitStruct;
index = get_pin(pin);
if (index == RT_NULL)
{
return;
}
/* Configure GPIO_InitStructure */
GPIO_InitStruct.Pin = index->pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
if (mode == PIN_MODE_OUTPUT)
{
/* output setting */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
}
else if (mode == PIN_MODE_INPUT)
{
/* input setting: not pull. */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
}
else if (mode == PIN_MODE_INPUT_PULLUP)
{
/* input setting: pull up. */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
}
else if (mode == PIN_MODE_INPUT_PULLDOWN)
{
/* input setting: pull down. */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
}
else if (mode == PIN_MODE_OUTPUT_OD)
{
/* output setting: od. */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
}
HAL_GPIO_Init(index->gpio, &GPIO_InitStruct);
}
rt_pin_read()
GPIO Read function , Here is the definition of the function :
int rt_pin_read(rt_base_t pin)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
}
and GPIO The mode configuration function is similar , It will call the corresponding function in the underlying driver , The underlying function is through HAL_GPIO_ReadPin()
To get GPIO The level of .
static int stm32_pin_read(rt_device_t dev, rt_base_t pin)
{
int value;
const struct pin_index *index;
value = PIN_LOW;
index = get_pin(pin);
if (index == RT_NULL)
{
return value;
}
value = HAL_GPIO_ReadPin(index->gpio, index->pin);
return value;
}
rt_thread_mdelay()
This is a RT-Thread Millisecond delay function , The definition is as follows :
rt_err_t rt_thread_mdelay(rt_int32_t ms)
{
rt_tick_t tick;
// Get the required clock beat
tick = rt_tick_from_millisecond(ms);
// Block the corresponding beat time
return rt_thread_sleep(tick);
}
rt_tick_from_millisecond()
/** * Work out ms The number of corresponding clock beats * * * @param ms the specified millisecond * - Negative Number wait forever * - Zero not wait * - Max 0x7fffffff * * @return the calculated tick */
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
{
rt_tick_t tick;
if (ms < 0)
{
tick = (rt_tick_t)RT_WAITING_FOREVER; // -1
}
else
{
// take “ Beats per second ” / 1000 * ms, Calculate the corresponding second beats
tick = RT_TICK_PER_SECOND * (ms / 1000);
// Plus less than 1000ms Part of the beat number
tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
}
/* return the calculated tick */
return tick;
}
rt_thread_sleep()
Thread sleep ( Hang up ) function , The parameter is the number of system beats :
/** * This function can make the current thread hang for a period of time ( from tick decision ) * * @param tick the sleep ticks * * @return RT_EOK */
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
register rt_base_t temp;
struct rt_thread *thread;
/* set to current thread */
thread = rt_thread_self();
RT_ASSERT(thread != RT_NULL);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
/* disable interrupt */
temp = rt_hw_interrupt_disable();
/* suspend thread */
rt_thread_suspend(thread);
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);
rt_timer_start(&(thread->thread_timer));
/* enable interrupt */
rt_hw_interrupt_enable(temp);
rt_schedule();
/* clear error number of this thread to RT_EOK */
if (thread->error == -RT_ETIMEOUT)
thread->error = RT_EOK;
return RT_EOK;
}
LOG_D()
In this study , We can LOG_D()
As rt_kprintf()
,
#define dbg_log_line(lvl, color_n, fmt, ...) \ do \ {
\ _DBG_LOG_HDR(lvl, color_n); \ rt_kprintf(fmt, ##__VA_ARGS__); \ _DBG_LOG_X_END; \ } \ while (0)
LOG_D yes RT-Thread A log printing function in the kernel , Details visible :《RT-Thread Document center ——ulog journal 》
RT-Thread Log API Include :
rt_pin_write()
GPIO Write function , Here is the definition of the function ,
void rt_pin_write(rt_base_t pin, rt_base_t value)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
}
and GPIO The mode configuration function is similar , It will call the corresponding function in the underlying driver , The underlying function is through HAL_GPIO_WritePin()
To complete GPIO Pin Modification of .
static void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
{
const struct pin_index *index;
index = get_pin(pin);
if (index == RT_NULL)
{
return;
}
HAL_GPIO_WritePin(index->gpio, index->pin, (GPIO_PinState)value);
}
边栏推荐
- Gorm data insertion (transfer)
- sql优化之查询优化器
- vscode 常用插件汇总
- Digi XBee 3 RF: 4个协议,3种封装,10个大功能
- Matters needing attention in overseas game Investment Agency
- (1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
- Use of arouter
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
- The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
- Some problems and ideas of data embedding point
猜你喜欢
Ruiji takeout notes
第十七章 进程内存
Count the running time of PHP program and set the maximum running time of PHP
使用CLion编译OGLPG-9th-Edition源码
Data center concept
Leetcode 61: 旋转链表
Compile oglpg-9th-edition source code with clion
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
数据仓库面试问题准备
Talk about 10 tips to ensure thread safety
随机推荐
Use of tiledlayout function in MATLAB
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
SqlServer函数,存储过程的创建和使用
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
Popular framework: the use of glide
NowCoder 反转链表
flink sql-client.sh 使用教程
Visual Studio调试方式详解
Test process arrangement (3)
redis 日常笔记
Leetcode t47: full arrangement II
Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
第十七章 进程内存
sql优化之explain
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!