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 * Copyright (c) 2012 Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 3 * All rights reserved.
sahilmgandhi 18:6a4db94011d3 4 *
sahilmgandhi 18:6a4db94011d3 5 * Redistribution and use in source and binary forms, with or without modification,
sahilmgandhi 18:6a4db94011d3 6 * are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * 1. Redistributions of source code must retain the above copyright notice, this list
sahilmgandhi 18:6a4db94011d3 9 * of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 10 *
sahilmgandhi 18:6a4db94011d3 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
sahilmgandhi 18:6a4db94011d3 12 * integrated circuit in a product or a software update for such product, must reproduce
sahilmgandhi 18:6a4db94011d3 13 * the above copyright notice, this list of conditions and the following disclaimer in
sahilmgandhi 18:6a4db94011d3 14 * the documentation and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 15 *
sahilmgandhi 18:6a4db94011d3 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
sahilmgandhi 18:6a4db94011d3 17 * used to endorse or promote products derived from this software without specific prior
sahilmgandhi 18:6a4db94011d3 18 * written permission.
sahilmgandhi 18:6a4db94011d3 19 *
sahilmgandhi 18:6a4db94011d3 20 * 4. This software, with or without modification, must only be used with a
sahilmgandhi 18:6a4db94011d3 21 * Nordic Semiconductor ASA integrated circuit.
sahilmgandhi 18:6a4db94011d3 22 *
sahilmgandhi 18:6a4db94011d3 23 * 5. Any software provided in binary or object form under this license must not be reverse
sahilmgandhi 18:6a4db94011d3 24 * engineered, decompiled, modified and/or disassembled.
sahilmgandhi 18:6a4db94011d3 25 *
sahilmgandhi 18:6a4db94011d3 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
sahilmgandhi 18:6a4db94011d3 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sahilmgandhi 18:6a4db94011d3 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
sahilmgandhi 18:6a4db94011d3 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sahilmgandhi 18:6a4db94011d3 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sahilmgandhi 18:6a4db94011d3 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
sahilmgandhi 18:6a4db94011d3 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
sahilmgandhi 18:6a4db94011d3 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sahilmgandhi 18:6a4db94011d3 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 36 *
sahilmgandhi 18:6a4db94011d3 37 */
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 /** @file
sahilmgandhi 18:6a4db94011d3 41 *
sahilmgandhi 18:6a4db94011d3 42 * @defgroup ble_flash_module Flash Manager
sahilmgandhi 18:6a4db94011d3 43 * @{
sahilmgandhi 18:6a4db94011d3 44 * @ingroup ble_sdk_lib
sahilmgandhi 18:6a4db94011d3 45 * @brief Module for accessing flash memory.
sahilmgandhi 18:6a4db94011d3 46 *
sahilmgandhi 18:6a4db94011d3 47 * @details It contains functions for reading, writing and erasing one page in flash.
sahilmgandhi 18:6a4db94011d3 48 *
sahilmgandhi 18:6a4db94011d3 49 * The module uses the first 32 bits of the flash page to write a magic number in order to
sahilmgandhi 18:6a4db94011d3 50 * determine if the page has been written or not.
sahilmgandhi 18:6a4db94011d3 51 *
sahilmgandhi 18:6a4db94011d3 52 * @note Be careful not to use a page number in the SoftDevice area (which currently occupies the
sahilmgandhi 18:6a4db94011d3 53 * range 0 to 127), or in your application space! In both cases, this would end up
sahilmgandhi 18:6a4db94011d3 54 * with a hard fault.
sahilmgandhi 18:6a4db94011d3 55 */
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 #ifndef BLE_FLASH_H__
sahilmgandhi 18:6a4db94011d3 58 #define BLE_FLASH_H__
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 #include <stdint.h>
sahilmgandhi 18:6a4db94011d3 61 #include <stdbool.h>
sahilmgandhi 18:6a4db94011d3 62 #include "nrf.h"
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 #define BLE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE) /**< Size of one flash page. */
sahilmgandhi 18:6a4db94011d3 65 #define BLE_FLASH_MAGIC_NUMBER 0x45DE0000 /**< Magic value to identify if flash contains valid data. */
sahilmgandhi 18:6a4db94011d3 66 #define BLE_FLASH_EMPTY_MASK 0xFFFFFFFF /**< Bit mask that defines an empty address in flash. */
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 /**@brief Macro for getting the end of the flash available for application.
sahilmgandhi 18:6a4db94011d3 70 *
sahilmgandhi 18:6a4db94011d3 71 * @details The result flash page number indicates the end boundary of the flash available
sahilmgandhi 18:6a4db94011d3 72 * to the application. If a bootloader is used, the end will be the start of the
sahilmgandhi 18:6a4db94011d3 73 * bootloader region. Otherwise, the end will be the size of the flash.
sahilmgandhi 18:6a4db94011d3 74 */
sahilmgandhi 18:6a4db94011d3 75 #define BLE_FLASH_PAGE_END \
sahilmgandhi 18:6a4db94011d3 76 ((NRF_UICR->NRFFW[0] != BLE_FLASH_EMPTY_MASK) \
sahilmgandhi 18:6a4db94011d3 77 ? (NRF_UICR->NRFFW[0] / BLE_FLASH_PAGE_SIZE) \
sahilmgandhi 18:6a4db94011d3 78 : NRF_FICR->CODESIZE)
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 /**@brief Function for erasing the specified flash page, and then writes the given data to this page.
sahilmgandhi 18:6a4db94011d3 81 *
sahilmgandhi 18:6a4db94011d3 82 * @warning This operation blocks the CPU. DO NOT use while in a connection!
sahilmgandhi 18:6a4db94011d3 83 *
sahilmgandhi 18:6a4db94011d3 84 * @param[in] page_num Page number to update.
sahilmgandhi 18:6a4db94011d3 85 * @param[in] p_in_array Pointer to a RAM area containing the elements to write in flash.
sahilmgandhi 18:6a4db94011d3 86 * This area has to be 32 bits aligned.
sahilmgandhi 18:6a4db94011d3 87 * @param[in] word_count Number of 32 bits words to write in flash.
sahilmgandhi 18:6a4db94011d3 88 *
sahilmgandhi 18:6a4db94011d3 89 * @return NRF_SUCCESS on successful flash write, otherwise an error code.
sahilmgandhi 18:6a4db94011d3 90 */
sahilmgandhi 18:6a4db94011d3 91 uint32_t ble_flash_page_write(uint8_t page_num, uint32_t * p_in_array, uint8_t word_count);
sahilmgandhi 18:6a4db94011d3 92
sahilmgandhi 18:6a4db94011d3 93 /**@brief Function for reading data from flash to RAM.
sahilmgandhi 18:6a4db94011d3 94 *
sahilmgandhi 18:6a4db94011d3 95 * @param[in] page_num Page number to read.
sahilmgandhi 18:6a4db94011d3 96 * @param[out] p_out_array Pointer to a RAM area where the found data will be written.
sahilmgandhi 18:6a4db94011d3 97 * This area has to be 32 bits aligned.
sahilmgandhi 18:6a4db94011d3 98 * @param[out] p_word_count Number of 32 bits words read.
sahilmgandhi 18:6a4db94011d3 99 *
sahilmgandhi 18:6a4db94011d3 100 * @return NRF_SUCCESS on successful upload, NRF_ERROR_NOT_FOUND if no valid data has been found
sahilmgandhi 18:6a4db94011d3 101 * in flash (first 32 bits not equal to the MAGIC_NUMBER+CRC).
sahilmgandhi 18:6a4db94011d3 102 */
sahilmgandhi 18:6a4db94011d3 103 uint32_t ble_flash_page_read(uint8_t page_num, uint32_t * p_out_array, uint8_t * p_word_count);
sahilmgandhi 18:6a4db94011d3 104
sahilmgandhi 18:6a4db94011d3 105 /**@brief Function for erasing a flash page.
sahilmgandhi 18:6a4db94011d3 106 *
sahilmgandhi 18:6a4db94011d3 107 * @note This operation blocks the CPU, so it should not be done while the radio is running!
sahilmgandhi 18:6a4db94011d3 108 *
sahilmgandhi 18:6a4db94011d3 109 * @param[in] page_num Page number to erase.
sahilmgandhi 18:6a4db94011d3 110 *
sahilmgandhi 18:6a4db94011d3 111 * @return NRF_SUCCESS on success, an error_code otherwise.
sahilmgandhi 18:6a4db94011d3 112 */
sahilmgandhi 18:6a4db94011d3 113 uint32_t ble_flash_page_erase(uint8_t page_num);
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 /**@brief Function for writing one word to flash.
sahilmgandhi 18:6a4db94011d3 116 *
sahilmgandhi 18:6a4db94011d3 117 * @note Flash location to be written must have been erased previously.
sahilmgandhi 18:6a4db94011d3 118 *
sahilmgandhi 18:6a4db94011d3 119 * @param[in] p_address Pointer to flash location to be written.
sahilmgandhi 18:6a4db94011d3 120 * @param[in] value Value to write to flash.
sahilmgandhi 18:6a4db94011d3 121 *
sahilmgandhi 18:6a4db94011d3 122 * @return NRF_SUCCESS.
sahilmgandhi 18:6a4db94011d3 123 */
sahilmgandhi 18:6a4db94011d3 124 uint32_t ble_flash_word_write(uint32_t * p_address, uint32_t value);
sahilmgandhi 18:6a4db94011d3 125
sahilmgandhi 18:6a4db94011d3 126 /**@brief Function for writing a data block to flash.
sahilmgandhi 18:6a4db94011d3 127 *
sahilmgandhi 18:6a4db94011d3 128 * @note Flash locations to be written must have been erased previously.
sahilmgandhi 18:6a4db94011d3 129 *
sahilmgandhi 18:6a4db94011d3 130 * @param[in] p_address Pointer to start of flash location to be written.
sahilmgandhi 18:6a4db94011d3 131 * @param[in] p_in_array Pointer to start of flash block to be written.
sahilmgandhi 18:6a4db94011d3 132 * @param[in] word_count Number of words to be written.
sahilmgandhi 18:6a4db94011d3 133 *
sahilmgandhi 18:6a4db94011d3 134 * @return NRF_SUCCESS.
sahilmgandhi 18:6a4db94011d3 135 */
sahilmgandhi 18:6a4db94011d3 136 uint32_t ble_flash_block_write(uint32_t * p_address, uint32_t * p_in_array, uint16_t word_count);
sahilmgandhi 18:6a4db94011d3 137
sahilmgandhi 18:6a4db94011d3 138 /**@brief Function for computing pointer to start of specified flash page.
sahilmgandhi 18:6a4db94011d3 139 *
sahilmgandhi 18:6a4db94011d3 140 * @param[in] page_num Page number.
sahilmgandhi 18:6a4db94011d3 141 * @param[out] pp_page_addr Pointer to start of flash page.
sahilmgandhi 18:6a4db94011d3 142 *
sahilmgandhi 18:6a4db94011d3 143 * @return NRF_SUCCESS.
sahilmgandhi 18:6a4db94011d3 144 */
sahilmgandhi 18:6a4db94011d3 145 uint32_t ble_flash_page_addr(uint8_t page_num, uint32_t ** pp_page_addr);
sahilmgandhi 18:6a4db94011d3 146
sahilmgandhi 18:6a4db94011d3 147 /**@brief Function for calculating a 16 bit CRC using the CRC-16-CCITT scheme.
sahilmgandhi 18:6a4db94011d3 148 *
sahilmgandhi 18:6a4db94011d3 149 * @param[in] p_data Pointer to data on which the CRC is to be calulated.
sahilmgandhi 18:6a4db94011d3 150 * @param[in] size Number of bytes on which the CRC is to be calulated.
sahilmgandhi 18:6a4db94011d3 151 * @param[in] p_crc Initial CRC value (if NULL, a preset value is used as the initial value).
sahilmgandhi 18:6a4db94011d3 152 *
sahilmgandhi 18:6a4db94011d3 153 * @return Calculated CRC.
sahilmgandhi 18:6a4db94011d3 154 */
sahilmgandhi 18:6a4db94011d3 155 uint16_t ble_flash_crc16_compute(uint8_t * p_data, uint16_t size, uint16_t * p_crc);
sahilmgandhi 18:6a4db94011d3 156
sahilmgandhi 18:6a4db94011d3 157 /**@brief Function for handling flashing module Radio Notification event.
sahilmgandhi 18:6a4db94011d3 158 *
sahilmgandhi 18:6a4db94011d3 159 * @note For flash writing to work safely while in a connection or while advertising, this function
sahilmgandhi 18:6a4db94011d3 160 * MUST be called from the Radio Notification module's event handler (see
sahilmgandhi 18:6a4db94011d3 161 * @ref ble_radio_notification for details).
sahilmgandhi 18:6a4db94011d3 162 *
sahilmgandhi 18:6a4db94011d3 163 * @param[in] radio_active TRUE if radio is active (or about to become active), FALSE otherwise.
sahilmgandhi 18:6a4db94011d3 164 */
sahilmgandhi 18:6a4db94011d3 165 void ble_flash_on_radio_active_evt(bool radio_active);
sahilmgandhi 18:6a4db94011d3 166
sahilmgandhi 18:6a4db94011d3 167 #endif // BLE_FLASH_H__
sahilmgandhi 18:6a4db94011d3 168
sahilmgandhi 18:6a4db94011d3 169 /** @} */