Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 5 → Rev 6

/branches/V0.1 killagreg/fat16.h
1,5 → 1,5
#ifndef __fat16_h
#define __fat16_h
#ifndef _FAT16_H
#define _FAT16_H
 
 
//________________________________________________________________________________________________________________________________________
184,7 → 184,7
extern u8 SectorsPerCluster;
 
extern struct time rtctime;
#endif
#endif //_FAT16_H
 
 
 
/branches/V0.1 killagreg/main.c
126,15 → 126,19
OutputStartupData();
// initialize the uart to MKGPS module
GPS_UART0_Init();
// iniitalize the gps position controller
GPS_Init();
 
// initialize usb
USB_ConfigInit();
// initialize SPI0 to FC
SPI0_Init();
// initialize timer 1
TIMER1_Init();
// initialize i2c bus to MK3MAG
I2C1_Init();
// get version from MK3MAG
I2C_Version.Major = 0xFF;
SendI2C_Command(I2C_CMD_VERSION);
TimerCompassUpdate = SetDelay(50);
/branches/V0.1 killagreg/sdc.c
67,21 → 67,22
// Last Modifikation: 24.07.2007
// Version: 1.05
// Authors: Stephan Busker
// Description: Source files for connecting to an sdcard using the SSC
// Description: Source files for connecting to an sd-card using the SSC
//
//........................................................................................................................................
// Functions: u8 SDC_init(void);
// u8 SDC_PutCommand (u8 *CMD);
// u8 SDC_PutSector(u32 addr,u8 *Buffer);
// u8 SDC_GetSector(u32 addr,u8 *Buffer);
// void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
// Functions: u8 SDC_init(void);
// u8 SDC_PutCommand (u8 *CMD);
// u8 SDC_PutSector(u32 addr,u8 *Buffer);
// u8 SDC_GetSector(u32 addr,u8 *Buffer);
// void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
//
////........................................................................................................................................
// ext. functions: extern void SSC_Init(void);
// extern u8 SSC_GetChar (void);
// extern u8 SSC_GetChar (void);
// extern void SSC_PutChar (u8);
// extern void SSC_Enable(void);
// extern void SSC_Disable(void);
// extern void SSC_ClearRxFifo();
//........................................................................................................................................
//
// URL: www.Mikro-Control.de
100,6 → 101,46
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
//________________________________________________________________________________________________________________________________________
 
/* SD-Command Format
// All SD-commands have a fixed code length of 48 bits (6 bytes)
// ______________________________________________________________________________
//| Bit position | 47 | 46 | [45:40] | [39:8] |[7:1]| 0 |
//| Width (bits) | 1 | 1 | 6 | 32 | 7 | 1 |
//| Value | ‘0’ | ‘1’ | x | x | x | ‘1’ |
//| Description |start bit|transmission bit|command index|argument|CRC7 |end bit|
//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// the start bit is always zero
// the transmission bit indicated the direction of he command
// 1 = host, 0 = card
// the command index has a range from 0 to 63
// the optional command argument is 4 bytes long
 
// the MSB of the response byte is allways zero.
// Therefore thae pattern 0xff is definitive an error
// The response byte is formated in the following way.
// Fromat for response type R1
// bit0-> in idle state
// bit1-> erase reset
// bit2-> illegal command
// bit3-> com crc error
// bit4-> erase sequence error
// bit5-> address error
// bit6-> parameter error
// bit7-> allways zero
 
// Fomat for response type R1b
// This response token is identical to the R1 format with the optional addition of the busy signal.
// The busy signal token can be any number of bytes. A zero value indicates card is busy.
// A non-zero value indicates the card is ready for the next command.
 
// for more details see:
// SD Specifications Part 1
// Physical Layer
// Simplified Specification
// Version 2.00
// September 25, 2006
// SD Group
*/
u8 SDC_Init(void)
{
 
112,44 → 153,54
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init(GPIO5, &GPIO_InitStructure);
GPIO_Init(GPIO5, &GPIO_InitStructure);
 
u16 Timeout = 0;
 
u16 Timeout = 0;
// initialized to CMD0 (GO_IDLE_STATE)
u8 CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
 
u8 b;
 
SSC_Init(); // Initialise SSC to transmit data to the sdcard.
SSC_Init(); // Initialise SSC to transmit data to the sdcard.
SerialPutString("Init SD...");
// Delay_ms(10);
 
for (b = 0;b<0x0f;b++) // sending 74Clocks brings the sdcard into spimode.
// relict from MMC reset! Useless for SD-Cards
for (b = 0;b<0x0f;b++)
{
SSC_PutChar(0xff);
}
SerialPutString("reset...");
while(SDC_PutCommand (CMD) !=1) // Sending CMD0 (Reset) to the sdcard.
// Try to enable the SPI-Mode at the sd-card.
// Send CMD0 (GO_IDLE_STATE) to the sd-card when CS-SD is activated (lowactive!)
// until the response byte indicates no error and in idle state (0x01).
SerialPutString("SD reset...");
while(SDC_PutCommand (CMD) != 0x01)
{
if (Timeout++ > 200)
{
{
return(1);
}
}
}
SerialPutString("ok.");
SerialPutString("ok.\r\n");
Timeout = 0;
CMD[0] = 0x41;
CMD[5] = 0xFF;
while( SDC_PutCommand (CMD) !=0) // Sending CMD1 to the sdcard.
// Sending CMD1 (SEND_OP_COND) to the sd-card.
// (Sends host capacity support information and activates the card's initialization process.)
// CMD1 is valid command for the Thin (1.4mm) Standard Size SD Memory Card
// only if used after re-initializing a card (not after power on reset).
CMD[0] = 0x41; // update command index
CMD[5] = 0xFF; // and CRC7 checksum
// Send CMD1 until response byte indicated
SerialPutString("SD initialize...");
while( SDC_PutCommand (CMD) != 0x00)
{
if (Timeout++ > 100)
{
{
return(2);
}
}
}
 
SerialPutString("ok.\r\n");
SSC_Disable(); // disable sdcard.
return(0);
}
171,29 → 222,26
u8 tmp = 0xff;
u16 Timeout = 0;
u16 a;
SSC_ClearRxFifo();
SSC_Disable(); // disable chipselect
SSC_PutChar(0xFF); // Send 8 Clocks to the sdcard while card is not selected.
SSC_Enable(); // enable chipselect.
 
// if (*CMD == 0x41) Delay_ms(10); // if command is CMD0 generate a short delay.
SSC_ClearRxFifo(); // clear the rx fifo
SSC_Disable(); // disable chipselect
SSC_PutChar(0xFF); // Send 8 Clocks to the sdcard while card is not selected. Why that?
SSC_Enable(); // enable chipselect.
for (a = 0;a<0x06;a++) // send the command sequence to the sdcard (6 bytes)
{
for (a = 0;a<0x06;a++) // send the command sequence to the sdcard (6 bytes)
{
SSC_PutChar(*CMD++);
}
SSC_ClearRxFifo();
while (tmp == 0xff) // Wait for response from sdcard.
SSC_ClearRxFifo(); // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes
while (tmp == 0xff) // wait for the response byte from sd-card.
{
tmp = SSC_GetChar();
if (Timeout++ > 200)
{
break; // or timeout.
break; // or timeout.
}
}
 
return(tmp);
}
 
/branches/V0.1 killagreg/sdc.h
1,33 → 1,20
#ifndef _SDC_H_
#define _SDC_H_
#ifndef _SDC_H
#define _SDC_H
 
extern unsigned char SDC_Init(void);
#include "91x_lib.h"
 
//________________________________________________________________________________________________________________________________________
//
// Functions needed for access to the sdcard.
// Functions needed for accessing the sdcard.
//
//________________________________________________________________________________________________________________________________________
 
extern unsigned char SSC_GetChar(void);
extern void SSC_PutChar(u8);
extern void MMC_Read_Block(u8 *,u8 *,u16);
extern u8 SDC_Init(void);
extern u8 SDC_GetSector (u32,u8 *);
extern u8 SDC_PutSector (u32,u8 *);
extern u8 SDC_PutCommand (u8 *);
extern void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
 
#endif // _SDC_H
 
//________________________________________________________________________________________________________________________________________
//
// Functions needed internaly for the fat16 implementation
//
//________________________________________________________________________________________________________________________________________
 
