spc5 stdio【串口通信】

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)
//{
//
//
//};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 串口通信是一种通过串行接口进行数据传输的通信方式。异步通信是串口通信中的一种传输方式,即不同步地传输数据。 在C语言中,可以编写一个异步通信的demo程序来演示串口通信。 首先,需要包含相关的头文件,如<termios.h>用于串口配置,<fcntl.h>用于文件控制,<unistd.h>用于文件操作等。 接下来,需要定义串口的文件路径,如"/dev/ttyS0"表示串口设备文件。 然后,可以使用open函数打开串口设备文件,并设置相关的串口参数,如波特率、数据位、校验位、停止位等。 之后,可以使用read函数读取串口接收到的数据,并使用write函数向串口发送数据。 最后,使用close函数关闭串口设备文件。 下面是一个简单的异步通信demo程序的代码示例: #include <stdio.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> int main() { int fd; char buf[100]; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); if (fd == -1) { perror("open"); return -1; } struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= CLOCAL | CREAD; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_cflag &= ~CSTOPB; tcsetattr(fd, TCSANOW, &options); write(fd, "Hello", 5); sleep(1); read(fd, buf, sizeof(buf)); printf("Received: %s\n", buf); close(fd); return 0; } 这段程序实现了通过串口向外发送"Hello",然后从串口接收数据,并打印接收到的数据。 在实际应用中,可以根据具体需求修改程序中的串口设备文件路径和参数设置,并进行相应的错误处理。 ### 回答2: 串口通信是一种用于在计算机和外部设备之间传输数据的标准通信方式。在串口通信中,数据按照位的形式一位一位地传输,而不是以字节的形式。异步通信是串口通信中的一种常见模式,它以不等时间间隔传输数据。 在C语言中实现一个串口通信的异步通信demo,可以使用以下步骤: 1. 引入所需的头文件,如stdio.h、fcntl.h、unistd.h和termios.h。这些头文件提供了访问串口通信所需的函数和数据类型。 2. 打开串口文件。使用open函数打开串口设备文件,如/dev/ttyS0。如果打开成功,该函数将返回文件描述符。 3. 配置串口属性。使用tcgetattr和tcsetattr函数获取和设置串口的属性,包括波特率、数据位、停止位和校验位等。 4. 设置串口为非阻塞模式。使用fcntl函数将串口文件描述符设置为非阻塞模式,这样可以实现异步通信。 5. 读取和写入数据。使用read和write函数从串口中读取和写入数据。可以使用循环来实现连续的数据传输。 6. 关闭串口文件。使用close函数关闭串口文件。 以下是一个简单的异步串口通信demo的示例代码: ```c #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> int main() { int fd; char data; // 打开串口文件 fd = open("/dev/ttyS0", O_RDWR); if (fd == -1) { perror("Error opening serial port"); return 1; } // 配置串口属性 struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~CRTSCTS; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; tcsetattr(fd, TCSANOW, &options); // 设置串口为非阻塞模式 fcntl(fd, F_SETFL, O_NONBLOCK); // 读取和写入数据 while(1) { // 从串口中读取数据 if (read(fd, &data, 1) > 0) { printf("Received: %c\n", data); } // 向串口中写入数据 data = 'A'; write(fd, &data, 1); sleep(1); } // 关闭串口文件 close(fd); return 0; } ``` 上述代码实现了一个简单的异步串口通信demo,其中打开了/dev/ttyS0串口文件,配置了波特率为9600,然后不断读取和写入数据。这只是一个简单的示例,实际应用中需要根据具体需求进行适当的修改和完善。 ### 回答3: 串口通信是指通过串行接口进行数据传输的一种通信方式。在计算机领域中,串口通信通常用于连接计算机与外部设备,如打印机、调制解调器、传感器等。 异步通信demo c是一个使用C语言编写的示例程序,用于演示如何实现串口异步通信。该示例程序通过调用相关的库函数和API,实现了串口的打开、设置波特率、写入数据和读取数据等操作。用户可以根据需要进行修改和扩展,以满足具体的通信需求。 在串口通信中,异步通信是指数据传输的起始和停止时刻不依赖于时钟信号,在传输数据时,发送和接收两端的时钟信号可以有一定的差异。异步通信通过在数据传输中插入起始位和停止位来同步数据的传输。与之相对的是同步通信,同步通信需要在发送和接收两端保持相同的时钟信号,以实现数据的同步传输。 串口通信的优点包括可靠性高、传输距离远、抗干扰能力强等。异步通信demo c通过提供示例代码和相关函数库,简化了程序员对串口通信的开发和调试过程,提高了开发效率和可靠性。 总之,串口通信和异步通信demo c是一种常用的通信方式和相应的示例程序,通过串口连接计算机与外部设备进行数据传输,并通过异步通信方式实现数据的同步和可靠传输。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值