Board Support Package (BSP)

µC/Modbus can work with just about any UART. You need to provide a few simple interface functions to work with your hardware. These functions should be placed in a file called mb_bsp.c. Micrium provides examples of mb_bsp.c as part of the µC/Modbus release.

BSP, MB_CommExit()

This function is called by MB_Exit() to close all serial interfaces used by µC/Modbus. Your application DOES NOT need to call this function. The pseudo-code for this function is shown below:


void  MB_CommExit (void)
{
    /* Disable all uC/Modbus Rx interrupts */
    /* Disable all uC/Modbus Tx interrupts */
    /* Remove interrupt vectors (if needed) */
}



BSP, MB_CommPortCfg()

This function is called by MB_CfgCh() to configure the UART communication settings for a channel. MB_CommPortCfg() must NOT be called by your application. The function prototype is shown below:


void  MB_CommPortCfg (MODBUS_CH *pch, 
                     CPU_INT08U port_nbr, 
                     CPU_INT32U baud, 
                     CPU_INT08U bits, 
                     CPU_INT08U parity, 
                     CPU_INT08U stops);


pch


is a pointer to the communication channel to configure. This pointer is returned to your application when you call MB_CfgCh().

port_nbr

is the ‘physical’ port number associated with the µC/Modbus communication channel. For example, µC/Modbuschannel #0 could be associated with your 5th UART. In other words, µC/Modbuschannels can be assigned to any ‘physical’ serial port in your system – there doesn’t need to be a one-to-one correspondence.

baud

is the desired baud rate for the channel. You should write code to support the standard baud rates: 9600, 19200, 38400, 76800, 115200 and 256000 baud.

bits

is the number of bits used for the UART. It’s typically 7 or 8. The most common is 8 bits.

parity

is the type of parity checking scheme used for the serial port. The choices are: MODBUS_PARITY_NONE, MODBUS_PARITY_ODD and MODBUS_PARITY_EVEN. The most common is MODBUS_PARITY_NONE.

stops

specifies the number of stop bits used. The choices are typically 1 or 2. 1 stop bit is the most common.

BSP, MB_CommRxTxISR_x_Handler()

Most UARTs allow you to generate an interrupt when either a byte is received or when a byte has been sent. If your UART generates an interrupt when either a byte is received or when one has been sent then, you would need to write a function that determines whether the interrupt was caused by a received by or by a byte sent. In this case, you would write a function called MBS_CommRxTxISR_x_Handler() where the ‘x’ indicates the physical UART (example 1, 2, 3 …). The pseudo-code for this function is shown below. The code in RED is code that you have to write. You should COPY all the other code as is.


void  MB_CommRxTxISR_x_Handler (void)
{
    CPU_INT08U   c;
    CPU_INT08U   ch;
    MODBUS_CH   *pch;

    pch = &MB_ChTbl[0];
    for (ch = 0; ch < MODBUS_MAX_CH; ch++) {
        if (pch->PortNbr == port_nbr) {
            if (Rx Interrupt) {
                c = Read byte from UART;
                Clear Rx Interrupt;
                pch->RxCtr++;
                MB_RxByte(pch, c);    // Pass byte to Modbus to process
            }
            if (Tx Interrupt) {
                pch->TxCtr++;
                MB_TxByte(pch);       // Send next byte in response
                Clear Tx Interrupt; 
            }   break;
        } else {
            pch++;
        }
    }
    Clear spurious interrupts;
}



BSP, MB_CommRxIntEn()

This function is called by µC/Modbus to enable Rx interrupts from a UART.


void  MB_CommRxIntEn (MODBUS_CH * pch)
{
    switch (pch->PortNbr) {
        /* Enable Rx interrupts for specified UART */
    }
}



BSP, MB_CommRxIntDis()

This function is called by µC/Modbus to disable Rx interrupts from a UART.


void  MB_CommRxIntDis(MODBUS_CH * pch)
{
    switch (pch->PortNbr) {
        /* Disable Rx interrupts for specified UART */
    }
}



BSP, MB_CommTx1()

This function is called by µC/Modbusto send a SINGLE byte to the UART associated with the µC/Modbuschannel.


void  MB_CommTx1 (MODBUS_CH  * pch, 
                 CPU_INT08U c)
{
    switch (pch->PortNbr) {
        /* Write byte 'c' to specified UART */
    }
}



BSP, MB_CommTxIntEn()

This function is called by µC/Modbusto enable Tx interrupts from a UART.


void  MB_CommTxIntEn(MODBUS_CH * pch)
{
    switch (pch->PortNbr) {
        /* Enable Tx interrupts from specified UART */
    }
}



BSP, MB_CommTxIntDis()

This function is called by µC/Modbusto disable Tx interrupts from a UART.


void  MB_CommTxIntDis(MODBUS_CH * pch)
{
    switch (pch->PortNbr) {
        /* Disable Tx interrupts from specified UART */
    }
}



BSP, MB_RTU_TmrInit()

This function is called by MB_Init() to initialize the RTU timer. freq specifies the frequency used for the RTU timer interrupts.


void MB_RTU_TmrInit(CPU_INT32U freq);



BSP, MB_RTU_TmrExit()

This function is called by MB_Exit() to stop RTU timer interrupts.


void  MB_RTU_TmrExit(void);



BSP, MB_RTU_TmrISR_Handler()

This function is the ISR handler for RTU timer interrupts. The pseudo-code for this function is shown below:


void  MB_RTU_TmrISR_Handler (void)
{
    Clear the RTU timer interrupt source;
    MB_RTU_TmrCtr++; // Indicate that we had activities on this interrupt
    MB_RTU_TmrUpdate(); // Check for RTU timers that have expired
}