1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "ble_config.h"
#include "pan_clktrim.h"
#include "pan_power.h"
#if BLE_EN
#include "pan_ble.h"
#endif
#if CONFIG_RTT_LOG_ENABLE
#include "SEGGER_RTT.h"
#endif
#include "app_log.h"
#include "soc_api.h"
#include "utility.h"
 
/*******************************************************************************
 * Macro
 ******************************************************************************/
 
 
/*******************************************************************************
 * Variable Define & Declaration
 ******************************************************************************/
 
 
/*******************************************************************************
 * Function Declaration
 ******************************************************************************/
 
 
/*******************************************************************************
 * Function Define
 ******************************************************************************/
#if CONFIG_OS_EN
 
#if CONFIG_AUTO_OPTIMIZE_POWER_PARAM
#define TEMP_CHECK_INTERVAL_MS      (CONFIG_TEMP_SAMPLE_INTERVAL_S*1000)
 
static TimerHandle_t temp_check_timer;
static ADC_HandleTypeDef adc_temperature = {
    .id = ADC_CH_TEMP,
};
 
void temp_check_cb(TimerHandle_t xtimer)
{
    float temp;
 
    //printf("[W] temp_check_cb\r\n");
 
    if (HAL_ADC_GetValue(&adc_temperature, &temp) == HAL_SUCCESS) {
        PW_AutoOptimizeParams((int)temp);
 
    #if BLE_EN
        /* Update SCA */
        static uint8_t config = 0xFF;
        uint16_t SCA = CONFIG_BT_CTLR_SCA;
        if(temp > 15 && temp < 30){
            if(config == 0)
                return;
            config = 0;
            SCA = CONFIG_BT_CTLR_SCA;
        }
        else{
            if(config == 1)
                return;
            config = 1;
            SCA = (CONFIG_LOW_SPEED_CLOCK_SRC==0 ? 1000:500);
        }
        pan_misc_upd_local_sca(SCA);
 
        //printf("update SCA:%d\n", SCA);
    #endif
    }
}
 
void temp_check_start(uint32_t ms)
{
    (void)ms;
 
    if(temp_check_timer){
        xTimerStart(temp_check_timer, 0);
    }
}
 
void temp_check_stop(void)
{
    if(temp_check_timer){
        xTimerStop(temp_check_timer, 0);
    }
}
 
void temp_check_init(void)
{
    temp_check_timer = xTimerCreate("pwr_param_upd_timer", pdMS_TO_TICKS(TEMP_CHECK_INTERVAL_MS), pdTRUE, NULL, temp_check_cb);
    if(temp_check_timer == NULL){
        printf("[E] %s %d temp check timer create failed\n", __FILE__, __LINE__);
    }
    temp_check_start(0);
}
#endif /* CONFIG_AUTO_OPTIMIZE_POWER_PARAM */
 
 
TaskHandle_t main_task_h;
 
void main_task(void *p)
{
#if CONFIG_AUTO_OPTIMIZE_POWER_PARAM
    if(!PW_ParamExists()) {
        printf("[E] Optimized power configuration is not found!\n");
        while(1);
    }
    HAL_ADC_Init(&adc_temperature);
    temp_check_cb(NULL);
    temp_check_init();
#endif /* CONFIG_AUTO_OPTIMIZE_POWER_PARAM */
 
    extern int $Super$$main(void);
    $Super$$main();
 
    vTaskDelete(NULL);
}
 
__NO_RETURN void os_schedule_main(void)
{
    xTaskCreate(main_task, "main_task", CONFIG_MAIN_TASK_STACK_SIZE, NULL, CONFIG_MAIN_TASK_PRIO, &main_task_h);
 
    /* Start OS */
    vTaskStartScheduler();
 
    printf("[E] This is not where the program should go\r\n");
    while(1);
}
 
#endif /* CONFIG_OS_EN */