diff --git a/Config.h b/Config.h index fb9f74d..986a243 100644 --- a/Config.h +++ b/Config.h @@ -60,6 +60,9 @@ // Use separate mode pins to switch external filters/bandwidth for example // #define STM32F4_NUCLEO_MODE_PINS +// For the VK6MST Pi3 Shield communicating over i2c. i2c address & speed defined in i2cTeensy.cpp +// #define VK6MST_TEENSY_PI3_SHIELD_I2C + // Pass RSSI information to the host // #define SEND_RSSI_DATA diff --git a/I2CTeensy.cpp b/I2CTeensy.cpp new file mode 100644 index 0000000..f034724 --- /dev/null +++ b/I2CTeensy.cpp @@ -0,0 +1,225 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * 28-4-2017 Created for MMDVM Pi shield for Teensy by Chris Huitema + */ + +#include "Config.h" +#include "Globals.h" + +#if defined(VK6MST_TEENSY_PI3_SHIELD_I2C) + +#include //available here https://github.com/nox771/i2c_t3 or maybe the normal wire will work #include justs need to test i guess + +#define I2C_ADDRESS 0x22 +#define I2C_SPEED 100000 + +// Function prototypes +void receiveEvent(size_t count); +void requestEvent(void); + +#define TX_FIFO_SIZE 512U +#define RX_FIFO_SIZE 512U + +volatile uint8_t TXfifo[TX_FIFO_SIZE]; +volatile uint8_t RXfifo[RX_FIFO_SIZE]; +volatile uint16_t TXfifohead, TXfifotail; +volatile uint16_t RXfifohead, RXfifotail; + +// Init queues +void TXfifoinit(void) +{ + TXfifohead = 0U; + TXfifotail = 0U; +} + +void RXfifoinit() +{ + RXfifohead = 0U; + RXfifotail = 0U; +} + +// How full is queue + +uint16_t TXfifolevel(void) +{ + uint32_t tail = TXfifotail; + uint32_t head = TXfifohead; + + if (tail > head) + return TX_FIFO_SIZE + head - tail; + else + return head - tail; +} + +uint16_t RXfifolevel(void) +{ + uint32_t tail = RXfifotail; + uint32_t head = RXfifohead; + + if (tail > head) + return RX_FIFO_SIZE + head - tail; + else + return head - tail; +} + + +uint8_t TXfifoput(uint8_t next) +{ + if (TXfifolevel() < TX_FIFO_SIZE) { + TXfifo[TXfifohead] = next; + + TXfifohead++; + if (TXfifohead >= TX_FIFO_SIZE) + TXfifohead = 0U; + return 1U; + } else { + return 0U; // signal an overflow occurred by returning a zero count + } +} + +void I2Cbegin(void) +{ + // Setup for Slave mode, address 0x22, pins 18/19, external pullups, speed in hz + Wire.begin(I2C_SLAVE, I2C_ADDRESS, I2C_PINS_18_19, I2C_PULLUP_EXT, I2C_SPEED); + + // register events + Wire.onReceive(receiveEvent); + Wire.onRequest(requestEvent); + + // initialize the fifos + TXfifoinit(); + RXfifoinit(); +} + + +int I2Cavailable(void) +{ + if (RXfifolevel() > 0U) + return 1U; + else + return 0U; +} + + +uint8_t I2Cread(void) +{ + uint8_t data_c = RXfifo[RXfifotail]; + + RXfifotail++; + if (RXfifotail >= RX_FIFO_SIZE) + RXfifotail = 0U; + + return data_c; +} + + +void I2Cwrite(const uint8_t* data, uint16_t length) +{ + for (uint16_t i = 0U; i < length; i++) + TXfifoput(data[i]); //puts it in the fifo +} + +// +// handle Rx Event (incoming I2C data) +// +void receiveEvent(size_t count) +{ + for (uint16_t i = 0U; i < count; i++) + { + if (RXfifolevel() < RX_FIFO_SIZE) { + RXfifo[RXfifohead] = Wire.readByte(); + if (RXfifo[RXfifohead] != -1){ + RXfifohead++; + if (RXfifohead >= RX_FIFO_SIZE) RXfifohead = 0U; + } + } else { + Wire.readByte(); // drop data if mem full. + } + + } +} + +// +// handle Tx Event (outgoing I2C data) +// +void requestEvent(void) +{ + if (TXfifolevel() > 0) { + if (Wire.write(TXfifo[TXfifotail])){ //write to i2c + TXfifotail++; + if (TXfifotail >= TX_FIFO_SIZE) TXfifotail = 0U; + } + } +} + + + +/************************************/ + +void CSerialPort::beginInt(uint8_t n, int speed) +{ + switch (n) { + case 1U: + return I2Cbegin(); + case 3U: + Serial3.begin(speed); + break; + default: + break; + } +} + +int CSerialPort::availableInt(uint8_t n) +{ + switch (n) { + case 1U: + return I2Cavailable(); + case 3U: + return Serial3.available(); + default: + return false; + } +} + +uint8_t CSerialPort::readInt(uint8_t n) +{ + switch (n) { + case 1U: + return I2Cread(); + case 3U: + return Serial3.read(); + default: + return 0U; + } +} + +void CSerialPort::writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush) +{ + switch (n) { + case 1U: + I2Cwrite(data, length); + break; + case 3U: + Serial3.write(data, length); + if (flush) + Serial3.flush(); + break; + default: + break; + } +} + +#endif diff --git a/IOSTM.cpp b/IOSTM.cpp index 701ebfa..e76fd65 100644 --- a/IOSTM.cpp +++ b/IOSTM.cpp @@ -419,6 +419,85 @@ EXT_CLK PA15 input #define PIN_TX GPIO_Pin_4 #define PIN_TX_CH DAC_Channel_1 +#elif defined(STM32F722_F7HAT) +/* +Pin definitions for MMDVM-F7Hat Pi-Hat F0DEI DB9MAT DF2ET board: + +PTT PB14 output +COSLED PB13 output +LED PB12 output +COS PC0 input + +DSTAR PB15 output +DMR PC6 output +YSF PC7 output +P25 PC8 output +NXDN PC9 output + +RX PA0 analog input +RSSI PA7 analog input +TX PA4 analog output + +EXT_CLK PA15 input +*/ + +#define PIN_COS GPIO_Pin_0 +#define PORT_COS GPIOC +#define RCC_Per_COS RCC_AHB1Periph_GPIOC + +#define PIN_PTT GPIO_Pin_14 +#define PORT_PTT GPIOB +#define RCC_Per_PTT RCC_AHB1Periph_GPIOB + +#define PIN_COSLED GPIO_Pin_13 +#define PORT_COSLED GPIOB +#define RCC_Per_COSLED RCC_AHB1Periph_GPIOB + +#define PIN_LED GPIO_Pin_12 +#define PORT_LED GPIOB +#define RCC_Per_LED RCC_AHB1Periph_GPIOB + +#define PIN_P25 GPIO_Pin_8 +#define PORT_P25 GPIOC +#define RCC_Per_P25 RCC_AHB1Periph_GPIOC + +#define PIN_NXDN GPIO_Pin_9 +#define PORT_NXDN GPIOC +#define RCC_Per_NXDN RCC_AHB1Periph_GPIOC + +#define PIN_POCSAG GPIO_Pin_12 +#define PORT_POCSAG GPIOB +#define RCC_Per_POCSAG RCC_AHB1Periph_GPIOB + +#define PIN_DSTAR GPIO_Pin_15 +#define PORT_DSTAR GPIOB +#define RCC_Per_DSTAR RCC_AHB1Periph_GPIOB + +#define PIN_DMR GPIO_Pin_6 +#define PORT_DMR GPIOC +#define RCC_Per_DMR RCC_AHB1Periph_GPIOC + +#define PIN_YSF GPIO_Pin_7 +#define PORT_YSF GPIOC +#define RCC_Per_YSF RCC_AHB1Periph_GPIOC + +#define PIN_EXT_CLK GPIO_Pin_15 +#define SRC_EXT_CLK GPIO_PinSource15 +#define PORT_EXT_CLK GPIOA + +#define PIN_RX GPIO_Pin_0 +#define PIN_RX_CH ADC_Channel_0 +#define PORT_RX GPIOA +#define RCC_Per_RX RCC_AHB1Periph_GPIOA + +#define PIN_RSSI GPIO_Pin_7 +#define PIN_RSSI_CH ADC_Channel_7 +#define PORT_RSSI GPIOA +#define RCC_Per_RSSI RCC_AHB1Periph_GPIOA + +#define PIN_TX GPIO_Pin_4 +#define PIN_TX_CH DAC_Channel_1 + #elif defined(STM32F4_NUCLEO) #if defined(STM32F4_NUCLEO_MORPHO_HEADER) diff --git a/Makefile b/Makefile index 2c5e005..433dc9b 100644 --- a/Makefile +++ b/Makefile @@ -128,6 +128,8 @@ DEFS_NUCLEO_F767=-DUSE_HAL_DRIVER -DSTM32F767xx -DSTM32F7XX -DSTM32F7_NUCLEO -DH DEFS_PI_F722=-DUSE_HAL_DRIVER -DSTM32F722xx -DSTM32F7XX -DSTM32F722_PI -DHSE_VALUE=$(OSC) -DMADEBYMAKEFILE # MMDVM-F7M F0DEI board: DEFS_F7M=-DUSE_HAL_DRIVER -DSTM32F722xx -DSTM32F7XX -DSTM32F722_F7M -DHSE_VALUE=$(OSC) -DMADEBYMAKEFILE +# MMDVM-F7-Hat F0DEI, DB9MAT, DF2ET board: +DEFS_F7HAT=-DUSE_HAL_DRIVER -DSTM32F722xx -DSTM32F7XX -DSTM32F722_F7HAT -DHSE_VALUE=$(OSC) -DMADEBYMAKEFILE # STM32F4 DVM board: DEFS_DVM=-DUSE_STDPERIPH_DRIVER -DSTM32F4XX -DSTM32F446xx -DSTM32F4_DVM -DHSE_VALUE=$(OSC) -DMADEBYMAKEFILE @@ -177,6 +179,12 @@ f7m: CXXFLAGS+=$(CXXFLAGS_F7) $(DEFS_F7M) f7m: LDFLAGS+=$(LDFLAGS_F722) f7m: release_f7 +f7hat: GitVersion.h +f7hat: CFLAGS+=$(CFLAGS_F7) $(DEFS_F7HAT) +f7hat: CXXFLAGS+=$(CXXFLAGS_F7) $(DEFS_F7HAT) +f7hat: LDFLAGS+=$(LDFLAGS_F722) +f7hat: release_f7 + nucleo: GitVersion.h nucleo: CFLAGS+=$(CFLAGS_F4) $(DEFS_NUCLEO) nucleo: CXXFLAGS+=$(CXXFLAGS_F4) $(DEFS_NUCLEO) @@ -336,7 +344,8 @@ ifneq ($(wildcard /usr/bin/stm32flash),) /usr/bin/stm32flash -v -w bin/$(BINBIN_F7) -g 0x0 -R -c /dev/ttyAMA0 endif -deploy-f7m: deploy-pi-f7 +deploy-f7m: deploy-pi-f7 +deploy-f7hat: deploy-pi-f7 # Export the current git version if the index file exists, else 000... GitVersion.h: diff --git a/SerialArduino.cpp b/SerialArduino.cpp index 7240a41..db49759 100644 --- a/SerialArduino.cpp +++ b/SerialArduino.cpp @@ -21,7 +21,10 @@ #include "SerialPort.h" -#if defined(__SAM3X8E__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) +#if defined(VK6MST_TEENSY_PI3_SHIELD_I2C) +//it will load I2CTeensy.cpp + +#elif defined(__SAM3X8E__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) void CSerialPort::beginInt(uint8_t n, int speed) { diff --git a/SerialSTM.cpp b/SerialSTM.cpp index 9eeb53e..f6d26f8 100644 --- a/SerialSTM.cpp +++ b/SerialSTM.cpp @@ -50,7 +50,7 @@ extern "C" { } /* ************* USART1 ***************** */ -#if defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_F7M) || defined(STM32F722_PI) || defined(STM32F4_DVM) || (defined(STM32F4_NUCLEO) && defined(STM32F4_NUCLEO_ARDUINO_HEADER)) +#if defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_F7M) || defined(STM32F722_PI) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) || (defined(STM32F4_NUCLEO) && defined(STM32F4_NUCLEO_ARDUINO_HEADER)) volatile uint8_t TXSerialfifo1[TX_SERIAL_FIFO_SIZE]; volatile uint8_t RXSerialfifo1[RX_SERIAL_FIFO_SIZE]; @@ -841,7 +841,7 @@ void CSerialPort::beginInt(uint8_t n, int speed) case 1U: #if defined(STM32F4_DISCOVERY) || defined(STM32F7_NUCLEO) InitUSART3(speed); - #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F4_DVM) + #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) InitUSART1(speed); #elif defined(STM32F4_NUCLEO) InitUSART2(speed); @@ -865,7 +865,7 @@ int CSerialPort::availableInt(uint8_t n) case 1U: #if defined(STM32F4_DISCOVERY) || defined(STM32F7_NUCLEO) return AvailUSART3(); - #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F4_DVM) + #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) return AvailUSART1(); #elif defined(STM32F4_NUCLEO) return AvailUSART2(); @@ -887,7 +887,7 @@ int CSerialPort::availableForWriteInt(uint8_t n) case 1U: #if defined(STM32F4_DISCOVERY) || defined(STM32F7_NUCLEO) return AvailForWriteUSART3(); - #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F4_DVM) + #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) return AvailForWriteUSART1(); #elif defined(STM32F4_NUCLEO) return AvailForWriteUSART2(); @@ -909,7 +909,7 @@ uint8_t CSerialPort::readInt(uint8_t n) case 1U: #if defined(STM32F4_DISCOVERY) || defined(STM32F7_NUCLEO) return ReadUSART3(); - #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F4_DVM) + #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) return ReadUSART1(); #elif defined(STM32F4_NUCLEO) return ReadUSART2(); @@ -933,7 +933,7 @@ void CSerialPort::writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool WriteUSART3(data, length); if (flush) TXSerialFlush3(); - #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F4_DVM) + #elif defined(STM32F4_PI) || defined(STM32F4_F4M) || defined(STM32F722_PI) || defined(STM32F722_F7M) || defined(STM32F722_F7HAT) || defined(STM32F4_DVM) WriteUSART1(data, length); if (flush) TXSerialFlush1();