ser2neo/serial.h

73 lines
1.9 KiB
C
Raw Permalink Normal View History

// Half-duplex interrupt based serial port.
//
2015-12-31 10:20:50 +01:00
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
2015-10-25 22:06:31 +01:00
#pragma once
#include <stdint.h>
2015-12-31 10:34:34 +01:00
/// Half-duplex interrupt based serial port.
///
/// Loosely based on AVR304.
///
2015-10-25 22:06:31 +01:00
class Serial {
public:
void init();
void putch(uint8_t ch);
void putstr(const char* str);
2015-12-31 10:20:50 +01:00
2015-10-25 22:06:31 +01:00
uint8_t getch();
private:
enum State : uint8_t {
2015-10-25 22:06:31 +01:00
Idle,
Receive,
Transmit,
};
// Note that these are all specific to an ATTINY85 USB board.
2015-10-25 22:06:31 +01:00
static const int RxPin = 0;
static const int TxPin = 2;
2015-10-25 22:06:31 +01:00
static const auto Prescale = 2;
static const auto Baud = 57600;
static const auto BitTime = F_CPU / Prescale / Baud - 1;
// Total number of clocks taken to service the pin change
// interrupt and start the receive. Use this to reduce the first
// bit-and-a-half time so sampling occurs half way through the
// first bit.
2015-10-25 22:06:31 +01:00
static const uint8_t PCIntDelay = 140;
void pcint0();
void timer1ovf();
volatile State state_;
uint8_t rxing_;
// Remaining number of bits to send or receive.
uint8_t bits_;
2015-10-25 22:06:31 +01:00
volatile uint8_t rxed_;
volatile bool rx_full_;
2015-12-31 10:20:50 +01:00
2015-10-25 22:06:31 +01:00
volatile uint16_t txing_;
static Serial* instance_;
public:
// Interrupt handler entry points.
2015-10-25 22:06:31 +01:00
static void pcint0_bounce();
static void timer1ovf_bounce();
};