update FM3 IAR project

add cpu usage displaying
control LED3 by ADC

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1326 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz 2011-03-11 09:44:54 +00:00
parent 79696e8649
commit 46743c4f34
10 changed files with 274 additions and 94 deletions

View File

@ -20,6 +20,7 @@
#include "mb9bf506r.h"
#include "adc.h"
#include "led.h"
static struct rt_device adc;
@ -46,7 +47,6 @@ static rt_err_t rt_adc_init(rt_device_t dev)
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
}
return RT_EOK;
}
@ -68,7 +68,6 @@ static rt_err_t rt_adc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
*((rt_uint16_t*)args) = (*((rt_uint16_t*)args)*3300)/1024;
break;
}
return RT_EOK;
}
@ -87,6 +86,7 @@ static void adc_thread_entry(void *parameter)
{
rt_device_control(device, RT_DEVICE_CTRL_ADC_START, RT_NULL);
rt_device_control(device, RT_DEVICE_CTRL_ADC_RESULT, &adc_value);
pwm_update(adc_value/3);
rtgui_thread_send(info_tid, &ecmd.parent, sizeof(ecmd));
rt_thread_delay(20);
}
@ -95,7 +95,7 @@ static void adc_thread_entry(void *parameter)
static rt_thread_t adc_thread;
void rt_hw_adc_init(void)
{
adc.type = RT_Device_Class_Char; /* fixme: should be adc type */
adc.type = RT_Device_Class_Char;
adc.rx_indicate = RT_NULL;
adc.tx_complete = RT_NULL;
adc.init = rt_adc_init;
@ -106,7 +106,7 @@ void rt_hw_adc_init(void)
adc.control = rt_adc_control;
adc.user_data = RT_NULL;
adc_thread = rt_thread_create("adc", adc_thread_entry, RT_NULL, 384, 29, 5);
adc_thread = rt_thread_create("adc", adc_thread_entry, RT_NULL, 384, 26, 5);
if(adc_thread != RT_NULL)
rt_thread_startup(adc_thread);

View File

@ -25,6 +25,7 @@
#include "key.h"
#include "adc.h"
#include "lcd.h"
#include "cpuusage.h"
#include <rtgui/rtgui.h>
extern void rtgui_startup();
#endif
@ -37,6 +38,7 @@ void rt_init_thread_entry(void *parameter)
rt_hw_key_init();
rt_hw_adc_init();
rt_hw_lcd_init();
rt_hw_cpu_init();
/* re-init device driver */
rt_device_init_all();

100
bsp/fm3/cpuusage.c Normal file
View File

@ -0,0 +1,100 @@
#include <rtthread.h>
#include <rthw.h>
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>
#include "cpuusage.h"
#define CPU_USAGE_CALC_TICK 10
#define CPU_USAGE_LOOP 100
static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0;
static rt_uint32_t total_count = 0;
static void cpu_usage_idle_hook()
{
rt_tick_t tick;
rt_uint32_t count;
volatile rt_uint32_t loop;
if (total_count == 0)
{
loop = 0;
/* get total count */
rt_enter_critical();
tick = rt_tick_get();
while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
total_count ++;
while (loop < CPU_USAGE_LOOP) loop ++;
}
rt_exit_critical();
}
count = 0;
loop = 0;
/* get CPU usage */
tick = rt_tick_get();
while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
count ++;
while (loop < CPU_USAGE_LOOP) loop ++;
}
/* calculate major and minor */
if (count < total_count)
{
count = total_count - count;
cpu_usage_major = (count * 100) / total_count;
cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
}
else
{
total_count = count;
/* no CPU usage */
cpu_usage_major = 0;
cpu_usage_minor = 0;
}
}
void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
{
RT_ASSERT(major != RT_NULL);
RT_ASSERT(minor != RT_NULL);
*major = cpu_usage_major;
*minor = cpu_usage_minor;
}
void cpu_usage_init()
{
/* set idle thread hook */
rt_thread_idle_sethook(cpu_usage_idle_hook);
}
extern rt_thread_t info_tid;
static void cpu_thread_entry(void *parameter)
{
struct rtgui_event_command ecmd;
RTGUI_EVENT_COMMAND_INIT(&ecmd);
ecmd.type = RTGUI_CMD_USER_INT;
ecmd.command_id = CPU_UPDATE;
while (1)
{
rtgui_thread_send(info_tid, &ecmd.parent, sizeof(ecmd));
rt_thread_delay(20);
}
}
static rt_thread_t cpu_thread;
void rt_hw_cpu_init(void)
{
cpu_usage_init();
cpu_thread = rt_thread_create("cpu", cpu_thread_entry, RT_NULL, 384, 27, 5);
if(cpu_thread != RT_NULL)
rt_thread_startup(cpu_thread);
}

23
bsp/fm3/cpuusage.h Normal file
View File

@ -0,0 +1,23 @@
/*
* File : cpuusage.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
*/
#ifndef __CPUUSAGE_H__
#define __CPUUSAGE_H__
#define CPU_UPDATE 1
void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor);
void rt_hw_cpu_init(void);
#endif /*__ADC_H__ */

View File

@ -1989,6 +1989,9 @@
<file>
<name>$PROJ_DIR$\board.c</name>
</file>
<file>
<name>$PROJ_DIR$\cpuusage.c</name>
</file>
<file>
<name>$PROJ_DIR$\info.c</name>
</file>

View File

@ -6,10 +6,11 @@
#include <rtgui/widgets/workbench.h>
#include "adc.h"
#include "cpuusage.h"
#include <rtthread.h>
extern rt_uint16_t adc_value;
static rt_uint8_t index = 0 ;
static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
if (event->type == RTGUI_EVENT_PAINT)
@ -35,58 +36,68 @@ static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_ev
/* draw text */
rtgui_widget_get_rect(widget, &rect);
rect.x1 += 1;
rect.y1 += 1;
rect.y1 += 25;
rtgui_dc_draw_text(dc, " FM3 Easy Kit Demo", &rect);
rect.y1 += 10;
rtgui_dc_draw_text(dc, "[rt-thread/RTGUI]", &rect);
rtgui_dc_draw_text(dc, " rt-thread / RTGUI", &rect);
rtgui_dc_end_drawing(dc);
return RT_FALSE;
}
else if (event->type == RTGUI_EVENT_KBD)
{
struct rtgui_dc* dc;
struct rtgui_rect rect;
struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
if (ekbd->type == RTGUI_KEYDOWN)
{
char key_str[16];
struct rtgui_dc* dc;
struct rtgui_rect rect;
switch (ekbd->key)
{
case RTGUIK_LEFT:
rt_sprintf(key_str, "KEY = %s", "LEFT");
rt_sprintf(key_str, "%s", "L");
break;
case RTGUIK_RIGHT:
rt_sprintf(key_str, "KEY = %s", "RIGHT");
rt_sprintf(key_str, "%s", "R");
break;
case RTGUIK_DOWN:
rt_sprintf(key_str, "KEY = %s", "DOWN");
rt_sprintf(key_str, "%s", "D");
break;
case RTGUIK_UP:
rt_sprintf(key_str, "KEY = %s", "UP");
rt_sprintf(key_str, "%s", "U");
break;
default:
rt_sprintf(key_str, "KEY = %s", "UNKNOWN");
rt_sprintf(key_str, "%s", "S");
break;
}
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL)
return RT_FALSE;
rect.x1 = 10;
rect.y1 = 30;
rect.x2 = 120;
rect.y2 = 40;
rect.x1 = 118;
rect.y1 = 1;
rect.x2 = 127;
rect.y2 = 10;
rtgui_dc_fill_rect(dc, &rect);
rtgui_dc_draw_text(dc, key_str, &rect);
rtgui_dc_end_drawing(dc);
}
else if (ekbd->type == RTGUI_KEYUP)
{
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL)
return RT_FALSE;
rect.x1 = 118;
rect.y1 = 1;
rect.x2 = 127;
rect.y2 = 10;
rtgui_dc_fill_rect(dc, &rect);
//rtgui_dc_draw_text(dc, key_str, &rect);
rtgui_dc_end_drawing(dc);
}
}
else if (event->type == RTGUI_EVENT_COMMAND)
{
char adc_str[10];
char str[16];
struct rtgui_dc* dc;
struct rtgui_rect rect;
dc = rtgui_dc_begin_drawing(widget);
@ -97,15 +108,40 @@ static rt_bool_t view_event_handler(struct rtgui_widget* widget, struct rtgui_ev
switch (ecmd->command_id)
{
case ADC_UPDATE:
rect.x1 = 10;
rect.y1 = 40;
rect.x2 = 120;
rect.y2 = 50;
rect.x1 = 1;
rect.y1 = 1;
rect.x2 = 117;
rect.y2 = 10;
rtgui_dc_fill_rect(dc, &rect);
rt_sprintf(str, "ADC = %d mv", adc_value);
rtgui_dc_draw_text(dc, str, &rect);
break;
case CPU_UPDATE:
rt_uint8_t major,minor;
cpu_usage_get(&major, &minor);
rect.x1 = 1;
rect.y1 = 12;
rect.x2 = 127;
rect.y2 = 22;
rtgui_dc_fill_rect(dc, &rect);
rt_sprintf(str, "CPU : %d.%d%", major, minor);
rtgui_dc_draw_text(dc, str, &rect);
rect.y1 = 23;
rect.y2 = 63;
index++;
if (index == 127)
{
index = 1;
rtgui_dc_fill_rect(dc, &rect);
rt_sprintf(adc_str, "ADC = %d mv", adc_value);
rtgui_dc_draw_text(dc, adc_str, &rect);
rtgui_dc_end_drawing(dc);
}
if (major>40)
rtgui_dc_draw_vline(dc, index, rect.y1, rect.y2);
else
rtgui_dc_draw_vline(dc, index, rect.y2-major, rect.y2);
break;
}
rtgui_dc_end_drawing(dc);
}
return rtgui_view_event_handler(widget, event);
@ -148,7 +184,7 @@ void info_init()
{
info_tid = rt_thread_create("info",
info_entry, RT_NULL,
2048, 26, 10);
2048, 25, 10);
if (info_tid != RT_NULL) rt_thread_startup(info_tid);
}

