Merge remote-tracking branch 'alexandre/ni-merge' into HEAD
[nit.git] / lib / bcm2835.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Services to control the bcm2835 chipset as used in the Raspberry Pi
18 # model B revision 1 Uses the C library by Mike McCauley from
19 # http://www.airspayce.com/mikem/bcm2835/
20 module bcm2835
21
22 import gpio
23
24 in "C Header" `{
25 #include <bcm2835.h>
26 `}
27
28 redef class Object
29 protected fun bcm2835_init: Bool `{ return bcm2835_init(); `}
30 protected fun bcm2835_close `{ bcm2835_close(); `}
31 protected fun bcm2835_debug=(v: Bool) `{ bcm2835_set_debug(v); `}
32 end
33
34 extern class RPiPin `{ RPiGPIOPin `}
35 super Pin
36
37 new p1_03 `{ return RPI_GPIO_P1_03; `}
38
39 new p1_05 `{ return RPI_GPIO_P1_05; `}
40 new p1_07 `{ return RPI_GPIO_P1_07; `}
41
42 new p1_11 `{ return RPI_GPIO_P1_11; `}
43 new p1_12 `{ return RPI_GPIO_P1_12; `}
44 new p1_13 `{ return RPI_GPIO_P1_13; `}
45
46 new p1_15 `{ return RPI_GPIO_P1_15; `}
47 new p1_16 `{ return RPI_GPIO_P1_16; `}
48
49 new p1_18 `{ return RPI_GPIO_P1_18; `}
50 new p1_19 `{ return RPI_GPIO_P1_19; `}
51
52 new p1_21 `{ return RPI_GPIO_P1_21; `}
53 new p1_22 `{ return RPI_GPIO_P1_22; `}
54 new p1_23 `{ return RPI_GPIO_P1_23; `}
55 new p1_24 `{ return RPI_GPIO_P1_24; `}
56
57 new p1_26 `{ return RPI_GPIO_P1_26; `}
58
59 # Select mode: input, output or alts
60 fun fsel=(mode: FunctionSelect) `{ bcm2835_gpio_fsel(recv, mode); `}
61
62 # Set output
63 redef fun write(high) `{ bcm2835_gpio_write(recv, high? HIGH: LOW); `}
64
65 # Set pull up mode
66 fun pud=(pud: PUDControl) `{ bcm2835_gpio_set_pud(recv, pud); `}
67
68 # Falling edge detect
69 # Do not use on raspbian, it is bugged!
70 fun fen `{ bcm2835_gpio_fen(recv); `}
71 fun clr_fen `{ bcm2835_gpio_clr_fen(recv); `}
72
73 # Raising edge detect
74 # Do not use on raspbian, it is bugged!
75 fun ren `{ bcm2835_gpio_ren(recv); `}
76 fun clr_ren `{ bcm2835_gpio_clr_ren(recv); `}
77
78 # High edge detect
79 # Do not use on raspbian, it is bugged!
80 fun hen `{ bcm2835_gpio_hen(recv); `}
81 fun clr_hen `{ bcm2835_gpio_clr_hen(recv); `}
82
83 # Low edge detect
84 # Do not use on raspbian, it is bugged!
85 fun len `{ bcm2835_gpio_len(recv); `}
86 fun clr_len `{ bcm2835_gpio_clr_len(recv); `}
87
88 fun set_eds `{ bcm2835_gpio_set_eds(recv); `}
89 fun eds: Bool `{ return bcm2835_gpio_eds(recv); `}
90
91 # Return input on pin, true for high and false for low
92 fun lev: Bool `{ return bcm2835_gpio_lev(recv); `}
93 end
94
95 extern class FunctionSelect `{ bcm2835FunctionSelect `}
96 # Input function
97 new inpt `{ return BCM2835_GPIO_FSEL_INPT; `}
98
99 # Output function
100 new outp `{ return BCM2835_GPIO_FSEL_OUTP; `}
101
102 new alt0 `{ return BCM2835_GPIO_FSEL_ALT0; `}
103 new alt1 `{ return BCM2835_GPIO_FSEL_ALT1; `}
104 new alt2 `{ return BCM2835_GPIO_FSEL_ALT2; `}
105 new alt3 `{ return BCM2835_GPIO_FSEL_ALT3; `}
106 new alt4 `{ return BCM2835_GPIO_FSEL_ALT4; `}
107 new alt5 `{ return BCM2835_GPIO_FSEL_ALT5; `}
108 new mask `{ return BCM2835_GPIO_FSEL_MASK; `}
109 end
110
111 extern class PUDControl `{ bcm2835PUDControl `}
112 new off `{ return BCM2835_GPIO_PUD_OFF; `}
113 new down `{ return BCM2835_GPIO_PUD_DOWN; `}
114 new up `{ return BCM2835_GPIO_PUD_UP; `}
115 end
116
117 redef universal Int
118 fun bcm2835_delay `{ bcm2835_delay(recv); `}
119 fun bcm2835_delay_micros `{ bcm2835_delayMicroseconds(recv); `}
120 end