上周分享了基于S32-SDK方法配置uart的方法,本次分享UART底层裸机配置,废话不多数,直接上代码:
1)初始化
void uart1_init(void) { /* * PTC6 uart1_rx * PTC7 uart1_tx * */ /*PIN MUX*/ PCC->PCCn[PCC_PORTC_INDEX] |= 1<<30; PORTC->PCR[6] |= 1<<9; PORTC->PCR[7] |= 1<<9; /*uart1 init*/ PCC->PCCn[PCC_LPUART1_INDEX] &= ~(1<<30); /*PCC UART1 CLK disable*/ PCC->PCCn[PCC_LPUART1_INDEX] |= (1<<25); /*PCS=010b,SIRC=8M*/ PCC->PCCn[PCC_LPUART1_INDEX] |= (1<<30); /*PCC UART1 CLK enable*/ /*关闭发送接收使能*/ LPUART1->CTRL &= ~(1<<19); LPUART1->CTRL &= ~(1<<18); /*配置串口波特率,clk/(波特率*OSR),OSR默认16*/ LPUART1->BAUD = 0x0f000034; /*8000000/(9600*16)波特率=9600*/ /*发送和接收使能*/ LPUART1->CTRL |= (1<<19)|(1<<18); LPUART1->CTRL |= (1<<2); /*空闲初始位置从stop开始*/ /*接收中断使能,空闲使能*/ LPUART1->CTRL |= (1<<21)|(1<<20); /*注册中断*/ S32_NVIC_EnableIRQ(LPUART1_RxTx_IRQn); while( ((LPUART1->CTRL>>19) & 0x1) == 0); }
2)发送和接收
//========================================================================== //UART1接收中断函数 //========================================================================== void LPUART1_RxTx_IRQHandler() { gpio_reverse(PTe, 23); LPUART1->STAT &= ~(1<<21); /*判断是否收到换行符*/ if( ((LPUART1->STAT >> 21) & 0x1) == 1 ) { if ((buffer[bufferIdx] != '\n') && (bufferIdx != (BUFFER_SIZE - 2U))) { buffer[bufferIdx] = LPUART1->DATA & 0x3ff; /*每接收一字节数据,就将储存位置指向数组的下一位,直到接收到换行符*/ bufferIdx++; } } if( ((LPUART1->STAT >> 20) & 0x1) == 1 ) { buffer[bufferIdx] = 0U; bufferIdx = 0; } }
//========================================================================== //UART1发送函数 //========================================================================== void uart1_send1Byte(uint8_t senddata) { while( ((LPUART1->STAT >> 23) & 0x1) == 0); LPUART1->DATA = senddata; } //========================================================================== //UART1发送函数 //========================================================================== void uart1_sendNByte(uint8_t *buff, uint16_t len) { for(uint16_t i=0;i<len;i++) { uart1_send1Byte(buff[i]); } }
标签:uart1,LPUART1,uart,void,裸机,1CTRL,bufferIdx,PCC,S32K148 From: https://www.cnblogs.com/taotaonihao/p/16636201.html