main.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. ChibiOS - Copyright (C) 2016 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. /**
  17. * @brief Global variables
  18. */
  19. BaseSequentialStream * chp = (BaseSequentialStream *) &SD1;
  20. /*
  21. * @brief Data to transmit over the spi bus.
  22. */
  23. char txbuf[] = "ABCD";
  24. /**
  25. * @brief Spi callback.
  26. */
  27. void spiCallback(SPIDriver *spip) {
  28. chnWrite(&SD1, (const uint8_t *)"*", 1);
  29. spip->txbuf = 0;
  30. };
  31. /**
  32. * @biref SPI configuration strucrture.
  33. */
  34. static const SPIConfig spiCfg = {
  35. spiCallback, /* SPI callback. */
  36. IOPORT2, /* SPI chip select port. */
  37. 7, /* SPI chip select pad. */
  38. SPI_CR_DORD_MSB_FIRST | /* SPI Data order. */
  39. SPI_CR_CPOL_CPHA_MODE(0) | /* SPI clock polarity and phase. */
  40. SPI_CR_SCK_FOSC_128, /* SPI clock. */
  41. SPI_SR_SCK_FOSC_2 /* SPI double speed bit. */
  42. };
  43. /*
  44. * Application entry point.
  45. */
  46. int main(void) {
  47. /*
  48. * System initializations.
  49. * - HAL initialization, this also initializes the configured device drivers
  50. * and performs the board-specific initializations.
  51. * - Kernel initialization, the main() function becomes a thread and the
  52. * RTOS is active.
  53. */
  54. halInit();
  55. chSysInit();
  56. /*
  57. * Activates the serial driver 1 using the driver default configuration.
  58. */
  59. sdStart(&SD1, NULL);
  60. /*
  61. * Activate the spi driver 1 by using the defined configuration.
  62. */
  63. spiStart(&SPID1, &spiCfg);
  64. chprintf(chp, "AVR SPI program testhal program example.\r\n");
  65. while (TRUE) {
  66. spiSelect(&SPID1);
  67. spiStartSend(&SPID1, 4, txbuf);
  68. spiUnselect(&SPID1);
  69. chThdSleepMilliseconds(1000);
  70. }
  71. /*
  72. * Stop the spi driver.
  73. */
  74. spiStop(&SPID1);
  75. }