system.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdint.h>
  18. #include "system.h"
  19. extern uint32_t SystemCoreClock;
  20. extern uint32_t _start_heap;
  21. static void flash_set_waitstates(int waitstates)
  22. {
  23. FLASH_ACR |= waitstates | FLASH_ACR_ENABLE_DATA_CACHE | FLASH_ACR_ENABLE_INST_CACHE;
  24. }
  25. void clock_pll_off(void)
  26. {
  27. uint32_t reg32;
  28. /* Enable internal high-speed oscillator. */
  29. RCC_CR |= RCC_CR_HSION;
  30. DMB();
  31. while ((RCC_CR & RCC_CR_HSIRDY) == 0) {};
  32. /* Select HSI as SYSCLK source. */
  33. reg32 = RCC_CFGR;
  34. reg32 &= ~((1 << 1) | (1 << 0));
  35. RCC_CFGR = (reg32 | RCC_CFGR_SW_HSI);
  36. DMB();
  37. /* Turn off PLL */
  38. RCC_CR &= ~RCC_CR_PLLON;
  39. DMB();
  40. }
  41. #include <string.h>
  42. /*
  43. size_t strlen(const char *s)
  44. {
  45. int i = 0;
  46. while (s[i] != 0)
  47. i++;
  48. return i;
  49. }
  50. */
  51. void * _sbrk(unsigned int incr)
  52. {
  53. static unsigned char *heap = (unsigned char *)&_start_heap;
  54. void *old_heap = heap;
  55. if (((incr >> 2) << 2) != incr)
  56. incr = ((incr >> 2) + 1) << 2;
  57. if (heap == NULL)
  58. heap = (unsigned char *)&_start_heap;
  59. else
  60. heap += incr;
  61. return old_heap;
  62. }