Rev 369 | Rev 379 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 369 | Rev 378 | ||
---|---|---|---|
Line 121... | Line 121... | ||
121 | 121 | ||
122 | void SSC_Init(void) |
122 | void SSC_Init(void) |
123 | { |
123 | { |
124 | GPIO_InitTypeDef GPIO_InitStructure; |
124 | GPIO_InitTypeDef GPIO_InitStructure; |
125 | SSP_InitTypeDef SSP_InitStructure; |
125 | SSP_InitTypeDef SSP_InitStructure; |
126 | WIU_InitTypeDef WIU_InitStructure; |
126 | // WIU_InitTypeDef WIU_InitStructure; |
127 | // enable APB clock for SPI1 |
127 | // enable APB clock for SPI1 |
128 | SCU_APBPeriphClockConfig(__SSP1 ,ENABLE); |
128 | SCU_APBPeriphClockConfig(__SSP1 ,ENABLE); |
129 | // configure P5.4 -> SD-CS as an output pin |
129 | // configure P5.4 -> SD-CS as an output pin |
130 | GPIO_StructInit(&GPIO_InitStructure); |
130 | GPIO_StructInit(&GPIO_InitStructure); |
Line 159... | Line 159... | ||
159 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
159 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
160 | // Set Baud Rate (Prescaler) |
160 | // Set Baud Rate (Prescaler) |
161 | // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate)) |
161 | // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate)) |
162 | // With MSCLK = 48MHz/2 = BRCLK we get for the SPICLK = 24Mhz / 8 / (1+5) = 500 kHz |
162 | // With MSCLK = 48MHz/2 = BRCLK we get for the SPICLK = 24Mhz / 8 / (1+5) = 500 kHz |
163 | SSP_InitStructure.SSP_ClockRate = 5; //5 |
163 | SSP_InitStructure.SSP_ClockRate = 5; //5 |
164 | SSP_InitStructure.SSP_ClockPrescaler = 8; |
164 | SSP_InitStructure.SSP_ClockPrescaler = 4; //8 |
165 | SSP_Init(SSP1, &SSP_InitStructure); |
165 | SSP_Init(SSP1, &SSP_InitStructure); |
166 | SSC_Disable(); |
166 | SSC_Disable(); |
167 | SSP_Cmd(SSP1, ENABLE); |
167 | SSP_Cmd(SSP1, ENABLE); |
Line 168... | Line 168... | ||
168 | 168 | ||
Line 247... | Line 247... | ||
247 | //________________________________________________________________________________________________________________________________________ |
247 | //________________________________________________________________________________________________________________________________________ |
Line 248... | Line 248... | ||
248 | 248 | ||
249 | u8 SSC_GetChar (void) |
249 | u8 SSC_GetChar (void) |
250 | { |
250 | { |
251 | u8 Byte = 0; |
251 | u8 Byte = 0; |
252 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET && (SD_WatchDog)); // wait for space in the tx fifo |
252 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET); // wait for space in the tx fifo |
253 | SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave |
253 | SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave |
254 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET && (SD_WatchDog)); // wait for the byte to be sent |
254 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent |
255 | Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave |
255 | Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave |
256 | return (Byte); |
256 | return (Byte); |
Line 257... | Line 257... | ||
257 | } |
257 | } |
258 | 258 | ||
259 | //________________________________________________________________________________________________________________________________________ |
259 | //________________________________________________________________________________________________________________________________________ |
260 | void SSC_ClearRxFifo (void) |
260 | void SSC_ClearRxFifo (void) |
261 | { |
261 | { |
262 | // wait that the tx fifo is empty |
262 | // wait that the tx fifo is empty |
263 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET && (SD_WatchDog)); |
263 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); |
264 | // then empty the rx fifo by reading all the bytes that are available |
264 | // then empty the rx fifo by reading all the bytes that are available |
265 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET && (SD_WatchDog)) SSP_ReceiveData(SSP1); |
265 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1); |
266 | } |
266 | } |
267 | //________________________________________________________________________________________________________________________________________ |
267 | //________________________________________________________________________________________________________________________________________ |
268 | // Function: SSC_PutChar(u8 Byte); |
268 | // Function: SSC_PutChar(u8 Byte); |
Line 274... | Line 274... | ||
274 | //________________________________________________________________________________________________________________________________________ |
274 | //________________________________________________________________________________________________________________________________________ |
Line 275... | Line 275... | ||
275 | 275 | ||
276 | void SSC_PutChar (u8 Byte) |
276 | void SSC_PutChar (u8 Byte) |
277 | { |
277 | { |
278 | // wait for some space in the tx fifo |
278 | // wait for some space in the tx fifo |
279 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET && (SD_WatchDog)); |
279 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET); |
280 | // put the byte to send in the tx fifo |
280 | // put the byte to send in the tx fifo |
281 | SSP_SendData(SSP1, Byte); |
281 | SSP_SendData(SSP1, Byte); |