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 /* mbed Microcontroller Library
sahilmgandhi 18:6a4db94011d3 2 * Copyright (c) 2006-2013 ARM Limited
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
sahilmgandhi 18:6a4db94011d3 5 * you may not use this file except in compliance with the License.
sahilmgandhi 18:6a4db94011d3 6 * You may obtain a copy of the License at
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * http://www.apache.org/licenses/LICENSE-2.0
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * Unless required by applicable law or agreed to in writing, software
sahilmgandhi 18:6a4db94011d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
sahilmgandhi 18:6a4db94011d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sahilmgandhi 18:6a4db94011d3 13 * See the License for the specific language governing permissions and
sahilmgandhi 18:6a4db94011d3 14 * limitations under the License.
sahilmgandhi 18:6a4db94011d3 15 */
sahilmgandhi 18:6a4db94011d3 16 #include "rtc_api.h"
sahilmgandhi 18:6a4db94011d3 17
sahilmgandhi 18:6a4db94011d3 18
sahilmgandhi 18:6a4db94011d3 19 #define LFCLK_FREQUENCY (32768UL)
sahilmgandhi 18:6a4db94011d3 20 #define RTC0_COUNTER_PRESCALER ((LFCLK_FREQUENCY/8) - 1)
sahilmgandhi 18:6a4db94011d3 21 #define COMPARE_COUNTERTIME (691200UL) //86400 x 8
sahilmgandhi 18:6a4db94011d3 22
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 time_t initTime;
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 void rtc_init(void) {
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 NVIC_EnableIRQ(RTC0_IRQn); // Enable Interrupt for the RTC in the core.
sahilmgandhi 18:6a4db94011d3 29 //NRF_RTC0->TASKS_STOP =1;
sahilmgandhi 18:6a4db94011d3 30 NRF_RTC0->PRESCALER = RTC0_COUNTER_PRESCALER; // Set prescaler to a TICK of RTC_FREQUENCY.
sahilmgandhi 18:6a4db94011d3 31 NRF_RTC0->CC[0] = COMPARE_COUNTERTIME; // Compare0 after approx COMPARE_COUNTERTIME seconds.
sahilmgandhi 18:6a4db94011d3 32
sahilmgandhi 18:6a4db94011d3 33 // Enable COMPARE0 event and COMPARE0 interrupt:
sahilmgandhi 18:6a4db94011d3 34 NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
sahilmgandhi 18:6a4db94011d3 35 NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
sahilmgandhi 18:6a4db94011d3 36 NRF_RTC0->TASKS_START = 1;
sahilmgandhi 18:6a4db94011d3 37 }
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 void rtc_free(void) {
sahilmgandhi 18:6a4db94011d3 40 // [TODO]
sahilmgandhi 18:6a4db94011d3 41 }
sahilmgandhi 18:6a4db94011d3 42
sahilmgandhi 18:6a4db94011d3 43 /*
sahilmgandhi 18:6a4db94011d3 44 * Little check routine to see if the RTC has been enabled
sahilmgandhi 18:6a4db94011d3 45 *
sahilmgandhi 18:6a4db94011d3 46 * Clock Control Register
sahilmgandhi 18:6a4db94011d3 47 * RTC_CCR[0] : 0 = Disabled, 1 = Enabled
sahilmgandhi 18:6a4db94011d3 48 *
sahilmgandhi 18:6a4db94011d3 49 */
sahilmgandhi 18:6a4db94011d3 50 int rtc_isenabled(void) {
sahilmgandhi 18:6a4db94011d3 51 // [TODO] return(((NRF_RTC0->TASKS_START) & 0x01) != 0);
sahilmgandhi 18:6a4db94011d3 52 }
sahilmgandhi 18:6a4db94011d3 53
sahilmgandhi 18:6a4db94011d3 54 time_t rtc_read(void) {
sahilmgandhi 18:6a4db94011d3 55
sahilmgandhi 18:6a4db94011d3 56 time_t t = initTime;
sahilmgandhi 18:6a4db94011d3 57 t += (86400*NRF_RTC0->EVENTS_COMPARE[0]);
sahilmgandhi 18:6a4db94011d3 58 t += (int)((NRF_RTC0->COUNTER)/8);
sahilmgandhi 18:6a4db94011d3 59 return(t);
sahilmgandhi 18:6a4db94011d3 60 }
sahilmgandhi 18:6a4db94011d3 61
sahilmgandhi 18:6a4db94011d3 62 void rtc_write(time_t t) {
sahilmgandhi 18:6a4db94011d3 63 // Convert the time in to a tm
sahilmgandhi 18:6a4db94011d3 64
sahilmgandhi 18:6a4db94011d3 65 // Pause clock, and clear counter register (clears us count)
sahilmgandhi 18:6a4db94011d3 66 NRF_RTC0->TASKS_STOP = 1;
sahilmgandhi 18:6a4db94011d3 67
sahilmgandhi 18:6a4db94011d3 68 initTime = t;
sahilmgandhi 18:6a4db94011d3 69 // Restart clock
sahilmgandhi 18:6a4db94011d3 70 NRF_RTC0->TASKS_START = 1;
sahilmgandhi 18:6a4db94011d3 71 }