Loading Session3-LF/Task2_LF/Task2_LF/Task2_LF.cproj +142 −140 Original line number Diff line number Diff line Loading @@ -38,6 +38,8 @@ </dependencies> </framework-data> </AsfFrameworkConfig> <ResetRule>0</ResetRule> <EraseKey /> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <ToolchainSettings> Loading Session3-LF/Task2_LF/Task2_LF/display.c +21 −8 Original line number Diff line number Diff line Loading @@ -4,14 +4,6 @@ #define F_CPU 3333333UL #define DSP_MODE_bm 1<<5 #define DSP_MODE_PORT PORTC #define DSP_RST_bm 1<<5 #define DSP_RST_PORT PORTB // System headers #include <avr/io.h> #include <util/delay.h> Loading @@ -23,6 +15,27 @@ #include "font8x8.h" //From spi.h: #ifndef EXT3 // for using EXT1 header: #define DSP_MODE_bm (1 << 7) #define DSP_MODE_PORT PORTB #define DSP_RST_bm (1 << 4) #define DSP_RST_PORT PORTA #else //For using EXT3 header: #define DSP_MODE_bm (1 << 5) #define DSP_MODE_PORT PORTC #define DSP_RST_bm (1 << 5) #define DSP_RST_PORT PORTB #endif /* EXT3 */ // This file contains a simple driver for the display so that one can print characters and strings. // You shouldn't need to do anything in this file for things to work. // Of course, you're welcome to check it out if you want to see how things work under the hood. Loading Session3-LF/Task2_LF/Task2_LF/main.c +4 −16 Original line number Diff line number Diff line Loading @@ -11,20 +11,6 @@ #include "display.h" /* In this exercise you will write a driver for the SPI module. You will connect the OLED screen to the EXT3 header and use your SPI driver to write bitmaps and text to the screen. A finished driver for the screen is provided (display.c), as as a font for it. These build uppon the SPI driver. You can read display.h to see what functions you can use with the screen. No help beyond the function headers is provided. To start, it's recommend you: a) glance over the chapter on SPI in the ATtiny817 datasheet b) find out what pins of the ATtiny the screen is connected to */ const uint8_t bitmap[512] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, Loading Loading @@ -64,6 +50,8 @@ const uint8_t bitmap[512] PROGMEM = int main(void) { // The EXT header can be selected though a define in spi.h DISP_init(); DISP_write_bitmap(bitmap); //Printing Bitmap Loading Session3-LF/Task2_LF/Task2_LF/spi.c +32 −23 Original line number Diff line number Diff line Loading @@ -9,17 +9,30 @@ // Project headers #include "spi.h" // In this file, you will need to write the contents of the SPI communication routines. // You need to setup SPI communication in SPI_MasterInit() and // transmit data in SPI_MasterTransmit(...). // // HINT: Check out the defines in the file defines.h to see defines // such as data direction registers and bit positions, and as always: // RTFD #define MOSI_bm (1 << 2) //on port C #define SCK_bm (1 << 0) //on port C #define SPI_PORT PORTC // //Oled is connected to alternate SPI pins #define MOSI_bm 1<<2 //on port C #define SCK_bm 1<<0 //on port C #define SS_bm 1<<3 //on port A #define SPI_PORT PORTC //We can now use SPI_PORT.DIR, .OUT etc. #define SS_PORT PORTA //SS is on a different port #ifndef EXT3 // for using EXT1 header: #define SS_bm (1 << 3) #define SS_PORT PORTC //We could also have defined bit positions for MOSI, SCL and SS. #else // for using EXT3 header: #define SS_bm (1 << 3) #define SS_PORT PORTA #endif /* EXT3 */ void SPI_MasterInit() Loading @@ -27,42 +40,38 @@ void SPI_MasterInit() // Initialize the SPI port as master // You will need to set MOSI, SCK, SS (slave select) as outputs //Connect OLED to EXT3 PORTMUX.CTRLB |= (PORTMUX_SPI0_bm); //To use alternative SPI pins PORTMUX.CTRLB |= PORTMUX_SPI0_bm; //To use alternative SPI pins SPI_PORT.DIRSET = (MOSI_bm) | (SCK_bm); //Set pins as output SS_PORT.DIRSET = (SS_bm); SPI_PORT.DIR |= MOSI_bm | SCK_bm; //Set pins as output SS_PORT.DIR |= SS_bm; SS_PORT.OUTSET = SS_bm; //Set SS high -> OLED inactive SS_PORT.OUTSET = (SS_bm); //Set SS high -> OLED inactive // Now enable SPI, Master and set clock rate SPI0.CTRLA |= SPI_ENABLE_bm | SPI_MASTER_bm; //Default clock divisor of 4 is fine SPI0.CTRLA |= (SPI_ENABLE_bm) | (SPI_MASTER_bm); //Default clock divisor of 4 is fine //Make sure SS does not disable master mode (Possibly not required) SPI0.CTRLB |= SPI_SSD_bm; //Make sure SS does not disable master mode SPI0.CTRLB |= (SPI_SSD_bm); } void SPI_MasterTransmit(char cData) { // First select the correct slave by setting its slave select (SS) LOW SS_PORT.OUTCLR = SS_bm; // First select the correct slave by setting the slave select (SS) bit LOW SS_PORT.OUTCLR = (SS_bm); // Then start the transmission by assigning the data to the SPI data register SPI0.DATA = cData; // Now wait for the data transmission to complete by periodically checking the SPI status register //the SPI_IF and SPI_WRCOL is the only interrupt flag with a function in non-buffered mode. while(!(SPI0.INTFLAGS & SPI_IF_bm)){ } //the SPI_IF is the only interupt flag with a function in non-buffered mode. while(!(SPI0.INTFLAGS & (SPI_IF_bm))); SPI0.DATA; //Dummy read to clear flag // Finally set the slave select bit HIGH before leaving the function SS_PORT.OUTSET = SS_bm; SS_PORT.OUTSET = (SS_bm); } Session3-LF/Task2_LF/Task2_LF/spi.h +5 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,11 @@ #ifndef SPI_H_ #define SPI_H_ /*SPI and OLED will use EXT1 default, uncomment to use EXT3: This define is used by spi.c and display.c to select the correct pins */ #define EXT3 // Function for initializing SPI void SPI_MasterInit(void); Loading Loading
Session3-LF/Task2_LF/Task2_LF/Task2_LF.cproj +142 −140 Original line number Diff line number Diff line Loading @@ -38,6 +38,8 @@ </dependencies> </framework-data> </AsfFrameworkConfig> <ResetRule>0</ResetRule> <EraseKey /> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <ToolchainSettings> Loading
Session3-LF/Task2_LF/Task2_LF/display.c +21 −8 Original line number Diff line number Diff line Loading @@ -4,14 +4,6 @@ #define F_CPU 3333333UL #define DSP_MODE_bm 1<<5 #define DSP_MODE_PORT PORTC #define DSP_RST_bm 1<<5 #define DSP_RST_PORT PORTB // System headers #include <avr/io.h> #include <util/delay.h> Loading @@ -23,6 +15,27 @@ #include "font8x8.h" //From spi.h: #ifndef EXT3 // for using EXT1 header: #define DSP_MODE_bm (1 << 7) #define DSP_MODE_PORT PORTB #define DSP_RST_bm (1 << 4) #define DSP_RST_PORT PORTA #else //For using EXT3 header: #define DSP_MODE_bm (1 << 5) #define DSP_MODE_PORT PORTC #define DSP_RST_bm (1 << 5) #define DSP_RST_PORT PORTB #endif /* EXT3 */ // This file contains a simple driver for the display so that one can print characters and strings. // You shouldn't need to do anything in this file for things to work. // Of course, you're welcome to check it out if you want to see how things work under the hood. Loading
Session3-LF/Task2_LF/Task2_LF/main.c +4 −16 Original line number Diff line number Diff line Loading @@ -11,20 +11,6 @@ #include "display.h" /* In this exercise you will write a driver for the SPI module. You will connect the OLED screen to the EXT3 header and use your SPI driver to write bitmaps and text to the screen. A finished driver for the screen is provided (display.c), as as a font for it. These build uppon the SPI driver. You can read display.h to see what functions you can use with the screen. No help beyond the function headers is provided. To start, it's recommend you: a) glance over the chapter on SPI in the ATtiny817 datasheet b) find out what pins of the ATtiny the screen is connected to */ const uint8_t bitmap[512] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, Loading Loading @@ -64,6 +50,8 @@ const uint8_t bitmap[512] PROGMEM = int main(void) { // The EXT header can be selected though a define in spi.h DISP_init(); DISP_write_bitmap(bitmap); //Printing Bitmap Loading
Session3-LF/Task2_LF/Task2_LF/spi.c +32 −23 Original line number Diff line number Diff line Loading @@ -9,17 +9,30 @@ // Project headers #include "spi.h" // In this file, you will need to write the contents of the SPI communication routines. // You need to setup SPI communication in SPI_MasterInit() and // transmit data in SPI_MasterTransmit(...). // // HINT: Check out the defines in the file defines.h to see defines // such as data direction registers and bit positions, and as always: // RTFD #define MOSI_bm (1 << 2) //on port C #define SCK_bm (1 << 0) //on port C #define SPI_PORT PORTC // //Oled is connected to alternate SPI pins #define MOSI_bm 1<<2 //on port C #define SCK_bm 1<<0 //on port C #define SS_bm 1<<3 //on port A #define SPI_PORT PORTC //We can now use SPI_PORT.DIR, .OUT etc. #define SS_PORT PORTA //SS is on a different port #ifndef EXT3 // for using EXT1 header: #define SS_bm (1 << 3) #define SS_PORT PORTC //We could also have defined bit positions for MOSI, SCL and SS. #else // for using EXT3 header: #define SS_bm (1 << 3) #define SS_PORT PORTA #endif /* EXT3 */ void SPI_MasterInit() Loading @@ -27,42 +40,38 @@ void SPI_MasterInit() // Initialize the SPI port as master // You will need to set MOSI, SCK, SS (slave select) as outputs //Connect OLED to EXT3 PORTMUX.CTRLB |= (PORTMUX_SPI0_bm); //To use alternative SPI pins PORTMUX.CTRLB |= PORTMUX_SPI0_bm; //To use alternative SPI pins SPI_PORT.DIRSET = (MOSI_bm) | (SCK_bm); //Set pins as output SS_PORT.DIRSET = (SS_bm); SPI_PORT.DIR |= MOSI_bm | SCK_bm; //Set pins as output SS_PORT.DIR |= SS_bm; SS_PORT.OUTSET = SS_bm; //Set SS high -> OLED inactive SS_PORT.OUTSET = (SS_bm); //Set SS high -> OLED inactive // Now enable SPI, Master and set clock rate SPI0.CTRLA |= SPI_ENABLE_bm | SPI_MASTER_bm; //Default clock divisor of 4 is fine SPI0.CTRLA |= (SPI_ENABLE_bm) | (SPI_MASTER_bm); //Default clock divisor of 4 is fine //Make sure SS does not disable master mode (Possibly not required) SPI0.CTRLB |= SPI_SSD_bm; //Make sure SS does not disable master mode SPI0.CTRLB |= (SPI_SSD_bm); } void SPI_MasterTransmit(char cData) { // First select the correct slave by setting its slave select (SS) LOW SS_PORT.OUTCLR = SS_bm; // First select the correct slave by setting the slave select (SS) bit LOW SS_PORT.OUTCLR = (SS_bm); // Then start the transmission by assigning the data to the SPI data register SPI0.DATA = cData; // Now wait for the data transmission to complete by periodically checking the SPI status register //the SPI_IF and SPI_WRCOL is the only interrupt flag with a function in non-buffered mode. while(!(SPI0.INTFLAGS & SPI_IF_bm)){ } //the SPI_IF is the only interupt flag with a function in non-buffered mode. while(!(SPI0.INTFLAGS & (SPI_IF_bm))); SPI0.DATA; //Dummy read to clear flag // Finally set the slave select bit HIGH before leaving the function SS_PORT.OUTSET = SS_bm; SS_PORT.OUTSET = (SS_bm); }
Session3-LF/Task2_LF/Task2_LF/spi.h +5 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,11 @@ #ifndef SPI_H_ #define SPI_H_ /*SPI and OLED will use EXT1 default, uncomment to use EXT3: This define is used by spi.c and display.c to select the correct pins */ #define EXT3 // Function for initializing SPI void SPI_MasterInit(void); Loading