Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display: Interrupt driven ADC sampling for the touch screen #3

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions display/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ COMMONDEPS = src/config.h
default: bin/firmware.hex

inc/lpc111x.h:
ln -s ../$(CODEBASEDIR)/lpc111x.h $@
ln -sf ../$(CODEBASEDIR)/lpc111x.h $@

inc/sysdefs.h:
ln -s ../$(CODEBASEDIR)/sysdefs.h $@
ln -sf ../$(CODEBASEDIR)/sysdefs.h $@

inc/projectconfig.h:
ln -s ../src/config.h $@
ln -sf ../src/config.h $@

src/generated_font_data.inc: $(GENFONT)
$(GENFONT) --no-flip \
Expand Down
1 change: 1 addition & 0 deletions display/src/comm_i2c.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void I2C_IRQHandler()
backbuffer_ready = true;
}
NVIC_SetPendingIRQ(received_irqn);
__attribute__((fallthrough));
}
case MSG_ADDRESS_HOST:
{
Expand Down
7 changes: 4 additions & 3 deletions display/src/comm_uart.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ static struct msg_header_t ping_header VAR_RAM =
MSG_ADDRESS_HOST,
0,
MSG_FLAG_ACK | MSG_FLAG_ECHO);
static volatile uint32_t buffer = 0;

