i2c-master: start extracting out i2c code
Working on f4 with an external sht21 i2c sensor. Still lots of f4 specifics yet. But, progress.
This commit is contained in:
parent
65301e5a0b
commit
d30f38ed3c
3 changed files with 201 additions and 123 deletions
|
|
@ -2,22 +2,203 @@
|
|||
* Feb 2017, Karl Palsson <karlp@tweak.net.au>
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <libopencm3/stm32/i2c.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
#include "trace.h"
|
||||
#include "hw.h"
|
||||
#include "i2c-master.h"
|
||||
|
||||
#define SENSOR_ADDRESS (0x40)
|
||||
|
||||
void i2cm_init(void) {
|
||||
enum sht21_cmd_e {
|
||||
SHT21_CMD_TEMP_HOLD = 0xe3,
|
||||
SHT21_CMD_HUMIDITY_HOLD = 0xe5,
|
||||
SHT21_CMD_TEMP_NOHOLD = 0xf3,
|
||||
SHT21_CMD_HUMIDITY_NOHOLD = 0xf5,
|
||||
SHT21_CMD_WRITE_REG = 0xe6,
|
||||
SHT21_CMD_READ_REG = 0xe7,
|
||||
SHT21_CMD_RESET = 0xfe,
|
||||
/* 0xfa, 0x0f to read serial */
|
||||
};
|
||||
|
||||
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_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! */
|
||||
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_peripheral_enable(hw_details.periph);
|
||||
}
|
||||
|
||||
static void sht21_send_data(uint32_t i2c, size_t n, uint8_t *data)
|
||||
{
|
||||
while ((I2C_SR2(i2c) & I2C_SR2_BUSY)) {
|
||||
}
|
||||
|
||||
i2c_send_start(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, SENSOR_ADDRESS, I2C_WRITE);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
uint32_t reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) {
|
||||
i2c_send_data(i2c, data[i]);
|
||||
while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));
|
||||
}
|
||||
}
|
||||
|
||||
static void sht21_send_cmd(uint32_t i2c, uint8_t cmd)
|
||||
{
|
||||
while ((I2C_SR2(i2c) & I2C_SR2_BUSY)) {
|
||||
}
|
||||
|
||||
i2c_send_start(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, SENSOR_ADDRESS, I2C_WRITE);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
uint32_t reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
i2c_send_data(i2c, cmd);
|
||||
while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));
|
||||
}
|
||||
|
||||
static void sht21_readn(uint32_t i2c, int n, uint8_t *res)
|
||||
{
|
||||
//assert(n > 0);
|
||||
i2c_send_start(i2c);
|
||||
|
||||
i2c_enable_ack(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, SENSOR_ADDRESS, I2C_READ);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
uint32_t reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
int i = 0;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (i == n - 1) {
|
||||
i2c_disable_ack(i2c);
|
||||
}
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_RxNE));
|
||||
res[i] = i2c_get_data(i2c);
|
||||
}
|
||||
i2c_send_stop(i2c);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static float sht21_convert_temp(uint16_t raw)
|
||||
{
|
||||
//assert((raw & 0x2) == 0x2);
|
||||
raw &= ~0x3; /* Clear lower status bits */
|
||||
float tf = -46.85 + 175.72 * ((float) raw / 65536.0);
|
||||
return tf;
|
||||
}
|
||||
|
||||
static float sht21_convert_humi(uint16_t raw)
|
||||
{
|
||||
//assert((raw & 0x2) == 0);
|
||||
raw &= ~0x3; /* Clear lower status bits */
|
||||
float tf = -6 + 125 * ((float) raw / 65536.0);
|
||||
return tf;
|
||||
}
|
||||
|
||||
static float sht21_read_temp_hold(uint32_t i2c)
|
||||
{
|
||||
// gpio_set(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN);
|
||||
sht21_send_cmd(i2c, SHT21_CMD_TEMP_HOLD);
|
||||
|
||||
uint8_t data[3];
|
||||
sht21_readn(i2c, 2, data);
|
||||
uint8_t crc = data[2];
|
||||
uint16_t temp = data[0] << 8 | data[1];
|
||||
printf("CRC=%#x, data0=%#x, data1=%#x\n", crc, data[0], data[1]);
|
||||
// gpio_clear(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN);
|
||||
return sht21_convert_temp(temp);
|
||||
}
|
||||
|
||||
static float sht21_read_humi_hold(uint32_t i2c)
|
||||
{
|
||||
// gpio_set(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN);
|
||||
sht21_send_cmd(i2c, SHT21_CMD_HUMIDITY_HOLD);
|
||||
|
||||
uint8_t data[3];
|
||||
sht21_readn(i2c, 2, data);
|
||||
uint8_t crc = data[2];
|
||||
uint16_t left = data[0] << 8 | data[1];
|
||||
printf("CRC=%#x, data0=%#x, data1=%#x\n", crc, data[0], data[1]);
|
||||
|
||||
// gpio_clear(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN);
|
||||
return sht21_convert_humi(left);
|
||||
}
|
||||
|
||||
static void sht21_readid(void)
|
||||
{
|
||||
sht21_send_cmd(I2C1, SHT21_CMD_READ_REG);
|
||||
uint8_t raw;
|
||||
sht21_readn(I2C1, 1, &raw);
|
||||
printf("raw user reg = %#x\n", raw);
|
||||
int resolution = ((raw & 0x80) >> 6) | (raw & 1);
|
||||
printf("temp resolution is in %d bits\n", 14 - resolution);
|
||||
printf("battery status: %s\n", (raw & (1 << 6) ? "failing" : "good"));
|
||||
printf("On chip heater: %s\n", (raw & 0x2) ? "on" : "off");
|
||||
|
||||
uint8_t req1[] = {0xfa, 0x0f};
|
||||
uint8_t res[8];
|
||||
sht21_send_data(I2C1, 2, req1);
|
||||
sht21_readn(I2C1, sizeof(res), res);
|
||||
uint8_t req2[] = {0xfc, 0xc9};
|
||||
uint8_t res2[8];
|
||||
sht21_send_data(I2C1, 2, req2);
|
||||
sht21_readn(I2C1, sizeof(res), res2);
|
||||
printf("Serial = %02x%02x %02x%02x %02x%02x %02x%02x\n",
|
||||
res2[3], res2[4], res[0], res[2], res[4], res[6], res2[0], res2[1]);
|
||||
}
|
||||
|
||||
void i2cm_task(void)
|
||||
{
|
||||
sht21_readid();
|
||||
float temp = sht21_read_temp_hold(I2C1);
|
||||
float humi = sht21_read_humi_hold(I2C1);
|
||||
printf("Temp: %f C, RH: %f\n", temp, humi);
|
||||
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ extern "C" {
|
|||
|
||||
|
||||
void i2cm_init(void);
|
||||
void i2cm_task(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,142 +18,47 @@
|
|||
#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! */
|
||||
// .pins = GPIO6 | GPIO9, /* FIXME - only for onboard! */
|
||||
.pins = GPIO8 | GPIO9, /* For SHT21 on I2c1 */
|
||||
.port = GPIOB,
|
||||
.port_rcc = RCC_GPIOB,
|
||||
};
|
||||
|
||||
|
||||
static void codec_gpio_init(void)
|
||||
static void setup_i2c_gpio(void)
|
||||
{
|
||||
/* reset pin */
|
||||
rcc_periph_clock_enable(RCC_GPIOD);
|
||||
gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4);
|
||||
// rcc_periph_clock_enable(RCC_GPIOD);
|
||||
// gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4);
|
||||
|
||||
/* i2c control lines */
|
||||
rcc_periph_clock_enable(RCC_GPIOB);
|
||||
gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO9);
|
||||
gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO6 | GPIO9);
|
||||
gpio_set_af(GPIOB, GPIO_AF4, GPIO6 | GPIO9);
|
||||
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_50MHZ, hw_details.pins);
|
||||
gpio_set_af(hw_details.port, GPIO_AF4, hw_details.pins);
|
||||
}
|
||||
|
||||
static void codec_init(void)
|
||||
{
|
||||
int i;
|
||||
/* Configure the Codec related IOs */
|
||||
codec_gpio_init();
|
||||
setup_i2c_gpio();
|
||||
|
||||
/* reset the codec */
|
||||
gpio_clear(GPIOD, GPIO4);
|
||||
// gpio_clear(GPIOD, GPIO4);
|
||||
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
|
||||
__asm__("NOP");
|
||||
}
|
||||
gpio_set(GPIOD, GPIO4);
|
||||
// gpio_set(GPIOD, GPIO4);
|
||||
|
||||
i2cm_init();
|
||||
}
|
||||
|
||||
static int codec_write_reg(uint8_t reg, uint8_t val)
|
||||
{
|
||||
uint32_t i2c = I2C1;
|
||||
|
||||
while ((I2C_SR2(i2c) & I2C_SR2_BUSY)) {
|
||||
}
|
||||
|
||||
i2c_send_start(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, CODEC_ADDRESS, I2C_WRITE);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
uint32_t reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
/* Common above here */
|
||||
|
||||
/* Sending the data. */
|
||||
i2c_send_data(i2c, reg);
|
||||
while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));
|
||||
i2c_send_data(i2c, val);
|
||||
while (!(I2C_SR1(i2c) & (I2C_SR1_BTF | I2C_SR1_TxE)));
|
||||
|
||||
/* Send STOP condition. */
|
||||
i2c_send_stop(i2c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t codec_read_reg(uint8_t reg)
|
||||
{
|
||||
uint32_t i2c = I2C1;
|
||||
|
||||
while ((I2C_SR2(i2c) & I2C_SR2_BUSY)) {
|
||||
}
|
||||
|
||||
i2c_send_start(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, CODEC_ADDRESS, I2C_WRITE);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
uint32_t reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
/* Common stuff ABOVE HERE */
|
||||
|
||||
i2c_send_data(i2c, reg);
|
||||
while (!(I2C_SR1(i2c) & (I2C_SR1_BTF)));
|
||||
|
||||
i2c_send_start(i2c);
|
||||
|
||||
/* Wait for master mode selected */
|
||||
while (!((I2C_SR1(i2c) & I2C_SR1_SB)
|
||||
& (I2C_SR2(i2c) & (I2C_SR2_MSL | I2C_SR2_BUSY))));
|
||||
|
||||
i2c_send_7bit_address(i2c, CODEC_ADDRESS, I2C_READ);
|
||||
|
||||
/* Waiting for address is transferred. */
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_ADDR));
|
||||
|
||||
i2c_disable_ack(i2c);
|
||||
|
||||
/* Cleaning ADDR condition sequence. */
|
||||
reg32 = I2C_SR2(i2c);
|
||||
(void) reg32; /* unused */
|
||||
|
||||
i2c_send_stop(i2c);
|
||||
|
||||
while (!(I2C_SR1(i2c) & I2C_SR1_RxNE));
|
||||
uint32_t result = i2c_get_data(i2c);
|
||||
|
||||
i2c_enable_ack(i2c);
|
||||
I2C_SR1(i2c) &= ~I2C_SR1_AF;
|
||||
return result;
|
||||
}
|
||||
|
||||
static void codec_readid(void)
|
||||
{
|
||||
uint8_t res = codec_read_reg(0x01);
|
||||
printf("raw res = %#x Codec is %#x (should be 0x1c), revision %d\n", res, res >> 3, res & 0x7);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
|
@ -165,22 +70,13 @@ int main(void)
|
|||
LED_DISCO_GREEN_PIN);
|
||||
printf("hi guys!\n");
|
||||
codec_init();
|
||||
codec_readid();
|
||||
|
||||
codec_write_reg(0x14, 0xff);
|
||||
for (i = 0; i < 8; i++) {
|
||||
uint8_t pass_vol_a = codec_read_reg(0x14);
|
||||
printf("Passthrough vol A was: %#x\n", pass_vol_a);
|
||||
codec_write_reg(0x14, pass_vol_a >> 1);
|
||||
gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN);
|
||||
for (j = 0; j < 100000; j++) { /* Wait a bit. */
|
||||
__asm__("NOP");
|
||||
}
|
||||
}
|
||||
|
||||
/* Nothing else to do */;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue