diff --git a/esp32test/Esp32DeepSleepTest/src/DS2438.cpp b/esp32test/Esp32DeepSleepTest/src/DS2438.cpp deleted file mode 100644 index 791b76d..0000000 --- a/esp32test/Esp32DeepSleepTest/src/DS2438.cpp +++ /dev/null @@ -1,286 +0,0 @@ -/* - * DS2438.cpp - * - * by Joe Bechter - * - * (C) 2012, bechter.com - * - * All files, software, schematics and designs are provided as-is with no warranty. - * All files, software, schematics and designs are for experimental/hobby use. - * Under no circumstances should any part be used for critical systems where safety, - * life or property depends upon it. You are responsible for all use. - * You are free to use, modify, derive or otherwise extend for your own non-commercial purposes provided - * 1. No part of this software or design may be used to cause injury or death to humans or animals. - * 2. Use is non-commercial. - * 3. Credit is given to the author (i.e. portions © bechter.com), and provide a link to the original source. - * - */ - -#include "DS2438.h" - -// DSROM FIELDS -#define DSROM_FAMILY 0 -#define DSROM_CRC 7 - -#define DS2438MODEL 0x26 - -DS2438::DS2438(OneWire *ow, float currentShunt = 1.0f) { - _ow = ow; - _currentShunt = currentShunt; -}; - -void DS2438::begin(){ - DeviceAddress searchDeviceAddress; - - _ow->reset_search(); - memset(searchDeviceAddress,0, 8); - _temperature = 0; - _voltageA = 0.0; - _voltageB = 0.0; - _error = true; - _mode = (DS2438_MODE_CHA | DS2438_MODE_CHB | DS2438_MODE_TEMPERATURE); - - deviceFound = false; // Reset the number of devices when we enumerate wire devices - - while (_ow->search(searchDeviceAddress)) { - if (validAddress(searchDeviceAddress)) { - if (validFamily(searchDeviceAddress)) { - memcpy(_address,searchDeviceAddress,8); - DEFAULT_PAGE0(defaultConfig); - writePage(0, defaultConfig); - deviceFound = true; - } - } - } -} - -bool DS2438::isFound(){ - return deviceFound; -} - -bool DS2438::validAddress(const uint8_t* deviceAddress) { - return (_ow->crc8(deviceAddress, 7) == deviceAddress[DSROM_CRC]); -} - -bool DS2438::validFamily(const uint8_t* deviceAddress) { - switch (deviceAddress[DSROM_FAMILY]) { - case DS2438MODEL: - return true; - default: - return false; - } -} - -void DS2438::update() { - uint8_t data[9]; - - _error = true; - if(!isFound()){ - return; - } - - if (_mode & DS2438_MODE_CHA || _mode == DS2438_MODE_TEMPERATURE) { - boolean doTemperature = _mode & DS2438_MODE_TEMPERATURE; - if (!startConversion(DS2438_CHA, doTemperature)) { - Serial.println("Error starting temp conversion ds2438 channel a"); - return; - } - if (!readPage(0, data)){ - - Serial.println("Error reading zero page ds2438 channel a"); - return; - } - Serial.print(data[0],16); - Serial.print(" "); - Serial.print(data[1],16); - Serial.print(" "); - Serial.print(data[2],16); - Serial.print(" "); - Serial.print(data[3],16); - Serial.print(" "); - Serial.print(data[4],16); - Serial.print(" "); - Serial.print(data[5],16); - Serial.print(" "); - Serial.print(data[6],16); - Serial.print(" "); - Serial.println(data[7],16); - - - if (doTemperature) { - _temperature = (double)(((((int16_t)data[2]) << 8) | (data[1] & 0x0ff)) >> 3) * 0.03125; - } - if (_mode & DS2438_MODE_CHA) { - _voltageA = (((data[4] << 8) & 0x00300) | (data[3] & 0x0ff)) / 100.0; - } - } - if (_mode & DS2438_MODE_CHB) { - boolean doTemperature = _mode & DS2438_MODE_TEMPERATURE && !(_mode & DS2438_MODE_CHA); - if (!startConversion(DS2438_CHB, doTemperature)) { - Serial.println("Error starting temp conversion channel b ds2438"); - return; - } - if (!readPage(0, data)){ - Serial.println("Error reading zero page ds2438 channel b"); - return; - } - if (doTemperature) { - int16_t upperByte = ((int16_t)data[2]) << 8; - int16_t lowerByte = data[1] >> 3; - int16_t fullByte = (upperByte | lowerByte); - _temperature = ((double)fullByte) * 0.03125; - } - _voltageB = (((data[4] << 8) & 0x00300) | (data[3] & 0x0ff)) / 100.0; - } - - int16_t upperByte = ((int16_t)data[6]) << 8; - int16_t lowerByte = data[5]; - int16_t fullByte = (int16_t)(upperByte | lowerByte); - float fullByteb = fullByte; - _current = (fullByteb) / ((4096.0f * _currentShunt)); - _error = false; - Serial.print(data[0],16); - Serial.print(" "); - Serial.print(data[1],16); - Serial.print(" "); - Serial.print(data[2],16); - Serial.print(" "); - Serial.print(data[3],16); - Serial.print(" "); - Serial.print(data[4],16); - Serial.print(" "); - Serial.print(data[5],16); - Serial.print(" "); - Serial.print(data[6],16); - Serial.print(" "); - Serial.println(data[7],16); - Serial.println("-"); - - - - uint16_t ICA = 0; - if (readPage(1, data)){ - PageOne_t *pOne = (PageOne_t *) data; - Serial.println(pOne->ICA); - float Ah = pOne->ICA / (2048.0f * _currentShunt); - Serial.print("Ah="); - Serial.println(Ah); - ICA = pOne->ICA; - } - - - - - if (readPage(7, data)){ - PageSeven_t *pSeven = (PageSeven_t *) data; - int16_t CCA = pSeven->CCA0 | ((int16_t) pSeven->CCA1) << 8; - int16_t DCA = pSeven->DCA0 | ((int16_t) pSeven->DCA1) << 8; - Serial.println("ICA, DCA, CCA"); - Serial.print(ICA); - Serial.print(", "); - Serial.print(DCA); - Serial.print(", "); - Serial.println(CCA); - } - -} - -double DS2438::getTemperature() { - return _temperature; -} - -float DS2438::getVoltage(int channel) { - if (channel == DS2438_CHA) { - return _voltageA; - } else if (channel == DS2438_CHB) { - return _voltageB; - } else { - return 0.0; - } -} - -float DS2438::getCurrent() { - return _current; -} - -boolean DS2438::isError() { - return _error; -} - -boolean DS2438::startConversion(int channel, boolean doTemperature) { - if(!isFound()){ - return false; - } - if (!selectChannel(channel)){ - return false; - } - _ow->reset(); - _ow->select(_address); - if (doTemperature) { - _ow->write(DS2438_TEMPERATURE_CONVERSION_COMMAND, 0); - delay(DS2438_TEMPERATURE_DELAY); - _ow->reset(); - _ow->select(_address); - } - _ow->write(DS2438_VOLTAGE_CONVERSION_COMMAND, 0); - delay(DS2438_VOLTAGE_CONVERSION_DELAY); - return true; -} - -boolean DS2438::selectChannel(int channel) { - if(!isFound()){ - return false; - } - uint8_t data[9]; - if (readPage(0, data)) { - if (channel == DS2438_CHB){ - data[0] = data[0] | 0x08; - } - else { - data[0] = data[0] & 0xf7; - } - writePage(0, data); - return true; - } - Serial.println("Could not read page zero data"); - return false; -} - -void DS2438::writePage(int page, uint8_t *data) { - _ow->reset(); - _ow->select(_address); - _ow->write(DS2438_WRITE_SCRATCHPAD_COMMAND, 0); - if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { - _ow->write(page, 0); - } else { - return; - } - for (int i = 0; i < 8; i++){ - _ow->write(data[i], 0); - } - _ow->reset(); - _ow->select(_address); - _ow->write(DS2438_COPY_SCRATCHPAD_COMMAND, 0); - _ow->write(page, 0); -} - -boolean DS2438::readPage(int page, uint8_t *data) { - //TODO if all data is 0 0 is a valid crc, but most likly not as intended - _ow->reset(); - _ow->select(_address); - _ow->write(DS2438_RECALL_MEMORY_COMMAND, 0); - if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { - _ow->write(page, 0); - } else { - return false; - } - _ow->reset(); - _ow->select(_address); - _ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0); - _ow->write(page, 0); - for (int i = 0; i < 9; i++){ - data[i] = _ow->read(); - } - return _ow->crc8(data, 8) == data[8]; -} - diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index a04df45..8d05b45 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -1,52 +1,90 @@ #include #include "driver/pcnt.h" +#include "driver/rtc_io.h" +#include "esp32/ulp.h" +#include "soc/rtc_cntl_reg.h" +#include "soc/rtc.h" -#define OUTPUT_SENSOR 14 /**< GPIO 16 - Enable Sensors */ -#define SENSOR_PLANT5 39 /**< SENSOR vn */ -#define SENSOR_PLANT6 36 /**< SENSOR VP */ +#define ULP_DATA_OFFSET 200 -int16_t pulses = 0; -int16_t pulses2 = 0; +#define ULP_START_OFFSET 0 +void ulp_start(void) { + // Slow memory initialization + //memset(RTC_SLOW_MEM, ULP_START_OFFSET, (8192-ULP_START_OFFSET)); + // GPIO32 initialization (set to output and initial value is 0) + rtc_gpio_init(GPIO_NUM_12); + rtc_gpio_set_direction(GPIO_NUM_12, RTC_GPIO_MODE_OUTPUT_ONLY); + rtc_gpio_set_level(GPIO_NUM_12, 0); + // Define ULP program + const ulp_insn_t ulp_prog[] = { + M_LABEL(1), + + I_MOVI(R2, 0), + I_MOVI(R3, 0), + I_LD(R2, R2, ULP_DATA_OFFSET), + I_LD(R3, R3, ULP_DATA_OFFSET+1), + + + I_WR_REG(RTC_GPIO_OUT_REG, 29, 29, 1), // on + + //wait for 100*r2 + I_MOVR(R0,R2), + M_LABEL(2), + I_DELAY(1), + I_SUBI(R0,R0,1), + M_BGE(2,1), + + I_WR_REG(RTC_GPIO_OUT_REG, 29, 29, 0), // off + + //wait for 100*R3 + I_MOVR(R0,R3), + M_LABEL(3), + I_DELAY(1), + I_SUBI(R0,R0,1), + M_BGE(3,1), + + M_BX(1), + }; + // Run ULP program + size_t size = sizeof(ulp_prog) / sizeof(ulp_insn_t); + ulp_process_macros_and_load(ULP_START_OFFSET, ulp_prog, &size); + assert(size < ULP_DATA_OFFSET && "ULP_DATA_OFFSET needs to be greater or equal to the program size"); + ulp_run(ULP_START_OFFSET); +} + +static inline void ulp_data_write(size_t offset, uint16_t value) +{ + RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] = value; +} + +static inline uint16_t ulp_data_read(size_t offset) +{ + return RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] & 0xffff; +} void setup() { Serial.begin(115200); - pinMode(OUTPUT_SENSOR, OUTPUT); - pinMode(SENSOR_PLANT5, INPUT); - - - pcnt_config_t pcnt_config = { }; // Instancia PCNT config - - pcnt_config.pulse_gpio_num = SENSOR_PLANT5; // Configura GPIO para entrada dos pulsos - pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem - pcnt_config.unit = PCNT_UNIT_0; // Unidade de contagem PCNT - 0 - pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0 - pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000 - pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso - pcnt_config.neg_mode = PCNT_COUNT_DIS; // Incrementa contagem na descida do pulso - pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // PCNT - modo lctrl desabilitado - pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando - pcnt_unit_config(&pcnt_config); // Configura o contador PCNT - - - pcnt_counter_pause(PCNT_UNIT_0); // Pausa o contador PCNT - pcnt_counter_clear(PCNT_UNIT_0); // Zera o contador PCNT - - - digitalWrite(OUTPUT_SENSOR, HIGH); - Serial.println("Start done"); + ulp_data_write(0,1000); + ulp_data_write(1,1000); + ulp_start(); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + //esp_deep_sleep_start(); } + void loop() { - pulses2 = pulseIn(SENSOR_PLANT5,HIGH); - pcnt_counter_resume(PCNT_UNIT_0); - - delay(500); - - pcnt_counter_pause(PCNT_UNIT_0); - pcnt_get_counter_value(PCNT_UNIT_0, &pulses); - pcnt_counter_clear(PCNT_UNIT_0); - - Serial.println(pulses2*2); - Serial.println(pulses*2); + delay(1000); + Serial.println(ulp_data_read(0)); + ulp_data_write(0, 50); + ulp_data_write(1, 150); + //test = 10; + //test2 = 255-test; + delay(1000); + //test = 50; + //test2 = 255 - test; + ulp_data_write(0, 150); + ulp_data_write(1, 50); + //Serial.print(); + Serial.println("loop"); } \ No newline at end of file