+ All Categories
Home > Documents > Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface...

Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface...

Date post: 14-Jan-2016
Category:
Upload: merry-moore
View: 217 times
Download: 2 times
Share this document with a friend
Popular Tags:
19
Chapter 7 - Chapter 7 - Performance Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance
Transcript
Page 1: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Chapter 7 - PerformanceChapter 7 - Performance

Understanding the PIC32 Clock and learning to optimize the memory

interface for maximum performance

Page 2: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

PIC32MX3xx Clock ModulePIC32MX3xx Clock Module

Page 3: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

The OSCCON registerThe OSCCON register

Page 4: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Primary Oscillator Clock ChainPrimary Oscillator Clock Chain

InputDivider

InputDivider PLLPLL

OutputDivider

OutputDivider

4 MHz 72 MHz8 MHz 1 : 2 1 x 18 1 : 1

SystemClock

72 MHz

Page 5: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

PIC32MX3xx Configuration SettingsPIC32MX3xx Configuration Settings

FPLLODIV = DIV_1 Divide by 1

FPLLODIV = DIV_2 Divide by 2

FPLLODIV = DIV_4 Divide by 4

FPLLODIV = DIV_8 Divide by 8

FPLLODIV = DIV_16 Divide by 16

FPLLODIV = DIV_32 Divide by 32

FPLLODIV = DIV_64 Divide by 64

FPLLODIV = DIV_256 Divide by 256

#pragma config POSCMOD=XT, FNOSC=PRIPLL#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1#pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF

NOTE: All PIC32MX devices are currently released to operate at frequencies up to 80 MHz. FPLLMUL = MUL_20 gives the required multiplier.

Page 6: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Fast Fourier TransformFast Fourier Transform// input vectorunsigned char inB[N_FFT];// input complex vectorfloat xr[N_FFT];float xi[N_FFT];

// Fast Fourier Transformation void FFT( void){ int m, k, i, j; float a,b,c,d,wwr,wwi,pr,pi; // FFT loop m = N_FFT/2; j = 0; while( m > 0) { /* log(N) cycle */ k = 0; while( k < N_FFT) { // batterflies loop for( i=0; i<m; i++) { // batterfly a = xr[i+k]; b = xi[i+k]; c = xr[i+k+m]; d = xi[i+k+m]; wwr = wr[i<<j]; wwi = wi[i<<j]; pr = a-c; pi = b-d; xr[i+k] = a + c; xi[i+k] = b + d; xr[i+k+m] = pr * wwr - pi * wwi; xi[i+k+m] = pr * wwi + pi * wwr; } // for i k += m<<1 ; } // while k m >>= 1; j++; } // while m} // FFT

Page 7: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

WindowingWindowing// apply Hann window to input vectorvoid windowFFT( unsigned char *s){ int i; float *xrp, *xip, *wwp; // apply window to input signal xrp=xr; xip=xi; wwp=ww; for( i=0; i<N_FFT; i++) { *xrp++ = (*s++ - 128) * (*wwp++); *xip++ = 0; } // for i} // windowFFT

Page 8: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Computing Power and ScalingComputing Power and Scalingvoid powerScale( unsigned char *r){ int i, j; float t, max; float xrp, xip; // compute signal power (in place) and find maximum max = 0; for( i=0; i<N_FFT/2; i++) { j = rev[ i]; xrp = xr[ j]; xip = xi[ j]; t = xrp*xrp + xip*xip; xr[ j]=t; if ( t > max) max = t; } // bit reversal, scaling of output vector as unsigned char max = 255.0/max; for( i=0; i<N_FFT/2; i++) { t = xr[ rev[i]] * max; *r++ = t; }} // powerScale

Page 9: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

InitializationInitialization// input vectorunsigned char inB[N_FFT];volatile int inCount;// rotation vectorsfloat wr[N_FFT/2];float wi[N_FFT/2];// bit reversal vectorshort rev[N_FFT/2];// window float ww[N_FFT];void initFFT( void){ int i, m, t, k; float *wwp; for(i=0; i<N_FFT/2; i++) { // rotations wr[i] = cos(PI2N * i); wi[i] = sin(PI2N * i); // bit reversal t = i; m = 0; k = N_FFT-1; while (k>0) { m = (m << 1) + (t & 1); t = t >> 1; k = k >> 1; } rev[i] = m; } // for i // initialize Hanning window vector for( wwp=ww, i=0; i<N_FFT; i++) *wwp++ = 0.5 - 0.5 * cos(PI2N * i); } // initFFT

Page 10: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

““FFT.h”FFT.h”/*** FFT.h**** power of two optimized algorithm*/#include <math.h>#define N_FFT 256 // samples must be power of 2#define PI2N 2 * M_PI / N_FFTextern unsigned char inB[];extern volatile int inCount;// preparation of the rotation vectorsvoid initFFT( void);// input windowvoid windowFFT( unsigned char *source);// fast fourier transform void FFT( void);// compute power and scale outputvoid powerScale( unsigned char *dest);

Page 11: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

The Running ProjectThe Running Project/*** Run.c***/// configuration bit settings#pragma config POSCMOD=XT, FNOSC=PRIPLL#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1#pragma config FWDTEN=OFF, CP=OFF, BWP=OFF#include <p32xxxx.h>#include <plib.h>#include "fft.h“main(){ int i, t; double f; // 1. initializations initFFT(); // test sinusoid for (i=0; i<N_FFT; i++) { f = sin(2 * PI2N * i ); inB[ i] = 128 + ( unsigned char) (120.0 * f); } // for

// 2. perform FFT algorithm windowFFT( inB); FFT(); powerScale( inB);

// 3. infinite loop while( 1);} // main

Page 12: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Capturing TimeCapturing Time // init 32-bit timer4/5 OpenTimer45( T4_ON | T4_SOURCE_INT, 0); // clear the 32-bit timer count WriteTimer45( 0);// insert FFT function calls here // read the timer count t = ReadTimer45();

Page 13: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

The CHECON RegisterThe CHECON Register

Page 14: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Optimizing WaitStatesOptimizing WaitStates

SYSTEMConfigWaitStatesAndPB(72000000L);

Page 15: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Turning the Cache ONTurning the Cache ON

SYSTEMConfigWaitStatesAndPB(72000000L); CheKseg0CacheOn();

Page 16: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Enabling Pre-FetchEnabling Pre-FetchSYSTEMConfigWaitStatesAndPB(72000000L); CheKseg0CacheOn();mCheConfigure( CHECON | 0x30);

Page 17: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Remove RAM Wait StatesRemove RAM Wait States // configure PB frequency and the number of wait states SYSTEMConfigWaitStatesAndPB(72000000L); // enable the cache for max performance CheKseg0CacheOn(); // enable instruction prefetch mCheConfigure( CHECON | 0x30); // disable RAM wait states mBMXDisableDRMWaitState();

SYSTEMConfigPerformance( 72000000L);

Page 18: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

Data Control & Monitor InterfaceData Control & Monitor Interface

Page 19: Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.

Di Jasio - Programming 32-bit Microcontrollers in C

The FFT OutputThe FFT Output


Recommended