Created
July 14, 2019 07:45
-
-
Save surinoel/0f0a381b7b041f0850820f21cc71ecca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* w25q64.c | |
* | |
* Created: 2019-07-14 오전 2:58:29 | |
* Author : yeong | |
*/ | |
#define F_CPU 16000000UL | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#include "spi.h" | |
#define W25Q_SS_PIN 1 | |
#define LOGIC_LOW 0 | |
#define LOGIC_HIGH 1 | |
int main(void) | |
{ | |
spi_init(); | |
uart0_init(); | |
// CS init | |
w25q_ss_pin_init(); | |
w25q_ss_pin_drive(LOGIC_HIGH); | |
while(1) | |
{ | |
uint8_t man_id = w25q_read_manufacturer_id(); | |
w25q_ss_pin_drive(LOGIC_LOW); | |
spi_transmit(man_id); | |
w25q_ss_pin_drive(LOGIC_HIGH); | |
_delay_ms(1); | |
printf("manID:0x%X\r\n", man_id); | |
uint8_t dev_id = w25q_read_device_id(); | |
w25q_ss_pin_drive(LOGIC_LOW); | |
spi_transmit(dev_id); | |
w25q_ss_pin_drive(LOGIC_HIGH); | |
printf("devID:0x%X\r\n", dev_id); | |
_delay_ms(5); | |
} | |
return 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* spi.c | |
* | |
* Created: 2019-07-14 오전 4:16:00 | |
* Author: yeong | |
*/ | |
#include "spi.h" | |
/**@brief: initializes the SPI peripheral | |
* sets the MOSI, SCLK and SS Pins as outputs | |
* enables the SPI in master mode, f/16 clk | |
*/ | |
void spi_init(void) | |
{ | |
DDRB = ((1<<SPI_MOSI_PIN)|(1<<SPI_SCLK_PIN)|(1<<SPI_SS_PIN)); //spi pins on port b MOSI SCK,SS outputs | |
SPCR = ((1<<SPE)|(1<<MSTR)); // SPI enable, Master, f/16, | |
// CPOL0 SCK is low when idle, CPHA0 sample leading edge | |
} | |
/** @brief: spi transmit function | |
* sends and receives a byte at the same time | |
*/ | |
char spi_transmit(char tx_byte) | |
{ | |
SPDR = tx_byte; | |
while(!(SPSR & (1<<SPIF))); | |
return SPDR; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* w25q64.c | |
* | |
* Created: 2019-07-14 오전 4:16:22 | |
* Author: yeong | |
*/ | |
#include "w25q64.h" | |
/** @brief: initialize the ss pin | |
*/ | |
void w25q_ss_pin_init(void) | |
{ | |
W25Q_CONTROL_DDR |= (1<<W25Q_SS_PIN); // Set as output | |
} | |
/** @brief: drives the ss pin | |
*/ | |
void w25q_ss_pin_drive(uint8_t high) | |
{ | |
if(high) | |
{ | |
W25Q_CONTROL_PORT |= (1<<W25Q_SS_PIN); | |
} | |
else | |
{ | |
W25Q_CONTROL_PORT &= ~(1<<W25Q_SS_PIN); | |
} | |
} | |
/** @brief: return the manufacturer ID | |
*/ | |
char w25q_read_manufacturer_id(void) | |
{ | |
w25q_ss_pin_drive(LOGIC_LOW); | |
spi_transmit(0x90); | |
spi_transmit(0x00); | |
spi_transmit(0x00); | |
spi_transmit(0x00); | |
uint8_t man_id = spi_transmit(0x00); | |
spi_transmit(0x00); | |
w25q_ss_pin_drive(LOGIC_HIGH); | |
return man_id; | |
} | |
/** @brief: return the device ID | |
*/ | |
char w25q_read_device_id(void) | |
{ | |
w25q_ss_pin_drive(LOGIC_LOW); | |
spi_transmit(0x90); | |
spi_transmit(0x00); | |
spi_transmit(0x00); | |
spi_transmit(0x00); | |
spi_transmit(0x00); | |
uint8_t dev_id = spi_transmit(0x00); | |
w25q_ss_pin_drive(LOGIC_HIGH); | |
return dev_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment