Ajout schema. init spi

This commit is contained in:
arnaud.houdelette 2019-09-06 17:14:54 +02:00
parent 410d7b7395
commit d6fa89ab4e
4 changed files with 29 additions and 74 deletions

View file

@ -54,7 +54,7 @@ RESETPORT = /dev/ttyU0
TARGET = pyrorf TARGET = pyrorf
SRC = main.c lcd.c SRC = main.c lcd.c spi.c
CXXSRC = CXXSRC =
ASRC = i2cmaster.S ASRC = i2cmaster.S
MCU = atmega2560 MCU = atmega2560

BIN
schema.pdf Normal file

Binary file not shown.

77
spi.c
View file

@ -2,78 +2,21 @@
#include <util/delay.h> #include <util/delay.h>
void spi_begin() {
#if defined(SPCR) void spi_init_master() {
#error to write ...
// Set SS to high so a connected chip will be "deselected" by default SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0);
digitalWrite(SS, HIGH);
// When the SS pin is set as OUTPUT, it can be used as SPI_PORT |= _BV(SCK_PIN);
// a general purpose output port (it doesn't influence SPI_DDR |= _BV(MOSI_PIN) | _BV(SCK_PIN) | _BV(SS_PIN);
// SPI operations).
pinMode(SS, OUTPUT);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
#else
PRR &= ~(_BV(PRUSI));
USICR = _BV(USIWM0);
USCK_DDR |= _BV(USCK_BIT); //set the USCK pin as output
DO_DDR |= _BV(DO_BIT); //set the DO pin as output
DI_DDR &= ~_BV(DI_BIT); //set the DI pin as input
#endif
} }
uint8_t spi_transfer(uint8_t b) { uint8_t spi_transfer(uint8_t b) {
#if defined(SPCR)
SPDR = b; SPDR = b;
while (!(SPSR & _BV(SPIF))); while (!(SPSR & _BV(SPIF)));
return SPDR; return SPDR;
#else
USIDR = b;
USISR = _BV(USIOIF);
do {
USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC);
// _delay_ms(1);
}while ((USISR & _BV(USIOIF)) == 0);
return USIDR;
#endif
}
void spi_end() {
#if defined(SPCR)
SPCR &= ~_BV(SPE);
#else
USICR &= ~(_BV(USIWM1) | _BV(USIWM0));
#endif
}
void spi_setBitOrder(uint8_t bitOrder)
{
#if defined(SPCR)
if(bitOrder == LSBFIRST) {
SPCR |= _BV(DORD);
} else {
SPCR &= ~(_BV(DORD));
}
#endif
}
void spi_setDataMode(uint8_t mode)
{
#if defined(SPCR)
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
#else
#endif
} }

24
spi.h
View file

@ -4,20 +4,32 @@
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
#include <util/atomic.h> #include <util/atomic.h>
#include "defines.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C"{ extern "C"{
#endif #endif
#if defined (__AVR_ATmega32__)
//SPI data modes #elif defined (__AVR_ATmega2560__)
#define SPI_MODE0 0x00 #define SPI_PORT PORTB
#define SPI_MODE1 0x04 #define SPI_DDR DDRB
#define SS_PIN PB0
#define SCK_PIN PB1
#define MOSI_PIN PB2
#define MISO_PIN PB3
#endif
void spi_init_master(void);
void spi_init_slave(void);
void spi_begin(void);
void spi_setDataMode(uint8_t spiDataMode);
uint8_t spi_transfer(uint8_t spiData); uint8_t spi_transfer(uint8_t spiData);
void spi_end(void);
#ifdef __cplusplus #ifdef __cplusplus