1 /****************************************************************************
4 * WS2812 Chainable leds example using Adafruit les strip
6 * Copyright 2013-2015 Nathael Pajani <nathael.pajani@ed3l.fr>
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.
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.
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/>.
22 *************************************************************************** */
25 #include "lib/stdint.h"
26 #include "core/system.h"
27 #include "core/systick.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
48 /***************************************************************************** */
49 /* Initial Pins configuration */
50 const struct pio_config common_pins[] = {
52 { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
53 { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
55 { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
56 { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
58 { LPC_ADC_AD2_PIO_0_14, LPC_FIXED, 0 }, /* Debug */
59 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
61 { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
62 { LPC_GPIO_0_3, LPC_SCT_POUT1, 0 }, /* Debug */
64 { LPC_GPIO_0_2, LPC_GPIO, 0 }, /* Led */
65 { LPC_GPIO_0_12, LPC_GPIO, 0 },
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 },
76 #define TMP101_ADDR 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
77 struct tmp101_sensor_config tmp101_sensor = {
80 .resolution = TMP_RES_ELEVEN_BITS,
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 /***************************************************************************** */
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)
98 ret = tmp101_sensor_config(&tmp101_sensor);
100 uprintf(uart_num, "Temp config error\n");
104 void temp_display(int uart_num)
107 int deci_degrees = 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);
114 uprintf(uart_num, "Temp read error\n");
116 uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\n",
117 (deci_degrees/10), (deci_degrees%10), raw);
122 /***************************************************************************** */
125 system_set_default_power_state();
126 clock_config(SELECTED_FREQ);
127 set_pins(common_pins);
129 /* System tick timer MUST be configured and running in order to use the sleeping
131 systick_timer_on(1); /* 1ms */
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.
140 void fault_info(const char* name, uint32_t len)
142 uprintf(UART0, name);
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.
151 void set_led_color(void)
153 uint16_t red = 0, green = 0, blue = 0;
156 adc_get_value(&blue, LPC_ADC(9));
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;
168 void trigger_message(uint32_t int_num) {
169 message_request = int_num + 1;
172 const struct lpc_timer_pwm_config pwm_conf = {
175 .outputs_initial_state = 0x01,
176 .match_values = { 10, },
180 /***************************************************************************** */
184 uart_on(UART0, 115200, NULL);
186 uprintf(UART0, "Sarting\n");
188 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
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);
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");
208 timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
209 timer_pwm_config(LPC_SCT, &pwm_conf);
210 uprintf(UART0, "Timer config done\n");
214 msleep(250); /* Wait for the end of the conversion : 40ms */
216 if (message_request != 0) {
217 uprintf(UART0, "Button pressed: %d\n", message_request);
220 /* Configure timer out and ADC in */
221 set_pins(capacitance_pins);
222 timer_start(LPC_SCT);
223 uprintf(UART0, "Timer started\n");
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");