36 #ifdef CONF_WINC_USE_SPI 38 #define USE_OLD_SPI_SW 43 #define NMI_PERIPH_REG_BASE 0x1000 44 #define NMI_INTR_REG_BASE (NMI_PERIPH_REG_BASE+0xa00) 45 #define NMI_CHIPID (NMI_PERIPH_REG_BASE) 46 #define NMI_PIN_MUX_0 (NMI_PERIPH_REG_BASE + 0x408) 47 #define NMI_INTR_ENABLE (NMI_INTR_REG_BASE) 49 #define NMI_SPI_REG_BASE 0xe800 50 #define NMI_SPI_CTL (NMI_SPI_REG_BASE) 51 #define NMI_SPI_MASTER_DMA_ADDR (NMI_SPI_REG_BASE+0x4) 52 #define NMI_SPI_MASTER_DMA_COUNT (NMI_SPI_REG_BASE+0x8) 53 #define NMI_SPI_SLAVE_DMA_ADDR (NMI_SPI_REG_BASE+0xc) 54 #define NMI_SPI_SLAVE_DMA_COUNT (NMI_SPI_REG_BASE+0x10) 55 #define NMI_SPI_TX_MODE (NMI_SPI_REG_BASE+0x20) 56 #define NMI_SPI_PROTOCOL_CONFIG (NMI_SPI_REG_BASE+0x24) 57 #define NMI_SPI_INTR_CTL (NMI_SPI_REG_BASE+0x2c) 59 #define NMI_SPI_PROTOCOL_OFFSET (NMI_SPI_PROTOCOL_CONFIG-NMI_SPI_REG_BASE) 61 #define SPI_BASE NMI_SPI_REG_BASE 65 #define CMD_INTERNAL_WRITE 0xc3 66 #define CMD_INTERNAL_READ 0xc4 69 #define CMD_DMA_EXT_WRITE 0xc7 70 #define CMD_DMA_EXT_READ 0xc8 71 #define CMD_SINGLE_WRITE 0xc9 72 #define CMD_SINGLE_READ 0xca 73 #define CMD_RESET 0xcf 81 #define DATA_PKT_SZ_256 256 82 #define DATA_PKT_SZ_512 512 83 #define DATA_PKT_SZ_1K 1024 84 #define DATA_PKT_SZ_4K (4 * 1024) 85 #define DATA_PKT_SZ_8K (8 * 1024) 86 #define DATA_PKT_SZ DATA_PKT_SZ_8K 88 static uint8 gu8Crc_off = 0;
89 #if (defined __SAMG55J19__) || (defined __SAM4SD32C__) || (defined __SAME70Q21__) || (defined __SAME70Q20B__) || (defined __SAME70Q21B__) 110 nmi_spi_write(bw, sz);
111 return nmi_spi_read(br, sz);
116 return nm_spi_rw(
NULL, b, sz);
120 return nm_spi_rw(b,
NULL, sz);
124 return nm_spi_rw(bw, br, sz);
133 static const uint8 crc7_syndrome_table[256] = {
134 0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f,
135 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
136 0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26,
137 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e,
138 0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d,
139 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45,
140 0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14,
141 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c,
142 0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b,
143 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13,
144 0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42,
145 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a,
146 0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69,
147 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21,
148 0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70,
149 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38,
150 0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e,
151 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36,
152 0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67,
153 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f,
154 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
155 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04,
156 0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55,
157 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d,
158 0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a,
159 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52,
160 0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03,
161 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b,
162 0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28,
163 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60,
164 0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31,
165 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79
171 return crc7_syndrome_table[(crc << 1) ^ data];
177 crc = crc7_byte(crc, *buffer++);
195 case CMD_SINGLE_READ:
196 bc[1] = (
uint8)(adr >> 16);
197 bc[2] = (
uint8)(adr >> 8);
201 case CMD_INTERNAL_READ:
202 bc[1] = (
uint8)(adr >> 8);
203 if(clockless) bc[1] |= (1 << 7);
208 #if defined(CMD_TERMINATE) 216 #if defined(CMD_REPEAT) 230 #if defined(CMD_DMA_WRITE) || defined(CMD_DMA_READ) 233 bc[1] = (
uint8)(adr >> 16);
234 bc[2] = (
uint8)(adr >> 8);
236 bc[4] = (
uint8)(sz >> 8);
241 case CMD_DMA_EXT_WRITE:
242 case CMD_DMA_EXT_READ:
243 bc[1] = (
uint8)(adr >> 16);
244 bc[2] = (
uint8)(adr >> 8);
246 bc[4] = (
uint8)(sz >> 16);
247 bc[5] = (
uint8)(sz >> 8);
251 case CMD_INTERNAL_WRITE:
252 bc[1] = (
uint8)(adr >> 8);
253 if(clockless) bc[1] |= (1 << 7);
254 bc[2] = (
uint8)(adr);
255 bc[3] = (
uint8)(u32data >> 24);
256 bc[4] = (
uint8)(u32data >> 16);
257 bc[5] = (
uint8)(u32data >> 8);
258 bc[6] = (
uint8)(u32data);
261 case CMD_SINGLE_WRITE:
262 bc[1] = (
uint8)(adr >> 16);
263 bc[2] = (
uint8)(adr >> 8);
264 bc[3] = (
uint8)(adr);
265 bc[4] = (
uint8)(u32data >> 24);
266 bc[5] = (
uint8)(u32data >> 16);
267 bc[6] = (
uint8)(u32data >> 8);
268 bc[7] = (
uint8)(u32data);
276 if (result == N_OK) {
278 bc[len-1] = (
crc7(0x7f, (
const uint8 *)&bc[0], len-1)) << 1;
283 M2M_ERR(
"[nmi spi]: Failed cmd write, bus error...\n");
300 #if defined(CMD_TERMINATE) 301 if (cmd == CMD_TERMINATE) {
308 #if defined(CMD_REPEAT) 309 if (cmd == CMD_REPEAT) {
322 M2M_ERR(
"[nmi spi]: Failed cmd response read, bus error...\n");
326 }
while((rsp != cmd) && (s8RetryCnt-- >0));
329 M2M_ERR(
"[nmi spi]: Failed cmd response read\n");
341 M2M_ERR(
"[nmi spi]: Failed cmd response read, bus error...\n");
345 }
while((rsp != 0x00) && (s8RetryCnt-- >0));
348 M2M_ERR(
"[nmi spi]: Failed cmd response read\n");
360 spi_cmd(CMD_RESET, 0, 0, 0, 0);
362 if (spi_cmd_rsp(CMD_RESET) != N_OK) {
365 uint8 w_buf[8] = {0xFF};
367 M2M_ERR(
"[nmi spi]: Failed rst cmd response\n");
368 nmi_spi_writeread(w_buf, r_buf, 8);
369 if (r_buf[7] != 0xFF)
371 M2M_ERR(
"[nmi spi]: Failed repeated reset\n");
391 if (sz <= DATA_PKT_SZ)
394 nbytes = DATA_PKT_SZ;
402 M2M_ERR(
"[nmi spi]: Failed data response read, bus error...\n");
407 if ((rsp & 0xf0) == 0xf0)
411 if (result == N_FAIL)
415 M2M_ERR(
"[nmi spi]: Failed data response read...(%02x)\n", rsp);
424 M2M_ERR(
"[nmi spi]: Failed data block read, bus error...\n");
435 M2M_ERR(
"[nmi spi]: Failed data block crc read, bus error...\n");
454 uint8 cmd, order, crc[2] = {0};
461 if (sz <= DATA_PKT_SZ)
464 nbytes = DATA_PKT_SZ;
471 if (sz <= DATA_PKT_SZ)
476 if (sz <= DATA_PKT_SZ)
483 M2M_ERR(
"[nmi spi]: Failed data block cmd write, bus error...\n");
491 if (
M2M_SUCCESS != nmi_spi_write(&b[ix], nbytes)) {
492 M2M_ERR(
"[nmi spi]: Failed data block write, bus error...\n");
502 M2M_ERR(
"[nmi spi]: Failed data block crc write, bus error...\n");
543 uint8 cmd = CMD_SINGLE_WRITE;
550 cmd = CMD_INTERNAL_WRITE;
559 #if defined USE_OLD_SPI_SW 560 result = spi_cmd(cmd, addr, u32data, 4, clockless);
561 if (result != N_OK) {
562 M2M_ERR(
"[nmi spi]: Failed cmd, write reg (%08x)...\n", (
unsigned int)addr);
566 result = spi_cmd_rsp(cmd);
567 if (result != N_OK) {
568 M2M_ERR(
"[nmi spi]: Failed cmd response, write reg (%08x)...\n", (
unsigned int)addr);
576 result = spi_cmd_complete(cmd, addr, (
uint8*)&u32data, 4, clockless);
577 if (result != N_OK) {
578 M2M_ERR(
"[nmi spi]: Failed cmd, write reg (%08x)...\n", addr);
604 uint8 cmd = CMD_DMA_EXT_WRITE;
610 #if defined USE_OLD_SPI_SW 616 result = spi_cmd(cmd, addr, 0, size,0);
617 if (result != N_OK) {
618 M2M_ERR(
"[nmi spi]: Failed cmd, write block (%08x)...\n", (
unsigned int)addr);
622 result = spi_cmd_rsp(cmd);
623 if (result != N_OK) {
624 M2M_ERR(
"[nmi spi ]: Failed cmd response, write block (%08x)...\n", (
unsigned int)addr);
629 result = spi_cmd_complete(cmd, addr,
NULL, size, 0);
630 if (result != N_OK) {
631 M2M_ERR(
"[nmi spi]: Failed cmd, write block (%08x)...\n", addr);
639 result = spi_data_write(buf, size);
640 if (result != N_OK) {
641 M2M_ERR(
"[nmi spi]: Failed block data write...\n");
664 uint8 cmd = CMD_SINGLE_READ;
673 cmd = CMD_INTERNAL_READ;
682 #if defined USE_OLD_SPI_SW 683 result = spi_cmd(cmd, addr, 0, 4, clockless);
684 if (result != N_OK) {
685 M2M_ERR(
"[nmi spi]: Failed cmd, read reg (%08x)...\n", (
unsigned int)addr);
689 result = spi_cmd_rsp(cmd);
690 if (result != N_OK) {
691 M2M_ERR(
"[nmi spi]: Failed cmd response, read reg (%08x)...\n", (
unsigned int)addr);
697 result = spi_data_read(tmp, 4, clockless);
698 if (result != N_OK) {
699 M2M_ERR(
"[nmi spi]: Failed data read...\n");
704 result = spi_cmd_complete(cmd, addr, (
uint8*)&tmp[0], 4, clockless);
705 if (result != N_OK) {
706 M2M_ERR(
"[nmi spi]: Failed cmd, read reg (%08x)...\n", addr);
736 uint8 cmd = CMD_DMA_EXT_READ;
739 #if defined USE_OLD_SPI_SW 741 uint8 single_byte_workaround = 0;
747 #if defined USE_OLD_SPI_SW 752 single_byte_workaround = 1;
754 result = spi_cmd(cmd, addr, 0, size,0);
755 if (result != N_OK) {
756 M2M_ERR(
"[nmi spi]: Failed cmd, read block (%08x)...\n", (
unsigned int)addr);
760 result = spi_cmd_rsp(cmd);
761 if (result != N_OK) {
762 M2M_ERR(
"[nmi spi]: Failed cmd response, read block (%08x)...\n", (
unsigned int)addr);
770 if (single_byte_workaround)
772 result = spi_data_read(tmp, size,0);
776 result = spi_data_read(buf, size,0);
777 if (result != N_OK) {
778 M2M_ERR(
"[nmi spi]: Failed block data read...\n");
783 result = spi_cmd_complete(cmd, addr, buf, size, 0);
784 if (result != N_OK) {
785 M2M_ERR(
"[nmi spi]: Failed cmd, read block (%08x)...\n", addr);
799 static void spi_init_pkt_sz(
void)
805 val32 &= ~(0x7 << 4);
808 case 256: val32 |= (0 << 4);
break;
809 case 512: val32 |= (1 << 4);
break;
810 case 1024: val32 |= (2 << 4);
break;
811 case 2048: val32 |= (3 << 4);
break;
812 case 4096: val32 |= (4 << 4);
break;
813 case 8192: val32 |= (5 << 4);
break;
843 M2M_ERR(
"[nmi spi]: Failed internal read protocol with CRC on, retyring with CRC off...\n");
846 M2M_ERR(
"[nmi spi]: Failed internal read protocol...\n");
856 M2M_ERR(
"[nmi spi]: Failed internal write protocol reg...\n");
866 M2M_ERR(
"[nmi spi]: Fail cmd read chip id...\n");
870 M2M_DBG(
"[nmi spi]: chipid (%08x)\n", (
unsigned int)chipid);
Structure holding SPI R/W parameters.
This module contains common APIs declarations.
signed short sint16
Range of values between -32768 to 32767.
signed char sint8
Range of values between -128 to 127.
sint8 nm_spi_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
This module contains WINC3400 bus wrapper APIs declarations.
unsigned short uint16
Range of values between 0 to 65535.
static void spi_reset(Spi *p_spi)
Reset SPI and set it to Slave mode.
sint8 nm_spi_write_reg(uint32 u32Addr, uint32 u32Val)
sint8 nm_spi_init(void)
Initialize the SPI.
USBInterfaceDescriptor data
sint8 nm_spi_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
uint32 nm_spi_read_reg(uint32 u32Addr)
unsigned long uint32
Range of values between 0 to 4294967295.
unsigned char uint8
Range of values between 0 to 255.
sint8 nm_spi_read_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
sint8 nm_bus_ioctl(uint8 u8Cmd, void *pvParameter)
static uint8 crc7(uint8 crc, const uint8 *buff, uint16 len)
This module contains WINC3400 SPI protocol bus APIs implementation.
sint8 nm_spi_deinit(void)
DeInitialize the SPI.