unsigned short ADS7843_read(volatile unsigned short mode) { volatile int tmp; GPIO_ClearValue(4,1<<12); SSP_SendData(LPC_SSP0,mode); SSP_SendData(LPC_SSP0,0); while(LPC_SSP0->SR & SSP_SR_BSY) ; mode=(SSP_ReceiveData(LPC_SSP0)); mode=(SSP_ReceiveData(LPC_SSP0)); return mode; // x = 0..127, y = 0..127 }
/***************************************************************************** * Function Name : Flash_ReadWriteByte * Description : send a byte to SST25VF016B * Input : - data: send data * Output : None * Return : return data * Attention : None *******************************************************************************/ uint8_t SPI_ReadByte(uint8_t* data) { uint16_t tmp; while ((SSP_GetStatus(RTC_FLASH_SSP, SSP_STAT_RXFIFO_NOTEMPTY) == SET )) { tmp = SSP_ReceiveData(RTC_FLASH_SSP); } SSP_SendData(RTC_FLASH_SSP, (uint16_t) 0xFFFF); while ((SSP_GetStatus(RTC_FLASH_SSP, SSP_STAT_RXFIFO_NOTEMPTY) == RESET )) { } tmp = SSP_ReceiveData(RTC_FLASH_SSP); return tmp; }
/*********************************************************************//** * @brief Get command from master in slave mode * @param[in] None * @return None **********************************************************************/ uint8_t ssp_MW_GetCMD(LPC_SSP_TypeDef *SSPx) { // Wait for coming CMD while (SSP_GetStatus(SSPx, SSP_STAT_RXFIFO_NOTEMPTY) == RESET); return ((uint8_t)(SSP_ReceiveData(SSPx))); }
/******************************************************************************* * Function Name : WR_CMD * Description : 向 ADS7843写数据 * Input : - cmd: 传输的数据 * Output : None * Return : None * Attention : None *******************************************************************************/ static uint8_t WR_CMD (uint8_t cmd) { /* wait for current SSP activity complete */ while (SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY) == SET); SSP_SendData(LPC_SSP0, (uint16_t) cmd); while (SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY) == RESET); return (SSP_ReceiveData(LPC_SSP0)); }
/******************************************************************************* * Function Name : LPC17xx_SPI_SendRecvByte * Description : Send one byte then recv one byte of response * Input : - byte_s: byte_s * Output : None * Return : None * Attention : None *******************************************************************************/ uint8_t LPC17xx_SPI_SendRecvByte (uint8_t byte_s) { /* wait for current SSP activity complete */ while (SSP_GetStatus(LPC_SSP0, SSP_STAT_TXFIFO_NOTFULL) == RESET); SSP_SendData(LPC_SSP0, (uint16_t) byte_s); while (SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY) == RESET); return (SSP_ReceiveData(LPC_SSP0)); //return 1; }
/*********************************************************************//** * @brief SSP Read write in polling mode function (Slave mode) * @param[in] SSPdev: Pointer to SSP device * @param[out] rbuffer: pointer to read buffer * @param[in] wbuffer: pointer to write buffer * @param[in] length: length of data to read and write * @return 0 if there no data to send, otherwise return 1 ***********************************************************************/ int32_t ssp_SlaveReadWrite (LPC_SSP_TypeDef *SSPx, void *rbuffer, void *wbuffer, uint32_t length) { uint16_t tmp; pRdBuf_S = (uint8_t *) rbuffer; pWrBuf_S = (uint8_t *) wbuffer; DatLen_S = length; RdIdx_S = 0; WrIdx_S = 0; // wait for current SSP activity complete while (SSP_GetStatus(SSPx, SSP_STAT_BUSY)) { tmp = SSP_ReceiveData(SSPx); } /* Clear all remaining data in RX FIFO */ while (SSP_GetStatus(SSPx, SSP_STAT_RXFIFO_NOTEMPTY)) { tmp = SSP_ReceiveData(SSPx); } #if (USEDSSPDEV_S == 0) if (length != 0) { SSP0_IRQHandler(); } #endif #if (USEDSSPDEV_S == 1) if (length != 0) { SSP1_IRQHandler(); } #endif // Return 0 return 0; }
/** * @brief Main program * @param None * @retval None */ int main() { //uint8_t tx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; //uint8_t rx_size[8] = { 2, 2, 2, 2, 2, 2, 2, 2 }; uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03}; uint8_t src_addr[4] = {192, 168, 0, 9}; uint8_t gw_addr[4] = {192, 168, 0, 1}; uint8_t sub_addr[4] = {255, 255, 255, 0}; //uint8_t dns_server[4] = {8, 8, 8, 8}; // for Example domain name server uint8_t tmp[8]; int32_t ret; uint16_t port=5000, size = 0, sentsize=0; uint8_t destip[4]; uint16_t destport; *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock setting 48MHz /* External Clock */ //CRG_PLL_InputFrequencySelect(CRG_OCLK); /* Set Systme init */ SystemInit(); SSP0_Initialize(); SSP1_Initialize(); GPIO_Initialize(); GPIO_SetBits(GPIOC, GPIO_Pin_8); // LED red off GPIO_SetBits(GPIOC, GPIO_Pin_9); // LED green off GPIO_ResetBits(GPIOC, GPIO_Pin_6); // Test off /* UART Init */ UART_StructInit(&UART_InitStructure); UART_Init(UART1,&UART_InitStructure); /* SysTick_Config */ SysTick_Config((GetSystemClock()/1000)); /* Set WZ_100US Register */ setTIC100US((GetSystemClock()/10000)); //getTIC100US(); //printf(" GetSystemClock: %X, getTIC100US: %X, (%X) \r\n", // GetSystemClock, getTIC100US(), *(uint32_t *)TIC100US); #ifdef __DEF_USED_IC101AG__ //For using IC+101AG *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - set PAD strengh and pull-up *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - set PAD strengh and pull-up *(volatile uint32_t *)(0x41003050) = 0x64; //TXE - set PAD strengh and pull-up #endif printf("PHY is linked. \r\n"); #ifdef __DEF_USED_MDIO__ /* mdio Init */ mdio_init(GPIOB, MDC, MDIO ); //mdio_error_check(GPIOB, MDC, MDIO); //need verify... /* PHY Link Check via gpio mdio */ while( link() == 0x0 ) { printf("."); delay(500); } printf("PHY is linked. \r\n"); #else delay(1000); delay(1000); #endif /* Network Configuration */ setSHAR(mac_addr); setSIPR(src_addr); setGAR(gw_addr); setSUBR(sub_addr); getSHAR(tmp); printf(" MAC ADDRESS : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\r\n",tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]); getSIPR(tmp); printf("IP ADDRESS : %d.%d.%d.%d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getGAR(tmp); printf("GW ADDRESS : %d.%d.%d.%d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); getSUBR(tmp); printf("SN MASK: %d.%d.%d.%d\r\n",tmp[0],tmp[1],tmp[2],tmp[3]); /* Set Network Configuration */ //wizchip_init(tx_size, rx_size); printf(" TEST- START \r\n"); while(1) { switch(getSn_SR(SOCK_NUM)) { case SOCK_ESTABLISHED: if(getSn_IR(SOCK_NUM) & Sn_IR_CON) { getSn_DIPR(SOCK_NUM, destip); destport = getSn_DPORT(SOCK_NUM); printf("%d:Connected - %d.%d.%d.%d : %d\r\n",SOCK_NUM, destip[0], destip[1], destip[2], destip[3], destport); setSn_IR(SOCK_NUM,Sn_IR_CON); } if((size = getSn_RX_RSR(SOCK_NUM)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur. { if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE; ret = recv(SOCK_NUM, test_buf, size); if(ret <= 0) return ret; // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY. /* Send only data to SSP1 */ for (TxIdx=0; TxIdx<size; TxIdx++) { SSP_SendData(SSP0, test_buf[TxIdx]); while( SSP_GetFlagStatus(SSP0, SSP_FLAG_BSY) ); } /* Receive only data from SSP0 */ while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RNE)) { SSP1_Buffer_Rx[RxIdx] = SSP_ReceiveData(SSP1); RxIdx++; } RxIdx=0; sentsize = 0; while(size != sentsize) { ret = send(SOCK_NUM, SSP1_Buffer_Rx+sentsize, size-sentsize); if(ret < 0) { close(SOCK_NUM); return ret; } sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. } } break; case SOCK_CLOSE_WAIT: printf("%d:CloseWait\r\n",SOCK_NUM); if((ret = disconnect(SOCK_NUM)) != SOCK_OK) return ret; printf("%d:Socket Closed\r\n", SOCK_NUM); break; case SOCK_INIT: printf("%d:Listen, TCP server loopback, port [%d]\r\n", SOCK_NUM, port); if( (ret = listen(SOCK_NUM)) != SOCK_OK) return ret; break; case SOCK_CLOSED: printf("%d:TCP server loopback start\r\n",SOCK_NUM); if((ret = socket(SOCK_NUM, Sn_MR_TCP, port, 0x00)) != SOCK_NUM) return ret; printf("%d:Socket opened\r\n",SOCK_NUM); break; default: break; } } }
/*********************************************************************//** * @brief SSP Read write data function * @param[in] SSPx Pointer to SSP peripheral, should be * - LPC_SSP0: SSP0 peripheral * - LPC_SSP1: SSP1 peripheral * @param[in] dataCfg Pointer to a SSP_DATA_SETUP_Type structure that * contains specified information about transmit * data configuration. * @param[in] xfType Transfer type, should be: * - SSP_TRANSFER_POLLING: Polling mode * - SSP_TRANSFER_INTERRUPT: Interrupt mode * @return Actual Data length has been transferred in polling mode. * In interrupt mode, always return (0) * Return (-1) if error. * Note: This function can be used in both master and slave mode. ***********************************************************************/ int32_t SSP_ReadWrite (LPC_SSP_TypeDef *SSPx, SSP_DATA_SETUP_Type *dataCfg, \ SSP_TRANSFER_Type xfType) { uint8_t *rdata8 = 0; uint8_t *wdata8 = 0; uint16_t *rdata16 = 0; uint16_t *wdata16 = 0; uint32_t stat; uint32_t tmp; int32_t dataword; dataCfg->rx_cnt = 0; dataCfg->tx_cnt = 0; dataCfg->status = 0; /* Clear all remaining data in RX FIFO */ while (SSPx->SR & SSP_SR_RNE){ tmp = (uint32_t) SSP_ReceiveData(SSPx); } // Clear status SSPx->ICR = SSP_ICR_BITMASK; if(SSP_GetDataSize(SSPx)>8) dataword = 1; else dataword = 0; // Polling mode ---------------------------------------------------------------------- if (xfType == SSP_TRANSFER_POLLING){ if (dataword == 0){ rdata8 = (uint8_t *)dataCfg->rx_data; wdata8 = (uint8_t *)dataCfg->tx_data; } else { rdata16 = (uint16_t *)dataCfg->rx_data; wdata16 = (uint16_t *)dataCfg->tx_data; } while ((dataCfg->tx_cnt != dataCfg->length) || (dataCfg->rx_cnt != dataCfg->length)){ if ((SSPx->SR & SSP_SR_TNF) && (dataCfg->tx_cnt != dataCfg->length)){ // Write data to buffer if(dataCfg->tx_data == NULL){ if (dataword == 0){ SSP_SendData(SSPx, 0xFF); dataCfg->tx_cnt++; } else { SSP_SendData(SSPx, 0xFFFF); dataCfg->tx_cnt += 2; } } else { if (dataword == 0){ SSP_SendData(SSPx, *wdata8); wdata8++; dataCfg->tx_cnt++; } else { SSP_SendData(SSPx, *wdata16); wdata16++; dataCfg->tx_cnt += 2; } } } // Check overrun error if ((stat = SSPx->RIS) & SSP_RIS_ROR){ // save status and return dataCfg->status = stat | SSP_STAT_ERROR; return (-1); } // Check for any data available in RX FIFO while ((SSPx->SR & SSP_SR_RNE) && (dataCfg->rx_cnt != dataCfg->length)){ // Read data from SSP data tmp = SSP_ReceiveData(SSPx); // Store data to destination if (dataCfg->rx_data != NULL) { if (dataword == 0){ *(rdata8) = (uint8_t) tmp; rdata8++; } else { *(rdata16) = (uint16_t) tmp; rdata16++; } } // Increase counter if (dataword == 0){ dataCfg->rx_cnt++; } else { dataCfg->rx_cnt += 2; } } } // save status dataCfg->status = SSP_STAT_DONE; if (dataCfg->tx_data != NULL){ return dataCfg->tx_cnt; } else if (dataCfg->rx_data != NULL){ return dataCfg->rx_cnt; } else { return (0); } } // Interrupt mode ---------------------------------------------------------------------- else if (xfType == SSP_TRANSFER_INTERRUPT){ while ((SSPx->SR & SSP_SR_TNF) && (dataCfg->tx_cnt != dataCfg->length)){ // Write data to buffer if(dataCfg->tx_data == NULL){ if (dataword == 0){ SSP_SendData(SSPx, 0xFF); dataCfg->tx_cnt++; } else { SSP_SendData(SSPx, 0xFFFF); dataCfg->tx_cnt += 2; } } else { if (dataword == 0){ SSP_SendData(SSPx, (*(uint8_t *)((uint32_t)dataCfg->tx_data + dataCfg->tx_cnt))); dataCfg->tx_cnt++; } else { SSP_SendData(SSPx, (*(uint16_t *)((uint32_t)dataCfg->tx_data + dataCfg->tx_cnt))); dataCfg->tx_cnt += 2; } } // Check error if ((stat = SSPx->RIS) & SSP_RIS_ROR){ // save status and return dataCfg->status = stat | SSP_STAT_ERROR; return (-1); } // Check for any data available in RX FIFO while ((SSPx->SR & SSP_SR_RNE) && (dataCfg->rx_cnt != dataCfg->length)){ // Read data from SSP data tmp = SSP_ReceiveData(SSPx); // Store data to destination if (dataCfg->rx_data != NULL) { if (dataword == 0){ *(uint8_t *)((uint32_t)dataCfg->rx_data + dataCfg->rx_cnt) = (uint8_t) tmp; } else { *(uint16_t *)((uint32_t)dataCfg->rx_data + dataCfg->rx_cnt) = (uint16_t) tmp; } } // Increase counter if (dataword == 0){ dataCfg->rx_cnt++; } else { dataCfg->rx_cnt += 2; } } } // If there more data to sent or receive if ((dataCfg->rx_cnt != dataCfg->length) || (dataCfg->tx_cnt != dataCfg->length)){ // Enable all interrupt SSPx->IMSC = SSP_IMSC_BITMASK; } else { // Save status dataCfg->status = SSP_STAT_DONE; } return (0); } return (-1); }
/** * @brief Main Function */ int main() { /* Set Systme init */ SystemInit(); // *(volatile uint32_t *)(0x41001014) = 0x0060100; //clock setting 48MHz /* CLK OUT Set */ // PAD_AFConfig(PAD_PA,GPIO_Pin_2, PAD_AF2); // PAD Config - CLKOUT used 3nd Function /* < SSP_StructInit default values SSP_InitStructure.SSP_SerialClockRate = 0x00; SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_MO; SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge; SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; SSP_InitStructure.SSP_DataSize = SSP_DataSize_8b; SSP_InitStructure.SSP_SOD = SSP_SOD_RESET; SSP_InitStructure.SSP_Mode = SSP_Mode_Master; SSP_InitStructure.SSP_NSS = SSP_NSS_Hard; SSP_InitStructure.SSP_LBM = SSP_LBM_RESET; SSP_InitStructure.SSP_SSE = SSP_SSE_SET; SSP_InitStructure.SSP_BaudRatePrescaler = SSP_BaudRatePrescaler_2; */ /* SSP0 Init -- SSP Master */ SSP_StructInit(&SSP0_InitStructure); SSP0_InitStructure.SSP_FrameFormat = SSP_FrameFormat_MO; // Motorora SPI mode SSP0_InitStructure.SSP_DataSize = SSP_DataSize_16b; SSP_Init(SSP0,&SSP0_InitStructure); /* SSP1 Init -- SSP Slave */ SSP_StructInit(&SSP1_InitStructure); SSP1_InitStructure.SSP_DataSize = SSP_DataSize_16b; SSP1_InitStructure.SSP_Mode = SSP_Mode_Slave; // SSP1 = Slave SSP_Init(SSP1,&SSP1_InitStructure); /* GPIO LED(R) Setting */ GPIO_InitDef.GPIO_Pin = GPIO_Pin_8; // Connecting GPIO_Pin_8(LED(R)) GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to GPIO Mode to Output Port GPIO_Init(GPIOC, &GPIO_InitDef); // Set to GPIOC PAD_AFConfig(PAD_PC,GPIO_Pin_8, PAD_AF1); // PAD Config - LED used 2nd Function /* GPIO LED(G) Setting */ GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Connecting GPIO_Pin_9(LED(G)) GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to GPIO Mode to Output Port GPIO_Init(GPIOC, &GPIO_InitDef); // Set to GPIOC PAD_AFConfig(PAD_PC,GPIO_Pin_9, PAD_AF1); // PAD Config - LED used 2nd Function GPIO_SetBits(GPIOC, GPIO_Pin_8); // LED red off GPIO_SetBits(GPIOC, GPIO_Pin_9); // LED green off /* Send only data to SSP1 */ for (TxIdx=0; TxIdx<BufferSize; TxIdx++) { SSP_SendData(SSP0, SSP0_Buffer_Tx[TxIdx]); while( SSP_GetFlagStatus(SSP0, SSP_FLAG_BSY) ); } /* Receive only data from SSP0 */ while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RNE)) { SSP1_Buffer_Rx[RxIdx] = (uint16_t)SSP_ReceiveData(SSP1); RxIdx++; } /* Check the received data with the send ones */ TransferStatus = Buffercmp(SSP0_Buffer_Tx, SSP1_Buffer_Rx, BufferSize); /* TransferStatus = PASSED, if the data transmitted and received are correct */ /* TransferStatus = FAILED, if the data transmitted and received are different */ if(TransferStatus == PASSED) { GPIO_ResetBits(GPIOC, GPIO_Pin_9); //Received are correct == LED green On } else if(TransferStatus == FAILED) { GPIO_ResetBits(GPIOC, GPIO_Pin_8); //Received are different == LED red On } }
uint16_t ReadSPDR (void) { return SSP_ReceiveData (SSP); }
/*********************************************************************//** * @brief SSP0 Interrupt used for reading and writing handler * @param None * @return None ***********************************************************************/ void SSP0_IRQHandler(void) { SSP_DATA_SETUP_Type *xf_setup; uint16_t tmp; uint8_t dataword; // Disable all SSP interrupts SSP_IntConfig(LPC_SSP0, SSP_INTCFG_ROR|SSP_INTCFG_RT|SSP_INTCFG_RX|SSP_INTCFG_TX, DISABLE); if(SSP_GetDataSize(LPC_SSP0)>8) dataword = 1; else dataword = 0; xf_setup = &xferConfig; // save status tmp = SSP_GetRawIntStatusReg(LPC_SSP0); xf_setup->status = tmp; // Check overrun error if (tmp & SSP_RIS_ROR){ // Clear interrupt SSP_ClearIntPending(LPC_SSP0, SSP_INTCLR_ROR); // update status xf_setup->status |= SSP_STAT_ERROR; // Set Complete Flag complete = SET; return; } if ((xf_setup->tx_cnt != xf_setup->length) || (xf_setup->rx_cnt != xf_setup->length)){ /* check if RX FIFO contains data */ while ((SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY)) && (xf_setup->rx_cnt != xf_setup->length)){ // Read data from SSP data tmp = SSP_ReceiveData(LPC_SSP0); // Store data to destination if (xf_setup->rx_data != NULL) { if (dataword == 0){ *(uint8_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint8_t) tmp; } else { *(uint16_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint16_t) tmp; } } // Increase counter if (dataword == 0){ xf_setup->rx_cnt++; } else { xf_setup->rx_cnt += 2; } } while ((SSP_GetStatus(LPC_SSP0, SSP_STAT_TXFIFO_NOTFULL)) && (xf_setup->tx_cnt != xf_setup->length)){ // Write data to buffer if(xf_setup->tx_data == NULL){ if (dataword == 0){ SSP_SendData(LPC_SSP0, 0xFF); xf_setup->tx_cnt++; } else { SSP_SendData(LPC_SSP0, 0xFFFF); xf_setup->tx_cnt += 2; } } else { if (dataword == 0){ SSP_SendData(LPC_SSP0, (*(uint8_t *)((uint32_t)xf_setup->tx_data + xf_setup->tx_cnt))); xf_setup->tx_cnt++; } else { SSP_SendData(LPC_SSP0, (*(uint16_t *)((uint32_t)xf_setup->tx_data + xf_setup->tx_cnt))); xf_setup->tx_cnt += 2; } } // Check overrun error if (SSP_GetRawIntStatus(LPC_SSP0, SSP_INTSTAT_RAW_ROR)){ // update status xf_setup->status |= SSP_STAT_ERROR; // Set Complete Flag complete = SET; return; } // Check for any data available in RX FIFO while ((SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY)) && (xf_setup->rx_cnt != xf_setup->length)){ // Read data from SSP data tmp = SSP_ReceiveData(LPC_SSP0); // Store data to destination if (xf_setup->rx_data != NULL) { if (dataword == 0){ *(uint8_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint8_t) tmp; } else { *(uint16_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint16_t) tmp; } } // Increase counter if (dataword == 0){ xf_setup->rx_cnt++; } else { xf_setup->rx_cnt += 2; } } } } // If there more data to sent or receive if ((xf_setup->rx_cnt != xf_setup->length) || (xf_setup->tx_cnt != xf_setup->length)){ // Enable all interrupt SSP_IntConfig(LPC_SSP0, SSP_INTCFG_ROR|SSP_INTCFG_RT|SSP_INTCFG_RX|SSP_INTCFG_TX, ENABLE); } else { // Save status xf_setup->status = SSP_STAT_DONE; // Set Complete Flag complete = SET; } }
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main() { #ifdef DEBUG debug(); #endif SCU_MCLKSourceConfig(SCU_MCLK_OSC); /*Use OSC as the default clock source*/ SCU_PCLKDivisorConfig(SCU_PCLK_Div1); /* ARM Peripheral bus clokdivisor = 1*/ /* SCU configuration */ SCU_Configuration(); /* GPIO pins configuration */ GPIO_Configuration(); /* SSP0 configuration */ SSP_DeInit(SSP0); SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola; SSP_InitStructure.SSP_Mode = SSP_Mode_Master; SSP_InitStructure.SSP_CPOL = SSP_CPOL_High; SSP_InitStructure.SSP_CPHA = SSP_CPHA_2Edge; SSP_InitStructure.SSP_DataSize = SSP_DataSize_8b; SSP_InitStructure.SSP_ClockRate = 5; SSP_InitStructure.SSP_ClockPrescaler = 2; SSP_Init(SSP0, &SSP_InitStructure); /* SSP1 configuration */ SSP_DeInit(SSP1); SSP_InitStructure.SSP_Mode = SSP_Mode_Slave; SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable; SSP_Init(SSP1, &SSP_InitStructure); /* SSP0 enable */ SSP_Cmd(SSP0, ENABLE); /* SSP1 enable */ SSP_Cmd(SSP1, ENABLE); /* Master to slave transfer procedure */ while(Tx_Idx<32) { SSP_SendData(SSP0, SSP0_Buffer_Tx[Tx_Idx++]); while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty)==RESET); SSP1_Buffer_Rx[Rx_Idx++] = SSP_ReceiveData(SSP1); } /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(SSP0_Buffer_Tx, SSP1_Buffer_Rx, 32); /* TransferStatus = PASSED, if the data transmitted from SSP0 and received by SSP1 are the same */ /* TransferStatus = FAILED, if the data transmitted from SSP0 and received by SSP1 are different */ /* Clear SSP0 receive Fifo */ for(k=0; k<8; k++) SSP0_Buffer_Rx[k] = SSP_ReceiveData(SSP0); /* Reset counters */ Tx_Idx=Rx_Idx=0; /* Slave to master transfer procedure */ while(Tx_Idx<32) { SSP_SendData(SSP1, SSP1_Buffer_Tx[Tx_Idx]); /* send a dummy bit to generate the clock */ SSP_SendData(SSP0, SSP0_Buffer_Tx[Tx_Idx++]); while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty)==RESET); SSP0_Buffer_Rx[Rx_Idx++] = SSP_ReceiveData(SSP0); } /* Check the received data with the send ones */ TransferStatus2 = Buffercmp(SSP1_Buffer_Tx, SSP0_Buffer_Rx, 32); /* TransferStatus = PASSED, if the data transmitted from SSP1 and received by SSP0 are the same */ /* TransferStatus = FAILED, if the data transmitted from SSP1 and received by SSP0 are different */ while(1); }
/*********************************************************************//** * @brief SSP Slave Interrupt sub-routine used for reading * and writing handler * @param None * @return None ***********************************************************************/ void ssp_Slave_IntHandler(void) { uint16_t tmp; /* Clear all interrupt */ SSP_ClearIntPending(SSPDEV_S, SSP_INTCLR_ROR); SSP_ClearIntPending(SSPDEV_S, SSP_INTCLR_RT); /* check if RX FIFO contains data */ while (SSP_GetStatus(SSPDEV_S, SSP_STAT_RXFIFO_NOTEMPTY) == SET) { tmp = SSP_ReceiveData(SSPDEV_S); if ((pRdBuf_S!= NULL) && (RdIdx_S < DatLen_S)) { *(pRdBuf_S + RdIdx_S) = (uint8_t) tmp; } RdIdx_S++; } /* Check if TX FIFO is not full */ while ((SSP_GetStatus(SSPDEV_S, SSP_STAT_TXFIFO_NOTFULL) == SET) && (WrIdx_S < DatLen_S)) { /* check if RX FIFO contains data */ while (SSP_GetStatus(SSPDEV_S, SSP_STAT_RXFIFO_NOTEMPTY) == SET) { tmp = SSP_ReceiveData(SSPDEV_S); if ((pRdBuf_S!= NULL) && (RdIdx_S < DatLen_S)) { *(pRdBuf_S + RdIdx_S) = (uint8_t) tmp; } RdIdx_S++; } if (pWrBuf_S != NULL) { SSP_SendData(SSPDEV_S, (uint16_t)(*(pWrBuf_S + WrIdx_S))); } else { SSP_SendData(SSPDEV_S, IDLE_CHAR); } WrIdx_S++; } /* There're more data to send */ if (WrIdx_S < DatLen_S) { SSP_IntConfig(SSPDEV_S, SSP_INTCFG_TX, ENABLE); } /* Otherwise */ else { SSP_IntConfig(SSPDEV_S, SSP_INTCFG_TX, DISABLE); } /* There're more data to receive */ if (RdIdx_S < DatLen_S) { SSP_IntConfig(SSPDEV_S, SSP_INTCFG_ROR, ENABLE); SSP_IntConfig(SSPDEV_S, SSP_INTCFG_RT, ENABLE); SSP_IntConfig(SSPDEV_S, SSP_INTCFG_RX, ENABLE); } /* Otherwise */ else { SSP_IntConfig(SSPDEV_S, SSP_INTCFG_ROR, DISABLE); SSP_IntConfig(SSPDEV_S, SSP_INTCFG_RT, DISABLE); SSP_IntConfig(SSPDEV_S, SSP_INTCFG_RX, DISABLE); } /* Set Flag if both Read and Write completed */ if ((WrIdx_S == DatLen_S) && (RdIdx_S == DatLen_S)) { complete_S = TRUE; } }
/*********************************************************************//** * @brief c_entry: Main MICROWIRE program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint32_t cnt; PINSEL_CFG_Type 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 pin connect * P0.6 - SSEL1 * P0.7 - SCK1 * P0.8 - MISO1 * P0.9 - MOSI1 */ PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 6; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 7; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 8; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 9; PINSEL_ConfigPin(&PinCfg); /* * Initialize SSP pin connect * P0.15 - SCK * P0.16 - SSEL * 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; PINSEL_ConfigPin(&PinCfg); /* Initializing Master SSP device section ------------------------------------------- */ // initialize SSP configuration structure to default SSP_ConfigStructInit(&SSP_ConfigStruct); // Re-configure SSP to MicroWire frame format SSP_ConfigStruct.FrameFormat = SSP_FRAME_MICROWIRE; // Initialize SSP peripheral with parameter given in structure above SSP_Init(SSPDEV_M, &SSP_ConfigStruct); // Enable SSP peripheral SSP_Cmd(SSPDEV_M, ENABLE); /* Initializing Slave SSP device section ------------------------------------------- */ // initialize SSP configuration structure to default SSP_ConfigStructInit(&SSP_ConfigStruct); /* Re-configure mode for SSP device */ SSP_ConfigStruct.Mode = SSP_SLAVE_MODE; // Re-configure SSP to MicroWire frame format SSP_ConfigStruct.FrameFormat = SSP_FRAME_MICROWIRE; // Initialize SSP peripheral with parameter given in structure above SSP_Init(SSPDEV_S, &SSP_ConfigStruct); // Enable SSP peripheral SSP_Cmd(SSPDEV_S, ENABLE); /* Initializing Buffer section ------------------------------------------------- */ Buffer_Init(); /* Start Transmit/Receive between Master and Slave ----------------------------- */ pRdBuf_M = (uint8_t *)&Master_Rx_Buf[0]; RdIdx_M = 0; DatLen_M = BUFFER_SIZE; pWrBuf_S = (uint8_t *)&Slave_Tx_Buf[0]; WrIdx_S = 0; DatLen_S = BUFFER_SIZE; /* Force Last command to Read command as default */ Last_cmd = MicroWire_RD_CMD; /* Clear all remaining data in RX FIFO */ while (SSP_GetStatus(SSPDEV_M, SSP_STAT_RXFIFO_NOTEMPTY)) { SSP_ReceiveData(SSPDEV_M); } while (SSP_GetStatus(SSPDEV_S, SSP_STAT_RXFIFO_NOTEMPTY)) { SSP_ReceiveData(SSPDEV_S); } for (cnt = 0; cnt < BUFFER_SIZE; cnt++) { /* The slave must initialize data in FIFO for immediately transfer from master * due to last received command */ if (Last_cmd == MicroWire_RD_CMD) { // Then send the respond to master, this contains data ssp_MW_SendRSP(SSPDEV_S, (uint16_t) *(pWrBuf_S + WrIdx_S++)); } else { // Then send the respond to master, this contains data ssp_MW_SendRSP(SSPDEV_S, 0xFF); } /* Master must send a read command to slave, * the slave then respond with its data in FIFO */ ssp_MW_SendCMD(SSPDEV_M, MicroWire_RD_CMD); // Master receive respond *(pRdBuf_M + RdIdx_M++) = (uint8_t) ssp_MW_GetRSP(SSPDEV_M); // Re-assign Last command Last_cmd = ssp_MW_GetCMD(SSPDEV_S); } /* Verify buffer */ Buffer_Verify(); _DBG_("Verify success!\n\r"); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief Get respond from slave after sending a command in master mode * @param[in] None * @return None **********************************************************************/ uint16_t ssp_MW_GetRSP(LPC_SSP_TypeDef *SSPx) { while (SSP_GetStatus(SSPx, SSP_STAT_RXFIFO_NOTEMPTY) == RESET); return (SSP_ReceiveData(SSPx)); }