/* only called for a single page, and that page is 0,1,2 only*/ static GOOD_OR_BAD OW_w_page(const BYTE * data, size_t size, off_t offset, const struct parsedname *pn) { int page = offset / _1W_2436_PAGESIZE; // integer round-down ok BYTE scratchin[] = { _1W_READ_SCRATCHPAD, offset, }; BYTE scratchout[] = { _1W_WRITE_SCRATCHPAD, offset, }; BYTE p[_1W_2436_PAGESIZE]; static BYTE copyout[] = { _1W_COPY_SP1_TO_NV1, _1W_COPY_SP2_TO_NV2, _1W_COPY_SP3_TO_NV3, }; BYTE *copy = ©out[page]; struct transaction_log twrite[] = { TRXN_START, TRXN_WRITE2(scratchout), TRXN_WRITE(data, size), TRXN_END, }; struct transaction_log tread[] = { TRXN_START, TRXN_WRITE2(scratchin), TRXN_READ(p, size), TRXN_COMPARE(data, p, size), TRXN_END, }; struct transaction_log tcopy[] = { TRXN_START, TRXN_WRITE1(copy), TRXN_DELAY(10), TRXN_END, }; RETURN_BAD_IF_BAD(BUS_transaction(twrite, pn)) ; RETURN_BAD_IF_BAD(BUS_transaction(tread, pn)) ; return BUS_transaction(tcopy, pn) ; }
/* PAged access -- pre-screened */ static GOOD_OR_BAD OW_w_mem(BYTE * data, size_t size, off_t offset, struct parsedname *pn) { BYTE p[4 + 32] = { _1W_WRITE_SCRATCHPAD, LOW_HIGH_ADDRESS(offset), }; struct transaction_log tcopy[] = { TRXN_START, TRXN_WRITE3(p), TRXN_WRITE(data, size), TRXN_END, }; struct transaction_log tread[] = { TRXN_START, TRXN_WRITE1(p), TRXN_READ(&p[1], 3 + size), TRXN_COMPARE(data, &p[4], size), TRXN_END, }; struct transaction_log tsram[] = { TRXN_START, TRXN_WRITE(p, 4), TRXN_DELAY(32), TRXN_END, }; /* Copy to scratchpad */ RETURN_BAD_IF_BAD(BUS_transaction(tcopy, pn)) ; /* Re-read scratchpad and compare */ p[0] = _1W_READ_SCRATCHPAD; RETURN_BAD_IF_BAD(BUS_transaction(tread, pn)) ; /* Copy Scratchpad to SRAM */ p[0] = _1W_COPY_SCRATCHPAD; return BUS_transaction(tsram, pn) ; }
/* Byte-oriented write */ static GOOD_OR_BAD OW_w_mem(const BYTE * data, size_t size, off_t offset, const struct parsedname *pn) { BYTE scratch[_DS2430A_MEM_SIZE]; BYTE vr[] = { _1W_READ_SCRATCHPAD, BYTE_MASK(offset), }; BYTE of[] = { _1W_WRITE_SCRATCHPAD, BYTE_MASK(offset), }; BYTE cp[] = { _1W_COPY_SCRATCHPAD, _1W_COPY_SCRATCHPAD_VALIDATION_KEY, }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE2(of), TRXN_WRITE(data, size), TRXN_START, TRXN_WRITE2(vr), TRXN_READ(scratch, size), TRXN_COMPARE(data,scratch,size), TRXN_START, TRXN_WRITE2(cp), TRXN_DELAY(10), TRXN_END, }; /* load scratch pad if incomplete write */ if ( size != _DS2430A_MEM_SIZE ) { RETURN_BAD_IF_BAD( OW_r_mem( scratch, 0, 0x00, pn)) ; } /* write data to scratchpad */ /* read back the scratchpad */ /* copy scratchpad to memory */ return BUS_transaction(t, pn); }
static GOOD_OR_BAD OW_r_app(BYTE * data, size_t size, off_t offset, const struct parsedname *pn) { BYTE c3[] = { _1W_READ_APPLICATION_REGISTER, BYTE_MASK(offset), }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE2(c3), TRXN_READ(data, size), TRXN_END, }; return BUS_transaction(t, pn) ; }
/* Byte-oriented write */ static GOOD_OR_BAD OW_r_mem( BYTE * data, size_t size, off_t offset, const struct parsedname *pn) { BYTE fo[] = { _1W_READ_MEMORY, BYTE_MASK(offset), }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE2(fo), TRXN_READ( data, size ) , TRXN_END, }; return BUS_transaction(t, pn); }
static GOOD_OR_BAD OW_r_scratch(BYTE * data, int length, const struct parsedname *pn) { BYTE read_command[1] = { _LCD_COMMAND_SCRATCHPAD_READ, }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE1(read_command), TRXN_READ(data, length), TRXN_END, }; return BUS_transaction(t, pn); }
/* paged, and pre-screened */ static GOOD_OR_BAD OW_w_23page(BYTE * data, size_t size, off_t offset, struct parsedname *pn) { BYTE p[1 + 2 + 32 + 2] = { _1W_WRITE_SCRATCHPAD, LOW_HIGH_ADDRESS(offset), }; struct transaction_log tcopy[] = { TRXN_START, TRXN_WR_CRC16(p, 3 + size, 0), TRXN_END, }; struct transaction_log treread[] = { TRXN_START, TRXN_WRITE1(p), TRXN_READ(&p[1], 3 + size), TRXN_COMPARE(&p[4], data, size), TRXN_END, }; struct transaction_log twrite33[] = { TRXN_START, TRXN_WRITE(p, 4), TRXN_DELAY(5), TRXN_END, }; struct transaction_log twriteEC20[] = { TRXN_START, TRXN_WRITE(p, 4), TRXN_DELAY(10), TRXN_END, }; /* Copy to scratchpad */ memcpy(&p[3], data, size); if (((offset + size) & 0x1F)) { // doesn't end on page boundary, no crc16 tcopy[2].type = tcopy[3].type = trxn_nop; } RETURN_BAD_IF_BAD(BUS_transaction(tcopy, pn)) ; /* Re-read scratchpad and compare */ /* Note that we tacitly shift the data one byte down for the E/S byte */ p[0] = _1W_READ_SCRATCHPAD; RETURN_BAD_IF_BAD(BUS_transaction(treread, pn)) ; /* Copy Scratchpad to SRAM */ p[0] = _1W_COPY_SCRATCHPAD; switch (pn->sn[0]) { case 0x23: // DS2433 return BUS_transaction(twrite33,pn); case 0x43: default: // DS28EC20 return BUS_transaction(twriteEC20,pn); } }
/* No CRC -- 0xF0 code */ GOOD_OR_BAD COMMON_read_memory_F0(struct one_wire_query *owq, size_t page, size_t pagesize) { off_t offset = OWQ_offset(owq) + page * pagesize; BYTE p[3] = { _1W_READ_F0, LOW_HIGH_ADDRESS(offset), }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE3(p), TRXN_READ((BYTE *) OWQ_buffer(owq), OWQ_size(owq)), TRXN_END, }; Set_OWQ_length(owq); return BUS_transaction(t, PN(owq)); }
// use new read_all command static GOOD_OR_BAD OW_r_all(BYTE * data, size_t size, off_t offset, struct parsedname *pn) { BYTE p[4] = { _1W_READ_ALL, LOW_HIGH_ADDRESS(offset), }; struct transaction_log t[] = { TRXN_START, TRXN_WRITE3(p), TRXN_READ1(&p[3]), TRXN_CRC8(p, 4), TRXN_READ(data, size), }; RETURN_BAD_IF_BAD(BUS_transaction(t, pn)) ; return gbGOOD; }
// reads to end of page and discards extra // uses CRC8 static GOOD_OR_BAD OW_r_page(BYTE * data, size_t size, off_t offset, struct parsedname *pn) { BYTE p[4] = { _1W_READ_DATA_CRC8, LOW_HIGH_ADDRESS(offset), }; BYTE q[33]; int rest = 33 - (offset & 0x1F); struct transaction_log t[] = { TRXN_START, TRXN_WRITE3(p), TRXN_READ1(&p[3]), TRXN_CRC8(p, 4), TRXN_READ(q, rest), TRXN_CRC8(q, rest), TRXN_END, }; RETURN_BAD_IF_BAD(BUS_transaction(t, pn)) ; memcpy(data, q, size); return gbGOOD; }
/* paged, and pre-screened */ static GOOD_OR_BAD OW_w_mem(BYTE * data, size_t size, off_t offset, struct parsedname *pn) { BYTE p[1 + 2 + 32 + 2] = { _1W_WRITE_SCRATCHPAD, LOW_HIGH_ADDRESS(offset), }; struct transaction_log tcopy[] = { TRXN_START, TRXN_WRITE(p, 3 + size), TRXN_END, }; struct transaction_log tcopy_crc16[] = { TRXN_START, TRXN_WR_CRC16(p, 3 + size, 0), TRXN_END, }; struct transaction_log tread[] = { TRXN_START, TRXN_WRITE1(p), TRXN_READ(&p[1], 3 + size), TRXN_COMPARE(data, &p[4], size), TRXN_END, }; struct transaction_log tsram[] = { TRXN_START, TRXN_WRITE(p, 4), TRXN_END, }; /* Copy to scratchpad */ memcpy(&p[3], data, size); RETURN_BAD_IF_BAD(BUS_transaction(((offset + size) & 0x1F) != 0 ? tcopy : tcopy_crc16, pn)) ; /* Re-read scratchpad and compare */ /* Note that we tacitly shift the data one byte down for the E/S byte */ p[0] = _1W_READ_SCRATCHPAD; RETURN_BAD_IF_BAD(BUS_transaction(tread, pn)) ; /* Copy Scratchpad to SRAM */ p[0] = _1W_COPY_SCRATCHPAD; return BUS_transaction(tsram, pn) ; }
/* only called for a single page, and that page is 0,1,2 only*/ static GOOD_OR_BAD OW_r_page(BYTE * data, size_t size, off_t offset, const struct parsedname *pn) { int page = offset / _1W_2436_PAGESIZE; // integer round-down ok BYTE scratchin[] = { _1W_READ_SCRATCHPAD, offset, }; static BYTE copyin[] = { _1W_COPY_NV1_TO_SP1, _1W_COPY_NV2_TO_SP2, _1W_COPY_NV3_TO_SP3, }; BYTE *copy = ©in[page]; struct transaction_log tcopy[] = { TRXN_START, TRXN_WRITE1(copy), TRXN_DELAY(10), TRXN_END, }; struct transaction_log tscratch[] = { TRXN_START, TRXN_WRITE2(scratchin), TRXN_READ(data, size), TRXN_END, }; RETURN_BAD_IF_BAD(BUS_transaction(tcopy, pn)) ; return BUS_transaction(tscratch, pn) ; }
static GOOD_OR_BAD OW_r_std(BYTE *buf, size_t *buflen, BYTE type, BYTE stype, const struct parsedname *pn) { BYTE p[3] = { _1W_READ_MOAT, type,stype }; size_t maxlen = *buflen; BYTE len; GOOD_OR_BAD ret = gbGOOD; struct transaction_log tfirst[] = { TRXN_START, TRXN_WRITE3(p), TRXN_READ1(&len), TRXN_END, }; if (maxlen == 0) { return gbGOOD; } LEVEL_DEBUG( "read: read len for %d %d",type,stype) ; /* 0xFF means the device was too slow */ if ( BAD(BUS_transaction(tfirst, pn)) || len == 0xFF) { goto out_bad; } LEVEL_DEBUG( "read: got len %d",len) ; if (len > maxlen) { /* don't read all and don't bother with CRC. * This will abort the read on the client side so that * there'll be no side effects like marked-as-sent buffers * or cleared 'conditional search' flags */ struct transaction_log tinfo[] = { TRXN_READ(buf,maxlen), TRXN_END, }; if ( BAD(BUS_transaction(tinfo, pn)) ) { goto out_bad; } } else { UINT crc; BYTE crcbuf[2]; struct transaction_log recv_buf[] = { TRXN_READ(buf,len), TRXN_READ2(crcbuf), TRXN_END, }; struct transaction_log recv_crc[] = { TRXN_READ2(crcbuf), TRXN_END, }; struct transaction_log xmit_crc[] = { TRXN_WRITE2(crcbuf), TRXN_END, }; if ( BAD(BUS_transaction(len ? recv_buf : recv_crc, pn)) ) { goto out_bad; } crc = CRC16compute(p,3,0); crc = CRC16compute(&len,1,crc); if (len) crc = CRC16compute(buf,len,crc); LEVEL_DEBUG( "read CRC: GOOD, got %02x%02x",crcbuf[0],crcbuf[1]) ; if ( CRC16seeded (crcbuf,2,crc) ) { LEVEL_DEBUG("CRC error"); goto out_bad; } crcbuf[0] = ~crcbuf[0]; crcbuf[1] = ~crcbuf[1]; if ( BAD(BUS_transaction(xmit_crc, pn)) ) { goto out_bad; } *buflen = len; } LEVEL_DEBUG( "read: GOOD, got %d",*buflen) ; return ret; out_bad: return gbBAD; }