From 0fd71c101cc346c1ef2ab58b0f9bf2f5ecb628ef Mon Sep 17 00:00:00 2001 From: Stefan Kratochwil Date: Sat, 1 Jun 2024 00:33:54 +0200 Subject: [PATCH] WIP: Reuse logic from arduino project wo make mfrc522 work. Hint: https://github.com/miguelbalboa/rfid.git --- main.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index a90cd32..de2c5b4 100644 --- a/main.c +++ b/main.c @@ -59,17 +59,107 @@ void mfrc522_integration_init(void) mfrc522_interface_debug_print("ERR: %s failed: %d\n", __FUNCTION__, init_retval); } + // Set IRQ pin of MFRC522 to CMOS, enable irq on pico on success uint8_t irq_pin_type_ret = mfrc522_set_interrupt_pin_type(&ctx, MFRC522_INTERRUPT_PIN_TYPE_STANDARD_CMOS); if (irq_pin_type_ret != 0) { - mfrc522_interface_debug_print("ERR: Could not set interrupt pin type."); + mfrc522_interface_debug_print("ERR: Could not set interrupt pin type.\n"); } else { mfrc522_integration_irq_init(&ctx); } - // TODO Clear all interrupt bits here? + // Set rf uart to 106000 baud (tx/rx) + uint8_t set_tx_speed_ret = mfrc522_set_tx_speed(&ctx, MFRC522_SPEED_106_KBD); + if (set_tx_speed_ret != 0) { + mfrc522_interface_debug_print("ERR: could not set rf tx speed\n"); + } + uint8_t set_rx_speed_ret = mfrc522_set_rx_speed(&ctx, MFRC522_SPEED_106_KBD); + if (set_rx_speed_ret != 0) { + mfrc522_interface_debug_print("ERR: could not set rf rx speed\n"); + } + + // Set modulation width to initial default value (datasheet section 9.3.3.4) + uint8_t set_modw_ret = mfrc522_set_modulation_width(&ctx, 0x26); + if (set_modw_ret != 0) { + mfrc522_interface_debug_print("ERR: could not set modulation width register\n"); + } + + // Timer setup. First set timer to auto, then set prescaler to 0xA9 + uint8_t set_tauto_ret = mfrc522_set_timer_auto(&ctx, MFRC522_BOOL_TRUE); + if (set_tauto_ret != 0) { + mfrc522_interface_debug_print("ERR: Could not set timer to auto\n"); + } + + uint8_t set_tprescaler = mfrc522_set_timer_prescaler(&ctx, (uint16_t) 0xA9); + if (set_tprescaler != 0) { + mfrc522_interface_debug_print("ERR: Could not set timer prescaler\n"); + } + + // Timer setup. Configure reload registers (hi and lo) + uint8_t set_treload = mfrc522_set_timer_reload(&ctx, (uint16_t) 0x3E8); + if (set_treload != 0) { + mfrc522_interface_debug_print("ERR: Could not set timer reload value"); + } + + // A magic modulation setting: force ASK 100% - whatever that is.. + uint8_t force_100ask_ret = mfrc522_set_force_100_ask(&ctx, MFRC522_BOOL_TRUE); + if (force_100ask_ret != 0) { + mfrc522_interface_debug_print("ERR: Could not force 100\% ASK modulation\n"); + } + + // More settings for the mode register (crc setup, wait for rf field) + uint8_t set_modereg_msbfirst_ret = mfrc522_set_crc_msb_first(&ctx, MFRC522_BOOL_FALSE); + if (set_modereg_msbfirst_ret != 0) { + mfrc522_interface_debug_print("ERR: Could not set crc to msb first\n"); + } + uint8_t set_modereg_txwaitrf_ret = mfrc522_set_tx_wait_rf(&ctx, MFRC522_BOOL_TRUE); + if (set_modereg_txwaitrf_ret != 0) { + mfrc522_interface_debug_print("ERR: Could not make transmitter wait for rf field\n"); + } + uint8_t set_modereg_polmfin_ret = mfrc522_set_mfin_polarity(&ctx, MFRC522_MFIN_POLARITY_HIGH); + if (set_modereg_polmfin_ret != 0) + { + mfrc522_interface_debug_print("ERR: Could not set mfin polarity\n"); + } + uint8_t set_modereg_crcpres_ret = mfrc522_set_crc_preset(&ctx, MFRC522_CRC_PRESET_6363); + if (set_modereg_crcpres_ret != 0) { + mfrc522_interface_debug_print("ERR: Could not set crc preset value\n"); + } + + // Enable antenna + uint8_t set_ant_invrx2_ret = mfrc522_set_antenna_driver(&ctx, MFRC522_ANTENNA_DRIVER_INV_TX2_RF_ON, MFRC522_BOOL_TRUE); + if (set_ant_invrx2_ret != 0) + { + mfrc522_interface_debug_print("ERR: Could not enable tx2 invert\n"); + } + uint8_t enable_ant_tx2rf_ret = mfrc522_set_antenna_driver(&ctx, MFRC522_ANTENNA_DRIVER_TX2_RF, MFRC522_BOOL_TRUE); + if (enable_ant_tx2rf_ret != 0) + { + mfrc522_interface_debug_print("ERR: Could not enable tx2 rf\n"); + } + uint8_t enable_ant_tx1rf_ret = mfrc522_set_antenna_driver(&ctx, MFRC522_ANTENNA_DRIVER_TX1_RF, MFRC522_BOOL_TRUE); + if (enable_ant_tx1rf_ret != 0) + { + mfrc522_interface_debug_print("ERR: Could not enable tx1 rf\n"); + } + + // Enable RX interrupt on rf side XXX Are we doing the right thing?!? + uint8_t set_irq_invert_ret = mfrc522_set_interrupt1_pin_invert(&ctx, MFRC522_BOOL_TRUE); + if (set_irq_invert_ret != 0) + { + mfrc522_interface_debug_print("ERR: Could not invert irq pin\n"); + } + + // Enable RX interrupt + uint8_t en_rx_irq = mfrc522_set_interrupt1(&ctx, MFRC522_INTERRUPT1_RX , MFRC522_BOOL_TRUE); + if (en_rx_irq != 0) + { + mfrc522_interface_debug_print("ERR: Could not enable rx interrupt on rf side\n"); + } + + // Enable analog section of the reader uint8_t analog_enable_retval = mfrc522_set_receiver_analog(&ctx, MFRC522_BOOL_TRUE); if (analog_enable_retval != 0) { mfrc522_interface_debug_print("ERR: Enabling analog receiver failed: %d\n", analog_enable_retval);