main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #define ADC_GRP1_NUM_CHANNELS 1
  16. #define ADC_GRP1_BUF_DEPTH 8
  17. #define ADC_GRP2_NUM_CHANNELS 8
  18. #define ADC_GRP2_BUF_DEPTH 16
  19. static adcsample_t samples1[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
  20. static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH];
  21. /*
  22. * ADC streaming callback.
  23. */
  24. size_t nx = 0, ny = 0;
  25. static void adccallback(ADCDriver *adcp) {
  26. if (adcIsBufferComplete(adcp)) {
  27. nx += 1;
  28. }
  29. else {
  30. ny += 1;
  31. }
  32. }
  33. static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
  34. (void)adcp;
  35. (void)err;
  36. }
  37. /*
  38. * ADC conversion group.
  39. * Mode: Linear buffer, 8 samples of 1 channel, SW triggered.
  40. * Channels: IN11.
  41. */
  42. static const ADCConversionGroup adcgrpcfg1 = {
  43. FALSE,
  44. ADC_GRP1_NUM_CHANNELS,
  45. NULL,
  46. adcerrorcallback,
  47. 0, /* CR1 */
  48. ADC_CR2_SWSTART, /* CR2 */
  49. ADC_SMPR1_SMP_AN11(ADC_SAMPLE_3),
  50. 0, /* SMPR2 */
  51. 0, /* HTR */
  52. 0, /* LTR */
  53. 0, /* SQR1 */
  54. 0, /* SQR2 */
  55. ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11)
  56. };
  57. /*
  58. * ADC conversion group.
  59. * Mode: Continuous, 16 samples of 8 channels, SW triggered.
  60. * Channels: IN11, IN12, IN11, IN12, IN11, IN12, Sensor, VRef.
  61. */
  62. static const ADCConversionGroup adcgrpcfg2 = {
  63. TRUE,
  64. ADC_GRP2_NUM_CHANNELS,
  65. adccallback,
  66. adcerrorcallback,
  67. 0, /* CR1 */
  68. ADC_CR2_SWSTART, /* CR2 */
  69. ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) |
  70. ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_144) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_144),
  71. 0, /* SMPR2 */
  72. 0, /* HTR */
  73. 0, /* LTR */
  74. 0, /* SQR1 */
  75. ADC_SQR2_SQ8_N(ADC_CHANNEL_SENSOR) | ADC_SQR2_SQ7_N(ADC_CHANNEL_VREFINT),
  76. ADC_SQR3_SQ6_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ5_N(ADC_CHANNEL_IN11) |
  77. ADC_SQR3_SQ4_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN11) |
  78. ADC_SQR3_SQ2_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11)
  79. };
  80. /*
  81. * Red LED blinker thread, times are in milliseconds.
  82. */
  83. static THD_WORKING_AREA(waThread1, 128);
  84. static THD_FUNCTION(Thread1, arg) {
  85. (void)arg;
  86. chRegSetThreadName("blinker");
  87. while (true) {
  88. palSetPad(GPIOD, GPIOD_LED5);
  89. chThdSleepMilliseconds(500);
  90. palClearPad(GPIOD, GPIOD_LED5);
  91. chThdSleepMilliseconds(500);
  92. }
  93. }
  94. /*
  95. * Application entry point.
  96. */
  97. int main(void) {
  98. /*
  99. * System initializations.
  100. * - HAL initialization, this also initializes the configured device drivers
  101. * and performs the board-specific initializations.
  102. * - Kernel initialization, the main() function becomes a thread and the
  103. * RTOS is active.
  104. */
  105. halInit();
  106. chSysInit();
  107. /*
  108. * Setting up analog inputs used by the demo.
  109. */
  110. palSetGroupMode(GPIOC, PAL_PORT_BIT(1) | PAL_PORT_BIT(2),
  111. 0, PAL_MODE_INPUT_ANALOG);
  112. /*
  113. * Creates the blinker thread.
  114. */
  115. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  116. /*
  117. * Activates the ADC1 driver and the temperature sensor.
  118. */
  119. adcStart(&ADCD1, NULL);
  120. adcSTM32EnableTSVREFE();
  121. /*
  122. * Linear conversion.
  123. */
  124. adcConvert(&ADCD1, &adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  125. chThdSleepMilliseconds(1000);
  126. /*
  127. * Starts an ADC continuous conversion.
  128. */
  129. adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);
  130. /*
  131. * Normal main() thread activity, in this demo it does nothing.
  132. */
  133. while (true) {
  134. if (palReadPad(GPIOA, GPIOA_BUTTON)) {
  135. adcStopConversion(&ADCD1);
  136. adcSTM32DisableTSVREFE();
  137. }
  138. chThdSleepMilliseconds(500);
  139. }
  140. }