main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Timing values are taken from the RM except the PRESC set to 9 because
  17. * the input clock is 72MHz.
  18. * The timings are critical, please always refer to the STM32 Reference
  19. * Manual before attempting changes.
  20. */
  21. #if 0
  22. static const I2CConfig i2cconfig = {
  23. STM32_TIMINGR_PRESC(8U) | /* 72MHz/9 = 8MHz I2CCLK. */
  24. STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
  25. STM32_TIMINGR_SCLH(3U) | STM32_TIMINGR_SCLL(9U),
  26. 0,
  27. 0
  28. };
  29. #endif
  30. static const I2CConfig i2cconfig = {
  31. STM32_TIMINGR_PRESC(15U) |
  32. STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) |
  33. STM32_TIMINGR_SCLH(15U) | STM32_TIMINGR_SCLL(21U),
  34. 0,
  35. 0
  36. };
  37. /*
  38. * This is a periodic thread that does absolutely nothing except flashing
  39. * a LED.
  40. */
  41. static THD_WORKING_AREA(blinker_wa, 128);
  42. static THD_FUNCTION(blinker, arg) {
  43. (void)arg;
  44. chRegSetThreadName("blinker");
  45. while (true) {
  46. palSetPad(GPIOC, GPIOC_LED1);
  47. chThdSleepMilliseconds(500);
  48. palClearPad(GPIOC, GPIOC_LED1);
  49. chThdSleepMilliseconds(500);
  50. }
  51. }
  52. /*
  53. * Application entry point.
  54. */
  55. int main(void) {
  56. /*
  57. * System initializations.
  58. * - HAL initialization, this also initializes the configured device drivers
  59. * and performs the board-specific initializations.
  60. * - Kernel initialization, the main() function becomes a thread and the
  61. * RTOS is active.
  62. */
  63. halInit();
  64. chSysInit();
  65. /*
  66. * Starting the I2C driver 2.
  67. */
  68. i2cStart(&I2CD2, &i2cconfig);
  69. /*
  70. * Starting the blinker thread.
  71. */
  72. chThdCreateStatic(blinker_wa, sizeof(blinker_wa),
  73. NORMALPRIO-1, blinker, NULL);
  74. /*
  75. * Normal main() thread activity, in this demo it does nothing.
  76. */
  77. while (true) {
  78. unsigned i;
  79. msg_t msg;
  80. static const uint8_t cmd[] = {0, 0};
  81. uint8_t data[16];
  82. msg = i2cMasterTransmitTimeout(&I2CD2, 0x52, cmd, sizeof(cmd),
  83. data, sizeof(data), TIME_INFINITE);
  84. if (msg != MSG_OK)
  85. palTogglePad(GPIOC, GPIOC_LED3);
  86. for (i = 0; i < 256; i++) {
  87. msg = i2cMasterReceiveTimeout(&I2CD2, 0x52,
  88. data, sizeof(data), TIME_INFINITE);
  89. if (msg != MSG_OK)
  90. palTogglePad(GPIOC, GPIOC_LED3);
  91. }
  92. chThdSleepMilliseconds(500);
  93. palTogglePad(GPIOC, GPIOC_LED2);
  94. }
  95. return 0;
  96. }