summaryrefslogtreecommitdiff
path: root/software/hhd70dongle/hhd70.c
blob: 359d754bb8cf6716d96ae38827611ffbd83d8d6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *
 *  mur.sat
 *
 *  Somewhen in the year 2012, mur.at will have a nano satellite launched
 *  into a low earth orbit (310 km above the surface of our planet). The
 *  satellite itself is a TubeSat personal satellite kit, developed and
 *  launched by interorbital systems. mur.sat is a joint venture of mur.at,
 *  ESC im Labor and realraum.
 *
 *  Please visit the project hompage at sat.mur.at for further information.
 *
 *
 *  Copyright (C) 2012 Bernhard Tittelbach <xro@realraum.at>
 *
 *  This file is part of mur.sat.
 *
 *  mur.sat 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 3 of the License, or
 *  any later version.
 *
 *  mur.sat is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with mur.sat. If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "avr/io.h"

#include "hhd70.h"

void hhd70_init(void)
{
    //configure Direction of SS / PB0 , MOSI and SCLK as Output to drive CS of CC1101
    SPI_DDR = (1<<MOSI)|(1<<SCK)|(1<<CS)|(1<<RE)|(1<<TE);
    SPI_PORT = (1<<CS) | (1<<RE) | (1<<TE);        
    SPCR = (1<<SPE)|(1<<MSTR); // | (0<<DORD)  //select MSB first: DORD == 0
    // SPSR = (0<<SPI2X) // f_osc/4
    // SPSR = (1<<SPI2X) // f_osc/2
    //  SPSR = (1<<SPI2X); (4MHz vs. 8MHz)
}

void hhd70_spi_cs_enable(void)
{
    //pull low
    SPI_PORT &= ~(1<<CS);
}

void hhd70_spi_cs_disable(void)
{
    //pull high
    SPI_PORT |= (1<<CS);
}

#include "usb_rawhid.h"
#include "util.h"

void hhd70_c1101_wait_chip_rdy(void)
{
    //c1101 will set MISO to low if ready
    while (SPI_PINB_REG & (1<<MISO));
    //~ unsigned int c;
    //~ for (c=0; c < 0xFFFFFFFF && (SPI_PINB_REG & (1<<MISO)); c++);
    //~ uint8_t debug_buff[6];
    //~ usb_rawhid_send((uint8_t*)"spi waited for:",255);
    //~ debug_sprint_int16hex(debug_buff, c);
    //~ usb_rawhid_send(debug_buff,255);
}

void hhd70_spi_write_byte(char byte)
{
    SPDR = byte;					//Load byte to Data register
    while(!(SPSR & (1<<SPIF))); 	// Wait for transmission complete
}

char hhd70_spi_exchange_byte(char byte)
{
    hhd70_spi_write_byte(byte);
    return SPDR;
}

char hhd70_spi_read_byte(void)
{
    //transmit something so SCLK runs for 8 bits, so that slave can transfer 1 byte
    return hhd70_spi_exchange_byte(0);
}

void hhd70_palna_txmode(void)
{
    SPI_PORT |= (1<<RE);
    SPI_PORT &= ~(1<<TE);
}

void hhd70_palna_rxmode(void)
{
    SPI_PORT |= (1<<TE);
    SPI_PORT &= ~(1<<RE);
}

void hhd70_palna_off(void)
{
    SPI_PORT |= (1<<RE) | (1<<TE);
}

int8_t hhd70_rx_data_available(void)
{
    return SPI_PINB_REG & (1 << GDO2);
}