소스 검색

Add ESP-IDF support.

Taiga Develop 2 주 전
부모
커밋
44da883237
4개의 변경된 파일과 145개의 추가작업 그리고 58개의 파일을 삭제
  1. 19 0
      CMakeLists..txt
  2. 5 58
      headers/cx/chlcd/hal.h
  3. 85 0
      sources/hal_esp32.c
  4. 36 0
      sources/hal_test.c

+ 19 - 0
CMakeLists..txt

@@ -0,0 +1,19 @@
+file(GLOB CHLCD_SOURCES CONFIGURE_DEPENDS
+    "${CMAKE_CURRENT_SOURCE_DIR}/sources/*.c"
+)
+
+
+idf_component_register(
+    SRCS $CHLCD_SOURCES
+
+    INCLUDE_DIRS "headers/"
+
+    PRIV_REQUIRES
+        freertos
+        esp_common
+        esp_driver_gpio
+        esp_driver_ledc
+
+    COMPILE_DEFINITIONS
+        CHLCD_ON_ESP
+)

+ 5 - 58
headers/cx/chlcd/hal.h

@@ -6,63 +6,10 @@
 
 #include "cx/chlcd/types.h"
 
-#ifdef CHLCD_ON_ESP
-
-static inline chlcd_error_t chlcd_pin_set_output(chlcd_pin_t target) {
-
-}
-
-static inline chlcd_error_t chlcd_pin_write(chlcd_pin_t target, bool state) {
-
-}
-
-static inline chlcd_error_t chlcd_pin_set_pwm(chlcd_pin_t target) {
-
-}
-
-static inline chlcd_error_t chlcd_pin_write_pwm(
-    chlcd_pin_t target, 
-    uint16_t pwm
-) {
-
-}
-
-static inline chlcd_error_t chlcd_sleep_us(uint32_t time) {
-
-}
-
-#elifdef  CHLCD_STDIO_TESTING
-
-#include <stdio.h>
-
-static inline chlcd_error_t chlcd_pin_set_output(chlcd_pin_t target) {
-    printf("Pin %u mode set to output.\n", (uint32_t) target);
-    return CHLCD_SUCCESS;
-}
-
-static inline chlcd_error_t chlcd_pin_write(chlcd_pin_t target, bool state) {
-    printf("Pin %u set to %s.\n", (uint32_t) target, state ? "HIGH" : "LOW");
-    return CHLCD_SUCCESS;
-}
-
-static inline chlcd_error_t chlcd_pin_set_pwm(chlcd_pin_t target) {
-    printf("Pin %u mode set to PWM.\n", (uint32_t) target);
-    return CHLCD_SUCCESS;
-}
-
-static inline chlcd_error_t chlcd_pin_write_pwm(
-    chlcd_pin_t target,
-    uint16_t pwm
-) {
-    printf("Pin %u write to PWM %i.\n", (uint32_t) target, pwm);
-    return CHLCD_SUCCESS;
-}
-
-static inline chlcd_error_t chlcd_sleep_us(uint32_t time) {
-    printf("Wait for %u ns.\n", time);
-    return CHLCD_SUCCESS;
-}
-
-#endif
+chlcd_error_t chlcd_pin_set_output(chlcd_pin_t target);
+chlcd_error_t chlcd_pin_write(chlcd_pin_t target, bool state);
+chlcd_error_t chlcd_pin_set_pwm(chlcd_pin_t target);
+chlcd_error_t chlcd_pin_write_pwm(chlcd_pin_t target, uint16_t pwm);
+chlcd_error_t chlcd_sleep_us(uint32_t time);
 
 #endif

+ 85 - 0
sources/hal_esp32.c

@@ -0,0 +1,85 @@
+#ifdef CHLCD_ON_ESP
+
+#include <cx/chlcd.h>
+
+#include <driver/gpio.h>
+#include <driver/ledc.h>
+#include <rom/ets_sys.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+
+#ifndef CHLCD_LED_PWM_TIMER
+#define CHLCD_LED_PWM_TIMER LEDC_TIMER_MAX
+#endif
+
+#ifndef CHLCD_LED_PWM_CHANNEL 
+#define CHLCD_LED_PWM_CHANNEL LEDC_CHANNEL_MAX
+#endif
+
+#ifndef CHLCD_LED_PWM_FREQUENCY
+#define CHLCD_LED_PWM_FREQUENCY 20000
+#endif
+
+
+chlcd_error_t chlcd_pin_set_output(chlcd_pin_t target) {
+    if (gpio_output_enable(target) == ESP_OK) {
+        return CHLCD_SUCCESS;
+    }
+
+    return CHLCD_BAD_ARGUMENT;
+}
+
+chlcd_error_t chlcd_pin_write(chlcd_pin_t target, bool state) {
+    if (gpio_set_level(target, state ? 1 : 0) == ESP_OK) {
+        return CHLCD_SUCCESS;
+    }
+
+    return CHLCD_BAD_ARGUMENT;
+}
+
+chlcd_error_t chlcd_pin_set_pwm(chlcd_pin_t target) {
+    ledc_timer_config_t timer_config[1] = {0};
+    timer_config->speed_mode = LEDC_LOW_SPEED_MODE;
+    timer_config->duty_resolution = LEDC_TIMER_12_BIT;
+    timer_config->clk_cfg = LEDC_AUTO_CLK;
+    timer_config->freq_hz = CHLCD_LED_PWM_FREQUENCY;
+    timer_config->timer_num = CHLCD_LED_PWM_CHANNEL;
+
+    if (ledc_timer_config(timer_config) != ESP_OK) {
+        return CHLCD_BAD_ARGUMENT;
+    }
+
+    ledc_channel_config_t channel_config[1] = {0};
+    channel_config->speed_mode = LEDC_LOW_SPEED_MODE;
+    channel_config->channel = CHLCD_LED_PWM_CHANNEL;
+    channel_config->timer_sel = CHLCD_LED_PWM_TIMER;
+    channel_config->gpio_num = target;
+    channel_config->duty = 0;
+    channel_config->hpoint = 0;
+    channel_config->sleep_mode = LEDC_SLEEP_MODE_KEEP_ALIVE;
+
+    if (ledc_channel_config(channel_config) != ESP_OK) {
+        return CHLCD_BAD_ARGUMENT;
+    }
+
+    return CHLCD_SUCCESS;
+}
+
+chlcd_error_t chlcd_pin_write_pwm(
+    chlcd_pin_t target, 
+    uint16_t pwm
+) {
+    return CHLCD_SUCCESS;
+}
+
+static inline chlcd_error_t chlcd_sleep_us(uint32_t time) {
+    if (time < 1000) {
+        ets_delay_us(time); 
+        return CHLCD_SUCCESS;
+    }
+
+    vTaskDelay(time / 1000 / portTICK_PERIOD_MS);
+    return CHLCD_SUCCESS;
+}
+
+#endif

+ 36 - 0
sources/hal_test.c

@@ -0,0 +1,36 @@
+#ifdef  CHLCD_STDIO_TESTING
+
+#include <cx/chlcd.h>
+
+#include <stdio.h>
+#include <stddef.h>
+
+
+chlcd_error_t chlcd_pin_set_output(chlcd_pin_t target) {
+    printf("Pin %u mode set to output.\n", (uint32_t) target);
+    return CHLCD_SUCCESS;
+}
+
+chlcd_error_t chlcd_pin_write(chlcd_pin_t target, bool state) {
+    printf("Pin %u set to %s.\n", (uint32_t) target, state ? "HIGH" : "LOW");
+    return CHLCD_SUCCESS;
+}
+
+chlcd_error_t chlcd_pin_set_pwm(chlcd_pin_t target) {
+    printf("Pin %u mode set to PWM.\n", (uint32_t) target);
+    return CHLCD_SUCCESS;
+}
+
+chlcd_error_t chlcd_pin_write_pwm(chlcd_pin_t target, uint16_t pwm) {
+    printf("Pin %u write to PWM %i.\n", (uint32_t) target, pwm);
+    return CHLCD_SUCCESS;
+}
+
+chlcd_error_t chlcd_sleep_us(uint32_t time) {
+    printf("Wait for %u ns.\n", time);
+    return CHLCD_SUCCESS;
+}
+
+#endif
+
+