bugfixes i2c bus access

This commit is contained in:
Klaus K Wilting 2019-02-02 10:35:20 +01:00
parent ab5cd0b0a2
commit e7a416cd7a
4 changed files with 89 additions and 88 deletions

View File

@ -40,7 +40,7 @@
#define SCREEN_MODE (0x80) #define SCREEN_MODE (0x80)
// I2C bus access control // I2C bus access control
#define I2C_MUTEX_LOCK() xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == pdTRUE #define I2C_MUTEX_LOCK() xSemaphoreTake(I2Caccess, (3 * DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) == pdTRUE
#define I2C_MUTEX_UNLOCK() xSemaphoreGive(I2Caccess) #define I2C_MUTEX_UNLOCK() xSemaphoreGive(I2Caccess)
// Struct holding devices's runtime configuration // Struct holding devices's runtime configuration

View File

@ -59,6 +59,9 @@ void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) {
void init_display(const char *Productname, const char *Version) { void init_display(const char *Productname, const char *Version) {
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
// show startup screen // show startup screen
uint8_t buf[32]; uint8_t buf[32];
u8x8.begin(); u8x8.begin();
@ -69,14 +72,14 @@ void init_display(const char *Productname, const char *Version) {
u8x8.draw2x2String(0, 0, Productname); u8x8.draw2x2String(0, 0, Productname);
u8x8.setInverseFont(0); u8x8.setInverseFont(0);
u8x8.draw2x2String(2, 2, Productname); u8x8.draw2x2String(2, 2, Productname);
delay(1500); delay(500);
u8x8.clear(); u8x8.clear();
u8x8.setFlipMode(1); u8x8.setFlipMode(1);
u8x8.setInverseFont(1); u8x8.setInverseFont(1);
u8x8.draw2x2String(0, 0, Productname); u8x8.draw2x2String(0, 0, Productname);
u8x8.setInverseFont(0); u8x8.setInverseFont(0);
u8x8.draw2x2String(2, 2, Productname); u8x8.draw2x2String(2, 2, Productname);
delay(1500); delay(500);
u8x8.setFlipMode(0); u8x8.setFlipMode(0);
u8x8.clear(); u8x8.clear();
@ -94,7 +97,8 @@ void init_display(const char *Productname, const char *Version) {
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : ""); (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
u8x8.printf("ESP Rev.%d\n", chip_info.revision); u8x8.printf("ESP Rev.%d\n", chip_info.revision);
u8x8.printf("%dMB %s Flash\n", spi_flash_get_chip_size() / (1024 * 1024), u8x8.printf("%dMB %s Flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "int." : "ext."); (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "int."
: "ext.");
#endif // VERBOSE #endif // VERBOSE
u8x8.print(Productname); u8x8.print(Productname);
@ -105,9 +109,8 @@ void init_display(const char *Productname, const char *Version) {
u8x8.println("DEVEUI:"); u8x8.println("DEVEUI:");
os_getDevEui((u1_t *)buf); os_getDevEui((u1_t *)buf);
DisplayKey(buf, 8, true); DisplayKey(buf, 8, true);
#endif // HAS_LORA
delay(3000); delay(3000);
#endif // HAS_LORA
u8x8.clear(); u8x8.clear();
u8x8.setPowerSave(!cfg.screenon); // set display off if disabled u8x8.setPowerSave(!cfg.screenon); // set display off if disabled
u8x8.draw2x2String(0, 0, "PAX:0"); u8x8.draw2x2String(0, 0, "PAX:0");
@ -120,6 +123,9 @@ void init_display(const char *Productname, const char *Version) {
u8x8.setCursor(0, 5); u8x8.setCursor(0, 5);
u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit); u8x8.printf(!cfg.rssilimit ? "RLIM:off " : "RLIM:%d", cfg.rssilimit);
I2C_MUTEX_UNLOCK(); // release i2c bus access
}
} // init_display } // init_display
void refreshtheDisplay() { void refreshtheDisplay() {

View File

@ -95,12 +95,15 @@ int if482_init(void) {
IF482.begin(HAS_IF482); IF482.begin(HAS_IF482);
// use external rtc 1Hz clock for triggering IF482 telegram // use external rtc 1Hz clock for triggering IF482 telegram
if (I2C_MUTEX_LOCK()) {
Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz); Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock);
I2C_MUTEX_UNLOCK();
} else {
ESP_LOGE(TAG, "I2c bus busy - IF482 initialization error");
return 0;
}
pinMode(RTC_INT, INPUT_PULLUP); pinMode(RTC_INT, INPUT_PULLUP);
ESP_LOGI(TAG, "IF482 generator initialized");
return 1; return 1;
} // if482_init } // if482_init

View File

@ -92,13 +92,9 @@ void setup() {
char features[100] = ""; char features[100] = "";
if (I2Caccess == NULL) // Check that semaphore has not already been created I2Caccess = xSemaphoreCreateMutex(); // for access management of i2c bus
{
I2Caccess = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use
// to manage the i2c bus
if ((I2Caccess) != NULL) if ((I2Caccess) != NULL)
xSemaphoreGive((I2Caccess)); // Flag the i2c bus available for use xSemaphoreGive((I2Caccess)); // Flag the i2c bus available for use
}
// disable brownout detection // disable brownout detection
#ifdef DISABLE_BROWNOUT #ifdef DISABLE_BROWNOUT
@ -188,31 +184,6 @@ void setup() {
0); // CPU core 0); // CPU core
#endif #endif
// initialize RTC
#ifdef HAS_RTC
strcat_P(features, " RTC");
assert(rtc_init());
setSyncProvider(&get_rtctime);
if (timeStatus() != timeSet)
ESP_LOGI(TAG, "Unable to sync system time with RTC");
else
ESP_LOGI(TAG, "RTC has set the system time");
setSyncInterval(TIME_SYNC_INTERVAL_RTC * 60);
#endif // HAS_RTC
#ifdef HAS_IF482
strcat_P(features, " IF482");
assert(if482_init());
ESP_LOGI(TAG, "Starting IF482 Generator...");
xTaskCreatePinnedToCore(if482_loop, // task function
"if482loop", // name of task
2048, // stack size of task
(void *)1, // parameter of the task
3, // priority of the task
&IF482Task, // task handle
0); // CPU core
#endif // HAS_IF482
// initialize wifi antenna // initialize wifi antenna
#ifdef HAS_ANTENNA_SWITCH #ifdef HAS_ANTENNA_SWITCH
strcat_P(features, " ANT"); strcat_P(features, " ANT");
@ -312,7 +283,7 @@ void setup() {
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
strcat_P(features, " OLED"); strcat_P(features, " OLED");
DisplayState = cfg.screenon; DisplayState = cfg.screenon;
init_display(PRODUCTNAME, PROGVERSION); init_display(PRODUCTNAME, PROGVERSION); // note: blocking call
// setup display refresh trigger IRQ using esp32 hardware timer // setup display refresh trigger IRQ using esp32 hardware timer
// https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ // https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/
@ -350,6 +321,18 @@ void setup() {
strcat_P(features, " LPPPKD"); strcat_P(features, " LPPPKD");
#endif #endif
// initialize RTC
#ifdef HAS_RTC
strcat_P(features, " RTC");
assert(rtc_init());
setSyncProvider(&get_rtctime);
if (timeStatus() != timeSet)
ESP_LOGI(TAG, "Unable to sync system time with RTC");
else
ESP_LOGI(TAG, "RTC has set the system time");
setSyncInterval(TIME_SYNC_INTERVAL_RTC * 60);
#endif // HAS_RTC
// show compiled features // show compiled features
ESP_LOGI(TAG, "Features:%s", features); ESP_LOGI(TAG, "Features:%s", features);
@ -436,11 +419,20 @@ void setup() {
setSyncInterval(TIME_SYNC_INTERVAL_GPS * 60); setSyncInterval(TIME_SYNC_INTERVAL_GPS * 60);
#endif #endif
// start RTC interrupt
#if defined HAS_IF482 && defined RTC_INT #if defined HAS_IF482 && defined RTC_INT
strcat_P(features, " IF482");
assert(if482_init());
ESP_LOGI(TAG, "Starting IF482 Generator...");
xTaskCreatePinnedToCore(if482_loop, // task function
"if482loop", // name of task
2048, // stack size of task
(void *)1, // parameter of the task
3, // priority of the task
&IF482Task, // task handle
0); // CPU core
// setup external interupt for active low RTC INT pin // setup external interupt for active low RTC INT pin
assert(IF482Task != NULL); // has if482loop task started? assert(IF482Task != NULL); // has if482loop task started?
ESP_LOGI(TAG, "Starting IF482 output...");
attachInterrupt(digitalPinToInterrupt(RTC_INT), IF482IRQ, FALLING); attachInterrupt(digitalPinToInterrupt(RTC_INT), IF482IRQ, FALLING);
#endif #endif