led.c 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/*
 * File      : led.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2011, RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-03-03     lgnq
 */
     
#include <rtthread.h>
#include <rthw.h>

#include "mb9bf506r.h"
#include "led.h"

void rt_hw_led_on(rt_uint8_t num)
{
	RT_ASSERT(num < LEDS_MAX_NUMBER);
    
    switch (num)
    {
        case 1:
            USER_LED_PDOR &= ~USER_LED1;
        break;
        case 2:
            USER_LED_PDOR &= ~USER_LED2;
        break;
        case 3:
            USER_LED_PDOR &= ~USER_LED3;        
        break;
        default:
        break;
    }	
}

void rt_hw_led_off(rt_uint8_t num)
{
	RT_ASSERT(num < LEDS_MAX_NUMBER);

    switch (num)
    {
        case 1:
            USER_LED_PDOR |= USER_LED1;
        break;
        case 2:
            USER_LED_PDOR |= USER_LED2;
        break;
        case 3:
            USER_LED_PDOR |= USER_LED3;        
        break;
        default:
        break;
    }	
}

void rt_hw_led_toggle(rt_uint8_t num)
{
	RT_ASSERT(num < LEDS_MAX_NUMBER);
    
    switch (num)
    {
        case 1:
            if (USER_LED_PDOR&USER_LED1)
                USER_LED_PDOR &= ~USER_LED1;
            else
                USER_LED_PDOR |= USER_LED1;
        break;
        case 2:
            if (USER_LED_PDOR&USER_LED2)
                USER_LED_PDOR &= ~USER_LED2;
            else
                USER_LED_PDOR |= USER_LED2;
        break;
        case 3:
            if (USER_LED_PDOR&USER_LED3)
                USER_LED_PDOR &= ~USER_LED3;
            else
                USER_LED_PDOR |= USER_LED3;    
        break;
        default:
        break;
    }	    
}

void led_init(void)
{
    /*Select CPIO function*/
    USER_LED_PFR &= ~USER_LED_MASK;
    /* disable analog input */
    FM3_GPIO->ADE &= ~USER_LED_MASK;
    /*Set CPIO Pull-Up function*/
    USER_LED_PCR |= USER_LED_MASK;
    /*Make led pins outputs*/
    USER_LED_DDR |= USER_LED_MASK;
    USER_LED_PDOR |= USER_LED_MASK;
}

void pwm_update(rt_uint16_t value)
{
    FM3_BT2_PWM->PDUT  = value;			
}

static void led1_thread_entry(void *parameter)
{
    while (1)
    {
        rt_hw_led_toggle(1);
        rt_thread_delay(RT_TICK_PER_SECOND);
    }
}

static void led2_thread_entry(void *parameter)
{
    while (1)
    {
        rt_hw_led_toggle(2);
        rt_thread_delay(RT_TICK_PER_SECOND/2);
    }
}

static rt_thread_t led1_thread;
static rt_thread_t led2_thread;
void rt_hw_led_init(void)
{
    led_init();

    led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
    if (led1_thread != RT_NULL) 
        rt_thread_startup(led1_thread);
    
    led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
    if (led2_thread != RT_NULL) 
        rt_thread_startup(led2_thread);
}