PIC24 Support Libraries
Macros | Functions | Variables
pic24_util.h File Reference
#include <stdint.h>
#include "pic24_chip.h"
#include "pic24_libconfig.h"

Go to the source code of this file.

Macros

#define REPORT_ERROR(msg)   reportError(ERROR_FILE_LINE(msg))
 
Bitfield to struct conversion

Given a bitfield struct representing an SFR, convert this to a word (uint16_t) or to the high and low bytes (uint8_t) of that word. Note: though this looks ugly, it introduces no unexpected compiler overhead at -O1. See u16_INTTREGlast for an example.

#define BITS2WORD(sfrBitfield)   ( *((uint16_t*) &sfrBitfield) )
 Convert a bitfield to a word (uint16_t).
 
#define BITS2BYTEL(sfrBitfield)   ( ((uint8_t*) &sfrBitfield)[0] )
 Return the low byte (as a uint8_t) of a bitfield.
 
#define BITS2BYTEH(sfrBitfield)   ( ((uint8_t*) &sfrBitfield)[1] )
 Return the high byte (as a uint8_t) of a bitfield.
 

Functions

void reportError (const char *szErrorMessage)
 
uint32_t readProgramMemory (uint32_t u32_address)
 
void checkDeviceAndRevision (void)
 
void checkOscOption (void)
 
void printResetCause (void)
 
void configBasic (const char *psz_helloMsg)
 
uint16_t compute_brg (uint32_t u32_fcy, uint16_t u16_brgh, uint32_t u32_baudrate)
 
uint32_t roundFloatToUint32 (float f_x)
 
uint16_t roundFloatToUint16 (float f_x)
 
void configHeartbeat (void)
 
void doHeartbeat (void)
 
void toggleHeartbeat (void)
 

Variables

_PERSISTENT const char * sz_lastTimeoutError
 
uint32_t u32_heartbeatCount
 

Detailed Description

This file contains miscellaneous functions that that do not fall under any particular category. See pic24_util.c for details on how these functions were implemented.

Definition in file pic24_util.h.

Macro Definition Documentation

◆ REPORT_ERROR

#define REPORT_ERROR (   msg)    reportError(ERROR_FILE_LINE(msg))

Report an error on reset via reportError, also printing the file and line number where this macro was called via a call to ERROR_FILE_LINE.

Parameters
msgError message to report

Definition at line 72 of file pic24_util.h.

Referenced by inChar(), isCharReady(), and outChar().

Function Documentation

◆ checkDeviceAndRevision()

void checkDeviceAndRevision ( void  )

Determines the device and revision of the PIC this program is executing on. This information is then output via the default UART. A warning message is issued if this program was not compiled for the chip it is running on.

Definition at line 208 of file pic24_util.c.

Referenced by printResetCause().

◆ checkOscOption()

void checkOscOption ( void  )

Reports the oscillator currently in use to the default serial port. See FNOSC_SEL for a table of which chips support which clocks.

Definition at line 280 of file pic24_util.c.

Referenced by printResetCause().

◆ compute_brg()

uint16_t compute_brg ( uint32_t  u32_fcy,
uint16_t  u16_brgh,
uint32_t  u32_baudrate 
)

Choose UART baud rate, based on u32_fcy. NOTE: Be careful about using BRGH=1 - this uses only four clock periods to sample each bit and can be very intolerant of baud rate % error - you may see framing errors. BRGH is selected via the DEFAULT_BRGH1 define above.

Definition at line 474 of file pic24_util.c.

Referenced by compute_brg(), and configUART1().

◆ configBasic()

void configBasic ( const char *  sz_helloMsg)

Perform basic chip configuration:

  • Configure the heartbeat
  • Configure the clock
  • Configure UART1
  • Determine and print the cause of reset
  • Print a hello message.
Parameters
sz_helloMsgHello message to print.

Definition at line 434 of file pic24_util.c.

◆ configHeartbeat()

void configHeartbeat ( void  )

Configures a GPIO pin for use with the heartbeat and sets up the heartbeat counter.

See also
doHeartbeat

Definition at line 89 of file pic24_util.c.

Referenced by configBasic().

◆ doHeartbeat()

void doHeartbeat ( void  )

This heartbeat function should be called repeatedly in any sort of blocking wait loop. It will periodically toggle an LED after HEARTBEAT_MAX increments.

Definition at line 104 of file pic24_util.c.

Referenced by checkRxErrorUART1(), configUART1(), doErasePageFlash(), doWriteRowFlash(), inChar1(), outChar1(), WAIT_UNTIL_CONVERSION_COMPLETE_ADC1(), and WAIT_UNTIL_TRANSMIT_COMPLETE_UART1().

◆ printResetCause()

void printResetCause ( void  )

Determines and prints the cause of a CPU reset. This should be called when the chip first starts up. For an example, see the configBasic function.

Definition at line 329 of file pic24_util.c.

Referenced by configBasic().

◆ readProgramMemory()

uint32_t readProgramMemory ( uint32_t  u32_address)

Reads a 24-bit program memory word at the given address.

Parameters
u32_addressAddress of program memory to read.
Returns
The 24-bit program memory word at u32_address. The upper 8 bits are 0.

Definition at line 196 of file pic24_util.c.

Referenced by checkDeviceAndRevision().

◆ reportError()

void reportError ( const char *  sz_errorMessage)

Report a critical error by recording a message in sz_lastError then resetting the chip, assuming hat printResetCause will be called during chip initialization.

Parameters
sz_errorMessageError message to report.
See also
REPORT_ERROR

Definition at line 183 of file pic24_util.c.

Referenced by _DefaultInterrupt(), checkOscOption(), checkRxErrorUART1(), and configUART1().

◆ roundFloatToUint16()

uint16_t roundFloatToUint16 ( float  f_x)

Round a floating-point number to the nearest integer.

Parameters
f_xFloating-point value to round
Returns
The nearest uint16_t to f_x.

Definition at line 460 of file pic24_util.c.

Referenced by compute_brg(), msToU16Ticks(), and usToU16Ticks().

◆ roundFloatToUint32()

uint32_t roundFloatToUint32 ( float  f_x)

Round a floating-point number to the nearest integer.

Parameters
f_xFloating-point value to round
Returns
The nearest uint32_t to f_x.

Definition at line 448 of file pic24_util.c.

Referenced by ticksToMs(), ticksToNs(), ticksToUs(), and usToU32Ticks().

◆ toggleHeartbeat()

void toggleHeartbeat ( void  )

A function which toggles the heartbeat LED.

See also
doHeartbeat

Definition at line 116 of file pic24_util.c.

Referenced by doHeartbeat().

Variable Documentation

◆ sz_lastTimeoutError

_PERSISTENT const char* sz_lastTimeoutError

Persistent storage for a timeout error, to be reported if a watchdog reset occurs.

Definition at line 134 of file pic24_util.c.

Referenced by convertADC1(), getI2C1(), printResetCause(), putI2C1(), putNoAckCheckI2C1(), rstartI2C1(), startI2C1(), and stopI2C1().

◆ u32_heartbeatCount

uint32_t u32_heartbeatCount

The current heartbeat count. When this value reaches HEARTBEAT_MAX, the heartbeat LED is toggled by doHeartbeat().

See also
doHeartbeat

Definition at line 71 of file pic24_util.c.

Referenced by configHeartbeat(), delayAndUpdateHeartbeatCount(), and doHeartbeat().