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
00117
00118 #include "board.h"
00119 #include <stdio.h>
00120
00121
00122
00123
00124 #define PIN_TC_TIOA_OUT {PIO_PA0B_TIOA0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
00125 #define PIN_TC_TIOA_IN {PIO_PD21C_TIOA11, PIOD, ID_PIOD, PIO_PERIPH_C, PIO_DEFAULT}
00126
00127 #define TC_WAVE_BASE TC0
00128 #define TC_WAVE_ID ID_TC0
00129 #define TC_WAVE_CHANNEL 0
00130
00131 #define TC_CAPTURE_BASE TC3
00132 #define TC_CAPTURE_ID ID_TC11
00133 #define TC_CAPTURE_CHANNEL 2
00134 #define TC_Handler TC11_Handler
00135 #define TC_IRQn TC11_IRQn
00136
00137
00138
00139
00140 struct WaveformConfiguration {
00141
00142 uint32_t clockSelection;
00143
00144 uint16_t frequency;
00145
00146 uint16_t dutyCycle;
00147 };
00148
00149
00150
00151
00152
00153
00154 static const Pin pTcPins[] = {PIN_TC_TIOA_OUT, PIN_TC_TIOA_IN};
00155
00156 static const struct WaveformConfiguration waveformConfigurations[] = {
00157
00158 {TC_CMR_TCCLKS_TIMER_CLOCK4, 400, 30},
00159 {TC_CMR_TCCLKS_TIMER_CLOCK3, 500, 50},
00160 {TC_CMR_TCCLKS_TIMER_CLOCK3, 800, 75},
00161 {TC_CMR_TCCLKS_TIMER_CLOCK2, 1000, 80},
00162 {TC_CMR_TCCLKS_TIMER_CLOCK2, 4000, 55}
00163 };
00164
00165
00166 static uint8_t configuration = 0;
00167
00168
00169 const uint8_t numConfigurations = sizeof(waveformConfigurations) /
00170 sizeof(struct WaveformConfiguration);
00171
00172 static uint32_t _dwCaptured_pulses;
00173 static uint32_t _dwCaptured_ra;
00174 static uint32_t _dwCaptured_rb;
00175 const uint32_t divisors[5] = {2, 8, 32, 128, BOARD_MCK / 32768};
00176
00177
00178
00179
00180
00181
00182
00183
00184 static void DisplayMenu(void)
00185 {
00186 uint8_t i;
00187
00188 printf("\n\rMenu :\n\r");
00189 printf("------\n\r");
00190 printf(" Output waveform property:\n\r");
00191
00192 for (i = 0; i < numConfigurations; i++) {
00193 printf(" %d: Set Frequency = %4u Hz, Duty Cycle = %2u%%\n\r",
00194 i,
00195 (unsigned int)waveformConfigurations[i].frequency,
00196 (unsigned int)waveformConfigurations[i].dutyCycle);
00197 }
00198 printf(" -------------------------------------------\n\r");
00199 printf(" c: Capture waveform from TC capture channel \n\r");
00200 printf(" s: Stop capture and display informations what have been captured \n\r");
00201 printf(" h: Display menu \n\r");
00202 printf("------\n\r\n\r");
00203 }
00204
00205
00206
00207
00208
00209 void TC_Handler(void)
00210 {
00211 uint32_t status;
00212 status = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_SR;
00213
00214 if ((status & TC_SR_LDRBS) == TC_SR_LDRBS) {
00215 _dwCaptured_pulses++;
00216 _dwCaptured_ra = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_RA;
00217 _dwCaptured_rb = TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_RB;
00218 }
00219 }
00220
00221
00222
00223
00224 static void TcWaveformConfigure(void)
00225 {
00226 uint32_t ra, rc;
00227
00228
00229 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CMR =
00230 waveformConfigurations[configuration].clockSelection
00231 | TC_CMR_WAVE
00232 | TC_CMR_ACPA_SET
00233 | TC_CMR_ACPC_CLEAR
00234 | TC_CMR_CPCTRG;
00235 rc = (BOARD_MCK / divisors[waveformConfigurations[configuration].clockSelection]) \
00236 / waveformConfigurations[configuration].frequency;
00237 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_RC = rc;
00238 ra = (100 - waveformConfigurations[configuration].dutyCycle) * rc / 100;
00239 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_RA = ra;
00240 }
00241
00242
00243
00244
00245 static void TcWaveformInitialize(void)
00246 {
00247
00248 PMC_EnablePeripheral(TC_WAVE_ID);
00249
00250 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CCR = TC_CCR_CLKDIS;
00251
00252 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_IDR = 0xFFFFFFFF;
00253
00254 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_SR;
00255
00256 TcWaveformConfigure();
00257
00258 TC_WAVE_BASE->TC_CHANNEL[TC_WAVE_CHANNEL].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
00259 printf ("Start waveform: Frequency = %d Hz,Duty Cycle = %2d%%\n\r",
00260 waveformConfigurations[configuration].frequency,
00261 waveformConfigurations[configuration].dutyCycle);
00262 }
00263
00264
00265
00266
00267 static void TcCaptureInitialize(void)
00268 {
00269
00270 PMC_EnablePeripheral(TC_CAPTURE_ID);
00271
00272 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CCR = TC_CCR_CLKDIS;
00273
00274 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IDR = 0xFFFFFFFF;
00275
00276 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_SR;
00277
00278 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CMR = \
00279 ( TC_CMR_TCCLKS_TIMER_CLOCK2
00280 | TC_CMR_LDRA_RISING
00281 | TC_CMR_LDRB_FALLING
00282 | TC_CMR_ABETRG
00283 | TC_CMR_ETRGEDG_FALLING
00284 );
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 int main( void )
00296 {
00297 uint8_t ucKey;
00298 uint16_t frequence, dutyCycle;
00299
00300
00301 WDT_Disable(WDT);
00302
00303
00304 SCB_EnableICache();
00305 SCB_EnableDCache();
00306
00307
00308 printf("-- TC capture waveform example %s --\n\r", SOFTPACK_VERSION);
00309 printf("-- %s\n\r", BOARD_NAME);
00310 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00311
00312
00313 PIO_Configure( pTcPins, PIO_LISTSIZE(pTcPins));
00314
00315 printf("Configure TC channel %d as waveform operating mode \n\r",
00316 TC_WAVE_CHANNEL);
00317 TcWaveformInitialize();
00318
00319 printf("Configure TC channel %d as capture operating mode \n\r",
00320 TC_CAPTURE_CHANNEL);
00321 TcCaptureInitialize();
00322
00323
00324 NVIC_ClearPendingIRQ(TC_IRQn);
00325 NVIC_EnableIRQ(TC_IRQn);
00326
00327
00328 DisplayMenu();
00329
00330 while (1) {
00331 ucKey = DBG_GetChar();
00332
00333 switch (ucKey) {
00334 case 'h':
00335 DisplayMenu();
00336 break;
00337
00338 case 's':
00339 if (_dwCaptured_pulses) {
00340 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IDR = TC_IDR_LDRBS;
00341 printf( "Captured %u pulses from TC capture channel , RA = %u, RB = %u \n\r",
00342 (unsigned int)_dwCaptured_pulses,
00343 (unsigned int)_dwCaptured_ra,
00344 (unsigned int)_dwCaptured_rb );
00345
00346 frequence = (BOARD_MCK / 8) / _dwCaptured_rb;
00347 dutyCycle = (_dwCaptured_rb - _dwCaptured_ra) * 100 / _dwCaptured_rb;
00348 printf( "Captured wave frequency = %d Hz, Duty cycle = %d%% \n\r",
00349 frequence, dutyCycle );
00350 _dwCaptured_pulses = 0;
00351 _dwCaptured_ra = 0;
00352 _dwCaptured_rb = 0;
00353 } else
00354 printf("No waveform has been captured\n\r");
00355 printf("\n\rPress 'h' to display menu\n\r");
00356 break;
00357
00358 case 'c':
00359 printf("Start capture, press 's' to stop \n\r");
00360 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_IER = TC_IER_LDRBS;
00361
00362 TC_CAPTURE_BASE->TC_CHANNEL[TC_CAPTURE_CHANNEL].TC_CCR = TC_CCR_CLKEN |
00363 TC_CCR_SWTRG;
00364 break;
00365 default :
00366
00367 if ((ucKey >= '0') && (ucKey <= ('0' + numConfigurations - 1 ))) {
00368 if (!_dwCaptured_pulses ) {
00369 configuration = ucKey - '0';
00370 TcWaveformInitialize();
00371 } else
00372 printf("In capturing ... , press 's' to stop capture first \n\r");
00373 }
00374 break;
00375 }
00376 }
00377 }
00378