From d6e9b188c71131cf0745b3b25d033a5e16db886e Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Wed, 1 Mar 2017 23:30:59 +0000 Subject: i2c-master: working on l1 too. Needs retest on f4. then ready to move to new hardware --- tests/i2c-master/Makefile.stm32l1-generic | 18 +++++++ tests/i2c-master/README.md | 4 ++ tests/i2c-master/hw.h | 1 + tests/i2c-master/i2c-master.c | 13 ++--- tests/i2c-master/main-stm32l1-generic.c | 80 +++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 tests/i2c-master/Makefile.stm32l1-generic create mode 100644 tests/i2c-master/main-stm32l1-generic.c (limited to 'tests') diff --git a/tests/i2c-master/Makefile.stm32l1-generic b/tests/i2c-master/Makefile.stm32l1-generic new file mode 100644 index 0000000..ea75919 --- /dev/null +++ b/tests/i2c-master/Makefile.stm32l1-generic @@ -0,0 +1,18 @@ +BOARD = stm32l1-generic +PROJECT = i2c-master-$(BOARD) +BUILD_DIR = bin-$(BOARD) + +SHARED_DIR = ../../shared + +CFILES = main-$(BOARD).c +CFILES += i2c-master.c +CFILES += trace.c trace_stdio.c + +VPATH += $(SHARED_DIR) + +INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) + +OPENCM3_DIR=../../libopencm3 +DEVICE=stm32l151xb +OOCD_FILE = openocd.stm32l1-generic.cfg +include ../../rules.mk diff --git a/tests/i2c-master/README.md b/tests/i2c-master/README.md index 1eb832f..3295328 100644 --- a/tests/i2c-master/README.md +++ b/tests/i2c-master/README.md @@ -18,9 +18,13 @@ Pinouts: (External PullUps REQUIRED!) board SCLK SDA i2cperiph trigger f4-disco PB8 PB9 i2c1 PB13 +l1-disco PB8 PB9 i2c1 PB13 Notes for monitoring with sigrok: $ sigrok-cli -d fx2lafw -C D0=SDA,D1=SCL,D2=Trig -c samplerate=4Mhz:captureratio=4 --time=150ms -t Trig=r -o cap2.cli.sr # Then open the .sr file in pulseview. something's wrong with decoding # directly from the cli! + +# or.... +$ sigrok-cli -d fx2lafw -C D0=SDA,D1=SCL,D2=Trig -c samplerate=4Mhz:captureratio=4 --time=150ms -t Trig=r -P i2c:scl=SCL:sda=SDA diff --git a/tests/i2c-master/hw.h b/tests/i2c-master/hw.h index 34aef32..8b235be 100644 --- a/tests/i2c-master/hw.h +++ b/tests/i2c-master/hw.h @@ -18,6 +18,7 @@ struct hw_detail uint32_t trigger_rcc; uint32_t trigger_port; uint32_t trigger_pin; + int i2c_clock_megahz; /* eg 42 for APB1 on an F4@168Mhz */ }; extern struct hw_detail hw_details; diff --git a/tests/i2c-master/i2c-master.c b/tests/i2c-master/i2c-master.c index 50beda8..5f13ddc 100644 --- a/tests/i2c-master/i2c-master.c +++ b/tests/i2c-master/i2c-master.c @@ -33,14 +33,11 @@ void i2cm_init(void) // i2c_enable_ack(hw_details.periph); /* NO ACK FOR SHT21! */ //i2c_set_dutycycle(hw_details.periph, I2C_CCR_DUTY_DIV2); /* default, no need to do this really */ - /* --------- board specific settings! */ - // TODO - rcc_apb2_clock / 1000000 and rounded somehow nicely? - i2c_set_clock_frequency(hw_details.periph, I2C_CR2_FREQ_42MHZ); - /* 42MHz / (100kHz * 2) */ - i2c_set_ccr(hw_details.periph, 210); - /* standard mode, freqMhz+1*/ - i2c_set_trise(hw_details.periph, 43); - /* --------- end of board specific settings!*/ + i2c_set_clock_frequency(hw_details.periph, hw_details.i2c_clock_megahz); + /* x Mhz / (100kHz * 2) */ + i2c_set_ccr(hw_details.periph, hw_details.i2c_clock_megahz * 5); + /* Sm mode, (100kHz) freqMhz + 1 */ + i2c_set_trise(hw_details.periph, hw_details.i2c_clock_megahz + 1); i2c_peripheral_enable(hw_details.periph); } diff --git a/tests/i2c-master/main-stm32l1-generic.c b/tests/i2c-master/main-stm32l1-generic.c new file mode 100644 index 0000000..874cf7d --- /dev/null +++ b/tests/i2c-master/main-stm32l1-generic.c @@ -0,0 +1,80 @@ +/* + * Feb 2017 Karl Palsson + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "trace.h" + +#include "hw.h" +#include "i2c-master.h" + +#define LED_DISCO_GREEN_PORT GPIOB +#define LED_DISCO_GREEN_PIN GPIO7 + + +struct hw_detail hw_details = { + .periph = I2C1, + .periph_rcc = RCC_I2C1, + .periph_rst = RST_I2C1, + .pins = GPIO8 | GPIO9, /* our external i2c device on I2c1 */ + .port = GPIOB, + .port_rcc = RCC_GPIOB, + .trigger_rcc = RCC_GPIOB, + .trigger_port = GPIOB, + .trigger_pin = GPIO13, + .i2c_clock_megahz = 32, +}; + + +/* provided in board files please*/ +/** + * Setup any gpios or anything hardware specific. + * Should _only_ be things that can't be done in shared i2cm_init! + */ +static void i2cm_hw_init(void) +{ + /* trigger pin gpio */ + rcc_periph_clock_enable(hw_details.trigger_rcc); + gpio_mode_setup(hw_details.trigger_port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, hw_details.trigger_pin); + + /* i2c control lines */ + rcc_periph_clock_enable(hw_details.port_rcc); + gpio_mode_setup(hw_details.port, GPIO_MODE_AF, GPIO_PUPD_NONE, hw_details.pins); + gpio_set_output_options(hw_details.port, GPIO_OTYPE_OD, GPIO_OSPEED_10MHZ, hw_details.pins); + gpio_set_af(hw_details.port, GPIO_AF4, hw_details.pins); +} + +static void setup(void) +{ + printf("hi guys!\n"); + i2cm_hw_init(); + i2cm_init(); +} + + +int main(void) +{ + int i, j; + rcc_clock_setup_pll(&rcc_clock_config[RCC_CLOCK_VRANGE1_HSI_PLL_32MHZ]); + /* green led for ticking */ + rcc_periph_clock_enable(RCC_GPIOB); + gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, + LED_DISCO_GREEN_PIN); + setup(); + + while (1) { + i2cm_task(); + gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN); + for (i = 0; i < 0x800000; i++) { /* Wait a bit. */ + __asm__("NOP"); + } + } + return 0; +} -- cgit