Compare commits
3 Commits
5487c27c50
...
e6b952a2dc
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | e6b952a2dc | |
Savanni D'Gerinel | a383e956ae | |
Savanni D'Gerinel | 02716cc58b |
22
flake.nix
22
flake.nix
|
@ -30,19 +30,26 @@
|
|||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
avr = pkgs.pkgsCross.avr.buildPackages;
|
||||
in pkgs.stdenv.mkDerivation rec {
|
||||
name = "hello";
|
||||
name = "pwm";
|
||||
src = ./.;
|
||||
|
||||
buildInputs = [ pkgs.simavr ];
|
||||
|
||||
/*
|
||||
MCU = "attiny85";
|
||||
CHIP_SELECT = "AVR_ATtiny85";
|
||||
F_CPU = "8000000";
|
||||
CFLAGS = ''-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -DF_CPU=${F_CPU} -std=gnu99 -D__${CHIP_SELECT}__=1 -mmcu=${MCU}'';
|
||||
CFLAGS = ''-finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -DF_CPU=${F_CPU} -std=gnu99 -D__${CHIP_SELECT}__=1 -mmcu=${MCU}'';
|
||||
|
||||
buildPhase = ''
|
||||
set -x
|
||||
${avr.gcc}/bin/avr-gcc ${CFLAGS} -I${src}/base/include/ -o main.elf ${src}/prototype/src/main.c
|
||||
${avr.gcc}/bin/avr-gcc ${CFLAGS} -I${pkgs.simavr}/include/ -I${src}/base/include/ -o main.elf ${src}/ws2812/src/main.c
|
||||
$OBJCOPY -O ihex main.elf main.hex
|
||||
'';
|
||||
*/
|
||||
buildPhase = ''
|
||||
cd ws2812
|
||||
make
|
||||
'';
|
||||
installPhase = ''
|
||||
mkdir $out
|
||||
cp main.elf main.hex $out
|
||||
|
@ -55,12 +62,17 @@
|
|||
avr = with pkgs.pkgsCross.avr.buildPackages; [
|
||||
binutils
|
||||
gcc
|
||||
gdb
|
||||
avrdude
|
||||
simavr
|
||||
gtkwave
|
||||
];
|
||||
in
|
||||
pkgs.mkShell {
|
||||
name = "Wearables-shell";
|
||||
buildInputs = avr;
|
||||
buildInputs = avr ++ [ pkgs.gnumake ];
|
||||
GCC = pkgs.pkgsCross.avr.buildPackages.gcc;
|
||||
SIMAVR = pkgs.pkgsCross.avr.buildPackages.simavr;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
avrdude -c usbtiny -p attiny85 -U flash:w:$1:i; sleep 5; avrdude -c usbtiny -p attiny85 -D -U flash:w:$1:i
|
||||
avrdude -c usbtiny -p attiny85 -U flash:w:$1:i; sleep 5; avrdude -c usbtiny -p attiny85 -D -U flash:w:$1:i
|
||||
# avrdude -c usbtiny -p attiny85 -D -U flash:w:$1:i
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include <base.h>
|
||||
|
||||
#define FIVE_SECOND_TICK (8000000 * 10) / 256
|
||||
|
||||
volatile uint32_t iteration = 0;
|
||||
|
||||
void set_up_pwm(void);
|
||||
void start_pwm(void);
|
||||
|
||||
ISR(TIMER0_OVF_vect) {
|
||||
if (iteration >= FIVE_SECOND_TICK) {
|
||||
GTCCR |= _BV(TSM);
|
||||
/*
|
||||
PORTB &= ~(_BV(2));
|
||||
PORTB |= _BV(1);
|
||||
*/
|
||||
PORTB = _BV(1);
|
||||
} else {
|
||||
iteration += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void set_up_pwm(void) {
|
||||
// Stop the clock
|
||||
GTCCR = _BV(TSM) | _BV(PSR0);
|
||||
|
||||
TCCR0B = _BV(CS00);
|
||||
|
||||
// Set normal mode
|
||||
TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00);
|
||||
|
||||
// Enable the overflow interrupt
|
||||
TIMSK = _BV(TOIE0);
|
||||
}
|
||||
|
||||
void start_pwm(void) {
|
||||
GTCCR &= ~(_BV(TSM));
|
||||
}
|
||||
|
||||
int main (void) {
|
||||
int8_t step = 1;
|
||||
uint8_t target = 0;
|
||||
|
||||
set_up_pwm();
|
||||
OCR0A = 0;
|
||||
|
||||
PORTB = 0;
|
||||
DDRB = _BV(2) | _BV(1) | _BV(0);
|
||||
_delay_ms(50);
|
||||
PORTB |= _BV(2);
|
||||
|
||||
sei();
|
||||
start_pwm();
|
||||
|
||||
while (iteration != FIVE_SECOND_TICK) {
|
||||
OCR0A = target;
|
||||
target += step;
|
||||
|
||||
if (target == 255) {
|
||||
step = -1;
|
||||
} else if (target == 0) {
|
||||
step = 1;
|
||||
}
|
||||
|
||||
_delay_ms(10);
|
||||
}
|
||||
|
||||
while (1) {}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
MCU=attiny85
|
||||
CHIP_SELECT=AVR_ATtiny85
|
||||
F_CPU=8000000
|
||||
CFLAGS=-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -DF_CPU=${F_CPU} -std=gnu99 -D__${CHIP_SELECT}__=1 -mmcu=${MCU}
|
||||
|
||||
main:
|
||||
${GCC}/bin/avr-gcc ${CFLAGS} -I../base/include/ -E -o main.E src/main.c
|
||||
${GCC}/bin/avr-gcc ${CFLAGS} -I../base/include/ -S -o main.S src/main.c
|
||||
${GCC}/bin/avr-gcc ${CFLAGS} -I${SIMAVR}/include/ -I../base/include/ -o main.elf src/main.c
|
||||
${OBJCOPY} -O ihex main.elf main.hex
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
// I have reduced noops at the end of each of these to take into account that there are several additional clock ticks of setup, after. However, I'm not totally sure that I get things right, seeing that there are four possible sequences, and I'm not really accounting for the timing of all four of them.
|
||||
#define write_zero(port, bit) \
|
||||
__asm__ __volatile__ ( \
|
||||
"sbi %0, %1" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"cbi %0, %1" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
: /* no outputs */ \
|
||||
: "I" (_SFR_IO_ADDR(port)), \
|
||||
"I" (bit) \
|
||||
)
|
||||
|
||||
#define write_one(port, bit) \
|
||||
__asm__ __volatile__ ( \
|
||||
"sbi %0, %1" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"cbi %0, %1" "\n\t" \
|
||||
: /* no outputs */ \
|
||||
: "I" (_SFR_IO_ADDR(port)), \
|
||||
"I" (bit) \
|
||||
)
|
||||
|
||||
#define write_byte(port, bit, byte) \
|
||||
__asm__ __volatile__ ( \
|
||||
"ldi %z, 8" "\n\t" \ // count out eight bits
|
||||
"ld __tmp_reg__, %[byte]" "\n\t" \ // load the current byte into a temporary register
|
||||
"L_%=: " "lsl __tmp_reg__" "\n\t" \ // shift the temporary register left, saving the msb in SREG (1 cycle)
|
||||
"brbs I_%=" "\n\t" \ // if SREG is set, branch to I_%= (2 cycles if true, 1 cycle if false)
|
||||
write_zero(port, bit) \ // SREG was zero, so write a zero to the port
|
||||
"rjmp J_%=" "\n\t" \ // Jump to J_%=, the loop cleanup (2 cycles)
|
||||
"I_%=: " write_one(port, bit) \ // SREG was one, so write a one to the port
|
||||
"J_%=: " "dec %z" "\n\t" \ // Decrement the bits counter (1 cycle)
|
||||
"cpi %z, 0" "\n\t" \ // are there any bits left to send? (1 cycle)
|
||||
"brne L_%=" "\n\t" \ // there are, so go back to L_%= (2 cycles)
|
||||
: /* no outputs */ \
|
||||
: [byte] "I" (byte) \
|
||||
)
|
||||
|
|
@ -0,0 +1,171 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include "ws2812.h"
|
||||
#include "np_common.c"
|
||||
|
||||
#include <simavr/avr/avr_mcu_section.h>
|
||||
AVR_MCU(F_CPU, "attiny85");
|
||||
|
||||
const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = {
|
||||
{ AVR_MCU_VCD_SYMBOL("DDRB"), .what = (void*)&DDRB, },
|
||||
{ AVR_MCU_VCD_SYMBOL("PORTB"), .what = (void*)&PORTB, },
|
||||
};
|
||||
|
||||
#define PIXEL_COUNT 7
|
||||
/*
|
||||
const uint8_t pixels_0[PIXEL_COUNT * 3] =
|
||||
{ 255, 0, 255,
|
||||
255, 255, 0,
|
||||
255, 255, 255 };
|
||||
*/
|
||||
|
||||
const uint8_t pixels_1[PIXEL_COUNT * 4] =
|
||||
{ 0, 0, 0, 0,
|
||||
32, 0, 0, 0,
|
||||
64, 0, 0, 0,
|
||||
96, 0, 0, 0,
|
||||
128, 0, 0, 0,
|
||||
160, 0, 0, 0,
|
||||
192, 0, 0, 0 };
|
||||
|
||||
const uint8_t pixels_2[PIXEL_COUNT * 4] =
|
||||
{ 0, 0, 0, 0,
|
||||
0, 32, 0, 0,
|
||||
0, 64, 0, 0,
|
||||
0, 96, 0, 0,
|
||||
0, 128, 0, 0,
|
||||
0, 160, 0, 0,
|
||||
0, 192, 0, 0 };
|
||||
|
||||
const uint8_t pixels_3[PIXEL_COUNT * 4] =
|
||||
{ 0, 0, 0, 0,
|
||||
0, 0, 32, 0,
|
||||
0, 0, 64, 0,
|
||||
0, 0, 96, 0,
|
||||
0, 0, 128, 0,
|
||||
0, 0, 160, 0,
|
||||
0, 0, 192, 0 };
|
||||
|
||||
const uint8_t pixels_4[PIXEL_COUNT * 4] =
|
||||
{ 0, 0, 0, 0,
|
||||
0, 0, 0, 32,
|
||||
0, 0, 0, 64,
|
||||
0, 0, 0, 96,
|
||||
0, 0, 0, 128,
|
||||
0, 0, 0, 160,
|
||||
0, 0, 0, 192 };
|
||||
|
||||
void blink(void) {
|
||||
PORTB |= _BV(2);
|
||||
_delay_ms(100);
|
||||
PORTB &= ~(_BV(2));
|
||||
}
|
||||
|
||||
/*
|
||||
void fade_in(const uint8_t *pixels);
|
||||
void fade_out(void);
|
||||
|
||||
void fade_in(const uint8_t *pixels) {
|
||||
uint8_t current[PIXEL_COUNT * 4];
|
||||
for (int i = 0; i < 255; i++) {
|
||||
for (int idx = 0; idx < PIXEL_COUNT * 4; idx++) {
|
||||
if (current[idx] < pixels[idx]) {
|
||||
current[idx] += 1;
|
||||
}
|
||||
}
|
||||
write_pixels(current, PIXEL_COUNT * 4);
|
||||
_delay_ms(5);
|
||||
}
|
||||
}
|
||||
|
||||
void fade_out() {
|
||||
uint8_t current[PIXEL_COUNT * 4];
|
||||
for (int i = 0; i < 255; i++) {
|
||||
for (int idx = 0; idx < PIXEL_COUNT * 4; idx++) {
|
||||
if (current[idx] > 0) {
|
||||
current[idx] -= 1;
|
||||
}
|
||||
}
|
||||
write_pixels(current, PIXEL_COUNT * 4);
|
||||
_delay_ms(5);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
int main (void) {
|
||||
PORTB = 0;
|
||||
DDRB = _BV(0) | _BV(1) | _BV(2) | _BV(3);
|
||||
_delay_ms(50);
|
||||
|
||||
blink();
|
||||
|
||||
_delay_ms(500);
|
||||
|
||||
/*
|
||||
while (1) {
|
||||
blink();
|
||||
write_pixels(pixels_1, PIXEL_COUNT * 4);
|
||||
_delay_ms(1000);
|
||||
write_pixels(pixels_2, PIXEL_COUNT * 4);
|
||||
_delay_ms(1000);
|
||||
write_pixels(pixels_3, PIXEL_COUNT * 4);
|
||||
_delay_ms(1000);
|
||||
write_pixels(pixels_4, PIXEL_COUNT * 4);
|
||||
_delay_ms(1000);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
uint8_t pixels[7 * 3];
|
||||
for (uint8_t i = 0; i < 7 * 3; i++) {
|
||||
pixels[i] = 0;
|
||||
}
|
||||
|
||||
int8_t r_step = 1;
|
||||
int8_t g_step = 0;
|
||||
int8_t b_step = 0;
|
||||
while (1) {
|
||||
pixels[0] += r_step;
|
||||
pixels[3] += r_step;
|
||||
pixels[6] += r_step;
|
||||
pixels[9] += r_step;
|
||||
pixels[12] += r_step;
|
||||
pixels[15] += r_step;
|
||||
pixels[18] += r_step;
|
||||
// pixels[1] += g_step;
|
||||
// pixels[2] += b_step;
|
||||
write_pixels(pixels, 7 * 3);
|
||||
if (pixels[0] == 255) {
|
||||
r_step = -1;
|
||||
} else if (pixels[0] == 0) {
|
||||
r_step = 1;
|
||||
}
|
||||
/*
|
||||
if (pixels[0] == 255) {
|
||||
r_step = -1;
|
||||
g_step = 1;
|
||||
} else if (pixels[0] == 0 && r_step == -1) {
|
||||
r_step = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
if (pixels[1] == 255) {
|
||||
g_step = -1;
|
||||
b_step = 1;
|
||||
} else if (pixels[1] == 0 && g_step == -1) {
|
||||
g_step = 0;
|
||||
}
|
||||
|
||||
if (pixels[2] == 255) {
|
||||
b_step = -1;
|
||||
r_step = 1;
|
||||
} else if (pixels[2] == 0 && b_step == -1) {
|
||||
b_step = 0;
|
||||
}
|
||||
*/
|
||||
_delay_ms(5);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "ws2812.h"
|
||||
|
||||
/*
|
||||
void latch(io_pin_t *addr) {
|
||||
if (addr->bit >= 8) return;
|
||||
*(addr->port) &= ~(1<<0);
|
||||
_delay_us(50);
|
||||
}
|
||||
*/
|
||||
|
||||
#define T0_HIGH 4
|
||||
#define T1_HIGH 7
|
||||
#define T_FRAME 11
|
||||
|
||||
#define write_bit(label, bit) \
|
||||
"out %[port], %[hi]" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"sbrc %[byte], " #bit "\n\t" \
|
||||
"rjmp .+0" "\n\t" \
|
||||
"out %[port], %[low]" "\n\t" \
|
||||
"rjmp .+0" "\n\t" \
|
||||
"rjmp .+0" "\n\t" \
|
||||
|
||||
/*
|
||||
#define write_bit(label, bit) \
|
||||
"bst %[byte], " #bit "\n\t" \
|
||||
"out %[port], %[hi]" "\n\t" \
|
||||
"nop" "\n\t" \
|
||||
"brtc " label "\n\t" \
|
||||
"rjmp .+0" "\n\t" \
|
||||
label ": " "out %[port], %[low]" "\n\t" \
|
||||
"rjmp .+0" "\n\t" \
|
||||
"rjmp .+0" "\n\t"
|
||||
*/
|
||||
|
||||
|
||||
inline void write_byte(volatile uint8_t *port, uint8_t hi, uint8_t low, uint8_t byte) {
|
||||
__asm__ __volatile__ (
|
||||
write_bit("A_%=", 7)
|
||||
write_bit("B_%=", 6)
|
||||
write_bit("C_%=", 5)
|
||||
write_bit("D_%=", 4)
|
||||
write_bit("E_%=", 3)
|
||||
write_bit("F_%=", 2)
|
||||
write_bit("G_%=", 1)
|
||||
write_bit("H_%=", 0)
|
||||
: /* No outputs */
|
||||
: [port] "I" (_SFR_IO_ADDR(PORTB)),
|
||||
[hi] "r" (hi),
|
||||
[low] "r" (low),
|
||||
[byte] "r" (byte)
|
||||
);
|
||||
|
||||
// __asm__ __volatile__ (
|
||||
/*
|
||||
"ldi r24, 8" "\n\t" // count out eight bits. One higher than normal because I do the final check *after* the counter decrements. I'm effectively 1-based
|
||||
"lsl %[byte]" "\n\t" // output data left, saving the msb in SREG (1 cycle)
|
||||
"bst sreg, 0" "\n\t" // Save hte carry flag to the transfer bit
|
||||
"L_%=: " "out %[port], %[hi]" "\n\t" // enable the port
|
||||
"brtc I_%=" "\n\t" // If we shifted out a 0, if SREG[C] is clear, immediately branch I_%= (2 cycles if true, 1 cycle if false)
|
||||
"nop" "\n\t" // We shifted out a 1, so wait just a touch longer
|
||||
"nop" "\n\t"
|
||||
"nop" "\n\t"
|
||||
"I_%=: " "out %[port], %[low]" "\n\t" // now clear the pin
|
||||
// I now have five ticks before I can re-enable the pin
|
||||
"lsl %[byte]" "\n\t" // Shift out the next bit
|
||||
"bst sreg, 0" "\n\t"
|
||||
"dec r24" "\n\t" // Decrement the bit counter
|
||||
"cpi r24, 0" "\n\t" // Is the bit counter 0?
|
||||
"brne L_%=" "\n\t" // If we haven't reached 0, we have more data to send
|
||||
*/
|
||||
}
|
||||
|
||||
void write_pixels(const uint8_t *pixels, uint8_t length) {
|
||||
uint8_t hi = PORTB | (1 << 3);
|
||||
uint8_t low = PORTB & ~(1 << 3);
|
||||
cli();
|
||||
for (int idx = 0; idx < length; idx++) {
|
||||
write_byte(&PORTB, hi, low, pixels[idx]);
|
||||
}
|
||||
_delay_us(100);
|
||||
sei();
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#include "np_common.c"
|
||||
|
||||
void np_write_rgb(io_pin_t *addr, rgb_t *values, uint8_t length) {
|
||||
write_pixels(addr, values, length * 3);
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef __neopixels_h__
|
||||
#define __neopixels_h__
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <base.h>
|
||||
|
||||
typedef struct RGB_s {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} rgb_t;
|
||||
|
||||
typedef struct RGBW_s {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t w;
|
||||
} rgbw_t;
|
||||
|
||||
// void np_initialize();
|
||||
// void np_write_rgb(io_pin_t *addr, rgb_t *values, uint8_t length);
|
||||
void write_pixels(const uint8_t *pixels, uint8_t length);
|
||||
// void np_write_grb(io_pin_t *addr, rgb_t *values, uint8_t length);
|
||||
// void np_write_rgbw(io_pin_t *addr, rgbw_t *values, uint8_t length);
|
||||
// void np_write_grbw(io_pin_t *addr, rgbw_t *values, uint8_t length);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue