main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include "ccportab.h"
  16. #include "portab.h"
  17. /*===========================================================================*/
  18. /* ADC driver related. */
  19. /*===========================================================================*/
  20. #define ADC_GRP1_BUF_DEPTH 1
  21. #define ADC_GRP2_BUF_DEPTH 64
  22. /* Buffers are allocated with size and address aligned to the cache
  23. line size.*/
  24. #if CACHE_LINE_SIZE > 0
  25. CC_ALIGN(CACHE_LINE_SIZE)
  26. #endif
  27. adcsample_t samples1[CACHE_SIZE_ALIGN(adcsample_t, ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH)];
  28. #if CACHE_LINE_SIZE > 0
  29. CC_ALIGN(CACHE_LINE_SIZE)
  30. #endif
  31. adcsample_t samples2[CACHE_SIZE_ALIGN(adcsample_t, ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH)];
  32. /*
  33. * ADC streaming callback.
  34. */
  35. size_t n= 0, nx = 0, ny = 0;
  36. void adccallback(ADCDriver *adcp) {
  37. (void)adcp;
  38. /* Updating counters.*/
  39. n++;
  40. if (adcIsBufferComplete(adcp)) {
  41. nx += 1;
  42. }
  43. else {
  44. ny += 1;
  45. }
  46. if ((n % 200) == 0U) {
  47. palToggleLine(PORTAB_LINE_LED2);
  48. }
  49. }
  50. /*
  51. * ADC errors callback, should never happen.
  52. */
  53. void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
  54. (void)adcp;
  55. (void)err;
  56. chSysHalt("it happened");
  57. }
  58. /*===========================================================================*/
  59. /* Application code. */
  60. /*===========================================================================*/
  61. /*
  62. * This is a periodic thread that does absolutely nothing except flashing
  63. * a LED attached to TP1.
  64. */
  65. static THD_WORKING_AREA(waThread1, 128);
  66. static THD_FUNCTION(Thread1, arg) {
  67. (void)arg;
  68. chRegSetThreadName("blinker");
  69. while (true) {
  70. palSetLine(PORTAB_LINE_LED1);
  71. chThdSleepMilliseconds(500);
  72. palClearLine(PORTAB_LINE_LED1);
  73. chThdSleepMilliseconds(500);
  74. }
  75. }
  76. /*
  77. * Application entry point.
  78. */
  79. int main(void) {
  80. /*
  81. * System initializations.
  82. * - HAL initialization, this also initializes the configured device drivers
  83. * and performs the board-specific initializations.
  84. * - Kernel initialization, the main() function becomes a thread and the
  85. * RTOS is active.
  86. */
  87. halInit();
  88. chSysInit();
  89. /* Board-dependent GPIO setup code.*/
  90. portab_setup();
  91. /*
  92. * Creates the example thread.
  93. */
  94. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  95. /*
  96. * Starting PORTAB_ADC1 driver and the temperature sensor.
  97. */
  98. adcStart(&PORTAB_ADC1, &portab_adccfg1);
  99. adcSTM32EnableVREF(&PORTAB_ADC1);
  100. adcSTM32EnableTS(&PORTAB_ADC1);
  101. /* Performing a one-shot conversion on two channels.*/
  102. adcConvert(&PORTAB_ADC1, &portab_adcgrpcfg1, samples1, ADC_GRP1_BUF_DEPTH);
  103. cacheBufferInvalidate(samples1, sizeof (samples1) / sizeof (adcsample_t));
  104. /*
  105. * Starting PORTAB_GPT1 driver, it is used for triggering the ADC.
  106. */
  107. gptStart(&PORTAB_GPT1, &portab_gptcfg1);
  108. /*
  109. * Starting an ADC continuous conversion triggered with a period of
  110. * 1/10000 second.
  111. */
  112. adcStartConversion(&PORTAB_ADC1, &portab_adcgrpcfg2,
  113. samples2, ADC_GRP2_BUF_DEPTH);
  114. gptStartContinuous(&PORTAB_GPT1, 100U);
  115. /*
  116. * Normal main() thread activity, if the button is pressed then the
  117. * conversion is stopped.
  118. */
  119. while (true) {
  120. if (palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED) {
  121. gptStopTimer(&PORTAB_GPT1);
  122. adcStopConversion(&PORTAB_ADC1);
  123. }
  124. chThdSleepMilliseconds(500);
  125. }
  126. return 0;
  127. }