Compare commits
4 Commits
d4136f0761
...
cb232ea55e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb232ea55e | ||
|
|
8ce717033a | ||
|
|
75132eb040 | ||
|
|
7b32859c88 |
@@ -22,6 +22,16 @@ void SHAL_init();
|
||||
|
||||
//Universal structs and defines ---------------------------
|
||||
|
||||
typedef bool (*condition_fn_t)(void);
|
||||
|
||||
#define SHAL_WAIT_FOR_CONDITION_US(cond, timeout_us) \
|
||||
SHAL_wait_for_condition_us([&](){ return (cond); }, (timeout_us))
|
||||
|
||||
#define SHAL_WAIT_FOR_CONDITION_MS(cond, timeout_ms) \
|
||||
SHAL_wait_for_condition_ms([&](){ return (cond); }, (timeout_ms))
|
||||
|
||||
|
||||
|
||||
//Currently configures systick to count down in microseconds
|
||||
void systick_init();
|
||||
|
||||
@@ -30,6 +40,28 @@ void SHAL_delay_us(uint32_t us);
|
||||
|
||||
void SHAL_delay_ms(uint32_t ms);
|
||||
|
||||
template<typename Condition>
|
||||
bool SHAL_wait_for_condition_us(Condition cond, uint32_t timeout_us) {
|
||||
while (timeout_us--) {
|
||||
if (cond()) {
|
||||
return true; // success
|
||||
}
|
||||
SHAL_delay_us(1);
|
||||
}
|
||||
return false; // timeout
|
||||
}
|
||||
|
||||
template<typename Condition>
|
||||
bool SHAL_wait_for_condition_ms(Condition cond, uint32_t timeout_ms) {
|
||||
while (timeout_ms--) {
|
||||
if (cond()) {
|
||||
return true; // success
|
||||
}
|
||||
SHAL_delay_ms(1);
|
||||
}
|
||||
return false; // timeout
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ public:
|
||||
/// \param readLen number of bytes to be read
|
||||
void masterWriteRead(uint8_t addr,const uint8_t* writeData, size_t writeLen, uint8_t* readData, size_t readLen);
|
||||
|
||||
uint8_t masterWriteReadByte(uint8_t addr, const uint8_t* writeData, size_t writeLen);
|
||||
|
||||
/// Function to write an array of commands to an I2C device
|
||||
/// \param addr Address of slave device
|
||||
/// \param writeData Pointer to array of commands
|
||||
|
||||
@@ -10,18 +10,29 @@ void SHAL_init(){
|
||||
|
||||
|
||||
void systick_init(){
|
||||
SysTick->CTRL = 0; //disable first
|
||||
SysTick->LOAD = 0xFFFFFF; //max 24-bit
|
||||
SysTick->VAL = 0; //clear
|
||||
SysTick->CTRL = 0; //Disable first
|
||||
SysTick->LOAD = 0xFFFFFF; //Max 24-bit
|
||||
SysTick->VAL = 0; //Clear
|
||||
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
|
||||
}
|
||||
|
||||
void SHAL_delay_us(uint32_t us){
|
||||
uint32_t start = SysTick->VAL;
|
||||
uint32_t ticks = us * (SystemCoreClock / 1000000U);
|
||||
|
||||
//handle wraparound with 24-bit mask
|
||||
while (((start - SysTick->VAL) & 0x00FFFFFF) < ticks) { }
|
||||
void SHAL_delay_us(uint32_t us){
|
||||
uint32_t ticks = us * (SystemCoreClock / 1000000U);
|
||||
uint32_t start = SysTick->VAL;
|
||||
|
||||
//Calculate target value (may wrap around)
|
||||
uint32_t target = (start >= ticks) ? (start - ticks) : (start + 0x01000000 - ticks);
|
||||
target &= 0x00FFFFFF;
|
||||
|
||||
//Wait until we reach the target
|
||||
if (start >= ticks) {
|
||||
//No wraparound case
|
||||
while (SysTick->VAL > target) {}
|
||||
} else {
|
||||
while (SysTick->VAL <= start) {} //Wait for wraparound
|
||||
while (SysTick->VAL > target) {} //Wait for target
|
||||
}
|
||||
}
|
||||
|
||||
void SHAL_delay_ms(uint32_t ms){
|
||||
|
||||
@@ -5,15 +5,21 @@
|
||||
#include "SHAL_I2C.h"
|
||||
#include "SHAL_GPIO.h"
|
||||
|
||||
#include "SHAL_UART.h"
|
||||
|
||||
void SHAL_I2C::init(I2C_Pair pair) volatile {
|
||||
m_I2CPair = pair;
|
||||
|
||||
SHAL_I2C_Pair I2CPair = getI2CPair(pair); //Get the UART_PAIR information to be initialized
|
||||
SHAL_I2C_Pair I2CPair = getI2CPair(pair); //Get the I2C_PAIR information to be initialized
|
||||
|
||||
//Get the SHAL_GPIO pins for this SHAL_I2C setup
|
||||
GPIO_Key SCL_Key = I2CPair.SCL_Key; //SCL pin
|
||||
GPIO_Key SDA_Key = I2CPair.SDA_Key; //SDA pin
|
||||
|
||||
SHAL_I2C_Enable_Reg pairI2CEnable = getI2CEnableReg(pair); //Register and mask to enable the I2C peripheral
|
||||
|
||||
*pairI2CEnable.reg &= ~pairI2CEnable.mask; //Enable I2C peripheral clock
|
||||
|
||||
GET_GPIO(SCL_Key).setPinMode(PinMode::ALTERNATE_FUNCTION_MODE); //Implicitly initializes and enables GPIO bus
|
||||
GET_GPIO(SDA_Key).setPinMode(PinMode::ALTERNATE_FUNCTION_MODE);
|
||||
|
||||
@@ -30,13 +36,12 @@ void SHAL_I2C::init(I2C_Pair pair) volatile {
|
||||
GET_GPIO(SCL_Key).setInternalResistor(InternalResistorType::PULLUP);
|
||||
GET_GPIO(SDA_Key).setInternalResistor(InternalResistorType::PULLUP);
|
||||
|
||||
SHAL_I2C_Enable_Reg pairI2CEnable = getI2CEnableReg(pair); //Register and mask to enable the I2C peripheral
|
||||
SHAL_I2C_Reset_Reg pairI2CReset = getI2CResetReg(pair);
|
||||
|
||||
*pairI2CReset.reg |= pairI2CReset.mask; //Reset peripheral
|
||||
*pairI2CEnable.reg |= pairI2CEnable.mask; //Enable I2C peripheral clock
|
||||
|
||||
I2CPair.I2CReg->CR1 |= I2C_CR1_PE; //Enable I2C peripheral
|
||||
*pairI2CReset.reg |= pairI2CReset.mask; //Reset peripheral
|
||||
*pairI2CReset.reg &= ~pairI2CReset.mask; //Reset peripheral
|
||||
}
|
||||
|
||||
void SHAL_I2C::setClockConfig(uint8_t prescaler, uint8_t dataSetupTime, uint8_t dataHoldTime, uint8_t SCLHighPeriod, uint8_t SCLLowPeriod) {
|
||||
@@ -48,46 +53,68 @@ void SHAL_I2C::setClockConfig(uint8_t prescaler, uint8_t dataSetupTime, uint8_t
|
||||
*clockReg.reg |= (dataHoldTime << clockReg.dataHoldTime_offset);
|
||||
*clockReg.reg |= (SCLHighPeriod << clockReg.SCLHighPeriod_offset);
|
||||
*clockReg.reg |= (SCLLowPeriod << clockReg.SCLLowPeriod_offset);
|
||||
|
||||
getI2CPair(m_I2CPair).I2CReg->CR1 |= I2C_CR1_PE; //Enable I2C peripheral
|
||||
}
|
||||
|
||||
void SHAL_I2C::setClockConfig(uint32_t configuration) {
|
||||
*getI2CTimerReg(m_I2CPair).reg = configuration;
|
||||
|
||||
getI2CPair(m_I2CPair).I2CReg->CR1 |= I2C_CR1_PE; //Enable I2C peripheral
|
||||
}
|
||||
|
||||
void SHAL_I2C::masterWriteRead(uint8_t addr,const uint8_t* writeData, size_t writeLen, uint8_t* readData, size_t readLen) {
|
||||
|
||||
volatile I2C_TypeDef* I2CPeripheral = getI2CPair(m_I2CPair).I2CReg;
|
||||
|
||||
//Wait for I2C bus
|
||||
while (I2CPeripheral->ISR & I2C_ISR_BUSY);
|
||||
if(!SHAL_WAIT_FOR_CONDITION_MS((I2CPeripheral->ISR & I2C_ISR_BUSY) == 0, 100)){
|
||||
SHAL_UART2.sendString("I2C timed out waiting for not busy\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//Write phase
|
||||
if (writeLen > 0) {
|
||||
//Configure: NBYTES = wlen, write mode, START
|
||||
I2CPeripheral->CR2 = (addr << 1) |
|
||||
(writeLen << I2C_CR2_NBYTES_Pos) |
|
||||
I2C_CR2_START;
|
||||
I2CPeripheral->CR2 = (addr << 1) | (writeLen << I2C_CR2_NBYTES_Pos) | I2C_CR2_START;
|
||||
|
||||
for (size_t i = 0; i < writeLen; i++) {
|
||||
while (!(I2CPeripheral->ISR & I2C_ISR_TXIS)); // TX ready
|
||||
if(!SHAL_WAIT_FOR_CONDITION_MS((I2CPeripheral->ISR & I2C_ISR_TXIS) != 0, 100)){
|
||||
SHAL_UART2.sendString("I2C timed out waiting for TX\r\n");
|
||||
return;
|
||||
}
|
||||
I2CPeripheral->TXDR = writeData[i];
|
||||
}
|
||||
|
||||
//Wait until transfer complete
|
||||
while (!(I2CPeripheral->ISR & I2C_ISR_TC));
|
||||
if(!SHAL_WAIT_FOR_CONDITION_MS((I2CPeripheral->ISR & I2C_ISR_TC) != 0, 100)){
|
||||
SHAL_UART2.sendString("I2C timed out waiting for TC\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//Read phase
|
||||
if (readLen > 0) {
|
||||
I2CPeripheral->CR2 = (addr << 1) |
|
||||
|
||||
SHAL_UART2.sendString("Read initiated\r\n");
|
||||
|
||||
I2CPeripheral->CR2 &= ~(I2C_CR2_NBYTES | I2C_CR2_SADD | I2C_CR2_RD_WRN);
|
||||
I2CPeripheral->CR2 |= (addr << 1) |
|
||||
I2C_CR2_RD_WRN |
|
||||
(readLen << I2C_CR2_NBYTES_Pos) |
|
||||
I2C_CR2_START | I2C_CR2_AUTOEND;
|
||||
|
||||
for (size_t i = 0; i < readLen; i++) {
|
||||
while (!(I2CPeripheral->ISR & I2C_ISR_RXNE)); //RX ready
|
||||
if(!SHAL_WAIT_FOR_CONDITION_MS((I2CPeripheral->ISR & I2C_ISR_RXNE) != 0 , 100)){
|
||||
SHAL_UART2.sendString("I2C timed out waiting for RXNE\r\n");
|
||||
return;
|
||||
}
|
||||
SHAL_UART2.sendString("Read byte");
|
||||
readData[i] = static_cast<uint8_t>(I2CPeripheral->RXDR);
|
||||
}
|
||||
}
|
||||
else{
|
||||
I2CPeripheral->CR2 |= I2C_CR2_STOP;
|
||||
}
|
||||
}
|
||||
|
||||
void SHAL_I2C::masterWrite(uint8_t addr, const uint8_t *writeData, uint8_t writeLen) {
|
||||
@@ -98,6 +125,12 @@ void SHAL_I2C::masterRead(uint8_t addr, uint8_t *readBuffer, uint8_t bytesToRead
|
||||
masterWriteRead(addr,nullptr,0,readBuffer,bytesToRead);
|
||||
}
|
||||
|
||||
uint8_t SHAL_I2C::masterWriteReadByte(uint8_t addr, const uint8_t *writeData, size_t writeLen) {
|
||||
uint8_t val = 0;
|
||||
masterWriteRead(addr, writeData, writeLen, &val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
SHAL_I2C& I2CManager::get(uint8_t I2CBus) {
|
||||
|
||||
if(I2CBus > NUM_I2C_BUSES - 1){
|
||||
|
||||
@@ -1,10 +1,44 @@
|
||||
#include "SHAL.h"
|
||||
#include "stm32f0xx.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void c3Interrupt(){
|
||||
PIN(A5).toggle();
|
||||
UART(2).sendString("New test");
|
||||
SHAL_UART2.sendString("Begin\r\n");
|
||||
|
||||
uint8_t cmd[3] = {0xAC, 0x33, 0x00};
|
||||
SHAL_I2C1.masterWrite(0x38, cmd, 3);
|
||||
|
||||
SHAL_delay_ms(100);
|
||||
|
||||
uint8_t dht_buf[7] = {0};
|
||||
|
||||
//Read 7 bytes (status + 5 data + CRC)
|
||||
SHAL_I2C1.masterRead(0x38, dht_buf, 7);
|
||||
|
||||
//Parse humidity (20 bits)
|
||||
uint32_t rawHumidity = ((uint32_t)dht_buf[1] << 12) |
|
||||
((uint32_t)dht_buf[2] << 4) |
|
||||
((uint32_t)dht_buf[3] >> 4);
|
||||
|
||||
uint32_t rawTemp = (((uint32_t)dht_buf[3] & 0x0F) << 16) |
|
||||
((uint32_t)dht_buf[4] << 8) |
|
||||
((uint32_t)dht_buf[5]);
|
||||
|
||||
// Use 64-bit intermediate to avoid overflow
|
||||
uint32_t hum_hundredths = (uint32_t)(((uint64_t)rawHumidity * 10000ULL) >> 20);
|
||||
int32_t temp_hundredths = (int32_t)((((uint64_t)rawTemp * 20000ULL) >> 20) - 5000);
|
||||
|
||||
char out[80];
|
||||
sprintf(out, "rawH=0x%05lX rawT=0x%05lX\r\n",
|
||||
(unsigned long)rawHumidity, (unsigned long)rawTemp);
|
||||
SHAL_UART2.sendString(out);
|
||||
|
||||
// print as X.YY
|
||||
sprintf(out, "Temp: %ld.%02ld C, Hum: %ld.%02ld %%\r\n",
|
||||
(long)(temp_hundredths / 100), (long)(abs(temp_hundredths % 100)),
|
||||
(long)(hum_hundredths / 100), (long)(hum_hundredths % 100));
|
||||
SHAL_UART2.sendString(out);
|
||||
}
|
||||
|
||||
void tim2Handler(){
|
||||
@@ -20,6 +54,7 @@ int main() {
|
||||
SHAL_UART2.begin(115200);
|
||||
|
||||
SHAL_I2C1.init(I2C_Pair::SCL1B6_SDA1B7);
|
||||
SHAL_I2C1.setClockConfig(0x2000090E);
|
||||
|
||||
//Use pin C3 to trigger a function on external interrupt
|
||||
PIN(C3).useAsExternalInterrupt(TriggerMode::RISING_EDGE,c3Interrupt);
|
||||
@@ -31,11 +66,24 @@ int main() {
|
||||
PIN(A4).setPinMode(PinMode::OUTPUT_MODE);
|
||||
PIN(A5).setPinMode(PinMode::OUTPUT_MODE);
|
||||
|
||||
|
||||
|
||||
SHAL_delay_ms(3000); //Wait 100 ms from datasheet
|
||||
|
||||
uint8_t cmd = 0x71;
|
||||
uint8_t status = 0;
|
||||
|
||||
SHAL_I2C1.masterWriteRead(0x38, &cmd, 1, &status, 1);
|
||||
|
||||
char statusString[32];
|
||||
sprintf(statusString, "Status = 0x%02X\r\n", status);
|
||||
SHAL_UART2.sendString(statusString);
|
||||
|
||||
|
||||
SHAL_delay_ms(10);
|
||||
|
||||
c3Interrupt();
|
||||
|
||||
SHAL_delay_ms(3000);
|
||||
|
||||
c3Interrupt(); //test
|
||||
//End setup
|
||||
|
||||
while (true) {
|
||||
__WFI();
|
||||
|
||||
Reference in New Issue
Block a user