how to code something このページをアンテナに追加 RSSフィード

2018-04-20

2018-04-19

PYNQ Arduino IOPでタイマー割込み(その3)

http://d.hatena.ne.jp/seinzumtode/20180416/1523849621 のコードは引数が間違っていたので修正した

#include <stdio.h>
#include "xil_types.h"
#include "xtmrctr.h"
#include "xparameters.h"
#include "xil_io.h"
#include "xil_exception.h"
#include "xintc.h"
#include "xgpio.h"

void Timer_InterruptHandler(void *data, u8 TmrCtrNumber)
{
	print(" Interrupt acknowledged \n \r ");
}

int main()
{
	XIntc IntcInstancePtr;
	XTmrCtr TimerInstancePtr;
	int xStatus;
	print("##### Application Starts #####\n\r");
	print("\r\n");
	xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_TMRCTR_1_DEVICE_ID);
	if(XST_SUCCESS != xStatus)
		print("TIMER INIT FAILED \n\r");

	XTmrCtr_SetHandler(&TimerInstancePtr,Timer_InterruptHandler,&TimerInstancePtr);
	XTmrCtr_SetResetValue(&TimerInstancePtr,0, 0xf8000000);
	XTmrCtr_SetOptions(&TimerInstancePtr,0,(XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION ));

	xStatus = XIntc_Initialize(&IntcInstancePtr, XPAR_INTC_0_DEVICE_ID);
	if (xStatus != XST_SUCCESS){
		print("intc init error\n\r");
		return xStatus;
	}
	xStatus = XIntc_Connect(&IntcInstancePtr, XPAR_INTC_0_TMRCTR_1_VEC_ID,
			(XInterruptHandler)XTmrCtr_InterruptHandler,
			(void*)&TimerInstancePtr);
	if (xStatus != XST_SUCCESS){
		print("connect error\n\r");
		return xStatus;
	}
	 xStatus = XIntc_Start(&IntcInstancePtr, XIN_REAL_MODE);
	 if (xStatus != XST_SUCCESS){
		 print("intc start error\n\r");
		 return xStatus;
	 }
	 XIntc_Enable(&IntcInstancePtr, XPAR_INTC_0_TMRCTR_1_VEC_ID);
	 microblaze_enable_interrupts();

	XTmrCtr_Start(&TimerInstancePtr,0);
	print("timer start \n\r");

	//Wait For interrupt;
	print("Wait for the Timer interrupt to tigger \r\n");
	print("########################################\r\n");
	print(" \r\n");

	while(1)
	{
	}
	cleanup_platform();
	return 0;
}

2018-04-18

PYNQのMicroBlazeでC++を使う

基本的なところでスタックした
以下にあるように、CでビルドしたライブラリC++ビルドしたライブラリ名前のマングリングが異なっている。
http://kaisk.hatenadiary.com/entry/2014/06/23/172411
Cのライブラリのヘッダを以下のように変えることでC++から呼べるようになる

#ifdef __cplusplus
extern "C" {
#endif
(見つけた関数宣言)
#ifdef __cplusplus
}
#endif

具体的にはi2c.hを以下のように変更した
PYNQ/pynq/lib/arduino/bsp_iop_arduino/iop_arduino_mb/include/i2c.h

#ifndef _I2C_H_
#define _I2C_H_

#include <xparameters.h>

#ifdef XPAR_XIIC_NUM_INSTANCES

/*
 * IIC API
 */
typedef int i2c;

#ifdef __cplusplus
extern "C" {
#endif
i2c i2c_open_device(unsigned int device);
i2c i2c_open(unsigned int sda, unsigned int scl);
void i2c_read(i2c dev_id, unsigned int slave_address,
              unsigned char* buffer, unsigned int length);
void i2c_write(i2c dev_id, unsigned int slave_address,
               unsigned char* buffer, unsigned int length);
void i2c_close(i2c dev_id);
unsigned int i2c_get_num_devices(void);
#ifdef __cplusplus
}
#endif

#endif
#endif  // _I2C_H_

2018-04-16

PYNQ Arduino IOPでタイマー割込み(その2)

