Subversion Repositories Projects

Rev

Rev 471 | Rev 514 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 471 Rev 507
Line 22... Line 22...
22
#include <avr/interrupt.h>
22
#include <avr/interrupt.h>
23
#include "spi.h"
23
#include "spi.h"
24
#include "main.h"
24
#include "main.h"
Line 25... Line 25...
25
 
25
 
26
volatile uint16_t icnt = 0;
-
 
27
volatile unsigned char * iptr;
26
volatile uint16_t icnt = 0;
28
volatile unsigned char spi_cmd_buffer[8];
27
volatile uint8_t request_count = 0;
-
 
28
volatile uint8_t spi_ready = 1;
29
volatile uint8_t spi_ready = 1;
29
volatile union SPI_buffer_t SPI_buffer;
30
int16_t ampere = 0, max_ampere = 0;
30
int16_t ampere = 0, max_ampere = 0, s_volt;
Line 31... Line 31...
31
int32_t ampere_wasted = 0;
31
int32_t ampere_wasted = 0;
32
 
32
 
Line 51... Line 51...
51
 
51
 
52
/**
52
/**
53
 * SPI interrupt handler
53
 * SPI interrupt handler
54
 */
54
 */
-
 
55
ISR(SPI_STC_vect) {
-
 
56
        if (request_count == 0) {
-
 
57
                SPI_buffer.buffer.chk = SPDR; // firs char received is check byte from last transfer
55
ISR(SPI_STC_vect) {
58
        } else {
-
 
59
                SPI_buffer.buffer.c[request_count - 1] = SPDR; // safe received byte to buffer
56
    *iptr++ = SPDR; // safe received byte to buffer
60
        }
57
    icnt--; // dec length
61
        request_count++;
58
    if (icnt) {
62
    if (--icnt) {
59
        //SPDR = *iptr; // send next byte
63
        //SPDR = *iptr; // send next byte
60
        spi_ready = 1; // we _should_ send later because the slave needs more time
64
        spi_ready = 1; // we _should_ send later because the slave needs more time
61
    } else {
65
    } else {
62
        SPCR &= ~_BV(SPIE); // deactivate interrupt
66
        SPCR &= ~_BV(SPIE); // deactivate interrupt
Line 70... Line 74...
70
int TransferIsBusy(void) {
74
int TransferIsBusy(void) {
71
    return SPCR & _BV(SPIE);
75
    return SPCR & _BV(SPIE);
72
}
76
}
Line 73... Line 77...
73
 
77
 
74
/**
78
/**
75
 * start a new transfer of <data> with length <len>
79
 * start a new transfer with length <len>
76
 */
80
 */
77
void StartTransfer(unsigned char *data, uint16_t len) {
81
void StartTransfer(uint16_t len) {
Line -... Line 82...
-
 
82
    INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen
-
 
83
 
-
 
84
        // this is a new request
78
    INT0_LOW // /SS LOW ^= SS HIGH ^= slave should listen
85
        request_count = 0;
79
 
-
 
80
    // set up pointer and length for interrupt handler
86
 
Line 81... Line 87...
81
    iptr = data;
87
    // set up pointer and length for interrupt handler
82
    icnt = len;
88
    icnt = len;
-
 
89
 
-
 
90
    SPCR |= _BV(SPIE); // enable spi interrupt
-
 
91
        SPDR = 'A'; // start transfer by first command char
-
 
92
}
-
 
93
 
-
 
94
/**
-
 
95
 * send next command through spi
83
 
96
 */