board_info.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. /*****************************************************************************
  27. * Include Section
  28. * add all #include here
  29. *****************************************************************************/
  30. #include "board_info.h"
  31. #include <rtdevice.h>
  32. #include <string.h>
  33. /*****************************************************************************
  34. * Define section
  35. * add all #define here
  36. *****************************************************************************/
  37. struct fh_board_info_list_node {
  38. struct fh_board_info obj;
  39. rt_list_t list;
  40. };
  41. #define CHECK_TEST_LIST_EMPTY \
  42. if(rt_list_isempty(&board_info_head.list)) \
  43. rt_kprintf("board info is null...please register first..\n")
  44. /****************************************************************************
  45. * ADT section
  46. * add definition of user defined Data Type that only be used in this file here
  47. ***************************************************************************/
  48. #define list_for_each_entry_safe(pos, n, head, member) \
  49. for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
  50. n = rt_list_entry(pos->member.next, typeof(*pos), member); \
  51. &pos->member != (head); \
  52. pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
  53. #define PARA_ERROR (-1)
  54. #define PROBE_FUNC_MISS (-2)
  55. /******************************************************************************
  56. * Function prototype section
  57. * add prototypes for all functions called by this file,execepting those
  58. * declared in header file
  59. *****************************************************************************/
  60. //static void fh81_soc_free_all_test(void);
  61. //static void fh81_soc_print_all_test(void);
  62. //static void fh81_soc_auto_test_all(void);
  63. //static void fh81_soc_test_help(void);
  64. /*****************************************************************************
  65. * Global variables section - Exported
  66. * add declaration of global variables that will be exported here
  67. * e.g.
  68. * int8_t foo;
  69. ****************************************************************************/
  70. /*****************************************************************************
  71. * static fun;
  72. *****************************************************************************/
  73. static struct fh_board_info_list_node board_info_head;
  74. /*****************************************************************************
  75. * Global variables section - Local
  76. * define global variables(will be refered only in this file) here,
  77. * static keyword should be used to limit scope of local variable to this file
  78. * e.g.
  79. * static uint8_t ufoo;
  80. *****************************************************************************/
  81. /* function body */
  82. /*****************************************************************************
  83. * Description:
  84. * add funtion description here
  85. * Parameters:
  86. * description for each argument, new argument starts at new line
  87. * Return:
  88. * what does this function returned?
  89. *****************************************************************************/
  90. int fh_board_info_init(void) {
  91. memset(&board_info_head, 0x0, sizeof(struct fh_board_info_list_node));
  92. rt_list_init(&board_info_head.list);
  93. board_info_head.obj.name = "NO INFO";
  94. return 0;
  95. }
  96. void fh_free_all_info(void) {
  97. rt_list_t *p_list;
  98. struct fh_board_info_list_node *info_node;
  99. struct fh_board_info_list_node *_info_node;
  100. p_list = &board_info_head.list;
  101. CHECK_TEST_LIST_EMPTY;
  102. list_for_each_entry_safe(info_node, _info_node, p_list, list)
  103. {
  104. if (info_node->obj.ops->exit) {
  105. info_node->obj.ops->exit(info_node->obj.data);
  106. }
  107. rt_kprintf("soc free list name:(%s)\n", info_node->obj.name);
  108. rt_free(info_node);
  109. }
  110. fh_board_info_init();
  111. }
  112. void fh_print_all_board_info(void) {
  113. rt_list_t *p_list;
  114. struct fh_board_info_list_node *info_node;
  115. struct fh_board_info_list_node *_info_node;
  116. p_list = &board_info_head.list;
  117. CHECK_TEST_LIST_EMPTY;
  118. list_for_each_entry_safe(info_node, _info_node, p_list, list)
  119. {
  120. rt_kprintf("%s\n", info_node->obj.name);
  121. }
  122. }
  123. /*****************************************************************************
  124. * Description:
  125. * add funtion description here
  126. * Parameters:
  127. * description for each argument, new argument starts at new line
  128. * Return:
  129. * what does this function returned?
  130. *****************************************************************************/
  131. //register the platform info such as base add,isr no..
  132. //caution:do not free the name and data because of here not copy
  133. struct fh_board_info *fh_board_info_register(char *info_name, void *data) {
  134. rt_list_t *p_list;
  135. struct fh_board_info_list_node *new_node;
  136. struct fh_board_info_list_node *info_node;
  137. struct fh_board_info_list_node *_info_node;
  138. p_list = &board_info_head.list;
  139. if (RT_NULL == info_name || RT_NULL == data) {
  140. rt_kprintf("info name or info data is NULL!\n");
  141. return RT_NULL;
  142. }
  143. //check if the func is already in the test list....
  144. #if(0)
  145. list_for_each_entry_safe(info_node, _info_node, p_list, list) {
  146. if (!memcmp(info_node->obj.name, info_name, strlen(info_name))) {
  147. rt_kprintf("info_name(%s) is already registered\n", info_name);
  148. return RT_NULL;
  149. }
  150. }
  151. #endif
  152. new_node = (struct fh_board_info_list_node *) rt_malloc(
  153. sizeof(struct fh_board_info_list_node));
  154. if (!new_node) {
  155. rt_kprintf("malloc new_list_node failed~\n");
  156. return RT_NULL;
  157. }
  158. new_node->obj.name = info_name;
  159. new_node->obj.data = data;
  160. //here insert "before" and test is "after" will make the list like a fifo...
  161. rt_list_insert_before(&board_info_head.list, &new_node->list);
  162. return &new_node->obj;
  163. }
  164. //back the platform info
  165. static void *fh_get_board_info_data(char *info_name) {
  166. rt_list_t *p_list;
  167. struct fh_board_info_list_node *info_node;
  168. struct fh_board_info_list_node *_info_node;
  169. p_list = &board_info_head.list;
  170. //check info name
  171. if (RT_NULL == info_name) {
  172. rt_kprintf("info name is NULL!\n");
  173. return RT_NULL;
  174. }
  175. CHECK_TEST_LIST_EMPTY;
  176. list_for_each_entry_safe(info_node, _info_node, p_list, list)
  177. {
  178. if (!strcmp(info_node->obj.name, info_name)) {
  179. return info_node->obj.data;
  180. }
  181. }
  182. rt_kprintf("Can't find the board info name:%s\n", info_name);
  183. }
  184. int fh_board_driver_register(char *info_name, struct fh_board_ops *ops) {
  185. rt_list_t *p_list;
  186. struct fh_board_info_list_node *new_node;
  187. struct fh_board_info_list_node *info_node;
  188. struct fh_board_info_list_node *_info_node;
  189. p_list = &board_info_head.list;
  190. if (RT_NULL == info_name || RT_NULL == ops) {
  191. rt_kprintf("info name or ops func is NULL!\n");
  192. return PARA_ERROR;
  193. }
  194. list_for_each_entry_safe(info_node, _info_node, p_list, list)
  195. {
  196. if (!strcmp(info_node->obj.name, info_name)) {
  197. info_node->obj.ops = ops;
  198. if (info_node->obj.ops->probe) {
  199. info_node->obj.ops->probe(info_node->obj.data);
  200. }
  201. //return info_node->obj.data;
  202. }
  203. }
  204. //rt_kprintf("Can't find the board info name:%s\n",info_name);
  205. return 0;
  206. }