搜索
bottom↓
回复: 7

发一个RL-ARM的UART例程,采用队列缓冲,动态分配内存。

[复制链接]

出0入0汤圆

发表于 2012-9-11 00:10:48 | 显示全部楼层 |阅读模式
本帖最后由 luoyiming1984 于 2012-9-11 00:22 编辑

采用LPC2142芯片,有点老了,keil4最新版。
UART采用了16字节FIFO和队列buffer的方式,UartRead支持接收超时。
两个任务,因为保密的需要,我把FLASH,LCM,无线通信的驱动全部扣了(不要扁我,人不为己天诛地灭)。
一个任务是接收Uart数据,一个是把收到的发出来。
每个任务的堆栈分配,我用的动态分配,这样在删除任务时,可以把内存也删除掉。




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。

出0入0汤圆

发表于 2012-9-11 00:27:42 | 显示全部楼层
那能看到啥?

出0入0汤圆

 楼主| 发表于 2012-9-11 09:32:12 | 显示全部楼层
没有人看吗??

出0入0汤圆

发表于 2012-9-21 12:15:42 | 显示全部楼层

MARK一下~~·

出0入0汤圆

发表于 2012-9-21 21:23:08 | 显示全部楼层
能整个stm32上的串口的?

出0入0汤圆

 楼主| 发表于 2012-9-22 11:45:27 | 显示全部楼层
roguebear2012 发表于 2012-9-21 21:23
能整个stm32上的串口的?

已经做好了,连续收发鸭梨不大

出0入0汤圆

发表于 2012-9-22 13:10:29 | 显示全部楼层
luoyiming1984 发表于 2012-9-22 11:45
已经做好了,连续收发鸭梨不大

我来贴个。keil测试通过,感觉效率不太高啊。

兄台把你的整上来……



/*----------------------------------------------------------------------------
* Name:    Usart.c
* Purpose: USART usage for STM32
* Version: V1.00
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* Copyright (c) 2005-2007 Keil Software. All rights reserved.
*----------------------------------------------------------------------------*/




#include <stm32f10x_conf.h>   



/* 注意: 打开 RXNE          TXE 中断  */
#define USARTx                          USART1
#define USARTx_IRQHandler          USART1_IRQHandler

/*----------------------------------------------------------------------------
  Notes:
  The length of the receive and transmit buffers must be a power of 2.
  Each buffer has a next_in and a next_out index.
  If next_in = next_out, the buffer is empty.
  (next_in - next_out) % buffer_size = the number of characters in the buffer.
*----------------------------------------------------------------------------*/
#define TBUF_SIZE   256             /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
#define RBUF_SIZE   256      /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/

/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
#if TBUF_SIZE < 2
#error TBUF_SIZE is too small.  It must be larger than 1.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif

#if RBUF_SIZE < 2
#error RBUF_SIZE is too small.  It must be larger than 1.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif

/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
struct buf_st {
  unsigned int in;                                // Next In Index
  unsigned int out;                               // Next Out Index
  char buf [RBUF_SIZE];                           // Buffer

};

static struct buf_st rbuf = { 0, 0, 0};
#define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out))

static struct buf_st tbuf = { 0, 0, 0};
#define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out))

static unsigned int tx_restart = 1;               // NZ if TX restart is required

/*----------------------------------------------------------------------------
  USARTx_IRQHandler
  Handles USARTx global interrupt request.
*----------------------------------------------------------------------------*/
void USARTx_IRQHandler (void) {
  volatile unsigned int IIR;
  struct buf_st *p;

    IIR = USARTx->SR;
    if (IIR & USART_FLAG_RXNE) {                  // read interrupt
      USARTx->SR &= ~USART_FLAG_RXNE;                  // clear interrupt

      p = &rbuf;

      if (((p->in - p->out) & ~(RBUF_SIZE-1)) == 0) {
        p->buf [p->in & (RBUF_SIZE-1)] = (USARTx->DR & 0x1FF);
        p->in++;
      }
    }

    if (IIR & USART_FLAG_TXE) {
      USARTx->SR &= ~USART_FLAG_TXE;                  // clear interrupt

      p = &tbuf;

      if (p->in != p->out) {
        USARTx->DR = (p->buf [p->out & (TBUF_SIZE-1)] & 0x1FF);
        p->out++;
        tx_restart = 0;
      }
      else {
        tx_restart = 1;
                USARTx->CR1 &= ~USART_FLAG_TXE;                      // disable TX interrupt if nothing to send

      }
    }
}



/*------------------------------------------------------------------------------
  SenChar
  transmit a character
*------------------------------------------------------------------------------*/
int SendChar (int c) {
  struct buf_st *p = &tbuf;

                                                  // If the buffer is full, return an error value
  if (SIO_TBUFLEN >= TBUF_SIZE)
    return (-1);
                                                  
  p->buf [p->in & (TBUF_SIZE - 1)] = c;           // Add data to the transmit buffer.
  p->in++;

  if (tx_restart) {                               // If transmit interrupt is disabled, enable it
    tx_restart = 0;
        USARTx->CR1 |= USART_FLAG_TXE;                          // enable TX interrupt
  }

  return (0);
}

/*------------------------------------------------------------------------------
  GetKey
  receive a character
*------------------------------------------------------------------------------*/
int GetKey (void) {
  struct buf_st *p = &rbuf;

  if (SIO_RBUFLEN == 0)
    return (-1);

  return (p->buf [(p->out++) & (RBUF_SIZE - 1)]);
}


/*------------------------------------------------------------------------------
  buffer_Init
  initialize the buffers
*------------------------------------------------------------------------------*/
void buffer_Init (void) {

          tbuf.in = 0;                                    // Clear com buffer indexes
          tbuf.out = 0;
          tx_restart = 1;

          rbuf.in = 0;
          rbuf.out = 0;
}









#if 0

/*----------------------------------------------------------------------------
  Demo function
*----------------------------------------------------------------------------*/
int main (void) {

          buffer_Init();                                  // init RX / TX buffers
  
          stm32_Init ();                                  // STM32 setup

          printf ("\r\n\r\nInterrupt driven Serial I/O Example\r\n\r\n");

        while (1) {                                     // Loop forever
            printf ("Press a key. ");
   
            printf ("You pressed '%c'.\r\n\r\n", getchar () );

          } // end while
} // end main
#endif

出0入0汤圆

发表于 2012-9-23 23:13:50 | 显示全部楼层
借鉴下,学习啦
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-10-3 02:18

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表