mirror of
https://github.com/kelvinlawson/atomthreads.git
synced 2026-01-11 18:33:16 +01:00
* Split assembler routines into -cosmic.s and -iar.s. * Split Cosmic and IAR specific code using __CSMC__ and __IAR_SYSTEMS_ICC__. * Context-switch virtual registers on IAR. * Add detailed description of IAR context-switch scheme. * Add sample IAR project. * Add support for IAR to STM8S peripheral driver modules. * Fix EWSTM8 compiler warnings.
139 lines
2.7 KiB
C
139 lines
2.7 KiB
C
#include <stdio.h>
|
|
#include <stddef.h>
|
|
|
|
#include "stm8s.h"
|
|
|
|
#include "atom.h"
|
|
#include "atommutex.h"
|
|
#include "uart.h"
|
|
|
|
|
|
/*
|
|
* Semaphore for single-threaded access to UART device
|
|
*/
|
|
static ATOM_MUTEX uart_mutex;
|
|
|
|
|
|
/*
|
|
* Initialize the UART to requested baudrate, tx/rx, 8N1.
|
|
*/
|
|
int uart_init(uint32_t baudrate)
|
|
{
|
|
int status;
|
|
|
|
/**
|
|
* Set up UART2 for putting out debug messages.
|
|
* This the UART used on STM8S Discovery, change if required.
|
|
*/
|
|
UART2_DeInit();
|
|
UART2_Init (baudrate, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO,
|
|
UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);
|
|
|
|
/* Create a mutex for single-threaded putchar() access */
|
|
if (atomMutexCreate (&uart_mutex) != ATOM_OK)
|
|
{
|
|
status = -1;
|
|
}
|
|
else
|
|
{
|
|
status = 0;
|
|
}
|
|
|
|
/* Finished */
|
|
return (status);
|
|
}
|
|
|
|
|
|
/**
|
|
* \b uart_putchar
|
|
*
|
|
* Write a char out via UART2
|
|
*
|
|
* @param[in] c Character to send
|
|
*
|
|
* @return Character sent
|
|
*/
|
|
char uart_putchar (char c)
|
|
{
|
|
/* Block on private access to the UART */
|
|
if (atomMutexGet(&uart_mutex, 0) == ATOM_OK)
|
|
{
|
|
/* Convert \n to \r\n */
|
|
if (c == '\n')
|
|
putchar('\r');
|
|
|
|
/* Write a character to the UART2 */
|
|
UART2_SendData8(c);
|
|
|
|
/* Loop until the end of transmission */
|
|
while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET)
|
|
;
|
|
|
|
/* Return mutex access */
|
|
atomMutexPut(&uart_mutex);
|
|
|
|
}
|
|
|
|
return (c);
|
|
}
|
|
|
|
|
|
/* COSMIC: Requires putchar() routine to override stdio */
|
|
#if defined(__CSMC__)
|
|
/**
|
|
* \b putchar
|
|
*
|
|
* Retarget putchar() to use UART2
|
|
*
|
|
* @param[in] c Character to send
|
|
*
|
|
* @return Character sent
|
|
*/
|
|
char putchar (char c)
|
|
{
|
|
return (uart_putchar(c));
|
|
}
|
|
#endif /* __CSMC__ */
|
|
|
|
|
|
/* IAR: Requires __write() routine to override stdio */
|
|
#if defined(__IAR_SYSTEMS_ICC__)
|
|
/**
|
|
* \b __write
|
|
*
|
|
* Override for IAR stream output
|
|
*
|
|
* @param[in] handle Stdio handle. -1 to flush.
|
|
* @param[in] buf Pointer to buffer to be written
|
|
* @param[in] bufSize Number of characters to be written
|
|
*
|
|
* @return Number of characters sent
|
|
*/
|
|
size_t __write(int handle, const unsigned char *buf, size_t bufSize)
|
|
{
|
|
size_t chars_written = 0;
|
|
|
|
/* Ignore flushes */
|
|
if (handle == -1)
|
|
{
|
|
chars_written = (size_t)0;
|
|
}
|
|
/* Only allow stdout/stderr output */
|
|
else if ((handle != 1) && (handle != 2))
|
|
{
|
|
chars_written = (size_t)-1;
|
|
}
|
|
/* Parameters OK, call the low-level character output routine */
|
|
else
|
|
{
|
|
while (chars_written < bufSize)
|
|
{
|
|
uart_putchar (buf[chars_written]);
|
|
chars_written++;
|
|
}
|
|
}
|
|
|
|
return (chars_written);
|
|
}
|
|
#endif /* __IAR_SYSTEMS_ICC__ */
|