显然,当我尝试通过 usart 从我的 PC (Ubuntu) 向我的 Nucleo64 板发送字符时,我遇到了 ttyACM0 问题。我打开文件:
#include <stdio.h>
#include <string.h>
// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
int main() {
int serial_port = open("/dev/ttyUSB0", O_RDWR);
// Check for errors
if (serial_port < 0) {
printf("Error %i from open: %s\n", errno, strerror(errno));
}
// Create new termios struct, we call it 'tty' for convention
// No need for "= {0}" at the end as we'll immediately write the existing
// config to this struct
struct termios tty;
// Read in existing settings, and handle any error
// NOTE: This is important! POSIX states that the struct passed to tcsetattr()
// must have been initialized with a call to tcgetattr() overwise behaviour
// is undefined
if(tcgetattr(serial_port, &tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
//tty.c_cflag |= PARENB; // Set parity bit, enabling parity
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
//tty.c_cflag |= CSTOPB; // Set stop field, two stop bits used in communication
tty.c_cflag &= ~CSIZE; // Clear all the size bits, then use one of the statements below
//tty.c_cflag |= CS5; // 5 bits per byte
//tty.c_cflag |= CS6; // 6 bits per byte
//tty.c_cflag |= CS7; // 7 bits per byte
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
//tty.c_cflag |= CRTSCTS; // Enable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON; // diseable canonical mode
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
//tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT IN LINUX)
//tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT IN LINUX)
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
// Set in/out baud rate to be 9600
cfsetspeed(&tty, B9600);
// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
unsigned char msg[] = { 1, 0, 100, 50, 100, 2, 'a','a', '\n' };
write(serial_port, msg, sizeof(msg));
}
对于时钟和 USART 配置,这里是代码
void init_UART2() {
/* Enable USART2 clock */
//RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
GPIOA->CRL = 0;
GPIOA->CRL |= (3<<8); // output mode 50 MHz for PA2
GPIOA->CRL |= (2<<10); // Alternate Func Push Pull For PA2
GPIOA->CRL &= ~(3<<12); // Intput Mode For PA3
GPIOA->CRL |= (2<<14); // Input Pull Up/ Down For PA3
GPIOA->ODR |= 1<<3; // Pull Up for PA3
USART2->CR1 = 0x00; // Clear ALL
USART2->CR1 |= (1<<13); // UE = 1... Enable USART
USART2->BRR = (7<<0) | (24<<4); // Baud rate of 115200, PCLK1 at 45MHz
USART2->CR1 |= (1<<2); // RE=1.. Enable the Receiver
USART2->CR1 |= (1<<3);
/* Configure the USART2 baud rate to 9600 */
USART2->BRR = 234 << USART_BRR_DIV_Mantissa_Pos;
USART2->BRR |= 6 << USART_BRR_DIV_Fraction_Pos;
USART2->CR1 |= USART_CR1_RXNEIE;
/* 8 data bits, parity control disabled, parity even */
USART2->CR1 &= ~(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS);
/* 1 stop bit */
USART2->CR2 &= ~(USART_CR2_STOP_Msk);
USART2->CR3 &= ~(USART_CR3_CTSE | USART_CR3_RTSE);
USART2->CR1 |= USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE;
/* Enable USART2 interrupt */
NVIC_EnableIRQ(USART2_IRQn);
}
void systemClockConfig(){
/* enables the internal high-speed clock (HSI) */
RCC->CR |= RCC_CR_HSION;
/* waits until HSI is ready */
while (!(RCC->CR & RCC_CR_HSIRDY)) {
;
}
/* sets PLL to HSI * 9 */
RCC->CFGR &= ~RCC_CFGR_PLLMULL16;
RCC->CFGR |= RCC_CFGR_PLLMULL9;
/* turns on PLL */
RCC->CR |= RCC_CR_PLLON;
while (!(RCC->CR & RCC_CR_PLLRDY)) {
;
}
/* set peripherals clock as PLL */
RCC->CFGR &= ~RCC_CFGR_SW;
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {
;
}
然后我通过这个串口传输数据,但是没有任何反应。我用windows的时候没有这个问题!也许是微型 USB 电缆的问题?有人可以向我强调这个问题我该如何解决?
我只有我的 nucleo64-stm32f10rb 通过 USB 线连接到我的电脑。我想使用打开 ttyACM0
的 C 代码将数据(字符)从我的电脑发送到开发板