Merge pull request #3412 from leptun/MK3_TWI_timeout

Hardware TWI timeout
This commit is contained in:
Alex Voinea 2022-02-17 09:26:47 +01:00 committed by GitHub
commit eae6b4e911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 12 deletions

View File

@ -4,12 +4,6 @@
#include <inttypes.h> #include <inttypes.h>
#if defined(__cplusplus)
extern "C" {
#endif //defined(__cplusplus)
extern uint8_t pat9125_PID1; extern uint8_t pat9125_PID1;
extern uint8_t pat9125_PID2; extern uint8_t pat9125_PID2;
@ -24,9 +18,4 @@ extern uint8_t pat9125_update(void); // update all sensor data
extern uint8_t pat9125_update_y(void); // update _y only extern uint8_t pat9125_update_y(void); // update _y only
extern uint8_t pat9125_update_bs(void); // update _b/_s only extern uint8_t pat9125_update_bs(void); // update _b/_s only
#if defined(__cplusplus)
}
#endif //defined(__cplusplus)
#endif //PAT9125_H #endif //PAT9125_H

View File

@ -24,6 +24,7 @@
#include "config.h" #include "config.h"
#include "fastio.h" #include "fastio.h"
#include "twi.h" #include "twi.h"
#include "Timer.h"
void twi_init(void) void twi_init(void)
@ -44,6 +45,9 @@ void twi_init(void)
void twi_disable(void) void twi_disable(void)
{ {
// disable TWI hardware.
TWCR = 0;
// deactivate internal pullups for twi. // deactivate internal pullups for twi.
WRITE(SDA_PIN, 0); WRITE(SDA_PIN, 0);
WRITE(SCL_PIN, 0); WRITE(SCL_PIN, 0);
@ -58,7 +62,13 @@ static void twi_stop()
static uint8_t twi_wait(uint8_t status) static uint8_t twi_wait(uint8_t status)
{ {
while(!(TWCR & _BV(TWINT))); ShortTimer timmy;
timmy.start();
while(!(TWCR & _BV(TWINT))) {
if (timmy.expired(TWI_TIMEOUT_MS)) {
return 2;
}
}
if(TW_STATUS != status) if(TW_STATUS != status)
{ {
twi_stop(); twi_stop();

View File

@ -26,6 +26,8 @@
#define TWI_FREQ 400000L #define TWI_FREQ 400000L
#endif #endif
#define TWI_TIMEOUT_MS 100
/* /*
* Function twi_init * Function twi_init
* Desc readys twi pins and sets twi bitrate * Desc readys twi pins and sets twi bitrate