extern unsigned char SDC_GetSector (u32,u8 *);
extern unsigned char SDC_PutSector (u32,u8 *);
extern unsigned char SDC_PutCommand (u8 *);
extern void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
 
 
#define nop() __asm__ __volatile__ ("nop" ::)
 
#endif
 
 
/branches/V0.1 killagreg/ssc.c
66,16 → 66,13
// Description: Source files for access to the synchrnous serial channel.
// Copyright (C) 2007 Stephan Busker
//........................................................................................................................................
// Functions: extern void SSC_Init(void);
// extern u8 SSC_GetChar (void);
// ext. Functions: extern void SSC_Init(void);
// extern u8 SSC_GetChar (void);
// extern void SSC_PutChar (u8 Byte);
// extern void SSC_Disable(void);
// extern void SSC_Enable(void);
// extern void SSC_ClearRxFifo();
//........................................................................................................................................
// ext. functions: extern u8 SDC_GetSector (unsigned long,u8 *);
// extern u8 SDC_PutSector (unsigned long,u8 *);
//........................................................................................................................................
//
// URL: www.Mikro-Control.de
// mailto: stephan.busker@mikro-control.de
//________________________________________________________________________________________________________________________________________
82,9 → 79,8
 
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SSC_Disable(void);
// Function: SSC_Enable(void);
//
// Description: This function enables chipselect of the sdcard (active low)
//
92,15 → 88,30
// Returnvalue: none
//________________________________________________________________________________________________________________________________________
 
void SSC_Enable(void)
{
// enable chipselect of the sd-card (P5.4 -> SD-CS, active low).
GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
}
//________________________________________________________________________________________________________________________________________
// Function: SSC_Disable(void);
//
// Description: This function disables chipselect of the sdcard (active low)
//
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________
 
void SSC_Disable(void)
{
//MMC_Write &= ~(1<<MMC_Chip_Select); // disable chipselect of the sdcard (active low).
// disable chipselect of the sd-card (P5.4 -> SD-CS, active low).
GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
}
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SSC_Init(void);
// Function: SSC_Init(void);
//
// Description: This function initialises the synchronus serial channel to the sdcard.
//
110,68 → 121,55
 
void SSC_Init(void)
{
 
SerialPutString("SPI1 init...");
GPIO_InitTypeDef GPIO_InitStructure;
SSP_InitTypeDef SSP_InitStructure;
 
SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
GPIO_Init (GPIO3, &GPIO_InitStructure);
 
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
GPIO_Init (GPIO5, &GPIO_InitStructure);
 
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init (GPIO3, &GPIO_InitStructure);
SSP_DeInit(SSP1);
SSP_StructInit(&SSP_InitStructure);
SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
// Hier muss die Baudrate noch richtig eingestellt werden (mit Prescaler)
SSP_InitStructure.SSP_ClockRate = 5;
SSP_InitStructure.SSP_ClockPrescaler = 8;
SSP_Init(SSP1, &SSP_InitStructure);
SSC_Disable();
SSP_Cmd(SSP1, ENABLE);
/* MMC_Direction_REG &=~(1<<SPI_DI); // Set the direction of the ssc-port
MMC_Direction_REG |= (1<<SPI_Clock); // _______ _______
MMC_Direction_REG |= (1<<SPI_DO); // CS \________________________/
MMC_Direction_REG |= (1<<MMC_Chip_Select); //
MMC_Direction_REG |= (1<<SPI_SS); // ___ ___ ___
// clk __________/ \___/ \___/ \_________
//
// ___ ___
// data_________/ \___________/ \___________
 
// initialise ssc, clock = Idel low
// devide clock by 32
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPOL); // Enable SSC in mastermode, invert clockpolarity (idle high)
SPSR = SPSR|(1<<SPI2X);*/
SerialPutString("ok\n\r");
SerialPutString("SPI1 init...");
GPIO_InitTypeDef GPIO_InitStructure;
SSP_InitTypeDef SSP_InitStructure;
// enable APB clock for SPI1
SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
// configure P5.4 -> SD-CS as an output pin
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
GPIO_Init (GPIO5, &GPIO_InitStructure);
// configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an output pin
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
GPIO_Init (GPIO3, &GPIO_InitStructure);
// configure P3.5 <- MISO1 as an input pin
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
GPIO_Init (GPIO3, &GPIO_InitStructure);
// configure SPI1
SSP_DeInit(SSP1);
SSP_StructInit(&SSP_InitStructure);
SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
// Set Baud Rate (Prescaler)
// bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate))
// With MSCLK = 48MHz = BRCLK we get for the SPICLK = 48Mhz / 8 / (1+5) = 1MHz
SSP_InitStructure.SSP_ClockRate = 5;
SSP_InitStructure.SSP_ClockPrescaler = 8;
SSP_Init(SSP1, &SSP_InitStructure);
SSC_Disable();
SSP_Cmd(SSP1, ENABLE);
SerialPutString("ok\r\n");
}
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SSC_GetChar(void);
// Function: SSC_GetChar(void);
//
// Description: This function reads one byte from the SSC
//
182,29 → 180,22
u8 SSC_GetChar (void)
{
u8 Byte = 0;
// SerialPutString("\n\rGetChar.");
SSP_SendData(SSP1, 0xFF);
while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
Byte = SSP_ReceiveData(SSP1);
/* SPDR = 0x00; // read one byte of data from the SSC
while(!(SPSR & (1<<SPIF))){}; // wait until the data has been read.
Byte = SPDR;
*/
// sprintf(text," %02X ", Byte); SerialPutString(text);SerialPutString("done.");
SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave
while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent
Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave
return (Byte);
 
}
 
//________________________________________________________________________________________________________________________________________
void SSC_ClearRxFifo (void)
{
// SerialPutString("\n\rClr RxFifo ");
// wait that the tx fifi is empty
while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
// then empty the rx fifo by reading all the bytes that are available
while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1);
// SerialPutString("done.");
}
//________________________________________________________________________________________________________________________________________
// Funtion: SSC_PutChar(u8 Byte);
// Function: SSC_PutChar(u8 Byte);
//
// Description: This function writes one byte to the SSC
//
214,11 → 205,10
 
void SSC_PutChar (u8 Byte)
{
//SerialPutString("\n\rPutChar."); sprintf(text," %02X ", Byte); SerialPutString(text);
// wait for some space in the tx fifo
while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);
// put the byte to send in the tx fifo
SSP_SendData(SSP1, Byte);
//SerialPutString("done.");
}
 
 
227,17 → 217,4
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SSC_Enable(void);
//
// Description: This function disables chipselect of the sdcard (active low)
//
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________
 
void SSC_Enable(void)
{
//MMC_Write |= (1<<MMC_Chip_Select); // enable chipselect of the sdcard (active low).
GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
}
/branches/V0.1 killagreg/ssc.h
1,18 → 1,20
#ifndef __SSC_H
#define __SSC_H
#ifndef _SSC_H
#define _SSC_H
 
 
#include "91x_lib.h"
//________________________________________________________________________________________________________________________________________
//
// Functions needed for accessing the sdcard.
// Functions needed for accessing the sdcard low level via SPI.
//
//________________________________________________________________________________________________________________________________________
 
extern void SSC_Init(void);
extern u8 SSC_GetChar (void);
extern void SSC_PutChar (u8);
extern void SSC_Enable(void);
extern void SSC_Disable(void);
extern void SSC_ClearRxFifo(void);
extern void SSC_Init(void);
extern u8 SSC_GetChar (void);
extern void SSC_PutChar (u8);
extern void SSC_Enable(void);
extern void SSC_Disable(void);
extern void SSC_ClearRxFifo(void);
 
 
#endif
#endif //_SSC_H
/branches/V0.1 killagreg/usb.c
61,91 → 61,93
//-----------------------------------------------------------------
void USB_ConfigInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
SerialPutString("USB init...");
SerialPutString("USB init...");
//USB clock = MCLK= 48MHz
SCU_USBCLKConfig(SCU_USBCLK_MCLK);
//Enable USB clock
SCU_AHBPeriphClockConfig(__USB,ENABLE);
SCU_AHBPeriphReset(__USB,DISABLE);
SCU_AHBPeriphClockConfig(__USB48M,ENABLE);
//USB clock = MCLK= 48MHz
SCU_USBCLKConfig(SCU_USBCLK_MCLK);
//Enable USB clock
SCU_AHBPeriphClockConfig(__USB,ENABLE);
SCU_AHBPeriphReset(__USB,DISABLE);
SCU_AHBPeriphClockConfig(__USB48M,ENABLE);
 
