Nhúng hệ điều hành uClinux và giao tiếp với USB, thẻ nhớ SD

Một phần của tài liệu Thiết kế hệ thống hiển thị video trên bảng LED đa sắc – hỗ trợ Flash USB, thẻ nhớ SD và giao tiếp Ethernet (Trang 106)

1. Nội dung thiết kế tốt nghiệp:

7.1.1 Nhúng hệ điều hành uClinux và giao tiếp với USB, thẻ nhớ SD

Giao tiếp với Flash USB chuẩn định dạng FAT16 hoặc FAT32 trên hệ điều hành nhúng.

Giao tiếp với thẻ nhớ SD chuẩn định dạng FAT16 không sử dụng hệ điều hành, với các chức năng như đọc tên và hiển thị trên các file được lưu trữ, đọc nội dung file tùy chọn.

7.1.2 Xây dựng chƣơng trình truyền file xuống kit qua mạng LAN

Xây dựng thành công chương trình Socket Server trên hệ thống Nios II nhận dữ liệu từ client trên PC truyền xuống thông qua giao thức RTP, môi trường truyền dẫn là mạng nội bộ LAN.

7.1.3 Xây dựng khối giải mã JPEG

Khối giải mã JPEG có khả năng giải mã liên tiếp chuỗi ảnh MJPEG, dữ liệu video nén được truyền từ phía vi điều khiển với các nguồn được đọc từ Flash USB, thẻ nhớ SD, hay từ mạng LAN.

7.2 Kết luận

Đồ án đã được hoàn thành, về cơ bản dự án này đáp ứng được các yêu cầu chức năng đã đặt ra từ trước. Có thể xử lý hiển thị các video có kích thước 320x240, chuẩn VGA 640x480, và chuẩn 1024x720 từ các thiết bị và nguồn nêu ở trên.

Với hỗ trợ hiển thị video từ USB:

 Video kích thước 320x240 có tốc độ hiển thị đạt khoảng 50 hình/s.

107

 Video kích thước 1024x768 có tốc độ hiển thị đạt khoảng 10 hình/s. Nhưng với đề tài này, việc kiểm thử chỉ tiến hành lưu trữ và hiển thị một vùng kích thước 64x64 của video trên. Hình ảnh sẽ được hiển thị đầy đủ với nhiều module bảng led 64x64, mỗi module điều khiển cho từng bảng sẽ chịu trách nhiệm hiển thị từng phần tương ứng của video sau khi đã giải mã.

Tài nguyên sử dụng của hệ thống trên kit DE2 sau khi được synthesis được thể hiện trong hình 7.1.

Hình 7.1 Tài nguyên sử dụng của hệ thống

Trong đó, một vài thông số cần chú ý trong thiết kế với công nghệ FPGA như: - “Total logic elements” là số thành phần logic FPGA, chiếm 17.543 LE

(53%).

- “Total memory bits” là tài nguyên bộ nhớ RAM của chip FPGA chiếm 295.282/483.840 (61%).

7.3 Hƣớng nghiên cứu tiếp theo

Dựa trên kết quả đã đạt được và các nhược điểm còn tồn tại trong quá trình phát triển hệ thống, nhóm tác giả nhận thấy để có thể đi vào ứng dụng thực tiễn, hệ thống cần được tiếp tục nghiên cứu theo các hướng như sau:

 Nâng cấp khối giải mã MJPEG thành MPEG-2 hoặc MPEG-4. Đây là các định dạng được sử dụng phổ biến hơn và hiệu quả hơn trong việc truyền và lưu trữ video.

108

Tài liệu tham khảo

[1] Volnei A. Pedroni, Circuit Design with VHDL, The MIT Press, 2004.

[2] Peter J. Ashenden, Digital Design- An Embedded Systems Approach Using

Verilog, Morgan Kaufmann, 2007.

[3] Ronald Sass and Andrew G. Schmidt, Embedded Systems Design with Platform

FPGAs - Principles and Practices, Morgan Kaufmann, 2010.

[4] R.C. Cofer and Benjamin F. Harding, Rapid System Prototyping with FPGAs, Newnes, 2005.

[5] Kimmo Nikkanen, Bachelor‟s Thesis “uClinux as an Embedded solution”, January 2003.

[6] Philipp Lutz, “Device drivers and Test application for a SOPC solution with

Nios II softcore processor and µClinux”, February 9, 2008.

[7] http://www.c-jump.com/CIS24/Slides/FAT/index.html, truy cập cuối cùng ngày 30/5/2012.

[8] http://en.wikipedia.org/wiki/File_Allocation_Table, truy cập cuối cùng ngày 30/5/2012.

[9] “Altera University Program Secure Data Card IP Core”, Altera corporation, March 2009.

[10] http://en.wikipedia.org/wiki/Real-time_Transport_Protocol, truy cập lần cuối ngày 31/5/2012.

[11] Brian "Beej" Hall, Beej’s Guide to Network Programming Using Internet

Sockets [Online]. Available: http://beej.us/guide/bgnet/.

[12] Michael Opdenacker (November 20, 2007), “Introduction to uClinux” [Online]. Avaiable: http://free-electrons.com/articles/uclinux.

[13] http://www.alterawiki.com/wiki/BinaryToolchain, truy cập lần cuối ngày 30/5/2012.

[14] http://www.alterawiki.com/wiki/ModuleProgramming, truy cập cuối cùng ngày 30/5/2012.

[15] Philips Semiconductors, “ISP 1362 embedded programming guide”, June 2002.

109

[16] http://www.alterawiki.com/wiki/FlashProgrammer, truy cập lần cuối ngày 30/5/2012.

[17] International Telecommunication Union, “Information technology digital compression and coding of continuos-tone still images requirements and

guidelines”, ITU, 1993.

[18] S.Stuijk, “Design and implementation of a JPEG decoder”, Faculty of electrical engineering, December 2001.

[19] http://www.alterawiki.com/wiki/CompileHello, truy cập cuối cùng ngày 30/5/2012.

[20] James O. Hamblen – T.S Hall – M.D.Furman, “Rapid prototyping of digital systems – SOPC Edition”, Springer Science, 2008.

[21] David Lariviere and Stephen A.Edwards, “uClinux on the altera DE2”, Columbia University, 2008.

[22] http://www.alterawiki.com/wiki/UsbHost, truy cập cuối cùng ngày 30/5/2012. [23] http://www.alterawiki.com/wiki/MmcSd, truy cập cuối cùng ngày 30/5/2012. [24] http://www.ccm.ece.vt.edu/twiki/bin/view/Main/Linux On NIOS2 Installation, truy cập cuối cùng ngày 30/5/2012.

[25] http://alumni.cs.ucr.edu/~amitra/sdcard/Additional/sdcard_appnote_foust.pdf, truy cập cuối cùng ngày 30/5/2012.

[26] http://opencores.org/project,mjpeg-decoder,overview, truy cập cuối cùng ngày 30/5/2012.

110

Bảng đối chiếu thuật ngữ Anh - Việt

Tên thuật ngữ tiếng Anh Tên thuật ngữ tiếng Việt

LED full color Bảng LED đa sắc

Partition Phân vùng

Streaming Tạo luồng video

End to End Truyền dữ liệu giữa hai điểm cuối Multicast Truyền dữ liệu tới nhiều đích

Real time Thời gian thực

Payload Dữ liệu tải

Profile Hồ sơ

Header Tiêu đề

Audio Âm thanh

Video Hình ảnh

Server Máy chủ

Client Máy khách

Smartphone Điện thoại thông minh

SW-HW codesign Đồng thiết kế phần cứng- phần mềm

Multithread Đa luồng

Clock Xung đồng hồ

Link Liên kết

Process Tiến trình

Stack Ngăn xếp

Flat memory model Mô hình bộ nhớ phẳng

Toolchain Gói công cụ dùng để phiên dịch kernel

Script File kịch bản

Kernel Nhân hệ điều hành

Segmentation Fault Lỗi truy cập bộ nhớ

111

Driver Trình quản lý, điều khiển ngoại vi

Boot Khởi động hệ điều hành

Download Nạp file ảnh zImage xuống KIT Component Thành phần của hệ thống

112

Phụ lục

Phụ lục 1: Chƣơng trình đọc file MJPEG từ một phân vùng trong uClinux và truyền tới khối hiển thị

#include <stdio.h> #include <stdlib.h> #include <pthread.h>

#define writel(b, addr) (void)((*(unsigned int *) (addr)) = (b)) #define readl(addr) \

({ unsigned int __v = (*(volatile unsigned int *) (addr)); __v; })

#define leng_buf 524288*2 unsigned char *buf;

FILE *fs;

unsigned int i,k,done; unsigned int readNum; void *readthread(); void *writethread();

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int main(int arg,char *arr[])

{

pthread_t thread1, thread2; int iret1, iret2;

fs = fopen(arr[1],"rb"); if(fs==NULL)

{

printf("Cannot open source file ! Press key to exit."); exit(0);

}

writel(0, 0x80400010);

buf = (unsigned char *) malloc(leng_buf);

/* Create independent threads each of which will execute function */

iret1 = pthread_create( &thread1, NULL, readthread, NULL); iret2 = pthread_create( &thread2, NULL, writethread, NULL); /* Wait till threads are complete before main continues. Unless we */

/* wait we run the risk of executing an exit which will terminate *

/* the process and all threads before the threads have completed. */

pthread_join( thread1, NULL); pthread_join( thread2, NULL);

113

printf("Thread 2 returns: %d\n",iret2); exit(0); return 1; } void *readthread() { unsigned char c; unsigned int j; j=0; while (1){ //pthread_mutex_lock( &mutex1 ); c = fgetc(fs); buf[j] = c; ++j; if (j>=leng_buf-2){j=0;} // pthread_mutex_unlock( &mutex1 ); } printf("END!\n"); } void *writethread() { start: k=0; done = 0; while(k<leng_buf-1) { i=0; readNum = 0;

while(i<4) // Buffer 32 bit tu file { if(buf[k] == 0xd8) if (buf[k-1] == 0xff) { writel(0, 0x80400030); writel(1, 0x80400030); readNum = 0xff; i = 1; } readNum += (buf[k]<<(8*i++)); k++; }

writel(readNum, 0x80400000); // Nap data

writel(0, 0x80400010); // Cho phep doc data while(readl(0x80400020)!=1); writel(1, 0x80400010); } done = 1; goto start; }

114

Phụ lục 2: Chƣơng trình đọc file trong thẻ nhớ SD không thông qua hệ điều hành (nhằm tăng tốc độ truy xuất)

#include "basic_io.h" #include "LCD.h" #include "Fat16.h" #include "Interrupt.h" #include <stdio.h> #include <string.h> //--- --- #define UP 0x08 #define DOWN 0x04 #define ENTER 0x02 //--- --- int numOfFile;

volatile int button = 0;

//--- ---

int ScanFile() //Duyet het tat ca cac file, tra ve so file tim duoc {

int j = 0, i = 0;

while (i < 256) // toi da 255 file {

i++;

//if(root[0]==0xE5) {LCD_Display("OH", "GHOST!");while(1);} rootEntry = i * 32;

if ((root[rootEntry] != 0xE5) && (root[rootEntry] > 0x20)) //Ki tu khong hop le

{

if (!GetHeader(&entry[i], root)) continue; // Neu khong doc duoc thi tiep tuc

entry[j++] = entry[i]; } } return j-1; } //--- ---

void BlockData(BYTE data[], char strres[], int i) // tra ve 1 mang 16 ky tu cua noi dung file, i la chi so cua block tuong ung

{ int j; for( j = 0; j<15; j++) strres[j] = (char)(data[15*i +j]); strres[j]='\0'; } //--- ---

void ShowData(int index) {

int dataindex = 0; char text1[16]; BYTE data[100000];

115 while(1) { if (!button) continue; switch (button) { case UP: if(dataindex != 0) dataindex--; break; case DOWN: if(dataindex < (entry[index].size) / 15){ dataindex++; break; } else return; case ENTER: break; default : return; }

BlockData(data, text1, dataindex); LCD_Display(" data ", text1); button = 0x00; } } //--- --- void ShowFile() { int fileindex = 0; int cursor = 1;

LCD_Display2(" LIST FILE:", entry[0].name, 2); while(1) { if (!button) continue; switch (button) { case UP: cursor--; if (!cursor) { cursor = 1; fileindex--; if (fileindex <= 0) { fileindex = 0;

LCD_Display2(" LIST FILE:", entry[0].name, 2); button = 0x00; continue; } } break; case DOWN: cursor++; if (cursor == 3) { cursor = 2; fileindex++;

if (fileindex == numOfFile) fileindex = numOfFile - 1;

116 break;

case ENTER: {

button = 0x00;

if (cursor == 1) ShowData(fileindex); else

ShowData(fileindex+1); } break; }

LCD_Display2(entry[fileindex].name, entry[fileindex+1].name,

cursor); // Coi nhu co 2 file tro len button = 0x00; } } //--- --- int main(void) { int i=0; LCD_Init(); LCD_Display("...", "..."); while (SD_card_init()); usleep(5000);

LCD_Display("SD card", "Pluged in"); //while(fat[0]!=0xF8)

SD_read_lba(fat, fatBase+i++, SectorsPerFAT);

SD_read_lba(root, rootBase, SectorsPerROOT); //WinHex ->

physical sector //---Interupt procedure--- ---// IOWR_ALTERA_AVALON_PIO_EDGE_CAP( BUTTON_PIO_BASE, 0 ); btn_int();

alt_irq_register( BUTTON_PIO_IRQ, (void *) &button,

(void*)buttons_isr ); //cau lenh khoi tao ngat chinh ? IOWR_ALTERA_AVALON_PIO_IRQ_MASK( BUTTON_PIO_BASE, 0xF ); // numOfFile = ScanFile(); ShowFile(); return 0; }

117

Phụ lục 3: Chƣơng trình convert chuỗi ảnh JPEG thành file MJPEG

#include <stdio.h> #include <stdlib.h> #include <conio.h>

int main(int argc, char *argv[]) {

FILE *in, *out; char ch;

char buf[14]; int i;

if((out=fopen("file.mjpeg", "wb")) == NULL) { printf("Cannot open output file.\n");

exit(1); }

i=85; while (1){

sprintf (buf, "video2%04d.jpg",i++); if((in=fopen(buf,"rb")) == NULL) {

printf("Cannot open input file: %s\n",buf); fclose(out);

getch(); exit(1); }

else

printf("open input file: %s\n",buf); while(!feof(in)) { ch = getc(in); if(ferror(in)) { printf("Read Error\n"); clearerr(in); break; } else {

if(!feof(in)) putc(ch, out); if(ferror(out)) { printf("Write Error\n"); clearerr(out); break; } } } fclose(in); } return 0; }

118

Phụ lục 4: Cấu hình cho các driver ngoại vi trong hệ thống uClinux

#

# Misc strange devices #

menu "Misc devices"

config DE2_BOARD

tristate "DE2 BOARD" depends on ALTERA_DE2

config DE2_LEDS

tristate "DE2 LEDS MODULE" depends on DE2_BOARD

config DE2_SWITCHES

tristate "DE2 SWITCHES MODULE" depends on DE2_BOARD

help

module, which shows the positions of the switches MAJOR number: 242

DEVICE FILE: /dev/switches config DE2_BUTTONS

tristate "DE2 BUTTONS MODULE" depends on DE2_BOARD

help

rudimentary module, which makes access to the buttons

(reading out the last pressed button(s)). MAJOR number: 244

DEVICE FILE: /dev/buttons config DE2_SEG7

tristate "DE2 7 SEGMENT DISPLAY MODULE" depends on DE2_BOARD

help

module, which controlls the 7 segment display MAJOR number: 243

DEVICE FILE: /dev/seg7 config DE2_LCD16207

tristate "DE2 LCD16207 MODULE" depends on DE2_BOARD

help

module, which controlls the LC-Display 16207 MAJOR number: 250

DEVICE FILE: /dev/lcd16207 Endmenu

Một phần của tài liệu Thiết kế hệ thống hiển thị video trên bảng LED đa sắc – hỗ trợ Flash USB, thẻ nhớ SD và giao tiếp Ethernet (Trang 106)