feat(ex00): should be done
This commit is contained in:
commit
9184272b75
7 changed files with 192 additions and 0 deletions
57
ex00/.clang-format
Normal file
57
ex00/.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
ex00/.gitignore
vendored
Normal file
8
ex00/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
*.o
|
||||
*.hex
|
||||
*.bin
|
||||
*.elf
|
||||
build/
|
||||
/avr-libc
|
||||
compile_commands.json
|
||||
.cache/
|
||||
43
ex00/Makefile
Normal file
43
ex00/Makefile
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# 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
|
||||
TARGET=main
|
||||
SERIAL=-P /dev/ttyUSB0 -b 115200
|
||||
|
||||
|
||||
SRC_DIR=.
|
||||
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))
|
||||
|
||||
all: flash
|
||||
|
||||
re: fclean all
|
||||
|
||||
fclean: clean
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR)
|
||||
rm -f $(TARGET).hex
|
||||
|
||||
hex: $(TARGET).hex
|
||||
|
||||
flash: hex
|
||||
avrdude -p $(MCU) -c arduino -U flash:w:$(TARGET).hex:i $(SERIAL)
|
||||
|
||||
$(OBJ_DIR)/$(TARGET).bin: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(OBJ) -o $@
|
||||
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
|
||||
mkdir -p $(shell dirname $@)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
|
||||
$(TARGET).hex: $(OBJ_DIR)/$(TARGET).bin
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
15
ex00/include/mystd.h
Normal file
15
ex00/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 */
|
||||
8
ex00/include/utils.h
Normal file
8
ex00/include/utils.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include "mystd.h"
|
||||
|
||||
void delay_ms(uint16_t count);
|
||||
|
||||
#endif /* UTILS_H */
|
||||
42
ex00/main.c
Normal file
42
ex00/main.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#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);
|
||||
|
||||
// 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(uint8_t data) {
|
||||
// wait for transmit buffer to be empty
|
||||
while (!(UCSR0A & _BV(UDRE0)))
|
||||
;
|
||||
// load data into transmit register
|
||||
UDR0 = data;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
uart_init();
|
||||
|
||||
while (true) {
|
||||
uart_tx('Z');
|
||||
delay_ms(1000);
|
||||
}
|
||||
}
|
||||
19
ex00/utils.c
Normal file
19
ex00/utils.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#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--;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue