/* David Valencia at DSPRelated.com
This example shows how to use the External Memory Interface
expansion port of Spectrum Digital DSK6713 card as
a set of digital imputs
The use of buffer ICs is mandatory, as these pins can't deliver
too much current */
#define OUTPUT 0xA0000000 // EMIF ADDRESS (updated 3/11/2010)
int *output = (int*)OUTPUT; // Declare a pointer to EMIF's address
void main()
{
// To see how the value is written to the EMIF pins,
// you may prefer to run the program step-by-step
// Write a 32-bit digital value to the pins
*output = 0x48120A00; // Test value
}
#include <stdio.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#define BUF_SIZE 2
/*buffers for sound device*/
unsigned char devbuf[BUF_SIZE];
/*File descriptor for device*/
int audio_fd;
/*The sound card is known as the DSP*/
char DEVICE_NAME[]="/dev/dsp";
/*Device Settings*/
int format;
int channels;
int fs;
int len;
int frag;
int devcaps;
int main()
{
unsigned int temp;
unsigned int yout;
/*Samples format is 16 bit unsigned*/
format = AFMT_U16_LE;
/*1 Channel, MONO*/
channels = 1;
/*Sampling Rate is 16 KHz*/
fs = 16000;
/*This is a parameter used to set the DSP for low latencies*/
frag = 0x00020004;
/******************************************************
Set parameters in sound card device
*****************************************************/
/*Open sound card device*/
if ((audio_fd = open(DEVICE_NAME,O_RDWR, 0)) == -1) {
/* Open of device failed */
perror(DEVICE_NAME);
exit(1);
}
if (ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
{
perror ("SNDCTL_DSP_SETFRAGMENT");
exit (-1);
}
/*Set audio format*/
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format) == -1) {
/* fatal error */
perror("SNDCTL_DSP_SETFMT");
exit(1);
}
/*Set number of channels*/
if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
/* Fatal error */
perror("SNDCTL_DSP_CHANNELS");
exit(1);
}
/*Set sampling rate*/
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &fs)==-1) {
/* Fatal error */
perror("SNDCTL_DSP_SPEED");
exit(1);
}
if (ioctl(audio_fd, SNDCTL_DSP_SYNC) == -1) {
/* fatal error */
perror("SNDCTL_DSP_SYNC");
exit(1);
}
/******************************************************
This is the infinite loop to:
1. Read a sample
2. Process the sample
3. Write the sample to soundcard output
*****************************************************/
while(1) {
/* This is a blocking read function, the application will wait
until the sound card captures a sample
*/
if ((len = read(audio_fd,devbuf, sizeof(devbuf))) == -1) {
perror("audio read");
exit(1);
}
/* We can pass this variable to a temporary value,
in this case as unsigned 16 value, and then use this value
for processing
*/
temp = (devbuf[0])|(devbuf[1]<<8);
/* In this case no processing is done so the value is passed to the output. You can also use this sample to build an audio file, or send it trought a communications interface, etc.
*/
yout = temp;
devbuf[0] = yout&0xFF;
devbuf[1] = (yout>>8)&0xFF;
/* This is the way the output samples are sent to the soundcard
*/
if ((len = write(audio_fd,devbuf,sizeof(devbuf)) == -1)) {
perror("audio write");
exit(1);
}
}
return 0;
}
// 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))
// 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
}
//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
}
//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
}
// 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
}
#include <stdio.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#define BUF_SIZE 2
/*buffers for sound device*/
unsigned char devbuf[BUF_SIZE];
/*File descriptor for device*/
int audio_fd;
/*The sound card is known as the DSP*/
char DEVICE_NAME[]="/dev/dsp";
/*Device Settings*/
int format;
int channels;
int fs;
int len;
int frag;
int devcaps;
int main()
{
unsigned int temp;
unsigned int yout;
/*Samples format is 16 bit unsigned*/
format = AFMT_U16_LE;
/*1 Channel, MONO*/
channels = 1;
/*Sampling Rate is 16 KHz*/
fs = 16000;
/*This is a parameter used to set the DSP for low latencies*/
frag = 0x00020004;
/******************************************************
Set parameters in sound card device
*****************************************************/
/*Open sound card device*/
if ((audio_fd = open(DEVICE_NAME,O_RDWR, 0)) == -1) {
/* Open of device failed */
perror(DEVICE_NAME);
exit(1);
}
if (ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
{
perror ("SNDCTL_DSP_SETFRAGMENT");
exit (-1);
}
/*Set audio format*/
if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format) == -1) {
/* fatal error */
perror("SNDCTL_DSP_SETFMT");
exit(1);
}
/*Set number of channels*/
if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels) == -1) {
/* Fatal error */
perror("SNDCTL_DSP_CHANNELS");
exit(1);
}
/*Set sampling rate*/
if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &fs)==-1) {
/* Fatal error */
perror("SNDCTL_DSP_SPEED");
exit(1);
}
if (ioctl(audio_fd, SNDCTL_DSP_SYNC) == -1) {
/* fatal error */
perror("SNDCTL_DSP_SYNC");
exit(1);
}
/******************************************************
This is the infinite loop to:
1. Read a sample
2. Process the sample
3. Write the sample to soundcard output
*****************************************************/
while(1) {
/* This is a blocking read function, the application will wait
until the sound card captures a sample
*/
if ((len = read(audio_fd,devbuf, sizeof(devbuf))) == -1) {
perror("audio read");
exit(1);
}
/* We can pass this variable to a temporary value,
in this case as unsigned 16 value, and then use this value
for processing
*/
temp = (devbuf[0])|(devbuf[1]<<8);
/* In this case no processing is done so the value is passed to the output. You can also use this sample to build an audio file, or send it trought a communications interface, etc.
*/
yout = temp;
devbuf[0] = yout&0xFF;
devbuf[1] = (yout>>8)&0xFF;
/* This is the way the output samples are sent to the soundcard
*/
if ((len = write(audio_fd,devbuf,sizeof(devbuf)) == -1)) {
perror("audio write");
exit(1);
}
}
return 0;
}
/* 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>>
// Fast InvSqrt approxumation, with an error of less than 4%.
// This approximation is attributed to Greg Walsh.
inline void InvSqrt(float *x) { *(int *)x = 0x5f3759df - (*(int *)x >> 1); }
inline float SqrtSqrt(float x) { InvSqrt(&x); InvSqrt(&x); return x; }
// Fast power-of-10 approximation, with RMS error of 1.77%.
// This approximation developed by Nicol Schraudolph (Neural Computation vol 11, 1999).
// Adapted for 32-bit floats by Lippold Haken of Haken Audio, April 2010.
// Set float variable's bits to integer expression.
// f=b^f is approximated by
// (int)f = f*0x00800000*log(b)/log(2) + 0x3F800000-60801*8
// f=10^f is approximated by
// (int)f = f*27866352.6 + 1064866808.0
inline void Pow10(float *f) { *(int *)f = *f * 27866352.6 + 1064866808.0; };