123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "ch.h"
- #include "hal.h"
- #if 0
- static const I2CConfig i2cconfig = {
- STM32_TIMINGR_PRESC(8U) |
- STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
- STM32_TIMINGR_SCLH(3U) | STM32_TIMINGR_SCLL(9U),
- 0,
- 0
- };
- #endif
- static const I2CConfig i2cconfig = {
- STM32_TIMINGR_PRESC(15U) |
- STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) |
- STM32_TIMINGR_SCLH(15U) | STM32_TIMINGR_SCLL(21U),
- 0,
- 0
- };
- static THD_WORKING_AREA(blinker_wa, 128);
- static THD_FUNCTION(blinker, arg) {
- (void)arg;
- chRegSetThreadName("blinker");
- while (true) {
- palSetPad(GPIOC, GPIOC_LED1);
- chThdSleepMilliseconds(500);
- palClearPad(GPIOC, GPIOC_LED1);
- chThdSleepMilliseconds(500);
- }
- }
- int main(void) {
-
- halInit();
- chSysInit();
-
- i2cStart(&I2CD2, &i2cconfig);
-
- chThdCreateStatic(blinker_wa, sizeof(blinker_wa),
- NORMALPRIO-1, blinker, NULL);
-
- while (true) {
- unsigned i;
- msg_t msg;
- static const uint8_t cmd[] = {0, 0};
- uint8_t data[16];
- msg = i2cMasterTransmitTimeout(&I2CD2, 0x52, cmd, sizeof(cmd),
- data, sizeof(data), TIME_INFINITE);
- if (msg != MSG_OK)
- palTogglePad(GPIOC, GPIOC_LED3);
- for (i = 0; i < 256; i++) {
- msg = i2cMasterReceiveTimeout(&I2CD2, 0x52,
- data, sizeof(data), TIME_INFINITE);
- if (msg != MSG_OK)
- palTogglePad(GPIOC, GPIOC_LED3);
- }
- chThdSleepMilliseconds(500);
- palTogglePad(GPIOC, GPIOC_LED2);
- }
- return 0;
- }
|