Skip to content

Instantly share code, notes, and snippets.

@dagon666
dagon666 / host_slip.pl
Created October 12, 2013 21:54
SLIP communication over serial port with Arduino in Perl
#!/usr/bin/env perl
use strict;
use warnings;
$|++;
use Device::SerialPort;
use Digest::CRC qw/crc16/;
use Time::HiRes qw/usleep/;
@dagon666
dagon666 / arduino_slip.c
Created October 12, 2013 21:27
arduino_slip demonstration
#include "pca.h"
#include "../model.h"
#include "main.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
#define LED_DDRD DDRB
#define LED_PORT PORTB
#define LED_PIN PORTB0
@dagon666
dagon666 / messages.h
Last active December 25, 2015 09:08
binary message format
#include <stdint.h>
typedef enum _e_status {
ACK = 0x00,
NACK
} e_status;
struct timer_data {
uint16_t crc16;
@dagon666
dagon666 / soft_timer.h
Created October 12, 2013 15:34
software timer interface
#include <stdint.h>
// modify this to change the timer resolution
typedef uint8_t st_type;
/**
* @brief software timer callback handler type declaration
*
* @return void
*/
@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
@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 / 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_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 / 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 / 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;