Ejemplo n.º 1
0
int _write(int fd, const void *buf, size_t count)
{
    int sent;
    char *ptr;

    if(fd <= 2){
        sent = count;
        ptr = (char *) buf;

        while(count > 0){
            if(*ptr == '\n'){
                usart_send_blocking(STD_CON, '\r');
            }
            usart_send_blocking(STD_CON, *ptr++);
            ++sent;
            --count;
        }

    }else{
        errno = EIO;
        sent = -1;
    }

    return sent;
}
Ejemplo n.º 2
0
void BT_Test()
{
    usleep(1000000);
    PORT_pin_set(BT_KEY);
    usleep(100000);
    BT_ResetPtr();
    usart_send_blocking(BTUART, 'A');
    usart_send_blocking(BTUART, 'T');
    usart_send_blocking(BTUART, '\r');
    usart_send_blocking(BTUART, '\n');
    while(1) {
        char tmp[32];
        while(ptr == bt_buf || *(ptr-1) != '\n') {
        }
        int len = ptr - bt_buf;
        memcpy(tmp, (char *)bt_buf, len);
        BT_ResetPtr();
        tmp[len] = 0;
        printf("%s", tmp);
        if((tmp[0] == 'O' && tmp[1] == 'K') || (tmp[0] == 'F' && tmp[1] == 'A')) {
            printf("CmdDone\n");
            break;
        }
    }
    PORT_pin_clear(BT_KEY);
    usleep(150000);
}
Ejemplo n.º 3
0
void
uart_cinit(void *config)
{
	usart = (uint32_t)config;

	/* board is expected to do pin and clock setup */

	/* do usart setup */
	//USART_CR1(usart) |= (1 << 15);	/* because libopencm3 doesn't know the OVER8 bit */
	usart_set_baudrate(usart, 115200);
	usart_set_databits(usart, 8);
	usart_set_stopbits(usart, USART_STOPBITS_1);
	usart_set_mode(usart, USART_MODE_TX_RX);
	usart_set_parity(usart, USART_PARITY_NONE);
	usart_set_flow_control(usart, USART_FLOWCONTROL_NONE);

	/* and enable */
	usart_enable(usart);


#if 0
	usart_send_blocking(usart, 'B');
	usart_send_blocking(usart, 'B');
	usart_send_blocking(usart, 'B');
	usart_send_blocking(usart, 'B');

	while (true) {
		int c;
		c = usart_recv_blocking(usart);
		usart_send_blocking(usart, c);
	}

#endif
}
Ejemplo n.º 4
0
int _write_r (struct _reent *r, int fd, char * ptr, int len)
{  
    (void)r;
    if(fd==1 || fd==2) {
        int index;

        if (0 == (USART_CR1(USART1) & USART_CR1_UE))
            return len; //Don't send if USART is disabled

        for(index=0; index<len; index++) {
            if (ptr[index] == '\n') {
                usart_send_blocking(USART1,'\r');
            }  
            usart_send_blocking(USART1, ptr[index]);
        }    
        return len;
    } else if(fd>2) {
        if(file_open[fd-3]) {
#ifdef MEDIA_DRIVE
            _drive_num = fd == 3 ? 0 : 1;
#endif
            WORD bytes_written;
            pf_switchfile(&fat[fd-3]);
            int res=pf_write(ptr, len, &bytes_written);
            dbgprintf("_write_r: len %d, bytes_written %d, result %d\r\n",len, bytes_written, res);
            if(res==FR_OK) return bytes_written;
        }
    }
    errno=EINVAL;
    return -1;
}
/* back up the cursor one space */
static inline void back_up(void)
{
	end_ndx = dec_ndx(end_ndx);
	usart_send_blocking(USART2, '\010');
	usart_send_blocking(USART2, ' ');
	usart_send_blocking(USART2, '\010');
}
Ejemplo n.º 6
0
void bt_update_characteristic(uint8_t *data, uint8_t len, const char *uuid)
{
	bt_last_query = UPDATE;
	uint8_t i,d;
	bt_send_string(B_USART,"SUW,");
	bt_send_string(B_USART,uuid);
	bt_send_string(B_USART,",");
	for (i=0; i < len; i++)
	{
		d = (data[i] & 0xF0) >> 4;
		if (d <= 9)
			d += 48;
		else
			d+= 55;
		usart_send_blocking(B_USART, d);
		d = data[i] & 0xF;
		if (d <= 9)
			d += 48;
		else
			d+= 55;
		usart_send_blocking(B_USART, d);
	}
	bt_send_string(B_USART,"\r\n");
	bt_query_result = 0;
	waiting_for_response = 1;
}
Ejemplo n.º 7
0
static void my_usart_print_int(uint32_t usart, int32_t value)
{
	int8_t i;
	int8_t nr_digits = 0;
	char buffer[25];

	if (value < 0) {
		usart_send_blocking(usart, '-');
		value = value * -1;
	}

	if (value == 0) {
		usart_send_blocking(usart, '0');
	}

	while (value > 0) {
		buffer[nr_digits++] = "0123456789"[value % 10];
		value /= 10;
	}

	for (i = nr_digits-1; i >= 0; i--) {
		usart_send_blocking(usart, buffer[i]);
	}

	usart_send_blocking(usart, '\r');
	usart_send_blocking(usart, '\n');
}
void send_USART_str(const unsigned char* in)
{
    int i;
    for(i = 0; in[i] != 0; i++) {
        usart_send_blocking(USART2, in[i]);
    }
    usart_send_blocking(USART2, '\r');
    usart_send_blocking(USART2, '\n');
}
Ejemplo n.º 9
0
int main(void)
{
	int i, j = 0, c = 0;

	clock_setup();
	gpio_setup();
	usart_setup();

	/* Blink the LED (PE10) on the board with every transmitted byte. */
	while (1) {
		gpio_toggle(GPIO_PE10);			/* LED on/off */
		usart_send_blocking(USART1, c + '0');	/* USART1: Send byte. */
		usart_send_blocking(USART2, c + '0');	/* USART2: Send byte. */
		usart_send_blocking(USART3, c + '0');	/* USART3: Send byte. */
		c = (c == 9) ? 0 : c + 1;	/* Increment c. */
		if ((j++ % 80) == 0) {	/* Newline after line full. */
			usart_send_blocking(USART1, '\r');
			usart_send_blocking(USART1, '\n');
			usart_send_blocking(USART2, '\r');
			usart_send_blocking(USART2, '\n');
			usart_send_blocking(USART3, '\r');
			usart_send_blocking(USART3, '\n');
		}
		for (i = 0; i < 800000; i++)	/* Wait a bit. */
			__asm__ ("nop");
	}

	return 0;
}
Ejemplo n.º 10
0
static void mandel(float cX, float cY, float scale)
{
	int x, y;
	for (x = -60; x < 60; x++) {
		for (y = -50; y < 50; y++) {
			int i = iterate(cX + x*scale, cY + y*scale);
			usart_send_blocking(USART2, color[i]);
		}
		usart_send_blocking(USART2, '\r');
		usart_send_blocking(USART2, '\n');
	}
}
Ejemplo n.º 11
0
void print(const char *s)
{

    // loop through entire string
    while (*s) {
    	if ( *s == '\n') {
        usart_send_blocking(USART1,'\r');
        //usart_send_blocking(USART1,'\n');
    	}
    	usart_send_blocking(USART1,*s);
        s++;
    }
}
int main(void)
{
	uint8_t channel_array[16];
	uint16_t temperature = 0;

	rcc_clock_setup_in_hse_12mhz_out_72mhz();
	gpio_setup();
	usart_setup();
	timer_setup();
	adc_setup();

	gpio_set(GPIOA, GPIO8);	                /* LED1 on */
	gpio_set(GPIOC, GPIO15);		/* LED2 on */

	/* Send a message on USART1. */
	usart_send_blocking(USART2, 's');
	usart_send_blocking(USART2, 't');
	usart_send_blocking(USART2, 'm');
	usart_send_blocking(USART2, '\r');
	usart_send_blocking(USART2, '\n');

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 16;
	/* Set the injected sequence here, with number of channels */
	adc_set_injected_sequence(ADC1, 1, channel_array);

	/* Continously convert and poll the temperature ADC. */
	while (1) {
		/*
		 * Since the injected sampling is triggered by the timer, it gets
		 * updated automatically, we just need to periodically read out the value.
		 * It would be better to check if the JEOC bit is set, and clear it following
		 * so that you do not read the same value twice, especially for a slower
		 * sampling rate.
		 */

		temperature = adc_read_injected(ADC1,1); //get the result from ADC_JDR1 on ADC1 (only bottom 16bits)

		/*
		 * That's actually not the real temperature - you have to compute it
		 * as described in the datasheet.
		 */
		my_usart_print_int(USART2, temperature);

		gpio_toggle(GPIOA, GPIO8); /* LED2 on */

	}

	return 0;
}
Ejemplo n.º 13
0
void printstr(char * ptr, int len)
{
    int index;

    if (0 == (USART_CR1(_USART) & USART_CR1_UE))
        return; //Don't send if USART is disabled

    for(index=0; index<len; index++) {
        if (ptr[index] == '\n') {
            usart_send_blocking(_USART,'\r');
        }  
        usart_send_blocking(_USART, ptr[index]);
    }    
    return;
}
Ejemplo n.º 14
0
int _write(int file, char *ptr, int len) {
	int i;

	if (file == STDOUT_FILENO || file == STDERR_FILENO) {
		for (i = 0; i < len; i++) {
			if (ptr[i] == '\n') {
				usart_send_blocking(USART1, '\r');
			}
			usart_send_blocking(USART1, ptr[i]);
		}
		return i;
	}
	errno = EIO;
	return -1;
}
Ejemplo n.º 15
0
int _write(int file, char *ptr, int len)
{
	int i;

	if (file == 1) {
		for (i = 0; i < len; i++) {
			if (ptr[i] == '\n') {
				usart_send_blocking(USART2, '\r');
			}
			usart_send_blocking(USART2, ptr[i]);
		}
		return i;
	}
	errno = EIO;
	return -1;
}
Ejemplo n.º 16
0
void uart7_cout(uint32_t whichUsart,uint8_t *buf,unsigned len)
{
	while (len--) {
			usart_send_blocking(whichUsart, *buf++);
			while(usart_get_flag(UART7, USART_SR_TC) !=1);
		}
}
Ejemplo n.º 17
0
void puts(const unsigned char *s)
{
    while(*s) {
        usart_send_blocking(USART1,*s);
        s++;
    }
}
Ejemplo n.º 18
0
 void handleEnd() {
   // make sure write finishes
   usart_send_blocking(USART1, 0);
   usart_wait_send_ready(USART1);
   
   gpio_clear(GPIOB, GPIO5);
 }
