Pass struct to function

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Pass struct to function

1,444件の閲覧回数
tnriley
Contributor III

I seem to be having some difficulty in passing a struct to a function. No errors generated by the make process but upon inspection the values inside the function are not equal to the values outside.

 

 

THE STRUCT:

struct sciData{
  uint8_t rxBuf[RSBuffSIZE]; // Receive buffer
  uint16_t RSindex; // receive buffer counter
  uint16_t RSread; // receive buffer counter

  uint32_t tempVal; //scratchpad calc storage space
  uint8_t tempChars[TBUFF_SIZE]; //temporary scratchpad for received chars
  //Data Transmit Parameters
  uint8_t txBuf[SendBuffSIZE]; /* Send buffer */
  uint16_t numTXBytes;
  uint16_t OutBytes; /* Number of bytes to send */
  uint16_t SentBytes; /* Number of bytes sent */
  uint16_t TRANSMIT; // transmit flag to let prog know interrupt
  // driven transmission in progress
  uint8_t tPtr; //pointer to current buffer item
  uint8_t dataReady; //flag to indicate return data ready
  uint8_t charCount; //number of chars received
  uint8_t commChar;
  uint16_t charsExpected;
};

 

 

STRUCT DEFINE IN MAIN:

struct sciData sci3Dat;

 

CALL FROM MAIN

//Send Mode change to Top Controller
sci3Dat.txBuf[0] = 'p';
sci3Dat.txBuf[1] = 'G';
sci3Dat.txBuf[2] = TC_START;
sci3Dat.numTXBytes = 3;

  (NOTE: At this point sci3Dat.numTXBytes = 3)

#ifdef SCI3
  SendSerialData3(&sci3Dat); //past end of data
  while(sci3Dat.TRANSMIT);
#endif

 

 

INSIDE THE FUNCTION:

/* routine called to start string transmission */
void SendSerialData3(struct sciData *sd3){
        (NOTE: At this point sd3->numTXBytes = 0)

   sd3->OutBytes = sd3->numTXBytes - 1;
   sd3->SentBytes = 0;
   sd3->TRANSMIT = 1; // We're transmitting data

   /* Set transmit buffer empty * transmit complete interrupts */
   INTR_OFF();
   SCI3CR2 = 0xEc;
   INTR_ON();
}

 

Other values are incorrect also, just included the numTXBytes as an example. Pointer to a function should be fairly simple right, but I am not sure where I have gone wrong here.

 

Any Ideas?

ラベル(1)
7 返答(返信)

1,211件の閲覧回数
tnriley
Contributor III

Stan,

Thanks, I will try that. I am new to the Codewarrior tools.

I set the project up with the small memory model option. Which I thought meant that it was a non paged contiguous space. What is the best source for documentation on learning and using the different memory models for CodeWarrior and the MC9S12?

Tim

0 件の賞賛

1,211件の閲覧回数
stanish
NXP Employee
NXP Employee

Hi,

Not sure which memory model you are using, but perhaps this issue is related to the fact your struct is located in a paged RAM and the function call doesn't pass the page number.

I'd suggest you to declare sciDATA pointer as a far pointer so it passes global address pointer instead:

void SendSerialData3(struct sciData  *far sd3)

Hope it helps.

Stan

0 件の賞賛

1,211件の閲覧回数
tnriley
Contributor III

Alice,

How is best to post files here?

Tim

0 件の賞賛

1,211件の閲覧回数
tnriley
Contributor III

Alice,

The chip is a Freescale MC9S12XDP512CAL. The CodeWarrior IDE version is 5.9.0 build 5294.

Sorry, I can't send the complete project.

Thanks,

Tim

0 件の賞賛

1,211件の閲覧回数
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Tim,

OK, it doesn't matter.

I create a new project about it , and copy these code into it , while when build there some error,

as to lack of some definition , for example INTR_OFF()...

So could you please also create a new project , then only copy these code related your question,

build with no error, check whether it can work well . If no, please share it to me this project .

BR

Alice

0 件の賞賛

1,211件の閲覧回数
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Tim,

Please tell me the chip part number and the version of CodeWarrior.

Also in order to check the problem , please share your project t me , thanks.

BR

Alice

0 件の賞賛

1,211件の閲覧回数
tnriley
Contributor III

I was looking at this some more and found the following

The value of sd3 inside the function void SendSerialData3(struct sciData *sd3) is reported as sd3=NULL by the Codewarrior IDE.

Whereas the value of sd3 in the function void out_serial3(struct sciData *sd3) (Shown Below) points to the correct struct sci3Dat defined back in main(). 

 

INSIDE THE FUNCTION CALLED BY THE ISR:

/* Send data routine. Called by sci interrupt handler */
void out_serial3(struct sciData *sd3){
   if(sd3->SentBytes > sd3->OutBytes){ // if last character transmitted
      INTR_OFF(); // turn off transmitter interrupts
      SCI3CR2 = 0x24; // Also turns off transmitter when last char is sent
      sd3->TRANSMIT = 0; // notify main program that tranmission is complete
      INTR_ON();
   }
   else
      SCI3DRL = sd3->txBuf[sd3->SentBytes++]; // stuff character in sci transmit buffer
   }

ISR SHOWING CALL TO out_serial3

/* interrupt handler for all SCI3 communications interrupts */
void interrupt com3(void){
   uint8_t scitest = SCI3SR1;
   if(scitest&BM_RDRF){ // if receive buffer full interrupt, call data receive routine
      in_serial3(&sci3Dat);
   }
   else{
      if(scitest&(BM_TDRE|BM_TC)){ // if transmit buffer empty or transmit complete interrupt,
         out_serial3(&sci3Dat); // call transmit data routine
      }
   }
}

 

The code for all three modules SendSerialData3, out_serial3, and com3 reside in the same file.

 Parameters to both modules are defined in the same manner (struct sciData *sd3) and calls are made in same fashion (&sci3Dat).

So the question is:  Why does sd3=NULL inside SendSerialData3, but contain the correct values inside out_serial3?