Link here

UART C Demo

A C demonstration program that provides examples of how to invoke the UART Wildcard driver functions

This section presents the ANSI C version of the demonstration program source code.

// ****************************************************************************************
// FILE NAME:   UModDemo.c
// copyright 2009 Mosaic Industries, Inc.  All rights reserved.
// ---------------------------------------------------------------------
// DATE:        4/14/2009
// VERSION:     1.6, for QED/Q-Line (HC-11) and PDQ line (HCS-12) controllers
// ---------------------------------------------------------------------
// This is the demonstration code for the Dual UART Module.
// Please see the UART Module User Guide for more details.
// This is an illustrative demonstration program that
// shows how to initialize the uarts for RS232 operation and run dual
// tasks using the two UART Module serial ports. Each task simply
// echoes incoming characters back to the terminal.
// When the top level function main() is running, the Mosaic Controller
// is simultaneously using 3 serial ports:
// the standard primary serial port is running the QED interactive monitor,
// and each of the two serial channels on the UART Module is echoing characters.
// Using the constants and/or the Default_UART_Init function
// defined in this file, you may customize the
// baud rate and protocol settings for the UART Module ports.
//
// The QED operating system supports revectorable I/O, meaning that
// in any given task the standard C serial I/O routines such as
// putchar, puts, getchar, gets, printf, and scanf can be made to use
// any specified serial channel.  All that is required is to customize
// three functions named Key, AskKey, and Emit to the specified serial channel
// for the specified task.  This file shows how to do this
// using the functions defined in the UART Module kernel extension.
//
// MAKE SURE THAT THE UART_MODULE_NUM CONSTANT MATCHES YOUR HARDWARE JUMPER SETTINGS!!
 
// ---------------------------------------------------------------------
// Demonstration functions defined in this file:
// UART_MODULE_NUM  // this constant MUST match hardware jumper settings!
// int Default_UART_Init( int module_num ) // demonstrates how to initialize module
// void main(void) // runs the demo program
 
// ---------------------------------------------------------------------
// Notes:
// Disclaimer: THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
//             ANY WARRANTIES OR REPRESENTATIONS EXPRESS OR IMPLIED,
//             INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES
//             OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// *********************************************************************
#include <mosaic\allqed.h>
 
#ifdef __GNUC__
// For PDQ line platforms, the driver is enabled by simply including
// the header file below.
#include <wuam.h>   // This is the UART Wildcard driver
// assume it's in the same directory as this file; if not, edit the file specification
 
#else
// For the QED/Q line platform, we include the kernel extension manager
// generated library.c.  We assume that it is present in this directory.
#include "library.h"
#include "library.c"
#endif    // __GNU__
 
// ******************* DEMONSTRATION PROGRAM ***************************
 
// The default task runs an interactive monitor as usual, using the 68HC11 UART.
// We create a second task and a third task that echo all incoming chars,
// each communicating using a serial channel on the UART Module.
// To run this demonstration, simply execute:
//       main
// You'll be running independent serial-echo tasks
// from your second and third terminals connected to the UART module.
 
// NOTE: YOU MUST MAKE SURE THAT UART_MODULE_NUM CONSTANT CORRESPONDS TO YOUR HARDWARE!!
#define UART_MODULE_NUM 0   // double check your hardware jumper settings!!!
 
// default values used in Default_UART_Init (edit these to suit your requirements):
#define DEFAULT_BITS_PER_CHAR   8
#define DEFAULT_STOP_BITS       1
#define DEFAULT_PARITY          NO_PARITY
#define DEFAULT_BAUDRATE        19200
#define DEFAULT_PROTOCOL        RS232
#define DEFAULT_MODEM_SUPPORT   FALSE
 
// Define and allocate RAM for the task areas:
TASK     ch1_task;  // 1 Kbyte per task area
TASK     ch2_task;  // 1 Kbyte per task area
 
