1
0

1wire.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * 1wire.c
  3. * k4be 2019
  4. * License: BSD
  5. */
  6. #include "1wire.h"
  7. #include <util/delay.h>
  8. #include <avr/interrupt.h>
  9. #include <avr/wdt.h>
  10. #ifdef SEARCHROM
  11. #ifdef INITST
  12. unsigned char initst;
  13. #endif
  14. #endif
  15. unsigned char _1WireInit(void){
  16. unsigned char out=0;
  17. _1WPORT &= ~_1WL;
  18. _1WLOW();
  19. _delay_us(480);
  20. cli();
  21. _1WHIGH();
  22. _delay_us(70);
  23. if(!_1WISHIGH()) out++;
  24. sei();
  25. _delay_us(410);
  26. if(!_1WISHIGH()) out++;
  27. return out; // 0 - short; 1 - ok; 2 - open/no slaves
  28. }
  29. void _1WireWriteByte(unsigned char data){
  30. register unsigned char i;
  31. for(i=0;i<8;i++){
  32. _1WireWriteSlot(data&1,0);
  33. data >>= 1;
  34. }
  35. }
  36. void _1WireWriteBytePR(unsigned char data){ // enable strong pullup right after the command
  37. register unsigned char i;
  38. for(i=0;i<7;i++){
  39. _1WireWriteSlot(data&1,0);
  40. data >>= 1;
  41. }
  42. _1WireWriteSlot(data,1);
  43. _1WPORT |= _1WL;
  44. _1WDDR |= _1WL;
  45. sei();
  46. if(!_1WISHIGH()) _1WirePoweroff();
  47. }
  48. void _1WireWriteSlot(unsigned char x, unsigned char pr){
  49. cli();
  50. _1WLOW();
  51. if(x) _delay_us(6); else _delay_us(60);
  52. _1WHIGH();
  53. if(x) _delay_us(64); else _delay_us(10);
  54. if(!pr) sei();
  55. _delay_us(1);
  56. }
  57. unsigned char _1WireReadByte(void){
  58. register unsigned char i=1;
  59. unsigned char data=0;
  60. while(i){
  61. if(_1WireReadSlot()) data |= i;
  62. i <<= 1;
  63. }
  64. return data;
  65. }
  66. unsigned char _1WireReadSlot(void){
  67. unsigned char out=0;
  68. cli();
  69. _1WLOW();
  70. _delay_us(6);
  71. _1WHIGH();
  72. _delay_us(9);
  73. if(_1WISHIGH()) out=1;
  74. sei();
  75. _delay_us(55);
  76. return out;
  77. }
  78. /*
  79. void _1WirePoweroff(void){ // turn off power
  80. _1WDDR &= ~_1WL;
  81. _1WPORT &= ~_1WL;
  82. }*/
  83. #ifdef SEARCHROM
  84. unsigned char _1WireSearch(unsigned char rv, unsigned char *buf){
  85. unsigned char i,a,b,mask=1,lz=0;
  86. #ifdef INITST
  87. if((initst = _1WireInit())!=1) return 0xff;
  88. #else
  89. if(_1WireInit()!=1) return 0xff;
  90. #endif
  91. _1WireWriteByte(0xf0);
  92. for(i=1;i<65;i++){
  93. a = _1WireReadSlot();
  94. b = _1WireReadSlot();
  95. if(a && b) return 0xff;
  96. if(a==b) {
  97. if(i>rv) a = 0;
  98. else if(i==rv) a = 1;
  99. else a = !!((*buf)&mask);
  100. if(!a) lz=i;
  101. }
  102. _1WireWriteSlot(a,0);
  103. if(a) (*buf)|=mask; else (*buf)&=~mask;
  104. mask<<=1;
  105. if(!mask){
  106. mask = 1;
  107. buf++;
  108. }
  109. }
  110. rv=lz;
  111. return rv;
  112. }
  113. void _1WireSendRom(unsigned char *rom){
  114. register unsigned char i;
  115. for(i=0;i<8;i++){
  116. _1WireWriteByte(rom[i]);
  117. }
  118. }
  119. #endif