📄 main.c
字号:
/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V3.0.1
* Date : 04/27/2009
* Description : Virtual Com Port Demo main file
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "app_cfg.h"
#include "stdio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_desc.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "platform_config.h"
//#include "i2c_ee.h"
#include "sconfig.h"
#include "Usb_To_I2c.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Extern variables ----------------------------------------------------------*/
extern __IO uint32_t count_out;
extern vu32 cmd_len;
extern uint8_t buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];
extern bool packet_received_flags;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* _Usb_Package_Header->bType */
#define TYPE_CON 0
#define TYPE_I2C 1
#define TYPE_SPI 2
#define TYPE_GPIO 3
#if 0
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define I2C1_SLAVE_ADDRESS7 0x30
#define I2C2_SLAVE_ADDRESS7 0x30
#define I2C2_SLAVE_ADDRESS10 0x0330
#define SMBusDefaultHeader 0xC2
#define Command 0x01
#define BufferSize 4
#define ClockSpeed 300000
#define I2C_Speed 200000
#define I2C_SLAVE_ADDRESS7 0xA0
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
I2C_InitTypeDef I2C_InitStructure;
__IO uint8_t ReceivedCommand = 0, PECValue = 0;
volatile FlagStatus Status = RESET;
uint8_t I2C1_Buffer_Tx[BufferSize] = {1,2,3,4};
uint8_t I2C2_Buffer_Rx[BufferSize];
uint8_t TxIdx = 0, RxIdx = 0;
volatile TestStatus TransferStatus = FAILED;
uint8_t HeaderAddressWrite = (((I2C2_SLAVE_ADDRESS10 & 0xFF00) >>7) | 0xF0) ;
#else
#endif
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
void mprint(unsigned char printEn,char *ptr,unsigned char *sMessage, unsigned short len);
//void SetLed(uint8_t led_num, uint8_t state);
/*******************************************************************************
* Function Name : main.
* Description : Main routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
int main(void)
{
//unsigned char tmpbuffer[100];
Set_System();
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
USART_Config();
/* Output a message on Hyperterminal using printf function */
printf("\r\n\r\n");
printf("\r\n I2C Analyzer\r\n");
printf("\r\nCopyright 2008-2012 BEIJING EDES Software Ltd.\r\n");
printf("\r\n CPU: STM32F103VBT6");
printf("\r\nHardware Version: %s", _HARDWARE_VER_STR);
printf("\r\nFirmware Version: %s", _FIRMWARE_VER_STR);
printf("\r\n Compiled Date: " __DATE__ ", " __TIME__ "");
printf("\r\n System State: Ready");
#if 0
/* Initialize the I2C EEPROM driver ----------------------------------------*/
I2C_EE_Init();
/* First write in the memory followed by a read of the written data --------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress1 */
I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);
/* Read from I2C EEPROM from EEPROM_ReadAddress1 */
I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);
/* Check if the data written to the memory is read correctly */
TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
printf("\r\n\r\n");
if (TransferStatus1 == PASSED)
{
printf("\r\nWrite/Read Buff1: OK!");
printf("\r\nRead Buff1: %s", Rx1_Buffer);
}
else
printf("\r\nWrite/Read Buff1: FAILED!");
//printf("\r\n Compiled Date: " __DATE__ ", " __TIME__ "");
//printf("\r\n System State: Ready");
/* TransferStatus1 = PASSED, if the transmitted and received data
to/from the EEPROM are the same */
/* TransferStatus1 = FAILED, if the transmitted and received data
to/from the EEPROM are different */
/* Wait for EEPROM standby state */
I2C_EE_WaitEepromStandbyState();
/* Second write in the memory followed by a read of the written data -------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress2 */
I2C_EE_BufferWrite(Tx2_Buffer, EEPROM_WriteAddress2, BufferSize2);
/* Read from I2C EEPROM from EEPROM_ReadAddress2 */
I2C_EE_BufferRead(Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2);
/* Check if the data written to the memory is read correctly */
TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BufferSize2);
/* TransferStatus2 = PASSED, if the transmitted and received data
to/from the EEPROM are the same */
/* TransferStatus2 = FAILED, if the transmitted and received data
to/from the EEPROM are different */
//printf("\r\n\r\n");
if (TransferStatus2 == PASSED)
{
printf("\r\nWrite/Read Buff2: OK!");
printf("\r\nRead Buff2: %s", Rx2_Buffer);
}
else
printf("\r\nWrite/Read Buff2: FAILED!");
//printf("\r\n Compiled Date: " __DATE__ ", " __TIME__ "");
//printf("\r\n System State: Ready");
/* Output a message on Hyperterminal using printf function */
//printf("\r\n\r\n");
//printf("\r\n I2C Analyzer\r\n");
//printf("\r\nCopyright 2008-2012 BEIJING EDES Software Ltd.\r\n");
#endif
while (1)
{
if(packet_received_flags)
{
Usb_Package_Header *rxUsbdata;
rxUsbdata = (Usb_Package_Header *)&buffer_out;
if(rxUsbdata->bType == TYPE_CON)
{
SConfigCmd(rxUsbdata);
}else if(rxUsbdata->bType == TYPE_I2C)
{
//printf("\r\n Command: I2c Bus Operation");
USB_TO_I2C_ProcessCmd(rxUsbdata);
}else if(rxUsbdata->bType == TYPE_SPI)
{
printf("\r\n Command: SPI Bus Operation");
//SetLed(usbdata->bData[0], usbdata->bData[1]);
}else if(rxUsbdata->bType == TYPE_GPIO)
{
printf("\r\n Command: GPIO Operation");
}else
{
printf("\r\n Command: Unknown");
//SetLed(usbdata->bData[0], usbdata->bData[1]);
}
/*
if(rep_len > 0)
{
// indicate reply data is valie
rep_len |= 0x80000000;
}
else
{
// no data to send, set cmd_len to 0
cmd_len = 0;
}
*/
packet_received_flags = FALSE;
cmd_len = 0;
count_out = 0; // set USB receive pointer to 0
}
#if 0
/* Turn on LD1 */
GPIO_SetBits(GPIO_LED, GPIO_LED1_PIN);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
/*
// Turn on LD2 and LD3
GPIO_SetBits(GPIO_LED, GPIO_LED2_PIN | GPIO_LED3_PIN);
// Turn off LD1 //
GPIO_ResetBits(GPIO_LED, GPIO_LED1_PIN);
// Insert delay //
Delay(0xAFFFF);
// Turn on LD4
GPIO_SetBits(GPIO_LED, GPIO_LED4_PIN);
// Turn off LD2 and LD3 //
GPIO_ResetBits(GPIO_LED, GPIO_LED2_PIN | GPIO_LED3_PIN);
// Insert delay //
Delay(0xAFFFF);
*/
/* Turn off LD4 */
GPIO_ResetBits(GPIO_LED, GPIO_LED1_PIN);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
#endif
}
}
/*
void I2C_AcknowledgePolling(void){
do{
I2C_GenerateSTART(I2C1,ENABLE);
I2C_ReadRegister(I2C1,I2C_Register_SR1);//清除ADDR位
I2C_Send7bitAddress(I2C1,0xa0,I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C1,I2C_Register_SR1)&0x0002));
I2C_ClearFlag(I2C1,I2C_FLAG_AF);
}
void I2C_SequentialRead_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){
u8 i;
I2C_AcknowledgePolling();
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_SendData(I2C1,sla);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,suba);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_SendData(I2C1,sla+1);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
if(len>1){
for(i=0;i<len-1;i++){
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED));
*pHead=I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1,ENABLE);
pHead++;
}
}
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED));
*pHead=I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
}
void I2C_PageWrite_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){
u8 wLen,addr;
wLen=len;
addr=suba;
ScrollPage:I2C_AcknowledgePolling();
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_SendData(I2C1,sla);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,addr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,*pHead++);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
addr++;
wLen--;
while(wLen){
if((addr%8)!=0){
I2C_SendData(I2C1,*pHead++);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
addr++;
wLen--;
}
else{
I2C_GenerateSTOP(I2C1,ENABLE);
goto ScrollPage;
}
}
I2C_GenerateSTOP(I2C1, ENABLE);
}
*/
#ifdef USE_FULL_ASSERT
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{}
}
#endif
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -