From 9a0ba7eb3c0d51ac9f201af43ac7da62edf8c8f3 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 11 Nov 2021 03:54:47 +0100 Subject: [PATCH] Namespace update for time and uSD part of Scialys support app Also add comments and reorder functions according to call order from main(). --- v10/config.c | 298 +++++++++++++++++++++++++----------------------- v10/config.h | 6 +- v10/interface.c | 4 +- v10/main.c | 4 +- 4 files changed, 161 insertions(+), 151 deletions(-) diff --git a/v10/config.c b/v10/config.c index cb43fc1..8b925c9 100644 --- a/v10/config.c +++ b/v10/config.c @@ -154,8 +154,34 @@ void fault_info(const char* name, uint32_t len) } +/***************************************************************************** */ +/* GPIO */ + +extern void zero_cross_detect(uint32_t gpio); +extern void overvoltage_protect(uint32_t gpio); + +void board_io_config(void) +{ + /* Immediatly turn off Mosfet / Triac */ + config_gpio(&ac_ctrl, 0, GPIO_DIR_OUT, 1); + /* Start with FAN ON */ + config_gpio(&fan_ctrl, 0, GPIO_DIR_OUT, 1); + + /* Configure Input GPIO */ + config_gpio(&ext_disable_in_pin, 0, GPIO_DIR_IN, 0); + config_gpio(&overtemperature_pin, 0, GPIO_DIR_IN, 0); + + /* Over-voltage protection */ + set_gpio_callback(overvoltage_protect, &overvoltage_pin, EDGE_RISING); + + /* Zero-crossing detection */ + set_gpio_callback(zero_cross_detect, &zero_cross_in_pin, EDGE_FALLING); +} + + /***************************************************************************** */ /* Internal modules */ + const struct lpc_tc_config ac_timer_conf_zc = { .mode = LPC_TIMER_MODE_TIMER | LPC_TIMER_MODE_MATCH, .match_control = { LPC_TIMER_INT_RESET_ON_MATCH, 0, 0, 0, }, @@ -183,43 +209,145 @@ void modules_config(void) ssp_master_on(SSP_BUS_0, LPC_SSP_FRAME_SPI, 8, 4*1000*1000); adc_on(NULL); adc_start_burst_conversion(ADC_MCH(0) | ADC_MCH(1) | ADC_MCH(2), LPC_ADC_SEQ(0)); - timer_on(LPC_TIMER_32B0, 0, zero_cross); /* This one is used to generate the second zero-crossing */ + /* Configure and start timers */ + /* This one is used to generate the second zero-crossing, which is not seen by the ZC detector */ + timer_on(LPC_TIMER_32B0, 0, zero_cross); timer_counter_config(LPC_TIMER_32B0, &ac_timer_conf_zc); - timer_on(LPC_TIMER_32B1, 0, ac_switch_on); /* This one is used to generate the power PMW */ + /* This one is used to generate the power PMW */ + timer_on(LPC_TIMER_32B1, 0, ac_switch_on); timer_counter_config(LPC_TIMER_32B1, &ac_timer_conf_delay); - timer_on(LPC_TIMER_16B0, 0, power_track); /* This one is used to call the load power tracking ADC sampler */ + /* This one is used to call the load power tracking ADC sampler */ + timer_on(LPC_TIMER_16B0, 0, power_track); timer_counter_config(LPC_TIMER_16B0, &ac_timer_conf_power_track); } /***************************************************************************** */ -extern void zero_cross_detect(uint32_t gpio); -extern void overvoltage_protect(uint32_t gpio); -void board_io_config(void) -{ - /* Immediatly turn off Mosfet / Triac */ - config_gpio(&ac_ctrl, 0, GPIO_DIR_OUT, 1); - /* Start with FAN ON */ - config_gpio(&fan_ctrl, 0, GPIO_DIR_OUT, 1); +/* External components */ - /* Configure Input GPIO */ - config_gpio(&ext_disable_in_pin, 0, GPIO_DIR_IN, 0); - config_gpio(&overtemperature_pin, 0, GPIO_DIR_IN, 0); +/* Thermocouple reading */ +const struct max31855_sensor_config thermocouple = { + .ssp_bus_num = 0, + .chip_select = LPC_GPIO_0_7, +}; - /* Over-voltage protection */ - set_gpio_callback(overvoltage_protect, &overvoltage_pin, EDGE_RISING); +/* RTC and time */ +#define RTC_ADDR 0xA2 - /* Zero-crossing detection */ - set_gpio_callback(zero_cross_detect, &zero_cross_in_pin, EDGE_FALLING); +/* TMP101 I2C temperature sensor on Power board */ +#define TMP101_ADDR1 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */ +struct tmp101_sensor_config tmp101_sensor_power = { + .bus_num = I2C0, + .addr = TMP101_ADDR1, + .resolution = TMP_RES_ELEVEN_BITS, +}; + +/* TMP101 I2C temperature sensor on display board */ +#define TMP101_ADDR0 0x90 /* Pin Addr0 (pin5 of tmp101) connected to GND */ +struct tmp101_sensor_config tmp101_sensor_display = { + .bus_num = I2C0, + .addr = TMP101_ADDR0, + .resolution = TMP_RES_ELEVEN_BITS, +}; + +int temp_read(uint32_t uart, int* deci_degrees_disp, int* deci_degrees_power) +{ + int ret = 0; + int conv = 0; + + if (interface_board_present != 0) { + ret = tmp101_sensor_read(&tmp101_sensor_display, NULL, deci_degrees_disp); + if (ret == 0) { + conv |= CONV_DISPLAY_OK; + } else { + uprintf(uart, "TMP101 read error on display board : %d\n", ret); + } + } + if (power_board_present != 0) { + ret = tmp101_sensor_read(&tmp101_sensor_power, NULL, deci_degrees_power); + if (ret == 0) { + conv |= CONV_POWER_OK; + } else { + uprintf(uart, "TMP101 read error on power board : %d\n", ret); + } + } + return conv; } +uint8_t interface_board_present = 0; +uint8_t power_board_present = 0; + +/* Configure external components */ +int external_components_config(uint32_t uart) +{ + int ret = 0; + int retcode = 0; + + /* RTC : must be done first to be able to access RTC RAM for uSD init */ + scialys_time_config(I2C0, RTC_ADDR); + scialys_time_init_check(uart); + + /* uSD card */ + scialys_uSD_config((struct pio *)&uSD_cs, SSP_BUS_0); + scialys_uSD_detect(uart); + scialys_uSD_logs_init(uart); + + /* Thermocouple configuration */ + max31855_sensor_config(&thermocouple); + + /* TMP101 sensor config on power board */ + power_board_present = 1; + ret = tmp101_sensor_config(&tmp101_sensor_power); + if (ret != 0) { + uprintf(uart, "Temp config error on power board: %d\n", ret); + power_board_present = 0; + retcode -= 1; + } else { + ret = tmp101_sensor_set_continuous_conversion(&tmp101_sensor_power); + if (ret != 0) { + uprintf(uart, "Temp config part 2 error on power board: %d\n", ret); + power_board_present = 0; + retcode -= 2; + } + } + + /* TMP101 sensor config on display board */ + ret = tmp101_sensor_config(&tmp101_sensor_display); + if (ret != 0) { + uprintf(uart, "Temp config error on display board: %d\n", ret); + interface_board_present = 0; + retcode -= 4; + } else { + ret = tmp101_sensor_set_continuous_conversion(&tmp101_sensor_display); + if (ret != 0) { + uprintf(uart, "Temp config part 2 error on display board: %d\n", ret); + interface_board_present = 0; + retcode -= 8; + } else { + interface_board_present = 1; + } + } + + /* Configure interface board if present */ + if (interface_board_present == 1) { + ret = interface_config(uart); + if (ret != 0) { + retcode -= 16; + } + } + + return retcode; +} + /***************************************************************************** */ -/* Load or update user configuration from internal User Flash */ +/* App config and/or default values : + * Load or update user configuration from internal User Flash */ + /* The configuration is stored in the first user info page. - * The third one is used to store the "last used" uSD block only once per week - * in order to prevent deterioration of the flash block. This information is + * The third user info page is used to store the "last used" uSD block only once per + * week in order to prevent deterioration of the flash block. This information is * stored in the RTC RAM backed by the supercapa each time a new block is used. */ @@ -229,7 +357,7 @@ struct scialys_config sc_conf; * the board config part to RAM so that access to these is quicker. * If in need of memory, the config could be used from flash at the expense of execution time */ -void read_internal_config(void) +void scialys_read_internal_config(void) { struct scialys_config* conf; uint8_t* mem = NULL; @@ -254,7 +382,7 @@ void read_internal_config(void) } /* Update the board config in user flash */ -int user_flash_update(void) +int scialys_user_flash_update(void) { uint8_t* mem = NULL; int ret = 0; @@ -324,7 +452,7 @@ uint32_t sunny_days_prod_value = 0; #define MANUAL_TYPE_TARGET 2 #define DEFAULT_MANUAL_FORCE_TYPE MANUAL_TYPE_TARGET -void check_config(void) +void scialys_check_config(void) { /* If config version error (old config) then only update new fields */ switch (sc_conf.conf_version) { @@ -363,7 +491,7 @@ void check_config(void) /* Read main configuration - or set to default one */ void read_scialys_main_config(void) { - read_internal_config(); + scialys_read_internal_config(); /* Config version error ? */ if (sc_conf.conf_version != CONFIG_VERSION) { sc_conf.config_ok = 0; @@ -376,124 +504,6 @@ void read_scialys_main_config(void) return; } /* Config is either blank or not up to date, update missing parts with defaults */ - check_config(); -} - - -/***************************************************************************** */ -/* Configure external components */ - -/* Thermocouple reading */ -const struct max31855_sensor_config thermocouple = { - .ssp_bus_num = 0, - .chip_select = LPC_GPIO_0_7, -}; - -/* RTC and time */ -#define RTC_ADDR 0xA2 - -/* TMP101 I2C temperature sensor on Power board */ -#define TMP101_ADDR1 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */ -struct tmp101_sensor_config tmp101_sensor_power = { - .bus_num = I2C0, - .addr = TMP101_ADDR1, - .resolution = TMP_RES_ELEVEN_BITS, -}; - -/* TMP101 I2C temperature sensor on display board */ -#define TMP101_ADDR0 0x90 /* Pin Addr0 (pin5 of tmp101) connected to GND */ -struct tmp101_sensor_config tmp101_sensor_display = { - .bus_num = I2C0, - .addr = TMP101_ADDR0, - .resolution = TMP_RES_ELEVEN_BITS, -}; - -int temp_read(uint32_t uart, int* deci_degrees_disp, int* deci_degrees_power) -{ - int ret = 0; - int conv = 0; - - if (interface_board_present != 0) { - ret = tmp101_sensor_read(&tmp101_sensor_display, NULL, deci_degrees_disp); - if (ret == 0) { - conv |= CONV_DISPLAY_OK; - } else { - uprintf(uart, "TMP101 read error on display board : %d\n", ret); - } - } - if (power_board_present != 0) { - ret = tmp101_sensor_read(&tmp101_sensor_power, NULL, deci_degrees_power); - if (ret == 0) { - conv |= CONV_POWER_OK; - } else { - uprintf(uart, "TMP101 read error on power board : %d\n", ret); - } - } - return conv; + scialys_check_config(); } - -uint8_t interface_board_present = 0; -uint8_t power_board_present = 0; - - -int external_comp_config(uint32_t uart) -{ - int ret = 0; - int retcode = 0; - - /* RTC : must be done first to be able to access RTC RAM for uSD init */ - time_config(I2C0, RTC_ADDR); - time_init_check(uart); - - /* uSD card */ - uSD_config((struct pio *)&uSD_cs, SSP_BUS_0); - uSD_detect(uart); - uSD_logs_init(uart); - - /* Thermocouple configuration */ - max31855_sensor_config(&thermocouple); - - /* TMP101 sensor config on power board */ - power_board_present = 1; - ret = tmp101_sensor_config(&tmp101_sensor_power); - if (ret != 0) { - uprintf(uart, "Temp config error on power board: %d\n", ret); - power_board_present = 0; - retcode -= 1; - } else { - ret = tmp101_sensor_set_continuous_conversion(&tmp101_sensor_power); - if (ret != 0) { - uprintf(uart, "Temp config part 2 error on power board: %d\n", ret); - power_board_present = 0; - retcode -= 2; - } - } - - /* TMP101 sensor config on display board */ - ret = tmp101_sensor_config(&tmp101_sensor_display); - if (ret != 0) { - uprintf(uart, "Temp config error on display board: %d\n", ret); - interface_board_present = 0; - retcode -= 4; - } else { - ret = tmp101_sensor_set_continuous_conversion(&tmp101_sensor_display); - if (ret != 0) { - uprintf(uart, "Temp config part 2 error on display board: %d\n", ret); - interface_board_present = 0; - retcode -= 8; - } else { - interface_board_present = 1; - } - } - - /* Configure interface board if present */ - if (interface_board_present == 1) { - ret = interface_config(uart); - if (ret != 0) { - retcode -= 16; - } - } - - return retcode; -} diff --git a/v10/config.h b/v10/config.h index 6870bd8..f70c351 100644 --- a/v10/config.h +++ b/v10/config.h @@ -106,12 +106,12 @@ extern struct scialys_config sc_conf; extern uint32_t sunny_days_prod_value; /* Check that current config is valid */ -void check_config(void); +void scialys_check_config(void); /* Read main configuration from internal user flash - or set to default one */ void read_scialys_main_config(void); -int user_flash_update(void); +int scialys_user_flash_update(void); extern uint8_t interface_board_present; extern uint8_t power_board_present; @@ -166,7 +166,7 @@ void modules_config(void); void board_io_config(void); /* Configure external components */ -int external_comp_config(uint32_t uart); +int external_components_config(uint32_t uart); /***************************************************************************** */ diff --git a/v10/interface.c b/v10/interface.c index 732b81b..2629901 100644 --- a/v10/interface.c +++ b/v10/interface.c @@ -549,7 +549,7 @@ void config_interface_handle(void) snprintf(line, DISP_LLEN, "Saving ..."); display_line(4, 2, line); ssd130x_display_full_screen(&display); - ret = user_flash_update(); + ret = scialys_user_flash_update(); uprintf(UART0, "\nFlash update from menu: %d\n", ret); msleep(1500); sub_menu_level = 0; @@ -574,7 +574,7 @@ void config_interface_handle(void) display_line(4, 8, line); ssd130x_display_full_screen(&display); sc_conf.conf_version = 0x00; - check_config(); + scialys_check_config(); uprintf(UART0, "\nSystem defaults reloaded from menu\n"); msleep(500); sub_menu_level = 0; diff --git a/v10/main.c b/v10/main.c index b220df0..8da9542 100644 --- a/v10/main.c +++ b/v10/main.c @@ -569,7 +569,7 @@ int main(void) msleep(800); /* Required for uSD to stabilise before SPI is setup .... */ board_io_config(); modules_config(); - external_comp_config(UART0); + external_components_config(UART0); read_scialys_main_config(); /* Compute max_intensity from config grid_power_limit. @@ -683,7 +683,7 @@ int main(void) if (cur_tick > (last_tick_store + (NB_SEC_STORE * 1000))) { /* Divide by the number of values */ /* FIXME */ - uSD_append_data(&data); + scialys_uSD_append_data(&data); uprintf(UART0, "Saved 5s data\n"); nb_val = 0; last_tick_store = cur_tick; -- 2.43.0