#include "common.h"
#include "SD.h"
#include "boostc.h"
#include "PIC16F1938.h"
#define FLAG_TIMEOUT 0x80
#define FLAG_PARAM_ERR 0x40
#define FLAG_ADDR_ERR 0x20
#define FLAG_ERZ_SEQ_ERR 0x10
#define FLAG_CMD_CRC_ERR 0x08
#define FLAG_ILLEGAL_CMD 0x04
#define FLAG_ERZ_RST 0x02
#define FLAG_IN_IDLE_MODE 0x01
#pragma CLOCK_FREQ 32000000
#pragma DATA _CONFIG1, _FOSC_INTOSC & _WDTE_SWDTEN & _PWRTE_ON & _MCLRE_OFF & _CP_ON & _CPD_ON & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
#pragma DATA _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _LVP_OFF
void UARTInit(){
//
// UART
//
baudcon.4 = 0; // SCKP synchronous bit polarity
baudcon.3 = 0; // BRG16 enable 16 bit brg
baudcon.1 = 0; // WUE wake up enable off
baudcon.0 = 0; // ABDEN auto baud detect
txsta.6 = 0; // TX9 8 bit transmission
txsta.5 = 1; // TXEN transmit enable
txsta.4 = 0; // SYNC async mode
txsta.3 = 0; // SEDNB break character
txsta.2 = 0; // BRGH high baudrate
txsta.0 = 0; // TX9D bit 9
rcsta.7 = 1; // SPEN serial port enable
rcsta.6 = 0; // RX9 8 bit operation
rcsta.5 = 1; // SREN enable receiver
rcsta.4 = 1; // CREN continuous receive enable
spbrgh = 0; // brg high byte
spbrg = 51; // brg low byte ()
apfcon.2=1; // tx onto RA.0
}
void UARTSend(unsigned char c){
txreg = c;
while(!txsta.1);
}
void UARTPrint(char *s){
while(*s) {
UARTSend(*s);
s++;
}
}
void UARTPrint(char c){UARTSend(c);}
void UARTPrintNumber(unsigned long n){
unsigned long k=1000000000;
while(k>0) {
UARTSend('0'+n/k);
n%=k;
k/=10;
}
}
void UARTPrintLn(char *s){
UARTPrint(s);
UARTPrint("\r\n");
}
void UARTByte(unsigned char b){
const char *hex="0123456789abcdef";
UARTPrint("0x");
UARTSend(hex[b>>4]);
UARTSend(hex[b & 0xf]);
}
void UARTLog(char *s, unsigned short iv){
UARTPrint(s);
UARTPrint(" ");
UARTPrintNumber(iv);
UARTPrintLn(" ");
}
void fatal(UInt8 val){ //fatal error: flash led then go to sleep
UInt8 i, j, k;
for(j = 0; j < 5; j++){
for(k = 0; k < val; k++)
{
delay_ms(100);
P_LED = 1;
delay_ms(100);
P_LED = 0;
}
UARTLog("Error",val);
delay_ms(250);
delay_ms(250);
}
while(1){
asm sleep
}
}