日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

受教黑金文档,再度优化兼容irq uart代码

發(fā)布時間:2023/12/31 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 受教黑金文档,再度优化兼容irq uart代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

主要修改了函數(shù)類型,兼容了普通和增強型中斷

主要函數(shù)如下:

(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結(jié)構(gòu)體
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
};

//發(fā)送一個字節(jié)數(shù)據(jù)
//將發(fā)送的數(shù)據(jù)放到發(fā)送數(shù)據(jù)緩沖區(qū)內(nèi),等待狀態(tài)寄存器 TRDY置 1,當(dāng) TRDY置 1,說明接收完畢
static alt_u8 uart_send_byte(unsigned char data)
{
??? UART->TXDATA.BITS.TRANSMIT_DATA = data;
??? while(!UART->STATUS.BITS.TRDY);
??? return 0;
}

//發(fā)送字符串
static void uart_send_string(unsigned int len, unsigned char *str)
{
??? while(len--)
??? {
??????? uart_send_byte(*str++);
??? }
}

//設(shè)置波特率
static alt_u8 uart_set_baudrate(unsigned int baudrate)
{
???? //設(shè)置波特率:波特率 = 時鐘頻率/(divisor+1),轉(zhuǎn)換以后就是下面了
???? 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,表示當(dāng)接收準(zhǔn)備好后,中斷使能
??? UART->CONTROL.BITS.IRRDY = 1;

??? //清除狀態(tài)寄存器,這是處理整個寄存器的方法,大家注意
??? 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,??????? // 中斷控制器標(biāo)號,從system.h復(fù)制
??????? UART_IRQ,???????????????????????????????? // 硬件中斷號,從system.h復(fù)制
??????? uart_ISR,???????????????????????????????? // 中斷服務(wù)子函數(shù)
??????? 0,????????????????????????????????????? // 指向與設(shè)備驅(qū)動實例相關(guān)的數(shù)據(jù)結(jié)構(gòu)體
??????? 0??????????????????????????????????????? // flags,保留未用
??? );
??? #else??????????????????????????????????????? //before nios2 91 edition
??? alt_irq_register
??? (
??????? UART_IRQ,??????????????????????????????? // 硬件中斷號,從system.h復(fù)制
??????? 0,??????????????????????????????????? // 指向與設(shè)備驅(qū)動實例相關(guān)的數(shù)據(jù)結(jié)構(gòu)體
??????? uart_ISR??????????????????????????????? // 中斷服務(wù)子函數(shù)
??? );
??? #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
{
??? //等徃狀態(tài)寄存器的接收數(shù)據(jù)狀態(tài)位rrdy,當(dāng)rrdy位為1時,說明新接收癿值傳輸?shù)搅私邮諗?shù)據(jù)寄存器
??? while(!(UART->STATUS.BITS.RRDY));

??? //reveive_buffer為我們通過棧的方式在內(nèi)存中開設(shè)內(nèi)存塊,將接收數(shù)據(jù)寄存器中的數(shù)據(jù)放到這個內(nèi)存塊中
??? uart.receive_buffer[uart.receive_count++] = UART->RXDATA.BITS.RECEIVE_DATA;

??? //當(dāng)接收數(shù)據(jù)的最后一位為\n(回車符)時,進入if語句,也就是說,\n作為了結(jié)束標(biāo)志
??? //符,每次發(fā)送數(shù)據(jù)后,要加一個回車符作為結(jié)束符
??? 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, 謝謝

總結(jié)

以上是生活随笔為你收集整理的受教黑金文档,再度优化兼容irq uart代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。