Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
sahilmgandhi 18:6a4db94011d3 2 *
sahilmgandhi 18:6a4db94011d3 3 * The information contained herein is property of Nordic Semiconductor ASA.
sahilmgandhi 18:6a4db94011d3 4 * Terms and conditions of usage are described in detail in NORDIC
sahilmgandhi 18:6a4db94011d3 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
sahilmgandhi 18:6a4db94011d3 6 *
sahilmgandhi 18:6a4db94011d3 7 * Licensees are granted free, non-transferable use of the information. NO
sahilmgandhi 18:6a4db94011d3 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
sahilmgandhi 18:6a4db94011d3 9 * the file.
sahilmgandhi 18:6a4db94011d3 10 *
sahilmgandhi 18:6a4db94011d3 11 */
sahilmgandhi 18:6a4db94011d3 12
sahilmgandhi 18:6a4db94011d3 13 #ifndef TWI_MASTER_H
sahilmgandhi 18:6a4db94011d3 14 #define TWI_MASTER_H
sahilmgandhi 18:6a4db94011d3 15
sahilmgandhi 18:6a4db94011d3 16
sahilmgandhi 18:6a4db94011d3 17 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 18 extern "C" {
sahilmgandhi 18:6a4db94011d3 19 #endif
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 /*lint ++flb "Enter library region" */
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23 #include <stdbool.h>
sahilmgandhi 18:6a4db94011d3 24 #include <stdint.h>
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 #include "nrf51.h"
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 /** @file
sahilmgandhi 18:6a4db94011d3 29 * @brief Software controlled TWI Master driver.
sahilmgandhi 18:6a4db94011d3 30 *
sahilmgandhi 18:6a4db94011d3 31 *
sahilmgandhi 18:6a4db94011d3 32 * @defgroup lib_driver_twi_master Software controlled TWI Master driver
sahilmgandhi 18:6a4db94011d3 33 * @{
sahilmgandhi 18:6a4db94011d3 34 * @ingroup nrf_drivers
sahilmgandhi 18:6a4db94011d3 35 * @brief Software controlled TWI Master driver.
sahilmgandhi 18:6a4db94011d3 36 *
sahilmgandhi 18:6a4db94011d3 37 * Supported features:
sahilmgandhi 18:6a4db94011d3 38 * - Repeated start
sahilmgandhi 18:6a4db94011d3 39 * - No multi-master
sahilmgandhi 18:6a4db94011d3 40 * - Only 7-bit addressing
sahilmgandhi 18:6a4db94011d3 41 * - Supports clock stretching (with optional SMBus style slave timeout)
sahilmgandhi 18:6a4db94011d3 42 * - Tries to handle slaves stuck in the middle of transfer
sahilmgandhi 18:6a4db94011d3 43 */
sahilmgandhi 18:6a4db94011d3 44
sahilmgandhi 18:6a4db94011d3 45 #define TWI_READ_BIT (0x01) //!< If this bit is set in the address field, transfer direction is from slave to master.
sahilmgandhi 18:6a4db94011d3 46
sahilmgandhi 18:6a4db94011d3 47 #define TWI_ISSUE_STOP ((bool)true) //!< Parameter for @ref twi_master_transfer
sahilmgandhi 18:6a4db94011d3 48 #define TWI_DONT_ISSUE_STOP ((bool)false) //!< Parameter for @ref twi_master_transfer
sahilmgandhi 18:6a4db94011d3 49
sahilmgandhi 18:6a4db94011d3 50 /* These macros are needed to see if the slave is stuck and we as master send dummy clock cycles to end its wait */
sahilmgandhi 18:6a4db94011d3 51 /*lint -e717 -save "Suppress do {} while (0) for these macros" */
sahilmgandhi 18:6a4db94011d3 52 /*lint ++flb "Enter library region" */
sahilmgandhi 18:6a4db94011d3 53 #define TWI_SCL_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line high */
sahilmgandhi 18:6a4db94011d3 54 #define TWI_SCL_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Pulls SCL line low */
sahilmgandhi 18:6a4db94011d3 55 #define TWI_SDA_HIGH() do { NRF_GPIO->OUTSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line high */
sahilmgandhi 18:6a4db94011d3 56 #define TWI_SDA_LOW() do { NRF_GPIO->OUTCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Pulls SDA line low */
sahilmgandhi 18:6a4db94011d3 57 #define TWI_SDA_INPUT() do { NRF_GPIO->DIRCLR = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as input */
sahilmgandhi 18:6a4db94011d3 58 #define TWI_SDA_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_DATA_PIN_NUMBER); } while(0) /*!< Configures SDA pin as output */
sahilmgandhi 18:6a4db94011d3 59 #define TWI_SCL_OUTPUT() do { NRF_GPIO->DIRSET = (1UL << TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER); } while(0) /*!< Configures SCL pin as output */
sahilmgandhi 18:6a4db94011d3 60 /*lint -restore */
sahilmgandhi 18:6a4db94011d3 61
sahilmgandhi 18:6a4db94011d3 62 #define TWI_SDA_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_DATA_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SDA */
sahilmgandhi 18:6a4db94011d3 63 #define TWI_SCL_READ() ((NRF_GPIO->IN >> TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER) & 0x1UL) /*!< Reads current state of SCL */
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 #define TWI_DELAY() nrf_delay_us(4) /*!< Time to wait when pin states are changed. For fast-mode the delay can be zero and for standard-mode 4 us delay is sufficient. */
sahilmgandhi 18:6a4db94011d3 66
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 /**
sahilmgandhi 18:6a4db94011d3 69 * @brief Function for initializing TWI bus IO pins and checks if the bus is operational.
sahilmgandhi 18:6a4db94011d3 70 *
sahilmgandhi 18:6a4db94011d3 71 * Both pins are configured as Standard-0, No-drive-1 (open drain).
sahilmgandhi 18:6a4db94011d3 72 *
sahilmgandhi 18:6a4db94011d3 73 * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1
sahilmgandhi 18:6a4db94011d3 74 * @return
sahilmgandhi 18:6a4db94011d3 75 * @retval true TWI bus is clear for transfers.
sahilmgandhi 18:6a4db94011d3 76 * @retval false TWI bus is stuck.
sahilmgandhi 18:6a4db94011d3 77 */
sahilmgandhi 18:6a4db94011d3 78 bool twi_master_init_and_clear(NRF_TWI_Type* twi);
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 /**
sahilmgandhi 18:6a4db94011d3 81 * @brief Function for transferring data over TWI bus.
sahilmgandhi 18:6a4db94011d3 82 *
sahilmgandhi 18:6a4db94011d3 83 * If TWI master detects even one NACK from the slave or timeout occurs, STOP condition is issued
sahilmgandhi 18:6a4db94011d3 84 * and the function returns false.
sahilmgandhi 18:6a4db94011d3 85 * Bit 0 (@ref TWI_READ_BIT) in the address parameter controls transfer direction;
sahilmgandhi 18:6a4db94011d3 86 * - If 1, master reads data_length number of bytes from the slave
sahilmgandhi 18:6a4db94011d3 87 * - If 0, master writes data_length number of bytes to the slave.
sahilmgandhi 18:6a4db94011d3 88 *
sahilmgandhi 18:6a4db94011d3 89 * @note Make sure at least data_length number of bytes is allocated in data if TWI_READ_BIT is set.
sahilmgandhi 18:6a4db94011d3 90 * @note @ref TWI_ISSUE_STOP
sahilmgandhi 18:6a4db94011d3 91 *
sahilmgandhi 18:6a4db94011d3 92 * @param address Data transfer direction (LSB) / Slave address (7 MSBs).
sahilmgandhi 18:6a4db94011d3 93 * @param data Pointer to data.
sahilmgandhi 18:6a4db94011d3 94 * @param data_length Number of bytes to transfer.
sahilmgandhi 18:6a4db94011d3 95 * @param issue_stop_condition If @ref TWI_ISSUE_STOP, STOP condition is issued before exiting function. If @ref TWI_DONT_ISSUE_STOP, STOP condition is not issued before exiting function. If transfer failed for any reason, STOP condition will be issued in any case.
sahilmgandhi 18:6a4db94011d3 96 * @param twi The TWI interface to use - either NRF_TWI0 or NRF_TWI1
sahilmgandhi 18:6a4db94011d3 97 * @return
sahilmgandhi 18:6a4db94011d3 98 * @retval true Data transfer succeeded without errors.
sahilmgandhi 18:6a4db94011d3 99 * @retval false Data transfer failed.
sahilmgandhi 18:6a4db94011d3 100 */
sahilmgandhi 18:6a4db94011d3 101 bool twi_master_transfer(uint8_t address, uint8_t *data, uint8_t data_length, bool issue_stop_condition, NRF_TWI_Type* twi);
sahilmgandhi 18:6a4db94011d3 102
sahilmgandhi 18:6a4db94011d3 103 /**
sahilmgandhi 18:6a4db94011d3 104 *@}
sahilmgandhi 18:6a4db94011d3 105 **/
sahilmgandhi 18:6a4db94011d3 106
sahilmgandhi 18:6a4db94011d3 107 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 108 }
sahilmgandhi 18:6a4db94011d3 109 #endif
sahilmgandhi 18:6a4db94011d3 110
sahilmgandhi 18:6a4db94011d3 111 /*lint --flb "Leave library region" */
sahilmgandhi 18:6a4db94011d3 112 #endif //TWI_MASTER_H