freemyipod r81 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r80‎ | r81 | r82 >
Date:18:12, 10 August 2010
Author:theseven
Status:new
Tags:
Comment:
Backport thread-safe console code from Nano 2G
Modified paths:
  • /embios/branches/4g_compat/SOURCES (modified) (history)
  • /embios/branches/4g_compat/console.c (modified) (history)
  • /embios/branches/4g_compat/console.h (modified) (history)
  • /embios/branches/4g_compat/drawing.h (modified) (history)
  • /embios/branches/4g_compat/lcdconsole.c (modified) (history)
  • /embios/branches/4g_compat/lcdconsole.h (modified) (history)
  • /embios/branches/4g_compat/target/ipodnano4g/lcd.S (modified) (history)
  • /embios/branches/4g_compat/target/ipodnano4g/lcd.h (modified) (history)
  • /embios/branches/4g_compat/target/ipodnano4g/power.c (modified) (history)

Diff [purge]

Index: embios/branches/4g_compat/console.c
@@ -24,10 +24,10 @@
2525 #include "global.h"
2626 #include "console.h"
2727 #include "lcdconsole.h"
 28+#include "usb/dbgconsole.h"
2829 #include "format.h"
29 -#include <stdio.h>
 30+#include "thread.h"
3031 #include <stdarg.h>
31 -#include <stdbool.h>
3232 #include <limits.h>
3333
3434
@@ -38,10 +38,27 @@
3939 };
4040
4141
 42+struct mutex console_mutex;
 43+struct mutex console_readmutex;
 44+
 45+
 46+void console_init()
 47+{
 48+ mutex_init(&console_mutex);
 49+ mutex_init(&console_readmutex);
 50+}
 51+
 52+void cputc_internal(unsigned int consoles, char string) ICODE_ATTR;
 53+void cputc_internal(unsigned int consoles, char string)
 54+{
 55+ if (consoles & 1) lcdconsole_putc(string, 0, -1);
 56+ if (consoles & 2) dbgconsole_putc(string);
 57+}
 58+
4259 static int cprfunc(void* ptr, unsigned char letter)
4360 {
4461 struct for_cprintf* pr = (struct for_cprintf*)ptr;
45 - cputc_noblit(pr->consoles, letter);
 62+ cputc_internal(pr->consoles, letter);
4663 pr->bytes++;
4764 return true;
4865 }
@@ -54,11 +71,11 @@
5572 pr.consoles = consoles;
5673 pr.bytes = 0;
5774
 75+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
5876 va_start(ap, fmt);
5977 format(cprfunc, &pr, fmt, ap);
6078 va_end(ap);
61 -
62 - lcdconsole_update();
 79+ mutex_unlock(&console_mutex);
6380
6481 return pr.bytes;
6582 }
@@ -70,9 +87,9 @@
7188 pr.consoles = consoles;
7289 pr.bytes = 0;
7390
 91+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
7492 format(cprfunc, &pr, fmt, ap);
75 -
76 - lcdconsole_update();
 93+ mutex_unlock(&console_mutex);
7794
7895 return pr.bytes;
7996 }
@@ -79,25 +96,61 @@
8097
8198 void cputc(unsigned int consoles, char string)
8299 {
83 - if (consoles & 1) lcdconsole_putc(string, 0, -1);
 100+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
 101+ cputc_internal(consoles, string);
 102+ mutex_unlock(&console_mutex);
84103 }
85104
86 -void cputc_noblit(unsigned int consoles, char string)
 105+void cputs(unsigned int consoles, const char* string)
87106 {
88 - if (consoles & 1) lcdconsole_putc_noblit(string, 0, -1);
 107+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
 108+ if (consoles & 1) lcdconsole_puts(string, 0, -1);
 109+ if (consoles & 2) dbgconsole_puts(string);
 110+ mutex_unlock(&console_mutex);
89111 }
90112
91 -void cputs(unsigned int consoles, const char* string)
 113+void cwrite(unsigned int consoles, const char* string, size_t length)
92114 {
93 - if (consoles & 1) lcdconsole_puts(string, 0, -1);
 115+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
 116+ if (consoles & 1) lcdconsole_write(string, length, 0, -1);
 117+ if (consoles & 2) dbgconsole_write(string, length);
 118+ mutex_unlock(&console_mutex);
94119 }
95120
96 -void cputs_noblit(unsigned int consoles, const char* string)
 121+void cflush(unsigned int consoles)
97122 {
98 - if (consoles & 1) lcdconsole_puts_noblit(string, 0, -1);
 123+ mutex_lock(&console_mutex, TIMEOUT_BLOCK);
 124+ if (consoles & 1) lcdconsole_update();
 125+ mutex_unlock(&console_mutex);
99126 }
100127
101 -void cflush(unsigned int consoles)
 128+int cgetc(unsigned int consoles, int timeout)
102129 {
103 - if (consoles & 1) lcdconsole_update();
 130+ int result;
 131+ mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
 132+ if ((consoles & 2) && (result = dbgconsole_getc(timeout)) != -1) return result;
 133+ mutex_unlock(&console_mutex);
104134 }
 135+
 136+int cread(unsigned int consoles, char* buffer, size_t length, int timeout)
 137+{
 138+ int result;
 139+ mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
 140+ if ((consoles & 2) && (result = dbgconsole_read(buffer, length, timeout))) return result;
 141+ mutex_unlock(&console_mutex);
 142+}
 143+
 144+void creada(unsigned int consoles, char* buffer, size_t length, int timeout)
 145+{
 146+ int result;
 147+ mutex_lock(&console_readmutex, TIMEOUT_BLOCK);
 148+ while (length)
 149+ {
 150+ if (length && (consoles & 2) && (result = dbgconsole_read(buffer, length, timeout)))
 151+ {
 152+ buffer = &buffer[result];
 153+ length -= result;
 154+ }
 155+ }
 156+ mutex_unlock(&console_mutex);
 157+}
Index: embios/branches/4g_compat/console.h
@@ -26,16 +26,21 @@
2727
2828
2929 #include "global.h"
 30+#include "gcc_extensions.h"
3031 #include <stdarg.h>
3132
3233
 34+void console_init() INITCODE_ATTR;
3335 void cputc(unsigned int consoles, char string) ICODE_ATTR;
34 -void cputc_noblit(unsigned int consoles, char string) ICODE_ATTR;
3536 void cputs(unsigned int consoles, const char* string) ICODE_ATTR;
36 -void cputs_noblit(unsigned int consoles, const char* string) ICODE_ATTR;
37 -int cprintf(unsigned int consoles, const char* fmt, ...) ICODE_ATTR;
 37+void cwrite(unsigned int consoles, const char* string, size_t length) ICODE_ATTR;
 38+int cprintf(unsigned int consoles, const char* fmt, ...) ICODE_ATTR
 39+ ATTRIBUTE_PRINTF(2, 3);
3840 int cvprintf(unsigned int consoles, const char* fmt, va_list ap) ICODE_ATTR;
3941 void cflush(unsigned int consoles) ICODE_ATTR;
 42+int cgetc(unsigned int consoles, int timeout) ICODE_ATTR;
 43+int cread(unsigned int consoles, char* buffer, size_t length, int timeout) ICODE_ATTR;
 44+void creada(unsigned int consoles, char* buffer, size_t length, int timeout) ICODE_ATTR;
4045
4146
4247 #endif
Index: embios/branches/4g_compat/drawing.h
@@ -32,7 +32,7 @@
3333 #define FONT_HEIGHT 8
3434
3535
36 -void rendertext(void* buffer, int fgcol, int bgcol, char* text, int stride);
 36+void renderchar(void* buffer, int fgcol, int bgcol, char text, int stride);
3737 void renderbmp(void* buffer, void* bitmap, int stride);
3838
3939
Index: embios/branches/4g_compat/SOURCES
@@ -11,11 +11,12 @@
1212 #ifdef TARGET_ipodnano4g
1313 target/ipodnano4g/mmu.c
1414 target/ipodnano4g/lcd.c
 15+target/ipodnano4g/timer.c
1516 target/ipodnano4g/i2c.S
 17+target/ipodnano4g/interrupt.c
 18+target/ipodnano4g/power.c
1619 target/ipodnano4g/accel.c
1720 target/ipodnano4g/backlight.c
18 -target/ipodnano4g/interrupt.c
19 -target/ipodnano4g/timer.c
2021 usb/synopsysotg.c
2122 #endif
2223
Index: embios/branches/4g_compat/target/ipodnano4g/lcd.h
@@ -35,6 +35,7 @@
3636 #define LCD_FRAMEBUFSIZE (LCD_WIDTH * LCD_HEIGHT * LCD_BYTESPERPIXEL)
3737
3838
 39+void lcd_init();