Ejemplo n.º 19
0
static int command(char *command, size_t length)
{
    size_t i;

    calculate_crc_and_ack(command, length);

    for (i = 0; i < length; i++)
        usart_send_blocking(USART2, command[i]);

    timer_set_counter(TIM5, 0);
    timer_clear_flag(TIM5, TIM_SR_UIF);
    timer_enable_counter(TIM5);

    for (i = 0; i < sizeof(expect_ack); )
    {
        while (!timer_get_flag(TIM5, TIM_SR_UIF) &&
               !usart_get_flag(USART2, USART_SR_RXNE));

        if (timer_get_flag(TIM5, TIM_SR_UIF))
            break;

        if (expect_ack[i] != usart_recv(USART2))
            break;
    }

    timer_disable_counter(TIM5);

    return (i == sizeof(expect_ack));
}
void send_USART_bytes(const unsigned char* in, int n)
{
    int i;
    for(i = 0; i < n; i++) {
        usart_send_blocking(USART2, in[i]);
    }
}
Ejemplo n.º 21
0
void
uart_cout(uint8_t *buf, unsigned len)
{
	while (len--) {
		usart_send_blocking(usart, *buf++);
	}
}
/*
 * A buffered line editing function.
 */
void
get_buffered_line(void) {
	char	c;

	if (start_ndx != end_ndx) {
		return;
	}
	while (1) {
		c = usart_recv_blocking(USART2);
		if (c == '\r') {
			buf[end_ndx] = '\n';
			end_ndx = inc_ndx(end_ndx);
			buf[end_ndx] = '\0';
			usart_send_blocking(USART2, '\r');
			usart_send_blocking(USART2, '\n');
			return;
		}
		/* ^H or DEL erase a character */
		if ((c == '\010') || (c == '\177')) {
			if (buf_len == 0) {
				usart_send_blocking(USART2, '\a');
			} else {
				back_up();
			}
		/* ^W erases a word */
		} else if (c == 0x17) {
			while ((buf_len > 0) &&
					(!(isspace((int) buf[end_ndx])))) {
				back_up();
			}
		/* ^U erases the line */
		} else if (c == 0x15) {
			while (buf_len > 0) {
				back_up();
			}
		/* Non-editing character so insert it */
		} else {
			if (buf_len == (BUFLEN - 1)) {
				usart_send_blocking(USART2, '\a');
			} else {
				buf[end_ndx] = c;
				end_ndx = inc_ndx(end_ndx);
				usart_send_blocking(USART2, c);
			}
		}
	}
}
Ejemplo n.º 23
0
int main(void)
{
	u8 channel_array[16];

	rcc_clock_setup_in_hse_12mhz_out_72mhz();
	gpio_setup();
	usart_setup();
	timer_setup();
	irq_setup();
	adc_setup();

	gpio_set(GPIOA, GPIO8);	                /* LED1 on */
	gpio_set(GPIOC, GPIO15);		/* LED2 on */

	/* Send a message on USART1. */
	usart_send_blocking(USART2, 's');
	usart_send_blocking(USART2, 't');
	usart_send_blocking(USART2, 'm');
	usart_send_blocking(USART2, '\r');
	usart_send_blocking(USART2, '\n');

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 16;
	/* Set the injected sequence here, with number of channels */
	adc_set_injected_sequence(ADC1, 1, channel_array);

	/* Continously convert and poll the temperature ADC. */
	while (1) {
		/*
		 * Since sampling is triggered by the timer and copying the value
		 * out of the data register is handled by the interrupt routine,
		 * we just need to print the value and toggle the LED. It may be useful
		 * to buffer the adc values in some cases.
		 */

		/*
		 * That's actually not the real temperature - you have to compute it
		 * as described in the datasheet.
		 */
		my_usart_print_int(USART2, temperature);

		gpio_toggle(GPIOA, GPIO8); /* LED2 on */

	}

	return 0;
}
/** Run the terminal */
void term_Run()
{
   char args[TERM_BUFSIZE];
   const TERM_CMD *pCurCmd = NULL;
   int lastIdx = 0;

   while (1)
   {
      int currentIdx = TERM_BUFSIZE - TERM_USART_CNDTR;

      if (0 == TERM_USART_CNDTR)
         ResetDMA();

      while (lastIdx < currentIdx) //echo
         usart_send_blocking(TERM_USART, inBuf[lastIdx++]);

      if (currentIdx > 0)
      {
         if (inBuf[currentIdx - 1] == '\n' || inBuf[currentIdx - 1] == '\r')
         {
            inBuf[currentIdx] = 0;
            lastIdx = 0;
            char *space = (char*)my_strchr(inBuf, ' ');

            if (0 == *space) //No args after command, look for end of line
            {
               space = (char*)my_strchr(inBuf, '\n');
               args[0] = 0;
            }
            else //There are arguments, copy everything behind the space
            {
               my_strcpy(args, space + 1);
            }

            if (0 == *space) //No \n found? try \r
               space = (char*)my_strchr(inBuf, '\r');

            *space = 0;
            pCurCmd = CmdLookup(inBuf);
            ResetDMA();

            if (NULL != pCurCmd)
            {
               pCurCmd->CmdFunc(args);
            }
            else if (currentIdx > 1)
            {
               term_send(TERM_USART, "Unknown command sequence\r\n");
            }
         }
         else if (inBuf[0] == '!' && NULL != pCurCmd)
         {
            ResetDMA();
            lastIdx = 0;
            pCurCmd->CmdFunc(args);
         }
      }
   } /* while(1) */
} /* term_Run */
Ejemplo n.º 25
0
static void usart_puts(uint32_t usart, const char *s)
{
    while(*s != '\0')
    {
        usart_send_blocking(usart, *s);
        s++;
    }
}
Ejemplo n.º 26
0
static void usart_send_string(u32 usart, u8 *string, u16 str_size)
{
	u16 iter = 0;
	do
	{
		usart_send_blocking(usart, string[iter++]);
	}while(string[iter] != 0 && iter < str_size);
}
Ejemplo n.º 27
0
void _debug_send(const char *data)
{
    while (*data)
    {
        usart_send_blocking(USART1, *data);
        data++;
    }
}
Ejemplo n.º 28
0
void send_string_blocking(char * string)
{
	u16 len = strlen(string);
	u16 i;
	for(i = 0; i < len; i++)
	{
		usart_send_blocking(USART, string[i]);
	}
}
Ejemplo n.º 29
0
int _write(int file, char *ptr, int len) {
    int i;
    if (file == 1) {
	for (i = 0; i < len; i++)
	    usart_send_blocking(USART1, ptr[i]);
	return i;
    }
    errno = EIO;
    return -1;
}
Ejemplo n.º 30
0
/**
 *  write_std_err - writes errors to the UART specified in config.h
 */
int write_std_err(const void *buffer, int len) {
  int c = len;
  char *cp = (char *)buffer;
  
  while(c--) {
    usart_send_blocking(STD_ERR_UART, *cp++);
  }

  return len;
}