初级会员
- 积分
- 53
- 金钱
- 53
- 注册时间
- 2013-4-19
- 在线时间
- 0 小时
|
楼主 |
发表于 2013-11-26 15:01:34
|
显示全部楼层
/* ============================================================ */
/*
* SPI operation
*
* */
static void spi2_init(void)
{
RCC->APB2ENR |= ( 1 << 3 ); // enable GPIOB clock
RCC->APB1ENR |= ( 1 << 14 ); // enable SPI2 clock
// config GPIOB B12,13,14,15
GPIOB->CRH &= ~( (unsigned long)0xffff << 16 );
GPIOB->CRH |= ( (unsigned long)0xbbb3 << 16 );
GPIOB->ODR |= ( 0xf << 12 );
// config SPI
SPI2->CR1 &= ~( 1 << 0 ); // CPHA == 0
SPI2->CR1 &= ~( 1 << 1 ); // CPOL == 0
SPI2->CR1 |= ( 1 << 2 ); // be master
SPI2->CR1 &= ~( 7 << 3 );
SPI2->CR1 |= ( 7 << 3 ); // CLK / 4
SPI2->CR1 &= ~( 1 << 7 ); // MSB first , Fsck = Fcpu / 256
SPI2->CR1 |= ( 1 << 8 ); // SSI
SPI2->CR1 |= ( 1 << 9 ); // SSM
SPI2->CR1 &= ~( 1 << 10); // full duplex
SPI2->CR1 &= ~( 1 << 11); // 8 bits format
SPI2->CR1 |= ( 1 << 6 ); // enable SPI2
spi2_read_writ_byte(0xff);
}
/* ============================================================ */
unsigned char spi2_read_writ_byte( unsigned char tx_data )
{
unsigned char retry = 0 ;
while( ( SPI2->SR & ( 1 << 1 ) ) == 0 )
{
retry ++ ;
}
SPI2->DR = tx_data ;
retry = 0 ;
while( ( SPI2->SR & ( 1 << 0 ) ) == 0 )
{
retry ++ ;
}
return SPI2->DR ;
} |
|