added boostc files

This commit is contained in:
Ziver Koc 2009-06-15 13:00:32 +00:00
parent 19025584c9
commit 42b5801a03
11 changed files with 647 additions and 14 deletions

152
boostc/lcd.c Normal file
View file

@ -0,0 +1,152 @@
#include <system.h>
#include "lcd.h"
#include "util.h"
/*******************************************************************
http://forum.sparkfun.com/viewtopic.php?p=33650
EXAMPLE:
const unsigned char LCD_CHAR_BAR1[] = {
0x1F, // ---11111
0x10, // ---10000
0x10, // ---10000
0x10, // ---10000
0x10, // ---10000
0x10, // ---10000
0x10, // ---10000
0x1F // ---11111
};
LCD_Init();
LCDCls();
x = 0;
LCD_DefineChar (1, LCD_CHAR_BAR1);
LCD_DefineChar (2, LCD_CHAR_BAR3);
LCD_DefineChar (3, LCD_CHAR_BAR2);
LCD_DefineChar (4, LCD_CHAR_BAR4);
LCD_DefineChar (5, LCD_CHAR_BAR5);
LCD_SetPosition ( LINE_1 + 0 );
printf("\1\5\2 RF Terminal", 0, LCD);
*****************************************************************/
/*******************************************************************/
// Prototypes
/*******************************************************************/
void lcd_init ( void ){
lcd_setData (0x00);
delay_ms (200); /* wait enough time after Vdd rise */
LCD_RS = 0;
lcd_setData (0x03); /* init with specific nibbles to start 4-bit mode */
lcd_pulseEnable();
lcd_pulseEnable();
lcd_pulseEnable();
lcd_setData (0x02); /* set 4-bit interface */
lcd_pulseEnable(); /* send dual nibbles hereafter, MSN first */
lcd_putChar (0x2C, CMD);/* function set (all lines, 5x7 characters) */
lcd_putChar (0x0C, CMD);/* display ON, cursor off, no blink */
lcd_putChar (0x01, CMD);/* clear display */
lcd_putChar (0x06, CMD);/* entry mode set, increment & scroll left */
}
/*******************************************************************/
void lcd_setPosition ( unsigned char cX ){
/* this subroutine works specifically for 4-bit Port A */
lcd_setData ((cX >> 4) | 0x08);
lcd_pulseEnable();
lcd_setData (cX);//(swap(cX));
lcd_pulseEnable();
}
/*******************************************************************/
void lcd_putChar (unsigned char cX, unsigned char cmd){
// this subroutine works specifically for 4-bit Port A
if (cmd == DATA) LCD_RS = 1;// data?
lcd_setData (cX >> 4); // Send high nibble
lcd_pulseEnable();
lcd_setData (cX); // Send low nibble
lcd_pulseEnable();
if (cmd == DATA) LCD_RS = 0;
}
/*******************************************************************/
void lcd_pulseEnable (void){
LCD_EN = 1;
delay_us (5);
LCD_EN = 0;
delay_us (5);
}
/*******************************************************************/
void lcd_setData (unsigned char cX){
LCD_D0 = cX.0;
LCD_D1 = cX.1;
LCD_D2 = cX.2;
LCD_D3 = cX.3;
}
/*******************************************************************/
void lcd_cls(void){
lcd_putChar(CLEAR_DISP, CMD);
}
/*******************************************************************/
/*
** LCD_DefineCharacter: Define dot pattern for user-defined character.
**
** inputs: address = address of character (0x00-0x07)
** pattern = pointer to 8-byte array containing the dot pattern
*/
void lcd_defineChar (unsigned char address, const unsigned char *pattern){
char i, addr;
addr = (address << 3);
lcd_putChar(0x40 + addr, CMD);
for (i=0; i<8; i++) {
lcd_putChar(*pattern++, DATA);
}
}
/*******************************************************************/
/*******************************************************************/
void lcd_putc (char ch){ // Char to send thru rs-232
lcd_putChar (ch, DATA);
}
/*******************************************************************/
void lcd_puts (const char *str){ // String to send thru rs-232
char ps;
ps = *str; // Pointer to the string start in ps
while( ps>0 ){ // While not end...
str++; // Increment pointer
if (ps== 0) break; // If end of string... finish.
lcd_putc (ps);
ps = *str; // Point to new char
}
}
/*******************************************************************/
void lcd_putd(int decimal){ // prints a decimal number
unsigned char decimal_output[5];
unsigned char m, temp;
//Print negative sign and take 2's compliment
if(decimal < 0){
lcd_putc ('-');
decimal *= -1;
}
if (decimal == 0)
lcd_putc ('0');
else{
//Divide number by a series of 10s
for(m = 4 ; decimal > 0 ; m--){
temp = decimal % (unsigned int)10;
decimal_output[m] = temp;
decimal = decimal / (unsigned int)10;
}
for(m++ ; m < 5 ; m++)
lcd_putc(bin2Hex(decimal_output[m]));
}
}
/*******************************************************************/

