Created
May 26, 2020 10:04
-
-
Save Alexander-Tengborg/dfb7960f1e9dc1f69af89080961a1e6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* asciidisplay_driver.c | |
* MD407 library | |
*/ | |
/* declarations goes to 'libMD407.h' */ | |
#include "libMD407.h" | |
/* Define variables here */ | |
/* Define functions here */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* general.c | |
* MD407 library | |
*/ | |
/* declarations goes to 'libMD407.h' */ | |
#include "libMD407.h" | |
#include "usart_driver" | |
#include "keypad_driver" | |
#include "asciidisplay_driver" | |
/* Define variables here */ | |
static char *heap_end; | |
PDEV_DRIVER_DESC device_table[MAX_FILENO] = | |
{ | |
&StdIn, | |
&StdOut, | |
&StdErr, | |
&KeyPad, | |
&AsciiDisplay, | |
}; | |
/* Define functions here */ | |
void crt_init() { | |
extern char __bss_start; | |
extern char __bss_end; | |
extern char __heap_low; | |
extern char __heap_top; | |
char *s; | |
s = &__bss_start; | |
while( s < &__bss_end) | |
*s++ = 0; | |
s = &__heap_low; | |
while( s < &__heap_top ) | |
*s++ = 0; | |
for( int i = 0; i < MAX_FILENO; i++ ) | |
{ | |
fd = device_table[i]; | |
if( fd‐>init != 0) | |
(void) fd‐>init( 0 ); | |
} | |
} | |
void crt_deinit(void) | |
{ | |
} | |
char* _sbrk( int incr) { | |
extern char __heap_low; | |
extern char __heap_top; | |
char *prev_heap_end; | |
if (heap_end == 0) { | |
heap_end = &__heap_low; | |
} | |
prev_heap_end = heap_end; | |
if (heap_end + incr > &__heap_top) { | |
/* Heap and stack collision */ | |
errno = ENOMEM; | |
return (char *)-1; | |
} | |
heap_end += incr; | |
return (char *) prev_heap_end; | |
} | |
int _close(int file) | |
{ | |
return -1; | |
} | |
int _fstat(int file, struct stat *st) | |
{ | |
st‐>st_mode = S_IFCHR; | |
return 0; | |
} | |
int _isatty(int file) | |
{ | |
return 1; | |
} | |
int _lseek(int file, int ptr, int dir) | |
{ | |
return 0; | |
} | |
int _open(const char *name, int flags, int mode) | |
{ | |
return -1; | |
} | |
int _read(int file, char *ptr, int len) { | |
PDEV_DRIVER_DESC drvr; | |
drvr = device_table[file]; | |
if(drvr == 0) | |
return 0; | |
return drvr‐>read(ptr,len); | |
} | |
int _write(int file, char *ptr, int len) | |
{ | |
PDEV_DRIVER_DESC drvr; | |
drvr = device_table[file]; | |
if(drvr == 0) | |
return 0; | |
return drvr‐>write(ptr,len); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* keypad_driver.c | |
* MD407 library | |
*/ | |
/* declarations goes to 'libMD407.h' */ | |
#include "libMD407.h" | |
/* Define variables here */ | |
/* Define functions here */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* libMD407.c | |
* Declaration of library functions, constants etc... | |
*/ | |
/* declarations goes to 'libMD407.h' */ | |
#include "libMD407.h" | |
#include "usart_driver.c" | |
#include "keypad_driver.c" | |
#include "asciidisplay_driver.c" | |
/* Define variables here */ | |
static char *heap_end; | |
/*PDEV_DRIVER_DESC device_table[MAX_FILENO] = | |
{ | |
&StdIn, | |
&StdOut, | |
&StdErr, | |
&KeyPad, | |
&AsciiDisplay, | |
};*/ | |
PDEV_DRIVER_DESC device_table[MAX_FILENO] = | |
{ | |
0, | |
0, | |
0, | |
0, | |
&AsciiDisplay, | |
}; | |
/* Define functions here */ | |
void crt_init() { | |
extern char __bss_start; | |
extern char __bss_end; | |
extern char __heap_low; | |
extern char __heap_top; | |
char *s; | |
s = &__bss_start; | |
while(s < &__bss_end) | |
*s++ = 0; | |
s = &__heap_low; | |
while(s < &__heap_top) | |
*s++ = 0; | |
for( int i = 0; i < MAX_FILENO; i++ ) | |
{ | |
*PDEV_DRIVER_DESC fd = device_table[i]; | |
if(fd->init != 0) | |
(void) fd->init(0); | |
} | |
} | |
void crt_deinit(void) | |
{ | |
} | |
char* _sbrk( int incr) { | |
extern char __heap_low; | |
extern char __heap_top; | |
char *prev_heap_end; | |
if (heap_end == 0) { | |
heap_end = &__heap_low; | |
} | |
prev_heap_end = heap_end; | |
if (heap_end + incr > &__heap_top) { | |
/* Heap and stack collision */ | |
errno = ENOMEM; | |
return (char *)-1; | |
} | |
heap_end += incr; | |
return (char *) prev_heap_end; | |
} | |
int _close(int file) | |
{ | |
return -1; | |
} | |
int _fstat(int file, struct stat *st) | |
{ | |
st->st_mode = S_IFCHR; | |
return 0; | |
} | |
int _isatty(int file) | |
{ | |
return 1; | |
} | |
int _lseek(int file, int ptr, int dir) | |
{ | |
return 0; | |
} | |
int _open(const char *name, int flags, int mode) | |
{ | |
return -1; | |
} | |
int _read(int file, char *ptr, int len) { | |
PDEV_DRIVER_DESC drvr; | |
drvr = device_table[file]; | |
if(drvr == 0) | |
return 0; | |
return drvr->read(ptr,len); | |
} | |
int _write(int file, char *ptr, int len) | |
{ | |
PDEV_DRIVER_DESC drvr; | |
drvr = device_table[file]; | |
if(drvr == 0) | |
return 0; | |
return drvr->write(ptr,len); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* libNAME.h | |
* Declaration of library functions, constants etc... | |
*/ | |
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/stat.h> | |
#ifndef HEADER_H | |
#define HEADER_H | |
// Rest of header file contents go here | |
/* Type definitions */ | |
typedef struct | |
{ | |
char name[16]; | |
int (*init) (int); | |
void (*deinit) (int); | |
int (*fstat)(struct stat *st); | |
int (*isatty)(void); | |
int (*open)(const char name,int flags,int mode); | |
int (*close)(void); | |
int (*lseek)(int ptr, int dir); | |
int (*write)(char *ptr, int len); | |
int (*read)(char *ptr, int len); | |
} DEV_DRIVER_DESC, *PDEV_DRIVER_DESC; | |
/* Constants */ | |
#define MAX_FILENO 5 | |
/* Constant defined by linker */ | |
extern char __heap_low; | |
extern char __heap_top; | |
extern char __bss_start; | |
extern char __bss_end; | |
/* Library defined functions */ | |
void crt_init(void); | |
void crt_deinit(void); | |
char *_sbrk(int); | |
int _close(int file); | |
int _fstat(int file, struct stat *st); | |
int _isatty(int file); | |
int _lseek(int file, int ptr, int dir); | |
int _open(const char *name, int flags, int mode); | |
int _read(int file, char *ptr, int len); | |
int _write(int file, char *ptr, int len); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* usart_driver.c | |
* MD407 library | |
*/ | |
/* declarations goes in 'libMD407.h' */ | |
#include "libMD407.h" | |
/* Define functions here */ | |
void usart_init(int initval) { | |
} | |
void usart_deinit(int deinitval) { | |
} | |
void usart_isatty(void) { | |
} | |
int usart_write(char *ptr, int len) { | |
} | |
int usart_read(char *ptr, int len) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment