feat(ex00): done
This commit is contained in:
commit
b39ed740d2
13 changed files with 610 additions and 0 deletions
57
.clang-format
Normal file
57
.clang-format
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
BasedOnStyle: Chromium
|
||||
AccessModifierOffset: 0
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
AlignCompound: true
|
||||
AlignFunctionPointers: true
|
||||
PadOperators: false
|
||||
AlignConsecutiveBitFields:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
AlignCompound: true
|
||||
AlignFunctionPointers: true
|
||||
PadOperators: true
|
||||
AlignConsecutiveDeclarations:
|
||||
Enabled: true
|
||||
AcrossComments: true
|
||||
AlignCompound: true
|
||||
AlignFunctionPointers: true
|
||||
PadOperators: true
|
||||
AlignConsecutiveMacros:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
AlignCompound: true
|
||||
AlignFunctionPointers: true
|
||||
PadOperators: true
|
||||
AlignConsecutiveShortCaseStatements:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
AlignCaseArrows: true
|
||||
AlignCaseColons: true
|
||||
ColumnLimit: 100
|
||||
NamespaceIndentation: All
|
||||
CommentPragmas: ""
|
||||
IncludeCategories:
|
||||
- Regex: ^<.*\.h>
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: ^<.*
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: .*
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
IndentPPDirectives: AfterHash
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Always
|
||||
Language: Cpp
|
||||
IndentAccessModifiers: true
|
||||
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
*.o
|
||||
*.hex
|
||||
*.bin
|
||||
*.elf
|
||||
build/
|
||||
/avr-libc
|
||||
compile_commands.json
|
||||
.cache/
|
||||
54
ex00/Makefile
Normal file
54
ex00/Makefile
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# 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 uart.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)
|
||||
|
||||
14
ex00/include/interupt.h
Normal file
14
ex00/include/interupt.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef INTERUPT_H
|
||||
#define INTERUPT_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
static inline void my_sei(void) {
|
||||
SREG |= _BV(SREG_I);
|
||||
}
|
||||
|
||||
static inline void my_cli(void) {
|
||||
SREG &= ~_BV(SREG_I);
|
||||
}
|
||||
|
||||
#endif /* INTERUPT_H */
|
||||
17
ex00/include/mystd.h
Normal file
17
ex00/include/mystd.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#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)
|
||||
|
||||
#define NULL ((void*)0)
|
||||
|
||||
#endif /* MYSTDINT_H */
|
||||
112
ex00/include/timer0.h
Normal file
112
ex00/include/timer0.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#ifndef TIMER0_H
|
||||
# define TIMER0_H
|
||||
|
||||
# include <avr/io.h>
|
||||
# include "mystd.h"
|
||||
|
||||
# include "timer_global.h"
|
||||
|
||||
static inline void t0_init_fpwm_3(e_timer_prescaler prescaler) {
|
||||
// Fast PWM (8-bit): WGM22:0 = 0b011
|
||||
TCCR0A = _BV(WGM00) | _BV(WGM01);
|
||||
|
||||
// reset to zero -> timer off
|
||||
TCCR0B &= ~(_BV(CS02) | _BV(CS01) | _BV(CS00));
|
||||
// set the correct prescaler
|
||||
switch (prescaler) {
|
||||
case (PRESCALER_1): {
|
||||
TCCR0B |= (_BV(CS00));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_8): {
|
||||
TCCR0B |= (_BV(CS01));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_64): {
|
||||
TCCR0B |= (_BV(CS01) | _BV(CS00));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_256): {
|
||||
TCCR0B |= (_BV(CS02));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_1024): {
|
||||
TCCR0B |= (_BV(CS02) | _BV(CS00));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_OFF): {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void t0_set_ocr(enum e_timer_output output, uint8_t value) {
|
||||
if (output & TO_A)
|
||||
OCR0A = value;
|
||||
if (output & TO_B)
|
||||
OCR0B = value;
|
||||
}
|
||||
|
||||
static inline void t0_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) {
|
||||
if (output & TO_A) {
|
||||
TCCR0A &= ~(_BV(COM0A1) | _BV(COM0A0));
|
||||
switch (mode) {
|
||||
case (TOM_00): {
|
||||
break;
|
||||
}
|
||||
case (TOM_10): {
|
||||
TCCR0A |= (_BV(COM0A1));
|
||||
break;
|
||||
}
|
||||
case (TOM_01): {
|
||||
TCCR0A |= (_BV(COM0A0));
|
||||
break;
|
||||
}
|
||||
case (TOM_11): {
|
||||
TCCR0A |= (_BV(COM0A1) | _BV(COM0A0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (output & TO_B) {
|
||||
TCCR0A &= ~(_BV(COM0B1) | _BV(COM0B0));
|
||||
switch (mode) {
|
||||
case (TOM_00): {
|
||||
break;
|
||||
}
|
||||
case (TOM_10): {
|
||||
TCCR0A |= (_BV(COM0B1));
|
||||
break;
|
||||
}
|
||||
case (TOM_01): {
|
||||
TCCR0A |= (_BV(COM0B0));
|
||||
break;
|
||||
}
|
||||
case (TOM_11): {
|
||||
TCCR0A |= (_BV(COM0B1) | _BV(COM0B0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OC2B => RED => PD3
|
||||
// OC0B => GREEN => PD5
|
||||
// OC0A => BLUE => PD6
|
||||
|
||||
#endif /* TIMER0_H */
|
||||
|
||||
/*
|
||||
// OC2B = PD3 → output
|
||||
DDRD |= _BV(DDD3) | _BV(DDD5) | _BV(DDD6);
|
||||
|
||||
// Fast PWM (8-bit): WGM22:0 = 0b011
|
||||
TCCR0A = _BV(WGM00) | _BV(WGM01);
|
||||
TCCR0A |= _BV(COM0B1);
|
||||
|
||||
// 50% duty cycle
|
||||
OCR0B = 128;
|
||||
|
||||
// Start timer, prescaler = 64
|
||||
TCCR0B = _BV(CS02);
|
||||
*/
|
||||
112
ex00/include/timer2.h
Normal file
112
ex00/include/timer2.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#ifndef TIMER2_H
|
||||
# define TIMER2_H
|
||||
|
||||
# include <avr/io.h>
|
||||
# include "mystd.h"
|
||||
|
||||
# include "timer_global.h"
|
||||
|
||||
static inline void t2_init_fpwm_3(e_timer_prescaler prescaler) {
|
||||
// Fast PWM (8-bit): WGM22:0 = 0b011
|
||||
TCCR2A = _BV(WGM20) | _BV(WGM21);
|
||||
|
||||
// reset to zero -> timer off
|
||||
TCCR2B &= ~(_BV(CS22) | _BV(CS21) | _BV(CS20));
|
||||
// set the correct prescaler
|
||||
switch (prescaler) {
|
||||
case (PRESCALER_1): {
|
||||
TCCR2B |= (_BV(CS20));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_8): {
|
||||
TCCR2B |= (_BV(CS21));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_64): {
|
||||
TCCR2B |= (_BV(CS21) | _BV(CS20));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_256): {
|
||||
TCCR2B |= (_BV(CS22));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_1024): {
|
||||
TCCR2B |= (_BV(CS22) | _BV(CS20));
|
||||
break;
|
||||
}
|
||||
case (PRESCALER_OFF): {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void t2_set_ocr(enum e_timer_output output, uint8_t value) {
|
||||
if (output & TO_A)
|
||||
OCR2A = value;
|
||||
if (output & TO_B)
|
||||
OCR2B = value;
|
||||
}
|
||||
|
||||
static inline void t2_set_out_mode(enum e_timer_output output, enum e_timer_output_mode mode) {
|
||||
if (output & TO_A) {
|
||||
TCCR2A &= ~(_BV(COM2A1) | _BV(COM2A0));
|
||||
switch (mode) {
|
||||
case (TOM_00): {
|
||||
break;
|
||||
}
|
||||
case (TOM_10): {
|
||||
TCCR2A |= (_BV(COM2A1));
|
||||
break;
|
||||
}
|
||||
case (TOM_01): {
|
||||
TCCR2A |= (_BV(COM2A0));
|
||||
break;
|
||||
}
|
||||
case (TOM_11): {
|
||||
TCCR2A |= (_BV(COM2A1) | _BV(COM2A0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (output & TO_B) {
|
||||
TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
|
||||
switch (mode) {
|
||||
case (TOM_00): {
|
||||
break;
|
||||
}
|
||||
case (TOM_10): {
|
||||
TCCR2A |= (_BV(COM2B1));
|
||||
break;
|
||||
}
|
||||
case (TOM_01): {
|
||||
TCCR2A |= (_BV(COM2B0));
|
||||
break;
|
||||
}
|
||||
case (TOM_11): {
|
||||
TCCR2A |= (_BV(COM2B1) | _BV(COM2B0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OC2B => RED => PD3
|
||||
// OC0B => GREEN => PD5
|
||||
// OC0A => BLUE => PD6
|
||||
|
||||
#endif /* TIMER0_H */
|
||||
|
||||
/*
|
||||
// OC2B = PD3 → output
|
||||
DDRD |= _BV(DDD3) | _BV(DDD5) | _BV(DDD6);
|
||||
|
||||
// Fast PWM (8-bit): WGM22:0 = 0b011
|
||||
TCCR2A = _BV(GM20) | _BV(GM21);
|
||||
TCCR2A |= _BV(COM2B1);
|
||||
|
||||
// 50% duty cycle
|
||||
OCR0B = 128;
|
||||
|
||||
// Start timer, prescaler = 64
|
||||
TCCR2B = _BV(CS22);
|
||||
*/
|
||||
25
ex00/include/timer_global.h
Normal file
25
ex00/include/timer_global.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef TIMER_GLOBAL_H
|
||||
#define TIMER_GLOBAL_H
|
||||
|
||||
typedef enum e_timer_prescaler {
|
||||
PRESCALER_OFF = 0,
|
||||
PRESCALER_1 = 1,
|
||||
PRESCALER_8 = 8,
|
||||
PRESCALER_64 = 64,
|
||||
PRESCALER_256 = 256,
|
||||
PRESCALER_1024 = 1024,
|
||||
} e_timer_prescaler;
|
||||
|
||||
typedef enum e_timer_output {
|
||||
TO_A = (1 << 0),
|
||||
TO_B = (1 << 1),
|
||||
} e_timer_output;
|
||||
|
||||
typedef enum e_timer_output_mode {
|
||||
TOM_00,
|
||||
TOM_01,
|
||||
TOM_10,
|
||||
TOM_11,
|
||||
} e_timer_output_mode;
|
||||
|
||||
#endif /* TIMER_GLOBAL_H */
|
||||
19
ex00/include/uart.h
Normal file
19
ex00/include/uart.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef UART_H
|
||||
#define UART_H
|
||||
|
||||
#include "mystd.h"
|
||||
|
||||
void uart_init(void);
|
||||
|
||||
void uart_tx(char data);
|
||||
void uart_sendstring(const char* str);
|
||||
|
||||
void uart_send_u8(uint8_t val);
|
||||
void uart_send_u16(uint16_t val);
|
||||
|
||||
void uart_send_u8_hex(uint8_t val);
|
||||
void uart_send_u16_hex(uint16_t val);
|
||||
|
||||
char uart_rx(void);
|
||||
|
||||
#endif /* UART_H */
|
||||
12
ex00/include/utils.h
Normal file
12
ex00/include/utils.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include "mystd.h"
|
||||
|
||||
void delay_ms(uint16_t count);
|
||||
|
||||
void ft_bzero(void *data, uint16_t size);
|
||||
|
||||
uint8_t ft_stridx(const char* str, char chr);
|
||||
|
||||
#endif /* UTILS_H */
|
||||
36
ex00/src/main.c
Normal file
36
ex00/src/main.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "interupt.h"
|
||||
#include "mystd.h"
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
// interrupt for INT0 (external interrupt)
|
||||
void __attribute__((signal, used)) __vector_1(void) {
|
||||
my_cli();
|
||||
_delay_ms(20);
|
||||
PORTB ^= _BV(PB0);
|
||||
// clear the register "pending" bit
|
||||
EIFR |= _BV(INTF0);
|
||||
my_sei();
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
uart_init();
|
||||
|
||||
DDRB |= _BV(PB0);
|
||||
DDRD &= ~_BV(PD2);
|
||||
|
||||
// Set INT0 to trigger on falling edge
|
||||
EICRA &= ~(_BV(ISC00) | _BV(ISC01));
|
||||
EICRA |= _BV(ISC01);
|
||||
|
||||
// yes we want the INT0 interupt !
|
||||
EIMSK |= _BV(INT0);
|
||||
|
||||
my_sei();
|
||||
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
105
ex00/src/uart.c
Normal file
105
ex00/src/uart.c
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include "uart.h"
|
||||
#include <avr/io.h>
|
||||
#include "mystd.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define BAUD_RATE 115200
|
||||
|
||||
#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1)
|
||||
|
||||
// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit
|
||||
// 115200 8N1
|
||||
void uart_init(void) {
|
||||
// Set baud rate
|
||||
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
|
||||
UBRR0L = (uint8_t)(UBRR_VALUE);
|
||||
|
||||
UCSR0A |= _BV(U2X0);
|
||||
// Enable transmitter
|
||||
UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(RXCIE0);
|
||||
|
||||
// Set frame format: 8 data bits, no parity, 1 stop bit
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
|
||||
// Set TX (PD1) as output
|
||||
DDRD |= _BV(PD1);
|
||||
}
|
||||
|
||||
void uart_tx(char data) {
|
||||
// wait for transmit buffer to be empty
|
||||
while (!(UCSR0A & _BV(UDRE0)))
|
||||
;
|
||||
// load data into transmit register
|
||||
UDR0 = data;
|
||||
}
|
||||
|
||||
char uart_rx(void) {
|
||||
while (!(UCSR0A & _BV(RXC0)))
|
||||
;
|
||||
return UDR0;
|
||||
}
|
||||
|
||||
void uart_sendstring(const char* str) {
|
||||
if (!str)
|
||||
return;
|
||||
while (*str) {
|
||||
uart_tx(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
/*
|
||||
void uart_send_u8(uint8_t val) {
|
||||
char buf[4] = {0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint8_t modulus = 100;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16(uint16_t val) {
|
||||
char buf[6] = {0, 0, 0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint16_t modulus = 10000;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u8_hex(uint8_t val) {
|
||||
char buf[3] = {0, 0, 0};
|
||||
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16_hex(uint16_t val) {
|
||||
char buf[5] = {0, 0, 0, 0, 0};
|
||||
|
||||
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
|
||||
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
*/
|
||||
39
ex00/src/utils.c
Normal file
39
ex00/src/utils.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include "utils.h"
|
||||
#include "mystd.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--;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ft_stridx(const char* str, char chr) {
|
||||
if (!str)
|
||||
return -1;
|
||||
for (uint8_t i = 0; str[i]; i++) {
|
||||
if (str[i] == chr)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue