common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file common.c
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. #include "sdk_config.h"
  11. #include "global_config.h"
  12. #include "OSAL_Tasks.h"
  13. #include "rf_phy_driver.h"
  14. #include "pwrmgr.h"
  15. #include "gpio.h"
  16. #include "timer.h"
  17. #include "uart.h"
  18. #include "log.h"
  19. #include "common.h"
  20. #include "spi.h"
  21. __asm uint32_t get_msp_addr()
  22. {
  23. mrs r0, msp;
  24. bx lr;// cppcheck-suppress missingReturn
  25. }
  26. #define DUMP_LOG
  27. #ifdef DUMP_LOG
  28. unsigned int cur_sp;
  29. unsigned int sp_data[16];
  30. void Dump_Record(void)
  31. {
  32. cur_sp = __current_sp();
  33. for(int i = 0; i< 0x10; i++)
  34. {
  35. sp_data[i] = ((uint32_t*)cur_sp)[i];
  36. }
  37. LOG("DUMP LOG V1\r\n");
  38. LOG("Hard Fault SP is %x\n",cur_sp);
  39. LOG("crash fun:0x%x\r\n", sp_data[9]);
  40. LOG("crash addr:0x%x\r\n", sp_data[12]);
  41. for(int i = 0; i< 0x10; i++)
  42. {
  43. LOG("crash data:0x%x\r\n", sp_data[i]);
  44. }
  45. }
  46. #endif
  47. void hard_fault(void)
  48. {
  49. #ifdef DUMP_LOG
  50. Dump_Record();
  51. #endif
  52. while(1);
  53. }
  54. __ATTR_SECTION_XIP__ uint32_t osal_memory_statics(uint32 memTotalSize)
  55. {
  56. osalMemHdr_t *header, *current;
  57. void *ptr;
  58. uint32 sum_alloc = 0;
  59. ptr = (void *)(g_largeHeap);
  60. header = (osalMemHdr_t *)ptr;
  61. current = (osalMemHdr_t *)ptr;
  62. HAL_ENTER_CRITICAL_SECTION(); // Hold off interrupts.
  63. do
  64. {
  65. if ((uint32)ptr >= (uint32)header + memTotalSize)
  66. {
  67. break;
  68. }
  69. // seek to the last block, return
  70. if ( current->val == 0 ) /// val = 0, so len = 0
  71. {
  72. break;
  73. }
  74. if (current->hdr.inUse)
  75. {
  76. sum_alloc += current->hdr.len;
  77. }
  78. current = (osalMemHdr_t *)((uint8 *)current + current->hdr.len);
  79. } while (1);
  80. HAL_EXIT_CRITICAL_SECTION(); // Re-enable interrupts.
  81. return sum_alloc;
  82. }
  83. #ifdef DBG_SPI_USE
  84. void dbg_spi_init(AP_SSI_TypeDef* SPIx)
  85. {
  86. spi_Cfg_t cfg;
  87. cfg.ssn_pin = P31;
  88. cfg.sclk_pin = P32;
  89. cfg.MISO = GPIO_DUMMY;
  90. cfg.MOSI = P33;
  91. cfg.baudrate = 12000000;
  92. cfg.spi_tmod = SPI_TRXD;
  93. cfg.spi_scmod = SPI_MODE0;
  94. cfg.spi_dfsmod = SPI_1BYTE;
  95. cfg.dma_rx_enable = false;
  96. cfg.dma_tx_enable = false;
  97. cfg.int_mode = false;
  98. cfg.evt_handler = NULL;
  99. cfg.force_cs = false;
  100. cfg.is_slave = false;
  101. HalSpiMasterConfig(SPIx, &cfg);
  102. }
  103. void dbg_spi_out_buf(uint8_t connId, uint8_t *buf, uint8_t len)
  104. {
  105. AP_SPI1->DataReg=0xA0+connId;
  106. while(len--)
  107. {
  108. AP_SPI1->DataReg = *buf++;
  109. }
  110. }
  111. #endif