Contiki-NG
buzzer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup sensortag-cc26xx-buzzer
33  * @{
34  *
35  * \file
36  * Driver for the Sensortag Buzzer
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "buzzer.h"
41 #include "ti-lib.h"
42 #include "lpm.h"
43 
44 #include <stdint.h>
45 #include <string.h>
46 #include <stdio.h>
47 /*---------------------------------------------------------------------------*/
48 static uint8_t buzzer_on;
49 LPM_MODULE(buzzer_module, NULL, NULL, NULL, LPM_DOMAIN_PERIPH);
50 /*---------------------------------------------------------------------------*/
51 void
53 {
54  buzzer_on = 0;
55 }
56 /*---------------------------------------------------------------------------*/
57 uint8_t
59 {
60  return buzzer_on;
61 }
62 /*---------------------------------------------------------------------------*/
63 void
64 buzzer_start(int freq)
65 {
66  uint32_t load;
67 
68  /* Enable GPT0 clocks under active, sleep, deep sleep */
69  ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_TIMER0);
70  ti_lib_prcm_peripheral_sleep_enable(PRCM_PERIPH_TIMER0);
71  ti_lib_prcm_peripheral_deep_sleep_enable(PRCM_PERIPH_TIMER0);
72  ti_lib_prcm_load_set();
73  while(!ti_lib_prcm_load_get());
74 
75  /* Drive the I/O ID with GPT0 / Timer A */
76  ti_lib_ioc_port_configure_set(BOARD_IOID_BUZZER, IOC_PORT_MCU_PORT_EVENT0,
77  IOC_STD_OUTPUT);
78 
79  /* GPT0 / Timer A: PWM, Interrupt Enable */
80  HWREG(GPT0_BASE + GPT_O_TAMR) = (TIMER_CFG_A_PWM & 0xFF) | GPT_TAMR_TAPWMIE;
81 
82  buzzer_on = 1;
83 
84  /*
85  * Register ourself with LPM. This will keep the PERIPH PD powered on
86  * during deep sleep, allowing the buzzer to keep working while the chip is
87  * being power-cycled
88  */
89  lpm_register_module(&buzzer_module);
90 
91  /* Stop the timer */
92  ti_lib_timer_disable(GPT0_BASE, TIMER_A);
93 
94  if(freq > 0) {
95  load = (GET_MCU_CLOCK / freq);
96 
97  ti_lib_timer_load_set(GPT0_BASE, TIMER_A, load);
98  ti_lib_timer_match_set(GPT0_BASE, TIMER_A, load / 2);
99 
100  /* Start */
101  ti_lib_timer_enable(GPT0_BASE, TIMER_A);
102  }
103 }
104 /*---------------------------------------------------------------------------*/
105 void
107 {
108  buzzer_on = 0;
109 
110  /*
111  * Unregister the buzzer module from LPM. This will effectively release our
112  * lock for the PERIPH PD allowing it to be powered down (unless some other
113  * module keeps it on)
114  */
115  lpm_unregister_module(&buzzer_module);
116 
117  /* Stop the timer */
118  ti_lib_timer_disable(GPT0_BASE, TIMER_A);
119 
120  /*
121  * Stop the module clock:
122  *
123  * Currently GPT0 is in use by clock_delay_usec (GPT0/TB) and by this
124  * module here (GPT0/TA).
125  *
126  * clock_delay_usec
127  * - is definitely not running when we enter here and
128  * - handles the module clock internally
129  *
130  * Thus, we can safely change the state of module clocks here.
131  */
132  ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_TIMER0);
133  ti_lib_prcm_peripheral_sleep_disable(PRCM_PERIPH_TIMER0);
134  ti_lib_prcm_peripheral_deep_sleep_disable(PRCM_PERIPH_TIMER0);
135  ti_lib_prcm_load_set();
136  while(!ti_lib_prcm_load_get());
137 
138  /* Un-configure the pin */
139  ti_lib_ioc_pin_type_gpio_input(BOARD_IOID_BUZZER);
140  ti_lib_ioc_io_input_set(BOARD_IOID_BUZZER, IOC_INPUT_DISABLE);
141 }
142 /*---------------------------------------------------------------------------*/
143 /** @} */
Header file with macros which rename TI CC26xxware functions.
void buzzer_stop()
Stop the buzzer.
Definition: buzzer.c:140
Header file for the Sensortag Buzzer.
uint8_t buzzer_state()
Retrieve the buzzer state.
Definition: buzzer.c:58
bool buzzer_init()
Initialise the buzzer.
Definition: buzzer.c:75
#define BOARD_IOID_BUZZER
Buzzer Pin.
Definition: board.h:120
bool buzzer_start(uint32_t freq)
Start the buzzer.
Definition: buzzer.c:106
#define LPM_MODULE(n, m, s, w, l)
Declare a variable to be used in order to get notifications from LPM.
Definition: lpm.h:92
void lpm_unregister_module(lpm_registered_module_t *module)
Unregister a module from LPM notifications.
Definition: lpm.c:545
void lpm_register_module(lpm_registered_module_t *module)
Register a module for LPM notifications.
Definition: lpm.c:539