STM32M4_SPI&USART
void SPIDRV_Initial(void)
{
// SPI GPIO Setting
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*!< SPI CE pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GP_GetPin(GPIO_FLASH_CS);
GPIO_Init(GP_GetPort(GPIO_FLASH_CS), &GPIO_InitStructure);
/*SP2's GPIO Configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
/*!< SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = GP_GetPin(SPI_SCK_FLASH);
GPIO_Init(GP_GetPort(SPI_SCK_FLASH), &GPIO_InitStructure);
/*!< SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = GP_GetPin(SPI_SDO_FLASH);
GPIO_Init(GP_GetPort(SPI_SDO_FLASH), &GPIO_InitStructure);
/*!< SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = GP_GetPin(SPI_SDI_FLASH);
GPIO_Init(GP_GetPort(SPI_SDI_FLASH), &GPIO_InitStructure);
/*!< Connect SPI pins to AF5 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
MCU_MemSPI_CS(IOHILO_HI);
/*!< Deselect the FLASH: Chip Select high */
RCC_APB1PeriphClockCmd(SPI_FLASH_RCC, ENABLE);
/*!< SPI configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //TBD
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI_FLASH, &SPI_InitStructure);
/*!< Enable the sFLASH_SPI */
SPI_Cmd(SPI_FLASH, ENABLE);
}
void SPIx_BusEndCheck(SPI_TypeDef* SPIx)
{
INT16U del;
while((SPIx->SR&SPI_SR_BSY) != 0x00) //wait spi bus idle status
{
del++;
if (del > 0xc000) //check SPI bus over process
{
break;
}
}
return;
}
void SPIDRV_BusEndCheck(void)
{
SPIx_BusEndCheck(SPI2);
return;
}
void SPIDRV_SendByte(uint8 byte)
{
INT16U del;
del = 0x00;
while((SPI_FLASH->SR&SPI_SR_TXE)== 0x00) //wait tx end delay process
{
del++;
if (del > 0x9000) //check tx over process
{
break;
}
}
SPI_FLASH->DR = byte;
}
uint8 SPIDRV_ReceiveByte()
{
INT16U del;
del = 0x00;
while ((SPI_FLASH->SR&SPI_SR_RXNE) == 0x00) //wait rx data process
{
del++;
if (del > 0xA000) //check rx over process
{
break;
}
}
return SPI_FLASH->DR;
}
void SPIDRV_SendDatas(uint32 Len,uint8* Data)
{
uint32 i;
for (i=0; i<Len; i++)
{
//DRV_SPI_2SndRecByte(Data[i]);
SPIDRV_SendByte(Data[i]);
}
SPIDRV_BusEndCheck(); //wait spi bus idle status
}
void SPIDRV_ReadDatas(uint32 Len,uint8* Data)
{
uint32 i;
Data[0] = SPIDRV_ReceiveByte(); //clr data buffer
for(i = 0x00; i < Len ; i++) //read data process
{
SPIDRV_SendByte(0xA5);
Data[i]= (INT8U)SPIDRV_ReceiveByte(); //wait rx data process
}
SPIDRV_BusEndCheck(); //wait spi bus idle status
}
//USART INIT
void UART4_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//USART_ClockInitTypeDef CLOCK_InitStructure;
/* Enable UART4 and GPIOA clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //开启时钟
/* Configure UART4 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPS_UART_PORT, GPIO_PinSource0, GPIO_AF_UART4);
GPIO_PinAFConfig(GPS_UART_PORT, GPIO_PinSource1, GPIO_AF_UART4);
/* Enable the USART OverSampling by 8 */
USART_OverSampling8Cmd(UART4, ENABLE);
/* USARTx configured as follows:
- BaudRate = 9600baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = _BORDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(UART4, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearFlag(UART4, USART_FLAG_TXE);
USART_ClearFlag(UART4, USART_FLAG_RXNE);
USART_ITConfig(UART4, USART_IT_TC, DISABLE); // DisEnable Tx Intrupt
USART_ITConfig(UART4, USART_IT_RXNE, DISABLE); // DisEnable Tx Intrupt
/* Enable UART4 */
USART_Cmd(UART4, ENABLE);
//UartSndObj.pIn=UartSndObj.cBuff;
//UartSndObj.pOut=UartSndObj.cBuff;
}
//串口中断程序处理发送和接收
void UART4_IRQHandler(void)
{
if ((UART4->SR& (1 << 5) ) != RESET) //判断接收不为空中断。
{
cTemp = (INT8U)(UART4->DR & 0xFF);
}
/* USART in Tramitter mode */
if( (UART4->CR1&(1<<6))!=RESET)
{
if ( (UART4->SR & (1 << 6) ) != RESET) //检测是否发射完成
{
//未发送完成继续发送
//否则
{
USART_ITConfig(UART4, USART_IT_TC, DISABLE);
}
}
}
}
//启动方式
使能接收或者发送中断
相关文章
- 排序层-深度模型-2016:Deep Crossing模型【经典的Embedding+MLP模型结构】【MLP采用“多层残差网络”结构】【与Deep&Cross不是同一个模型】
- [na]tcp&udp扫描原理(nmap常用10条命令)
- IoT设备网络会帮你削减开支&提高生产力吗?
- URL刷新工具(绿色版) > 也许对你有用哦~~!
- 【LeetCode从零单排】No118Pascal's Triangle
- Unity学习-实现玩家&&按钮&&门之间的互动
- 2018HDU多校联赛第六场 6373 Pinball——水题&&物理题
- 【8】Broadcast 自定义 静态&动态广播注册
- [认证 & 授权] 2. OAuth2授权(续) & JWT(JSON Web Token)
- 我的Android进阶之旅------>Android中android:windowSoftInputMode的用法