/** * Issue ROM skip command. * @return OW_OK if some device is present or OW_NO_DEV if not. */ OW_State OW_ROMSkip(void) { if (OW_Reset() == OW_NO_DEV) return OW_NO_DEV; OW_ByteWrite(OW_ROM_SKIP); return OW_OK; }
int8_t Device_Search(void) { uint8_t OW_Status; OW_Status = OW_Reset(); CheckOwStatus(OW_Status); return OW_Status; }
/** * Read ROM address of device, works only for one device on the bus. * @return 64-bit device address. */ uint64_t OW_ROMRead(void) { uint64_t iRes = 0; int i; if (OW_Reset() == OW_NO_DEV) return 0; OW_ByteWrite(OW_ROM_READ); for (i = 0; i < 8; i++) ((uint8_t*) & iRes)[i] = OW_ByteRead(); return iRes; }
/** * Issue ROM match command. * @param iAddress 64-bit device address. * @return OW_OK if device is present or OW_NO_DEV if not. */ OW_State OW_ROMMatch(uint64_t iAddress) { int i; if (iAddress == OW_ADDRESS_ALL) return OW_ROMSkip(); if (OW_Reset() == OW_NO_DEV) return OW_NO_DEV; OW_ByteWrite(OW_ROM_MATCH); for (i = 0; i < 8; i++) OW_ByteWrite(((uint8_t*) & iAddress)[i]); return OW_OK; }
/** * Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing * search state. * @return 64-bit device address or 0 if no device found. */ uint64_t OW_SearchNext(void) { uint8_t iSearchDirection; int iIDBit, iCmpIDBit; /* Initialize for search */ uint8_t iROMByteMask = 1; uint8_t iCRC = 0; int iIDBitNumber = 1; int iLastZero = 0; int iROMByteNumber = 0; int iSearchResult = 0; /* If the last call was not the last one */ if (!stSearch.iLastDeviceFlag) { /* 1-Wire reset */ if (!OW_Reset()) { /* Reset the search */ stSearch.iLastDiscrepancy = 0; stSearch.iLastDeviceFlag = 0; stSearch.iLastFamilyDiscrepancy = 0; return 0; } /* Issue the search command */ OW_ByteWrite(OW_ROM_SEARCH); /* Loop to do the search */ do { #ifdef OW_USE_PARASITE_POWER #warning "FixMe: Charging IC, should work without this." OW_StrongPullUp(); int i; for (i = 0; i < 0xFFF; i++); OW_WeakPullUp(); #endif /* Read a bit and its complement */ iIDBit = OW_BitRead(); iCmpIDBit = OW_BitRead(); /* Check for no devices on 1-wire */ if ((iIDBit == 1) && (iCmpIDBit == 1)) break; else { /* All devices coupled have 0 or 1 */ if (iIDBit != iCmpIDBit) /* Bit write value for search */ iSearchDirection = iIDBit; else { /* if this discrepancy if before the Last Discrepancy on a previous next then pick the same as last time */ if (iIDBitNumber < stSearch.iLastDiscrepancy) iSearchDirection = ((((uint8_t*) & stSearch.ROM)[iROMByteNumber] & iROMByteMask) > 0); else /* If equal to last pick 1, if not then pick 0 */ iSearchDirection = (iIDBitNumber == stSearch.iLastDiscrepancy); /* If 0 was picked then record its position in iLastZero */ if (iSearchDirection == 0) { iLastZero = iIDBitNumber; /* Check for Last discrepancy in family */ if (iLastZero < 9) stSearch.iLastFamilyDiscrepancy = iLastZero; } } /* Set or clear the bit in the ROM byte with mask rom_byte_mask */ if (iSearchDirection == 1) ((uint8_t*) & stSearch.ROM)[iROMByteNumber] |= iROMByteMask; else ((uint8_t*) & stSearch.ROM)[iROMByteNumber] &= ~iROMByteMask; /* Set serial number search direction */ OW_BitWrite(iSearchDirection); /* Increment the byte counter and shift the mask */ iIDBitNumber++; iROMByteMask <<= 1; /* If the mask is 0 then go to new ROM byte number and reset mask */ if (iROMByteMask == 0) { /* Accumulate the CRC */ iCRC = OW_CRCCalculate(iCRC, ((uint8_t*) & stSearch.ROM)[iROMByteNumber]); iROMByteNumber++; iROMByteMask = 1; } } } while (iROMByteNumber < 8); /* Loop until through all ROM bytes 0-7 */ /* If the search was successful then */ if (!((iIDBitNumber < 65) || (iCRC != 0))) { stSearch.iLastDiscrepancy = iLastZero; /* Check for last device */ if (stSearch.iLastDiscrepancy == 0) stSearch.iLastDeviceFlag = 1; iSearchResult = 1; } } /* If no device found then reset counters so next 'search' will be like a first */ if (!iSearchResult || !((uint8_t*) & stSearch.ROM)[0]) { stSearch.iLastDiscrepancy = 0; stSearch.iLastDeviceFlag = 0; stSearch.iLastFamilyDiscrepancy = 0; return 0; } return stSearch.ROM; }
//----------------------------------------------------------------------------- // процедура общения с шиной 1-wire // sendReset - посылать RESET в начале общения. // OW_SEND_RESET или OW_NO_RESET // command - массив байт, отсылаемых в шину. Если нужно чтение - отправляем OW_READ_SLOTH // cLen - длина буфера команд, столько байт отошлется в шину // data - если требуется чтение, то ссылка на буфер для чтения // dLen - длина буфера для чтения. Прочитается не более этой длины // readStart - с какого символа передачи начинать чтение (нумеруются с 0) // можно указать OW_NO_READ, тогда можно не задавать data и dLen //----------------------------------------------------------------------------- uint8_t OW_Send(uint8_t sendReset, uint8_t *command, uint8_t cLen, uint8_t *data, uint8_t dLen, uint8_t readStart) { // если требуется сброс - сбрасываем и проверяем на наличие устройств if (sendReset == OW_SEND_RESET) { if (OW_Reset() == OW_NO_DEVICE) { return OW_NO_DEVICE; } } while (cLen > 0) { OW_toBits(*command, ow_buf); command++; cLen--; DMA_InitTypeDef DMA_InitStructure; // DMA на чтение DMA_DeInit(OW_DMA_CH_RX); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) ow_buf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 8; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(OW_DMA_CH_RX, &DMA_InitStructure); // DMA на запись DMA_DeInit(OW_DMA_CH_TX); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) ow_buf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 8; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(OW_DMA_CH_TX, &DMA_InitStructure); // старт цикла отправки USART_ClearFlag(OW_USART, USART_FLAG_RXNE | USART_FLAG_TC | USART_FLAG_TXE); USART_DMACmd(OW_USART, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE); DMA_Cmd(OW_DMA_CH_RX, ENABLE); DMA_Cmd(OW_DMA_CH_TX, ENABLE); // Ждем, пока не примем 8 байт while (DMA_GetFlagStatus(OW_DMA_FLAG) == RESET){ #ifdef OW_GIVE_TICK_RTOS taskYIELD(); IWDG_ReloadCounter(); #endif } // отключаем DMA DMA_Cmd(OW_DMA_CH_TX, DISABLE); DMA_Cmd(OW_DMA_CH_RX, DISABLE); USART_DMACmd(OW_USART, USART_DMAReq_Tx | USART_DMAReq_Rx, DISABLE); // если прочитанные данные кому-то нужны - выкинем их в буфер if (readStart == 0 && dLen > 0) { *data = OW_toByte(ow_buf); data++; dLen--; } else { if (readStart != OW_NO_READ) { readStart--; } } } return OW_OK; }