Skip to content

Commit

Permalink
Adding auto-rotation to the screen
Browse files Browse the repository at this point in the history
Adds auto rotation support using the proper orientation detection. (Not
using raw values).
Should Fix and close #29
  • Loading branch information
Ralim committed Jul 27, 2017
1 parent af9b8dc commit 1cbcba9
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 116 deletions.
3 changes: 2 additions & 1 deletion workspace/ts100/inc/I2C.h
Expand Up @@ -11,6 +11,7 @@

void I2C_Configuration(void);
void I2C_PageWrite(u8* pbuf, u8 numbyte, u8 deviceaddr);
void I2C_PageRead(u8* pbuf, u8 numbyte, u8 deviceaddr, u8 readaddr);
int I2C_Master_Read(uint8_t deviceAddr, uint8_t readAddr, uint8_t* pBuffer,
uint16_t numByteToRead);
#endif
/******************************** END OF FILE *********************************/
9 changes: 3 additions & 6 deletions workspace/ts100/inc/MMA8652FC.h
Expand Up @@ -14,9 +14,8 @@
#ifndef __MMA8652FC__H
#define __MMA8652FC__H


void StartUp_Accelerometer(uint8_t sensitivity);//This is the only function we expose

void StartUp_Accelerometer(uint8_t sensitivity); //This is the only function we expose
uint8_t getOrientation();
//--------------MMA8652 Device ID----------------------------------------------//

#define DEVICE_ADDR 0X1D
Expand Down Expand Up @@ -99,8 +98,6 @@ void StartUp_Accelerometer(uint8_t sensitivity);//This is the only function we e
#define INT_SOURCE 0X0C
#define DEVICE_ID 0X0D



//-----STATUS_REG(0X00)-----Bit Define----------------------------------------//
#define ZYXDR_BIT 0X08
//----XYZ_DATA_CFG_REG(0xE)-Bit Define----------------------------------------//
Expand All @@ -123,7 +120,7 @@ void StartUp_Accelerometer(uint8_t sensitivity);//This is the only function we e
//---------CTRL_REG2(0X2B)Bit Define------------------------------------------//
#define MODS_MASK 0x03 //Oversampling Mode 4
#define Normal_Mode 0x0 //Normal=0,Low Noise Low Power MODS=1,
//HI RESOLUTION=2,LOW POWER MODS = 11
//HI RESOLUTION=2,LOW POWER MODS = 11
//----CTRL_REG4---Interrupt Enable BIT ---------------------------------------//
//0 interrupt is disabled (default)
//1 interrupt is enabled
Expand Down
3 changes: 2 additions & 1 deletion workspace/ts100/inc/Modes.h
Expand Up @@ -16,6 +16,7 @@
#include "PID.h"
#include "Settings.h"
#include "Analog.h"
#include "MMA8652FC.h"
#include <string.h>
typedef enum {
STARTUP, //we are sitting on the prompt to push a button
Expand All @@ -41,7 +42,7 @@ typedef enum {
TEMPDISPLAY,
TEMPROUNDING,
DISPUPDATERATE,
LEFTY,
SCREENROTATION,
BOOSTMODE,
BOOSTTEMP,
POWERDISPLAY,
Expand Down
3 changes: 2 additions & 1 deletion workspace/ts100/inc/Oled.h
Expand Up @@ -32,5 +32,6 @@ void OLED_DrawThreeNumber(uint16_t in, uint8_t x);
void OLED_DrawIDLELogo();
void OLED_DrawSymbol(uint8_t x, uint8_t symbol);
const u8* Oled_DrawArea(u8 x0, u8 y0, u8 wide, u8 high, const u8* ptr);
void OLED_SetOrientation(uint8_t ori);
uint8_t OLED_GetOrientation();
#endif
/******************************** END OF FILE *********************************/
6 changes: 3 additions & 3 deletions workspace/ts100/inc/Settings.h
Expand Up @@ -12,7 +12,7 @@
#include <stdint.h>
#include "stm32f10x_flash.h"
#include "Oled.h"
#define SETTINGSVERSION 13 /*Change this if you change the struct below to prevent people getting out of sync*/
#define SETTINGSVERSION 14 /*Change this if you change the struct below to prevent people getting out of sync*/
//Display Speeds
#define DISPLAYMODE_FAST (0x00)
#define DISPLAYMODE_MEDIUM (0x01)
Expand All @@ -30,10 +30,10 @@ typedef struct {
uint32_t SleepTemp; //temp to drop to in sleep
uint8_t version; //Used to track if a reset is needed on firmware upgrade
uint8_t SleepTime; //minutes timeout to sleep
uint8_t cutoutSetting:4; //(3 bits) The voltage we cut out at for under voltage
uint8_t cutoutSetting:3; //(3 bits) The voltage we cut out at for under voltage
uint8_t powerDisplay:1; //Toggle to swap the arrows with a power readout instead
uint8_t displayTempInF:1; //If we need to convert the C reading to F
uint8_t flipDisplay:1; //If true we want to invert the display for lefties
uint8_t OrientationMode:2; //If true we want to invert the display for lefties
uint8_t sensitivity:6; //Sensitivity of accelerometer (5 bits)
uint8_t ShutdownTime:6; //Time until unit shuts down if left alone
uint8_t displayUpdateSpeed:2; //How fast the display updates / temp showing mode
Expand Down
266 changes: 206 additions & 60 deletions workspace/ts100/src/I2C.c
Expand Up @@ -4,6 +4,19 @@
*/
#include "I2C.h"

/* I2C STOP mask */
#define CR1_STOP_Set ((uint16_t)0x0200)
#define CR1_STOP_Reset ((uint16_t)0xFDFF)

/* I2C ACK mask */
#define CR1_ACK_Set ((uint16_t)0x0400)
#define CR1_ACK_Reset ((uint16_t)0xFBFF)

/* I2C POS mask */
#define CR1_POS_Set ((uint16_t)0x0800)
#define CR1_POS_Reset ((uint16_t)0xF7FF)

#define NULL ((void *)0)
/*
* Configure the I2C port hardware
*/
Expand Down Expand Up @@ -60,89 +73,222 @@ void I2C_PageWrite(u8* buf, u8 nbyte, u8 deviceaddr) {
}

}
//Based on code from http://iamjustinwang.blogspot.com.au/2016/03/stm32f103-i2c-master-driver.html
int I2C_Master_Read(uint8_t deviceAddr, uint8_t readAddr, uint8_t* pBuffer,
uint16_t numByteToRead) {

/*
* Read Page of data using I2C1 peripheral
*/
__IO uint32_t temp = 0;
volatile int I2C_TimeOut = 0;

void I2C_PageRead(u8* buf, u8 nbyte, u8 deviceaddr, u8 readaddr) {
I2C_GenerateSTART(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_SB) == RESET)
;
I2C_Send7bitAddress(I2C1, deviceaddr << 1, I2C_Direction_Transmitter);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR) == RESET)
;
I2C_GetFlagStatus(I2C1, I2C_FLAG_MSL);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) == RESET)
;
// Send an 8bit byte address
I2C_SendData(I2C1, readaddr);
// /* While the bus is busy * /
I2C_TimeOut = 3000;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);

