-
Notifications
You must be signed in to change notification settings - Fork 50
/
gpio.go
99 lines (82 loc) · 1.82 KB
/
gpio.go
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
// NXP GPIO support
// https://github.com/usbarmory/tamago
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
// Package gpio implements helpers for GPIO configuration on NXP SoCs.
//
// This package is only meant to be used with `GOOS=tamago GOARCH=arm` as
// supported by the TamaGo framework for bare metal Go on ARM SoCs, see
// https://github.com/usbarmory/tamago.
package gpio
import (
"errors"
"fmt"
"github.com/usbarmory/tamago/internal/reg"
)
// GPIO registers
const (
GPIO_DR = 0x00
GPIO_GDIR = 0x04
)
// GPIO controller instance
type GPIO struct {
// Controller index
Index int
// Base register
Base uint32
// Clock gate register
CCGR uint32
// Clock gate
CG int
clk bool
}
// Pin instance
type Pin struct {
num int
data uint32
dir uint32
}
// Init initializes a GPIO.
func (hw *GPIO) Init(num int) (gpio *Pin, err error) {
if hw.Base == 0 || hw.CCGR == 0 {
return nil, errors.New("invalid GPIO controller instance")
}
if num > 31 {
return nil, fmt.Errorf("invalid GPIO number %d", num)
}
gpio = &Pin{
num: num,
data: hw.Base + GPIO_DR,
dir: hw.Base + GPIO_GDIR,
}
if !hw.clk {
// enable clock
reg.SetN(hw.CCGR, hw.CG, 0b11, 0b11)
hw.clk = true
}
return
}
// Out configures a GPIO as output.
func (gpio *Pin) Out() {
reg.Set(gpio.dir, gpio.num)
}
// In configures a GPIO as input.
func (gpio *Pin) In() {
reg.Clear(gpio.dir, gpio.num)
}
// High configures a GPIO signal as high.
func (gpio *Pin) High() {
reg.Set(gpio.data, gpio.num)
}
// Low configures a GPIO signal as low.
func (gpio *Pin) Low() {
reg.Clear(gpio.data, gpio.num)
}
// Value returns the GPIO signal level.
func (gpio *Pin) Value() (high bool) {
return reg.Get(gpio.data, gpio.num, 1) == 1
}