PIC24 Support Libraries
Functions
pic24_stdio_uart.c File Reference
#include "pic24_all.h"
#include <stdio.h>

Go to the source code of this file.

Functions

static int16_t stdioOpen (void)
 
int _LIBC_FUNCTION open (const char *name, int access, int mode)
 
int _LIBC_FUNCTION read (int handle, void *buffer, unsigned int len)
 
int _LIBC_FUNCTION write (int handle, void *buffer, unsigned int len)
 
int _LIBC_FUNCTION close (int handle)
 
long _LIBC_FUNCTION lseek (int handle, long offset, int origin)
 

Detailed Description

STDIO UART support functions

This file provided courtesy of David Weaver who has given permission for it to be included in the code archive.
Provides standard I/O functions compatible with our single character library functions. See chap10/reverse_string_stdio.c or chap10/stdio_test.c for examples using scanf().
See libc documentation for definitions of open(), close(), read(), write(), and lseek(). Exceptions and limitations imposed by this implementation are documented here.
Use of stdio functions is memory intensive especially for floating point!
fopen() access specifiers work as docummented in libc. Translation mode may be set seperately for reading and writing as in example 1. If using read+write access, remember to seek during transitions from write to read and read to write. Use fseek() or rewind(). See example 2.
Example 1:
file1 = fopen("uart1", "r"); // uart1 open for reading text using file1
file2 = fopen("uart1", "wb"); // uart1 open for writing binary using file2
fprintf(file2, "Enter string:\n"); // binary - no "\n" substituion allowed - see open()
fscanf(file1, "%s", buffer);
Example 2:
file3 = fopen("uart1", "r+"); // uart1 open for reading and writing text using file3
fseek(file3, 0, SEEK_END); // move to end of file for output - or use rewind()
fprintf(file3, "Enter string:\n"); // text - may substitute for "\n" - see open()
fseek(file3, 0, SEEK_SET); // move to start of file for input - or use rewind()
fscanf(file3, "%s", buffer);

Definition in file pic24_stdio_uart.c.

Function Documentation

◆ close()

int _LIBC_FUNCTION close ( int  handle)

Stub required by fclose().

Parameters
handlenot used.
Returns
SUCCESS.

Definition at line 369 of file pic24_stdio_uart.c.

◆ lseek()

long _LIBC_FUNCTION lseek ( int  handle,
long  offset,
int  origin 
)

Stub required by rewind() and fseek().

Parameters
handlenot used.
offsetnot used.
originnot used.
Returns
SUCCESS.

Definition at line 382 of file pic24_stdio_uart.c.

◆ open()

int _LIBC_FUNCTION open ( const char *  name,
int  access,
int  mode 
)

Initiate I/O on UART specified by name

Parameters
nameof file (UART) to open. Limited to "stdin", "stdout", "stderr", "uart1", "uart2", "uart3", and "uart4".
UART number specified by __C30_UART is reserved and can only be opened as stdin, stdout, and stderr.
accessis a bit field. Default, 0x0, is for binary read. Set it to include 0x4000 for character translation mode:
input - possible break on '\r' and '\n' - See SERIAL_BREAK_NL and SERIAL_BREAK_CR.
output - possible substitutions for '\n' - See SERIAL_EOL_CR and SERIAL_EOL_CR_LF.
If desired, OR it with 0x1 for write, or 0x2 for read and write.
modenot used
Returns
handle or FAIL.

Definition at line 215 of file pic24_stdio_uart.c.

◆ read()

int _LIBC_FUNCTION read ( int  handle,
void *  buffer,
unsigned int  len 
)

Input len characters from UART specified for handle to buffer. Uses mode specified via open(). If handle is for stdin, calls open() with character translation read access as needed.

Parameters
handlespecifies UART to read from.
bufferstorage for read characters.
lenmaximum number of characters to read.
Returns
number of charaters in buffer or FAIL.

Definition at line 295 of file pic24_stdio_uart.c.

◆ stdioOpen()

static int16_t stdioOpen ( void  )
static

Check __C30_UART for the UART to use. If set to 1, for example, then set up stdin, stdout, and stderr for UART1 and call configUART1() if not currently enabled.

Returns
SUCCESS or FAIL.

Definition at line 143 of file pic24_stdio_uart.c.

Referenced by open().

◆ write()

int _LIBC_FUNCTION write ( int  handle,
void *  buffer,
unsigned int  len 
)

Output len characters from buffer to UART specified for handle. Uses mode specified via open(). If handle is for stdout or stderr, calls open() with character translation write access as needed.

Parameters
handlespecifies UART to write to.
buffercontains characters to write.
lennumber of characters to write.
Returns
number of charaters written from buffer or FAIL.

Definition at line 333 of file pic24_stdio_uart.c.