Skip to content

Fixed IAR serial fgets fgetc #770

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

Merged
merged 2 commits into from
Dec 9, 2014
Merged
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
4 changes: 4 additions & 0 deletions libraries/mbed/api/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

namespace mbed {

extern void mbed_set_unbuffered_stream(FILE *_file);
extern int mbed_getc(FILE *_file);
extern char* mbed_gets(char *s, int size, FILE *_file);

class Stream : public FileLike {

public:
Expand Down
6 changes: 3 additions & 3 deletions libraries/mbed/common/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Stream::Stream(const char *name) : FileLike(name), _file(NULL) {
char buf[12]; /* :0x12345678 + null byte */
std::sprintf(buf, ":%p", this);
_file = std::fopen(buf, "w+");
setbuf(_file, NULL);
mbed_set_unbuffered_stream(_file);
}

Stream::~Stream() {
Expand All @@ -41,11 +41,11 @@ int Stream::puts(const char *s) {
}
int Stream::getc() {
fflush(_file);
return std::fgetc(_file);
return mbed_getc(_file);
}
char* Stream::gets(char *s, int size) {
fflush(_file);
return std::fgets(s,size,_file);
return mbed_gets(s,size,_file);
}

int Stream::close() {
Expand Down
52 changes: 52 additions & 0 deletions libraries/mbed/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,55 @@ extern "C" caddr_t _sbrk(int incr) {
return (caddr_t) prev_heap;
}
#endif


namespace mbed {

void mbed_set_unbuffered_stream(FILE *_file) {
#if defined (__ICCARM__)
char buf[2];
std::setvbuf(_file,buf,_IONBF,NULL);
#else
setbuf(_file, NULL);
#endif
}

int mbed_getc(FILE *_file){
#if defined (__ICCARM__)
/*This is only valid for unbuffered streams*/
int res = std::fgetc(_file);
if (res>=0){
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
_file->_Rend = _file->_Wend;
_file->_Next = _file->_Wend;
}
return res;
#else
return std::fgetc(_file);
#endif
}

char* mbed_gets(char*s, int size, FILE *_file){
#if defined (__ICCARM__)
/*This is only valid for unbuffered streams*/
char *str = fgets(s,size,_file);
if (str!=NULL){
_file->_Mode = (unsigned short)(_file->_Mode & ~ 0x1000);/* Unset read mode */
_file->_Rend = _file->_Wend;
_file->_Next = _file->_Wend;
}
return str;
#else
return std::fgets(s,size,_file);
#endif
}

} // namespace mbed