--- /dev/null
+/****************************************************************************
+ * apps/scialys/beta/main.c
+ *
+ * Scialys system for solar-panel power generation tracking and fair use.
+ *
+ * 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 "lib/errno.h"
+#include "drivers/serial.h"
+#include "drivers/gpio.h"
+#include "drivers/adc.h"
+#include "drivers/ssp.h"
+#include "drivers/i2c.h"
+#include "drivers/timers.h"
+
+#include "extdrv/status_led.h"
+#include "extdrv/ws2812.h"
+#include "extdrv/max31855_thermocouple.h"
+#include "extdrv/tmp101_temp_sensor.h"
+#include "extdrv/rtc_pcf85363a.h"
+#include "extdrv/ssd130x_oled_driver.h"
+#include "extdrv/ssd130x_oled_buffer.h"
+#include "lib/font.h"
+#include "lib/time.h"
+
+#include "extdrv/sdmmc.h"
+
+#define MODULE_VERSION 0x05
+#define MODULE_NAME "Scialys uC"
+
+
+#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 : Config / Debug / USB */
+ { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL },
+ { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL },
+ /* UART 1 : UEXT */
+ { LPC_UART1_RX_PIO_0_8, LPC_IO_DIGITAL },
+ { LPC_UART1_TX_PIO_0_9, LPC_IO_DIGITAL },
+ /* I2C : RTC, Display, UEXT */
+ { 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) },
+ /* SPI (Thermocouple + uSD card + UEXT) */
+ { 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 },
+ /* GPIO */
+ { LPC_GPIO_0_0, LPC_IO_DIGITAL }, /* Clkout / interrupt from RTC */
+ { LPC_GPIO_0_3, LPC_IO_DIGITAL }, /* EJP / External switch input */
+ { LPC_GPIO_0_4, LPC_IO_DIGITAL }, /* Zero crossing detection input */
+ { LPC_GPIO_0_6, LPC_IO_DIGITAL }, /* Mosfet control */
+ { LPC_GPIO_0_7, LPC_IO_DIGITAL }, /* Fan Control */
+ { LPC_GPIO_0_12, LPC_IO_DIGITAL }, /* ISP / User button OK */
+ { LPC_GPIO_0_15, LPC_IO_DIGITAL }, /* Thermocouple chip select */
+ { LPC_GPIO_0_21, LPC_IO_DIGITAL }, /* WS2812B RGB Leds control */
+ { LPC_GPIO_0_22, LPC_IO_DIGITAL }, /* User button B4 */
+ { LPC_GPIO_0_23, LPC_IO_DIGITAL }, /* User button B3 */
+ { LPC_GPIO_0_24, LPC_IO_DIGITAL }, /* User button B2 */
+ { LPC_GPIO_0_25, LPC_IO_DIGITAL }, /* User button B1 */
+ { LPC_GPIO_0_28, LPC_IO_DIGITAL }, /* Charge State */
+ { LPC_GPIO_1_1, LPC_IO_DIGITAL }, /* Uext Chip select / Module eeprom select */
+ { LPC_GPIO_1_6, LPC_IO_DIGITAL }, /* uSD Card SPI Chip Select */
+ ARRAY_LAST_PIO,
+};
+
+const struct pio_config adc_pins[] = {
+ { LPC_ADC_AD0_PIO_0_30, LPC_IO_ANALOG }, /* ADC0 */
+ { LPC_ADC_AD1_PIO_0_31, LPC_IO_ANALOG }, /* ADC1 */
+ { LPC_ADC_AD2_PIO_1_0, LPC_IO_ANALOG }, /* ADC2 */
+ { LPC_ADC_AD7_PIO_1_5, LPC_IO_ANALOG }, /* ADC3 */
+ ARRAY_LAST_PIO,
+};
+
+const struct pio status_led_green = LPC_GPIO_0_26;
+const struct pio status_led_red = LPC_GPIO_0_27;
+
+/* Inputs */
+/* Buttons */
+const struct pio button_ok = LPC_GPIO_0_12;
+const struct pio button_b1 = LPC_GPIO_0_25;
+const struct pio button_b2 = LPC_GPIO_0_24;
+const struct pio button_b3 = LPC_GPIO_0_23;
+const struct pio button_b4 = LPC_GPIO_0_22;
+/* External signals */
+const struct pio rtc_in_pin = LPC_GPIO_0_0;
+const struct pio ejp_in_pin = LPC_GPIO_0_3;
+const struct pio zero_cross_in_pin = LPC_GPIO_0_4;
+const struct pio charge_status_in_pin = LPC_GPIO_0_28;
+
+/* Outputs */
+/* Led control data pin */
+const struct pio ws2812_data_out_pin = LPC_GPIO_0_21;
+/* AC output control (Mosfet) */
+const struct pio ac_ctrl = LPC_GPIO_0_6;
+const struct pio fan_ctrl = LPC_GPIO_0_7;
+
+
+/* Thermocouple reading */
+const struct max31855_sensor_config thermo = {
+ .ssp_bus_num = 0,
+ .chip_select = LPC_GPIO_0_15,
+};
+
+
+const struct lpc_tc_config ac_timer_conf = {
+ .mode = LPC_TIMER_MODE_TIMER | LPC_TIMER_MODE_MATCH,
+ .match_control = { LPC_TIMER_INT_RESET_AND_STOP_ON_MATCH, 0, 0, 0, },
+ .match = { 10, 0, 0, 0, },
+ .ext_match_config = { LPC_TIMER_SET_ON_MATCH, 0, 0, 0, },
+};
+
+
+
+/***************************************************************************** */
+/* Basic system init and configuration */
+static volatile int got_wdt_int = 0;
+void wdt_callback(void)
+{
+ got_wdt_int = 1;
+}
+
+const struct wdt_config wdconf = {
+ .clk_sel = WDT_CLK_IRC,
+ .intr_mode_only = 0,
+ .callback = wdt_callback,
+ .locks = 0,
+ .nb_clk = 0x03FFFFFF, /* 0x3FF to 0x03FFFFFF */
+ .wdt_window = 0,
+ .wdt_warn = 0x3FF,
+};
+
+void system_init()
+{
+ /* Configure the Watchdog */
+ watchdog_config(&wdconf);
+ system_set_default_power_state();
+ clock_config(SELECTED_FREQ);
+ set_pins(common_pins);
+ set_pins(adc_pins);
+ gpio_on();
+ status_led_config(&status_led_green, &status_led_red);
+ /* 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);
+}
+
+
+/***************************************************************************** */
+/* Some global flags or variables */
+volatile static int act_cmd = 0;
+volatile static uint32_t fan_on = 0;
+
+
+/***************************************************************************** */
+/* System configuration over USB */
+void config_rx(uint8_t c)
+{
+ /* FAN control */
+ if (c == 'f') {
+ gpio_set(fan_ctrl);
+ fan_on = 1;
+ } else {
+ gpio_clear(fan_ctrl);
+ fan_on = 0;
+ }
+}
+
+
+/***************************************************************************** */
+/* AC control */
+
+void set_ctrl_duty_cycle(uint8_t value)
+{
+ act_cmd = value;
+ if (act_cmd >= 97) {
+ /* 100 is the maximum allowed value, but also due to electronics (zero-cross detection)
+ and alternative waveform, it has no meaning to differentiate between close to 100
+ and a real 100. Consider anything above 96% as 100% */
+ act_cmd = 100;
+ } else if (act_cmd <= 2) {
+ /* Due to electronics limitations and the physics of the alternative waveform, it has no
+ meaning to turn "ON" below 3%. Consider it a OFF (0%) */
+ act_cmd = 0;
+ }
+}
+
+void ac_switch_on(uint32_t flags)
+{
+ gpio_clear(ac_ctrl);
+ gpio_clear(button_b2);
+}
+
+static uint32_t clk_cycles_ac_zc = 0;
+static volatile uint32_t zc_count = 0; /* Wraps every 1.36 year ... */
+void zero_cross(uint32_t gpio)
+{
+ uint32_t delay = 0;
+
+ zc_count ++;
+ gpio_toggle(button_b1);
+
+ /* If full ON, then do not use timer for delayed start */
+ if (act_cmd == 100) {
+ gpio_clear(ac_ctrl);
+ gpio_clear(button_b2);
+ return;
+ }
+
+ /* Start with command "off" */
+ gpio_set(ac_ctrl);
+ gpio_set(button_b2);
+ /* Set timer to trigger ac out ON at given delay */
+ delay = clk_cycles_ac_zc * (100 - act_cmd);
+ timer_set_match(LPC_TIMER_32B1, CHAN0, delay);
+ timer_restart(LPC_TIMER_32B1);
+}
+
+
+
+
+
+/***************************************************************************** */
+/* Oled Display */
+#define DISPLAY_ADDR 0x78
+static uint8_t gddram[ 4 + GDDRAM_SIZE ];
+struct oled_display display = {
+ .address = DISPLAY_ADDR,
+ .bus_num = I2C0,
+ .video_mode = SSD130x_DISP_NORMAL,
+ .contrast = 128,
+ .scan_dir = SSD130x_SCAN_BOTTOM_TOP,
+ .read_dir = SSD130x_RIGHT_TO_LEFT,
+ .display_offset_dir = SSD130x_MOVE_TOP,
+ .display_offset = 4,
+ .gddram = gddram,
+};
+
+#define ROW(x) VERTICAL_REV(x)
+DECLARE_FONT(font);
+
+void display_char(uint8_t line, uint8_t col, uint8_t c)
+{
+ uint8_t tile = (c > FIRST_FONT_CHAR) ? (c - FIRST_FONT_CHAR) : 0;
+ uint8_t* tile_data = (uint8_t*)(&font[tile]);
+ ssd130x_buffer_set_tile(gddram, col, line, tile_data);
+}
+#define OLED_LINE_CHAR_LENGTH (SSD130x_NB_COL / 8)
+#define DISPLAY_LINE_LENGTH (OLED_LINE_CHAR_LENGTH + 1)
+int display_line(uint8_t line, uint8_t col, char* text)
+{
+ int len = strlen((char*)text);
+ int i = 0;
+
+ for (i = 0; i < len; i++) {
+ uint8_t tile = (text[i] > FIRST_FONT_CHAR) ? (text[i] - FIRST_FONT_CHAR) : 0;
+ uint8_t* tile_data = (uint8_t*)(&font[tile]);
+ ssd130x_buffer_set_tile(gddram, col++, line, tile_data);
+ if (col >= (OLED_LINE_CHAR_LENGTH)) {
+ col = 0;
+ line++;
+ if (line >= SSD130x_NB_PAGES) {
+ return i;
+ }
+ }
+ }
+ return len;
+}
+
+
+void menu_buttons(uint32_t gpio)
+{
+}
+
+
+/***************************************************************************** */
+void scialys_config(void)
+{
+ /* Immediatly turn off Mosfet */
+ config_gpio(&ac_ctrl, 0, GPIO_DIR_OUT, 0);
+ /* Start with FAN ON */
+ config_gpio(&fan_ctrl, 0, GPIO_DIR_OUT, 1);
+
+ /* Thermocouple configuration */
+ max31855_sensor_config(&thermo);
+ uprintf(UART0, "Thermocouple config done\n");
+
+ /* FIXME : DEBUG */
+ config_gpio(&button_b1, 0, GPIO_DIR_OUT, 0);
+ config_gpio(&button_b2, 0, GPIO_DIR_OUT, 0);
+
+ set_gpio_callback(menu_buttons, &button_b3, EDGE_FALLING);
+ set_gpio_callback(menu_buttons, &button_b4, EDGE_FALLING);
+
+ /* Zero cross */
+ set_gpio_callback(zero_cross, &zero_cross_in_pin, EDGE_FALLING);
+
+ /* Start ADC sampling */
+ adc_start_burst_conversion(ADC_MCH(0) | ADC_MCH(1) | ADC_MCH(2) | ADC_MCH(7), LPC_ADC_SEQ(0));
+
+ /* Configure Input GPIO */
+ config_gpio(&ejp_in_pin, 0, GPIO_DIR_IN, 0);
+ config_gpio(&rtc_in_pin, 0, GPIO_DIR_IN, 0);
+ config_gpio(&charge_status_in_pin, 0, GPIO_DIR_IN, 1);
+
+ /* WS2812B Leds on display board */
+ ws2812_config(&ws2812_data_out_pin);
+
+ /* AC Switch Config */
+ timer_counter_config(LPC_TIMER_32B1, &ac_timer_conf);
+ /* We want 100 Hz (50 Hz but two zero crossings) with 1% granularity */
+ clk_cycles_ac_zc = get_main_clock() / (100 * 100);
+
+ status_led(green_only);
+
+ /* Configure and start display */
+ ssd130x_display_on(&display);
+ /* Erase screen */
+ ssd130x_buffer_set(gddram, 0x00);
+ ssd130x_display_full_screen(&display);
+
+ /* FIXME : DEBUG */
+ add_systick_callback(zero_cross, 10);
+
+ /* Add a systick callback to handle time counting */
+ //add_systick_callback(handle_dec_request, DEC_PERIOD);
+
+ msleep(50);
+}
+
+/***************************************************************************** */
+#define NB_VAL 20
+
+/***************************************************************************** */
+int main(void)
+{
+ uint16_t isnail_solar_values[NB_VAL];
+ uint16_t isnail_home_values[NB_VAL];
+ uint8_t idx = 0;
+ uint32_t loop = 0;
+
+ system_init();
+ status_led(red_only);
+ uart_on(UART0, 115200, config_rx);
+ i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
+ ssp_master_on(thermo.ssp_bus_num, LPC_SSP_FRAME_SPI, 8, 4*1000*1000);
+ adc_on(NULL);
+ timer_on(LPC_TIMER_32B1, 0, ac_switch_on);
+
+ scialys_config();
+
+ while (1) {
+ static uint8_t command_val = 0;
+ int moyenne_solar = 0;
+ int moyenne_home = 0;
+ uint16_t isnail_val_solar = 0;
+ uint16_t isnail_val_home = 0;
+ uint16_t user_potar = 0;
+ int water_centi_degrees = 0;
+
+ /* Always track power consumption and production */
+ adc_get_value(&isnail_val_solar, LPC_ADC(1));
+ adc_get_value(&isnail_val_home, LPC_ADC(0));
+ adc_get_value(&user_potar, LPC_ADC(7));
+ /* Convert to mA value */
+ isnail_val_solar = ((isnail_val_solar * 32) * 2); /* 3.2mV / digit, 50mV -> 1A */
+ isnail_val_home = ((isnail_val_home * 32) * 2); /* 3.2mV / digit, 50mV -> 1A */
+ /* Store value */
+ isnail_solar_values[idx] = isnail_val_solar;
+ isnail_home_values[idx++] = isnail_val_home;
+ if (idx == NB_VAL) {
+ idx = 0;
+ }
+ /* Compute average value when we sampled enough values */
+ /* FIXME : Improve by substracting oldest value before storing new one in table and adding new one */
+ if ((idx == 0) || (idx == (NB_VAL / 2))) {
+ int i = 0;
+ for (i = 0; i < NB_VAL; i++) {
+ moyenne_solar += isnail_solar_values[i];
+ moyenne_home += isnail_home_values[i];
+ }
+ moyenne_solar = moyenne_solar / NB_VAL;
+ moyenne_home = moyenne_home / NB_VAL;
+ } else {
+ /* Sleep for a litle more than a period (20ms at 50Hz) */
+ msleep(23);
+ continue;
+ }
+
+ /* Feed the dog */
+ if ((moyenne_solar != 0) && (moyenne_home != 0)) {
+ watchdog_feed();
+ }
+
+ /* Get thermocouple value */
+ if (1) {
+ int ret = 0;
+ ret = max31855_sensor_read(&thermo, NULL, &water_centi_degrees);
+ if (ret != 0) {
+ uprintf(UART0, "Water Temp read error : %d\n", ret);
+ }
+ }
+
+ /* Set Control Output duty cycle */
+ /* Debug Nath TMP */
+ set_ctrl_duty_cycle( (user_potar - 5) / 10 );
+
+ /* Display */
+ if (1) {
+ int abs_centi = water_centi_degrees;
+
+ if (water_centi_degrees < 0) {
+ abs_centi = -water_centi_degrees;
+ }
+
+ if (1) {
+ uprintf(UART0, "%d - Is: %d,%04d - Ih: %d,%04d\n", loop++,
+ (moyenne_solar / 1000), (moyenne_solar % 1000),
+ (moyenne_home / 1000), (moyenne_home % 1000));
+ uprintf(UART0, "Temp : % 4d.%02d\n", (water_centi_degrees / 100), (abs_centi % 100));
+ uprintf(UART0, "ADC: Sol: %dmA, Home: %dmA, User: %d\n",
+ isnail_val_solar, isnail_val_home, user_potar);
+ uprintf(UART0, "ZC_cnt: %d, user: %d\n", zc_count, user_potar / 10);
+ uprintf(UART0, "CMD: %d/%d, Fan: %d\n\n", command_val, act_cmd, fan_on);
+ }
+
+ if (1) {
+ ws2812_set_pixel(0, (isnail_val_home / 2000), (isnail_val_solar / 2000), (fan_on * 20));
+ ws2812_set_pixel(1, 0, 0, (user_potar >> 2));
+ ws2812_send_frame(0);
+ }
+
+ if (1) {
+ char line[DISPLAY_LINE_LENGTH];
+ /* Erase screen (internal copy) */
+ ssd130x_buffer_set(gddram, 0x00);
+ /* Display info */
+ snprintf(line, DISPLAY_LINE_LENGTH, "Water:% 2d.%03d %cC", (water_centi_degrees / 100), (abs_centi % 100), 0x1F);
+ display_line(2, 0, line);
+ snprintf(line, DISPLAY_LINE_LENGTH, "Prod :% 2d,%03dA", (isnail_val_solar / 1000), ((isnail_val_solar % 1000) / 10));
+ display_line(3, 0, line);
+ snprintf(line, DISPLAY_LINE_LENGTH, "Conso:% 2d,%03dA", (isnail_val_home / 1000), ((isnail_val_home % 1000) / 10));
+ display_line(4, 0, line);
+ snprintf(line, DISPLAY_LINE_LENGTH, "Command: %d%%", act_cmd);
+ display_line(5, 0, line);
+ /* Update Oled display */
+ ssd130x_display_full_screen(&display);
+ }
+ }
+ }
+ return 0;
+}
+
+
+