Exanh test app changed according to ADC and timers API changes. Includes changes...
[soft/lpc82x/exanh] / test / main.c
1 /****************************************************************************
2  *   apps/ledstrip/main.c
3  *
4  * WS2812 Chainable leds example using Adafruit les strip
5  *
6  * Copyright 2013-2015 Nathael Pajani <nathael.pajani@ed3l.fr>
7  *
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  *************************************************************************** */
25 #include "lib/stdint.h"
26 #include "core/system.h"
27 #include "core/systick.h"
28 #include "core/pio.h"
29 #include "lib/stdio.h"
30 #include "drivers/serial.h"
31 #include "drivers/gpio.h"
32 #include "drivers/adc.h"
33 #include "drivers/i2c.h"
34 #include "drivers/timers.h"
36 #include "extdrv/ws2812.h"
37 #include "extdrv/tmp101_temp_sensor.h"
40 #define MODULE_VERSION    0x01
41 #define MODULE_NAME "E-Xanh Gardener"
44 #define SELECTED_FREQ  FREQ_SEL_36MHz
46 #define APP_NB_LEDS    1
48 /***************************************************************************** */
49 /* Initial Pins configuration */
50 const struct pio_config common_pins[] = {
51         /* UART 0 */
52         { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
53         { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
54         /* I2C 0 */
55         { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
56         { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
57         /* ADC */
58         { LPC_ADC_AD2_PIO_0_14, LPC_FIXED, 0 }, /* Debug */
59         { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
60         /* Timers */
61         { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
62         { LPC_GPIO_0_3, LPC_SCT_POUT1, 0 }, /* Debug */
63         /* GPIO */
64         { LPC_GPIO_0_2, LPC_GPIO, 0 },  /* Led */
65         { LPC_GPIO_0_12, LPC_GPIO, 0 },
66         ARRAY_LAST_PIO,
67 };
69 /* Configure pins as used for capacitance sensing */
70 const struct pio_config capacitance_pins[] = {
71         { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
72         { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
73         ARRAY_LAST_PIO,
74 };
76 #define TMP101_ADDR  0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
77 struct tmp101_sensor_config tmp101_sensor = {
78         .bus_num = I2C0,
79         .addr = TMP101_ADDR,
80         .resolution = TMP_RES_ELEVEN_BITS,
81 };
83 const struct pio button = LPC_GPIO_0_12; /* ISP button */
84 const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
86 const struct pio cap_trig_gpio = LPC_GPIO_0_9;
87 const struct pio cap_adc_gpio = LPC_GPIO_0_17;
89 /***************************************************************************** */
90 /* Temperature */
91 /* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */
92 /* The I2C Temperature sensor is at address 0x94 */
93 void temp_config(int uart_num)
94 {
95         int ret = 0;
97         /* Temp sensor */
98         ret = tmp101_sensor_config(&tmp101_sensor);
99         if (ret != 0) {
100                 uprintf(uart_num, "Temp config error\n");
101         }
104 void temp_display(int uart_num)
106         uint16_t raw = 0;
107         int deci_degrees = 0;
108         int len = 0;
110         tmp101_sensor_start_conversion(&tmp101_sensor);
111         msleep(50); /* Wait for the end of the conversion : 40ms */
112         len = tmp101_sensor_read(&tmp101_sensor, &raw, &deci_degrees);
113         if (len != 0) {
114                 uprintf(uart_num, "Temp read error\n");
115         } else {
116                 uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\n",
117                                 (deci_degrees/10), (deci_degrees%10), raw);
118         }
122 /***************************************************************************** */
123 void system_init()
125         system_set_default_power_state();
126         clock_config(SELECTED_FREQ);
127         set_pins(common_pins);
128         gpio_on();
129         /* System tick timer MUST be configured and running in order to use the sleeping
130          * functions */
131         systick_timer_on(1); /* 1ms */
132         systick_start();
135 /* Define our fault handler. This one is not mandatory, the dummy fault handler
136  * will be used when it's not overridden here.
137  * Note : The default one does a simple infinite loop. If the watchdog is deactivated
138  * the system will hang.
139  */
140 void fault_info(const char* name, uint32_t len)
142         uprintf(UART0, name);
143         while (1);
148 /* This mode reads values from ADC2, ADC9 and ADC10 every 150ms and uses the values to set the leds.
149  * Pixels are updated when all pixel are set from ADC input.
150  */
151 void set_led_color(void)
153         uint16_t red = 0, green = 0, blue = 0;
155         /* Get ADC values */
156         adc_get_value(&blue, LPC_ADC(9));
158         /* Set pixel */
159         ws2812_set_pixel(0, red, green, (((blue >> 6) & 0xFF) - 30));
160         ws2812_send_frame(0);
162         uprintf(UART0, "ADC9: %d\n", blue);
166 uint8_t message_request = 0;
167 uint8_t trigger = 0;
168 void trigger_message(uint32_t int_num) {
169         message_request = int_num + 1;
172 const struct lpc_timer_pwm_config pwm_conf = {
173         .nb_channels = 1,
174         .period = 30,
175         .outputs_initial_state = 0x01,
176         .match_values = { 10, },
177         .outputs = { 0, },
178 };
180 /***************************************************************************** */
181 int main(void)
183         system_init();
184         uart_on(UART0, 115200, NULL);
186         uprintf(UART0, "Sarting\n");
187         /* I2C config */
188         i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
189         temp_config(UART0);
190         uprintf(UART0, "I2C config done\n");
192         /* ADC for potentiometer color settings */
193         config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
194         config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
195         adc_on(NULL);
196         adc_start_burst_conversion(ADC_MCH(9), LPC_ADC_SEQA);
197         uprintf(UART0, "ADC config done\n");
199         /* Led strip configuration */
200         ws2812_config(&ws2812_data_out_pin);
201         uprintf(UART0, "Led config done\n");
203         /* Activate the message request on Rising edge (button release) */
204         set_gpio_callback(trigger_message, &button, EDGE_RISING);
205         uprintf(UART0, "Button config done\n");
207         /* Timer config */
208         timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
209         timer_pwm_config(LPC_SCT, &pwm_conf);
210         uprintf(UART0, "Timer config done\n");
212         while (1) {
213                 set_led_color();
214                 msleep(250); /* Wait for the end of the conversion : 40ms */
215                 temp_display(UART0);
216                 if (message_request != 0) {
217                         uprintf(UART0, "Button pressed: %d\n", message_request);
218                         message_request = 0;
219                         if (trigger == 0) {
220                                 /* Configure timer out and ADC in */
221                                 set_pins(capacitance_pins);
222                                 timer_start(LPC_SCT);
223                                 uprintf(UART0, "Timer started\n");
224                                 trigger = 1;
225                         } else {
226                                 timer_stop(LPC_SCT);
227                                 /* Turn timer output and ADC input as inputs to discharge all the capacitances. */
228                                 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
229                                 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
230                                 uprintf(UART0, "Timer stoped\n");
231                                 trigger = 0;
232                         }
233                 }
234         }
235         return 0;