Fixed references to missing functions

This commit is contained in:
Jan Mrna 2022-06-14 22:47:00 +02:00
parent a0ed61200d
commit cd15bbfa24
2 changed files with 61 additions and 47 deletions

104
sgp40.c
View File

@ -5,8 +5,26 @@
* Author: david
*/
#include <sgp40.h>
#include "main.h" /* for uart_disable_interrupts() */
#include "sgp40.h"
/*
* Functions to be implemented by user
*/
/* I2C */
int8_t sgp40_i2c_transmit(uint8_t address, uint8_t *buffer, int len) __attribute__((weak));
int8_t sgp40_i2c_receive(uint8_t address, uint8_t *buffer, int len) __attribute__((weak));
/* CRC */
uint8_t sensirion_crc8_calculate(const uint8_t *data, uint16_t count) __attribute__((weak));
/* Interrupts */
int8_t sgp40_disable_interrupts(void) __attribute__((weak));
int8_t sgp40_enable_interrupts(void) __attribute__((weak));
/* delay function */
void delay_ms(int delay_ms) __attribute__((weak));
/*
* Public functions
*/
int8_t sgp40_send_cmd(sgp40_cmd_t cmd)
{
@ -16,10 +34,10 @@ int8_t sgp40_send_cmd(sgp40_cmd_t cmd)
// start measurement
buffer[0] = cmd >> 8;
buffer[1] = cmd & 0x00ff;
uart_disable_interrupts();
result = i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 2);
uart_enable_interrupts();
if (result == I2C_ERROR_TX_INCOMPLETE) {
sgp40_disable_interrupts();
result = sgp40_i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 2);
sgp40_enable_interrupts();
if (result != 0) {
return SGP40_ERROR;
}
@ -43,26 +61,26 @@ int8_t sgp40_measure_raw_signal(uint16_t * voc_ticks)
buffer[7] = 0x93;
/* Returns NACK if CRC is wrong */
uart_disable_interrupts();
result = i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 8);
uart_enable_interrupts();
if (result != I2C_OK) {
sgp40_disable_interrupts();
result = sgp40_i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 8);
sgp40_enable_interrupts();
if (result != 0) {
return SGP40_ERROR;
}
LL_mDelay(SGP40_MAX_MEAS_DURATION_MS); // 30ms
delay_ms(SGP40_MAX_MEAS_DURATION_MS); // 30ms
uart_disable_interrupts();
result = i2c_receive(SGP40_I2C_ADDRESS<<1, buffer, 3);
uart_enable_interrupts();
if (result != I2C_OK)
sgp40_disable_interrupts();
result = sgp40_i2c_receive(SGP40_I2C_ADDRESS<<1, buffer, 3);
sgp40_enable_interrupts();
if (result != 0)
{
return SGP40_ERROR;
}
*voc_ticks = (buffer[0] << 8) + buffer[1];
uint8_t voc_ticks_crc = buffer[2];
uint8_t crc_correct = crc8_calculate(buffer, 2) == voc_ticks_crc;
uint8_t crc_correct = sensirion_crc8_calculate(buffer, 2) == voc_ticks_crc;
if (!crc_correct) {
return SGP40_CRC8_ERROR;
}
@ -82,32 +100,32 @@ int8_t sgp40_measure_raw_signal_compensated(uint16_t * voc_ticks, uint16_t relat
buffer[1] = SGP40_MEASURE_RAW_SIGNAL & 0x00ff;
buffer[2] = (uint8_t)(rh_ticks >> 8);
buffer[3] = (uint8_t)rh_ticks;
buffer[4] = crc8_calculate(buffer+2, 2);
buffer[4] = sensirion_crc8_calculate(buffer+2, 2);
buffer[5] = (uint8_t)(t_ticks >> 8);
buffer[6] = (uint8_t)t_ticks;
buffer[7] = crc8_calculate(buffer+5, 2);
buffer[7] = sensirion_crc8_calculate(buffer+5, 2);
/* Returns NACK if CRC is wrong */
uart_disable_interrupts();
result = i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 8);
uart_enable_interrupts();
if (result != I2C_OK) {
sgp40_disable_interrupts();
result = sgp40_i2c_transmit(SGP40_I2C_ADDRESS<<1, buffer, 8);
sgp40_enable_interrupts();
if (result != 0) {
return SGP40_ERROR;
}
LL_mDelay(SGP40_MAX_MEAS_DURATION_MS); // 30ms
delay_ms(SGP40_MAX_MEAS_DURATION_MS); // 30ms
uart_disable_interrupts();
result = i2c_receive(SGP40_I2C_ADDRESS<<1, buffer, 3);
uart_enable_interrupts();
if (result != I2C_OK)
sgp40_disable_interrupts();
result = sgp40_i2c_receive(SGP40_I2C_ADDRESS<<1, buffer, 3);
sgp40_enable_interrupts();
if (result != 0)
{
return SGP40_ERROR;
}
*voc_ticks = (buffer[0] << 8) + buffer[1];
uint8_t voc_ticks_crc = buffer[2];
uint8_t crc_correct = crc8_calculate(buffer, 2) == voc_ticks_crc;
uint8_t crc_correct = sensirion_crc8_calculate(buffer, 2) == voc_ticks_crc;
if (!crc_correct) {
return SGP40_CRC8_ERROR;
}
@ -120,16 +138,16 @@ int8_t SGP40_execute_self_test ( uint8_t * test_result)
int8_t result;
result = sgp40_send_cmd(SGP40_EXECUTE_SELF_TEST);
if (result != I2C_OK) {
if (result != 0) {
return SGP40_ERROR;
}
LL_mDelay(350);
delay_ms(350);
uart_disable_interrupts();
result = i2c_receive(SGP40_I2C_ADDRESS << 1, buffer, 3);
uart_enable_interrupts();
if (result != I2C_OK) {
sgp40_disable_interrupts();
result = sgp40_i2c_receive(SGP40_I2C_ADDRESS << 1, buffer, 3);
sgp40_enable_interrupts();
if (result != 0) {
return SGP40_ERROR;
}
@ -147,15 +165,15 @@ int8_t SGP40_get_serial_number(uint8_t serial[6])
{
uint8_t buffer[16];
uart_disable_interrupts();
sgp40_disable_interrupts();
sgp40_send_cmd(SGP40_GET_SERIAL_NUMBER);
uart_enable_interrupts();
sgp40_enable_interrupts();
LL_mDelay(5);
delay_ms(5);
uart_disable_interrupts();
i2c_receive(SGP40_I2C_ADDRESS << 1, buffer, 9);
uart_enable_interrupts();
sgp40_disable_interrupts();
sgp40_i2c_receive(SGP40_I2C_ADDRESS << 1, buffer, 9);
sgp40_enable_interrupts();
serial[0] = buffer[0];
serial[1] = buffer[1];
@ -167,9 +185,9 @@ int8_t SGP40_get_serial_number(uint8_t serial[6])
serial[5] = buffer[8];
uint8_t crc_ser45 = buffer[9];
uint8_t crc_correct = crc8_calculate(buffer, 2) == crc_ser01;
crc_correct &= crc8_calculate(buffer + 3, 2) == crc_ser23;
crc_correct &= crc8_calculate(buffer + 6, 2) == crc_ser45;
uint8_t crc_correct = sensirion_crc8_calculate(buffer, 2) == crc_ser01;
crc_correct &= sensirion_crc8_calculate(buffer + 3, 2) == crc_ser23;
crc_correct &= sensirion_crc8_calculate(buffer + 6, 2) == crc_ser45;
if (!crc_correct) {
return SGP40_CRC8_ERROR;
}

View File

@ -10,10 +10,6 @@
#include "stdint.h"
#include "stm32l0xx_ll_i2c.h"
#include "stm32l0xx_ll_utils.h"
#include "i2c.h"
#include "crc8.h"
/*
* Defines & macros