main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 <string.h>
  14. #include "ch.h"
  15. #include "hal.h"
  16. /**
  17. * DMA operation timeout failure handler.
  18. */
  19. static void tmo(void *p) {
  20. chSysHalt((const char *)p);
  21. }
  22. /*--------------------------------------------------------------------------*/
  23. /* ADC related code. */
  24. /*--------------------------------------------------------------------------*/
  25. #define ADC_GRP2_NUM_CHANNELS 8
  26. #define ADC_GRP2_BUF_DEPTH 16
  27. static virtual_timer_t adcvt;
  28. static adcsample_t samples2[ADC_GRP2_NUM_CHANNELS * ADC_GRP2_BUF_DEPTH];
  29. static void adccallback(ADCDriver *adcp) {
  30. (void)adcp;
  31. chSysLockFromISR();
  32. chVTSetI(&adcvt, TIME_MS2I(10), tmo, (void *)"ADC timeout");
  33. chSysUnlockFromISR();
  34. }
  35. static void adcerrorcallback(ADCDriver *adcp, adcerror_t err) {
  36. (void)adcp;
  37. (void)err;
  38. chSysHalt("ADC error");
  39. }
  40. /*
  41. * ADC conversion group.
  42. * Mode: Continuous, 16 samples of 8 channels, SW triggered.
  43. * Channels: IN11, IN12, IN11, IN12, IN11, IN12, Sensor, VRef.
  44. */
  45. static const ADCConversionGroup adcgrpcfg2 = {
  46. true,
  47. ADC_GRP2_NUM_CHANNELS,
  48. adccallback,
  49. adcerrorcallback,
  50. 0, /* CR1 */
  51. ADC_CR2_SWSTART, /* CR2 */
  52. ADC_SMPR1_SMP_AN12(ADC_SAMPLE_56) | ADC_SMPR1_SMP_AN11(ADC_SAMPLE_56) |
  53. ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_144) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_144),
  54. 0, /* SMPR2 */
  55. 0, /* HTR */
  56. 0, /* LTR */
  57. 0, /* SQR1 */
  58. ADC_SQR2_SQ8_N(ADC_CHANNEL_SENSOR) | ADC_SQR2_SQ7_N(ADC_CHANNEL_VREFINT),
  59. ADC_SQR3_SQ6_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ5_N(ADC_CHANNEL_IN11) |
  60. ADC_SQR3_SQ4_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ3_N(ADC_CHANNEL_IN11) |
  61. ADC_SQR3_SQ2_N(ADC_CHANNEL_IN12) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN11)
  62. };
  63. /*--------------------------------------------------------------------------*/
  64. /* SPI related code. */
  65. /*--------------------------------------------------------------------------*/
  66. /*
  67. * Maximum speed SPI configuration (21MHz, CPHA=0, CPOL=0, MSb first).
  68. */
  69. static const SPIConfig hs_spicfg = {
  70. false,
  71. NULL,
  72. GPIOB,
  73. 12,
  74. 0,
  75. 0
  76. };
  77. /*
  78. * SPI thread.
  79. */
  80. static THD_WORKING_AREA(waSPI1, 1024);
  81. static THD_WORKING_AREA(waSPI2, 1024);
  82. static THD_WORKING_AREA(waSPI3, 1024);
  83. static THD_FUNCTION(spi_thread, p) {
  84. unsigned i;
  85. SPIDriver *spip = (SPIDriver *)p;
  86. virtual_timer_t vt;
  87. uint8_t txbuf[256];
  88. uint8_t rxbuf[256];
  89. chRegSetThreadName("SPI overlord");
  90. chVTObjectInit(&vt);
  91. /* Prepare transmit pattern.*/
  92. for (i = 0; i < sizeof(txbuf); i++)
  93. txbuf[i] = (uint8_t)i;
  94. /* Continuous transmission.*/
  95. while (true) {
  96. /* Starts a VT working as watchdog to catch a malfunction in the SPI
  97. driver.*/
  98. chVTSet(&vt, TIME_MS2I(10), tmo, (void *)"SPI timeout");
  99. spiExchange(spip, sizeof(txbuf), txbuf, rxbuf);
  100. /* Stops the watchdog.*/
  101. chVTReset(&vt);
  102. }
  103. }
  104. /*--------------------------------------------------------------------------*/
  105. /* Application code. */
  106. /*--------------------------------------------------------------------------*/
  107. /*
  108. * This is a periodic thread that does absolutely nothing except flashing
  109. * a LED. If the application fails then the LED stops flashing.
  110. */
  111. static THD_WORKING_AREA(waThread1, 128);
  112. static THD_FUNCTION(Thread1, arg) {
  113. (void)arg;
  114. chRegSetThreadName("blinker");
  115. while (true) {
  116. palSetPad(GPIOD, GPIOD_LED3); /* Orange. */
  117. chThdSleepMilliseconds(500);
  118. palClearPad(GPIOD, GPIOD_LED3); /* Orange. */
  119. chThdSleepMilliseconds(500);
  120. }
  121. }
  122. /*
  123. * Application entry point.
  124. */
  125. int main(void) {
  126. unsigned i;
  127. static uint8_t patterns1[4096], patterns2[4096], buf1[4096], buf2[4096];
  128. /* System initializations.
  129. - HAL initialization, this also initializes the configured device drivers
  130. and performs the board-specific initializations.
  131. - Kernel initialization, the main() function becomes a thread and the
  132. RTOS is active.*/
  133. halInit();
  134. chSysInit();
  135. /* Creates the blinker thread.*/
  136. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 10,
  137. Thread1, NULL);
  138. /* Activates the ADC1 driver and the temperature sensor.*/
  139. adcStart(&ADCD1, NULL);
  140. adcSTM32EnableTSVREFE();
  141. /* Starts an ADC continuous conversion and its watchdog virtual timer.*/
  142. chVTSet(&adcvt, TIME_MS2I(10), tmo, (void *)"ADC timeout");
  143. adcStartConversion(&ADCD1, &adcgrpcfg2, samples2, ADC_GRP2_BUF_DEPTH);
  144. /* Activating SPI drivers.*/
  145. spiStart(&SPID1, &hs_spicfg);
  146. spiStart(&SPID2, &hs_spicfg);
  147. spiStart(&SPID3, &hs_spicfg);
  148. /* Starting SPI threads instances.*/
  149. chThdCreateStatic(waSPI1, sizeof(waSPI1), NORMALPRIO + 1, spi_thread, &SPID1);
  150. chThdCreateStatic(waSPI2, sizeof(waSPI2), NORMALPRIO + 1, spi_thread, &SPID2);
  151. chThdCreateStatic(waSPI3, sizeof(waSPI3), NORMALPRIO + 1, spi_thread, &SPID3);
  152. /* Allocating two DMA2 streams for memory copy operations.*/
  153. if (dmaStreamAlloc(STM32_DMA_STREAM_ID(2, 6), 0, NULL, NULL) == NULL)
  154. chSysHalt("DMA already in use");
  155. if (dmaStreamAlloc(STM32_DMA_STREAM_ID(2, 7), 0, NULL, NULL) == NULL)
  156. chSysHalt("DMA already in use");
  157. for (i = 0; i < sizeof (patterns1); i++)
  158. patterns1[i] = (uint8_t)i;
  159. for (i = 0; i < sizeof (patterns2); i++)
  160. patterns2[i] = (uint8_t)(i ^ 0xAA);
  161. /* Normal main() thread activity, it does continues memory copy operations
  162. using 2 DMA streams at the lowest priority.*/
  163. while (true) {
  164. virtual_timer_t vt;
  165. chVTObjectInit(&vt);
  166. /* Starts a VT working as watchdog to catch a malfunction in the DMA
  167. driver.*/
  168. chVTSet(&vt, TIME_MS2I(10), tmo, (void *)"copy timeout");
  169. /* Copy pattern 1.*/
  170. dmaStartMemCopy(STM32_DMA2_STREAM6,
  171. STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
  172. STM32_DMA_CR_MSIZE_BYTE,
  173. patterns1, buf1, sizeof (patterns1));
  174. dmaStartMemCopy(STM32_DMA2_STREAM7,
  175. STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
  176. STM32_DMA_CR_MSIZE_BYTE,
  177. patterns1, buf2, sizeof (patterns1));
  178. dmaWaitCompletion(STM32_DMA2_STREAM6);
  179. dmaWaitCompletion(STM32_DMA2_STREAM7);
  180. if (memcmp(patterns1, buf1, sizeof (patterns1)))
  181. chSysHalt("pattern error");
  182. if (memcmp(patterns1, buf2, sizeof (patterns1)))
  183. chSysHalt("pattern error");
  184. /* Copy pattern 2.*/
  185. dmaStartMemCopy(STM32_DMA2_STREAM6,
  186. STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
  187. STM32_DMA_CR_MSIZE_BYTE,
  188. patterns2, buf1, sizeof (patterns2));
  189. dmaStartMemCopy(STM32_DMA2_STREAM7,
  190. STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
  191. STM32_DMA_CR_MSIZE_BYTE,
  192. patterns2, buf2, sizeof (patterns2));
  193. dmaWaitCompletion(STM32_DMA2_STREAM6);
  194. dmaWaitCompletion(STM32_DMA2_STREAM7);
  195. if (memcmp(patterns2, buf1, sizeof (patterns2)))
  196. chSysHalt("pattern error");
  197. if (memcmp(patterns2, buf2, sizeof (patterns2)))
  198. chSysHalt("pattern error");
  199. /* Stops the watchdog.*/
  200. chVTReset(&vt);
  201. chThdSleepMilliseconds(2);
  202. }
  203. return 0;
  204. }