parent
0906626513
commit
a18e9d38a1
@ -0,0 +1,5 @@ |
||||
#pragma once |
||||
|
||||
#define loop(varname, count) for(uint8_t varname = 0; varname < (count); varname++) |
||||
#define repeat(count) for(uint8_t _repeat_i = 0; _repeat_i < (count); _repeat_i++) |
||||
#define until(cond) while(!(cond)) |
@ -1,6 +1,8 @@ |
||||
MightyPorsk's AVR utils library |
||||
=============================== |
||||
AVR utils library |
||||
================= |
||||
|
||||
This is my ever-evolving library. When I'm done with a project, I copy the current library to the project, so it doesn't break when I do further improvements. |
||||
This is my ever-evolving library (not only) for AVR programming. |
||||
|
||||
When I'm done with a project, I copy the current library to the project, so it doesn't break when I do further improvements. |
||||
|
||||
Each library file contains a large comment block explaining it's function. |
||||
|
@ -1,46 +1,89 @@ |
||||
#pragma once |
||||
|
||||
/**
|
||||
General purpose calculation and bit manipulation utilities. |
||||
Bit and byte manipulation utilities |
||||
*/ |
||||
|
||||
// if max, go to zero. Else increment.
|
||||
#define inc_wrap(var, min, max) do { if ((var) >= (max)) { (var)=min; } else { (var)++; } } while(0) |
||||
|
||||
// If zero, go to max. Else decrement,
|
||||
#define dec_wrap(var, min, max) do { if ((var) > min) { (var)--; } else { (var)=(max); } } while(0) |
||||
// --- Increment in range ---
|
||||
// when overflown, wraps within range. Lower bound < upper bound.
|
||||
// ..., upper bound excluded
|
||||
#define inc_wrap(var, min, max) do { if ((var) >= (max - 1)) { (var) = (min); } else { (var)++; } } while(0) |
||||
// ..., upper bound included
|
||||
#define inc_wrapi(var, min, max) inc_wrap((var), (min), (max) + 1) |
||||
|
||||
// === general bit manipulation with register ===
|
||||
|
||||
// --- Decrement in range ---
|
||||
// when underflown, wraps within range. Lower bound < upper bound.
|
||||
// ..., upper bound excluded
|
||||
#define dec_wrap(var, min, max) do { if ((var) <= (min)) { (var) = (max) - 1; } else { (var)--; } } while(0) |
||||
// ..., upper bound included
|
||||
#define dec_wrapi(var, min, max) dec_wrap((var), (min), (max) + 1) |
||||
|
||||
|
||||
// --- Bit manipulation --
|
||||
|
||||
// Set bit
|
||||
#define sbi(reg, bit) do { (reg) |= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// Clear bit
|
||||
#define cbi(reg, bit) do { (reg) &= ~(1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
#define read_bit(reg, bit) ((((uint8_t)(reg)) >> (uint8_t)(bit)) & 0x1) |
||||
// Get n-th bit
|
||||
#define read_bit(reg, bit) (((reg) >> (uint8_t)(bit)) & 0x1) |
||||
#define get_bit(reg, bit) read_bit(reg, bit) |
||||
|
||||
// Test n-th bit (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||
#define bit_is_high(reg, bit) read_bit(reg, bit) |
||||
#define bit_is_low(reg, bit) !read_bit(reg, bit) |
||||
// Can't use bit_is_set, as it's redefined in sfr_def.h
|
||||
#define bit_is_low(reg, bit) (!read_bit(reg, bit)) |
||||
|
||||
// Write value to n-th bit
|
||||
#define write_bit(reg, bit, value) do { (reg) = ((reg) & ~(1 << (uint8_t)(bit))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) |
||||
#define set_bit(reg, bit, value) write_bit(reg, bit, value) |
||||
|
||||
// Invert n-th bit
|
||||
#define toggle_bit(reg, bit) do { (reg) ^= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// general pin manipulation - with pointer to register
|
||||
|
||||
// --- Bit manipulation with pointer to variable ---
|
||||
|
||||
// Set n-th bit in pointee
|
||||
#define sbi_p(reg_p, bit) do { (*(reg_p)) |= (1 << (uint8_t)(bit)); } while(0) |
||||
// Clear n-th bit in pointee
|
||||
#define cbi_p(reg_p, bit) do { (*(reg_p)) &= ~(1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
// Get n-th bit in pointee
|
||||
#define read_bit_p(reg_p, bit) ((*(reg_p) >> (uint8_t)(bit)) & 0x1) |
||||
#define get_bit_p(reg_p, bit) read_bit_p(reg_p, bit) |
||||
|
||||
// Test n-th bit in pointee (Can't use bit_is_set, as it's redefined in sfr_def.h)
|
||||
#define bit_is_high_p(reg_p, bit) read_bit_p(reg_p, bit) |
||||
#define bit_is_low_p(reg_p, bit) (!read_bit_p(reg_p, bit)) |
||||
|
||||
// Write value to a bit in pointee
|
||||
#define write_bit_p(reg_p, bit, value) do { *(reg_p) = (*(reg_p) & ~(1 << ((uint8_t)(bit) & 0x1))) | (((uint8_t)(value) & 0x1) << (uint8_t)(bit)); } while(0) |
||||
#define set_bit_p(reg, bit, value) write_bit_p(reg_p, bit, value) |
||||
#define set_bit_p(reg_p, bit, value) write_bit_p(reg_p, bit, value) |
||||
#define toggle_bit_p(reg_p, bit) do { *(reg_p) ^= (1 << (uint8_t)(bit)); } while(0) |
||||
|
||||
|
||||
// --- Nibble manipulation ---
|
||||
|
||||
// Replace nibble in a byte
|
||||
#define write_low_nibble(reg, value) do { (reg) = ((reg) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) |
||||
#define write_high_nibble(reg, value) do { (reg) = ((reg) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) |
||||
|
||||
#define write_low_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0xF0) | ((uint8_t)(value) & 0xF); } while(0) |
||||
#define write_high_nibble_p(reg_p, value) do { *(reg_p) = (*(reg_p) & 0x0F) | (((uint8_t)(value) & 0xF) << 4); } while(0) |
||||
|
||||
|
||||
// --- Range tests ---
|
||||
|
||||
// Check if value is in range A..B or B..A
|
||||
#define in_range(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) < (low) || (x) > (high))) |
||||
// Test if X is within low..high, regardless of bounds order
|
||||
#define in_range(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (high) && (x) < (low)))) |
||||
// ..., include greater bound
|
||||
#define in_rangei(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (high) && (x) <= (low)))) |
||||
|
||||
// Check if value is in range A..B. If B < A, matches all outside B..A
|
||||
#define in_range_wrap(x, low, high) (((low) < (high)) && ((x) > (low) && (x) < (high))) || (((low) > (high)) && ((x) > (low) || (x) < (high))) |
||||
// Test if X in low..high, wrap around ends if needed.
|
||||
#define in_range_wrap(x, low, high) ((((low) < (high)) && ((x) >= (low) && (x) < (high))) || (((low) > (high)) && ((x) >= (low) || (x) < (high)))) |
||||
// ..., include upper bound
|
||||
#define in_range_wrapi(x, low, high) ((((low) <= (high)) && ((x) >= (low) && (x) <= (high))) || (((low) > (high)) && ((x) >= (low) || (x) <= (high)))) |
||||
|
@ -0,0 +1,25 @@ |
||||
#pragma once |
||||
|
||||
/**
|
||||
Custom loops |
||||
*/ |
||||
|
||||
// Repeat code n times (uint8_t counter)
|
||||
#define repeat(count) repeat_aux(count, _repeat_##__COUNTER__) |
||||
#define repeat_aux(count, cntvar) for (uint8_t cntvar = 0; cntvar < (count); cntvar++) |
||||
|
||||
// Repeat code n times (uint16_t counter)
|
||||
#define repeatx(count) repeatx_aux(count, _repeatx_##__COUNTER__) |
||||
#define repeatx_aux(count, cntvar) for (uint16_t cntvar = 0; cntvar < (count); cntvar++) |
||||
|
||||
// Repeat with custom counter name (uint8_t)
|
||||
#define loop(var, count) repeat_aux(count, var) |
||||
// ..., uint16_t
|
||||
#define loopx(var, count) repeatx_aux(count, var) |
||||
|
||||
// Do until condition is met
|
||||
#define until(what) while(!(what)) |
||||
|
||||
// Because why the hell not
|
||||
#define whilst(what) while((what)) |
||||
|
@ -0,0 +1 @@ |
||||
/home/ondra/devel/avr/avr-projects/devel/lib |
@ -0,0 +1,8 @@ |
||||
#!/bin/bash |
||||
|
||||
dir=/tmp/avrclib_unittest |
||||
|
||||
[ -d "$dir" ] || mkdir "$dir" |
||||
|
||||
gcc test_calc.c -o "$dir/test_calc" && "$dir/test_calc" |
||||
|
@ -0,0 +1,356 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <stdint.h> |
||||
#include <assert.h> |
||||
|
||||
#include "lib/calc.h" |
||||
|
||||
void main() |
||||
{ |
||||
printf("== calc.h ==\n"); |
||||
|
||||
int i; |
||||
int a; |
||||
|
||||
printf("fn: inc_wrap()\n"); |
||||
{ |
||||
// normal operation
|
||||
a = 1; |
||||
inc_wrap(a, 0, 15); |
||||
assert(a == 2); |
||||
|
||||
// overflow
|
||||
a = 14; |
||||
inc_wrap(a, 0, 15); |
||||
assert(a == 0); |
||||
} |
||||
|
||||
|
||||
printf("fn: inc_wrapi()\n"); |
||||
{ |
||||
// normal operation
|
||||
a = 1; |
||||
inc_wrapi(a, 0, 15); |
||||
assert(a == 2); |
||||
|
||||
// not overflow yet
|
||||
a = 14; |
||||
inc_wrapi(a, 0, 15); |
||||
assert(a == 15); |
||||
|
||||
// overflow
|
||||
a = 15; |
||||
inc_wrap(a, 0, 15); |
||||
assert(a == 0); |
||||
} |
||||
|
||||
|
||||
printf("fn: dec_wrap()\n"); |
||||
{ |
||||
// normal operation
|
||||
a = 5; |
||||
dec_wrap(a, 0, 15); |
||||
assert(a == 4); |
||||
|
||||
// underflow
|
||||
a = 0; |
||||
dec_wrap(a, 0, 15); |
||||
assert(a == 14); |
||||
} |
||||
|
||||
|
||||
printf("fn: dec_wrapi()\n"); |
||||
{ |
||||
// normal operation
|
||||
a = 5; |
||||
dec_wrapi(a, 0, 15); |
||||
assert(a == 4); |
||||
|
||||
// underflow
|
||||
a = 0; |
||||
dec_wrapi(a, 0, 15); |
||||
assert(a == 15); |
||||
} |
||||
|
||||
|
||||
printf("fn: sbi()\n"); |
||||
{ |
||||
a = 0; |
||||
sbi(a, 2); |
||||
assert(a == 0b100); |
||||
} |
||||
|
||||
|
||||
printf("fn: cbi()\n"); |
||||
{ |
||||
a = 0b11111; |
||||
cbi(a, 2); |
||||
assert(a == 0b11011); |
||||
} |
||||
|
||||
|
||||
printf("fn: read_bit()\n"); |
||||
{ |
||||
a = 0b101; |
||||
assert(read_bit(a, 0) == 1); |
||||
assert(read_bit(a, 1) == 0); |
||||
assert(read_bit(a, 2) == 1); |
||||
} |
||||
|
||||
|
||||
printf("fn: bit_is_high()\n"); |
||||
{ |
||||
a = 0b101; |
||||
assert(bit_is_high(a, 0) == 1); |
||||
assert(bit_is_high(a, 1) == 0); |
||||
assert(bit_is_high(a, 2) == 1); |
||||
} |
||||
|
||||
|
||||
printf("fn: bit_is_low()\n"); |
||||
{ |
||||
a = 0b101; |
||||
assert(bit_is_low(a, 0) == 0); |
||||
assert(bit_is_low(a, 1) == 1); |
||||
assert(bit_is_low(a, 2) == 0); |
||||
} |
||||
|
||||
|
||||
printf("fn: toggle_bit()\n"); |
||||
{ |
||||
a = 0; |
||||
toggle_bit(a, 2); |
||||
assert(a == 0b00100); |
||||
toggle_bit(a, 4); |
||||
assert(a == 0b10100); |
||||
toggle_bit(a, 4); |
||||
assert(a == 0b00100); |
||||
} |
||||
|
||||
// pointer variants
|
||||
|
||||
int* b = &a; |
||||
|
||||
printf("fn: sbi_p()\n"); |
||||
{ |
||||
*b = 0; |
||||
sbi_p(b, 2); |
||||
assert(*b == 0b100); |
||||
} |
||||
|
||||
|
||||
printf("fn: cbi_p()\n"); |
||||
{ |
||||
*b = 0b11111; |
||||
cbi_p(b, 2); |
||||
assert(*b == 0b11011); |
||||
} |
||||
|
||||
|
||||
printf("fn: read_bit_p()\n"); |
||||
{ |
||||
*b = 0b101; |
||||
assert(read_bit_p(b, 0) == 1); |
||||
assert(read_bit_p(b, 1) == 0); |
||||
assert(read_bit_p(b, 2) == 1); |
||||
} |
||||
|
||||
|
||||
printf("fn: bit_is_high_p()\n"); |
||||
{ |
||||
*b = 0b101; |
||||
assert(bit_is_high_p(b, 0) == 1); |
||||
assert(bit_is_high_p(b, 1) == 0); |
||||
assert(bit_is_high_p(b, 2) == 1); |
||||
} |
||||
|
||||
|
||||
printf("fn: bit_is_low_p()\n"); |
||||
{ |
||||
*b = 0b101; |
||||
assert(bit_is_low_p(b, 0) == 0); |
||||
assert(bit_is_low_p(b, 1) == 1); |
||||
assert(bit_is_low_p(b, 2) == 0); |
||||
} |
||||
|
||||
|
||||
printf("fn: toggle_bit_p()\n"); |
||||
{ |
||||
*b = 0; |
||||
toggle_bit_p(b, 2); |
||||
assert(*b == 0b00100); |
||||
toggle_bit_p(b, 4); |
||||
assert(*b == 0b10100); |
||||
toggle_bit_p(b, 4); |
||||
assert(*b == 0b00100); |
||||
} |
||||
|
||||
|
||||
// -- nibbles --
|
||||
|
||||
printf("fn: write_low_nibble()\n"); |
||||
{ |
||||
a = 0xAC; |
||||
write_low_nibble(a, 0x5); // shifted 4 left
|
||||
assert(a == 0xA5); |
||||
|
||||
a = 0xAB; |
||||
write_low_nibble(a, 0x65); // shifted 4 left, extra ignored
|
||||
assert(a == 0xA5); |
||||
} |
||||
|
||||
|
||||
printf("fn: write_high_nibble()\n"); |
||||
{ |
||||
a = 0xAC; |
||||
write_high_nibble(a, 0x5); // shifted 4 left
|
||||
assert(a == 0x5C); |
||||
|
||||
a = 0xAB; |
||||
write_high_nibble(a, 0x65); // shifted 4 left, extra ignored
|
||||
assert(a == 0x5B); |
||||
} |
||||
|
||||
|
||||
printf("fn: write_low_nibble_p()\n"); |
||||
{ |
||||
*b = 0xAC; |
||||
write_low_nibble_p(b, 0x5); // shifted 4 left
|
||||
assert(*b == 0xA5); |
||||
|
||||
*b = 0xAB; |
||||
write_low_nibble_p(b, 0x65); // shifted 4 left, extra ignored
|
||||
assert(*b == 0xA5); |
||||
} |
||||
|
||||
|
||||
printf("fn: write_high_nibble_p()\n"); |
||||
{ |
||||
*b = 0xAC; |
||||
write_high_nibble_p(b, 0x5); // shifted 4 left
|
||||
assert(*b == 0x5C); |
||||
|
||||
*b = 0xAB; |
||||
write_high_nibble_p(b, 0x65); // shifted 4 left, extra ignored
|
||||
assert(*b == 0x5B); |
||||
} |
||||
|
||||
|
||||
printf("fn: in_range()\n"); |
||||
{ |
||||
// regular
|
||||
assert(in_range(10, 10, 15)); |
||||
assert(in_range(14, 10, 15)); |
||||
assert(in_range(13, 10, 15)); |
||||
// under
|
||||
assert(!in_range(9, 10, 15)); |
||||
assert(!in_range(0, 10, 15)); |
||||
// above
|
||||
assert(!in_range(15, 10, 15)); |
||||
assert(!in_range(99, 10, 15)); |
||||
|
||||
// swapped bounds
|
||||
|
||||
// regular
|
||||
assert(in_range(10, 15, 10)); |
||||
assert(in_range(14, 15, 10)); |
||||
assert(in_range(13, 15, 10)); |
||||
// under
|
||||
assert(!in_range(9, 15, 10)); |
||||
assert(!in_range(0, 15, 10)); |
||||
// above
|
||||
assert(!in_range(15, 15, 10)); |
||||
assert(!in_range(99, 15, 10)); |
||||
} |
||||
|
||||
|
||||
printf("fn: in_rangei()\n"); |
||||
{ |
||||
// regular
|
||||
assert(in_rangei(10, 10, 15)); |
||||
assert(in_rangei(15, 10, 15)); |
||||
assert(in_rangei(13, 10, 15)); |
||||
// under
|
||||
assert(!in_rangei(9, 10, 15)); |
||||
assert(!in_rangei(0, 10, 15)); |
||||
// above
|
||||
assert(!in_rangei(16, 10, 15)); |
||||
assert(!in_rangei(99, 10, 15)); |
||||
|
||||
// -- swapped bounds --
|
||||
|
||||
// regular
|
||||
assert(in_rangei(10, 15, 10)); |
||||
assert(in_rangei(15, 15, 10)); |
||||
assert(in_rangei(13, 15, 10)); |
||||
// under
|
||||
assert(!in_rangei(9, 15, 10)); |
||||
assert(!in_rangei(0, 15, 10)); |
||||
// above
|
||||
assert(!in_rangei(16, 15, 10)); |
||||
assert(!in_rangei(99, 15, 10)); |
||||
} |
||||
|
||||
|
||||
printf("fn: in_range_wrap()\n"); |
||||
{ |
||||
// as regular range
|
||||
|
||||
// regular
|
||||
assert(in_range_wrap(10, 10, 15)); |
||||
assert(in_range_wrap(14, 10, 15)); |
||||
assert(in_range_wrap(13, 10, 15)); |
||||
// under
|
||||
assert(!in_range_wrap(9, 10, 15)); |
||||
assert(!in_range_wrap(0, 10, 15)); |
||||
// above
|
||||
assert(!in_range_wrap(15, 10, 15)); |
||||
assert(!in_range_wrap(16, 10, 15)); |
||||
assert(!in_range_wrap(99, 10, 15)); |
||||
|
||||
// with wrap (>= 15, < 10)
|
||||
|
||||
// regular
|
||||
assert(!in_range_wrap(10, 15, 10)); |
||||
assert(!in_range_wrap(14, 15, 10)); |
||||
assert(!in_range_wrap(13, 15, 10)); |
||||
// under
|
||||
assert(in_range_wrap(9, 15, 10)); |
||||
assert(in_range_wrap(0, 15, 10)); |
||||
// above
|
||||
assert(in_range_wrap(16, 15, 10)); |
||||
assert(in_range_wrap(99, 15, 10)); |
||||
} |
||||
|
||||
|
||||
printf("fn: in_range_wrapi()\n"); |
||||
{ |
||||
// as regular range
|
||||
|
||||
// regular
|
||||
assert(in_range_wrapi(10, 10, 15)); |
||||
assert(in_range_wrapi(15, 10, 15)); |
||||
assert(in_range_wrapi(13, 10, 15)); |
||||
// under
|
||||
assert(!in_range_wrapi(9, 10, 15)); |
||||
assert(!in_range_wrapi(0, 10, 15)); |
||||
// above
|
||||
assert(!in_range_wrapi(16, 10, 15)); |
||||
assert(!in_range_wrapi(99, 10, 15)); |
||||
|
||||
// with wrap (>= 15, < 10)
|
||||
|
||||
// regular
|
||||
assert(in_range_wrapi(10, 15, 10)); |
||||
assert(!in_range_wrapi(14, 15, 10)); |
||||
assert(!in_range_wrapi(13, 15, 10)); |
||||
// under
|
||||
assert(in_range_wrapi(10, 15, 10)); |
||||
assert(in_range_wrapi(0, 15, 10)); |
||||
// above
|
||||
assert(in_range_wrapi(16, 15, 10)); |
||||
assert(in_range_wrapi(99, 15, 10)); |
||||
} |
||||
|
||||
printf("== PASSED ==\n"); |
||||
} |
@ -0,0 +1,26 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <stdint.h> |
||||
#include <assert.h> |
||||
|
||||
#include "lib/loops.h" |
||||
|
||||
void main() |
||||
{ |
||||
repeat(5) printf("Five times!\n"); |
||||
repeatx(500) printf("."); |
||||
printf("\n"); |
||||
int i = 0; |
||||
until (i == 100) i++; |
||||
|
||||
whilst (i > 0) { |
||||
i--; |
||||
printf("i = %d\n", i); |
||||
} |
||||
|
||||
printf("\n"); |
||||
|
||||
loop(moo, 7) { |
||||
printf("moo = %d\n", moo); |
||||
} |
||||
} |
Loading…
Reference in new issue