HAL
HAL layer above libopencm3 library.
 All Files Functions Macros Groups Pages
PIN manipulation module

PIN manipulation API. More...

Modules

 PIN Peripheral Clock API
 Pin Peripheral Clock manipulation.
 
 PIN State API
 Pin state manipulation.
 
 PIN PullUp/PullDown API
 Pin pullup manipulation.
 
 PIN Direction API
 Pin direction manipulation.
 
 PIN Speed API
 Pin speed manipulation.
 
 PIN Alternate function API
 Pin Alternate Function manipulation.
 

Detailed Description

PIN manipulation API.

LGPL License Terms HAL License

Basic example of usage:

1 
2 #include <hal/pin.h>
3 
4 #define LED PA8 // dependent on board
5 
6 int main(void)
7 {
8  pin_clock_enable(LED);
9  pin_set(LED, false);
11 
12  while (true) {
13  pin_toggle(LED);
14  }
15 }

Extended usage with board dependency:

1 
2 #include <hal/pin.h>
3 
4 #if BOARD_VERSION==1
5 # define LED PA8
6 # define BUTTON PC1
7 # define BUTTON_LEVEL false
8 #elsif BOARD_VERSION==2
9 # define LED PC3
10 # define BUTTON PC1
11 # define BUTTON_LEVEL false
12 #elsif BOARD_VERSION==3
13 # define LED PA1
14 # define BUTTON PB6
15 # define BUTTON_LEVEL true
16 #else
17 # error "unsupported board"
18 #endif
19 
20 int main(void)
21 {
22  /* Enable the clocks for pins */
23  pin_clock_enable(LED);
24  pin_clock_enable(BUTTON);
25 
26  /* Configure LED */
27  pin_set(LED, false);
29  pin_speed_fast(LED);
30 
31  /* Configure button */
32  pin_input(BUTTON);
33  if (BUTTON_LEVEL) {
34  pin_pull_down(BUTTON);
35  } else {
36  pin_pull_up(BUTTON);
37  }
38 
39  while (true) {
40  pin_toggle(LED);
41  if (pin_get(BUTTON) == BUTTON_LEVEL) {
42  /* when button was pressed, slow down blinking */
43 
44  /* TODO: This should be HAL delay function call */
45  for (int i=0; i<10000;i++) {
46  __asm volatile ("nop");
47  }
48  }
49  }
50 }

Extended usage with alternate function selection:

1 
2 #include <hal/pin.h>
3 
4 #if BOARD_VERSION==1
5 # define LED PA8
6 
7 /* the RMII peripheral */
8 # define PHY_CRSDV PC5
9 # define PHY_RXD0 PC1
10 # define PHY_RXD1 PC2
11 # define PHY_TXEN PC6
12 # define PHY_TXD0 PC3
13 # define PHY_TXD1 PC4
14 # define PHY_REFCLK PC5
15 # define PHY_MDIO PD4
16 # define PHY_MDC PD5
17 /* bit banging pins */
18 # define PHY_RST PA6
19 # define PHY_INTRP PA7
20 
21 /* the usart peripheral */
22 # define USART_TXD PA3
23 # define USART_RXD PA4
24 
25 #else
26 # error "unsupported board"
27 #endif
28 
29 /* Initialize pins in complex peripheral */
30 void init_eth(void)
31 {
32  int phy_pins[] = {
33  PHY_CRSDV, PHY_RXD0, PHY_RXD1, PHY_REFCLK,
34  PHY_TXEN, PHY_TXD0, PHY_TXD1, PHY_MDIO,
35  PHY_MDC
36  };
37 
38  /* Initialize all pins that belongs to RMII */
39  for (int i = 0; i < sizeof(phy_pins) / sizeof(phy_pins[0]); i++) {
40  pin_clock_enable(phy_pins[i]);
41  pin_af_pushpull(phy_pins[i]);
42  pin_speed_high(phy_pins[i]);
43  pin_af_map(phy_pins[i], GPIO_AF_ETH);
44  }
45 
46  pin_input(PHY_INTRP);
47  pin_pull_up(PHY_INTRP);
48  pin_set(PHY_RST, true);
49  pin_output_pushpull(PHY_RST);
50 
51 
52  /* Reset the connected PHY by signal */
53  pin_set(PHY_RST, false);
54  for (int i = 0; i < 10000; i++) {
55  __asm volatile ("nop");
56  }
57  pin_set(PHY_RST, true);
58  for (int i = 0; i < 10000; i++) {
59  __asm volatile ("nop");
60  }
61 
62  /* Initialize phy over SNI */
63  phy_reset(1); /* out of scope of this example */
64 
65  /* Initialize ethernet MAC */
66  eth_init(); /* out of scope of this example */
67 }
68 
69 /* Initialize pins in simple peripheral */
70 void init_usart(void)
71 {
72  pin_clock_enable(USART_TXD);
73  pin_clock_enable(USART_RXD);
74 
75  pin_af_pushpull(USART_TXD);
76  pin_af_pushpull(USART_RXD);
77 
78  pin_af_map(USART_RXD, GPIO_AF_USART6);
79  pin_af_map(USART_TXD, GPIO_AF_USART6);
80 
81  usart_init(USART6); /* out of scope of this example */
82 }
83 
84 /* Initialize pins for general purpose bit-banging */
85 void init_led(void)
86 {
87  /* Enable the clocks for pins */
88  pin_clock_enable(LED);
89 
90  /* Configure LED */
91  pin_set(LED, false);
93  pin_speed_fast(LED);
94 }
95 
96 int main(void)
97 {
98  init_led();
99  init_usart();
100  init_led();
101 
102  while (true) {
103  eth_poll(); /* out of scope of this example */
104 
105  pin_toggle(LED);
106  }
107 }