static void OneWireHigh() {
/* Set as output */
drv_gpio_output_en(GPIO_TEMP, true);
drv_gpio_input_en(GPIO_TEMP, false);
/* Set high */
drv_gpio_write(GPIO_TEMP, 1);
// BUSDIR = 0; // Set as output
// BUSOUT = 1; // Set high
}
static void OneWireRelease() {
/* Set as output */
drv_gpio_output_en(GPIO_TEMP, true);
drv_gpio_input_en(GPIO_TEMP, false);
/* Set low */
drv_gpio_write(GPIO_TEMP, 0);
// BUSDIR = 0; // Set as output
// BUSOUT = 0; // Set low
}
static u8 OneWireRead() {
drv_gpio_output_en(GPIO_TEMP, false);
drv_gpio_input_en(GPIO_TEMP, true);
return drv_gpio_read(GPIO_TEMP);
// return BUSIN;
}
static u8 OneWireReset() {
OneWireRelease();
WaitUs(480); // 480uS Delay
OneWireHigh();
WaitUs(70); // wait 70 uS before reading
u8 OW = OneWireRead(); // check for OneWire
WaitUs(410); // 410 uS delay
OneWireHigh(); // give bus back to OneWire
return OW;
}
static void OneWireWriteBit(u8 b){
if(b){
OneWireRelease();
WaitUs(6); // wait 6uS
OneWireHigh();
WaitUs(64); // wait 64uS
}
else{
OneWireRelease();
WaitUs(60); // wait 60uS
OneWireHigh();
WaitUs(10); // wait 10uS
}
}
static u8 OneWireReadBit(){
OneWireRelease();
WaitUs(6); // wait 6uS
OneWireHigh();
WaitUs(9); // wait 9uS
u8 out = OneWireRead();
WaitUs(55); // wait 55uS
return out;
}
static void OneWireWriteByte(u8 b){
for(u8 i = 0; i < 8; i++){
OneWireWriteBit(b & 0x01); // send LS bit first
b = b >> 1;
}
}
static u8 OneWireReadByte(void){
u8 out = 0;
for(u8 i = 0; i < 8; i++){ // read in LS bit first
out = out >> 1; // get out ready for next bit
if(OneWireReadBit() & 0x01) // if its a one
out = out | 0x80; // place a 1
}
return out;
}
u16 OneWireTemp() {
u8 reset = OneWireReset(); // Reset Pulse
printf("1 return WireReset: %d\r\n", reset);
OneWireWriteByte(0xCC); // Issue skip ROM command (CCh)
OneWireWriteByte(0x44); // Convert T command (44h)
while(!OneWireRead()); // DS will hold line low while making measurement
reset = OneWireReset(); // Start new command sequence
printf("2 return WireReset: %d\r\n", reset);
OneWireWriteByte(0xCC); // Issue skip ROM command
OneWireWriteByte(0xBE); // Read Scratchpad (BEh) - 15 bits
u8 LSB = OneWireReadByte();
u8 MSB = OneWireReadByte();
reset = OneWireReset(); // Stop Reading
printf("3 return WireReset: %d, LSB: %d, MSB: %d\r\n", reset, LSB, MSB);
u16 data = MSB;
u16 temperature = (data << 8) | LSB;
return temperature;
}
void OneWireInit() {
drv_gpio_up_down_resistor(GPIO_TEMP, PM_PIN_PULLUP_10K);
drv_gpio_irq_dis(GPIO_TEMP);
// Leave the pin low initially.
OneWireRelease();
}