main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*
  16. * Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first).
  17. */
  18. static const SPIConfig hs_spicfg = {
  19. false,
  20. NULL,
  21. GPIOB,
  22. 10,
  23. 0,
  24. SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0
  25. };
  26. /*
  27. * Low speed SPI configuration (140.625kHz, CPHA=0, CPOL=0, MSb first).
  28. */
  29. static const SPIConfig ls_spicfg = {
  30. false,
  31. NULL,
  32. GPIOB,
  33. 10,
  34. SPI_CR1_BR_2 | SPI_CR1_BR_1,
  35. SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0
  36. };
  37. /*
  38. * SPI TX and RX buffers.
  39. */
  40. static uint8_t txbuf[512];
  41. static uint8_t rxbuf[512];
  42. /*
  43. * SPI bus contender 1.
  44. */
  45. static THD_WORKING_AREA(spi_thread_1_wa, 256);
  46. static THD_FUNCTION(spi_thread_1, p) {
  47. (void)p;
  48. chRegSetThreadName("SPI thread 1");
  49. while (true) {
  50. spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */
  51. palClearPad(GPIOC, GPIOC_LED2); /* LED ON. */
  52. spiStart(&SPID2, &hs_spicfg); /* Setup transfer parameters. */
  53. spiSelect(&SPID2); /* Slave Select assertion. */
  54. spiExchange(&SPID2, 512,
  55. txbuf, rxbuf); /* Atomic transfer operations. */
  56. spiUnselect(&SPID2); /* Slave Select de-assertion. */
  57. spiReleaseBus(&SPID2); /* Ownership release. */
  58. }
  59. }
  60. /*
  61. * SPI bus contender 2.
  62. */
  63. static THD_WORKING_AREA(spi_thread_2_wa, 256);
  64. static THD_FUNCTION(spi_thread_2, p) {
  65. (void)p;
  66. chRegSetThreadName("SPI thread 2");
  67. while (true) {
  68. spiAcquireBus(&SPID2); /* Acquire ownership of the bus. */
  69. palSetPad(GPIOC, GPIOC_LED2); /* LED OFF. */
  70. spiStart(&SPID2, &ls_spicfg); /* Setup transfer parameters. */
  71. spiSelect(&SPID2); /* Slave Select assertion. */
  72. spiExchange(&SPID2, 512,
  73. txbuf, rxbuf); /* Atomic transfer operations. */
  74. spiUnselect(&SPID2); /* Slave Select de-assertion. */
  75. spiReleaseBus(&SPID2); /* Ownership release. */
  76. }
  77. }
  78. /*
  79. * This is a periodic thread that does absolutely nothing except flashing
  80. * a LED.
  81. */
  82. static THD_WORKING_AREA(blinker_wa, 128);
  83. static THD_FUNCTION(blinker, arg) {
  84. (void)arg;
  85. chRegSetThreadName("blinker");
  86. while (true) {
  87. palSetPad(GPIOC, GPIOC_LED1);
  88. chThdSleepMilliseconds(500);
  89. palClearPad(GPIOC, GPIOC_LED1);
  90. chThdSleepMilliseconds(500);
  91. }
  92. }
  93. /*
  94. * Application entry point.
  95. */
  96. int main(void) {
  97. unsigned i;
  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. * SPI2 I/O pins setup.
  109. */
  110. palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(5) |
  111. PAL_STM32_OSPEED_HIGHEST); /* New MISO. */
  112. palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(5) |
  113. PAL_STM32_OSPEED_HIGHEST); /* New MOSI. */
  114. /*
  115. * Prepare transmit pattern.
  116. */
  117. for (i = 0; i < sizeof(txbuf); i++)
  118. txbuf[i] = (uint8_t)i;
  119. /*
  120. * Starting the transmitter and receiver threads.
  121. */
  122. chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa),
  123. NORMALPRIO + 1, spi_thread_1, NULL);
  124. chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa),
  125. NORMALPRIO + 1, spi_thread_2, NULL);
  126. /*
  127. * Starting the blinker thread.
  128. */
  129. chThdCreateStatic(blinker_wa, sizeof(blinker_wa),
  130. NORMALPRIO-1, blinker, NULL);
  131. /*
  132. * Normal main() thread activity, in this demo it does nothing.
  133. */
  134. while (true) {
  135. chThdSleepMilliseconds(500);
  136. }
  137. return 0;
  138. }