forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPin.c
More file actions
110 lines (90 loc) · 3.18 KB
/
Copy pathPin.c
File metadata and controls
110 lines (90 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
// SPDX-FileCopyrightText: Copyright (c) 2019 Lucian Copeland for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-bindings/microcontroller/Pin.h"
#include "pins.h"
#if defined(TFBGA216)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK};
#elif defined(UFBGA176)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI};
#elif defined(LQFP144) || defined(WLCSP144)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG};
#elif defined(LQFP100_f4) || (LQFP100_x7)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD, GPIOE};
#elif defined(LQFP64)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC, GPIOD};
#elif defined(UFQFPN48)
GPIO_TypeDef *ports[] = {GPIOA, GPIOB, GPIOC};
#else
#error Unknown package type
#endif
#define GPIO_PORT_COUNT (MP_ARRAY_SIZE(ports))
static uint16_t claimed_pins[GPIO_PORT_COUNT];
static uint16_t __ALIGNED(4) never_reset_pins[GPIO_PORT_COUNT];
void reset_all_pins(void) {
// Reset claimed pins
for (uint8_t i = 0; i < GPIO_PORT_COUNT; i++) {
claimed_pins[i] = never_reset_pins[i];
}
for (uint8_t i = 0; i < GPIO_PORT_COUNT; i++) {
HAL_GPIO_DeInit(ports[i], ~never_reset_pins[i]);
}
}
// Mark pin as free and return it to a quiescent state.
void reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
if (pin_number == NO_PIN) {
return;
}
if (pin_port == 0x0F) {
return;
}
// Clear claimed bit & reset
claimed_pins[pin_port] &= ~(1 << pin_number);
never_reset_pins[pin_port] &= ~(1 << pin_number);
HAL_GPIO_DeInit(ports[pin_port], 1 << pin_number);
}
void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
if (pin_number == NO_PIN) {
return;
}
never_reset_pins[pin_port] |= 1 << pin_number;
// Make sure never reset pins are also always claimed
claimed_pins[pin_port] |= 1 << pin_number;
}
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
never_reset_pin_number(pin->port, pin->number);
}
void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
if (pin == NULL) {
return;
}
reset_pin_number(pin->port, pin->number);
}
void claim_pin(uint8_t pin_port, uint8_t pin_number) {
// Set bit in claimed_pins bitmask.
claimed_pins[pin_port] |= 1 << pin_number;
}
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) {
return !(claimed_pins[pin_port] & 1 << pin_number);
}
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
return pin_number_is_free(pin->port, pin->number);
}
GPIO_TypeDef *pin_port(uint8_t pin_port) {
return ports[pin_port];
}
uint16_t pin_mask(uint8_t pin_number) {
return 1 << pin_number;
}
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) {
return pin->port * 16 + pin->number;
}
void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
claim_pin(pin->port, pin->number);
}
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
reset_pin_number(pin_no / 16, pin_no % 16);
}