static inline void uart_init(const uint32_t baudrate)
{
NVIC_DisableIRQ(UART_IRQn);

buffer = 0xdeadbeef;

/* Set 1.6 UART RXD */
IOCON_PIO1_6 &= ~IOCON_PIO1_6_FUNC_MASK;
IOCON_PIO1_6 |= IOCON_PIO1_6_FUNC_UART_RXD;
Expand Down Expand Up @@ -141,6 +138,7 @@ void uart_tx_irq()
uart.state.trns_src = (const volatile uint8_t*)uart.queue.items[uart.queue.active_item].header;
uart.state.trns_end = uart.state.trns_src + sizeof(struct msg_header_t);
}
__attribute__((fallthrough));
}
case TXU_SEND_HEADER:
{
Expand Down Expand Up @@ -298,6 +296,7 @@ void uart_rx_irq()
// missing break is intentional: receive the first bytes immediately
// turn the timer on
TMR_COMM_TIMEOUT_TCR = (1<<0) | (0<<1);
__attribute__((fallthrough));
}
case RXU_RECEIVE_HEADER:
{
Expand Down Expand Up @@ -376,6 +375,7 @@ void uart_rx_irq()
uart.state.recv_dest = &uart.state.dest_msg->msg.data[0];
uart.state.recv_end = uart.state.recv_dest + HDR_GET_PAYLOAD_LENGTH(uart.state.curr_header);
// we can smoothly continue here if more data is available
__attribute__((fallthrough));
}
case RXU_RECEIVE_PAYLOAD:
{
Expand All @@ -385,6 +385,7 @@ void uart_rx_irq()
uart_rx_state = RXU_RECEIVE_CHECKSUM;
uart.state.recv_dest = (uint8_t*)&uart.state.dest_msg->msg.checksum;
uart.state.recv_end = uart.state.recv_dest + sizeof(msg_checksum_t);
__attribute__((fallthrough));
}
case RXU_RECEIVE_CHECKSUM:
{
Expand Down
12 changes: 6 additions & 6 deletions display/src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "lcd.h"

inline void clamp_x(coord_int_t *x)
static inline void clamp_x(coord_int_t *x)
{
if (*x < 0) {
*x = 0;
Expand All @@ -11,7 +11,7 @@ inline void clamp_x(coord_int_t *x)
}
}

inline void clamp_y(coord_int_t *y)
static inline void clamp_y(coord_int_t *y)
{
if (*y < 0) {
*y = 0;
Expand All @@ -20,17 +20,17 @@ inline void clamp_y(coord_int_t *y)
}
}

inline void rectangle_clamp(coord_int_t *x0, coord_int_t *y0,
coord_int_t *x1, coord_int_t *y1)
static inline void rectangle_clamp(coord_int_t *x0, coord_int_t *y0,
coord_int_t *x1, coord_int_t *y1)
{
clamp_x(x0);
clamp_x(x1);
clamp_y(y0);
clamp_y(y1);
}

inline void rectangle_clamp_and_swap(coord_int_t *x0, coord_int_t *y0,
coord_int_t *x1, coord_int_t *y1)
static inline void rectangle_clamp_and_swap(coord_int_t *x0, coord_int_t *y0,
coord_int_t *x1, coord_int_t *y1)
{
rectangle_clamp(x0, y0, x1, y1);

Expand Down
4 changes: 2 additions & 2 deletions display/src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <stdlib.h>

inline const struct glyph_t *font_find_glyph(const struct font_t *font,
const codepoint_t codepoint)
static inline const struct glyph_t *font_find_glyph(const struct font_t *font,
const codepoint_t codepoint)
{
uint16_t offset = 0;

Expand Down
22 changes: 11 additions & 11 deletions display/src/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ static uint16_t lcd_brightness_awake_backup VAR_RAM = 0xC000;

/* alphabetical order broken so that inlines appear before their use */

inline void _configure_pwm()
static inline void _configure_pwm()
{
GPIO_GPIO1DIR |= (1<<9);
GPIO_GPIO1DATA &= ~(1<<9);
IOCON_PIO1_9 = (0x1<<0);
}

inline void _disable_pwm()
static inline void _disable_pwm()
{
TMR_TMR16B0TCR = 0; // pwm timer
TMR_TMR16B1TCR = 0; // fade timer
IOCON_PIO1_9 = 0;
GPIO_GPIO1DATA &= ~(1<<9);
}

inline void _enable_pwm()
static inline void _enable_pwm()
{
GPIO_GPIO1DATA &= ~(1<<9);
TMR_TMR16B0TC = 0;
Expand All @@ -58,7 +58,7 @@ inline void _enable_pwm()
TMR_TMR16B1TCR = 1; // fade timer
}

inline void lcd_wrcmd8(uint8_t cmd)
void lcd_wrcmd8(uint8_t cmd)
{
LCD_MASKED_GPIO(LCD_RS_MASK, 0);
LCD_MASKED_GPIO(LCD_WR_MASK, 0);
Expand All @@ -68,41 +68,41 @@ inline void lcd_wrcmd8(uint8_t cmd)
LCD_MASKED_GPIO(LCD_RS_MASK, LCD_RS_MASK);
}

inline void lcd_wrdata8(uint8_t data)
void lcd_wrdata8(uint8_t data)
{
LCD_MASKED_GPIO(LCD_WR_MASK, 0);
LCD_MASKED_GPIO(LCD_DATA_MASK, data);
NOP();
LCD_MASKED_GPIO(LCD_WR_MASK, LCD_WR_MASK);
}

inline void lcd_wrdata16(uint16_t data)
void lcd_wrdata16(uint16_t data)
{
lcd_wrdata8((data >> 8) & 0xff);
lcd_wrdata8(data & 0xff);
}

inline void lcd_draw(uint16_t colour)
void lcd_draw(uint16_t colour)
{
lcd_wrdata16(colour);
}

inline void lcd_disable()
void lcd_disable()
{
LCD_MASKED_GPIO(LCD_CS_MASK, LCD_CS_MASK);
}

inline void lcd_enable()
void lcd_enable()
{
LCD_MASKED_GPIO(LCD_CS_MASK, 0);
}

inline void lcd_drawstart()
void lcd_drawstart()
{
lcd_wrcmd8(LCD_CMD_WRITE);
}

inline void lcd_drawstop()
void lcd_drawstop()
{

}
Expand Down
68 changes: 37 additions & 31 deletions display/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ void uint32_to_hex(const uint32_t c, uint8_t *dest)
} while (shift);
}

static int irq_called VAR_RAM = 0;
static volatile bool msg_pending = false;


void ADC_IRQHandler(void)
{
// only used for touch right now
irq_called = 1;
touch_intr_sm();
}

Expand Down Expand Up @@ -112,23 +109,28 @@ static coord_int_t prevz VAR_RAM = 0;

static inline enum event_t wait_for_event()
{
struct ticks_t t1 = ticks_get();
while (1) {
do {
if (msg_pending) {
msg_pending = false;
return EV_COMM;
__asm__ volatile("wfi");
struct ticks_t t1 = ticks_get();
if (ticks_delta(&last_touch_sample, &t1) > TOUCH_SAMPLE_INTERVAL) {
touch_intr_start();
}

if (msg_pending) {
msg_pending = false;
return EV_COMM;
}

if (touch_pending) {
last_touch_sample = ticks_get();
touch_pending = false;
if (touch_get_raw_z() > TOUCH_MIN_PRESSURE) {
prevz = 1;
return EV_TOUCH;
} else if (prevz > 0) {
prevz = 0;
return EV_TOUCH;
}
t1 = ticks_get();
} while (ticks_delta(&last_touch_sample, &t1) < TOUCH_SAMPLE_INTERVAL);
touch_sample();
last_touch_sample = ticks_get();
if (touch_get_raw_z() != 0) {
prevz = 1;
return EV_TOUCH;
} else if (prevz > 0) {
prevz = 0;
return EV_TOUCH;
}
}
}
Expand Down Expand Up @@ -421,7 +423,6 @@ int main(void)
DISABLE_IRQ();

SCB_PDRUNCFG &= ~SCB_PDRUNCFG_ADC;
//~ NVIC_EnableIRQ(ADC_IRQn);

SCB_SYSAHBCLKCTRL |= SCB_SYSAHBCLKCTRL_GPIO
| SCB_SYSAHBCLKCTRL_IOCON
Expand Down Expand Up @@ -509,6 +510,8 @@ int main(void)
touch_wait_for_clear();
}

NVIC_EnableIRQ(ADC_IRQn);

//~ comm_debug_tx_pong();

fill_rectangle(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1, 0x0000);
Expand Down Expand Up @@ -541,26 +544,21 @@ int main(void)
z = touch_get_z();

if ((z > 0) && (abs(prevx - x) + abs(prevy - y) <= 3)) {
prevx = x;
prevy = y;
break;
}

prevx = x;
prevy = y;

if (z == 0) {
prevx = -100;
prevy = -100;
}

//~ lcd_enable();
//~ fill_rectangle(x-2, y-2, x+2, y+2, 0x001f);
//~ lcd_disable();

msg_payload.subject = LPC_SUBJECT_TOUCH_EVENT;
msg_payload.payload.touch_ev.x = x;
msg_payload.payload.touch_ev.y = y;
if (z == 0) {
msg_payload.payload.touch_ev.x = prevx;
msg_payload.payload.touch_ev.y = prevy;
} else {
msg_payload.payload.touch_ev.x = x;
msg_payload.payload.touch_ev.y = y;
}
msg_payload.payload.touch_ev.z = z;
HDR_SET_PAYLOAD_LENGTH(msg_header, sizeof(struct lpc_msg_t));
msg_checksum = checksum((const uint8_t*)&msg_payload,
Expand All @@ -570,6 +568,14 @@ int main(void)
(const uint8_t*)&msg_payload,
msg_checksum);

prevx = x;
prevy = y;

if (z == 0) {
prevx = -100;
prevy = -100;
}

break;
}
case EV_NONE:
Expand Down
Loading