parent
a31091d88c
commit
c51eacf06c
@ -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. |
||||
|
@ -0,0 +1,224 @@ |
||||
#include <avr/io.h> |
||||
#include <util/delay.h> |
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
|
||||
#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 <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
// 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; |
||||
} |
||||
|
Loading…
Reference in new issue