- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# /space/home/slher/rel/Beken/v6_4_5/macsw/plf/refip/src/driver/led/led.c

Go to the documentation of this file.

    00001
    00020 /*
    00021  * INCLUDE FILES
    00022  ****************************************************************************************
    00023  */
    00024 #include "led.h"
    00025 #include "rwnx_config.h"
    00026
    00027 #if (NX_PLF_VER >= 30)
    00028 #include "reg_karst_if.h"
    00029 #include "mm_timer.h"
    00030 #include "hal_machw.h"
    00031
    00033 struct led_tag
    00034 {
    00036     uint32_t idx;
    00038     uint32_t period_us;
    00040     struct mm_timer_tag timer;
    00041 };
    00042
    00044 struct led_tag led_env[LED_MAX];
    00045 /*
    00046  * FUNCTION DEFINITIONS
    00047  ****************************************************************************************
    00048  */
    00057 __INLINE struct led_tag* led_get(enum led_name led_idx)
    00058 {
    00059     if (!karst_v7_gpio_en_getf() || (led_idx >= LED_MAX))
    00060         return NULL;
    00061
    00062     return &led_env[led_idx];
    00063 }
    00064
    00074 static void led_blink_cb(void *env)
    00075 {
    00076     struct led_tag *led = env;
    00077     uint32_t period_us = led->period_us;
    00078
    00079     // reset period_us to 0 to avoid trying the clear timer
    00080     led->period_us = 0;
    00081     led_toggle(led->idx);
    00082     led->period_us = period_us;
    00083     mm_timer_set(&led->timer,  hal_machw_time() + period_us);
    00084 }
    00085
    00086 void led_enable()
    00087 {
    00088     uint32_t i;
    00089
    00090     memset(led_env, 0, sizeof(led_env));
    00091
    00092     for (i = 0 ; i < LED_MAX; i++)
    00093     {
    00094         led_env[i].idx = i;
    00095         led_env[i].timer.cb = led_blink_cb;
    00096         led_env[i].timer.env = &led_env[i];
    00097     }
    00098
    00099     karst_v7_gpio_en_setf(1);
    00100 }
    00101
    00102 void led_disable()
    00103 {
    00104     uint32_t i;
    00105
    00106     for (i = 0 ; i < LED_MAX; i++)
    00107     {
    00108         if (led_env[i].period_us)
    00109         {
    00110             mm_timer_clear(&led_env[i].timer);
    00111             led_env[i].period_us = 0;
    00112         }
    00113     }
    00114     karst_v7_gpio_en_setf(0);
    00115 }
    00116
    00117 void led_set(enum led_name led_idx, int state)
    00118 {
    00119     struct led_tag *led = led_get(led_idx);
    00120
    00121     if (!led)
    00122         return;
    00123
    00124     // Stop blinking if any
    00125     if (led->period_us)
    00126     {
    00127         mm_timer_clear(&led->timer);
    00128         led->period_us = 0;
    00129     }
    00130
    00131     switch (led_idx)
    00132     {
    00133         case LED_RED:
    00134             karst_v7_gpio5_setf(state);
    00135             break;
    00136         case LED_GREEN:
    00137             karst_v7_gpio6_setf(state);
    00138             break;
    00139         default:
    00140             break;
    00141     }
    00142 }
    00143
    00144 void led_toggle(enum led_name led_idx)
    00145 {
    00146     switch (led_idx)
    00147     {
    00148         case LED_RED:
    00149             return led_set(led_idx, !karst_v7_gpio5_getf());
    00150         case LED_GREEN:
    00151             return led_set(led_idx, !karst_v7_gpio6_getf());
    00152         default:
    00153             return;
    00154     }
    00155 }
    00156
    00157 void led_blink(enum led_name led_idx, int period_ms)
    00158 {
    00159     struct led_tag *led = led_get(led_idx);
    00160
    00161     if (!led)
    00162         return;
    00163
    00164     if (period_ms < 100)
    00165         period_ms = 100;
    00166
    00167     led_toggle(led_idx);
    00168     led->period_us = (period_ms * 1000) >> 1;
    00169     mm_timer_set(&led->timer,  hal_machw_time() + led->period_us);
    00170 }
    00171 #endif //(NX_PLF_VER >= 30)
    00172