本帖最后由 jobszheng5 于 2023-1-15 20:37 编辑
FreeModbus协议栈在LPC1768上的移植核心源代码 本次上传的源代码为核心源代码,非整个工程包。
核心源代码主要由portserial.c文件与porttime.c文件构成,其余文件均基于官方示例所直接拷贝。
Portserial.c
对于Portserial.c文件的移植,版主按移植要求使用中断接收与中断发送,即单字节触发模式。
源代码如下:
- /* ----------------------- Start implementation -----------------------------*/
- void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
- {
- /* If xRXEnable enable serial receive interrupts. If xTxENable enable
- * transmitter empty interrupts.
- */
- if(xTxEnable == TRUE)
- {
- Chip_UART_TXEnable(LPC_UART0);
- Chip_UART_IntEnable(LPC_UART0, UART_IER_THREINT);
- Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_RX_RS |
- UART_FCR_TX_RS | UART_FCR_TRG_LEV3));
- }
- else
- {
- Chip_UART_IntDisable(LPC_UART0, UART_IER_THREINT);
- Chip_UART_TXDisable(LPC_UART0);
- }
- }
- BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity)
- {
- return TRUE;
- }
- BOOL xMBPortSerialPutByte(CHAR ucByte)
- {
- /* Put a byte in the UARTs transmit buffer. This function is called
- * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
- * called. */
- uart_sendbyte_blocking(ucByte);
- return TRUE;
- }
- BOOL xMBPortSerialGetByte(CHAR *pucByte)
- {
- /* Return the byte in the UARTs receive buffer. This function is called
- * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
- */
- *pucByte = Chip_UART_ReadByte(LPC_UART0);
- return TRUE;
- }
复制代码
Porttime.c
对于Porttime.c文件的移植,使用了Time1定时器的通道0的匹配中断。按移植要求,设计产生50us的时间单位,具体时间间隔以传入参数而定。
源代码如下:
- /* ----------------------- Start implementation -----------------------------*/
- BOOL xMBPortTimersInit(USHORT usTim1Timerout50us)
- {
- Chip_TIMER_Init(LPC_TIMER1);
- Chip_TIMER_Disable(LPC_TIMER1);
- Chip_TIMER_PrescaleSet(LPC_TIMER1, 99);
- Chip_TIMER_SetMatch(LPC_TIMER1, 0, 50 * usTim1Timerout50us - 1);
- Chip_TIMER_MatchDisableInt(LPC_TIMER1, 0);
- NVIC_SetPriority(TIMER1_IRQn, 1);
- NVIC_EnableIRQ(TIMER1_IRQn);
- return TRUE;
- }
- void vMBPortTimersEnable()
- {
- /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
- Chip_TIMER_ClearMatch(LPC_TIMER1, 0);
- Chip_TIMER_MatchEnableInt(LPC_TIMER1, 0);
- Chip_TIMER_ResetOnMatchEnable(LPC_TIMER1, 0);
- Chip_TIMER_Enable(LPC_TIMER1);
- }
- void vMBPortTimersDisable()
- {
- /* Disable any pending timers. */
- Chip_TIMER_Disable(LPC_TIMER1);
- Chip_TIMER_MatchDisableInt(LPC_TIMER1, 0);
- Chip_TIMER_ClearMatch(LPC_TIMER1, 0);
- }
- /* Create an ISR which is called whenever the timer has expired. This function
- * must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
- * the timer has expired.
- */
- // static void prvvTIMERExpiredISR(void)
- void TIMER1_IRQHandler(void)
- {
- if (Chip_TIMER_MatchPending(LPC_TIMER1, 0))
- {
- Chip_TIMER_ClearMatch(LPC_TIMER1, 0);
- (void)pxMBPortCBTimerExpired();
- }
- }
复制代码
|