Advertisement
Guest User

fast toggle

a guest
Apr 4th, 2017
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include "freertos/FreeRTOS.h"
  2. #include "esp_wifi.h"
  3. #include "esp_system.h"
  4. #include "esp_event.h"
  5. #include "esp_event_loop.h"
  6. #include "nvs_flash.h"
  7. #include "driver/gpio.h"
  8.  
  9. esp_err_t event_handler(void *ctx, system_event_t *event) { return ESP_OK; }
  10. void loop(void *pvParameter);
  11. void toggle(void *pvParameter);
  12.  
  13. #define TogglePin GPIO_NUM_23
  14.  
  15.  
  16. void app_main(void) {
  17.     nvs_flash_init();
  18.     tcpip_adapter_init();
  19.     ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  20.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  21.     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  22.     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  23.     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  24.     wifi_config_t sta_config = {
  25.         .sta = {
  26.             .ssid = "access_point_name",
  27.             .password = "password",
  28.             .bssid_set = false
  29.         }
  30.     };
  31.     ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
  32.     ESP_ERROR_CHECK( esp_wifi_start() );
  33.     ESP_ERROR_CHECK( esp_wifi_connect() );
  34.  
  35.     gpio_pad_select_gpio(TogglePin);
  36.     gpio_set_direction(TogglePin, GPIO_MODE_OUTPUT);
  37.     xTaskCreatePinnedToCore(&toggle, "toggle", 4096, NULL, 1, NULL, 1);
  38.     xTaskCreatePinnedToCore(&loop, "loop", 4096, NULL, 1, NULL, 0);
  39. }
  40.  
  41.  
  42. void loop(void *pvParameter) {
  43.     printf("loop called\n");
  44.     while (1) {
  45.         vTaskDelay(500 / portTICK_PERIOD_MS);
  46.     }
  47. }
  48.  
  49.  
  50. void toggle(void *pvParameter) {
  51.     printf("toggle called.\n");
  52.     portDISABLE_INTERRUPTS();
  53.  
  54.     while (1) {
  55.         GPIO.out_w1ts |= (1 << TogglePin);
  56.         __asm__ __volatile__("nop;nop;nop;nop;nop;nop;nop;"); // Bug workaround
  57.  
  58.         GPIO.out_w1tc |= (1 << TogglePin);
  59.         __asm__ __volatile__("nop;nop;nop;nop;nop;nop;nop;");
  60.     }
  61.  
  62.     // v  doesn't seems to make any difference (as expected)
  63.     /*while (1) {
  64.         GPIO.out |= (1 << TogglePin);
  65.         __asm__ __volatile__("nop;nop;nop;nop;nop;nop;nop;");
  66.  
  67.         GPIO.out &= ~(1 << TogglePin);
  68.         __asm__ __volatile__("nop;nop;nop;nop;nop;nop;nop;");
  69.     }*/
  70.  
  71.     portENABLE_INTERRUPTS();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement