1
0
forked from me/IronOS

I2C into nicer wrapper for FreeRToS

This commit is contained in:
Ben V. Brown
2018-04-14 13:37:42 +10:00
parent cc09157106
commit f599624b6f
14 changed files with 223 additions and 104 deletions

View File

@@ -50,7 +50,7 @@ Dma.I2C1_RX.0.MemInc=DMA_MINC_ENABLE
Dma.I2C1_RX.0.Mode=DMA_NORMAL Dma.I2C1_RX.0.Mode=DMA_NORMAL
Dma.I2C1_RX.0.PeriphDataAlignment=DMA_PDATAALIGN_BYTE Dma.I2C1_RX.0.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.I2C1_RX.0.PeriphInc=DMA_PINC_DISABLE Dma.I2C1_RX.0.PeriphInc=DMA_PINC_DISABLE
Dma.I2C1_RX.0.Priority=DMA_PRIORITY_LOW Dma.I2C1_RX.0.Priority=DMA_PRIORITY_MEDIUM
Dma.I2C1_RX.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority Dma.I2C1_RX.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
Dma.I2C1_TX.1.Direction=DMA_MEMORY_TO_PERIPH Dma.I2C1_TX.1.Direction=DMA_MEMORY_TO_PERIPH
Dma.I2C1_TX.1.Instance=DMA1_Channel6 Dma.I2C1_TX.1.Instance=DMA1_Channel6
@@ -59,7 +59,7 @@ Dma.I2C1_TX.1.MemInc=DMA_MINC_ENABLE
Dma.I2C1_TX.1.Mode=DMA_NORMAL Dma.I2C1_TX.1.Mode=DMA_NORMAL
Dma.I2C1_TX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE Dma.I2C1_TX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.I2C1_TX.1.PeriphInc=DMA_PINC_DISABLE Dma.I2C1_TX.1.PeriphInc=DMA_PINC_DISABLE
Dma.I2C1_TX.1.Priority=DMA_PRIORITY_LOW Dma.I2C1_TX.1.Priority=DMA_PRIORITY_MEDIUM
Dma.I2C1_TX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority Dma.I2C1_TX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
Dma.Request0=I2C1_RX Dma.Request0=I2C1_RX
Dma.Request1=I2C1_TX Dma.Request1=I2C1_TX
@@ -74,8 +74,9 @@ FREERTOS.configMINIMAL_STACK_SIZE=256
FREERTOS.configTICK_RATE_HZ=100 FREERTOS.configTICK_RATE_HZ=100
FREERTOS.configTOTAL_HEAP_SIZE=10240 FREERTOS.configTOTAL_HEAP_SIZE=10240
File.Version=6 File.Version=6
I2C1.DutyCycle=I2C_DUTYCYCLE_2
I2C1.I2C_Mode=I2C_Fast I2C1.I2C_Mode=I2C_Fast
I2C1.IPParameters=I2C_Mode I2C1.IPParameters=I2C_Mode,DutyCycle
IWDG.IPParameters=Prescaler IWDG.IPParameters=Prescaler
IWDG.Prescaler=IWDG_PRESCALER_256 IWDG.Prescaler=IWDG_PRESCALER_256
KeepUserPlacement=false KeepUserPlacement=false
@@ -111,27 +112,30 @@ Mcu.Pin7=PA14
Mcu.Pin8=PB3 Mcu.Pin8=PB3
Mcu.Pin9=PB4 Mcu.Pin9=PB4
Mcu.PinsNb=18 Mcu.PinsNb=18
Mcu.ThirdPartyNb=0
Mcu.UserConstants= Mcu.UserConstants=
Mcu.UserName=STM32F103T8Ux Mcu.UserName=STM32F103T8Ux
MxCube.Version=4.22.0 MxCube.Version=4.25.0
MxDb.Version=DB.4.0.220 MxDb.Version=DB.4.0.250
NVIC.ADC1_2_IRQn=true\:5\:0\:false\:false\:true\:true NVIC.ADC1_2_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.DMA1_Channel1_IRQn=true\:5\:0\:false\:false\:true\:true NVIC.DMA1_Channel1_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.DMA1_Channel6_IRQn=true\:5\:0\:false\:false\:true\:true NVIC.DMA1_Channel6_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.DMA1_Channel7_IRQn=true\:5\:0\:false\:false\:true\:true NVIC.DMA1_Channel7_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.I2C1_ER_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.I2C1_EV_IRQn=true\:5\:0\:false\:false\:true\:true\:true
NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:true
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:true
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:true NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:true\:true
NVIC.TIM1_UP_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.TIM1_UP_IRQn=true\:0\:0\:false\:false\:true\:false\:true
NVIC.TimeBase=TIM1_UP_IRQn NVIC.TimeBase=TIM1_UP_IRQn
NVIC.TimeBaseIP=TIM1 NVIC.TimeBaseIP=TIM1
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:true
PA13.Locked=true PA13.Locked=true
PA13.Mode=Serial_Wire PA13.Mode=Serial_Wire
PA13.Signal=SYS_JTMS-SWDIO PA13.Signal=SYS_JTMS-SWDIO
@@ -193,29 +197,30 @@ PCC.Vdd=3.3
PinOutPanel.RotationAngle=0 PinOutPanel.RotationAngle=0
ProjectManager.AskForMigrate=true ProjectManager.AskForMigrate=true
ProjectManager.BackupPrevious=false ProjectManager.BackupPrevious=false
ProjectManager.CompilerOptimize=2 ProjectManager.CompilerOptimize=3
ProjectManager.ComputerToolchain=false ProjectManager.ComputerToolchain=false
ProjectManager.CoupleFile=false ProjectManager.CoupleFile=false
ProjectManager.CustomerFirmwarePackage=C\:/Users/Ben V. Brown/STM32Cube/Repository/STM32Cube_FW_F1_V1.6.0 ProjectManager.CustomerFirmwarePackage=C\:/Users/Ralim/STM32Cube/Repository/STM32Cube_FW_F1_V1.6.1
ProjectManager.DefaultFWLocation=true ProjectManager.DefaultFWLocation=true
ProjectManager.DeletePrevious=true ProjectManager.DeletePrevious=true
ProjectManager.DeviceId=STM32F103T8Ux ProjectManager.DeviceId=STM32F103T8Ux
ProjectManager.FirmwarePackage=STM32Cube FW_F1 V1.6.0 ProjectManager.FirmwarePackage=STM32Cube FW_F1 V1.6.1
ProjectManager.FreePins=true ProjectManager.FreePins=true
ProjectManager.HalAssertFull=false ProjectManager.HalAssertFull=false
ProjectManager.HeapSize=0x200 ProjectManager.HeapSize=0x200
ProjectManager.KeepUserCode=true ProjectManager.KeepUserCode=true
ProjectManager.LastFirmware=true ProjectManager.LastFirmware=true
ProjectManager.LibraryCopy=1 ProjectManager.LibraryCopy=1
ProjectManager.MainLocation=Src
ProjectManager.PreviousToolchain=SW4STM32 ProjectManager.PreviousToolchain=SW4STM32
ProjectManager.ProjectBuild=false ProjectManager.ProjectBuild=false
ProjectManager.ProjectFileName=TS100.ioc ProjectManager.ProjectFileName=TS100.ioc
ProjectManager.ProjectName=TS100 ProjectManager.ProjectName=TS100
ProjectManager.StackSize=0x400 ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=SW4STM32 ProjectManager.TargetToolchain=SW4STM32
ProjectManager.ToolChainLocation=C\:\\Users\\Ralim\\Repo\\ts100\\TS100 ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=false ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL,2-MX_DMA_Init-DMA-false-HAL,3-MX_I2C1_Init-I2C1-false-HAL,4-MX_ADC1_Init-ADC1-false-HAL,5-SystemClock_Config-RCC-false-HAL,6-MX_TIM3_Init-TIM3-false-HAL,7-MX_IWDG_Init-IWDG-false-HAL ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-MX_I2C1_Init-I2C1-false-HAL-true,4-MX_ADC1_Init-ADC1-false-HAL-true,5-SystemClock_Config-RCC-false-HAL-true,6-MX_TIM3_Init-TIM3-false-HAL-true,7-MX_IWDG_Init-IWDG-false-HAL-true
RCC.ADCFreqValue=8000000 RCC.ADCFreqValue=8000000
RCC.ADCPresc=RCC_ADCPCLK2_DIV8 RCC.ADCPresc=RCC_ADCPCLK2_DIV8
RCC.AHBFreq_Value=64000000 RCC.AHBFreq_Value=64000000

View File

@@ -0,0 +1,32 @@
/*
* FRToSI2C.hpp
*
* Created on: 14Apr.,2018
* Author: Ralim
*/
#ifndef FRTOSI2C_HPP_
#define FRTOSI2C_HPP_
#include "stm32f1xx_hal.h"
#include "cmsis_os.h"
class FRToSI2C {
public:
FRToSI2C(I2C_HandleTypeDef* i2chandle);
void MasterTxCpltCallback(); //Normal Tx Callback
void MemRxCpltCallback(); //Callback from memory read cycles
void MemTxCpltCallback(); //Callback from memory write cycles
void Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize,
uint8_t *pData, uint16_t Size);
void Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize,
uint8_t *pData, uint16_t Size);
void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
private:
I2C_HandleTypeDef* i2c;
};
#endif /* FRTOSI2C_HPP_ */

View File

@@ -8,10 +8,11 @@
#ifndef LIS2DH12_HPP_ #ifndef LIS2DH12_HPP_
#define LIS2DH12_HPP_ #define LIS2DH12_HPP_
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "FRToSI2C.hpp"
#include "LIS2DH12_defines.hpp" #include "LIS2DH12_defines.hpp"
class LIS2DH12 { class LIS2DH12 {
public: public:
LIS2DH12(I2C_HandleTypeDef* i2cHandle); LIS2DH12(FRToSI2C* i2cHandle);
void initalize(); void initalize();
uint8_t getOrientation(); uint8_t getOrientation();
void getAxisReadings(int16_t *x, int16_t *y, int16_t *z); void getAxisReadings(int16_t *x, int16_t *y, int16_t *z);
@@ -21,7 +22,7 @@ private:
void I2C_RegisterWrite(uint8_t reg, uint8_t data); void I2C_RegisterWrite(uint8_t reg, uint8_t data);
uint8_t I2C_RegisterRead(uint8_t reg); uint8_t I2C_RegisterRead(uint8_t reg);
I2C_HandleTypeDef* i2c; FRToSI2C* i2c;
}; };
#endif /* LIS2DH12_HPP_ */ #endif /* LIS2DH12_HPP_ */

View File

@@ -9,11 +9,12 @@
#define MMA8652FC_HPP_ #define MMA8652FC_HPP_
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include "MMA8652FC_defines.h" #include "MMA8652FC_defines.h"
#include "FRToSI2C.hpp"
class MMA8652FC { class MMA8652FC {
public: public:
MMA8652FC(I2C_HandleTypeDef* i2cHandle); MMA8652FC(FRToSI2C* i2cHandle);
void initalize(); // Initalize the system void initalize(); // Initalize the system
uint8_t getOrientation();// Reads the I2C register and returns the orientation (true == left) uint8_t getOrientation();// Reads the I2C register and returns the orientation (true == left)
void getAxisReadings(int16_t *x, int16_t *y, int16_t *z); void getAxisReadings(int16_t *x, int16_t *y, int16_t *z);
@@ -23,7 +24,7 @@ private:
void I2C_RegisterWrite(uint8_t reg, uint8_t data); void I2C_RegisterWrite(uint8_t reg, uint8_t data);
uint8_t I2C_RegisterRead(uint8_t reg); uint8_t I2C_RegisterRead(uint8_t reg);
I2C_HandleTypeDef* i2c; FRToSI2C* i2c;
}; };

View File

@@ -12,6 +12,7 @@
#include <hardware.h> #include <hardware.h>
#include "stm32f1xx_hal.h" #include "stm32f1xx_hal.h"
#include <stdbool.h> #include <stdbool.h>
#include "FRToSI2C.hpp"
#include "Font.h" #include "Font.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -25,7 +26,7 @@ extern "C" {
class OLED { class OLED {
public: public:
OLED(I2C_HandleTypeDef* i2cHandle); // Initialize Driver and store I2C pointer OLED(FRToSI2C* i2cHandle); // Initialize Driver and store I2C pointer
void initialize(); // Startup the I2C coms (brings screen out of reset etc) void initialize(); // Startup the I2C coms (brings screen out of reset etc)
void refresh(); // Draw the buffer out to the LCD using the DMA Channel void refresh(); // Draw the buffer out to the LCD using the DMA Channel
void drawChar(char c, char preCursorCommand = '\0');// Draw a character to a specific location void drawChar(char c, char preCursorCommand = '\0');// Draw a character to a specific location
@@ -50,7 +51,7 @@ private:
//Draw a buffer to the screen buffer //Draw a buffer to the screen buffer
I2C_HandleTypeDef* i2c; //i2c Pointer FRToSI2C* i2c; //i2c Pointer
const uint8_t* currentFont; // Pointer to the current font used for rendering to the buffer const uint8_t* currentFont; // Pointer to the current font used for rendering to the buffer
uint8_t screenBuffer[12 + 96 + 96 + 10]; // The data buffer uint8_t screenBuffer[12 + 96 + 96 + 10]; // The data buffer
uint8_t* firstStripPtr; // Pointers to the strips to allow for buffer having extra content uint8_t* firstStripPtr; // Pointers to the strips to allow for buffer having extra content

View File

@@ -17,9 +17,10 @@ extern "C" {
extern ADC_HandleTypeDef hadc1; extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_adc1; extern DMA_HandleTypeDef hdma_adc1;
extern DMA_HandleTypeDef hdma_i2c1_rx;
extern DMA_HandleTypeDef hdma_i2c1_tx;
extern I2C_HandleTypeDef hi2c1; extern I2C_HandleTypeDef hi2c1;
extern IWDG_HandleTypeDef hiwdg; extern IWDG_HandleTypeDef hiwdg;
extern TIM_HandleTypeDef htim2; extern TIM_HandleTypeDef htim2;

View File

@@ -0,0 +1,50 @@
/*
* FRToSI2C.cpp
*
* Created on: 14Apr.,2018
* Author: Ralim
*/
#include "FRToSI2C.hpp"
FRToSI2C::FRToSI2C(I2C_HandleTypeDef* i2chandle) {
i2c = i2chandle;
}
void FRToSI2C::MasterTxCpltCallback() {
}
void FRToSI2C::MemRxCpltCallback() {
}
void FRToSI2C::MemTxCpltCallback() {
}
void FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
uint16_t MemAddSize, uint8_t* pData, uint16_t Size) {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskENTER_CRITICAL();
HAL_I2C_Mem_Read(i2c, DevAddress, MemAddress, MemAddSize, pData, Size,
5000);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskEXIT_CRITICAL();
}
void FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
uint16_t MemAddSize, uint8_t* pData, uint16_t Size) {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskENTER_CRITICAL();
HAL_I2C_Mem_Write(i2c, DevAddress, MemAddress, MemAddSize, pData, Size,
5000);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskEXIT_CRITICAL();
}
void FRToSI2C::Transmit(uint16_t DevAddress, uint8_t* pData, uint16_t Size) {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskENTER_CRITICAL();
HAL_I2C_Master_Transmit(i2c, DevAddress, pData, Size, 5000);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskEXIT_CRITICAL();
}

View File

@@ -7,7 +7,7 @@
#include <LIS2DH12.hpp> #include <LIS2DH12.hpp>
#include "cmsis_os.h" #include "cmsis_os.h"
LIS2DH12::LIS2DH12(I2C_HandleTypeDef* i2cHandle) { LIS2DH12::LIS2DH12(FRToSI2C* i2cHandle) {
i2c = i2cHandle; i2c = i2cHandle;
} }
@@ -33,9 +33,7 @@ void LIS2DH12::initalize() {
uint8_t LIS2DH12::getOrientation() { uint8_t LIS2DH12::getOrientation() {
// 8=right handed,4=left,16=flat // 8=right handed,4=left,16=flat
//So we ignore if not 8/4 //So we ignore if not 8/4
taskENTER_CRITICAL();
uint8_t pos = I2C_RegisterRead(LIS_INT2_SRC); uint8_t pos = I2C_RegisterRead(LIS_INT2_SRC);
taskEXIT_CRITICAL();
if (pos == 8) if (pos == 8)
return 1; return 1;
else if (pos == 4) else if (pos == 4)
@@ -46,12 +44,9 @@ uint8_t LIS2DH12::getOrientation() {
void LIS2DH12::getAxisReadings(int16_t* x, int16_t* y, int16_t* z) { void LIS2DH12::getAxisReadings(int16_t* x, int16_t* y, int16_t* z) {
uint8_t tempArr[6]; uint8_t tempArr[6];
taskENTER_CRITICAL(); i2c->Mem_Read(LIS2DH_I2C_ADDRESS, 0xA8, I2C_MEMADD_SIZE_8BIT,
while (HAL_I2C_Mem_Read(i2c, LIS2DH_I2C_ADDRESS, 0xA8, (uint8_t*) tempArr, 6);
I2C_MEMADD_SIZE_8BIT, (uint8_t*) tempArr, 6, 5000) != HAL_OK) {
HAL_Delay(5);
}
taskEXIT_CRITICAL();
(*x) = ((uint16_t) (tempArr[1] << 8 | tempArr[0])); (*x) = ((uint16_t) (tempArr[1] << 8 | tempArr[0]));
(*y) = ((uint16_t) (tempArr[3] << 8 | tempArr[2])); (*y) = ((uint16_t) (tempArr[3] << 8 | tempArr[2]));
(*z) = ((uint16_t) (tempArr[5] << 8 | tempArr[4])); (*z) = ((uint16_t) (tempArr[5] << 8 | tempArr[4]));
@@ -61,13 +56,12 @@ void LIS2DH12::setSensitivity(uint8_t threshold, uint8_t filterTime) {
} }
void LIS2DH12::I2C_RegisterWrite(uint8_t reg, uint8_t data) { void LIS2DH12::I2C_RegisterWrite(uint8_t reg, uint8_t data) {
HAL_I2C_Mem_Write(i2c, LIS2DH_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &data, i2c->Mem_Write(LIS2DH_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &data, 1);
1, 500);
} }
uint8_t LIS2DH12::I2C_RegisterRead(uint8_t reg) { uint8_t LIS2DH12::I2C_RegisterRead(uint8_t reg) {
uint8_t tx_data[1]; uint8_t tx_data[1];
HAL_I2C_Mem_Read(i2c, LIS2DH_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, i2c->Mem_Read( LIS2DH_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, tx_data, 1);
tx_data, 1, 500);
return tx_data[0]; return tx_data[0];
} }

View File

@@ -8,20 +8,18 @@
#include <MMA8652FC.hpp> #include <MMA8652FC.hpp>
#include "cmsis_os.h" #include "cmsis_os.h"
MMA8652FC::MMA8652FC(I2C_HandleTypeDef* i2cHandle) { MMA8652FC::MMA8652FC(FRToSI2C* i2cHandle) {
i2c = i2cHandle; i2c = i2cHandle;
} }
void MMA8652FC::I2C_RegisterWrite(uint8_t reg, uint8_t data) { void MMA8652FC::I2C_RegisterWrite(uint8_t reg, uint8_t data) {
i2c->Mem_Write( MMA8652FC_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, &data, 1);
HAL_I2C_Mem_Write(i2c, MMA8652FC_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT,
&data, 1, 500);
} }
uint8_t MMA8652FC::I2C_RegisterRead(uint8_t reg) { uint8_t MMA8652FC::I2C_RegisterRead(uint8_t reg) {
uint8_t tx_data[1]; uint8_t tx_data[1];
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, i2c->Mem_Read( MMA8652FC_I2C_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT, tx_data,
tx_data, 1, 500); 1);
return tx_data[0]; return tx_data[0];
} }
@@ -43,44 +41,35 @@ void MMA8652FC::initalize() {
I2C_RegisterWrite( HP_FILTER_CUTOFF_REG, 0x03); //select high pass filtered data I2C_RegisterWrite( HP_FILTER_CUTOFF_REG, 0x03); //select high pass filtered data
I2C_RegisterWrite( CTRL_REG1, 0x19); // ODR=12 Hz, Active mode I2C_RegisterWrite( CTRL_REG1, 0x19); // ODR=12 Hz, Active mode
HAL_Delay(2); // ~1ms delay
} }
void MMA8652FC::setSensitivity(uint8_t threshold, uint8_t filterTime) { void MMA8652FC::setSensitivity(uint8_t threshold, uint8_t filterTime) {
uint8_t sens = 9 * 2 + 17; uint8_t sens = 9 * 2 + 17;
sens -= 2 * threshold; sens -= 2 * threshold;
taskENTER_CRITICAL();
I2C_RegisterWrite( CTRL_REG1, 0); // sleep mode I2C_RegisterWrite( CTRL_REG1, 0); // sleep mode
I2C_RegisterWrite(FF_MT_THS_REG, (sens & 0x7F));// Set accumulation threshold I2C_RegisterWrite(FF_MT_THS_REG, (sens & 0x7F));// Set accumulation threshold
I2C_RegisterWrite(FF_MT_COUNT_REG, filterTime); // Set debounce threshold I2C_RegisterWrite(FF_MT_COUNT_REG, filterTime); // Set debounce threshold
I2C_RegisterWrite( CTRL_REG1, 0x31); // ODR=12 Hz, Active mode I2C_RegisterWrite( CTRL_REG1, 0x31); // ODR=12 Hz, Active mode
taskEXIT_CRITICAL();
} }
uint8_t MMA8652FC::getOrientation() { uint8_t MMA8652FC::getOrientation() {
//First read the PL_STATUS register //First read the PL_STATUS register
taskENTER_CRITICAL();
uint8_t plStatus = I2C_RegisterRead(PL_STATUS_REG); uint8_t plStatus = I2C_RegisterRead(PL_STATUS_REG);
taskEXIT_CRITICAL();
if ((plStatus & 0b10000000) == 0b10000000) { if ((plStatus & 0b10000000) == 0b10000000) {
plStatus >>= 1; //We don't need the up/down bit plStatus >>= 1; //We don't need the up/down bit
plStatus &= 0x03; //mask to the two lower bits plStatus &= 0x03; //mask to the two lower bits
//0 == left handed //0 == left handed
//1 == right handed //1 == right handed
return plStatus==0?2:1; return plStatus == 0 ? 2 : 1;
} else } else
return 0; return 0;
} }
void MMA8652FC::getAxisReadings(int16_t *x, int16_t *y, int16_t *z) { void MMA8652FC::getAxisReadings(int16_t *x, int16_t *y, int16_t *z) {
uint8_t tempArr[6]; uint8_t tempArr[6];
taskENTER_CRITICAL(); i2c->Mem_Read( MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG,I2C_MEMADD_SIZE_8BIT, (uint8_t*) tempArr, 6);
while (HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG,
I2C_MEMADD_SIZE_8BIT, (uint8_t*) tempArr, 6, 0xFFFF) != HAL_OK) {
HAL_Delay(5);
}
taskEXIT_CRITICAL();
(*x) = tempArr[0] << 8 | tempArr[1]; (*x) = tempArr[0] << 8 | tempArr[1];
(*y) = tempArr[2] << 8 | tempArr[3]; (*y) = tempArr[2] << 8 | tempArr[3];
(*z) = tempArr[4] << 8 | tempArr[5]; (*z) = tempArr[4] << 8 | tempArr[5];

View File

@@ -43,7 +43,7 @@ uint8_t OLED_Setup_Array[] = { /**/
}; };
//Setup based on the SSD1307 and modified for the SSD1306 //Setup based on the SSD1307 and modified for the SSD1306
OLED::OLED(I2C_HandleTypeDef* i2cHandle) { OLED::OLED(FRToSI2C* i2cHandle) {
i2c = i2cHandle; i2c = i2cHandle;
cursor_x = cursor_y = 0; cursor_x = cursor_y = 0;
currentFont = FONT_12; currentFont = FONT_12;
@@ -63,8 +63,7 @@ void OLED::initialize() {
HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_RESET_GPIO_Port, OLED_RESET_Pin, GPIO_PIN_SET);
HAL_Delay(5); HAL_Delay(5);
//Send the setup settings //Send the setup settings
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, i2c->Transmit( DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, configLength);
configLength, 0xFFFF);
//displayOnOff(true); //displayOnOff(true);
} }
@@ -86,12 +85,8 @@ void OLED::refresh() {
screenBuffer[11] = 0x01; screenBuffer[11] = 0x01;
screenBuffer[12] = 0x40; //start of data marker screenBuffer[12] = 0x40; //start of data marker
taskENTER_CRITICAL();
//Because I2C is shared, we cant task switch in the middle of the xfer
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, screenBuffer, 12 + 96 * 2 + 1, i2c->Transmit( DEVICEADDR_OLED, screenBuffer, 12 + 96 * 2 + 1);
500);
taskEXIT_CRITICAL();
} }
@@ -101,10 +96,9 @@ void OLED::refresh() {
* Precursor is the command char that is used to select the table. * Precursor is the command char that is used to select the table.
*/ */
void OLED::drawChar(char c, char PrecursorCommand) { void OLED::drawChar(char c, char PrecursorCommand) {
if( c=='\n' && cursor_y==0) if (c == '\n' && cursor_y == 0) {
{ cursor_x = 0;
cursor_x=0; cursor_y = 8;
cursor_y=8;
} }
if (c < ' ') { if (c < ' ') {
return; return;
@@ -128,10 +122,10 @@ void OLED::drawChar(char c, char PrecursorCommand) {
index = (128) + (c); index = (128) + (c);
break; break;
#if defined(LANG_RU) || defined(LANG_UK) || defined(LANG_SR) || defined(LANG_BG) || defined(LANG_MK) #if defined(LANG_RU) || defined(LANG_UK) || defined(LANG_SR) || defined(LANG_BG) || defined(LANG_MK)
case 0xD0: case 0xD0:
index = (192) + (c); index = (192) + (c);
break; break;
case 0xD1: case 0xD1:
index = (256) + (c); index = (256) + (c);
break; break;
#else #else
@@ -165,8 +159,7 @@ void OLED::displayOnOff(bool on) {
data[5] = 0xAE; data[5] = 0xAE;
} }
taskENTER_CRITICAL(); taskENTER_CRITICAL();
i2c->Transmit( DEVICEADDR_OLED, data, 6);
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, data, 6, 0xFFFF);
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
displayOnOffState = on; displayOnOffState = on;
} }
@@ -184,8 +177,7 @@ void OLED::setRotation(bool leftHanded) {
} }
taskENTER_CRITICAL(); taskENTER_CRITICAL();
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, i2c->Transmit( DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, 50);
(uint8_t*) OLED_Setup_Array, 50, 0xFFFF);
taskEXIT_CRITICAL(); taskEXIT_CRITICAL();
inLeftHandedMode = leftHanded; inLeftHandedMode = leftHanded;
} }
@@ -300,11 +292,11 @@ void OLED::drawArea(int16_t x, int8_t y, uint8_t wide, uint8_t height,
uint8_t visibleEnd = wide; uint8_t visibleEnd = wide;
// trimming to draw partials // trimming to draw partials
if(x < 0) { if (x < 0) {
visibleStart -= x; //subtract negative value == add absolute value visibleStart -= x; //subtract negative value == add absolute value
} }
if(x + wide > 96) { if (x + wide > 96) {
visibleEnd = 96 - x; visibleEnd = 96 - x;
} }
if (y == 0) { if (y == 0) {

View File

@@ -9,6 +9,8 @@ ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1; DMA_HandleTypeDef hdma_adc1;
I2C_HandleTypeDef hi2c1; I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;
IWDG_HandleTypeDef hiwdg; IWDG_HandleTypeDef hiwdg;
TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim2;

View File

@@ -10,12 +10,14 @@
#include "string.h" #include "string.h"
#include "LIS2DH12.hpp" #include "LIS2DH12.hpp"
#include <gui.hpp> #include <gui.hpp>
#include "FRToSI2C.hpp"
#define ACCELDEBUG 0 #define ACCELDEBUG 0
// C++ objects // C++ objects
OLED lcd(&hi2c1); FRToSI2C i2cDev(&hi2c1);
MMA8652FC accel(&hi2c1); OLED lcd(&i2cDev);
LIS2DH12 accel2(&hi2c1); MMA8652FC accel(&i2cDev);
LIS2DH12 accel2(&i2cDev);
uint8_t PCBVersion = 0; uint8_t PCBVersion = 0;
// File local variables // File local variables
uint16_t currentlyActiveTemperatureTarget = 0; uint16_t currentlyActiveTemperatureTarget = 0;
@@ -620,6 +622,7 @@ static void gui_solderingMode() {
// Undervoltage test // Undervoltage test
if (checkVoltageForExit()) { if (checkVoltageForExit()) {
lastButtonTime = xTaskGetTickCount();
return; return;
} }
@@ -628,6 +631,7 @@ static void gui_solderingMode() {
if (xTaskGetTickCount() - lastMovementTime > sleepThres if (xTaskGetTickCount() - lastMovementTime > sleepThres
&& xTaskGetTickCount() - lastButtonTime > sleepThres) { && xTaskGetTickCount() - lastButtonTime > sleepThres) {
if (gui_SolderingSleepingMode()) { if (gui_SolderingSleepingMode()) {
lastButtonTime = xTaskGetTickCount();
return; // If the function returns non-0 then exit return; // If the function returns non-0 then exit
} }
} }

View File

@@ -28,10 +28,10 @@ void HAL_MspInit(void) {
/**NOJTAG: JTAG-DP Disabled and SW-DP Enabled /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled
*/ */
__HAL_AFIO_REMAP_SWJ_NOJTAG(); __HAL_AFIO_REMAP_SWJ_NOJTAG()
;
// __HAL_AFIO_REMAP_SWJ_DISABLE(); /*Disable swd for debug io use*/ // __HAL_AFIO_REMAP_SWJ_DISABLE(); /*Disable swd for debug io use*/
} }
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) { void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
@@ -77,22 +77,51 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) {
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
if (hi2c->Instance == I2C1) { /**I2C1 GPIO Configuration
/**I2C1 GPIO Configuration PB6 ------> I2C1_SCL
PB6 ------> I2C1_SCL PB7 ------> I2C1_SDA
PB7 ------> I2C1_SDA */
*/ GPIO_InitStruct.Pin = SCL_Pin | SDA_Pin;
GPIO_InitStruct.Pin = SCL_Pin | SDA_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */ /* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE() __HAL_RCC_I2C1_CLK_ENABLE()
; ;
/* I2C1 DMA Init */
/* I2C1_RX Init */
hdma_i2c1_rx.Instance = DMA1_Channel7;
hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
HAL_DMA_Init(&hdma_i2c1_rx);
} __HAL_LINKDMA(hi2c, hdmarx, hdma_i2c1_rx);
/* I2C1_TX Init */
hdma_i2c1_tx.Instance = DMA1_Channel6;
hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_i2c1_tx.Init.Mode = DMA_NORMAL;
hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
HAL_DMA_Init(&hdma_i2c1_tx);
__HAL_LINKDMA(hi2c, hdmatx, hdma_i2c1_tx);
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_SetPriority(I2C1_ER_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
} }

View File

@@ -7,6 +7,7 @@
extern TIM_HandleTypeDef htim1; //used for the systick extern TIM_HandleTypeDef htim1; //used for the systick
/******************************************************************************/ /******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */ /* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/ /******************************************************************************/
@@ -55,6 +56,7 @@ void DMA1_Channel1_IRQHandler(void) {
HAL_DMA_IRQHandler(&hdma_adc1); HAL_DMA_IRQHandler(&hdma_adc1);
} }
//ADC interrupt used for DMA //ADC interrupt used for DMA
void ADC1_2_IRQHandler(void) { void ADC1_2_IRQHandler(void) {
HAL_ADC_IRQHandler(&hadc1); HAL_ADC_IRQHandler(&hadc1);
@@ -82,5 +84,21 @@ void EXTI3_IRQHandler(void) {
//EXTI 5 is triggered via the accelerometer on movement //EXTI 5 is triggered via the accelerometer on movement
void EXTI9_5_IRQHandler(void) { void EXTI9_5_IRQHandler(void) {
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5); HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
}
void I2C1_EV_IRQHandler(void) {
HAL_I2C_EV_IRQHandler(&hi2c1);
}
void I2C1_ER_IRQHandler(void) {
HAL_I2C_ER_IRQHandler(&hi2c1);
}
void DMA1_Channel6_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_i2c1_tx);
}
void DMA1_Channel7_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_i2c1_rx);
} }