📄 uartexample.c
字号:
/*********************************************************************************
Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved.
This software is proprietary and confidential. By using this software you agree
to the terms of the associated Analog Devices License Agreement.
*********************************************************************************/
/*********************************************************************
Include files
*********************************************************************/
#include <services/services.h> // system service includes
#include <drivers/adi_dev.h> // device manager includes
#include <drivers/adi_uart.h> // UART device driver includes
/*********************************************************************
Static memory
*********************************************************************/
#define NUM_BUFFERS (2) // number of buffers to use
static u8 Data[NUM_BUFFERS]; // storage for data sent and received
static ADI_DEV_1D_BUFFER Buffer[NUM_BUFFERS]; // the actual buffers
static ADI_DEV_DEVICE_HANDLE UARTHandle; // handle to the UART driver
/*********************************************************************
Function prototypes
*********************************************************************/
static void CallbackFunction(void *ClientHandle, u32 Event, void *pArg); // callback function
static void InitSystemServices(void); // system services initialization
/*********************************************************************
Function: main
Description: Using the UART driver, this program creates a simple
echo program. Characters received over the UART are
sent back to the terminal.
*********************************************************************/
void main(void) {
u32 i; // counter
u32 Result; // return code
ADI_DEV_MANAGER_HANDLE DevMgrHandle; // handle to device manager
// memory for device manager
static u8 DevMgrData[ADI_DEV_BASE_MEMORY + (ADI_DEV_DEVICE_MEMORY * 1)];
// UART configuration table
ADI_DEV_CMD_VALUE_PAIR UARTConfiguration[] = {
{ ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *)ADI_DEV_MODE_CHAINED },
{ ADI_UART_CMD_SET_DATA_BITS, (void *)8 },
{ ADI_UART_CMD_ENABLE_PARITY, (void *)FALSE },
{ ADI_UART_CMD_SET_STOP_BITS, (void *)1 },
{ ADI_UART_CMD_SET_BAUD_RATE, (void *)57600 },
{ ADI_DEV_CMD_END, NULL },
};
// initialize the system services
InitSystemServices();
// initialize the device manager, parameters are
// pointer to data for the device manager to use
// size of the data in bytes
// location where the number of devices that can be managed will be stored
// location where the device manager handle will be stored
// parameter for adi_int_EnterCriticalRegion() function (always NULL for standalone and VDK)
Result = adi_dev_Init(DevMgrData,
sizeof(DevMgrData),
&i,
&DevMgrHandle,
NULL);
// open the UART driver, parameters are
// device manager handle
// entry point of device driver to open
// the device number (0th UART)
// client handle (callback will always be passed this value)
// location where UART device driver handle will be stored
// direction the device is to be opened for
// DMA manager handle (NULL cause we're not using DMA)
// DCB handle (NULL cause we want live callbacks)
// address of callback function
Result = adi_dev_Open(DevMgrHandle,
&ADIUARTEntryPoint,
0,
(void *)0x12345678,
&UARTHandle,
ADI_DEV_DIRECTION_BIDIRECTIONAL,
NULL,
NULL,
CallbackFunction);
// configure the UART driver, parameters are
// UART device driver handle
// command ID stating that we're passing in a command table
// address of the command table
Result = adi_dev_Control(UARTHandle,
ADI_DEV_CMD_TABLE,
UARTConfiguration);
// create 1D buffers for the UART to process
for (i = 0; i < NUM_BUFFERS; i++) {
Buffer[i].Data = &Data[i]; // address of the data
Buffer[i].ElementCount = 1; // one element in the buffer
Buffer[i].ElementWidth = 1; // element is one byte wide
Buffer[i].CallbackParameter = &Buffer[i]; // pass the buffer address to the callback
Buffer[i].pNext = &Buffer[i+1]; // point to the next buffer in the chain
Buffer[i].pAdditionalInfo = (void *)0; // (for this example, 0 means an inbound buffer
}
Buffer[NUM_BUFFERS - 1].pNext = NULL; // terminate the chain of buffers
// give the buffers to the driver to fill with data, parameters are
// UART device driver handle
// 1D buffers
// Address of first buffer
Result = adi_dev_Read(UARTHandle,
ADI_DEV_1D,
(ADI_DEV_BUFFER *)Buffer);
// enable data flow, parameters are
// UART device driver handle
// SET_DATAFLOW command ID
// TRUE (enable dataflow)
Result = adi_dev_Control(UARTHandle,
ADI_DEV_CMD_SET_DATAFLOW,
(void *)TRUE);
// spin
while (1);
}
/*********************************************************************
Function: CallbackFunction
Description: Invoked when a buffer has been processed. When an
inbound buffer has been processed, meaning a character
has been received, the buffer is sent back to the terminal
by calling the adi_dev_Write() function. When an outbound
buffer has been processed, meaning the character has been
sent to the terminal, the buffer is then ready for new data
and is sent to the adi_dev_Read() function.
Parameters:
ClientHandle - value that was passed in adi_dev_Open()
Event - event that occured (see adi_dev.h). In
this example, event will always be a
buffer processed event.
pArg - event specific parameter (see adi_dev.h)
In this example, pArg will always contain
the buffer's address.
*********************************************************************/
static void CallbackFunction(void *ClientHandle, u32 Event, void *pArg) {
ADI_DEV_1D_BUFFER *pBuffer; // pointer to the buffer
u32 Result; // return code
// point to the buffer
pBuffer = (ADI_DEV_1D_BUFFER *)pArg;
// CASEOF (event type)
switch (Event) {
// CASE (buffer processed)
case ADI_DEV_EVENT_BUFFER_PROCESSED:
// IF (an inbound buffer)
if (pBuffer->pAdditionalInfo == 0) {
// tag it as an outbound buffer and the only buffer in the chain
pBuffer->pAdditionalInfo = (void *)1;
pBuffer->pNext = NULL;
// send the buffer out, parameters are
// UART device driver handle
// 1D buffer
// buffer address
Result = adi_dev_Write(UARTHandle,
ADI_DEV_1D,
(ADI_DEV_BUFFER *)pBuffer);
// ELSE
} else {
// tag it as an inbound buffer and the only buffer in the chain
pBuffer->pAdditionalInfo = (void *)0;
pBuffer->pNext = NULL;
// fill the buffer with more data, parameters are
// UART device driver handle
// 1D buffer
// buffer address
Result = adi_dev_Read(UARTHandle,
ADI_DEV_1D,
(ADI_DEV_BUFFER *)pBuffer);
// ENDIF
}
break;
// ENDCASE
}
// return
}
/*********************************************************************
Function: InitSystemServices
Description: Initializes the necessary system services.
*********************************************************************/
void InitSystemServices(void) {
u32 i;
u32 Result;
ADI_EBIU_SDRAM_BANK_VALUE bank_size;
ADI_EBIU_SDRAM_BANK_VALUE bank_width;
u32 cl_threshold = 100; // set cl threshold to 100 Mhz
ADI_EBIU_TIMING_VALUE twrmin = {1,{7500, ADI_EBIU_TIMING_UNIT_PICOSEC}}; // set min TWR to 1 SCLK cycle + 7.5ns
ADI_EBIU_TIMING_VALUE refresh = {8192,{64, ADI_EBIU_TIMING_UNIT_MILLISEC}}; // set refresh period to 8192 cycles in 64ms
ADI_EBIU_TIME trasmin = {44, ADI_EBIU_TIMING_UNIT_NANOSEC}; // set min TRAS to 44ns
ADI_EBIU_TIME trpmin = {20, ADI_EBIU_TIMING_UNIT_NANOSEC}; // set min TRP to 20ns
ADI_EBIU_TIME trcdmin = {20, ADI_EBIU_TIMING_UNIT_NANOSEC}; // set min TRCD to 20ns
bank_size.value.size = ADI_EBIU_SDRAM_BANK_64MB; // set bank size to 64MB
bank_width.value.width = ADI_EBIU_SDRAM_BANK_COL_10BIT; // set column address width to 10-Bit
ADI_EBIU_COMMAND_PAIR ezkit_sdram[] = {
{ ADI_EBIU_CMD_SET_SDRAM_BANK_SIZE, (void*)&bank_size },
{ ADI_EBIU_CMD_SET_SDRAM_BANK_COL_WIDTH,(void*)&bank_width },
{ ADI_EBIU_CMD_SET_SDRAM_CL_THRESHOLD, (void*)cl_threshold },
{ ADI_EBIU_CMD_SET_SDRAM_TRASMIN, (void*)&trasmin },
{ ADI_EBIU_CMD_SET_SDRAM_TRPMIN, (void*)&trpmin },
{ ADI_EBIU_CMD_SET_SDRAM_TRCDMIN, (void*)&trcdmin },
{ ADI_EBIU_CMD_SET_SDRAM_TWRMIN, (void*)&twrmin },
{ ADI_EBIU_CMD_SET_SDRAM_REFRESH, (void*)&refresh },
{ ADI_EBIU_CMD_END, 0 }
};
ADI_PWR_COMMAND_PAIR ezkit_power[] = {
{ ADI_PWR_CMD_SET_PROC_VARIANT,(void*)ADI_PWR_PROC_BF537SKBC1600 }, // 600Mhz ADSP-BF533 variant
{ ADI_PWR_CMD_SET_PACKAGE, (void*)ADI_PWR_PACKAGE_MBGA }, // in MBGA packaging, as on all EZ-KITS
{ ADI_PWR_CMD_SET_VDDEXT, (void*)ADI_PWR_VDDEXT_330 }, // external voltage supplied to the voltage regulator is 3.3V
{ ADI_PWR_CMD_SET_CLKIN, (void*)ADI_PWR_CLKIN_EZKIT_BF537 }, // the CLKIN frequency 25 Hz
{ ADI_PWR_CMD_END, 0 }
};
// initialize the interrupt manager, parameters are
// pointer to memory for interrupt manager to use
// memory size (in bytes)
// location where the number of secondary handlers that can be supported will be stored
// parameter for adi_int_EnterCriticalRegion (always NULL for VDK and standalone systems)
Result = adi_int_Init(NULL,
0,
&i,
NULL);
// initialize the EBIU, parameters are
// address of table containing SDRAM parameters
// 0 - always 0 when EBIU initialized before power service
Result = adi_ebiu_Init(ezkit_sdram,
0);
// initialize power, parameters are
// address of table containing processor information
Result = adi_pwr_Init( ezkit_power );
// return
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -