Simple RTOS for Microchip Baseline and Midrange MCUs
//============================================================================== // SimpleRTOS - Very simple RTOS for Microchip(R) Baseline and Midrange uCs // v1.00 (2008-09-23) // isaacbavaresco@yahoo.com.br //============================================================================== /* Copyright (c) 2007-2008, Isaac Marino Bavaresco All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ //============================================================================== /* Notes: - Works with Hi-Tech PICC v9.60PL2; - Assembler optimizations must be OFF for every file that implements task functions (it is better to have the task functions in one or more files with no standard functions, to minimize the un-optimized code length). - Every task function must have a call to the macro "TASK_INIT( <task function name> );" as the first statement; - Every function called directly or indirectly by more than one task must have a call to the macro "FUNC_INIT( <function name> );" as the first statement, unless it doesn't have any parameters nor automatic variables, or all paths through the call-graphs that it belongs have at least one function that calls "FUNC_INIT" before the function is called. Unfortunately this impose certain restrictions when using library functions, unless each library function is called from just one task, or it is present only in call-graphs that at least one function calls "FUNC_INIT" before the library function is called, or you rebuild the library adding the call to "FUNC_INIT" to the library functions that need it. - SimpleRTOS functions and macros that may cause a context switch (Eg: Yield, Sleep, DeleteTask (when deleting itself) and MutexTake ) must be called only directly inside the task function, never indirectly by other functions. TO DO: - Find a way to reuse local variables of tasks that are never active at the same time. - Implement semaphores. - Implement priorities and slice length. */ //============================================================================== // Set TAB width to 4 characters //============================================================================== // // PIC16F648A // // +-----------------U-----------------+ // | | // <- ==| 1 RA2/AN2/Vref AN1/RA1 18 |== -> // | | // <- ==| 2 RA3/AN3/CMP1 AN0/RA0 15 |== -> // | | // <- ==| 3 RA4/T0CKI/CMP2 OSC1/RA7 16 |== <x // | | // -> ==| 4 RA5/!MCLR OSC2/RA6 15 |== x> // | | // ==| 5 GND VCC 14 |== // | | // <- ==| 6 RB0/INT T1OSI/RB7 13 |== -> // | | // RXD -> ==| 7 RB1/RX/DT T1CKI/T1OSO/RB6 12 |== -> // | | // TXD <- ==| 8 RB2/TX/CK RB5 11 |== -> // | | // <- ==| 9 RB3/CCP1 RB4 10 |== -> // | | // +-----------------------------------+ // // //============================================================================== #include <pic.h> #include "SimpleRTOS.h" void memcpy( const void *dst, const void *src, unsigned char len ); //============================================================================== __CONFIG( CPD & PROTECT & BORDIS & LVPDIS & MCLRDIS & PWRTEN & WDTEN & HS ); //============================================================================== extern volatile unsigned short TMR1 @ 0x00e; extern volatile unsigned short CCPR1 @ 0x015; //============================================================================== #define CLOCK 20000000ul #define BAUD_RATE 19200ul #define TICK_RATE 1000ul #define COUNTER_RATE 100ul //============================================================================== unsigned char bank1 SerialRXBuffer[32]; unsigned char SerialRXLength = 0; unsigned char SerialRXInsert = 0; unsigned char bank1 SerialTXBuffer[16]; unsigned char SerialTXLength = 0; unsigned char SerialTXRemove = 0; bit Overrun = 0; //============================================================================== void interrupt ISR( void ) { //-------------------------------------------------------------------------- if( CCP1IF && CCP1IE ) { Ticks++; CCP1IF = 0; } //-------------------------------------------------------------------------- if( RCIF && RCIE ) { if( OERR ) { CREN = 0; CREN = 1; Overrun = 1; } if( SerialRXLength < sizeof SerialRXBuffer ) { SerialRXBuffer[SerialRXInsert] = RCREG; if( ++SerialRXInsert >= sizeof SerialRXBuffer ) SerialRXInsert = 0; SerialRXLength++; } else { (void)RCREG; Overrun = 1; } } //-------------------------------------------------------------------------- if( TXIF && TXIE ) { if( SerialTXLength != 0 ) { TXREG = SerialTXBuffer[SerialTXRemove]; if( ++SerialTXRemove >= sizeof SerialTXBuffer ) SerialTXRemove = 0; if( --SerialTXLength == 0 ) TXIE = 0; } else TXIE = 0; } //-------------------------------------------------------------------------- } //============================================================================== // Receive one char from the UART. If there is no available char, returns -1. short SerialReceive( void ) { unsigned char c; if( SerialRXLength == 0 ) return -1; GIE = 0; c = SerialRXBuffer[ ( SerialRXInsert - SerialRXLength ) & ( sizeof SerialRXBuffer - 1 ) ]; SerialRXLength--; GIE = 1; return (unsigned short)c; } //============================================================================== bit SerialTransmit( unsigned char c ) { // Needed because this function is called by more than one task. FUNC_INIT( SerialTransmit ); if( SerialTXLength >= sizeof SerialTXBuffer ) return 0; GIE = 0; SerialTXBuffer[ ( SerialTXRemove + SerialTXLength ) & ( sizeof SerialTXBuffer - 1 ) ] = c; SerialTXLength++; TXIE = 1; GIE = 1; return 1; } //============================================================================== bit TransmitInProgress( void ) { return SerialTXLength || ! TRMT ? 1 : 0; } //============================================================================== TS_Context Contexts[6]; // We will have a maximum of 6 simultaneously running tasks. //============================================================================== void MainTask( void ); void main( void ) { CMCON = 0x07; // Comparators off. PORTA = 0x00; // All PORTA pins off. PORTB = 0x06; TRISA = 0xe0; TRISB = 0x02; SPBRG = CLOCK / ( 16ul * BAUD_RATE ) - 1; TXSTA = 0x26; // 0010 0110 RCSTA = 0x90; // 1001 0000 CCPR1 = CLOCK / ( 4ul * TICK_RATE ); CCP1CON = 0x0b; // CCP1 in compare mode. TMR1 = 0; T1CON = 0x01; // Start TMR1. PIR1 = 0x00; // Clear all interrupt flags. PIE1 = 0x34; // Enable CCP1, UART RX and UART TX interrupts. INTCON = 0xc0; // Enable selected interrupts. // Initialize the contexts and the rest of the RTOS. InitRTOS( Contexts, sizeof Contexts / sizeof Contexts[0] ); // Create just one task, it will create the others. CreateTask( MainTask ); // From now, the RTOS will take over. StartRTOS(); } //==============================================================================
file: /Techref/member/IMB-yahoo-J86/TestMain.c.htm, 10KB, , updated: 2010/4/14 14:17, local time: 2024/11/25 23:43,
owner: IMB-yahoo-J86,
18.217.224.165:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://ecomorder.com/Techref/member/IMB-yahoo-J86/TestMain.c.htm"> Simple RTOS for Microchip Baseline and Midrange MCUs - TestMain.c</A> |
Did you find what you needed? |