9e496cc338e7951482c0985817b71badd0113b12
[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         { LPC_ADC_AD10_PIO_0_13, LPC_FIXED, 0 },
61         /* Timers */
62         { LPC_GPIO_0_8, LPC_SCT_POUT0, 0 },
63         { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 },
64         { LPC_GPIO_0_3, LPC_SCT_POUT2, 0 }, /* Debug */
65         /* GPIO */
66         { LPC_GPIO_0_2, LPC_GPIO, 0 },
67         { LPC_GPIO_0_12, LPC_GPIO, 0 },
68         ARRAY_LAST_PIO,
69 };
71 /* Configure pins as used for capacitance sensing */
72 const struct pio_config capacitance_pins[] = {
73         { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 },
74         { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
75         ARRAY_LAST_PIO,
76 };
78 #define TMP101_ADDR  0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
79 struct tmp101_sensor_config tmp101_sensor = {
80         .bus_num = I2C0,
81         .addr = TMP101_ADDR,
82         .resolution = TMP_RES_ELEVEN_BITS,
83 };
85 const struct pio button = LPC_GPIO_0_12; /* ISP button */
86 const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
88 const struct pio cap_trig_gpio = LPC_GPIO_0_9;
89 const struct pio cap_adc_gpio = LPC_GPIO_0_17;
91 /***************************************************************************** */
92 /* Temperature */
93 /* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */
94 /* The I2C Temperature sensor is at address 0x94 */
95 void temp_config(int uart_num)
96 {
97         int ret = 0;
99         /* Temp sensor */
100         ret = tmp101_sensor_config(&tmp101_sensor);
101         if (ret != 0) {
102                 serial_write(uart_num, "Temp config error\r\n", 19);
103         }
106 void temp_display(int uart_num)
108         uint16_t raw = 0;
109         int deci_degrees = 0;
110         int len = 0;
112         tmp101_sensor_start_conversion(&tmp101_sensor);
113         msleep(50); /* Wait for the end of the conversion : 40ms */
114         len = tmp101_sensor_read(&tmp101_sensor, &raw, &deci_degrees);
115         if (len != 0) {
116                 serial_write(uart_num, "Temp read error\r\n", 19);
117         } else {
118                 uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\r\n",
119                                 (deci_degrees/10), (deci_degrees%10), raw);
120         }
124 /***************************************************************************** */
125 void system_init()
127         system_set_default_power_state();
128         clock_config(SELECTED_FREQ);
129         set_pins(common_pins);
130         gpio_on();
131         /* System tick timer MUST be configured and running in order to use the sleeping
132          * functions */
133         systick_timer_on(1); /* 1ms */
134         systick_start();
137 /* Define our fault handler. This one is not mandatory, the dummy fault handler
138  * will be used when it's not overridden here.
139  * Note : The default one does a simple infinite loop. If the watchdog is deactivated
140  * the system will hang.
141  */
142 void fault_info(const char* name, uint32_t len)
144         uprintf(UART0, name);
145         while (1);
150 /* This mode reads values from ADC2, ADC9 and ADC10 every 150ms and uses the values to set the leds.
151  * Pixels are updated when all pixel are set from ADC input.
152  */
153 void set_led_color(void)
155         uint16_t red = 0, green = 0, blue = 0;
156         static uint16_t tmp = 0;
158         /* Get ADC values */
159         adc_get_value(&tmp, LPC_ADC_NUM(9));
160         red = tmp;
161         adc_get_value(&tmp, LPC_ADC_NUM(10));
162         blue = tmp;
164         /* Set pixel */
165         ws2812_set_pixel(0, (((red >> 6) & 0xFF) - 30), green, (((blue >> 6) & 0xFF) - 30));
166         ws2812_send_frame(0);
168         uprintf(UART0, "ADC9: %d, ADC10: %d\n", red, blue);
172 uint8_t message_request = 0;
173 uint8_t trigger = 0;
174 void trigger_message(uint32_t int_num) {
175         message_request = int_num + 1;
178 const struct lpc_timer_pwm_config pwm_conf = {
179         .nb_channels = 2,
180         .period = 30,
181         .outputs_initial_state = 0x07,
182         .match_values = { 15, 10, 15, },
183         .outputs = { 0, 1, 2, },
184 };
186 /***************************************************************************** */
187 int main(void)
189         system_init();
190         uart_on(UART0, 115200, NULL);
192         uprintf(UART0, "Sarting\n");
193         /* I2C config */
194         i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
195         temp_config(UART0);
196         uprintf(UART0, "I2C config done\n");
198         /* ADC for potentiometer color settings */
199         config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
200         config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
201         adc_on(NULL);
202         adc_start_burst_conversion(LPC_ADC_CHANNEL(2) | LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA);
203 //      adc_start_burst_conversion(LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA);
204         uprintf(UART0, "ADC config done\n");
206         /* Led strip configuration */
207         ws2812_config(&ws2812_data_out_pin);
208         uprintf(UART0, "Led config done\n");
210         /* Activate the message request on Rising edge (button release) */
211         set_gpio_callback(trigger_message, &button, EDGE_RISING);
212         uprintf(UART0, "Button config done\n");
214         /* Timer config */
215         timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
216         timer_pwm_config(LPC_SCT, &pwm_conf);
217         uprintf(UART0, "Timer config done\n");
219         while (1) {
220                 set_led_color();
221                 msleep(250); /* Wait for the end of the conversion : 40ms */
222                 temp_display(UART0);
223                 if (message_request != 0) {
224                         uprintf(UART0, "Button pressed: %d\n", message_request);
225                         message_request = 0;
226                         if (trigger == 0) {
227                                 /* Configure timer out and ADC in */
228                                 set_pins(capacitance_pins);
229                                 timer_start(LPC_SCT);
230                                 uprintf(UART0, "Timer started\n");
231                                 trigger = 1;
232                         } else {
233                                 timer_stop(LPC_SCT);
234                                 /* Turn timer output and ADC input as inputs to discharge all the capacitances. */
235                                 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
236                                 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
237                                 uprintf(UART0, "Timer stoped\n");
238                                 trigger = 0;
239                         }
240                 }
241         }
242         return 0;