led.c 2.9 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-03-03     lgnq
 */
10

M
Ming, Bai 已提交
11 12 13 14 15 16 17 18
#include <rtthread.h>
#include <rthw.h>

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

void rt_hw_led_on(rt_uint8_t num)
{
19 20
    RT_ASSERT(num < LEDS_MAX_NUMBER);

M
Ming, Bai 已提交
21 22 23 24 25 26 27 28 29
    switch (num)
    {
        case 1:
            LED_PDOR &= ~LED1;
        break;
        case 2:
            LED_PDOR &= ~LED2;
        break;
        case 3:
30
            LED_PDOR &= ~LED3;
M
Ming, Bai 已提交
31 32 33
        break;
        default:
        break;
34
    }
M
Ming, Bai 已提交
35 36 37 38
}

void rt_hw_led_off(rt_uint8_t num)
{
39
    RT_ASSERT(num < LEDS_MAX_NUMBER);
M
Ming, Bai 已提交
40 41 42 43 44 45 46 47 48 49

    switch (num)
    {
        case 1:
            LED_PDOR |= LED1;
        break;
        case 2:
            LED_PDOR |= LED2;
        break;
        case 3:
50
            LED_PDOR |= LED3;
M
Ming, Bai 已提交
51 52 53
        break;
        default:
        break;
54
    }
M
Ming, Bai 已提交
55 56 57 58
}

void rt_hw_led_toggle(rt_uint8_t num)
{
59 60
    RT_ASSERT(num < LEDS_MAX_NUMBER);

M
Ming, Bai 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    switch (num)
    {
        case 1:
            if (LED_PDOR&LED1)
                LED_PDOR &= ~LED1;
            else
                LED_PDOR |= LED1;
        break;
        case 2:
            if (LED_PDOR&LED2)
                LED_PDOR &= ~LED2;
            else
                LED_PDOR |= LED2;
        break;
        case 3:
            if (LED_PDOR&LED3)
                LED_PDOR &= ~LED3;
            else
79
                LED_PDOR |= LED3;
M
Ming, Bai 已提交
80 81 82
        break;
        default:
        break;
83
    }
M
Ming, Bai 已提交
84 85 86 87 88 89 90 91 92 93
}

static rt_err_t led_io_init(void)
{
    /*Select CPIO function*/
    LED_PFR &= ~LED_MASK;
    /*Set Pin to turn off leds*/
    LED_PDOR |= LED_MASK;
    /*Make led pins outputs*/
    LED_DDR |= LED_MASK;
94

M
Ming, Bai 已提交
95 96 97 98
    //LED3 is controled by PWM
    FM3_GPIO->PFR3 = 0x1000;
    FM3_GPIO->EPFR04 = 0x00080000;
    FM3_BT2_PWM->TMCR = 0x0018;
99
    FM3_BT2_PWM->TMCR2 = 0x01;          /* cks=0b1000 count clk 1/512 */
M
Ming, Bai 已提交
100
    FM3_BT2_PWM->STC = 0x00;
101 102 103 104
    FM3_BT2_PWM->PCSR  = 0x61A;         /* Down count = 1562 */
    FM3_BT2_PWM->PDUT  = 0x0;           /* Duty count = 16/1562=10% */

    FM3_BT2_PWM->TMCR |= 0x03;          /* start base timer(softwere TRG) */
M
Ming, Bai 已提交
105 106 107 108 109
    return RT_EOK;
}

void pwm_update(rt_uint16_t value)
{
110
    FM3_BT2_PWM->PDUT  = value;
M
Ming, Bai 已提交
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
}

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_io_init();

    led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
138
    if (led1_thread != RT_NULL)
M
Ming, Bai 已提交
139
        rt_thread_startup(led1_thread);
140

M
Ming, Bai 已提交
141
    led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 384, 30, 5);
142
    if (led2_thread != RT_NULL)
M
Ming, Bai 已提交
143 144 145 146
        rt_thread_startup(led2_thread);
}