⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 RT-Thread是发展中的下一代微内核嵌入式实时操作系统
💻 C
字号:
/*
 * File      : main.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://openlab.rt-thread.com/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-09-23     Vai      first version (For ARM7TDMI core)
 */

#include "arm7.h"
#include "spi.h"
#include <fifo.h>

#define _MaxRetry 5
#define _MaxRange 30

static signed int xscale, yscale;
static signed int xoffset, yoffset;

static void nds_fifo_send(unsigned int command)
{
	while (NDS_REG_IPCFIFOCNT & FIFO_SEND_FULL)
		; /* do nothing */
	NDS_REG_IPCFIFOSEND = command;
}


/* sends touch state to ARM9 */
static void sendTouchState(unsigned short buttons)
{
    signed short x, y, px, py;
    static unsigned char lastx = 0, lasty = 0;

    if (buttons & TOUCH_RELEASED) 
    {
        if (lasty != 255)
            nds_fifo_send(FIFO_TOUCH);
        lastx = 255;
        lasty = 255;
    } 
    else 
    {
        /* Some dude is smacking his fingerprint on the touchscreen. */

        /* Code from devkitpro/libnds/source/arm7/touch.c */
        x = touch_read_value(TSC_MEASURE_X, _MaxRetry, _MaxRange);
        y = touch_read_value(TSC_MEASURE_Y, _MaxRetry, _MaxRange);

        px = ( x * xscale - xoffset + xscale/2 ) >>19;
        py = ( y * yscale - yoffset + yscale/2 ) >>19;

        if ( px < 0) px = 0;
        if ( py < 0) py = 0;
        if ( px > (256 -1)) px = 256 -1;
        if ( py > (192 -1)) py = 192 -1;

        x = px;
        y = py;

        if (lastx + 6 > x && lastx < x + 6 &&
            lasty + 6 > y && lasty < y + 6) 
        {
            nds_fifo_send(FIFO_TOUCH | 1 << 16 | x << 8 | y);
        }
        
        lastx = x;
        lasty = y;
    }

    return;
}

void InterruptHandler(void)
{
    unsigned short buttons;
    static unsigned short oldbuttons = 0x43;
    unsigned int wif;

    wif = NDS_IF;

    if (wif & IRQ_RECV) 
    {
        /* Acknowledge Interrupt */
        NDS_IF = IRQ_RECV;
        wif &= ~IRQ_RECV;

//        recieveFIFOCommand();
    }

    if (wif & IRQ_VBLANK) 
    {
        /* read X and Y, lid and touchscreen buttons */
        buttons = XKEYS;

        /* send button state to ARM9 */
        if (buttons != oldbuttons) 
        {
            nds_fifo_send(FIFO_BUTTONS | buttons);
            oldbuttons = buttons;
        }

        sendTouchState(buttons);

        /* clear FIFO errors (just in case) */
        if (NDS_REG_IPCFIFOCNT & (1 << 14))
            NDS_REG_IPCFIFOCNT |= (1 << 15) | (1 << 14);

        /* Acknowledge Interrupt */
        NDS_IF = IRQ_VBLANK;
        wif &= ~IRQ_VBLANK;
    }

    if (wif & IRQ_TIMER0) 
    {
        /* Acknowledge Interrupt */
        NDS_IF = IRQ_TIMER0;
        wif &= ~IRQ_TIMER0;
//        wifi_timer_handler();
    }

    if (wif & IRQ_ARM9)
    {
        /* Acknowledge Interrupt */
        NDS_IF = IRQ_ARM9;
        wif &= ~IRQ_ARM9;
    }

    if ((wif & IRQ_WIFI) || WIFI_IF)  
    {
        NDS_IF = IRQ_WIFI;
        wif &= ~IRQ_WIFI;

//        wifi_interrupt();
    }

    /* Acknowledge unhandled Interrupts */
    NDS_IF = wif;

    return;
}


int main(void)
{
    /* Disable Interrupts */
    NDS_IME = 0;

    /* Read calibration values, the arm9 will probably overwrite the originals later */
    // Code from devkitpro/libnds/source/arm7/touch.c
    xscale = ((PersonalData->calX2px - PersonalData->calX1px) << 19) / ((PersonalData->calX2) - (PersonalData->calX1));
    yscale = ((PersonalData->calY2px - PersonalData->calY1px) << 19) / ((PersonalData->calY2) - (PersonalData->calY1));

    xoffset = ((PersonalData->calX1 + PersonalData->calX2) * xscale  - ((PersonalData->calX1px + PersonalData->calX2px) << 19) ) / 2;
    yoffset = ((PersonalData->calY1 + PersonalData->calY2) * yscale  - ((PersonalData->calY1px + PersonalData->calY2px) << 19) ) / 2;

    /* Dummy read to enable the touchpad PENIRQ */
    touch_read_value(TSC_MEASURE_X, _MaxRetry, _MaxRange);

    /* Enable VBLANK Interrupt */
    DISP_SR = DISP_VBLANK_IRQ;

    /* Set the INT enable flags */
    NDS_IE = IRQ_VBLANK | IRQ_RECV | IRQ_ARM9 | IRQ_WIFI;

    /* Set the INT bitmask for the swi wait call */
    VBLANK_INTR_WAIT_FLAGS = IRQ_VBLANK;

    /* Enable FIFO */
    NDS_REG_IPCFIFOCNT = FIFO_ENABLE | FIFO_IRQ_ENABLE | FIFO_CLEAR | FIFO_ERROR ;

    /* Set interrupt handler */
    IRQ_HANDLER = (unsigned int) &InterruptHandler;

    /* init the wifi stuff */
    //wifi_init();

    /* Enable Interrupts */
    NDS_IF = ~0;
    NDS_IME = 1;
  
    /* power down ARM7, until the next activated interrupt */
    while (1)
    {
        swiWaitForVBlank();
    }

    /* Should never reach here */
    return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -