1
0
forked from me/IronOS

Putting guards on I2C

This commit is contained in:
Ben V. Brown
2017-10-28 13:19:20 +11:00
parent 3bf52cfc69
commit 812fbcd902
3 changed files with 60 additions and 45 deletions

View File

@@ -6,6 +6,7 @@
*/ */
#include <MMA8652FC.hpp> #include <MMA8652FC.hpp>
#include "cmsis_os.h"
MMA8652FC::MMA8652FC(I2C_HandleTypeDef* i2cHandle) { MMA8652FC::MMA8652FC(I2C_HandleTypeDef* i2cHandle) {
i2c = i2cHandle; i2c = i2cHandle;
@@ -54,8 +55,10 @@ void MMA8652FC::setSensitivity(uint8_t threshold, uint8_t filterTime) {
} }
bool MMA8652FC::getOrientation() { bool MMA8652FC::getOrientation() {
//First read the PL_STATUS register //First read the PL_STATUS registertaskENTER_CRITICAL();
taskENTER_CRITICAL();
uint8_t plStatus = I2C_RegisterRead(PL_STATUS_REG); uint8_t plStatus = I2C_RegisterRead(PL_STATUS_REG);
taskEXIT_CRITICAL();
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
@@ -64,23 +67,14 @@ bool MMA8652FC::getOrientation() {
return !plStatus; return !plStatus;
} }
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 temp = 0; uint8_t tempArr[6];
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF); taskENTER_CRITICAL();
(*x) = temp; while (HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_X_MSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) tempArr, 6,
(*x) <<= 8; 0xFFFF) != HAL_OK) {
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_X_LSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF); HAL_Delay(5);
(*x) |= temp; }
taskEXIT_CRITICAL();
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_Y_MSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF); (*x) = tempArr[0] << 8 | tempArr[1];
(*y) = temp; (*y) = tempArr[2] << 8 | tempArr[3];
(*y) <<= 8; (*z) = tempArr[4] << 8 | tempArr[5];
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_Y_LSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF);
(*y) |= temp;
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_Z_MSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF);
(*z) = temp;
(*z) <<= 8;
HAL_I2C_Mem_Read(i2c, MMA8652FC_I2C_ADDRESS, OUT_Z_LSB_REG, I2C_MEMADD_SIZE_8BIT, (uint8_t*) &temp, 1, 0xFFFF);
(*z) |= temp;
} }

View File

@@ -8,6 +8,7 @@
#include <OLED.hpp> #include <OLED.hpp>
#include <string.h> #include <string.h>
#include "Translation.h" #include "Translation.h"
#include "cmsis_os.h"
/*Setup params for the OLED screen*/ /*Setup params for the OLED screen*/
/*http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf*/ /*http://www.displayfuture.com/Display/datasheet/controller/SSD1307.pdf*/
/*All commands are prefixed with 0x80*/ /*All commands are prefixed with 0x80*/
@@ -84,8 +85,10 @@ 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();
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, screenBuffer, 12 + 96 * 2 + 1, 0xFFFF); HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, screenBuffer, 12 + 96 * 2 + 1, 0xFFFF);
taskEXIT_CRITICAL();
} }
@@ -136,7 +139,10 @@ void OLED::displayOnOff(bool on) {
data[3] = 0x10; data[3] = 0x10;
data[5] = 0xAE; data[5] = 0xAE;
} }
taskENTER_CRITICAL();
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, data, 6, 0xFFFF); HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, data, 6, 0xFFFF);
taskEXIT_CRITICAL();
displayOnOffState = on; displayOnOffState = on;
} }
} }
@@ -151,7 +157,10 @@ void OLED::setRotation(bool leftHanded) {
OLED_Setup_Array[11] = 0xC0; OLED_Setup_Array[11] = 0xC0;
OLED_Setup_Array[19] = 0xA0; OLED_Setup_Array[19] = 0xA0;
} }
taskENTER_CRITICAL();
HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, 50, 0xFFFF); HAL_I2C_Master_Transmit(i2c, DEVICEADDR_OLED, (uint8_t*) OLED_Setup_Array, 50, 0xFFFF);
taskEXIT_CRITICAL();
inLeftHandedMode = leftHanded; inLeftHandedMode = leftHanded;
} }
} }

View File

