Backing up settings from READ_PERI_REG

AgentSmithers
Posts: 159
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Backing up settings from READ_PERI_REG

Postby AgentSmithers » Mon Dec 24, 2018 10:27 am

Anyone have a sniblet or any recommendations for restoring the PIN function after being overwritten with PIN_FUNC_SELECT. I would like to restore its function if the pin fails to detect it on that particular GPIO so it does not interrupt anything else going on the pin after it's restored.

My guess is that I need to call READ_PERI_REG(PIN_NAME) beforehand and store the bitvalues but after reviewing the definition I'm not quite sure. It would be appreciated if anyone had some guidance.
I am using the code below to autodetect which pin the DHT22 is on.

Code: Select all

void AutoDetect()
{
   //This code may need to get the values before setting the Pin_Func_Select to restore after
   //I think the code for Pin_Func_Selct macro is in Eagle_soc.h
   /*
   #define PIN_FUNC_SELECT(PIN_NAME, FUNC)  do { \
   WRITE_PERI_REG(PIN_NAME,         \
        (READ_PERI_REG(PIN_NAME)            \
    &  (~(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S))) \
        |  ( (((FUNC&BIT2)<<2)|(FUNC&0x3))<<PERIPHS_IO_MUX_FUNC_S) ); \
   } while (0)
   */
   ThermostatConfig.programmed = 1;
   ThermostatConfig.ThermostatType = MySensor_DHT22;
   ThermostatConfig.PowerMethod = 0;

   int i;

   for(i = 15; i >= 0; i--)
   {
      os_printf("Detecting Therm on GPIO %d", i);
      PIN_FUNC_SELECT(g_pin_muxes[i], g_pin_funcs[i]);
      PIN_PULLUP_EN(g_pin_muxes[i]);
      ThermostatConfig.GPIO = i;
      if (pollDHTData())
      {
         os_printf("Therm Detected on GPIO %d", i);
         return;
      }
      if (i == 12)
      {
         i = 6; //Skip over bad Pins
      }
      if (i == 3)
      {
         i = 1; //Pin 2 and 3 crashes the esp when using this method
      }
   }

   ThermostatConfig.programmed = 0;
   ThermostatConfig.ThermostatType = 0;
   ThermostatConfig.PowerMethod = 0;
}

Who is online

Users browsing this forum: No registered users and 12 guests