ULP+BLE

jacky19971997
Posts: 2
Joined: Tue Apr 05, 2022 2:28 pm

ULP+BLE

Postby jacky19971997 » Tue Apr 05, 2022 4:30 pm

Hi, I wanna use the ULP processor as a pulse counter together with BLE on the main processor. I ran the ULP example code and it works fine. Once I enable BLE through the sdkconfig or idf.py menuconfig and add the all the neccesary BLE includes it doesn't work. It gives me an error saying: "main/ulp_example_main.c:22:10: fatal error: esp_bt.h: No such file or directory". How can I fix this problem?
  1. /* ULP Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "esp_sleep.h"
  12. #include "nvs.h"
  13. #include "nvs_flash.h"
  14. #include "soc/rtc_cntl_reg.h"
  15. #include "soc/sens_reg.h"
  16. #include "soc/rtc_periph.h"
  17. #include "driver/gpio.h"
  18. #include "driver/rtc_io.h"
  19. #include "esp32/ulp.h"
  20. #include "ulp_main.h"
  21.  
  22. #include "esp_bt.h"
  23.  
  24. #include "esp_gap_ble_api.h"
  25. #include "esp_gatts_api.h"
  26. #include "esp_bt_main.h"
  27. #include "gatts_table_creat_demo.h"
  28. #include "esp_gatt_common_api.h"
  29.  
  30. extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
  31. extern const uint8_t ulp_main_bin_end[]   asm("_binary_ulp_main_bin_end");
  32.  
  33. static void init_ulp_program(void);
  34. static void update_pulse_count(void);
  35.  
  36. void app_main(void)
  37. {
  38.     esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  39.     if (cause != ESP_SLEEP_WAKEUP_ULP) {
  40.         printf("Not ULP wakeup, initializing ULP\n");
  41.         init_ulp_program();
  42.     } else {
  43.         printf("ULP wakeup, saving pulse count\n");
  44.         update_pulse_count();
  45.     }
  46.  
  47.     printf("Entering deep sleep\n\n");
  48.     ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
  49.     esp_deep_sleep_start();
  50. }
  51.  
  52. static void init_ulp_program(void)
  53. {
  54.     esp_err_t err = ulp_load_binary(0, ulp_main_bin_start,
  55.             (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
  56.     ESP_ERROR_CHECK(err);
  57.  
  58.     /* GPIO used for pulse counting. */
  59.     gpio_num_t gpio_num = GPIO_NUM_2;
  60.     int rtcio_num = rtc_io_number_get(gpio_num);
  61.     assert(rtc_gpio_is_valid_gpio(gpio_num) && "GPIO used for pulse counting must be an RTC IO");
  62.  
  63.     /* Initialize some variables used by ULP program.
  64.      * Each 'ulp_xyz' variable corresponds to 'xyz' variable in the ULP program.
  65.      * These variables are declared in an auto generated header file,
  66.      * 'ulp_main.h', name of this file is defined in component.mk as ULP_APP_NAME.
  67.      * These variables are located in RTC_SLOW_MEM and can be accessed both by the
  68.      * ULP and the main CPUs.
  69.      *
  70.      * Note that the ULP reads only the lower 16 bits of these variables.
  71.      */
  72.     ulp_debounce_counter = 1;
  73.     ulp_debounce_max_count = 1;
  74.     ulp_next_edge = 0;
  75.     ulp_io_number = rtcio_num; /* map from GPIO# to RTC_IO# */
  76.     ulp_edge_count_to_wake_up = 1000;
  77.  
  78.     /* Initialize selected GPIO as RTC IO, enable input, disable pullup and pulldown */
  79.     rtc_gpio_init(gpio_num);
  80.     rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_INPUT_ONLY);
  81.     rtc_gpio_pulldown_dis(gpio_num);
  82.     rtc_gpio_pullup_dis(gpio_num);
  83.     rtc_gpio_hold_en(gpio_num);
  84.  
  85.     /* Disconnect GPIO12 and GPIO15 to remove current drain through
  86.      * pullup/pulldown resistors.
  87.      * GPIO12 may be pulled high to select flash voltage.
  88.      */
  89.     rtc_gpio_isolate(GPIO_NUM_12);
  90.     rtc_gpio_isolate(GPIO_NUM_15);
  91.     esp_deep_sleep_disable_rom_logging(); // suppress boot messages
  92.  
  93.     /* Set ULP wake up period to T = 20ms.
  94.      * Minimum pulse width has to be T * (ulp_debounce_counter + 1) = 80ms.
  95.      */
  96.     ulp_set_wakeup_period(0, 200);
  97.  
  98.     /* Start the program */
  99.     err = ulp_run(&ulp_entry - RTC_SLOW_MEM);
  100.     ESP_ERROR_CHECK(err);
  101. }
  102.  
  103. static void update_pulse_count(void)
  104. {
  105.     /* ULP program counts signal edges, convert that to the number of pulses */
  106.     uint32_t pulse_count_from_ulp = (ulp_edge_count & UINT16_MAX) / 2;
  107.     /* In case of an odd number of edges, keep one until next time */
  108.     ulp_edge_count = ulp_edge_count % 2;
  109.     printf("Pulse count from ULP: %5d\n", pulse_count_from_ulp);
  110.  
  111. }
Attachments
ulp_example_main.c
(3.63 KiB) Downloaded 147 times

jacky19971997
Posts: 2
Joined: Tue Apr 05, 2022 2:28 pm

Re: ULP+BLE

Postby jacky19971997 » Wed Apr 06, 2022 1:56 pm

So I got it working, but whenever it goes into sleep mode the ULP pulse counter seems a bit unstable. Without going into sleep mode the value changes -+1 and with sleep mode enabled it changes -+5. Is there a way to make it more stable?

hScorpio
Posts: 2
Joined: Tue Oct 06, 2020 3:00 pm

Re: ULP+BLE

Postby hScorpio » Mon Apr 25, 2022 2:02 pm

How did you fix the esp_bt.h not found issue? I am seeing the same thing with the ULP + BLE. Many thanks!

Who is online

Users browsing this forum: No registered users and 83 guests