diff --git a/.vs/TE_Controller/v14/.atsuo b/.vs/TE_Controller/v14/.atsuo index 0b9b262..a75a1e9 100644 Binary files a/.vs/TE_Controller/v14/.atsuo and b/.vs/TE_Controller/v14/.atsuo differ diff --git a/TE_Controller/TE_Controller.cproj b/TE_Controller/TE_Controller.cproj index 36009e4..4cfe186 100644 --- a/TE_Controller/TE_Controller.cproj +++ b/TE_Controller/TE_Controller.cproj @@ -1766,9 +1766,15 @@ compile + + compile + compile + + compile + compile @@ -1811,9 +1817,15 @@ compile + + compile + compile + + compile + compile diff --git a/TE_Controller/src/config/FreeRTOSConfig.h b/TE_Controller/src/config/FreeRTOSConfig.h index c88e882..a881def 100644 --- a/TE_Controller/src/config/FreeRTOSConfig.h +++ b/TE_Controller/src/config/FreeRTOSConfig.h @@ -56,7 +56,7 @@ #define configCPU_CLOCK_HZ ( 48000000) #define configTICK_RATE_HZ ( 1000 ) #define configMAX_PRIORITIES ( 5 ) -#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 130 ) +#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 64 ) #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 4 * 1024 ) ) #define configMAX_TASK_NAME_LEN ( 24 ) #define configUSE_TRACE_FACILITY 1 diff --git a/TE_Controller/src/include/adc_user.h b/TE_Controller/src/include/adc_user.h index 66e2f25..f960163 100644 --- a/TE_Controller/src/include/adc_user.h +++ b/TE_Controller/src/include/adc_user.h @@ -25,4 +25,5 @@ float adc_get_V(uint16_t value); float adc_get_Q(uint16_t value); uint16_t adc_read_value_spec(ADC_chan_t); float adc_get_V_spec(ADC_chan_t chan); + #endif /* ADC_H_ */ \ No newline at end of file diff --git a/TE_Controller/src/include/mcu_control.h b/TE_Controller/src/include/mcu_control.h index b8ebbda..88f7cef 100644 --- a/TE_Controller/src/include/mcu_control.h +++ b/TE_Controller/src/include/mcu_control.h @@ -8,10 +8,11 @@ #ifndef MCU_CONTROL_H_ #define MCU_CONTROL_H_ - +#include "stdint-gcc.h" #define MAX_STR_FLOAT 12 void MCU_control_init(void); void MCU_control(void); +float GetTecCurrent(float currentSenseResistence); #endif /* MCU_CONTROL_H_ */ \ No newline at end of file diff --git a/TE_Controller/src/include/power_regulator.h b/TE_Controller/src/include/power_regulator.h new file mode 100644 index 0000000..21e7398 --- /dev/null +++ b/TE_Controller/src/include/power_regulator.h @@ -0,0 +1,20 @@ +/* + * power_regulator.h + * + * Created: 18.01.2021 22:07:56 + * Author: Lexus + */ + + +#ifndef POWER_REGULATOR_H_ +#define POWER_REGULATOR_H_ +#include "stdbool.h" +#include + + +void temperature_control(float TEC_Temp, float *TEC_Power, bool enable); +//void TEC_power_set(bool value); + + + +#endif /* POWER_REGULATOR_H_ */ \ No newline at end of file diff --git a/TE_Controller/src/include/tcc_user.h b/TE_Controller/src/include/tcc_user.h new file mode 100644 index 0000000..4c45901 --- /dev/null +++ b/TE_Controller/src/include/tcc_user.h @@ -0,0 +1,17 @@ +/* + * tcc.h + * + * Created: 18.01.2021 22:11:39 + * Author: Lexus + */ + + +#ifndef TCC_H_ +#define TCC_H_ +#include "tc.h" +void configure_tcc(void); +void print_tcc_status(void); +void configure_tc(void); +void configure_tc_callbacks(void); +void tc_callback(struct tc_module *const module_inst); +#endif /* TCC_H_ */ \ No newline at end of file diff --git a/TE_Controller/src/main.c b/TE_Controller/src/main.c index 011e26e..676b1e4 100644 --- a/TE_Controller/src/main.c +++ b/TE_Controller/src/main.c @@ -1,24 +1,22 @@ /** * \file * - * \brief CDC Application Main functions + * \brief TE_Controller * - * Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries. - * - * \asf_license_start + * Copyright (c) 2020-2021 Realsence Inc. and its subsidiaries. * * \page License * - * Subject to your compliance with these terms, you may use Microchip - * software and any derivatives exclusively with Microchip products. + * Subject to your compliance with these terms, you may use Realsence + * software and any derivatives exclusively with Realsence products. * It is your responsibility to comply with third party license terms applicable * to your use of third party software (including open source software) that - * may accompany Microchip software. + * may accompany Realsence software. * - * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, + * THIS SOFTWARE IS SUPPLIED BY Realsence "AS IS". NO WARRANTIES, * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, - * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE + * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL Realsence BE * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE @@ -26,14 +24,9 @@ * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. - * - * \asf_license_stop - * - */ -/* - * Support and FAQ: visit Microchip Support */ + #include #include "conf_usb.h" #include "ui.h" @@ -43,6 +36,13 @@ #include "realsence.h" #include "ntc.h" #include +#include "dht.h" +#include "mcu_control.h" +#include "adc_user.h" +#include "power_regulator.h" +#include "tec.h" +#include "backlight.h" +#include "ws2812.h" #define PORT_STDIO 0 #define PORT_DATA 1 @@ -50,150 +50,232 @@ uint16_t adc_value = 100; //char rcvBuff[128] = {0}; char str[128] = {0}; -UBaseType_t uxHighWaterMark_cdc_rx_check; -UBaseType_t uxHighWaterMark_led_blink, uxHighWaterMark_mesure; +UBaseType_t uxHighWaterMark_cdc_rx_check, uxHighWaterMark_cdc_tx; +UBaseType_t uxHighWaterMark_led_blink, uxHighWaterMark_mesure, uxHighWaterMark_regulator, uxHighWaterMark_backlight; static volatile bool main_b_cdc_enable = false; struct measured_params m_params; Controller_t Controller; -/*! \brief Main function. Execution starts here. - */ +/************************************************************************ +* \brief Main function. Execution starts here. +************************************************************************/ int main(void) { - irq_initialize_vectors(); - cpu_irq_enable(); - - // Initialize the sleep manager + cpu_irq_enable(); sleepmgr_init(); + system_init(); -#if !SAM0 - sysclk_init(); - board_init(); -#else - system_init(); -#endif - - // Start USB stack to authorize VBus monitoring - InitTask_cdc_rx_check(); - // Init LED + InitTask_cdc_rx_tx(); InitTask_led_blink();//ui_init();//ui_powerdown(); InitTask_measure(); + InitTask_regulator(); + InitTask_backlight(); vTaskStartScheduler(); - while(true){ - __BKPT(); + __BKPT(); +} + + +/************************************************************************ +* \fn void Task_measure(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_measure(void *parameters) +{ + uxHighWaterMark_mesure = uxTaskGetStackHighWaterMark( NULL ); + for (;;) + { + Controller.temps.MCU_Temp = NTC_MCU_get_temp(NULL); + Controller.temps.TEC_Temp = NTC_TEC_get_temp(NULL, NULL); + //measurement_DHT22(); + Controller.tecState.tecV_N = adc_get_V_spec(chan_LFB); + Controller.tecState.tecV_P = adc_get_V_spec(chan_SFB); + Controller.tecState.tecI = GetTecCurrent(CURRENT_SENSE_RESISTENCE); + + uxHighWaterMark_mesure = uxTaskGetStackHighWaterMark( NULL ); + vTaskDelay(20); } } -void Task_cdc_rx_check(void *parameters) + +/************************************************************************ +* \fn void Task_regulator(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_regulator(void *parameters) +{ + uxHighWaterMark_regulator = uxTaskGetStackHighWaterMark( NULL ); + for (;;) + { + temperature_control(Controller.temps.MCU_Temp, &m_params.TEC_Power, ENABLE); + uxHighWaterMark_regulator = uxTaskGetStackHighWaterMark( NULL ); + //vTaskDelay(1000); + } +} + + +/************************************************************************ +* \fn void Task_cdc_rx(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_cdc_rx(void *parameters) { #define PORT0 0 - iram_size_t len = 0; - uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); + iram_size_t len = 0; + uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); while(true) - { - if(main_b_cdc_enable && - (len = udi_cdc_multi_get_nb_received_data(PORT0)) > 0 && - udi_cdc_read_no_polling(str, (len<=128)?len:128) > 0 ) - { + { + if(main_b_cdc_enable && + (len = udi_cdc_multi_get_nb_received_data(PORT0)) > 0 && + udi_cdc_read_no_polling(str, (len<=128)?len:128) > 0 ) + { str[len]=0; - printf("<%s\n", str); + printf("<%s\n", str); } uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); } } -/*void Task_cdc_rx_check(void *parameters) + + +/************************************************************************ +* \fn void Task_cdc_tx(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_cdc_tx(void *parameters) { #define PORT0 0 - char rcvBuf[128]; - char *pStr = rcvBuf; - int len=0; - uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); + uxHighWaterMark_cdc_tx = uxTaskGetStackHighWaterMark( NULL ); while(true) - { - if (main_b_cdc_enable) + { + if (main_b_cdc_enable && udi_cdc_multi_is_tx_ready(PORT0)) { - int symb = udi_cdc_getc(); - if(symb) - { - len += sprintf(pStr++, "%c", symb); - } - if(symb == '\n') - { - udi_cdc_write_buf(rcvBuf, len); - pStr = rcvBuf; - len = 0; - } - } - uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); + if(fpclassify(Controller.temps.MCU_Temp) == FP_NAN) + printf(">NTC_MCU_TEMP = NAN\n\r"); + else + printf(">NTC_MCU_TEMP = %d\n\r", (int)Controller.temps.MCU_Temp);//printf(">NTC_MCU_TEMP = %d, NTC_TEC_TEMP = %d\n\r", (int)Controller.temps.MCU_Temp, (int)Controller.temps.TEC_Temp); + } + LED_Toggle(LED_PIN); + vTaskDelay(1000); + uxHighWaterMark_cdc_tx = uxTaskGetStackHighWaterMark( NULL ); } -}*/ -void InitTask_cdc_rx_check(void) +} + +/************************************************************************ +* \fn void Task_led_blink(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_backlight(void *parameters) +{ uint32_t cnt10 = 0; + uxHighWaterMark_backlight = uxTaskGetStackHighWaterMark( NULL ); + while(1) + { + backlight_color_show(255, 0, 0); + vTaskDelay(1000); + backlight_color_show(255, 100, 0); + vTaskDelay(1000); + backlight_color_show(0, 255, 0); + vTaskDelay(1000); + /*backlight_event_100ms(); + vTaskDelay(100); + if(!((++cnt10)%10)) + backlight_event_1s();*/ + uxHighWaterMark_backlight = uxTaskGetStackHighWaterMark( NULL ); + } +} + +/************************************************************************ +* \fn void InitTask_cdc_rx_tx(void) +* \brief +* \param +* \return +************************************************************************/ +void InitTask_cdc_rx_tx(void) { // Enable USB Stack Device stdio_usb_init();//udc_start(); stdio_usb_enable(); - xTaskCreate(Task_cdc_rx_check, (const char*)"Task_cdc_rx_check", configMINIMAL_STACK_SIZE*3, NULL,configMAX_PRIORITIES-1, NULL); + xTaskCreate(Task_cdc_rx, (const char*)"Task_cdc_rx", configMINIMAL_STACK_SIZE*1, NULL,configMAX_PRIORITIES-1, NULL); + xTaskCreate(Task_cdc_tx, (const char*)"Task_cdc_tx", configMINIMAL_STACK_SIZE*3, NULL,configMAX_PRIORITIES-1, NULL); } -void Task_led_blink(void *parameters) +/************************************************************************ +* \fn void InitTask_regulator(void) +* \brief +* \param +* \return +************************************************************************/ +void InitTask_regulator(void) { - //long int c1=0; - //long int c2=0; - int cnt=0; - /* Inspect our own high water mark on entering the task. */ + xTaskCreate(Task_regulator, (const char*)"Task_regulator", configMINIMAL_STACK_SIZE*3, NULL,configMAX_PRIORITIES-1, NULL); +} + +/************************************************************************ +* \fn void InitTask_measure(void) +* \brief +* \param +* \return +************************************************************************/ +void InitTask_measure(void) +{ + configure_adc(); + xTaskCreate(Task_measure, (const char*)"Task_measure", configMINIMAL_STACK_SIZE*2, NULL,configMAX_PRIORITIES-1, NULL); +} + +/************************************************************************ +* \fn void InitTask_backlight(void) +* \brief +* \param +* \return +************************************************************************/ +void InitTask_backlight(void) +{ + ws2812_configure_port_pins(); + backlight_init(); + backlight_mode_demo(); + xTaskCreate(Task_backlight, (const char*)"Task_backlight", configMINIMAL_STACK_SIZE*1, NULL,configMAX_PRIORITIES-1, NULL); +} + + + +/************************************************************************ +* \fn void Task_led_blink(void *parameters) +* \brief +* \param +* \return +************************************************************************/ +void Task_led_blink(void *parameters) +{ + int cnt=0; uxHighWaterMark_led_blink = uxTaskGetStackHighWaterMark( NULL ); while(1) { if (main_b_cdc_enable && udi_cdc_multi_is_tx_ready(PORT0)) printf(">%u sec\n\r", (cnt++));//// stdio_usb_putchar (NULL, "data");// vTaskDelay(1000); - LED_Toggle(LED_PIN); - /*if((c1 % 50000) == 0){ - //periodic_event_1s(); - if(!stdio_cdc_opened){ - if(c2 % 2){ - set_led(false); - }else{ - set_led(true); - } - }else{ - if(c2 % 3){ - set_led(false); - }else{ - set_led(true); - } - } - c2++; - } - c1++;*/ + LED_Toggle(LED_PIN); uxHighWaterMark_led_blink = uxTaskGetStackHighWaterMark( NULL ); } } -void Task_measure(void *parameters) -{ - uxHighWaterMark_mesure = uxTaskGetStackHighWaterMark( NULL ); - for (;;) - { - Controller.temps.MCU_Temp = NTC_MCU_get_temp(NULL); - Controller.temps.TEC_Temp = NTC_TEC_get_temp(NULL, NULL); - if (main_b_cdc_enable && udi_cdc_multi_is_tx_ready(PORT0)) - { - if(fpclassify(Controller.temps.MCU_Temp) == FP_NAN) - printf(">NTC_MCU_TEMP = NAN\n\r"); - else - printf(">NTC_MCU_TEMP = %d\n\r", (int)Controller.temps.MCU_Temp);//printf(">NTC_MCU_TEMP = %d, NTC_TEC_TEMP = %d\n\r", (int)Controller.temps.MCU_Temp, (int)Controller.temps.TEC_Temp); - } - LED_Toggle(LED_PIN); - uxHighWaterMark_mesure = uxTaskGetStackHighWaterMark( NULL ); - vTaskDelay(1000); - } -} - +/************************************************************************ +* \fn void Task_led_blink(void *parameters) +* \brief +* \param +* \return +************************************************************************/ void InitTask_led_blink(void) { led_configure_port_pins(); @@ -201,11 +283,7 @@ void InitTask_led_blink(void) //xTaskCreate(Task_led_blink, (const char*)"Task_led_blink", configMINIMAL_STACK_SIZE*2, NULL,configMAX_PRIORITIES-1, NULL); } -void InitTask_measure(void) -{ - configure_adc(); - xTaskCreate(Task_measure, (const char*)"Task_measure", configMINIMAL_STACK_SIZE*2, NULL,configMAX_PRIORITIES-1, NULL); -} + void main_suspend_action(void) @@ -257,16 +335,7 @@ void main_cdc_disable(uint8_t port) uart_close(port); } -/*void main_cdc_set_dtr(uint8_t port, bool b_enable) -{ - if (b_enable) { - // Host terminal has open COM - ui_com_open(port); - }else{ - // Host terminal has close COM - ui_com_close(port); - } -}*/ + void main_cdc_set_dtr(uint8_t port, bool b_enable) { if(port == PORT_STDIO){ @@ -292,21 +361,42 @@ void led_configure_port_pins(void) port_pin_set_config(LED_PIN, &config_port_pin); } -void vApplicationMallocFailedHook (void) { +/************************************************************************ +* \fn void vApplicationMallocFailedHook (void) +* \brief +* \param +* \return +************************************************************************/ +void vApplicationMallocFailedHook (void) +{ while (1) { __BKPT(); }; } -void vApplicationStackOverflowHook (void) { +/************************************************************************ +* \fn void vApplicationStackOverflowHook (void) +* \brief +* \param +* \return +************************************************************************/ +void vApplicationStackOverflowHook (void) +{ while (1) { __BKPT(); }; } -void prvGetRegistersFromStack (uint32_t *pulFaultStackAddress) { +/************************************************************************ +* \fn void prvGetRegistersFromStack (uint32_t *pulFaultStackAddress) +* \brief +* \param +* \return +************************************************************************/ +void prvGetRegistersFromStack (uint32_t *pulFaultStackAddress) +{ __attribute__((unused)) volatile uint32_t r0; __attribute__((unused)) volatile uint32_t r1; __attribute__((unused)) volatile uint32_t r2; @@ -407,3 +497,42 @@ void prvGetRegistersFromStack (uint32_t *pulFaultStackAddress) { * - conf_foo.h configuration of each module * - ui.c implement of user's interface (leds,buttons...) */ + +/*void Task_cdc_rx(void *parameters) +{ + #define PORT0 0 + char rcvBuf[128]; + char *pStr = rcvBuf; + int len=0; + uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); + + while(true) + { + if (main_b_cdc_enable) + { + int symb = udi_cdc_getc(); + if(symb) + { + len += sprintf(pStr++, "%c", symb); + } + if(symb == '\n') + { + udi_cdc_write_buf(rcvBuf, len); + pStr = rcvBuf; + len = 0; + } + } + uxHighWaterMark_cdc_rx_check = uxTaskGetStackHighWaterMark( NULL ); + } +}*/ + +/*void main_cdc_set_dtr(uint8_t port, bool b_enable) +{ + if (b_enable) { + // Host terminal has open COM + ui_com_open(port); + }else{ + // Host terminal has close COM + ui_com_close(port); + } +}*/ diff --git a/TE_Controller/src/main.h b/TE_Controller/src/main.h index 9c2a1f3..af3760a 100644 --- a/TE_Controller/src/main.h +++ b/TE_Controller/src/main.h @@ -39,6 +39,10 @@ #include "usb_protocol_cdc.h" +#define CURRENT_SENSE_RESISTENCE 0.005f +#define TEC_TEMP_MAX 35.0f +#define TEC_TEMP_MIN 5.0f +#define TEC_TEMP_NORM 25.0f typedef struct measured_params{ float TEC_Temp; @@ -62,12 +66,14 @@ typedef struct typedef struct { - float tecV; + float tecV_P; + float tecV_N; float tecI; } TecState_t; + typedef struct { - Temperatures_t temps; + Temperatures_t temps; TecState_t tecState; } Controller_t; @@ -75,14 +81,18 @@ void prvGetRegistersFromStack (uint32_t *pulFaultStackAddress); void led_configure_port_pins(void); void vApplicationMallocFailedHook (void); void vApplicationStackOverflowHook (void); -void Task_cdc_rx_check(void *parameters); +void Task_cdc_rx(void *parameters); +void Task_cdc_tx(void *parameters); void Task_led_blink(void *parameters); void Task_measure(void *parameters); void Task_regulator(void *parameters); -void InitTask_cdc_rx_check(void); +void Task_backlight(void *parameters); +void InitTask_backlight(void); +void InitTask_cdc_rx(void); void InitTask_led_blink(void); void InitTask_regulator(void); void InitTask_measure(void); +void InitTask_cdc_rx_tx(void); /*! \brief Opens the communication port * This is called by CDC interface when USB Host enable it. diff --git a/TE_Controller/src/source/adc_user.c b/TE_Controller/src/source/adc_user.c index 4e5ba31..32ae601 100644 --- a/TE_Controller/src/source/adc_user.c +++ b/TE_Controller/src/source/adc_user.c @@ -39,12 +39,6 @@ void configure_adc(void) //adc_start_conversion(&adc_instance); } -/*uint16_t adc_read_value(void) -{ - uint16_t result = 0xFFFF; - adc_read(&adc_instance, &result); - return result; -}*/ uint16_t adc_read_value(void){ adc_start_conversion(&adc_instance); uint16_t result; @@ -71,4 +65,9 @@ float adc_get_V(uint16_t value) { float Uvcc=3.3; return adc_get_Q(value) * Uvcc; +} + +float adc_get_V_spec(ADC_chan_t chan){ + uint16_t value = adc_read_value_spec(chan); + return adc_get_V(value); } \ No newline at end of file diff --git a/TE_Controller/src/source/backlight.c b/TE_Controller/src/source/backlight.c index 44bee0f..a22efbb 100644 --- a/TE_Controller/src/source/backlight.c +++ b/TE_Controller/src/source/backlight.c @@ -31,7 +31,7 @@ void backlight_event_100ms(void){ led_data[0 + (LEN_WS2812/2 - i - 1)*3 + (LEN_WS2812/2)*3] = 0x00; led_data[1 + (LEN_WS2812/2 - i - 1)*3 + (LEN_WS2812/2)*3] = 0xff; led_data[2 + (LEN_WS2812/2 - i - 1)*3 + (LEN_WS2812/2)*3] = 0x00; - }else{ + }else{ led_data[0 + i*3] = 0x00; led_data[1 + i*3] = 0x00; led_data[2 + i*3] = 0x00; @@ -63,31 +63,31 @@ void backlight_event_1s(void){ led_data[1 + i*3] = 0xff; led_data[2 + i*3] = 0xff; } - }else if((s1_cnt % modes) == 1){ + }else if((s1_cnt % modes) == 1){ for(uint16_t i=0; i < LEN_WS2812;i++){ led_data[0 + i*3] = 0xff; led_data[1 + i*3] = 0x00; led_data[2 + i*3] = 0x00; } - }else if((s1_cnt % modes) == 2){ + }else if((s1_cnt % modes) == 2){ for(uint16_t i=0; i < LEN_WS2812;i++){ led_data[0 + i*3] = 0x00; led_data[1 + i*3] = 0xff; led_data[2 + i*3] = 0x00; } - }else if((s1_cnt % modes) == 3){ + }else if((s1_cnt % modes) == 3){ for(uint16_t i=0; i < LEN_WS2812;i++){ led_data[0 + i*3] = 0x00; led_data[1 + i*3] = 0x00; led_data[2 + i*3] = 0xff; } - }else if((s1_cnt % modes) == 4){ + }else if((s1_cnt % modes) == 4){ for(uint16_t i=0; i < LEN_WS2812;i++){ led_data[0 + i*3] = 0x00; led_data[1 + i*3] = 0x00; led_data[2 + i*3] = 0x00; } - }else if((s1_cnt % modes) == 5){ + }else if((s1_cnt % modes) == 5){ mode_run_light=true; mode_run_light_cnt=0; } @@ -111,13 +111,14 @@ void backlight_init(void){ } void backlight_ws2812_sendarray(void){ - cpu_irq_disable(); + portENTER_CRITICAL();//cpu_irq_disable(); ws2812_sendarray(led_data,sizeof(led_data)); - cpu_irq_enable(); + portEXIT_CRITICAL();//cpu_irq_enable(); } void backlight_color_show(uint8_t R, uint8_t G, uint8_t B){ - for(uint16_t i=0; i < LEN_WS2812; i++){ + for(uint16_t i=0; i < LEN_WS2812; i++) + { led_data[0 + i*3] = G; led_data[1 + i*3] = R; led_data[2 + i*3] = B; diff --git a/TE_Controller/src/source/dht.c b/TE_Controller/src/source/dht.c index c7799c6..18f3f8d 100644 --- a/TE_Controller/src/source/dht.c +++ b/TE_Controller/src/source/dht.c @@ -10,7 +10,7 @@ #define lineDown() port_pin_set_output_level(DHT_Pin, 0) #define lineUp() port_pin_set_output_level(DHT_Pin, 1) #define getLine() port_pin_get_input_level(DHT_Pin) -#define Delay(d) delay_ms(d) +#define Delay(d) vTaskDelay(d) #define CPU_IRQ_enable() cpu_irq_enable() #define CPU_IRQ_disable() cpu_irq_disable() @@ -37,9 +37,9 @@ static void goToInput(void) { uint8_t DHT_getData(DHT_type t, DHT_data* p_data){ uint8_t ret; - CPU_IRQ_disable(); + //CPU_IRQ_disable(); ret = _DHT_getData(t, p_data); - CPU_IRQ_enable(); + //CPU_IRQ_enable(); return ret; } diff --git a/TE_Controller/src/source/light_ws2812_cortex.c b/TE_Controller/src/source/light_ws2812_cortex.c index f9a2c37..b9b2a57 100644 --- a/TE_Controller/src/source/light_ws2812_cortex.c +++ b/TE_Controller/src/source/light_ws2812_cortex.c @@ -30,7 +30,6 @@ #define ws2812_DEL ws2812_DEL8 ws2812_DEL2 - void ws2812_sendarray(uint8_t *data,int datlen) { uint8_t gpio_pin = PIN_PA23; diff --git a/TE_Controller/src/source/mcu_control.c b/TE_Controller/src/source/mcu_control.c index c212370..52f2b29 100644 --- a/TE_Controller/src/source/mcu_control.c +++ b/TE_Controller/src/source/mcu_control.c @@ -11,11 +11,16 @@ #include "adn8831.h" #include "tm_onewire.h" #include "tm_ds18b20.h" - +#include "adc_user.h" void MCU_control_init(void){ } +float GetTecCurrent(float currentSenseResistence) +{ + return adc_get_V_spec(chan_CS)/currentSenseResistence; +} + void MCU_control(void){ char t_TEC_str[MAX_STR_FLOAT]; char t_MCU_str[MAX_STR_FLOAT]; @@ -56,7 +61,7 @@ void MCU_control(void){ char DS1820_Temp_str[MAX_STR_FLOAT]; //bool ow_connected __attribute__((used)) =false; float DS1820_temp=0.0; - cpu_irq_disable(); + //cpu_irq_disable(); if (TM_OneWire_First(&ow_instance)) { //ow_connected=true; if(TM_DS18B20_Is(ow_instance.ROM_NO)){ @@ -71,7 +76,7 @@ void MCU_control(void){ } } } - cpu_irq_enable(); + //cpu_irq_enable(); make_float2str(DS1820_Temp_str, MAX_STR_FLOAT, (float)DS1820_temp); // printf("Info: t_MCU: %10s, t_TEC:%10s, Vin_LFB: %6s, Vin_SFB: %6s, Vin_CS: %6s, DHT22_Temp: %s, DHT22_Hum: %s, OW_STATE: %d, DS1820_Temp: %s\r\n",t_MCU_str, t_TEC_str, Vin_LFB_str, Vin_SFB_str, Vin_CS_str, DHT22_Temp, DHT22_Hum, (int)ow_connected, DS1820_Temp_str); diff --git a/TE_Controller/src/source/power_regulator.c b/TE_Controller/src/source/power_regulator.c new file mode 100644 index 0000000..5185a27 --- /dev/null +++ b/TE_Controller/src/source/power_regulator.c @@ -0,0 +1,43 @@ +/* + * power_regulator.c + * + * Created: 18.01.2021 22:07:23 + * Author: Lexus + */ +#include "power_regulator.h" + +#include +#include "tec.h" +struct tcc_module tcc_instance; + + +void temperature_control(float TEC_Temp, float *TEC_Power, bool enable) +{ + if(!enable) + { + TEC_set_level(0); + return; + } + float temp_goal=10; + float temp_diff = (temp_goal - TEC_Temp)*1; + + float TEC_intensity_max=0.5; + float TEC_power_calc = *TEC_Power; + float K=1.0/10.0; // 100% power per 20 grad. + float Power = K*temp_diff; + if(Power > 1.0) Power = 1.0; + if(Power < -1.0) Power = -1.0; + + TEC_power_calc = Power*TEC_intensity_max; + if(fabsf(fabsf(TEC_power_calc) - fabsf(*TEC_Power)) > 0.010)//(isless(TEC_power_calc,*TEC_Power) || isgreater(TEC_power_calc,*TEC_Power)) + { + *TEC_Power = TEC_power_calc; + TEC_set_level(*TEC_Power); + } +} + +/*void TEC_power_set(bool value){ + TEC_power = value; +}*/ + + diff --git a/TE_Controller/src/source/tcc_user.c b/TE_Controller/src/source/tcc_user.c new file mode 100644 index 0000000..dcff3eb --- /dev/null +++ b/TE_Controller/src/source/tcc_user.c @@ -0,0 +1,78 @@ +/* + * tcc.c + * + * Created: 18.01.2021 22:11:12 + * Author: Lexus + */ +#include "tcc_user.h" +#include "stdbool.h" +#include + +struct tcc_module tcc_instance; +struct tc_module tc_instance; +bool tc_callback_flag=false; + + +void configure_tcc(void) +{ + + struct tcc_config config_tcc; + tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); + config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1; + config_tcc.counter.period = PWM_GCLK_PERIOD; + + config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; + + tcc_init(&tcc_instance, CONF_PWM_MODULE, &config_tcc); + tcc_enable(&tcc_instance); +} + +void print_tcc_status(void){ + uint32_t tcc_status, tcc_counter; + uint16_t ch; + + tcc_status = tcc_get_status(&tcc_instance); + tcc_counter = tcc_get_count_value(&tcc_instance); + printf("TCC: CNT: %6ld, STATUS: %10ld, SYNC_READY: %d, CAPTURE_OVERFLOW: %d, COUNTER_EVENT: %d, COUNTER_RETRIGGERED: %d, COUNT_OVERFLOW: %d, RAMP_CYCLE_INDEX: %d, STOPPED: %d\r\n", tcc_counter, tcc_status, + (int)((tcc_status & TCC_STATUS_SYNC_READY)?1:0), + (int)((tcc_status & TCC_STATUS_CAPTURE_OVERFLOW)?1:0), + (int)((tcc_status & TCC_STATUS_COUNTER_EVENT)?1:0), + (int)((tcc_status & TCC_STATUS_COUNTER_RETRIGGERED)?1:0), + (int)((tcc_status & TCC_STATUS_COUNT_OVERFLOW)?1:0), + (int)((tcc_status & TCC_STATUS_RAMP_CYCLE_INDEX)?1:0), + (int)((tcc_status & TCC_STATUS_STOPPED)?1:0) + ); + for(ch=0;ch<4;ch++){ + printf("channel: %d, CHANNEL_MATCH_CAPTURE: %d, CHANNEL_OUTPUT: %d\r\n", ch, + (int)((tcc_status & TCC_STATUS_CHANNEL_MATCH_CAPTURE(ch))?1:0), + (int)((tcc_status & TCC_STATUS_CHANNEL_OUTPUT(ch))?1:0) + ); + } +} + +void configure_tc(void) +{ + struct tc_config config_tc; + tc_get_config_defaults(&config_tc); + config_tc.counter_size = TC_COUNTER_SIZE_16BIT; + config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV1024; + config_tc.counter_16_bit.compare_capture_channel[0] = 4687; // 1000ms + tc_init(&tc_instance, TC3, &config_tc); + tc_enable(&tc_instance); +} + +void tc_callback(struct tc_module *const module_inst){ + tc_callback_flag=true; + tc_set_count_value(&tc_instance, 0); +} + +void configure_tc_callbacks(void) +{ + tc_register_callback( + &tc_instance, + tc_callback, + TC_CALLBACK_CC_CHANNEL0); + tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0); +} + + diff --git a/TE_Controller/src/source/tec.c b/TE_Controller/src/source/tec.c index 2c2b5e2..8e85b42 100644 --- a/TE_Controller/src/source/tec.c +++ b/TE_Controller/src/source/tec.c @@ -65,31 +65,34 @@ void pin_set_output(uint8_t pin, bool output_flag, uint8_t value){ port_pin_set_config(pin, &config_port_pin); } -// Range from -1 to 0 -void TEC_set_level(float value){ +// Range from -1 to 1 +void TEC_set_level(float value) +{ tcc_reset(&tcc_instance); - if(fpclassify(value) == FP_ZERO){//if(value == (float)0.0){// - // TEC OFF + if(fpclassify(value) == FP_ZERO || fpclassify(value) == FP_NAN) + { pin_set_output(PIN_SPGATE, true, 1); pin_set_output(PIN_SNGATE, true, 0); pin_set_output(PIN_LPGATE, true, 1); pin_set_output(PIN_LNGATE, true, 0); - }else{ + } + else + { struct port_config config_port_pin; port_get_config_defaults(&config_port_pin); - config_port_pin.direction = PORT_PIN_DIR_INPUT; - config_port_pin.input_pull = PORT_PIN_PULL_NONE; + config_port_pin.direction = PORT_PIN_DIR_INPUT; + config_port_pin.input_pull = PORT_PIN_PULL_NONE; port_pin_set_config(PIN_SNGATE, &config_port_pin); struct tcc_config config_tcc; tcc_get_config_defaults(&config_tcc, CONF_PWM_MODULE); - config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1; - config_tcc.counter.period = PWM_GCLK_PERIOD; - - config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; + config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1; + config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; + config_tcc.counter.period = PWM_GCLK_PERIOD; uint8_t pin_output, chan; - if(value > 0.0){ + if(value > 0.0) + { TEC_L_set(true); // to +12 pin_set_output(PIN_SPGATE, true, 1); pin_set_output(PIN_SNGATE, false, 0); @@ -99,7 +102,9 @@ void TEC_set_level(float value){ config_tcc.pins.wave_out_pin_mux[pin_output] = MUX_PA17F_TCC0_WO7; config_tcc.compare.match[chan] = PWM_GCLK_PERIOD*value; - }else{ + } + else + { TEC_L_set(false); // to GND pin_set_output(PIN_SPGATE, false, 0); pin_set_output(PIN_SNGATE, true, 0);