From 46743c4f3435d94e38737ee36b4dd11b32ba2380 Mon Sep 17 00:00:00 2001 From: dzzxzz Date: Fri, 11 Mar 2011 09:44:54 +0000 Subject: [PATCH] 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 --- bsp/fm3/adc.c | 8 +-- bsp/fm3/application.c | 2 + bsp/fm3/cpuusage.c | 100 +++++++++++++++++++++++++++++++++++ bsp/fm3/cpuusage.h | 23 ++++++++ bsp/fm3/fm3_easy_kit.ewp | 3 ++ bsp/fm3/info.c | 110 ++++++++++++++++++++++++++------------- bsp/fm3/key.c | 48 +++++++++-------- bsp/fm3/lcd.c | 28 +++++----- bsp/fm3/led.c | 45 ++++++++++------ bsp/fm3/led.h | 1 + 10 files changed, 274 insertions(+), 94 deletions(-) create mode 100644 bsp/fm3/cpuusage.c create mode 100644 bsp/fm3/cpuusage.h diff --git a/bsp/fm3/adc.c b/bsp/fm3/adc.c index d167c3d1ea..a41811b65f 100644 --- a/bsp/fm3/adc.c +++ b/bsp/fm3/adc.c @@ -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); diff --git a/bsp/fm3/application.c b/bsp/fm3/application.c index 2b05017556..1963821d51 100644 --- a/bsp/fm3/application.c +++ b/bsp/fm3/application.c @@ -25,6 +25,7 @@ #include "key.h" #include "adc.h" #include "lcd.h" +#include "cpuusage.h" #include 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(); diff --git a/bsp/fm3/cpuusage.c b/bsp/fm3/cpuusage.c new file mode 100644 index 0000000000..2c600625fa --- /dev/null +++ b/bsp/fm3/cpuusage.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#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); +} diff --git a/bsp/fm3/cpuusage.h b/bsp/fm3/cpuusage.h new file mode 100644 index 0000000000..e77bec0146 --- /dev/null +++ b/bsp/fm3/cpuusage.h @@ -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__ */ diff --git a/bsp/fm3/fm3_easy_kit.ewp b/bsp/fm3/fm3_easy_kit.ewp index fb6411113c..ea1dd4b908 100644 --- a/bsp/fm3/fm3_easy_kit.ewp +++ b/bsp/fm3/fm3_easy_kit.ewp @@ -1989,6 +1989,9 @@ $PROJ_DIR$\board.c + + $PROJ_DIR$\cpuusage.c + $PROJ_DIR$\info.c diff --git a/bsp/fm3/info.c b/bsp/fm3/info.c index b9654d738e..e9a065b6de 100644 --- a/bsp/fm3/info.c +++ b/bsp/fm3/info.c @@ -6,19 +6,20 @@ #include #include "adc.h" +#include "cpuusage.h" #include 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) + if (event->type == RTGUI_EVENT_PAINT) { struct rtgui_dc* dc; struct rtgui_rect rect; dc = rtgui_dc_begin_drawing(widget); - if(dc == RT_NULL) + if (dc == RT_NULL) return RT_FALSE; rtgui_widget_get_rect(widget, &rect); @@ -35,77 +36,112 @@ 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; - rtgui_dc_draw_text(dc, "FM3 Easy Kit Demo", &rect); + 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) + 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) + if (ekbd->type == RTGUI_KEYDOWN) { char key_str[16]; - struct rtgui_dc* dc; - struct rtgui_rect rect; - - switch(ekbd->key) + 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) + 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) + 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); - if(dc == RT_NULL) + if (dc == RT_NULL) return RT_FALSE; struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event; - switch(ecmd->command_id) + switch (ecmd->command_id) { case ADC_UPDATE: - rect.x1 = 10; - rect.y1 = 40; - rect.x2 = 120; - rect.y2 = 50; - 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); + 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); + } + 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); } diff --git a/bsp/fm3/key.c b/bsp/fm3/key.c index 6509408d37..0f92fafc8d 100644 --- a/bsp/fm3/key.c +++ b/bsp/fm3/key.c @@ -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 #include "key.h" @@ -33,17 +47,16 @@ static void key_thread_entry(void *parameter) kbd_event.key = RTGUIK_UNKNOWN; kbd_event.type = RTGUI_KEYDOWN; - if(KEY_ENTER_GETVALUE() == 0 ) + if (KEY_ENTER_GETVALUE() == 0 ) { for(i=0; ; i++) { rt_thread_delay( next_delay ); if (KEY_ENTER_GETVALUE() == 0) { - if(i>=4) + if (i>=4) { /* HOME key */ - //rt_kprintf("key_home\n"); kbd_event.key = RTGUIK_HOME; next_delay = RT_TICK_PER_SECOND/5; break; @@ -51,51 +64,42 @@ static void key_thread_entry(void *parameter) } else { - //rt_kprintf("key_enter\n"); kbd_event.key = RTGUIK_RETURN; break; } } } - if(KEY_DOWN_GETVALUE() == 0) + if (KEY_DOWN_GETVALUE() == 0) { - // rt_kprintf("key_down\n"); kbd_event.key = RTGUIK_DOWN; } - if(KEY_UP_GETVALUE() == 0) + if (KEY_UP_GETVALUE() == 0) { - // rt_kprintf("key_up\n"); kbd_event.key = RTGUIK_UP; } - if(KEY_RIGHT_GETVALUE() == 0) + if (KEY_RIGHT_GETVALUE() == 0) { - // rt_kprintf("key_right\n"); kbd_event.key = RTGUIK_RIGHT; } - if(KEY_LEFT_GETVALUE() == 0) + if (KEY_LEFT_GETVALUE() == 0) { - // rt_kprintf("key_left\n"); kbd_event.key = RTGUIK_LEFT; } - if(kbd_event.key != RTGUIK_UNKNOWN) + if (kbd_event.key != RTGUIK_UNKNOWN) { /* 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); - if(key_thread != RT_NULL) + key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 28, 5); + if (key_thread != RT_NULL) rt_thread_startup(key_thread); } diff --git a/bsp/fm3/lcd.c b/bsp/fm3/lcd.c index b5594bc792..1bfc5ba789 100644 --- a/bsp/fm3/lcd.c +++ b/bsp/fm3/lcd.c @@ -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; @@ -59,7 +56,7 @@ void LCD_WriteCmd(unsigned char command) LCD_CD_LOW(); for(i=0; i<8; i++) { - if(command & (0x80 >> i)) + if (command & (0x80 >> i)) LCD_DATA_HIGH(); else LCD_DATA_LOW(); @@ -81,7 +78,7 @@ void LCD_WriteData(unsigned char data) LCD_CD_HIGH(); for(i=0; i<8; i++) { - if(data & (0x80 >> i)) + if (data & (0x80 >> i)) LCD_DATA_HIGH(); else LCD_DATA_LOW(); @@ -103,13 +100,13 @@ static void rt_hw_lcd_update(rtgui_rect_t *rect) rt_uint8_t i,j = GUI_LCM_XMAX; rt_uint8_t* p = (rt_uint8_t*)gui_disp_buf; - for(i=0; iPFR3 = 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) + while (1) { rt_hw_led_toggle(1); - rt_thread_delay(10); + rt_thread_delay(RT_TICK_PER_SECOND); } } static void led2_thread_entry(void *parameter) { - while(1) + while (1) { rt_hw_led_toggle(2); - rt_thread_delay(20); + rt_thread_delay(RT_TICK_PER_SECOND/2); } } @@ -122,13 +137,13 @@ 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, 31, 5); - if(led1_thread != RT_NULL) + + 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) + if (led2_thread != RT_NULL) rt_thread_startup(led2_thread); } diff --git a/bsp/fm3/led.h b/bsp/fm3/led.h index 7fbab2833c..5c115f244b 100644 --- a/bsp/fm3/led.h +++ b/bsp/fm3/led.h @@ -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 \ No newline at end of file