OrthyV202B-使用UART RX WorksCurror(禁用Tx和错误中断,错误中断在启用时导致随机数据故障)------------------MHCV2024UART,静态、中断、字节模型+回调(非阻塞)----------------- Txbool bUartTx(UARTES EU,UIT88T UDATA)//非阻塞(如果不发送则返回false){if(!)!DrviUsARTHEngPuffeSeriSULL(SUART[EU]句柄)/ /如果UART TX FIFO具有空间{DRVU.UARTARGWORKETENEL字节(SUART [EU]句柄,UDATA);//输出数据SUARTAR[EU]。UTXCOUNT++;返回(true);}返回(false);/ /没有空间用于Tx} VUARTTX(EUARTES EU,UIT88UT UDATA)/ /阻塞{(DrvuasAtARTU-TURMI)tBuffelISFULL(SUART[EU]句柄){{/*Walth*/} / /如果UART TX具有空间DrvU.AtARTHARDIGETELL字节(SuART[Eu]句柄,UDATA);//输出数据}---------------init多个UART与CalbutSuin88t PUUARTITIDENET[EULISTRONT] = {DRVU.UARTARXIDEXX0 0,DRVIUASARTI索引X1,DRVIUS UARTARXIDEX2,DRVIUS USAARTI索引3在Initprintf(“OpenUART:”)中允许重新映射EUART实例;(EU=0;Eu& lt;EulistEnter;Eu+++)/检查RX数据{UARTEX= EUE]的所有UART。UTS(“n”);PrtTf(“%s”,PPCuARTeNe[Eu]);Suale[Eu]。句柄= DRVIUSAARTHOPENT(SUART[EU] UDCHIVE,DRVYIOIN ItNeTyRead Read DrvyIOIONTIN非阻塞);/todo:ItrimyV202b中的意图不做任何事(DrvH-AdLeIOAL==SuART[Eu]句柄)/*打开错误?检查句柄是否有效*/{PrtTf(“**uARTITITVIRT[%s] ***n”,PPCuARTeNe[Eu]);} DRVIUTARETECVECVECARECABSET(SUART [Eu]VCARBACKIOSUARTARJORX(const SysMuleMeX索引UDATA){UTI8EYTURUDATA;UTI88UTURX字节=0;//用于更新SUART [UnCurry]。URXMAX交换机(UQUEST)/ / UART { EuDiag:外壳EUXP:CASE EULA:情况EULB:EUOO:CASE EUOB://我的Uarts IF(DRVIUS UsARTHARVER VBR)(SUART [UnQual]句柄){SUART [UnQue],BRXNONE= TRUE;} //获得PIC32 MZ的数据{//DrvuSuthARTAccess VBuffSersieGETE()==8字节,同时(!)DrviUsRealFuffelFielSISULL(SUART [UnQue]句柄)//读取UART FIFO直到空{ //OLIDHHOTGGELE();;/SUART [UnQue][x]句柄);SuART[uQue] UrxCuth++;BruBuff88Adl(PPUARTrxRunBufs[uClult],UDATA);和/或您的缓冲选择,如果URX字节[gt;SuART[uQue] UrxMax){SuART[UnQuest.UrxMax=URXBythOb];//RX FIFO深度峰值检测器,可以帮助检测如果系统对数据速率太慢,则中断;默认值:UUARTARTEXIDENDIDCOUNTION+++;BREACK;} ------------MunPUTC,用于将PrimTF映射到所需的UART(替换系统默认值)ValueMun-Putc(char cByb)/重定向STDIO到EUDIAG{{//REF:http://MychIpPrdult/COX/XC32:重定向:-如果您的代码{VuARTTXBEL(EUDIAG,CBOLL)中启用了缓冲(StdOUT)/可选的检查,则/ /通过缓冲区输出/否则/直接输出到端口(在系统异常期间有用,调试Uart Buffering等){(DrvuSuthARTApple BuffeSeriSULL(DY-DGJI IN)){/*Walth**/}//等待直到空间(阻塞)DrviUsARTHARCHEADEYBEY(DY-DGYIN TythHANDLE,cBEYE);//直接输出数据到EUDIAG}} ------------ISR(未修改的生成系统Sypult.c)ValuyO.ISR(uUART1xTXY向量,IPL0AUTO)。[0UTAR1ORXY向量,IPL1AUTO),in thundLrdvARSARTIFESTATION 0(空隙){DrvU.AtARTHEASTASKS接收(SysObj.DrVuSART0);} Vo.Y-ISR(保罗,UART1IOFARTTHORE向量,IPL0AUTO)。TT
以上来自于百度翻译
以下为原文
HarmonyV202b - Using the callbacks for UART Rx works
(Disable Tx and Error interrupts, the Error Interrupt causes random data glitches when enabled).
--------------------- MHCv2024
UART, Static, Interrupt, Byte Model+Callback (non-blocking)
--------------------- Tx
bool bUartTx(EUARTs eU, uint8_t uData) //non-blocking (returns false if not sent)
{
if(!DRV_USART_TransmitBufferIsFull(sUart[eU].handle)) //If UART Tx FIFO has Space
{
DRV_USART_WriteByte(sUart[eU].handle, uData); // Output Data
sUart[eU].uTxCount++;
return(true);
}
return(false); //no space for Tx
}
void vUartTx(EUARTs eU, uint8_t uData) //blocking
{
while(DRV_USART_TransmitBufferIsFull(sUart[eU].handle)) {/*wait*/ } //If UART Tx has Space
DRV_USART_WriteByte(sUart[eU].handle, uData); // Output Data
}
--------------------- Init multiple UARTs with Callbacks
uint8_t puUartInitIndex[EuListend] = { DRV_USART_INDEX_0, DRV_USART_INDEX_1, DRV_USART_INDEX_2, DRV_USART_INDEX_3, DRV_USART_INDEX_4, DRV_USART_INDEX_5 }; //Allows re-Mapping EUART Instances during Init
printf("OpenUart:");
for(eU=0; eU
{
sUart[eU].uIndex = puUartInitIndex[eU];
//vUartDG_Init_puts("OpenUart:");vUartDG_Init_puts(ppcUartNames[eU]);vUartDG_Init_puts("n");
printf(" %s", ppcUartNames[eU]);
sUart[eU].handle = DRV_USART_Open(sUart[eU].uIndex, DRV_IO_INTENT_READWRITE|DRV_IO_INTENT_NONBLOCKING);//ToDo: Intents do nothing in HarmonyV202b
if(DRV_HANDLE_INVALID == sUart[eU].handle)/* Open Error? Check if the handle is valid */
{
printf("*** UartInitFail[%s] ***n", ppcUartNames[eU]);
}
DRV_USART_ByteReceiveCallbackSet(sUart[eU].uIndex, vCallback_usart_rx);
//unused: DRV_USART_ByteErrorCallbackSet(sUart[eU].uIndex, vCallback_usart_err);
}
printf("n");
--------------------- Callback
void vCallback_usart_rx(const SYS_MODULE_INDEX uIndex)
{
uint8_t uData;
uint8_t uRxBytes=0; // for updating sUart[uIndex].uRxMax
switch(uIndex) //which UART
{
case EuDiag: case EuExp: case EuLA: case EuLB: case EuOA: case EuOB: //My Uarts
if(DRV_USART_ReceiverBufferIsEmpty(sUart[uIndex].handle))
{
sUart[uIndex].bRxNone = true;
}
else //Got Data
{
//DRV_USART_ReceiverBufferSizeGet()==8bytes for PIC32MZ
while(!DRV_USART_ReceiverBufferIsEmpty(sUart[uIndex].handle))// Read Uart FIFO till empty
{
//oLEDH_Toggle();
//sUart[uIndex].bRx = true;
//sUart[uIndex].uRxByte = DRV_USART_ReadByte(sUart[uIndex].handle);
uRxBytes++;
uData = DRV_USART_ReadByte(sUart[uIndex].handle);
sUart[uIndex].uRxCount++;
bRingBufA8_add(ppUartRxRingBufs[uIndex], uData);//Your choice of buffering, I have my own ring buffers
}
if(uRxBytes > sUart[uIndex].uRxMax){sUart[uIndex].uRxMax = uRxBytes;}//Peak Detector for Rx FIFO Depth used, Can help detect if system too slow for data rate
}
break;
default:
uUartIndexInvalidCount++;
break;
}
}
--------------------- mon_putc for mapping printf to desired UART (replaces system default)
void _mon_putc(char cByte)//Redirect Stdio to EuDiag
{ // Ref:
http://microchipdeveloper.com/xc32:redirect-stdout
if(bUartInitOK)//Optional check if buffering enabled in your code
{
vUartTxByte(EuDiag, cByte);//Output via Buffer
}
else //Direct Output to port (Useful during system exceptions, debugging of Uart Buffering, etc.)
{
while(DRV_USART_TransmitBufferIsFull(D_DG_InitHandle)){/*wait*/} //Wait Till Space (Blocking)
DRV_USART_WriteByte(D_DG_InitHandle, cByte); //Direct Output Data to EuDiag
}
}
--------------------- ISR (Unmodified generated system_interrupt.c)
void __ISR(_UART1_TX_VECTOR, ipl0AUTO) _IntHandlerDrvUsartTransmitInstance0(void)
{
DRV_USART_TasksTransmit(sysObj.drvUsart0);
}
void __ISR(_UART1_RX_VECTOR, ipl1AUTO) _IntHandlerDrvUsartReceiveInstance0(void)
{
DRV_USART_TasksReceive(sysObj.drvUsart0);
}
void __ISR(_UART1_FAULT_VECTOR, ipl0AUTO) _IntHandlerDrvUsartErrorInstance0(void)
{
DRV_USART_TasksError(sysObj.drvUsart0);
}
---------------------
Paul