-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreertos_tickless.c
218 lines (181 loc) · 7.21 KB
/
freertos_tickless.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************
*/
/* MXC */
#include "mxc_device.h"
#include "board.h"
#include "mxc_assert.h"
/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "task.h"
/* Maxim CMSIS */
#include "lp.h"
#include "pwrseq_regs.h"
#include "wut.h"
#include "mcr_regs.h"
#include "icc.h"
#include "pb.h"
#include "led.h"
#include "uart.h"
/* Bluetooth Cordio library */
#include "pal_timer.h"
#include "pal_uart.h"
#include "pal_bb.h"
#define WUT_RATIO (configRTC_TICK_RATE_HZ / configTICK_RATE_HZ)
#define MAX_WUT_SNOOZE (5*configRTC_TICK_RATE_HZ)
#define MIN_SYSTICK 2
#define MIN_WUT_TICKS 150
#define WAKEUP_US 1000
/*
* Sleep-check function
*
* Your code should over-ride this weak function and return E_NO_ERROR if
* tickless sleep is permissible (ie. no UART/SPI/I2C activity). Any other
* return code will prevent FreeRTOS from entering tickless idle.
*/
int freertos_permit_tickless(void)
{
/* Can not disable BLE DBB and 32 MHz clock while trim procedure is ongoing */
if(MXC_WUT_TrimPending() != E_NO_ERROR) {
return E_BUSY;
}
/* Figure out if the UART is active */
if(PalUartGetState(PAL_UART_ID_TERMINAL) == PAL_UART_STATE_BUSY) {
return E_BUSY;
}
/* Figure out if the scheduler timer is active */
if(PalTimerGetState() != PAL_TIMER_STATE_BUSY) {
return E_BUSY;
}
/* Prevent characters from being corrupted if still transmitting,
UART will shutdown in deep sleep */
if(MXC_UART_GetActive(MXC_UART_GET_UART(CONSOLE_UART)) != E_NO_ERROR) {
/* We will not get another UART interrupt, sleep for a short amount of time
before determining if we can enter standby */
return E_BUSY;
}
return E_NO_ERROR;
}
/*
* This function overrides vPortSuppressTicksAndSleep in portable/.../ARM_CM4F/port.c
*
* DEEPSLEEP mode will stop SysTick from counting, so that can't be
* used to wake up. Instead, calculate a wake-up period for the WUT to
* interrupt the WFI and continue execution.
*
*/
void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
{
uint32_t idleTicks, actualTicks, preCapture, postCapture;
uint32_t schUsec, schUsecElapsed, bleSleepTicks;
/* We do not currently handle to case where the WUT is slower than the RTOS tick */
MXC_ASSERT(configRTC_TICK_RATE_HZ >= configTICK_RATE_HZ);
if (SysTick->VAL < MIN_SYSTICK) {
/* Avoid sleeping too close to a systick interrupt */
return;
}
/* Calculate the number of WUT ticks, but we need one to synchronize */
idleTicks = (xExpectedIdleTime - 1) * WUT_RATIO;
if(idleTicks > MAX_WUT_SNOOZE) {
idleTicks = MAX_WUT_SNOOZE;
}
/* Check to see if we meet the minimum requirements for deep sleep */
if (idleTicks < MIN_WUT_TICKS) {
return;
}
/* Enter a critical section but don't use the taskENTER_CRITICAL()
method as that will mask interrupts that should exit sleep mode. */
__asm volatile( "cpsid i" );
/* If a context switch is pending or a task is waiting for the scheduler
to be unsuspended then abandon the low power entry. */
/* Also check the MXC drivers for any in-progress activity */
if ((eTaskConfirmSleepModeStatus() == eAbortSleep) ||
(freertos_permit_tickless() != E_NO_ERROR)) {
/* Re-enable interrupts - see comments above the cpsid instruction()
above. */
__asm volatile( "cpsie i" );
return;
}
/* Disable SysTick */
SysTick->CTRL &= ~(SysTick_CTRL_ENABLE_Msk);
/* Snapshot the current WUT value */
MXC_WUT_Store();
preCapture = MXC_WUT_GetCount();
schUsec = PalTimerGetExpTime();
/* Regular sleep if we don't have time for deep sleep */
if (schUsec < (MIN_WUT_TICKS * 1000000 / configRTC_TICK_RATE_HZ)) {
MXC_LP_EnterSleepMode();
} else {
/* Adjust idleTicks for the time it takes to restart the BLE hardware */
idleTicks -= (uint64_t)(WAKEUP_US) *
(uint64_t)configRTC_TICK_RATE_HZ / (uint64_t)1000000;
/* Calculate the time to the next BLE scheduler event */
bleSleepTicks = (uint64_t)(schUsec - WAKEUP_US) *
(uint64_t)configRTC_TICK_RATE_HZ / (uint64_t)1000000;
if(bleSleepTicks < idleTicks) {
MXC_WUT->cmp = preCapture + bleSleepTicks;
} else {
MXC_WUT->cmp = preCapture + idleTicks;
}
/* Stop the scheduler timer */
PalTimerStop();
/* Shutdown BB */
PalBbDisable();
LED_Off(1);
MXC_LP_EnterSleepMode();
LED_On(1);
/* Restore the BB hardware */
PalBbEnable();
PalBbRestore();
/* Restore the BB counter */
MXC_WUT_RestoreBBClock(BB_CLK_RATE_HZ);
/* Update the scheduler timer */
actualTicks = MXC_WUT->cnt - preCapture;
schUsecElapsed = (uint64_t)actualTicks * (uint64_t)1000000 / (uint64_t)configRTC_TICK_RATE_HZ;
PalTimerRestore(schUsec - schUsecElapsed);
}
/* Recalculate actualTicks for the FreeRTOS tick counter update */
postCapture = MXC_WUT_GetCount();
actualTicks = postCapture - preCapture;
/* Re-enable interrupts - see comments above the cpsid instruction()
above. */
__asm volatile( "cpsie i" );
/*
* Advance ticks by # actually elapsed
*/
portENTER_CRITICAL();
vTaskStepTick( (actualTicks / WUT_RATIO) );
portEXIT_CRITICAL();
/* Re-enable SysTick */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}