#include "stm3210b_eval.h"
|
#include "stm32f10x.h"
|
#include "platform_config.h"
|
#include "stm32_eval_spi_flash.h"
|
#define __CAN1_USED__
|
#define CANx CAN1
|
#define GPIO_CAN GPIO_CAN1
|
#define GPIO_CAN GPIO_CAN1
|
#define GPIO_Pin_CAN_RX GPIO_Pin_CAN1_RX
|
#define GPIO_Pin_CAN_TX GPIO_Pin_CAN1_TX
|
|
u8 __IO USART2_RX_Timout;
|
extern u8 panel_ID;
|
CAN_InitTypeDef CAN_InitStructure;
|
CAN_FilterInitTypeDef CAN_FilterInitStructure;
|
DMA_InitTypeDef DMA_InitStructure;
|
extern SPI_InitTypeDef SPI_InitStructure;
|
CanTxMsg TxMessage;
|
u32 SPI1_TX_Buf;
|
extern u8 dis_now;
|
extern u8 FR;
|
u8 fre_Data[4]={0};
|
void GPIO_init(void)
|
{
|
GPIO_InitTypeDef GPIO_InitStructure;
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
USART_InitTypeDef USART_InitStructure;
|
USART_ClockInitTypeDef USART_ClockInitStructure;
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
|
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_10;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
|
|
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
|
GPIO_ResetBits(GPIOA, GPIO_Pin_10);
|
GPIO_SetBits(GPIOA, GPIO_Pin_9);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_RX;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5 | GPIO_Pin_7;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
// A3 ×öR2X
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
// A2 ×öT2X
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
/* Configure CAN pin: TX */
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_TX;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
|
USART_InitStructure.USART_BaudRate = 115200;
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
|
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
|
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
|
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
|
|
USART_ClockInit(USART3, &USART_ClockInitStructure);
|
USART_Init(USART3, &USART_InitStructure);
|
|
USART_Cmd(USART3, ENABLE);
|
//´®¿Ú2ʹÓýÓÊÕÖжÏ
|
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
|
|
/* TIM2 clock enable */
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
/* »ù´¡ÉèÖÃ*/
|
TIM_TimeBaseStructure.TIM_Period = 1000; //¼ÆÊýÖµ
|
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //Ô¤·ÖƵ,´ËÖµ+1Ϊ·ÖƵµÄ³ýÊý
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
/*ʹÄÜÔ¤×°ÔØ*/
|
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
/*Ô¤ÏÈÇå³ýËùÓÐÖжÏλ*/
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
/* 4¸öͨµÀºÍÒç³ö¶¼ÅäÖÃÖжÏ*/
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
/* ÔÊÐíTIM2¿ªÊ¼¼ÆÊý */
|
TIM_Cmd(TIM2, DISABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
TIM_TimeBaseStructure.TIM_Period = 1000; //¼ÆÊýÖµ
|
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //Ô¤·ÖƵ,´ËÖµ+1Ϊ·ÖƵµÄ³ýÊý
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
|
|
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
|
TIM_ARRPreloadConfig(TIM3, ENABLE);
|
/*Ô¤ÏÈÇå³ýËùÓÐÖжÏλ*/
|
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
|
/* 4¸öͨµÀºÍÒç³ö¶¼ÅäÖÃÖжÏ*/
|
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
|
/* ÔÊÐíTIM2¿ªÊ¼¼ÆÊý */
|
TIM_Cmd(TIM3, DISABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
TIM_TimeBaseStructure.TIM_Period = 1000; //¼ÆÊýÖµ
|
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //Ô¤·ÖƵ,´ËÖµ+1Ϊ·ÖƵµÄ³ýÊý
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
|
|
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
|
TIM_ARRPreloadConfig(TIM4, ENABLE);
|
/*Ô¤ÏÈÇå³ýËùÓÐÖжÏλ*/
|
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
|
/* 4¸öͨµÀºÍÒç³ö¶¼ÅäÖÃÖжÏ*/
|
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
|
/* ÔÊÐíTIM2¿ªÊ¼¼ÆÊý */
|
TIM_Cmd(TIM4, DISABLE);
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
|
|
CAN_DeInit(CANx);
|
CAN_StructInit(&CAN_InitStructure);
|
|
/* CAN cell init */
|
CAN_InitStructure.CAN_TTCM = DISABLE;
|
CAN_InitStructure.CAN_ABOM = DISABLE;
|
CAN_InitStructure.CAN_AWUM = DISABLE;
|
CAN_InitStructure.CAN_NART = DISABLE;
|
CAN_InitStructure.CAN_RFLM = DISABLE;
|
CAN_InitStructure.CAN_TXFP = DISABLE;
|
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
|
|
/* CAN Baudrate = 1MBps*/
|
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
|
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
|
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
|
CAN_InitStructure.CAN_Prescaler =6;
|
CAN_Init(CAN1, &CAN_InitStructure);
|
|
/* CAN filter init */
|
|
CAN_FilterInitStructure.CAN_FilterNumber = 0;
|
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
|
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
|
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
|
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
|
CAN_FilterInit(&CAN_FilterInitStructure);
|
|
CAN_FilterInitStructure.CAN_FilterNumber = 1;
|
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
|
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
|
CAN_FilterInitStructure.CAN_FilterIdHigh = panel_ID<<5; //001;
|
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xffe0;
|
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
|
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
|
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
|
CAN_FilterInit(&CAN_FilterInitStructure);
|
|
/* Transmit */
|
TxMessage.StdId = 0x001;
|
TxMessage.ExtId = 0x00;
|
TxMessage.RTR = 0;
|
TxMessage.IDE = 0;
|
TxMessage.DLC = 2;
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
SPI_Init(SPI1, &SPI_InitStructure);
|
SPI_Cmd(SPI1, ENABLE);
|
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
|
DMA_DeInit(DMA1_Channel2);
|
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&SPI1->DR;//SPI1µÄDR¼Ä´æÆ÷µØÖ·
|
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&fre_Data;
|
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //ÄÚ´æÊÇÄ¿µÄµØ
|
DMA_InitStructure.DMA_BufferSize = 4;
|
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DMAÄÚ´æµØÖ·×Ô¶¯Ôö¼Óģʽ
|
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
|
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
|
DMA_Cmd(DMA1_Channel2, DISABLE);
|
}
|
int USART3_Wait_Response(char * buf)
|
{ int c=0;
|
TIM_Cmd(TIM2, ENABLE);
|
while(1)
|
{ /* Loop until timeout */
|
while(((USART3->SR&USART_FLAG_RXNE) == 0)&&(USART2_RX_Timout == 0));
|
if(USART2_RX_Timout == 1) break;
|
* buf = USART3->DR & 0x01FF;
|
buf ++;
|
c++;
|
}
|
USART2_RX_Timout = 0;
|
TIM_Cmd(TIM2, DISABLE);
|
return c;
|
}
|
|
void USART3_Puts(char * str)
|
{
|
while(*str)
|
{
|
USART_SendData(USART3, *str++);
|
/* Loop until the end of transmission */
|
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
|
}
|
|
}
|
void USART3_Putc(unsigned char c)
|
{
|
USART_SendData(USART3, c);
|
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET );
|
}
|
void USART3_Putdata(char *str,int i)
|
{ int c;
|
for(c=0;c<i;c++)
|
{
|
USART3->DR = (* str ++);
|
while((USART3->SR & USART_FLAG_TXE) == 0);
|
|
}
|
|
}
|