The tutorial black gold documentation optimizes irq uart code compatibility again

Source: Internet
Author: User

It mainly modifies the function type and is compatible with normal and enhanced interruptions.

The main functions are as follows:

(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
{< br> // receiving register
Union
{< br> struct
{
> volatile unsigned long int receive_data: 8;
volatile unsigned long int NC: 24;
}bits;
volatile unsigned long int word;
}rxdata;

// Register sending register
Union
{
Struct
{
Volatile unsigned long int transmit_data: 8;
Volatile unsigned long int NC: 24;
} Bits;
Volatile unsigned long int word;
} Txdata;

// Memory register
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;
// Control register
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;
// Baud rate Divider
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

 

// initialize the UART struct
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
};

// Send a byte of data
// Put the sent data in the sending data buffer, and wait for the Status Register trdy to set 1. When trdy is set to 1, the receiving is complete.
Static alt_u8 uart_send_byte (unsigned char data)
{
UART-> txdata. Bits. transmit_data = data;
While (! UART-> status. Bits. trdy );
Return 0;
}

// Send a string
Static void uart_send_string (unsigned int Len, unsigned char * Str)
{
While (Len --)
{
Uart_send_byte (* STR ++ );
}
}

// Set the baud rate
Static alt_u8 uart_set_baudrate (unsigned int baudrate)
{
// Set the baud rate: baud rate = clock frequency/(divisor + 1). After the conversion
UART-> divisor. Word = (unsigned INT) (alt_cpu_freq/baudrate + 0.5 );
Return 0;
}

// InitializationProgram
Static alt_u8 uart_init (void)
{
// The default value is 115200bps.
Uart_set_baudrate (115200 );

// Set the irrdy of the control mail register to 1, indicating that when the receiving is ready, the enabling is interrupted.
UART-> control. Bits. irrdy = 1;

// Clear the status register. This is the method for processing the entire register.
UART-> status. Word = 0;

// UART registration interrupted. ISR is uart_isr
# Ifdef alt_enhanced_interrupt_api_present // nios2 91 edition or later
Alt_ic_isr_register
(
Uart_irq_interrupt_controller_id, // Number of the interrupt controller, copied from system. h
Uart_irq, // hardware interrupt number, copied from system. h
Uart_isr, // interrupt service sub-function
0, // point to the data structure related to the device driver instance
0 // flags, reserved for unused
);
# Else // before nios2 91 Edition
Alt_irq_register
(
Uart_irq, // hardware interrupt number, copied from system. h
0, // point to the data structure related to the device driver instance
Uart_isr // interrupt service subfunction
);
# Endif
Return 0;
}

// Serial port interruption
# Ifdef alt_enhanced_interrupt_api_present
Static void uart_isr (void * context)
# Else
Static void uart_isr (void * context, alt_u32 ID)
# Endif
{
// When the rrdy bit is 1, it indicates that the newly received rst value is transmitted to the received data register.
While (! (UART-> status. Bits. rrdy ));

// Reveive_buffer opens a memory block for us through the stack method in the memory, put the data in the received data register into this memory block
UART. receive_buffer [UART. receive_count ++] = UART-> rxdata. Bits. receive_data;

// When the last digit of the received data is \ n (carriage return), enter the if statement, that is, \ n serves as the end mark.
// Character. Each time the data is sent, a carriage return is added as the end character.
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, thank you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.