大家好,
我打算用PC的串口进行DSP和Matlab之间传递数据,急求DSP读写PC串口的程序,我的板子是EVM6678L,CCS是v5.0.3版本。
非常感谢!
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
大家好,
我打算用PC的串口进行DSP和Matlab之间传递数据,急求DSP读写PC串口的程序,我的板子是EVM6678L,CCS是v5.0.3版本。
非常感谢!
补充一下,Titan提供的例子是MSCDK的POST工具例程,POST是“上电自检程序”,详细解释在此:processors.wiki.ti.com/.../BIOS_MCSDK_2.0_User_Guide
POST主要是实现对板上的外设做一些简单的测试看是否运行正常,比如进行“External memory read/write test, NAND read test, UART write test, LED test”等,测试完后将测试结果写给UART。
所以可以参考在例程中(主要看src文件夹下的post.c)的post_write_uart函数,此函数既用于测试UART口,又用于将所有测试结果写给UART口。
Melody您好,
感谢您的回复!我正在看post.c的例程,请问这个例程是不是只能写串口,是否可以读串口的数据呢?因为我的应用需要DSP和Matlab进行数据交换,所以既要写串口也要读串口。
谢谢!
Melody您好,
我在测试post的例程,例程可以成功运行并在串口上打印出结果,但是只能打印在USB的串口上,而不打印在RS-232的串口上面。我想通过RS-232的串口与Matlab传递数据,请问该如何设置呢?
谢谢!
platform_uart_read和platform_uart_write函数的定义是在需要函数库头文件platform.h里面。
您可以从
includes-->\pdk_C6678_1_0_0_17\packages\ti\platform\evmc6678l\platform_lib ---->platform.h ---->找到platform_uart_init, platform_uart_write和platform_uart_read
Titan您好,
我加了头文件和文件库,现在可以编译通过并运行了,但是我运行写串口程序,串口上只打印出所要写的第一个字母。如下是我的程序:
#include <string.h>
#include "ti\platform\platform.h"
#include "ti\platform\resource_mgr.h"
/* OSAL functions for Platform Library */
uint8_t *Osal_platformMalloc (uint32_t num_bytes, uint32_t alignment)
{
return malloc(num_bytes);
}
void Osal_platformFree (uint8_t *dataPtr, uint32_t num_bytes)
{
/* Free up the memory */
if (dataPtr)
{
free(dataPtr);
}
}
void Osal_platformSpiCsEnter(void)
{
/* Get the hardware semaphore.
*
* Acquire Multi core CPPI synchronization lock
*/
while ((CSL_semAcquireDirect (PLATFORM_SPI_HW_SEM)) == 0);
return;
}
void Osal_platformSpiCsExit (void)
{
/* Release the hardware semaphore
*
* Release multi-core lock.
*/
CSL_semReleaseSemaphore (PLATFORM_SPI_HW_SEM);
return;
}
int post_write_uart
(
char* msg
)
{
uint32_t i;
uint32_t msg_len = strlen(msg);
/* Write the message to the UART */
for (i = 0; i < msg_len; i++)
{
if (platform_uart_write(msg[i]) != Platform_EOK)
{
return -1;
}
}
return 0;
}
void main(void)
{
post_write_uart("HELLO");
}
其中OSAL部分我不清楚是什么意思,但是去掉的话不能编译通过。执行上述程序后,串口只打印H,而不是HELLO。请问这是为什么呢?
谢谢!
您好,
OSAL部分是一定要加的,因为这部分函数的返回值为以下三点
• Memory Management
• Critical Sections
• Cache Coherency
你也可以参见platform_osal.c文件或者引用它在你的工程里
它的位置\mcsdk_2_00_09_21\demos\image_processing\ipc\master\src\system
关于你连续运行出错你尝试加
Uint32 oldTSCL;
oldTSCL = CSL_chipWriteTSCL(0);
语句在你函数的前边,因为有时TSCL寄存器必须要写一下才行(任何值这里就写零了)
Titan您好,
感谢您的回复!我在main函数里加上了如下语句
platform_init_flags init_flags;
platform_init_config init_config;
memset(&init_config, 0, sizeof(platform_init_config));
memset(&init_flags, 0x01, sizeof(platform_init_flags));
platform_init(&init_flags, &init_config);
应该是初始化platform的语句,现在可以正确写串口了。但是这个写串口函数是写char型变量,我想写float型的数组该如何改写函数呢?我把函数改成如下形式,但是打印出来的结果是乱码。
int post_write_uart(int*H)
{
uint32_t i;
for (i = 0; i < 10; i++)
{
if (platform_uart_write(H[i]) != Platform_EOK)
{
return -1;
}
}
return 0;
}
Yang您好,
在TI_PlatformLibrary_BIOS-MCSDK-API-Guide的帮助文档中你可以找到platform_uart_write函数的定义
文件所在的位置:\pdk_C6678_1_0_0_21\packages\ti\platform\docs\platform
Platform_STATUS platform_uart_write (uint8_t chr) \\ Write a character to the UART.
所以你用该函数直接传float型的变量是不可以的,正如Titan所说的“串口打印的是字节”
如果你想把数据通过串口发到matlab,我的一些建议:
1. 你要打开matlab的串口功能(估计你已经实现了)
2.你要把所传数据进行转换,比如你想传float类型的0.15,你可以先在dsp中将其乘以100转化成15在强制类型转换成uint8_t类型,然后通过串口传到matlab,在matlab端进行除以100来实现原值。当然这样做优缺点,但只能用这种相当于数据协议的方式传递
希望有问题继续讨论!
Titan & Yue Cao
非常感谢二位的解答,我大概知道应该如何操作float数据了。这里我想再问一下DSP读串口的函数问题。我从Matlab发数据到串口,然后用DSP通过platform_uart_read函数读取,但是函数返回的状态值是-1,也就是读数失败。下面是我的DSP程序的主函数以及Matlab写串口的代码,请二位帮忙看一下,问题出在哪里,非常感谢!
DSP程序主函数:
void main(void)
{
platform_init_flags init_flags;
platform_init_config init_config;
uint8_t buf;
uint32_t delay=1;
Platform_STATUS sts;
memset(&init_config, 0, sizeof(platform_init_config));
memset(&init_flags, 0x01, sizeof(platform_init_flags));
platform_init(&init_flags, &init_config);
platform_uart_init();
platform_uart_set_baudrate(115200);
sts=platform_uart_read(&buf, delay);
}
Matlab写串口代码:
s=serial('COM3');
set(s,'BaudRate',115200,'Timeout',20,'InputBufferSize',50,'OutputBufferSize',50);
fopen(s);
fprintf(s,'HELLO');
fclose(s);
delete(s);
clear s;