Subversion Repositories Projects

Rev

Rev 514 | Rev 761 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
471 cascade 1
/****************************************************************************
728 cascade 2
 *   Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje               *
471 cascade 3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
7
 *   it under the terms of the GNU General Public License as published by   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 ****************************************************************************/
20
 
21
#include <avr/io.h>
22
#include <avr/interrupt.h>
23
#include "spi.h"
24
#include "main.h"
25
 
514 cascade 26
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
27
 
471 cascade 28
volatile uint16_t icnt = 0;
507 cascade 29
volatile uint8_t request_count = 0;
471 cascade 30
volatile uint8_t spi_ready = 1;
507 cascade 31
volatile union SPI_buffer_t SPI_buffer;
32
int16_t ampere = 0, max_ampere = 0, s_volt;
471 cascade 33
int32_t ampere_wasted = 0;
34
 
35
#define INT0_HIGH                       PORTD |=  (1 << PD2);
36
#define INT0_LOW                        PORTD &= ~(1 << PD2);
37
 
38
/**
39
 * init the SPI as master
40
 */
41
void SpiMasterInit(void) {
42
    volatile char IOReg;
43
    // set PB4(/SS), PB5(MOSI), PB7(SCK) as output
44
    DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7);
45
    PORTB |= (1 << PB4); // pullup SS
46
    // enable SPI Interrupt and SPI in Master Mode with SCK = CK/128
47
    SPCR = (1 << SPIE) | (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1);
48
    IOReg = SPSR; // clear SPIF bit in SPSR
49
    IOReg = SPDR;
50
    //sei(); // we do it later
51
}
52
 
53
 
54
/**
55
 * SPI interrupt handler
56
 */
57
ISR(SPI_STC_vect) {
507 cascade 58
        if (request_count == 0) {
59
                SPI_buffer.buffer.chk = SPDR; // firs char received is check byte from last transfer
60
        } else {
61
                SPI_buffer.buffer.c[request_count - 1] = SPDR; // safe received byte to buffer
62
        }
63
        request_count++;
64
    if (--icnt) {
471 cascade 65
        //SPDR = *iptr; // send next byte
66
        spi_ready = 1; // we _should_ send later because the slave needs more time
67
    } else {
68
        SPCR &= ~_BV(SPIE); // deactivate interrupt
69
        INT0_HIGH // transfer is done, slave does not need to listen
70
    }
71
}
72
 
73
/**
74
 * check if SPI transfer is still busy
75
 */
76
int TransferIsBusy(void) {
77
    return SPCR & _BV(SPIE);
78
}
79
 
80
/**
507 cascade 81
 * start a new transfer with length <len>
471 cascade 82
 */
507 cascade 83
void StartTransfer(uint16_t len) {
471 cascade 84
    INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen
85
 
507 cascade 86
        // this is a new request
87
        request_count = 0;
88
 
471 cascade 89
    // set up pointer and length for interrupt handler
90
    icnt = len;
91
 
92
    SPCR |= _BV(SPIE); // enable spi interrupt
507 cascade 93
        SPDR = 'A'; // start transfer by first command char
471 cascade 94
}
507 cascade 95
 
96
/**
97
 * send next command through spi
98
 */
99
void spi_send_next() {
100
        SPDR = 'A' + request_count;
101
}
514 cascade 102
 
103
#endif