19 #ifndef HARDWARE_AVR_SPI_H 20 #define HARDWARE_AVR_SPI_H 53 uint16_t scale = F_CPU / frequency;
54 if (scale <= 2)
return (0b100);
55 if (scale <= 4)
return (0b000);
56 if (scale <= 8)
return (0b101);
57 if (scale <= 16)
return (0b001);
58 if (scale <= 32)
return (0b110);
59 if (scale <= 64)
return (0b111);
78 | (bitorder == LSBFIRST ? _BV(DORD) : 0)
79 | ((mode & 0x03) << CPHA)
81 SPSR = ((prescale >> 2) & 0x01);
91 __attribute__((always_inline))
94 __asm__ __volatile__(
"nop" :::
"memory");
95 loop_until_bit_is_set(SPSR, SPIF);
107 virtual void transfer(
void* dest,
const void* src,
size_t count)
109 if (count == 0 || dest == NULL || src == NULL)
return;
110 const uint8_t* sp = (
const uint8_t*) src;
111 uint8_t* dp = (uint8_t*) dest;
112 uint8_t value = *sp++;
116 __asm__ __volatile__(
"" :::
"memory");
117 loop_until_bit_is_set(SPSR, SPIF);
119 __asm__ __volatile__(
"" :::
"memory");
122 loop_until_bit_is_set(SPSR, SPIF);
132 virtual void read(
void* buf,
size_t count)
134 if (count == 0 || buf == NULL)
return;
135 uint8_t* bp = (uint8_t*) buf;
138 __asm__ __volatile__(
"nop" :::
"memory");
139 loop_until_bit_is_set(SPSR, SPIF);
141 __asm__ __volatile__(
"" :::
"memory");
144 loop_until_bit_is_set(SPSR, SPIF);
154 virtual void write(
const void* buf,
size_t count)
156 if (count == 0 || buf == NULL)
return;
157 uint8_t* bp = (uint8_t*) buf;
158 uint8_t value = *bp++;
162 __asm__ __volatile__(
"" :::
"memory");
163 loop_until_bit_is_set(SPSR, SPIF);
165 __asm__ __volatile__(
"" :::
"memory");
167 loop_until_bit_is_set(SPSR, SPIF);
217 m_sck.write(mode & 0x02);
220 m_usicr = (_BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC));
221 if (mode == 1 || mode == 2) m_usicr |= _BV(USICS0);
230 virtual uint8_t
transfer(uint8_t value)
234 register uint8_t cntl = m_usicr;
237 }
while ((USISR & _BV(USIOIF)) == 0);
243 GPIO<BOARD::SS>
m_ss;
246 GPIO<BOARD::SCK>
m_sck;
virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t prescale)
virtual void transfer(void *dest, const void *src, size_t count)
virtual uint8_t transfer(uint8_t value)
virtual uint8_t prescale(uint32_t frequency)
virtual void write(const void *buf, size_t count)
virtual void read(void *buf, size_t count)
GPIO< BOARD::MOSI > m_mosi
GPIO< BOARD::MISO > m_miso