diff options
Diffstat (limited to 'tests/i2c-master')
-rw-r--r-- | tests/i2c-master/Makefile | 15 | ||||
-rw-r--r-- | tests/i2c-master/Makefile.stm32f072-disco | 23 | ||||
-rw-r--r-- | tests/i2c-master/Makefile.stm32f4-disco | 40 | ||||
-rw-r--r-- | tests/i2c-master/Makefile.stm32l1-generic | 18 | ||||
-rw-r--r-- | tests/i2c-master/README.md | 31 | ||||
-rw-r--r-- | tests/i2c-master/hw.h | 42 | ||||
-rw-r--r-- | tests/i2c-master/i2c-master.c | 121 | ||||
-rw-r--r-- | tests/i2c-master/i2c-master.h | 19 | ||||
-rw-r--r-- | tests/i2c-master/main-stm32f072-disco.c | 104 | ||||
-rw-r--r-- | tests/i2c-master/main-stm32f4-disco.c | 79 | ||||
-rw-r--r-- | tests/i2c-master/main-stm32l1-generic.c | 80 |
11 files changed, 0 insertions, 572 deletions
diff --git a/tests/i2c-master/Makefile b/tests/i2c-master/Makefile deleted file mode 100644 index 52841cd..0000000 --- a/tests/i2c-master/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -# This is just a stub makefile used for travis builds -# to keep things all compiling. Normally you'd use -# one of the makefiles directly. - -# These hoops are to enable parallel make correctly. -GZ_ALL := $(wildcard Makefile.*) - -all: $(GZ_ALL:=.all) -clean: $(GZ_ALL:=.clean) - -%.all: - make -f $* all -%.clean: - make -f $* clean - diff --git a/tests/i2c-master/Makefile.stm32f072-disco b/tests/i2c-master/Makefile.stm32f072-disco deleted file mode 100644 index 02e987c..0000000 --- a/tests/i2c-master/Makefile.stm32f072-disco +++ /dev/null @@ -1,23 +0,0 @@ -BOARD = stm32f072-disco -PROJECT = i2c-master-$(BOARD) -BUILD_DIR = bin-$(BOARD) - -SHARED_DIR = ../../shared - -CFILES = main-$(BOARD).c -CFILES += i2c-master.c -# No trace on cm0! -#CFILES += trace.c trace_stdio.c -CFILES += usart_stdio.c - -VPATH += $(SHARED_DIR) - -INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) - -OPENCM3_DIR=../../libopencm3 -DEVICE=stm32f072rb -#OOCD_INTERFACE = stlink-v2 -#OOCD_TARGET = stm32f0x -OOCD_FILE = ../../openocd/openocd.stm32f072-disco.cfg - -include ../../rules.mk diff --git a/tests/i2c-master/Makefile.stm32f4-disco b/tests/i2c-master/Makefile.stm32f4-disco deleted file mode 100644 index 2456820..0000000 --- a/tests/i2c-master/Makefile.stm32f4-disco +++ /dev/null @@ -1,40 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## This library is free software: you can redistribute it and/or modify -## it under the terms of the GNU Lesser General Public License as published by -## the Free Software Foundation, either version 3 of the License, or -## (at your option) any later version. -## -## This library is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU Lesser General Public License for more details. -## -## You should have received a copy of the GNU Lesser General Public License -## along with this library. If not, see <http://www.gnu.org/licenses/>. -## - -BOARD = stm32f4-disco -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/ - -### This section can go to an arch shared rules eventually... -DEVICE=stm32f405xg -#OOCD_INTERFACE = stlink-v2 -#OOCD_TARGET = stm32f4x -OOCD_FILE = ../../openocd/openocd.stm32f4-disco.cfg - -include ../../rules.mk diff --git a/tests/i2c-master/Makefile.stm32l1-generic b/tests/i2c-master/Makefile.stm32l1-generic deleted file mode 100644 index ea75919..0000000 --- a/tests/i2c-master/Makefile.stm32l1-generic +++ /dev/null @@ -1,18 +0,0 @@ -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 deleted file mode 100644 index 9ebb3c5..0000000 --- a/tests/i2c-master/README.md +++ /dev/null @@ -1,31 +0,0 @@ -i2c master mode tests. - -While many of the disco boards have some form of i2c device onboard, -which would, on the face of it, make testing easy, it's a different -device on each board, and there are boards without it. - -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, uart (tx only) on less capable cores - -Pinouts: (External PullUps REQUIRED!) - -board SCLK SDA i2cperiph trigger uart -f4-disco PB8 PB9 i2c1 PB13 -l1-disco PB8 PB9 i2c1 PB13 -f072-disco PB8 PB9 i2c1 PB12 PA9! - - -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 deleted file mode 100644 index 8b235be..0000000 --- a/tests/i2c-master/hw.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 */ - 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; - -#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 diff --git a/tests/i2c-master/i2c-master.c b/tests/i2c-master/i2c-master.c deleted file mode 100644 index 3adbbf0..0000000 --- a/tests/i2c-master/i2c-master.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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/gpio.h> -#include <libopencm3/stm32/rcc.h> -#include "trace.h" -#include "hw.h" -#include "i2c-master.h" - -#define SENSOR_ADDRESS (0x40) - -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_speed(hw_details.periph, i2c_speed_sm_100k, hw_details.i2c_clock_megahz); - //i2c_set_speed(hw_details.periph, i2c_speed_fm_400k, hw_details.i2c_clock_megahz); - - i2c_peripheral_enable(hw_details.periph); -} - - -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) -{ - uint8_t data[3]; - uint8_t cmd = SHT21_CMD_TEMP_HOLD; - i2c_transfer7(i2c, SENSOR_ADDRESS, &cmd, 1, data, sizeof(data)); - uint8_t crc = data[2]; - uint16_t temp = data[0] << 8 | data[1]; - // TODO - calcualte CRC and check! - printf("CRC=%#x, data0=%#x, data1=%#x\n", crc, data[0], data[1]); - return sht21_convert_temp(temp); -} - -static float sht21_read_humi_hold(uint32_t i2c) -{ - uint8_t data[3]; - uint8_t cmd = SHT21_CMD_HUMIDITY_HOLD; - i2c_transfer7(i2c, SENSOR_ADDRESS, &cmd, 1, data, sizeof(data)); - - uint8_t crc = data[2]; - uint16_t left = data[0] << 8 | data[1]; - // TODO - calcualte CRC and check! - printf("CRC=%#x, data0=%#x, data1=%#x\n", crc, data[0], data[1]); - return sht21_convert_humi(left); -} - -static void sht21_readid(void) -{ - uint8_t raw = 0; - uint8_t cmd = SHT21_CMD_READ_REG; - printf("RP..."); - i2c_transfer7(hw_details.periph, SENSOR_ADDRESS, &cmd, 1, &raw, 1); - printf("..S/S\n"); - // or, with stop/start - i2c_transfer7(hw_details.periph, SENSOR_ADDRESS, &cmd, 1, 0, 0); - i2c_transfer7(hw_details.periph, SENSOR_ADDRESS, 0, 0, &raw, 1); - - 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]; - i2c_transfer7(hw_details.periph, SENSOR_ADDRESS, req1, sizeof(req1), res, 8); - uint8_t req2[] = {0xfc, 0xc9}; - uint8_t res2[8]; - i2c_transfer7(hw_details.periph, SENSOR_ADDRESS, req1, sizeof(req1), res2, 8); - - 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) -{ - static int i = 1; - printf(">>>>Starting iteration %d\n", i++); - gpio_set(hw_details.trigger_port, hw_details.trigger_pin); - sht21_readid(); - float temp = sht21_read_temp_hold(hw_details.periph); - float humi = sht21_read_humi_hold(hw_details.periph); - gpio_clear(hw_details.trigger_port, hw_details.trigger_pin); - printf("Temp: %f C, RH: %f\n", temp, humi); - -}
\ No newline at end of file diff --git a/tests/i2c-master/i2c-master.h b/tests/i2c-master/i2c-master.h deleted file mode 100644 index 7a44b66..0000000 --- a/tests/i2c-master/i2c-master.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * common i2c master code headers. - * Karl Palsson <karlp@tweak.net.au> - */ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - - - void i2cm_init(void); - void i2cm_task(void); - -#ifdef __cplusplus -} -#endif - diff --git a/tests/i2c-master/main-stm32f072-disco.c b/tests/i2c-master/main-stm32f072-disco.c deleted file mode 100644 index 41a4902..0000000 --- a/tests/i2c-master/main-stm32f072-disco.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2017 Karl Palsson <karlp@tweak.net.au> - */ - -#include <libopencm3/cm3/nvic.h> -#include <libopencm3/stm32/crs.h> -#include <libopencm3/stm32/gpio.h> -#include <libopencm3/stm32/i2c.h> -#include <libopencm3/stm32/rcc.h> -#include <libopencm3/stm32/usart.h> - -#include <stdio.h> -#include "hw.h" -#include "i2c-master.h" - -#define LED_DISCO_GREEN_PORT GPIOC -#define LED_DISCO_GREEN_PIN GPIO9 - - - -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 = GPIO12, - .i2c_clock_megahz = 48, -}; - - -static -void setup_usart(void) -{ - uint32_t dev = USART1; - rcc_periph_clock_enable(RCC_USART1); - rcc_periph_clock_enable(RCC_GPIOA); - gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); - gpio_set_af(GPIOA, GPIO_AF1, GPIO9); - - usart_set_baudrate(dev, 115200); - usart_set_databits(dev, 8); - usart_set_parity(dev, USART_PARITY_NONE); - usart_set_stopbits(dev, USART_CR2_STOPBITS_1); - usart_set_mode(dev, USART_MODE_TX); - usart_set_flow_control(dev, USART_FLOWCONTROL_NONE); - - /* Finally enable the USART. */ - usart_enable(dev); -} - -/** - * 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_HIGH, hw_details.pins); - gpio_set_af(hw_details.port, GPIO_AF1, hw_details.pins); - - /* select sysclk as i2c clock! */ - RCC_CFGR3 |= RCC_CFGR3_I2C1SW; -} - - -static void setup(void) -{ - setup_usart(); - printf("hi guys!\n"); - i2cm_hw_init(); - i2cm_init(); -} - -int main(void) -{ - int i; - rcc_clock_setup_in_hsi48_out_48mhz(); - /* green led for ticking */ - rcc_periph_clock_enable(RCC_GPIOC); - gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_DISCO_GREEN_PIN); - gpio_set(LED_DISCO_GREEN_PORT, 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"); - } - } - -} - diff --git a/tests/i2c-master/main-stm32f4-disco.c b/tests/i2c-master/main-stm32f4-disco.c deleted file mode 100644 index 605872f..0000000 --- a/tests/i2c-master/main-stm32f4-disco.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 GPIOD -#define LED_DISCO_GREEN_PIN GPIO12 - - -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, -}; - - -/* 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_50MHZ, 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_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); - /* green led for ticking */ - rcc_periph_clock_enable(RCC_GPIOD); - 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; -} diff --git a/tests/i2c-master/main-stm32l1-generic.c b/tests/i2c-master/main-stm32l1-generic.c deleted file mode 100644 index 874cf7d..0000000 --- a/tests/i2c-master/main-stm32l1-generic.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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; -} |