2011-06-09 18:48:44 +03:00
|
|
|
/*
|
|
|
|
* fw/board_app.c - Board-specific functions (for the application)
|
|
|
|
*
|
|
|
|
* Written 2011 by Werner Almesberger
|
|
|
|
* Copyright 2011 Werner Almesberger
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-06-20 21:49:33 +03:00
|
|
|
#include <stddef.h>
|
2011-06-09 18:48:44 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
2011-06-11 07:59:03 +03:00
|
|
|
#include <avr/interrupt.h>
|
2011-06-09 18:48:44 +03:00
|
|
|
|
|
|
|
#define F_CPU 8000000UL
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
2011-06-20 21:49:33 +03:00
|
|
|
#include "usb.h"
|
|
|
|
#include "at86rf230.h"
|
2011-06-09 18:48:44 +03:00
|
|
|
#include "spi.h"
|
2011-07-12 17:23:21 +03:00
|
|
|
#include "mac.h"
|
|
|
|
#include "board.h"
|
2011-06-09 18:48:44 +03:00
|
|
|
|
|
|
|
|
2011-06-11 07:59:03 +03:00
|
|
|
static volatile uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */
|
2011-06-09 18:48:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
void reset_cpu(void)
|
|
|
|
{
|
|
|
|
WDTCSR = 1 << WDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t read_irq(void)
|
|
|
|
{
|
|
|
|
return PIN(IRQ_RF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void slp_tr(void)
|
|
|
|
{
|
|
|
|
SET(SLP_TR);
|
|
|
|
CLR(SLP_TR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-11 07:59:03 +03:00
|
|
|
ISR(TIMER1_OVF_vect)
|
2011-06-09 18:48:44 +03:00
|
|
|
{
|
|
|
|
timer_h++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-11 17:06:18 +03:00
|
|
|
uint64_t timer_read(void)
|
2011-06-09 18:48:44 +03:00
|
|
|
{
|
|
|
|
uint32_t high;
|
|
|
|
uint8_t low, mid;
|
|
|
|
|
|
|
|
do {
|
2011-06-11 07:59:03 +03:00
|
|
|
if (TIFR1 & (1 << TOV1)) {
|
|
|
|
TIFR1 = 1 << TOV1;
|
|
|
|
timer_h++;
|
|
|
|
}
|
2011-06-09 18:48:44 +03:00
|
|
|
high = timer_h;
|
|
|
|
low = TCNT1L;
|
|
|
|
mid = TCNT1H;
|
|
|
|
}
|
|
|
|
while (TIFR1 & (1 << TOV1));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need all these casts because the intermediate results are handled
|
|
|
|
* as if they were signed and thus get sign-expanded. Sounds wrong-ish.
|
|
|
|
*/
|
|
|
|
return (uint64_t) high << 16 | (uint64_t) mid << 8 | (uint64_t) low;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-11 07:52:16 +03:00
|
|
|
void timer_init(void)
|
|
|
|
{
|
|
|
|
/* configure timer 1 as a free-running CLK counter */
|
|
|
|
|
|
|
|
TCCR1A = 0;
|
|
|
|
TCCR1B = 1 << CS10;
|
2011-06-11 07:59:03 +03:00
|
|
|
|
|
|
|
/* enable timer overflow interrupt */
|
|
|
|
|
|
|
|
TIMSK1 = 1 << TOIE1;
|
2011-06-11 07:52:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-09 18:48:44 +03:00
|
|
|
int gpio(uint8_t port, uint8_t data, uint8_t dir, uint8_t mask, uint8_t *res)
|
|
|
|
{
|
2011-06-21 05:11:09 +03:00
|
|
|
EIMSK = 0; /* recover INT_RF to ATUSB_GPIO_CLEANUP or an MCU reset */
|
2011-06-21 02:50:22 +03:00
|
|
|
|
2011-06-09 18:48:44 +03:00
|
|
|
switch (port) {
|
|
|
|
case 1:
|
|
|
|
DDRB = (DDRB & ~mask) | dir;
|
|
|
|
PORTB = (PORTB & ~mask) | data;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
DDRC = (DDRC & ~mask) | dir;
|
|
|
|
PORTC = (PORTC & ~mask) | data;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
DDRD = (DDRD & ~mask) | dir;
|
|
|
|
PORTD = (PORTD & ~mask) | data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disable the UART so that we can meddle with these pins as well. */
|
2011-06-09 20:02:26 +03:00
|
|
|
spi_off();
|
2011-06-09 18:48:44 +03:00
|
|
|
_delay_ms(1);
|
|
|
|
|
|
|
|
switch (port) {
|
|
|
|
case 1:
|
|
|
|
res[0] = PINB;
|
|
|
|
res[1] = PORTB;
|
|
|
|
res[2] = DDRB;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
res[0] = PINC;
|
|
|
|
res[1] = PORTC;
|
|
|
|
res[2] = DDRC;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
res[0] = PIND;
|
|
|
|
res[1] = PORTD;
|
|
|
|
res[2] = DDRD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-06-20 21:49:33 +03:00
|
|
|
|
|
|
|
|
2011-06-21 05:11:09 +03:00
|
|
|
void gpio_cleanup(void)
|
|
|
|
{
|
|
|
|
EIMSK = 1 << 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-23 19:03:34 +03:00
|
|
|
static void done(void *user)
|
2011-06-20 21:49:33 +03:00
|
|
|
{
|
2011-06-23 19:03:34 +03:00
|
|
|
led(0);
|
2011-06-23 15:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
atusb/fw/: added improved support for interrupt synchronization
At an interrupt barrier, the host must be able to ensure that no
interrupt generated before reaching the barrier is still pending and
will be delivered after crossing the barrier.
For this, we introduce the following concept:
- interrupts have a serial number. This number is sent to the host
on EP 1 (currently bulk) to signal the interrupt, instead of the
zero byte we used previously.
- the new request ATUSB_SPI_WRITE2_SYNC returns the interrupt
serial number from after the register write (the register write
itself is the interrupt barrier).
- the host can now check if the serial indicated from bulk and the
serial from ATUSB_SPI_WRITE2_SYNC are the same. If yes, interrupts
are synchronized. If not, it has to wait for the interrupt to be
signaled on EP 1.
We should also consider the case that the interrupt serial has gotten
ahead of ATUSB_SPI_WRITE2_SYNC. But that seems to happen rarely. In
any case, it's something for the host driver to worry about, not for
the firmware.
- board.h (irq_serial), board_app.c (irq_serial, INT0_vect): count
the interrupt serial number and return it when signaling the
interrupt
- include/atusb/ep0.h (ATUSB_SPI_WRITE2_SYNC), ep0.c (my_setup):
new request ATUSB_SPI_WRITE2_SYNC that does a register write, then
returns the interrupt serial
2011-07-07 21:51:07 +03:00
|
|
|
uint8_t irq_serial;
|
|
|
|
|
|
|
|
|
2011-06-23 15:41:40 +03:00
|
|
|
ISR(INT0_vect)
|
|
|
|
{
|
2011-07-12 17:23:21 +03:00
|
|
|
if (mac_irq) {
|
2011-07-12 18:03:12 +03:00
|
|
|
if (mac_irq())
|
|
|
|
return;
|
2011-07-12 17:23:21 +03:00
|
|
|
}
|
2011-06-23 19:03:34 +03:00
|
|
|
if (eps[1].state == EP_IDLE) {
|
|
|
|
led(1);
|
2011-07-12 10:40:03 +03:00
|
|
|
irq_serial = (irq_serial+1) | 0x80;
|
atusb/fw/: added improved support for interrupt synchronization
At an interrupt barrier, the host must be able to ensure that no
interrupt generated before reaching the barrier is still pending and
will be delivered after crossing the barrier.
For this, we introduce the following concept:
- interrupts have a serial number. This number is sent to the host
on EP 1 (currently bulk) to signal the interrupt, instead of the
zero byte we used previously.
- the new request ATUSB_SPI_WRITE2_SYNC returns the interrupt
serial number from after the register write (the register write
itself is the interrupt barrier).
- the host can now check if the serial indicated from bulk and the
serial from ATUSB_SPI_WRITE2_SYNC are the same. If yes, interrupts
are synchronized. If not, it has to wait for the interrupt to be
signaled on EP 1.
We should also consider the case that the interrupt serial has gotten
ahead of ATUSB_SPI_WRITE2_SYNC. But that seems to happen rarely. In
any case, it's something for the host driver to worry about, not for
the firmware.
- board.h (irq_serial), board_app.c (irq_serial, INT0_vect): count
the interrupt serial number and return it when signaling the
interrupt
- include/atusb/ep0.h (ATUSB_SPI_WRITE2_SYNC), ep0.c (my_setup):
new request ATUSB_SPI_WRITE2_SYNC that does a register write, then
returns the interrupt serial
2011-07-07 21:51:07 +03:00
|
|
|
usb_send(&eps[1], &irq_serial, 1, done, NULL);
|
2011-06-23 19:03:34 +03:00
|
|
|
}
|
2011-06-20 21:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void board_app_init(void)
|
|
|
|
{
|
|
|
|
/* enable INT0, trigger on rising edge */
|
|
|
|
EICRA = 1 << ISC01 | 1 << ISC00;
|
2011-06-21 02:50:22 +03:00
|
|
|
EIMSK = 1 << 0;
|
2011-06-20 21:49:33 +03:00
|
|
|
}
|