Added parts of the avr library
This commit is contained in:
parent
0be8c0258c
commit
0cc3435cdc
|
@ -0,0 +1,2 @@
|
||||||
|
-I/usr/avr/include
|
||||||
|
-D__AVR_ATmega328__
|
|
@ -0,0 +1,3 @@
|
||||||
|
*.o
|
||||||
|
*.elf
|
||||||
|
*.hex
|
|
@ -0,0 +1,59 @@
|
||||||
|
AVRMCU ?= atmega8
|
||||||
|
F_CPU ?= 16000000
|
||||||
|
ISPPORT ?= /dev/ttyUSB0
|
||||||
|
|
||||||
|
VERSION = 0.1
|
||||||
|
|
||||||
|
HEADERS =
|
||||||
|
SRC = main.c
|
||||||
|
TARGET = sss7
|
||||||
|
OBJDIR = bin
|
||||||
|
|
||||||
|
CC = avr-gcc
|
||||||
|
OBJCOPY = avr-objcopy
|
||||||
|
OBJDUMP = avr-objdump
|
||||||
|
SIZE = avr-size
|
||||||
|
|
||||||
|
OBJ = $(SRC:%.c=$(OBJDIR)/$(AVRMCU)/%.o)
|
||||||
|
|
||||||
|
CFLAGS = -Os -Wall -Wstrict-prototypes
|
||||||
|
CFLAGS += -ffunction-sections -fdata-sections
|
||||||
|
CFLAGS += -fshort-enums -fpack-struct -funsigned-char -funsigned-bitfields
|
||||||
|
CFLAGS += -mmcu=$(AVRMCU) -DF_CPU=$(F_CPU)UL -DVERSION=$(VERSION)
|
||||||
|
|
||||||
|
LDFLAGS = -mmcu=$(AVRMCU) -Wl,--gc-sections
|
||||||
|
|
||||||
|
all: start $(OBJDIR)/$(AVRMCU)/$(TARGET).hex size
|
||||||
|
@echo ":: Done !"
|
||||||
|
|
||||||
|
start:
|
||||||
|
@echo "AS5043 demo version $(VERSION)"
|
||||||
|
@echo "=============================="
|
||||||
|
@echo ":: Building for $(AVRMCU)"
|
||||||
|
@echo ":: MCU operating frequency is $(F_CPU)Hz"
|
||||||
|
|
||||||
|
$(OBJDIR)/$(AVRMCU)/%.o : %.c $(HEADERS)
|
||||||
|
@mkdir -p $$(dirname $@)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJDIR)/$(AVRMCU)/$(TARGET).elf : $(OBJ)
|
||||||
|
$(CC) $(LDFLAGS) $+ -o $@
|
||||||
|
|
||||||
|
$(OBJDIR)/$(AVRMCU)/$(TARGET).hex : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
|
||||||
|
$(OBJCOPY) -O ihex $< $@
|
||||||
|
|
||||||
|
size : $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
|
||||||
|
@echo
|
||||||
|
@$(SIZE) --mcu=$(AVRMCU) -C $(OBJDIR)/$(AVRMCU)/$(TARGET).elf
|
||||||
|
@echo
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -rf $(OBJDIR)
|
||||||
|
|
||||||
|
flash : all
|
||||||
|
avrdude -c arduino \
|
||||||
|
-p $(AVRMCU) -P $(ISPPORT) \
|
||||||
|
-U flash:w:$(OBJDIR)/$(AVRMCU)/$(TARGET).hex
|
||||||
|
|
||||||
|
test : flash
|
||||||
|
screen $(ISPPORT) 38400
|
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
|
||||||
|
#include "sss7.h"
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,143 @@
|
||||||
|
#include "sss7.h"
|
||||||
|
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <util/crc16.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
volatile enum sss7State sss7_state;
|
||||||
|
|
||||||
|
uint8_t sss7_rx_buffer[2][SSS7_PAYLOAD_SIZE];
|
||||||
|
uint8_t sss7_rx_active_buffer;
|
||||||
|
uint8_t sss7_rx_pos;
|
||||||
|
|
||||||
|
uint8_t sss7_tx_buffer[SSS7_PAYLOAD_SIZE];
|
||||||
|
uint8_t sss7_tx_pos;
|
||||||
|
uint8_t sss7_tx_crc;
|
||||||
|
uint8_t sss7_tx_failed;
|
||||||
|
uint8_t sss7_tx_last_byte = 0;
|
||||||
|
|
||||||
|
void sss7_init() {
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
|
||||||
|
sss7_rx_pos = 0;
|
||||||
|
sss7_rx_oldest_buffer = 0;
|
||||||
|
sss7_rx_active_buffer = 0;
|
||||||
|
|
||||||
|
sss7_tx_pos = 0;
|
||||||
|
sss7_tx_crc = 0;
|
||||||
|
sss7_tx_failed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint8_t sss7_payload_crc(uint8_t buffer[SSS7_PAYLOAD_SIZE]) {
|
||||||
|
uint8_t crc = 0;
|
||||||
|
for(uint8_t i = 0; i < SSS7_PAYLOAD_SIZE; i++) {
|
||||||
|
crc = _crc_ibutton_update(crc, buffer[i]);
|
||||||
|
}
|
||||||
|
return crc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
|
// Check that we can send, because we will overwritte the buffer
|
||||||
|
if(sss7_state != SSS7_IDLE) {
|
||||||
|
sss7_tx_failed = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(sss7_tx_buffer, msg, SSS7_PAYLOAD_SIZE);
|
||||||
|
|
||||||
|
sss7_tx_pos = 0;
|
||||||
|
sss7_tx_crc = sss7_payload_crc(sss7_tx_buffer);
|
||||||
|
|
||||||
|
// Commit to send state
|
||||||
|
sss7_state = SSS7_TX_HEADER;
|
||||||
|
uart_sent_byte(SSS7_HEADER[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sss7_receive_byte() {
|
||||||
|
uint8_t byte = uart_get_byte();
|
||||||
|
uint8_t crc = 0;
|
||||||
|
|
||||||
|
switch(sss7_state) {
|
||||||
|
case SSS7_IDLE:
|
||||||
|
if(byte == SSS7_HEADER[0]) {
|
||||||
|
sss7_state = SSS7_RX_HEADER;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SSS7_RX_HEADER:
|
||||||
|
if(byte == SSS7_HEADER[1]) {
|
||||||
|
sss7_state = SSS7_RX_PAYLOAD;
|
||||||
|
sss7_rx_pos = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SSS7_RX_PAYLOAD:
|
||||||
|
sss7_rx_buffer[sss7_rx_active_buffer][sss7_rx_pos] = byte;
|
||||||
|
sss7_rx_pos++;
|
||||||
|
if(sss7_rx_pos >= SSS7_PAYLOAD_SIZE) {
|
||||||
|
sss7_state = SSS7_RX_CRC;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SSS7_RX_CRC:
|
||||||
|
crc = sss7_payload_crc(sss7_rx_buffer[sss7_rx_active_buffer]);
|
||||||
|
if(byte == crc) {
|
||||||
|
sss7_rx_active_buffer = (sss7_rx_active_buffer + 1);
|
||||||
|
}
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
// all the TX states
|
||||||
|
default:
|
||||||
|
if(byte != sss7_tx_last_byte) {
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
sss7_tx_failed = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sss7_transmit_byte() {
|
||||||
|
uint8_t byte;
|
||||||
|
switch(sss7_state) {
|
||||||
|
case SSS7_TX_HEADER:
|
||||||
|
uart_sent_byte(SSS7_HEADER[1]);
|
||||||
|
sss7_state = SSS7_TX_PAYLOAD;
|
||||||
|
sss7_tx_pos = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SSS7_TX_PAYLOAD:
|
||||||
|
byte = sss7_tx_buffer[sss7_tx_pos];
|
||||||
|
uart_sent_byte(byte);
|
||||||
|
sss7_tx_last_byte = byte;
|
||||||
|
sss7_tx_pos++;
|
||||||
|
|
||||||
|
if(sss7_tx_pos >= SSS7_PAYLOAD_SIZE) {
|
||||||
|
sss7_state = SSS7_TX_CRC;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SSS7_TX_CRC:
|
||||||
|
uart_sent_byte(sss7_tx_crc);
|
||||||
|
sss7_tx_last_byte = sss7_tx_crc;
|
||||||
|
sss7_state = SSS7_IDLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
//All the RX states
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
|
if(sss7_has_received()) {
|
||||||
|
memcpy(msg, sss7_rx_buffer[sss7_rx_oldest_buffer], SSS7_PAYLOAD_SIZE);
|
||||||
|
sss7_rx_oldest_buffer++;
|
||||||
|
};
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
#ifndef _SSS7_H_
|
||||||
|
#define _SSS7_H_
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
enum sss7State {
|
||||||
|
SSS7_IDLE,
|
||||||
|
SSS7_TX_HEADER,
|
||||||
|
SSS7_TX_PAYLOAD,
|
||||||
|
SSS7_TX_CRC,
|
||||||
|
|
||||||
|
SSS7_RX_HEADER,
|
||||||
|
SSS7_RX_PAYLOAD,
|
||||||
|
SSS7_RX_CRC
|
||||||
|
};
|
||||||
|
|
||||||
|
const uint8_t SSS7_HEADER[] = {0xAA, 0xFE};
|
||||||
|
|
||||||
|
#define SSS7_PAYLOAD_SIZE 16
|
||||||
|
#define SSS7_RX_BUFFER_COUNT 2
|
||||||
|
|
||||||
|
extern volatile enum sss7State sss7_state;
|
||||||
|
extern uint8_t sss7_tx_failed;
|
||||||
|
extern uint8_t sss7_rx_active_buffer;
|
||||||
|
extern uint8_t sss7_rx_oldest_buffer;
|
||||||
|
|
||||||
|
void sss7_init();
|
||||||
|
|
||||||
|
static inline uint8_t sss7_can_send() {
|
||||||
|
return sss7_state == SSS7_IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]);
|
||||||
|
|
||||||
|
static inline uint8_t sss7_send_failed() {
|
||||||
|
return sss7_state != SSS7_IDLE && sss7_tx_failed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint8_t sss7_has_received() {
|
||||||
|
return sss7_rx_oldest_buffer < sss7_rx_active_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void sss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue