Skip to content

Instantly share code, notes, and snippets.

@dagon666
dagon666 / Makefile
Created September 30, 2013 20:30
generating tones with libpca
TARGET=beep_libpca
SOURCES=beep_libpca.c
DEPS=
COBJ=$(SOURCES:.c=.o)
PCA_PREFIX=../pca
CC=avr-gcc
OBJC=avr-objcopy
MCU=atmega328p
@dagon666
dagon666 / usart_init.c
Created October 4, 2013 18:24
atmega328p usart init 57600 baud
void serial_init() {
power_usart0_enable();
// configure ports double mode
UCSR0A = _BV(U2X0);
// configure the ports speed
UBRR0H = 0x00;
@dagon666
dagon666 / usart_poll.c
Created October 4, 2013 18:27
usart poll rx/tx
unsigned char serial_poll_recv(unsigned char *a_data, unsigned int a_size) {
unsigned int cnt = a_size;
while (a_size) {
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) );
/* Get and return received data from buffer */
*a_data = UDR0;
a_data++;
@dagon666
dagon666 / ring_buffer.h
Created October 4, 2013 18:31
ring buffer
typedef struct _t_buffer {
/// data storage space for the ring buffer
volatile unsigned char ring[SERIAL_RX_RING_SIZE];
/// head index
volatile unsigned char head;
/// tail index
volatile unsigned char tail;
@dagon666
dagon666 / isr_rx.c
Created October 4, 2013 18:32
interrupt driven data reception
// the RX ring buffer
static volatile t_buffer g_rx_buff;
ISR(USART_RX_vect, ISR_BLOCK) {
// no frame error
// UCSR0A must be read before UDR0 !!!
if (bit_is_clear(UCSR0A, FE0)) {
/// must read the data in order to clear the interrupt flag
volatile unsigned char data = UDR0;
@dagon666
dagon666 / isr_tx.c
Created October 4, 2013 18:34
interrupt driven transmission
ISR(USART_UDRE_vect, ISR_BLOCK) {
// proceed if there still is data to be send
if (g_tx_buff.head != g_tx_buff.tail) {
UDR0 = g_tx_buff.ring[g_tx_buff.tail];
g_tx_buff.tail = (g_tx_buff.tail + 1) % SERIAL_TX_RING_SIZE;
}
else {
// mask the interrupt everything has been send
UCSR0B &= ~_BV(UDRIE0);
@dagon666
dagon666 / serial_echo.c
Created October 4, 2013 18:38
libpca echo program
#include "pca.h"
int main(int argc, char const *argv[])
{
uint8_t c = 0x00;
serial_init(E_BAUD_57600);
serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT);
serial_flush();
@dagon666
dagon666 / serial_stdio.c
Created October 8, 2013 20:41
serial_stdio
void _serial_putc(char c, FILE *stream) {
if ('\n' == c) {
_serial_putc('\r', stream);
}
while (!serial_sendc(c));
}
char _serial_getc(FILE *stream) {
@dagon666
dagon666 / serial_stdio.c
Created October 8, 2013 20:50
Serial stdio example with libpca
#include "pca.h"
#include <util/delay.h>
int main(void)
{
int cnt = 0;
serial_init(E_BAUD_9600);
serial_install_interrupts(E_FLAGS_SERIAL_RX_INTERRUPT);
serial_flush();
@dagon666
dagon666 / slip.h
Last active December 25, 2015 08:59
libpca slip.h excerpt
/**
* @brief if there are no SEND/RECV routine definitions already existing
* use the default definitions provided bellow
*/
#if !defined(SLIP_CHAR_SEND) || !defined(SLIP_CHAR_RECV)
#include "config.h"
#include "serial.h"
/// redefine it, in your code to use different implementation of SEND routine