蜂鸟E203系列——按键中断设计

欲观原文,请君移步

原理图

LED 原理图如下图所示,连接GPIO19,GPIO21,GPIO22。

按键 BUTTON 原理图如下图所示,连接 BTN1 和 BTN2 。

image

物理连接如下图所示。

开发板的按键引脚 BTN1 和 BTN2 分别连接到 GPIO30 和 GPIO31 。

源码

开发板的按键引脚连接到 GPIO30 和 GPIO31 。

image

主程序如下:

#include <stdio.h>
#include <stdlib.h>
#include "platform.h"
#include <string.h>
#include "plic/plic_driver.h"
#include "encoding.h"
#include <unistd.h>
#include "stdatomic.h"


// Structures for registering different interrupt handlers
// for different parts of the application.
typedef void (*function_ptr_t) (void);

void no_interrupt_handler (void) {};

function_ptr_t g_ext_interrupt_handlers[PLIC_NUM_INTERRUPTS];


// Instance data for the PLIC.

plic_instance_t g_plic;

//处理外部中断
void handle_m_ext_interrupt(){
  plic_source int_num  = PLIC_claim_interrupt(&g_plic);
  if ((int_num >=1 ) && (int_num < PLIC_NUM_INTERRUPTS)) {
    g_ext_interrupt_handlers[int_num]();
  }
  else {
    exit(1 + (uintptr_t) int_num);
  }
  PLIC_complete_interrupt(&g_plic, int_num);
}

void button_1_handler(void) {

	  printf("open led");
  // Green LED On
	  GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << RED_LED_GPIO_OFFSET);
	  GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << GREEN_LED_GPIO_OFFSET);
	  GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << BLUE_LED_GPIO_OFFSET);

  // Clear the GPIO Pending interrupt by writing 1.
  GPIO_REG(GPIO_RISE_IP) = (0x1 << BUTTON_1_GPIO_OFFSET);

};

void button_2_handler(void) {

	printf("close  led");
	 GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << RED_LED_GPIO_OFFSET);

	 GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << GREEN_LED_GPIO_OFFSET);

	 GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << BLUE_LED_GPIO_OFFSET);

  GPIO_REG(GPIO_RISE_IP) = (0x1 << BUTTON_2_GPIO_OFFSET);

};
void register_plic_irqs (){

   /**************************************************************************
   * Set up the PLIC
   *
   *************************************************************************/
  PLIC_init(&g_plic,
	    PLIC_CTRL_ADDR,
	    PLIC_NUM_INTERRUPTS,
	    PLIC_NUM_PRIORITIES);


  for (int ii = 0; ii < PLIC_NUM_INTERRUPTS; ii ++){
    g_ext_interrupt_handlers[ii] = no_interrupt_handler;
  }

  g_ext_interrupt_handlers[PLIC_INT_DEVICE_BUTTON_1] = button_1_handler;
  g_ext_interrupt_handlers[PLIC_INT_DEVICE_BUTTON_2] = button_2_handler;


  // Have to enable the interrupt both at the GPIO level,
  // and at the PLIC level.
  PLIC_enable_interrupt (&g_plic, PLIC_INT_DEVICE_BUTTON_1);
  PLIC_enable_interrupt (&g_plic, PLIC_INT_DEVICE_BUTTON_2);

  // Priority must be set > 0 to trigger the interrupt.
  PLIC_set_priority(&g_plic, PLIC_INT_DEVICE_BUTTON_1, 1);
  PLIC_set_priority(&g_plic, PLIC_INT_DEVICE_BUTTON_2, 1);

 }

int main(int argc, char **argv)
{

  //关闭LED GPIO的输入使能
    GPIO_REG(GPIO_INPUT_EN)    &= ~((0x1<< RED_LED_GPIO_OFFSET) | (0x1<< GREEN_LED_GPIO_OFFSET) | (0x1 << BLUE_LED_GPIO_OFFSET)) ;

  //打开LED GPIO的输出使能:三个颜色的LED灯
  GPIO_REG(GPIO_OUTPUT_EN)   |=  ((0x1<< RED_LED_GPIO_OFFSET)| (0x1<< GREEN_LED_GPIO_OFFSET) | (0x1 << BLUE_LED_GPIO_OFFSET)) ;


  //设置LED GPIO的输出值
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~ (0x1 << RED_LED_GPIO_OFFSET) ;
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~ (0x1 << GREEN_LED_GPIO_OFFSET) ;
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~ (0x1 << BLUE_LED_GPIO_OFFSET) ;


  //关闭按键GPIO的输出使能
 GPIO_REG(GPIO_OUTPUT_EN)   &= ~((0x1<< BUTTON_1_GPIO_OFFSET) | (0x1<< BUTTON_2_GPIO_OFFSET)) ;
 //关闭按键GPIO的输入上拉
GPIO_REG(GPIO_PULLUP_EN)   &= ~((0x1<< BUTTON_1_GPIO_OFFSET) | (0x1<< BUTTON_2_GPIO_OFFSET)) ;
//打开GPIO的输入使能
  GPIO_REG(GPIO_INPUT_EN)    |= ((0x1<< BUTTON_1_GPIO_OFFSET) | (0x1<< BUTTON_2_GPIO_OFFSET)) ;
  //上升沿触发的中断来源
    GPIO_REG(GPIO_RISE_IE)    |= ((0x1<< BUTTON_1_GPIO_OFFSET) | (0x1<< BUTTON_2_GPIO_OFFSET)) ;


    printf("set gpio finish");
//关闭外部中断局部使能
  clear_csr(mie, MIP_MEIP);

//计时器初始化
  register_plic_irqs();



    // 打开外部中断局部使能
    set_csr(mie, MIP_MEIP);

    //打开中断的全局使能
    set_csr(mstatus, MSTATUS_MIE);
    printf("set interrupt gpio finish");
  return 0;

}

实验结果

按键 BTN1 会将 LED 灯关闭,按键 BTN2 会将 LED 灯打开。

RISC-V 按键中断设计实验结果

工程获取

posted @ 2020-07-20 10:18  瓜大三哥  阅读(400)  评论(0编辑  收藏  举报