/*=========================================================================
|--------------------------------------------------------------------------
| Project : Cann Bootloader Sample Test Code
| File : can_bltest.c
|
| Edited by qjwang@ti.com for Hercules MCU CAN Bootloader Test
|
|--------------------------------------------------------------------------
| Redistribution and use of this file in source and binary forms, with
| or without modification, are permitted.
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
| FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
| COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
| BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
| ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
| POSSIBILITY OF SUCH DAMAGE.
========================================================================*/
//=========================================================================
// INCLUDES
//=========================================================================
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // Include file for Win32 time functions
#include <conio.h> // Include file for _getch/kbhit
#include <string.h>
#include "Nican.h"
#include "bl_commands.h"
#include "can_bltest.h"
#define CAN_CMD_SUCCESS 0x00
#define CAN_CMD_FAIL 0x01
//*****************************************************************************
//
// A buffer used to construct commands to be sent to the target device.
//
//*****************************************************************************
#define TMS570LS04
//#define TMS570LS31
//#define TMS570LS12
#define BUFFER_SIZE 2048
static unsigned char g_pucBuffer[256];
unsigned char image[BUFFER_SIZE*8];
unsigned long image_size;
static unsigned char data_in[BUFFER_SIZE];
static unsigned char data_out[BUFFER_SIZE];
//=========================================================================
// CONSTANTS
//=========================================================================
// Handle for Network Interface Object (receiver)
NCTYPE_OBJH RxHandle = 0;
// Handle for the CAN object (transmitter)
NCTYPE_OBJH TxHandle = 0;
/* NI-CAN handle */
NCTYPE_OBJH TxRx=0;
/* This function converts the absolute time obtained from ncReadMult into a string. */
void AbsTimeToString(NCTYPE_ABS_TIME *time, char *TimeString)
{
SYSTEMTIME stime;
FILETIME localftime;
FileTimeToLocalFileTime((FILETIME *)(time), &localftime);
FileTimeToSystemTime(&localftime, &stime);
sprintf(TimeString, "%02d:%02d:%02d.%03d",
stime.wHour, stime.wMinute, stime.wSecond,
stime.wMilliseconds);
}
/* This function is used to print a description of an NI-CAN error/warning. */
void PrintStat(NCTYPE_STATUS status, char *source)
{
char statusString[1024];
if (status != 0)
{
ncStatusToString(status, sizeof(statusString), statusString);
printf("\n%s\nSource = %s\n", statusString, source);
if (status < 0)
{
// On error, close object handles, then exit.
ncCloseObject(TxHandle);
ncCloseObject(RxHandle);
exit(1);
}
}
}
void delay(void) {
static volatile unsigned int delayval;
delayval = 10000000; // 100000 are about 10ms
while(delayval--);
}
void Get_BinaryData( char *filename )
{
FILE *file;
int trans_num = 0;
unsigned char *pImage;
image_size = 0;
pImage = &image[0];
// Open the file
file = fopen(filename, "rb");
if (!file) {
printf("Unable to open file '%s'\n", filename);
return;
}
while (!feof(file)) {
int num_write, count;
int i;
// Read from the file
num_write = fread((void *)pImage, 1, BUFFER_SIZE, file);
if (!num_write) break;
printf("*** Transaction #%02d\n", trans_num);
// Dump the data to the screen
printf("Data written to device:");
for (i = image_size; i < (image_size+num_write); ++i) {
if ((i&0x0f) == 0) printf("\n%04x: ", i);
printf("%02x ", image[i] & 0xff);
if (((i+1)&0x07) == 0) printf(" ");
}
printf("\n\n");
image_size += num_write;
pImage += num_write;
++trans_num;
}
cleanup:
fclose(file);
}
//=========================================================================
// MAIN PROGRAM
//=========================================================================
int main(int argc, char *argv[])
{
NCTYPE_STATUS Status;
NCTYPE_ATTRID AttrIdList[8];
NCTYPE_UINT32 AttrValueList[8];
NCTYPE_CAN_FRAME Transmit;
NCTYPE_UINT32 Baudrate = 125000;
NCTYPE_CAN_STRUCT ReceiveBuf[10];
NCTYPE_CAN_STRUCT WriteBuf[150];
NCTYPE_UINT32 ActualDataSize=0;
u32 i;
u16 j;
u8 k;
char Interface[6] = "CAN0";
char output[15];
char CharBuff[50];
/* Set XTD = 1 to send extended ID frames */
int XTD = 0;
int id = CAN_COMMAND_PING;
char data_length = 8;
char *filename;
int rcvID;
int rcvData;
int rcvDataLen;
unsigned char ulIdx;
unsigned short ulOffset;
unsigned long ulLength;
unsigned long ulAddress;
#if defined (TMS570LS04)
filename = "TMS570LS04Blinky.bin";
#else
filename = "TMS570LS31Blinky.bin";
#endif
/* Configure the CAN Network Interface Object */
AttrIdList[0] = NC_ATTR_BAUD_RATE;
AttrValueList[0] = Baudrate;
AttrIdList[1] = NC_ATTR_START_ON_OPEN;
AttrValueList[1] = NC_TRUE;
AttrIdList[2] = NC_ATTR_READ_Q_LEN;
AttrValueList[2] = 150;
AttrIdList[3] = NC_ATTR_WRITE_Q_LEN;
AttrValueList[3] = 0;
AttrIdList[4] = NC_ATTR_CAN_COMP_STD;
AttrValueList[4] = 0;
AttrIdList[5] = NC_ATTR_CAN_MASK_STD;
AttrValueList[5] = NC_CAN_MASK_STD_DONTCARE;
AttrIdList[6] = NC_ATTR_CAN_COMP_XTD;
AttrValueList[6] = 0;
AttrIdList[7] = NC_ATTR_CAN_MASK_XTD;
AttrValueList[7] = NC_CAN_MASK_XTD_DONTCARE;
Status = ncConfig(Interface, 8, AttrIdList, AttrValueList);
if (Status < 0)
{
PrintStat(Status, "ncConfig");
}
/* open the CAN Network Interface Object */
Status = ncOpenObject(Interface, &TxHandle);
if (Status < 0)
{
PrintStat(Status, "ncOpenObject");
}
/* open the CAN Network Interface Object */
Status = ncOpenObject(Interface, &RxHandle);
if (Status < 0)
{
PrintStat(Status, "ncOpenObject");
}
/* print out the instructions to the I/O window */
printf("\n\ninitialized successfuly on CAN0 ... \n\n");
/*==============================================================================
* COMMAND PING
*===============================================================================*/
Transmit.DataLength = 0;
Transmit.IsRemote = 0;
Transmit.ArbitrationId = CAN_COMMAND_PING;
Status= ncWrite(TxHandle, sizeof(Transmit), &Transmit);
if (Status < 0)
{
PrintStat(Status, "ncWrite");
}
Status = ncRead(RxHandle, sizeof(ReceiveBuf), (void *)ReceiveBuf);
if (Status < 0)
{
PrintStat(Status, "ncReadMult");
}
rcvID = ReceiveBuf[0].ArbitrationId;
rcvData = ReceiveBuf[0].Data[0];
rcvDataLen = ReceiveBuf[0].DataLength;
if((rcvID != 0x05a6) && (rcvData != 0))
{
printf(" - COMMAND_PIN
评论0
最新资源