35
boostc/lcd.h Normal file
View file

@ -0,0 +1,35 @@
/*******************************************************************/
// Defines
/*******************************************************************/
#define LCD_RS porta.0
#define LCD_EN porta.1
#define LCD_D0 porta.2
#define LCD_D1 porta.3
#define LCD_D2 porta.4
#define LCD_D3 porta.5
#define DISPLAY_COLS 16
#define LINE_1 0x00
#define LINE_2 0x40
#define LINE_3 0x10
#define LINE_4 0x50
#define CLEAR_DISP 0x01
#define ACTIVITY_SYMBOL 0xFF
#define DATA 1
#define CMD 0
/*******************************************************************/
// Functions
/*******************************************************************/
void lcd_init (void);
void lcd_setPosition (unsigned char cX);
void lcd_putChar (unsigned char cX, unsigned char cmd);
void lcd_pulseEnable (void);
void lcd_setData (unsigned char cX);
void lcd_cls (void);
void lcd_defineChar (unsigned char address, const unsigned char *pattern);
void lcd_putc (char ch);
void lcd_putd (int decimal);
void lcd_puts (const char *str);
void lcd_printf (const char *nate, int my_byte);

232
boostc/sdcard.c Normal file
View file

@ -0,0 +1,232 @@
/*
* http://ghmicro.com/index.php/pic/13-18f4620/11-18f4620-a-sd-card
*/
#include <system.h>
#include <memory.h>
#include "sdcard.h"
#include "spi.h"
char sd_output_buffer1[256];
char sd_output_buffer2[256];
char sd_input_buffer1[256];
char sd_input_buffer2[256];
void sd_init(void){
int i,r;
CSel=1; //init chip select (active low)
SD_LED=0; //LED off
for(i=0; i<256 ;i++){ //fill the send buffers
sd_output_buffer1[i]=0;
sd_output_buffer2[i]=0;
sd_input_buffer1[i]=0; //and clear the receive buffers
sd_input_buffer2[i]=0;
}
r = sd_initMedia();
if(r){ //card init failed - 1 blink
while(1){
SD_LED = 1;
delay_ms(150);
SD_LED = 0;
delay_s(1);
}
}
}
int sd_initMedia(void){
int i,r;
CSel = 1;
spi_putc(0xff); //while card is not selected
for(i=0;i<16;i++) //send 80 clock cycles to start up
spi_putc(0xff);
CSel = 0; //then select the card
r = sd_sendCmd(0,0); //send reset command to enter SPI mode
CSel = 1;
spi_putc(0xff); //disable SD
if(r != 1) //error check - need 1
return 0x84;
i = 10000; //send init for up to 0.3s
CSel = 0;
do{
r = sd_sendCmd(1,0); //send init command
CSel = 1;
spi_putc(0xff); //disable SD
if(!r) break;
}while(--i > 0);
if(i==0) //time out error 0x85
return 0x85;
sspcon1 = 0b00010000; //speed up spi clock
sspcon1.SSPEN = 1;
return 0;
}
int sd_writeSector(LBA a, char *p1, char *p2){
unsigned r,i;
SD_LED = 1; //turn on write LED
r = sd_sendCmd(24,(a<<9));
if(r == 0){
spi_putc(0xfe); //send Data Start byte
for(i=0;i<256;i++) //first 256 bytes
spi_putc(*p1++);
for(i=0;i<256;i++) //second 256 bytes
spi_putc(*p2++);
spi_putc(0xff); //send dummy CRC
spi_putc(0xff);
if((r=spi_putc(0xff) & 0x0f) == 0x05){ //check if data accepted
for(i=10000;i>0;i--){
if(r=spi_putc(0xff))
break;
}
}
else
r=0; //fail
}
CSel = 1; spi_putc(0xff); //disable SD
SD_LED = 0; //LED off
return(r);
}
int sd_readSector(LBA a, char *p1, char *p2){
int r,i;
SD_LED = 1; //turn on read LED
r = sd_sendCmd(17,(a<<9));
if(r == 0){ //check if command was accepted
i=10000; //wait for a response
do{
r = spi_putc(0xff);
if(r==0xfe)
break;
}while(--i > 0);
if(i){ //if no timeout, read 512 byte sector
for(i=0;i<256;i++)
*p1++ = spi_putc(0xff);
for(i=0;i<256;i++)
*p2++ = spi_putc(0xff);
spi_putc(0xff); //ignore CRC
spi_putc(0xff);
}
}
CSel = 1;spi_putc(0xff); //disable SD
SD_LED = 0; //read LED off
return(r == 0xfe);
}
int sd_sendCmd(unsigned char c, LBA a){
int i,r;
CSel = 0; //send command packet (6 bytes)
spi_putc(c|0x40); //send command & frame bit
spi_putc(a>>24); //send 32-bit address
spi_putc(a>>16);
spi_putc(a>>8);
spi_putc(a);
spi_putc(0x95); //send CRC
i = 9; //wait for response
do{
r = spi_putc(0xff); //check if ready
if(r != 0xff)
break;
}while(--i > 0);
return(r);
}
/*
void main(void){
LBA addr;
int i,r;
trisd=0;
trisc=0b00010000;
CSel=1; //init chip select (active low)
LED=0; //LED off
osccon=0x72; //8MHz clock
osctune.6=0; //disable PLL
while(!osccon.IOFS); //wait for osc stable
sspstat = 0b00000000; //SMP(7)=0, CKE(6)=0 (clock edge idle to active)
sspcon1 = 0b00010010; //CKP(4)=1 clock polarity (idle high)
//SSPM3:SSPM0(3:0)=010 spi clock FOSC/64 (<400kHz)
sspcon1.SSPEN=1; //SSPEN(5)=1 enable SPI
for(i=0;i<256;i++){ //fill the send buffers
data1[i]=i;
data2[i]=i;
buffer1[i]=0; //and clear the receive buffers
buffer2[i]=0;
}
r=initMedia();
if(r){ //card init failed - 1 blink
while(1){
LED=1;
delay_ms(150);
LED=0;
delay_s(1);
}
}
else{ //write card
addr = 10000;
for(i=0;i<1000;i++){
if(!writeSector(addr+i,data1,data2)){
while(1){ //write failed - 2 blinks
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_s(1);
}
}
}
addr=10000; //verify write
for(i=0;i<1000;i++){
if(!readSector(addr+i,buffer1,buffer2)){
while(1){ //verify failed - 3 blinks
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_s(1);
}
}
//verify
if(memcmp(data1,buffer1,256)||memcmp(data2,buffer2,256)){
while(1){ //mismatch - 4 blinks
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_ms(150);
LED=1;
delay_ms(150);
LED=0;
delay_s(1);
}
}
}
}
while(1){ //success!
LED=1;
delay_s(1);
LED=0;
delay_s(1);
}
}
*/

