Mouse code for the MacroRat
mbed-dev/targets/TARGET_ARM_SSG/TARGET_BEETLE/us_ticker.c@46:b156ef445742, 2017-06-03 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2015 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 <stddef.h> |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "us_ticker_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "PeripheralNames.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | /* Private data */ |
sahilmgandhi | 18:6a4db94011d3 | 21 | /* us_ticker reload value */ |
sahilmgandhi | 18:6a4db94011d3 | 22 | static uint32_t us_ticker_reload = 0x0; /* Max Value */ |
sahilmgandhi | 18:6a4db94011d3 | 23 | /* us ticker initialized */ |
sahilmgandhi | 18:6a4db94011d3 | 24 | static uint32_t us_ticker_inited = 0; |
sahilmgandhi | 18:6a4db94011d3 | 25 | /* us ticker overflow */ |
sahilmgandhi | 18:6a4db94011d3 | 26 | static uint32_t us_ticker_overflow_delta = 0; |
sahilmgandhi | 18:6a4db94011d3 | 27 | /* us ticker overflow limit */ |
sahilmgandhi | 18:6a4db94011d3 | 28 | static uint32_t us_ticker_overflow_limit = 0; |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | void __us_ticker_irq_handler(void) { |
sahilmgandhi | 18:6a4db94011d3 | 31 | Timer_ClearInterrupt(TIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 32 | /* |
sahilmgandhi | 18:6a4db94011d3 | 33 | * For each overflow event adds the timer max represented value to |
sahilmgandhi | 18:6a4db94011d3 | 34 | * the delta. This allows the us_ticker to keep track of the elapsed |
sahilmgandhi | 18:6a4db94011d3 | 35 | * time: |
sahilmgandhi | 18:6a4db94011d3 | 36 | * elapsed_time = (num_overflow * overflow_limit) + current_time |
sahilmgandhi | 18:6a4db94011d3 | 37 | */ |
sahilmgandhi | 18:6a4db94011d3 | 38 | us_ticker_overflow_delta += us_ticker_overflow_limit; |
sahilmgandhi | 18:6a4db94011d3 | 39 | } |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | void us_ticker_init(void) { |
sahilmgandhi | 18:6a4db94011d3 | 42 | uint32_t us_ticker_irqn0 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 43 | uint32_t us_ticker_irqn1 = 0; |
sahilmgandhi | 18:6a4db94011d3 | 44 | |
sahilmgandhi | 18:6a4db94011d3 | 45 | if (us_ticker_inited) |
sahilmgandhi | 18:6a4db94011d3 | 46 | return; |
sahilmgandhi | 18:6a4db94011d3 | 47 | us_ticker_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /* Initialize Timer 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 50 | Timer_Initialize(TIMER0, us_ticker_reload); |
sahilmgandhi | 18:6a4db94011d3 | 51 | /* Enable Timer 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 52 | Timer_Enable(TIMER0); |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | /* Initialize Timer 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 55 | Timer_Initialize(TIMER1, us_ticker_reload); |
sahilmgandhi | 18:6a4db94011d3 | 56 | /* Enable Timer 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | Timer_Enable(TIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 58 | |
sahilmgandhi | 18:6a4db94011d3 | 59 | /* Timer 0 get IRQn */ |
sahilmgandhi | 18:6a4db94011d3 | 60 | us_ticker_irqn0 = Timer_GetIRQn(TIMER0); |
sahilmgandhi | 18:6a4db94011d3 | 61 | NVIC_SetVector((IRQn_Type)us_ticker_irqn0, (uint32_t)us_ticker_irq_handler); |
sahilmgandhi | 18:6a4db94011d3 | 62 | NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn0); |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | /* Timer 1 get IRQn */ |
sahilmgandhi | 18:6a4db94011d3 | 65 | us_ticker_irqn1 = Timer_GetIRQn(TIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 66 | NVIC_SetVector((IRQn_Type)us_ticker_irqn1, (uint32_t)__us_ticker_irq_handler); |
sahilmgandhi | 18:6a4db94011d3 | 67 | NVIC_EnableIRQ((IRQn_Type)us_ticker_irqn1); |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | /* Timer set interrupt on TIMER1 */ |
sahilmgandhi | 18:6a4db94011d3 | 70 | Timer_SetInterrupt(TIMER1, TIMER_DEFAULT_RELOAD); |
sahilmgandhi | 18:6a4db94011d3 | 71 | |
sahilmgandhi | 18:6a4db94011d3 | 72 | /* |
sahilmgandhi | 18:6a4db94011d3 | 73 | * Set us_ticker Overflow limit. The us_ticker overflow limit is required |
sahilmgandhi | 18:6a4db94011d3 | 74 | * to calculated the return value of the us_ticker read function in us |
sahilmgandhi | 18:6a4db94011d3 | 75 | * on 32bit. |
sahilmgandhi | 18:6a4db94011d3 | 76 | * A 32bit us value cannot be represented directly in the Timer Load |
sahilmgandhi | 18:6a4db94011d3 | 77 | * register if it is greater than (0xFFFFFFFF ticks)/TIMER_DIVIDER_US. |
sahilmgandhi | 18:6a4db94011d3 | 78 | */ |
sahilmgandhi | 18:6a4db94011d3 | 79 | us_ticker_overflow_limit = Timer_GetReloadValue(TIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 80 | } |
sahilmgandhi | 18:6a4db94011d3 | 81 | |
sahilmgandhi | 18:6a4db94011d3 | 82 | uint32_t us_ticker_read() { |
sahilmgandhi | 18:6a4db94011d3 | 83 | uint32_t return_value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 84 | |
sahilmgandhi | 18:6a4db94011d3 | 85 | if (!us_ticker_inited) |
sahilmgandhi | 18:6a4db94011d3 | 86 | us_ticker_init(); |
sahilmgandhi | 18:6a4db94011d3 | 87 | |
sahilmgandhi | 18:6a4db94011d3 | 88 | return_value = us_ticker_overflow_delta + Timer_Read(TIMER1); |
sahilmgandhi | 18:6a4db94011d3 | 89 | |
sahilmgandhi | 18:6a4db94011d3 | 90 | return return_value; |
sahilmgandhi | 18:6a4db94011d3 | 91 | } |
sahilmgandhi | 18:6a4db94011d3 | 92 | |
sahilmgandhi | 18:6a4db94011d3 | 93 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
sahilmgandhi | 18:6a4db94011d3 | 94 | int32_t delta = 0; |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | if (!us_ticker_inited) |
sahilmgandhi | 18:6a4db94011d3 | 97 | us_ticker_init(); |
sahilmgandhi | 18:6a4db94011d3 | 98 | delta = (int32_t)(timestamp - us_ticker_read()); |
sahilmgandhi | 18:6a4db94011d3 | 99 | /* Check if the event was in the past */ |
sahilmgandhi | 18:6a4db94011d3 | 100 | if (delta <= 0) { |
sahilmgandhi | 18:6a4db94011d3 | 101 | /* This event was in the past */ |
sahilmgandhi | 18:6a4db94011d3 | 102 | Timer_SetInterrupt(TIMER0, 0); |
sahilmgandhi | 18:6a4db94011d3 | 103 | return; |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | /* If the event was not in the past enable interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 107 | Timer_SetInterrupt(TIMER0, delta); |
sahilmgandhi | 18:6a4db94011d3 | 108 | } |
sahilmgandhi | 18:6a4db94011d3 | 109 | |
sahilmgandhi | 18:6a4db94011d3 | 110 | void us_ticker_disable_interrupt(void) { |
sahilmgandhi | 18:6a4db94011d3 | 111 | Timer_DisableInterrupt(TIMER0); |
sahilmgandhi | 18:6a4db94011d3 | 112 | } |
sahilmgandhi | 18:6a4db94011d3 | 113 | |
sahilmgandhi | 18:6a4db94011d3 | 114 | void us_ticker_clear_interrupt(void) { |
sahilmgandhi | 18:6a4db94011d3 | 115 | Timer_ClearInterrupt(TIMER0); |
sahilmgandhi | 18:6a4db94011d3 | 116 | } |