FreeRTOS PL to PS Interrupt (ZYNQ Zedboard) - xilinx

I’m working with FreeRTOS on a ZYNQ (Zedboard).
I have the problem that my InterruptHandler does not get called after the “enable ap_done interrupt” of my ipcore gets high…
Does anyone have an idea whats wrong?
here my setup:
code:
int main( void )
{
prvSetupHardware();
xil_printf( "Hello from Freertos+TCP++ example main\r\n" );
xTaskCreate( prvIPCoreTask,
"IPCORE",
configMINIMAL_STACK_SIZE,
NULL,
tskIDLE_PRIORITY + 2,
&xIPCoreTask );
/* Start the RTOS scheduler. */
vTaskStartScheduler();
}
static void prvIPCoreTask( void *pvParameters )
{
XCounter_Config *counter_config = XCounter_LookupConfig(XPAR_COUNTER_0_DEVICE_ID);;
xStatus = XCounter_CfgInitialize(&xCounter, counter_config);
configASSERT( xStatus == XST_SUCCESS );
( void ) xStatus;
xPortInstallInterruptHandler(XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR, FreeRTOS_Counter_Handler,( void * ) &xCounter);
XScuGic_SetPriorityTriggerType( &xInterruptController, XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR, configMAX_API_CALL_INTERRUPT_PRIORITY << portPRIORITY_SHIFT, ucRisingEdge );
vPortEnableInterrupt(XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR);
XCounter_InterruptEnable(&xCounter, 0x1);
XCounter_InterruptGlobalEnable(&xCounter);
XCounter_Set_delay_time(&xCounter,1000);
xil_printf( "Before: %d\n", XCounter_InterruptGetStatus(&xCounter));
XCounter_Start(&xCounter);
for( ;; )
{
xil_printf( "after: %d\n", XCounter_InterruptGetStatus(&xCounter));
vTaskDelay( pdMS_TO_TICKS( DELAY_1_SECOND ) );;
}
}
void FreeRTOS_Counter_Handler( void *pvNotUsed )
{
xil_printf( "Counter Event\r\n" );
}
so I testet the ipcore in a simple standaloneproject and the interrupt ISR is also not called. whats wrong with my code?
void FreeRTOS_Counter_Handler( void *pvNotUsed )
{
int a = 1;
}
int main()
{
init_platform();
pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );
XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress );
XCounter_Config *counter_config = XCounter_LookupConfig(XPAR_COUNTER_0_DEVICE_ID);;
XCounter_CfgInitialize(&xCounter, counter_config);
XCounter_InterruptEnable(&xCounter, 0x1);
XCounter_InterruptGlobalEnable(&xCounter);
XScuGic_Connect( &xInterruptController, XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR, (Xil_ExceptionHandler) FreeRTOS_Counter_Handler, ( void * ) &xCounter );
XScuGic_SetPriorityTriggerType( &xInterruptController, XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR, 4, 0x3 );
XScuGic_Enable( &xInterruptController, XPAR_FABRIC_COUNTER_0_INTERRUPT_INTR );
XCounter_Set_delay_time(&xCounter,1000);
XCounter_Start(&xCounter);
print("Hello World\n\r");
print("Successfully ran Hello World application");
while(1){
}
cleanup_platform();
return 0;
}

Related

Access Lua coroutine from C

I have implemented a co-routine system. When I press ENTER to clear the first textbox, it calls contscript() which in turn calls lua_resume() but it doesn't continue the co-routine.
So what do I pass to lua_resume() to make the co-routine continue?
static lua_State *lua;
static int luapanic(lua_State *L)
{
allegro_exit();
const char *err = lua_tostring(L, -1);
DEBUGF("lua panic: %s\n", err);
printf("lua panic: %s\n", err);
return 0;
}
static int textbox(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
message(str);
return 1;
}
void contscript(void)
{
lua_resume(lua,NULL,0);
}
static int transfer_player(lua_State *L)
{
int x, y;
SpriteObj *p;
x = luaL_checkint(L, 1);
y = luaL_checkint(L, 2);
p = findobject(0);
setposition(p,x,y);
scrollToAndCentre(x,y);
return 1;
}
bool initscript(void)
{
lua = luaL_newstate();
lua_atpanic(lua, luapanic);
luaL_openlibs(lua);
lua_register(lua, "textbox", textbox);
lua_register(lua, "transfer_player", transfer_player);
return true;
}
Here is the script in question:
local co = coroutine.wrap(
function()
textbox("Dear me! I never knew birds could sing so\nbeautifully!")
coroutine.yield()
textbox("Text message #2")
end
)
co()

