From 5e89ce093ff5314bb38d6201327e3b669a9e7d94 Mon Sep 17 00:00:00 2001 From: Daniel Peter Chokola Date: Sat, 4 Mar 2023 01:47:18 -0500 Subject: [PATCH] move BME280 power line to a GPIO and add a skeleton driver for the SX127x LoRA radio --- .cproject | 4 ++ Core/Inc/main.h | 4 ++ Core/Inc/spi.h | 3 + Core/Src/gpio.c | 20 +++++++ Core/Src/main.c | 5 ++ Core/Src/spi.c | 81 ++++++++++++++++++++++++++- Drivers/SX127x/Inc/sx127x.h | 27 +++++++++ Drivers/SX127x/Src/sx127x.c | 107 ++++++++++++++++++++++++++++++++++++ hothouse-mon.ioc | 69 ++++++++++++++++------- 9 files changed, 298 insertions(+), 22 deletions(-) create mode 100644 Drivers/SX127x/Inc/sx127x.h create mode 100644 Drivers/SX127x/Src/sx127x.c diff --git a/.cproject b/.cproject index 8ff1d4c..a7ac93c 100644 --- a/.cproject +++ b/.cproject @@ -34,6 +34,7 @@ @@ -56,6 +57,7 @@ + @@ -120,6 +122,7 @@ @@ -141,6 +144,7 @@ + diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 046e5ce..2f7b0b5 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -57,8 +57,12 @@ void Error_Handler(void); /* USER CODE END EFP */ /* Private defines -----------------------------------------------------------*/ +#define BME280_PWR_Pin GPIO_PIN_13 +#define BME280_PWR_GPIO_Port GPIOC #define SPI2_CS_Pin GPIO_PIN_12 #define SPI2_CS_GPIO_Port GPIOB +#define SPI1_CS_Pin GPIO_PIN_15 +#define SPI1_CS_GPIO_Port GPIOA /* USER CODE BEGIN Private defines */ #define APP_NAME "hothouse" diff --git a/Core/Inc/spi.h b/Core/Inc/spi.h index 0e0f702..d1afe89 100644 --- a/Core/Inc/spi.h +++ b/Core/Inc/spi.h @@ -32,12 +32,15 @@ extern "C" { /* USER CODE END Includes */ +extern SPI_HandleTypeDef hspi1; + extern SPI_HandleTypeDef hspi2; /* USER CODE BEGIN Private defines */ /* USER CODE END Private defines */ +void MX_SPI1_Init(void); void MX_SPI2_Init(void); /* USER CODE BEGIN Prototypes */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 8527541..84129c7 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -50,9 +50,22 @@ void MX_GPIO_Init(void) __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(BME280_PWR_GPIO_Port, BME280_PWR_Pin, GPIO_PIN_RESET); + /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET); + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, GPIO_PIN_RESET); + + /*Configure GPIO pin : PtPin */ + GPIO_InitStruct.Pin = BME280_PWR_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(BME280_PWR_GPIO_Port, &GPIO_InitStruct); + /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = SPI2_CS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; @@ -60,6 +73,13 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(SPI2_CS_GPIO_Port, &GPIO_InitStruct); + /*Configure GPIO pin : PtPin */ + GPIO_InitStruct.Pin = SPI1_CS_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(SPI1_CS_GPIO_Port, &GPIO_InitStruct); + } /* USER CODE BEGIN 2 */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 49f388c..20e043b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,6 +26,7 @@ /* USER CODE BEGIN Includes */ #include "usbd_cdc_if.h" #include "bme280.h" +#include "sx127x.h" #include "terminal.h" /* USER CODE END Includes */ @@ -68,6 +69,7 @@ int main(void) { /* USER CODE BEGIN 1 */ bme280_t bme280; + sx127x_t sx127x; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ @@ -90,8 +92,11 @@ int main(void) MX_GPIO_Init(); MX_SPI2_Init(); MX_USB_DEVICE_Init(); + MX_SPI1_Init(); /* USER CODE BEGIN 2 */ + HAL_GPIO_WritePin(BME280_PWR_GPIO_Port, BME280_PWR_Pin, GPIO_PIN_SET); bme280_init(&bme280, &hspi2, SPI2_CS_GPIO_Port, SPI2_CS_Pin); + sx127x_init(&sx127x, &hspi1, SPI1_CS_GPIO_Port, SPI1_CS_Pin); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/Core/Src/spi.c b/Core/Src/spi.c index 23a72eb..e0156a9 100644 --- a/Core/Src/spi.c +++ b/Core/Src/spi.c @@ -24,8 +24,41 @@ /* USER CODE END 0 */ +SPI_HandleTypeDef hspi1; SPI_HandleTypeDef hspi2; +/* SPI1 init function */ +void MX_SPI1_Init(void) +{ + + /* USER CODE BEGIN SPI1_Init 0 */ + + /* USER CODE END SPI1_Init 0 */ + + /* USER CODE BEGIN SPI1_Init 1 */ + + /* USER CODE END SPI1_Init 1 */ + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_2LINES; + hspi1.Init.DataSize = SPI_DATASIZE_8BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 10; + if (HAL_SPI_Init(&hspi1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN SPI1_Init 2 */ + + /* USER CODE END SPI1_Init 2 */ + +} /* SPI2 init function */ void MX_SPI2_Init(void) { @@ -63,7 +96,32 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(spiHandle->Instance==SPI2) + if(spiHandle->Instance==SPI1) + { + /* USER CODE BEGIN SPI1_MspInit 0 */ + + /* USER CODE END SPI1_MspInit 0 */ + /* SPI1 clock enable */ + __HAL_RCC_SPI1_CLK_ENABLE(); + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**SPI1 GPIO Configuration + PB3 ------> SPI1_SCK + PB4 ------> SPI1_MISO + PB5 ------> SPI1_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN SPI1_MspInit 1 */ + + /* USER CODE END SPI1_MspInit 1 */ + } + else if(spiHandle->Instance==SPI2) { /* USER CODE BEGIN SPI2_MspInit 0 */ @@ -93,7 +151,26 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) { - if(spiHandle->Instance==SPI2) + if(spiHandle->Instance==SPI1) + { + /* USER CODE BEGIN SPI1_MspDeInit 0 */ + + /* USER CODE END SPI1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_SPI1_CLK_DISABLE(); + + /**SPI1 GPIO Configuration + PB3 ------> SPI1_SCK + PB4 ------> SPI1_MISO + PB5 ------> SPI1_MOSI + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5); + + /* USER CODE BEGIN SPI1_MspDeInit 1 */ + + /* USER CODE END SPI1_MspDeInit 1 */ + } + else if(spiHandle->Instance==SPI2) { /* USER CODE BEGIN SPI2_MspDeInit 0 */ diff --git a/Drivers/SX127x/Inc/sx127x.h b/Drivers/SX127x/Inc/sx127x.h new file mode 100644 index 0000000..9d186ef --- /dev/null +++ b/Drivers/SX127x/Inc/sx127x.h @@ -0,0 +1,27 @@ +/* + * sx127x.h + * + * Created on: Mar 4, 2023 + * Author: Daniel Peter Chokola + */ + +#ifndef SX127X_INC_SX127X_H_ +#define SX127X_INC_SX127X_H_ + +/* Includes */ +#include +#include "spi.h" + +/* Definitions */ + +/* Data Structures */ +typedef struct sx127x_s { + SPI_HandleTypeDef *hspi; + GPIO_TypeDef *port; + uint16_t pin; +} sx127x_t; + +/* Function Prototypes */ +int32_t sx127x_init(sx127x_t *sx127x, SPI_HandleTypeDef *hspi, GPIO_TypeDef *port, uint16_t pin); + +#endif /* SX127X_INC_SX127X_H_ */ diff --git a/Drivers/SX127x/Src/sx127x.c b/Drivers/SX127x/Src/sx127x.c new file mode 100644 index 0000000..260db12 --- /dev/null +++ b/Drivers/SX127x/Src/sx127x.c @@ -0,0 +1,107 @@ +/* + * sx127x.c + * + * Created on: Mar 4, 2023 + * Author: Daniel Peter Chokola + */ + +/* Includes */ +#include "sx127x.h" +#include "spi.h" +#include "util.h" + +/* Definitions */ +#define SX127X_REG_FIFO (0x00) /* Default: 0x00 */ +#define SX127X_REG_OPMODE (0x01) /* Default: 0x01 */ +#define SX127X_REG_BITRATEMSB (0x02) /* Default: 0x1A */ +#define SX127X_REG_BITRATELSB (0x03) /* Default: 0x0B */ +#define SX127X_REG_FDEVMSB (0x04) /* Default: 0x00 */ +#define SX127X_REG_FDEVLSB (0x05) /* Default: 0x52 */ +#define SX127X_REG_FRFMSB (0x06) /* Default: 0x6C */ +#define SX127X_REG_FRFMID (0x07) /* Default: 0x80 */ +#define SX127X_REG_FRFLSB (0x08) /* Default: 0x00 */ +#define SX127X_REG_PACONFIG (0x09) /* Default: 0x4F */ +#define SX127X_REG_PARAMP (0x0A) /* Default: 0x09 */ +#define SX127X_REG_OCP (0x0B) /* Default: 0x2B */ +#define SX127X_REG_LNA (0x0C) /* Default: 0x20 */ +#define SX127X_REG_RXCONFIG (0x0D) /* Default: 0x08 (POR), 0x0E (FSK) */ +#define SX127X_REG_RSSICONFIG (0x0E) /* Default: 0x02 */ +#define SX127X_REG_RSSICOLLISION (0x0F) /* Default: 0x0A */ +#define SX127X_REG_RSSITHRESH (0x10) /* Default: 0xFF */ +#define SX127X_REG_RSSIVALUE (0x11) /* Default: n/a */ +#define SX127X_REG_RXBW (0x12) /* Default: 0x15 */ +#define SX127X_REG_AFCBW (0x13) /* Default: 0x0B */ +#define SX127X_REG_OOKPEAK (0x14) /* Default: 0x28 */ +#define SX127X_REG_OOKFIX (0x15) /* Default: 0x0C */ +#define SX127X_REG_OOKAVG (0x16) /* Default: 0x12 */ +#define SX127X_REG_ERVED17 (0x17) /* Default: 0x47 */ +#define SX127X_REG_ERVED18 (0x18) /* Default: 0x32 */ +#define SX127X_REG_ERVED19 (0x19) /* Default: 0x3E */ +#define SX127X_REG_AFCFEI (0x1A) /* Default: 0x00 */ +#define SX127X_REG_AFCMSB (0x1B) /* Default: 0x00 (POR), n/a (FSK) */ +#define SX127X_REG_AFCLSB (0x1C) /* Default: 0x00 (POR), n/a (FSK) */ +#define SX127X_REG_FEIMSB (0x1D) /* Default: 0x00 (POR), n/a (FSK) */ +#define SX127X_REG_FEILSB (0x1E) /* Default: 0x00 (POR), n/a (FSK) */ +#define SX127X_REG_PREAMBLEDETECT (0x1F) /* Default: 0x40 (POR), 0xAA (FSK) */ +#define SX127X_REG_RXTIMEOUT1 (0x20) /* Default: 0x00 */ +#define SX127X_REG_RXTIMEOUT2 (0x21) /* Default: 0x00 */ +#define SX127X_REG_RXTIMEOUT3 (0x22) /* Default: 0x00 */ +#define SX127X_REG_RXDELAY (0x23) /* Default: 0x00 */ +#define SX127X_REG_OSC (0x24) /* Default: 0x05 (POR), 0x07 (FSK) */ +#define SX127X_REG_PREAMBLEMSB (0x25) /* Default: 0x00 */ +#define SX127X_REG_PREAMBLELSB (0x26) /* Default: 0x03 */ +#define SX127X_REG_SYNCCONFIG (0x27) /* Default: 0x93 */ +#define SX127X_REG_SYNCVALUE1 (0x28) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE2 (0x29) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE3 (0x2A) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE4 (0x2B) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE5 (0x2C) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE6 (0x2D) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE7 (0x2E) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_SYNCVALUE8 (0x2F) /* Default: 0x55 (POR), 0x01 (FSK) */ +#define SX127X_REG_PACKETCONFIG1 (0x30) /* Default: 0x90 */ +#define SX127X_REG_PACKETCONFIG2 (0x31) /* Default: 0x40 */ +#define SX127X_REG_PAYLOADLENGTH (0x32) /* Default: 0x40 */ +#define SX127X_REG_NODEADRS (0x33) /* Default: 0x00 */ +#define SX127X_REG_BROADCASTADRS (0x34) /* Default: 0x00 */ +#define SX127X_REG_FIFOTHRESH (0x35) /* Default: 0x1F */ +#define SX127X_REG_SEQCONFIG1 (0x36) /* Default: 0x00 */ +#define SX127X_REG_SEQCONFIG2 (0x37) /* Default: 0x00 */ +#define SX127X_REG_TIMERRESOL (0x38) /* Default: 0x00 */ +#define SX127X_REG_TIMER1COEF (0x39) /* Default: 0xF5 (POR), 0x12 (FSK) */ +#define SX127X_REG_TIMER2COEF (0x3A) /* Default: 0x20 */ +#define SX127X_REG_IMAGECAL (0x3B) /* Default: 0x82 (POR), 0x02 (FSK) */ +#define SX127X_REG_TEMP (0x3C) /* Default: - */ +#define SX127X_REG_LOWBAT (0x3D) /* Default: 0x02 */ +#define SX127X_REG_IRQFLAGS1 (0x3E) /* Default: 0x80 */ +#define SX127X_REG_IRQFLAGS2 (0x3F) /* Default: 0x40 */ +#define SX127X_REG_DIOMAPPING1 (0x40) /* Default: 0x00 */ +#define SX127X_REG_DIOMAPPING2 (0x41) /* Default: 0x00 */ +#define SX127X_REG_VERSION (0x42) /* Default: 0x12 */ +#define SX127X_REG_PLLHOP (0x44) /* Default: 0x2D */ +#define SX127X_REG_TCXO (0x4B) /* Default: 0x09 */ +#define SX127X_REG_PADAC (0x4D) /* Default: 0x84 */ +#define SX127X_REG_FORMERTEMP (0x5B) /* Default: - */ +#define SX127X_REG_BITRATEFRAC (0x5D) /* Default: 0x00 */ +#define SX127X_REG_AGCREF (0x61) /* Default: 0x13 */ +#define SX127X_REG_AGCTHRESH1 (0x62) /* Default: 0x0E */ +#define SX127X_REG_AGCTHRESH2 (0x63) /* Default: 0x5B */ +#define SX127X_REG_AGCTHRESH3 (0x64) /* Default: 0xDB */ +#define SX127X_REG_PLL (0x70) /* Default: 0xD0 */ + +/* Private Variables */ + +/* Function Prototypes */ + +/* Function Definitions */ +int32_t sx127x_init(sx127x_t *sx127x, SPI_HandleTypeDef *hspi, GPIO_TypeDef *port, uint16_t pin) +{ + return_val_if_fail(sx127x, -1); + return_val_if_fail(hspi, -1); + + sx127x->hspi = hspi; + sx127x->port = port; + sx127x->pin = pin; + + return 0; +} diff --git a/hothouse-mon.ioc b/hothouse-mon.ioc index 4a4d5ae..eccc8cb 100644 --- a/hothouse-mon.ioc +++ b/hothouse-m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Wire PA13.Signal=SYS_JTMS-SWDIO PA14.Mode=Serial_Wire PA14.Signal=SYS_JTCK-SWCLK +PA15.GPIOParameters=GPIO_Label +PA15.GPIO_Label=SPI1_CS +PA15.Locked=true +PA15.Signal=GPIO_Output PB12.GPIOParameters=GPIO_Speed,PinState,GPIO_Label PB12.GPIO_Label=SPI2_CS PB12.GPIO_Speed=GPIO_SPEED_FREQ_LOW @@ -68,6 +78,19 @@ PB14.Mode=Full_Duplex_Master PB14.Signal=SPI2_MISO PB15.Mode=Full_Duplex_Master PB15.Signal=SPI2_MOSI +PB3.Locked=true +PB3.Mode=Full_Duplex_Master +PB3.Signal=SPI1_SCK +PB4.Locked=true +PB4.Mode=Full_Duplex_Master +PB4.Signal=SPI1_MISO +PB5.Locked=true +PB5.Mode=Full_Duplex_Master +PB5.Signal=SPI1_MOSI +PC13-ANTI_TAMP.GPIOParameters=GPIO_Label +PC13-ANTI_TAMP.GPIO_Label=BME280_PWR +PC13-ANTI_TAMP.Locked=true +PC13-ANTI_TAMP.Signal=GPIO_Output PC14-OSC32_IN.Mode=LSE-External-Oscillator PC14-OSC32_IN.Signal=RCC_OSC32_IN PC15-OSC32_OUT.Mode=LSE-External-Oscillator @@ -133,6 +156,12 @@ RCC.VCOI2SOutputFreq_Value=192000000 RCC.VCOInputFreq_Value=1000000 RCC.VCOOutputFreq_Value=144000000 RCC.VcooutputI2S=96000000 +SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_8 +SPI1.CalculateBaudRate=9.0 MBits/s +SPI1.Direction=SPI_DIRECTION_2LINES +SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,BaudRatePrescaler +SPI1.Mode=SPI_MODE_MASTER +SPI1.VirtualType=VM_MASTER SPI2.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4 SPI2.CalculateBaudRate=9.0 MBits/s SPI2.Direction=SPI_DIRECTION_2LINES