core_dac.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. typedef struct
  23. {
  24. CBFuncEx_t Callback;
  25. void *pParam;
  26. uint16_t *TxData;
  27. uint32_t Pos;
  28. uint32_t MaxLen;
  29. uint32_t Freq;
  30. uint8_t DMATxStream;
  31. uint8_t IsBusy;
  32. }DAC_ResourceStruct;
  33. static DAC_ResourceStruct prvDAC;
  34. static int32_t DAC_DMADoneCB(void *pData, void *pParam)
  35. {
  36. uint32_t TxLevel;
  37. if (prvDAC.MaxLen > prvDAC.Pos)
  38. {
  39. TxLevel = ((prvDAC.MaxLen - prvDAC.Pos) > 4090)?4000:(prvDAC.MaxLen - prvDAC.Pos);
  40. DMA_ClearStreamFlag(prvDAC.DMATxStream);
  41. DMA_ForceStartStream(prvDAC.DMATxStream, &prvDAC.TxData[prvDAC.Pos], TxLevel, DAC_DMADoneCB, NULL, 1);
  42. prvDAC.Pos += TxLevel;
  43. }
  44. else
  45. {
  46. prvDAC.IsBusy = 0;
  47. prvDAC.Callback(pParam, prvDAC.pParam);
  48. }
  49. }
  50. static int32_t DAC_DummyCB(void *pData, void *pParam)
  51. {
  52. DBG("!");
  53. return 0;
  54. }
  55. void DAC_DMAInit(uint8_t DAC_ID, uint8_t Stream)
  56. {
  57. DMA_InitTypeDef DMA_InitStruct;
  58. DMA_BaseConfig(&DMA_InitStruct);
  59. DMA_InitStruct.DMA_Peripheral = SYSCTRL_PHER_CTRL_DMA_CHx_IF_DAC;
  60. DMA_InitStruct.DMA_PeripheralBurstSize = DMA_BurstSize_4;
  61. DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&DAC->DAC_DATA;
  62. DMA_InitStruct.DMA_Priority = DMA_Priority_1;
  63. DMA_InitStruct.DMA_MemoryBurstSize = DMA_BurstSize_4;
  64. DMA_InitStruct.DMA_MemoryDataSize = DMA_DataSize_HalfWord;
  65. DMA_InitStruct.DMA_PeripheralDataSize = DMA_DataSize_HalfWord;
  66. DMA_ConfigStream(Stream, &DMA_InitStruct);
  67. prvDAC.DMATxStream = Stream;
  68. }
  69. void DAC_Setup(uint8_t DAC_ID, uint32_t Freq, uint32_t OutRMode)
  70. {
  71. uint32_t Ctrl;
  72. DAC->DAC_CR1 |= (1 << 4);
  73. while (DAC->DAC_CR1 & (1 << 29));
  74. DAC->DAC_TIMER = ((SystemCoreClock >> 3) / Freq) - 1;
  75. DAC->DAC_FIFO_THR = 10;
  76. DAC->DAC_CR1 = (OutRMode << 5) | 0x18;
  77. }
  78. void DAC_Send(uint8_t DAC_ID, const uint16_t *Data, uint32_t Len, CBFuncEx_t Callback, void *pParam)
  79. {
  80. uint32_t TxLevel;
  81. if (prvDAC.IsBusy)
  82. {
  83. DMA_PrintReg(prvDAC.DMATxStream);
  84. return;
  85. }
  86. prvDAC.IsBusy = 1;
  87. prvDAC.TxData = Data;
  88. prvDAC.MaxLen = Len;
  89. prvDAC.Pos = 0;
  90. if (Callback)
  91. {
  92. prvDAC.Callback = Callback;
  93. }
  94. else
  95. {
  96. prvDAC.Callback = DAC_DummyCB;
  97. }
  98. prvDAC.pParam = pParam;
  99. TxLevel = ((prvDAC.MaxLen - prvDAC.Pos) > 4090)?4000:(prvDAC.MaxLen - prvDAC.Pos);
  100. DMA_ClearStreamFlag(prvDAC.DMATxStream);
  101. DMA_ForceStartStream(prvDAC.DMATxStream, &prvDAC.TxData[prvDAC.Pos], TxLevel, DAC_DMADoneCB, DAC_ID, 1);
  102. prvDAC.Pos += TxLevel;
  103. DAC->DAC_CR1 &= ~(1 << 4);
  104. DAC->DAC_CR1 |= 0x04;
  105. ADC0->ADC_CR1 &= ~(1 << 8);
  106. }
  107. uint8_t DAC_CheckRun(uint8_t DAC_ID)
  108. {
  109. return (DAC->DAC_CR1 & (1 << 29)) >> 29;
  110. }
  111. void DAC_Stop(uint8_t DAC_ID)
  112. {
  113. while (DAC->DAC_CR1 & (1 << 29));
  114. DAC_ForceStop(DAC_ID);
  115. }
  116. void DAC_ForceStop(uint8_t DAC_ID)
  117. {
  118. DMA_StopStream(prvDAC.DMATxStream);
  119. DAC->DAC_CR1 |= (1 << 4);
  120. DAC->DAC_CR1 &= ~0x04;
  121. }