@@ -9,6 +9,7 @@
#include "string.h" #include "string.h"
#include "gui.h" #include "gui.h"
#include "stdlib.h" #include "stdlib.h"
//C++ objects //C++ objects
OLED lcd(&hi2c1); OLED lcd(&hi2c1);
MMA8652FC accel(&hi2c1); MMA8652FC accel(&hi2c1);
@@ -72,7 +73,7 @@ int main(void) {
} }
} }
void GUIDelay() { void GUIDelay() {
osDelay(50); osDelay(60);
} }
ButtonState getButtonState() { ButtonState getButtonState() {
/* /*
@@ -620,7 +621,7 @@ static void gui_solderingMode() {
} }
} }
#define ACCELDEBUG 0
/* StartGUITask function */ /* StartGUITask function */
void startGUITask(void const * argument) { void startGUITask(void const * argument) {
/* /*
@@ -654,12 +655,13 @@ void startGUITask(void const * argument) {
if (showBootLogoIfavailable()) if (showBootLogoIfavailable())
waitForButtonPressOrTimeout(1000); waitForButtonPressOrTimeout(1000);
HAL_IWDG_Refresh(&hiwdg); HAL_IWDG_Refresh(&hiwdg);
/* #if ACCELDEBUG
for (;;) {
HAL_IWDG_Refresh(&hiwdg); for (;;) {
osDelay(100); HAL_IWDG_Refresh(&hiwdg);
} osDelay(100);
*/ }
#endif
//^ Kept here for a way to block this thread //^ Kept here for a way to block this thread
for (;;) { for (;;) {
ButtonState buttons = getButtonState(); ButtonState buttons = getButtonState();
@@ -839,27 +841,32 @@ void startPIDTask(void const * argument) {
osDelay(100); // 10 Hz temp loop osDelay(100); // 10 Hz temp loop
} }
} }
#define MOVFilter 4 #define MOVFilter 8
void startMOVTask(void const * argument) { void startMOVTask(void const * argument) {
osDelay(4000); //wait for accel to stabilize osDelay(4000); //wait for accel to stabilize
int16_t datax[MOVFilter]; int16_t datax[MOVFilter];
int16_t datay[MOVFilter]; int16_t datay[MOVFilter];
int16_t dataz[MOVFilter]; int16_t dataz[MOVFilter];
uint8_t currentPointer = 0; uint8_t currentPointer = 0;
memset(datax, 0, MOVFilter); memset(datax, 0, MOVFilter * sizeof(int16_t));
memset(datay, 0, MOVFilter); memset(datay, 0, MOVFilter * sizeof(int16_t));
memset(dataz, 0, MOVFilter); memset(dataz, 0, MOVFilter * sizeof(int16_t));
int16_t tx, ty, tz; int16_t tx, ty, tz;
int32_t avgx, avgy, avgz; int32_t avgx, avgy, avgz;
if (systemSettings.sensitivity > 9)
systemSettings.sensitivity = 9;
#if ACCELDEBUG
uint32_t max = 0;
#endif
for (;;) { for (;;) {
int32_t threshold = 600 + (9 * 120); int32_t threshold = 800 + (9 * 200);
threshold -= systemSettings.sensitivity * 120; threshold -= systemSettings.sensitivity * 200; // 200 is the step size
accel.getAxisReadings(&tx, &ty, &tz); accel.getAxisReadings(&tx, &ty, &tz);
datax[currentPointer] = tx; datax[currentPointer] = (int32_t) tx;
datay[currentPointer] = ty; datay[currentPointer] = (int32_t) ty;
dataz[currentPointer] = tz; dataz[currentPointer] = (int32_t) tz;
currentPointer = (currentPointer + 1) % MOVFilter; currentPointer = (currentPointer + 1) % MOVFilter;
#if ACCELDEBUG #if ACCELDEBUG
//Debug for Accel //Debug for Accel
@@ -874,14 +881,20 @@ void startMOVTask(void const * argument) {
avgy /= MOVFilter; avgy /= MOVFilter;
avgz /= MOVFilter; avgz /= MOVFilter;
lcd.setFont(1); lcd.setFont(1);
lcd.setCursor(0,0); lcd.setCursor(0, 0);
lcd.printNumber(abs(avgx - tx), 5); lcd.printNumber(abs(avgx - (int32_t) tx), 5);
lcd.print(" "); lcd.print(" ");
lcd.printNumber(abs(avgy - ty), 5); lcd.printNumber(abs(avgy - (int32_t) ty), 5);
if ((abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz)) > max)
max = (abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz));
lcd.setCursor(0, 8); lcd.setCursor(0, 8);
lcd.printNumber(abs(avgz - tz), 5); lcd.printNumber(max, 5);
lcd.refresh(); lcd.print(" ");
lcd.printNumber((abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz)), 5);
lcd.refresh();
if (HAL_GPIO_ReadPin(KEY_A_GPIO_Port, KEY_A_Pin) == GPIO_PIN_RESET)
max = 0;
#endif #endif
//Only run the actual processing if the sensitivity is set (aka we are enabled) //Only run the actual processing if the sensitivity is set (aka we are enabled)
if (systemSettings.sensitivity) { if (systemSettings.sensitivity) {
@@ -898,14 +911,13 @@ void startMOVTask(void const * argument) {
//So now we have averages, we want to look if these are different by more than the threshold //So now we have averages, we want to look if these are different by more than the threshold
int32_t error = (abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz)); int32_t error = (abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz));
//If error has occured then we update the tick timer
if (error > threshold) { if (error > threshold) {
lastMovementTime = HAL_GetTick(); lastMovementTime = HAL_GetTick();
} }
} }
osDelay(20); //Slow down update rate osDelay(100); //Slow down update rate
} }
} }