//Configure GPIO0 (D+ Pull-Up on P0.1)
SCU_APBPeriphClockConfig(__GPIO0 ,ENABLE);
SCU_APBPeriphReset(__GPIO0,DISABLE);
//Configure GPIO0 (D+ Pull-Up on P0.1)
SCU_APBPeriphClockConfig(__GPIO0 ,ENABLE);
SCU_APBPeriphReset(__GPIO0,DISABLE);
 
// GPIO_DeInit(GPIO0);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1;
GPIO_Init (GPIO0, &GPIO_InitStructure);
// GPIO_DeInit(GPIO0);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt1;
GPIO_Init (GPIO0, &GPIO_InitStructure);
 
PowerOff();
Virtual_Com_Port_Reset();
PowerOff();
Virtual_Com_Port_Reset();
VIC_Config(USBLP_ITLine, VIC_IRQ, 2);
VIC_ITCmd(USBLP_ITLine, ENABLE);
VIC_Config(USBLP_ITLine, VIC_IRQ, 2);
VIC_ITCmd(USBLP_ITLine, ENABLE);
USB_Init();
USB_Init();
SerialPutString("ok\n\r");
SerialPutString("ok\n\r");
}
 
//-----------------------------------------------------------------
void USB_Cable_Config (FunctionalState NewState)
{
if (NewState == ENABLE)
GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_RESET);
else
GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_SET);
if (NewState == ENABLE)
GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_RESET);
else
GPIO_WriteBit(GPIO0, GPIO_Pin_1, Bit_SET);
}
 
//-----------------------------------------------------------------
void USB_Send_String(u8 *StrPtr)
{
u8 i = 0;
u16 timeout = 0;
 
while (StrPtr[i++] !=0){}
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK){ if (timeout++ > 60000) return;}
UserToPMABufferCopy(StrPtr, ENDP1_TXADDR, ++i);
SetEPTxCount(ENDP1,i);
SetEPTxValid(ENDP1);
u8 i = 0;
u16 timeout = 0;
while (StrPtr[i++] !=0){}
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK){ if (timeout++ > 60000) return;}
UserToPMABufferCopy(StrPtr, ENDP1_TXADDR, ++i);
SetEPTxCount(ENDP1,i);
SetEPTxValid(ENDP1);
}
 
//-----------------------------------------------------------------
void USB_Send_Char(u8 ch)
{
u16 timeout = 0;
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK){ if (timeout++ > 60000) return;}
UserToPMABufferCopy(&ch, ENDP1_TXADDR, 2);
SetEPTxCount(ENDP1,2);
SetEPTxValid(ENDP1);
}
u16 timeout = 0;
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK){ if (timeout++ > 60000) return;}
UserToPMABufferCopy(&ch, ENDP1_TXADDR, 2);
SetEPTxCount(ENDP1,2);
SetEPTxValid(ENDP1);
}
//-----------------------------------------------------------------
void USB_Send_Data(u8 *data, u16 count)
{
u8 i;
count++;
for (i=0;i< (count/64)+1;i++)
{
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK);
if (i < (count/64))
{ UserToPMABufferCopy(&data[i*64], ENDP1_TXADDR, 64);
SetEPTxCount(ENDP1,64);
u8 i;
count++;
for (i=0;i< (count/64)+1;i++)
{
while (_GetEPTxStatus(ENDP1) != EP_TX_NAK);
if (i < (count/64))
{
UserToPMABufferCopy(&data[i*64], ENDP1_TXADDR, 64);
SetEPTxCount(ENDP1,64);
}
else
{
UserToPMABufferCopy(&data[i*64], ENDP1_TXADDR, count % 64);
SetEPTxCount(ENDP1, count % 64);
}
SetEPTxValid(ENDP1);
}
else
{ UserToPMABufferCopy(&data[i*64], ENDP1_TXADDR, count % 64);
SetEPTxCount(ENDP1, count % 64);
}
SetEPTxValid(ENDP1);
}
}