From 79b6705bfc60ed17ddbf6c36ead99e9f5c3c7404 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 26 Feb 2014 21:06:30 +0100 Subject: Old SPI code is now disabled, new SPI code succesfully reads out WM8523 chip id. Needs lots of cleaning... --- firmware/src/p2m.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 138 insertions(+), 14 deletions(-) (limited to 'firmware/src') diff --git a/firmware/src/p2m.c b/firmware/src/p2m.c index 11fe393..d44a01c 100644 --- a/firmware/src/p2m.c +++ b/firmware/src/p2m.c @@ -6,9 +6,11 @@ #include -//#define SPI -#define IRQ_BLINKY +//#define IRQ_BLINKY //#define BLINKY +//#define BLINKY +//#define WM8523 +#define SPI #ifdef DMA @@ -71,7 +73,7 @@ int main (void) } -#endif +#endif/*IRQ_BLINKY*/ #ifdef BLINKY @@ -90,7 +92,7 @@ int main (void) } -#endif +#endif/*BLINKY*/ #ifdef UART @@ -130,9 +132,9 @@ int main (void) } } -#endif +#endif/*UART*/ -#ifdef SPI +#ifdef WM8523 #include #include @@ -145,24 +147,146 @@ int main (void) LED_Init(); - cli_init(); - - cli_write("init"); - _delay(1 << 22); WM8523_init(); - cli_write("pre"); - _delay(1 << 22); WM8523_configure(); - cli_write("post"); + // Indicate that we didn't crash before the end... + int i = 0; + while(1) { + LED_toggle(); + _delay(1 << 21); + i = 1 - i; + } } -#endif +#endif/*WM8523*/ + +#ifdef SPI +// 1. Introduction +/* + SPI is one of the most used serial interfaces on PCB level. This tutorial + explains how to use SPI to read or write data. For SPI the SPI library is + required. It can be downloaded from our repository. The library provides + commands for communicating over SPI. +*/ + +// 2. Includes +#include +//#include "lpc_types.h" +#include +#include +#include +#include + +typedef unsigned char uint8_t; + +int main (void) +{ + LED_Init(); // Bent code + + // 3. Initializing + /* + SPI uses 4 IO pins, 1 clock, 1 MOSI, data from master to slave, 1 MISO, + data from slave to master and one Chip select pin. Every SPI IC has a Chip + Select pin, when the pin is low that IC will be selected to communicate + with. Every SPI IC that has a high value on the Chip Select pin will ignore + all signals on their data pins. 1 normal IO pin is used for the Chip Select, + the clock; MOSI and MISO are SPI pins. Any IO pin can be used for Chip + Select, For the tutorial pin 0.0 is used. + The timer has to be initialized to use it for delays and the GPIO pin for + Chip Select has to be initialized as output and made high, the code for + that is: + */ + // TimerInit(0, 1000); + + GPIOSetDir(0, 16, 1); + GPIOSetValue(0, 16, 1); + + /* + There are 2 SPI channels, SSP0 and SSP1. SSP0 uses pin 0.15 for the clock; + pin 0.17 for MISO and pin 0.18 for MOSI. SSP1 uses pin 0.7 for the clock; + pin 0.8 for MISO and pin 0.9 for MOSI. To initialize a SPI port the + command SSP*channelno*init(); is used. To initialize SPI channel 1 the + command is: + */ + SSP0Init(); + + /* + Some variables are also needed. SPI needs 2 array buffers, one for data + that has to be send trough the SPI slave and one for data received from + the SPI slave. Also its recommended to declare a variable with the value + of the SPI channel that is used: + */ + uint8_t src_addr[16]; //16 byte Write buffer + uint8_t dest_addr[16]; //16 byte Read buffer + uint8_t portnum = 0; + + // 4. Write data to SPI + /* + First the Chip Select has to be made low with the GPIOSetValue command: + GPIOSetValue(0, 0, 0;) + The data that has to be written should be placed in the src_addr array that + was declared earlier. To write 5 bytes of data (0x48, 0x65, 0x6c, 0x6c, + 0x6f the ascii code for Hello) place the hex data in the buffer array: + */ + + GPIOSetValue(0, 16, 0); + + src_addr[0] = 0x1 << 7; + src_addr[1] = 0xff; + src_addr[2] = 0xff; + + /* + When the data is stored in the array all that is left is start the SPI data + transmission with the SSPSend( portnum, (uint8_t *)src_addr, datanumbers); + portnum is the used SPI port, (uint8_t *)src_addr is the send buffer, + datanumbers is the amount of databytes to be send. The command needed to + send the 5 databytes declared above is: + */ + SSPSend(portnum, (uint8_t *)src_addr, 1); + + /* + After the data is send the Chip Select pin needs to be made high again with + the command GPIOSetValue(0, 0, 1); . Also a small delay can be needed for + some SPI IC’s, 1 millisecond is enough most of the time, the command for a + small delay is: delayMs(0, 1); + */ + // delayMs(0, 1); + + // GPIOSetValue(0, 16, 1); + + // 5. Read data from SPI + /* + To read data the Chip Select has to be made low with GPIOSetValue(0,0,0); . + To read data from SPI the command SSPReceive(portnum, (uint8_t *)dest_addr, + datanumbers); is used. Portnum is the used SPI port, (uint8_t *)dest_addr + is the receive buffer, datanumbers is the amount of databytes that have to + be read. A command to read 4 databytes is: + */ + // GPIOSetValue(0, 16, 0); + + SSPReceive(portnum, (uint8_t *)dest_addr, 3); + + GPIOSetValue(0, 16, 1); + /* + The 4 databytes are now stored in dest_addr[0] to dest_addr[3] + */ + + // Indicate that we didn't crash before the end... + int i = 0; + while(1) { + LED_toggle(); + _delay(1 << 21); + i = 1 - i; + } +} + +#endif/*SPI*/ void _delay(uint32_t del) { -- cgit v1.2.3