3940 void displaylcd(unsigned int startx, unsigned int endx,
4041 unsigned int starty, unsigned int endy, void* data, int color);
4142 void displaylcd_sync();
Index: embios/branches/4g_compat/target/ipodnano4g/lcd.S
@@ -21,24 +21,14 @@
2222 @
2323
2424
25 -.section .text.displaylcd_safe, "ax", %progbits
 25+.section .initcode.lcd_init, "ax", %progbits
2626 .align 2
27 -.global displaylcd_safe
28 -.type displaylcd_safe, %function
29 -displaylcd_safe:
30 - mov r0, #1
 27+.global lcd_init
 28+.type lcd_init, %function
 29+lcd_init:
3130 mov pc, lr
32 -.size displaylcd_safe, .-displaylcd_safe
 31+.size lcd_init, .-lcd_init
3332
34 -.section .text.displaylcd_busy, "ax", %progbits
35 -.align 2
36 -.global displaylcd_busy
37 -.type displaylcd_busy, %function
38 -displaylcd_busy:
39 - mov r0, #0
40 - mov pc, lr
41 -.size displaylcd_busy, .-displaylcd_busy
42 -
4333 .section .icode.displaylcd, "ax", %progbits
4434 .align 2
4535 .global displaylcd
@@ -108,3 +98,22 @@
10999 displaylcd_sync:
110100 mov pc, lr
111101 .size displaylcd_sync, .-displaylcd_sync
 102+
 103+.section .icode.displaylcd_busy, "ax", %progbits
 104+.align 2
 105+.global displaylcd_busy
 106+.type displaylcd_busy, %function
 107+displaylcd_busy:
 108+ mov r0, #0
 109+ mov pc, lr
 110+.size displaylcd_busy, .-displaylcd_busy
 111+
 112+.section .icode.displaylcd_safe, "ax", %progbits
 113+.align 2
 114+.global displaylcd_safe
 115+.type displaylcd_safe, %function
 116+displaylcd_safe:
 117+ mov r0, #1
 118+ mov pc, lr
 119+.size displaylcd_safe, .-displaylcd_safe
 120+
Index: embios/branches/4g_compat/target/ipodnano4g/power.c
@@ -22,9 +22,7 @@
2323
2424
2525 #include "global.h"
26 -#include "s5l8701.h"
2726 #include "power.h"
28 -#include "pmu.h"
2927
3028
3129 void reset();
Index: embios/branches/4g_compat/lcdconsole.c
@@ -25,6 +25,7 @@
2626 #include "lcdconsole.h"
2727 #include "drawing.h"
2828 #include "util.h"
 29+#include "contextswitch.h"
2930
3031
3132 #define OFFSETX LCDCONSOLE_OFFSETX
@@ -37,62 +38,73 @@
3839
3940
4041 static unsigned char framebuf[LCD_FRAMEBUFSIZE];
41 -static unsigned int current_row;
42 -static unsigned int current_col;
 42+static unsigned int current_row IBSS_ATTR;
 43+static unsigned int current_col IBSS_ATTR;
 44+static bool lcdconsole_needs_update IBSS_ATTR;
4345
4446
4547 void lcdconsole_init()
4648 {
47 - memset(framebuf, -1, sizeof(framebuf));
48 - current_row = 0;
49 - current_col = -1;
 49+ memset(framebuf, -1, sizeof(framebuf));
 50+ current_row = 0;
 51+ current_col = -1;
 52+ lcdconsole_needs_update = false;
5053 }
5154
5255 void lcdconsole_putc_noblit(char string, int fgcolor, int bgcolor)
5356 {
54 - if (string == '\r')
55 - {
56 - current_col = 0;
57 - return;
58 - }
59 - current_col++;
60 - if (string == '\n')
61 - {
62 - current_col = -1;
63 - current_row++;
64 - return;
65 - }
66 - if (string == '\t')
67 - {
68 - current_col |= 3;
69 - return;
70 - }
71 - if (current_col >= LCDCONSOLE_COLS)
72 - {
73 - current_col = 0;
74 - current_row++;
75 - }
76 - if (current_row >= LCDCONSOLE_ROWS)
77 - {
78 - int offset = current_row - LCDCONSOLE_ROWS + 1;
79 - memcpy(&framebuf[LINEBYTES * OFFSETY], &framebuf[LINEBYTES * OFFSETY + ROWBYTES * offset],
80 - ROWBYTES * (LCDCONSOLE_ROWS - offset));
81 - memset(&framebuf[LINEBYTES * OFFSETY + ROWBYTES * (LCDCONSOLE_ROWS - offset)],
82 - -1, ROWBYTES * offset);
83 - current_row = LCDCONSOLE_ROWS - 1;
84 - }
85 - renderchar(&framebuf[OFFSETBYTES + ROWBYTES * current_row + COLBYTES * current_col],
86 - fgcolor, bgcolor, string, LINEBYTES);
 57+ if (string == '\r') return;
 58+ current_col++;
 59+ if (string == '\n')
 60+ {
 61+ current_col = -1;
 62+ current_row++;
 63+ return;
 64+ }
 65+ if (string == '\t')
 66+ {
 67+ current_col |= 3;
 68+ return;
 69+ }
 70+ if (current_col >= LCDCONSOLE_COLS)
 71+ {
 72+ current_col = 0;
 73+ current_row++;
 74+ }
 75+ if (current_row >= LCDCONSOLE_ROWS)
 76+ {
 77+ int offset = current_row - LCDCONSOLE_ROWS + 1;
 78+ memcpy(&framebuf[LINEBYTES * OFFSETY], &framebuf[LINEBYTES * OFFSETY + ROWBYTES * offset],
 79+ ROWBYTES * (LCDCONSOLE_ROWS - offset));
 80+ memset(&framebuf[LINEBYTES * OFFSETY + ROWBYTES * (LCDCONSOLE_ROWS - offset)],
 81+ -1, ROWBYTES * offset);
 82+ current_row = LCDCONSOLE_ROWS - 1;
 83+ }
 84+ renderchar(&framebuf[OFFSETBYTES + ROWBYTES * current_row + COLBYTES * current_col],
 85+ fgcolor, bgcolor, string, LINEBYTES);
8786 }
8887
8988 void lcdconsole_puts_noblit(const char* string, int fgcolor, int bgcolor)
9089 {
91 - while (*string) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
 90+ while (*string) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
9291 }
9392
 93+void lcdconsole_write_noblit(const char* string, size_t length, int fgcolor, int bgcolor)
 94+{
 95+ while (length--) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
 96+}
 97+
9498 void lcdconsole_update()
9599 {
96 - displaylcd(0, LCD_WIDTH - 1, 0, LCD_HEIGHT - 1, framebuf, 0);
 100+ uint32_t mode = enter_critical_section();
 101+ if (displaylcd_busy())
 102+ {
 103+ lcdconsole_needs_update = true;
 104+ leave_critical_section(mode);
 105+ return;
 106+ }
 107+ leave_critical_section(mode);
 108+ displaylcd(0, LCD_WIDTH - 1, 0, LCD_HEIGHT - 1, framebuf, 0);
97109 }
98110
99111 void lcdconsole_putc(char string, int fgcolor, int bgcolor)
@@ -106,3 +118,18 @@
107119 while (*string) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
108120 lcdconsole_update();
109121 }
 122+
 123+void lcdconsole_write(const char* string, size_t length, int fgcolor, int bgcolor)
 124+{
 125+ while (length--) lcdconsole_putc_noblit(*string++, fgcolor, bgcolor);
 126+ lcdconsole_update();
 127+}
 128+
 129+void lcdconsole_callback()
 130+{
 131+ if (lcdconsole_needs_update)
 132+ {
 133+ displaylcd(0, LCD_WIDTH - 1, 0, LCD_HEIGHT - 1, framebuf, 0);
 134+ lcdconsole_needs_update = false;
 135+ }
 136+}
Index: embios/branches/4g_compat/lcdconsole.h
@@ -39,9 +39,12 @@
4040 void lcdconsole_init();
4141 void lcdconsole_putc(char string, int fgcolor, int bgcolor) ICODE_ATTR;
4242 void lcdconsole_puts(const char* string, int fgcolor, int bgcolor) ICODE_ATTR;
 43+void lcdconsole_write(const char* string, size_t length, int fgcolor, int bgcolor) ICODE_ATTR;
4344 void lcdconsole_putc_noblit(char string, int fgcolor, int bgcolor) ICODE_ATTR;
4445 void lcdconsole_puts_noblit(const char* string, int fgcolor, int bgcolor) ICODE_ATTR;
 46+void lcdconsole_write_noblit(const char* string, size_t length, int fgcolor, int bgcolor) ICODE_ATTR;
4547 void lcdconsole_update() ICODE_ATTR;
 48+void lcdconsole_callback() ICODE_ATTR;
4649
4750
4851 #endif