i2c-master: start progressing to extracting common i2c code

This commit is contained in:
Karl Palsson 2017-02-21 23:11:43 +00:00
parent 115b771e56
commit 65301e5a0b
5 changed files with 102 additions and 18 deletions

View file

@ -22,7 +22,7 @@ BUILD_DIR = bin-$(BOARD)
SHARED_DIR = ../../shared
CFILES = main-$(BOARD).c
#CFILES += adc-power.c
CFILES += i2c-master.c
CFILES += trace.c trace_stdio.c
VPATH += $(SHARED_DIR)

38
tests/i2c-master/hw.h Normal file
View file

@ -0,0 +1,38 @@
/*
* Feb 2017 Karl Palsson <karlp@tweak.net.au>
*/
#include <stdbool.h>
#include <stdint.h>
#pragma once
struct hw_detail
{
uint32_t periph; /* eg: I2C1 */
uint32_t periph_rcc; /* eg: RCC_I2C1 */
uint32_t periph_rst; /* eg: RST_I2C1 */
uint32_t pins; /* eg: GPIO8 | GPIO9 */ /* ASSUMES SAME PORT*/
uint32_t port; /* eg GPIOB */
uint32_t port_rcc; /* eg RCC_GPIOB */
};
extern struct hw_detail hw_details;
#ifdef __cplusplus
extern "C" {
#endif
/**
* Expected to setup clocks, turn on all peripherals, and configure
* any gpios necessary.
* @param hw pointer to hw details necessary
*/
void hw_setup(struct hw_detail* hw);
/* let devices have a status led */
void hw_set_led(bool val);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,23 @@
/*
* Feb 2017, Karl Palsson <karlp@tweak.net.au>
*/
#include <libopencm3/stm32/i2c.h>
#include <libopencm3/stm32/rcc.h>
#include "hw.h"
#include "i2c-master.h"
void i2cm_init(void) {
rcc_periph_clock_enable(hw_details.periph_rcc);
rcc_periph_reset_pulse(hw_details.periph_rst);
i2c_set_standard_mode(hw_details.periph);
i2c_enable_ack(hw_details.periph);
//i2c_set_dutycycle(hw_details.periph, I2C_CCR_DUTY_DIV2); /* default, no need to do this really */
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);
i2c_peripheral_enable(hw_details.periph);
}

View file

@ -0,0 +1,26 @@
/*
* 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
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void i2cm_init(void);
#ifdef __cplusplus
}
#endif

View file

@ -12,12 +12,25 @@
#include "trace.h"
#include "hw.h"
#include "i2c-master.h"
#define LED_DISCO_GREEN_PORT GPIOD
#define LED_DISCO_GREEN_PIN GPIO12
#define CODEC_ADDRESS 0x4a
struct hw_detail hw_details = {
.periph = I2C1,
.periph_rcc = RCC_I2C1,
.periph_rst = RST_I2C1,
.pins = GPIO6 | GPIO9, /* FIXME - only for onboard! */
.port = GPIOB,
.port_rcc = RCC_GPIOB,
};
static void codec_gpio_init(void)
{
/* reset pin */
@ -31,22 +44,6 @@ static void codec_gpio_init(void)
gpio_set_af(GPIOB, GPIO_AF4, GPIO6 | GPIO9);
}
static void codec_i2c_init(void)
{
rcc_periph_clock_enable(RCC_I2C1);
i2c_peripheral_disable(I2C1);
i2c_reset(I2C1);
i2c_set_standard_mode(I2C1);
i2c_enable_ack(I2C1);
i2c_set_dutycycle(I2C1, I2C_CCR_DUTY_DIV2); /* default, no need to do this really */
i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_42MHZ);
/* 42MHz / (100kHz * 2) */
i2c_set_ccr(I2C1, 210);
/* standard mode, freqMhz+1*/
i2c_set_trise(I2C1, 43);
i2c_peripheral_enable(I2C1);
}
static void codec_init(void)
{
int i;
@ -60,7 +57,7 @@ static void codec_init(void)
}
gpio_set(GPIOD, GPIO4);
codec_i2c_init();
i2cm_init();
}
static int codec_write_reg(uint8_t reg, uint8_t val)