Mouse code for the MacroRat
mbed-dev/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/services/delay/sam0/systick_counter.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 | #include "delay.h" |
sahilmgandhi | 18:6a4db94011d3 | 2 | |
sahilmgandhi | 18:6a4db94011d3 | 3 | /** |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Value used to calculate ms delay. Default to be used with a 8MHz clock; |
sahilmgandhi | 18:6a4db94011d3 | 5 | */ |
sahilmgandhi | 18:6a4db94011d3 | 6 | static uint32_t cycles_per_ms = 8000000UL / 1000; |
sahilmgandhi | 18:6a4db94011d3 | 7 | static uint32_t cycles_per_us = 8000000UL / 1000000; |
sahilmgandhi | 18:6a4db94011d3 | 8 | |
sahilmgandhi | 18:6a4db94011d3 | 9 | /** |
sahilmgandhi | 18:6a4db94011d3 | 10 | * \brief Initialize the delay driver. |
sahilmgandhi | 18:6a4db94011d3 | 11 | * |
sahilmgandhi | 18:6a4db94011d3 | 12 | * This must be called during start up to initialize the delay routine with |
sahilmgandhi | 18:6a4db94011d3 | 13 | * the current used main clock. It must run any time the main CPU clock is changed. |
sahilmgandhi | 18:6a4db94011d3 | 14 | */ |
sahilmgandhi | 18:6a4db94011d3 | 15 | void delay_init(void) |
sahilmgandhi | 18:6a4db94011d3 | 16 | { |
sahilmgandhi | 18:6a4db94011d3 | 17 | cycles_per_ms = system_gclk_gen_get_hz(0); |
sahilmgandhi | 18:6a4db94011d3 | 18 | cycles_per_ms /= 1000; |
sahilmgandhi | 18:6a4db94011d3 | 19 | cycles_per_us = cycles_per_ms / 1000; |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 22 | } |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | /** |
sahilmgandhi | 18:6a4db94011d3 | 25 | * \brief Delay loop to delay at least n number of microseconds |
sahilmgandhi | 18:6a4db94011d3 | 26 | * |
sahilmgandhi | 18:6a4db94011d3 | 27 | * \param n Number of microseconds to wait |
sahilmgandhi | 18:6a4db94011d3 | 28 | */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | void delay_cycles_us( |
sahilmgandhi | 18:6a4db94011d3 | 30 | uint32_t n) |
sahilmgandhi | 18:6a4db94011d3 | 31 | { |
sahilmgandhi | 18:6a4db94011d3 | 32 | while (n--) { |
sahilmgandhi | 18:6a4db94011d3 | 33 | /* Devide up to blocks of 10u */ |
sahilmgandhi | 18:6a4db94011d3 | 34 | delay_cycles(cycles_per_us); |
sahilmgandhi | 18:6a4db94011d3 | 35 | } |
sahilmgandhi | 18:6a4db94011d3 | 36 | } |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | /** |
sahilmgandhi | 18:6a4db94011d3 | 39 | * \brief Delay loop to delay at least n number of milliseconds |
sahilmgandhi | 18:6a4db94011d3 | 40 | * |
sahilmgandhi | 18:6a4db94011d3 | 41 | * \param n Number of milliseconds to wait |
sahilmgandhi | 18:6a4db94011d3 | 42 | */ |
sahilmgandhi | 18:6a4db94011d3 | 43 | void delay_cycles_ms( |
sahilmgandhi | 18:6a4db94011d3 | 44 | uint32_t n) |
sahilmgandhi | 18:6a4db94011d3 | 45 | { |
sahilmgandhi | 18:6a4db94011d3 | 46 | while (n--) { |
sahilmgandhi | 18:6a4db94011d3 | 47 | /* Devide up to blocks of 1ms */ |
sahilmgandhi | 18:6a4db94011d3 | 48 | delay_cycles(cycles_per_ms); |
sahilmgandhi | 18:6a4db94011d3 | 49 | } |
sahilmgandhi | 18:6a4db94011d3 | 50 | } |