I tried this bare minimum example first but I still don't get anything when I run the scanner. I understand that it ignores any received bytes so maybe that's the reason. But I tried putting a print statement in the I2C_SERCOM_IRQ_HANDLER and it never gets printed. I used your samd21 starter to set up the UART, here is the code even tho it's basically your UART example with the copy pasted minimum example. But maybe I did something silly when putting them together.
/*
* 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"
//-----------------------------------------------------------------------------
#define PERIOD_FAST 100
#define PERIOD_SLOW 500
HAL_GPIO_PIN(UART_TX, B, 22)
HAL_GPIO_PIN(UART_RX, B, 23)
//-----------------------------------------------------------------------------
HAL_GPIO_PIN(SDA, A, 8)
HAL_GPIO_PIN(SCL, A, 9)
#define I2C_BASE_ADDRESS 0x52// 8-bit address
#define I2C_SERCOM SERCOM0
#define I2C_SERCOM_PMUX HAL_GPIO_PMUX_C
#define I2C_SERCOM_APBCMASK PM_APBCMASK_SERCOM0
#define I2C_SERCOM_GCLK_ID SERCOM0_GCLK_ID_CORE
#define I2C_SERCOM_IRQ_HANDLER irq_handler_sercom0
#define I2C_SERCOM_IRQ SERCOM0_IRQn
#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) (uint32_t)(F_CPU / 1000 * ms / CYCLES_IN_DLYTICKS_FUNC) // ((float)(F_CPU)) / 1000.0
//-----------------------------------------------------------------------------
static void timer_set_period(uint16_t i)
{
TC3->COUNT16.CC[0].reg = (F_CPU / 1000ul / 256) * i;
TC3->COUNT16.COUNT.reg = 0;
}
//-----------------------------------------------------------------------------
void irq_handler_tc3(void)
{
if (TC3->COUNT16.INTFLAG.reg & TC_INTFLAG_MC(1))
{
TC3->COUNT16.INTFLAG.reg = TC_INTFLAG_MC(1);
}
}
//-----------------------------------------------------------------------------
static void timer_init(void)
{
PM->APBCMASK.reg |= PM_APBCMASK_TC3;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(TC3_GCLK_ID) |
GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
TC3->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | TC_CTRLA_WAVEGEN_MFRQ |
TC_CTRLA_PRESCALER_DIV256 | TC_CTRLA_PRESCSYNC_RESYNC;
TC3->COUNT16.COUNT.reg = 0;
timer_set_period(PERIOD_SLOW);
TC3->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE;
TC3->COUNT16.INTENSET.reg = TC_INTENSET_MC(1);
NVIC_EnableIRQ(TC3_IRQn);
}
//-----------------------------------------------------------------------------
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_D_Val);
HAL_GPIO_UART_RX_in();
HAL_GPIO_UART_RX_pmuxen(PORT_PMUX_PMUXE_D_Val);
PM->APBCMASK.reg |= PM_APBCMASK_SERCOM5;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(SERCOM5_GCLK_ID_CORE) |
GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
SERCOM5->USART.CTRLA.reg =
SERCOM_USART_CTRLA_DORD | SERCOM_USART_CTRLA_MODE_USART_INT_CLK |
SERCOM_USART_CTRLA_RXPO(3/*PAD1*/) | SERCOM_USART_CTRLA_TXPO(1/*PAD0*/);
SERCOM5->USART.CTRLB.reg = SERCOM_USART_CTRLB_RXEN | SERCOM_USART_CTRLB_TXEN |
SERCOM_USART_CTRLB_CHSIZE(0/*8 bits*/);
SERCOM5->USART.BAUD.reg = (uint16_t)br;
SERCOM5->USART.CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE;
}
//-----------------------------------------------------------------------------
static void uart_putc(char c)
{
while (!(SERCOM5->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_DRE));
SERCOM5->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;
}
static void i2c_init(void)
{
HAL_GPIO_SDA_pmuxen(I2C_SERCOM_PMUX);
HAL_GPIO_SCL_pmuxen(I2C_SERCOM_PMUX);
PM->APBCMASK.reg |= I2C_SERCOM_APBCMASK;
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(I2C_SERCOM_GCLK_ID) | GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(0);
I2C_SERCOM->I2CS.ADDR.reg = I2C_BASE_ADDRESS;
I2C_SERCOM->I2CS.CTRLB.reg = SERCOM_I2CS_CTRLB_SMEN;
I2C_SERCOM->I2CS.CTRLA.reg = SERCOM_I2CS_CTRLA_ENABLE | SERCOM_I2CS_CTRLA_MODE_I2C_SLAVE;
I2C_SERCOM->I2CS.INTENSET.reg = SERCOM_I2CS_INTENCLR_PREC | SERCOM_I2CS_INTENCLR_AMATCH |
SERCOM_I2CS_INTENCLR_DRDY;
NVIC_EnableIRQ(I2C_SERCOM_IRQ);
}
//-----------------------------------------------------------------------------
void I2C_SERCOM_IRQ_HANDLER(void)
{
uart_puts("\r\n Interrupt!\r\n");
int flags;
flags = I2C_SERCOM->I2CS.INTFLAG.reg;
if (flags & SERCOM_I2CS_INTFLAG_AMATCH)
{
I2C_SERCOM->I2CS.CTRLB.reg &= ~SERCOM_I2CM_CTRLB_ACKACT;
I2C_SERCOM->I2CS.CTRLB.reg |= SERCOM_I2CS_CTRLB_CMD(3);
}
if (flags & SERCOM_I2CS_INTFLAG_PREC)
{
I2C_SERCOM->I2CS.INTFLAG.reg = SERCOM_I2CS_INTFLAG_PREC;
}
if (flags & SERCOM_I2CS_INTFLAG_DRDY)
{
if (I2C_SERCOM->I2CS.STATUS.reg & SERCOM_I2CS_STATUS_DIR)
{
static int cnt = 0;
I2C_SERCOM->I2CS.DATA.reg = cnt++;
}
else
{
I2C_SERCOM->I2CS.CTRLB.reg |= SERCOM_I2CM_CTRLB_ACKACT;
int data = I2C_SERCOM->I2CS.DATA.reg;
(void)data;
}
I2C_SERCOM->I2CS.INTFLAG.reg = SERCOM_I2CS_INTFLAG_DRDY;
}
}
//-----------------------------------------------------------------------------
int main(void)
{
uint32_t cnt = 0;
bool fast = false;
sys_init();
timer_init();
uart_init(115200);
i2c_init();
DelayMs(1000);
uart_puts("\r\nHello, world!\r\n");
DelayMs(1000);
while (1)
{
}
return 0;
}
Could it be something on the arduino side of the exchange? I am reading the correct sensor addresses so I assumed everything is working properly.