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-buzzer
33 * @{
34 *
35 * \file
36 * Driver for the Sensortag Buzzer.
37 * \author
38 * Edvard Pettersen <e.pettersen@ti.com>
39 */
40/*---------------------------------------------------------------------------*/
41#include "contiki.h"
42#include "buzzer.h"
43/*---------------------------------------------------------------------------*/
44#include <Board.h>
45#include <ti/drivers/PIN.h>
46#include <ti/drivers/Power.h>
47#include <ti/drivers/power/PowerCC26XX.h>
48#include <ti/drivers/pin/PINCC26XX.h>
49#include <ti/drivers/timer/GPTimerCC26XX.h>
50/*---------------------------------------------------------------------------*/
51#include <stdint.h>
52#include <string.h>
53#include <stdio.h>
54/*---------------------------------------------------------------------------*/
55/* Configure BUZZER pin */
56#ifndef Board_BUZZER
57#error "Board file doesn't define pin Board_BUZZER"
58#endif
59#define BUZZER_PIN Board_BUZZER
60/*---------------------------------------------------------------------------*/
61static const PIN_Config pin_table[] = {
62 BUZZER_PIN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW,
63 PIN_TERMINATE
64};
65
66static PIN_State pin_state;
67static PIN_Handle pin_handle;
68
69static GPTimerCC26XX_Handle gpt_handle;
70
71static bool has_init;
72static volatile bool is_running;
73/*---------------------------------------------------------------------------*/
74bool
76{
77 if(has_init) {
78 return true;
79 }
80
81 GPTimerCC26XX_Params gpt_params;
82 GPTimerCC26XX_Params_init(&gpt_params);
83
84 gpt_params.width = GPT_CONFIG_16BIT;
85 gpt_params.mode = GPT_MODE_PWM;
86 gpt_params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;
87
88 gpt_handle = GPTimerCC26XX_open(Board_GPTIMER0A, &gpt_params);
89 if(!gpt_handle) {
90 return false;
91 }
92
93 is_running = false;
94
95 has_init = true;
96 return true;
97}
98/*---------------------------------------------------------------------------*/
99bool
101{
102 return is_running;
103}
104/*---------------------------------------------------------------------------*/
105bool
106buzzer_start(uint32_t freq)
107{
108 if(!has_init) {
109 return false;
110 }
111
112 if(freq == 0) {
113 return false;
114 }
115
116 if(is_running) {
117 return true;
118 }
119
120 pin_handle = PIN_open(&pin_state, pin_table);
121 if(!pin_handle) {
122 return false;
123 }
124
125 Power_setDependency(PowerCC26XX_XOSC_HF);
126
127 PINCC26XX_setMux(pin_handle, BUZZER_PIN, GPT_PIN_0A);
128
129 /* MCU runs at 48 MHz */
130 GPTimerCC26XX_Value load_value = (48 * 1000 * 1000) / freq;
131
132 GPTimerCC26XX_setLoadValue(gpt_handle, load_value);
133 GPTimerCC26XX_setMatchValue(gpt_handle, load_value / 2);
134 GPTimerCC26XX_start(gpt_handle);
135
136 is_running = true;
137 return true;
138}
139/*---------------------------------------------------------------------------*/
140void
142{
143 if(!gpt_handle) {
144 return;
145 }
146
147 if(!is_running) {
148 return;
149 }
150
151 Power_releaseDependency(PowerCC26XX_XOSC_HF);
152
153 GPTimerCC26XX_stop(gpt_handle);
154
155 PIN_close(pin_handle);
156 pin_handle = NULL;
157
158 is_running = false;
159}
160/*---------------------------------------------------------------------------*/
161/** @} */
bool buzzer_running()
Retrieve the buzzer state.
Definition: buzzer.c:100
void buzzer_stop()
Stop the buzzer.
Definition: buzzer.c:106
void buzzer_init()
Initialise the buzzer.
Definition: buzzer.c:52
void buzzer_start(int freq)
Start the buzzer.
Definition: buzzer.c:64