From 06cf23794d752755fc8ce2fdafed6d521298ad77 Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Fri, 3 May 2019 01:04:28 +0200 Subject: [PATCH] feat(i2c): scan bus in hello world --- Hello_World/Makefile | 2 ++ Hello_World/main.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Hello_World/Makefile b/Hello_World/Makefile index 2efde347..f399063a 100644 --- a/Hello_World/Makefile +++ b/Hello_World/Makefile @@ -90,6 +90,8 @@ PROJ_CFLAGS+=-DMXC_ASSERT_ENABLE #PROJ_CFLAGS+=--specs=nano.specs #PROJ_LDFLAGS+=--specs=nano.specs +PROJ_CFLAGS+=-std=c99 + # Point this variable to a startup file to override the default file #STARTUPFILE=start.S diff --git a/Hello_World/main.c b/Hello_World/main.c index 74ffc81c..7e5a7b6a 100644 --- a/Hello_World/main.c +++ b/Hello_World/main.c @@ -47,13 +47,22 @@ #include "led.h" #include "board.h" #include "tmr_utils.h" +#include "i2c.h" /***** Definitions *****/ +#define I2C_DEVICE MXC_I2C0_BUS0 + /***** Globals *****/ /***** Functions *****/ - +#if 0 +void I2C0_IRQHandler(void) +{ + I2C_Handler(I2C_DEVICE); + return; +} +#endif // ***************************************************************************** int main(void) { @@ -61,6 +70,23 @@ int main(void) printf("Hello World!\n"); + //Setup the I2CM + I2C_Shutdown(I2C_DEVICE); + I2C_Init(I2C_DEVICE, I2C_FAST_MODE, NULL); + #if 0 + NVIC_EnableIRQ(I2C0_IRQn); // Not sure if we actually need this when not doing async requests + #endif + + uint8_t dummy[1] = {0}; + // "7-bit addresses 0b0000xxx and 0b1111xxx are reserved" + for (int addr = 0x8; addr < 0x78; ++addr) { + // A 0 byte write does not seem to work so always send a single byte. + int res = I2C_MasterWrite(I2C_DEVICE, addr << 1, dummy, 1, 0); + if(res == 1) { + printf("Found (7 bit) address 0x%02x\n", addr); + } + } + while (1) { LED_On(0); TMR_Delay(MXC_TMR0, MSEC(500), 0); -- GitLab