📄 hwdriver.c
字号:
//#include "includes.h"
#include <p18cxxx.h>
#include "includes.h"
#include "comstruct.h"
extern OS_MEM *pMemSml;
extern OS_MEM *pMemLge;
extern OS_EVENT *rom peventDEBUG;
extern OS_EVENT *rom peventWICResp;
extern OS_EVENT *rom peventHardware;
//zwm for debug
extern unsigned char DebugReadLength;
extern unsigned char DebugContent[48];
extern void Do_Key_Init(void);
extern unsigned char Key_Scan(void);
/***************************************************************************************************************/
#define COM_DBG 0 //The COM port is occupied by the DEBUG interface
#define COM_WLM 1 //The COM port is occupied by the wireless module interface
#define COM_DBG_B 2 //The COM port is busy on a DEBUG session debug message
//In such state, a port switch is forbiden
#define COM_WLM_B 3 //The COM port is busy on a session with wireless module
//In such state, a port switch is forbiden
/***************************************************************************************************************/
//values for IO pins direction
#define P_INPUT 1
#define P_OUTPUT 0
//Defination for PORTA
#define RST_WL LATAbits.LATA0 //Reset wireless module
#define PA0_DIRECT P_OUTPUT
#define KSI4 LATAbits.LATA1 //Key scan output
#define PA1_DIRECT P_OUTPUT
#define PA2_DIRECT P_INPUT //......
#define PA3_DIRECT P_INPUT //......
#define EN_HF LATAbits.LATA4 //Hand free amplifier control
#define PA4_DIRECT P_OUTPUT
#define RST_WL2 LATAbits.LATA5 //Secondary reset control pin of wirelless module, ON/OFF
#define PA5_DIRECT P_OUTPUT
//PortB
#define PB0_DIRECT P_INPUT //INT0
#define PB1_DIRECT P_INPUT //INT1
#define PB2_DIRECT P_INPUT //INT2
#define PB3_DIRECT P_INPUT //......
#define KEY_PRESS PORTBbits.RB4 //Key press state
#define PB4_DIRECT P_INPUT
#define HANDLE PORTBbits.RB5 //Handle state
#define PB5_DIRECT P_INPUT
//leave RB6, RB7 for debug pins
//PortC
#define RST_LCD LATCbits.LATC0 //Reset LCD
#define PC0_DIRECT P_OUTPUT
#define LED_CTL LATCbits.LATC1 //Control keypad leds and background light of LCD
#define PC1_DIRECT P_OUTPUT
#define PC2_DIRECT P_INPUT //......
//RC3, RC4 for I2C
#define WP LATCbits.LATC5 //Write protect control of serial EEPROM
#define PC5_DIRECT P_OUTPUT
//RC6, RC7 for USART
//PortD as system bus
//PortE as system bus
//PortF
#define KSO0 PORTFbits.RF0 //Key scan input
#define PF0_DIRECT P_INPUT
#define KSO1 PORTFbits.RF1 //Key scan input
#define PF1_DIRECT P_INPUT
#define KSI1 LATFbits.LATF2 //Key scan output
#define PF2_DIRECT P_OUTPUT
#define PF3_DIRECT P_INPUT //......
#define PF4_DIRECT P_INPUT //......
#define PF5_DIRECT P_INPUT //......
#define KSI2 LATFbits.LATF6 //Key scan output
#define PF6_DIRECT P_OUTPUT
#define KSI3 LATFbits.LATF7 //Key scan output
#define PF7_DIRECT P_OUTPUT
//PortG as system bus control
//PortH
#define KSI0 LATHbits.LATH4 //Key scan output
#define PH4_DIRECT P_OUTPUT
#define KSO4 PORTHbits.RH5 //Key scan input
#define PH5_DIRECT P_INPUT
#define KSO3 PORTHbits.RH6 //Key scan input
#define PH6_DIRECT P_INPUT
#define KSO2 PORTHbits.RH7 //Key scan input
#define PH7_DIRECT P_INPUT
//PortJ as system bus
#pragma romdata EXTRAM
INT8U ComCurStt;
INT8U chrCOMBusy;
INT16U intTemp1;
INT8U rom * ptrTemp2;
INT16U intCurRxNum;
INT8U rom * pCurRxTail;
INT8U rom * pCurRxHead;
INT16U intCurTxNum;
INT8U rom * pCurTxHead;
INT8U rom *pMemT;
INT8U rom *pMemT2;
INT8U erri;
INT8U chrKeyTemp;
INT8U chrCurHandle;
#pragma romdata COMMODE_P = 0x131e00
volatile unsigned char rom ComCtl;
#pragma code MYCODE
/***********************************************Board test******************************************************/
void Func_Trap(void){
for(;;){
}
}
/***************************************************************************************************************/
void Func_Brd_Init(void){
//Init the oscillator system
PSPCONbits.CMLK1 = 1;
PSPCONbits.CMLK0 = 1;
OSCCON = 0b00000000; //Disable the PLL, use primary oscillator input
Nop(); //Wait until the lock closed
Nop();
//Init the external memory interface and chip select pins
PSPCONbits.CMLK1 = 1;
PSPCONbits.CMLK0 = 1;
MEMCON = 0b00110000;
Nop(); //Wait until the lock closed
Nop();
PSPCONbits.CMLK1 = 1;
PSPCONbits.CMLK0 = 1;
CSEL2 = 0x80; //External RAM, 128K bytes start from 0x100000
Nop(); //Wait until the lock closed
Nop();
PSPCONbits.CMLK1 = 1;
PSPCONbits.CMLK0 = 1;
CSELIO=0x98; //IO address 8k bytes start from 0x130000
//Close all the interrupts enable, priority and flag
INTCON = 0;
INTCON2 = 0;
INTCON3 = 0;
IPR2 = 0;
IPR1 = 0;
PIR2 = 0;
PIR1 = 0;
PIE2 = 0;
PIE1 = 0;
RCONbits.IPEN = 1; //Enable interrupt priority
INTCON2bits.RBIP = 0; //Set RB port change interrupt low priority
INTCONbits.RBIE = 0; //Disanable RB port change interrupt
INTCON2bits.TMR0IP = 0; //Set timer0 interrupt low priority
T0CONbits.TMR0ON = 0; //Disable timer0
//Set I/O pins to proper direction
//If the function of pins is changed, this part shall be modified
//PortA
TRISAbits.TRISA0 = PA0_DIRECT;
TRISAbits.TRISA1 = PA1_DIRECT;
TRISAbits.TRISA2 = PA2_DIRECT;
TRISAbits.TRISA3 = PA3_DIRECT;
TRISAbits.TRISA4 = PA4_DIRECT;
TRISAbits.TRISA5 = PA5_DIRECT;
//PortB
TRISBbits.TRISB0 = PB0_DIRECT;
TRISBbits.TRISB1 = PB1_DIRECT;
TRISBbits.TRISB2 = PB2_DIRECT;
TRISBbits.TRISB3 = PB3_DIRECT;
TRISBbits.TRISB4 = PB4_DIRECT;
TRISBbits.TRISB5 = PB5_DIRECT;
//leave RB6, RB7 for debug pins
//PortC
TRISCbits.TRISC0 = PC0_DIRECT;
TRISCbits.TRISC1 = PC1_DIRECT;
TRISCbits.TRISC2 = PC2_DIRECT;
//RC3, RC4 for I2C
TRISCbits.TRISC5 = PC5_DIRECT;
//RC6, RC7 for USART
//PortD as system bus
//PortE as system bus
//PortF
TRISFbits.TRISF0 = PF0_DIRECT;
TRISFbits.TRISF1 = PF1_DIRECT;
TRISFbits.TRISF2 = PF2_DIRECT;
TRISFbits.TRISF3 = PF3_DIRECT;
TRISFbits.TRISF4 = PF4_DIRECT;
TRISFbits.TRISF5 = PF5_DIRECT;
TRISFbits.TRISF6 = PF6_DIRECT;
TRISFbits.TRISF7 = PF7_DIRECT;
//PortG as system bus control
//PortH
//RH0~RH3 as system bus
TRISHbits.TRISH4 = PH4_DIRECT;
TRISHbits.TRISH5 = PH5_DIRECT;
TRISHbits.TRISH6 = PH6_DIRECT;
TRISHbits.TRISH7 = PH7_DIRECT;
//
ADCON1 = 0x0f; //Set all PORT pins digital
}
void Func_Start_TMR1(void){
IPR1bits.TMR1IP = 0; //set timer1 interrupt low priority
T1CON = 0b10110101;
TMR1H = 0xfb;
TMR1L = 0xa0;
PIE1bits.TMR1IE = 1; //Enable timer1 interrupt
}
void Func_Start_UART(void){
TXSTAbits.TX9 = 0; //select 8bit per byte
RCSTAbits.RX9 = 0;
TXSTAbits.TXEN = 1; //enable transmit
TXSTAbits.SYNC = 0; //asynchronous mode
TXSTAbits.BRGH = 1; //high baud rate
RCSTAbits.SPEN = 1; //enable serial port
RCSTAbits.CREN = 1; //enable continous receive
SPBRG = 22; //select baud rate
IPR1bits.RCIP = 1; //set RX high priority
IPR1bits.TXIP = 0; //set TX low priority
PIE1bits.RCIE = 0; //close interrupt, it will be open in tskHardware
PIE1bits.TXIE = 0;
INTCON3bits.INT2E = 1; //enable INT2 for receive
T0CON = 0b00000110; //Timer0 is used for receiver time out
}
void Func_Start_I2C(void){
SSPSTATbits.SMP = 0;
SSPCON1 = 0x08; //select I2C master mode
SSPCON1bits.SSPEN = 1; //enable I2C
PIR2bits.BCLIF = 0;
SSPADD = 10;
WP = 0; //Enable write to eeprom
}
unsigned char Func_ChangeCOMMode(unsigned char mode){
if(mode > 3) return (1); //error mode
OS_ENTER_CRITICAL();
ComCtl = mode;
intCurTxNum = 0;
pCurTxHead = (INT8U rom *)0;
PIE1bits.TXIE = 0;
chrCOMBusy = FALSE;
intCurRxNum = 0;
pCurRxTail = pCurRxHead;
ComCurStt = mode;
OS_EXIT_CRITICAL();
}
unsigned char Func_ReadCOMMode(void){
return (ComCtl);
}
void Func_ResetWM(void){
unsigned int i;
RST_WL = 1;
for(i=0;i<1000;i++){
Nop();
}
RST_WL = 0;
RST_WL2 = 1;
}
//Serial port transmit interrupt service function, called
//from low priority interrupt
void Func_Transmit_ISR(void){
unsigned char i;
if(STKPTR >= 0x10){
//for(;;){
Nop();
Nop();
DebugReadLength=STKPTR+STKPTR+STKPTR;
for(i=0;i<DebugReadLength;){
DebugContent[i++]=TOSL;
DebugContent[i++]=TOSH;
DebugContent[i++]=TOSU;
if(i>=48) break;
STKPTR--;
}
/* _asm
movf STKPTR,0,0
decf WREG,1,0
movwf STKPTR,0
movff TOSU, TBLPTRU
movff TOSH, TBLPTRH
movff TOSL, TBLPTRL
_endasm
*/
for(;;){
Nop();
Nop();
}
//}
}
if (PIE1bits.TXIE){
if(PIR1bits.TXIF){//May be the TX is not enabled
if (intCurTxNum != 0){
TXREG = *pCurTxHead++;
intCurTxNum --;
}
else{
PIE1bits.TXIE = 0; //Close transmit interrupt
chrCOMBusy = FALSE;
OSQPost(peventHardware, (void rom *)MSG_COM0_TXFINISH);
}
}
// PIR1bits.TXIF = 0;
}
}
void Func_INT2_ISR(void){
if(INTCON3bits.INT2F){
INTCON3bits.INT2F = 0;
INTCONbits.GIEH = 0;
if(intCurRxNum != 0){
INTCONbits.GIEH = 1;
pMemT = OSMemGet(pMemLge, &erri);
if(erri != OS_NO_ERR) {
INTCONbits.GIEH = 0;
intCurRxNum = 0;
pCurRxTail = pCurRxHead;
INTCONbits.GIEH = 1;
return;
}
pMemT2 = OSMemGet(pMemSml, &erri);
if(erri != OS_NO_ERR) {
INTCONbits.GIEH = 0;
intCurRxNum = 0;
pCurRxTail = pCurRxHead;
INTCONbits.GIEH = 1;
return;
}
INTCONbits.GIEH = 0;
intTemp1 = intCurRxNum;
ptrTemp2 = pCurRxHead;
intCurRxNum = 0;
pCurRxTail = pMemT;
pCurRxHead = pMemT;
INTCONbits.GIEH = 1;
//make the message and send it
if(ComCurStt = S_DEBUG){
((MSG_HEAD *)pMemT2)->Msg_ID = MSG_DEBUG_RECEIVE;
}
else{
((MSG_HEAD *)pMemT2)->Msg_ID = MSG_MODULE_RECEIVE;
}
((MSG_HEAD *)pMemT2)->Origin = peventHardware;
((MSG_HEAD *)pMemT2)->pmemME = pMemSml;
((MSG_HEAD *)pMemT2)->Attached = TRUE;
((MSG_HEAD *)pMemT2)->LenOfAttach = intTemp1;
((MSG_HEAD *)pMemT2)->pMem = ptrTemp2;
((MSG_HEAD *)pMemT2)->pmemATT = pMemLge;
((MSG_HEAD *)pMemT2)->LenOfBody = 0;
if(ComCurStt = S_DEBUG){
OSQPost(peventDEBUG, pMemT2);
}
else{
OSQPost(peventWICResp, pMemT2);
}
}
INTCONbits.GIEH = 1;
}
}
void Func_TMR0_ISR(void){
if(INTCONbits.TMR0IE && INTCONbits.TMR0IF){
INTCONbits.TMR0IE = 0;
INTCONbits.TMR0IF = 0;
T0CONbits.TMR0ON = 0;
INTCONbits.GIEH = 0;
if(intCurRxNum) INTCON3bits.INT2F = 1;
INTCONbits.GIEH = 1;
}
}
void Func_RB_ISR(void){
while(INTCONbits.RBIE && INTCONbits.RBIF){
INTCONbits.RBIF = 0;
if(PORTBbits.RB4 == 0){//Key pressed
chrKeyTemp = Key_Scan();
Do_Key_Init();
//Send key press message
}
else{
}
if(PORTBbits.RB5 != chrCurHandle){
chrCurHandle = PORTBbits.RB5;
//Send handle message
}
else{
}
}
}
//Serial port receive ISR, called from high priority interrupt.
//contains two parts, one for receive and one for time out.
//the data received is handled by the INT2 ISR in the low
//priority ISR
void Func_Receive_ISR(void){
if(PIR1bits.RCIF){
PIR1bits.RCIF = 0;
//intCurRxNum = RCREG;
//intCurRxNum = 0;
*pCurRxTail++ = RCREG;
intCurRxNum ++;
TMR0H = RXTIMEOUTH;
TMR0L = RXTIMEOUTL;
T0CONbits.TMR0ON = 1;
INTCONbits.TMR0IE = 1;
if(intCurRxNum >= RXTHREHOLD) INTCON3bits.INT2F = 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -