i2c-master: working on l1 too.

Needs retest on f4.  then ready to move to new hardware
This commit is contained in:
Karl Palsson 2017-03-01 23:30:59 +00:00
parent 12f454b41b
commit d6e9b188c7
5 changed files with 108 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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);
}

View file

@ -0,0 +1,80 @@
/*
* Feb 2017 Karl Palsson <karlp@tweak.net.au>
*/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/i2c.h>
#include <libopencm3/stm32/rcc.h>
#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;
}