RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for Pin:

Data Structures

struct  rt_device_pin
 
struct  rt_device_pin_mode
 
struct  rt_device_pin_value
 
struct  rt_pin_irq_hdr
 
struct  rt_pin_ops
 

Macros

#define PIN_LOW   0x00
 
#define PIN_HIGH   0x01
 
#define PIN_MODE_OUTPUT   0x00
 
#define PIN_MODE_INPUT   0x01
 
#define PIN_MODE_INPUT_PULLUP   0x02
 
#define PIN_MODE_INPUT_PULLDOWN   0x03
 
#define PIN_MODE_OUTPUT_OD   0x04
 
#define PIN_IRQ_MODE_RISING   0x00
 
#define PIN_IRQ_MODE_FALLING   0x01
 
#define PIN_IRQ_MODE_RISING_FALLING   0x02
 
#define PIN_IRQ_MODE_HIGH_LEVEL   0x03
 
#define PIN_IRQ_MODE_LOW_LEVEL   0x04
 
#define PIN_IRQ_DISABLE   0x00
 
#define PIN_IRQ_ENABLE   0x01
 
#define PIN_IRQ_PIN_NONE   PIN_NONE
 

Functions

int rt_device_pin_register (const char *name, const struct rt_pin_ops *ops, void *user_data)
 
void rt_pin_mode (rt_base_t pin, rt_uint8_t mode)
 
void rt_pin_write (rt_base_t pin, rt_ssize_t value)
 
rt_ssize_t rt_pin_read (rt_base_t pin)
 
rt_base_t rt_pin_get (const char *name)
 
rt_err_t rt_pin_attach_irq (rt_base_t pin, rt_uint8_t mode, void(*hdr)(void *args), void *args)
 
rt_err_t rt_pin_detach_irq (rt_base_t pin)
 
rt_err_t rt_pin_irq_enable (rt_base_t pin, rt_uint8_t enabled)
 
rt_err_t rt_pin_debounce (rt_base_t pin, rt_uint32_t debounce)
 

Detailed Description

Pin driver api.

Example

#include <rtthread.h>
#include <rtdevice.h>
#ifndef BEEP_PIN_NUM
#define BEEP_PIN_NUM 35 // PB0
#endif
#ifndef KEY0_PIN_NUM
#define KEY0_PIN_NUM 55 // PD8
#endif
#ifndef KEY1_PIN_NUM
#define KEY1_PIN_NUM 56 // PD9
#endif
void beep_on(void *args)
{
rt_kprintf("turn on beep!\n");
rt_pin_write(BEEP_PIN_NUM, PIN_HIGH);
}
void beep_off(void *args)
{
rt_kprintf("turn off beep!\n");
rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
}
static void pin_beep_sample(void)
{
rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
}
MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);
#define PIN_LOW
Definition: dev_pin.h:116
#define PIN_MODE_INPUT_PULLUP
Definition: dev_pin.h:121
void rt_pin_write(rt_base_t pin, rt_ssize_t value)
write pin value
void rt_pin_mode(rt_base_t pin, rt_uint8_t mode)
set pin mode
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint8_t enabled)
enable or disable the pin interrupt
#define PIN_MODE_OUTPUT
Definition: dev_pin.h:119
#define PIN_HIGH
Definition: dev_pin.h:117
#define PIN_IRQ_ENABLE
Definition: dev_pin.h:165
#define PIN_IRQ_MODE_FALLING
Definition: dev_pin.h:159
rt_err_t rt_pin_attach_irq(rt_base_t pin, rt_uint8_t mode, void(*hdr)(void *args), void *args)
bind the pin interrupt callback function
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142

Macro Definition Documentation

◆ PIN_LOW

#define PIN_LOW   0x00

low level

◆ PIN_HIGH

#define PIN_HIGH   0x01

high level

◆ PIN_MODE_OUTPUT

