Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 * \file
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * \brief Sleep manager
sahilmgandhi 18:6a4db94011d3 5 *
sahilmgandhi 18:6a4db94011d3 6 * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * \asf_license_start
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * \page License
sahilmgandhi 18:6a4db94011d3 11 *
sahilmgandhi 18:6a4db94011d3 12 * Redistribution and use in source and binary forms, with or without
sahilmgandhi 18:6a4db94011d3 13 * modification, are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 14 *
sahilmgandhi 18:6a4db94011d3 15 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 16 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 17 *
sahilmgandhi 18:6a4db94011d3 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 19 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 20 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 21 *
sahilmgandhi 18:6a4db94011d3 22 * 3. The name of Atmel may not be used to endorse or promote products derived
sahilmgandhi 18:6a4db94011d3 23 * from this software without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 24 *
sahilmgandhi 18:6a4db94011d3 25 * 4. This software may only be redistributed and used in connection with an
sahilmgandhi 18:6a4db94011d3 26 * Atmel microcontroller product.
sahilmgandhi 18:6a4db94011d3 27 *
sahilmgandhi 18:6a4db94011d3 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
sahilmgandhi 18:6a4db94011d3 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
sahilmgandhi 18:6a4db94011d3 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
sahilmgandhi 18:6a4db94011d3 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sahilmgandhi 18:6a4db94011d3 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sahilmgandhi 18:6a4db94011d3 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sahilmgandhi 18:6a4db94011d3 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
sahilmgandhi 18:6a4db94011d3 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sahilmgandhi 18:6a4db94011d3 38 * POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 39 *
sahilmgandhi 18:6a4db94011d3 40 * \asf_license_stop
sahilmgandhi 18:6a4db94011d3 41 *
sahilmgandhi 18:6a4db94011d3 42 */
sahilmgandhi 18:6a4db94011d3 43 /*
sahilmgandhi 18:6a4db94011d3 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
sahilmgandhi 18:6a4db94011d3 45 */
sahilmgandhi 18:6a4db94011d3 46 #ifndef SLEEPMGR_H
sahilmgandhi 18:6a4db94011d3 47 #define SLEEPMGR_H
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 #include <compiler.h>
sahilmgandhi 18:6a4db94011d3 50 #include <parts.h>
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 #if (SAM3S || SAM3U || SAM3N || SAM3XA || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM || SAMV71 || SAMV70 || SAMS70 || SAME70)
sahilmgandhi 18:6a4db94011d3 53 # include "sam/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 54 #elif XMEGA
sahilmgandhi 18:6a4db94011d3 55 # include "xmega/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 56 #elif UC3
sahilmgandhi 18:6a4db94011d3 57 # include "uc3/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 58 #elif SAM4L
sahilmgandhi 18:6a4db94011d3 59 # include "sam4l/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 60 #elif MEGA
sahilmgandhi 18:6a4db94011d3 61 # include "mega/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 62 #elif (SAMD20 || SAMD21 || SAMR21 || SAMD11 || SAMDA1)
sahilmgandhi 18:6a4db94011d3 63 # include "samd/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 64 #elif (SAML21 || SAML22)
sahilmgandhi 18:6a4db94011d3 65 # include "saml/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 66 #elif (SAMC21)
sahilmgandhi 18:6a4db94011d3 67 # include "samc/sleepmgr.h"
sahilmgandhi 18:6a4db94011d3 68 #else
sahilmgandhi 18:6a4db94011d3 69 # error Unsupported device.
sahilmgandhi 18:6a4db94011d3 70 #endif
sahilmgandhi 18:6a4db94011d3 71
sahilmgandhi 18:6a4db94011d3 72 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 73 extern "C" {
sahilmgandhi 18:6a4db94011d3 74 #endif
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 /**
sahilmgandhi 18:6a4db94011d3 77 * \defgroup sleepmgr_group Sleep manager
sahilmgandhi 18:6a4db94011d3 78 *
sahilmgandhi 18:6a4db94011d3 79 * The sleep manager is a service for ensuring that the device is not put to
sahilmgandhi 18:6a4db94011d3 80 * sleep in deeper sleep modes than the system (e.g., peripheral drivers,
sahilmgandhi 18:6a4db94011d3 81 * services or the application) allows at any given time.
sahilmgandhi 18:6a4db94011d3 82 *
sahilmgandhi 18:6a4db94011d3 83 * It is based on the use of lock counting for the individual sleep modes, and
sahilmgandhi 18:6a4db94011d3 84 * will put the device to sleep in the shallowest sleep mode that has a non-zero
sahilmgandhi 18:6a4db94011d3 85 * lock count. The drivers/services/application can change these counts by use
sahilmgandhi 18:6a4db94011d3 86 * of \ref sleepmgr_lock_mode and \ref sleepmgr_unlock_mode.
sahilmgandhi 18:6a4db94011d3 87 * Refer to \ref sleepmgr_mode for a list of the sleep modes available for
sahilmgandhi 18:6a4db94011d3 88 * locking, and the device datasheet for information on their effect.
sahilmgandhi 18:6a4db94011d3 89 *
sahilmgandhi 18:6a4db94011d3 90 * The application must supply the file \ref conf_sleepmgr.h.
sahilmgandhi 18:6a4db94011d3 91 *
sahilmgandhi 18:6a4db94011d3 92 * For the sleep manager to be enabled, the symbol \ref CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 93 * must be defined, e.g., in \ref conf_sleepmgr.h. If this symbol is not
sahilmgandhi 18:6a4db94011d3 94 * defined, the functions are replaced with dummy functions and no RAM is used.
sahilmgandhi 18:6a4db94011d3 95 *
sahilmgandhi 18:6a4db94011d3 96 * @{
sahilmgandhi 18:6a4db94011d3 97 */
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 /**
sahilmgandhi 18:6a4db94011d3 100 * \def CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 101 * \brief Configuration symbol for enabling the sleep manager
sahilmgandhi 18:6a4db94011d3 102 *
sahilmgandhi 18:6a4db94011d3 103 * If this symbol is not defined, the functions of this service are replaced
sahilmgandhi 18:6a4db94011d3 104 * with dummy functions. This is useful for reducing code size and execution
sahilmgandhi 18:6a4db94011d3 105 * time if the sleep manager is not needed in the application.
sahilmgandhi 18:6a4db94011d3 106 *
sahilmgandhi 18:6a4db94011d3 107 * This symbol may be defined in \ref conf_sleepmgr.h.
sahilmgandhi 18:6a4db94011d3 108 */
sahilmgandhi 18:6a4db94011d3 109 #if defined(__DOXYGEN__) && !defined(CONFIG_SLEEPMGR_ENABLE)
sahilmgandhi 18:6a4db94011d3 110 # define CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 111 #endif
sahilmgandhi 18:6a4db94011d3 112
sahilmgandhi 18:6a4db94011d3 113 /**
sahilmgandhi 18:6a4db94011d3 114 * \enum sleepmgr_mode
sahilmgandhi 18:6a4db94011d3 115 * \brief Sleep mode locks
sahilmgandhi 18:6a4db94011d3 116 *
sahilmgandhi 18:6a4db94011d3 117 * Identifiers for the different sleep mode locks.
sahilmgandhi 18:6a4db94011d3 118 */
sahilmgandhi 18:6a4db94011d3 119
sahilmgandhi 18:6a4db94011d3 120 /**
sahilmgandhi 18:6a4db94011d3 121 * \brief Initialize the lock counts
sahilmgandhi 18:6a4db94011d3 122 *
sahilmgandhi 18:6a4db94011d3 123 * Sets all lock counts to 0, except the very last one, which is set to 1. This
sahilmgandhi 18:6a4db94011d3 124 * is done to simplify the algorithm for finding the deepest allowable sleep
sahilmgandhi 18:6a4db94011d3 125 * mode in \ref sleepmgr_enter_sleep.
sahilmgandhi 18:6a4db94011d3 126 */
sahilmgandhi 18:6a4db94011d3 127 static inline void sleepmgr_init(void)
sahilmgandhi 18:6a4db94011d3 128 {
sahilmgandhi 18:6a4db94011d3 129 #ifdef CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 130 uint8_t i;
sahilmgandhi 18:6a4db94011d3 131
sahilmgandhi 18:6a4db94011d3 132 for (i = 0; i < SLEEPMGR_NR_OF_MODES - 1; i++) {
sahilmgandhi 18:6a4db94011d3 133 sleepmgr_locks[i] = 0;
sahilmgandhi 18:6a4db94011d3 134 }
sahilmgandhi 18:6a4db94011d3 135 sleepmgr_locks[SLEEPMGR_NR_OF_MODES - 1] = 1;
sahilmgandhi 18:6a4db94011d3 136 #endif /* CONFIG_SLEEPMGR_ENABLE */
sahilmgandhi 18:6a4db94011d3 137 }
sahilmgandhi 18:6a4db94011d3 138
sahilmgandhi 18:6a4db94011d3 139 /**
sahilmgandhi 18:6a4db94011d3 140 * \brief Increase lock count for a sleep mode
sahilmgandhi 18:6a4db94011d3 141 *
sahilmgandhi 18:6a4db94011d3 142 * Increases the lock count for \a mode to ensure that the sleep manager does
sahilmgandhi 18:6a4db94011d3 143 * not put the device to sleep in the deeper sleep modes.
sahilmgandhi 18:6a4db94011d3 144 *
sahilmgandhi 18:6a4db94011d3 145 * \param mode Sleep mode to lock.
sahilmgandhi 18:6a4db94011d3 146 */
sahilmgandhi 18:6a4db94011d3 147 static inline void sleepmgr_lock_mode(enum sleepmgr_mode mode)
sahilmgandhi 18:6a4db94011d3 148 {
sahilmgandhi 18:6a4db94011d3 149 #ifdef CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 150 irqflags_t flags;
sahilmgandhi 18:6a4db94011d3 151
sahilmgandhi 18:6a4db94011d3 152 if(sleepmgr_locks[mode] >= 0xff) {
sahilmgandhi 18:6a4db94011d3 153 while (true) {
sahilmgandhi 18:6a4db94011d3 154 // Warning: maximum value of sleepmgr_locks buffer is no more than 255.
sahilmgandhi 18:6a4db94011d3 155 // Check APP or change the data type to uint16_t.
sahilmgandhi 18:6a4db94011d3 156 }
sahilmgandhi 18:6a4db94011d3 157 }
sahilmgandhi 18:6a4db94011d3 158
sahilmgandhi 18:6a4db94011d3 159 // Enter a critical section
sahilmgandhi 18:6a4db94011d3 160 flags = cpu_irq_save();
sahilmgandhi 18:6a4db94011d3 161
sahilmgandhi 18:6a4db94011d3 162 ++sleepmgr_locks[mode];
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 // Leave the critical section
sahilmgandhi 18:6a4db94011d3 165 cpu_irq_restore(flags);
sahilmgandhi 18:6a4db94011d3 166 #else
sahilmgandhi 18:6a4db94011d3 167 UNUSED(mode);
sahilmgandhi 18:6a4db94011d3 168 #endif /* CONFIG_SLEEPMGR_ENABLE */
sahilmgandhi 18:6a4db94011d3 169 }
sahilmgandhi 18:6a4db94011d3 170
sahilmgandhi 18:6a4db94011d3 171 /**
sahilmgandhi 18:6a4db94011d3 172 * \brief Decrease lock count for a sleep mode
sahilmgandhi 18:6a4db94011d3 173 *
sahilmgandhi 18:6a4db94011d3 174 * Decreases the lock count for \a mode. If the lock count reaches 0, the sleep
sahilmgandhi 18:6a4db94011d3 175 * manager can put the device to sleep in the deeper sleep modes.
sahilmgandhi 18:6a4db94011d3 176 *
sahilmgandhi 18:6a4db94011d3 177 * \param mode Sleep mode to unlock.
sahilmgandhi 18:6a4db94011d3 178 */
sahilmgandhi 18:6a4db94011d3 179 static inline void sleepmgr_unlock_mode(enum sleepmgr_mode mode)
sahilmgandhi 18:6a4db94011d3 180 {
sahilmgandhi 18:6a4db94011d3 181 #ifdef CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 182 irqflags_t flags;
sahilmgandhi 18:6a4db94011d3 183
sahilmgandhi 18:6a4db94011d3 184 if(sleepmgr_locks[mode] == 0) {
sahilmgandhi 18:6a4db94011d3 185 while (true) {
sahilmgandhi 18:6a4db94011d3 186 // Warning: minimum value of sleepmgr_locks buffer is no less than 0.
sahilmgandhi 18:6a4db94011d3 187 // Check APP.
sahilmgandhi 18:6a4db94011d3 188 }
sahilmgandhi 18:6a4db94011d3 189 }
sahilmgandhi 18:6a4db94011d3 190
sahilmgandhi 18:6a4db94011d3 191 // Enter a critical section
sahilmgandhi 18:6a4db94011d3 192 flags = cpu_irq_save();
sahilmgandhi 18:6a4db94011d3 193
sahilmgandhi 18:6a4db94011d3 194 --sleepmgr_locks[mode];
sahilmgandhi 18:6a4db94011d3 195
sahilmgandhi 18:6a4db94011d3 196 // Leave the critical section
sahilmgandhi 18:6a4db94011d3 197 cpu_irq_restore(flags);
sahilmgandhi 18:6a4db94011d3 198 #else
sahilmgandhi 18:6a4db94011d3 199 UNUSED(mode);
sahilmgandhi 18:6a4db94011d3 200 #endif /* CONFIG_SLEEPMGR_ENABLE */
sahilmgandhi 18:6a4db94011d3 201 }
sahilmgandhi 18:6a4db94011d3 202
sahilmgandhi 18:6a4db94011d3 203 /**
sahilmgandhi 18:6a4db94011d3 204 * \brief Retrieves the deepest allowable sleep mode
sahilmgandhi 18:6a4db94011d3 205 *
sahilmgandhi 18:6a4db94011d3 206 * Searches through the sleep mode lock counts, starting at the shallowest sleep
sahilmgandhi 18:6a4db94011d3 207 * mode, until the first non-zero lock count is found. The deepest allowable
sahilmgandhi 18:6a4db94011d3 208 * sleep mode is then returned.
sahilmgandhi 18:6a4db94011d3 209 */
sahilmgandhi 18:6a4db94011d3 210 static inline enum sleepmgr_mode sleepmgr_get_sleep_mode(void)
sahilmgandhi 18:6a4db94011d3 211 {
sahilmgandhi 18:6a4db94011d3 212 enum sleepmgr_mode sleep_mode = SLEEPMGR_ACTIVE;
sahilmgandhi 18:6a4db94011d3 213
sahilmgandhi 18:6a4db94011d3 214 #ifdef CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 215 uint8_t *lock_ptr = sleepmgr_locks;
sahilmgandhi 18:6a4db94011d3 216
sahilmgandhi 18:6a4db94011d3 217 // Find first non-zero lock count, starting with the shallowest modes.
sahilmgandhi 18:6a4db94011d3 218 while (!(*lock_ptr)) {
sahilmgandhi 18:6a4db94011d3 219 lock_ptr++;
sahilmgandhi 18:6a4db94011d3 220 sleep_mode = (enum sleepmgr_mode)(sleep_mode + 1);
sahilmgandhi 18:6a4db94011d3 221 }
sahilmgandhi 18:6a4db94011d3 222
sahilmgandhi 18:6a4db94011d3 223 // Catch the case where one too many sleepmgr_unlock_mode() call has been
sahilmgandhi 18:6a4db94011d3 224 // performed on the deepest sleep mode.
sahilmgandhi 18:6a4db94011d3 225 Assert((uintptr_t)(lock_ptr - sleepmgr_locks) < SLEEPMGR_NR_OF_MODES);
sahilmgandhi 18:6a4db94011d3 226
sahilmgandhi 18:6a4db94011d3 227 #endif /* CONFIG_SLEEPMGR_ENABLE */
sahilmgandhi 18:6a4db94011d3 228
sahilmgandhi 18:6a4db94011d3 229 return sleep_mode;
sahilmgandhi 18:6a4db94011d3 230 }
sahilmgandhi 18:6a4db94011d3 231
sahilmgandhi 18:6a4db94011d3 232 /**
sahilmgandhi 18:6a4db94011d3 233 * \fn sleepmgr_enter_sleep
sahilmgandhi 18:6a4db94011d3 234 * \brief Go to sleep in the deepest allowed mode
sahilmgandhi 18:6a4db94011d3 235 *
sahilmgandhi 18:6a4db94011d3 236 * Searches through the sleep mode lock counts, starting at the shallowest sleep
sahilmgandhi 18:6a4db94011d3 237 * mode, until the first non-zero lock count is found. The device is then put to
sahilmgandhi 18:6a4db94011d3 238 * sleep in the sleep mode that corresponds to the lock.
sahilmgandhi 18:6a4db94011d3 239 *
sahilmgandhi 18:6a4db94011d3 240 * \note This function enables interrupts before going to sleep, and will leave
sahilmgandhi 18:6a4db94011d3 241 * them enabled upon return. This also applies if sleep is skipped due to ACTIVE
sahilmgandhi 18:6a4db94011d3 242 * mode being locked.
sahilmgandhi 18:6a4db94011d3 243 */
sahilmgandhi 18:6a4db94011d3 244
sahilmgandhi 18:6a4db94011d3 245 static inline void sleepmgr_enter_sleep(void)
sahilmgandhi 18:6a4db94011d3 246 {
sahilmgandhi 18:6a4db94011d3 247 #ifdef CONFIG_SLEEPMGR_ENABLE
sahilmgandhi 18:6a4db94011d3 248 enum sleepmgr_mode sleep_mode;
sahilmgandhi 18:6a4db94011d3 249
sahilmgandhi 18:6a4db94011d3 250 cpu_irq_disable();
sahilmgandhi 18:6a4db94011d3 251
sahilmgandhi 18:6a4db94011d3 252 // Find the deepest allowable sleep mode
sahilmgandhi 18:6a4db94011d3 253 sleep_mode = sleepmgr_get_sleep_mode();
sahilmgandhi 18:6a4db94011d3 254 // Return right away if first mode (ACTIVE) is locked.
sahilmgandhi 18:6a4db94011d3 255 if (sleep_mode==SLEEPMGR_ACTIVE) {
sahilmgandhi 18:6a4db94011d3 256 cpu_irq_enable();
sahilmgandhi 18:6a4db94011d3 257 return;
sahilmgandhi 18:6a4db94011d3 258 }
sahilmgandhi 18:6a4db94011d3 259 // Enter the deepest allowable sleep mode with interrupts enabled
sahilmgandhi 18:6a4db94011d3 260 sleepmgr_sleep(sleep_mode);
sahilmgandhi 18:6a4db94011d3 261 #else
sahilmgandhi 18:6a4db94011d3 262 cpu_irq_enable();
sahilmgandhi 18:6a4db94011d3 263 #endif /* CONFIG_SLEEPMGR_ENABLE */
sahilmgandhi 18:6a4db94011d3 264 }
sahilmgandhi 18:6a4db94011d3 265
sahilmgandhi 18:6a4db94011d3 266
sahilmgandhi 18:6a4db94011d3 267 //! @}
sahilmgandhi 18:6a4db94011d3 268
sahilmgandhi 18:6a4db94011d3 269 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 270 }
sahilmgandhi 18:6a4db94011d3 271 #endif
sahilmgandhi 18:6a4db94011d3 272
sahilmgandhi 18:6a4db94011d3 273 #endif /* SLEEPMGR_H */