day7
1.中断
硬件中断:直接让外部的硬件产生中断,CPU获取中断源并执行异常处理流程
1.需求:(中断的原理一样,但外设是按键)按键产生中断,并在中断处理中串口发送消息
2.原理图:
UART_RING --> key2 --> 按键按下会有电平变化
GPX1_1 --> 功能复用为 XEINT9
3,芯片手册:
GPIO:
GPX1_1: 0x11000C20 [7:4] 0xF = EXT_INT41[1]
EXT_INT41_CON[1] 0x11000E04 [6:4] 0x2 = Triggers Falling edge
EXT_INT41_MASK 0x11000F04 [1] 0x0 = Enables Interrupt
EXT_INT41_PEND 0x11000F44 [1] 0x1 = Interrupt Occurs ?
GIC中断管理:
中断ID号: 25(只包含SPI)–>57(包含SGI 和PPI)
The Distributor provides a programming interface for:
分发器做的事情
1. Enabling the forwarding of interrupts to the CPU interfaces globally.
使能到 CPU interface的功能
ICDDCR 0x10490000 [0] 1 = GIC monitors the peripheral interrupt signals and forwards pending interrupts to the CPU interfaces.
2. Enabling or disabling each interrupt.
使能某一个中断
ICDISER1 0x10490104 [25] 1 = Enables the corresponding interrupt.
3. Setting the priority level of each interrupt.
设置每一个中断的优先级
ICDIPR14 0x10490438 [15:8] 0x1 0~255
4. Setting the target processor list of each interrupt.
设置要去哪个处理器
ICDIPTR14 0x10490838 [15:8] 0x1 =CPU interface 0
5. Setting each peripheral interrupt to be level-sensitive or edge-triggered.
设置外设中断怎么触发 //寄存器只读,所以不用
6. Setting each interrupt as either secure or Non-secure if the GIC implements the Security Extensions.
安全模式 //不用
7. Sending an SGI to one or more target processors.
设置软件中断 //不用
清除中断标志:
ICDICPR1:0x10490284 [25] 1=For SPIs, the corresponding
interrupt is pending on at least one processor.
CPU interface:
Each CPU interface block provides:
Interface for a processor that operates with the GIC.
Programming interface for:
CPU interface做的事情:
1. Enabling the signaling of interrupt requests by the CPU interface.
使能CPU interface通道,让interface能够与CPU链接
ICCICR_CPU0 0x10480000 [0] 1 = Enables signaling of interrupts
2. Acknowledging an interrupt.
应答中断(中断处理前)
ICCIAR_CPUn 0x1048000C [9:0] return interrupt ID(保存应答的ID)
3. Indicating completion of the processing of an interrupt.
应答中断(中断处理后)
ICCEOIR_CPUn 0x10480010 [9:0] (告知CPU保存的ID这个中断执行结束)
4. Setting an interrupt priority mask for the processor.
设置优先级掩码
ICCPMR_CPUn 0x10480004 [7:0] 0~255
5. Defining the preemption policy for the processor.
定义抢占策略 //不用
6. Determining the highest priority pending interrupt for the processor.
//不用
/*===============================================
* 文件名称:main1.h
* 创 建 者: memories
* 创建日期:2023年06月28日
* 描 述:have a nice day
================================================*/
#ifndef __main_H_
#define __main_H_
#define GPA1CON *(volatile unsigned int *)0x11400020
#define ULCON2 *(volatile unsigned int *)0x13820000
#define UCON2 *(volatile unsigned int *)0x13820004
#define UTRSTAT2 *(volatile unsigned int *)0x13820010
#define UTXH2 *(volatile unsigned int *)0x13820020
#define URXH2 *(volatile unsigned int *)0x13820024
#define UBRDIV2 *(volatile unsigned int *)0x13820028
#define UFRACVAL2 *(volatile unsigned int *)0x1382002C
#define GPX1_1 *(volatile unsigned int *)0x11000C20
#define EXT_INT41_CON *(volatile unsigned int *)0x11000E04
#define EXT_INT41_MASK *(volatile unsigned int *)0x11000F04
#define EXT_INT41_PEND *(volatile unsigned int *)0x11000F44
#define ICDDCR *(volatile unsigned int *)0x10490000
#define ICDISER1 *(volatile unsigned int *)0x10490104
#define ICDIPR14 *(volatile unsigned int *)0x10490438
#define ICDIPTR14 *(volatile unsigned int *)0x10490838
#define ICDICPR1 *(volatile unsigned int *)0x10490284
#define ICCICR_CPU0 *(volatile unsigned int *)0x10480000
#define ICCIAR_CPUn *(volatile unsigned int *)0x1048000C
#define ICCEOIR_CPUn *(volatile unsigned int *)0x10480010
#define ICCPMR_CPUn *(volatile unsigned int *)0x10480004
void GIC_init();
void UART_init();
void interrupt_init();
void send_str();
#endif
/*===============================================
* 文件名称:main1.c
* 创 建 者: memories
* 创建日期:2023年06月28日
* 描 述:have a nice day
================================================*/
#include "main1.h"
int main(int argc, char *argv[])
{
UART_init();
GIC_init();
interrupt_init();
while(1);
}
void UART_init()
{
//设置波特率
UBRDIV2 = 53;
UFRACVAL2 = 4;
//设置串口的工作模式
GPA1CON &= ~(0xf << 4);
GPA1CON |= (0x2 << 4);
//设置UART属性
ULCON2 = 0x3;
UCON2 &= ~(0x3 << 2);
UCON2 |= 1 << 2; //开启发送轮循模式
}
void GIC_init()
{
ICDDCR |= 0x1;
ICDISER1 |= 1 << 25;
ICDIPR14 &= ~(0xff << 8);
ICDIPR14 |= (0x1 << 8);
ICDIPTR14 &= ~(0xff << 8);
ICDIPTR14 |= (0x1 << 8);
ICCICR_CPU0 |= 0x1;
ICCPMR_CPUn = 255;
}
void interrupt_init()
{
GPX1_1 |= 0xf << 4;
EXT_INT41_CON &= ~(0x7 << 4);
EXT_INT41_CON |= 0x2 << 4;
EXT_INT41_MASK &= ~(1 << 1);
}
void do_irq()
{
int data = ICCIAR_CPUn & 0x3ff;
send_str();
EXT_INT41_PEND |= 1 << 1;
ICDICPR1 |= 1 << 25;
ICCEOIR_CPUn = data;
}
void send_str()
{
int i = 0;
char *p = "press\n\r";
while(p[i] != '\0')
{
while(! (UTRSTAT2 & 1<<2));
UTXH2 = p[i];
i++;
}
}
2.pwm
1.需求:蜂鸣器响起来
2.原理图:
MOTOR_PWM <--> GPD0_0
3.芯片手册
GPIO:
GPD0CON 0x114000A0 [3:0] 0x2 = TOUT_0 //PWM功能
PWM:
TCONn:可以使能
TCNTBn:给递减计数器赋初值,以及重新装载计数器
TCMPBn:比较值
Timer Input Clock Frequency = PCLK/({prescaler value + 1})/{divider value}
TCFG0:0x139D0000 [7:0] 1~255 Prescaler 0 value for timer 0 and 1
TCFG1: 0x139D0004 [3:0] 0010 = 1/4 div4分频
TCON: 0x139D0008 [3] 1 = Interval mode (auto-reload)
[2] 1 = TOUT_0 inverter-on?
[1] 1 = Updates TCNTB0 andTCMPB0
0 = No operation 先置一再置0
[0] 1 = Starts Timer 0
TCNTB0:0x139D000C [31:0] 自动装载值
TCMPB0:0x139D0010 [31:0] 比较值
/*===============================================
* 文件名称:main2.h
* 创 建 者: memories
* 创建日期:2023年06月28日
* 描 述:have a nice day
================================================*/
#ifndef __main2_H_
#define __main2_H_
#define GPD0CON *(volatile unsigned int *)0x114000A0
#define TCFG0 *(volatile unsigned int *)0x139D0000
#define TCFG1 *(volatile unsigned int *)0x139D0004
#define TCON *(volatile unsigned int *)0x139D0008
#define TCNTB0 *(volatile unsigned int *)0x139D000C
#define TCMPB0 *(volatile unsigned int *)0x139D0010
void pwm_init();
void pwm_on();
#endif
/*===============================================
* 文件名称:main2.c
* 创 建 者: memories
* 创建日期:2023年06月28日
* 描 述:have a nice day
================================================*/
#include "main2.h"
int main(int argc, char *argv[])
{
pwm_init();
pwm_on();
while(1);
}
void pwm_init()
{
GPD0CON &= ~(0xf);
GPD0CON |= (0x2);
TCFG0 |= 0xff;
TCFG1 &= ~(0xf);
TCFG1 |= (0x2);
TCNTB0 = 3000;
TCMPB0 = 1500;
TCON |= 1 << 3;
TCON |= 1 << 2;
TCON |= 1 << 1;
TCON &= ~(1 << 1);
}
void pwm_on()
{
TCON |= 1;
}