diff --git a/bsp/efm32/SConscript b/bsp/efm32/SConscript index 764e7bcec2cc964c42d3b9f523cb63fcdd00dcd7..2bf92bdccdd1093ae9c4a46d10b91e8428317272 100644 --- a/bsp/efm32/SConscript +++ b/bsp/efm32/SConscript @@ -7,13 +7,13 @@ if rtconfig.PLATFORM == 'gcc': src_bsp.append('start_gcc.S') src_drv1 = ['drv_dma.c', 'drv_rtc.c', 'drv_adc.c', 'drv_acmp.c', 'drv_usart.c', 'drv_leuart.c', 'drv_iic.c', 'drv_timer.c'] src_drv2 = ['drv_sdcard.c', 'drv_ethernet.c'] -src_dev = ['dev_misc.c', 'dev_led.c', 'dev_accel.c', 'dev_sflash.c', 'dev_lcd.c'] +src_dev = ['dev_misc.c', 'dev_led.c', 'dev_accel.c', 'dev_sflash.c', 'dev_lcd.c', 'dev_keys.c'] src_hdl = ['hdl_interrupt.c'] src_app = ['httpd.c'] src = src_bsp + src_drv1 + src_drv2 + src_dev + src_hdl + src_app CPPPATH = [RTT_ROOT + '/bsp/efm32'] -CPPDEFINES = [rtconfig.EFM32_BOARD, rtconfig.EFM32_TYPE] +CPPDEFINES = [rtconfig.EFM32_BOARD, rtconfig.EFM32_LCD, rtconfig.EFM32_TYPE] group = DefineGroup('Startup', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) Return('group') diff --git a/bsp/efm32/application.c b/bsp/efm32/application.c index c0a76b22ffa086e9a8be4d032b14c3688cd19e98..bd4aacadeb270aec97e1cf1f62052ac093785460 100644 --- a/bsp/efm32/application.c +++ b/bsp/efm32/application.c @@ -1,6 +1,6 @@ /***************************************************************************//** * @file application.c - * @brief application tasks + * @brief Demo application * COPYRIGHT (C) 2011, RT-Thread Development Team * @author Bernard, onelife * @version 0.4 beta @@ -19,6 +19,7 @@ * 2011-08-23 onelife Modify Ethernet DEMO according to the changes of * lwIP API in reversion 1668 * 2011-12-20 onelife Add LCD DEMO + * 2012-02-16 onelife Add photo frame DEMO ******************************************************************************/ /***************************************************************************//** @@ -62,168 +63,34 @@ #include #include #include +#include + #if defined(RTGUI_USING_DFS_FILERW) + #include + #define PATH_SEPARATOR '/' + #endif + +#define APP_PHOTO_FRAME #endif /* Private typedef -----------------------------------------------------------*/ +struct photo_event +{ + struct rtgui_event parent; + rt_uint32_t cmd; + rt_uint8_t* path; + rt_uint8_t* format; +}; + /* Private define ------------------------------------------------------------*/ +#define APP_CMD_PHOTO_FRAME 0x00000001 /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ volatile rt_uint32_t rt_system_status = 0; /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ -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) - { - struct rtgui_dc* dc; - struct rtgui_rect rect; - - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) - return RT_FALSE; - rtgui_widget_get_rect(widget, &rect); - - rtgui_dc_fill_rect(dc, &rect); - rect.x2 -= 1; rect.y2 -= 1; - rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1); - rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2); - - rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y2); - rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2 + 1); - - /* shrink border */ - rtgui_rect_inflate(&rect, -1); - - /* draw text */ - rtgui_widget_get_rect(widget, &rect); - rect.y1 += 25; - rtgui_dc_draw_text(dc, " EFM3 EFM32GG_DK3750 Kit", &rect); - rect.y1 += 10; - rtgui_dc_draw_text(dc, " RT-Thread & RTGUI", &rect); - rect.y1 += 10; - rtgui_dc_draw_text(dc, " ÖÐÎÄÒ²ÐÐ!", &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]; - switch (ekbd->key) - { - case RTGUIK_LEFT: - rt_sprintf(key_str, "%s", "L"); - break; - case RTGUIK_RIGHT: - rt_sprintf(key_str, "%s", "R"); - break; - case RTGUIK_DOWN: - rt_sprintf(key_str, "%s", "D"); - break; - case RTGUIK_UP: - rt_sprintf(key_str, "%s", "U"); - break; - default: - rt_sprintf(key_str, "%s", "S"); - break; - } - 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 (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 str[16]; - struct rtgui_dc* dc; - struct rtgui_rect rect; - struct rtgui_event_command* ecmd; - rt_uint8_t major,minor; - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) - return RT_FALSE; - - ecmd = (struct rtgui_event_command*)event; - switch (ecmd->command_id) - { - default: - rect.x1 = 1; - rect.y1 = 1; - rect.x2 = 117; - rect.y2 = 10; - rtgui_dc_fill_rect(dc, &rect); - rt_sprintf(str, "ADC = %d mv", 123); - rtgui_dc_draw_text(dc, str, &rect); - break; - /* case ADC_UPDATE: - 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: - 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); -} - static void wb_info(void* parameter) { rt_mq_t mq; @@ -258,7 +125,7 @@ static void wb_info(void* parameter) // rtgui_widget_set_event_handler(RTGUI_WIDGET(view), view_event_handler); /* Create a lable */ - rtgui_label_t *label = rtgui_label_create("R-Thread & RTGUI"); + rtgui_label_t *label = rtgui_label_create("RT-Thread & RTGUI"); if(label == RT_NULL) { rt_kprintf("Create lable failed!\n"); @@ -293,6 +160,65 @@ static void wb_info(void* parameter) rt_mq_delete(mq); } +static rt_bool_t pic_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event) +{ + rt_bool_t result; + rt_bool_t load = RT_FALSE; + + result = rtgui_view_event_handler(widget, event); + + switch(event->type) + { + case RTGUI_EVENT_PAINT: + load = RT_TRUE; + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + { + struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse *)event; + + if (mouse->button == RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP) + { + rt_kprintf("APP: left click (%x)\n", mouse->button); + } + } + break; + } + + if (load) + { + struct rtgui_dc* dc; + rtgui_rect_t rect; + rtgui_image_t* image; + + // image = rtgui_image_create_from_file("jpg", "/test9.jpg", RT_FALSE); + image = rtgui_image_create_from_file("bmp", "/test_565.bmp", RT_FALSE); + + dc = rtgui_dc_begin_drawing(widget); + if (dc == RT_NULL) + { + return result; + } + + rtgui_widget_get_rect(widget, &rect); + rtgui_widget_rect_to_device(widget, &rect); + rect.y1 +=20; rect.y2 +=20; + + if (image != RT_NULL) + { + rtgui_image_blit(image, dc, &rect); + rtgui_image_destroy(image); + } + else + { + rt_kprintf("APP err: no image found!\n"); + } + + rtgui_dc_end_drawing(dc); + } + + return result; +} static void wb_main(void* parameter) { @@ -317,7 +243,7 @@ static void wb_main(void* parameter) return; } /* Create a view */ - view = rtgui_view_create("view_2"); + view = rtgui_view_create("pic_view"); if(view == RT_NULL) { rt_kprintf("Create view failed!\n"); @@ -325,7 +251,7 @@ static void wb_main(void* parameter) } RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = white; RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(view)) = red; -// rtgui_widget_set_event_handler(RTGUI_WIDGET(view), view_event_handler); + rtgui_widget_set_event_handler(RTGUI_WIDGET(view), pic_view_event_handler); /* Create a lable */ rtgui_label_t* label = rtgui_label_create("EFM32GG_DK3750 Kit"); @@ -426,6 +352,128 @@ static void win_hello(void* parameter) rt_mq_delete(mq); } +static rt_bool_t photo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event) +{ + rt_bool_t result = RT_FALSE; + struct photo_event *photo_event = (struct photo_event *)event; + + result = rtgui_view_event_handler(widget, event); + rt_kprintf("view event %x\n", event->type); + + if ((event->type == RTGUI_EVENT_COMMAND) && \ + (photo_event->cmd == APP_CMD_PHOTO_FRAME)) + { + rtgui_rect_t rect; + rtgui_image_t* image; + struct rtgui_dc* dc; + + rtgui_widget_get_rect(widget, &rect); + rtgui_widget_rect_to_device(widget, &rect); + rect.y1 +=20; rect.y2 +=20; + + dc = rtgui_dc_begin_drawing(widget); + if (dc == RT_NULL) + { + return result; + } + + image = rtgui_image_create_from_file(photo_event->format, + photo_event->path, RT_TRUE); + if (image != RT_NULL) + { + rtgui_image_blit(image, dc, &rect); + rtgui_image_destroy(image); + return result; + } + + return RT_TRUE; + } + + return result; +} + +static rt_bool_t photo_lable_event_handler(rtgui_widget_t* widget, rtgui_event_t *event) +{ + rt_bool_t result = RT_FALSE; + + rt_kprintf("lable event %x\n", event->type); + + if (event->type == RTGUI_EVENT_COMMAND) + { + struct photo_event *photo = (struct photo_event *)event; + + rtgui_label_set_text((rtgui_label_t *)widget, photo->path); + } + + return result; +} + +static void wb_photo(void* parameter) +{ + rt_mq_t mq; + rtgui_view_t *view; + rtgui_workbench_t *workbench; + + /* Create message queue for self */ + mq = rt_mq_create("photo", 256, 4, RT_IPC_FLAG_FIFO); + if(mq == RT_NULL) + { + rt_kprintf("Create mq failed!\n"); + return; + } + rtgui_thread_register(rt_thread_self(), mq); + + /* Create workbench */ + workbench = rtgui_workbench_create("main", "photo"); + if(workbench == RT_NULL) + { + rt_kprintf("Create wb failed!\n"); + return; + } + /* Create a view */ + view = rtgui_view_create("view"); + if(view == RT_NULL) + { + rt_kprintf("Create view failed!\n"); + return; + } + RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = white; + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(view)) = blue; + rtgui_widget_set_event_handler(RTGUI_WIDGET(view), photo_view_event_handler); +// rtgui_widget_set_oncommand(RTGUI_WIDGET(view), photo_view_event_handler); + + /* Create a lable */ + rtgui_label_t* label = rtgui_label_create("Photo Frame Demo"); + if(label == RT_NULL) + { + rt_kprintf("Create lable failed!\n"); + return; + } + RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(label)) = white; + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(label)) = blue; + + /* Set lable position */ + rtgui_rect_t rect; + rect.x1 = 0; rect.y1 = 2; + rect.x2 = 300; rect.y2 = 20; + rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); + rtgui_widget_set_event_handler(RTGUI_WIDGET(label), photo_lable_event_handler); + rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label)); + + /* Add view to workbench */ + rtgui_workbench_add_view(workbench, view); + + /* Show view */ + rtgui_view_show(view, RT_FALSE); + + /* Workbench loop */ + rtgui_workbench_event_loop(workbench); + + /* Prepare for exit */ + rtgui_thread_deregister(rt_thread_self()); + rt_mq_delete(mq); +} + void rt_demo_thread_entry(void* parameter) { #if 0 //defined(EFM32_USING_ACCEL) @@ -530,7 +578,7 @@ void rt_demo_thread_entry(void* parameter) } #endif /* defined(EFM32_USING_ETHERNET) */ -#if defined(EFM32_USING_LCD) +#if (defined(EFM32_USING_LCD) && !defined(APP_PHOTO_FRAME)) { rt_kprintf("LCD DEMO start...\n"); @@ -613,6 +661,109 @@ void rt_demo_thread_entry(void* parameter) rt_kprintf("LCD DEMO end.\n"); } #endif + +#if defined(APP_PHOTO_FRAME) +{ + rt_kprintf("Photo frame DEMO start...\n"); + + /* find lcd device */ + rt_device_t lcd = rt_device_find(LCD_DEVICE_NAME); + if (lcd == RT_NULL) + { + rt_kprintf("Can't find LCD\n"); + } + + /* read LCD info */ + struct rt_device_graphic_info lcd_info; + lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info); + rt_kprintf("LCD size: %dX%d\n", lcd_info.width, lcd_info.height); + + /* register panels */ + rtgui_rect_t rect; + rect.x1 = 0; + rect.y1 = 0; + rect.x2 = lcd_info.width; + rect.y2 = lcd_info.height; + rtgui_panel_register("main", &rect); + rtgui_panel_set_default_focused("main"); + + /* Creat thread */ + rt_thread_t photo = rt_thread_create( + "photo", + wb_photo, + RT_NULL, + 2048, + 25, + 10); + if (photo != RT_NULL) + { + rt_thread_startup(photo); + } + else + { + rt_kprintf("Create workbench \"photo\" failed!\n"); + } + + /* start display photos */ + DIR* dir = opendir("/photo"); + struct photo_event event; + struct dirent* dirent; + rt_uint8_t path[100]; + const rt_uint8_t bmp[] = "bmp"; + const rt_uint8_t jpeg[] = "jpeg"; + + event.parent.type = RTGUI_EVENT_COMMAND; + event.parent.user = sizeof(struct photo_event); + event.parent.sender = RT_NULL; + event.parent.ack = RT_NULL; + event.cmd = APP_CMD_PHOTO_FRAME; + event.path = path; + + rt_thread_sleep(100); + do + { + /* get a photo */ + dirent = readdir(dir); + if (dirent == RT_NULL) + { + break; + } + if ((strcmp(dirent->d_name, ".") == 0) || \ + (strcmp(dirent->d_name, "..") == 0)) + { + continue; + } + rt_sprintf(path, "%s%c%s", "/photo", PATH_SEPARATOR, dirent->d_name); + + /* display it */ + if ((rt_strstr(path, ".bmp") != RT_NULL) || \ + (rt_strstr(path, ".BMP") != RT_NULL)) + { + event.format = &bmp[0]; + rt_kprintf("bmp: %s\n", path); + } + else if ((rt_strstr(path, ".jpg") != RT_NULL) || \ + (rt_strstr(path, ".JPG") != RT_NULL)) + { + event.format = &jpeg[0]; + rt_kprintf("jpeg: %s\n", path); + } + else + { + rt_kprintf("skip: %s\n", path); + continue; + } + + rtgui_server_post_event((&event.parent), sizeof(event)); + rt_thread_sleep(1000); + } while (dirent != RT_NULL); + closedir(dir); + + rt_kprintf("Photo frame end.\n"); +} + +#endif + rt_kprintf("All Demo end.\n"); while(1) diff --git a/bsp/efm32/board.c b/bsp/efm32/board.c index 6133dda79dd55b01c2c11eda9e6c41aa326a2dab..20029e9734f71400e28436c3340719ff4a8436e5 100644 --- a/bsp/efm32/board.c +++ b/bsp/efm32/board.c @@ -19,9 +19,11 @@ * 2011-12-09 onelife Add LEUART module support * 2011-12-14 onelife Add LFXO enabling routine in driver initialization * function - * 2011-12-15 onelife Add MicroSD enabling routine in driver + * 2011-12-15 onelife Add MicroSD initialization routine in driver * initialization function - * 2011-12-20 onelife Add LCD driver initialization routine + * 2011-12-29 onelife Add keys and joystick initialization routine in + * driver initialization function + * 2012-02-15 onelife Modify SWO setup function to support giant gecko ******************************************************************************/ /***************************************************************************//** @@ -203,32 +205,41 @@ void efm_swo_setup(void) rt_uint32_t *tpiu_prescaler = (rt_uint32_t *) 0xE0040010; rt_uint32_t *tpiu_protocol = (rt_uint32_t *) 0xE00400F0; - CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; - /* Enable Serial wire output pin */ - GPIO->ROUTE |= GPIO_ROUTE_SWOPEN; - /* Set location 1 */ - GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC1; - /* Enable output on pin */ - GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK); - GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL; - /* Enable debug clock AUXHFRCO */ - CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN; - - while(!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)); - - /* Enable trace in core debug */ - CoreDebug->DHCSR |= 1; - CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - - /* Enable PC and IRQ sampling output */ - *dwt_ctrl = 0x400113FF; - /* Set TPIU prescaler to 16. */ - *tpiu_prescaler = 0xf; - /* Set protocol to NRZ */ - *tpiu_protocol = 2; - /* Unlock ITM and output data */ - ITM->LAR = 0xC5ACCE55; - ITM->TCR = 0x10009; + CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; + /* Enable Serial wire output pin */ + GPIO->ROUTE |= GPIO_ROUTE_SWOPEN; +#if defined(_EFM32_GIANT_FAMILY) + /* Set location 0 */ + GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC0; + + /* Enable output on pin - GPIO Port F, Pin 2 */ + GPIO->P[5].MODEL &= ~(_GPIO_P_MODEL_MODE2_MASK); + GPIO->P[5].MODEL |= GPIO_P_MODEL_MODE2_PUSHPULL; + #else + /* Set location 1 */ + GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC1; + /* Enable output on pin */ + GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK); + GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL; +#endif + /* Enable debug clock AUXHFRCO */ + CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN; + + while(!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)); + + /* Enable trace in core debug */ + CoreDebug->DHCSR |= 1; + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + + /* Enable PC and IRQ sampling output */ + *dwt_ctrl = 0x400113FF; + /* Set TPIU prescaler to 16. */ + *tpiu_prescaler = 0xf; + /* Set protocol to NRZ */ + *tpiu_protocol = 2; + /* Unlock ITM and output data */ + ITM->LAR = 0xC5ACCE55; + ITM->TCR = 0x10009; } /***************************************************************************//** @@ -250,6 +261,10 @@ void rt_hw_board_init(void) DVK_init(); #elif defined(EFM32GG_DK3750) DVK_init(DVK_Init_EBI); + + /* Disable all DVK interrupts */ + DVK_disableInterrupt(BC_INTEN_MASK); + DVK_clearInterruptFlags(BC_INTFLAG_MASK); #endif /* NVIC Configuration */ @@ -370,6 +385,14 @@ void rt_hw_driver_init(void) #if defined(EFM32_USING_LCD) efm32_spiLcd_init(); #endif + + /* Initialize Keys */ +#if defined(EFM32_USING_KEYS) + #if defined(EFM32GG_DK3750) + efm32_hw_keys_init(); + #endif +#endif + } /***************************************************************************//** diff --git a/bsp/efm32/board.h b/bsp/efm32/board.h index 091ea3c6436a7052bc2bcf41369e5f8e5891dbd5..2494d1bfbfb84d282d40211ab7b3048ea019016b 100644 --- a/bsp/efm32/board.h +++ b/bsp/efm32/board.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -155,8 +156,8 @@ extern volatile rt_uint32_t rt_system_status; #endif /* SECTION: ADC */ -#define ADC_CALI_REF adcRef2V5 -#define ADC_CALI_CH adcSingleInpCh5 +#define ADC_CALI_REF (adcRef2V5) +#define ADC_CALI_CH (adcSingleInpCh5) #define ADC_CONVERT_FREQUENCY (7000000) #if (RT_CONSOLE_DEVICE == EFM_USART0) diff --git a/bsp/efm32/dev_keys.c b/bsp/efm32/dev_keys.c new file mode 100644 index 0000000000000000000000000000000000000000..4767dcfac3f6f06ac6fef722ce368779724518ed --- /dev/null +++ b/bsp/efm32/dev_keys.c @@ -0,0 +1,324 @@ +/***************************************************************************//** + * @file dev_keys.c + * @brief Keys driver of RT-Thread RTOS for EFM32 + * COPYRIGHT (C) 2011, RT-Thread Development Team + * @author onelife + * @version 0.4 beta + ******************************************************************************* + * @section License + * 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 + ******************************************************************************* + * @section Change Logs + * Date Author Notes + * 2011-12-29 onelife Initial creation for EFM32GG_DK3750 board + ******************************************************************************/ + +/***************************************************************************//** + * @addtogroup EFM32GG_DK3750 + * @{ + ******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "board.h" +#include "hdl_interrupt.h" +#include "dev_keys.h" + +#if defined(EFM32_USING_KEYS) +#include + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +#ifdef EFM32_KEYS_DEBUG +#define keys_debug(format,args...) rt_kprintf(format, ##args) +#else +#define keys_debug(format,args...) +#endif + +/* Private function prototypes -----------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +static struct efm32_joy_device joy; +static struct rt_device joy_dev; +static struct rtgui_event_mouse mouse; +static rt_bool_t click; + +/* Private functions ---------------------------------------------------------*/ +/***************************************************************************//** + * @brief + * Keys interrupt handler + * + * @details + * + * @note + * + * @param[in] device + * Pointer to device descriptor + ******************************************************************************/ +static void efm32_keys_isr(rt_device_t dev) +{ + rt_uint16_t flag, joystick; + + /* Clear DEK interrupt */ + flag = DVK_getInterruptFlags(); + DVK_clearInterruptFlags(flag); + + if (flag & BC_INTFLAG_PB) + { + } + + if (flag & BC_INTFLAG_DIP) + { + + } + + if (flag & BC_INTFLAG_JOYSTICK) + { + joystick = DVK_getJoystick(); + keys_debug("Keys: joystick %x\n", joystick); + +#ifdef RT_USING_RTGUI + switch (joystick) + { + case BC_UIF_JOYSTICK_RIGHT: + joy.x += 2; + if (joy.x > joy.max_x) + { + joy.x = joy.max_x; + } + break; + case BC_UIF_JOYSTICK_LEFT: + joy.x -= 2; + if (joy.x < joy.min_x) + { + joy.x = joy.min_x; + } + break; + case BC_UIF_JOYSTICK_DOWN: + joy.y += 2; + if (joy.y > joy.max_y) + { + joy.y = joy.max_y; + } + break; + case BC_UIF_JOYSTICK_UP: + joy.y -= 2; + if (joy.y < joy.min_y) + { + joy.y = joy.min_y; + } + break; + case BC_UIF_JOYSTICK_CENTER: + break; + default: + break; + } +#endif + + if (joystick) + { + if (joystick != BC_UIF_JOYSTICK_CENTER) + { + mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION; + mouse.x = joy.x; + mouse.y = joy.y; + rtgui_server_post_event((&mouse.parent), sizeof(mouse)); + rt_timer_start(&joy.timer); + } + else + { + click = RT_TRUE; + mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; + mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN; + rtgui_server_post_event((&mouse.parent), sizeof(mouse)); + } + } + else + { + if (click) + { + click = RT_FALSE; + mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON; + mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP; + rtgui_server_post_event((&mouse.parent), sizeof(mouse)); + } + else + { + rt_timer_stop(&joy.timer); + } + } + } + + if (flag & BC_INTFLAG_AEM) + { + } +} + +/***************************************************************************//** + * @brief + * Keys timeout handler + * + * @details + * + * @note + * + * @param[in] param + * Parameter + ******************************************************************************/ +static void efm32_keys_timer_isr(void *param) +{ + rt_uint16_t joystick; + + joystick = DVK_getJoystick(); + +#ifdef RT_USING_RTGUI + switch (joystick) + { + case BC_UIF_JOYSTICK_RIGHT: + joy.x += 2; + if (joy.x > joy.max_x) + { + joy.x = joy.max_x; + } + break; + case BC_UIF_JOYSTICK_LEFT: + joy.x -= 2; + if (joy.x < joy.min_x) + { + joy.x = joy.min_x; + } + break; + case BC_UIF_JOYSTICK_DOWN: + joy.y += 2; + if (joy.y > joy.max_y) + { + joy.y = joy.max_y; + } + break; + case BC_UIF_JOYSTICK_UP: + joy.y -= 2; + if (joy.y < joy.min_y) + { + joy.y = joy.min_y; + } + break; + } + + if (joystick) + { + mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION; + mouse.x = joy.x; + mouse.y = joy.y; + rtgui_server_post_event((&mouse.parent), sizeof(mouse)); + } +#endif +} + +/***************************************************************************//** + * @brief + * Initialize keys device + * + * @details + * + * @note + * + * @param[in] dev + * Pointer to device descriptor + * + * @return + * Error code + ******************************************************************************/ +static rt_err_t efm32_keys_init (rt_device_t dev) +{ + struct rt_device_graphic_info lcd_info; + rt_device_t lcd; + + lcd = rt_device_find(LCD_DEVICE_NAME); + if (lcd == RT_NULL) + { + keys_debug("Keys err: Can't find LCD\n"); + return -RT_ERROR; + } + + lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info); + + click = RT_FALSE; + joy.x = 0; + joy.y = 0; + joy.min_x = 0; + joy.max_x = lcd_info.width; + joy.min_y = 0; + joy.max_y = lcd_info.height; + + mouse.parent.sender = RT_NULL; + mouse.wid = RT_NULL; + mouse.button = 0; + + return RT_EOK; +} + +/***************************************************************************//** + * @brief + * Initialize keys related hardware and register joystic device to kernel + * + * @details + * + * @note + * + ******************************************************************************/ +void efm32_hw_keys_init(void) +{ + /* Configure joystick interrupt pin */ + GPIO_PinModeSet(KEYS_INT_PORT, KEYS_INT_PIN, gpioModeInputPullFilter, 1); + + /* Enable joystick interrupt */ + GPIO_IntConfig(KEYS_INT_PORT, KEYS_INT_PIN, true, true, true); + + efm32_irq_hook_init_t hook; + hook.type = efm32_irq_type_gpio; + hook.unit = KEYS_INT_PIN; + hook.cbFunc = efm32_keys_isr; + hook.userPtr = RT_NULL; + efm32_irq_hook_register(&hook); + + if ((rt_uint8_t)KEYS_INT_PIN % 2) + { + NVIC_ClearPendingIRQ(GPIO_ODD_IRQn); + NVIC_SetPriority(GPIO_ODD_IRQn, EFM32_IRQ_PRI_DEFAULT); + NVIC_EnableIRQ(GPIO_ODD_IRQn); + } + else + { + NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn); + NVIC_SetPriority(GPIO_EVEN_IRQn, EFM32_IRQ_PRI_DEFAULT); + NVIC_EnableIRQ(GPIO_EVEN_IRQn); + } + + /* Enable DVK joystick interrupt */ + DVK_enableInterrupt(BC_INTEN_JOYSTICK); + + rt_timer_init(&joy.timer, + "joy_tmr", + efm32_keys_timer_isr, + RT_NULL, + KEYS_POLL_TIME, + RT_TIMER_FLAG_PERIODIC); + + joy_dev.init = efm32_keys_init; + joy_dev.open = RT_NULL; + joy_dev.close = RT_NULL; + joy_dev.read = RT_NULL; + joy_dev.write = RT_NULL; + joy_dev.control = RT_NULL; + joy_dev.user_data = (void *)&joy; + + /* register joy stick device */ + rt_device_register(&joy_dev, "joy", RT_DEVICE_FLAG_RDWR); + + keys_debug("Keys: H/W init OK!\n"); +} + +#endif /* defined(EFM32_USING_KEYS) */ +/***************************************************************************//** + * @} + ******************************************************************************/ diff --git a/bsp/efm32/dev_keys.h b/bsp/efm32/dev_keys.h new file mode 100644 index 0000000000000000000000000000000000000000..a4c0570044f1cdc6b1fb471861c7f27bb9bfa907 --- /dev/null +++ b/bsp/efm32/dev_keys.h @@ -0,0 +1,40 @@ +/***************************************************************************//** + * @file dev_keys.h + * @brief Keys driver of RT-Thread RTOS for EFM32 + * COPYRIGHT (C) 2011, RT-Thread Development Team + * @author onelife + * @version 0.4 beta + ******************************************************************************* + * @section License + * 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 + ******************************************************************************* + * @section Change Logs + * Date Author Notes + * 2011-12-29 onelife Initial creation for EFM32GG_DK3750 board + ******************************************************************************/ +#ifndef __DEV_KEYS_H__ +#define __DEV_KEYS_H__ + +/* Includes ------------------------------------------------------------------*/ +/* Exported defines ----------------------------------------------------------*/ +#define KEYS_INT_PORT (gpioPortE) +#define KEYS_INT_PIN (0) +#define KEYS_POLL_TIME (RT_TICK_PER_SECOND / 10) + +/* Exported types ------------------------------------------------------------*/ +struct efm32_joy_device +{ + rt_int16_t x, y; + rt_uint16_t min_x, max_x; + rt_uint16_t min_y, max_y; + + struct rt_timer timer; +}; + +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void efm32_hw_keys_init(void); + +#endif /* __DEV_KEYS_H__ */ diff --git a/bsp/efm32/dev_lcd.c b/bsp/efm32/dev_lcd.c index e678c9a6179dcc2b390e8d901425e8506ac18e9e..328266a94f821fbb88a2e61cdee45bfc0d9efcff 100644 --- a/bsp/efm32/dev_lcd.c +++ b/bsp/efm32/dev_lcd.c @@ -11,11 +11,13 @@ ******************************************************************************* * @section Change Logs * Date Author Notes - * 2011-12-16 onelife Initial creation for EFM32 + * 2011-12-16 onelife Initial creation of address mapped method (pixel + * drive) for EFM32GG_DK3750 board + * 2011-12-29 onelife Add direct drive method (frame buffer) support ******************************************************************************/ /***************************************************************************//** - * @addtogroup efm32 + * @addtogroup EFM32GG_DK3750 * @{ ******************************************************************************/ @@ -25,6 +27,9 @@ #include "dev_lcd.h" #if defined(EFM32_USING_LCD) + #if (!defined(LCD_MAPPED) && !defined(LCD_DIRECT)) + #error "Unknown LCD access mode" + #endif #include #include @@ -40,16 +45,20 @@ #endif /* Private function prototypes -----------------------------------------------*/ +#if defined(LCD_MAPPED) static void efm32_spiLcd_setPixel(rtgui_color_t *c, int x, int y); static void efm32_spiLcd_getPixel(rtgui_color_t *c, int x, int y); static void efm32_spiLcd_drawRawHLine(rt_uint8_t *pixels, int x1, int x2, int y); static void efm32_spiLcd_drawHLine(rtgui_color_t *c, int x1, int x2, int y); static void efm32_spiLcd_drawVLine(rtgui_color_t *c, int x1, int x2, int y); +#endif /* Private variables ---------------------------------------------------------*/ static rt_device_t lcd; static struct rt_device lcd_device; +static rt_bool_t lcdAutoCs = true; static struct rt_device_graphic_info lcd_info; +#if defined(LCD_MAPPED) static const struct rtgui_graphic_driver_ops lcd_ops = { efm32_spiLcd_setPixel, @@ -58,35 +67,8 @@ static const struct rtgui_graphic_driver_ops lcd_ops = efm32_spiLcd_drawVLine, efm32_spiLcd_drawRawHLine }; -static rt_bool_t lcdAutoCs = true; /* Private functions ---------------------------------------------------------*/ -/***************************************************************************//** - * @brief - * Set/Clear chip select - * - * @details - * - * @note - * - * @param[in] enable - * Chip select pin setting - ******************************************************************************/ -static void efm32_spiLcd_cs(rt_uint8_t enable) -{ - if (!lcdAutoCs) - { - if (enable) - { - GPIO_PinOutClear(LCD_CS_PORT, LCD_CS_PIN); - } - else - { - GPIO_PinOutSet(LCD_CS_PORT, LCD_CS_PIN); - } - } -} - /***************************************************************************//** * @brief * Draw a pixel with specified color @@ -129,8 +111,7 @@ static void efm32_spiLcd_setPixel(rtgui_color_t *c, int x, int y) return; } while(0); - lcd_debug("LCD err: Set pixel at (%d,%d: %x) failed (%x)!\n", - x, y, *c, ret); +// lcd_debug("LCD err: Set pixel at (%d,%d: %x) failed (%x)!\n", x, y, *c, ret); } /***************************************************************************//** @@ -273,8 +254,7 @@ static void efm32_spiLcd_drawHLine(rtgui_color_t *c, int x1, int x2, int y) return; } while(0); - lcd_debug("LCD err: Draw hline at (%d-%d,%d: %x) failed (%x)!\n", - x1, x2, y, *c, ret); +// lcd_debug("LCD err: Draw hline at (%d-%d,%d: %x) failed (%x)!\n", x1, x2, y, *c, ret); } /***************************************************************************//** @@ -360,9 +340,9 @@ static void efm32_spiLcd_drawVLine(rtgui_color_t *c, int x , int y1, int y2) return; } while(0); - lcd_debug("LCD err: Draw vline at (%d,%d-%d: %x) failed (%x)!\n", - x, y1, y2, *c, ret); +// lcd_debug("LCD err: Draw vline at (%d,%d-%d: %x) failed (%x)!\n", x, y1, y2, *c, ret); } +#endif /***************************************************************************//** * @brief @@ -404,6 +384,32 @@ static rt_err_t efm32_spiLcd_control (rt_device_t dev, rt_uint8_t cmd, void *arg return RT_EOK; } +/***************************************************************************//** + * @brief + * Set/Clear chip select + * + * @details + * + * @note + * + * @param[in] enable + * Chip select pin setting + ******************************************************************************/ +static void efm32_spiLcd_cs(rt_uint8_t enable) +{ + if (!lcdAutoCs) + { + if (enable) + { + GPIO_PinOutClear(LCD_CS_PORT, LCD_CS_PIN); + } + else + { + GPIO_PinOutSet(LCD_CS_PORT, LCD_CS_PIN); + } + } +} + /***************************************************************************//** * @brief * Write data to SSD2119 controller @@ -417,7 +423,7 @@ static rt_err_t efm32_spiLcd_control (rt_device_t dev, rt_uint8_t cmd, void *arg * @note * It's not possible to read back register value through SSD2119 SPI interface ******************************************************************************/ -rt_err_t efm32_spiLed_writeRegister(rt_uint8_t reg, rt_uint16_t data) +rt_err_t efm32_spiLcd_writeRegister(rt_uint8_t reg, rt_uint16_t data) { struct efm32_usart_device_t *usart; rt_uint8_t buf_ins[3]; @@ -518,20 +524,16 @@ void efm32_spiLcd_init(void) } lcd_debug("LCD: Find device %s\n", LCD_USING_DEVICE_NAME); - /* Reconfig speed */ - usart = (struct efm32_usart_device_t *)(lcd->user_data); - USART_BaudrateSyncSet(usart->usart_device, 0, EFM32_LCD_SPICLK); - /* Config CS pin */ + usart = (struct efm32_usart_device_t *)(lcd->user_data); if (!(usart->state & USART_STATE_AUTOCS)) { GPIO_PinModeSet(LCD_CS_PORT, LCD_CS_PIN, gpioModePushPull, 1); lcdAutoCs = false; } - // TODO: add another method - /* TFT initialize or reinitialize to Address Mapped Mode - Assumes EBI has been configured correctly in DVK_init(DVK_Init_EBI) */ + /* TFT initialize or reinitialize. Assumes EBI has been configured + correctly in DVK_init(DVK_Init_EBI) */ rt_uint32_t freq = SystemCoreClockGet(); rt_uint32_t i; rt_bool_t warning = RT_FALSE; @@ -561,7 +563,8 @@ void efm32_spiLcd_init(void) { __NOP(); } - /* Configure display for Direct Drive + SPI mode */ +#if defined(LCD_MAPPED) + /* Configure display for address mapped method + 3-wire SPI mode */ DVK_displayControl(DVK_Display_Mode8080); DVK_displayControl(DVK_Display_PowerEnable); DVK_displayControl(DVK_Display_ResetRelease); @@ -578,6 +581,61 @@ void efm32_spiLcd_init(void) lcd_debug("LCD err: driver init failed %x\n", ret); break; } +#elif defined(LCD_DIRECT) + /* Configure TFT direct drive method from EBI BANK2 */ + const EBI_TFTInit_TypeDef tftInit = + { + ebiTFTBank2, /* Select EBI Bank 2 */ + ebiTFTWidthHalfWord, /* Select 2-byte (16-bit RGB565) increments */ + ebiTFTColorSrcMem, /* Use memory as source for mask/blending */ + ebiTFTInterleaveUnlimited, /* Unlimited interleaved accesses */ + ebiTFTFrameBufTriggerVSync, /* VSYNC as frame buffer update trigger */ + false, /* Drive DCLK from negative edge of internal clock */ + ebiTFTMBDisabled, /* No masking and alpha blending enabled */ + ebiTFTDDModeExternal, /* Drive from external memory */ + ebiActiveLow, /* CS Active Low polarity */ + ebiActiveHigh, /* DCLK Active High polarity */ + ebiActiveLow, /* DATAEN Active Low polarity */ + ebiActiveLow, /* HSYNC Active Low polarity */ + ebiActiveLow, /* VSYNC Active Low polarity */ + 320, /* Horizontal size in pixels */ + 1, /* Horizontal Front Porch */ + 30, /* Horizontal Back Porch */ + 2, /* Horizontal Synchronization Pulse Width */ + 240, /* Vertical size in pixels */ + 1, /* Vertical Front Porch */ + 4, /* Vertical Back Porch */ + 2, /* Vertical Synchronization Pulse Width */ + 0x0000, /* Frame Address pointer offset to EBI memory base */ + 4, /* DCLK Period */ + 0, /* DCLK Start cycles */ + 0, /* DCLK Setup cycles */ + 0, /* DCLK Hold cycles */ + }; + + DVK_enablePeripheral(DVK_TFT); + + /* Configure display for Direct Drive + 3-wire SPI mode */ + DVK_displayControl(DVK_Display_ModeGeneric); + DVK_displayControl(DVK_Display_PowerEnable); + DVK_displayControl(DVK_Display_ResetRelease); + + /* Configure GPIO for EBI and TFT */ + /* EBI TFT DCLK/Dot Clock */ + GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 0); + /* EBI TFT DATAEN */ + GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 0); + /* EBI TFT VSYNC */ + GPIO_PinModeSet(gpioPortA, 10, gpioModePushPull, 0); + /* EBI TFT HSYNC */ + GPIO_PinModeSet(gpioPortA, 11, gpioModePushPull, 0); + + /* Initialize display */ + DMD_init(0, (rt_uint32_t)EBI_BankAddress(EBI_BANK2)); + + /* Configure EBI TFT direct drive */ + EBI_TFTInit(&tftInit); +#endif } /* Get LCD geometry */ @@ -589,14 +647,18 @@ void efm32_spiLcd_init(void) } /* Init LCD info */ + flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_TX; lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P; lcd_info.bits_per_pixel = 16; - lcd_info.width = geometry->xSize - 1; - lcd_info.height = geometry->ySize - 1; + lcd_info.width = geometry->xSize; + lcd_info.height = geometry->ySize; +#if defined(LCD_MAPPED) lcd_info.framebuffer = RT_NULL; - - flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_TX; efm32_spiLcd_register(&lcd_device, LCD_DEVICE_NAME, flag, (void *)&lcd_ops); +#elif defined(LCD_DIRECT) + lcd_info.framebuffer = (rt_uint8_t *)EBI_BankAddress(EBI_BANK2); + efm32_spiLcd_register(&lcd_device, LCD_DEVICE_NAME, flag, RT_NULL); +#endif /* Set clipping area */ ret = DMD_setClippingArea(0, 0, geometry->xSize, geometry->ySize); @@ -607,12 +669,13 @@ void efm32_spiLcd_init(void) } /* Read device code */ rt_uint16_t code = 0xFFFF; +#if defined(LCD_MAPPED) code = DMDIF_readDeviceCode(); - +#endif /* Set as rtgui graphic driver */ rtgui_graphic_set_device(&lcd_device); - lcd_debug("LCD: H/W (%x) init OK!\n", code); + lcd_debug("LCD: H/W init OK!\n"); return; } while(0); diff --git a/bsp/efm32/dev_lcd.h b/bsp/efm32/dev_lcd.h index af6a145b620f3141dbf0fe60b3e495c0ef512b5d..8566adac023a198885df85075f55be7cfc158f36 100644 --- a/bsp/efm32/dev_lcd.h +++ b/bsp/efm32/dev_lcd.h @@ -11,7 +11,8 @@ ******************************************************************************* * @section Change Logs * Date Author Notes - * 2011-12-16 onelife Initial creation for EFM32 + * 2011-12-16 onelife Initial creation of address mapped method (pixel + * drive) for EFM32GG_DK3750 board ******************************************************************************/ #ifndef __DEV_LCD_H__ #define __DEV_LCD_H__ @@ -20,9 +21,6 @@ /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ -#define EFM32_LCD_SPICLK (1000000) -#define SPI_TFT_WriteRegister efm32_spiLed_writeRegister - /* Exported functions ------------------------------------------------------- */ void efm32_spiLcd_init(void); diff --git a/bsp/efm32/drv_iic.c b/bsp/efm32/drv_iic.c index 2468d66bafba9073c4caae2a17a8f72834d30f2d..65f40097c8f3f0397f7156a2e0f901185e901031 100644 --- a/bsp/efm32/drv_iic.c +++ b/bsp/efm32/drv_iic.c @@ -1,8 +1,8 @@ /***************************************************************************//** - * @file drv_iic.c - * @brief Serial API of RT-Thread RTOS for EFM32 - * COPYRIGHT (C) 2011, RT-Thread Development Team - * @author onelife + * @file drv_iic.c + * @brief Serial API of RT-Thread RTOS for EFM32 + * COPYRIGHT (C) 2011, RT-Thread Development Team + * @author onelife * @version 0.4 beta ******************************************************************************* * @section License @@ -10,18 +10,19 @@ * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE ******************************************************************************* * @section Change Logs - * Date Author Notes - * 2011-01-06 onelife Initial creation for EFM32 - * 2011-06-17 onelife Modify init function for EFM32 library v2.0.0 + * Date Author Notes + * 2011-01-06 onelife Initial creation for EFM32 + * 2011-06-17 onelife Modify init function for EFM32 library v2.0.0 * upgrading - * 2011-07-11 onelife Add lock (semaphore) to prevent simultaneously + * 2011-07-11 onelife Add lock (semaphore) to prevent simultaneously * access - * 2011-08-04 onelife Change the usage of the second parameter of Read + * 2011-08-04 onelife Change the usage of the second parameter of Read * and Write functions from (seldom used) "Offset" to "Slave address" - * 2011-08-04 onelife Add a timer to prevent from forever waiting - * 2011-11-29 onelife Modify init function for EFM32 library v2.2.2 + * 2011-08-04 onelife Add a timer to prevent from forever waiting + * 2011-11-29 onelife Modify init function for EFM32 library v2.2.2 * upgrading - * 2011-12-27 onelife Utilize "I2C_PRESENT" and "I2C_COUNT" + * 2011-12-27 onelife Utilize "I2C_PRESENT" and "I2C_COUNT" + * 2011-12-27 onelife Change IIC read format ******************************************************************************/ /***************************************************************************//** @@ -41,15 +42,15 @@ /* Private typedef -----------------------------------------------------------*/ struct efm32_iic_block { - struct rt_device device; - struct rt_semaphore lock; - struct rt_timer timer; + struct rt_device device; + struct rt_semaphore lock; + struct rt_timer timer; }; /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ #ifdef RT_IIC_DEBUG -#define iic_debug(format,args...) rt_kprintf(format, ##args) +#define iic_debug(format,args...) rt_kprintf(format, ##args) #else #define iic_debug(format,args...) #endif @@ -59,7 +60,7 @@ struct efm32_iic_block #if (RT_USING_IIC0 > EFM32_IIC_LOCATION_COUNT) #error "Wrong location number" #endif -static struct efm32_iic_block iic0; +static struct efm32_iic_block iic0; #endif #ifdef RT_USING_IIC1 @@ -69,7 +70,7 @@ static struct efm32_iic_block iic0; #if (RT_USING_IIC1 > EFM32_IIC_LOCATION_COUNT) #error "Wrong location number" #endif -static struct efm32_iic_block iic1; +static struct efm32_iic_block iic1; #endif /* Private function prototypes -----------------------------------------------*/ @@ -90,21 +91,21 @@ static struct efm32_iic_block iic1; ******************************************************************************/ static rt_err_t rt_iic_init (rt_device_t dev) { - struct efm32_iic_device_t* iic; + struct efm32_iic_device_t* iic; - iic = (struct efm32_iic_device_t*)dev->user_data; + iic = (struct efm32_iic_device_t*)dev->user_data; - if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) - { - /* Enable IIC */ - I2C_Enable(iic->iic_device, true); - iic->rx_buffer = RT_NULL; - iic->state = 0; + if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED)) + { + /* Enable IIC */ + I2C_Enable(iic->iic_device, true); + iic->rx_buffer = RT_NULL; + iic->state = 0; - dev->flag |= RT_DEVICE_FLAG_ACTIVATED; - } + dev->flag |= RT_DEVICE_FLAG_ACTIVATED; + } - return RT_EOK; + return RT_EOK; } /***************************************************************************//** @@ -126,15 +127,15 @@ static rt_err_t rt_iic_init (rt_device_t dev) ******************************************************************************/ static rt_err_t rt_iic_open(rt_device_t dev, rt_uint16_t oflag) { - RT_ASSERT(dev != RT_NULL); + RT_ASSERT(dev != RT_NULL); - struct efm32_iic_device_t *iic; + struct efm32_iic_device_t *iic; - iic = (struct efm32_iic_device_t *)(dev->user_data); - iic->counter++; + iic = (struct efm32_iic_device_t *)(dev->user_data); + iic->counter++; - iic_debug("IIC: Open with flag %x\n", oflag); - return RT_EOK; + iic_debug("IIC: Open with flag %x\n", oflag); + return RT_EOK; } /***************************************************************************//** @@ -153,18 +154,18 @@ static rt_err_t rt_iic_open(rt_device_t dev, rt_uint16_t oflag) ******************************************************************************/ static rt_err_t rt_iic_close(rt_device_t dev) { - RT_ASSERT(dev != RT_NULL); - - struct efm32_iic_device_t *iic; - - iic = (struct efm32_iic_device_t *)(dev->user_data); - if (--iic->counter == 0) - { - rt_free(iic->rx_buffer->data_ptr); - rt_free(iic->rx_buffer); - iic->rx_buffer = RT_NULL; - } - return RT_EOK; + RT_ASSERT(dev != RT_NULL); + + struct efm32_iic_device_t *iic; + + iic = (struct efm32_iic_device_t *)(dev->user_data); + if (--iic->counter == 0) + { + rt_free(iic->rx_buffer->data_ptr); + rt_free(iic->rx_buffer); + iic->rx_buffer = RT_NULL; + } + return RT_EOK; } /***************************************************************************//** @@ -191,131 +192,141 @@ static rt_err_t rt_iic_close(rt_device_t dev) * Error code ******************************************************************************/ static rt_size_t rt_iic_read ( - rt_device_t dev, - rt_off_t pos, - void* buffer, - rt_size_t size) + rt_device_t dev, + rt_off_t pos, + void* buffer, + rt_size_t size) { - rt_err_t err_code; - rt_size_t read_size; - struct efm32_iic_device_t* iic; - I2C_TransferSeq_TypeDef seq; - I2C_TransferReturn_TypeDef ret; - - if (!size) - { - return 0; - } - - err_code = RT_EOK; - read_size = 0; - iic = (struct efm32_iic_device_t*)dev->user_data; - - /* Lock device */ - if (rt_hw_interrupt_check()) - { - ret = rt_sem_take(iic->lock, RT_WAITING_NO); - } - else - { - ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); - } - if (ret != RT_EOK) - { - return ret; - } - - if (iic->state & IIC_STATE_MASTER) - { - seq.addr = (rt_uint16_t)pos << 1; - seq.flags = I2C_FLAG_WRITE_READ; - /* Set register to be read */ - seq.buf[0].data = (rt_uint8_t *)buffer; - seq.buf[0].len = 1; - /* Set read buffer pointer and size */ - seq.buf[1].data = (rt_uint8_t *)buffer; - seq.buf[1].len = size; - - /* Do a polled transfer */ - iic->timeout = false; - rt_timer_stop(iic->timer); - rt_timer_start(iic->timer); - ret = I2C_TransferInit(iic->iic_device, &seq); - while ((ret == i2cTransferInProgress) && !iic->timeout) - { - ret = I2C_Transfer(iic->iic_device); - } - - if (ret != i2cTransferDone) - { - iic_debug("IIC: read error %x\n", ret); - iic_debug("IIC: read address %x\n", seq.addr); - iic_debug("IIC: read data0 %x -> %x\n", seq.buf[0].data, *seq.buf[0].data); - iic_debug("IIC: read len0 %x\n", seq.buf[0].len); - iic_debug("IIC: read data1 %x -> %x\n", seq.buf[1].data, *seq.buf[1].data); - iic_debug("IIC: read len1 %x\n", seq.buf[1].len); - err_code = (rt_err_t)ret; - } - else - { - read_size = size; - iic_debug("IIC: read size %d\n", read_size); - } - } - else - { - rt_uint8_t* ptr; - - ptr = buffer; - - /* interrupt mode Rx */ - while (size) - { - rt_base_t level; - struct efm32_iic_int_mode_t *int_rx; - - int_rx = iic->rx_buffer; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - if (int_rx->read_index != int_rx->save_index) - { - /* read a character */ - *ptr++ = int_rx->data_ptr[int_rx->read_index]; - size--; - - /* move to next position */ - int_rx->read_index ++; - if (int_rx->read_index >= IIC_RX_BUFFER_SIZE) - { - int_rx->read_index = 0; - } - } - else - { - /* set error code */ - err_code = -RT_EEMPTY; - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - break; - } - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } - - read_size = (rt_uint32_t)ptr - (rt_uint32_t)buffer; - iic_debug("IIC: slave read size %d\n", read_size); - } - - /* Unlock device */ - rt_sem_release(iic->lock); - - /* set error code */ - rt_set_errno(err_code); - return read_size; + rt_err_t err_code; + rt_size_t read_size; + struct efm32_iic_device_t* iic; + I2C_TransferSeq_TypeDef seq; + I2C_TransferReturn_TypeDef ret; + + if (!size) + { + return 0; + } + + err_code = RT_EOK; + read_size = 0; + iic = (struct efm32_iic_device_t*)dev->user_data; + + /* Lock device */ + if (rt_hw_interrupt_check()) + { + ret = rt_sem_take(iic->lock, RT_WAITING_NO); + } + else + { + ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); + } + if (ret != RT_EOK) + { + return ret; + } + + if (iic->state & IIC_STATE_MASTER) + { + seq.addr = (rt_uint16_t)pos << 1; + if (*(rt_uint8_t *)buffer == IIC_OP_READ_ONLY) + { + seq.flags = I2C_FLAG_READ; + /* Set read buffer pointer and size */ + seq.buf[0].data = (rt_uint8_t *)buffer; + seq.buf[0].len = size; + } + else + { + seq.flags = I2C_FLAG_WRITE_READ; + /* Set register to be read */ + seq.buf[0].data = (rt_uint8_t *)buffer; + seq.buf[0].len = 1; + /* Set read buffer pointer and size */ + seq.buf[1].data = (rt_uint8_t *)buffer; + seq.buf[1].len = size; + } + + /* Do a polled transfer */ + iic->timeout = false; + rt_timer_stop(iic->timer); + rt_timer_start(iic->timer); + ret = I2C_TransferInit(iic->iic_device, &seq); + while ((ret == i2cTransferInProgress) && !iic->timeout) + { + ret = I2C_Transfer(iic->iic_device); + } + + if (ret != i2cTransferDone) + { + iic_debug("IIC: read error %x\n", ret); + iic_debug("IIC: read address %x\n", seq.addr); + iic_debug("IIC: read data0 %x -> %x\n", seq.buf[0].data, *seq.buf[0].data); + iic_debug("IIC: read len0 %x\n", seq.buf[0].len); + iic_debug("IIC: read data1 %x -> %x\n", seq.buf[1].data, *seq.buf[1].data); + iic_debug("IIC: read len1 %x\n", seq.buf[1].len); + err_code = (rt_err_t)ret; + } + else + { + read_size = size; + iic_debug("IIC: read size %d\n", read_size); + } + } + else + { + rt_uint8_t* ptr; + + ptr = buffer; + + /* interrupt mode Rx */ + while (size) + { + rt_base_t level; + struct efm32_iic_int_mode_t *int_rx; + + int_rx = iic->rx_buffer; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + if (int_rx->read_index != int_rx->save_index) + { + /* read a character */ + *ptr++ = int_rx->data_ptr[int_rx->read_index]; + size--; + + /* move to next position */ + int_rx->read_index ++; + if (int_rx->read_index >= IIC_RX_BUFFER_SIZE) + { + int_rx->read_index = 0; + } + } + else + { + /* set error code */ + err_code = -RT_EEMPTY; + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + break; + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } + + read_size = (rt_uint32_t)ptr - (rt_uint32_t)buffer; + iic_debug("IIC: slave read size %d\n", read_size); + } + + /* Unlock device */ + rt_sem_release(iic->lock); + + /* set error code */ + rt_set_errno(err_code); + return read_size; } /***************************************************************************//** @@ -342,78 +353,78 @@ static rt_size_t rt_iic_read ( * Error code ******************************************************************************/ static rt_size_t rt_iic_write ( - rt_device_t dev, - rt_off_t pos, - const void* buffer, - rt_size_t size) + rt_device_t dev, + rt_off_t pos, + const void* buffer, + rt_size_t size) { - rt_err_t err_code; - rt_size_t write_size; - struct efm32_iic_device_t* iic; - I2C_TransferSeq_TypeDef seq; - I2C_TransferReturn_TypeDef ret; - - if (!size) - { - return 0; - } - - err_code = RT_EOK; - write_size = 0; - iic = (struct efm32_iic_device_t*)dev->user_data; - - /* Lock device */ - if (rt_hw_interrupt_check()) - { - ret = rt_sem_take(iic->lock, RT_WAITING_NO); - } - else - { - ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); - } - if (ret != RT_EOK) - { - return ret; - } - - if (iic->state & IIC_STATE_MASTER) - { - seq.addr = (rt_uint16_t)pos << 1; - seq.flags = I2C_FLAG_WRITE; - /* Set write buffer pointer and size */ - seq.buf[0].data = (rt_uint8_t *)buffer; - seq.buf[0].len = size; - } - else - { - // TODO: Slave mode TX - } - - /* Do a polled transfer */ - iic->timeout = false; - rt_timer_stop(iic->timer); - rt_timer_start(iic->timer); - ret = I2C_TransferInit(iic->iic_device, &seq); - while ((ret == i2cTransferInProgress) && !iic->timeout) - { - ret = I2C_Transfer(iic->iic_device); - } - - if (ret != i2cTransferDone) - { - err_code = (rt_err_t)ret; - } - else - { - write_size = size; - } - - /* Unlock device */ + rt_err_t err_code; + rt_size_t write_size; + struct efm32_iic_device_t* iic; + I2C_TransferSeq_TypeDef seq; + I2C_TransferReturn_TypeDef ret; + + if (!size) + { + return 0; + } + + err_code = RT_EOK; + write_size = 0; + iic = (struct efm32_iic_device_t*)dev->user_data; + + /* Lock device */ + if (rt_hw_interrupt_check()) + { + ret = rt_sem_take(iic->lock, RT_WAITING_NO); + } + else + { + ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); + } + if (ret != RT_EOK) + { + return ret; + } + + if (iic->state & IIC_STATE_MASTER) + { + seq.addr = (rt_uint16_t)pos << 1; + seq.flags = I2C_FLAG_WRITE; + /* Set write buffer pointer and size */ + seq.buf[0].data = (rt_uint8_t *)buffer; + seq.buf[0].len = size; + } + else + { + // TODO: Slave mode TX + } + + /* Do a polled transfer */ + iic->timeout = false; + rt_timer_stop(iic->timer); + rt_timer_start(iic->timer); + ret = I2C_TransferInit(iic->iic_device, &seq); + while ((ret == i2cTransferInProgress) && !iic->timeout) + { + ret = I2C_Transfer(iic->iic_device); + } + + if (ret != i2cTransferDone) + { + err_code = (rt_err_t)ret; + } + else + { + write_size = size; + } + + /* Unlock device */ rt_sem_release(iic->lock); - /* set error code */ - rt_set_errno(err_code); - return write_size; + /* set error code */ + rt_set_errno(err_code); + return write_size; } /***************************************************************************//** @@ -437,112 +448,112 @@ static rt_size_t rt_iic_write ( * Error code ******************************************************************************/ static rt_err_t rt_iic_control ( - rt_device_t dev, - rt_uint8_t cmd, - void *args) + rt_device_t dev, + rt_uint8_t cmd, + void *args) { - RT_ASSERT(dev != RT_NULL); - - rt_err_t ret; - struct efm32_iic_device_t *iic; - - iic = (struct efm32_iic_device_t*)dev->user_data; - - /* Lock device */ - if (rt_hw_interrupt_check()) - { - ret = rt_sem_take(iic->lock, RT_WAITING_NO); - } - else - { - ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); - } - if (ret != RT_EOK) - { - return ret; - } - - switch (cmd) - { - case RT_DEVICE_CTRL_SUSPEND: - /* suspend device */ - dev->flag |= RT_DEVICE_FLAG_SUSPENDED; - I2C_Enable(iic->iic_device, false); - break; - - case RT_DEVICE_CTRL_RESUME: - /* resume device */ - dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; - I2C_Enable(iic->iic_device, true); - break; - - case RT_DEVICE_CTRL_IIC_SETTING: - { - /* change device setting */ - struct efm32_iic_control_t *control; - - control = (struct efm32_iic_control_t *)args; - iic->state = control->config & (IIC_STATE_MASTER | IIC_STATE_BROADCAST); - iic->address = control->address << 1; - - if (!(iic->state & IIC_STATE_MASTER)) - { - if (iic->rx_buffer == RT_NULL) - { - iic->rx_buffer = rt_malloc(sizeof(struct efm32_iic_int_mode_t)); - if (iic->rx_buffer == RT_NULL) - { - iic_debug("IIC err: no MEM for IIC RX structure\n"); - return -RT_ENOMEM; - } - - /* Allocate RX buffer */ - if ((iic->rx_buffer->data_ptr = \ - rt_malloc(IIC_RX_BUFFER_SIZE)) == RT_NULL) - { - iic_debug("IIC err: no MEM for IIC RX buffer\n"); - rt_free(iic->rx_buffer); - return -RT_ENOMEM; - } - rt_memset(iic->rx_buffer->data_ptr, 0, IIC_RX_BUFFER_SIZE); - iic->rx_buffer->data_size = IIC_RX_BUFFER_SIZE; - iic->rx_buffer->read_index = 0; - iic->rx_buffer->save_index = 0; - } - - /* Enable slave mode */ - I2C_SlaveAddressSet(iic->iic_device, iic->address); - I2C_SlaveAddressMaskSet(iic->iic_device, 0xFF); - iic->iic_device->CTRL |= I2C_CTRL_SLAVE | I2C_CTRL_AUTOACK | I2C_CTRL_AUTOSN; - - /* Enable interrupts */ - I2C_IntEnable(iic->iic_device, I2C_IEN_ADDR | I2C_IEN_RXDATAV | I2C_IEN_SSTOP); - I2C_IntClear(iic->iic_device, _I2C_IFC_MASK); - - /* Enable I2Cn interrupt vector in NVIC */ - if (dev == &iic0.device) - { - NVIC_ClearPendingIRQ(I2C0_IRQn); - NVIC_SetPriority(I2C0_IRQn, EFM32_IRQ_PRI_DEFAULT); - NVIC_EnableIRQ(I2C0_IRQn); - } + RT_ASSERT(dev != RT_NULL); + + rt_err_t ret; + struct efm32_iic_device_t *iic; + + iic = (struct efm32_iic_device_t*)dev->user_data; + + /* Lock device */ + if (rt_hw_interrupt_check()) + { + ret = rt_sem_take(iic->lock, RT_WAITING_NO); + } + else + { + ret = rt_sem_take(iic->lock, RT_WAITING_FOREVER); + } + if (ret != RT_EOK) + { + return ret; + } + + switch (cmd) + { + case RT_DEVICE_CTRL_SUSPEND: + /* suspend device */ + dev->flag |= RT_DEVICE_FLAG_SUSPENDED; + I2C_Enable(iic->iic_device, false); + break; + + case RT_DEVICE_CTRL_RESUME: + /* resume device */ + dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED; + I2C_Enable(iic->iic_device, true); + break; + + case RT_DEVICE_CTRL_IIC_SETTING: + { + /* change device setting */ + struct efm32_iic_control_t *control; + + control = (struct efm32_iic_control_t *)args; + iic->state = control->config & (IIC_STATE_MASTER | IIC_STATE_BROADCAST); + iic->address = control->address << 1; + + if (!(iic->state & IIC_STATE_MASTER)) + { + if (iic->rx_buffer == RT_NULL) + { + iic->rx_buffer = rt_malloc(sizeof(struct efm32_iic_int_mode_t)); + if (iic->rx_buffer == RT_NULL) + { + iic_debug("IIC err: no MEM for IIC RX structure\n"); + return -RT_ENOMEM; + } + + /* Allocate RX buffer */ + if ((iic->rx_buffer->data_ptr = \ + rt_malloc(IIC_RX_BUFFER_SIZE)) == RT_NULL) + { + iic_debug("IIC err: no MEM for IIC RX buffer\n"); + rt_free(iic->rx_buffer); + return -RT_ENOMEM; + } + rt_memset(iic->rx_buffer->data_ptr, 0, IIC_RX_BUFFER_SIZE); + iic->rx_buffer->data_size = IIC_RX_BUFFER_SIZE; + iic->rx_buffer->read_index = 0; + iic->rx_buffer->save_index = 0; + } + + /* Enable slave mode */ + I2C_SlaveAddressSet(iic->iic_device, iic->address); + I2C_SlaveAddressMaskSet(iic->iic_device, 0xFF); + iic->iic_device->CTRL |= I2C_CTRL_SLAVE | I2C_CTRL_AUTOACK | I2C_CTRL_AUTOSN; + + /* Enable interrupts */ + I2C_IntEnable(iic->iic_device, I2C_IEN_ADDR | I2C_IEN_RXDATAV | I2C_IEN_SSTOP); + I2C_IntClear(iic->iic_device, _I2C_IFC_MASK); + + /* Enable I2Cn interrupt vector in NVIC */ + if (dev == &iic0.device) + { + NVIC_ClearPendingIRQ(I2C0_IRQn); + NVIC_SetPriority(I2C0_IRQn, EFM32_IRQ_PRI_DEFAULT); + NVIC_EnableIRQ(I2C0_IRQn); + } #if (I2C_COUNT > 1) - if (dev == &iic1.device) - { - NVIC_ClearPendingIRQ(I2C1_IRQn); - NVIC_SetPriority(I2C1_IRQn, EFM32_IRQ_PRI_DEFAULT); - NVIC_EnableIRQ(I2C1_IRQn); - } + if (dev == &iic1.device) + { + NVIC_ClearPendingIRQ(I2C1_IRQn); + NVIC_SetPriority(I2C1_IRQn, EFM32_IRQ_PRI_DEFAULT); + NVIC_EnableIRQ(I2C1_IRQn); + } #endif - } - } - break; - } + } + } + break; + } - /* Unlock device */ - rt_sem_release(iic->lock); + /* Unlock device */ + rt_sem_release(iic->lock); - return RT_EOK; + return RT_EOK; } /***************************************************************************//** @@ -554,276 +565,276 @@ static rt_err_t rt_iic_control ( * @note * * @param[in] parameter - * Parameter + * Parameter ******************************************************************************/ static void rt_iic_timer(void *timeout) { - *(rt_bool_t *)timeout = true; + *(rt_bool_t *)timeout = true; } /***************************************************************************//** * @brief - * Register IIC device + * Register IIC device * * @details * * @note * * @param[in] device - * Pointer to device descriptor + * Pointer to device descriptor * * @param[in] name - * Device name + * Device name * * @param[in] flag - * Configuration flags + * Configuration flags * * @param[in] iic - * Pointer to IIC device descriptor + * Pointer to IIC device descriptor * * @return - * Error code + * Error code ******************************************************************************/ rt_err_t rt_hw_iic_register( - rt_device_t device, - const char *name, - rt_uint32_t flag, - struct efm32_iic_device_t *iic) + rt_device_t device, + const char *name, + rt_uint32_t flag, + struct efm32_iic_device_t *iic) { - RT_ASSERT(device != RT_NULL); - - if ((flag & RT_DEVICE_FLAG_DMA_TX) || (flag & RT_DEVICE_FLAG_DMA_RX) || - (flag & RT_DEVICE_FLAG_INT_TX)) - { - RT_ASSERT(0); - } - - device->type = RT_Device_Class_I2C; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - device->init = rt_iic_init; - device->open = rt_iic_open; - device->close = rt_iic_close; - device->read = rt_iic_read; - device->write = rt_iic_write; - device->control = rt_iic_control; - device->user_data = iic; - - /* register a character device */ - return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag); + RT_ASSERT(device != RT_NULL); + + if ((flag & RT_DEVICE_FLAG_DMA_TX) || (flag & RT_DEVICE_FLAG_DMA_RX) || + (flag & RT_DEVICE_FLAG_INT_TX)) + { + RT_ASSERT(0); + } + + device->type = RT_Device_Class_I2C; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + device->init = rt_iic_init; + device->open = rt_iic_open; + device->close = rt_iic_close; + device->read = rt_iic_read; + device->write = rt_iic_write; + device->control = rt_iic_control; + device->user_data = iic; + + /* register a character device */ + return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag); } /***************************************************************************//** * @brief - * IIC slave mode RX data valid interrupt handler + * IIC slave mode RX data valid interrupt handler * * @details * * @note * * @param[in] dev - * Pointer to device descriptor + * Pointer to device descriptor ******************************************************************************/ static void rt_hw_iic_slave_isr(rt_device_t dev) { - struct efm32_iic_device_t *iic; - struct efm32_iic_int_mode_t *int_rx; - rt_uint32_t status; - volatile rt_uint32_t temp; - - /* interrupt mode receive */ - RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX); - - iic = (struct efm32_iic_device_t*)dev->user_data; - int_rx = iic->rx_buffer; - status = iic->iic_device->IF; - - if (status & I2C_IF_ADDR) - { - /* Address Match */ - /* Indicating that reception is started */ - temp = iic->iic_device->RXDATA & 0xFFUL; - if ((temp != 0x00) || (iic->state & IIC_STATE_BROADCAST)) - { - iic->state |= IIC_STATE_RX_BUSY; - } - } - else if (status & I2C_IF_RXDATAV) - { - if (iic->state & IIC_STATE_RX_BUSY) - { - rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - /* save character */ - int_rx->data_ptr[int_rx->save_index] = \ - (rt_uint8_t)(iic->iic_device->RXDATA & 0xFFUL); - int_rx->save_index ++; - if (int_rx->save_index >= IIC_RX_BUFFER_SIZE) - int_rx->save_index = 0; - - /* if the next position is read index, discard this 'read char' */ - if (int_rx->save_index == int_rx->read_index) - { - int_rx->read_index ++; - if (int_rx->read_index >= IIC_RX_BUFFER_SIZE) - { - int_rx->read_index = 0; - } - } - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } - else - { - temp = iic->iic_device->RXDATA; - } - } - - if(status & I2C_IF_SSTOP) - { - /* Stop received, reception is ended */ - iic->state &= ~(rt_uint8_t)IIC_STATE_RX_BUSY; - } + struct efm32_iic_device_t *iic; + struct efm32_iic_int_mode_t *int_rx; + rt_uint32_t status; + volatile rt_uint32_t temp; + + /* interrupt mode receive */ + RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX); + + iic = (struct efm32_iic_device_t*)dev->user_data; + int_rx = iic->rx_buffer; + status = iic->iic_device->IF; + + if (status & I2C_IF_ADDR) + { + /* Address Match */ + /* Indicating that reception is started */ + temp = iic->iic_device->RXDATA & 0xFFUL; + if ((temp != 0x00) || (iic->state & IIC_STATE_BROADCAST)) + { + iic->state |= IIC_STATE_RX_BUSY; + } + } + else if (status & I2C_IF_RXDATAV) + { + if (iic->state & IIC_STATE_RX_BUSY) + { + rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + /* save character */ + int_rx->data_ptr[int_rx->save_index] = \ + (rt_uint8_t)(iic->iic_device->RXDATA & 0xFFUL); + int_rx->save_index ++; + if (int_rx->save_index >= IIC_RX_BUFFER_SIZE) + int_rx->save_index = 0; + + /* if the next position is read index, discard this 'read char' */ + if (int_rx->save_index == int_rx->read_index) + { + int_rx->read_index ++; + if (int_rx->read_index >= IIC_RX_BUFFER_SIZE) + { + int_rx->read_index = 0; + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } + else + { + temp = iic->iic_device->RXDATA; + } + } + + if(status & I2C_IF_SSTOP) + { + /* Stop received, reception is ended */ + iic->state &= ~(rt_uint8_t)IIC_STATE_RX_BUSY; + } } /***************************************************************************//** * @brief - * Initialize the specified IIC unit + * Initialize the specified IIC unit * * @details * * @note * * @param[in] unitNumber - * Unit number + * Unit number * * @param[in] location - * Pin location number + * Pin location number ******************************************************************************/ static struct efm32_iic_device_t *rt_hw_iic_unit_init( - struct efm32_iic_block *block, - rt_uint8_t unitNumber, - rt_uint8_t location) + struct efm32_iic_block *block, + rt_uint8_t unitNumber, + rt_uint8_t location) { - struct efm32_iic_device_t *iic; - CMU_Clock_TypeDef iicClock; - GPIO_Port_TypeDef port_scl, port_sda; - rt_uint32_t pin_scl, pin_sda; - I2C_Init_TypeDef init = I2C_INIT_DEFAULT; - efm32_irq_hook_init_t hook; - rt_uint8_t name[RT_NAME_MAX]; - - do - { - /* Allocate device */ - iic = rt_malloc(sizeof(struct efm32_iic_device_t)); - if (iic == RT_NULL) - { - iic_debug("IIC err: no MEM for IIC%d driver\n", unitNumber); - break; - } - iic->counter = 0; - iic->timer = &block->timer; - iic->timeout = false; - iic->state |= IIC_STATE_MASTER; - iic->address = 0x0000; - iic->rx_buffer = RT_NULL; - - /* Initialization */ - if (unitNumber >= I2C_COUNT) - { - break; - } - switch (unitNumber) - { - case 0: - iic->iic_device = I2C0; - iicClock = (CMU_Clock_TypeDef)cmuClock_I2C0; - port_scl = AF_I2C0_SCL_PORT(location); - pin_scl = AF_I2C0_SCL_PIN(location); - port_sda = AF_I2C0_SDA_PORT(location); - pin_sda = AF_I2C0_SDA_PIN(location); - break; + struct efm32_iic_device_t *iic; + CMU_Clock_TypeDef iicClock; + GPIO_Port_TypeDef port_scl, port_sda; + rt_uint32_t pin_scl, pin_sda; + I2C_Init_TypeDef init = I2C_INIT_DEFAULT; + efm32_irq_hook_init_t hook; + rt_uint8_t name[RT_NAME_MAX]; + + do + { + /* Allocate device */ + iic = rt_malloc(sizeof(struct efm32_iic_device_t)); + if (iic == RT_NULL) + { + iic_debug("IIC err: no MEM for IIC%d driver\n", unitNumber); + break; + } + iic->counter = 0; + iic->timer = &block->timer; + iic->timeout = false; + iic->state |= IIC_STATE_MASTER; + iic->address = 0x0000; + iic->rx_buffer = RT_NULL; + + /* Initialization */ + if (unitNumber >= I2C_COUNT) + { + break; + } + switch (unitNumber) + { + case 0: + iic->iic_device = I2C0; + iicClock = (CMU_Clock_TypeDef)cmuClock_I2C0; + port_scl = AF_I2C0_SCL_PORT(location); + pin_scl = AF_I2C0_SCL_PIN(location); + port_sda = AF_I2C0_SDA_PORT(location); + pin_sda = AF_I2C0_SDA_PIN(location); + break; #if (I2C_COUNT > 1) - case 1: - iic->iic_device = I2C1; - iicClock = (CMU_Clock_TypeDef)cmuClock_I2C1; - port_scl = AF_I2C1_SCL_PORT(location); - pin_scl = AF_I2C1_SCL_PIN(location); - port_sda = AF_I2C1_SDA_PORT(location); - pin_sda = AF_I2C1_SDA_PIN(location); - break; + case 1: + iic->iic_device = I2C1; + iicClock = (CMU_Clock_TypeDef)cmuClock_I2C1; + port_scl = AF_I2C1_SCL_PORT(location); + pin_scl = AF_I2C1_SCL_PIN(location); + port_sda = AF_I2C1_SDA_PORT(location); + pin_sda = AF_I2C1_SDA_PIN(location); + break; #endif - default: - break; - } - rt_sprintf(name, "iic%d", unitNumber); - - /* Enabling clock */ - CMU_ClockEnable(iicClock, true); - - /* Reset */ - I2C_Reset(iic->iic_device); - - /* Config GPIO */ - GPIO_PinModeSet( - port_scl, - pin_scl, - gpioModeWiredAndPullUpFilter, - 1); - GPIO_PinModeSet( - port_sda, - pin_sda, - gpioModeWiredAndPullUpFilter, - 1); - - hook.type = efm32_irq_type_iic; - hook.unit = unitNumber; - hook.cbFunc = rt_hw_iic_slave_isr; - hook.userPtr = (void *)&block->device; - efm32_irq_hook_register(&hook); - - /* Enable SDZ and SCL pins and set location */ - iic->iic_device->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | \ - (location << _I2C_ROUTE_LOCATION_SHIFT); - - /* Initializing IIC */ - init.enable = false; - I2C_Init(iic->iic_device, &init); - - /* Abort current TX data and clear TX buffers */ - iic->iic_device->CMD = I2C_CMD_ABORT | I2C_CMD_CLEARPC | I2C_CMD_CLEARTX; - - /* Initialize lock */ - iic->lock = &block->lock; - if (rt_sem_init(iic->lock, name, 1, RT_IPC_FLAG_FIFO) != RT_EOK) - { - break; - } - - /* Initialize timer */ - rt_timer_init(iic->timer, name, rt_iic_timer, &iic->timeout, - IIC_TIMEOUT_PERIOD, RT_TIMER_FLAG_ONE_SHOT); - - return iic; - } while(0); - - if (iic) - { - rt_free(iic); - } - - iic_debug("IIC err: Unit %d init failed!\n", unitNumber); - return RT_NULL; + default: + break; + } + rt_sprintf(name, "iic%d", unitNumber); + + /* Enabling clock */ + CMU_ClockEnable(iicClock, true); + + /* Reset */ + I2C_Reset(iic->iic_device); + + /* Config GPIO */ + GPIO_PinModeSet( + port_scl, + pin_scl, + gpioModeWiredAndPullUpFilter, + 1); + GPIO_PinModeSet( + port_sda, + pin_sda, + gpioModeWiredAndPullUpFilter, + 1); + + hook.type = efm32_irq_type_iic; + hook.unit = unitNumber; + hook.cbFunc = rt_hw_iic_slave_isr; + hook.userPtr = (void *)&block->device; + efm32_irq_hook_register(&hook); + + /* Enable SDZ and SCL pins and set location */ + iic->iic_device->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN | \ + (location << _I2C_ROUTE_LOCATION_SHIFT); + + /* Initializing IIC */ + init.enable = false; + I2C_Init(iic->iic_device, &init); + + /* Abort current TX data and clear TX buffers */ + iic->iic_device->CMD = I2C_CMD_ABORT | I2C_CMD_CLEARPC | I2C_CMD_CLEARTX; + + /* Initialize lock */ + iic->lock = &block->lock; + if (rt_sem_init(iic->lock, name, 1, RT_IPC_FLAG_FIFO) != RT_EOK) + { + break; + } + + /* Initialize timer */ + rt_timer_init(iic->timer, name, rt_iic_timer, &iic->timeout, + IIC_TIMEOUT_PERIOD, RT_TIMER_FLAG_ONE_SHOT); + + return iic; + } while(0); + + if (iic) + { + rt_free(iic); + } + + iic_debug("IIC err: Unit %d init failed!\n", unitNumber); + return RT_NULL; } /***************************************************************************//** * @brief - * Initialize all IIC module related hardware and register IIC device to kernel + * Initialize all IIC module related hardware and register IIC device to kernel * * @details * @@ -831,39 +842,39 @@ static struct efm32_iic_device_t *rt_hw_iic_unit_init( ******************************************************************************/ void rt_hw_iic_init(void) { - struct efm32_iic_device_t *iic; - rt_uint32_t flag; - - do - { - flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX; - /* Initialize and register iic0 */ - if ((iic = rt_hw_iic_unit_init(&iic0, 0, RT_USING_IIC0)) != RT_NULL) - { - rt_hw_iic_register(&iic0.device, RT_IIC0_NAME, flag, iic); - } - else - { - break; - } + struct efm32_iic_device_t *iic; + rt_uint32_t flag; + + do + { + flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX; + /* Initialize and register iic0 */ + if ((iic = rt_hw_iic_unit_init(&iic0, 0, RT_USING_IIC0)) != RT_NULL) + { + rt_hw_iic_register(&iic0.device, RT_IIC0_NAME, flag, iic); + } + else + { + break; + } #if (I2C_COUNT > 1) - /* Initialize and register iic1 */ - if ((iic = rt_hw_iic_unit_init(&iic1, 1, RT_USING_IIC1)) != RT_NULL) - { - rt_hw_iic_register(&iic1.device, RT_IIC1_NAME, flag, iic); - } - else - { - break; - } + /* Initialize and register iic1 */ + if ((iic = rt_hw_iic_unit_init(&iic1, 1, RT_USING_IIC1)) != RT_NULL) + { + rt_hw_iic_register(&iic1.device, RT_IIC1_NAME, flag, iic); + } + else + { + break; + } #endif - iic_debug("IIC: H/W init OK!\n"); - return; - } while (0); + iic_debug("IIC: H/W init OK!\n"); + return; + } while (0); - rt_kprintf("IIC: H/W init failed!\n"); + rt_kprintf("IIC: H/W init failed!\n"); } #endif /* (defined(RT_USING_IIC0) || defined(RT_USING_IIC1)) */ diff --git a/bsp/efm32/drv_iic.h b/bsp/efm32/drv_iic.h index 53df7b42e43327e0ed1811327845218d9c8e81a5..8a67613a309d66b96271294c7b5a30628ea775c5 100644 --- a/bsp/efm32/drv_iic.h +++ b/bsp/efm32/drv_iic.h @@ -1,22 +1,23 @@ /***************************************************************************//** - * @file drv_iic.h - * @brief IIC driver of RT-Thread RTOS for EFM32 - * COPYRIGHT (C) 2011, RT-Thread Development Team - * @author onelife + * @file drv_iic.h + * @brief IIC driver of RT-Thread RTOS for EFM32 + * COPYRIGHT (C) 2011, RT-Thread Development Team + * @author onelife * @version 0.4 beta ******************************************************************************* * @section License - * The license and distribution terms for this file may be found in the file + * 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 ******************************************************************************* * @section Change Logs - * Date Author Notes - * 2011-01-07 onelife Initial creation for EFM32 - * 2011-07-11 onelife Add lock (semaphore) to prevent simultaneously + * Date Author Notes + * 2011-01-07 onelife Initial creation for EFM32 + * 2011-07-11 onelife Add lock (semaphore) to prevent simultaneously * access - * 2011-08-04 onelife Change the usage of the second parameter of Read + * 2011-08-04 onelife Change the usage of the second parameter of Read * and Write functions from (seldom used) "Offset" to "Slave address" - * 2011-08-04 onelife Add a timer to prevent from forever waiting + * 2011-08-04 onelife Add a timer to prevent from forever waiting + * 2011-12-27 onelife Change IIC read format ******************************************************************************/ #ifndef __DRV_IIC_H__ #define __DRV_IIC_H__ @@ -25,44 +26,46 @@ /* Exported types ------------------------------------------------------------*/ struct efm32_iic_int_mode_t { - rt_uint8_t *data_ptr; - rt_uint8_t data_size; - rt_uint32_t read_index, save_index; + rt_uint8_t *data_ptr; + rt_uint8_t data_size; + rt_uint32_t read_index, save_index; }; struct efm32_iic_device_t { - /* Counter */ - rt_uint32_t counter; - /* Lock */ - struct rt_semaphore *lock; - /* Pointer to timer */ - rt_timer_t timer; - /* Timeout flag */ - volatile rt_bool_t timeout; - /* State */ - rt_uint8_t state; - /* Pointer to IIC device structure */ - I2C_TypeDef *iic_device; - /* Self address */ - rt_uint16_t address; - /* RX structure */ - struct efm32_iic_int_mode_t *rx_buffer; + /* Counter */ + rt_uint32_t counter; + /* Lock */ + struct rt_semaphore *lock; + /* Pointer to timer */ + rt_timer_t timer; + /* Timeout flag */ + volatile rt_bool_t timeout; + /* State */ + rt_uint8_t state; + /* Pointer to IIC device structure */ + I2C_TypeDef *iic_device; + /* Self address */ + rt_uint16_t address; + /* RX structure */ + struct efm32_iic_int_mode_t *rx_buffer; }; struct efm32_iic_control_t { - rt_uint8_t config; - rt_uint16_t address; + rt_uint8_t config; + rt_uint16_t address; }; /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ -#define IIC_STATE_MASTER (1 << 0) +#define IIC_STATE_MASTER (1 << 0) #define IIC_STATE_BROADCAST (1 << 1) -//#define IIC_STATE_TX_BUSY (1 << 2) -#define IIC_STATE_RX_BUSY (1 << 3) -#define IIC_TIMEOUT_PERIOD (RT_TICK_PER_SECOND) +//#define IIC_STATE_TX_BUSY (1 << 2) +#define IIC_STATE_RX_BUSY (1 << 3) +#define IIC_TIMEOUT_PERIOD (RT_TICK_PER_SECOND) + +#define IIC_OP_READ_ONLY (0xFF) /* Exported functions --------------------------------------------------------*/ void rt_hw_iic_init(void); diff --git a/bsp/efm32/drv_usart.c b/bsp/efm32/drv_usart.c index dee65dbd6dec64bdabacbbb01a83245bd235f596..00751388a28560838520850e27c730f76cc31ed9 100644 --- a/bsp/efm32/drv_usart.c +++ b/bsp/efm32/drv_usart.c @@ -372,10 +372,10 @@ static rt_size_t rt_usart_read ( void *buffer, rt_size_t size) { + rt_err_t err_code; struct efm32_usart_device_t *usart; + rt_size_t read_len, len; rt_uint8_t *ptr; - rt_err_t err_code; - rt_size_t read_len; rt_uint32_t rx_flag, tx_flag, b8_flag; usart = (struct efm32_usart_device_t *)(dev->user_data); @@ -411,9 +411,8 @@ static rt_size_t rt_usart_read ( if (dev->flag & RT_DEVICE_FLAG_INT_RX) { - rt_size_t len = size; + len = size; ptr = buffer; - /* interrupt mode Rx */ while (len) { @@ -457,13 +456,6 @@ static rt_size_t rt_usart_read ( } else { - struct efm32_usart_device_t *usart; - USART_TypeDef *usart_device; - rt_size_t len; - - usart = (struct efm32_usart_device_t *)(dev->user_data); - usart_device = ((struct efm32_usart_device_t *)(dev->user_data))->usart_device; - if (usart->state & USART_STATE_SYNC) { /* SPI read */ @@ -472,67 +464,70 @@ static rt_size_t rt_usart_read ( rt_uint8_t *rx_buf = *((rt_uint8_t **)(buffer + inst_len + 1)); rt_off_t i; - ptr = rx_buf; + ptr = inst_ptr; len = inst_len; - /* Write instructions */ if (len) { if (usart->state & USART_STATE_9BIT) { - usart_device->CTRL &= ~b8_flag; + usart->usart_device->CTRL &= ~b8_flag; } while (len) { while (!(usart->usart_device->STATUS & tx_flag)); - usart->usart_device->TXDATA = (rt_uint32_t)*inst_ptr; - ++inst_ptr; --len; + usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++); + len--; } if (usart->state & USART_STATE_9BIT) { - usart_device->CTRL |= b8_flag; + usart->usart_device->CTRL |= b8_flag; } } + /* Flushing RX */ - usart_device->CMD = USART_CMD_CLEARRX; + usart->usart_device->CMD = USART_CMD_CLEARRX; /* Skip some bytes if necessary */ for (i = 0; i < pos; i++) { /* dummy write */ - while (!(usart_device->STATUS & tx_flag)); - usart_device->TXDATA = (rt_uint32_t)0xff; + while (!(usart->usart_device->STATUS & tx_flag)); + usart->usart_device->TXDATA = (rt_uint32_t)0xff; /* dummy read */ - while (!(usart_device->STATUS & rx_flag)); - *((rt_uint32_t *)0x00) = usart_device->RXDATA; + while (!(usart->usart_device->STATUS & rx_flag)); + *((rt_uint32_t *)0x00) = usart->usart_device->RXDATA; } + + ptr = rx_buf; + len = size; /* Read data */ - while (((rt_uint32_t)ptr - (rt_uint32_t)rx_buf) < size) + while (len) { /* dummy write */ - while (!(usart_device->STATUS & tx_flag)); - usart_device->TXDATA = (rt_uint32_t)0xff; + while (!(usart->usart_device->STATUS & tx_flag)); + usart->usart_device->TXDATA = (rt_uint32_t)0xff; /* read a byte of data */ - while (!(usart_device->STATUS & rx_flag)); - *ptr = usart_device->RXDATA & 0xff; - ptr ++; + while (!(usart->usart_device->STATUS & rx_flag)); + *(ptr++) = usart->usart_device->RXDATA & 0xff; + len--; } } else { ptr = buffer; - + len = size; /* polling mode */ - while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size) + while (len) { - while (usart_device->STATUS & rx_flag) + while (usart->usart_device->STATUS & rx_flag) { - *ptr = usart_device->RXDATA & 0xff; - ptr ++; + *(ptr++) = usart->usart_device->RXDATA & 0xff; } } + len--; } - read_len = size; + read_len = size - len; } /* Unlock device */ @@ -540,6 +535,7 @@ static rt_size_t rt_usart_read ( /* set error code */ rt_set_errno(err_code); + return read_len; } @@ -573,9 +569,10 @@ static rt_size_t rt_usart_write ( rt_size_t size) { rt_err_t err_code; - rt_size_t write_size = 0; struct efm32_usart_device_t* usart = (struct efm32_usart_device_t*)(dev->user_data); - rt_uint8_t *tx_buf; + rt_size_t read_len, len; + rt_uint8_t *ptr; + rt_size_t write_size = 0; rt_uint32_t tx_flag, b8_flag; #if defined(UART_PRESENT) @@ -610,23 +607,25 @@ static rt_size_t rt_usart_write ( { /* SPI write */ rt_uint8_t inst_len = *((rt_uint8_t *)buffer); rt_uint8_t *inst_ptr = (rt_uint8_t *)(buffer + 1); - tx_buf = *((rt_uint8_t **)(buffer + inst_len + 1)); + rt_uint8_t *tx_buf = *((rt_uint8_t **)(buffer + inst_len + 1)); + ptr = inst_ptr; + len = inst_len; /* Write instructions */ - if (inst_len) + if (len) { if (usart->state & USART_STATE_9BIT) { usart->usart_device->CTRL &= ~b8_flag; } - if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (inst_len > 2)) + if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (len > 2)) { /* DMA mode Tx */ struct efm32_usart_dma_mode_t *dma_tx; - usart_debug("USART: DMA TX INS (%d)\n", inst_len); + usart_debug("USART: DMA TX INS (%d)\n", len); dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode); - dma_tx->data_ptr = (rt_uint32_t *)inst_ptr; - dma_tx->data_size = inst_len; + dma_tx->data_ptr = (rt_uint32_t *)ptr; + dma_tx->data_size = len; usart->state |= USART_STATE_TX_BUSY; DMA_ActivateBasic( @@ -634,8 +633,8 @@ static rt_size_t rt_usart_write ( true, false, (void *)&(usart->usart_device->TXDATA), - (void *)inst_ptr, - (rt_uint32_t)(inst_len - 1)); + (void *)ptr, + (rt_uint32_t)(len - 1)); /* Wait, otherwise the TX buffer is overwrite */ // TODO: This function blocks the process => goto low power mode? // if (usart->state & USART_STATE_CONSOLE) @@ -652,15 +651,12 @@ static rt_size_t rt_usart_write ( } else { /* polling mode */ - rt_uint8_t *ptr = (rt_uint8_t *)inst_ptr; - rt_size_t len = inst_len; - - usart_debug("USART: Polling TX INS (%d)\n", inst_len); + usart_debug("USART: Polling TX INS (%d)\n", len); while (len) { while (!(usart->usart_device->STATUS & tx_flag)); - usart->usart_device->TXDATA = (rt_uint32_t)*ptr; - ++ptr; --len; + usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++); + len--; } } if (usart->state & USART_STATE_9BIT) @@ -668,30 +664,33 @@ static rt_size_t rt_usart_write ( usart->usart_device->CTRL |= b8_flag; } } + + ptr = tx_buf; } else { - tx_buf = (rt_uint8_t *)buffer; + ptr = (rt_uint8_t *)buffer; } + len = size; /* Write data */ if (dev->flag & RT_DEVICE_FLAG_STREAM) { - if (*(tx_buf + size - 1) == '\n') + if (*(ptr + len - 1) == '\n') { - *(tx_buf + size - 1) = '\r'; - *(tx_buf + size++) = '\n'; - *(tx_buf + size) = 0; + *(ptr + len - 1) = '\r'; + *(ptr + len++) = '\n'; + *(ptr + len) = 0; } } - if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (size > 2)) + if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (len > 2)) { /* DMA mode Tx */ struct efm32_usart_dma_mode_t *dma_tx; - usart_debug("USART: DMA TX data (%d)\n", size); + usart_debug("USART: DMA TX data (%d)\n", len); dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode); - dma_tx->data_ptr = (rt_uint32_t *)tx_buf; - dma_tx->data_size = size; + dma_tx->data_ptr = (rt_uint32_t *)ptr; + dma_tx->data_size = len; usart->state |= USART_STATE_TX_BUSY; DMA_ActivateBasic( @@ -699,8 +698,8 @@ static rt_size_t rt_usart_write ( true, false, (void *)&(usart->usart_device->TXDATA), - (void *)tx_buf, - (rt_uint32_t)(size - 1)); + (void *)ptr, + (rt_uint32_t)(len - 1)); /* Wait, otherwise the TX buffer is overwrite */ // TODO: This function blocks the process => goto low power mode? @@ -719,18 +718,15 @@ static rt_size_t rt_usart_write ( } else { /* polling mode */ - rt_uint8_t *ptr = (rt_uint8_t *)tx_buf; - rt_size_t len = size; - - usart_debug("USART: Polling TX data (%d)\n", size); + usart_debug("USART: Polling TX data (%d)\n", len); while (len) { while (!(usart->usart_device->STATUS & tx_flag)); - usart->usart_device->TXDATA = (rt_uint32_t)*ptr; - ++ptr; --len; + usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++); + len--; } - write_size = (rt_size_t)ptr - (rt_size_t)tx_buf; + write_size = size - len; } /* Unlock device */ @@ -738,6 +734,7 @@ static rt_size_t rt_usart_write ( /* set error code */ rt_set_errno(err_code); + return write_size; } @@ -1396,7 +1393,7 @@ static struct efm32_usart_device_t *rt_hw_usart_unit_init( } if (callback) { - rt_free(usart); + rt_free(callback); } #if defined(UART_PRESENT) @@ -1436,7 +1433,7 @@ void rt_hw_usart_init(void) #ifdef RT_USART0_SYNC_MODE config |= USART_STATE_SYNC; config |= (RT_USART0_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT; - #if (!(RT_USART0_SYNC_MODE & EFM32_SPI_MASTER)) + #if (!((RT_USART0_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER)) flag |= RT_DEVICE_FLAG_INT_RX; #endif #else @@ -1485,7 +1482,7 @@ void rt_hw_usart_init(void) #ifdef RT_USART1_SYNC_MODE config |= USART_STATE_SYNC; config |= (RT_USART1_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT; - #if (!(RT_USART1_SYNC_MODE & EFM32_SPI_MASTER)) + #if (!((RT_USART1_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER)) flag |= RT_DEVICE_FLAG_INT_RX; #endif #else @@ -1535,7 +1532,7 @@ void rt_hw_usart_init(void) #ifdef RT_USART2_SYNC_MODE config |= USART_STATE_SYNC; config |= (RT_USART1_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT; - #if (!(RT_USART2_SYNC_MODE & EFM32_SPI_MASTER)) + #if (!((RT_USART2_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER)) flag |= RT_DEVICE_FLAG_INT_RX; #endif #else diff --git a/bsp/efm32/graphics/SConscript b/bsp/efm32/graphics/SConscript index aa6db6c73274c2a63b35222bd22cb531227fa2e8..de28cece152c720d98292597b26a0de77a71c83d 100644 --- a/bsp/efm32/graphics/SConscript +++ b/bsp/efm32/graphics/SConscript @@ -1,7 +1,7 @@ import rtconfig from building import * -if rtconfig.EFM32_LCD == 'Mapped': +if rtconfig.EFM32_LCD == 'LCD_MAPPED': src = Split(""" dmd/ssd2119/dmd_ssd2119_16bit.c dmd/ssd2119/dmdif_ssd2119_ebi16.c @@ -10,5 +10,13 @@ if rtconfig.EFM32_LCD == 'Mapped': CPPPATH.append(GetCurrentDir() + '/dmd/ssd2119') group = DefineGroup('EFM32GG_DK3750_LCD', src, depend = [''], CPPPATH = CPPPATH) Return('group') +elif rtconfig.EFM32_LCD == 'LCD_DIRECT': + src = Split(""" + dmd/ssd2119/dmd_ssd2119_direct.c + """) + CPPPATH = [GetCurrentDir()] + CPPPATH.append(GetCurrentDir() + '/dmd/ssd2119') + group = DefineGroup('EFM32GG_DK3750_LCD', src, depend = [''], CPPPATH = CPPPATH) + Return('group') else: Return('') diff --git a/bsp/efm32/graphics/tftspi.h b/bsp/efm32/graphics/tftspi.h new file mode 100644 index 0000000000000000000000000000000000000000..28e694402b4a3194678edb612113dedd4e86b674 --- /dev/null +++ b/bsp/efm32/graphics/tftspi.h @@ -0,0 +1,29 @@ +/***************************************************************************//** + * @file tftspi.h + * @brief Stub functions of EFM32 LCD driver + * COPYRIGHT (C) 2011, RT-Thread Development Team + * @author onelife + * @version 0.4 beta + ******************************************************************************* + * @section License + * 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 + ******************************************************************************* + * @section Change Logs + * Date Author Notes + * 2011-12-20 onelife Initial creation for EFM32 + ******************************************************************************/ +#ifndef __TFTSPI_H__ +#define __TFTSPI_H__ + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +#define SPI_TFT_Init() +#define SPI_TFT_WriteRegister(reg, data) efm32_spiLcd_writeRegister(reg, data) + +/* Exported functions ------------------------------------------------------- */ +extern rt_err_t efm32_spiLcd_writeRegister(rt_uint8_t reg, rt_uint16_t data); + +#endif /* __TFTSPI_H__ */ diff --git a/bsp/efm32/httpd.c b/bsp/efm32/httpd.c index fc6e5dbd3525e932f1ee3efcedad54a11b9a8837..9280496d4d74a8d4e113d093aa80c0075846e811 100644 --- a/bsp/efm32/httpd.c +++ b/bsp/efm32/httpd.c @@ -83,8 +83,8 @@ #include "dev_misc.h" #if defined(RT_USING_LWIP) && defined(EFM32_USING_ETH_HTTPD) -#include "lwip\tcp.h" -#include "lwip\ip_addr.h" +#include "lwip/tcp.h" +#include "lwip/ip_addr.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ diff --git a/bsp/efm32/rtconfig.h b/bsp/efm32/rtconfig.h index ac4f09c51ec868d5cf46006059e0e4de9a9a7c41..33507d515c5112ab850103629d84d41fcb59cddb 100644 --- a/bsp/efm32/rtconfig.h +++ b/bsp/efm32/rtconfig.h @@ -55,7 +55,7 @@ //#define EFM32_SDCARD_DEBUG #define EFM32_ETHERNET_DEBUG #define EFM32_LCD_DEBUG - +//#define EFM32_KEYS_DEBUG /* Using Hook */ //#define RT_USING_HOOK @@ -106,7 +106,7 @@ #if defined(EFM32GG_DK3750_USING_LEUART1) #define RT_USING_LEUART1 (0x0UL) #define RT_LEUART1_NAME "debug0" - #define RT_LEUART1_USING_DMA (0x0UL) + //#define RT_LEUART1_USING_DMA (0x0UL) #else #define RT_USING_UART1 (0x2UL) #define RT_UART1_NAME "debug" @@ -228,6 +228,7 @@ #define EFM32_USING_SPISD /* MicroSD card */ //#define EFM32_USING_ETHERNET /* Ethernet controller */ #define EFM32_USING_LCD /* TFT LCD */ +#define EFM32_USING_KEYS /* Keys and joystick */ #endif #if defined(EFM32_USING_ACCEL) @@ -333,7 +334,7 @@ /* SECTION: RTGUI support */ #if defined(EFM32_USING_LCD) #define LCD_USING_DEVICE_NAME RT_USART1_NAME -#define LCD_DEVICE_NAME "spiLcd" +#define LCD_DEVICE_NAME "lcd" /* using RTGUI support */ #define RT_USING_RTGUI @@ -354,10 +355,11 @@ /* use small size in RTGUI */ /* #define RTGUI_USING_SMALL_SIZE */ /* use mouse cursor */ -/* #define RTGUI_USING_MOUSE_CURSOR */ +#define RTGUI_USING_MOUSE_CURSOR /* RTGUI image options */ #define RTGUI_IMAGE_XPM //#define RTGUI_IMAGE_JPEG +#define RTGUI_IMAGE_TJPGD //#define RTGUI_IMAGE_PNG #define RTGUI_IMAGE_BMP #endif /* defined(EFM32_USING_LCD) */ diff --git a/bsp/efm32/rtconfig.py b/bsp/efm32/rtconfig.py index bfe546a01bd0de0278f06c20928838fac4db1b8a..d4dd8634346597ca2c7c99bfc7e67656c0c8edc1 100644 --- a/bsp/efm32/rtconfig.py +++ b/bsp/efm32/rtconfig.py @@ -8,7 +8,7 @@ if CROSS_TOOL == 'gcc': EXEC_PATH = 'C:\Program Files (x86)\CodeSourcery\Sourcery G++ Lite\bin' #EXEC_PATH = 'C:\Program Files (x86)\yagarto\bin' -BUILD = 'run' +BUILD = 'debug' # EFM32_BOARD = 'EFM32_G8XX_STK' # EFM32_BOARD = 'EFM32_GXXX_DK' EFM32_BOARD = 'EFM32GG_DK3750' @@ -22,8 +22,8 @@ elif EFM32_BOARD == 'EFM32_GXXX_DK': elif EFM32_BOARD == 'EFM32GG_DK3750': EFM32_FAMILY = 'Giant Gecko' EFM32_TYPE = 'EFM32GG990F1024' - EFM32_LCD = 'Mapped' -# EFM32_LCD = 'Direct' +# EFM32_LCD = 'LCD_MAPPED' + EFM32_LCD = 'LCD_DIRECT' if PLATFORM == 'gcc': # toolchains diff --git a/bsp/efm32/startup.c b/bsp/efm32/startup.c index 64db1125b0ec4d6cd88369c333d681e36ccacff7..8eadfb47ad169cb9c5fe00e647da3256da6a5056 100644 --- a/bsp/efm32/startup.c +++ b/bsp/efm32/startup.c @@ -1,5 +1,5 @@ /***************************************************************************//** - * @file interrupt.c + * @file startup.c * @brief This file is part of RT-Thread RTOS * COPYRIGHT (C) 2011, RT-Thread Development Team * @author Bernard, onelife @@ -23,7 +23,6 @@ /* Includes ------------------------------------------------------------------*/ #include "board.h" -#include /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ @@ -39,15 +38,9 @@ extern int __bss_end; /* Private variables ---------------------------------------------------------*/ /* External function prototypes ----------------------------------------------*/ -extern int rt_application_init(void); -#ifdef RT_USING_FINSH -extern void finsh_system_init(void); -extern void finsh_set_device(const char* device); -#endif - /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ -#ifdef DEBUG +#ifdef RT_DEBUG /***************************************************************************//** * @brief * Reports the name of the source file and the source line number where the @@ -63,7 +56,7 @@ extern void finsh_set_device(const char* device); * @param[in] line * Assert error line source number ******************************************************************************/ -void assert_failed(u8* file, u32 line) +void assert_failed(uint8_t * file, uint32_t line) { rt_kprintf("\n\r Wrong parameter value detected on\r\n"); rt_kprintf(" file %s\r\n", file);