aboutsummaryrefslogtreecommitdiffhomepage
path: root/workspace/TS100/src/FRToSI2C.cpp
blob: 59b4d70405bfd6fd9f1cc10e17cb132976010efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * FRToSI2C.cpp
 *
 *  Created on: 14Apr.,2018
 *      Author: Ralim
 */

#include "FRToSI2C.hpp"

FRToSI2C::FRToSI2C(I2C_HandleTypeDef* i2chandle) {
	i2c = i2chandle;

}

void FRToSI2C::MasterTxCpltCallback() {
	xSemaphoreGive(I2CSemaphore);
}

void FRToSI2C::MemRxCpltCallback() {
	xSemaphoreGive(I2CSemaphore);
}
void FRToSI2C::MemTxCpltCallback() {
	xSemaphoreGive(I2CSemaphore);
}

void FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
		uint16_t MemAddSize, uint8_t* pData, uint16_t Size) {
	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED
			|| RToSUP == false) {
		//no RToS, run blocking code
		HAL_I2C_Mem_Read(i2c, DevAddress, MemAddress, MemAddSize, pData, Size,
				5000);
	} else {
		//RToS is active, run threading
		//Get the mutex so we can use the I2C port
		//Wait up to 1 second for the mutex
		if ( xSemaphoreTake( I2CSemaphore, ( TickType_t ) 1000 ) == pdTRUE) {
			HAL_I2C_Mem_Read(i2c, DevAddress, MemAddress, MemAddSize, pData,
					Size, 5000);
			xSemaphoreGive(I2CSemaphore);

		}

	}

}

void FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress,
		uint16_t MemAddSize, uint8_t* pData, uint16_t Size) {
	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED
			|| RToSUP == false) {
		//no RToS, run blocking code
		HAL_I2C_Mem_Write(i2c, DevAddress, MemAddress, MemAddSize, pData, Size,
				5000);
	} else {
		//RToS is active, run threading
		//Get the mutex so we can use the I2C port
		//Wait up to 1 second for the mutex
		if ( xSemaphoreTake( I2CSemaphore, ( TickType_t ) 1000 ) == pdTRUE) {
			HAL_I2C_Mem_Write(i2c, DevAddress, MemAddress, MemAddSize, pData,
					Size, 5000);
			xSemaphoreGive(I2CSemaphore);

		}

	}

}

void FRToSI2C::FRToSInit() {
	I2CSemaphore = xSemaphoreCreateMutex();
	xSemaphoreGive(I2CSemaphore);
	RToSUP = true;
}

void FRToSI2C::Transmit(uint16_t DevAddress, uint8_t* pData, uint16_t Size) {
	if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED
			|| RToSUP == false) {
		//no RToS, run blocking code
		HAL_I2C_Master_Transmit(i2c, DevAddress, pData, Size, 5000);
	} else {
		//RToS is active, run threading
		//Get the mutex so we can use the I2C port
		//Wait up to 1 second for the mutex
		if ( xSemaphoreTake( I2CSemaphore, ( TickType_t ) 1000 ) == pdTRUE) {
			HAL_I2C_Master_Transmit(i2c, DevAddress, pData, Size, 5000);

			xSemaphoreGive(I2CSemaphore);

		}
	}

}