📄 owtrnu.c
字号:
// check if not EPROM if (!is_eprom) { // write the page if (!Write_Scratchpad(portnum,construct_buffer,start_page,buffer_cnt)) {#if DEBUG_OW_TRNU printf ("owWritePacketStd: couldn't Write_Scratchpad\n");#endif return FALSE; } // copy the scratchpad if (!Copy_Scratchpad(portnum,start_page,buffer_cnt)) {#if DEBUG_OW_TRNU printf ("owWritePacketStd: couldn't Copy_Scratchpad\n");#endif return FALSE; } // copy scratch pad was good then success return TRUE; } // is EPROM else { // calculate the start address start_address = ((start_page >> 3) << 8) | ((start_page << 5) & 0xFF); do_access = TRUE; // loop to program each byte for (i = 0; i < buffer_cnt; i++) { if (owProgramByte(portnum,construct_buffer[i], start_address + i, 0x0F, crc_type, do_access) != construct_buffer[i]) return FALSE; do_access = FALSE; } return TRUE; }}//--------------------------------------------------------------------------// Write a byte to an EPROM 1-Wire device.//// Supported devices: crc_type=0(CRC8)// DS1982// crc_type=1(CRC16) // DS1985, DS1986, DS2407 //// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to// indicate the symbolic port number.// 'write_byte' - byte to program// 'addr' - address of byte to program// 'write_cmd' - command used to write (0x0F reg mem, 0x55 status)// 'crc_type' - CRC used (0 CRC8, 1 CRC16)// 'do_access' - Flag to access device for each byte // (0 skip access, 1 do the access)// WARNING, only use do_access=0 if programing the NEXT// byte immediatly after the previous byte.//// Returns: >=0 success, this is the resulting byte from the program// effort// -1 error, device not connected or program pulse voltage// not available//int owProgramByte(int portnum, int write_byte, int addr, int write_cmd, int crc_type, int do_access){ ushort lastcrc16; uchar lastcrc8; // optionally access the device if (do_access) { if (!owAccess(portnum)) return -1; // send the write command if (!owWriteByte(portnum,write_cmd)) return -1; // send the address if (!owWriteByte(portnum,addr & 0xFF)) return -1; if (!owWriteByte(portnum,addr >> 8)) return -1; } // send the data to write if (!owWriteByte(portnum,write_byte)) return -1; // read the CRC if (crc_type == 0) { // calculate CRC8 if (do_access) { setcrc8(portnum,0); docrc8(portnum,(uchar)write_cmd); docrc8(portnum,(uchar)(addr & 0xFF)); docrc8(portnum,(uchar)(addr >> 8)); } else setcrc8(portnum,(uchar)(addr & 0xFF)); docrc8(portnum,(uchar)write_byte); // read and calculate the read crc lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum)); // crc should now be 0x00 if (lastcrc8 != 0) return -1; } else { // CRC16 if (do_access) { setcrc16(portnum,0); docrc16(portnum,(ushort)write_cmd); docrc16(portnum,(ushort)(addr & 0xFF)); docrc16(portnum,(ushort)(addr >> 8)); } else setcrc16(portnum,(ushort)addr); docrc16(portnum,(ushort)write_byte); // read and calculate the read crc docrc16(portnum,(ushort)owReadByte(portnum)); lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum)); // crc should now be 0xB001 if (lastcrc16 != 0xB001) return -1; } // send the program pulse if (!owProgramPulse(portnum)) return -1; // read back and return the resulting byte return owReadByte(portnum);}//--------------------------------------------------------------------------// Write the scratchpad of a standard NVRam device such as the DS1992,3,4// and verify its contents. //// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to// indicate the symbolic port number.// 'write_buf' - pointer to buffer containing data to write// 'start_page' - page number to write packet to// 'write_len' - number of data byte in write_buf//// Returns: TRUE(1) success, the data was written and verified// FALSE(0) failure, the data could not be written// //int Write_Scratchpad(int portnum, uchar *write_buf, int start_page, int write_len){ int i,sendlen=0; uchar sendpacket[50]; #if DEBUG_OW_TRNU printf ("Write_Scratchpad: %d %d\n", start_page, write_len);#endif // match command sendpacket[sendlen++] = 0x55; for (i = 0; i < 8; i++) sendpacket[sendlen++] = SerialNum[portnum][i]; // write scratchpad command sendpacket[sendlen++] = 0x0F; // write the target address sendpacket[sendlen++] = ((start_page << 5) & 0xFF); sendpacket[sendlen++] = (start_page >> 3); // write packet bytes for (i = 0; i < write_len; i++) sendpacket[sendlen++] = write_buf[i]; // send/recieve the transfer buffer if (owBlock(portnum,TRUE,sendpacket,sendlen)) { // now attempt to read back to check sendlen = 0; // match command sendpacket[sendlen++] = 0x55; for (i = 0; i < 8; i++) sendpacket[sendlen++] = SerialNum[portnum][i]; // read scratchpad command sendpacket[sendlen++] = 0xAA; // read the target address, offset and data for (i = 0; i < (write_len + 3); i++) sendpacket[sendlen++] = 0xFF; // send/recieve the transfer buffer if (owBlock(portnum,TRUE,sendpacket,sendlen)) { // check address and offset of scratchpad read if ((sendpacket[10] != (int)((start_page << 5) & 0xFF)) || (sendpacket[11] != (int)(start_page >> 3)) || (sendpacket[12] != (int)(write_len - 1))) {#if DEBUG_OW_TRNU printf ("\nWrite_Scratchpad: check failed\n");#endif //return FALSE; } // verify each data byte for (i = 0; i < write_len; i++) if (sendpacket[i+13] != write_buf[i]) {#if DEBUG_OW_TRNU printf ("\nWrite_Scratchpad: data check failed\n");#endif return FALSE; } // must have verified return TRUE; } } #if DEBUG_OW_TRNU printf ("\nWrite_Scratchpad: owBlock failed\n");#endif // failed a block tranfer return FALSE;}//--------------------------------------------------------------------------// Copy the contents of the scratchpad to its intended nv ram page. The// page and length of the data is needed to build the authorization bytes// to copy.//// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to// indicate the symbolic port number.// 'start_page' - page number to write packet to// 'write_len' - number of data bytes that are being copied//// Returns: TRUE(1) success// FALSE(0) failure//int Copy_Scratchpad(int portnum, int start_page, int write_len){ int i,sendlen=0; uchar sendpacket[50];#if DEBUG_OW_TRNU printf ("Copy_Scratchpad: %d %d\n", start_page, write_len);#endif // match command sendpacket[sendlen++] = 0x55; for (i = 0; i < 8; i++) sendpacket[sendlen++] = SerialNum[portnum][i]; // copy scratchpad command sendpacket[sendlen++] = 0x55; // write the target address sendpacket[sendlen++] = ((start_page << 5) & 0xFF); sendpacket[sendlen++] = (start_page >> 3); sendpacket[sendlen++] = write_len - 1; // read copy result sendpacket[sendlen++] = 0xFF; #if DEBUG_OW_TRNU printf ("Copy_Scratchpad: %d, %02x %02x %02x %02x\n", sendlen, sendpacket[10],sendpacket[11],sendpacket[12],sendpacket[13]);#endif // send/recieve the transfer buffer if (owBlock(portnum,TRUE,sendpacket,sendlen)) { // check address and offset of scratchpad read if ((sendpacket[10] != (int)((start_page << 5) & 0xFF)) || (sendpacket[11] != (int)(start_page >> 3)) || (sendpacket[12] != (int)(write_len - 1)) || (sendpacket[13] & 0xF0)) {#if DEBUG_OW_TRNU printf ("Copy_Scratchpad: %d, check failed: %02x %02x %02x %02x\n", sendlen, sendpacket[10],sendpacket[11],sendpacket[12],sendpacket[13]);#endif return FALSE; } else {#if DEBUG_OW_TRNU printf ("Copy_Scratchpad: %02x %02x %02x %02x\n", sendpacket[10],sendpacket[11],sendpacket[12],sendpacket[13]);#endif return TRUE; } }#if DEBUG_OW_TRNU printf ("Copy_Scratchpad: owBlock failed\n");#endif // failed a block tranfer return FALSE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -