OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / nxtvm / platform / nxt / sensors.c
1 #include "platform_config.h"
2
3 #include "types.h"
4 #include "stack.h"
5 #include "threads.h"
6 #include "classes.h"
7 #include "language.h"
8 #include "sensors.h"
9 #include "poll.h"
10 #include "at91sam7s256.h"
11 #include "nxt_avr.h"
12
13 extern int verbose;
14
15 sensor_t sensors[N_SENSORS] = {
16   {0, 0, 0, 0, 0},
17   {0, 0, 0, 0, 0},
18   {0, 0, 0, 0, 0},
19   {0, 0, 0, 0, 0}
20 };
21
22 void
23 init_sensors(void)
24 {
25   int i;
26
27   for (i = 0; i < N_SENSORS; i++) {
28     unset_digi0(i);
29     unset_digi1(i);
30     nxt_avr_set_input_power(i, 0);
31   }
32   /* Ensure RS485 is inactive. Otherwise it can interfere with
33    * the operation of port 4.
34    */
35   *AT91C_PIOA_PER |= AT91C_PIO_PA5 | AT91C_PIO_PA6 | AT91C_PIO_PA7;
36   *AT91C_PIOA_PPUDR |= AT91C_PIO_PA5 | AT91C_PIO_PA6 | AT91C_PIO_PA7;
37   *AT91C_PIOA_OER |= AT91C_PIO_PA5 | AT91C_PIO_PA6 | AT91C_PIO_PA7;
38   *AT91C_PIOA_CODR |= AT91C_PIO_PA5 | AT91C_PIO_PA6 | AT91C_PIO_PA7;
39 }
40
41 /**
42  * Read sensor values
43  */
44 void
45 poll_sensors(void)
46 {
47   byte i;
48   sensor_t *pSensor = sensors;
49
50   for (i = 0; i < N_SENSORS; i++, pSensor++) {
51     pSensor->value = sensor_adc(i);
52   }
53 }
54
55 void
56 read_buttons(int dummy, short *output)
57 {
58   *output = (short) buttons_get();
59 }
60
61
62 void
63 check_for_data(char *valid, char **nextbyte)
64 {
65   *valid = 0;
66 }
67
68 void
69 set_digi0(int sensor)
70 {
71   /* Enable output on the pin */
72
73   int functions[] = { AT91C_PIO_PA23, AT91C_PIO_PA28,
74     AT91C_PIO_PA29, AT91C_PIO_PA30
75   };
76
77   *AT91C_PIOA_PER |= functions[sensor];
78
79   *AT91C_PIOA_OER |= functions[sensor];
80
81   /* Set high */
82
83   *AT91C_PIOA_SODR |= functions[sensor];
84
85 }
86
87 void
88 unset_digi0(int sensor)
89 {
90   /* Enable output on the pin */
91
92   int functions[] = { AT91C_PIO_PA23, AT91C_PIO_PA28,
93     AT91C_PIO_PA29, AT91C_PIO_PA30
94   };
95
96   *AT91C_PIOA_PER |= functions[sensor];
97
98   *AT91C_PIOA_OER |= functions[sensor];
99
100   /* Set low */
101
102   *AT91C_PIOA_CODR |= functions[sensor];
103
104 }
105
106 void
107 set_digi1(int sensor)
108 {
109   /* Enable output on the pin */
110
111   int functions[] = { AT91C_PIO_PA18, AT91C_PIO_PA19,
112     AT91C_PIO_PA20, AT91C_PIO_PA2
113   };
114
115   *AT91C_PIOA_PER |= functions[sensor];
116
117   *AT91C_PIOA_OER |= functions[sensor];
118
119   /* Set high */
120
121   *AT91C_PIOA_SODR |= functions[sensor];
122
123 }
124
125 void
126 unset_digi1(int sensor)
127 {
128   /* Enable output on the pin */
129
130   int functions[] = { AT91C_PIO_PA18, AT91C_PIO_PA19,
131     AT91C_PIO_PA20, AT91C_PIO_PA2
132   };
133
134   *AT91C_PIOA_PER |= functions[sensor];
135
136   *AT91C_PIOA_OER |= functions[sensor];
137
138   /* Set low */
139
140   *AT91C_PIOA_CODR |= functions[sensor];
141
142 }