CircleOS  1
buzzer_spe.c
Go to the documentation of this file.
1 /****************** COPYRIGHT (C) 2007-2013 KEOLABS S.A.S. ********************/
15 /******************************************************************************/
16 
17 /* Includes ------------------------------------------------------------------*/
18 #include "circle.h"
19 
21 
22 /* Private typedef -----------------------------------------------------------*/
23 
24 /* Private define ------------------------------------------------------------*/
25 
26 /* Private macro -------------------------------------------------------------*/
27 /* Private variables ---------------------------------------------------------*/
28 #if !AUDIO_AVAIL
29 static TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
30 static TIM_OCInitTypeDef TIM_OCInitStructure;
31 #endif
32 u16 CCR_Val = 0x2EE0;
33 
34 /* Private function prototypes -----------------------------------------------*/
35 
36 /* Private functions ---------------------------------------------------------*/
37 
38 /* External variables --------------------------------------------------------*/
39 extern enum BUZZER_mode Buzzer_Mode;
40 extern RCC_ClocksTypeDef RCC_ClockFreq;
41 
42 /***********************************************************************************
43 *
44 * BUZZER_SetFrequency
45 *
46 ************************************************************************************/
54 /********************************************************************************/
55 void BUZZER_SetFrequency( u16 freq )
56 {
57 
58 #if !AUDIO_AVAIL
59  /* Calculate the frequency (depend on the PCLK1 clock value) */
60  CCR_Val = ( RCC_ClockFreq.PCLK1_Frequency / freq );
61 
62  TIM_TimeBaseStructure.TIM_Period = CCR_Val * 2;
63  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
64  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
65  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
66 
67  TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
68 
69  /* Output Compare Toggle Mode configuration: Channel3 */
70  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
71  /* in FWLib v1.0 : TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;*/
72  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* FWLib v2.0*/
73  TIM_OCInitStructure.TIM_Pulse = CCR_Val;
74 
75  TIM_OC3Init( TIM3, &TIM_OCInitStructure ); /* FWLib v2.0*/
76  TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Enable );
77 
78 #else
79  AUDIO_BUZZER_SetToneFrequency( freq );
80 #endif
81 }
82 
83 /* Public functions for CircleOS ---------------------------------------------*/
84 
85 /*******************************************************************************
86 *
87 * BUZZER_Init
88 *
89 *******************************************************************************/
97 /******************************************************************************/
98 NODEBUG2 void BUZZER_Init( void )
99 {
100 #if !AUDIO_AVAIL
101  GPIO_InitTypeDef GPIO_InitStructure;
102 
103  /* Enable GPIOB clock */
104  RCC_PERIPH_GPIO_CLOCK_CMD( RCC_APB2Periph_GPIOB, ENABLE );
105 
106  /* GPIOB Configuration: TIM3 3in Output */
107  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
108  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
109  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
110 
111  GPIO_Init( GPIOB, &GPIO_InitStructure );
112 
113  /* TIM3 Configuration ------------------------------------------------------*/
114  /* TIM3CLK = 18 MHz, Prescaler = 0x0, TIM3 counter clock = 18 MHz */
115  /* CC update rate = TIM3 counter clock / (2* CCR_Val) ~= 750 Hz */
116 
117  /* Enable TIM3 clock */
118  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );
119  TIM_DeInit( TIM3 );
120  TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );
121  TIM_OCStructInit( &TIM_OCInitStructure );
122 
123  /* Time base configuration */
124  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
125  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
126  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
127  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
128 
129  TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );
130 
131  /* Output Compare Toggle Mode configuration: Channel3 */
132  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
133  /* in FWLib v1.0 : TIM_OCInitStructure.TIM_Channel = TIM_Channel_3;*/
134  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; /* now in in FWLib v2.0*/
135  TIM_OCInitStructure.TIM_Pulse = CCR_Val;
136 
137  TIM_OC3Init( TIM3, &TIM_OCInitStructure ); /* changed against FWLib v2.0*/
138  TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Disable );
139 
140  BUZZER_SetFrequency( 440 );
141 #endif
142 
143 #if AUDIO_AVAIL
144  /* RQ : init done by Audio init*/
145 #endif
146 
148 }
149 
150 
152 
153 /* Public functions ----------------------------------------------------------*/
154 
155 
156 /*******************************************************************************
157 *
158 * BUZZER_SetMode
159 *
160 *******************************************************************************/
168 /******************************************************************************/
169 void BUZZER_SetMode( enum BUZZER_mode mode )
170 {
171 
172 #if AUDIO_AVAIL
173  /* Don't use the buzzer if audio playing */
174  if (( AUDIO_Playback_status != NO_SOUND )
175  || ( AUDIO_Recording_status != NO_RECORD )
176  || ( AUDIO_BuzzerOn == 0 ) )
177  {
178  mode = BUZZER_OFF;
179  }
180 #endif
181 
182  Buzzer_Mode = mode;
183  Buzzer_Counter = 0;
184 
185  switch ( mode )
186  {
187  case BUZZER_PLAYMUSIC :
188 #if AUDIO_AVAIL
189  AUDIO_BUZZER_OnOff( ON );
190 #endif
191  BUZZER_PlayNote(); /* play melody*/
192  /* no break */
193 
194  case BUZZER_LONGBEEP :
195  case BUZZER_SHORTBEEP :
196  case BUZZER_ON :
197 #if AUDIO_AVAIL
198  AUDIO_BUZZER_OnOff( ON );
199 #else
200  TIM_Cmd( TIM3, ENABLE );
201 #endif
202  break;
203 
204  case BUZZER_OFF :
205  default :
206 #if AUDIO_AVAIL
207  AUDIO_BUZZER_OnOff( OFF );
208 #else
209  TIM_Cmd( TIM3, DISABLE );
210 #endif
211  break;
212 
213  }
214 }
215