I2C Materwritebuf not working inside a function

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

   

I have the following piece of code which is working fine on the main thread

   

uint8 MOTOR_bufsize=3
uint8 xSpeedLSB = value & 0xff;
uint8 xSpeedMSB = value >> 8;
uint8 MOTOR_waarde[3] = {0x0, xSpeedLSB, xSpeedMSB};
//set MOTOR
I2C_MOTOR_MasterClearStatus(); /* Clear any previous status */
I2C_MOTOR_MasterWriteBuf(MOTOR_I2C_SLAVEADRES, MOTOR_waarde, MOTOR_bufsize, I2C_MOTOR_MODE_COMPLETE_XFER);

   

But as soon I put the same lines into a function, nothing happens when the function is called. The function is shown below;

   

void set_speed(axis, speed) {
if (axis == Xaxis) {
  uint8 channel = 0x0;
  }
uint8 bufsize=3;
uint8 SpeedLSB = speed & 0xff;
uint8 SpeedMSB = speed >> 8;
uint8 MOTOR_waarde[3] = {channel, SpeedLSB, SpeedMSB};
I2C_MOTOR_MasterClearStatus(); /* Clear any previous status */
I2C_MOTOR_MasterWriteBuf(MOTOR_I2C_SLAVEADRES, MOTOR_waarde, bufsize, I2C_MOTOR_MODE_COMPLETE_XFER);

}

   

the function is called by:

   

set_speed(Xaxis, value);

   

in which Xaxis is an integer (0) and value is an integer between 0 and 1023.

   

The function is initialized by:

   

void set_speed(int axis, int speed);

   

Can anybody help me on my way why my function does not work?

0 Likes
1 Solution
Anonymous
Not applicable

Hi,

   

It seems my 'problem' is solved. I just write this for possible future reference if somebody has the same question.

   

First of all, the UART goes to pins 12.6 and 12.7. It is ossible to route it to these pins in the topdesign (by possible, I mean these pins sho up in the list and they can be chosen) However, choosing these pins directly doesnt work. Don't know why yet. I fount a tutorial on internet showing I had to route Rx and Tx to 2.0 and 2.1 and the 'hardwire' these pins to 12.6 and 12.7. Works great!

   

Then for the freezing I2C communication; as soon I added a line below the function to output the result to the UART, the function worked fine. If I removed this line, the function froze again. To resolve it, I added 1 ms of delay as last line of the function. Again, don't know why, but it works. I think it would be better to have the function check for a complete transfer before the function is 'left'.

   

Thanks so far for your support

View solution in original post

0 Likes
7 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Interrupts enabled?

   

Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition.

   

When still stuck, upload project archive.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello Bob,

   

Interrupts are enabled at the start of the main thread. For now, I did not include any error handling. I included the archive in this reply. The lines of code to set the speed have been commented out on the main thread (lines 52 - 58) on line 59 the function to set the speed is called.

   

The uploaded fie is a testfile which increases and decreases the output (0 - 10V) and reverses direction. I'm now at the point where I want to move repeating pieces of code to functions.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see anything wrong. Check the returned value from MasterClearStatus() and MasterWriteBuf().

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Ok, I'll try to hookup error handling to a puTTy screen. Didn't find a tutorial on that one for Psoc5 LP yet, but will let you know as soon as I got it working!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

When you are using a CY8CKIT-059 you need to specify port P12_7 and P12_6 for Tx and Rx for your UART to connect to PuTTY.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I'm using Freesoc2 / Psoc 5LP, but I think the same goes for this module

0 Likes
Anonymous
Not applicable

Hi,

   

It seems my 'problem' is solved. I just write this for possible future reference if somebody has the same question.

   

First of all, the UART goes to pins 12.6 and 12.7. It is ossible to route it to these pins in the topdesign (by possible, I mean these pins sho up in the list and they can be chosen) However, choosing these pins directly doesnt work. Don't know why yet. I fount a tutorial on internet showing I had to route Rx and Tx to 2.0 and 2.1 and the 'hardwire' these pins to 12.6 and 12.7. Works great!

   

Then for the freezing I2C communication; as soon I added a line below the function to output the result to the UART, the function worked fine. If I removed this line, the function froze again. To resolve it, I added 1 ms of delay as last line of the function. Again, don't know why, but it works. I think it would be better to have the function check for a complete transfer before the function is 'left'.

   

Thanks so far for your support

0 Likes