#define MT29F_ADDR_ADDR 0x60200000
#define MT29F_DATA_ADDR 0x60000000
+// Get chip select mask for command register
+#define MT29F_CSID(chip) (((chip)->chip_select << NFC_CMD_CSID_SHIFT) & NFC_CMD_CSID_MASK)
+
/*
* Translate flash page index plus a byte offset
}
-static bool isOperationComplete(void)
+static bool isOperationComplete(Mt29f *chip)
{
uint8_t status;
- sendCommand(
- NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
MT29F_CMD_STATUS << 2,
0, 0, 0);
}
-static void chipReset(void)
+static void chipReset(Mt29f *chip)
{
- sendCommand(
- NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
MT29F_CMD_RESET << 2,
0, 0, 0);
getAddrCycles(page, 0, &cycle0, &cycle1234);
- sendCommand(
- NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
(MT29F_CMD_ERASE_2 << 10) | (MT29F_CMD_ERASE_1 << 2),
3, 0, cycle1234 >> 8);
waitReadyBusy();
- if (!isOperationComplete())
+ if (!isOperationComplete(chip))
{
LOG_ERR("mt29f: error erasing block\n");
chip->status |= MT29F_ERR_ERASE;
*/
bool mt29f_getDevId(Mt29f *chip, uint8_t dev_id[5])
{
- sendCommand(
- NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_ONE |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_ONE |
MT29F_CMD_READID << 2,
1, 0, 0);
static bool checkEcc(void)
{
- // TODO: implement it actually...
- LOG_INFO("ECC_SR1: 0x%lx\n", SMC_ECC_SR1);
- return true;
+ uint32_t sr1 = SMC_ECC_SR1;
+
+ if (sr1)
+ {
+ LOG_INFO("ECC error, ECC_SR1=0x%lx\n", sr1);
+ return false;
+ }
+ else
+ return true;
}
getAddrCycles(page, offset, &cycle0, &cycle1234);
- sendCommand(
- NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
(MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
5, cycle0, cycle1234);
/*
- * Read page data and ECC, checking for errors and fixing them if
- * possible.
+ * Read page data and ECC, checking for errors.
+ * TODO: fix errors with ECC when possible.
*/
bool mt29f_read(Mt29f *chip, uint32_t page, void *buf, uint16_t size)
{
memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
- checkEcc();
-
- return true;
+ return checkEcc();
}
getAddrCycles(page, offset, &cycle0, &cycle1234);
- sendCommand(
- NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_FIVE |
MT29F_CMD_WRITE_1 << 2,
5, cycle0, cycle1234);
return false;
}
- sendCommand(
- NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
+ sendCommand(MT29F_CSID(chip) |
+ NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
MT29F_CMD_WRITE_2 << 2,
0, 0, 0);
waitReadyBusy();
- if (!isOperationComplete())
+ if (!isOperationComplete(chip))
{
LOG_ERR("mt29f: error writing page\n");
chip->status |= MT29F_ERR_WRITE;
static void initPio(void)
{
/*
- * TODO: put following stuff in hw_ file dependent (and configurable cs?)
+ * TODO: put following stuff in hw_ file dependent
* Parameters for MT29F8G08AAD
*/
pmc_periphEnable(PIOA_ID);
}
-void mt29f_init(Mt29f *chip)
+void mt29f_init(Mt29f *chip, uint8_t chip_select)
{
- initPio();
- initSmc();
+ memset(chip, 0, sizeof(Mt29f));
- mt29f_clearError(chip);
+ chip->chip_select = chip_select;
- chipReset();
+ initPio();
+ initSmc();
+ chipReset(chip);
}
#define NFC_CMD_ACYCLE_THREE (0x3 << 19) ///< Three address cycles
#define NFC_CMD_ACYCLE_FOUR (0x4 << 19) ///< Four address cycles
#define NFC_CMD_ACYCLE_FIVE (0x5 << 19) ///< Five address cycles
-#define NFC_CMD_CSID_MASK (0x7 << 22) ///< Chip Select Identifier
-#define NFC_CMD_CSID_0 (0x0 << 22) ///< CS0
-#define NFC_CMD_CSID_1 (0x1 << 22) ///< CS1
-#define NFC_CMD_CSID_2 (0x2 << 22) ///< CS2
-#define NFC_CMD_CSID_3 (0x3 << 22) ///< CS3
-#define NFC_CMD_CSID_4 (0x4 << 22) ///< CS4
-#define NFC_CMD_CSID_5 (0x5 << 22) ///< CS5
-#define NFC_CMD_CSID_6 (0x6 << 22) ///< CS6
-#define NFC_CMD_CSID_7 (0x7 << 22) ///< CS7
+#define NFC_CMD_CSID_SHIFT 22 ///< Chip Select shift
+#define NFC_CMD_CSID_MASK (0x7 << NFC_CMD_CSID_SHIFT) ///< Chip Select mask
#define NFC_CMD_NFCEN BV(25) ///< NFC Enable
#define NFC_CMD_NFCWR BV(26) ///< NFC Write Enable
#define NFC_CMD_NFCCMD BV(27) ///< NFC Command Enable