void usart_enable(usart_dev *dev) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); /* Enable USART */ USART_Cmd(dev->USARTx, ENABLE); }
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); /* Transmit Data */ USARTx->DR = (Data & (uint16_t)0x01FF); }
void usart_setup(const usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); assert_param(IS_USART_BAUDRATE(baud)); assert_param(IS_USART_STOPBITS(stopbits)); assert_param(IS_USART_PARITY(parity)); assert_param(IS_USART_WORD_LENGTH(wordLength)); assert_param(IS_USART_MODE(mode)); assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl)); memset(dev->state, 0, sizeof(*dev->state)); dev->state->txbusy = 0; dev->state->callback = 0; /* Disable USARTx */ usart_disable(dev); rb_init(dev->txrb, USART_TX_BUF_SIZE, dev->state->tx_buf); rb_init(dev->rxrb, USART_RX_BUF_SIZE, dev->state->rx_buf); /* Enables the USART's 8x oversampling mode. */ USART_OverSampling8Cmd(dev->USARTx, ENABLE); USART_ClockInitTypeDef USART_InitClock; USART_ClockStructInit(&USART_InitClock); USART_ClockInit(dev->USARTx, &USART_InitClock); USART_InitTypeDef USART_config; USART_StructInit(&USART_config); USART_config.USART_BaudRate = baudRate; USART_config.USART_WordLength = wordLength; USART_config.USART_StopBits = stopBits; USART_config.USART_Parity = parity; USART_config.USART_Mode = mode; USART_config.USART_HardwareFlowControl = hardwareFlowControl; USART_Init(dev->USARTx, &USART_config); dev->USARTx->CR1 &= ~(USART_MASK_IDLEIE | USART_MASK_RXNEIE | USART_MASK_TCEIE | USART_MASK_TXEIE | USART_MASK_PEIE); dev->USARTx->CR2 &= ~(USART_MASK2_LBDIE); dev->USARTx->CR3 &= ~(USART_MASK3_CTSIE | USART_MASK3_EIE); if(mode & USART_Mode_Rx) { /* Enable Rx request */ USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE); dev->USARTx->CR1 |= USART_MASK_RXNEIE; } if(mode & USART_Mode_Tx) { USART_ClearFlag(dev->USARTx, USART_FLAG_TC); } enable_nvic_irq(dev->irq, UART_INT_PRIORITY); }
uint32_t usart_tx(usart_dev *dev, const uint8_t *buf, uint32_t len) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); uint32_t tosend = len; uint32_t sent = 0; if (dev->usetxrb) { while (tosend) { if (rb_is_full(dev->txrb)) break; rb_insert(dev->txrb, *buf++); sent++; tosend--; } if (dev->txbusy == 0 && sent > 0) { dev->txbusy = 1; USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE); } } else { #if 0 uint32_t rtime; if (dev->use_timeout) stopwatch_reset(); #endif while (tosend) { while (!(dev->USARTx->SR & USART_FLAG_TXE)) { #if 0 if (dev->use_timeout) { rtime = stopwatch_getus(); if (rtime >= dev->tx_timeout) { return sent; } } #endif } dev->USARTx->DR = *buf++; tosend--; sent++; } } return sent; }
void USART_SendData_By_ZZB(USART_TypeDef* USARTx, u16 Data) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); /* Transmit Data */ while(!(USARTx->SR & 0x00000080)); USARTx->DR = (Data & (u16)0x01FF); while(!(USARTx->SR & 0x00000040)); }
/** * @brief Initialize a serial port. * @param dev Serial port to be initialized */ void usart_init(const usart_dev *dev) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); // Turn on peripheral clocks if (dev->USARTx == USART1 || dev->USARTx == USART6 ) RCC_APB2PeriphClockCmd(dev->clk, ENABLE); // we must wait some time before access to else RCC_APB1PeriphClockCmd(dev->clk, ENABLE); }
void usart_disable(usart_dev *dev) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); /* Enable USART */ USART_Cmd(dev->USARTx, DISABLE); /* Clean up buffer */ usart_reset_rx(dev); usart_reset_tx(dev); }
void USART_SendString_By_ZZB(USART_TypeDef* USARTx, u8* String) { assert_param(IS_USART_ALL_PERIPH(USARTx)); while(*String != '\0') { while(!(USARTx->SR & 0x00000080));//bit7 TXE:发送数据寄存器为空标记 USARTx->DR = (u16)*String & 0x01ff; while(!(USARTx->SR & 0x00000040));//bit6 TC:发送完成标记 String++; } }
/** * @brief Initialize a serial port. * @param dev Serial port to be initialized */ void usart_init(usart_dev *dev) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); rb_init(dev->txrb, USART_TX_BUF_SIZE, dev->tx_buf); rb_init(dev->rxrb, USART_RX_BUF_SIZE, dev->rx_buf); // Turn on peripheral clocks if (dev->USARTx == USART1 || dev->USARTx == USART6 ) RCC_APB2PeriphClockCmd(dev->clk, ENABLE); else RCC_APB1PeriphClockCmd(dev->clk, ENABLE); }
//开启一次DMA传输 void USART_DMA_Enable(USART_TypeDef *USARTx) { DMA_Channel_TypeDef *DMAy_Channelx; assert_param(IS_USART_ALL_PERIPH(USARTx)); if (USARTx == USART1) DMAy_Channelx = DMA1_Channel4; else if (USARTx == USART2) DMAy_Channelx = DMA1_Channel7; else if (USARTx == USART3) DMAy_Channelx = DMA1_Channel2; DMA_Cmd(DMAy_Channelx, DISABLE ); //关闭USART1 TX DMA1 所指示的通道 DMA_SetCurrDataCounter(DMA1_Channel7, SENDBUFF_SIZE); //DMA通道的DMA缓存的大小 DMA_Cmd(DMAy_Channelx, ENABLE); //使能USART1 TX DMA1 所指示的通道 }
/** ********************************************************************************* * @Function: USART_SetBaudRate; * * @Description: 设置波特率 * * @Input: --usart ; * --baudRate 波特率 * * @Output: none; * * @Return: none; * * @Note: USARTx->BRR = Fclk / baudRate ; *********************************************************************************** */ void USART_SetBaudRate(USART_TypeDef *usart , INT32U baudRate) { RCC_ClocksTypeDef Clocks; INT32U tmp = 0 ; assert_param(IS_USART_ALL_PERIPH(usart)); RCC_GetClocksFreq(&Clocks); //获取时钟 if(usart==USART1){ tmp = Clocks.PCLK2_Frequency ; }else{ tmp = Clocks.PCLK1_Frequency ; } tmp /= baudRate ; usart->BRR = tmp ; }
/** ********************************************************************************* * @Function: USART_GetBaudRate; * * @Description: 获取波特率 * * @Input: --usart ; * * @Output: none; * * @Return: INT32U 波特率; * * @Note: baudRate = Fclk / USARTx->BRR ; *********************************************************************************** */ INT32U USART_GetBaudRate(USART_TypeDef *usart) { RCC_ClocksTypeDef Clocks; INT32U ret = 0 ; INT32U tmp = 0 ; assert_param(IS_USART_ALL_PERIPH(usart)); tmp = usart->BRR ; RCC_GetClocksFreq(&Clocks); //获取时钟 if(usart==USART1){ ret = Clocks.PCLK2_Frequency ; }else{ ret = Clocks.PCLK1_Frequency ; } ret /= tmp ; return ret ; }
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG) { FlagStatus bitstatus = RESET; /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_FLAG(USART_FLAG)); /* The CTS flag is not available for UART4 and UART5 */ if (USART_FLAG == USART_FLAG_CTS) { assert_param(IS_USART_1236_PERIPH(USARTx)); } if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) { bitstatus = SET; } else { bitstatus = RESET; } return bitstatus; }
uint32_t usart_tx(const usart_dev *dev, const uint8_t *buf, uint32_t len) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); uint32_t tosend = len; uint32_t sent = 0; while (tosend) { if (rb_is_full(dev->txrb)) break; rb_insert(dev->txrb, *buf++); sent++; tosend--; } if (dev->state->txbusy == 0 && sent > 0) { dev->state->txbusy = 1; dev->USARTx->CR1 |= USART_MASK_TXEIE; } return sent; }
void usart_setup(usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl, uint32_t tx_timeout) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); assert_param(IS_USART_BAUDRATE(baud)); assert_param(IS_USART_STOPBITS(stopbits)); assert_param(IS_USART_PARITY(parity)); assert_param(IS_USART_WORD_LENGTH(wordLength)); assert_param(IS_USART_MODE(mode)); assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl)); dev->tx_timeout = tx_timeout; dev->txbusy = 0; dev->usetxrb = 1; dev->use_timeout = 0; /* Disable USARTx */ USART_Cmd(dev->USARTx, DISABLE); /* Enables the USART's 8x oversampling mode. */ USART_OverSampling8Cmd(dev->USARTx, ENABLE); USART_ClockInitTypeDef USART_InitClock; USART_ClockStructInit(&USART_InitClock); USART_ClockInit(dev->USARTx, &USART_InitClock); USART_InitTypeDef USART_config; USART_StructInit(&USART_config); USART_config.USART_BaudRate = baudRate; USART_config.USART_WordLength = wordLength; USART_config.USART_StopBits = stopBits; USART_config.USART_Parity = parity; USART_config.USART_Mode = mode; USART_config.USART_HardwareFlowControl = hardwareFlowControl; USART_Init(dev->USARTx, &USART_config); NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the USART Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = dev->irq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(dev->USARTx, USART_IT_PE, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_IDLE, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_LBD, DISABLE); if (IS_USART_1236_PERIPH(dev->USARTx)) USART_ITConfig(dev->USARTx, USART_IT_CTS, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_ERR, DISABLE); /* Enable USART2 Rx request */ USART_ITConfig(dev->USARTx, USART_IT_RXNE, ENABLE); USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE); USART_ITConfig(dev->USARTx, USART_IT_TC, DISABLE); USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE); USART_ClearFlag(dev->USARTx, USART_FLAG_TC); /* USART_ITConfig(dev->USARTx, USART_IT_RXNE, ENABLE); USART_ITConfig(dev->USARTx, USART_IT_PE, ENABLE); USART_ITConfig(dev->USARTx, USART_IT_ERR, ENABLE); */ }
void USART_DMA_Config(USART_TypeDef *USARTx, u32 SendBuff) { DMA_Channel_TypeDef *DMAy_Channelx; // uint8_t DMAy_Channelx_IRQn; DMA_InitTypeDef DMA_InitStructure; //NVIC_InitTypeDef NVIC_InitStructure; assert_param(IS_USART_ALL_PERIPH(USARTx)); if (USARTx == USART1) { DMAy_Channelx = DMA1_Channel4; // DMAy_Channelx_IRQn = DMA1_Channel4_IRQn; } else if (USARTx == USART2) { DMAy_Channelx = DMA1_Channel7; // DMAy_Channelx_IRQn = DMA1_Channel7_IRQn; } else if (USARTx == USART3) { DMAy_Channelx = DMA1_Channel2; // DMAy_Channelx_IRQn = DMA1_Channel2_IRQn; } DMA_DeInit(DMAy_Channelx); //½«DMAµÄͨµÀ1¼Ä´æÆ÷ÖØÉèΪȱʡֵ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //开启DMA时钟 //NVIC_Config(); //配置DMA中断 // NVIC_InitStructure.NVIC_IRQChannel = DMAy_Channelx_IRQn; // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStructure); /*设置DMA源:内存地址&串口数据寄存器地址*/ //DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USARTx->DR; /*内存地址(要传输的变量的指针)*/ DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff; /*方向:从内存到外设*/ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /*传输大小DMA_BufferSize=SENDBUFF_SIZE*/ DMA_InitStructure.DMA_BufferSize = sizeof(SendBuff);//SENDBUFF_SIZE; /*外设地址不增*/ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /*内存地址自增*/ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /*外设数据单位*/ DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; /*内存数据单位 8bit*/ DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; /*DMA模式:一次传输,循环*/ DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; /*优先级:中*/ DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; /*禁止内存到内存的传输 */ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /*配置DMA1的4通道*/ DMA_Init(DMAy_Channelx, &DMA_InitStructure); //DMA_Cmd (DMAy_Channelx, ENABLE); //使能DMA DMA_ITConfig(DMAy_Channelx, DMA_IT_TC, ENABLE); //配置DMA发送完成后产生中断 USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); //使能串口1的DMA发送 }