Description : cpp program body.
******************************************************************************/
#include "drv_cpp.h"
#define DRV_DEBUG
#include "drv_log.h"
/ semaphore used for rec /
#define CPP_RX_SEM_NAME "cpp_rx_sem"
static rt_sem_t cpp_rx_sem = RT_NULL;
static rt_device_t cpp_dev = RT_NULL;
struct serial_configure cpp_init_config = {
.baud_rate = BAUD_RATE_9600, / 9600 bits/s /
.data_bits = DATA_BITS_8, / 8 databits /
.stop_bits = STOP_BITS_1, / 1 stopbit /
.parity = PARITY_NONE, / No parity /
.bit_order = BIT_ORDER_LSB, / LSB first sent /
.invert = NRZ_NORMAL, / Normal mode /
.bufsz = RT_SERIAL_RB_BUFSZ, / Buffer size /
.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE, / Off flowcontrol /
.reserved = 0,
};
#define CPP_THREAD_NAME "cpp_thread"
#define CPP_THREAD_PRIORITY 25
#define CPP_THREAD_TIMESLICE 10
#define CPP_THREAD_STACKSIZE 1024
static rt_thread_t cpp_thread = RT_NULL;
rt_err_t drv_cpp_recv_cb(rt_device_t dev, rt_size_t size)
{
/ 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(cpp_rx_sem);
rt_kprintf("release.");
return RT_EOK;
}
static void cpp_thread_entry(void parameter)
{
// char cpp_recv[128];
while (1)
{
/ 从串口读取一个字节的数据,没有读取到则等待接收信号量 /
// while (rt_device_read(cpp_dev, -1, cpp_recv, 128))
// {
// / 阻塞等待接收信号量,等到信号量后再次读取数据 /
// rt_sem_take(cpp_rx_sem, RT_WAITING_FOREVER);
// }
/ 读取到的数据通过串口错位输出 /
// ch = ch + 1;
// rt_device_control(cpp_dev, cmd, arg);
// rt_device_write(cpp_dev, 0, &ch, 1);
// rt_kprintf("%s", cpp_recv);
// rt_device_write(cpp_dev, -1, "asdf", 4);
// rt_thread_mdelay(2000);
}
}
static int drv_cpp_init(void)
{
int result = RT_EOK;
/ sem create /
cpp_rx_sem = rt_sem_create(CPP_RX_SEM_NAME, 0, RT_IPC_FLAG_PRIO);
if(RT_NULL == cpp_rx_sem)
{
LOG_E("init %s failed.", CPP_RX_SEM_NAME);
}
/ cpp device init */
cpp_dev = rt_device_find(CPP_CLI_NAME);
if(RT_NULL == cpp_dev)
{
LOG_E("can't find device %s", CPP_CLI_NAME);
return RT_ERROR;
}
result = rt_device_control(cpp_dev, RT_DEVICE_CTRL_CONFIG, (void )&cpp_init_config);
if(RT_EOK != result)
{
LOG_E("can't control device %s", CPP_CLI_NAME);
return RT_ERROR;
}
// rt_device_open(cpp_dev, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
rt_device_open(cpp_dev, RT_DEVICE_FLAG_INT_RX);
rt_device_set_rx_indicate(cpp_dev, drv_cpp_recv_cb); // set rx callback function
/ create and start cpp_thread */
cpp_thread = rt_thread_create(CPP_THREAD_NAME, cpp_thread_entry, RT_NULL,
CPP_THREAD_STACKSIZE, CPP_THREAD_PRIORITY, CPP_THREAD_TIMESLICE);
if(RT_NULL == cpp_thread)
{
LOG_E("init thread %s failed.", CPP_THREAD_NAME);
return RT_ERROR;
}
rt_thread_startup(cpp_thread);
return RT_EOK;
}
INIT_DEVICE_EXPORT(drv_cpp_init);
int drv_cpp_send(char *str)
{
int result = RT_EOK;
rt_device_control(cpp_dev, RT_DEVICE_CTRL_CLR_INT, RT_NULL);
result = rt_device_write(cpp_dev, -1, str, sizeof(str));
rt_device_control(cpp_dev, RT_DEVICE_CTRL_SET_INT, RT_NULL);
return result;
}
想请教一下应该如何解决这种问题。。。