Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_helpers.h File Reference

Netcruzer Helper Functions. More...

Go to the source code of this file.

Macros

#define delay_us(us)   nzDelayUs(us)
 
#define delay_ms(ms)   nzDelayMs(ms)
 
#define DWORD_BYTE0(dw)   ((unsigned char)((dw)&0xFF))
 
#define DWORD_BYTE1(dw)   ((unsigned char)(((dw)>>8)&0xFF))
 
#define DWORD_BYTE2(dw)   ((unsigned char)(((dw)>>16)&0xFF))
 
#define DWORD_BYTE3(dw)   ((unsigned char)(((dw)>>24)&0xFF))
 
#define nzDwordGetLowByte_MACRO(dw)   ((unsigned char)((dw)&0xFF))
 
#define DWORD_LOW_BYTE(dw)   ((unsigned char)((dw)&0xFF))
 
#define nzDwordGetHighByte_MACRO(dw)   ((unsigned char)(((dw)>>24)&0xFF))
 
#define DWORD_HIGH_BYTE(dw)   ((unsigned char)(((dw)>>24)&0xFF))
 
#define nzDwordGetLowWord_MACRO(dw)   ((unsigned char)((dw)&0xFFFF))
 
#define DWORD_LOW_WORD(dw)   ((unsigned char)((dw)&0xFFFF))
 
#define nzDwordGetHighWord_MACRO(dw)   ((unsigned char)(((dw)>>16)&0xFFFF))
 
#define DWORD_HIGH_WORD(dw)   ((unsigned char)(((dw)>>16)&0xFFFF))
 
#define WORD_BYTE0(w)   ((unsigned char)((w)&0xFF))
 
#define WORD_BYTE1(w)   ((unsigned char)(((w)>>8)&0xFF))
 
#define nzWordGetLowByte_MACRO(w)   ((unsigned char)((w)&0xFF))
 
#define WORD_LOW_BYTE(w)   ((unsigned char)((w)&0xFF))
 
#define nzWordGetHighByte_MACRO(w)   ((unsigned char)(((w)>>8)&0xFF))
 
#define WORD_HIGH_BYTE(w)   ((unsigned char)(((w)>>8)&0xFF))
 
#define nzByteGetLowNibble_MACRO(b)   ((unsigned char)((b)&0x0F))
 
#define BYTE_LOW_NIBBLE(b)   ((unsigned char)((b)&0x0F))
 
#define nzByteGetHighNibble_MACRO(b)   ((unsigned char)(((b)>>4)&0x0F))
 
#define BYTE_HIGH_NIBBLE(b)   ((unsigned char)(((b)>>4)&0x0F))
 
#define nzWordSetHighByte_MACRO(w, b)   (((BYTE*)&(w))[1] = (b))
 
#define nzWordSetLowByte_MACRO(w, b)   (((BYTE*)&(w))[0] = (b))
 
#define nzWordFromBytes_MACRO(msb, lsb)   ( ((WORD)((msb)<<8)) | ((WORD)(lsb)) )
 
#define WORD_FROM_BYTES(msb, lsb)   ( ((WORD)((msb)<<8)) | ((WORD)(lsb)) )
 

Functions

void nzDelayMs (WORD msDelay)
 
void nzDelayUs (WORD usDelay)
 
BYTE nzAsciiHexToByte (WORD_VAL asciiChars)
 
BYTE nzAsciiHexNibbleToByte (BYTE c)
 
WORD_VAL nzByteToAsciiHex (BYTE b)
 
void nzByteToAsciiHexStr (BYTE b, char *dst)
 
BYTE nzLowNibbleToAsciiHex (BYTE b)
 
BYTE nzHighNibbleToAsciiHex (BYTE b)
 
BYTE nzBitCount (WORD x)
 
BYTE nzBitCountArr (BYTE *p, BYTE size)
 
WORD nzParseDecHexWord (const char *str, BYTE *retFlags)
 
WORD nzParseHexDecWord (const char *str, BYTE *retFlags)
 
void nzUintToAsciiStr (WORD val, BYTE *buf)
 
void nzFlashWriteConst (DWORD adr, BYTE newVal)
 
void nzMemCpy (BYTE *pDst, BYTE *pSrc, WORD count)
 
void nzMemCpyNoCheck (BYTE *pDst, BYTE *pSrc, WORD count)
 
void nzMemCpyDecNoCheck (BYTE *pDst, BYTE *pSrc, WORD count)
 

Detailed Description

Netcruzer Helper Functions.

Author
Modtronix Engineering
Compiler:
MPLAB XC16 compiler

Description

This module contains code for implementing a variable size circular buffers. The size can be configured at run time. It is not limited to a power of 2 value.

Configuration

The following defines are used to configure this module, and should be placed in projdefs.h. Note that all items marked [-DEFAULT-] are defaults, and do not have to be placed in projdefs.h if they contain desired configuration! For details, see Project Configuration.

// *********************************************************************
// ----------- Netcruzer Helper Configuration (nz_helpers.h) -----------
// *********************************************************************

Software License Agreement

The software supplied herewith is owned by Modtronix Engineering, and is protected under applicable copyright laws. The software supplied herewith is intended and supplied to you, the Company customer, for use solely and exclusively on products manufactured by Modtronix Engineering. The code may be modified and can be used free of charge for commercial and non commercial applications. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license.

THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.

File History

2010-11-24, David H. (DH):

  • Initial version

Macro Definition Documentation

#define nzByteGetHighNibble_MACRO (   b)    ((unsigned char)(((b)>>4)&0x0F))
Function:
BYTE nzByteGetHighNibble_MACRO(BYTE b)

Gets the high nibble (4 bits) of given BYTE.

Parameters
bThe BYTE
Returns
The high nibble (bits 4-7)
#define nzByteGetLowNibble_MACRO (   b)    ((unsigned char)((b)&0x0F))
Function:
BYTE nzByteGetLowNibble_MACRO(BYTE b)

Gets the low nibble (4 bits) of given BYTE.

Parameters
bThe BYTE
Returns
The low nibble (bits 0-3)
#define nzDwordGetHighByte_MACRO (   dw)    ((unsigned char)(((dw)>>24)&0xFF))
Function:
BYTE nzDwordGetHighByte_MACRO(DWORD dw)

Get the high BYTE of the given DWORD.

Parameters
dwThe DWORD
Returns
Returns the high BYTE (bits 24-31)
#define nzDwordGetHighWord_MACRO (   dw)    ((unsigned char)(((dw)>>16)&0xFFFF))
Function:
WORD nzDwordGetHighWord_MACRO(DWORD dw)

Get the high WORD of the given DWORD.

Parameters
dwThe DWORD
Returns
Returns the high WORD (bits 16-31)
#define nzDwordGetLowByte_MACRO (   dw)    ((unsigned char)((dw)&0xFF))
Function:
BYTE nzDwordGetLowByte_MACRO(DWORD dw)

Get the low BYTE of the given DWORD.

Parameters
dwThe DWORD
Returns
Returns the low BYTE (bits 0-7)
#define nzDwordGetLowWord_MACRO (   dw)    ((unsigned char)((dw)&0xFFFF))
Function:
WORD nzDwordGetLowWord_MACRO(DWORD dw)

Get the low WORD of the given DWORD.

Parameters
dwThe DWORD
Returns
Returns the low WORD (bits 0-15)
#define nzWordFromBytes_MACRO (   msb,
  lsb 
)    ( ((WORD)((msb)<<8)) | ((WORD)(lsb)) )
Function:
WORD nzWordFromBytes_MACRO(BYTE msb, BYTE lsb)

Convert the given 2 BYTEs to a single WORD

Parameters
lsbThe BYTE to use for the LSB (bits 0-7) of the WORD
msbThe BYTE to use for the MSB (bits 8-15) of the WORD
Returns
The WORD consisting of the given BYTEs
#define nzWordGetHighByte_MACRO (   w)    ((unsigned char)(((w)>>8)&0xFF))

Get the high BYTE of the given WORD.

Parameters
wThe WORD
Returns
Returns the high BYTE (bits 8-15)
#define nzWordGetLowByte_MACRO (   w)    ((unsigned char)((w)&0xFF))
Function:
BYTE nzWordGetLowByte_MACRO(WORD w)

Get the low BYTE of the given WORD.

Parameters
wThe WORD
Returns
Returns the low BYTE (bits 0-7)
#define nzWordSetHighByte_MACRO (   w,
 
)    (((BYTE*)&(w))[1] = (b))
Function:
void nzWordSetHighByte_MACRO(WORD w, BYTE b)

Sets the high BYTE of given WORD.

Parameters
wThe WORD to set the high byte of
bThe BYTE to set in the given WORD
#define nzWordSetLowByte_MACRO (   w,
 
)    (((BYTE*)&(w))[0] = (b))
Function:
void nzWordSetLowByte_MACRO(WORD w, BYTE b)

Sets the low BYTE of given WORD.

Parameters
wThe WORD to set the low byte of
bThe BYTE to set in the given WORD

Function Documentation

BYTE nzAsciiHexNibbleToByte ( BYTE  c)

Get the hex value for the given hex encoded character (0-9, a-f). The returned value will be from 0 - 15. If the given byte is not a hex encoded character, 0xff is returned!

Parameters
cA hex encoded character, value values are 0-9, A-F and a-f
Returns
Returns a value from 0 to 15 representing the hex value of given parameter. Or, 0xff if error.
BYTE nzAsciiHexToByte ( WORD_VAL  asciiChars)

Converts a "2 character ASCII hex" string to a single packed byte.

Parameters
asciiChars- WORD_VAL where asciiChars.v[0] is the ASCII value for the lower nibble and asciiChars.v[1] is the ASCII value for the upper nibble. Each must range from '0'-'9', 'A'-'F', or 'a'-'f'.
Returns
Returns byte representation of given "2 character ASCII byte"
BYTE nzBitCount ( WORD  x)

Counts number of bits in x

param x: Given parameter to count bit's that are 1

BYTE nzBitCountArr ( BYTE *  p,
BYTE  size 
)

Counts number of bits in given array that are set to 1

Parameters
p,:Pointer to array whos bits we must count
size,:Number of bytes in array we must count bits of
WORD_VAL nzByteToAsciiHex ( BYTE  b)

Converts a byte to a "2-character uppercase ASCII hex" value. The 2 bytes are returned in the bytes of the returned WORD_VAL. For example, nzByteToAsciiHex(0xAE) will return 'E' in the LSB, and 'A' in the MSB of the returned WORD.

Parameters
bThe byte containing the nibble to convert
Returns
The LSB of the returned word contains the ASCII value for the lower nibble. The MSB of the returned word contains the ASCII value for the upper nibble.
void nzByteToAsciiHexStr ( BYTE  b,
char *  dst 
)

Converts a byte to a "2-character uppercase ASCII hex" string. For example, nzByteToAsciiHex(0xAE) will return the string "AE" (LSB='E'). Will always return 2 characters, for example 0x5 will return "05". A NULL termination is NOT added to the back of the returned 2 characters.

Parameters
bThe byte containing the nibble to convert
dstPointer to buffer to write the result to. Will always write 2 characters, without a NULL termination.
void nzDelayMs ( WORD  msDelay)

Delays for the given number of Milli Seconds. Value must be in the range from 1 to 65,536.

Parameters
msDelayMilli Seconds to delay
Examples:
uart_debug_demo1/main.c, and uart_xc16_demo1/main.c.
void nzDelayUs ( WORD  usDelay)

Delays for the given number of Micro Seconds. Value must be in the range from 2 to 65,536.

Parameters
usDelayMicro Seconds to delay
void nzFlashWriteConst ( DWORD  adr,
BYTE  newVal 
)

Writes the given value to the "const BYTE" at the given address. The FLASH location is NOT erased, so bits of variable at given address can only change from 1's to 0's. When erasing a FLASH, all bits are set to 1's. For example, if variable is 0xff, it can be written with any value. If varialbe is 0, it can not be changed (all bits are already 0's).

When placing data in program memory with "const BYTE" syntax, only the lower words of instructions are used. The upper word of instructions has a usable byte, and the "phantom byte", which are both not used. When using the __builtin_tbladdress() function, we get the word address used in program memory for given "const BYTE" variable.

  • "const BYTE" variable with even address is stored in LSB of lower word of instruction.
    To update this byte, place it in LSB of __builtin_tblwtl(). Supply varialbes address to __builtin_tblwtl().
    MSB of __builtin_tblwtl() will update variable one address higher, at address: ((varialbe's address)+1).
    __builtin_tblwth() is not used for "const BYTE" variable.
  • "const BYTE" with odd address is stored in MSB of lower word of instruction.
    To update this byte, place it in MSB of __builtin_tblwtl(). Supply ((varialbe's address)&0xfffe) to __builtin_tblwtl().
    LSB of __builtin_tblwtl() will update variable one address lower, at address: ((varialbe's address)&0xfffe).
    __builtin_tblwth() is not used for "const BYTE" variable.
Parameters
adr,:Address of "const BYTE" variable to update. Use "__builtin_tbladdress(&var)" to get address
newVal,:New value that should be assigned to given "const BYTE" variable. Only 0 bits will be written.
BYTE nzHighNibbleToAsciiHex ( BYTE  b)

Converts the lower nibble of a binary value to a hexadecimal ASCII byte. For example, nzLowNibbleToAsciiHex(0xAE) will return 'E'.

Parameters
bThe byte containing the nibble to convert
Returns
The converted ASCII hex character for the given nibble
BYTE nzLowNibbleToAsciiHex ( BYTE  b)

Converts the upper nibble of a binary value to a hexadecimal ASCII byte. For example, nzHighNibbleToAsciiHex(0xAE) will return 'A'.

Parameters
bThe byte containing the nibble to convert
Returns
The converted ASCII hex character for the given nibble
void nzMemCpy ( BYTE *  pDst,
BYTE *  pSrc,
WORD  count 
)

Copy src array to destination array, countReg times, using fast REPEAT instructions. This function will check that count is NOT 0. If it is, it will return with no error.

Parameters
pDstDestination array
pSrcSource array
countNumber of bytes to write, ENSURE IT IS NOT 0 !
void nzMemCpyDecNoCheck ( BYTE *  pDst,
BYTE *  pSrc,
WORD  count 
)

Save as nzMemCpyNoCheck(), but increments pointers backwards.
!!! IMPORTANT !!!

  • Ensure countReg IS NOT 0
  • Ensure countReg is a WORD (or cast to a WORD)

When returning, pDst and pScr will both BE DECREMENTED countReg times !!!

void nzMemCpyNoCheck ( BYTE *  pDst,
BYTE *  pSrc,
WORD  count 
)

Copy src array to destination array, countReg times, using fast REPEAT instructions.


!!! IMPORTANT !!!

  • Ensure countReg IS NOT 0

This function does NOT check that count is NOT 0. If it is, it will cause an error!

Parameters
pDstDestination array
pSrcSource array
countNumber of bytes to write, ENSURE IT IS NOT 0 !
WORD nzParseDecHexWord ( const char *  str,
BYTE *  retFlags 
)

Get the value of the given decimal string. String value can have leading 0s. Maximum value is 65,535. Following possible leading format specifiers are checked for:

  • x = Upper Case Hex value to follow

Some examples: "255" will return 0x00FF "x010" will return 0x0010

Parameters
strString containing hex value
retFlagsA value returned from this function.
  • bit 0-3 is the number of bytes parsed by this function
  • bits 4-7 are reserved for flags, like if the parsed value is a byte or word for example.
Returns
Parsed word value
WORD nzParseHexDecWord ( const char *  str,
BYTE *  retFlags 
)

Get the value of the given Upper Case Hex, or decimal string. String value can have leading 0s. Maximum value is 0xFFFF. Following possible leading format specifiers are checked for:

  • d = Decimal value to follow

Some examples: "A100" will return 0xA100 "d010" will return 0xA

Parameters
strString containing hex value
retFlagsA value returned from this function.
  • bit 0-3 is the number of bytes parsed by this function
  • bits 4-7 are reserved for flags, like if the parsed value is a byte or word for example.
Returns
Parsed word value
void nzUintToAsciiStr ( WORD  val,
BYTE *  buf 
)

Converts a 16-bit unsigned integer to a null-terminated decimal string.

Parameters
val- The number to be converted
buf- Pointer in which to store the converted string