/**
 * \file LDMOD16X.C
 * 
 * \author Axel Wolf, SCI Cupertino
 * \author Dr. Jens Barrenscheen, HL MC PD, Munich
 * \author Hubert Piontek, University of Ulm
 *
 * Routine for loading data into message objects.
 *
 * Most of this is based von the Infineon/Siemens Application Note AP2922.
 * Some additions made at University of Ulm, February 2003.
 *
 * This CAN driver is mainly meant to be used by the publisher/subscriber
 * library developed at the University of Ulm, 1999-2003.
 */

/*********************************************************************
 * Program name:	"LDMOD16X.C"                                     *
 * Compiler used:	BSO/Tasking C166                                 *
 * Task:		Source File for procedure ld_modata_16x              *
 *			belonging to Siemens ApNote AP2922                       *
 *			"'C' CAN Driver Routines for the C166 family"            *
 *                                                                   * 
 * Last modifications:	April 28nd 1997                              *
 * Version:		1.0                                                  *
 * Authors:		Axel Wolf,  SCI Cupertino                            *
 *			Dr. Jens Barrenscheen, HL MC PD, Munich                  *
 *********************************************************************/

extern unsigned int  *id_ptr_16x[16];		///< pointer to message id's (UAReg)
extern unsigned char *db0_ptr_16x[16];		///< pointer to 'databyte 0's
extern unsigned int  *msgctrl_ptr_16x[16];	///< pointer to message control registers
extern unsigned char *msgconf_ptr_16x[16];	///< pointer to message configuration registers
extern unsigned char dir_bit_16x[16];		///< DIR bits MO 1...15
extern unsigned char xtd_bit_16x[16];		///< XTD bits MO 1...15
extern unsigned char dlc_16x[16];			///< data byte lengths MO 1...15

/**
 * Use this function to load the data just before sending the message object. The message object must of course be
 * defined before using def_mo_16x().
 *
 * \param nr Number of the message object to load (1..14)
 * \param *upl_data_ptr Pointer to the data to be copied into the message object.
 * 
 * \warning *upl_data_ptr should point to exactly the amount of data specified when defining the message object.
 *          Otherwise, the message might contain useless "random" data.
 */
void ld_modata_16x(unsigned char nr, unsigned char *upl_data_ptr)
{

	unsigned char i;
	unsigned char *dummy_dbptr;

	if ((nr<15) && (nr)) {
		*msgctrl_ptr_16x[nr]=0xfaff;		/* set CPUUPD, NEWDAT */
	   	dummy_dbptr=db0_ptr_16x[nr];		/* load dummy ptr (db 0) */
		/* move data bytes from upload buffer to MO data bytes */
	   	for (i=0;i<dlc_16x[nr];i++) 
			*dummy_dbptr++ = *upl_data_ptr++;
		/* reset CPUUPD (NEWDAT is reset by CAN Module on Transm.) */
	   	*msgctrl_ptr_16x[nr]=0xf7ff;
	}
}