typedef signed long s32;
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define CLAMP(x,min,max) {if ((x) <= (min)) (x) = (min); else if ((x) >= (max)) (x) = (max);}
#define KLPF_MAX_ADAPT 232L
#define KLPF_MIN_ADAPT 0L
#define KLPF_DEFAULT_ADAPT 160L
#define ACCEL_MIN_ADAPT 5
#define ACCEL_MAX_ADAPT 15
s32 gnKLpf = KLPF_DEFAULT_ADAPT; // IIR filter coefficient, 0 to 255
s32 gnKLpfMax = KLPF_MAX_ADAPT;
s32 gnAccel = 0; // rate of change of climbrate
s32 gnCpsx256 = 0; // climbrate in centimeters per second, with 8 bit fractional precision
s32 gnCps; // climbrate in centimeters per second
// IIR low pass filter using fixed point integer arithmetic. 8bits of fractional precision for IIR coefficient K.
// So the actual floating point coefficient is k = K/256, 0.0 <= k < 1.0. The filter computes
// climbRateFiltered = climbRateFiltered*k + newClimbRate*(1.0 - k). So K values close to 256 will result in
// heavy damping, K values close to 0 will result in almost no damping. The IIR low pass filtered output gnCps
// is the rounded up integer value, the 8bit fraction precision value gnCpsx256 is a global variable, so is
// maintained between calls to the filter. No integer divides required.
void sns_LPFClimbRate(void) {
s32 tmp;
tmp = gnCpsx256*gnKLpf + (gnCps<<8)*(256L-gnKLpf);
gnCpsx256 = (tmp >= 0 ? ((tmp + 128L)>>8) : ((tmp - 128L)>>8));
gnCps = (gnCpsx256>>8);
}
// Adapt the IIR filter coeffient to the rate of change. If samples are not changing much, increase the damping.
// If the samples are changing by a large amount, reduce the damping. This is done to reduce the response delay to a
// a step change, while still being able to heavily damp out jitter in steady state noisy samples.
// This function basically linearly interpolates KLpf between KLPF_MAX and KLPF_MIN based on the acceleration.
// Values of acceleration outside experimentally determined limits are clamped to the limits, depends on the
// application. A variable is used for KLpfMax to allow user to adjust the maximum allowed damping.
s32 sns_AdaptKLpf(void) {
s32 klpf,nAccel;
nAccel = ABS(gnAccel);
CLAMP(nAccel, ACCEL_MIN_ADAPT, ACCEL_MAX_ADAPT);
klpf = gnKLpfMax + ((KLPF_MIN_ADAPT-gnKLpfMax)*(nAccel - ACCEL_MIN_ADAPT))/(ACCEL_MAX_ADAPT-ACCEL_MIN_ADAPT);
CLAMP(klpf, KLPF_MIN_ADAPT, KLPF_MAX_ADAPT);
return klpf;
}
// usage :
// For each new data sample that arrives
// 1. calculate new unfiltered climbrate nCps from sample data buffer
// 2. gnAccel = (nCps - gnCps);
// 3. (optionally low pass filter gnAccel using same type of IIR filter, if required)
// 4. gnCps = nCps;
// 5. gnKLpf = sns_AdaptKLpf();
// 6. sns_LPFClimbRate();
#define COMB_FILTER_ORDER 3 // Filter Order, i.e., M
#define DELAY_ARRAY_SIZE 9 // Number of (delayed) input samples to store
int filtersum; // Sum-of-products accumulator for calculating the filter output value
int delay[DELAY_ARRAY_SIZE]; // Array for storing (delayed) input samples
static int gain1 = 1;
static int gain2 = 4;
interrupt void isr() //Interrupt service routine
{
short i; // Loop counter
delay[0] = get_sample() / gain1; // Read filter input sample from ADC and scale the sample
filtersum = delay[0]; // Initialize the accumulator
for (i = COMB_FILTER_ORDER; i > 0; i--)
{
filtersum += delay[i]; // Accumulate array elements
delay[i] = delay[i-1]; // Shift delay elements by one sample
}
filtersum *= gain2; // Scale the sum-of-products
send_output(filtersum); // write filter output sample to DAC
return; // interrupt servicing complete
}
void main()
{
short i; // loop counter
for (i=0; i< DELAY_ARRAY_SIZE; i++) // Initialize delay elements with 0
{
delay[i] = 0;
}
init_all(); // global initialization
while(1); // infinite loop
// do nothing except wait for the interrupt
}
/* Interruption.c
JOSE DAVID VALENCIA PESQUEIRA
UPIITA-IPN
THIS PROGRAM DETECTS AN EXTERNAL INTERRUPTION ON A GPIO PIN*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <csl_gpio.h>
#include <csl_gpiohal.h>
#include <csl_irq.h>
/* GLOBAL VARIABLES
The flags are marked as volatile, to tell the compiler
that these can be modified by an external event, avoiding
to put them on registers and rather putting them on RAM
so they're ready for immediate use
*/
volatile int startflag = 0;
GPIO_Handle gpio_handle; /* Handle for the GPIO */
// GPIO Registers configuration
GPIO_Config gpio_config = {
0x00000000,
// gpgc = Interruption passtrhrough mode and direct GPIO control mode
0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
0x00000000, // gdir = All GPIO pins as inputs
0x00000000, // gpval = Stores logical level of pins
0x00000010, // IRQ enable for pin 4
0x00000010, // Enable Event for pin 4
0x00000000 // gppol -- default state
};
irq_ext_enable(){
/* First, globally disable interruptions
in the CSR (Control Status Register).
Bit 0 is the GIE (Global Interrupt Enable)*/
CSR = (CSR)&(0xFFFFFFFE);
// Enable NMIE bit in the IER (bit 1)
IER |= 0x00000002;
// Enable INT4 bit in the IER (4th bit)
IER |= 0x00000010;
// Finally, globally enable interruptions in the CSR
CSR |= 0x00000001;
}
main()
{
// To configure the GPIO
gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET );
GPIO_config(gpio_handle,&gpio_config);
irq_ext_enable();
comm_intr(); //init DSK
DSK6713_LED_init();
while(startflag == 0);
printf(“La interrupción externa encendió el led 3 del dsk6713\n”);
while(1) // Infinite While
}
interrupt void c_int04() //ISR
{
startflag = 1;
iter = 0;
DSK6713_LED_on(0); //To turn on the led
}
// GPIO.c UPIITA-IPN
// JOSE DAVID VALENCIA PESQUEIRA
//
// THIS PROGRAM ALLOWS THE DSP TO SEND A BIT THROUGH A GPIO PIN (PIN 7 IN THIS EXAMPLE)
// TO TURN ON A LED ON
#include <stdio.h>
#include <stdlib.h>
#include <csl_gpio.h>
#include <csl_gpiohal.h>
#include <csl_irq.h>
// GLOBAL VARIABLES
volatile int flag = 1;
volatile int pulse_flag = 1;
// CODE TO DEFINE GPIO HANDLE
GPIO_Handle gpio_handle;
// GPIO REGISTER CONFIGURATION
GPIO_Config gpio_config = {
0x00000000, // gpgc = Interruption passthrough mode
0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
0x0000FFFF, // gdir = All GPIO pins as outputs
0x00000000, // gpval = Saves the logical states of pins
0x00000000, // gphm All interrupts disabled for IO pins
0x00000000, // gplm All interrupts for CPU EDMA disabled
0x00000000 // gppol -- default state */
};
// Function prototypes
void send_edge();
void delay();
// Function definitions
void delay()
{
// Delay function
int count, count2;
for(count = 0; count < 200; count++)
{
for(count2 = 0; count2 < 50000; count2++);
}
}
void send_edge()
{
DSK6713_init();
// Open and configure the GPIO
gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET );
GPIO_config(gpio_handle,&gpio_config);
// Send values through the pins
GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
delay();
GPIO_pinWrite(gpio_handle,GPIO_PIN7,1);
delay();
GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
}
main()
{
send_edge(); // Configures the pins and sends a rising edge
printf(“ ¡Felicidades! ¡Has logrado encender el led! ”);
}
// End of program>>
/*******************FLANGER.C******************************/
#include "Delay.h"
#include "Flanger.h"
static short samp_freq;
static double var_delay;
static short counter;
static short counter_limit;
static short control;
static short max_delay;
static short min_delay;
static double mix_vol;
static double delay_step;
/*
This is the initialization function, basically
it passes the initialization parameters to the delay block
and initializes the flanger control variables.
*/
void Flanger_init(short effect_rate,short sampling,short maxd,short mind,double fwv,double stepd,double fbv) {
Delay_Init(2,fbv,fwv,1);
samp_freq = sampling;
counter = effect_rate;
control = 1;
var_delay = mind;
//User Parameters
counter_limit = effect_rate;
max_delay = maxd;
min_delay = mind;
mix_vol = 1;
delay_step = stepd;
}
/*This is the flanging process task
that uses the delay task inside*/
double Flanger_process(double xin) {
double yout;
yout = Delay_task(xin);
return yout;
}
/*
This sweep function creates a slow frequency
ramp that will go up and down changing the
delay value at the same time. The counter
variable is a counter of amount of samples that
the function waits before it can change the delay.
*/
void Flanger_sweep(void) {
if (!--counter) {
var_delay+=control*delay_step;
if (var_delay > max_delay) {
control = -1;
}
if (var_delay < min_delay) {
control = 1;
}
Delay_set_delay(var_delay);
counter = counter_limit;
}
}
/*****************FLANGER.H********************************/
#ifndef __FLANGER_H__
#define __FLANGER_H__
extern void Flanger_init(short effect_rate,short sampling,short maxd,short mind,double fwv,double stepd,double fbv);
extern double Flanger_process(double xin);
extern void Flanger_sweep(void);
#endif
/*****USAGE EXAMPLE****************************************/
#include "Flanger.h"
void main(void) {
double xin;
double yout;
Flanger_init(500,16000,70,2,0.3,1,0.3);
while(1) {
if (new_sample_flag()) {
/*When there's new sample at your ADC or CODEC input*/
/*Read the sample*/
xin = read_sample();
/*Apply the Flanger_process function to the sample*/
yout = Flanger_process(0.7*xin);
/*Send the output value to your DAC or codec output*/
write_output(yout);
/*Makes the delay vary*/
Flanger_sweep();
}
}
}
// Include the filter coefficients and corresponding variables
#include "IIRBSF.h"
// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3]; // delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function t=125us, f = 8kHz
{
short i; // i loops through the MWSPT_NSEC number of sections
// Do the filtering if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, read in the x-value, apply the first stage gain
sectionOut = NUM[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
// Get the new delay_w[0];
delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];
// Get the output of this section
sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
// Delay the w's for the next interrupt
delay_w[i][2] = delay_w[i][1];
delay_w[i][1] = delay_w[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; //interrupt done
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC; i++)
for (j=0; j<3; j++)
delay_w[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
//iirFilter.c
// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#define A 1 //The amplitude of added wave
#define FREQ 1000 //The frequency of the sine wave in Hz
// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3];
// delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function t=125us, f = 8kHz
{
short i; // i loops through the MWSPT_NSEC number of sections
int period = 8000 / FREQ;
float rad = FREQ * 2 * pi;
int j = 0;
// Do the filtering if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, we read in the x-value, apply the first stage gain
sectionOut = NUM[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
// Get the new delay_w[0];
delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];
// Get the output of this section
sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
// Delay the w's for the next interrupt
delay_w[i][2] = delay_w[i][1];
delay_w[i][1] = delay_w[i][0];
}
//Add a tone to sectionOut
sectionOut = sectionOut + A*sin(rad*j); //Add a sine wave of freq rad to sectionOut
j = (j + 1) % period; //Increment the sine wave counter
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; // return from interrupt
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC; i++)
for (j=0; j<3; j++)
delay_w[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
//iirFilterSwitch.c
// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#include "IIRHPF.h"
// The intermediate values in the Direct Form II filter
float delay_w1[MWSPT_NSEC1][3];
float delay_w2[MWSPT_NSEC2][3];
// delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function
{
short i; // i loops through the MWSPT_NSEC number of sections
// Use the LPF if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, we read in the x-value, apply the first stage gain
sectionOut = NUM1[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC1; i++) { // Loop through all the sections
// Get the new delay_w1[0];
delay_w1[i][0] = sectionOut - DEN1[i][1]*delay_w1[i][1] - DEN1[i][2]*delay_w1[i][2];
// Get the output of this section
sectionOut = NUM1[i][0]*delay_w1[i][0] + NUM1[i][1]*delay_w1[i][1] + NUM1[i][2]*delay_w1[i][2];
// Delay the w's for the next interrupt
delay_w1[i][2] = delay_w1[i][1];
delay_w1[i][1] = delay_w1[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then use HPF
sectionOut = NUM2[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC2; i++) { // Loop through all the sections
// Get the new delay_w2[0];
delay_w2[i][0] = sectionOut - DEN2[i][1]*delay_w2[i][1] - DEN2[i][2]*delay_w2[i][2];
// Get the output of this section
sectionOut = NUM2[i][0]*delay_w2[i][0] + NUM2[i][1]*delay_w2[i][1] + NUM2[i][2]*delay_w2[i][2];
// Delay the w's for the next interrupt
delay_w2[i][2] = delay_w2[i][1];
delay_w2[i][1] = delay_w2[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
}
return; // return from interrupt
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC1; i++)
for (j=0; j<3; j++)
delay_w1[i][j] = 0; // init intermediate array
for (i=0; i<MWSPT_NSEC2; i++)
for (j=0; j<3; j++)
delay_w2[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
// These coefficients are used to filter
#include "yourfiltercoeffs.h"
// scale factor, S_h = 2^K.
#define K 15
int filterout = 0; // In fixed-point implementation, the accumulator is 32-bit int
short delay[LENGTH]; // Short is the 16-bit integer variable
interrupt void isr() // Interrupt function
{
short i;
int Sx = 1;
// Do the filtering as per fixfilt if DIP switch 1 up
if (get_DIP1() == 1) {
// Direct-Form FIR
delay[0] = Sx * get_sample(); // input for filter
filterout = hMax[0] * delay[0]; // Set up filter sum
// Notice, C-arrays go from 0..LENGTH-1
for (i = LENGTH-1; i > 0; i--){ // Get sum of products
filterout += hMax[i] * delay[i];
delay[i] = delay[i-1]; // Renew input array
}
filterout = (filterout>>K); //Move into the lower 16 bits, reverses scaling
//Sign extension carries the upper bit down if negative
send_output(filterout); // output for filter
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; // return from interrupt
}
void main()
{
short i;
for (i=0; i<= LENGTH-1; i++) {
delay[i] = 0; // init filter processing array
}
init_all(); // init all
while(1); // infinite loop
}
#define COMB_FILTER_ORDER 3 // Filter Order, i.e., M
#define DELAY_ARRAY_SIZE 9 // Number of (delayed) input samples to store
int filtersum; // Sum-of-products accumulator for calculating the filter output value
int delay[DELAY_ARRAY_SIZE]; // Array for storing (delayed) input samples
static int gain1 = 1;
static int gain2 = 4;
interrupt void isr() //Interrupt service routine
{
short i; // Loop counter
delay[0] = get_sample() / gain1; // Read filter input sample from ADC and scale the sample
filtersum = delay[0]; // Initialize the accumulator
for (i = COMB_FILTER_ORDER; i > 0; i--)
{
filtersum += delay[i]; // Accumulate array elements
delay[i] = delay[i-1]; // Shift delay elements by one sample
}
filtersum *= gain2; // Scale the sum-of-products
send_output(filtersum); // write filter output sample to DAC
return; // interrupt servicing complete
}
void main()
{
short i; // loop counter
for (i=0; i< DELAY_ARRAY_SIZE; i++) // Initialize delay elements with 0
{
delay[i] = 0;
}
init_all(); // global initialization
while(1); // infinite loop
// do nothing except wait for the interrupt
}
// Random number generator that sounds pleasant in audio algorithms.
// The argument and return value is a 30-bit (positive nonzero) integer.
// This is Duttaro's 30-bit pseudorandom number generator, p.124 J.AES, vol 50 no 3 March 2002;
// I found this 30-bit pseudorandom number generator sounds far superior to the 31-bit
// pseudorandom number generator presented in the same article.
// To make result a signed fixed-point value, do not shift left into the sign bit;
// instead, subtract 0x20000000 then multiply by a scale factor.
#define NextRand(rr) (((((rr >> 16) ^ (rr >> 15) ^ (rr >> 1) ^ rr) & 1) << 29) | (rr >> 1))