2010-07-07 15:32:02 +00:00
|
|
|
//
|
|
|
|
// basiciotest.c : test code for the io and buffer ops of the UART and SPI ports
|
|
|
|
//
|
|
|
|
// Copyright (c) 2010 flukso.net
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
2010-07-07 15:32:02 +00:00
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
#include "uart.h"
|
2010-07-07 15:32:02 +00:00
|
|
|
#include "spi.h"
|
|
|
|
#include "ctrl.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define NO_OP_1 1
|
|
|
|
#define NO_OP_2 2
|
2010-10-16 19:39:35 +00:00
|
|
|
#define START_TX 4
|
|
|
|
#define TRANSMIT 8
|
|
|
|
#define HIGH_HEX 16
|
|
|
|
#define TO_FROM_UART 32
|
|
|
|
#define NEW_CTRL_MSG 64
|
2010-07-07 15:32:02 +00:00
|
|
|
|
|
|
|
#define SPI_END_OF_TX 0x00
|
2010-10-16 19:39:35 +00:00
|
|
|
#define SPI_END_OF_MESSAGE '.'
|
2010-07-07 15:32:02 +00:00
|
|
|
#define SPI_FORWARD_TO_UART_PORT 'u'
|
2010-08-07 21:29:15 +00:00
|
|
|
#define SPI_FORWARD_TO_CTRL_PORT 'l' // 'l'ocal port
|
2010-07-07 15:32:02 +00:00
|
|
|
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
volatile uint8_t spi_status, high_hex;
|
2010-07-07 15:32:02 +00:00
|
|
|
|
2010-08-07 21:29:15 +00:00
|
|
|
// hex to binary/byte decoding
|
2010-10-16 19:39:35 +00:00
|
|
|
uint8_t htob(uint16_t hex)
|
|
|
|
{
|
2010-07-07 15:32:02 +00:00
|
|
|
uint8_t low_hex = (uint8_t) hex;
|
|
|
|
uint8_t high_hex = (uint8_t) (hex >> 8);
|
|
|
|
uint8_t byte;
|
|
|
|
|
|
|
|
byte = (high_hex > 0x40) ? (high_hex & 0x0F) + 9 : high_hex & 0x0F;
|
|
|
|
byte = byte << 4;
|
|
|
|
byte |= (low_hex > 0x40) ? (low_hex & 0x0F) + 9 : low_hex & 0x0F;
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2010-08-07 21:29:15 +00:00
|
|
|
// binary/byte to hex encoding
|
2010-10-16 19:39:35 +00:00
|
|
|
uint16_t btoh(uint8_t byte)
|
|
|
|
{
|
2010-07-07 15:32:02 +00:00
|
|
|
uint8_t low_nibble = (byte & 0x0F);
|
|
|
|
uint8_t high_nibble = (byte & 0xF0) >> 4;
|
|
|
|
uint16_t hex;
|
|
|
|
|
|
|
|
hex = (high_nibble > 0x09) ? high_nibble - 9 + 0x60 : high_nibble + 0x30;
|
|
|
|
hex = hex << 8;
|
|
|
|
hex |= (low_nibble > 0x09) ? low_nibble - 9 + 0x60 : low_nibble + 0x30;
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
ISR(SPI_STC_vect)
|
|
|
|
{
|
|
|
|
uint8_t spi_rx, rx, tx;
|
|
|
|
uint16_t spi_tx;
|
2010-07-07 15:32:02 +00:00
|
|
|
|
2010-08-07 21:29:15 +00:00
|
|
|
// the SPI is double-buffered, requiring two NO_OPs when switching from Tx to Rx
|
2010-07-07 15:32:02 +00:00
|
|
|
if (spi_status & (NO_OP_1 | NO_OP_2)) {
|
|
|
|
spi_status--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
// do we have to transmit the first byte?
|
|
|
|
if (spi_status & START_TX) {
|
|
|
|
received_from_spi(SPI_FORWARD_TO_CTRL_PORT);
|
|
|
|
spi_status &= ~START_TX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-07 21:29:15 +00:00
|
|
|
// are we in Tx mode?
|
2010-07-07 15:32:02 +00:00
|
|
|
if (spi_status & TRANSMIT) {
|
2010-10-16 19:39:35 +00:00
|
|
|
if (spi_status & HIGH_HEX) {
|
|
|
|
received_from_spi(high_hex);
|
|
|
|
spi_status &= ~HIGH_HEX;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-07 15:32:02 +00:00
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
if (spi_status & TO_FROM_UART) {
|
|
|
|
if (!uartReceiveByte(&tx)) {
|
|
|
|
received_from_spi(SPI_END_OF_TX);
|
|
|
|
spi_status &= ~TRANSMIT;
|
|
|
|
spi_status |= NO_OP_2;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-10-16 19:39:35 +00:00
|
|
|
if (ctrlGetFromTxBuffer(&tx)) {
|
|
|
|
if (tx == SPI_END_OF_MESSAGE) {
|
|
|
|
received_from_spi(tx);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-10-16 19:39:35 +00:00
|
|
|
received_from_spi(SPI_FORWARD_TO_UART_PORT);
|
|
|
|
spi_status |= TO_FROM_UART;
|
|
|
|
return;
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
spi_tx = btoh(tx);
|
|
|
|
high_hex = (uint8_t)spi_tx;
|
|
|
|
spi_status |= HIGH_HEX;
|
|
|
|
received_from_spi((uint8_t)(spi_tx >> 8));
|
2010-07-07 15:32:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-07 21:29:15 +00:00
|
|
|
// we're in Rx mode
|
2010-07-07 15:32:02 +00:00
|
|
|
switch (spi_rx = received_from_spi(0x00)) {
|
|
|
|
case SPI_END_OF_TX:
|
2010-10-16 19:39:35 +00:00
|
|
|
spi_status |= TRANSMIT | START_TX;
|
2010-07-07 15:32:02 +00:00
|
|
|
spi_status &= ~(HIGH_HEX | TO_FROM_UART);
|
|
|
|
break;
|
|
|
|
case SPI_END_OF_MESSAGE:
|
2010-10-16 19:39:35 +00:00
|
|
|
if (!(spi_status & TO_FROM_UART)) {
|
2010-07-07 15:32:02 +00:00
|
|
|
ctrlAddToRxBuffer(spi_rx);
|
|
|
|
spi_status |= NEW_CTRL_MSG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPI_FORWARD_TO_UART_PORT:
|
|
|
|
spi_status |= TO_FROM_UART;
|
|
|
|
break;
|
2010-08-07 21:29:15 +00:00
|
|
|
case SPI_FORWARD_TO_CTRL_PORT:
|
|
|
|
spi_status &= ~TO_FROM_UART;
|
|
|
|
break;
|
2010-07-07 15:32:02 +00:00
|
|
|
default:
|
2010-10-16 19:39:35 +00:00
|
|
|
if (spi_status & HIGH_HEX) {
|
|
|
|
rx = htob(((uint16_t)high_hex << 8) + spi_rx);
|
|
|
|
|
|
|
|
if (spi_status & TO_FROM_UART) {
|
|
|
|
uartAddToTxBuffer(rx);
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-10-16 19:39:35 +00:00
|
|
|
ctrlAddToRxBuffer(rx);
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-10-16 19:39:35 +00:00
|
|
|
high_hex = spi_rx;
|
|
|
|
}
|
|
|
|
// toggle the HEX bit in spi_status
|
|
|
|
spi_status ^= HIGH_HEX;
|
2010-07-07 15:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-16 19:39:35 +00:00
|
|
|
ISR(TIMER1_COMPA_vect)
|
|
|
|
{
|
|
|
|
/* void */
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
// Configure PD5=DE as output pin with low as default
|
|
|
|
DDRD |= (1<<DDD5);
|
|
|
|
|
|
|
|
// Timer1 clock prescaler set to 1 => fTOV1 = 3686.4kHz / 65536 = 56.25Hz (DS p.134)
|
|
|
|
TCCR1B |= (1<<CS10);
|
|
|
|
// Increase sampling frequency to 2kHz (= 667Hz per channel) with an error of 0.01% (DS p.122)
|
|
|
|
OCR1A = 0x0732;
|
|
|
|
// Timer1 set to CTC mode (DS p.133)
|
|
|
|
TCCR1B |= 1<<WGM12;
|
|
|
|
// Enable output compare match interrupt for timer1 (DS p.136)
|
|
|
|
TIMSK1 |= (1<<OCIE1A);
|
|
|
|
#if DBG > 0
|
|
|
|
// Set PB1=OC1A as output pin
|
|
|
|
DDRB |= (1<<DDB1);
|
|
|
|
// Toggle pin OC1A=PB1 on compare match
|
|
|
|
TCCR1A |= 1<<COM1A0;
|
|
|
|
#endif
|
|
|
|
|
2010-07-07 15:32:02 +00:00
|
|
|
// initialize the CTRL buffers
|
|
|
|
ctrlInit();
|
2010-10-16 19:39:35 +00:00
|
|
|
// initialize the UART hardware and buffers
|
2010-07-07 15:32:02 +00:00
|
|
|
uartInit();
|
|
|
|
// initialize the SPI in slave mode
|
|
|
|
setup_spi(SPI_MODE_0, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
|
|
|
|
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (spi_status & NEW_CTRL_MSG) {
|
2010-10-16 19:39:35 +00:00
|
|
|
ctrlRxToTxLoop();
|
2010-07-07 15:32:02 +00:00
|
|
|
spi_status &= ~NEW_CTRL_MSG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|