cosmetics

This commit is contained in:
midelic 2016-09-19 21:21:24 +01:00 committed by GitHub
parent 62f96c3105
commit a5c1b2e5d2

View File

@ -1,137 +1,144 @@
/*SPI routines for STM32 /*SPI routines for STM32
based on arduino maple library based on arduino maple library
By Midelic By Midelic
on RCGroups on RCGroups
*/ */
#ifdef STM32_board #ifdef STM32_board
SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port SPIClass SPI_2(2); //Create an instance of the SPI Class called SPI_2 that uses the 2nd SPI Port
void initSPI2() { void initSPI2() {
//SPI_DISABLE(); //SPI_DISABLE();
SPI_2.end(); SPI_2.end();
SPI2_BASE->CR1 &= ~SPI_CR1_DFF_8_BIT;//8 bits format This bit should be written only when SPI is disabled (SPE = ?0?) for correct operation. SPI2_BASE->CR1 &= ~SPI_CR1_DFF_8_BIT;//8 bits format This bit should be written only when SPI is disabled (SPE = ?0?) for correct operation.
SPI_2.begin(); //Initialize the SPI_2 port. SPI_2.begin(); //Initialize the SPI_2 port.
SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order SPI_2.setBitOrder(MSBFIRST); // Set the SPI_2 bit order
SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0 SPI_2.setDataMode(SPI_MODE0); //Set the SPI_2 data mode 0
SPI_2.setClockDivider(SPI_CLOCK_DIV8); //// speed (36 / 8 = 4.5 MHz SPI_2 speed) SPI_2.setClockDivider(SPI_CLOCK_DIV8); //// speed (36 / 8 = 4.5 MHz SPI_2 speed)
} }
#endif #endif
#if defined STM32_board #if defined STM32_board
void spi_write(uint8_t command){//working OK void spi_write(uint8_t command){//working OK
SPI2_BASE->DR = command;//Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)." SPI2_BASE->DR = command;//Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
while (!(SPI2_BASE->SR & SPI_SR_RXNE)); while (!(SPI2_BASE->SR & SPI_SR_RXNE));
command = SPI2_BASE->DR;// "... and read the last received data." command = SPI2_BASE->DR;// "... and read the last received data."
} }
uint8_t spi_read(void) { uint8_t spi_read(void) {
spi_write(0x00); spi_write(0x00);
return SPI2_BASE->DR; return SPI2_BASE->DR;
} }
uint8_t spi_Read(){ uint8_t spi_Read(){
uint8_t rx=0; uint8_t rx=0;
while(!(SPI2_BASE->SR & SPI_SR_TXE)); while(!(SPI2_BASE->SR & SPI_SR_TXE));
while((SPI2_BASE->SR & SPI_SR_BSY)); while((SPI2_BASE->SR & SPI_SR_BSY));
// //
SPI_DISABLE(); SPI_DISABLE();
SPI_SET_BIDIRECTIONAL(); SPI_SET_BIDIRECTIONAL();
volatile uint8_t x = SPI2_BASE->DR; volatile uint8_t x = SPI2_BASE->DR;
(void)x; (void)x;
SPI_ENABLE(); SPI_ENABLE();
// //
SPI_DISABLE(); SPI_DISABLE();
while(!(SPI2_BASE->SR& SPI_SR_RXNE)); while(!(SPI2_BASE->SR& SPI_SR_RXNE));
rx=SPI2_BASE->DR; rx=SPI2_BASE->DR;
SPI_SET_UNIDIRECTIONAL(); SPI_SET_UNIDIRECTIONAL();
SPI_ENABLE(); SPI_ENABLE();
return rx; return rx;
} }
void SPI_ENABLE(){ void SPI_ENABLE(){
SPI2_BASE->CR1 |= SPI_CR1_SPE; SPI2_BASE->CR1 |= SPI_CR1_SPE;
} }
void SPI_DISABLE(){ void SPI_DISABLE(){
SPI2_BASE->CR1 &= ~SPI_CR1_SPE; SPI2_BASE->CR1 &= ~SPI_CR1_SPE;
} }
void SPI_SET_BIDIRECTIONAL() void SPI_SET_BIDIRECTIONAL()
{ {
SPI2_BASE->CR1 |= SPI_CR1_BIDIMODE; SPI2_BASE->CR1 |= SPI_CR1_BIDIMODE;
SPI2_BASE->CR1 &= ~ SPI_CR1_BIDIOE;//receive only SPI2_BASE->CR1 &= ~ SPI_CR1_BIDIOE;//receive only
} }
void SPI_SET_UNIDIRECTIONAL() void SPI_SET_UNIDIRECTIONAL()
{ {
SPI2_BASE->CR1 &= ~SPI_CR1_BIDIMODE; SPI2_BASE->CR1 &= ~SPI_CR1_BIDIMODE;
} }
#else #else
void spi_write(uint8_t command) void spi_write(uint8_t command)
{ {
uint8_t n=8; uint8_t n=8;
SCK_off;//SCK start low SCK_off;//SCK start low
SDI_off; XNOP();
do SDI_off;
{ XNOP();
if(command&0x80) do
SDI_on; {
else if(command&0x80)
SDI_off; SDI_on;
XNOP() ; else
SCK_on; SDI_off;
NOP(); XNOP() ;
XNOP() ; SCK_on;
XNOP() ; NOP();
SCK_off; XNOP() ;
command = command << 1; XNOP() ;
} command = command << 1;
while(n--); SCK_off;
SDI_on; XNOP() ;
} }
while(n--);
uint8_t spi_Read(void) { SDI_on;
uint8_t result=0; }
uint8_t i;
uint8_t spi_Read(void) {
SDI_SET_INPUT; uint8_t result=0;
for(i=0;i<8;i++) { uint8_t i;
result<<=1;
if(SDI_1) ///if SDIO =1 SDI_SET_INPUT;
result |= 0x01; for(i=0;i<8;i++) {
SCK_on; result<<=1;
NOP(); if(SDI_1) ///if SDIO =1
SCK_off; result |= 0x01;
NOP(); SCK_on;
} NOP();
SDI_SET_OUTPUT; SCK_off;
return result; NOP();
} }
SDI_SET_OUTPUT;
return result;
uint8_t spi_read() }
{
uint8_t result=0,i;
for(i=0;i<8;i++) uint8_t spi_read()
{ {
result<<=1; uint8_t result=0,i;
if(SDO_1) /// for(i=0;i<8;i++)
result|=0x01; {
SCK_on; result<<=1;
NOP(); if(SDO_1) ///
SCK_off; result|=0x01;
} SCK_on;
return result; XNOP();
} XNOP();
NOP();
SCK_off;
XNOP();
XNOP();
}
return result;
}
#endif #endif