11664436fSMatthias Ringwald /*
21664436fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
31664436fSMatthias Ringwald *
41664436fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
51664436fSMatthias Ringwald * modification, are permitted provided that the following conditions
61664436fSMatthias Ringwald * are met:
71664436fSMatthias Ringwald *
81664436fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
91664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
101664436fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
111664436fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
121664436fSMatthias Ringwald * documentation and/or other materials provided with the distribution.
131664436fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
141664436fSMatthias Ringwald * contributors may be used to endorse or promote products derived
151664436fSMatthias Ringwald * from this software without specific prior written permission.
161664436fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
171664436fSMatthias Ringwald * personal benefit and not for any commercial purpose or for
181664436fSMatthias Ringwald * monetary gain.
191664436fSMatthias Ringwald *
201664436fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211664436fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221664436fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251664436fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261664436fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271664436fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281664436fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291664436fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301664436fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311664436fSMatthias Ringwald * SUCH DAMAGE.
321664436fSMatthias Ringwald *
331664436fSMatthias Ringwald * Please inquire about commercial licensing options at
341664436fSMatthias Ringwald * [email protected]
351664436fSMatthias Ringwald *
361664436fSMatthias Ringwald */
371664436fSMatthias Ringwald
381664436fSMatthias Ringwald
391664436fSMatthias Ringwald /*
401664436fSMatthias Ringwald * hal_tick.c
411664436fSMatthias Ringwald *
421664436fSMatthias Ringwald * Implementation for MSP430 Experimenter board using 250 ms ticks provided by Timer A1
431664436fSMatthias Ringwald *
441664436fSMatthias Ringwald */
451664436fSMatthias Ringwald
461664436fSMatthias Ringwald #include <msp430x54x.h>
471664436fSMatthias Ringwald #include <stdlib.h>
481664436fSMatthias Ringwald #include "hal_compat.h"
491664436fSMatthias Ringwald
501664436fSMatthias Ringwald #include "hal_tick.h"
511664436fSMatthias Ringwald
dummy_handler(void)521664436fSMatthias Ringwald static void dummy_handler(void){};
531664436fSMatthias Ringwald
541664436fSMatthias Ringwald static void (*tick_handler)(void) = &dummy_handler;
551664436fSMatthias Ringwald
561664436fSMatthias Ringwald // Auxillary Clock (ACLK) = 32768 hz
571664436fSMatthias Ringwald // 8192 ticks = 1/4 second
581664436fSMatthias Ringwald
591664436fSMatthias Ringwald #define TIMER_COUNTDOWN 8192
601664436fSMatthias Ringwald
hal_tick_init(void)611664436fSMatthias Ringwald void hal_tick_init(void){
621664436fSMatthias Ringwald TA1CCTL0 = CCIE; // CCR0 interrupt enabled
631664436fSMatthias Ringwald TA1CTL = TASSEL_1 | MC_2 | TACLR; // use ACLK (32768), contmode, clear TAR
641664436fSMatthias Ringwald TA1CCR0 = TIMER_COUNTDOWN; // -> 1/4 s
651664436fSMatthias Ringwald }
661664436fSMatthias Ringwald
hal_tick_set_handler(void (* handler)(void))671664436fSMatthias Ringwald void hal_tick_set_handler(void (*handler)(void)){
681664436fSMatthias Ringwald if (handler == NULL){
691664436fSMatthias Ringwald tick_handler = &dummy_handler;
701664436fSMatthias Ringwald return;
711664436fSMatthias Ringwald }
721664436fSMatthias Ringwald tick_handler = handler;
731664436fSMatthias Ringwald }
741664436fSMatthias Ringwald
hal_tick_get_tick_period_in_ms(void)751664436fSMatthias Ringwald int hal_tick_get_tick_period_in_ms(void){
761664436fSMatthias Ringwald return 250;
771664436fSMatthias Ringwald }
781664436fSMatthias Ringwald
791664436fSMatthias Ringwald // Timer A1 interrupt service routine
801664436fSMatthias Ringwald #ifdef __GNUC__
811664436fSMatthias Ringwald __attribute__((interrupt(TIMER1_A0_VECTOR)))
821664436fSMatthias Ringwald #endif
831664436fSMatthias Ringwald #ifdef __IAR_SYSTEMS_ICC__
841664436fSMatthias Ringwald #pragma vector=TIMER1_A0_VECTOR
851664436fSMatthias Ringwald __interrupt
861664436fSMatthias Ringwald #endif
timerA0ISR(void)871664436fSMatthias Ringwald void timerA0ISR(void){
881664436fSMatthias Ringwald TA1CCR0 += TIMER_COUNTDOWN;
891664436fSMatthias Ringwald (*tick_handler)();
901664436fSMatthias Ringwald
911664436fSMatthias Ringwald // force exit low power mode
921664436fSMatthias Ringwald __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
931664436fSMatthias Ringwald }
94