common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file common.h
  30. * @brief Contains all functions support for common function driver£¬such as string function,you can use this driver for spi,adc,uart and so on
  31. * @version 0.0
  32. * @date 19. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __COMMON__H__
  40. #define __COMMON__H__
  41. #include "types.h"
  42. #define CLAER_RTC_COUNT *(volatile unsigned int *)0x4000f024 |= BIT(1)
  43. #define RUN_RTC *(volatile unsigned int *)0x4000f024 |= BIT(0)
  44. #define STOP_RTC *(volatile unsigned int *)0x4000f024 &= ~BIT(0)
  45. #define TIME_BASE (1000000) // 24bit count shift 2 bit as 1us/bit
  46. #define TIME_DELTA(x,y) ( (x>=y) ? x-y : TIME_BASE-y+x )
  47. /**************************************************************************************
  48. * @fn hal_setMem
  49. *
  50. * @brief This function process for set some memory addr with a value
  51. *
  52. * input parameters
  53. *
  54. * @param uint8_t *buf: set memory buffer
  55. * uint8_t value: memory value
  56. * uint32_t length: set memory length
  57. *
  58. * output parameters
  59. *
  60. * @param uint8_t *buf: set memory buffer
  61. *
  62. * @return None.
  63. **************************************************************************************/
  64. void hal_setMem(uint8_t *buf, uint8_t value, uint32_t length);
  65. /**************************************************************************************
  66. * @fn hal_cpyMem
  67. *
  68. * @brief This function process for copying data from source addr to dest addr,once copy one byte
  69. *
  70. * input parameters
  71. *
  72. * @param uint8_t *dst: copy destnation buffer
  73. * uint8_t *src: copy source buffer
  74. * uint32_t length: copy length
  75. *
  76. * output parameters
  77. *
  78. * @param uint8_t *dst: copy destnation buffer
  79. *
  80. * @return None.
  81. **************************************************************************************/
  82. void hal_cpyMem(uint8_t *dst, uint8_t *src, uint32_t length);
  83. /**************************************************************************************
  84. * @fn hal_cpyMem32
  85. *
  86. * @brief This function process for copying data from source addr to dest addr,once copy 4 bytes
  87. *
  88. * input parameters
  89. *
  90. * @param uint32_t *dst: copy destnation buffer
  91. * uint32_t *src: copy source buffer
  92. * uint32_t length: copy length
  93. *
  94. * output parameters
  95. *
  96. * @param uint32_t *dst: copy destnation buffer
  97. *
  98. * @return None.
  99. **************************************************************************************/
  100. void hal_cpyMem32(uint32_t *dst, uint32_t *src, uint32_t length);
  101. /**************************************************************************************
  102. * @fn hal_my_strcmp
  103. *
  104. * @brief This function process for compare two strings, return 1 means same, 0 means different
  105. *
  106. * input parameters
  107. *
  108. * @param const uint8_t *str: the first string
  109. * const uint8_t *ptr: the second string
  110. *
  111. * output parameters
  112. *
  113. * @param None.
  114. *
  115. * @return 1:the same
  116. * 0:the different
  117. **************************************************************************************/
  118. int hal_my_strcmp( const uint8_t *str, const uint8_t *ptr);
  119. /**************************************************************************************
  120. * @fn hal_copy_bin_from_flash_to_sram
  121. *
  122. * @brief This function process for copy bin from flash to sram
  123. *
  124. * input parameters
  125. *
  126. * @param int toAddr: destnation address
  127. * int fromAddr: source address
  128. * int length: copy length
  129. *
  130. * output parameters
  131. *
  132. * @param (uint8_t *) toAddr: destnation buffer
  133. *
  134. * @return None.
  135. **************************************************************************************/
  136. void hal_copy_bin_from_flash_to_sram( int toAddr, int fromAddr, int length);
  137. /**************************************************************************************
  138. * @fn hal_my_sizeof
  139. *
  140. * @brief This function process for calculate the string length
  141. *
  142. * input parameters
  143. *
  144. * @param const uint8_t *str: the source string
  145. *
  146. * output parameters
  147. *
  148. * @param None.
  149. *
  150. * @return the string length(int)
  151. **************************************************************************************/
  152. int hal_my_sizeof(const uint8_t *str);
  153. /**************************************************************************************
  154. * @fn hal_my_strlen
  155. *
  156. * @brief This function process for calculate the string length,PS:the char[] must give the '\0'
  157. *
  158. * input parameters
  159. *
  160. * @param const uint8_t *str: the source string
  161. *
  162. * output parameters
  163. *
  164. * @param None.
  165. *
  166. * @return the string length(int)
  167. **************************************************************************************/
  168. int hal_my_strlen(const uint8_t *str);
  169. /**************************************************************************************
  170. * @fn hal_convert_hex_to_char
  171. *
  172. * @brief This function process for convert a hex data to ASCII code type,include the 0x symbol
  173. *
  174. * input parameters
  175. *
  176. * @param unsigned char *ch: the char string buffer
  177. * unsigned int data: the source hex data
  178. *
  179. * output parameters
  180. *
  181. * @param unsigned char *ch: the char string buffer
  182. *
  183. * @return None.
  184. **************************************************************************************/
  185. void hal_convert_hex_to_char(unsigned char *ch, unsigned int data);
  186. /**************************************************************************************
  187. * @fn hal_convert_hex_to_char_wo_x
  188. *
  189. * @brief This function process for convert a hex data to ASCII code type,without the 0x symbol
  190. *
  191. * input parameters
  192. *
  193. * @param unsigned char *ch: the char string buffer
  194. * unsigned int data: the source hex data
  195. *
  196. * output parameters
  197. *
  198. * @param unsigned char *ch: the char string buffer
  199. *
  200. * @return None.
  201. **************************************************************************************/
  202. void hal_convert_hex_to_char_wo_x(unsigned char *ch, unsigned int data);
  203. /**************************************************************************************
  204. * @fn hal_convert_char_to_hex
  205. *
  206. * @brief This function process for convert a data from ASCII code to hex type
  207. *
  208. * input parameters
  209. *
  210. * @param const unsigned char *ch: the source char string
  211. *
  212. * output parameters
  213. *
  214. * @param None.
  215. *
  216. * @return hex data
  217. **************************************************************************************/
  218. uint32_t hal_convert_char_to_hex(const unsigned char *ch);
  219. /**************************************************************************************
  220. * @fn hal_convert_char_to_dec
  221. *
  222. * @brief This function process for convert a data from ASCII code to decimal type
  223. *
  224. * input parameters
  225. *
  226. * @param const unsigned char *ch: the source char string
  227. *
  228. * output parameters
  229. *
  230. * @param None.
  231. *
  232. * @return decimal data
  233. **************************************************************************************/
  234. uint32_t hal_convert_char_to_dec(const unsigned char *ch);
  235. /**************************************************************************************
  236. * @fn hal_is_an_valid_number
  237. *
  238. * @brief This function process for judge if a char is hex number or not, return 1 means yes, 0 means no
  239. *
  240. * input parameters
  241. *
  242. * @param uint8_t ch: the source data
  243. *
  244. * output parameters
  245. *
  246. * @param None.
  247. *
  248. * @return 1: yes
  249. * 0: no
  250. **************************************************************************************/
  251. int hal_is_an_valid_number(uint8_t ch);
  252. /**************************************************************************************
  253. * @fn clock_time_rtc
  254. *
  255. * @brief This function process for return rtc count
  256. *
  257. * input parameters
  258. *
  259. * @param None.
  260. *
  261. * output parameters
  262. *
  263. * @param None.
  264. *
  265. * @return RTC count
  266. **************************************************************************************/
  267. //static inline uint32_t clock_time_rtc(void){
  268. // return (*(volatile unsigned int *)0x4000f028) & 0xffffff;
  269. //}
  270. //
  271. extern uint32_t clock_time_rtc(void);
  272. /**************************************************************************************
  273. * @fn subWriteReg
  274. *
  275. * @brief This function process for write register with sub bit
  276. *
  277. * input parameters
  278. *
  279. * @param uint32_t addr: register address
  280. * uint8_t hOff: high bit offset
  281. * uint8_t lOff: low bit offset
  282. * uint32_t value: write value
  283. *
  284. * output parameters
  285. *
  286. * @param None.
  287. *
  288. * @return None.
  289. **************************************************************************************/
  290. void subWriteReg(uint32_t addr,uint8_t hOff,uint8_t lOff,uint32_t value);
  291. /**************************************************************************************
  292. * @fn WaitMs
  293. *
  294. * @brief This function process for wait program msecond,use RTC
  295. *
  296. * input parameters
  297. *
  298. * @param uint32_t msecond: the msecond value
  299. *
  300. * output parameters
  301. *
  302. * @param None.
  303. *
  304. * @return None.
  305. **************************************************************************************/
  306. void WaitMs(uint32_t msecond);
  307. #define WR_REG(a,b) *(volatile unsigned int *)a=b
  308. #define RD_REG(a) *(volatile unsigned int *)a
  309. #define ADD_IDX(a,b) (a==b)? a=0:a++;
  310. //below is system initial defination
  311. #define ENABLE_XTAL_TRIGGER *(volatile unsigned int *)0x4000f040 |= BIT(16)
  312. #define ENABLE_XTAL_OUTPUT *(volatile unsigned int *)0x4000f040 |= BIT(18)
  313. #define ENABLE_DLL *(volatile unsigned int *)0x4000f044 |= BIT(7)
  314. #define DLL32M_CLOCK_ENABLE *(volatile unsigned int *)0x4000f044 |= BIT(13)
  315. #define DLLn_CLOCK_ENABLE(n) *(volatile unsigned int *)0x4000f044 |= BIT(n+11)
  316. #define DBLE_CLOCK_DISABLE *(volatile unsigned int *)0x4000f044 &= ~BIT(21)
  317. #define CLK_1P28M_ENABLE *(volatile unsigned int *)0x4000f03c |= BIT(6) //the source is 32M dll
  318. #define HCLK_DIV2_ENABLE *(volatile unsigned int *)0x4000f03c |= BIT(15) //use carefully
  319. #define PCLK_DIV_ENABLE *(volatile unsigned int *)0x4000001c & 0x02
  320. enum H_SYSCLK_SEL {
  321. SYS_CLK_RC_32M = 0,
  322. SYS_CLK_DLL_32M = 1,
  323. SYS_CLK_XTAL_16M = 2,//RF OK
  324. SYS_CLK_DLL_48M = 3,//RF OK
  325. SYS_CLK_DLL_64M = 4,
  326. SYS_CLK_DLL_96M = 5,
  327. SYS_CLK_DBL_32M = 6
  328. };
  329. enum LOWCLK_SEL{
  330. RC_32K,
  331. XTAL_32K
  332. };
  333. /**************************************************************************************
  334. * @fn wdt_clk_sel
  335. *
  336. * @brief This function process for low clock select; 0----RC 32k 1----XTAL 32k
  337. *
  338. * input parameters
  339. *
  340. * @param enum LOWCLK_SEL clk: 0----RC 32k 1----XTAL 32k
  341. *
  342. * output parameters
  343. *
  344. * @param None.
  345. *
  346. * @return None.
  347. **************************************************************************************/
  348. static inline void wdt_clk_sel(enum LOWCLK_SEL clk){
  349. if(clk){
  350. BM_SET((volatile unsigned int *)0x4000f03c,BIT(16)); //use xtal 32k
  351. }else{
  352. BM_CLR((volatile unsigned int *)0x4000f03c,BIT(16)); //use rc 32k
  353. }
  354. }
  355. /**************************************************************************************
  356. * @fn hal_system_init
  357. *
  358. * @brief This function process for system initial,you can select diff source,such as RC_32M XTAL_16M and so on
  359. *
  360. * input parameters
  361. *
  362. * @param uint8_t h_system_clk_sel: system clock select SYS_CLK_RC_32M rc32M
  363. * SYS_CLK_DLL_32M dll32M
  364. * SYS_CLK_XTAL_16M xtal16M
  365. * SYS_CLK_DLL_48M dll48M
  366. * SYS_CLK_DLL_64M dll64M
  367. * SYS_CLK_DLL_96M dll96M
  368. *
  369. * output parameters
  370. *
  371. * @param None.
  372. *
  373. * @return None.
  374. **************************************************************************************/
  375. void hal_system_init(uint8_t h_system_clk_sel);
  376. void WaitUs(uint32_t wtTime);
  377. void WaitRTCCount(uint32_t rtcDelyCnt);
  378. uint8 char_array_cmp(uint8 *s1, uint8 *s2, uint8 len);
  379. void hal_system_soft_reset(void);
  380. /**************************************************************************************
  381. * @fn osal_memory_statics
  382. *
  383. * @brief This function process for osal memory analize
  384. *
  385. * input parameters
  386. *
  387. * @param None.
  388. *
  389. * output parameters
  390. *
  391. * @param None.
  392. *
  393. * @return Memmory free size.
  394. **************************************************************************************/
  395. uint32_t osal_memory_statics(void) ;
  396. #endif