View File

@ -1,3 +1,17 @@
/*
* File : key.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 "key.h"
@ -43,7 +57,6 @@ static void key_thread_entry(void *parameter)
if (i>=4)
{
/* HOME key */
//rt_kprintf("key_home\n");
kbd_event.key = RTGUIK_HOME;
next_delay = RT_TICK_PER_SECOND/5;
break;
@ -51,7 +64,6 @@ static void key_thread_entry(void *parameter)
}
else
{
//rt_kprintf("key_enter\n");
kbd_event.key = RTGUIK_RETURN;
break;
}
@ -60,25 +72,21 @@ static void key_thread_entry(void *parameter)
if (KEY_DOWN_GETVALUE() == 0)
{
// rt_kprintf("key_down\n");
kbd_event.key = RTGUIK_DOWN;
}
if (KEY_UP_GETVALUE() == 0)
{
// rt_kprintf("key_up\n");
kbd_event.key = RTGUIK_UP;
}
if (KEY_RIGHT_GETVALUE() == 0)
{
// rt_kprintf("key_right\n");
kbd_event.key = RTGUIK_RIGHT;
}
if (KEY_LEFT_GETVALUE() == 0)
{
// rt_kprintf("key_left\n");
kbd_event.key = RTGUIK_LEFT;
}
@ -86,16 +94,12 @@ static void key_thread_entry(void *parameter)
{
/* post down event */
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
//next_delay = RT_TICK_PER_SECOND/10;
/* delay to post up event */
rt_thread_delay(next_delay);
/* post up event */
}
else
{
kbd_event.type = RTGUI_KEYUP;
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
}
/* wait next key press */
rt_thread_delay(next_delay);
}
@ -104,7 +108,7 @@ static void key_thread_entry(void *parameter)
static rt_thread_t key_thread;
void rt_hw_key_init(void)
{
key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 30, 5);
key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 28, 5);
if (key_thread != RT_NULL)
rt_thread_startup(key_thread);
}

View File

@ -20,11 +20,8 @@ static rt_uint8_t gui_disp_buf[GUI_LCM_YMAX/8][GUI_LCM_XMAX];
struct rtgui_lcd_device
{
struct rt_device parent;
/* screen width and height */
rt_uint16_t width;
rt_uint16_t height;
void* hw_framebuffer;
};
static struct rtgui_lcd_device *lcd = RT_NULL;
@ -311,7 +308,6 @@ static rt_err_t rt_lcd_control (rt_device_t dev, rt_uint8_t cmd, void *args)
LCD_WriteCmd(Display_Off);
break;
}
return RT_EOK;
}

View File

@ -96,15 +96,30 @@ static rt_err_t led_io_init(void)
/*Make led pins outputs*/
LED_DDR |= LED_MASK;
//LED3 is controled by PWM
FM3_GPIO->PFR3 = 0x1000;
FM3_GPIO->EPFR04 = 0x00080000;
FM3_BT2_PWM->TMCR = 0x0018;
FM3_BT2_PWM->TMCR2 = 0x01; /* cks=0b1000 count clk 1/512 */
FM3_BT2_PWM->STC = 0x00;
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) */
return RT_EOK;
}
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(10);
rt_thread_delay(RT_TICK_PER_SECOND);
}
}
@ -113,7 +128,7 @@ static void led2_thread_entry(void *parameter)
while (1)
{
rt_hw_led_toggle(2);
rt_thread_delay(20);
rt_thread_delay(RT_TICK_PER_SECOND/2);
}
}
@ -123,7 +138,7 @@ void rt_hw_led_init(void)
{
led_io_init();
led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 31, 5);
led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 384, 29, 5);
if (led1_thread != RT_NULL)
rt_thread_startup(led1_thread);

View File

@ -37,5 +37,6 @@ void rt_hw_led_init(void);
void rt_hw_led_on(rt_uint8_t num);
void rt_hw_led_off(rt_uint8_t num);
void rt_hw_led_toggle(rt_uint8_t num);
void pwm_update(rt_uint16_t value);
#endif