onewire_master: minor, add space around operators

This commit is contained in:
King Kévin 2020-02-17 14:45:12 +01:00
parent 7681007d95
commit 41bd39f123
1 changed files with 44 additions and 45 deletions

View File

@ -147,7 +147,7 @@ void TIM_ISR(ONEWIRE_MASTER_TIMER)(void)
if (gpio_get(GPIO_PORT(ONEWIRE_MASTER_PIN),GPIO_PIN(ONEWIRE_MASTER_PIN))) { // check if the slave kept it low
buffer[buffer_bit / 8] |= (1 << (buffer_bit % 8)); // save bit "1"
} else {
buffer[buffer_bit/8] &= ~(1<<(buffer_bit%8)); // save bit "0"
buffer[buffer_bit / 8] &= ~(1 << (buffer_bit % 8)); // save bit "0"
}
buffer_bit++; // got to next bit
} else {
@ -176,26 +176,26 @@ void TIM_ISR(ONEWIRE_MASTER_TIMER)(void)
void onewire_master_setup(void)
{
// setup GPIO with external interrupt
rcc_periph_clock_enable(RCC_GPIO(ONEWIRE_MASTER_PORT)); // enable clock for GPIO peripheral
gpio_set(GPIO(ONEWIRE_MASTER_PORT),GPIO(ONEWIRE_MASTER_PIN)); // idle is high (using pull-up resistor)
gpio_set_mode(GPIO(ONEWIRE_MASTER_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(ONEWIRE_MASTER_PIN)); // normal 1-Wire communication (only using external pull-up resistor)
rcc_periph_clock_enable(GPIO_RCC(ONEWIRE_MASTER_PIN)); // enable clock for GPIO peripheral
gpio_set(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_PIN(ONEWIRE_MASTER_PIN)); // idle is high (using pull-up resistor)
gpio_set_mode(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(ONEWIRE_MASTER_PIN)); // normal 1-Wire communication (only using external pull-up resistor)
// setup timer to generate/measure signal timing
rcc_periph_clock_enable(RCC_TIM(ONEWIRE_MASTER_TIMER)); // enable clock for timer peripheral
rcc_periph_reset_pulse(RST_TIM(ONEWIRE_MASTER_TIMER)); // reset timer state
timer_disable_counter(TIM(ONEWIRE_MASTER_TIMER)); // disable timer to configure it
timer_set_mode(TIM(ONEWIRE_MASTER_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler(TIM(ONEWIRE_MASTER_TIMER), 1-1); // don't use prescale since this 16 bits timer allows to wait > 480 us used for the reset pulse ( 1/(72E6/1/(2**16))=910us )
timer_set_prescaler(TIM(ONEWIRE_MASTER_TIMER), 1 - 1); // don't use prescale since this 16 bits timer allows to wait > 480 us used for the reset pulse ( 1/(72E6/1/(2**16))=910us )
// use comparator to time signal (without using the output), starting at slot start
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC1IF); // clear flag
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC1, 1*(rcc_ahb_frequency/1000000)-1); // use compare function to time master pulling low when reading (1 < Tlowr < 15)
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC1, 1 * (rcc_ahb_frequency / 1000000) - 1); // use compare function to time master pulling low when reading (1 < Tlowr < 15)
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC2IF); // clear flag
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC2, 7*(rcc_ahb_frequency/1000000)-1); // use compare function to read or write 0 or 1 (1 < Trw < 15)
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC2, 7 * (rcc_ahb_frequency / 1000000) - 1); // use compare function to read or write 0 or 1 (1 < Trw < 15)
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC3IF); // clear flag
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC3, 62*(rcc_ahb_frequency/1000000)-1); // use compare function to end time slot (60 < Tslot < 120), this will be followed by a recovery time (end of timer)
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC3, 62 * (rcc_ahb_frequency / 1000000) - 1); // use compare function to end time slot (60 < Tslot < 120), this will be followed by a recovery time (end of timer)
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC4IF); // clear flag
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC4, (70-10)*(rcc_ahb_frequency/1000000)-1); // use compare function to detect slave presence (15 < Tpdh < 60 + 60 < Tpdl < 240), with hand tuning
timer_set_oc_value(TIM(ONEWIRE_MASTER_TIMER), TIM_OC4, (70 - 10) * (rcc_ahb_frequency / 1000000) - 1); // use compare function to detect slave presence (15 < Tpdh < 60 + 60 < Tpdl < 240), with hand tuning
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_UIF); // clear update (overflow) flag
timer_update_on_overflow(TIM(ONEWIRE_MASTER_TIMER)); // only use counter overflow as UEV source (use overflow as start time or timeout)
timer_enable_irq(TIM(ONEWIRE_MASTER_TIMER), TIM_DIER_UIE); // enable update interrupt for overflow
@ -235,8 +235,8 @@ bool onewire_master_reset(void)
{
// prepare timer
timer_disable_counter(TIM(ONEWIRE_MASTER_TIMER)); // disable timer to reconfigure it
timer_set_counter(TIM(ONEWIRE_MASTER_TIMER),0); // reset counter
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), 490*(rcc_ahb_frequency/1000000)-1); // set timeout to > 480 us (480 < Trst)
timer_set_counter(TIM(ONEWIRE_MASTER_TIMER), 0); // reset counter
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), 490 * (rcc_ahb_frequency / 1000000) - 1); // set timeout to > 480 us (480 < Trst)
slave_presence = false; // reset state
onewire_master_state = ONEWIRE_STATE_MASTER_RESET; // set new state
@ -245,10 +245,10 @@ bool onewire_master_reset(void)
gpio_clear(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_PIN(ONEWIRE_MASTER_PIN)); // pull signal low to start reset (it's not important if it was low in the first place since the reset pulse has no maximum time)
timer_enable_counter(TIM(ONEWIRE_MASTER_TIMER)); // start timer
while (onewire_master_state!=ONEWIRE_STATE_DONE && onewire_master_state!=ONEWIRE_STATE_ERROR) { // wait until reset procedure completed
while (onewire_master_state != ONEWIRE_STATE_DONE && onewire_master_state != ONEWIRE_STATE_ERROR) { // wait until reset procedure completed
__WFI(); // go to sleep
}
if (ONEWIRE_STATE_ERROR==onewire_master_state) { // an error occurred
if (ONEWIRE_STATE_ERROR == onewire_master_state) { // an error occurred
return false;
}
@ -267,7 +267,7 @@ static bool onewire_master_write(void)
// prepare timer
timer_disable_counter(TIM(ONEWIRE_MASTER_TIMER)); // disable timer to reconfigure it
timer_set_counter(TIM(ONEWIRE_MASTER_TIMER), 0); // reset counter
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), TIM_CCR3(TIM(ONEWIRE_MASTER_TIMER))+2*(rcc_ahb_frequency/1000000)); // set time for new time slot (recovery timer Trec>1, after time slot end )
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), TIM_CCR3(TIM(ONEWIRE_MASTER_TIMER)) + 2 * (rcc_ahb_frequency / 1000000)); // set time for new time slot (recovery timer Trec>1, after time slot end )
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC2IF); // clear output compare flag
timer_enable_irq(TIM(ONEWIRE_MASTER_TIMER), TIM_DIER_CC2IE); // enable compare interrupt for bit setting
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC3IF); // clear output compare flag
@ -277,10 +277,10 @@ static bool onewire_master_write(void)
gpio_set_mode(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(ONEWIRE_MASTER_PIN)); // normal 1-Wire communication (only using external pull-up resistor)
gpio_clear(GPIO_PORT(ONEWIRE_MASTER_PIN),GPIO_PIN(ONEWIRE_MASTER_PIN)); // pull signal low to start slot
timer_enable_counter(TIM(ONEWIRE_MASTER_TIMER)); // start timer
while (onewire_master_state!=ONEWIRE_STATE_DONE && onewire_master_state!=ONEWIRE_STATE_ERROR) { // wait until write procedure completed
while (onewire_master_state != ONEWIRE_STATE_DONE && onewire_master_state != ONEWIRE_STATE_ERROR) { // wait until write procedure completed
__WFI(); // go to sleep
}
if (ONEWIRE_STATE_ERROR==onewire_master_state) { // an error occurred
if (ONEWIRE_STATE_ERROR == onewire_master_state) { // an error occurred
return false;
}
return true;
@ -292,7 +292,7 @@ static bool onewire_master_write(void)
*/
static bool onewire_master_read(void)
{
if (0==buffer_size) { // check input
if (0 == buffer_size) { // check input
return false;
}
buffer_bit = 0; // reset bit index
@ -301,7 +301,7 @@ static bool onewire_master_read(void)
// prepare timer
timer_disable_counter(TIM(ONEWIRE_MASTER_TIMER)); // disable timer to reconfigure it
timer_set_counter(TIM(ONEWIRE_MASTER_TIMER), 0); // reset counter
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), TIM_CCR3(TIM(ONEWIRE_MASTER_TIMER))+2*(rcc_ahb_frequency/1000000)); // set time for new time slot (recovery timer Trec>1, after time slot end )
timer_set_period(TIM(ONEWIRE_MASTER_TIMER), TIM_CCR3(TIM(ONEWIRE_MASTER_TIMER)) + 2*(rcc_ahb_frequency / 1000000)); // set time for new time slot (recovery timer Trec>1, after time slot end )
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC1IF); // clear output compare flag
timer_enable_irq(TIM(ONEWIRE_MASTER_TIMER), TIM_DIER_CC1IE); // enable compare interrupt for stop pulling low
timer_clear_flag(TIM(ONEWIRE_MASTER_TIMER), TIM_SR_CC2IF); // clear output compare flag
@ -313,10 +313,10 @@ static bool onewire_master_read(void)
gpio_set_mode(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(ONEWIRE_MASTER_PIN)); // normal 1-Wire communication (only using external pull-up resistor)
gpio_clear(GPIO_PORT(ONEWIRE_MASTER_PIN), GPIO_PIN(ONEWIRE_MASTER_PIN)); // pull signal low to start slot
timer_enable_counter(TIM(ONEWIRE_MASTER_TIMER)); // start timer
while (onewire_master_state!=ONEWIRE_STATE_DONE && onewire_master_state!=ONEWIRE_STATE_ERROR) { // wait until read procedure completed
while (onewire_master_state != ONEWIRE_STATE_DONE && onewire_master_state != ONEWIRE_STATE_ERROR) { // wait until read procedure completed
__WFI(); // go to sleep
}
if (ONEWIRE_STATE_ERROR==onewire_master_state) { // an error occurred
if (ONEWIRE_STATE_ERROR == onewire_master_state) { // an error occurred
return false;
}
return true;
@ -324,16 +324,16 @@ static bool onewire_master_read(void)
uint8_t onewire_master_crc(uint8_t* data, uint32_t length)
{
if (NULL==data || 0==length) { // check input
if (NULL == data || 0 == length) { // check input
return 0; // wrong input
}
uint8_t crc = 0x00; // initial value
for (uint8_t i=0; i<length; i++) { // go through every byte
for (uint8_t i = 0; i < length; i++) { // go through every byte
crc ^= data[i]; // XOR byte
for (uint8_t b=0; b<8; b++) { // go through every bit
if (crc&0x01) { // least significant bit is set (we are using the reverse way)
crc = (crc>>1)^0x8C; // // shift to the right (for the next bit) and XOR with (reverse) polynomial
for (uint8_t b = 0; b < 8; b++) { // go through every bit
if (crc & 0x01) { // least significant bit is set (we are using the reverse way)
crc = (crc >> 1) ^ 0x8C; // // shift to the right (for the next bit) and XOR with (reverse) polynomial
} else {
crc >>= 1; // just shift right (for the next bit)
}
@ -344,7 +344,7 @@ uint8_t onewire_master_crc(uint8_t* data, uint32_t length)
bool onewire_master_read_byte(uint8_t* data)
{
if (NULL==data) { // check input
if (NULL == data) { // check input
return false; // wrong input
}
@ -375,7 +375,7 @@ bool onewire_master_function_read(uint8_t function, uint8_t* data, uint32_t bits
return false; // an error occurred
}
if (NULL==data || 0==bits) { // there is no data to read
if (NULL == data || 0 == bits) { // there is no data to read
return true; // operation completed
}
@ -396,7 +396,7 @@ bool onewire_master_function_write(uint8_t function, uint8_t* data, uint32_t bit
return false; // an error occurred
}
if (NULL==data || 0==bits) { // there is no data to read
if (NULL == data || 0 == bits) { // there is no data to read
return true; // operation completed
}
@ -423,8 +423,8 @@ uint64_t onewire_master_rom_read(void)
// return ROM code
uint64_t code = 0;
for (uint32_t i=0; i<8; i++) {
code += (uint64_t)rom_code[i]<<(8*i); // add byte
for (uint32_t i = 0; i < 8; i++) {
code += (uint64_t)rom_code[i] << (8 * i); // add byte
}
return code;
@ -445,45 +445,45 @@ bool onewire_master_rom_search(uint64_t* code, bool alarm)
goto end; // an error occurred
}
if (conflict_last>=64) { // no previous conflict has been detected
if (conflict_last >= 64) { // no previous conflict has been detected
*code = 0; // restart search codes
}
buffer = bits; // buffer to read up to two bits
for (uint8_t bit=0; bit<64; bit++) { // go through all 64 bits ROM code
for (uint8_t bit = 0; bit < 64; bit++) { // go through all 64 bits ROM code
buffer_size = 2; // read two first bits to detect conflict
if (!onewire_master_read()) { // read ROM ID from slave
goto end; // an error occurred
}
switch (buffer[0]&0x03) { // check 2 bits received
switch (buffer[0] & 0x03) { // check 2 bits received
case 0: // collision detected
if (bit==conflict_last) { // this conflict is known
*code |= (((uint64_t)1)<<bit); // use 0 as next bit
if (bit == conflict_last) { // this conflict is known
*code |= (((uint64_t)1) << bit); // use 0 as next bit
} else { // unknown conflict
conflict_current = bit; // remember conflict
*code &= ~(((uint64_t)1)<<bit); // use 1 as next bit
*code &= ~(((uint64_t)1) << bit); // use 1 as next bit
}
break;
case 1: // no conflict, valid bit is 1
*code |= (((uint64_t)1)<<bit); // remember valid bit 1
*code |= (((uint64_t)1) << bit); // remember valid bit 1
break;
case 2: // no conflict, valid bit is 0
*code &= ~(((uint64_t)1)<<bit); // remember valid bit 0
*code &= ~(((uint64_t)1) << bit); // remember valid bit 0
break;
default: // two 1's indicate there is no slave
conflict_current = 64; // remember there was no conflict because there is no slave
goto end; // an error has occurred
}
buffer_size = 1; // to send next bit
buffer[0] = ((*code)>>bit); // set bit to send
buffer[0] = ((*code) >> bit); // set bit to send
if (!onewire_master_write()) { // send bit
goto end; // an error has occurred
}
}
// verify ROM code
uint8_t rom_code[8] = {0}; // to store ROM code
for (uint8_t i=0; i<LENGTH(rom_code); i++) {
rom_code[i] = (*code)>>(8*i); // split and save last code in ROM code
for (uint8_t i = 0; i < LENGTH(rom_code); i++) {
rom_code[i] = (*code) >> (8 * i); // split and save last code in ROM code
}
if (onewire_master_crc(rom_code, LENGTH(rom_code))) { // verify checksum
*code = 0; // return the last code found since it's valid
@ -491,7 +491,7 @@ bool onewire_master_rom_search(uint64_t* code, bool alarm)
end:
conflict_last = conflict_current; // update the last seen and unknown conflict
if (conflict_current<64) { // we have seen an unknown conflict
if (conflict_current < 64) { // we have seen an unknown conflict
return true; // tell there are more slaves
} else { // no conflict seen
return false; // no more slaves
@ -509,12 +509,11 @@ bool onewire_master_rom_skip(void)
bool onewire_master_rom_match(uint64_t code)
{
uint8_t rom_code[8] = {0}; // to store ROM code
for (uint8_t i=0; i<LENGTH(rom_code); i++) {
rom_code[i] = code>>(8*i); // split and save code in ROM code
for (uint8_t i = 0; i < LENGTH(rom_code); i++) {
rom_code[i] = code >> (8 * i); // split and save code in ROM code
}
if (!onewire_master_function_write(0x55, rom_code, 64)) { // send MATCH ROM command with ROM code
return false; // an error occurred
}
return true;
}