Thanks for the answer, I tried to use the variable to avoid having that problem, however it didn't change anything, when I try to send data it gets struck. I just print the value every loop. Updated code:
/*
* Copyright (c) 2015, Alex Taradov <alex@taradov.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "samd21.h"
#include "hal_gpio.h"
HAL_GPIO_PIN(UART_TX, A, 22)
HAL_GPIO_PIN(UART_RX, A, 23)
#define DelayTicks(ticks) {volatile uint32_t n=ticks; while(n--);} //takes 8 cycles
#define DelayMs(ms) DelayTicks(MS_TO_DLYTICKS(ms))
#define F_CPU 8000000
#define CYCLES_IN_DLYTICKS_FUNC 8
#define MS_TO_DLYTICKS(ms) (F_CPU / (uint64_t)1000 * ms / CYCLES_IN_DLYTICKS_FUNC) // ((float)(F_CPU)) / 1000.0
uint16_t rxData;
static void uart_init(uint32_t baud)
{
uint64_t br = (uint64_t)65536 * (F_CPU - 16 * baud) / F_CPU;
HAL_GPIO_UART_TX_out();
HAL_GPIO_UART_TX_pmuxen(PORT_PMUX_PMUXE_C_Val);
HAL_GPIO_UART_RX_in();
HAL_GPIO_UART_RX_pmuxen(PORT_PMUX_PMUXE_C_Val);
PM->APBCMASK.reg |= PM_APBCMASK_SERCOM3;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(SERCOM3_GCLK_ID_CORE) | GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
SERCOM3->USART.CTRLA.reg = SERCOM_USART_CTRLA_DORD | SERCOM_USART_CTRLA_MODE_USART_INT_CLK | SERCOM_USART_CTRLA_RXPO(1/*PAD1*/) | SERCOM_USART_CTRLA_TXPO(0/*PAD0*/);
SERCOM3->USART.CTRLB.reg = SERCOM_USART_CTRLB_RXEN | SERCOM_USART_CTRLB_TXEN | SERCOM_USART_CTRLB_CHSIZE(0/*8 bits*/);
while (SERCOM3->USART.SYNCBUSY.bit.CTRLB) ;
SERCOM3->USART.INTENSET.reg = SERCOM_USART_INTENSET_RXC;
NVIC_DisableIRQ(SERCOM3_IRQn);
NVIC_ClearPendingIRQ(SERCOM3_IRQn);
NVIC_SetPriority(SERCOM3_IRQn, 0);
NVIC_EnableIRQ(SERCOM3_IRQn);
SERCOM3->USART.BAUD.reg = (uint16_t)br;
SERCOM3->USART.CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE;
}
static void uart_putc(char c)
{
while (!(SERCOM3->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_DRE));
SERCOM3->USART.DATA.reg = c;
}
static void uart_puts(char *s)
{
while (*s)
uart_putc(*s++);
}
static void sys_init(void)
{
// Switch to 8MHz clock (disable prescaler)
SYSCTRL->OSC8M.bit.PRESC = 0;
}
void SERCOM3_Handler()
{
uart_puts("I");
if (SERCOM3->USART.INTFLAG.bit.RXC)
{
// Got a character
rxData = SERCOM3->USART.DATA.reg;
return;
}
}
int main(void)
{
sys_init();
uart_init(115200);
uart_puts("\r\nHello, world!\r\n");
while (1)
{
uart_puts("test\r\n");
char tmp[30];
itoa(rxData, tmp, 10);
uart_puts("\r\ndata received ");
uart_puts(tmp);
uart_puts("\r\n");
DelayMs(1000);
}
return 0;
}