mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-27 04:36:31 +00:00
Packed global variables into a struct
This commit is contained in:
parent
c1c0b25c17
commit
76c5be7634
@ -42,10 +42,6 @@ enum
|
|||||||
DALI_ERROR
|
DALI_ERROR
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DALI {
|
|
||||||
bool present = false;
|
|
||||||
} Dali;
|
|
||||||
|
|
||||||
// http and json defines
|
// http and json defines
|
||||||
#define D_NAME_DALI "DALI"
|
#define D_NAME_DALI "DALI"
|
||||||
|
|
||||||
@ -59,16 +55,29 @@ enum DALI_Commands { // commands for Console
|
|||||||
|
|
||||||
/* Private variables ---------------------------------------------------------*/
|
/* Private variables ---------------------------------------------------------*/
|
||||||
// Data variables
|
// Data variables
|
||||||
uint16_t send_dali_data; // data to send to DALI bus
|
// uint16_t send_dali_data; // data to send to DALI bus
|
||||||
uint16_t received_dali_data; // data received from DALI bus
|
// uint16_t received_dali_data; // data received from DALI bus
|
||||||
// Processing variables
|
// Processing variables
|
||||||
uint8_t flag; // DALI status flag
|
// uint8_t flag; // DALI status flag
|
||||||
uint8_t bit_count; // nr of rec/send bits
|
// uint8_t bit_count; // nr of rec/send bits
|
||||||
uint16_t tick_count; // nr of ticks of the timer
|
// uint16_t tick_count; // nr of ticks of the timer
|
||||||
|
|
||||||
bool bit_value; // value of actual bit
|
// bool bit_value; // value of actual bit
|
||||||
bool actual_val; // bit value in this tick of timer
|
// bool actual_val; // bit value in this tick of timer
|
||||||
bool former_val; // bit value in previous tick of timer
|
// bool former_val; // bit value in previous tick of timer
|
||||||
|
|
||||||
|
struct DALI {
|
||||||
|
// Data variables
|
||||||
|
uint16_t send_dali_data; // data to send to DALI bus
|
||||||
|
uint16_t received_dali_data; // data received from DALI bus
|
||||||
|
// Processing variables
|
||||||
|
uint8_t flag; // DALI status flag
|
||||||
|
uint8_t bit_count; // nr of rec/send bits
|
||||||
|
uint16_t tick_count; // nr of ticks of the timer
|
||||||
|
bool former_val; // bit value in previous tick of timer
|
||||||
|
|
||||||
|
bool present = false;// DALI initialized
|
||||||
|
} Dali;
|
||||||
|
|
||||||
hw_timer_t *DALI_timer = NULL;
|
hw_timer_t *DALI_timer = NULL;
|
||||||
|
|
||||||
@ -83,11 +92,11 @@ hw_timer_t *DALI_timer = NULL;
|
|||||||
*/
|
*/
|
||||||
void IRAM_ATTR DALI_Tick_Handler(void)
|
void IRAM_ATTR DALI_Tick_Handler(void)
|
||||||
{
|
{
|
||||||
if (get_flag() == DALI_RECEIVING_DATA)
|
if (getDaliFlag() == DALI_RECEIVING_DATA)
|
||||||
{
|
{
|
||||||
receive_tick();
|
receive_tick();
|
||||||
}
|
}
|
||||||
else if (get_flag() == DALI_SENDING_DATA)
|
else if (getDaliFlag() == DALI_SENDING_DATA)
|
||||||
{
|
{
|
||||||
send_tick();
|
send_tick();
|
||||||
}
|
}
|
||||||
@ -99,7 +108,7 @@ void IRAM_ATTR DALI_Tick_Handler(void)
|
|||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
void enableDaliRxInterrupt() {
|
void enableDaliRxInterrupt() {
|
||||||
flag = DALI_NO_ACTION;
|
Dali.flag = DALI_NO_ACTION;
|
||||||
timerAlarmDisable(DALI_timer);
|
timerAlarmDisable(DALI_timer);
|
||||||
attachInterrupt(Pin(GPIO_DALI_RX), receiveDaliData, FALLING);
|
attachInterrupt(Pin(GPIO_DALI_RX), receiveDaliData, FALLING);
|
||||||
}
|
}
|
||||||
@ -119,9 +128,9 @@ void disableRxInterrupt() {
|
|||||||
* @param None
|
* @param None
|
||||||
* @retval uint8_t flag
|
* @retval uint8_t flag
|
||||||
*/
|
*/
|
||||||
uint8_t get_flag(void)
|
uint8_t getDaliFlag(void)
|
||||||
{
|
{
|
||||||
return flag;
|
return Dali.flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,7 +139,7 @@ uint8_t get_flag(void)
|
|||||||
* @retval uint8_t flag
|
* @retval uint8_t flag
|
||||||
*/
|
*/
|
||||||
void DataReceivedCallback() {
|
void DataReceivedCallback() {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("DLI: Received: %d %d"), received_dali_data>>9, received_dali_data&0xff);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("DLI: Received: %d %d"), Dali.received_dali_data>>9, Dali.received_dali_data&0xff);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************** R E C E I V E * P R O C E D U R E S *******/
|
/*************** R E C E I V E * P R O C E D U R E S *******/
|
||||||
@ -143,12 +152,12 @@ void DataReceivedCallback() {
|
|||||||
void receiveDaliData()
|
void receiveDaliData()
|
||||||
{
|
{
|
||||||
// null variables
|
// null variables
|
||||||
received_dali_data = 0;
|
Dali.received_dali_data = 0;
|
||||||
bit_count = 0;
|
Dali.bit_count = 0;
|
||||||
tick_count = 0;
|
Dali.tick_count = 0;
|
||||||
former_val = true;
|
Dali.former_val = true;
|
||||||
|
|
||||||
flag = DALI_RECEIVING_DATA;
|
Dali.flag = DALI_RECEIVING_DATA;
|
||||||
|
|
||||||
disableRxInterrupt();
|
disableRxInterrupt();
|
||||||
}
|
}
|
||||||
@ -176,63 +185,62 @@ bool get_DALIIN(void)
|
|||||||
void receive_tick(void)
|
void receive_tick(void)
|
||||||
{
|
{
|
||||||
// four ticks per bit
|
// four ticks per bit
|
||||||
actual_val = get_DALIIN();
|
bool actual_val = get_DALIIN();
|
||||||
tick_count++;
|
Dali.tick_count++;
|
||||||
|
|
||||||
// edge detected
|
// edge detected
|
||||||
if(actual_val != former_val)
|
if(actual_val != Dali.former_val)
|
||||||
{
|
{
|
||||||
switch(bit_count)
|
switch(Dali.bit_count)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
if (tick_count > 2)
|
if (Dali.tick_count > 2)
|
||||||
{
|
{
|
||||||
tick_count = 0;
|
Dali.tick_count = 0;
|
||||||
bit_count = 1; // start bit
|
Dali.bit_count = 1; // start bit
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 17: // 1st stop bit
|
case 17: // 1st stop bit
|
||||||
if(tick_count > 6) { // stop bit error, no edge should exist
|
if(Dali.tick_count > 6) { // stop bit error, no edge should exist
|
||||||
flag = DALI_ERROR;
|
Dali.flag = DALI_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default: // other bits
|
default: // other bits
|
||||||
if(tick_count > 6)
|
if(Dali.tick_count > 6)
|
||||||
{
|
{
|
||||||
received_dali_data |= (actual_val << (16-bit_count));
|
Dali.received_dali_data |= (actual_val << (16-Dali.bit_count));
|
||||||
bit_count++;
|
Dali.bit_count++;
|
||||||
tick_count = 0;
|
Dali.tick_count = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}else // voltage level stable
|
}else // voltage level stable
|
||||||
{
|
{
|
||||||
switch(bit_count)
|
switch(Dali.bit_count)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
if(tick_count==8) { // too long start bit
|
if(Dali.tick_count==8) { // too long start bit
|
||||||
flag = DALI_ERROR;
|
Dali.flag = DALI_ERROR;
|
||||||
Serial.println("Too long start bit.");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 17:
|
case 17:
|
||||||
// First stop bit
|
// First stop bit
|
||||||
if (tick_count==8)
|
if (Dali.tick_count==8)
|
||||||
{
|
{
|
||||||
if (actual_val==0) // wrong level of stop bit
|
if (actual_val==0) // wrong level of stop bit
|
||||||
{
|
{
|
||||||
flag = DALI_ERROR;
|
Dali.flag = DALI_ERROR;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bit_count++;
|
Dali.bit_count++;
|
||||||
tick_count = 0;
|
Dali.tick_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 18:
|
case 18:
|
||||||
// Second stop bit
|
// Second stop bit
|
||||||
if (tick_count==8)
|
if (Dali.tick_count==8)
|
||||||
{
|
{
|
||||||
enableDaliRxInterrupt();
|
enableDaliRxInterrupt();
|
||||||
DataReceivedCallback();
|
DataReceivedCallback();
|
||||||
@ -240,15 +248,15 @@ void receive_tick(void)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default: // normal bits
|
default: // normal bits
|
||||||
if(tick_count==10)
|
if(Dali.tick_count==10)
|
||||||
{ // too long delay before edge
|
{ // too long delay before edge
|
||||||
flag = DALI_ERROR;
|
Dali.flag = DALI_ERROR;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
former_val = actual_val;
|
Dali.former_val = actual_val;
|
||||||
if(flag==DALI_ERROR)
|
if(getDaliFlag() == DALI_ERROR)
|
||||||
{
|
{
|
||||||
enableDaliRxInterrupt();
|
enableDaliRxInterrupt();
|
||||||
}
|
}
|
||||||
@ -285,12 +293,12 @@ bool get_DALIOUT(void)
|
|||||||
*/
|
*/
|
||||||
void sendDaliData(uint8_t firstByte, uint8_t secondByte)
|
void sendDaliData(uint8_t firstByte, uint8_t secondByte)
|
||||||
{
|
{
|
||||||
send_dali_data = firstByte << 8;
|
Dali.send_dali_data = firstByte << 8;
|
||||||
send_dali_data += secondByte & 0xff;
|
Dali.send_dali_data += secondByte & 0xff;
|
||||||
bit_count = 0;
|
Dali.bit_count = 0;
|
||||||
tick_count = 0;
|
Dali.tick_count = 0;
|
||||||
|
|
||||||
flag = DALI_SENDING_DATA;
|
Dali.flag = DALI_SENDING_DATA;
|
||||||
|
|
||||||
disableRxInterrupt();
|
disableRxInterrupt();
|
||||||
}
|
}
|
||||||
@ -307,67 +315,67 @@ void sendDaliData(uint8_t firstByte, uint8_t secondByte)
|
|||||||
void send_tick(void)
|
void send_tick(void)
|
||||||
{
|
{
|
||||||
// access to the routine just every 4 ticks = every half bit
|
// access to the routine just every 4 ticks = every half bit
|
||||||
if ((tick_count & 0x03) == 0)
|
if ((Dali.tick_count & 0x03) == 0)
|
||||||
{
|
{
|
||||||
if (tick_count < 160)
|
if (Dali.tick_count < 160)
|
||||||
{
|
{
|
||||||
// settling time between forward and backward frame
|
// settling time between forward and backward frame
|
||||||
if (tick_count < 24)
|
if (Dali.tick_count < 24)
|
||||||
{
|
{
|
||||||
tick_count++;
|
Dali.tick_count++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start of the start bit
|
// start of the start bit
|
||||||
if (tick_count == 24)
|
if (Dali.tick_count == 24)
|
||||||
{
|
{
|
||||||
// GPIOB->ODR ^= GPIO_ODR_7;
|
// GPIOB->ODR ^= GPIO_ODR_7;
|
||||||
set_DALIOUT(false);
|
set_DALIOUT(false);
|
||||||
tick_count++;
|
Dali.tick_count++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// edge of the start bit
|
// edge of the start bit
|
||||||
// 28 ticks = 28/9600 = 2,92ms = delay between forward and backward message frame
|
// 28 ticks = 28/9600 = 2,92ms = delay between forward and backward message frame
|
||||||
if (tick_count == 28)
|
if (Dali.tick_count == 28)
|
||||||
{
|
{
|
||||||
set_DALIOUT(true);
|
set_DALIOUT(true);
|
||||||
tick_count++;
|
Dali.tick_count++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bit value (edge) selection
|
// bit value (edge) selection
|
||||||
bit_value = (bool)((send_dali_data >> (15 - bit_count)) & 0x01);
|
bool bit_value = (bool)((Dali.send_dali_data >> (15 - Dali.bit_count)) & 0x01);
|
||||||
|
|
||||||
// Every half bit -> Manchester coding
|
// Every half bit -> Manchester coding
|
||||||
if (!((tick_count - 24) & 0x0007))
|
if (!((Dali.tick_count - 24) & 0x0007))
|
||||||
{ // div by 8
|
{ // div by 8
|
||||||
if (get_DALIOUT() == bit_value) // former value of bit = new value of bit
|
if (get_DALIOUT() == bit_value) // former value of bit = new value of bit
|
||||||
set_DALIOUT((bool)(1 - bit_value));
|
set_DALIOUT((bool)(1 - bit_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate edge for actual bit
|
// Generate edge for actual bit
|
||||||
if (!((tick_count - 28) & 0x0007))
|
if (!((Dali.tick_count - 28) & 0x0007))
|
||||||
{
|
{
|
||||||
set_DALIOUT(bit_value);
|
set_DALIOUT(bit_value);
|
||||||
bit_count++;
|
Dali.bit_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ // end of data byte, start of stop bits
|
{ // end of data byte, start of stop bits
|
||||||
if (tick_count == 160)
|
if (Dali.tick_count == 160)
|
||||||
{
|
{
|
||||||
set_DALIOUT(true); // start of stop bit
|
set_DALIOUT(true); // start of stop bit
|
||||||
}
|
}
|
||||||
|
|
||||||
// end of stop bits, no settling time
|
// end of stop bits, no settling time
|
||||||
if (tick_count == 176)
|
if (Dali.tick_count == 176)
|
||||||
{
|
{
|
||||||
enableDaliRxInterrupt();
|
enableDaliRxInterrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tick_count++;
|
Dali.tick_count++;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user