From 237b81f430c88dba00183a22ba2186e70071d34c Mon Sep 17 00:00:00 2001 From: Michael DM Dryden Date: Mon, 27 Feb 2017 19:25:43 -0500 Subject: [PATCH] Move TWI init into separate function. --- src/config/dstat_config.h | 20 ++++++++++++++++++++ src/ext_twi.c | 30 ++++++++++++++++++++++++++++++ src/ext_twi.h | 17 +++++++++++++++++ src/main.c | 2 ++ src/tcs.c | 26 ++++---------------------- src/tcs.h | 3 --- 6 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/config/dstat_config.h create mode 100644 src/ext_twi.c create mode 100644 src/ext_twi.h diff --git a/src/config/dstat_config.h b/src/config/dstat_config.h new file mode 100644 index 0000000..6792275 --- /dev/null +++ b/src/config/dstat_config.h @@ -0,0 +1,20 @@ +// +// dstat_config.h +// dstat-firmware +// +// Created by Michael Dryden on 2017-02-19. +// Copyright © 2017 Michael Dryden. All rights reserved. +// + +#ifndef dstat_config_h +#define dstat_config_h + +#include + +#define EXT_TWI0 TWIC +#define EXT_TWI0_SDA IOPORT_CREATE_PIN(PORTC,0) +#define EXT_TWI0_SCL IOPORT_CREATE_PIN(PORTC,1) +#define EXT_TWI0_MASTER_ADDR 0x42 +#define EXT_TWI0_SPEED 100000 + +#endif /* dstat_config_h */ diff --git a/src/ext_twi.c b/src/ext_twi.c new file mode 100644 index 0000000..ff0d099 --- /dev/null +++ b/src/ext_twi.c @@ -0,0 +1,30 @@ +// +// ext_twi.c +// dstat-firmware +// +// Created by Michael Dryden on 2017-02-19. +// Copyright © 2017 Michael Dryden. All rights reserved. +// + +#include "ext_twi.h" + +#include "config/dstat_config.h" +#include +#include +#include +#include + +void ext_twi_init(void){ + ioport_set_pin_mode(EXT_TWI0_SDA, IOPORT_MODE_WIREDANDPULL); + ioport_set_pin_mode(EXT_TWI0_SCL, IOPORT_MODE_WIREDANDPULL); + sysclk_enable_peripheral_clock(&EXT_TWI0); + twi_master_enable(&EXT_TWI0); + + twi_master_options_t opt = { + .speed = EXT_TWI0_SPEED, + .chip = EXT_TWI0_MASTER_ADDR + }; + + twi_master_setup(&EXT_TWI0, &opt); + printf("#INFO: TWI master enabled\n\r"); +} diff --git a/src/ext_twi.h b/src/ext_twi.h new file mode 100644 index 0000000..2fa405c --- /dev/null +++ b/src/ext_twi.h @@ -0,0 +1,17 @@ +// +// ext_twi.h +// dstat-firmware +// +// Created by Michael Dryden on 2017-02-19. +// Copyright © 2017 Michael Dryden. All rights reserved. +// + +#ifndef ext_twi_h +#define ext_twi_h + +#include +#include "config/dstat_config.h" + +void ext_twi_init(void); + +#endif /* ext_twi_h */ diff --git a/src/main.c b/src/main.c index f875541..49ef95b 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include #include #include "conf_board.h" +#include "ext_twi.h" //Internal function declarations int8_t command_handler(char command); @@ -109,6 +110,7 @@ int main(void){ settings_read_eeprom(); + ext_twi_init(); tcs_init(); shutter_init(); diff --git a/src/tcs.c b/src/tcs.c index 25cf47f..6f012d6 100644 --- a/src/tcs.c +++ b/src/tcs.c @@ -11,6 +11,9 @@ #include #include #include +#include "dstat_config.h" + +#define TCS_TWI EXT_TWI0 #define SLAVE_ADDR 0x29 #define TCS_EN 0x00 @@ -19,28 +22,7 @@ #define TCS_INTERRUPT 0b10000010 #define TCS_CTRL 0x0F -static void tcs_on(void); - -static uint8_t _tcs_active = 0; - void tcs_init(void){ - ioport_set_port_mode(IOPORT_PORTC, PIN0_bm|PIN1_bm, IOPORT_MODE_WIREDANDPULL); - - twi_master_options_t opt = { - .speed = 50000, - .chip = 0x42, - }; - - sysclk_enable_peripheral_clock(&TCS_TWI); - twi_master_setup(&TCS_TWI, &opt); - twi_master_enable(&TCS_TWI); - - printf("#INFO: TWI master enabled\n\r"); - - tcs_on(); -} - -static void tcs_on(void){ twi_package_t twi_pack; uint8_t data_buffer = 0b00000011; @@ -86,4 +68,4 @@ void tcs_readvalues(uint16_t data[4]){ data[3] = data_buffer.uint16[3]; return; -} \ No newline at end of file +} diff --git a/src/tcs.h b/src/tcs.h index a6d3988..d9d18e4 100644 --- a/src/tcs.h +++ b/src/tcs.h @@ -10,9 +10,6 @@ #define tcs_h #include -//#include - -#define TCS_TWI TWIC void tcs_init(void); void tcs_readvalues(uint16_t data[4]); -- GitLab