main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * SPI TX and RX buffers.
  19. */
  20. CC_ALIGN(32) static uint8_t txbuf[512];
  21. CC_ALIGN(32) static uint8_t rxbuf[512];
  22. #if SPI_SUPPORTS_CIRCULAR == TRUE
  23. /*
  24. * SPI callback for circular operations.
  25. */
  26. void spi_circular_cb(SPIDriver *spip) {
  27. if (spiIsBufferComplete(spip)) {
  28. /* 2nd half.*/
  29. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_OFF);
  30. }
  31. else {
  32. /* 1st half.*/
  33. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_ON);
  34. }
  35. }
  36. #endif
  37. /*
  38. * SPI bus contender 1.
  39. */
  40. static THD_WORKING_AREA(spi_thread_1_wa, 256);
  41. static THD_FUNCTION(spi_thread_1, p) {
  42. (void)p;
  43. chRegSetThreadName("SPI thread 1");
  44. while (true) {
  45. spiAcquireBus(&PORTAB_SPI1); /* Acquire ownership of the bus. */
  46. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_ON);
  47. spiStart(&PORTAB_SPI1, &hs_spicfg); /* Setup transfer parameters. */
  48. spiSelect(&PORTAB_SPI1); /* Slave Select assertion. */
  49. spiExchange(&PORTAB_SPI1, 512,
  50. txbuf, rxbuf); /* Atomic transfer operations. */
  51. spiUnselect(&PORTAB_SPI1); /* Slave Select de-assertion. */
  52. cacheBufferInvalidate(&txbuf[0], /* Cache invalidation over the */
  53. sizeof txbuf);/* buffer. */
  54. spiReleaseBus(&PORTAB_SPI1); /* Ownership release. */
  55. }
  56. }
  57. /*
  58. * SPI bus contender 2.
  59. */
  60. static THD_WORKING_AREA(spi_thread_2_wa, 256);
  61. static THD_FUNCTION(spi_thread_2, p) {
  62. (void)p;
  63. chRegSetThreadName("SPI thread 2");
  64. while (true) {
  65. spiAcquireBus(&PORTAB_SPI1); /* Acquire ownership of the bus. */
  66. palWriteLine(PORTAB_LINE_LED1, PORTAB_LED_OFF);
  67. spiStart(&PORTAB_SPI1, &ls_spicfg); /* Setup transfer parameters. */
  68. spiSelect(&PORTAB_SPI1); /* Slave Select assertion. */
  69. spiExchange(&PORTAB_SPI1, 512,
  70. txbuf, rxbuf); /* Atomic transfer operations. */
  71. spiUnselect(&PORTAB_SPI1); /* Slave Select de-assertion. */
  72. cacheBufferInvalidate(&txbuf[0], /* Cache invalidation over the */
  73. sizeof txbuf);/* buffer. */
  74. spiReleaseBus(&PORTAB_SPI1); /* Ownership release. */
  75. }
  76. }
  77. /*
  78. * LED blinker thread, times are in milliseconds.
  79. */
  80. static THD_WORKING_AREA(waThread1, 128);
  81. static THD_FUNCTION(Thread1, arg) {
  82. (void)arg;
  83. chRegSetThreadName("blinker");
  84. while (true) {
  85. bool key_pressed = palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED;
  86. systime_t time = key_pressed ? 250 : 500;
  87. #if SPI_SUPPORTS_CIRCULAR == TRUE
  88. if (key_pressed) {
  89. spiAbort(&PORTAB_SPI1);
  90. }
  91. #endif
  92. #if defined(PORTAB_LINE_LED2)
  93. palToggleLine(PORTAB_LINE_LED2);
  94. #endif
  95. chThdSleepMilliseconds(time);
  96. }
  97. }
  98. /*
  99. * Application entry point.
  100. */
  101. int main(void) {
  102. unsigned i;
  103. /*
  104. * System initializations.
  105. * - HAL initialization, this also initializes the configured device drivers
  106. * and performs the board-specific initializations.
  107. * - Kernel initialization, the main() function becomes a thread and the
  108. * RTOS is active.
  109. */
  110. halInit();
  111. chSysInit();
  112. /*
  113. * Board-dependent GPIO setup code.
  114. */
  115. portab_setup();
  116. /*
  117. * Creates the blinker thread.
  118. */
  119. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  120. /*
  121. * Prepare transmit pattern.
  122. */
  123. for (i = 0; i < sizeof(txbuf); i++)
  124. txbuf[i] = (uint8_t)i;
  125. cacheBufferFlush(&txbuf[0], sizeof txbuf);
  126. #if SPI_SUPPORTS_CIRCULAR == TRUE
  127. /*
  128. * Starting a continuous operation for test.
  129. */
  130. spiStart(&PORTAB_SPI1, &c_spicfg); /* Setup transfer parameters. */
  131. spiSelect(&PORTAB_SPI1); /* Slave Select assertion. */
  132. spiExchange(&PORTAB_SPI1, 512,
  133. txbuf, rxbuf); /* Atomic transfer operations. */
  134. spiUnselect(&PORTAB_SPI1); /* Slave Select de-assertion. */
  135. cacheBufferInvalidate(&txbuf[0], /* Cache invalidation over the */
  136. sizeof txbuf);/* buffer. */
  137. #endif
  138. /*
  139. * Starting the transmitter and receiver threads.
  140. */
  141. chThdCreateStatic(spi_thread_1_wa, sizeof(spi_thread_1_wa),
  142. NORMALPRIO + 1, spi_thread_1, NULL);
  143. chThdCreateStatic(spi_thread_2_wa, sizeof(spi_thread_2_wa),
  144. NORMALPRIO + 1, spi_thread_2, NULL);
  145. /*
  146. * Normal main() thread activity, in this demo it does nothing.
  147. */
  148. while (true) {
  149. chThdSleepMilliseconds(500);
  150. }
  151. return 0;
  152. }