Skip to content
Snippets Groups Projects
Commit 3fe13756 authored by Isabel Morales's avatar Isabel Morales
Browse files

I2C without STP condition - BioHub SEN_15219 Application mode working

parent 001e158b
No related branches found
No related tags found
No related merge requests found
......@@ -22,8 +22,6 @@
#define SCL (0X0007)
#define SDA (0X0006)
#define BYTE_COUNTER 2 //Initial value for counter
#ifndef INCLUDE_I2C_H_
#define INCLUDE_I2C_H_
......@@ -37,17 +35,13 @@ void initializeI2C(void);
/**
* @brief Starts transmission of a generic write command
* @param int slave_address: Slave address to communicate with master
*
*/
void I2CTxOperation(int slave_address);
void I2CTxOperation();
/**
* @brief Starts reception of a generic read command
* @param int slave_address: Slave address to communicate with master
*
*/
void I2CRxOperation(int slave_address);
void I2CRxOperation();
/**
* @brief Sets the I2C pointer to a
......
......@@ -37,6 +37,8 @@
#define GET_HUB_STATUS 0x00
#define GET_HUB_MODE 0x02
#define SET_HUB_OUTPUT_MODE 0x10
#define SET_HUB_ENABLE_SENSOR 0x44
#define SET_HUB_ENABLE_ALGORITHM 0x52
//INDEX BYTE
#define INDEX_BYTE_0 0X00
......@@ -46,6 +48,10 @@
#define INDEX_BYTE_4 0X04
#define INDEX_BYTE_5 0X05
#define INDEX_BYTE_ENABLE 0X01
#define INDEX_BYTE_DISABLE 0X00
//WRITE BYTES
//Family Byte: SET_HUB_OUTPUT_MODE (0x10) and INDEX_BYTE_0: (0X00)
// OUTPUT_MODE_WRITE_BYTE
......
......@@ -22,34 +22,31 @@ void main(void)
__enable_irq();
initializeSen15219();
//Read I2C initializaModeSen15219 status
//I2CRxOperation(0x55);
char flag_main = 0;
setFlagI2C(&flag_main);
while(flag_main==0){
I2CTxOperation(0x55);
__delay_cycles(60);
flag_main=getI2CResponse();
}
while(flag_main==1){
I2CRxOperation(0x55);
__delay_cycles(60);
flag_main=getI2CResponse();}
while(1)
{
if(!functionQueueIsEmpty())
{
task = getFromFunctionQueue();
task();
if(flag_main==0)
{
I2CTxOperation(0x55);
}else if(flag_main==1)
{
__delay_cycles(60);
//Read I2C initializaModeSen15219 status
I2CRxOperation(0x55);
}
}
else
{
__low_power_mode_0();
}
}
}
......
......@@ -4,19 +4,24 @@
#include "common.h"
#include "stdint.h"
static int counter = BYTE_COUNTER;
#define COMMAND_WORD_SIZE 2
static int status = 0x00;
uint8_t RXData[2] = {0};
uint8_t RXDataPointer;
int i=0;
static char *flag_i2rx;
uint8_t ReadByte[0] = {0};
uint8_t BytePointer;
uint8_t Word[3];
uint8_t CommandWordSize;
uint8_t Family_byte;
uint8_t Index_byte;
uint8_t Write_byte;
void initializeI2C(void)
{
//Green led output for testing
//TODO clear green led when merging to master
P2OUT &= ~BIT1;
P2DIR |= BIT1;
//Configure I2C pins P1.7 - SCL / P1.6 - SDA
P1SEL0 |= SCL;
P1SEL1 &= ~SCL;
......@@ -35,27 +40,22 @@ void initializeI2C(void)
EUSCI_B_CTLW0_MODE_3 |EUSCI_B_CTLW0_SYNC |
EUSCI_B_CTLW0_SSEL__SMCLK;
// ASTP enabled. Automatically generates a STOP condition
//when (UCBxTBCNT) counter value is reached - BCNTIGF flag
EUSCI_B0->CTLW1 |= EUSCI_B_CTLW1_ASTP_2;
//_0 UCBxTBCNT is a dont care. The STOP condition is generated by the user
EUSCI_B0->CTLW1 |= EUSCI_B_CTLW1_ASTP_0;
//TODO Increase to 400kHz
// SMCLK / BRW = 100kHz - desired baudrate (12MHz/100kHz)=120
EUSCI_B0->BRW = 120;
// Byte counter threshold register. Sets the number of data bytes
// after which the UCSTPIFG EUSCI_B_IFG_STPIFG
//(or automatic stop) should occur. STPx must be different from 0
EUSCI_B0->TBCNT = counter;
//Slave address
EUSCI_B0->I2CSA = 0x55;
//Turn on the module
EUSCI_B0->CTLW0 &= ~EUSCI_A_CTLW0_SWRST;
//Enable RX interrupts:receive, NACK and byte counter interrupt
EUSCI_B0->IE |= EUSCI_B_IE_RXIE | EUSCI_B_IE_NACKIE | EUSCI_B_IE_BCNTIE ;
EUSCI_B0->IE |= EUSCI_B_IE_RXIE | EUSCI_B_IE_NACKIE ;
//Enable TX interrupts: transmit and stop interrupt
EUSCI_B0->IE |= EUSCI_B_IE_TXIE;
EUSCI_B0->IE |= EUSCI_B_IE_TXIE | EUSCI_B_IE_STPIE;
NVIC_SetPriority(EUSCIB0_IRQn,0);
NVIC_EnableIRQ(EUSCIB0_IRQn);
......@@ -63,18 +63,15 @@ void initializeI2C(void)
void I2CRxOperation(int slave_address)
{
BytePointer=0;
//UCTR Receiver mode
// Send NACK before the STOP condition as a master receiver
EUSCI_B0->CTLW0 &= ~EUSCI_B_CTLW0_TR;
// SLAVE address
//EUSCI_B0->I2CSA = slave_address;
//while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);
while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);
// I2C START RX condition
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
// Time between messages
// __delay_cycles(60);
status = 0x00;
}
......@@ -91,87 +88,89 @@ int getI2CResponse(void)
void I2CTxOperation(int slave_address)
{
char value;
//Initialize Command Word
//Read the device operating mode
Family_byte = 0x02;
Index_byte = 0x00;
//Don't care
Write_byte = 0x00;
Word[0] = Family_byte;
Word[1] = Index_byte;
Word[2] = Write_byte;
CommandWordSize =0;
//UCTR Transmitter mode
// Send ACK before the STOP condition as a master transmitter
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TR;
// SLAVE address
//EUSCI_B0->I2CSA = slave_address;
//STOP condition sent reassured
//TODO ASSERT????
//while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);
while (EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTP);
// I2C START condition
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
// Time between messages
// __delay_cycles(60);
}
void EUSCIB0_IRQHandler(void)
{
//Writing the UCTXBUF clears UCTXIFG0
//Write byte
if (EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG0)
{
if(i==0){
//family byte
EUSCI_B0->TXBUF = 0x02; //enable sensor 30101
i++;
}
else if(i==1){
//Index byte
EUSCI_B0->TXBUF = 0x00; // enable max30101
i=0;
}
P2OUT &= ~BIT1;
__low_power_mode_off_on_exit();
// Only transmit the data if it is less than the
// transmit data size
if (CommandWordSize < COMMAND_WORD_SIZE)
{
EUSCI_B0->TXBUF =Word[CommandWordSize] ;
CommandWordSize++;
}
//IFG interrupt flag register and Byte counter interrupt flag
//A stop condition is automatically generated see line 34.
//TODO switch case to 3 data, 5 data, 1 data
if (EUSCI_B0->IFG & EUSCI_B_IFG_BCNTIFG)
else
{
// This byte does not get clocked out
EUSCI_B0->TXBUF = 0;
// I2C STOP condition triggered on the last byte
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTP;
// Reset the queue
// flag to receive status byte
*flag_i2rx=1;
EUSCI_B0->IFG &= ~ EUSCI_B_IFG_BCNTIFG;
CommandWordSize=0;
}
////Reading the UCRXBUF clears UCRXIFG0
if (EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG0)
{
RXData[RXDataPointer++] = EUSCI_B0->RXBUF;
if (RXDataPointer > sizeof(RXData))
{
RXDataPointer = 0;
}
//READ bYTE
//In case of a NACK
/*if (EUSCI_B0->IFG & EUSCI_B_IFG_NACKIFG)
{
EUSCI_B0->IFG &= ~ EUSCI_B_IFG_NACKIFG;
// Get RX data UCRX IFGX flag is reseted when RXBUF readed
// I2C start condition
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
}*/
if (EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG0)
{
//EUSCI_B0->IFG &= ~ EUSCI_B_IFG_RXIFG0;
// Get RX data
ReadByte[BytePointer] = EUSCI_B0->RXBUF;
BytePointer++;
//addToQueue(EUSCI_B0->RXBUF,&head_rx,rx_buffer);
// Function queue
if (BytePointer > sizeof(ReadByte))
{
BytePointer = 0;
// I2C STOP condition
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTP;
// Wake up on exit
*flag_i2rx=0;
__low_power_mode_off_on_exit();
}
//IFG interrupt flag register and Byte counter interrupt flag
//A stop condition is automatically generated see line 34.
//TODO switch case to 3 data, 5 data, 1 data
if (EUSCI_B0->IFG & EUSCI_B_IFG_BCNTIFG)
{
EUSCI_B0->IFG &= ~ EUSCI_B_IFG_BCNTIFG;
}
//IFG interrupt flag register and NACK interrupt flag
//Slave did not ack then NACKIFG up. A STOP o RESTART is requested.
if (EUSCI_B0->IFG & EUSCI_B_IFG_NACKIFG)
//STPIFG flag is set when read/write ends
if (EUSCI_B0->IFG & EUSCI_B_IFG_STPIFG)
{
EUSCI_B0->IFG &= ~ EUSCI_B_IFG_NACKIFG;
// Clear stop condition clear flag
EUSCI_B0->IFG &= ~EUSCI_B_IFG_STPIFG;
//In master receiver mode a repeated START condition is preceded by a NACK
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;
}
}
......@@ -24,6 +24,8 @@ void initializeSen15219(void)
//Application mode
initializaModeSen15219(0);
//Configuration mode
}
int initializaModeSen15219(int mode)
......@@ -41,15 +43,36 @@ int initializaModeSen15219(int mode)
while((current_time+1000)>getAuxiliaryMilisecondsTime());//After 1 sec, enable MFIO interrupt
P6REN |= MFIO; //Enable interrupt
//Read I2C initializaModeSen15219 status
//I2CRxOperation(0x55);
}
return status;
}
void configureSen15219(void)
{
//int configureSen15219(int mode)
//{
//Configures BioHub Sen15219 to read:
//Heartrate
//SpO2
//Confidence
//Finger presence
// if(mode==0)
// {
//Delay for at least 100ms to start reading samples
// current_time=getCurrentMilisecondsTime();
// while((current_time+100)>getAuxiliaryMilisecondsTime());
// }
/// return status;
//}
//unsigned int commandWord(unsigned int family_byte, unsigned int index_byte, unsigned int write_byte)
//{
// return status;
//}
}
void setSen15219Function(void *callback_function)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment