Change in osmo-ccid-firmware[master]: add slot_set_isorate to the baud rate using ISO 7816 values

Kévin Redon gerrit-no-reply at lists.osmocom.org
Thu May 2 13:56:22 UTC 2019


Kévin Redon has uploaded this change for review. ( https://gerrit.osmocom.org/13847


Change subject: add slot_set_isorate to the baud rate using ISO 7816 values
......................................................................

add slot_set_isorate to the baud rate using ISO 7816 values

Change-Id: I604f72bb28944962aee055e6d88a435827f79bec
---
M sysmoOCTSIM/main.c
1 file changed, 38 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ccid-firmware refs/changes/47/13847/1

diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index 70099e1..55e3bd9 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -30,6 +30,7 @@
 #include "i2c_bitbang.h"
 #include "octsim_i2c.h"
 #include "ncn8025.h"
+#include "iso7816_3.h"
 
 #include "command.h"
 
@@ -158,6 +159,40 @@
 	return true;
 }
 
+/** change ISO baud rate of card slot
+ *  @param[in] slotnr slot number for which the baud rate should be set
+ *  @param[in] clkdiv can clock divider
+ *  @param[in] f clock rate conversion integer F
+ *  @param[in] d baud rate adjustment factor D
+ *  @return if the baud rate has been set, else a parameter is out of range
+ */
+static bool slot_set_isorate(uint8_t slotnr, enum ncn8025_sim_clkdiv clkdiv, uint16_t f, uint8_t d)
+{
+	// input checks
+	ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
+	if (clkdiv != 1 && clkdiv != 2 && clkdiv != 4 && clkdiv != 8) {
+		return false;
+	}
+	if (!iso7816_3_valid_f(f)) {
+		return false;
+	}
+	if (!iso7816_3_valid_d(d)) {
+		return false;
+	}
+
+	// set clockdiv
+	struct ncn8025_settings settings;
+	ncn8025_get(slotnr, &settings);
+	if (settings.clkdiv != clkdiv) {
+		settings.clkdiv = clkdiv;
+		ncn8025_set(slotnr, &settings);
+	}
+
+	// set baud rate
+	uint32_t baudrate = ((20000000UL / (1 << clkdiv)) * d) / f; // calculate actual baud rate
+	return slot_set_baudrate(slotnr, baudrate); // // set baud rate
+}
+
 DEFUN(sim_status, cmd_sim_status, "sim-status", "Get state of specified NCN8025")
 {
 	struct ncn8025_settings settings;
@@ -322,10 +357,12 @@
 	// TODO wait some time for card to be completely deactivated
 	usart_async_flush_rx_buffer(SIM_peripheral_descriptors[slotnr]); // flush RX buffer to start from scratch
 
-	slot_set_baudrate(slotnr, 2500000 / (372 / 1)); // set USART baud rate to match the interface (f = 2.5 MHz) and card default settings (Fd = 372, Dd = 1)
+	
 	// set clock to lowest frequency (20 MHz / 8 = 2.5 MHz)
 	// note: according to ISO/IEC 7816-3:2006 section 5.2.3 the minimum value is 1 MHz, and maximum is 5 MHz during activation
 	settings.clkdiv = SIM_CLKDIV_8;
+	// set USART baud rate to match the interface (f = 2.5 MHz) and card default settings (Fd = 372, Dd = 1)
+	slot_set_isorate(slotnr, settings.clkdiv, ISO7816_3_DEFAULT_FD, ISO7816_3_DEFAULT_DD);
 	// set card voltage to 3.0 V (the most supported)
 	// note: according to ISO/IEC 7816-3:2006 no voltage should damage the card, and you should cycle from low to high
 	settings.vsel = SIM_VOLT_3V0;

-- 
To view, visit https://gerrit.osmocom.org/13847
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ccid-firmware
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I604f72bb28944962aee055e6d88a435827f79bec
Gerrit-Change-Number: 13847
Gerrit-PatchSet: 1
Gerrit-Owner: Kévin Redon <kredon at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190502/d739e996/attachment.html>


More information about the gerrit-log mailing list