xref: /btstack/port/archive/msp430f5229lp-cc2564b/src/hal_tick.c (revision 1664436fd643daf66517dc309e6cc72448e8a86d)
1*1664436fSMatthias Ringwald /*
2*1664436fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*1664436fSMatthias Ringwald  *
4*1664436fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*1664436fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*1664436fSMatthias Ringwald  * are met:
7*1664436fSMatthias Ringwald  *
8*1664436fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*1664436fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*1664436fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*1664436fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*1664436fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*1664436fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*1664436fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*1664436fSMatthias Ringwald  *    from this software without specific prior written permission.
16*1664436fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*1664436fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*1664436fSMatthias Ringwald  *    monetary gain.
19*1664436fSMatthias Ringwald  *
20*1664436fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*1664436fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*1664436fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*1664436fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*1664436fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*1664436fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*1664436fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*1664436fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*1664436fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*1664436fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*1664436fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*1664436fSMatthias Ringwald  * SUCH DAMAGE.
32*1664436fSMatthias Ringwald  *
33*1664436fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*1664436fSMatthias Ringwald  * [email protected]
35*1664436fSMatthias Ringwald  *
36*1664436fSMatthias Ringwald  */
37*1664436fSMatthias Ringwald 
38*1664436fSMatthias Ringwald 
39*1664436fSMatthias Ringwald /*
40*1664436fSMatthias Ringwald  *  hal_tick.c
41*1664436fSMatthias Ringwald  *
42*1664436fSMatthias Ringwald  *  Implementation for MSP430 Experimenter board using 250 ms ticks provided by Timer A1
43*1664436fSMatthias Ringwald  *
44*1664436fSMatthias Ringwald  */
45*1664436fSMatthias Ringwald 
46*1664436fSMatthias Ringwald #include <msp430.h>
47*1664436fSMatthias Ringwald #include <stdlib.h>
48*1664436fSMatthias Ringwald #include "hal_compat.h"
49*1664436fSMatthias Ringwald 
50*1664436fSMatthias Ringwald #include "hal_tick.h"
51*1664436fSMatthias Ringwald 
52*1664436fSMatthias Ringwald static void dummy_handler(void){};
53*1664436fSMatthias Ringwald 
54*1664436fSMatthias Ringwald static void (*tick_handler)(void) = &dummy_handler;
55*1664436fSMatthias Ringwald 
56*1664436fSMatthias Ringwald // Auxillary Clock (ACLK) = 32768 hz
57*1664436fSMatthias Ringwald // 8192 ticks = 1/4 second
58*1664436fSMatthias Ringwald 
59*1664436fSMatthias Ringwald #define TIMER_COUNTDOWN 8192
60*1664436fSMatthias Ringwald 
61*1664436fSMatthias Ringwald void hal_tick_init(void){
62*1664436fSMatthias Ringwald     TA1CCTL0 = CCIE;                   // CCR0 interrupt enabled
63*1664436fSMatthias Ringwald     TA1CTL = TASSEL_1 | MC_2 | TACLR;  // use ACLK (32768), contmode, clear TAR
64*1664436fSMatthias Ringwald     TA1CCR0 = TIMER_COUNTDOWN;    // -> 1/4 s
65*1664436fSMatthias Ringwald }
66*1664436fSMatthias Ringwald 
67*1664436fSMatthias Ringwald void hal_tick_set_handler(void (*handler)(void)){
68*1664436fSMatthias Ringwald     if (handler == NULL){
69*1664436fSMatthias Ringwald         tick_handler = &dummy_handler;
70*1664436fSMatthias Ringwald         return;
71*1664436fSMatthias Ringwald     }
72*1664436fSMatthias Ringwald     tick_handler = handler;
73*1664436fSMatthias Ringwald }
74*1664436fSMatthias Ringwald 
75*1664436fSMatthias Ringwald int  hal_tick_get_tick_period_in_ms(void){
76*1664436fSMatthias Ringwald     return 250;
77*1664436fSMatthias Ringwald }
78*1664436fSMatthias Ringwald 
79*1664436fSMatthias Ringwald // Timer A1 interrupt service routine
80*1664436fSMatthias Ringwald #ifdef __GNUC__
81*1664436fSMatthias Ringwald __attribute__((interrupt(TIMER1_A0_VECTOR)))
82*1664436fSMatthias Ringwald #endif
83*1664436fSMatthias Ringwald #ifdef __IAR_SYSTEMS_ICC__
84*1664436fSMatthias Ringwald #pragma vector=TIMER1_A0_VECTOR
85*1664436fSMatthias Ringwald __interrupt
86*1664436fSMatthias Ringwald #endif
87*1664436fSMatthias Ringwald void timerA0ISR(void){
88*1664436fSMatthias Ringwald     TA1CCR0 += TIMER_COUNTDOWN;
89*1664436fSMatthias Ringwald     (*tick_handler)();
90*1664436fSMatthias Ringwald 
91*1664436fSMatthias Ringwald     // force exit low power mode
92*1664436fSMatthias Ringwald     __bic_SR_register_on_exit(LPM0_bits);   // Exit active CPU
93*1664436fSMatthias Ringwald }
94