main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. /* TRUE means that DMA-accessible buffers are placed in a non-cached RAM
  16. area and that no cache management is required.*/
  17. #define DMA_BUFFERS_COHERENCE TRUE
  18. /*===========================================================================*/
  19. /* GPT driver related. */
  20. /*===========================================================================*/
  21. /*
  22. * GPT4 configuration. This timer is used as trigger for the ADC.
  23. */
  24. static const GPTConfig gpt4cfg1 = {
  25. .frequency = 1000000U,
  26. .callback = NULL,
  27. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  28. .dier = 0U
  29. };
  30. /*===========================================================================*/
  31. /* ADC driver related. */
  32. /*===========================================================================*/
  33. #define ADC_GRP1_NUM_CHANNELS 2
  34. #define ADC_GRP1_BUF_DEPTH 64
  35. #if !DMA_BUFFERS_COHERENCE
  36. /* Note, the buffer is aligned to a 32 bytes boundary because limitations
  37. imposed by the data cache. Note, this is GNU specific, it must be
  38. handled differently for other compilers.
  39. Only required if the ADC buffer is placed in a cache-able area.*/
  40. #if defined(__GNUC__)
  41. __attribute__((aligned (32)))
  42. #endif
  43. #endif
  44. static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
  45. /*
  46. * ADC streaming callback.
  47. */
  48. size_t nx = 0, ny = 0;
  49. static void adccallback(ADCDriver *adcp) {
  50. #if !DMA_BUFFERS_COHERENCE
  51. /* DMA buffer invalidation because data cache, only invalidating the
  52. half buffer just filled.
  53. Only required if the ADC buffer is placed in a cache-able area.*/
  54. dmaBufferInvalidate(buffer,
  55. n * adcp->grpp->num_channels * sizeof (adcsample_t));
  56. #endif
  57. /* Updating counters.*/
  58. if (adcIsBufferComplete(adcp)) {
  59. nx += 1;
  60. }
  61. else {
  62. ny += 1;
  63. }
  64. }
  65. /*
  66. * ADC errors callback, should never happen.
  67. */
  68. static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
  69. (void)adcp;
  70. (void)err;
  71. }
  72. /*
  73. * ADC conversion group.
  74. * Mode: Continuous, 16 samples of 2 channels, HS triggered by
  75. * GPT4-TRGO.
  76. * Channels: Sensor, VRef.
  77. */
  78. static const ADCConversionGroup adcgrpcfg1 = {
  79. true,
  80. ADC_GRP1_NUM_CHANNELS,
  81. adccallback,
  82. adcerrorcallback,
  83. 0, /* CR1 */
  84. ADC_CR2_EXTEN_RISING | ADC_CR2_EXTSEL_SRC(12), /* CR2 */
  85. ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_144) |
  86. ADC_SMPR1_SMP_VREF(ADC_SAMPLE_144), /* SMPR1 */
  87. 0, /* SMPR2 */
  88. 0, /* HTR */
  89. 0, /* LTR */
  90. 0, /* SQR1 */
  91. 0, /* SQR2 */
  92. ADC_SQR3_SQ2_N(ADC_CHANNEL_SENSOR) |
  93. ADC_SQR3_SQ1_N(ADC_CHANNEL_VREFINT) /* SQR3 */
  94. };
  95. /*===========================================================================*/
  96. /* Application code. */
  97. /*===========================================================================*/
  98. /*
  99. * This is a periodic thread that does absolutely nothing except flashing
  100. * a LED attached to TP1.
  101. */
  102. static THD_WORKING_AREA(waThread1, 128);
  103. static THD_FUNCTION(Thread1, arg) {
  104. (void)arg;
  105. chRegSetThreadName("blinker");
  106. palSetLineMode(LINE_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL);
  107. while (true) {
  108. palSetLine(LINE_ARD_D13);
  109. chThdSleepMilliseconds(500);
  110. palClearLine(LINE_ARD_D13);
  111. chThdSleepMilliseconds(500);
  112. }
  113. }
  114. /*
  115. * Application entry point.
  116. */
  117. int main(void) {
  118. /*
  119. * System initializations.
  120. * - HAL initialization, this also initializes the configured device drivers
  121. * and performs the board-specific initializations.
  122. * - Kernel initialization, the main() function becomes a thread and the
  123. * RTOS is active.
  124. */
  125. halInit();
  126. chSysInit();
  127. /*
  128. * Activates the serial driver 1 using the driver default configuration.
  129. */
  130. sdStart(&SD1, NULL);
  131. /*
  132. * Starting GPT4 driver, it is used for triggering the ADC.
  133. */
  134. gptStart(&GPTD4, &gpt4cfg1);
  135. /*
  136. * Fixed an errata on the STM32F7xx, the DAC clock is required for ADC
  137. * triggering.
  138. */
  139. rccEnableDAC1(false);
  140. /*
  141. * Activates the ADC1 driver and the temperature sensor.
  142. */
  143. adcStart(&ADCD1, NULL);
  144. adcSTM32EnableTSVREFE();
  145. /*
  146. * Starts an ADC continuous conversion triggered with a period of
  147. * 1/10000 second.
  148. */
  149. adcStartConversion(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  150. gptStartContinuous(&GPTD4, 100);
  151. /*
  152. * Creates the example thread.
  153. */
  154. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  155. /*
  156. * Normal main() thread activity, in this demo it does nothing.
  157. */
  158. while (true) {
  159. chThdSleepMilliseconds(500);
  160. }
  161. }