-
Notifications
You must be signed in to change notification settings - Fork 30
/
bcm2835.go
202 lines (163 loc) · 4.38 KB
/
bcm2835.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Package bcm2835 provides functions for the bcm2835 as used in the Raspberry Pi
package bcm2835
// #include "bcm2835.h"
import "C"
import "errors"
import "unsafe"
const (
Low = 0
High = 1
Input = 0
Output = 1
Pin3 = 0
Pin5 = 1
Pin7 = 4
Pin8 = 14
Pin10 = 15
Pin11 = 17
Pin12 = 18
Pin13 = 21
Pin15 = 22
Pin16 = 23
Pin18 = 24
Pin19 = 10
Pin21 = 9
Pin22 = 25
Pin23 = 11
Pin24 = 8
Pin26 = 7
)
// Init initialise the library by opening /dev/mem and getting pointers to the
// internal memory for BCM 2835 device registers. You must call this
// (successfully) before calling any other functions in this library (except
// SetDebug)
func Init() (err error) {
if C.bcm2835_init() == 0 {
return errors.New("Init: failed")
}
return
}
// Close closes the library, deallocating any allocaterd memory and closing
// /dev/mem
func Close() (err error) {
if C.bcm2835_close() == 0 {
return errors.New("Close: failed")
}
return
}
// SetDebug sets the debug level of the library. A value of 1 prevents mapping
// to /dev/mem, and makes the library print out what it would do, rather than
// accessing the GPIO registers. A value of 0, the default, causes normal
// operation. Call this before calling Init()
func SetDebug(debug int) {
C.bcm2835_set_debug(C.uint8_t(debug))
}
// GpioFsel sets the function select register for the given pin, which
// configures the pin as either Input or Output
func GpioFsel(pin, mode int) {
C.bcm2835_gpio_fsel(C.uint8_t(pin), C.uint8_t(mode))
}
// GpioSet sets the specified pin output to high.
func GpioSet(pin int) {
C.bcm2835_gpio_set(C.uint8_t(pin))
}
// GpioClr sets the specified pin output to low.
func GpioClr(pin int) {
C.bcm2835_gpio_clr(C.uint8_t(pin))
}
// GpioLev reads the current level on the specified pin and returns either high
// or low. Works whether or not the pin is an input or an output.
func GpioLev(pin int) int {
return int(C.bcm2835_gpio_lev(C.uint8_t(pin)))
}
func GpioEds(pin int) int {
return int(C.bcm2835_gpio_eds(C.uint8_t(pin)))
}
func GpioSetEds(pin int) {
C.bcm2835_gpio_set_eds(C.uint8_t(pin))
}
func GpioRen(pin int) {
C.bcm2835_gpio_ren(C.uint8_t(pin))
}
func GpioClrRen(pin int) {
C.bcm2835_gpio_clr_ren(C.uint8_t(pin))
}
func GpioFen(pin int) {
C.bcm2835_gpio_fen(C.uint8_t(pin))
}
func GpioClrFen(pin int) {
C.bcm2835_gpio_clr_fen(C.uint8_t(pin))
}
func GpioHen(pin int) {
C.bcm2835_gpio_hen(C.uint8_t(pin))
}
func GpioClrHen(pin int) {
C.bcm2835_gpio_clr_hen(C.uint8_t(pin))
}
func GpioLen(pin int) {
C.bcm2835_gpio_len(C.uint8_t(pin))
}
func GpioClrLen(pin int) {
C.bcm2835_gpio_clr_len(C.uint8_t(pin))
}
func GpioAren(pin int) {
C.bcm2835_gpio_aren(C.uint8_t(pin))
}
func GpioClrAren(pin int) {
C.bcm2835_gpio_clr_aren(C.uint8_t(pin))
}
func GpioAfen(pin int) {
C.bcm2835_gpio_afen(C.uint8_t(pin))
}
func GpioClrAfen(pin int) {
C.bcm2835_gpio_clr_afen(C.uint8_t(pin))
}
func GpioPud(pud int) {
C.bcm2835_gpio_pud(C.uint8_t(pud))
}
func GpioPudclk(pin, on int) {
C.bcm2835_gpio_pudclk(C.uint8_t(pin), C.uint8_t(on))
}
func GpioPad(group int) uint32 {
return uint32(C.bcm2835_gpio_pad(C.uint8_t(group)))
}
func GpioSetPad(group int, control uint32) {
C.bcm2835_gpio_set_pad(C.uint8_t(group), C.uint32_t(control))
}
/// GpioWrite sets the output state of the specified pin
func GpioWrite(pin, on int) {
C.bcm2835_gpio_write(C.uint8_t(pin), C.uint8_t(on))
}
func GpioSetPud(pin, pud int) {
C.bcm2835_gpio_set_pud(C.uint8_t(pin), C.uint8_t(pud))
}
func SpiBegin() {
C.bcm2835_spi_begin()
}
func SpiEnd() {
C.bcm2835_spi_end()
}
func SpiSetBitOrder(order int) {
C.bcm2835_spi_setBitOrder(C.uint8_t(order))
}
func SpiSetClockDivider(divider uint16) {
C.bcm2835_spi_setClockDivider(C.uint16_t(divider))
}
func SpiSetDataMode(mode int) {
C.bcm2835_spi_setDataMode(C.uint8_t(mode))
}
func SpiChipSelect(cs int) {
C.bcm2835_spi_chipSelect(C.uint8_t(cs))
}
func SpiSetChipSelectPolarity(cs, active int) {
C.bcm2835_spi_setChipSelectPolarity(C.uint8_t(cs), C.uint8_t(active))
}
func SpiTransfer(value int) int {
return int(C.bcm2835_spi_transfer(C.uint8_t(value)))
}
func SpiTransfern(data []byte) {
C.bcm2835_spi_transfern((*C.char)(unsafe.Pointer(&data[0])), C.uint32_t(len(data)))
}
func SpiTransfernb(dst []byte, data []byte) {
C.bcm2835_spi_transfernb((*C.char)(unsafe.Pointer(&data[0])), (*C.char)(unsafe.Pointer(&dst[0])), C.uint32_t(len(data)))
}