MQL4 How to call OnChartEvent() from init()

I want to call OnChartEvent() from init() like the code below, so the EA should process first the password and then the rest of the code.
I am just newbie not an expert in coding.
The idea or objective, the password must enter first and correctly, after successful then process the rest of code.
#include <ChartObjects/ChartObjectsTxtControls.mqh>
int init()
{
password_edit.Create(0, "password_edit", 0, 10, 10, 260, 25);
password_edit.BackColor(White);
password_edit.BorderColor(Black);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent( const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}
}
//+------------------------------------------------------------------+
int OnInit(){
passwordOperation();
return INIT_SUCCEED;
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
passwordOperation();
}
}
void passwordOperation()
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}

A core-dump when using lua_yield and lua_resume

I just want to resume the func coroutine twice, yield if n==0, and return if n==1 , but it core dumps, what't wrong with it?
the "hello world" should always be left in LL's stack, I can't figure out what is wrong.
[liangdong#cq01-clientbe-code00.vm.baidu.com lua]$ ./main
func_top=1 top=hello world
first_top=1 top_string=hello world
Segmentation fault (core dumped)
[liangdong#cq01-clientbe-code00.vm.baidu.com lua]$ cat main.c
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int n = 0;
int func(lua_State *L) {
printf("func_top=%d top=%s\n", lua_gettop(L), lua_tostring(L, -1));
if (!n) {
++ n;
return lua_yield(L, 1);
} else {
return 1;
}
}
int main(int argc, char* const argv[]) {
lua_State *L = luaL_newstate();
/* init lua library */
lua_pushcfunction(L, luaopen_base);
if (lua_pcall(L, 0, 0, 0) != 0) {
return 1;
}
lua_pushcfunction(L, luaopen_package);
if (lua_pcall(L, 0, 0, 0 ) != 0) {
return 2;
}
/* create the coroutine */
lua_State *LL = lua_newthread(L);
lua_pushcfunction(LL, func);
lua_pushstring(LL, "hello world");
/* first time resume */
if (lua_resume(LL, 1) == LUA_YIELD) {
printf("first_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
/* twice resume */
if (lua_resume(LL, 1) == 0) {
printf("second_top=%d top_string=%s\n", lua_gettop(LL), lua_tostring(LL, -1));
}
}
lua_close(L);
return 0;
}
it core dumps in lua5.1, but works well in lua5.2 if change lua_resume(LL, 1) to lua_resume(LL, NULL, 1).
EDIT: I was actually totally wrong.
You cannot resume a C function.

Using videoGrabber with ofMesh in OpenFrameworks on iOS

First time diving into OF on iOS...exciting! As a first run I'm trying to port an app I made before into an iOS app. Its a pretty simple rutt etra-like effect on video coming in from the video camera. I have it working as a mac app, but I can't seem to get it displaying properly on my iPhone. The mesh is drawing, but I don't think I'm getting pixel values from my camera to vidPixels in order to change the color of my mesh. I'm basing this off of the videoGrabberExample in OF iOS 0072. I'm on a MacBook Pro, 10.7.5, running Xcode 4.5.2.
Can anyone give this a look and let me know if I'm doing something wrong? :) Thanks so much in advance.
Code:
testApp.mm
#include "testApp.h"
#include "ofGLUtils.h"
#include "ofGLRenderer.h"
//--------------------------------------------------------------
void testApp::setup(){
ofxiPhoneSetOrientation(OFXIPHONE_ORIENTATION_LANDSCAPE_RIGHT);
ofSetFrameRate(30);
grabber.initGrabber(480, 360);
yStep = 5;
xStep = 5;
// drawRuttEtra = false;
ofBackground(0, 0, 0);
}
//--------------------------------------------------------------
void testApp::update(){
//ofBackground(255,255,255);
grabber.update();
if(grabber.isFrameNew()){
vidPixels = grabber.getPixelsRef();
}
}
//--------------------------------------------------------------
void testApp::draw(){
glEnable(GL_DEPTH_TEST);
ofMesh mesh;
int rowCount = 0;
for (int y = 0; y<grabber.height; y+=yStep){
ofNoFill();
mesh.setMode(OF_PRIMITIVE_LINE_STRIP);
if (rowCount % 2 == 0) {
for (int x = 0; x < grabber.width; x += xStep){
ofColor curColor = vidPixels.getColor(x, y);
mesh.addColor(ofColor(curColor));
mesh.addVertex(ofVec3f(x,y, curColor.getBrightness() * 0.3));
}
} else {
for (int x = grabber.width-1; x >= 0; x -= xStep){
ofColor curColor = vidPixels.getColor(x, y);
mesh.addColor(ofColor(curColor));
mesh.addVertex(ofVec3f(x,y, curColor.getBrightness() * 0.3)); }
}
rowCount++;
}
mesh.draw();
// grabber.draw(0,0);
}
testApp.h
#pragma once
#include "ofMain.h"
#include "ofxiPhone.h"
#include "ofxiPhoneExtras.h"
class testApp : public ofxiPhoneApp{
public:
void setup();
void update();
void draw();
void exit();
void touchDown(ofTouchEventArgs & touch);
void touchMoved(ofTouchEventArgs & touch);
void touchUp(ofTouchEventArgs & touch);
void touchDoubleTap(ofTouchEventArgs & touch);
void touchCancelled(ofTouchEventArgs & touch);
void lostFocus();
void gotFocus();
void gotMemoryWarning();
void deviceOrientationChanged(int newOrientation);
ofVideoGrabber grabber;
ofTexture tex;
unsigned char * pix;
//rutt etra effect
int yStep;
int xStep;
bool drawRuttEtra;
ofPixels vidPixels;
};
main.mm
#include "ofMain.h"
#include "testApp.h"
int main(){
ofSetupOpenGL(1024,768, OF_FULLSCREEN); // <-------- setup the GL context
ofRunApp(new testApp);
}
I tried doing some debugging to see what might be happening.
I wanted to make sure ifFrameNew() works. Trying:
if(grabber.isFrameNew()){
cout<< "i'm grabbing new pixels!" << endl;
vidPixels = grabber.getPixelsRef();
}
prints "i'm grabbing new pixels!", so that if block is working.
In my double for loop, if I cout the value of vidPixels.getColor(x,y)
cout<<vidPixels.getColor(x,y) << endl;
I get all 255...which makes me think grabber.getPixelsRef() isn't working how I thought it should.
Any ideas?

A question of libevent example code: how is invoked?

I'm learning libev however the code is so hard to understand, so I choose to learn libevent first whose code is relatively clearer. But I encounter a problem when try the example (http://www.wangafu.net/~nickm/libevent-book/01_intro.html).
How is the code event_add(state->write_event, NULL) in do_read() make do_write() function invoked?
/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>
#include <event2/event.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MAX_LINE 16384
void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);
char
rot13_char(char c)
{
return c;
/* We don't want to use isalpha here; setting the locale would change
* which characters are considered alphabetical. */
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
return c + 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
return c - 13;
else
return c;
}
struct fd_state {
char buffer[MAX_LINE];
size_t buffer_used;
size_t n_written;
size_t write_upto;
struct event *read_event;
struct event *write_event;
};
struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
struct fd_state *state = malloc(sizeof(struct fd_state));
if (!state)
return NULL;
state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
if (!state->read_event) {
free(state);
return NULL;
}
state->write_event =
event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
if (!state->write_event) {
event_free(state->read_event);
free(state);
return NULL;
}
state->buffer_used = state->n_written = state->write_upto = 0;
assert(state->write_event);
return state;
}
void
free_fd_state(struct fd_state *state)
{
event_free(state->read_event);
event_free(state->write_event);
free(state);
}
void
do_read(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = arg;
char buf[1024];
int i;
ssize_t result;
while (1) {
assert(state->write_event);
result = recv(fd, buf, sizeof(buf), 0);
if (result <= 0)
break;
for (i=0; i < result; ++i) {
if (state->buffer_used < sizeof(state->buffer))
state->buffer[state->buffer_used++] = rot13_char(buf[i]);
if (buf[i] == '\n') {
assert(state->write_event);
**event_add(state->write_event, NULL);**
state->write_upto = state->buffer_used;
}
}
}
if (result == 0) {
free_fd_state(state);
} else if (result < 0) {
if (errno == EAGAIN) // XXXX use evutil macro
return;
perror("recv");
free_fd_state(state);
}
}
void
**do_write(evutil_socket_t fd, short events, void *arg)**
{
struct fd_state *state = arg;
while (state->n_written < state->write_upto) {
ssize_t result = send(fd, state->buffer + state->n_written,
state->write_upto - state->n_written, 0);
if (result < 0) {
if (errno == EAGAIN) // XXX use evutil macro
return;
free_fd_state(state);
return;
}
assert(result != 0);
state->n_written += result;
}
if (state->n_written == state->buffer_used)
state->n_written = state->write_upto = state->buffer_used = 1;
event_del(state->write_event);
}
void
do_accept(evutil_socket_t listener, short event, void *arg)
{
struct event_base *base = arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*)&ss, &slen);
if (fd < 0) { // XXXX eagain??
perror("accept");
} else if (fd > FD_SETSIZE) {
close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
} else {
struct fd_state *state;
evutil_make_socket_nonblocking(fd);
state = alloc_fd_state(base, fd);
assert(state); /*XXX err*/
assert(state->write_event);
event_add(state->read_event, NULL);
}
}
void
run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
struct event_base *base;
struct event *listener_event;
base = event_base_new();
if (!base)
return; /*XXXerr*/
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(40713);
listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
#ifndef WIN32
{
int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
#endif
if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
perror("bind");
return;
}
if (listen(listener, 16)<0) {
perror("listen");
return;
}
listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
/*XXX check it */
event_add(listener_event, NULL);
event_base_dispatch(base);
}
int
main(int c, char **v)
{
setvbuf(stdout, NULL, _IONBF, 0);
run();
return 0;
}
I'm not sure if I'm answering the same question you asked - I understand it as:
How does calling event_add(state->write_event, NULL) in do_read() lead to do_write() being invoked?
The key to figuring this out is understanding what the do_read() function is actually doing. do_read() is a callback function associated with a socket which has data to be read: this is set up with allocate_fd_state():
struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
/*
* Allocate a new fd_state structure, which will hold our read and write events
* /
struct fd_state *state = malloc(sizeof(struct fd_state));
[...]
/*
* Initialize a read event on the given file descriptor: associate the event with
* the given base, and set up the do_read callback to be invoked whenever
* data is available to be read on the file descriptor.
* /
state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
[...]
/*
* Set up another event on the same file descriptor and base, which invoked the
* do_write callback anytime the file descriptor is ready to be written to.
*/
state->write_event =
event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
[...]
return state;
}
At this point, though, neither of these events have been event_add()'ed to the event_base base. The instructions for what to do are all written out, but no one is looking at them. So how does anything get read? state->read_event is event_add()'ed to the base after an incoming connection is made. Look at do_accept():
void
do_accept(evutil_socket_t listener, short event, void *arg)
{
[ ... accept a new connection and give it a file descriptor fd ... ]
/*
* If the file descriptor is invalid, close it.
*/
if (fd < 0) { // XXXX eagain??
perror("accept");
} else if (fd > FD_SETSIZE) {
close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
/*
* Otherwise, if the connection was successfully accepted...
*/
} else {
[ ... allocate a new fd_state structure, and make the file descriptor non-blocking ...]
/*
* Here's where the magic happens. The read_event created back in alloc_fd_state()
* is finally added to the base associated with it.
*/
event_add(state->read_event, NULL);
}
}
So right after accepting a new connection, the program tells libevent to wait until there's data available on the connection, and then run the do_read() callback. At this point, it's still impossible for do_write() to be called. It needs to be event_add()'ed. This happens in do_read():
void
do_read(evutil_socket_t fd, short events, void *arg)
{
/* Create a temporary buffer to receive some data */
char buf[1024];
while (1) {
[ ... Receive the data, copying it into buf ... ]
[ ... if there is no more data to receive, or there was an error, exit this loop... ]
[ ... else, result = number of bytes received ... ]
for (i=0; i < result; ++i) {
[ ... if there's room in the buffer, copy in the rot13() encoded
version of the received data ... ]
/*
* Boom, headshot. If we've reached the end of the incoming data
* (assumed to be a newline), then ...
*/
if (buf[i] == '\n') {
[...]
/*
* Have libevent start monitoring the write_event, which calls do_write
* as soon as the file descriptor is ready to be written to.
*/
event_add(state->write_event, NULL);
[...]
}
}
}
[...]
}
So, after reading in some data from a file descriptor, the program starts waiting until
the file descriptor is ready to be written to, and then invokes do_write(). Program
flow looks like this:
[ set up an event_base and start waiting for events ]
[ if someone tries to connect ]
[ accept the connection ]
[ ... wait until there is data to read on the connection ... ]
[ read in data from the connection until there is no more left ]
[ ....wait until the connection is ready to be written to ... ]
[ write out our rot13() encoded response ]
I hope that a) that was the correct interpretation of your question, and b) this was a helpful answer.

Resources