/** * @brief Starts the SDIO clock and sets it to init mode (400kHz or less). * * @param[in] sdcp pointer to the @p SDCDriver object * * @notapi */ void sdc_lld_start_clk(SDCDriver *sdcp) { /* Initial clock setting: 400kHz, 1bit mode.*/ sdcp->sdmmc->CLKCR = SDMMC_CLKDIV_LS; sdcp->sdmmc->POWER |= SDMMC_POWER_PWRCTRL_0 | SDMMC_POWER_PWRCTRL_1; sdcp->sdmmc->CLKCR |= SDMMC_CLKCR_CLKEN; /* Clock activation delay.*/ osalThreadSleep(OSAL_MS2ST(STM32_SDC_SDMMC_CLOCK_DELAY)); }
/** * @brief Read some bites from slave device. * * @param[in] owp pointer to the @p onewireDriver object * @param[in] txbuf pointer to the buffer with data to be written * @param[in] txbytes amount of data to be written * @param[in] pullup_time how long strong pull up must be activated. Set * it to 0 if not needed. */ void onewireWrite(onewireDriver *owp, uint8_t *txbuf, size_t txbytes, systime_t pullup_time) { PWMDriver *pwmd; PWMConfig *pwmcfg; size_t mch, sch; osalDbgCheck((NULL != owp) && (NULL != txbuf)); osalDbgCheck((txbytes > 0) && (txbytes <= ONEWIRE_MAX_TRANSACTION_LEN)); osalDbgAssert(owp->reg.state == ONEWIRE_READY, "Invalid state"); #if !ONEWIRE_USE_STRONG_PULLUP osalDbgAssert(0 == pullup_time, "Non zero time is valid only when strong pull enabled"); #endif pwmd = owp->config->pwmd; pwmcfg = owp->config->pwmcfg; mch = owp->config->master_channel; sch = owp->config->sample_channel; owp->buf = txbuf; owp->reg.bit = 0; owp->reg.final_timeslot = false; owp->reg.bytes = txbytes; pwmcfg->period = ONEWIRE_ZERO_WIDTH + ONEWIRE_RECOVERY_WIDTH; pwmcfg->callback = pwm_write_bit_cb; pwmcfg->channels[mch].callback = NULL; pwmcfg->channels[mch].mode = owp->config->pwmmode; pwmcfg->channels[sch].callback = NULL; pwmcfg->channels[sch].mode = PWM_OUTPUT_DISABLED; #if ONEWIRE_USE_STRONG_PULLUP if (pullup_time > 0) { owp->reg.state = ONEWIRE_PULL_UP; owp->reg.need_pullup = true; } #endif ow_bus_active(owp); osalSysLock(); pwmEnablePeriodicNotificationI(pwmd); osalThreadSuspendS(&owp->thread); osalSysUnlock(); pwmDisablePeriodicNotification(pwmd); ow_bus_idle(owp); #if ONEWIRE_USE_STRONG_PULLUP if (pullup_time > 0) { osalThreadSleep(pullup_time); owp->config->pullup_release(); owp->reg.state = ONEWIRE_READY; } #endif }
/** * @brief Waits that the card reaches an idle state. * * @param[in] mmcp pointer to the @p MMCDriver object * * @notapi */ static void sync(MMCDriver *mmcp) { uint8_t buf[1]; spiSelect(mmcp->config->spip); while (TRUE) { spiReceive(mmcp->config->spip, 1, buf); if (buf[0] == 0xFF) break; #ifdef MMC_NICE_WAITING /* Trying to be nice with the other threads.*/ osalThreadSleep(1); #endif } spiUnselect(mmcp->config->spip); }
/** * @brief Waits an idle condition. * * @param[in] mmcp pointer to the @p MMCDriver object * * @notapi */ static void wait(MMCDriver *mmcp) { int i; uint8_t buf[4]; for (i = 0; i < 16; i++) { spiReceive(mmcp->config->spip, 1, buf); if (buf[0] == 0xFF) return; } /* Looks like it is a long wait.*/ while (TRUE) { spiReceive(mmcp->config->spip, 1, buf); if (buf[0] == 0xFF) break; #ifdef MMC_NICE_WAITING /* Trying to be nice with the other threads.*/ osalThreadSleep(1); #endif } }
void onewireTest(void) { int16_t tmp; uint8_t rombuf[24]; size_t devices_on_bus = 0; size_t i = 0; bool presence; onewireObjectInit(&OWD1); onewireStart(&OWD1, &ow_cfg); #if ONEWIRE_SYNTH_SEARCH_TEST synthSearchRomTest(&OWD1); #endif for (i=0; i<3; i++) temperature[i] = -666; while (true) { if (true == onewireReset(&OWD1)){ memset(rombuf, 0x55, sizeof(rombuf)); search_led_on(); devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3); search_led_off(); osalDbgCheck(devices_on_bus <= 3); osalDbgCheck(devices_on_bus > 0); if (1 == devices_on_bus){ /* test read rom command */ presence = onewireReset(&OWD1); osalDbgCheck(true == presence); testbuf[0] = ONEWIRE_CMD_READ_ROM; onewireWrite(&OWD1, testbuf, 1, 0); onewireRead(&OWD1, testbuf, 8); osalDbgCheck(testbuf[7] == onewireCRC(testbuf, 7)); osalDbgCheck(0 == memcmp(rombuf, testbuf, 8)); } /* start temperature measurement on all connected devices at once */ presence = onewireReset(&OWD1); osalDbgCheck(true == presence); testbuf[0] = ONEWIRE_CMD_SKIP_ROM; testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP; #if ONEWIRE_USE_STRONG_PULLUP onewireWrite(&OWD1, testbuf, 2, MS2ST(750)); #else onewireWrite(&OWD1, testbuf, 2, 0); /* poll bus waiting ready signal from all connected devices */ testbuf[0] = 0; while (testbuf[0] == 0){ osalThreadSleepMilliseconds(50); onewireRead(&OWD1, testbuf, 1); } #endif for (i=0; i<devices_on_bus; i++) { /* read temperature device by device from their scratchpads */ presence = onewireReset(&OWD1); osalDbgCheck(true == presence); testbuf[0] = ONEWIRE_CMD_MATCH_ROM; memcpy(&testbuf[1], &rombuf[i*8], 8); testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD; onewireWrite(&OWD1, testbuf, 10, 0); onewireRead(&OWD1, testbuf, 9); osalDbgCheck(testbuf[8] == onewireCRC(testbuf, 8)); memcpy(&tmp, &testbuf, 2); temperature[i] = ((int32_t)tmp * 625) / 10; } } else { osalSysHalt("No devices found"); } osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */ } onewireStop(&OWD1); }