Exemplo n.º 1
0
int usart_putc2(char c, FILE *stream)
{
    if (c == '\n')
        usart_putc('\r');
    usart_putc(c);
    return 0;
}
Exemplo n.º 2
0
void usart_puti(USART_INTEGER n)
{
    char str[10]; // Maximum length of string is 10
    int i = 0, j;

#ifdef USART_SIGNED_SUPPORT
    if(n < 0){
        usart_putc('-');
        n = -n;
    }
#endif

    while (n > 0) {
        str[i++] = n % 10 + '0';
        n /= 10;
    }

    for (j = i - 1; 0 <= j; j--) {
        usart_putc(str[j]);
    }

    if (i == 0) {
        usart_putc('0');
    }
}
Exemplo n.º 3
0
/* (Called from exc.S with global interrupts disabled.) */
void __error(int num) {
    nvic_globalirq_enable();
    usart_putstr(ERROR_USART, "\r\nexception: ");
    usart_putudec(ERROR_USART, num);
    usart_putc(ERROR_USART, '\n');
    usart_putc(ERROR_USART, '\r');
	for(;;);
    throb();
	
	/* Turn off peripheral interrupts */
    nvic_irq_disable_all();

    /* Turn off timers */
    timer_disable_all();

    /* Turn off ADC */
    adc_disable_all();

    /* Turn off all USARTs */
    usart_disable_all();

    /* Turn the USB interrupt back on so the bootloader keeps on functioning */
    nvic_irq_enable(NVIC_USB_HP_CAN_TX);
    nvic_irq_enable(NVIC_USB_LP_CAN_RX0);

    /* Reenable global interrupts */
    nvic_globalirq_enable();
    throb();
}
Exemplo n.º 4
0
void
IRQ_USART2(void)
{
	char t = usart_getc(&usartcfg);

	if (t == '\r')
		usart_putc(&usartcfg, '\n');

	usart_putc(&usartcfg, t);
}
Exemplo n.º 5
0
void usart_puts(const char *ptr)
{
	int i = 0;

	while (ptr[i] != '\0') {
		if (ptr[i] == '\n')
			usart_putc('\r');
		usart_putc(ptr[i]);
		i++;
	}
}
Exemplo n.º 6
0
void usart_puts(char *data) {
  int len, count;
  
  len = strlen(data);
  for (count = 0; count < len; count++) 
    usart_putc(*(data+count));
}
Exemplo n.º 7
0
void usart_putstr_P(PGM_P str)
{
    char ch;
    while((ch = pgm_read_byte(str++)) != '\0') {
        usart_putc(ch);
    }
}
Exemplo n.º 8
0
void usart_puts(char *dat)
{
  do {
    while( usart_busy() );
    usart_putc( *dat );
  } while( *dat++ );
}
Exemplo n.º 9
0
unsigned int usart_write(const char* c, unsigned int length)
{
  unsigned int written = 0;
  for (unsigned int i=0; i<length; i++)
    written += usart_putc(c[i]);
  return written;
}
Exemplo n.º 10
0
/**
 * @brief	Syscall for console write
 *
 * Used as an entry for newlib _write syscall. It automatically
 * adds '\r' along with each '\n'. By default, put all
 * characters to configured USART port.
 *
 * @param file	File descriptor (unused)
 * @param ptr	Message pointer
 * @param len	Length of the buffer
 *
 * @return Number of characters successfully printed.
 */
int
_write(int file, char *ptr, int len)
{
	int todo;

	if (file) {};

	for (todo = 0; todo < len; todo++) {
		usart_putc(&usartcfg, *ptr);
		/* Froce sending CR with new line */
		if (*ptr == '\n')
			usart_putc(&usartcfg, '\r');
		ptr++;
	}
	return len;
}
Exemplo n.º 11
0
unsigned int usart_print(const char* c)
{
  unsigned int written = 0;
  for (const char* i = c; *i != 0; i++)
    written += usart_putc(*i);
  return written;
}
Exemplo n.º 12
0
/* usart_puts(char s[])
 * Sends a string over the USART by repeatedly calling usart_putc() 
 */
void usart_puts(const char s[]) {
    int i = 0;
    while(i < USART_TXBUFFERSIZE) // don't get stuck if it is a bad string
    {
        if( s[i] == '\0' ) break; // quit on string terminator
        usart_putc(s[i++]);
    }
}
Exemplo n.º 13
0
/* usart_puts_p(const char *data)
 * Sends strings stored in flash memory to the usart.
 */
void usart_puts_p(const char *data_ptr) {
    uint8_t txdata = 1; // Dummy initialization value
    while ( txdata != 0x00 ) {
        txdata = pgm_read_byte(data_ptr);
        usart_putc(txdata);
        data_ptr++;
    }
}
Exemplo n.º 14
0
void usart_puthex(unsigned char ch)
{
    unsigned char up = (ch >> 4) & 0xf;
    unsigned char down = ch & 0xf;

    if (up < 10) {
        usart_putc(up + '0');
    } else {
        usart_putc(up + 'A' - 10);
    }

    if (down < 10) {
        usart_putc(down + '0');
    } else {
        usart_putc(down + 'A' - 10);
    }
}
Exemplo n.º 15
0
// write a program-memory string to usart
void usart_print_pstr(PGM_P pstr) {
    uint8_t i = 0;
    char c = pgm_read_byte(&(pstr[i]));

    while(c) {
	usart_putc(c);
	c = pgm_read_byte(&(pstr[++i]));
    }
}
Exemplo n.º 16
0
static void dbg_sync_putchar(char chr)
{
	if (chr == '\n')
		dbg_sync_putchar('\r');

	usart_putc(&console_uart, chr);
	while (!usart_status(&console_uart, USART_TC))
		/* wait */ ;
}
Exemplo n.º 17
0
static void
task_uart(void* unused)
{

	/* Blink LED and print dot mark each iteration */
	while (1) {
		vTaskDelay(1000);
		usart_putc(&usartcfg, '.');
	}
}
Exemplo n.º 18
0
/**
 * @brief Print an error message on a UART upon a failed assertion
 *        and throb the error LED, if there is one defined.
 * @param file Source file of failed assertion
 * @param line Source line of failed assertion
 * @param exp String representation of failed assertion
 * @sideeffect Turns of all peripheral interrupts except USB.
 */
void _fail(const char* file, int line, const char* exp) {
    /* Initialize the error USART */
    gpio_set_mode(ERROR_TX_PORT, ERROR_TX_PIN, GPIO_AF_OUTPUT_PP);
    usart_init(ERROR_USART);
    usart_set_baud_rate(ERROR_USART, ERROR_USART_CLK_SPEED, ERROR_USART_BAUD);

    /* Print failed assert message */
    usart_putstr(ERROR_USART, "ERROR: FAILED ASSERT(");
    usart_putstr(ERROR_USART, exp);
    usart_putstr(ERROR_USART, "): ");
    usart_putstr(ERROR_USART, file);
    usart_putstr(ERROR_USART, ": ");
    usart_putudec(ERROR_USART, line);
    usart_putc(ERROR_USART, '\n');
    usart_putc(ERROR_USART, '\r');

    /* Error fade */
    __error();
}
Exemplo n.º 19
0
// print an integer to usart
void usart_print_int(int32_t n) {
    if(n < 0) {
	usart_putc('-');
	n *= -1;
    }

    uint8_t print = 0;

    for(uint32_t order = 1000000000UL; order; order /= 10) {
	uint8_t digit = n / order;
	if(print || digit) {
	    usart_putc('0' + digit);
	    print = 1;
	}
	n %= order;
    }

    if(!print) {
	usart_putc('0');
    }
}
Exemplo n.º 20
0
void usart_printf(const char *ptr){

	// Send NULL-terminated data from FLASH.
	// Uses polling (and it blocks).

	char c;

	while(pgm_read_byte_near(ptr)) {
		c = pgm_read_byte_near(ptr++);
		usart_putc(c);
	}
}
Exemplo n.º 21
0
/** Write string.  In non-blocking mode this is likely to 
    ignore all but the first character.  */
int
usart_puts (usart_t usart, const char *str)
{
    while (*str)
    {
        int ret;
        
        ret = usart_putc (usart, *str++);
        if (ret < 1)
            return ret;
    }
    return 1;
}
Exemplo n.º 22
0
/**
 * @brief Transmit an unsigned integer to the specified serial port in
 *        decimal format.
 *
 * This function blocks until the integer's digits have been
 * completely transmitted.
 *
 * @param dev Serial port to send on
 * @param val Number to print
 */
void usart_putudec(usart_dev *dev, uint32 val) {
    char digits[12];

    int i = 0;
    do {
        digits[i++] = val % 10 + '0';
        val /= 10;
    } while (val > 0);

    while (--i >= 0) {
        usart_putc(dev, digits[i]);
    }
}
Exemplo n.º 23
0
// UART
void getDate(int *yy,int *mm, int*dd)
{
	char temp[5];
	int i;
	do {
	usart_prints("\nPlease Enter Year (yyyy):");
	for (i = 0; i <= 4; i++)
	{
		temp[i] = usart_getc();    // Get character
		usart_putc(temp[i]);       // Echo it back
	}
	temp[i] = '\0';
	sscanf(temp,"%d",yy);
	} while(*yy < 2016 || *yy > 2020);
	
	do {
	usart_prints("\nPlease Enter Month (mm):");
	for (i = 0; i <= 2; i++)
	{
		temp[i] = usart_getc();    // Get character
		usart_putc(temp[i]);       // Echo it back
	}
	temp[i] = '\0';
	sscanf(temp,"%d",mm);
	} while(*mm < 1 || *mm > 12);
	
	do {
	usart_prints("\nPlease Enter Day (dd):");
	for (i = 0; i <= 2; i++)
	{
		temp[i] = usart_getc();    // Get character
		usart_putc(temp[i]);       // Echo it back
	}
	temp[i] = '\0';
	sscanf(temp,"%d",dd);
	} while(*dd < 1 || *dd > 31);
	usart_prints("\n");
}
Exemplo n.º 24
0
int usart_write(char *buf, size_t buflen)
{
    int  i;
    int  cnt = 0;

    for (i=0; i < buflen; i++)
    {
        if (usart_putc(*(buf+i)) == -1)
            break;
    }

    return cnt;

}
Exemplo n.º 25
0
size_t usart_getstr_s(char* str, size_t len)
{
    uint16_t i;
    for (i = 0; i < len - 1; i++) {
        char ch = usart_getc();
        switch (ch) {
        case '\r':
            goto finish;
        case 0x1b:
            str -= i;
            i = 0;
            goto finish;
        default:
            usart_putc(ch);
            *str++ = ch;
            break;
        }
    }
  finish: *str = 0;
    usart_putc('\r');
    usart_putc('\n');
    return i;
}
Exemplo n.º 26
0
static void dbg_async_putchar(char chr)
{
	/* If UART is busy, try to put chr into queue until slot is freed,
	 * else write directly into UART
	 */
	if (!dbg_uart.ready) {
		while (queue_push(&(dbg_uart.tx), chr) != QUEUE_OK)
			/* wait */ ;
	}
	else {
		usart_putc(&console_uart, chr);
		dbg_uart.ready = 0;
		usart_config_interrupt(&console_uart, USART_IT_TXE, 1);
	}
}
Exemplo n.º 27
0
Arquivo: usart.c Projeto: checko/F4OS
int usart_puts(char *s, void *env) {
    int total = 0;
    while (*s) {
        int ret = usart_putc(*s++, env);
        if (ret > 0) {
            total += ret;
        }
        else {
            total = ret;
            break;
        }
    }

    return total;
}
Exemplo n.º 28
0
static void dbg_uart_send(int avail)
{
	uint8_t chr;

	if (avail) {
		if (!queue_is_empty(&(dbg_uart.tx))) {
			queue_pop(&(dbg_uart.tx), &chr);
			usart_putc(&console_uart, chr);
			dbg_uart.ready = 0;
			usart_config_interrupt(&console_uart, USART_IT_TXE, 1);
		}
		else {
			dbg_uart.ready = 1;
			usart_config_interrupt(&console_uart, USART_IT_TXE, 0);
		}
	}
}
Exemplo n.º 29
0
static void setupUSART(usart_dev *dev, uint32 baud)
{
    uint32 i = USART_RX_BUF_SIZE;
    /* FIXME: need some preprocess here, according to specific board */
    if (dev == USART1) {
        gpio_set_mode(GPIOA, 9, GPIO_AF_OUTPUT_PP);
        gpio_set_mode(GPIOA, 10, GPIO_INPUT_FLOATING);
    }
    usart_init(dev);
    usart_set_baud_rate(dev, 72000000UL, baud);
    usart_disable(dev);
    usart_enable(dev);
    /* flush buffer */
    while (i--) {
        usart_putc(dev, '\r');
    }
}
Exemplo n.º 30
0
int main(void) {
    os_init();
    os_semaphore_init(&lcd_sem, 1);
    os_semaphore_init(&btn_sem, 1);
    os_semaphore_init(&stt_sem, 1);
    os_semaphore_init(&tck_sem, 1);
    os_add_task(uart_task, &uart_task_stack[STACK_SIZE + 64], 1, "uart");
    os_add_task(adc_task, &adc_task_stack[STACK_SIZE + 64], 0, "adc");
    os_add_task(button_task, &button_task_stack[STACK_SIZE + 64], 2, "btn");
    os_start_ticker();
    
    os_semaphore_wait(&lcd_sem);
    lcd_init();
    os_semaphore_signal(&lcd_sem);
    
    i2c_init();
    
    usart_init(USART_TRANSMIT | USART_RECEIVE);
    usart_putc('\f');
    
    while(1) {
        char buff[6];
        os_delay(os_get_current_pid(), 2000);
        /*usart_puts("Start I2C\r\n");
        int8_t start_ = i2c_start();
        int8_t send_ = i2c_send_address(0xa0);
        i2c_stop();
        if (start_ != 0) {
            usart_puts("Start error\r\n");
            uint8_t_to_ascii((uint8_t) start_, &(buff[0]));
            usart_puts(buff);
            usart_puts("\r\n");
            
        }
        if (send_ != 0) {
            usart_puts("Send error\r\n");
            uint8_t_to_ascii((uint8_t) send_, &(buff[0]));
            usart_puts(buff);
            usart_puts("\r\n");
        }
        usart_puts("Stop I2C\r\n\r\n");*/
    }
    
    return 0;
}