You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.7 KiB
C

#include "SWM221.h"
#define LIN_ID_Sensor 0x11 // <20>˴ӻ<CBB4><D3BB><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define LIN_ID_Switch 0x21 // <20>˴ӻ<CBB4><D3BB><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD> ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
#define LIN_NB_Sensor 6 // Number of Byte, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20>ֽ<EFBFBD>
#define LIN_NB_Switch 4
uint8_t Buffer_Sensor[LIN_NB_Sensor] = { 0 };
uint8_t Buffer_Switch[LIN_NB_Switch] = { 0x12, 0x34, 0x56, 0x78 };
volatile uint8_t LIN_Now_ID = 0;
volatile bool LIN_Now_enhanced_checksum = false;
volatile uint32_t LIN_Rcv_Index = 0;
volatile bool LIN_Rcv_Complete = false;
volatile uint32_t LIN_Trx_Index = 0;
void SerialInit(void);
void LIN_Master_Init(void);
void LIN_Master_Start(uint8_t lin_id, bool enhanced_checksum);
int main(void)
{
SystemInit();
SerialInit();
LIN_Master_Init();
TIMR_Init(BTIMR0, TIMR_MODE_TIMER, CyclesPerUs, 1000000/9600, 1); // <20><><EFBFBD><EFBFBD> break <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>һ<EFBFBD><D2BB> bit ʱ<><CAB1><EFBFBD>ٷ<EFBFBD><D9B7>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
TIMR_Init(BTIMR1, TIMR_MODE_TIMER, CyclesPerUs, 20000, 0);
while(1==1)
{
LIN_Master_Start(LIN_ID_Sensor, false); // <20><> Sensor <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
TIMR_Stop(BTIMR1);
TIMR_Start(BTIMR1);
TIMR_INTClr(BTIMR1);
while(!LIN_Rcv_Complete)
{
if(TIMR_INTStat(BTIMR1)) // 20ms δ<><CEB4><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD>ճ<EFBFBD>ʱ
{
goto retry;
}
}
LIN_Rcv_Complete = false;
printf("Data from Sensor: ");
for(int i = 0; i < LIN_NB_Sensor; i++)
{
printf("%02X, ", Buffer_Sensor[i]);
}
printf("\n\n");
LIN_Master_Start(LIN_ID_Switch, false); // <20><> Switch д<><D0B4><EFBFBD><EFBFBD>
retry:
for(int i = 0; i < SystemCoreClock/4; i++) __NOP();
}
}
void LIN_Master_Init(void)
{
UART_InitStructure UART_initStruct;
PORT_Init(PORTA, PIN5, PORTA_PIN5_UART1_RX, 1);
PORT_Init(PORTA, PIN4, PORTA_PIN4_UART1_TX, 0);
UART_initStruct.Baudrate = 9600;
UART_initStruct.DataBits = UART_DATA_8BIT;
UART_initStruct.Parity = UART_PARITY_NONE;
UART_initStruct.StopBits = UART_STOP_1BIT;
UART_initStruct.RXThreshold = 0;
UART_initStruct.RXThresholdIEn = 1;
UART_initStruct.TXThreshold = 0;
UART_initStruct.TXThresholdIEn = 0;
UART_initStruct.TimeoutTime = 10;
UART_initStruct.TimeoutIEn = 0;
UART_Init(UART1, &UART_initStruct);
UART_LINConfig(UART1, 13, 0, 14, 1);
UART_Open(UART1);
}
void LIN_Master_Start(uint8_t lin_id, bool enhanced_checksum)
{
LIN_Now_ID = lin_id;
LIN_Now_enhanced_checksum = enhanced_checksum;
UART_LINGenerate(UART1);
}
void UART1_Handler(void)
{
if(UART_LININTStat(UART1, UART_IT_LIN_GEN))
{
UART_LININTClr(UART1, UART_IT_LIN_GEN);
LIN_Rcv_Index = 0;
LIN_Trx_Index = 0;
TIMR_Start(BTIMR0);
}
else if(UART_INTStat(UART1, UART_IT_RX_THR))
{
uint32_t chr;
UART_ReadByte(UART1, &chr);
if(LIN_Rcv_Index < 2)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0x55 <20><> ID <20><><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>
}
else if(LIN_Rcv_Index < 2 + LIN_NB_Sensor)
{
Buffer_Sensor[LIN_Rcv_Index - 2] = chr;
}
else if(LIN_Rcv_Index == 2 + LIN_NB_Sensor)
{
/* TODO: <20><><EFBFBD><EFBFBD> Checksum <20><><EFBFBD>Ƚ<EFBFBD> */
LIN_Rcv_Complete = true;
}
LIN_Rcv_Index++;
}
else if(UART_INTStat(UART1, UART_IT_TX_THR))
{
if(LIN_Trx_Index < LIN_NB_Switch)
{
UART_WriteByte(UART1, Buffer_Switch[LIN_Trx_Index++]);
}
else if(LIN_Trx_Index == LIN_NB_Switch)
{
uint8_t checksum = UART_LIN_Checksum(LIN_Now_ID, Buffer_Switch, LIN_NB_Switch, LIN_Now_enhanced_checksum);
UART_WriteByte(UART1, checksum);
UART_INTDis(UART1, UART_IT_TX_THR);
}
}
}
void BTIMR0_Handler(void)
{
TIMR_Stop(BTIMR0);
TIMR_INTClr(BTIMR0);
UART_WriteByte(UART1, 0x55);
UART_WriteByte(UART1, UART_LIN_IDParity(LIN_Now_ID));
if(LIN_Now_ID == LIN_ID_Switch)
{
UART_INTEn(UART1, UART_IT_TX_THR);
}
}
void SerialInit(void)
{
UART_InitStructure UART_initStruct;
PORT_Init(PORTA, PIN0, PORTA_PIN0_UART0_RX, 1); //GPIOA.0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪUART0 RXD
PORT_Init(PORTA, PIN1, PORTA_PIN1_UART0_TX, 0); //GPIOA.1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪUART0 TXD
UART_initStruct.Baudrate = 57600;
UART_initStruct.DataBits = UART_DATA_8BIT;
UART_initStruct.Parity = UART_PARITY_NONE;
UART_initStruct.StopBits = UART_STOP_1BIT;
UART_initStruct.RXThreshold = 3;
UART_initStruct.RXThresholdIEn = 0;
UART_initStruct.TXThreshold = 3;
UART_initStruct.TXThresholdIEn = 0;
UART_initStruct.TimeoutTime = 10;
UART_initStruct.TimeoutIEn = 0;
UART_Init(UART0, &UART_initStruct);
UART_Open(UART0);
}
/******************************************************************************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: fputc()
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: printf()ʹ<EFBFBD>ô˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʵĴ<EFBFBD><EFBFBD>ڴ<EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: int ch Ҫ<EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
* FILE *f <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
* ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <EFBFBD><EFBFBD>
******************************************************************************************************************************************/
int fputc(int ch, FILE *f)
{
UART_WriteByte(UART0, ch);
while(UART_IsTXBusy(UART0));
return ch;
}