_Q int Default_UART_Init( int module_num )
    // initializes BOTH channel1 and channel2 on the specified uart module_num.
    // result = SUCCESS (=0) or BAD_PROTOCOL_COMBO (=1)
    // this routine demonstrates how to initialize the uarts using default settings;
    // the user should customize the parameters to suit the application.
{       // configure channel1:
    Set_Data_Format(DEFAULT_BITS_PER_CHAR,DEFAULT_STOP_BITS,DEFAULT_PARITY,1,module_num);
    Set_Baud(DEFAULT_BAUDRATE,1,module_num);
        // configure channel2:
    Set_Data_Format(DEFAULT_BITS_PER_CHAR,DEFAULT_STOP_BITS,DEFAULT_PARITY,2,module_num);
    Set_Baud(DEFAULT_BAUDRATE,2,module_num);
        // set protocols for each channel:
    return (Set_Protocols(DEFAULT_MODEM_SUPPORT,DEFAULT_PROTOCOL,DEFAULT_PROTOCOL,module_num));
}
 
_Q void CH1_Monitor(void)
    // infinite task loop for ch1_task, simply echoes all incoming chars on channel1
{   uchar this_char;
 
//  Set up this task to use UART channel 1
#ifdef __GNUC__
    Use_Uart_Ch1();
#else
    UKEY = (xaddr) CH1_KEY_XADDR; // defined in library.h
    UASK_KEY = (xaddr) CH1_ASK_KEY_XADDR; // defined in library.h
    UEMIT = (xaddr) CH1_EMIT_XADDR; // defined in library.h
#endif    // __GNU__
 
    printf("\nWe are ready to echo incoming characters on Channel1...\r\n");
 
    while(1)
    {   this_char = Ch1_Key();  // Waits until a character is input, PAUSES as
        Ch1_Emit(this_char);    // well, so other tasks can run while waiting
    }
    UseSerial1();  // never reached
}
 
_Q void CH2_Monitor(void)
    // infinite task loop for ch2_task, simply echoes all incoming chars on channel2
{   uchar this_char;
 
//  Set up this task to use UART channel 2
#ifdef __GNUC__
    Use_Uart_Ch2();
#else
    UKEY = (xaddr) CH2_KEY_XADDR; // defined in library.h
    UASK_KEY = (xaddr) CH2_ASK_KEY_XADDR; // defined in library.h
    UEMIT = (xaddr) CH2_EMIT_XADDR; // defined in library.h
#endif    // __GNU__
 
 
    printf("\nReady to echo incoming characters on Channel2...\r\n");
 
 
    while(1)
    {   this_char = Ch2_Key();
        Ch2_Emit(this_char);
    }
    UseSerial1();    // never reached
}
 
_Q void Run_Demo(void)
    // builds and activates two forth-monitor tasks,
    // each using a separate channel on the uart module.
{
    Set_UART_Number(UART_MODULE_NUM);
    if(Default_UART_Init(UART_MODULE_NUM))  // initialize the hardware
        printf("\nError: Invalid protocol combination was specified!\r\n");
    else
    {
        printf("\nStarting UART Module Demo...\r\n");
        SERIAL_ACCESS = RELEASE_ALWAYS;     // ensure lots of PAUSEs in Forth task
        NEXT_TASK = TASKBASE;  // required! empty the round-robin task loop
        // CH1_Monitor();
        TASKBASE->USER_AREA.user_nextTask =(struct userArea *) TASKBASE;
 
        BUILD_C_TASK(0,0,&ch1_task);       // no heap needed
        BUILD_C_TASK(0,0,&ch2_task);       // no heap needed
 
        ACTIVATE(CH1_Monitor,&ch1_task);
        ACTIVATE(CH2_Monitor,&ch2_task);
 
        StartTimeslicer();  // enable task switching
    }
}
 
int main(void)
{  Run_Demo();
   return 0;
}


The UART (Universal Asynchronous Receiver Transmitter) Wildcard provides two full-duplex serial ports that can be configured for RS232, RS422, and RS485 protocols to implement the communications links that are often needed in instrument control applications. This tiny 2" by 2.5" board is a member of the Wildcard™ series that connects to Mosaic's line of microcontroller-based embedded computers. A UART chip (sometimes called a USART) on the Wildcard implements the conversion between the parallel Wildcard bus and the RS232 and RS485 asynchronous serial links. The UART Wildcard makes it easy to implement implement the serial communications links that are often needed for instrumentation and automation solutions.



See also → UART Forth Demo
UART Glossary
UART Wildcard User Guide

 
This page is about: C Language Program for Controlling RS232, RS422, and RS485 Serial Communications Protocols – A C demonstration program that provides examples of how to invoke the UART Wildcard driver functions
 
 
Navigation