From: Nathael Pajani Date: Fri, 24 Jun 2016 06:45:00 +0000 (+0200) Subject: Test App for the E-Xanh gardener sensor X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;ds=inline;h=b3366e5a43c4e86f72608404f06e440fbc95af13;p=soft%2Flpc82x%2Fexanh Test App for the E-Xanh gardener sensor --- b3366e5a43c4e86f72608404f06e440fbc95af13 diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..a944f0f --- /dev/null +++ b/test/Makefile @@ -0,0 +1,12 @@ +# Makefile for exanh apps + +MODULE = $(shell basename $(shell cd .. && pwd && cd -)) +NAME = $(shell basename $(CURDIR)) + +.PHONY: $(NAME).bin +$(NAME).bin: + @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@ + +clean mrproper: + @make -C ../../.. --no-print-directory $@ + diff --git a/test/README b/test/README new file mode 100644 index 0000000..111f8c8 --- /dev/null +++ b/test/README @@ -0,0 +1,21 @@ +E-Xanh Gardener Support + +Copyright 2016 Nathael Pajani + + +/* **************************************************************************** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..9e496cc --- /dev/null +++ b/test/main.c @@ -0,0 +1,247 @@ +/**************************************************************************** + * apps/ledstrip/main.c + * + * WS2812 Chainable leds example using Adafruit les strip + * + * Copyright 2013-2015 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + + +#include "lib/stdint.h" +#include "core/system.h" +#include "core/systick.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "drivers/serial.h" +#include "drivers/gpio.h" +#include "drivers/adc.h" +#include "drivers/i2c.h" +#include "drivers/timers.h" + +#include "extdrv/ws2812.h" +#include "extdrv/tmp101_temp_sensor.h" + + +#define MODULE_VERSION 0x01 +#define MODULE_NAME "E-Xanh Gardener" + + +#define SELECTED_FREQ FREQ_SEL_36MHz + +#define APP_NB_LEDS 1 + +/***************************************************************************** */ +/* Initial Pins configuration */ +const struct pio_config common_pins[] = { + /* UART 0 */ + { LPC_GPIO_0_0, LPC_UART0_RX, 0 }, + { LPC_GPIO_0_4, LPC_UART0_TX, 0 }, + /* I2C 0 */ + { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 }, + { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 }, + /* ADC */ + { LPC_ADC_AD2_PIO_0_14, LPC_FIXED, 0 }, /* Debug */ + { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 }, + { LPC_ADC_AD10_PIO_0_13, LPC_FIXED, 0 }, + /* Timers */ + { LPC_GPIO_0_8, LPC_SCT_POUT0, 0 }, + { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 }, + { LPC_GPIO_0_3, LPC_SCT_POUT2, 0 }, /* Debug */ + /* GPIO */ + { LPC_GPIO_0_2, LPC_GPIO, 0 }, + { LPC_GPIO_0_12, LPC_GPIO, 0 }, + ARRAY_LAST_PIO, +}; + +/* Configure pins as used for capacitance sensing */ +const struct pio_config capacitance_pins[] = { + { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 }, + { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 }, + ARRAY_LAST_PIO, +}; + +#define TMP101_ADDR 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */ +struct tmp101_sensor_config tmp101_sensor = { + .bus_num = I2C0, + .addr = TMP101_ADDR, + .resolution = TMP_RES_ELEVEN_BITS, +}; + +const struct pio button = LPC_GPIO_0_12; /* ISP button */ +const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */ + +const struct pio cap_trig_gpio = LPC_GPIO_0_9; +const struct pio cap_adc_gpio = LPC_GPIO_0_17; + +/***************************************************************************** */ +/* Temperature */ +/* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */ +/* The I2C Temperature sensor is at address 0x94 */ +void temp_config(int uart_num) +{ + int ret = 0; + + /* Temp sensor */ + ret = tmp101_sensor_config(&tmp101_sensor); + if (ret != 0) { + serial_write(uart_num, "Temp config error\r\n", 19); + } +} + +void temp_display(int uart_num) +{ + uint16_t raw = 0; + int deci_degrees = 0; + int len = 0; + + tmp101_sensor_start_conversion(&tmp101_sensor); + msleep(50); /* Wait for the end of the conversion : 40ms */ + len = tmp101_sensor_read(&tmp101_sensor, &raw, &deci_degrees); + if (len != 0) { + serial_write(uart_num, "Temp read error\r\n", 19); + } else { + uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\r\n", + (deci_degrees/10), (deci_degrees%10), raw); + } +} + + +/***************************************************************************** */ +void system_init() +{ + system_set_default_power_state(); + clock_config(SELECTED_FREQ); + set_pins(common_pins); + gpio_on(); + /* System tick timer MUST be configured and running in order to use the sleeping + * functions */ + systick_timer_on(1); /* 1ms */ + systick_start(); +} + +/* Define our fault handler. This one is not mandatory, the dummy fault handler + * will be used when it's not overridden here. + * Note : The default one does a simple infinite loop. If the watchdog is deactivated + * the system will hang. + */ +void fault_info(const char* name, uint32_t len) +{ + uprintf(UART0, name); + while (1); +} + + + +/* This mode reads values from ADC2, ADC9 and ADC10 every 150ms and uses the values to set the leds. + * Pixels are updated when all pixel are set from ADC input. + */ +void set_led_color(void) +{ + uint16_t red = 0, green = 0, blue = 0; + static uint16_t tmp = 0; + + /* Get ADC values */ + adc_get_value(&tmp, LPC_ADC_NUM(9)); + red = tmp; + adc_get_value(&tmp, LPC_ADC_NUM(10)); + blue = tmp; + + /* Set pixel */ + ws2812_set_pixel(0, (((red >> 6) & 0xFF) - 30), green, (((blue >> 6) & 0xFF) - 30)); + ws2812_send_frame(0); + + uprintf(UART0, "ADC9: %d, ADC10: %d\n", red, blue); +} + + +uint8_t message_request = 0; +uint8_t trigger = 0; +void trigger_message(uint32_t int_num) { + message_request = int_num + 1; +} + +const struct lpc_timer_pwm_config pwm_conf = { + .nb_channels = 2, + .period = 30, + .outputs_initial_state = 0x07, + .match_values = { 15, 10, 15, }, + .outputs = { 0, 1, 2, }, +}; + +/***************************************************************************** */ +int main(void) +{ + system_init(); + uart_on(UART0, 115200, NULL); + + uprintf(UART0, "Sarting\n"); + /* I2C config */ + i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER); + temp_config(UART0); + uprintf(UART0, "I2C config done\n"); + + /* ADC for potentiometer color settings */ + config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0); + config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0); + adc_on(NULL); + adc_start_burst_conversion(LPC_ADC_CHANNEL(2) | LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA); +// adc_start_burst_conversion(LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA); + uprintf(UART0, "ADC config done\n"); + + /* Led strip configuration */ + ws2812_config(&ws2812_data_out_pin); + uprintf(UART0, "Led config done\n"); + + /* Activate the message request on Rising edge (button release) */ + set_gpio_callback(trigger_message, &button, EDGE_RISING); + uprintf(UART0, "Button config done\n"); + + /* Timer config */ + timer_on(LPC_SCT, (10 * 1000 * 1000), NULL); + timer_pwm_config(LPC_SCT, &pwm_conf); + uprintf(UART0, "Timer config done\n"); + + while (1) { + set_led_color(); + msleep(250); /* Wait for the end of the conversion : 40ms */ + temp_display(UART0); + if (message_request != 0) { + uprintf(UART0, "Button pressed: %d\n", message_request); + message_request = 0; + if (trigger == 0) { + /* Configure timer out and ADC in */ + set_pins(capacitance_pins); + timer_start(LPC_SCT); + uprintf(UART0, "Timer started\n"); + trigger = 1; + } else { + timer_stop(LPC_SCT); + /* Turn timer output and ADC input as inputs to discharge all the capacitances. */ + config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0); + config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0); + uprintf(UART0, "Timer stoped\n"); + trigger = 0; + } + } + } + return 0; +} + + + +