// * Send START condition * /
I2C_GenerateSTART(I2C1, ENABLE);

// / * Test on EV5 and clear it * /
I2C_TimeOut = 3000;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}

// / * Send address for write * /
I2C_Send7bitAddress(I2C1, deviceAddr, I2C_Direction_Transmitter);

// / * Test on EV6 and clear it * /
I2C_TimeOut = 3000;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
I2C_Send7bitAddress(I2C1, deviceaddr << 1, I2C_Direction_Receiver);
while (!I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR)) {

// / * Send the internal address to read from: Only one byte address * /
I2C_SendData(I2C1, readAddr);

/// * Test on EV8 and clear it * /
I2C_TimeOut = 3000;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
if (nbyte == 1) {
// Clear Ack bit
I2C_AcknowledgeConfig(I2C1, DISABLE);
// EV6_1 -- must be atomic -- Clear ADDR, generate STOP

/// * Send STRAT condition a second time * /
I2C_GenerateSTART(I2C1, ENABLE);

/// * Test on EV5 and clear it * /
I2C_TimeOut = 3000;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}

// * Send address for read * /
I2C_Send7bitAddress(I2C1, deviceAddr, I2C_Direction_Receiver);

if (numByteToRead == 1) {
/* Wait until ADDR is set */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x0002) != 0x0002) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* Clear ACK bit */
I2C1->CR1 &= CR1_ACK_Reset;
/* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
software sequence must complete before the current byte end of transfer */
__disable_irq();
(void) I2C1->SR2;
/* Clear ADDR flag */
temp = I2C1->SR2;
/* Program the STOP */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Re-enable IRQs */
__enable_irq();
// Receive data EV7
while (!I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE)) {
/* Wait until a data is received in DR register (RXNE = 1) EV7 */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x00040) != 0x000040) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
*buf++ = I2C_ReceiveData(I2C1);
} else if (nbyte == 2) {
// Set POS flag
I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Next);
// EV6_1 -- must be atomic and in this order
/* Read the data */
*pBuffer = I2C1->DR;

} else if (numByteToRead == 2) {

/* Set POS bit */
I2C1->CR1 |= CR1_POS_Set;
/* Wait until ADDR is set: EV6 */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x0002) != 0x0002) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* EV6_1: The acknowledge disable should be done just after EV6,
that is after ADDR is cleared, so disable all active IRQs around ADDR clearing and
ACK clearing */
__disable_irq();
(void) I2C1->SR2; // Clear ADDR flag
I2C_AcknowledgeConfig(I2C1, DISABLE); // Clear Ack bit
/* Clear ADDR by reading SR2 register */
temp = I2C1->SR2;
/* Clear ACK */
I2C1->CR1 &= CR1_ACK_Reset;
/*Re-enable IRQs */
__enable_irq();
// EV7_3 -- Wait for BTF, program stop, read data twice
while (!I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF)) {
/* Wait until BTF is set */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x00004) != 0x000004) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* Disable IRQs around STOP programming and data reading */
__disable_irq();
/* Program the STOP */
I2C_GenerateSTOP(I2C1, ENABLE);
*buf++ = I2C1->DR;
/* Read first data */
*pBuffer = I2C1->DR;
/* Re-enable IRQs */
__enable_irq();
*buf++ = I2C1->DR;
} else {
(void) I2C1->SR2; // Clear ADDR flag
while (nbyte-- != 3) {
// EV7 -- cannot guarantee 1 transfer completion time, wait for BTF
// instead of RXNE
while (!I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF)) {
/**/
pBuffer++;
/* Read second data */
*pBuffer = I2C1->DR;
/* Clear POS bit */
I2C1->CR1 &= CR1_POS_Reset;
}

else { //numByteToRead > 2
// * Test on EV6 and clear it * /
I2C_TimeOut = 3000;
while (!I2C_CheckEvent(I2C1,
I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
*buf++ = I2C_ReceiveData(I2C1);
}
// * While there is data to be read * /
while (numByteToRead) {
/* Receive bytes from first byte until byte N-3 */
if (numByteToRead != 3) {
/* Poll on BTF to receive data because in polling mode we can not guarantee the
EV7 software sequence is managed before the current byte transfer completes */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x00004) != 0x000004) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* Read data */
*pBuffer = I2C1->DR;
pBuffer++;
/* Decrement the read bytes counter */
numByteToRead--;
}

while (!I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF)) {
}
// EV7_2 -- Figure 1 has an error, doesn't read N-2 !
I2C_AcknowledgeConfig(I2C1, DISABLE); // clear ack bit
__disable_irq();
*buf++ = I2C_ReceiveData(I2C1); // receive byte N-2
I2C_GenerateSTOP(I2C1, ENABLE); // program stop
__enable_irq();
*buf++ = I2C_ReceiveData(I2C1); // receive byte N-1
// wait for byte N
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
/* it remains to read three data: data N-2, data N-1, Data N */
if (numByteToRead == 3) {
/* Wait until BTF is set: Data N-2 in DR and data N -1 in shift register */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x00004) != 0x000004) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* Clear ACK */
I2C1->CR1 &= CR1_ACK_Reset;

