/*********************************************************************//** * @brief Transmit a single data through SPIx peripheral * @param[in] SPIx SPI peripheral selected, should be SPI * @param[in] Data Data to transmit (must be 16 or 8-bit long, * this depend on SPI data bit number configured) * @return none **********************************************************************/ void SPI_SendData(LPC_SPI_TypeDef* SPIx, uint16_t Data) { CHECK_PARAM(PARAM_SPIx(SPIx)); CS_Force(0); for(int i =0;i<1000;i++); //9600 //for(int i =0;i<300;i++); //115200, controller need configure system clock to 16MHz SPIx->SPDR = Data & SPI_SPDR_BITMASK; }
// ============================= Отправляет команду на СП из буфера rd_UART и читает ответ в wr_UART ============================================= // Status SendRcvdCmd() { uint8_t length; uint16_t delay = 100; unsigned char *p_Out = &data_wr_UART[0]; Timer1_Start(2500); //-----------очистка буфера от мусора(заполнение нулями) memset(data_wr_UART,0,255); CS_Force(0); //активируем SS if(WAIT) //если перепад при неактивном WAIT { CS_Force(1); //сбрасываем SS while(WAIT); //ждем перепада WAIT в активное состояние CS_Force(0); //снова активируем SS } //------------отправка команды for(length=0;length<UART_RCV_COUNT;length++) { delay = 50; while(delay--); SPY_Byte(data_rd_UART[length]); } CS_Force(1); Timer1_Stop(); while(!WAIT); //ждем перепада в 1 while(WAIT); //ждем готовности(перепада в 0) Timer1_Start(2500); CS_Force(0); //-------------получение ответа SPY_Byte(0);//отправка лишнего байта чтобы избавиться от мусора(остатка последней команды) for(length=1;length<255;length++) { *p_Out++ = SPY_Byte(length); delay = 50; while(delay--); if(WAIT) break; //если придет WAIT==1, значит пора прекратить прием данных } CS_Force(1); return SUCCESS; }
/*********************************************************************//** * @brief Standard Private SPI Interrupt handler * @param[in] None * @return None ***********************************************************************/ void SPI_IntHandler(void) { SPI_DATA_SETUP_Type *xf_setup; uint16_t tmp; xf_setup = (SPI_DATA_SETUP_Type *)spidat.txrx_setup; CS_Force(1); /* Dummy read to clear SPI interrupt flag */ if (LPC_SPI->SPINT & SPI_SPINT_INTFLAG){ LPC_SPI->SPINT = SPI_SPINT_INTFLAG; } // save status tmp = LPC_SPI->SPSR; xf_setup->status = tmp; // Check for error if (tmp & (SPI_SPSR_ABRT | SPI_SPSR_MODF | SPI_SPSR_ROVR | SPI_SPSR_WCOL)){ xf_setup->status |= SPI_STAT_ERROR; // Disable Interrupt and call call-back SPI_IntCmd(LPC_SPI, DISABLE); if (xf_setup->callback != NULL){ xf_setup->callback(); } return; } /* Check SPI complete flag */ if (tmp & SPI_SPSR_SPIF){ // Read data from SPI data tmp = SPI_ReceiveData(LPC_SPI); if (xf_setup->rx_data != NULL) { // if (spidat.dataword == 0){ // *(uint8_t *)(xf_setup->rx_data + xf_setup->counter) = (uint8_t) tmp; // } else { // *(uint16_t *)(xf_setup->rx_data + xf_setup->counter) = (uint8_t) tmp; // } if (spidat.dataword == 0){ *(uint8_t *)((uint8_t *)(xf_setup->rx_data) + xf_setup->counter) = (uint8_t) tmp; } else { *(uint16_t *)((uint8_t *)(xf_setup->rx_data) + xf_setup->counter) = (uint8_t) tmp; } } if (xf_setup->rd_length) { xf_setup->rd_length--; } if(xf_setup->rd_length==0) { xf_setup->rx_data = NULL; } /* if (!(GPIO_ReadValue(0) & (1<<3))||(xf_setup->tx_data != NULL)) { if (spidat.dataword == 0){ xf_setup->counter++; } else { xf_setup->counter += 2; } }*/ if ((xf_setup->rd_length == 0) && (xf_setup->wr_length == 0)) { xf_setup->counter = xf_setup->length; } else { // Increase counter if (spidat.dataword == 0){ xf_setup->counter++; } else { xf_setup->counter += 2; } } } if (xf_setup->counter < xf_setup->length) { if (xf_setup->wr_length) { xf_setup->wr_length--; } if(xf_setup->wr_length==0) { xf_setup->tx_data = NULL; } // Write data to buffer if(xf_setup->tx_data == NULL){ if (spidat.dataword == 0){ SPI_SendData(LPC_SPI, 0xFF); } else { SPI_SendData(LPC_SPI, 0xFFFF); } } else { // if (spidat.dataword == 0){ // SPI_SendData(SPI, (*(uint8_t *)(xf_setup->tx_data + xf_setup->counter))); // } else { // SPI_SendData(SPI, (*(uint16_t *)(xf_setup->tx_data + xf_setup->counter))); // } if (spidat.dataword == 0){ SPI_SendData(LPC_SPI, (*(uint8_t *)((uint8_t *)(xf_setup->tx_data) + xf_setup->counter))); } else { SPI_SendData(LPC_SPI, (*(uint16_t *)((uint8_t *)(xf_setup->tx_data) + xf_setup->counter))); } } } // No more data to send else { xf_setup->status |= SPI_STAT_DONE; // Disable Interrupt and call call-back SPI_IntCmd(LPC_SPI, DISABLE); if (xf_setup->callback != NULL){ xf_setup->callback(); } } }
Status Crypto_Command(uint8_t c_command) { uint8_t tmp; uint8_t mass[512]; uint32_t i; if(c_command > M_FPO) return ERROR; if (!(W_Wait(1000))) return ERROR; switch (c_command) { case M_DESTROY: break; case M_WRITE: break; case M_READ: break; case M_ERASE: break; case M_STATUS: case M_DUMMY: case M_NOP: CS_Force(0); Crypto.c_status = SPY_Byte(M_STATUS); CS_Force(1); break; case M_WRBUF: for (i=0;i<511;i++) mass[i]=0; CS_Force(0); Crypto.c_status = SPY_Byte(M_WRBUF); for(i=0;i<500;i++){ if (!W_Wait(1000)) { i=511; } else { SPY_Byte(i); for (tmp = 50; tmp; tmp--); } } CS_Force(1); CS_Force(0); Crypto.c_status = SPY_Byte(M_RDBUF); for(i=0;i<500;i++){ if (!W_Wait(1000)) { i = 511; } else { mass[i]=SPY_Byte(0xFF); for (tmp = 50; tmp; tmp--); } } CS_Force(1); break; case M_RDBUF: break; case M_SET0: break; case M_SET1: break; case M_FPO: break; default: break; } return SUCCESS; }
/*********************************************************************//** * @brief c_entry: Main SSP program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t tmpchar[2] = {0, 0}; PINSEL_CFG_Type PinCfg; __IO FlagStatus exitflag; /* * Initialize SPI pin connect * P0.15 - SCK * P0.16 - SSEL - used as GPIO * P0.17 - MISO * P0.18 - MOSI */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 15; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 17; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 18; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 16; PinCfg.Funcnum = 0; PINSEL_ConfigPin(&PinCfg); /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); // initialize SSP configuration structure to default SSP_ConfigStructInit(&SSP_ConfigStruct); // Initialize SSP peripheral with parameter given in structure above SSP_Init(LPC_SSP0, &SSP_ConfigStruct); // Initialize /CS pin to GPIO function CS_Init(); // Enable SSP peripheral SSP_Cmd(LPC_SSP0, ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(SSP0_IRQn, ((0x01<<3)|0x01)); /* Enable SSP0 interrupt */ NVIC_EnableIRQ(SSP0_IRQn); /* First, send some command to reset SC16IS740 chip via SSP bus interface * note driver /CS pin to low state before transferring by CS_Enable() function */ complete = RESET; CS_Force(0); xferConfig.tx_data = iocon_cfg; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iocon_cfg); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_INTERRUPT); while (complete == RESET); CS_Force(1); complete = RESET; CS_Force(0); xferConfig.tx_data = iodir_cfg; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iodir_cfg); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_INTERRUPT); while (complete == RESET); CS_Force(1); // Reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { while((tmpchar[0] = _DG) == 0); if (tmpchar[0] == 27){ /* ESC key, set exit flag */ _DBG_(menu2); exitflag = SET; } else if (tmpchar[0] == 'r'){ print_menu(); } else { if (tmpchar[0] == '1') { // LEDs are ON now... CS_Force(0); xferConfig.tx_data = iostate_on; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iostate_on); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); } else if (tmpchar[0] == '2') { // LEDs are OFF now... CS_Force(0); xferConfig.tx_data = iostate_off; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iostate_off); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); } /* Then Echo it back */ _DBG_(tmpchar); } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy(LPC_UART0) == SET ); // DeInitialize UART0 peripheral UART_DeInit(LPC_UART0); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main SPI program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { PINSEL_CFG_Type PinCfg; SPI_DATA_SETUP_Type xferConfig; uint32_t tmp; /* * Initialize SPI pin connect * P0.15 - SCK; * P0.16 - SSEL - used as GPIO * P0.17 - MISO * P0.18 - MOSI */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 15; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 17; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 18; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 16; PinCfg.Funcnum = 0; PINSEL_ConfigPin(&PinCfg); /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); SPI_ConfigStruct.CPHA = SPI_CPHA_SECOND; SPI_ConfigStruct.CPOL = SPI_CPOL_LO; SPI_ConfigStruct.ClockRate = 2000000; SPI_ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST; SPI_ConfigStruct.Databit = SPI_DATABIT_SIZE; SPI_ConfigStruct.Mode = SPI_MASTER_MODE; // Initialize SPI peripheral with parameter given in structure above SPI_Init(LPC_SPI, &SPI_ConfigStruct); _DBG_("Press '1' to start transfer..."); while (_DG != '1'); _DBG_("Init buffer"); /* Initialize Buffer */ Buffer_Init(); CS_Init(); _DBG_("Start transfer..."); CS_Force(0); // delay for a while for (tmp = 10000; tmp; tmp--); xferConfig.tx_data = Tx_Buf; xferConfig.rx_data = Rx_Buf; xferConfig.length = BUFFER_SIZE; SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_POLLING); // delay for a while for (tmp = 10000; tmp; tmp--); CS_Force(1); _DBG_("Complete!"); // Verify buffer after transferring Buffer_Verify(); _DBG_("Verify complete!"); SPI_DeInit(LPC_SPI); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief Main SSP program body **********************************************************************/ int c_entry(void) { uint8_t tmpchar[2] = {0, 0}; PINSEL_CFG_Type PinCfg; __IO FlagStatus exitflag; SSP_DATA_SETUP_Type xferConfig; // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif /* * Initialize SPI pin connect * P0.15 - SCK * P0.16 - SSEL - used as GPIO * P0.17 - MISO * P0.18 - MOSI */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 15; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 17; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 18; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 16; PinCfg.Funcnum = 0; PINSEL_ConfigPin(&PinCfg); /* * Initialize debug via UART */ debug_frmwrk_init(); // print welcome screen print_menu(); // initialize SSP configuration structure to default SSP_ConfigStructInit(&SSP_ConfigStruct); // Initialize SSP peripheral with parameter given in structure above SSP_Init(LPC_SSP0, &SSP_ConfigStruct); // Initialize /CS pin to GPIO function CS_Init(); // Enable SSP peripheral SSP_Cmd(LPC_SSP0, ENABLE); /* First, send some command to reset SC16IS740 chip via SSP bus interface * note driver /CS pin to low state before transferring by CS_Enable() function */ CS_Force(0); xferConfig.tx_data = iocon_cfg; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iocon_cfg); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); CS_Force(0); xferConfig.tx_data = iodir_cfg; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iodir_cfg); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); // Reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { while((tmpchar[0] = _DG) == 0); if (tmpchar[0] == 27){ /* ESC key, set exit flag */ _DBG_(menu2); exitflag = SET; } else if (tmpchar[0] == 'r'){ print_menu(); } else { if (tmpchar[0] == '1') { // LEDs are ON now... CS_Force(0); xferConfig.tx_data = iostate_on; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iostate_on); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); } else if (tmpchar[0] == '2') { // LEDs are OFF now... CS_Force(0); xferConfig.tx_data = iostate_off; xferConfig.rx_data = sspreadbuf; xferConfig.length = sizeof (iostate_off); SSP_ReadWrite(LPC_SSP0, &xferConfig, SSP_TRANSFER_POLLING); CS_Force(1); } /* Then Echo it back */ _DBG_(tmpchar); } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy(LPC_UART0) == SET); // DeInitialize UART0 peripheral UART_DeInit(LPC_UART0); /* Loop forever */ while(1); return 1; }