2011-02-14 17:48:42 +02:00
|
|
|
/*
|
|
|
|
* fw/spi.c - ATmega8 family SPI I/O
|
|
|
|
*
|
2013-03-30 00:14:11 +02:00
|
|
|
* Written 2011, 2013 by Werner Almesberger
|
|
|
|
* Copyright 2011, 2013 Werner Almesberger
|
2011-02-14 17:48:42 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-03-30 00:14:11 +02:00
|
|
|
#include <stdbool.h>
|
2011-02-14 04:46:36 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "spi.h"
|
|
|
|
|
|
|
|
|
2013-03-30 00:14:11 +02:00
|
|
|
static bool spi_initialized = 0;
|
2011-06-09 20:02:26 +03:00
|
|
|
|
|
|
|
|
2011-02-14 04:46:36 +02:00
|
|
|
void spi_begin(void)
|
|
|
|
{
|
2011-06-09 20:02:26 +03:00
|
|
|
if (!spi_initialized)
|
|
|
|
spi_init();
|
2011-02-14 04:46:36 +02:00
|
|
|
CLR(nSS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t spi_io(uint8_t v)
|
|
|
|
{
|
|
|
|
// while (!(UCSR1A & 1 << UDRE1));
|
2015-05-24 15:37:38 +03:00
|
|
|
SPI_DATA = v;
|
|
|
|
SPI_WAIT_DONE();
|
2016-03-18 23:25:24 +02:00
|
|
|
return SPI_DATA;
|
2011-02-14 04:46:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void spi_end(void)
|
|
|
|
{
|
|
|
|
// while (!(UCSR1A & 1 << TXC1));
|
|
|
|
SET(nSS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-31 01:46:55 +03:00
|
|
|
void spi_recv_block(uint8_t *buf, uint8_t n)
|
|
|
|
{
|
|
|
|
if (!n)
|
|
|
|
return;
|
2015-05-24 15:37:38 +03:00
|
|
|
SPI_DATA = 0;
|
2013-07-31 01:46:55 +03:00
|
|
|
while (--n) {
|
2015-05-24 15:37:38 +03:00
|
|
|
SPI_WAIT_DONE();
|
|
|
|
*buf++ = SPI_DATA;
|
|
|
|
SPI_DATA = 0;
|
2013-07-31 01:46:55 +03:00
|
|
|
}
|
2015-05-24 15:37:38 +03:00
|
|
|
SPI_WAIT_DONE();
|
|
|
|
*buf++ = SPI_DATA;
|
2013-07-31 01:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-09 20:02:26 +03:00
|
|
|
void spi_off(void)
|
|
|
|
{
|
|
|
|
spi_initialized = 0;
|
2015-05-24 15:37:38 +03:00
|
|
|
#ifdef ATUSB
|
|
|
|
UCSR1B = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef RZUSB
|
|
|
|
SPCR &= ~(1 << SPE);
|
|
|
|
#endif
|
2011-06-09 20:02:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-14 04:46:36 +02:00
|
|
|
void spi_init(void)
|
|
|
|
{
|
|
|
|
SET(nSS);
|
|
|
|
OUT(SCLK);
|
|
|
|
OUT(MOSI);
|
|
|
|
OUT(nSS);
|
|
|
|
IN(MISO);
|
|
|
|
|
2015-05-24 15:37:38 +03:00
|
|
|
#ifdef ATUSB
|
2011-02-14 04:46:36 +02:00
|
|
|
UBRR1 = 0; /* set bit rate to zero to begin */
|
|
|
|
UCSR1C = 1 << UMSEL11 | 1 << UMSEL10;
|
|
|
|
/* set MSPI, MSB first, SPI data mode 0 */
|
|
|
|
UCSR1B = 1 << RXEN1 | 1 << TXEN1;
|
|
|
|
/* enable receiver and transmitter */
|
|
|
|
UBRR1 = 0; /* reconfirm the bit rate */
|
2015-05-24 15:37:38 +03:00
|
|
|
#endif
|
|
|
|
#ifdef RZUSB
|
|
|
|
SPCR = (1 << SPE) | (1 << MSTR);
|
|
|
|
SPSR = (1 << SPI2X);
|
|
|
|
#endif
|
2011-06-09 20:02:26 +03:00
|
|
|
|
|
|
|
spi_initialized = 1;
|
2011-02-14 04:46:36 +02:00
|
|
|
}
|