diff --git a/README.md b/README.md index a289f7d..f510dcd 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,15 @@ -# MightyPork's AVR Library +# "porklib", MightyPork's Arduino Library -I program my Arduinos in plain C, compile it with `avr-gcc` and flash with `avrdude` (all on Linux). +This library aims to make development for AVR in C easy and fun. -Whenever I learn how to do something, I make a library file and put it here. +Modules of the library provide facilities for pin numbering and aliasing, +bitwise operations and accessing internal and external peripherals of the AVR. -The code is tested and optimized for **ATmega328P**, which is used in most Arduinos. I use "Pro Mini" and "Nano". +It takes some inspiration from Arduino, but is written in pure C, which makes it faster +and smaller. -# How to use +Pull requests to add new modules are welcome, please go ahead! -Link the `lib/` folder to your project, and make sure you add all lib `.c` files to your `Makefile`, so it builds are the needed code. - -Some library files don't have `.c`, but many do. - -## Useful things - -- To easily alias I/O pins, use `lib/pins.h`. -- For Arduino pins, there are presets in `lib/arduino_pins.h` -- Binary/byte manipulation utilities are in `lib/calc.h` -- `lib/meta.h` contains some generally useful things that didn't fit elsewhere - -Each header file contains a comment block with explanation, which will help you understand them. +## License +The library is provided under MIT license, see the LICENSE file for more info. diff --git a/lib/onewire.c b/lib/onewire.c new file mode 100644 index 0000000..ac9e58e --- /dev/null +++ b/lib/onewire.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include + +#include "iopins.h" +#include "onewire.h" + + +/** Perform bus reset. Returns true if any device is connected */ +bool ow_reset(const uint8_t pin) +{ + as_output_n(pin); + pin_low_n(pin); + _delay_us(480); + + as_input_pu_n(pin); + _delay_us(70); + + const bool a = get_pin_n(pin); + + _delay_us(410); + + return a; +} + + +/** Send a single bit */ +void ow_tx_bit(const uint8_t pin, const bool bit) +{ + as_output_n(pin); + pin_low_n(pin); + + if (bit) { + _delay_us(6); + as_input_pu_n(pin); + _delay_us(64); + } else { + _delay_us(60); + as_input_pu_n(pin); + _delay_us(10); + } +} + + +/** Send a single byte */ +void ow_send(const uint8_t pin, const uint8_t byte) +{ + for (uint8_t i = 0; i < 8; i++) + { + ow_tx_bit(pin, (byte >> i) & 0x01); + } +} + + +/** Read a single bit */ +bool ow_rx_bit(const uint8_t pin) +{ + as_output_n(pin); + pin_low_n(pin); + _delay_us(6); + + as_input_pu_n(pin); + _delay_us(9); + + const bool a = get_pin_n(pin); + + _delay_us(55); + + return a; +} + + +/** Read a single byte */ +uint8_t ow_read(const uint8_t pin) +{ + uint8_t byte = 0; + + for (uint8_t i = 0; i < 8; i++) + { + byte = (byte >> 1) | (ow_rx_bit(pin) << 7); + } + + return byte; +} + + +/** Wait until the device is ready. Returns false on timeout */ +bool ow_wait_ready(const uint8_t pin) +{ + uint16_t timeout = 700; + as_input_pu_n(pin); + + while (--timeout > 0) + { + if (is_high_n(pin)) return true; + _delay_ms(1); + } + + return false; +} + + +/** Read bytes into an array */ +void ow_read_arr(const uint8_t pin, uint8_t* array, const uint8_t count) +{ + for (uint8_t i = 0; i < count; i++) + { + array[i] = ow_read(pin); + } +} + + + +// ---------- CRC utils ---------- + +/* + Dallas 1-wire CRC routines for Arduino with examples of usage. + The 16-bit routine is new. + The 8-bit routine is from http://github.com/paeaetech/paeae/tree/master/Libraries/ds2482/ + + Copyright (C) 2010 Kairama Inc + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +// Dallas 1-wire 16-bit CRC calculation. Developed from Maxim Application Note 27. + +/** Compute a CRC16 checksum */ +uint16_t crc16( uint8_t *data, uint8_t len) +{ + uint16_t crc = 0; + + for (uint8_t i = 0; i < len; i++) + { + uint8_t inbyte = data[i]; + for (uint8_t j = 0; j < 8; j++) + { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc = crc >> 1; + if (mix) + crc = crc ^ 0xA001; + + inbyte = inbyte >> 1; + } + } + return crc; +} + + +// The 1-Wire CRC scheme is described in Maxim Application Note 27: +// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" + +/** Compute a CRC8 checksum */ +uint8_t crc8(uint8_t *addr, uint8_t len) +{ + uint8_t crc = 0; + + for (uint8_t i = 0; i < len; i++) + { + uint8_t inbyte = addr[i]; + for (uint8_t j = 0; j < 8; j++) + { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) + crc ^= 0x8C; + + inbyte >>= 1; + } + } + + return crc; +} + + +// --- utils for DS1820 --- + +/** Read temperature in 0.0625°C, or TEMP_ERROR on error */ +int16_t ds1820_read_temp(uint8_t pin) +{ + ow_send(pin, READ_SCRATCHPAD); + uint8_t bytes[9]; + ow_read_arr(pin, bytes, 9); + + uint8_t crc = crc8(bytes, 8); + if (crc != bytes[8]) { + return TEMP_ERROR; + } else { + int16_t a = ((bytes[1] << 8) | bytes[0]) >> 1; + a = a << 4; + a += (16 - bytes[6]) & 0x0F; + a -= 0x04; + + return a; + } +} + +/** Read temperature in 0.1°C, or TEMP_ERROR on error */ +int16_t ds1820_read_temp_c(uint8_t pin) +{ + int32_t temp = ds1820_read_temp(pin); + + if (temp == TEMP_ERROR) + return TEMP_ERROR; + + temp *= 625; + uint16_t rem = temp % 1000; + temp /= 1000; + if (rem >= 500) temp++; + + return (int16_t) temp; +} + diff --git a/lib/onewire.h b/lib/onewire.h index f0fd2c0..bf0c252 100644 --- a/lib/onewire.h +++ b/lib/onewire.h @@ -1,194 +1,46 @@ #pragma once -#include -#include #include #include -#include "lib/iopins.h" -#include "lib/uart.h" -#include "lib/stream.h" - #define SKIP_ROM 0xCC #define CONVERT_T 0x44 #define READ_SCRATCHPAD 0xBE - /** Perform bus reset. Returns true if any device is connected */ -bool ow_reset(const uint8_t pin) -{ - as_output_n(pin); - pin_low_n(pin); - _delay_us(480); - - as_input_pu_n(pin); - _delay_us(70); - - const bool a = get_pin_n(pin); - - _delay_us(410); - - return a; -} +bool ow_reset(const uint8_t pin); /** Send a single bit */ -void ow_tx_bit(const uint8_t pin, const bool bit) -{ - as_output_n(pin); - pin_low_n(pin); - - if (bit) { - _delay_us(6); - as_input_pu_n(pin); - _delay_us(64); - } else { - _delay_us(60); - as_input_pu_n(pin); - _delay_us(10); - } -} +void ow_tx_bit(const uint8_t pin, const bool bit); /** Send a single byte */ -void ow_send(const uint8_t pin, const uint8_t byte) -{ - for (uint8_t i = 0; i < 8; i++) - { - ow_tx_bit(pin, (byte >> i) & 0x01); - } -} +void ow_send(const uint8_t pin, const uint8_t byte); /** Read a single bit */ -bool ow_rx_bit(const uint8_t pin) -{ - as_output_n(pin); - pin_low_n(pin); - _delay_us(6); - - as_input_pu_n(pin); - _delay_us(9); - - const bool a = get_pin_n(pin); - - _delay_us(55); - - return a; -} +bool ow_rx_bit(const uint8_t pin); /** Read a single byte */ -uint8_t ow_read(const uint8_t pin) -{ - uint8_t byte = 0; - - for (uint8_t i = 0; i < 8; i++) - { - byte = (byte >> 1) | (ow_rx_bit(pin) << 7); - } - - return byte; -} +uint8_t ow_read(const uint8_t pin); /** Wait until the device is ready. Returns false on timeout */ -bool ow_wait_ready(const uint8_t pin) -{ - uint16_t timeout = 700; - as_input_pu_n(pin); - - while (--timeout > 0) - { - if (is_high_n(pin)) return true; - _delay_ms(1); - } - - return false; -} +bool ow_wait_ready(const uint8_t pin); /** Read bytes into an array */ -void ow_read_arr(const uint8_t pin, uint8_t* array, const uint8_t count) -{ - for (uint8_t i = 0; i < count; i++) - { - array[i] = ow_read(pin); - } -} - +void ow_read_arr(const uint8_t pin, uint8_t* array, const uint8_t count); -// ---------- CRC utils ---------- - -/* - Dallas 1-wire CRC routines for Arduino with examples of usage. - The 16-bit routine is new. - The 8-bit routine is from http://github.com/paeaetech/paeae/tree/master/Libraries/ds2482/ - - Copyright (C) 2010 Kairama Inc - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ -// Dallas 1-wire 16-bit CRC calculation. Developed from Maxim Application Note 27. - /** Compute a CRC16 checksum */ -uint16_t crc16( uint8_t *data, uint8_t len) -{ - uint16_t crc = 0; - - for (uint8_t i = 0; i < len; i++) - { - uint8_t inbyte = data[i]; - for (uint8_t j = 0; j < 8; j++) - { - uint8_t mix = (crc ^ inbyte) & 0x01; - crc = crc >> 1; - if (mix) - crc = crc ^ 0xA001; - - inbyte = inbyte >> 1; - } - } - return crc; -} - - -// The 1-Wire CRC scheme is described in Maxim Application Note 27: -// "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products" - -/** Compute a CRC8 checksum */ -uint8_t crc8(uint8_t *addr, uint8_t len) -{ - uint8_t crc = 0; - - for (uint8_t i = 0; i < len; i++) - { - uint8_t inbyte = addr[i]; - for (uint8_t j = 0; j < 8; j++) - { - uint8_t mix = (crc ^ inbyte) & 0x01; - crc >>= 1; - if (mix) - crc ^= 0x8C; +uint16_t crc16( uint8_t *data, uint8_t len); - inbyte >>= 1; - } - } - return crc; -} +/** Compute a CRC8 checksum */ +uint8_t crc8(uint8_t *addr, uint8_t len); // --- utils for DS1820 --- @@ -196,40 +48,9 @@ uint8_t crc8(uint8_t *addr, uint8_t len) #define TEMP_ERROR -32768 /** Read temperature in 0.0625°C, or TEMP_ERROR on error */ -int16_t ds1820_read_temp(uint8_t pin) -{ - ow_send(pin, READ_SCRATCHPAD); - uint8_t bytes[9]; - ow_read_arr(pin, bytes, 9); - - put_nl(uart); - - uint8_t crc = crc8(bytes, 8); - if (crc != bytes[8]) { - return TEMP_ERROR; - } else { - int16_t a = ((bytes[1] << 8) | bytes[0]) >> 1; - a = a << 4; - a += (16 - bytes[6]) & 0x0F; - a -= 0x04; - - return a; - } -} +int16_t ds1820_read_temp(uint8_t pin); -/** Read temperature in 0.1°C, or TEMP_ERROR on error */ -int16_t ds1820_read_temp_c(uint8_t pin) -{ - int32_t temp = ds1820_read_temp(pin); - if (temp == TEMP_ERROR) - return TEMP_ERROR; - - temp *= 625; - uint16_t rem = temp % 1000; - temp /= 1000; - if (rem >= 500) temp++; - - return (int16_t) temp; -} +/** Read temperature in 0.1°C, or TEMP_ERROR on error */ +int16_t ds1820_read_temp_c(uint8_t pin);