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,19 +6,20 @@
#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)
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);
}

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"
@ -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);
}

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;
@ -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; i<GUI_LCM_PAGE; i++)
for (i=0; i<GUI_LCM_PAGE; i++)
{
LCD_WriteCmd(Set_Page_Addr_0|i);
LCD_WriteCmd(Set_ColH_Addr_0);
LCD_WriteCmd(Set_ColL_Addr_0);
j = GUI_LCM_XMAX;
while(j--)
while (j--)
{
LCD_WriteData(*p++);
Delay();
@ -127,10 +124,10 @@ static void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
rt_uint8_t page;
page = y/8;
if(*c == black)
if (*c == black)
gui_disp_buf[page][x] |= 1<<(y%8);
else
if(*c == white)
if (*c == white)
gui_disp_buf[page][x] &= ~(1<<(y%8));
}
@ -139,7 +136,7 @@ static void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
rt_uint8_t page;
page = y/8;
if(gui_disp_buf[page][x] & (1<<(y%8)))
if (gui_disp_buf[page][x] & (1<<(y%8)))
*c = black;
else
*c = white;
@ -151,12 +148,12 @@ static void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, r
rt_uint8_t i;
page = y/8;
for(i=x1; i<x2; i++)
for (i=x1; i<x2; i++)
{
if(*c == black)
if (*c == black)
gui_disp_buf[page][i] |= 1<<(y%8);
else
if(*c == white)
if (*c == white)
gui_disp_buf[page][i] &= ~(1<<(y%8));
}
}
@ -165,7 +162,7 @@ static void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt
{
rt_uint8_t y;
for(y = y1; y < y2; y ++)
for (y = y1; y < y2; y ++)
{
rt_hw_lcd_set_pixel(c, x, y);
}
@ -180,7 +177,7 @@ static void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t
page = y/8;
for(i=x1; i<x2; i++)
for (i=x1; i<x2; i++)
{
gui_disp_buf[page][i] |= 1<<(y%8);
coll = i & 0x0f;
@ -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

@ -22,7 +22,7 @@ void rt_hw_led_on(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch(num)
switch (num)
{
case 1:
LED_PDOR &= ~LED1;
@ -42,7 +42,7 @@ void rt_hw_led_off(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch(num)
switch (num)
{
case 1:
LED_PDOR |= LED1;
@ -62,22 +62,22 @@ void rt_hw_led_toggle(rt_uint8_t num)
{
RT_ASSERT(num < LEDS_MAX_NUMBER);
switch(num)
switch (num)
{
case 1:
if(LED_PDOR&LED1)
if (LED_PDOR&LED1)
LED_PDOR &= ~LED1;
else
LED_PDOR |= LED1;
break;
case 2:
if(LED_PDOR&LED2)
if (LED_PDOR&LED2)
LED_PDOR &= ~LED2;
else
LED_PDOR |= LED2;
break;
case 3:
if(LED_PDOR&LED3)
if (LED_PDOR&LED3)
LED_PDOR &= ~LED3;
else
LED_PDOR |= LED3;
@ -95,25 +95,40 @@ static rt_err_t led_io_init(void)
LED_PDOR |= LED_MASK;
/*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)
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);
}

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