#define PIN_MODE_OUTPUT   0x00

output mode

◆ PIN_MODE_INPUT

#define PIN_MODE_INPUT   0x01

input mode

◆ PIN_MODE_INPUT_PULLUP

#define PIN_MODE_INPUT_PULLUP   0x02

input mode with pull-up

◆ PIN_MODE_INPUT_PULLDOWN

#define PIN_MODE_INPUT_PULLDOWN   0x03

input mode with pull-down

◆ PIN_MODE_OUTPUT_OD

#define PIN_MODE_OUTPUT_OD   0x04

output mode with open-drain

◆ PIN_IRQ_MODE_RISING

#define PIN_IRQ_MODE_RISING   0x00

rising edge trigger

◆ PIN_IRQ_MODE_FALLING

#define PIN_IRQ_MODE_FALLING   0x01

falling edge trigger

◆ PIN_IRQ_MODE_RISING_FALLING

#define PIN_IRQ_MODE_RISING_FALLING   0x02

rising and falling edge trigger

◆ PIN_IRQ_MODE_HIGH_LEVEL

#define PIN_IRQ_MODE_HIGH_LEVEL   0x03

high level trigger

◆ PIN_IRQ_MODE_LOW_LEVEL

#define PIN_IRQ_MODE_LOW_LEVEL   0x04

low level trigger

◆ PIN_IRQ_DISABLE

#define PIN_IRQ_DISABLE   0x00

disable irq

◆ PIN_IRQ_ENABLE

#define PIN_IRQ_ENABLE   0x01

enable irq

◆ PIN_IRQ_PIN_NONE

#define PIN_IRQ_PIN_NONE   PIN_NONE

no pin irq

Function Documentation

◆ rt_device_pin_register()

int rt_device_pin_register ( const char *  name,
const struct rt_pin_ops ops,
void *  user_data 
)

register a pin device

Parameters
namethe name of pin device
opsthe operations of pin device
user_datathe user data of pin device
Returns
int error code

◆ rt_pin_mode()

void rt_pin_mode ( rt_base_t  pin,
rt_uint8_t  mode 
)

set pin mode

Parameters
pinthe pin number
modethe pin mode

◆ rt_pin_write()

void rt_pin_write ( rt_base_t  pin,
rt_ssize_t  value 
)

write pin value

Parameters
pinthe pin number
valuethe pin value

◆ rt_pin_read()

rt_ssize_t rt_pin_read ( rt_base_t  pin)

read pin value

Parameters
pinthe pin number
Returns
rt_ssize_t the pin value

◆ rt_pin_get()

rt_base_t rt_pin_get ( const char *  name)

get pin number by name

Parameters
namethe pin name
Returns
rt_base_t the pin number

◆ rt_pin_attach_irq()

rt_err_t rt_pin_attach_irq ( rt_base_t  pin,
rt_uint8_t  mode,
void(*)(void *args)  hdr,
void *  args 
)

bind the pin interrupt callback function

Parameters
pinthe pin number
modethe irq mode
hdrthe irq callback function
argsthe argument of the callback function
Returns
rt_err_t error code

◆ rt_pin_detach_irq()

rt_err_t rt_pin_detach_irq ( rt_base_t  pin)

detach the pin interrupt callback function

Parameters
pinthe pin number
Returns
rt_err_t error code

◆ rt_pin_irq_enable()

rt_err_t rt_pin_irq_enable ( rt_base_t  pin,
rt_uint8_t  enabled 
)

enable or disable the pin interrupt

Parameters
pinthe pin number
enabledPIN_IRQ_ENABLE or PIN_IRQ_DISABLE
Returns
rt_err_t error code

◆ rt_pin_debounce()

rt_err_t rt_pin_debounce ( rt_base_t  pin,
rt_uint32_t  debounce 
)

set the pin's debounce time

Parameters
pinthe pin number
debouncetime
Returns
rt_err_t error code