6 #include <avr/interrupt.h>
28 #define SPI_EDGE POSITIVE
30 uint8_t tx_b
[32], rx_b
[32];
31 queue_t tx
= Q_INIT(tx_b
);
32 queue_t rx
= Q_INIT(rx_b
);
34 #define spi_isr_on() (USICR |= (1<<USIOIE))
35 #define spi_isr_off() (USICR &= (uint8_t)~(1<<USIOIE))
37 #ifdef SPI_IO_STANDARD
38 int spi_putc(char c
, FILE * stream
);
39 int spi_getc(FILE * stream
);
40 static FILE _spi_io
= FDEV_SETUP_STREAM(spi_putc
, spi_getc
,_FDEV_SETUP_RW
);
43 void spi_io_init(void) {
46 // Clr the flags we interupt on + counter bits in same reg.
47 // (default is not raised)
48 //USISR = (1<<USIOIF);
50 // Clr datareg to avoid junk data being sent out. (defaults to 0)
57 //#warning Using Port B (default)
58 //USIPP &= (uint8_t)~(1<<USIPOS);
66 DDR(USI_PORT
) |= (1<<I_DO
);
67 DDR(USI_PORT
) &= (uint8_t)~((1<<I_DI
)|(1<<I_CLK
));
68 PORT(USI_PORT
)&= (uint8_t)~( (1<<I_DI
) | (1<<I_CLK
) | (1<<I_DO
) );
70 USICR
= (0<<USISIE
) | // Start IE(2w), CLK edge IE(3w)
71 (1<<USIOIE
) | // Ovf interupt (to refil register)
72 (0<<USIWM1
) | (1<<USIWM0
) | // 01 = 3w, 00 = disable, 10&11 = 2w
73 (1<<USICS1
) | (SPI_EDGE
<<USICS0
) | // 10 = positive edge, 11 = neg.
74 (0<<USICLK
) | // 4bit timer : 0 = external, 1 = sw
75 (0<<USITC
); // Clock toggle
77 #ifdef SPI_IO_STANDARD
83 ISR( SIG_USI_OVERFLOW
) {
84 USISR
= (1<<USIOIF
); // Clear interupt flag and counter.
94 if (in
!= 0 && !q_full(&rx
)) {
100 /*// Alternate if we don't care about losing old data.
108 #ifdef SPI_IO_STANDARD
109 int spi_putc(char c
, FILE * stream
) {
112 //while(q_full(&tx));
116 r
= q_push(&tx
,(uint8_t)c
);
122 int spi_getc(FILE * stream
) {
125 //while(q_empty(&rx));
135 int spi_getchar(void) {
138 //while(q_empty(&rx));
147 void spi_puts(const char * string
) {
150 _spi_putchar(*string
);
156 void spi_o_puts(const char * string
) {
168 q_push_o(&tx
,*string
);
174 static inline uint8_t hex2ascii(uint8_t hex
) {
176 hex
= (uint8_t) (hex
+ '0');
178 return (uint8_t) (hex
+ 7); // 7 characters between nums and caps.
183 static inline void _spi_putchar(uint8_t ch
) {
184 // expects spi interupt disabled on entry, leaves it disabled on exit.
196 void spi_putchar(uint8_t ch
) {
202 void spi_puth(uint8_t hex
) {
205 _spi_putchar( hex2ascii( (hex
>>4 ) ) );
206 _spi_putchar( hex2ascii( (hex
>>0 ) ) );
211 void spi_puth2(uint16_t hex
) {
214 _spi_putchar( hex2ascii( (uint8_t)(hex
>>12) ) );
215 _spi_putchar( hex2ascii( (uint8_t)(hex
>>8 ) ) );
216 _spi_putchar( hex2ascii( (uint8_t)(hex
>>4 ) ) );
217 _spi_putchar( hex2ascii( (uint8_t)(hex
>>0 ) ) );