i2c-master: tidy up more.

Still f4 only, but cleaner, and more testing with sigrok for
verification
This commit is contained in:
Karl Palsson 2017-03-01 22:53:58 +00:00
parent d30f38ed3c
commit 12f454b41b
5 changed files with 39 additions and 34 deletions

View file

@ -8,10 +8,19 @@ Instead, use a known I2C peripheral on all boards, and require/expect
a known fixed i2c slave device. (Eventually, this will be a soft
controllable i2c slave in the auto test setup ;)
the "trigger" pin is bounced when each iteration of the test code starts,
allowing synchronization with a sigrok script that helps assure that
results are as expected.
Debug is via SWO wherever possible, PA2 (tx only) on less capable cores
Pinouts:
Pinouts: (External PullUps REQUIRED!)
board SCLK SDA
f4-disco PB8 PB9 i2c1
board SCLK SDA i2cperiph trigger
f4-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!

View file

@ -15,6 +15,9 @@ struct hw_detail
uint32_t pins; /* eg: GPIO8 | GPIO9 */ /* ASSUMES SAME PORT*/
uint32_t port; /* eg GPIOB */
uint32_t port_rcc; /* eg RCC_GPIOB */
uint32_t trigger_rcc;
uint32_t trigger_port;
uint32_t trigger_pin;
};
extern struct hw_detail hw_details;

View file

@ -6,6 +6,7 @@
#include <stdint.h>
#include <stdio.h>
#include <libopencm3/stm32/i2c.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include "trace.h"
#include "hw.h"
@ -33,6 +34,7 @@ void i2cm_init(void)
//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);
@ -174,7 +176,7 @@ static float sht21_read_humi_hold(uint32_t i2c)
static void sht21_readid(void)
{
sht21_send_cmd(I2C1, SHT21_CMD_READ_REG);
uint8_t raw;
uint8_t raw = 0;
sht21_readn(I2C1, 1, &raw);
printf("raw user reg = %#x\n", raw);
int resolution = ((raw & 0x80) >> 6) | (raw & 1);
@ -196,9 +198,11 @@ static void sht21_readid(void)
void i2cm_task(void)
{
gpio_set(hw_details.trigger_port, hw_details.trigger_pin);
sht21_readid();
float temp = sht21_read_temp_hold(I2C1);
float humi = sht21_read_humi_hold(I2C1);
gpio_clear(hw_details.trigger_port, hw_details.trigger_pin);
printf("Temp: %f C, RH: %f\n", temp, humi);
}

View file

@ -1,14 +1,6 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: i2c-master.h
* Author: karlp
*
* Created on February 21, 2017, 10:59 PM
* common i2c master code headers.
* Karl Palsson <karlp@tweak.net.au>
*/
#pragma once

View file

@ -23,18 +23,25 @@ struct hw_detail hw_details = {
.periph = I2C1,
.periph_rcc = RCC_I2C1,
.periph_rst = RST_I2C1,
// .pins = GPIO6 | GPIO9, /* FIXME - only for onboard! */
.pins = GPIO8 | GPIO9, /* For SHT21 on 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,
};
static void setup_i2c_gpio(void)
/* 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)
{
/* reset pin */
// rcc_periph_clock_enable(RCC_GPIOD);
// gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4);
/* 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);
@ -43,19 +50,10 @@ static void setup_i2c_gpio(void)
gpio_set_af(hw_details.port, GPIO_AF4, hw_details.pins);
}
static void codec_init(void)
static void setup(void)
{
int i;
/* Configure the Codec related IOs */
setup_i2c_gpio();
/* reset the codec */
// gpio_clear(GPIOD, GPIO4);
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
__asm__("NOP");
}
// gpio_set(GPIOD, GPIO4);
printf("hi guys!\n");
i2cm_hw_init();
i2cm_init();
}
@ -68,8 +66,7 @@ int main(void)
rcc_periph_clock_enable(RCC_GPIOD);
gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LED_DISCO_GREEN_PIN);
printf("hi guys!\n");
codec_init();
setup();
while (1) {
i2cm_task();