void* malloc_align(size_t bytes, int alignement) { if (alignement < 8) alignement = 8; int pow = next_two_power(alignement); int mask = pow - 1; printf("alignement: %i\nbit and mask:\n", alignement); bitprint(pow); bitprint(mask); int min_offset = 4; if (min_offset < pow/8) min_offset = pow/8; char* base = malloc(bytes + min_offset); if (!base) return NULL; printf("base pointer: "); bitprint((long)base); int align = pow - (((long)base) & mask); char* aligned_pointer = base + align; *( ((int*)aligned_pointer) - 1 ) = align; printf("aligned pointer: "); bitprint((long)aligned_pointer); return (void*) aligned_pointer; }
static const char * tick(const entry *e) { static char result[1024]; result[0] = '\0'; printone(result, sizeof(result), bitprint(e->minute, MINUTE_COUNT)); printone(result, sizeof(result), bitprint(e->hour, HOUR_COUNT)); printone(result, sizeof(result), bitprint(e->dom, DOM_COUNT)); printone(result, sizeof(result), bitprint(e->month, MONTH_COUNT)); printone(result, sizeof(result), bitprint(e->dow, DOW_COUNT)); strlcat(result, "flags=", sizeof(result)); strlcat(result, flagsprint(e->flags), sizeof(result)); return result; }
int main(int argc,char *argv[]) { if(argc != 2){ return -1; } unsigned char value=strtoul(argv[1],NULL,16); bitprint(value); return 0; }
void free_align(void* aligned_pointer) { if (!aligned_pointer) return; int align = *( ((int*)aligned_pointer) - 1); char* offset = ((char*)aligned_pointer) - align; printf("freeing pointer: "); bitprint((long)offset); free(((char*)aligned_pointer) - align); }
/* ds2338mem_rd Reading DS2438 memory returns one page of memory input parameters portnum the port number of the port being used for the 1-Wire Network. SNum the serial number for the part that the read is to be done on. pageno the page number of memory to be read */ int ds2438mem_rd(int portnum, uchar *SNum, uchar *pagemem, uchar pageno, char *device) { int block_cnt; int i; ushort lastcrc8; owSerialNum(portnum,SNum,FALSE); block_cnt = 0; // Recall the Status/Configuration page // Recall command pagemem[block_cnt++] = 0xB8; // Page to Recall pagemem[block_cnt++] = pageno; owAccess(portnum); owBlock(portnum,FALSE,pagemem,block_cnt); syslog(LOG_DEBUG, "ds2438mem_rd: recall memory (B8h %xh): %s\n", pageno, ppagemem(pagemem)); block_cnt = 0; // Read the Status/Configuration byte // Read scratchpad command pagemem[block_cnt++] = 0xBE; // Page for the Status/Configuration byte pagemem[block_cnt++] = pageno; for(i=0;i<9;i++) pagemem[block_cnt++] = 0xFF; owAccess(portnum); owBlock(portnum,FALSE,pagemem,block_cnt); syslog(LOG_DEBUG,"ds2438mem_rd: read scratchpad (BEh %xh): %s \n", pageno, ppagemem( pagemem)); setcrc8(portnum,0); for(i=2;i<block_cnt;i++) { lastcrc8 = docrc8(portnum,pagemem[i]); } if(lastcrc8 != 0x00) { syslog(LOG_ALERT, "ds2438mem_rd: CRC error "); bitprint( lastcrc8, "lastcrc8"); return 1; } return 0; }
int ds2438mem_dump( int portnum, int verbose, uchar *serialnum, char *device) { int i; int vs_sign, t_sign; long vs_low, vs_high, vs_val; long t_low, t_high, t_val; uchar pageno; uchar pagemem[NBUFF+1] = ""; uchar mempage[8] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; float temp, Vsens, Vacc; syslog(LOG_INFO, "DS2438 memory pages dump:\n"); if ( verbose == FALSE) { for ( i = 0; i < 8; i++) { ds2438mem_rd( 0, serialnum, pagemem, mempage[i], device); syslog(LOG_INFO, "page# <%xh>: %s\n", mempage[i], ppagemem(pagemem)); } } else { pageno = 0x00; ds2438mem_rd( 0, serialnum, pagemem, pageno, device); syslog(LOG_INFO, "page# <%xh>: %s\n", pageno, ppagemem(pagemem)); bitprint( pagemem[2], "\tstatus config : page <0h> byte <0h>"); /* Current A/D Control Bit */ if (pagemem[2] & IAD) { syslog(LOG_INFO, "\t\tIAD: 1 ( current A/D and ICA enabled)\n"); } else { syslog(LOG_INFO, "\t\tIAD: 0 ( current A/D and ICA disabled)\n"); } /* Current Accumulator Configuration */ if ( pagemem[2] & CA) { syslog(LOG_INFO, "\t\tCA : 1 ( CCA/DCA enabled)\n"); } else { syslog(LOG_INFO, "\t\tCA : 0 ( CCA/DCA disabled)\n"); } /* Current Accumulator Shadow Selector Bit */ if ( pagemem[2] & EE ) { syslog(LOG_INFO, "\t\tEE : 1 ( CCA/DCA data shadowed to EEPROM)\n"); } else { syslog(LOG_INFO, "\t\tEE : 0 ( CCA/DCA data not shadowd to EEPROM)\n"); } /* Voltage A/D Input Select Bit */ if ( pagemem[2] & AD) { syslog(LOG_INFO, "\t\tAD : 1 ( Vdd selected for AD conversion)\n"); } else { syslog(LOG_INFO, "\t\tAD : 0 ( Vad selected for AD conversion)\n"); } /* Temperature Busy Flag */ if ( pagemem[2] & TB) { syslog(LOG_INFO, "\t\tTB : 1 ( temperature conversion in progress)\n"); } else { syslog(LOG_INFO, "\t\tTB : 0 ( temperature conversion completed)\n"); } /* Nonvolatile Memory Busy Flag */ if ( pagemem[2] & NVB) { syslog(LOG_INFO, "\t\tNVB: 1 ( copy from scratchpad to EEPROM in progress)\n"); } else { syslog(LOG_INFO, "\t\tNVB: 0 ( nonvolatile memory not busy)\n"); } /* A/D Converter Busy Flag */ if ( pagemem[2] & ADB) { syslog(LOG_INFO, "\t\tADB: 1 ( A/D conversion in progress on battery voltage)\n"); } else { syslog(LOG_INFO, "\t\tADB: 0 ( A/D conversion complete)\n"); } bitprint( pagemem[3], "\ttemperature lsb ( page <0h> byte <1h>)"); bitprint( pagemem[4], "\ttemperature msb ( page <0h> byte <2h>)"); /* temperature readout */ /* temperature sign is bit 7 of temperature msb */ if ( pagemem[4] & TS) { t_sign = -1; syslog(LOG_INFO, "\t\tTemperature sign bit set: temperature negative\n"); } else { syslog(LOG_INFO, "\t\tTemperature sign bit not set: temperature positive\n"); t_sign = 1; } /* temperature value is composed of temperature lsb and temperature msb */ t_low = pagemem[3] >> 3; t_high = ( pagemem[4] & ~TS) << 5; //t_high = pagemem[4] << 5; t_val = t_high + t_low; temp = 0.03125 * t_val * t_sign; syslog(LOG_INFO, "\t\tTemperature: %f [deg C]\n", temp); bitprint( pagemem[5], "\tvoltage lsb ( page <0h> byte <3h>)"); bitprint( pagemem[6], "\tvoltage msb ( page <0h> byte <4h>)"); bitprint( pagemem[7], "\tcurrent lsb ( page <0h> byte <5h>)"); bitprint( pagemem[8], "\tcurrent msb ( page <0h> byte <6h>)"); /* Vsens is contained in current lsb and current msb */ if ( pagemem[8] & VSS) { vs_sign = -1; } else { vs_sign = 1; } vs_low = pagemem[7]; vs_high = ( pagemem[8] & ~VSS) << 8; //vs_high = pagemem[8] << 8; vs_val = vs_high + vs_low; Vsens = 0.2441 * vs_val * vs_sign; syslog(LOG_INFO,"\t\tVsens: %f [mV]\n", Vsens); bitprint( pagemem[9], "\tthreshold ( page <0h> byte <7h>)"); pageno = 0x01; ds2438mem_rd( 0, serialnum, pagemem, pageno, device); syslog(LOG_INFO,"page# <%xh>: %s\n", pageno, ppagemem(pagemem)); bitprint( pagemem[2], "\tetm byte 0 ( page <1h> byte <0h>)"); bitprint( pagemem[3], "\tetm byte 1 ( page <1h> byte <1h>)"); bitprint( pagemem[4], "\tetm byte 2 ( page <1h> byte <2h>)"); bitprint( pagemem[5], "\tetm byte 3 ( page <1h> byte <3h>)"); bitprint( pagemem[6], "\tica ( page <1h> byte <4h>)"); Vacc = 0.4882 * pagemem[6]; syslog(LOG_INFO,"\t\tIntegrated current accumulator: Vacc: %f [mVhr]\n", Vacc); bitprint( pagemem[7], "\toffset lsb ( page <1h> byte <5h>)"); bitprint( pagemem[8], "\toffset msb ( page <1h> byte <6h>)"); bitprint( pagemem[9], "\treserved ( page <1h> byte <7h>)"); pageno = 0x02; ds2438mem_rd( 0, serialnum, pagemem, pageno, device); syslog(LOG_INFO, "page# <%xh>: %s\n", pageno, ppagemem(pagemem)); bitprint( pagemem[2], "\tdisconnect #0 ( page <2h> byte <0h>)"); bitprint( pagemem[3], "\tdisconnect #1 ( page <2h> byte <1h>)"); bitprint( pagemem[4], "\tdisconnect #2 ( page <2h> byte <2h>)"); bitprint( pagemem[5], "\tdisconnect #3 ( page <2h> byte <3h>)"); bitprint( pagemem[6], "\tend of charge #0( page <2h> byte <4h>)"); bitprint( pagemem[7], "\tend of charge #1( page <2h> byte <5h>)"); bitprint( pagemem[8], "\tend of charge #2( page <2h> byte <6h>)"); bitprint( pagemem[9], "\tend of charge #3( page <2h> byte <7h>)"); pageno = 0x07; ds2438mem_rd( 0, serialnum, pagemem, pageno, device); syslog(LOG_INFO,"page# <%xh>: %s\n", pageno, ppagemem(pagemem)); bitprint( pagemem[2], "\tuser byte 0 ( page <7h> byte <0h>)"); bitprint( pagemem[3], "\tuser byte 1 ( page <7h> byte <1h>)"); bitprint( pagemem[4], "\tuser byte 2 ( page <7h> byte <2h>)"); bitprint( pagemem[5], "\tuser byte 3 ( page <7h> byte <3h>)"); bitprint( pagemem[6], "\tcca lsb ( page <7h> byte <4h>)"); bitprint( pagemem[7], "\tcca msb ( page <7h> byte <5h>)"); bitprint( pagemem[8], "\tdca lsb ( page <7h> byte <6h>)"); bitprint( pagemem[9], "\tdca msb ( page <7h> byte <7h>)"); } return(0); }