所以我正在关注教程,但是在C中进行操作,因为汇编太笨拙了。这是我在C中完成的第一个“真正的项目” ,所以这肯定是一个愚蠢的错误,或者是C工作方式的一些细微差别。
无论如何,我有这个代码:
/*闪烁.c*/
#包括“gpio.h”
#include "timer.h"
#include "morse.h"
#include “util.h”
#include "邮箱.h"
#包括“gpu.h”
#include“文本.h”
无效闪烁(){
gpio_setFunction(16, 1);
gpio_setFor(16, 0, 0x3f0000, 1);
gpu_makeDefaultBuffer();
gpu_fill(0xf8ff);
//gpio_setFor(16, 0, 0x3f0000, 1);
无符号整数 i = 0;
而(1){
gpu_drawPixelBridge(i, 0, i);
//gpu_fill(i);
如果(我 >= 0xffff){
休息;
}
我++;
}
//gpu_drawPixel(0, 0, 0x0000);
gpu_drawAsciiChar(0x0, 0xffff, 0x0000, 50, 10);
gpu_drawAsciiChar(0x0, 0b0011111, 0b00000011, 0, 50);
//gpu_drawPixel(50, 50, 0x0000);
gpu_drawAsciiChar(0x0, 0b0011111, 0b00000011, 0, 0);
print("Hello World!");
gpu_drawPixelBridge(10, 10, 0xffff);
gpu_drawPixel(100, 100, 0x0008);
gpu_drawPixelBridge(101, 101, 0x000f);
gpu_drawPixelBridge(102, 102, 0x0080);
gpio_setFor(16, 0, 0x1f0000, 1);
而(1){
// 东西
}
}
/*gpu.h*/
#ifndef H_GPUH
#define H_GPUH
#define CHARACTER_WIDTH 8
#define CHARACTER_HEIGHT 16
类型定义结构{
无符号整数 pwidth;
无符号整数 pheight;
无符号整数 vwidth;
无符号整数高度;
无符号整数间距;
无符号整数深度;
无符号整数 x;
无符号整数 y;
无效*缓冲区;
无符号整数大小;
} gpu_request __attribute__ ((aligned (16)));
int gpu_makeDefaultBuffer();
int gpu_makeBuffer(volatile gpu_request* req);
int gpu_fill(无符号短颜色);
int gpu_drawPixelBridge(unsigned int x, unsigned int y, unsigned short colour);
int gpu_drawPixel(unsigned int x, unsigned int y, unsigned short colour);
int gpu_drawAsciiChar(unsigned char chr, unsigned short front, unsigned short back, unsigned int ox, unsigned int oy);
#万一
/*gpu.c*/
#包括“gpu.h”
#include "邮箱.h"
易失性 gpu_request* gpu_buffer = 0;
const unsigned char gpu_font[];
int gpu_makeDefaultBuffer() {
易失性 gpu_request r;
r.pwidth = 1366;
r.pheight = 768;
r.vwidth = 1366;
r.vheight = 768;
r.pitch = 0;
r.depth = 16;
rx = 0;
ry = 0;
r.buffer = 0;
r.size = 0;
gpu_makeBuffer(&r);
}
int gpu_makeBuffer(volatile gpu_request* req) {
gpu_buffer = 请求;
if(mail_write((unsigned int)req+0x40000000, 1) < 0) {
//写入失败
gpio_errorHang(0, 1);
返回 1;
}
unsigned int resp = mail_read(1);
如果(响应){
//失败
gpio_errorHang(0, 2);
返回 2;
}
}
int gpu_fill(无符号短颜色){
/*无符号整数 i = 0;
while(i < /*(*_gpu_buffer).size* / 1366*768*2) {
*(volatile unsigned short *)((*_gpu_buffer).buffer+i) = colour;
我+=2;
}*/
整数 y = 0;
而(y < 100){
是++;
诠释 x = 0;
而(x < 100){
x++;
gpu_drawPixel(x, y, colour);
}
}
}
int gpu_drawPixel(unsigned int x, unsigned int y, unsigned short colour) {
unsigned int dest = (y * (*gpu_buffer).pitch) + x*2;
*(volatile unsigned short *)((*gpu_buffer).buffer+dest) = colour;
}
int gpu_drawPixelBridge(unsigned int x, unsigned int y, unsigned short colour) {
gpu_drawPixel(x, y, colour);
}
int gpu_drawAsciiChar(unsigned char chr, unsigned short front, unsigned short back, unsigned int ox, unsigned int oy) {
unsigned int base = (chr*16)+((int)&gpu_font);
无符号整数 y = 0;
而(y < 16){
无符号整数 x = 0;
而(x < 8){
if((*(unsigned char*)base >> x) & 0b1) {
gpu_drawPixel(ox+x, oy+y, 前);
}别的{
gpu_drawPixel(ox+x, oy+y, back);
}
x++;
}
是++;
基础++;
}
}
(在这篇文章中,为了测试和简洁,验证被删除了)
好吧,我会用如此不一致和“古怪”的词来描述它是如何工作的。目前,我认为至少有一个问题是从它所在的文件以外的任何文件调用drawPixel。从blink.c 调用drawPixelBridge 可以工作,drawAsciiCharacter 也是如此,但是直接从blink.c 调用drawPixel 似乎都不起作用并且(可能) 导致任何未来尝试绘制任何东西失败。
我想我应该解释一下绘画是如何工作的。通常,gpu_makeBuffer 向 GPU 发送带有指向 gpu_request 结构的指针的“邮件”。然后,GPU 通过将该结构的“缓冲区”属性设置为指向将在屏幕上绘制的字节序列的指针来对此做出响应。鉴于这些东西出现在屏幕上的正确位置(尽管尺寸和间距不是我所期望的),我认为这是可行的。