Menu

canlib

Mike Johnson

CANLIB

Library or example code that may be useful in writting other modules.

Common ECAN, EEPROM, Program Flash memory, FLiM routines An attempt to reduce the amount of editing when a change is made or bug fixed by having a library of common stuff.

Processor type P2480 or P4580 must be defined before including "lib.h" lib.c does this by defining macros in the workspace. Uses 4 MHz resonator and PLL for 16 MHz clock CAN bit rate of 125 Kbits/sec, Standard frame only, 4-12 bytes, 256-768 usec per frame ECAN in mode 2, 3 transmit buffers and 8 receive buffers

Some simple types and operations

  • BOOL A variable which can be set TRUE or FALSE.
  • BYTE An unsigned char.
  • LO(n) The low byte of a 16 or 32 bit variable.
  • HI(n) The 2nd byte of a 16 or 32 bit variable.
  • UP(n) The 3rd byte of a 24 or 32 bit variable.
  • LOHI(n) The 3rd byte of a 24 or 32 bit variable.
  • HIHI(n) The 4th byte of a 32 bit variable.
  • LOWD(n) The first 16 bits starting at the address.

ECAN global variables

BYTE CB_canid
7 bit CBUS node number

BYTE CB_priority
4 bit CBUS priority

BYTE CB_data[[8]](/apps/trac/mergcbus/changeset/8)
up to 8 bytes of data

BYTE CB_datalen
length of data

ECAN_MSG_FLAGS CB_msgFlags
message flags or'ed together

  • ECAN_STD_FRAME = 0b00000000,
  • ECAN_RTR_FRAME = 0b01000000,
  • ECAN_OVERFLOW = 0b00001000,
  • ECAN_INVALID_MSG = 0b00010000

Routines in the library

void wait(BYTE t)
A loop to wait t msec.

void ECANInitialize(void)
Initializes the ECAN hardware.

BYTE far * ECANBufferEmpty(void)
Checks for an empty transmit buffer.

BOOL ECANSendMessage(void)
Send an ECAN buffer from global variables. BYTE CB_canid; // 7 bit CBUS node number BYTE CB_priority; // 4 bit CBUS priority BYTE CB_data[[8]](/apps/trac/mergcbus/changeset/8); // up to 8 bytes of data BYTE CB_datalen; // length of data ECAN_MSG_FLAGS CB_msgFlags; // message flags or'ed together ECAN_STD_FRAME = 0b00000000, ECAN_RTR_FRAME = 0b01000000, ECAN_OVERFLOW = 0b00001000, ECAN_INVALID_MSG = 0b00010000

BOOL ECANReceiveMessage(void)
Receives an ECAN packet into the global variables.

BYTE EEPROMRead(unsigned int adr)
Reads from the EEPROM, allows for larger chips with more than 256 bytes.

void EEPROMWrite(unsigned int adr, BYTE data)
Write to EEPROM.

void ProgramMemoryRead?(unsigned long adr, BYTE len, BYTE * far buffer)
Reads a block of up to 255 bytes from program memory.

void ProgramMemoryWrite?(unsigned long adr, BYTE len, BYTE * far buffer)
Erase and Write an aligned block of up to 64 bytes, the length must be a multiple of 8 bytes.

void GetNewCANID(void)
Used by FlimSetup? to get a new CANID.

void FlimSetup?(BYTE callstate)
Called to initialize the nodenumber or after a duplicate CANID or nodenumber is detected. Callstate = 0 after reset, = 1 for setup button pushed, =2 for canid or nodenumber conflict.

Routines used by the library

void main(void)
Main entry point code

void hpinterrupt(void)
high priority interrupt routine

void lpinterrupt(void)
low priority interrupt routine

void GreenLED(BOOL on)
Turn on or off the green led

void YellowLED(BOOL on)
Turn on or off the yellow led

BOOL SetupButtonDown?(void)
Detect the setup button pushed


Related

Wiki: Home

MongoDB Logo MongoDB