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