📄 btc_cdemo.c
字号:
/////////////////////////////////////////////////////////////////////////////
//
// Example C program using Background Telemetry Channel (BTC)
// Analog Devices 2003
//
// This program defines several BTCs to allow transfer of data over the BTC
// interface while the DSP is running. Use the BTC Memory window in the
// debugger to view each channels data. The defined channels are described
// below:
//
// Timer Interrupt Counter: This channel is defined to be 1-word (4-bytes)
// in length and simply counts the number of timer
// interrupts that have occured.
//
// PF4 Counter: This channel is defined to be 1-word (4-bytes) in length and
// simply counts the number of times the PF4 pushbutton is pressed
// on the BF535 Ez-Kit.
//
// PF5 Counter: This channel is defined to be 1-word (4-bytes) in length and
// simply counts the number of times the PF5 pushbutton is pressed
// on the BF535 Ez-Kit.
//
// 256 word channel (PF6): This channel is defined to be 256-words (1024-bytes) in length and
// simply counts the number of times the PF6 pushbutton is pressed
// on the BF535 Ez-Kit. Each word in the channel is updated with the count.
//
// 256 word channel (PF7): This channel is defined to be 256-words (1024-bytes) in length and
// simply counts the number of times the PF7 pushbutton is pressed
// on the BF535 Ez-Kit. Each word in the channel is updated with the count.
//
// 256 byte channel: This channel is defined to be 256 bytes in length. The first word of the
// channel is used to count the number of timer interrupts that have occured.
//
// 4k byte channel: This channel is defined to be 4-kbytes in length. The first word of the
// channel is used to count the number of timer interrupts that have occured.
//
/////////////////////////////////////////////////////////////////////////////
#include <defbf535.h>
#include <defblackfin.h>
#include <sys/exception.h>
#include "btc.h"
////////////////////////
// function prototypes
////////////////////////
void initTimer(void);
void initGPIO(void);
/////////////////////////////////
// interrupt handler prototypes
/////////////////////////////////
EX_INTERRUPT_HANDLER(timerISR); // timer interrupt handler
EX_INTERRUPT_HANDLER(int12ISR); // int12 interrupt handler (pushbuttons)
/////////////////////
// global variables
/////////////////////
int timerCounter; // counts the number of timer interrupts that have fired
int pbCounter; // counts the number of times any pushbutton on the ez-kit was pressed
int pf4Counter; // counts the number of times the PF4 pushbutton on the ez-kit was pressed
int pf5Counter; // counts the number of times the PF5 pushbutton on the ez-kit was pressed
int pf6Counter; // counts the number of times the PF6 pushbutton on the ez-kit was pressed
int pf7Counter; // counts the number of times the PF7 pushbutton on the ez-kit was pressed
int pf6Array[0x100]; // a 256-word array
int pf7Array[0x100]; // a 256-word array
char array1[0x0100]; // a 256 byte array
char array2[0x1000]; // a 4k byte array
////////////////////
// BTC Definitions
////////////////////
BTC_MAP_BEGIN
// Channel Name, Starting Address, Length
BTC_MAP_ENTRY("Timer Interrupt Counter", (long)&timerCounter, sizeof(timerCounter))
BTC_MAP_ENTRY("PF4 Counter", (long)&pf4Counter, sizeof(pf4Counter))
BTC_MAP_ENTRY("PF5 Counter", (long)&pf5Counter, sizeof(pf5Counter))
BTC_MAP_ENTRY("256 word channel (PF6)", (long)&pf6Array, sizeof(pf6Array))
BTC_MAP_ENTRY("256 word channel (PF7)", (long)&pf7Array, sizeof(pf7Array))
BTC_MAP_ENTRY("256 byte channel", (long)&array1, sizeof(array1))
BTC_MAP_ENTRY("4k byte channel", (long)&array2, sizeof(array2))
BTC_MAP_END
///////////////
// main
///////////////
void main()
{
// an example of getting the starting address and length of
// a defined channel using macros defined in btc.h
int addr, len;
addr = BTC_CHANNEL_ADDR(0);
len = BTC_CHANNEL_LEN(0);
// install our interrupt handlers
register_handler(ik_ivg12, int12ISR);
register_handler(ik_timer, timerISR);
// initialize the timer and the programmable flags
initTimer();
initGPIO();
// initialize the BTC
btc_init();
// this is the polling loop for servicing all BTC transactions
while (1)
btc_poll();
}
//////////////////////
// initTimer
//////////////////////
void initTimer()
{
unsigned int *mmrPtr;
mmrPtr = (unsigned int*)TCNTL; // timer control register
*mmrPtr = TMPWR | TAUTORLD;
mmrPtr = (unsigned int*)TPERIOD; // timer period register
*mmrPtr = 0x00010000;
mmrPtr = (unsigned int*)TSCALE; // timer scale register
*mmrPtr = 0x00000080;
mmrPtr = (unsigned int*)TCOUNT; // timer count register
*mmrPtr = 0x00010000;
mmrPtr = (unsigned int*)TCNTL; // timer control register
*mmrPtr = TMPWR | TMREN | TAUTORLD; // enable the timer
}
/////////////////////
// initGPIO
/////////////////////
void initGPIO()
{
unsigned short *smmrPtr;
unsigned int *mmrPtr;
unsigned int nSiRev;
smmrPtr = (unsigned short*)FIO_DIR; // PF Config register
*smmrPtr = 0xf; // PF0-PF3 as outputs, PF4-PF15 as inputs
smmrPtr = (unsigned short*)FIO_FLAG_S; // PF Set register
*smmrPtr = 0xffff; // turn all LEDs on
smmrPtr = (unsigned short*)FIO_FLAG_C; // PF Clear register
*smmrPtr = 0xffff; // turn all LEDs off
smmrPtr = (unsigned short*)FIO_MASKA_S; // PF Interrupt A Mask Set register
*smmrPtr = 0x00f0; // unmask PF4-PF7
smmrPtr = (unsigned short*)FIO_POLAR; // PF Polarity register
*smmrPtr = 0x0000; // all PFs are active high
smmrPtr = (unsigned short*)FIO_EDGE; // PF Interrupt Sensitivity register
*smmrPtr = 0x00f0; // make PF4-PF7 edge triggered ints
// check the silicon revision
// any rev silicon of 1.0 or later will have system interrupts enabled by writing
// a 1 to the appropriate bit in SIC_IMASK. Revisions prior to 1.0 are enabled
// by writing a 0.
mmrPtr = (unsigned int*)0xffc048c0;
nSiRev = *mmrPtr & 0xf0000000;
if (nSiRev)
{
mmrPtr = (unsigned int*)SIC_IMASK; // System Interrupt Control Mask register
*mmrPtr = *mmrPtr | 0x00020000; // enable peripheral #17 (PF Int A), set the bit
}
else
{
mmrPtr = (unsigned int*)SIC_IMASK; // System Interrupt Control Mask register
*mmrPtr = *mmrPtr & 0xfffdffff; // enable peripheral #17 (PF Int A), clear the bit
}
}
////////////////////////////
// timer interrupt handler
////////////////////////////
EX_INTERRUPT_HANDLER(timerISR)
{
unsigned short *smmrPtr;
int i;
smmrPtr = (unsigned short*)FIO_FLAG_C; // PF Clear register
*smmrPtr = timerCounter & 0x00000001; // turn off the timer LED
++timerCounter; // count the timer interrupts
for (i = 0; i < 4; ++i)
{
// update the first location of the 32kB and 64kB arrays with the timerCounter
array1[i] = ((char*)&timerCounter)[i];
array2[i] = ((char*)&timerCounter)[i];
}
smmrPtr = (unsigned short*)FIO_FLAG_S; // PF Set register
*smmrPtr = timerCounter & 0x00000001; // turn on the timer LED
}
////////////////////////
// interrupt12 handler
////////////////////////
EX_INTERRUPT_HANDLER(int12ISR)
{
unsigned short *smmrPtr;
unsigned long *memPtr;
unsigned long length;
unsigned short temp;
bool bFlag = false;
unsigned long i;
// figure out which push button was pressed and increment the appropriate
// counter...if PF4-PF7 did not generate the interrupt then just return
smmrPtr = (unsigned short*)FIO_FLAG_S; // PF Set register
temp = *smmrPtr; // get which flags are currently set
// check if PF4 is set
if (temp & 0x0010)
{
++pf4Counter;
bFlag = true;
}
// check if PF5 is set
if (temp & 0x0020)
{
++pf5Counter;
bFlag = true;
}
// check if PF6 is set
if (temp & 0x0040)
{
++pf6Counter;
// update each word of the array
for (i = 0; i < sizeof(pf6Array)/4; ++i)
{
pf6Array[i] = pf6Counter;
}
bFlag = true;
}
// check if PF7 is set
if (temp & 0x0080)
{
++pf7Counter;
// update each word of the array
for (i = 0; i < sizeof(pf7Array)/4; ++i)
{
pf7Array[i] = pf7Counter;
}
bFlag = true;
}
// if any of PF4-PF7 were set then update the LEDs
if (bFlag)
{
// toggle the LED
smmrPtr = (unsigned short*)FIO_FLAG_C; // PF Clear register
*smmrPtr = 0x00f0; // clear the input pins (PF4-PF7)
smmrPtr = (unsigned short*)FIO_FLAG_C; // PF Clear register
*smmrPtr = (pbCounter & 0x00000001) << 3;
++pbCounter; // inc the global pushbutton counter
smmrPtr = (unsigned short*)FIO_FLAG_S; // PF Set register
*smmrPtr = (pbCounter & 0x00000001) << 3;
}
else
{
smmrPtr = (unsigned short*)FIO_FLAG_C; // PF Clear register
*smmrPtr = 0xff00; // clear the "other" input pins
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -