受教黑金文档,再度优化兼容irq uart代码
主要修改了函數類型,兼容了普通和增強型中斷
主要函數如下:
(1)uart_regs.h
(2)mcu_uart.h
(3)mcu_uart.c
(4)sys_main.c
?
//--------------------------------------------------------------------------
/*
* uart_regs.h
*
*? Created on: 2011-4-4
*????? Author: CrazyBingo
*/
#ifndef UART_REGS_H_
#define UART_REGS_H_
#define _UART
//-----------------------------------------
typedef struct
{
???? //接收寄存器
???? union
???? {
???????? struct
???????? {
???????????? volatile unsigned long int RECEIVE_DATA??? :8;
???????????? volatile unsigned long int NC??????????????? :24;
???????? }BITS;
???????? volatile unsigned long int WORD;
???? }RXDATA;
???? //収送寄存器
???? union
???? {
???????? struct
???????? {
???????????? volatile unsigned long int TRANSMIT_DATA??? :8;
???????????? volatile unsigned long int NC??????????????? :24;
???????? }BITS;
???????? volatile unsigned long int WORD;
???? }TXDATA;
???? //狀忞寄存器
???? union
???? {
???????? struct
???????? {
??????????? volatile unsigned long int PE??????? :1;
??????????? volatile unsigned long int FE??????? :1;
??????????? volatile unsigned long int BRK??????? :1;
??????????? volatile unsigned long int ROE??????? :1;
??????????? volatile unsigned long int TOE??????? :1;
??????????? volatile unsigned long int TMT??????? :1;
??????????? volatile unsigned long int TRDY??????? :1;
??????????? volatile unsigned long int RRDY??????? :1;
??????????? volatile unsigned long int E??????? :1;
??????????? volatile unsigned long int NC??????? :1;
??????????? volatile unsigned long int DCTS??????? :1;
??????????? volatile unsigned long int CTS??????? :1;
??????????? volatile unsigned long int EOP??????? :1;
??????????? volatile unsigned long int NC1??????? :19;
???????? }BITS;
???????? volatile unsigned long int WORD;
???? }STATUS;
???? //控刢寄存器
???? union
???? {
???????? struct
???????? {
???????????? volatile unsigned long int IPE??????? :1;
???????????? volatile unsigned long int IFE??????? :1;
???????????? volatile unsigned long int IBRK??? :1;
???????????? volatile unsigned long int IROE??? :1;
???????????? volatile unsigned long int ITOE??? :1;
???????????? volatile unsigned long int ITMT??? :1;
???????????? volatile unsigned long int ITRDY??? :1;
???????????? volatile unsigned long int IRRDY??? :1;
???????????? volatile unsigned long int IE??????? :1;
???????????? volatile unsigned long int TRBK??? :1;
???????????? volatile unsigned long int IDCTS??? :1;
???????????? volatile unsigned long int RTS??????? :1;
???????????? volatile unsigned long int IEOP??? :1;
???????????? volatile unsigned long int NC??????? :19;
???????? }BITS;
???????? volatile unsigned long int WORD;
???? }CONTROL;
???? //波特率分頻器
???? union
???? {
???????? struct
???????? {
???????????? volatile unsigned long int BAUD_RATE_DIVISOR??? :16;
???????????? volatile unsigned long int NC????????????????? :16;
???????? }BITS;
???????? volatile unsigned int WORD;
???? }DIVISOR;
}UART_STR;
#ifdef _UART
#define UART??? ((UART_STR *)UART_BASE)
#endif
#endif /* UART_REGS_H_ */
?
//--------------------------------------------------------------------------
/*
* uart.h
*
*? Created on: 2011-4-4
*????? Author: CrazyBingo
*/
#ifndef MCU_UART_H_
#define MCU_UART_H_
#include "../inc/uart_regs.h"
#define BUFFER_SIZE 200
typedef struct{
???????????????? unsigned char??? mode_flag;????? //xmodem 1;uart 0;
???????????????? unsigned int??? receive_flag;
???????????????? unsigned int??? receive_count;
???????????????? unsigned char??? receive_buffer??? [BUFFER_SIZE];
???????????????? alt_u8??????????? (* send_byte)??? (unsigned char data);
???????????????? void???????????? (* send_string)??? (unsigned int len, unsigned char *str);
???????????????? alt_u8??????????? (* init)??????? (void);
???????????????? alt_u8??????????? (* baudrate)??? (unsigned int baudrate);
}UART_T;
extern UART_T uart;
#endif /*MCU_UART_H_*/
?
//--------------------------------------------------------------------------
/*
* uart.c
*
*? Created on: 2011-4-4
*????? Author: CrazyBingo
*/
#include <system.h>
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "../inc/uart_regs.h"
#include "../inc/mcu_uart.h"
static alt_u8??? uart_send_byte(unsigned char data);
static void???? uart_send_string(unsigned int len, unsigned char *str);
static alt_u8???? uart_init(void);
static alt_u8???? uart_set_baudrate(unsigned int baudrate);
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
static void uart_ISR(void* context);
#else
static void uart_ISR(void* context, alt_u32 id);
#endif
?
//初始化 UART結構體
UART_T uart=
{
???? .mode_flag??????? =??? 0,
???? .receive_flag??? =??? 0,
???? .receive_count??? =??? 0,
???? .send_byte??????? =??? uart_send_byte,
???? .send_string??? =??? uart_send_string,
???? .init??????????? =??? uart_init,
???? .baudrate??????? =??? uart_set_baudrate
};
//發送一個字節數據
//將發送的數據放到發送數據緩沖區內,等待狀態寄存器 TRDY置 1,當 TRDY置 1,說明接收完畢
static alt_u8 uart_send_byte(unsigned char data)
{
??? UART->TXDATA.BITS.TRANSMIT_DATA = data;
??? while(!UART->STATUS.BITS.TRDY);
??? return 0;
}
//發送字符串
static void uart_send_string(unsigned int len, unsigned char *str)
{
??? while(len--)
??? {
??????? uart_send_byte(*str++);
??? }
}
//設置波特率
static alt_u8 uart_set_baudrate(unsigned int baudrate)
{
???? //設置波特率:波特率 = 時鐘頻率/(divisor+1),轉換以后就是下面了
???? UART->DIVISOR.WORD = (unsigned int)(ALT_CPU_FREQ/baudrate + 0.5);
???? return 0;
}
//初始化程序
static alt_u8 uart_init(void)
{
??? //默認為115200bps
??? uart_set_baudrate(115200);
??? //對控制寄寄存器的irrdy進行置1,表示當接收準備好后,中斷使能
??? UART->CONTROL.BITS.IRRDY = 1;
??? //清除狀態寄存器,這是處理整個寄存器的方法,大家注意
??? UART->STATUS.WORD = 0;
??? //注冊uart中斷,ISR為uart_ISR
??? #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT??? //nios2 91 edition or later
??? alt_ic_isr_register
??? (
??????? UART_IRQ_INTERRUPT_CONTROLLER_ID,??????? // 中斷控制器標號,從system.h復制
??????? UART_IRQ,???????????????????????????????? // 硬件中斷號,從system.h復制
??????? uart_ISR,???????????????????????????????? // 中斷服務子函數
??????? 0,????????????????????????????????????? // 指向與設備驅動實例相關的數據結構體
??????? 0??????????????????????????????????????? // flags,保留未用
??? );
??? #else??????????????????????????????????????? //before nios2 91 edition
??? alt_irq_register
??? (
??????? UART_IRQ,??????????????????????????????? // 硬件中斷號,從system.h復制
??????? 0,??????????????????????????????????? // 指向與設備驅動實例相關的數據結構體
??????? uart_ISR??????????????????????????????? // 中斷服務子函數
??? );
??? #endif
??? return 0;
}
//串口中斷
#ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT
static void uart_ISR(void* context)
#else
static void uart_ISR(void* context, alt_u32 id)
#endif
{
??? //等徃狀態寄存器的接收數據狀態位rrdy,當rrdy位為1時,說明新接收癿值傳輸到了接收數據寄存器
??? while(!(UART->STATUS.BITS.RRDY));
??? //reveive_buffer為我們通過棧的方式在內存中開設內存塊,將接收數據寄存器中的數據放到這個內存塊中
??? uart.receive_buffer[uart.receive_count++] = UART->RXDATA.BITS.RECEIVE_DATA;
??? //當接收數據的最后一位為\n(回車符)時,進入if語句,也就是說,\n作為了結束標志
??? //符,每次發送數據后,要加一個回車符作為結束符
??? if(uart.receive_buffer[uart.receive_count - 1]=='\n')
??? {
??????? uart.receive_buffer[uart.receive_count] = '\0';
??????? uart_send_string(uart.receive_count, uart.receive_buffer);
??????? uart.receive_count = 0;
??????? uart.receive_flag = 1;
??? }
}
?
?
//--------------------------------------------------------------------------
/*
* sys_main.c
*
*? Created on: 2011-4-1
*????? Author: CrazyBingo
*????? irq, timer, DMA, uart
*/
#include <stdio.h>
#include "unistd.h"
#include "system.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "io.h"
#include "altera_avalon_pio_regs.h"
#include "../inc/my_sopc.h"
#include "../inc/key_scan.h"
#include "../inc/mcu_uart.h"
int main(void)
{
??? //------------------------------------------------------------
??? //uart test
??? unsigned char buffer[50] = "I will successfully one day!\n";
??? uart.init();
??? while(1)
??? {
??????? uart.send_string(sizeof(buffer), buffer);
??????? usleep(1000000);
??? }
??? return 0;
}
?
?
OK? Go? on, 謝謝
總結
以上是生活随笔為你收集整理的受教黑金文档,再度优化兼容irq uart代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 涨知识!seo优化中百度快照不更新的原因
- 下一篇: css怎样设置图片之间的间隔