将AWTK移植到aworks平台

AWTK的可移植性很高,在移植时只需要实现平台初始化、lcd和mainloop三个方面的东西。本文以aworks-rt1052平台为例介绍移植AWTK到新平台的过程。

一、平台初始化

除了基本的libc函数外,AWTK对平台没有特别要求,在aworks上没有函数gettimeofday,所以要实现一个获取当前时间的函数get_time_ms。另外需要给GUI分配一块内存空间,并调用tk_mem_init。

  1. #include "aw_system.h"
  2. #include "aw_delay.h"
  3. #include "base/mem.h"
  4. #include "base/timer.h"
  5. uint32_t get_time_ms() {
  6. return aw_sys_tick_get();
  7. }
  8. void sleep_ms(uint32_t ms) {
  9. aw_mdelay(ms);
  10. }
  11. #define TK_MEM_SIZE 4 * 1024 * 1024
  12. ret_t platform_prepare(void) {
  13. uint32_t* mem = (uint32_t*) aw_mem_alloc(TK_MEM_SIZE);
  14. return_value_if_fail(mem != NULL, RET_OOM);
  15. tk_mem_init(mem, TK_MEM_SIZE);
  16. return RET_OK;
  17. }

参考:awtk-port/platform.c

二、实现lcd

aworks使用基于framebuffer的lcd的缺省实现,只需要调用函数lcd_mem_create_double_fb即可。

  1. #include "aw_emwin_fb.h"
  2. #include "aw_sem.h"
  3. #include "aw_cache.h"
  4. #include "aw_delay.h"
  5. #include "aw_vdebug.h"
  6. #include <string.h>
  7. #include "base/types_def.h"
  8. static uint32_t *s_frame_buffer = 0;
  9. static uint32_t *s_offline_frame_buffer = 0;
  10. static aw_emwin_fb_info_t *s_awtk_fb = NULL;
  11. uint32_t* aworks_get_online_fb(void) {
  12. return s_frame_buffer;
  13. }
  14. uint32_t* aworks_get_offline_fb(void) {
  15. return s_offline_frame_buffer;
  16. }
  17. aw_emwin_fb_info_t* aworks_lcd_init(void) {
  18. int fb_size = 0;
  19. s_awtk_fb = aw_emwin_fb_open("imx1050_emwin_fb", 0);
  20. return_value_if_fail(s_awtk_fb != NULL, NULL);
  21. fb_size = s_awtk_fb->x_res * s_awtk_fb->y_res * 2;
  22. s_frame_buffer = (uint32_t*) aw_cache_dma_malloc(fb_size);
  23. return_value_if_fail(s_frame_buffer != NULL, NULL);
  24. memset(s_frame_buffer, 0x00, fb_size);
  25. s_offline_frame_buffer = (uint32_t*) aw_cache_dma_malloc(fb_size);
  26. return_value_if_fail(s_offline_frame_buffer != NULL, NULL);
  27. memset(s_offline_frame_buffer, 0x00, fb_size);
  28. return_value_if_fail(
  29. AW_OK == aw_emwin_fb_init(s_awtk_fb, s_frame_buffer, AW_FB_VERTICAL_SCREEN),
  30. NULL);
  31. aw_emwin_fb_run(s_awtk_fb);
  32. aw_emwin_fb_backlight(s_awtk_fb, 100);
  33. return s_awtk_fb;
  34. }

参考 awtk-port/lcd_aworks.c

三、实现main_loop

main_loop主要负责事件分发和绘制这个不断循环的过程。main_loop_raw.inc里实现了裸系统main_loop的基本功能,在移植时加上输入事件的的分发即可:

  1. static aw_ts_id ts_app_init(void) {
  2. aw_ts_id sys_ts = sys_ts = aw_ts_serv_id_get("480x272", 0, 0);
  3. return_value_if_fail(sys_ts != NULL, NULL);
  4. return_value_if_fail(aw_ts_calc_data_read(sys_ts) == AW_OK, NULL);
  5. return sys_ts;
  6. }
  7. ret_t platform_disaptch_input(main_loop_t* loop) {
  8. int tsret = 0;
  9. static aw_ts_id ts_id = NULL;
  10. struct aw_ts_state ts_state;
  11. memset(&ts_state, 0x00, sizeof(ts_state));
  12. if (ts_id == NULL) {
  13. ts_id = ts_app_init();
  14. }
  15. return_value_if_fail(ts_id != NULL, RET_FAIL);
  16. tsret = aw_ts_exec(ts_id, &ts_state, 1);
  17. if (tsret >= 0) {
  18. if (ts_state.pressed) {
  19. main_loop_post_pointer_event(loop, ts_state.pressed, ts_state.x,
  20. ts_state.y);
  21. } else {
  22. main_loop_post_pointer_event(loop, ts_state.pressed, ts_state.x,
  23. ts_state.y);
  24. }
  25. }
  26. return RET_OK;
  27. }
  28. extern uint32_t* aworks_get_online_fb(void);
  29. extern uint32_t* aworks_get_offline_fb(void);
  30. lcd_t* platform_create_lcd(wh_t w, wh_t h) {
  31. lcd_t* lcd = lcd_mem_rgb565_create_double_fb(w, h, (uint8_t*) aworks_get_online_fb(),
  32. (uint8_t*) aworks_get_offline_fb());
  33. return lcd;
  34. }
  35. #include "main_loop/main\_loop_raw.inc"

参考 awtk-port/main_loop_aworks.c

完整项目和源码请参考:awtk-aworks-rt1052