hal_tick_overrides.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2018 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "hal/us_ticker_api.h"
  17. #include "us_ticker_data.h"
  18. // This variable is set to 1 at the of mbed_sdk_init function.
  19. // The ticker_read_us function must not be called until the mbed_sdk_init is terminated.
  20. extern int mbed_sdk_inited;
  21. // Defined in us_ticker.c
  22. void init_16bit_timer(void);
  23. void init_32bit_timer(void);
  24. #if TIM_MST_BIT_WIDTH == 16
  25. // Variables also reset in us_ticker_init()
  26. uint32_t prev_time = 0;
  27. uint32_t elapsed_time = 0;
  28. #endif
  29. // Overwrite default HAL functions defined as "weak"
  30. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  31. {
  32. #if TIM_MST_BIT_WIDTH == 16
  33. init_16bit_timer();
  34. #else
  35. init_32bit_timer();
  36. #endif
  37. return HAL_OK;
  38. }
  39. uint32_t HAL_GetTick()
  40. {
  41. #if TIM_MST_BIT_WIDTH == 16
  42. uint32_t new_time;
  43. if (mbed_sdk_inited) {
  44. // Apply the latest time recorded just before the sdk is inited
  45. new_time = ticker_read_us(get_us_ticker_data()) + prev_time;
  46. prev_time = 0; // Use this time only once
  47. return (new_time / 1000);
  48. }
  49. else {
  50. new_time = us_ticker_read();
  51. elapsed_time += (new_time - prev_time) & 0xFFFF; // Only use the lower 16 bits
  52. prev_time = new_time;
  53. return (elapsed_time / 1000);
  54. }
  55. #else // 32-bit timer
  56. if (mbed_sdk_inited) {
  57. return (ticker_read_us(get_us_ticker_data()) / 1000);
  58. }
  59. else {
  60. return (us_ticker_read() / 1000);
  61. }
  62. #endif
  63. }
  64. void HAL_SuspendTick(void)
  65. {
  66. // Do nothing
  67. }
  68. void HAL_ResumeTick(void)
  69. {
  70. // Do nothing
  71. }