10
boostc/sdcard.h Normal file
View file

@ -0,0 +1,10 @@
#define CSel latd.2 //chip select line
#define SD_LED latd.1
typedef unsigned long LBA;
void sd_init(void);
int sd_initMedia(void);
int sd_writeSector(LBA,char *,char *);
int sd_readSector(LBA, char *,char *);
int sd_sendCmd(unsigned char,LBA);

17
boostc/spi.c Normal file
View file

@ -0,0 +1,17 @@
#include <system.h>
#include "spi.h"
void spi_init(){
while(!osccon.IOFS){} //wait for osc stable
sspstat = 0b00000000; //SMP(7)=0, CKE(6)=0 (clock edge idle to active)
sspcon1 = 0b00010010; //CKP(4)=1 clock polarity (idle high)
//SSPM3:SSPM0(3:0)=010 spi clock FOSC/64 (<400kHz)
sspcon1.SSPEN=1; //SSPEN(5)=1 enable SPI
}
unsigned char spi_putc(unsigned char send){
sspbuf=send;
while(!sspstat.BF){}
return sspbuf;
}

3
boostc/spi.h Normal file
View file

@ -0,0 +1,3 @@
void spi_init(void);
unsigned char spi_putc(unsigned char);

116
boostc/usart.c Normal file
View file

@ -0,0 +1,116 @@
#include <system.h>
#include "usart.h"
#include "util.h"
void usart_init_core(void){
// pins
trisc.7 = 1;
trisc.6 = 0;
// baud rate
txsta.BRGH = BAUD_BRGH;
baudcon.BRG16 = BAUD_BRG16;
baudcon.WUE = 0;
txsta.SYNC = 0;
rcsta.SPEN = 1;
// transmitter
txsta.TXEN = 1;
pie1.TXIE = 0;
txsta.TX9 = 0;
// reception
pie1.RCIE = 0;
rcsta.RX9 = 0;
rcsta.CREN = 1;
}
void usart_init(void){
// baud rate
spbrgh = (BAUD_RATE >> 8) & 0xff;
spbrg = BAUD_RATE & 0xff;
usart_init_core();
}
/*
The Auto-Baud Rate Detection must receive a
byte with the value 55h (ASCII U)
*/
void usart_init_auto(void){
spbrg = 0;
spbrgh = 0;
//baudcon.WUE = 1;
baudcon.ABDEN = 1;
usart_init_core();
while(baudcon.ABDEN == 1){
usart_getc();
}
}
void usart_putc(unsigned char data){
while(!pir1.TXIF){
delay_ms(1);
}
txreg = data;
#ifdef TX_LED
TX_LED = 1;
delay_us(1);
TX_LED = 0;
#endif
}
unsigned char usart_getc(void){
while(!pir1.RCIF){
delay_ms(1);
}
#ifdef RX_LED
RX_LED = 1;
delay_us(1);
RX_LED = 0;
#endif
if (rcsta.OERR){
rcsta.CREN = 0;
rcsta.CREN = 1;
}
return rcreg;
}
void usart_gets(unsigned char *destination){
while ((*destination++ = usart_getc()) != 0x0d); // wait until tx register is empty
*--destination = 0;
}
void usart_puts(unsigned char *source){
while (*source != 0) // wait until tx register is empty
usart_putc(*source++);
}
void usart_putsln(unsigned char *source){
usart_puts(source);
usart_put_newline();
}
void usart_put_newline(){
usart_putc(0x0d);
usart_putc(0x0a);
}
void usart_waitfor(unsigned char c){
while(usart_getc() != c){}
}
void usart_put_baoud(){
usart_puts("Baud rate: 0x");
usart_putc(bin2Hex( (spbrgh>>0x4) & 0x0f ));
usart_putc(bin2Hex( spbrgh & 0x0f ));
usart_putc(bin2Hex( (spbrg>>0x4) & 0x0f ));
usart_putc(bin2Hex( spbrg & 0x0f ));
usart_put_newline();
}

33
boostc/usart.h Normal file
View file

@ -0,0 +1,33 @@
/*******************************************************************/
// Defines
/*******************************************************************/
//******* usart_init
// (FOSC/(4 x Baud rate)) -1 BRGH=1 BRG16=1
// (FOSC/(16 x Baud rate)) -1 (BRGH=1 BRG16=0) or (BRGH=0 BRG16=1) - High speed
// (FOSC/(64 x Baud rate)) -1 BRGH=0 BRG16=0 - Low speed
//******* usart_init_auto
// BRG16=0 BRGH=0 => FOSC/512
// BRG16=0 BRGH=1 => FOSC/128
// BRG16=1 BRGH=0 => FOSC/128
// BRG16=1 BRGH=1 => FOSC/32
#define BAUD_RATE 0x0196 //0x0065 // 4800
#define BAUD_BRGH 1
#define BAUD_BRG16 1
#define TX_LED portb.4
#define RX_LED portb.4
/*******************************************************************/
// Functions
/*******************************************************************/
void usart_init(void);
void usart_init_auto(void);
void usart_putc(unsigned char data);
unsigned char usart_getc(void);
void usart_gets(unsigned char *destination);
void usart_puts(unsigned char *source);
void usart_putsln(unsigned char *source);
void usart_put_newline(void);
void usart_waitfor(unsigned char c);
void usart_put_baoud(void);

39
boostc/util.c Normal file
View file

@ -0,0 +1,39 @@
#include <system.h>
#include "util.h"
/*******************************************************************/
//Returns ASCII Decimal and Hex values
unsigned char bin2Hex(unsigned char x){
//skip(x);
//#pragma return[16] = "0123456789ABCDEF"
switch(x){
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}
return '?';
}
/*******************************************************************/
unsigned char swap ( unsigned char cX ){
unsigned char tmp = (cX >> 4) & 0x0f;
cX = (cX << 4) & 0xf0;
cX = cX | tmp;
return cX;
}
/*******************************************************************/

10
boostc/util.h Normal file
View file

@ -0,0 +1,10 @@
/*******************************************************************/
// Defines
/*******************************************************************/
/*******************************************************************/
// Functions
/*******************************************************************/
//unsigned char* bin2Hexs(unsigned char x);
unsigned char bin2Hex(unsigned char x);
unsigned char swap ( unsigned char cX );

View file

@ -1,14 +0,0 @@
// Delays a multiple of 1 milliseconds at 4 MHz
// using the TMR0 timer
void delay_ms( uns16 millisec){
char next = 0;
OPTION = 2; // prescaler divide TMR0 rate by 8
TMR0 = 2; // deduct 2*8 fixed instruction cycles delay
do {
next += 125;
clrwdt(); // needed only if watchdog is enabled
while (TMR0 != next) // 125 * 8 = 1000 (= 1 ms)
;
} while ( -- millisec != 0);
}