--- /dev/null
+/****************************************************************************
+ * apps/dev/cpe_eti/main.c
+ *
+ * Code for CPE courses
+ *
+ * Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ *************************************************************************** */
+
+
+#include "core/system.h"
+#include "core/systick.h"
+#include "core/pio.h"
+#include "lib/stdio.h"
+#include "drivers/i2c.h"
+#include "drivers/serial.h"
+#include "drivers/gpio.h"
+#include "drivers/adc.h"
+#include "extdrv/status_led.h"
+#include "extdrv/tmp101_temp_sensor.h"
+#include "lib/protocols/dtplug/slave.h"
+#include "extdrv/ws2812.h"
+
+
+
+#define MODULE_VERSION 0x04
+#define MODULE_NAME "GPIO Demo Module"
+
+#define DEBUG 1
+#define BUFF_LEN 60
+
+#define SELECTED_FREQ FREQ_SEL_48MHz
+
+/***************************************************************************** */
+/* Pins configuration */
+/* pins blocks are passed to set_pins() for pins configuration.
+ * Unused pin blocks can be removed safely with the corresponding set_pins() call
+ * All pins blocks may be safelly merged in a single block for single set_pins() call..
+ */
+const struct pio_config common_pins[] = {
+ /* UART 0 */
+ { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL },
+ { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL },
+ /* I2C 0 */
+ { LPC_I2C0_SCL_PIO_0_10, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
+ { LPC_I2C0_SDA_PIO_0_11, (LPC_IO_DIGITAL | LPC_IO_OPEN_DRAIN_ENABLE) },
+ /* UART 1 */
+ { LPC_UART1_RX_PIO_0_8, LPC_IO_DIGITAL },
+ { LPC_UART1_TX_PIO_0_9, LPC_IO_DIGITAL },
+ /* SPI */
+ { LPC_SSP0_SCLK_PIO_0_14, LPC_IO_DIGITAL },
+ { LPC_SSP0_MOSI_PIO_0_17, LPC_IO_DIGITAL },
+ { LPC_SSP0_MISO_PIO_0_16, LPC_IO_DIGITAL },
+ /* ADC */
+ { LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG },
+ { LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG },
+ { LPC_ADC_AD2_PIO_1_0, LPC_IO_ANALOG },
+ { LPC_ADC_AD3_PIO_1_1, LPC_IO_ANALOG },
+ /* GPIO */
+ { LPC_GPIO_0_19, (LPC_IO_MODE_PULL_UP | LPC_IO_DIGITAL) },
+ 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 temp_alert = LPC_GPIO_0_7;
+
+const struct pio status_led_green = LPC_GPIO_1_4;
+const struct pio status_led_red = LPC_GPIO_1_5;
+
+const struct pio button = LPC_GPIO_0_12; /* ISP button */
+const struct pio ws2812_data_out_pin = LPC_GPIO_0_19; /* Led control data pin */
+
+static struct dtplug_protocol_handle uart_handle;
+
+
+/***************************************************************************** */
+void system_init()
+{
+ /* Stop the watchdog */
+ startup_watchdog_disable(); /* Do it right now, before it gets a chance to break in */
+ 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(UART1, name);
+ while (1);
+}
+
+/***************************************************************************** */
+/* Temperature */
+/* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */
+/* The I2C Temperature sensor is at address 0x94 */
+void WAKEUP_Handler(void)
+{
+}
+
+void temp_config(int uart_num)
+{
+ int ret = 0;
+
+ /* Temp sensor */
+ ret = tmp101_sensor_config(&tmp101_sensor);
+ if (ret != 0) {
+ uprintf(UART1, "Temp config error: %d\n", ret);
+ }
+}
+
+
+/***************************************************************************** */
+/* Data sent on radio comes from the UART when appropriate command is received. */
+void handle_uart_commands(struct packet* command)
+{
+ uint8_t channel = 0;
+
+ status_led(green_on);
+ /* Many commands use only this one, get it right now. */
+ if (command->info.seq_num & QUICK_DATA_PACKET) {
+ channel = command->info.quick_data[0];
+ } else {
+ channel = command->data[0];
+ }
+ switch (command->info.type) {
+ case PKT_TYPE_START_TEMP_CONVERSION:
+ /* Request a Temp conversion on I2C TMP101 temperature sensors (A conversion takes about 40ms) */
+ tmp101_sensor_start_conversion(&tmp101_sensor);
+ /* Get value from SPI connected thermocouple */
+ break;
+ case PKT_TYPE_GET_TEMPERATURE:
+ {
+ uint16_t value = 0;
+ int i = 0;
+ tmp101_sensor_read(&tmp101_sensor, NULL, &i);
+ value = (int16_t)i;
+ /* Swap endianness */
+ value = (uint16_t)byte_swap_16(value);
+ /* And send reply */
+ dtplug_protocol_send_reply(&uart_handle, command, NO_ERROR, 2, (uint8_t*)(&value));
+ }
+ break;
+ case PKT_TYPE_START_ADC_CONVERSION:
+ /* Nothing to do */
+ break;
+ case PKT_TYPE_GET_ADC_VALUE:
+ {
+ uint16_t value = 0;
+ adc_get_value(&value, channel);
+ dtplug_protocol_send_reply(&uart_handle, command, NO_ERROR, 2, (uint8_t*)(&value));
+ }
+ break;
+ case PKT_TYPE_SET_RGB_LED:
+ if (command->info.seq_num & QUICK_DATA_PACKET) {
+ ws2812_set_pixel(channel, 0, 0, 0);
+ } else {
+ ws2812_set_pixel(channel, command->data[1], command->data[2], command->data[3]);
+ }
+ ws2812_send_frame(0);
+ break;
+ case PKT_TYPE_GET_RGB_LED:
+ {
+ uint8_t values[4];
+ values[0] = channel;
+ ws2812_get_pixel(channel, &(values[1]), &(values[2]), &(values[3]));
+ dtplug_protocol_send_reply(&uart_handle, command, NO_ERROR, 4, values);
+ }
+ break;
+ case PKT_TYPE_CLEAR_LEDS:
+ ws2812_clear();
+ break;
+ default:
+ uprintf(UART1, "Unknown packet type : packet not handled\n");
+ dtplug_protocol_send_reply(&uart_handle, command, ERROR_PKT_NOT_HANDLED, 0, NULL);
+ status_led(red_on);
+ break;
+ }
+ dtplug_protocol_release_old_packet(&uart_handle);
+}
+
+
+static volatile uint8_t button_request_received = 0;
+void button_request(uint32_t gpio)
+{
+ button_request_received = 1;
+}
+
+
+
+/***************************************************************************** */
+int main(void)
+{
+ system_init();
+
+ status_led_config(&status_led_green, &status_led_red);
+ status_led(none);
+
+ uart_on(UART1, 115200, NULL); /* Debug */
+ dtplug_protocol_set_dtplug_comm_uart(UART0, &uart_handle);
+
+ adc_on(NULL);
+ adc_start_burst_conversion(ADC_MCH(0) | ADC_MCH(1) | ADC_MCH(2) | ADC_MCH(3), LPC_ADC_SEQ(0));
+
+ set_gpio_callback(button_request, &button, EDGE_RISING);
+
+ /* Configure onboard temp sensor */
+ i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
+ temp_config(UART1);
+
+ /* Led strip configuration */
+ ws2812_config(&ws2812_data_out_pin);
+
+ while (1) {
+ struct packet* pkt = NULL;
+
+ status_led(red_off);
+
+ /* UART */
+ pkt = dtplug_protocol_get_next_packet_ok(&uart_handle);
+ if (pkt != NULL) {
+ handle_uart_commands(pkt);
+ status_led(green_off);
+ }
+ }
+ return 0;
+}
+
+
+
+