/* Disable IRQs around data reading and STOP programming */
__disable_irq();
/* Read Data N-2 */
*pBuffer = I2C1->DR;
/* Increment */
pBuffer++;
/* Program the STOP */
I2C1->CR1 |= CR1_STOP_Set;
/* Read DataN-1 */
*pBuffer = I2C1->DR;
/* Re-enable IRQs */
__enable_irq();
/* Increment */
pBuffer++;
/* Wait until RXNE is set (DR contains the last data) */
I2C_TimeOut = 3000;
while ((I2C1->SR1 & 0x00040) != 0x000040) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
/* Read DataN */
*pBuffer = I2C1->DR;
/* Reset the number of bytes to be read by master */
numByteToRead = 0;
}
}
*buf++ = I2C_ReceiveData(I2C1);
nbyte = 0;
}
// Wait for stop
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)) {

/* Make sure that the STOP bit is cleared by Hardware before CR1 write access */
I2C_TimeOut = 3000;
while ((I2C1->CR1 & 0x200) == 0x200) {
if (I2C_TimeOut-- <= 0) {
return 1;
}
}
return;

// * Enable Acknowledgment to be ready for another reception * /
I2C_AcknowledgeConfig(I2C1, ENABLE);

return 0;

}

0 comments on commit 1cbcba9

Please sign in to comment.