📄 rtosinit.c
字号:
/*********************************************************************
* SEGGER MICROCONTROLLER SYSTEME GmbH *
* Solutions for real time microcontroller applications *
**********************************************************************
File : RTOSInit.c
Purpose : Initializes the hardware for embOS.
Feel free to modify this file acc. to your
target system.
---------------------------END-OF-HEADER------------------------------
*/
#include "mb90545.h"
#include "RTOS.H"
/*********************************************************************
*
* Configuration
*
**********************************************************************
*/
#ifndef OS_FSYS
/* Internal clock of system timer. Value depends on prescaler, */
/* external crystal and PLL settings. */
#define OS_FSYS 2000000L
#endif
#ifndef OS_UART
#define OS_UART 1
#endif
#ifndef OS_BAUDRATE
/* 9600, 19200 and 38400 are allowed */
/* if internal CPU-Clock is 16MHz */
#define OS_BAUDRATE 38400L
#endif
/*********************************************************************
*
* OS_InitHW
*
**********************************************************************
*/
void OS_InitHW(void) {
/* Initialize system timer with 1ms resolution */
/* Reload timer 0 is used. If any other timer should be used, */
/* modify this init routine and also modify */
/* Interrupt vector tables (vectors.c) */
ICR03 = 7; /* First disable interrupt */
TMRLR0 = (OS_FSYS/1000-1); /* Generate Interrupts every 1ms */
TMCSR0 = (1<<10) /* Clock source: uPClk divided by 8 */
|(1<<4) /* Reload Mode */
|(1<<3) /* Interrupt enable */
|(1<<1) /* Count enable */
|(1<<0); /* Trigger to start timer */
ICR03 = 6; /* Enable, lowest priority */
OS_COM_Init(); /* Initialize comm. to embOSView */
}
/*********************************************************************
*
* Idle Loop
*
**********************************************************************
Please note:
This is basically the "core" of the idle loop.
This core loop can be changed, but:
The idle loop does not have a stack of its own, therefore no
functionality should be implemented that relies on the stack
to be preserved. However, a simple program loop can be programmed
(like toggeling an output or incrementing a counter)
*/
void OS_Idle(void) { /* Idle loop: No task is ready to exec */
OS_EI();
for (;;);
}
/*********************************************************************
*
* Run-time error reaction (OS_Error)
*
**********************************************************************
Run-time error reaction
When this happens, a fatal error has occured and the kernel
can not continue. In linux, the equivalent would be a
"kernel panic"
This routine can be modified to suit your needs ...
E.g. a red LED could light up. When using an
emulator, you may set a breakpoint here.
In the release builds of the library (R), this routine is not required
(as no checks are performed).
In the stack check builds (S/SP), only error 120 may occur.
In the debug builds(D/DP), all of the listed errors may occur.
The following are the current errorcodes:
(Please refer to the documentation for more info)
OS_ERR_STACK (120)
*** invalid or non-initialized data structures ***
OS_ERR_INV_TASK (128)
OS_ERR_INV_TIMER (129)
OS_ERR_INV_MAILBOX (130)
OS_ERR_INV_CSEMA (132)
OS_ERR_INV_RSEMA (133)
*** Using GetMail1 or PutMail1 or
GetMailCond1 or PutMailCond1 on a non-1 byte mailbox ***
OS_ERR_MAILBOX_NOT1 (135)
***internal errors, please contact SEGGER Microcontrollersysteme ***
OS_ERR_MAILBOX_NOT_IN_LIST (140)
OS_ERR_TASKLIST_CORRUPT (142)
*** not matching routine calls or macro usage ***
OS_ERR_UNUSE_BEFORE_USE (150)
OS_ERR_LEAVEREGION_BEFORE_ENTERREGION (151)
OS_ERR_LEAVEINT (152)
OS_ERR_DICNT (153)
OS_ERR_INTERRUPT_DISABLED (154)
*** not a legal system call during interrupt ***
OS_ERR_ILLEGAL_IN_ISR (160)
*** not a legal system call during timer ***
OS_ERR_ILLEGAL_IN_TIMER (161)
** double used data structures **
OS_ERR_2USE_TASK (170)
OS_ERR_2USE_TIMER (171)
OS_ERR_2USE_MAILBOX (172)
OS_ERR_2USE_BSEMA (173)
OS_ERR_2USE_CSEMA (174)
OS_ERR_2USE_RSEMA (175)
*/
void OS_Error(int ErrCode) {
OS_EnterRegion(); /* Avoid further task switches */
OS_DICnt =0; /* Allow interrupts so we can communicate */
OS_EI();
OS_Status = ErrCode;
while (OS_Status);
}
/*********************************************************************
*
* Get time [cycles]
*
**********************************************************************
This routine is required for task-profiling only. It returns
the system time in clock cycles.
*/
OS_U32 OS_GetTime_Cycles(void) {
OS_U32 time = OS_Time+1;
unsigned int t_cnt = TMR0;
/* Check if timer interrupt pending ... */
if (TMCSR0_UF) {
t_cnt = TMR0;
time++;
}
return (OS_FSYS/1000)*time - t_cnt;
}
/*********************************************************************
*
* Convert cycles to microseconds
*
**********************************************************************
Convert Cycles into micro seconds. When using 20.0 MHz, this
means dividing by 20. If you have a different clock frequency,
you may have to modify this routine in order to get proper
diagonstics. Please note: Operation of theOS is not affected
by this routine.
*/
OS_U32 OS_ConvertCycles2us(OS_U32 Cycles) {
return Cycles/(OS_FSYS/1000000);
}
/*********************************************************************
*
* Communication UART 0
*
**********************************************************************
*/
#if OS_UART == 0
#if OS_BAUDRATE == 38400
#define BRG_DIV 0x0C
#elif OS_BAUDRATE == 19200
#define BRG_DIV 0x2C
#elif OS_BAUDRATE == 9600
#define BRG_DIV 0x3C
#else
#error Baudrate OS_BAUDRATE setting no supported !!!
#endif
void OS_COM_Init(void) {
/* Initialize UART port for profiling */
/* Set dataformat to 8N1 */
/* Initialize Baudrate geneartor */
/* Enable Rx, Tx and Rx- Tx-Interrupts */
DDR4_D40 = 1; /* Activate SOT0 as output */
ICR13 = 0x06; /* Setup ICR, lowest priority */
UMC0 = 0x19; /* set data format 8N1 */
URD0 = 0x3C; /* set selected baud rate */
USR0 = 0x0C; /* enable/disable interrupts (00011100) */
}
void OS_COM_Send1(unsigned char c) {
UODR0 = c; /* Write character */
USR0_TIE = 1; /* Enable Tx interrupt */
}
/* OS_Com Receiver interrupt handler */
__interrupt void OS_Com_ISR_rx (void) {
int Data;
OS_EnterNestableInterrupt();
Data = UIDR0;
if (USR0_ORFE) { /* Check if errors occurred */
UMC0_RFC = 0; /* Reset error */
} else {
OS_OnRx(Data);
}
OS_LeaveNestableInterrupt();
}
/* OS_Com Transmitter interrupt handler */
__interrupt void OS_Com_ISR_tx (void) {
OS_EnterInterrupt();
if (OS_OnTx()) {
/* No more characters to send => disable Tx interrupt */
USR0_TIE = 0;
}
OS_LeaveInterrupt();
}
/*********************************************************************
*
* Communication UART 1
*
**********************************************************************
*/
#elif OS_UART == 1
#if OS_BAUDRATE == 38400
#define BRG_DIV 0x0E
#elif OS_BAUDRATE == 19200
#define BRG_DIV 0x0C
#elif OS_BAUDRATE == 9600
#define BRG_DIV 0x08
#else
#error Baudrate OS_BAUDRATE setting no supported !!!
#endif
void OS_COM_Init(void) {
/* Initialize UART port for profiling */
/* Set dataformat to 8N1 */
/* Initialize Baudrate geneartor */
/* Enable Rx, Tx and Rx- Tx-Interrupts */
SCR1 = (0<<7) /* Disable Parity */
|(0<<6) /* Parity EVEN */
|(0<<5) /* 1 Stop bit */
|(1<<4) /* 8 Data bits */
|(0<<3) /* Data Frame */
|(0<<3) /* Reset Rx error */
|(0<<1) /* Disable Rx */
|(0<<0); /* Disable Tx */
SSR1 = 0x00; /* Disable Rx- Tx- interrupts */
U1CDCR = (1<<7) /* enable prescaler */
|BRG_DIV; /* with specified divisor */
SMR1 = 0x01;
ICR14 = 0x06; /* Setup ICR, lowest priority */
SCR1_RXE = 1; /* Enable Rx, Tx */
SCR1_TXE = 1;
SSR1_RIE = 1; /* Enable Interrupts */
SSR1_TIE = 1;
}
void OS_COM_Send1(unsigned char c) {
SODR1 = c; /* Write character */
SSR1_TIE = 1; /* Enable Tx interrupt */
}
/* OS_Com Receiver interrupt handler */
__interrupt void OS_Com_ISR_rx (void) {
int Data;
OS_EnterNestableInterrupt();
Data = SIDR1;
if (SSR1 & 0xE0) { /* Check if errors occurred */
SCR1_REC = 0; /* Reset error */
} else {
OS_OnRx(Data);
}
OS_LeaveNestableInterrupt();
}
/* OS_Com Transmitter interrupt handler */
__interrupt void OS_Com_ISR_tx (void) {
OS_EnterInterrupt();
if (OS_OnTx()) {
/* No more characters to send => disable Tx interrupt */
SSR1_TIE = 0;
}
OS_LeaveInterrupt();
}
#else /* UART handlers not defined, using dummys */
void OS_COM_Init(void) {}
void OS_COM_Send1(OS_U8 c) {
/* putchar(c); */c = c;
OS_COM_ClearTxActive(); /* let OS know that transmitter is not busy */
}
#endif
/*********************************************************************
*
* OS_Tick ISR
*
**********************************************************************
*/
#pragma intvect OS_Timer_IRQHandler 17 /* 16-bit reload timer #0 */
__interrupt void OS_Timer_IRQHandler (void) {
OS_DI(); /* We have to disable interrupts first ! */
TMCSR0 &= ~(1<<2); /* Reset Timer Underflow flag (clear Int) */
OS_TickHandler();
}
/*********************************************************************
*
* Interrupt vectors
*
**********************************************************************
*/
/*------------------------------------------------------------------------
DefaultIRQHandler()
This function is a placeholder for all vector definitions. Either use
your own placeholder or add necessary code here.
*/
__interrupt void DefaultIRQHandler (void) {
__DI(); /* disable interrupts */
while(1)
__wait_nop(); /* halt system */
}
#pragma intvect DefaultIRQHandler 18
#pragma intvect DefaultIRQHandler 19
#pragma intvect DefaultIRQHandler 20
#pragma intvect DefaultIRQHandler 21
#pragma intvect DefaultIRQHandler 22
#pragma intvect DefaultIRQHandler 23
#pragma intvect DefaultIRQHandler 24
#pragma intvect DefaultIRQHandler 25
#pragma intvect DefaultIRQHandler 26
#pragma intvect DefaultIRQHandler 27
#pragma intvect DefaultIRQHandler 28
#pragma intvect DefaultIRQHandler 29
#pragma intvect DefaultIRQHandler 30
#pragma intvect DefaultIRQHandler 31
#pragma intvect DefaultIRQHandler 32
#pragma intvect DefaultIRQHandler 33
#pragma intvect DefaultIRQHandler 34
#pragma intvect DefaultIRQHandler 35
#ifdef OSTEST /* Used for SEGGER internal tests ... feel free to eliminate this section */
__interrupt void OSTEST_ISR0(void); /* For embOS test program only.*/
#pragma intvect OSTEST_ISR0 36 /* Reload timer 1 is used */
#else
#pragma intvect DefaultIRQHandler 36 /* Reload timer 1 */
#endif
#if OS_UART == 0
#pragma intvect OS_Com_ISR_rx 37
#pragma intvect OS_Com_ISR_tx 38
#else
#pragma intvect DefaultIRQHandler 37
#pragma intvect DefaultIRQHandler 38
#endif
#if OS_UART == 1
#pragma intvect OS_Com_ISR_rx 39
#pragma intvect OS_Com_ISR_tx 40
#else
#pragma intvect DefaultIRQHandler 39
#pragma intvect DefaultIRQHandler 40
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -