00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 #include "board.h"
00117 #include <stdio.h>
00118
00119
00120
00121
00122 #define PIN_TC_TIOA_OUT {PIO_PA0B_TIOA0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
00123 #define PIN_TC_TIOA_IN {PIO_PD21C_TIOA11, PIOD, ID_PIOD, PIO_PERIPH_C, PIO_DEFAULT}
00124
00125 #define TC_WAVE_BASE TC0
00126 #define TC_WAVE_ID ID_TC0
00127 #define TC_WAVE_CHANNEL 0
00128
00129 #define TC_CAPTURE_BASE TC3
00130 #define TC_CAPTURE_ID ID_TC11
00131 #define TC_CAPTURE_CHANNEL 2
00132 #define TC_Handler TC11_Handler
00133 #define TC_IRQn TC11_IRQn
00134
00135
00136
00137
00138 struct WaveformConfiguration {
00139
00140 uint32_t clockSelection;
00141
00142 uint16_t frequency;
00143
00144 uint16_t dutyCycle;
00145 };
00146
00147
00148
00149
00150
00151
00152 static const Pin pTcPins[] = {PIN_TC_TIOA_OUT, PIN_TC_TIOA_IN};
00153
00154 static const struct WaveformConfiguration waveformConfigurations[] = {
00155
00156 {TC_CMR_TCCLKS_TIMER_CLOCK4, 400, 30},
00157 {TC_CMR_TCCLKS_TIMER_CLOCK3, 500, 50},
00158 {TC_CMR_TCCLKS_TIMER_CLOCK3, 800, 75},
00159 {TC_CMR_TCCLKS_TIMER_CLOCK2, 1000, 80},
00160 {TC_CMR_TCCLKS_TIMER_CLOCK2, 4000, 55}
00161 };
00162
00163
00164 static uint8_t configuration = 0;
00165
00166
00167 const uint8_t numConfigurations = sizeof(waveformConfigurations) /
00168 sizeof(struct WaveformConfiguration);
00169
00170 static uint32_t _dwCaptured_pulses;
00171 static uint32_t _dwCaptured_ra ;
00172 static uint32_t _dwCaptured_rb ;
00173 const uint32_t divisors[5] = {2, 8, 32, 128, BOARD_MCK / 32768};
00174
00175
00176
00177
00178
00179
00180
00181
00182 static void DisplayMenu(void)
00183 {
00184 uint8_t i;
00185 printf("\n\rMenu :\n\r");
00186 printf("------\n\r");
00187
00188 printf(" Output waveform property:\n\r");
00189 for (i = 0; i < numConfigurations; i++) {
00190 printf(" %d: Set Frequency = %4u Hz, Duty Cycle = %2u%%\n\r",
00191 i,
00192 (unsigned int)waveformConfigurations[i].frequency,
00193 (unsigned int)waveformConfigurations[i].dutyCycle);
00194 }
00195 printf(" -------------------------------------------\n\r");
00196 printf(" c: Capture waveform from TC capture channel \n\r");
00197 printf(" s: Stop capture and display informations what have been captured \n\r");
00198 printf(" h: Display menu \n\r");
00199 printf("------\n\r\n\r");
00200 }
00201
00202
00203
00204
00205
00206 void TC_Handler( void )
00207 {
00208 uint32_t status ;
00209 status = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_SR;
00210
00211 if ( (status & TC_SR_LDRBS) == TC_SR_LDRBS ) {
00212 _dwCaptured_pulses++ ;
00213 _dwCaptured_ra = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_RA ;
00214 _dwCaptured_rb = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_RB ;
00215 }
00216 }
00217
00218
00219
00220
00221 static void TcWaveformConfigure(void)
00222 {
00223 uint32_t ra, rc;
00224
00225 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CMR =
00226 waveformConfigurations[configuration].clockSelection
00227 | TC_CMR_WAVE
00228 | TC_CMR_ACPA_SET
00229 | TC_CMR_ACPC_CLEAR
00230 | TC_CMR_CPCTRG;
00231 rc = (BOARD_MCK / divisors[waveformConfigurations[configuration].clockSelection]) \
00232 / waveformConfigurations[configuration].frequency;
00233 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_RC = rc;
00234 ra = (100 - waveformConfigurations[configuration].dutyCycle) * rc / 100;
00235 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_RA = ra;
00236 }
00237
00238
00239
00240
00241 static void TcWaveformInitialize(void)
00242 {
00243
00244 PMC_EnablePeripheral(TC_WAVE_ID);
00245
00246 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CCR = TC_CCR_CLKDIS;
00247
00248 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_IDR = 0xFFFFFFFF;
00249
00250 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_SR;
00251
00252 TcWaveformConfigure();
00253
00254 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG ;
00255 printf ("Start waveform: Frequency = %d Hz,Duty Cycle = %2d%%\n\r",
00256 waveformConfigurations[configuration].frequency,
00257 waveformConfigurations[configuration].dutyCycle);
00258 }
00259
00260
00261
00262
00263 static void TcCaptureInitialize(void)
00264 {
00265
00266 PMC_EnablePeripheral(TC_CAPTURE_ID);
00267
00268 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CCR = TC_CCR_CLKDIS;
00269
00270 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IDR = 0xFFFFFFFF;
00271
00272 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_SR;
00273
00274 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CMR = \
00275 ( TC_CMR_TCCLKS_TIMER_CLOCK2
00276 | TC_CMR_LDRA_RISING
00277 | TC_CMR_LDRB_FALLING
00278 | TC_CMR_ABETRG
00279 | TC_CMR_ETRGEDG_FALLING
00280 );
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 int main( void )
00292 {
00293 uint8_t ucKey ;
00294 uint16_t frequence, dutyCycle ;
00295
00296
00297 WDT_Disable( WDT ) ;
00298
00299
00300 SCB_EnableICache();
00301 SCB_EnableDCache();
00302
00303
00304 printf( "-- TC capture waveform example %s --\n\r", SOFTPACK_VERSION ) ;
00305 printf( "-- %s\n\r", BOARD_NAME ) ;
00306 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00307
00308
00309 PIO_Configure( pTcPins, PIO_LISTSIZE( pTcPins ) ) ;
00310
00311 printf("Configure TC channel %d as waveform operating mode \n\r",
00312 TC_WAVE_CHANNEL);
00313 TcWaveformInitialize() ;
00314
00315 printf("Configure TC channel %d as capture operating mode \n\r",
00316 TC_CAPTURE_CHANNEL);
00317 TcCaptureInitialize() ;
00318
00319
00320 NVIC_ClearPendingIRQ(TC_IRQn);
00321 NVIC_EnableIRQ(TC_IRQn);
00322
00323
00324 DisplayMenu() ;
00325
00326 while ( 1 ) {
00327 ucKey = DBG_GetChar() ;
00328
00329 switch ( ucKey ) {
00330 case 'h' :
00331 DisplayMenu() ;
00332 break ;
00333
00334 case 's' :
00335 if ( _dwCaptured_pulses ) {
00336 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IDR = TC_IDR_LDRBS ;
00337 printf( "Captured %u pulses from TC capture channel , RA = %u, RB = %u \n\r",
00338 (unsigned int)_dwCaptured_pulses,
00339 (unsigned int)_dwCaptured_ra,
00340 (unsigned int)_dwCaptured_rb ) ;
00341
00342 frequence = (BOARD_MCK / 8)/ _dwCaptured_rb;
00343 dutyCycle = (_dwCaptured_rb - _dwCaptured_ra) * 100 / _dwCaptured_rb;
00344 printf( "Captured wave frequency = %d Hz, Duty cycle = %d%% \n\r",
00345 frequence, dutyCycle ) ;
00346 _dwCaptured_pulses = 0 ;
00347 _dwCaptured_ra = 0 ;
00348 _dwCaptured_rb = 0 ;
00349 } else {
00350 printf( "No waveform has been captured\n\r" ) ;
00351 }
00352 printf( "\n\rPress 'h' to display menu\n\r" ) ;
00353 break ;
00354
00355 case 'c' :
00356 printf ("Start capture, press 's' to stop \n\r");
00357 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IER = TC_IER_LDRBS;
00358
00359 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CCR = TC_CCR_CLKEN |
00360 TC_CCR_SWTRG;
00361 break ;
00362 default :
00363
00364 if ( (ucKey >= '0') && (ucKey <= ('0' + numConfigurations - 1 )) ) {
00365 if ( !_dwCaptured_pulses ) {
00366 configuration = ucKey - '0' ;
00367 TcWaveformInitialize() ;
00368 } else {
00369 printf( "In capturing ... , press 's' to stop capture first \n\r" ) ;
00370 }
00371 }
00372 break ;
00373 }
00374 }
00375 }
00376