main.c
/****************************************************************************
*
* Copyright 漏 2015-2021 STMicroelectronics - All Rights Reserved
*
* This software is licensed under SLA0098 terms that can be found in the
* DM00779817_1_0.pdf file in the licenses directory of this software product.
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS," AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*****************************************************************************/
/* Inclusion of the main header files of all the imported components in the
order specified in the application wizard. The file is generated
automatically.*/
#include "components.h"
#include "serial_lld_cfg.h"
#include "serial_lld.h"
#include "delay.h"
#include "usart.h"
uint8_t receiver_buffer[200];
//uint16_t len;
// extern uint8_t buff[100];
// extern uint8_t len;
// extern uint8_t isToSend;
uint8_t buff[]= "helloworld";
//uint8_t buff1[20]="helloworldhelloworld";
uint8_t buff1[20];
uint8_t len;
/*
* Application entry point.
*/
int main(void) {
/* Initialization of all the imported components in the order specified in
the application wizard. The function is generated automatically.*/
componentsInit();
/* Uncomment the below routine to Enable Interrupts. */
irqIsrEnable(); //中断使能
sd_lld_start(&SD3,&serial_config_Serial_9600_cfg); //串口初始化
//serial_RcvDataSet(); //串口接收使能中断
// Driver_USART1_SendChar(&SD3,'a');
//irqIsrDisable();
/* Application main loop.*/
for ( ; ; ) {
pal_lld_togglepad(PORT_E,LD1);
//Driver_USART1_SendChar(&SD3,'a');
//Driver_USART1_ReceiveString(&SD3,buff,&len);
//Driver_USART1_SendString(&SD3,buff,len);
//if(sdp->linflexlp->LINIER.B.DRIE = 1);
serial_rx(&SD3,buff1,20); //串口接收数据
delay_ms(1000);
//serial_SendDataSet(); //串口发送使能中断
serial_tx(&SD3,buff1,20); //串口发送数据
//sd_lld_write(&SD3,(uint8_t *)"100ms run here\r\n", 16);
//sd_lld_read(&SD3,receiver_buffer, 20);
//sd_lld_write(&SD3,receiver_buffer, 20);
delay_ms(1000);
}
}
void SendData(SerialDriver *sdp)
{
}
void RxHandle(SerialDriver *sdp)
{
}
usart.h
/*
* usart.h
*
* Created on: 2024锟斤拷6锟斤拷17锟斤拷
* Author: Administrator
*/
#ifndef INCLUDE_USART_H_
#define INCLUDE_USART_H_
#include <spc5_lld.h>
#include <lldconf.h>
#include "xpc560bc.h"
#include "spc5_linflex.h"
#define BITS(start, end) ((0xFFFFFFFFUL << (start)) & (0xFFFFFFFFUL >> (31U - (uint32_t)(end))))
#define GET_BITS(regval, start, end) (((regval) & BITS((start),(end))) >> (start))
//extern uint8_t receiver_buffer[10];
//extern uint8_t Serial_RcvFrame = "helloworld";
//extern uint8_t usartRxBuffer[]
void serial_RcvDataSet(void);
void serial_SendDataSet(void);
void serial_rx(SerialDriver* sdp, uint8_t* buffer, uint16_t len);
void serial_tx(SerialDriver *sdp, uint8_t *buffer, uint16_t len);
void Driver_USART1_SendChar(SerialDriver* sdp,uint8_t byte);
void Driver_USART1_SendString(SerialDriver* sdp,uint8_t *str, uint16_t len);
void Driver_USART1_ReceiveString(SerialDriver* sdp,uint8_t buff[], uint8_t *len);
void serial_RcvData(uint8_t* buffer, uint16_t len);
//uint16_t usart_data_receive(uint32_t usart_periph);
//void USART_RxHandle(SerialDriver *sdp);
#endif /* INCLUDE_USART_H_ */
uasrt.c
/*
* usart.c
*
* Created on: 2024锟斤拷6锟斤拷17锟斤拷
* Author: Administrator
*/
#include "serial_lld.h"
#include "usart.h"
#include "serial_lld_cfg.h"
#include "xpc560bc.h"
#include "spc5_linflex.h"
//uint8_t receiver_buffer[100];
// uint8_t buff[100];
//发送
void serial_SendDataSet(void)
{
SD3.linflexlp->UARTCR.B.RXEN = 0;
SD3.linflexlp->LINIER.B.DRIE = 0;
}
//接收
void serial_RcvDataSet(void)
{
SD3.linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DRIE |SPC5_LIN_INTERRUPTS);
SD3.linflexlp->UARTCR.B.RXEN = 1;
SD3.linflexlp->LINIER.B.DRIE = 1;
}
void serial_tx(SerialDriver *sdp, uint8_t *buffer, uint16_t len) {
if (len > 0UL) {
uint8_t c;
sdp->tx_buf = buffer;
sdp->tx_len = len - 1U;
sdp->linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DTIE | SPC5_LIN_INTERRUPTS); /* Interrupts enabled. */
sdp->linflexlp->UARTCR.B.TXEN = 1;
/* Update of tx_buf is shared with interrupt routine.
* A write to DATA0 register can trigger an interrupt, make sure
* to increment tx_buf before writing to DATA0 register.
*/
c = *sdp->tx_buf++;
sdp->linflexlp->BDRL.B.DATA0 = c;
}
}
void serial_rx(SerialDriver* sdp, uint8_t* buffer, uint16_t len) {
if (len > 0UL) {
if(sdp->config->api_mode == SPC5_LIN_API_MODE_BUFFERED_IO) {
uint16_t l = len;
sdp->linflexlp->LINIER.B.DRIE = 0;
while (l != 0U) {
if (sdp->rx_read_ptr != sdp->rx_write_ptr) {
*buffer++ = *sdp->rx_read_ptr++;
}
l--;
}
sdp->linflexlp->LINIER.B.DRIE = 1; //数据接收完成标志
} else {
sdp->rx_buf = buffer;
sdp->rx_len = len;
sdp->linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DRIE | SPC5_LIN_INTERRUPTS); /* Interrupts enabled. */
sdp->linflexlp->UARTCR.B.RXEN = 1; //接收使能
}
}
//return len;
}
//void serial_rx(SerialDriver* sdp, uint8_t* buffer, uint16_t len) {
//
// if (len > 0UL) {
// if(sdp->config->api_mode == SPC5_LIN_API_MODE_SYNCHRONOUS) {
// uint16_t l = len;
// sdp->linflexlp->LINIER.B.DRIE = 0;
// while (l != 0U) {
// if (sdp->rx_read_ptr != sdp->rx_write_ptr) {
// sdp->linflexlp->LINIER.B.DRIE = 0;
// *buffer++ = *sdp->rx_read_ptr++;
// if (sdp->rx_read_ptr == sdp->rx_end_ptr) {
// sdp->rx_read_ptr = sdp->rx_buffered_io;
// }
// l--;
// } else {
// sdp->linflexlp->LINIER.B.DRIE = 1;
// }
// }
// sdp->linflexlp->LINIER.B.DRIE = 1;
// } else {
// sdp->rx_buf = buffer;
// sdp->rx_len = len;
// sdp->linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DRIE | SPC5_LIN_INTERRUPTS); /* Interrupts enabled. */
// sdp->linflexlp->UARTCR.B.RXEN = 1;
// }
// }
// //return len;
//}
//void serial_rx(SerialDriver *sdp, uint8_t *buffer, uint16_t len)
//{
//
// if (len > 0UL) {
// uint8_t c;
//
// sdp->rx_buf = buffer;
// sdp->rx_len = len - 1U;
// sdp->linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DRIE | SPC5_LIN_INTERRUPTS); /* Interrupts enabled. */
// sdp->linflexlp->UARTCR.B.RXEN = 1;
//
// uint16_t l = len;
// sdp->linflexlp->LINIER.B.DRIE = 0;
// while (l != 0U) {
// if (sdp->rx_read_ptr != sdp->rx_write_ptr) {
// sdp->linflexlp->LINIER.B.DRIE = 0;
// *buffer++ = *sdp->rx_read_ptr++;
// if (sdp->rx_read_ptr == sdp->rx_end_ptr) {
// sdp->rx_read_ptr = sdp->rx_buffered_io;
// }
// l--;
// } else {
// sdp->linflexlp->LINIER.B.DRIE = 1;
//
// c = *sdp->rx_buf++;
// sdp->linflexlp->BDRL.B.DATA0 = c;
// }
// }
// }
// (void)sdp;
//}
//void serial_rx(SerialDriver *sdp, uint8_t *buffer, uint16_t len)
//{
//
// if (len > 0UL) {
// uint8_t c;
//
// sdp->rx_buf = buffer;
// sdp->rx_len = len - 1U;
// sdp->linflexlp->LINIER.R |= (uint16_t)(SPC5_LINIER_DRIE | SPC5_LIN_INTERRUPTS); /* Interrupts enabled. */
// sdp->linflexlp->UARTCR.B.RXEN = 1;
//
// c = *sdp->rx_buf++;
// sdp->linflexlp->BDRL.B.DATA0 = c;
// }
// (void)sdp;
//}
/**
* @description: 发送一个字节
* @param {uint8_t} byte 要发送的字节
*/
void Driver_USART1_SendChar(SerialDriver* sdp,uint8_t byte)
{
//while(irqGetExtIntEnable() == 0U);
while(sdp->linflexlp->LINSR.B.DTF == 1U); //数据发送完成标志
//sdp->tx_buf = byte;
sdp->linflexlp->BDRL.B.DATA0 = byte;
}
/**
* @description: 发送一个字符串
* @param {uint8_t} *str 要发送的字符串
* @param {uint16_t} len 字符串中字节的长度
* @return {*}
*/
void Driver_USART1_SendString(SerialDriver* sdp,uint8_t *str, uint16_t len)
{
for (uint16_t i = 0; i < len; i++)
{
Driver_USART1_SendChar(sdp,str[i]);
}
}
void Driver_USART1_ReceiveString(SerialDriver* sdp,uint8_t buff[], uint8_t *len){
uint8_t i =0;
while(1)
{
if(sdp->linflexlp->LINIER.B.DRIE == 1)
{
*len = i;
return;
}
buff[i] =sdp->linflexlp->BDRL.B.DATA0;
i++;
}
}
//uint8_t Serial_RcvFrame = "helloworld";
//uint8_t usartRxBuffer[] = "helloworld";
//uint8_t usartRxBuffer[]="helloworld";
//uint8_t usartTxBuffer[]="ms";
//
// void USART_TxHandle(SerialDriver *sdp)
// {
// sd_lld_write(&SD3, usartTxBuffer, 2);
// delay_ms(1000);
//
// };
//
// void USART_RxHandle(SerialDriver *sdp)
// {
// sd_lld_read(&SD3, usartTxBuffer, 10);
// delay_ms(1000);
//
// };
// void USART_TxHandle(SerialDriver *sdp)
// {
//
// };
//
// void USART_RxHandle(SerialDriver *sdp)
// {
//
// };
//void USART_RxHandle(SerialDriver *sdp)
//{
// //uint8_t receiver_buffer;
// uint8_t i;
// for(i =0;i<10;i++){
// receiver_buffer[i]=1;
// }
// //(void)sdp;
//};
//uint16_t usart_data_receive(uint32_t usart_periph)
//{
// return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
//};
//void serial_RcvData(uint8_t* buffer, uint16_t len)
//{
//
//
//};
//uint16_t usart_data_receive(uint32_t usart_periph)
//{
// return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
//};
//void serial_RcvData(uint8_t* buffer, uint16_t len)
//{
//
//
//};