//========================================================================================
//
// USB MASS STORAGE DRIVER FOR PIC18F452
// Version: 2.01
//
//========================================================================================
// Author : Guillaume Fondeville
// Email : g.fondeville@free.fr
// Site : http://g.fondeville.free.fr
//========================================================================================
// Micro : PIC18F452 16MHz
// SIE : SL811HS (Cypress)
// WatchDog disable, Oscillator HS
// UART : 115200, 8 data bits, no parity, 1 start, 1 stop
//
//--------- GNU GPL LICENCE --------------------------------------------------
// This file is subject to the terms of the GNU General Public License as
// published by the Free Software Foundation. A copy of this license is
// included with this software distribution in the file COPYING. If you
// do not have a copy, you may obtain a copy by writing to the Free
// Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details
//========================================================================================
#include <pic18fxx2.h>
#include <pic18.h>
//========================================================================================
// Functions
//========================================================================================
//UART
void init232(char speed);
void tx232(char val);
char rx232_wait(void);
void tx232_puts(const char * s);
void tx232_2ascii(unsigned char car);
void tx232_long(unsigned long car);
//USB
unsigned char sl811_init(void);
unsigned char sl811_usb_ready(void);
unsigned char sl811_write_sector(unsigned long sector, unsigned char *buf);
unsigned char sl811_read_sector(unsigned long sector, unsigned char *buf);
void display_sector(unsigned char * buf);
void sl811_write_next(unsigned char value);
unsigned char sl811_read_next(void);
void pause_ms(unsigned char tps);
//========================================================================================
// Global Variables
//========================================================================================
unsigned char buffer[512];
unsigned char data_toggle, adr, endp_in, endp_out;
//========================================================================================
// Constant definitions
//========================================================================================
#define INTRQ RB0
#define NRST RB1
#define LED_G RB2
#define LED_R RB3
#define BP_R RB4
#define BP_N RB5
#define NWR RC0
#define NCS RC1
#define NRD RC2
#define A0 RC3
#define SL811_DATA PORTD
#define READY 0
#define COMPLETED 0
#define NO_DEVICE 1
#define NOT_FULL_SPEED 2
#define NOT_MASS_STORAGE 3
#define ERROR 4
#define BUSY 5
#define STALL 6
//Macros
#define p_data_out(); {TRISD = 0x00;};
#define p_data_in(); {TRISD = 0xFF;};
#define USB_DEBUGMODE
//========================================================================================
// Interruption
//========================================================================================
static void interrupt
isr(void)
{;}
//========================================================================================
// Programme main
//========================================================================================
void main (void)
{
unsigned char tmp;
unsigned long sector;
// port configuration
TRISA = 0b11111111;
TRISB = 0b11110001;
TRISC = 0b11110000;
TRISD = 0b11111111;
TRISE = 0b00000111;
ADCON1 = 7; //enable digital I/O
//port initialisation
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
pause_ms(255);
init232(8); //Init UART 115200 8 for an oscillator 16 MHz
LED_G = 1;
tx232_puts("\n\n\rStart program\n\r");
sl811_init(); //SIE init & wait device attachment
LED_R = 1;
tx232_puts("\n\rPlease BP_G to read a sector\n\r");
while(BP_N == 1);
//read sector
sector = 0x00000000;
sl811_read_sector(sector, buffer);
//display sector
tx232_puts("\r\nSector 0x");
tx232_long(sector);
display_sector(buffer);
tx232_puts("\n\rPlease BP_O to write modified sector\n\r");
while(BP_R == 1);
//Modify sector
buffer[0x1FC] = buffer[0x1FC] + 1; //increment byte 508 of the sector
//Write sector
sl811_write_sector(sector, buffer);
//Check sector
sl811_read_sector(sector, buffer);
//Display sector
tx232_puts("\r\nSector 0x");
tx232_long(sector);
display_sector(buffer);
while(1); //end of program, reset PIC to restart
}
//************************************************************
// SL811 FUNCTIONS
//************************************************************
/****************************************************************************************/
//Write one byte in sl811 register
/****************************************************************************************/
void sl811_write(unsigned char adr, unsigned char value)
{
p_data_out();
NCS = 0;
A0 = 0;
SL811_DATA = adr;
NWR = 0;
asm("nop");
NWR = 1;
A0 = 1;
SL811_DATA = value;
NWR = 0;
asm("nop");
NWR = 1;
NCS = 1;
p_data_in();
}
/****************************************************************************************/
// Read one byte in sl811 register
/****************************************************************************************/
unsigned char sl811_read(unsigned char adr)
{
unsigned char value;
p_data_out();
NCS = 0;
A0 = 0;
SL811_DATA = adr;
NWR = 0;
asm("nop");
NWR = 1;
A0 = 1;
p_data_in();
NRD = 0;
asm("nop");
value = SL811_DATA;
NRD = 1;
NCS = 1;
p_data_in();
return value;
}
/****************************************************************************************/
//Write buffer
/****************************************************************************************/
void sl811_write_buf(unsigned char adr, unsigned char * buffer, unsigned char size)
{
unsigned char i;
sl811_write(adr, buffer[0]);
size--;
i = 1;
while(size != 0)
{
sl811_write_next(buffer[i]);
i++;
size--;
}
}
/****************************************************************************************/
//Read buffer
/****************************************************************************************/
void sl811_read_buf(unsigned char adr, unsigned char * buffer, unsigned char size)
{
unsigned char i;
buffer[0] = sl811_read(adr);
size --;
i = 1;
while(size != 0)
{
buffer[i] = sl811_read_next();
i++;
size--;
}
}
/****************************************************************************************/
//Write without specify address
/****************************************************************************************/
void sl811_write_next(unsigned char value)
{
A0 = 1;
NCS = 0;
p_data_out();
SL811_DATA = value;
NWR = 0;
asm("nop");
NWR = 1;
NCS = 1;
p_data_in();
}
/****************************************************************************************/
//Read without specify addre
SL811HS USB MASS STORAGE DRIVER FOR PIC.rar
需积分: 0 72 浏览量
更新于2008-11-20
1
收藏 25KB RAR 举报
SL811HS USB MASS STORAGE DRIVER FOR PIC 是一个专为Microchip PIC18F系列微控制器设计的驱动程序,主要用于使这些MCU支持USB大容量存储设备(如U盘)的功能。这个驱动程序的核心是针对SL811HS USB控制器芯片的,该芯片常用于嵌入式系统中实现USB接口功能。
SL811HS是Silicon Labs生产的一款低功耗、高性能的USB接口控制器。它提供了一个简单的方法来连接嵌入式系统到USB总线,使得系统能够作为USB设备运行,例如USB大容量存储设备。SL811HS芯片集成了USB物理层和数据链路层,使得开发者可以专注于应用层的编程,而不需要深入了解USB协议的底层细节。
PIC18F452是Microchip Technology公司推出的一种8位微控制器,广泛应用于各种嵌入式系统中。这个驱动程序的目标是将PIC18F452与SL811HS芯片结合,使MCU能够识别并处理通过USB接口接入的存储设备,实现USB Mass Storage Class(UMSC)标准功能。
在压缩包中,"USB MASS STORAGE DRIVER FOR PIC18F452.c" 文件很可能是驱动程序的主要源代码,包含初始化USB控制器、处理USB传输、管理存储设备等关键功能的C语言实现。开发者可以通过阅读和修改这个源代码来适应特定的应用需求或解决兼容性问题。
而"USB MASS STORAGE DRIVER FOR PIC18F452.png" 文件可能是一个相关示意图或者流程图,帮助理解驱动程序的工作原理,包括USB数据流的处理、中断处理机制以及与SL811HS芯片交互的过程。
开发这样的驱动程序涉及到的知识点包括:
1. USB协议:了解USB的架构、数据传输模式、枚举过程、设备类规范等,特别是UMSC的相关规定。
2. Microchip PIC18F系列微控制器:熟悉其硬件特性、寄存器配置、中断系统以及程序开发环境。
3. SL811HS USB控制器:掌握其工作模式、配置寄存器、中断处理以及与主机的通信协议。
4. C语言编程:编写驱动程序需要对C语言有深入理解,包括指针、结构体、函数调用等。
5. 嵌入式系统开发:理解嵌入式系统的内存管理、中断处理、时序控制等基本概念。
6. 文件系统:驱动程序需要与不同的文件系统打交道,如FAT16/32,需要了解文件系统的组织结构和操作命令。
通过分析和调试这个驱动程序,开发者可以学习到如何在嵌入式系统中实现USB设备功能,并且可以将这些知识应用到其他类型的USB设备驱动开发中。这是一项涉及硬件、软件和协议的综合技术实践。