/* * sensor.c * * Created on: 14.08.2021 * Author: Luca Engelmann */ #include "sensor.h" static void assertSensor(); static void deassertSensor(); void sensorTask(void *p) { (void)p; TickType_t xLastWakeTime = xTaskGetTickCount(); bool sensorInitialized = false; while (1) { if (!sensorInitialized) { //write config assertSensor(); uint8_t send[10]; send[0] = 0x46; //Command send[1] = 0xF3; //CONFIG0 send[2] = 0xC; //CONFIG1 send[3] = 0x8B; //CONFIG2 send[4] = 0x80; //CONFIG3 send[5] = 0; //IRQ send[6] = 0xDE; send[7] = 0; send[8] = 0xFF; send[9] = 0xFF; for (size_t i = 0; i < 10; i++) { spiMove(0, send[i]); } deassertSensor(); sensorInitialized = true; } //For debugging only GPIOSetPin(GPIO_PORTA, PWM0_PIN); //Set mux uint8_t command = 0x5A; uint8_t mux = 0xDE; //MUX: Int. temperature assertSensor(); spiMove(0, command); spiMove(0, mux); deassertSensor(); //Start conversion command = 0x68; assertSensor(); spiMove(0, command); deassertSensor(); //Read value command = 0x43; uint8_t status = 0; const uint8_t maxTimeout = 255; uint8_t timeout = maxTimeout; uint8_t rec[3]; do { assertSensor(); status = spiMove(0, command); if (status == 0x13) { timeout = maxTimeout; break; } deassertSensor(); timeout--; } while (timeout > 0); for (size_t i = 0; i < 3; i++) { rec[i] = spiMove(0, 0); } deassertSensor(); //For debugging only GPIOResetPin(GPIO_PORTA, PWM0_PIN); vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(200)); } } void assertSensor() { GPIOResetPin(GPIO_PORTE, SENSOR_SS_PIN); } void deassertSensor() { GPIOSetPin(GPIO_PORTE, SENSOR_SS_PIN); }