Implicit declaration esp_timer_get_time()

Jorggels
Posts: 1
Joined: Tue Apr 25, 2023 9:56 am

Implicit declaration esp_timer_get_time()

Postby Jorggels » Tue Apr 25, 2023 10:19 am

  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "driver/gpio.h"
  4. #include "esp32/rom/ets_sys.h"
  5. #include "ds18b20.h"
  6.  
  7. // OneWire commands
  8. #define GETTEMP         0x44  // Tells device to take a temperature reading and put it on the scratchpad
  9. #define SKIPROM         0xCC  // Command to address all devices on the bus
  10. #define SELECTDEVICE    0x55  // Command to address all devices on the bus
  11. #define COPYSCRATCH     0x48  // Copy scratchpad to EEPROM
  12. #define READSCRATCH     0xBE  // Read from scratchpad
  13. #define WRITESCRATCH    0x4E  // Write to scratchpad
  14. #define RECALLSCRATCH   0xB8  // Recall from EEPROM to scratchpad
  15. #define READPOWERSUPPLY 0xB4  // Determine if device needs parasite power
  16. #define ALARMSEARCH     0xEC  // Query bus for devices with an alarm condition
  17. // Scratchpad locations
  18. #define TEMP_LSB        0
  19. #define TEMP_MSB        1
  20. #define HIGH_ALARM_TEMP 2
  21. #define LOW_ALARM_TEMP  3
  22. #define CONFIGURATION   4
  23. #define INTERNAL_BYTE   5
  24. #define COUNT_REMAIN    6
  25. #define COUNT_PER_C     7
  26. #define SCRATCHPAD_CRC  8
  27. // DSROM FIELDS
  28. #define DSROM_FAMILY    0
  29. #define DSROM_CRC       7
  30. // Device resolution
  31. #define TEMP_9_BIT  0x1F //  9 bit
  32. #define TEMP_10_BIT 0x3F // 10 bit
  33. #define TEMP_11_BIT 0x5F // 11 bit
  34. #define TEMP_12_BIT 0x7F // 12 bit
  35.  
  36. uint8_t DS_GPIO;
  37. uint8_t init=0;
  38. uint8_t bitResolution=12;
  39. uint8_t devices=0;
  40.  
  41. DeviceAddress ROM_NO;
  42. uint8_t LastDiscrepancy;
  43. uint8_t LastFamilyDiscrepancy;
  44. bool LastDeviceFlag;
  45.  
  46. /// Sends one bit to bus
  47. void ds18b20_write(char bit){
  48.     if (bit & 1) {
  49.         gpio_set_direction(DS_GPIO, GPIO_MODE_OUTPUT);
  50.         noInterrupts();
  51.         gpio_set_level(DS_GPIO,0);
  52.         ets_delay_us(6);
  53.         gpio_set_direction(DS_GPIO, GPIO_MODE_INPUT);   // release bus
  54.         ets_delay_us(64);
  55.         interrupts();
  56.     } else {
  57.         gpio_set_direction(DS_GPIO, GPIO_MODE_OUTPUT);
  58.         noInterrupts();
  59.         gpio_set_level(DS_GPIO,0);
  60.         ets_delay_us(60);
  61.         gpio_set_direction(DS_GPIO, GPIO_MODE_INPUT);   // release bus
  62.         ets_delay_us(10);
  63.         interrupts();
  64.     }
  65. }
  66.  
  67. // Reads one bit from bus
  68. unsigned char ds18b20_read(void){
  69.     unsigned char value = 0;
  70.     gpio_set_direction(DS_GPIO, GPIO_MODE_OUTPUT);
  71.     noInterrupts();
  72.     gpio_set_level(DS_GPIO, 0);
  73.     ets_delay_us(6);
  74.     gpio_set_direction(DS_GPIO, GPIO_MODE_INPUT);
  75.     ets_delay_us(9);
  76.     value = gpio_get_level(DS_GPIO);
  77.     ets_delay_us(55);
  78.     interrupts();
  79.     return (value);
  80. }
  81. // Sends one byte to bus
  82. void ds18b20_write_byte(char data){
  83.   unsigned char i;
  84.   unsigned char x;
  85.   for(i=0;i<8;i++){
  86.     x = data>>i;
  87.     x &= 0x01;
  88.     ds18b20_write(x);
  89.   }
  90.   ets_delay_us(100);
  91. }
  92. // Reads one byte from bus
  93. unsigned char ds18b20_read_byte(void){
  94.   unsigned char i;
  95.   unsigned char data = 0;
  96.   for (i=0;i<8;i++)
  97.   {
  98.     if(ds18b20_read()) data|=0x01<<i;
  99.     ets_delay_us(15);
  100.   }
  101.   return(data);
  102. }
  103. // Sends reset pulse
  104. unsigned char ds18b20_reset(void){
  105.     unsigned char presence;
  106.     gpio_set_direction(DS_GPIO, GPIO_MODE_OUTPUT);
  107.     noInterrupts();
  108.     gpio_set_level(DS_GPIO, 0);
  109.     ets_delay_us(480);
  110.     gpio_set_level(DS_GPIO, 1);
  111.     gpio_set_direction(DS_GPIO, GPIO_MODE_INPUT);
  112.     ets_delay_us(70);
  113.     presence = (gpio_get_level(DS_GPIO) == 0);
  114.     ets_delay_us(410);
  115.     interrupts();
  116.     return presence;
  117. }
  118.  
  119. bool ds18b20_setResolution(const DeviceAddress tempSensorAddresses[], int numAddresses, uint8_t newResolution) {
  120.     bool success = false;
  121.     // handle the sensors with configuration register
  122.     newResolution = constrain(newResolution, 9, 12);
  123.     uint8_t newValue = 0;
  124.     ScratchPad scratchPad;
  125.     // loop through each address
  126.     for (int i = 0; i < numAddresses; i++){
  127.         // we can only update the sensor if it is connected
  128.         if (ds18b20_isConnected((DeviceAddress*) tempSensorAddresses[i], scratchPad)) {
  129.             switch (newResolution) {
  130.             case 12:
  131.                 newValue = TEMP_12_BIT;
  132.                 break;
  133.             case 11:
  134.                 newValue = TEMP_11_BIT;
  135.                 break;
  136.             case 10:
  137.                 newValue = TEMP_10_BIT;
  138.                 break;
  139.             case 9:
  140.             default:
  141.                 newValue = TEMP_9_BIT;
  142.                 break;
  143.             }
  144.             // if it needs to be updated we write the new value
  145.             if (scratchPad[CONFIGURATION] != newValue) {
  146.                 scratchPad[CONFIGURATION] = newValue;
  147.                 ds18b20_writeScratchPad((DeviceAddress*) tempSensorAddresses[i], scratchPad);
  148.             }
  149.             // done
  150.             success = true;
  151.         }
  152.     }
  153.     return success;
  154. }
  155.  
  156. void ds18b20_writeScratchPad(const DeviceAddress *deviceAddress, const uint8_t *scratchPad) {
  157.     ds18b20_reset();
  158.     ds18b20_select(deviceAddress);
  159.     ds18b20_write_byte(WRITESCRATCH);
  160.     ds18b20_write_byte(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
  161.     ds18b20_write_byte(scratchPad[LOW_ALARM_TEMP]); // low alarm temp
  162.     ds18b20_write_byte(scratchPad[CONFIGURATION]);
  163.     ds18b20_reset();
  164. }
  165.  
  166. bool ds18b20_readScratchPad(const DeviceAddress *deviceAddress, uint8_t* scratchPad) {
  167.     // send the reset command and fail fast
  168.     int b = ds18b20_reset();
  169.     if (b == 0) return false;
  170.     ds18b20_select(deviceAddress);
  171.     ds18b20_write_byte(READSCRATCH);
  172.     // Read all registers in a simple loop
  173.     // byte 0: temperature LSB
  174.     // byte 1: temperature MSB
  175.     // byte 2: high alarm temp
  176.     // byte 3: low alarm temp
  177.     // byte 4: DS18B20 & DS1822: configuration register
  178.     // byte 5: internal use & crc
  179.     // byte 6: DS18B20 & DS1822: store for crc
  180.     // byte 7: DS18B20 & DS1822: store for crc
  181.     // byte 8: SCRATCHPAD_CRC
  182.     for (uint8_t i = 0; i < 9; i++) {
  183.         scratchPad[i] = ds18b20_read_byte();
  184.     }
  185.     b = ds18b20_reset();
  186.     return (b == 1);
  187. }
  188.  
  189. void ds18b20_select(const DeviceAddress *address){
  190.     uint8_t i;
  191.     ds18b20_write_byte(SELECTDEVICE);           // Choose ROM
  192.     for (i = 0; i < 8; i++) ds18b20_write_byte(((uint8_t *)address)[i]);
  193. }
  194.  
  195. void ds18b20_requestTemperatures(){
  196.     ds18b20_reset();
  197.     ds18b20_write_byte(SKIPROM);
  198.     ds18b20_write_byte(GETTEMP);
  199.     unsigned long start = esp_timer_get_time() / 1000ULL;
  200.     while (!isConversionComplete() && ((esp_timer_get_time() / 1000ULL) - start < millisToWaitForConversion())) vPortYield();
  201. }
  202.  
  203. bool isConversionComplete() {
  204.     uint8_t b = ds18b20_read();
  205.     return (b == 1);
  206. }
  207.  
  208. uint16_t millisToWaitForConversion() {
  209.     switch (bitResolution) {
  210.     case 9:
  211.         return 94;
  212.     case 10:
  213.         return 188;
  214.     case 11:
  215.         return 375;
  216.     default:
  217.         return 750;
  218.     }
  219. }
  220.  
  221. bool ds18b20_isConnected(const DeviceAddress *deviceAddress, uint8_t *scratchPad) {
  222.     bool b = ds18b20_readScratchPad(deviceAddress, scratchPad);
  223.     return b && !ds18b20_isAllZeros(scratchPad) && (ds18b20_crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
  224. }
  225.  
  226. uint8_t ds18b20_crc8(const uint8_t *addr, uint8_t len){
  227.     uint8_t crc = 0;
  228.     while (len--) {
  229.         crc = *addr++ ^ crc;  // just re-using crc as intermediate
  230.         crc = pgm_read_byte(dscrc2x16_table + (crc & 0x0f)) ^
  231.         pgm_read_byte(dscrc2x16_table + 16 + ((crc >> 4) & 0x0f));
  232.     }
  233.     return crc;
  234. }
  235.  
  236. bool ds18b20_isAllZeros(const uint8_t * const scratchPad) {
  237.     for (size_t i = 0; i < 9; i++) {
  238.         if (scratchPad[i] != 0) {
  239.             return false;
  240.         }
  241.     }
  242.     return true;
  243. }
  244.  
  245. float ds18b20_getTempF(const DeviceAddress *deviceAddress) {
  246.     ScratchPad scratchPad;
  247.     if (ds18b20_isConnected(deviceAddress, scratchPad)){
  248.         int16_t rawTemp = calculateTemperature(deviceAddress, scratchPad);
  249.         if (rawTemp <= DEVICE_DISCONNECTED_RAW)
  250.             return DEVICE_DISCONNECTED_F;
  251.         // C = RAW/128
  252.         // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
  253.         return ((float) rawTemp * 0.0140625f) + 32.0f;
  254.     }
  255.     return DEVICE_DISCONNECTED_F;
  256. }
  257.  
  258. float ds18b20_getTempC(const DeviceAddress *deviceAddress) {
  259.     ScratchPad scratchPad;
  260.     if (ds18b20_isConnected(deviceAddress, scratchPad)){
  261.         int16_t rawTemp = calculateTemperature(deviceAddress, scratchPad);
  262.         if (rawTemp <= DEVICE_DISCONNECTED_RAW)
  263.             return DEVICE_DISCONNECTED_F;
  264.         // C = RAW/128
  265.         // F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
  266.         return (float) rawTemp/128.0f;
  267.     }
  268.     return DEVICE_DISCONNECTED_F;
  269. }
  270.  
  271. // reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
  272. int16_t calculateTemperature(const DeviceAddress *deviceAddress, uint8_t* scratchPad) {
  273.     int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11) | (((int16_t) scratchPad[TEMP_LSB]) << 3);
  274.     return fpTemperature;
  275. }
  276.  
  277. // Returns temperature from sensor
  278. float ds18b20_get_temp(void) {
  279.   if(init==1){
  280.     unsigned char check;
  281.     char temp1=0, temp2=0;
  282.       check=ds18b20_RST_PULSE();
  283.       if(check==1)
  284.       {
  285.         ds18b20_send_byte(0xCC);
  286.         ds18b20_send_byte(0x44);
  287.         vTaskDelay(750 / portTICK_PERIOD_MS);
  288.         check=ds18b20_RST_PULSE();
  289.         ds18b20_send_byte(0xCC);
  290.         ds18b20_send_byte(0xBE);
  291.         temp1=ds18b20_read_byte();
  292.         temp2=ds18b20_read_byte();
  293.         check=ds18b20_RST_PULSE();
  294.         float temp=0;
  295.         temp=(float)(temp1+(temp2*256))/16;
  296.         return temp;
  297.       }
  298.       else{return 0;}
  299.  
  300.   }
  301.   else{return 0;}
  302. }
  303.  
  304. void ds18b20_init(int GPIO) {
  305.     DS_GPIO = GPIO;
  306.     gpio_pad_select_gpio(DS_GPIO);
  307.     init = 1;
  308. }
  309.  
  310. //
  311. // You need to use this function to start a search again from the beginning.
  312. // You do not need to do it for the first search, though you could.
  313. //
  314. void reset_search() {
  315.     devices=0;
  316.     // reset the search state
  317.     LastDiscrepancy = 0;
  318.     LastDeviceFlag = false;
  319.     LastFamilyDiscrepancy = 0;
  320.     for (int i = 7; i >= 0; i--) {
  321.         ROM_NO[i] = 0;
  322.     }
  323. }
  324. // --- Replaced by the one from the Dallas Semiconductor web site ---
  325. //--------------------------------------------------------------------------
  326. // Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
  327. // search state.
  328. // Return TRUE  : device found, ROM number in ROM_NO buffer
  329. //        FALSE : device not found, end of search
  330.  
  331. bool search(uint8_t *newAddr, bool search_mode) {
  332.     uint8_t id_bit_number;
  333.     uint8_t last_zero, rom_byte_number;
  334.     bool search_result;
  335.     uint8_t id_bit, cmp_id_bit;
  336.  
  337.     unsigned char rom_byte_mask, search_direction;
  338.  
  339.     // initialize for search
  340.     id_bit_number = 1;
  341.     last_zero = 0;
  342.     rom_byte_number = 0;
  343.     rom_byte_mask = 1;
  344.     search_result = false;
  345.  
  346.     // if the last call was not the last one
  347.     if (!LastDeviceFlag) {
  348.         // 1-Wire reset
  349.         if (!ds18b20_reset()) {
  350.             // reset the search
  351.             LastDiscrepancy = 0;
  352.             LastDeviceFlag = false;
  353.             LastFamilyDiscrepancy = 0;
  354.             return false;
  355.         }
  356.  
  357.         // issue the search command
  358.         if (search_mode == true) {
  359.             ds18b20_write_byte(0xF0);   // NORMAL SEARCH
  360.         } else {
  361.             ds18b20_write_byte(0xEC);   // CONDITIONAL SEARCH
  362.         }
  363.  
  364.         // loop to do the search
  365.         do {
  366.             // read a bit and its complement
  367.             id_bit = ds18b20_read();
  368.             cmp_id_bit = ds18b20_read();
  369.  
  370.             // check for no devices on 1-wire
  371.             if ((id_bit == 1) && (cmp_id_bit == 1)) {
  372.                 break;
  373.             } else {
  374.                 // all devices coupled have 0 or 1
  375.                 if (id_bit != cmp_id_bit) {
  376.                     search_direction = id_bit;  // bit write value for search
  377.                 } else {
  378.                     // if this discrepancy if before the Last Discrepancy
  379.                     // on a previous next then pick the same as last time
  380.                     if (id_bit_number < LastDiscrepancy) {
  381.                         search_direction = ((ROM_NO[rom_byte_number]
  382.                                 & rom_byte_mask) > 0);
  383.                     } else {
  384.                         // if equal to last pick 1, if not then pick 0
  385.                         search_direction = (id_bit_number == LastDiscrepancy);
  386.                     }
  387.                     // if 0 was picked then record its position in LastZero
  388.                     if (search_direction == 0) {
  389.                         last_zero = id_bit_number;
  390.  
  391.                         // check for Last discrepancy in family
  392.                         if (last_zero < 9)
  393.                             LastFamilyDiscrepancy = last_zero;
  394.                     }
  395.                 }
  396.  
  397.                 // set or clear the bit in the ROM byte rom_byte_number
  398.                 // with mask rom_byte_mask
  399.                 if (search_direction == 1)
  400.                     ROM_NO[rom_byte_number] |= rom_byte_mask;
  401.                 else
  402.                     ROM_NO[rom_byte_number] &= ~rom_byte_mask;
  403.  
  404.                 // serial number search direction write bit
  405.                 ds18b20_write(search_direction);
  406.  
  407.                 // increment the byte counter id_bit_number
  408.                 // and shift the mask rom_byte_mask
  409.                 id_bit_number++;
  410.                 rom_byte_mask <<= 1;
  411.  
  412.                 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
  413.                 if (rom_byte_mask == 0) {
  414.                     rom_byte_number++;
  415.                     rom_byte_mask = 1;
  416.                 }
  417.             }
  418.         } while (rom_byte_number < 8);  // loop until through all ROM bytes 0-7
  419.  
  420.         // if the search was successful then
  421.         if (!(id_bit_number < 65)) {
  422.             // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
  423.             LastDiscrepancy = last_zero;
  424.  
  425.             // check for last device
  426.             if (LastDiscrepancy == 0) {
  427.                 LastDeviceFlag = true;
  428.             }
  429.             search_result = true;
  430.         }
  431.     }
  432.  
  433.     // if no device found then reset counters so next 'search' will be like a first
  434.     if (!search_result || !ROM_NO[0]) {
  435.         devices=0;
  436.         LastDiscrepancy = 0;
  437.         LastDeviceFlag = false;
  438.         LastFamilyDiscrepancy = 0;
  439.         search_result = false;
  440.     } else {
  441.         for (int i = 0; i < 8; i++){
  442.             newAddr[i] = ROM_NO[i];
  443.         }
  444.         devices++;
  445.     }
  446.     return search_result;
  447. }
Attachments
Error.PNG
Error.PNG (2.63 KiB) Viewed 5304 times
Temp.PNG
Error
Temp.PNG (11.42 KiB) Viewed 5304 times
Include.PNG
List of includes
Include.PNG (4.34 KiB) Viewed 5304 times

MicroController
Posts: 1692
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Implicit declaration esp_timer_get_time()

Postby MicroController » Mon May 08, 2023 9:14 pm

I suggest including the esp_timer header file from the respective component.

Who is online

Users browsing this forum: No registered users and 56 guests