This question is a bit of a long read to explain. Recently, I have been using Microchip’s XC8 C compiler with a PIC16F18875 board (Curiosity HPC). In this case, I used MCC (Microchip’s code configurator) to configure I2C and it is the first time that I have ever used MCC, but my question is not about the merits or demerits of MCC.
The generated code produces a function:
that can be called to write to an I2C device. This function is working as I think it should and I am using a cheap I2C LCD as the device.
Notice that the function does not return a variable. Status of the transaction, however, is ascertainable by the *pstatus which points to a status variable that is updated during the transaction.
The variable is an enum type defined as follows:
So, error checking can be accomplished by examining I2C1_MESSAGE_STATUS after calling the I2C1_MasterWrite. OK fine, I'm getting to my question….
I have a function LCD_writexy that sends a string to the LCD at position x (0-15) and y (0-1) – all standard stuff. LCD_writexy() returns I2C1_MESSAGE_STATUS. The intention is to simply test if the I2C transaction was successful.
If LCD_writexy() is called with an out of bounds value, it returns without further action like this:
With this set up, this code works as I expect it to (e.g., code below writes '99' on the LCD):
I would like to keep going with it like that but wonder how “kosher” it is to do so and that is my question. The only alternative that I can easily see is to add my own status values to the enum declaration. I am cautious about doing that because it is MCC generated code and I don’t want to uncover unanticipated effects.
Enum types annoy me sometimes because it seems like they are ints except when they are not. Any advice?
The generated code produces a function:
C:
void I2C1_MasterWrite(
uint8_t *pdata,
uint8_t length,
uint16_t address,
I2C1_MESSAGE_STATUS *pflag)
{
Notice that the function does not return a variable. Status of the transaction, however, is ascertainable by the *pstatus which points to a status variable that is updated during the transaction.
The variable is an enum type defined as follows:
C:
typedef enum
{
I2C1_MESSAGE_COMPLETE,
I2C1_MESSAGE_FAIL,
I2C1_MESSAGE_PENDING,
I2C1_STUCK_START,
I2C1_MESSAGE_ADDRESS_NO_ACK,
I2C1_DATA_NO_ACK,
I2C1_LOST_STATE
} I2C1_MESSAGE_STATUS;
I have a function LCD_writexy that sends a string to the LCD at position x (0-15) and y (0-1) – all standard stuff. LCD_writexy() returns I2C1_MESSAGE_STATUS. The intention is to simply test if the I2C transaction was successful.
If LCD_writexy() is called with an out of bounds value, it returns without further action like this:
C:
// bounds check
if ( (y > 1) || (x > 15) ) {
return (99); // error return
}
C:
if( LCD_writexy(18,1,"oob")==99 )
{
LCD_writexy(0,1,"99");
}
Enum types annoy me sometimes because it seems like they are ints except when they are not. Any advice?