I'm editing USB audio SW for XUF208 procesor, I have disabled SPDIF as I don't need it and I'm using spared resources for my own user task. I've connected OLED display, rotary encoder, buttons and IR remote receiver.
Most things are fine, but I have problems with events. As I'm used to using interrupts on other platforms, I used events the same way, obviously not correct way, as it's taking whole core just to do IR receiver - if I put anything into the IR task loop that takes more than about 1ms, it just messes up the timing. It seems that its not working as an event, but its just pooling the input port for a change. Can you guys please have a look at my code what im doing wrong?
Code: Select all
#include <timer.h>
#include <platform.h>
#include <safestring.h>
#include <print.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <print.h>
#include "xc_ptr.h"
#include "gpio_access.h"
#include "SSD1331.h"
#include "gfx.h"
#include "audio.h"
UG_GUI gui;
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
in port sensor = XS1_PORT_1B;
in port p_enc = XS1_PORT_1L;
in port p_dir = XS1_PORT_4E;
void wait_us_task(int microseconds)
{
timer t;
unsigned time;
t :> time;
t when timerafter(time + (microseconds * 100)) :> void;
}
void audiohw_task(void)
{
char str[15];
SSD1331_init();
UG_Init(&gui, 96, 64);
UG_SelectGUI(&gui);
UG_FillScreen(0x00);
UG_FontSelect(FONT_6X10);
uint8_t debug = 0;
uint32_t low_pulse = 0;
uint8_t ir_codes[3] = {0,0,0};
uint32_t fresh_codes = 0;
uint32_t last_edge = 0;
uint32_t buffer = 0;
uint32_t buffer_count = 0;
uint32_t can_repeat = 0;
uint32_t preamble = 0;
uint32_t idle_max = 120*1000*100;
timer tick;
uint32_t next_tick;
tick :> next_tick;
set_port_inv(sensor);
while(1)
{
//wait_us_task(1000); //this messes up timing
select {
case tick when timerafter(next_tick) :> void:
// timeout, clean up
next_tick += idle_max;
can_repeat = buffer_count = preamble = 0;
break;
case sensor when pinsneq(low_pulse) :> low_pulse:
// sensor signal changed state
uint32_t now;
tick :> now;
next_tick = now + idle_max;
uint32_t width = (now - last_edge)/100;
last_edge = now;
if (!low_pulse)
{ // look for preamble mark
if ((7200<width) && (10800>width))
{
//sprintf(str, "width: %d", width);
//UG_PutString(10, 30, C_RED, (char*) str);
preamble = 1;
buffer_count = 0;
}
}
else switch (preamble)
{
case 1: // look for preamble space, or repeat code
if ((3600<width) && (5400>width))
{
can_repeat = 0;
preamble = 2;
break;
}
else if ((1800<width) && (2700>width) && can_repeat)
{
fresh_codes = 1;
//remote.codeReady();
}
preamble = 0;
break;
case 2: // look for 32 data bits
buffer >>= 1;
if ((1350<width) && (2025>width))
{
buffer |= 0x80000000;
}
else if ((450>=width) || (675<=width))
{
preamble = 0;
break;
}
if (32 <= ++buffer_count)
{
ir_codes[1] = buffer; // low address
buffer>>=8;
ir_codes[2] = buffer; // high address
buffer>>=8;
ir_codes[0] = buffer; // ir code
buffer>>=8;
uint8_t check = ~buffer;
// accept any address at the driver level
can_repeat = fresh_codes = (check==ir_codes[0]);
if (fresh_codes)
{
sprintf(str, "h%x h%x h%x\n", ir_codes[1], ir_codes[2], ir_codes[0]);
UG_PutString(10, 30, C_RED, (char*) str);
}
preamble = 0;
}
break;
}
break;
}
}
}
Cheers.