feat(ex01): done
This commit is contained in:
parent
20cab5c109
commit
3b45f18fa0
5 changed files with 159 additions and 0 deletions
55
ex01/Makefile
Normal file
55
ex01/Makefile
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# Makefile
|
||||
MCU=atmega328p
|
||||
F_CPU=16000000
|
||||
CC=avr-gcc
|
||||
OBJCOPY=avr-objcopy
|
||||
CFLAGS=-std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Iinclude
|
||||
IFLAGS=
|
||||
TARGET=main
|
||||
SERIAL=-P /dev/ttyUSB0 -b 115200
|
||||
|
||||
|
||||
SRC_DIR=src
|
||||
OBJ_DIR=build
|
||||
|
||||
SRC_FILES=main.c utils.c
|
||||
OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES))
|
||||
|
||||
SRC=$(addprefix $(SRC_DIR)/,$(SRC_FILES))
|
||||
OBJ=$(addprefix $(OBJ_DIR)/,$(OBJ_FILES))
|
||||
|
||||
ELF_FILE=$(OBJ_DIR)/$(TARGET).elf
|
||||
HEX_FILE=$(OBJ_DIR)/$(TARGET).hex
|
||||
|
||||
all: flash
|
||||
|
||||
re: fclean all
|
||||
|
||||
fclean: clean
|
||||
rm -f $(HEX_FILE)
|
||||
rm -f $(ELF_FILE)
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR)
|
||||
|
||||
hex: $(HEX_FILE)
|
||||
|
||||
flash: hex
|
||||
avrdude -p $(MCU) -c arduino -U flash:w:$(HEX_FILE):i $(SERIAL)
|
||||
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
@mkdir -p $(shell dirname $@)
|
||||
$(CC) $(CFLAGS) $(IFLAGS) -c $< -o $@
|
||||
|
||||
$(ELF_FILE): $(OBJ)
|
||||
$(CC) $(CFLAGS) $(OBJ) -o $@
|
||||
|
||||
$(HEX_FILE): $(ELF_FILE)
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
print:
|
||||
@echo $(SRC)
|
||||
@echo $(OBJ)
|
||||
@echo $(ELF_FILE)
|
||||
@echo $(HEX_FILE)
|
||||
|
||||
15
ex01/include/mystd.h
Normal file
15
ex01/include/mystd.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef MYSTDINT_H
|
||||
#define MYSTDINT_H
|
||||
|
||||
typedef unsigned int uint16_t;
|
||||
typedef signed int int16_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef signed char int8_t;
|
||||
|
||||
typedef uint8_t bool;
|
||||
|
||||
#define true (1)
|
||||
#define false (0)
|
||||
|
||||
#endif /* MYSTDINT_H */
|
||||
10
ex01/include/utils.h
Normal file
10
ex01/include/utils.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include "mystd.h"
|
||||
|
||||
void delay_ms(uint16_t count);
|
||||
|
||||
void ft_bzero(void *data, uint16_t size);
|
||||
|
||||
#endif /* UTILS_H */
|
||||
51
ex01/src/main.c
Normal file
51
ex01/src/main.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
#include "mystd.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define D5_R PD5
|
||||
#define D5_G PD6
|
||||
#define D5_B PD3
|
||||
|
||||
#define RGB_MASK (_BV(D5_R) | _BV(D5_G) | _BV(D5_B))
|
||||
|
||||
/*
|
||||
name R# G# B#
|
||||
red ff 0 0
|
||||
green 0 ff 0
|
||||
blue 0 0 ff
|
||||
|
||||
yellow ff ff 0
|
||||
cyan 0 ff ff
|
||||
magenta ff 0 ff
|
||||
|
||||
white ff ff ff
|
||||
*/
|
||||
#define COL_RED (_BV(D5_R))
|
||||
#define COL_GREEN (_BV(D5_G))
|
||||
#define COL_BLUE (_BV(D5_B))
|
||||
|
||||
#define COL_YELLOW (_BV(D5_R) | _BV(D5_G))
|
||||
#define COL_CYAN (_BV(D5_G) | _BV(D5_B))
|
||||
#define COL_MAGENTA (_BV(D5_R) | _BV(D5_B))
|
||||
|
||||
#define COL_WHITE (_BV(D5_R) | _BV(D5_G) | _BV(D5_B))
|
||||
|
||||
int main(void) {
|
||||
DDRD |= RGB_MASK;
|
||||
|
||||
PORTD = (PORTD & ~RGB_MASK);
|
||||
|
||||
uint8_t colors[7] = {
|
||||
COL_RED, COL_GREEN, COL_BLUE, COL_YELLOW,
|
||||
COL_CYAN, COL_MAGENTA, COL_WHITE,
|
||||
};
|
||||
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 7; i++) {
|
||||
PORTD = (PORTD & ~RGB_MASK) | colors[i];
|
||||
delay_ms(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
ex01/src/utils.c
Normal file
28
ex01/src/utils.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "utils.h"
|
||||
|
||||
// this just burns cycles.
|
||||
// the volatile is important, it means that the cpu can't optimize any
|
||||
// read/writes for the value
|
||||
static inline void spin_loop(volatile uint16_t counts) {
|
||||
while (counts)
|
||||
counts--;
|
||||
}
|
||||
|
||||
void delay_ms(uint16_t ms) {
|
||||
while (ms) {
|
||||
// this value was taken using a delay of 500ms, and just recording the led
|
||||
// blinking. it seems to be high enough such that each loop of delay_loop
|
||||
// takes 1ms :D
|
||||
spin_loop((F_CPU) / 5000);
|
||||
ms--;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_bzero(void* data, uint16_t size) {
|
||||
char* d = data;
|
||||
while (size) {
|
||||
*d = 0;
|
||||
d++;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue