Skip to content

Commit db12f26

Browse files
committed
MPAE-19442 Changed main to printf with error handling, working as expected
1 parent d4b2bff commit db12f26

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

pic18f57q43-i2c-proximity-sensor-polled-printf-errors.X/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern void DELAY_milliseconds(uint16_t milliseconds);
4545

4646
uint8_t VCNL4200_Initialize(void);
4747
uint8_t VCNL4200_ProximityRead(uint16_t* proximityValue);
48+
const uint8_t* I2CErrorToString(uint8_t error);
4849

4950
uint16_t proximityValue; // proximity sensor (VCNL4200) result
5051
uint8_t errorState = I2C_ERROR_NONE;
@@ -67,9 +68,32 @@ uint8_t VCNL4200_ProximityRead(uint16_t* proximityValue)
6768
}
6869
*proximityValue = proximityResponse.value;
6970
}
71+
errorState = I2C_Host.ErrorGet();
72+
73+
// Error states:
74+
// 0x00 = I2C_ERROR_NONE
75+
// 0x01 = I2C_ERROR_ADDR_NACK
76+
// 0x02 = I2C_ERROR_DATA_NACK
77+
// 0x03 = I2C_ERROR_BUS_COLLISION
7078
return errorState;
7179
}
7280

81+
const uint8_t* I2CErrorToString(uint8_t error)
82+
{
83+
switch (error)
84+
{
85+
case I2C_ERROR_NONE:
86+
return "I2C_ERROR_NONE";
87+
case I2C_ERROR_ADDR_NACK:
88+
return "I2C_ERROR_ADDR_NACK";
89+
case I2C_ERROR_DATA_NACK:
90+
return "I2C_ERROR_DATA_NACK";
91+
case I2C_ERROR_BUS_COLLISION:
92+
return "I2C_ERROR_BUS_COLLISION";
93+
default:
94+
return "UNKNOWN_ERROR";
95+
}
96+
}
7397

7498
uint8_t VCNL4200_Initialize(void)
7599
{
@@ -92,18 +116,24 @@ uint8_t VCNL4200_Initialize(void)
92116
I2C_Host.Tasks();
93117
}
94118
}
119+
errorState = I2C_Host.ErrorGet();
95120
return errorState;
96121
}
97122

98123
int main(void)
99124
{
100125
SYSTEM_Initialize();
101126
errorState = VCNL4200_Initialize(); // Initializes the proximity sensor (VCNL4200) over the I2C bus
127+
printf("I2C proximity sensor initialize status: %d - %s\r\n", errorState, I2CErrorToString(errorState));
102128

103129
while(1)
104130
{
105131
DELAY_milliseconds(250);
106132
errorState = VCNL4200_ProximityRead(&proximityValue);
133+
if (errorState != I2C_ERROR_NONE)
134+
{
135+
printf("The proximity sensor error: %d - %s\r\n", errorState, I2CErrorToString(errorState));
136+
}
107137
printf("The proximity value is: %d\r\n", proximityValue);
108138
IO_LED_Toggle();
109139
IO_Debug_Toggle();

pic18f57q43-i2c-proximity-sensor-polled-printf-errors.X/mcc-manifest-autosave.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
manifest_file_version: 1.0.0
44
project: pic18f57q43-i2c-proximity-sensor-polled-printf-errors
5-
creation_date: 2025-04-04T11:50:39.073+02:00[Europe/Berlin]
5+
creation_date: 2025-04-04T12:03:18.693+02:00[Europe/Berlin]
66
operating_system: Windows 11
77
mcc_mode: IDE
88
mcc_mode_version: v6.25

pic18f57q43-i2c-proximity-sensor-polled-printf-errors.X/mcc-manifest-generated-success.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
manifest_file_version: 1.0.0
44
project: pic18f57q43-i2c-proximity-sensor-polled-printf-errors
5-
creation_date: 2025-04-04T11:50:39.033+02:00[Europe/Berlin]
5+
creation_date: 2025-04-04T12:03:18.661+02:00[Europe/Berlin]
66
operating_system: Windows 11
77
mcc_mode: IDE
88
mcc_mode_version: v6.25

pic18f57q43-i2c-proximity-sensor-polled-printf-errors.X/mcc_generated_files/examples/I2C-Host-Example/i2c_host_example.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @brief Generated file for
3131
* Example: 4. I2C Proximity Sensor
3232
* Implementation: Polled
33-
* Visualization: Printf
33+
* Visualization: Printf with error handling
3434
* MCU Device family: PIC18F/18F
3535
*/
3636
/* ------------------------------------------------------------------
@@ -47,6 +47,7 @@ extern void DELAY_milliseconds(uint16_t milliseconds);
4747
4848
uint8_t VCNL4200_Initialize(void);
4949
uint8_t VCNL4200_ProximityRead(uint16_t* proximityValue);
50+
const uint8_t* I2CErrorToString(uint8_t error);
5051
5152
uint16_t proximityValue; // proximity sensor (VCNL4200) result
5253
uint8_t errorState = I2C_ERROR_NONE;
@@ -69,9 +70,32 @@ uint8_t VCNL4200_ProximityRead(uint16_t* proximityValue)
6970
}
7071
*proximityValue = proximityResponse.value;
7172
}
73+
errorState = I2C_Host.ErrorGet();
74+
75+
// Error states:
76+
// 0x00 = I2C_ERROR_NONE
77+
// 0x01 = I2C_ERROR_ADDR_NACK
78+
// 0x02 = I2C_ERROR_DATA_NACK
79+
// 0x03 = I2C_ERROR_BUS_COLLISION
7280
return errorState;
7381
}
7482
83+
const uint8_t* I2CErrorToString(uint8_t error)
84+
{
85+
switch (error)
86+
{
87+
case I2C_ERROR_NONE:
88+
return "I2C_ERROR_NONE";
89+
case I2C_ERROR_ADDR_NACK:
90+
return "I2C_ERROR_ADDR_NACK";
91+
case I2C_ERROR_DATA_NACK:
92+
return "I2C_ERROR_DATA_NACK";
93+
case I2C_ERROR_BUS_COLLISION:
94+
return "I2C_ERROR_BUS_COLLISION";
95+
default:
96+
return "UNKNOWN_ERROR";
97+
}
98+
}
7599
76100
uint8_t VCNL4200_Initialize(void)
77101
{
@@ -94,18 +118,24 @@ uint8_t VCNL4200_Initialize(void)
94118
I2C_Host.Tasks();
95119
}
96120
}
121+
errorState = I2C_Host.ErrorGet();
97122
return errorState;
98123
}
99124
100125
int main(void)
101126
{
102127
SYSTEM_Initialize();
103128
errorState = VCNL4200_Initialize(); // Initializes the proximity sensor (VCNL4200) over the I2C bus
129+
printf("I2C proximity sensor initialize status: %d - %s\r\n", errorState, I2CErrorToString(errorState));
104130
105131
while(1)
106132
{
107133
DELAY_milliseconds(250);
108134
errorState = VCNL4200_ProximityRead(&proximityValue);
135+
if (errorState != I2C_ERROR_NONE)
136+
{
137+
printf("The proximity sensor error: %d - %s\r\n", errorState, I2CErrorToString(errorState));
138+
}
109139
printf("The proximity value is: %d\r\n", proximityValue);
110140
IO_LED_Toggle();
111141
IO_Debug_Toggle();

0 commit comments

Comments
 (0)