microwire_master: minor, put spaces around operators

This commit is contained in:
King Kévin 2020-02-17 14:06:15 +01:00
parent 55e44d4c5e
commit ddd0d18d15
1 changed files with 23 additions and 23 deletions

View File

@ -57,7 +57,7 @@ bool mirowire_master_organization_x16 = true;
void microwire_master_setup(uint32_t frequency, bool organization_x16, uint8_t address_size)
{
// sanity checks
if (0==frequency || 0==address_size) {
if (0 == frequency || 0 == address_size) {
return;
}
mirowire_master_address_size = address_size; // save address size
@ -77,10 +77,10 @@ void microwire_master_setup(uint32_t frequency, bool organization_x16, uint8_t a
rcc_periph_clock_enable(RCC_TIM(MICROWIRE_MASTER_TIMER)); // enable clock for timer domain
rcc_periph_reset_pulse(RST_TIM(MICROWIRE_MASTER_TIMER)); // reset timer state
timer_set_mode(TIM(MICROWIRE_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
uint16_t prescaler = rcc_ahb_frequency/(frequency*2)/(uint32_t)(1<<16)+1; // calculate prescaler for most accurate timing for this speed
timer_set_prescaler(TIM(MICROWIRE_MASTER_TIMER), prescaler-1); // set calculated prescaler
uint16_t period = (rcc_ahb_frequency/prescaler)/(frequency*2); // calculate period to get most accurate timing based on the calculated prescaler
timer_set_period(TIM(MICROWIRE_MASTER_TIMER), period-1); // set calculated period
uint16_t prescaler = rcc_ahb_frequency / (frequency * 2) / (uint32_t)(1 << 16) + 1; // calculate prescaler for most accurate timing for this speed
timer_set_prescaler(TIM(MICROWIRE_MASTER_TIMER), prescaler - 1); // set calculated prescaler
uint16_t period = (rcc_ahb_frequency / prescaler) / (frequency * 2); // calculate period to get most accurate timing based on the calculated prescaler
timer_set_period(TIM(MICROWIRE_MASTER_TIMER), period - 1); // set calculated period
timer_update_on_overflow(TIM(MICROWIRE_MASTER_TIMER)); // only use counter overflow as UEV source (use overflow as timeout)
SCB_SCR |= SCB_SCR_SEVONPEND; // enable wake up on event (instead of using ISR)
timer_enable_irq(TIM(MICROWIRE_MASTER_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
@ -133,12 +133,12 @@ static void microwire_master_start(uint8_t operation, uint32_t address)
// send '1' start bit
microwire_master_send_bit(true); // send start bit
// send two bits operation code
if (operation&0x2) { // send first bit (MSb first)
if (operation & 0x2) { // send first bit (MSb first)
microwire_master_send_bit(true); // send '1'
} else {
microwire_master_send_bit(false); // send '2'
}
if (operation&0x1) { // send second bit (LSb last)
if (operation & 0x1) { // send second bit (LSb last)
microwire_master_send_bit(true); // send '1'
} else {
microwire_master_send_bit(false); // send '2'
@ -146,7 +146,7 @@ static void microwire_master_start(uint8_t operation, uint32_t address)
// send address
for (uint8_t bit = mirowire_master_address_size; bit > 0; bit--) {
if ((address>>(bit-1))&0x01) {
if ((address >> (bit - 1)) & 0x01) {
microwire_master_send_bit(true); // send '1' address bit
} else {
microwire_master_send_bit(false); // send '0' address bit
@ -159,7 +159,7 @@ static void microwire_master_start(uint8_t operation, uint32_t address)
static void microwire_master_stop(void)
{
timer_disable_counter(TIM(MICROWIRE_MASTER_TIMER)); // disable timer
timer_set_counter(TIM(MICROWIRE_MASTER_TIMER),0); // reset timer counter
timer_set_counter(TIM(MICROWIRE_MASTER_TIMER), 0); // reset timer counter
timer_clear_flag(TIM(MICROWIRE_MASTER_TIMER), TIM_SR_UIF); // clear timer flag
nvic_clear_pending_irq(NVIC_TIM_IRQ(MICROWIRE_MASTER_TIMER)); // clear IRQ flag
gpio_clear(GPIO_PORT(MICROWIRE_MASTER_SCK_PIN), GPIO_PIN(MICROWIRE_MASTER_SCK_PIN)); // ensure clock is idle low
@ -182,7 +182,7 @@ static bool microwire_master_read_bit(void)
void microwire_master_read(uint32_t address, uint16_t* data, size_t length)
{
// to sanity checks
if (NULL==data || 0==length || 0==mirowire_master_address_size) { // can't save data
if (NULL == data || 0 == length || 0 == mirowire_master_address_size) { // can't save data
return;
}
@ -194,12 +194,12 @@ void microwire_master_read(uint32_t address, uint16_t* data, size_t length)
}
// read data
for (size_t i=0; i<length; i++) {
for (uint8_t b=(mirowire_master_organization_x16 ? 16 : 8); b>0; b--) {
for (size_t i = 0; i < length; i++) {
for (uint8_t b = (mirowire_master_organization_x16 ? 16 : 8); b > 0; b--) {
if (microwire_master_read_bit()) { // read bit, MSb first
data[i] |= (1<<(b-1)); // set bit
data[i] |= (1 << (b - 1)); // set bit
} else {
data[i] &= ~(1<<(b-1)); // clear bit
data[i] &= ~(1 << (b - 1)); // clear bit
}
}
}
@ -211,18 +211,18 @@ clean:
void microwire_master_write_enable(void)
{
// to sanity checks
if (mirowire_master_address_size<2) { // can't send '11...' address
if (mirowire_master_address_size < 2) { // can't send '11...' address
return;
}
microwire_master_start(0x0, 0x3<<(mirowire_master_address_size-2)); // send '00' WEN operation code and '11...' address
microwire_master_start(0x0, 0x3 << (mirowire_master_address_size - 2)); // send '00' WEN operation code and '11...' address
microwire_master_stop(); // clean up
}
void microwire_master_write_disable(void)
{
// to sanity checks
if (mirowire_master_address_size<2) { // can't send '00...' address
if (mirowire_master_address_size < 2) { // can't send '00...' address
return;
}
@ -282,25 +282,25 @@ void microwire_master_erase(uint32_t address)
void microwire_master_erase_all(void)
{
// sanity checks
if (mirowire_master_address_size<2) { // can't send '11...' address
if (mirowire_master_address_size < 2) { // can't send '11...' address
return;
}
microwire_master_start(0x00, 0x2<<(mirowire_master_address_size-2)); // send '00' ERAL operation code and '10...' address
microwire_master_start(0x00, 0x2 << (mirowire_master_address_size - 2)); // send '00' ERAL operation code and '10...' address
microwire_master_stop(); // clean up
}
void microwire_master_write_all(uint16_t data)
{
// sanity checks
if (0==mirowire_master_address_size) { // can't send address
if (0 == mirowire_master_address_size) { // can't send address
return;
}
microwire_master_start(0x00, 0x1<<(mirowire_master_address_size-2)); // send '00' WRAL operation code and '01...' address
microwire_master_start(0x00, 0x1 << (mirowire_master_address_size - 2)); // send '00' WRAL operation code and '01...' address
// write data (MSb first)
for (uint8_t b=(mirowire_master_organization_x16 ? 16 : 8); b>0; b--) {
if (data&(1<<(b-1))) { // bit is set
for (uint8_t b = (mirowire_master_organization_x16 ? 16 : 8); b > 0; b--) {
if (data&(1 << (b - 1))) { // bit is set
microwire_master_send_bit(true); // send '1' data bit
} else {
microwire_master_send_bit(false); // send '0' data bit