main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. ChibiOS - Copyright (C) 2016..2018 Theodore Ateba
  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 "chprintf.h"
  16. BaseSequentialStream *chp = (BaseSequentialStream *) &SD1;
  17. uint16_t cpt = 0; /* Counter for serial print. */
  18. /*
  19. * @brief Data to transmit over the spi bus.
  20. */
  21. char txbuf[] = "ABCD";
  22. /**
  23. * @brief Spi callback.
  24. */
  25. void spiCallback(SPIDriver *spip) {
  26. chnWrite(&SD1, (const uint8_t *)"*", 1);
  27. spip->txbuf = 0;
  28. };
  29. /**
  30. * @biref SPI configuration strucrture.
  31. */
  32. static const SPIConfig spiCfg = {
  33. spiCallback, /* SPI callback. */
  34. IOPORT2, /* SPI chip select port. */
  35. 7, /* SPI chip select pad. */
  36. SPI_PRESCALER_DIV4_gc, /* prescaler. */
  37. SPI_MODE_0_gc, /* SPI clock mode. */
  38. true, /* SPI role "master/slave". */
  39. false, /* SPI data order. */
  40. false, /* SPI double speed bit. */
  41. SPI_INTLVL_OFF_gc, /* SPI interrupt level. */
  42. };
  43. /**
  44. * @brief Blink thread.
  45. */
  46. static THD_WORKING_AREA(waThread1, 32);
  47. static THD_FUNCTION(Thread1, arg) {
  48. (void)arg;
  49. chRegSetThreadName("Blinker");
  50. while (true) {
  51. palClearPad(IOPORT5, PORTE_LED);
  52. chThdSleepMilliseconds(1000);
  53. palSetPad(IOPORT5, PORTE_LED);
  54. chThdSleepMilliseconds(100);
  55. }
  56. }
  57. /*
  58. * Application entry point.
  59. */
  60. int main(void) {
  61. /*
  62. * System initializations.
  63. * - HAL initialization, this also initializes the configured device drivers
  64. * and performs the board-specific initializations.
  65. * - Kernel initialization, the main() function becomes a thread and the
  66. * RTOS is active.
  67. */
  68. halInit();
  69. chSysInit();
  70. palClearPad(IOPORT5, PORTE_LED);
  71. /*
  72. * Configure TX (PINC3) and RX (PIN2) for the USART1.
  73. */
  74. palSetPadMode(IOPORT3, PIN3, PAL_MODE_OUTPUT_PUSHPULL);
  75. palSetPadMode(IOPORT3, PIN2, PAL_MODE_INPUT_PULLUP);
  76. /*
  77. * Start the Serial driver 1 by using the default configuration.
  78. */
  79. sdStart(&SD1, NULL);
  80. /*
  81. * Activate the spi driver 1 by using the defined configuration.
  82. */
  83. spiStart(&SPID1, &spiCfg);
  84. /*
  85. * Starts the LED blinker thread.
  86. */
  87. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  88. chprintf(chp, "AVR SPI program testhal program example.\r\n");
  89. while (TRUE) {
  90. chprintf(chp, "Hello world %i.\r\n", cpt++);
  91. spiSelect(&SPID1);
  92. spiStartSend(&SPID1, 4, txbuf);
  93. spiUnselect(&SPID1);
  94. chThdSleepMilliseconds(1000);
  95. }
  96. return 0;
  97. }