【追記】以下のコードは複数タイマで動かない→修正した( http://d.hatena.ne.jp/seinzumtode/20180419/1524107851
インスタンス名をARM Cortex A-9コアでやったとき(= http://d.hatena.ne.jp/seinzumtode/20171011/1507704033 )と合わせてみた

#include <stdio.h>
#include "xil_types.h"
#include "xtmrctr.h"
#include "xparameters.h"
#include "xil_io.h"
#include "xil_exception.h"
#include "xintc.h"
#include "xgpio.h"

void Timer_InterruptHandler(void *data, u8 TmrCtrNumber)
{
	print(" Interrupt acknowledged \n \r ");
}

int main()
{
	XIntc IntcInstancePtr;
	XTmrCtr TimerInstancePtr;
	int xStatus;
	print("##### Application Starts #####\n\r");
	print("\r\n");
	xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_TMRCTR_0_DEVICE_ID);
	if(XST_SUCCESS != xStatus)
		print("TIMER INIT FAILED \n\r");

	XTmrCtr_SetHandler(&TimerInstancePtr,Timer_InterruptHandler,&TimerInstancePtr);
	XTmrCtr_SetResetValue(&TimerInstancePtr,0, 0xf8000000);
	XTmrCtr_SetOptions(&TimerInstancePtr,XPAR_TMRCTR_0_DEVICE_ID,(XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION ));

	xStatus = XIntc_Initialize(&IntcInstancePtr, XPAR_INTC_0_DEVICE_ID);
	if (xStatus != XST_SUCCESS){
		print("intc init error\n\r");
		return xStatus;
	}
	xStatus = XIntc_Connect(&IntcInstancePtr, XPAR_TMRCTR_0_DEVICE_ID,
			(XInterruptHandler)XTmrCtr_InterruptHandler,
			(void*)&TimerInstancePtr);
	if (xStatus != XST_SUCCESS){
		print("connect error\n\r");
		return xStatus;
	}
	 xStatus = XIntc_Start(&IntcInstancePtr, XIN_REAL_MODE);
	 if (xStatus != XST_SUCCESS){
		 print("intc start error\n\r");
		 return xStatus;
	 }
	 XIntc_Enable(&IntcInstancePtr, XPAR_INTC_0_DEVICE_ID);
	 microblaze_enable_interrupts();

	XTmrCtr_Start(&TimerInstancePtr,0);
	print("timer start \n\r");

	//Wait For interrupt;
	print("Wait for the Timer interrupt to tigger \r\n");
	print("########################################\r\n");
	print(" \r\n");

	while(1)
	{
	}
	cleanup_platform();
	return 0;
}

2018-04-11

PYNQ Arduino IOPでタイマー割り込み

ここを参考に
http://e-trees.jp/wp/2015/10/14/post-316/

PYNQ Arduino IOPにはタイマーが複数(0~5までの6個)あるので
今回はタイマー0を使ってみた
main.c

#include <stdio.h>
#include "platform.h"

#include "xtmrctr.h"
#include "xintc.h"
#include "xparameters.h"
#include "xbasic_types.h"

//void myprint(char *str);

void timer_int_handler();

void timer_int_handler()
{
        volatile static int cnt;
        volatile unsigned int csr;
        csr = XTmrCtr_GetControlStatusReg(XPAR_IOP_ARDUINO_TIMERS_SUBSYSTEM_TIMER_0_BASEADDR, 0);
        xil_printf("Interrupt timer!! %d\r\n", cnt++);
        XTmrCtr_SetControlStatusReg(XPAR_IOP_ARDUINO_TIMERS_SUBSYSTEM_TIMER_0_BASEADDR, 0, csr);
}

XTmrCtr tmr;
XIntc intc;

int main()
{
    init_platform();

    print("Hello World\n\r");

    XStatus status;

    status = XIntc_Initialize(&intc, XPAR_INTC_0_DEVICE_ID);
    if (status != XST_SUCCESS){print("intc init error\n\r"); return status;}
    status = XTmrCtr_Initialize(&tmr, XPAR_IOP_ARDUINO_TIMERS_SUBSYSTEM_TIMER_0_DEVICE_ID);
    if (status != XST_SUCCESS){print("timer init error\n\r"); return status;}

    status = XIntc_Connect(&intc, XPAR_INTC_0_TMRCTR_0_VEC_ID, (XInterruptHandler)XTmrCtr_InterruptHandler, (void*)&tmr);
    if (status != XST_SUCCESS){print("connect error\n\r"); return status;}
    status = XIntc_Start(&intc, XIN_REAL_MODE);
    if (status != XST_SUCCESS){print("intc start error\n\r"); return status;}
    XIntc_Enable(&intc, XPAR_INTC_0_TMRCTR_0_VEC_ID);

    XTmrCtr_SetHandler(&tmr, (void*)timer_int_handler, (void*)0);

    microblaze_enable_interrupts();
    print("setup finished\n\r");

    // see LogiCORE IP AXI Timer Product Guide, PG079
    XTmrCtr_SetOptions(&tmr, 0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION);
    XTmrCtr_SetResetValue(&tmr, 0, 66666667);
    XTmrCtr_Start(&tmr, 0);

    print("wait for interrupts\n\r");

    for(;;){}

    return 0;
}