Joined: 19 Dec 2012 Posts: 43 Location: Connecticut, USA
Reproducing a CCS convenience function in XC16?
Posted: Fri Sep 05, 2014 10:04 am
Hi all,
I've got an interesting situation on my hands and I'm hoping someone here might be familiar enough with the CCS built in functions to shed some light on it.
I'm working on some I2C code with the XC16 toolchain from microchip and I'm thinking it will be much easier for me to port over some already working code that has been written in CCS rather than having to do it over from scratch. Of course this means bringing over the CCS function that gives me the status of the I2C transaction.
The function in question I'm trying to reproduce is the I2C_ISR_STATE() function.
From what I can gather, this function could be reproduced in code as such:
Code:
uint8_t i2c_isr_state(void){
uint8_t state;
if (I2C1STATbits.D_A == 0){ /*address match received*/
if (I2C1STATbits.R_W == 1){
state = 0x80; /*Master is requesting data*/
} else {
state = 0; /*Master is sending data*/
}
I2C_ISR_State = state;
} else {
I2C_ISR_State++; /*Data Byte from master, increment state*/
state = I2C_ISR_State;
}
return state;
}
In this situation I2C_ISR_State is a global that is set to either 0 or 0x80 when an address is received and incremented from there for each successive byte. As far as I can see, I should never need to reset I2C_ISR_State outside this function as it should reset itself at the beginning of each I2C Transaction.
I'm running into issues getting the code in my new XC16 SI2C1 ISR to work reliably and I'm just trying to confirm that my new I2C_ISR_STATE function should (at least in theory) perform the same action as the one built into CCS (so I can at least rule it out as a potential issue).
Any insight is greatly appreciated!
KTrenholm
Joined: 19 Dec 2012 Posts: 43 Location: Connecticut, USA
Posted: Fri Sep 05, 2014 12:33 pm
Okay I resolved my issues I was having. The issue was in the layout of my ISR. The function I posted does seem to be a suitable port of the convenience function.
However I did turn the I2C_ISR_State global into a static local variable just to have one less global hanging around.
Code:
uint8_t i2c_isr_state(void){
static uint8_t i2c_state;
if (I2C1STATbits.D_A == 0){ /*address match received*/
if (I2C1STATbits.R_W == 1){
i2c_state = 0x80; /*Master is requesting data*/
} else {
i2c_state = 0; /*Master is sending data*/
}
} else {
i2c_state++;
}
return i2c_state;
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum