Transfer spi into c++
This commit is contained in:
parent
7f1721a536
commit
53aa3ceda6
File diff suppressed because it is too large
Load Diff
@ -1,8 +0,0 @@
|
||||
#ifndef __BOARD_H__
|
||||
#define __BOARD_H__
|
||||
|
||||
#if defined(BOARD_PCA10040)
|
||||
#include "platform/nrf52/nrf52-dk.h"
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,34 +0,0 @@
|
||||
#ifndef __DRIVER_H__
|
||||
#define __DRIVER_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
struct driver;
|
||||
|
||||
typedef int (*fp_open_t)(const struct driver *);
|
||||
typedef int (*fp_close_t)(const struct driver *);
|
||||
typedef int (*fp_read_t)(const struct driver *, char *, unsigned int);
|
||||
typedef int (*fp_write_t)(const struct driver *, const char *, unsigned int);
|
||||
typedef int (*fp_ioctl_t)(const struct driver *, unsigned int, unsigned int argc, va_list);
|
||||
|
||||
struct driver_fp {
|
||||
fp_open_t open;
|
||||
fp_close_t close;
|
||||
fp_read_t read;
|
||||
fp_write_t write;
|
||||
fp_ioctl_t ioctl;
|
||||
};
|
||||
|
||||
struct driver {
|
||||
const char *name;
|
||||
const struct driver_fp *fp;
|
||||
const void *dev;
|
||||
};
|
||||
|
||||
int drv_open(const struct driver *drv);
|
||||
int drv_close(const struct driver *drv);
|
||||
int drv_read(const struct driver *drv, char *buffer, unsigned int length);
|
||||
int drv_write(const struct driver *drv, const char *buffer, unsigned int length);
|
||||
int drv_ioctl(const struct driver *drv, unsigned int cmd, unsigned int argc, ...);
|
||||
|
||||
#endif
|
@ -1,12 +0,0 @@
|
||||
#ifndef __INCLUDE_FRAMEBUFFER_H__
|
||||
#define __INCLUDE_FRAMEBUFFER_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color);
|
||||
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
|
||||
|
||||
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length);
|
||||
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length);
|
||||
|
||||
#endif
|
@ -1,34 +0,0 @@
|
||||
#ifndef __GPIO_H__
|
||||
#define __GPIO_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "driver.h"
|
||||
|
||||
#define IOCTL_CMD_SET_DIRECTION 0
|
||||
|
||||
int gpio_open(const struct driver *drv);
|
||||
int gpio_close(const struct driver *drv);
|
||||
|
||||
int gpio_read(const struct driver *drv, char *buffer, unsigned int len);
|
||||
int gpio_write(const struct driver *drv, const char *buffer, unsigned int len);
|
||||
|
||||
enum direction {
|
||||
IN = 0,
|
||||
OUT
|
||||
};
|
||||
|
||||
struct gpio {
|
||||
int pin;
|
||||
enum direction dir;
|
||||
};
|
||||
|
||||
static const struct driver_fp gpio_fp = {
|
||||
.open = gpio_open,
|
||||
.close = gpio_close,
|
||||
.read = gpio_read,
|
||||
.write = gpio_write,
|
||||
.ioctl = NULL
|
||||
};
|
||||
|
||||
#endif
|
@ -1,117 +0,0 @@
|
||||
#ifndef __NRF52_DK_H__
|
||||
#define __NRF52_DK_H__
|
||||
|
||||
#include "driver.h"
|
||||
|
||||
#include "gpio.h"
|
||||
#include "spi.h"
|
||||
#include "st7789.h"
|
||||
|
||||
// LED 1
|
||||
const struct gpio nrf_led_1 = {
|
||||
.pin = 17,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver led_1 = {
|
||||
.name = "LED1",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_led_1
|
||||
};
|
||||
|
||||
// LED 2
|
||||
const struct gpio nrf_led_2 = {
|
||||
.pin = 18,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver led_2 = {
|
||||
.name = "LED2",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_led_2
|
||||
};
|
||||
|
||||
// LED 3
|
||||
const struct gpio nrf_led_3 = {
|
||||
.pin = 19,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver led_3 = {
|
||||
.name = "LED3",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_led_3
|
||||
};
|
||||
|
||||
// LED 4
|
||||
const struct gpio nrf_led_4 = {
|
||||
.pin = 20,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver led_4 = {
|
||||
.name = "LED4",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_led_4
|
||||
};
|
||||
|
||||
// LCD
|
||||
// SPI 0
|
||||
const struct spi nrf_spi_0 = {
|
||||
.sck_pin = 2,
|
||||
.mosi_pin = 3,
|
||||
.miso_pin = 4
|
||||
};
|
||||
const struct driver spi_0 = {
|
||||
.name = "SPI0",
|
||||
.fp = &spi_fp,
|
||||
.dev = &nrf_spi_0
|
||||
};
|
||||
const struct gpio nrf_dc_pin = {
|
||||
.pin = 18,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver dc_pin = {
|
||||
.name = "DC",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_dc_pin
|
||||
};
|
||||
const struct gpio nrf_bl_pin = {
|
||||
.pin = 23,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver bl_pin = {
|
||||
.name = "BACKLIGHT",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_bl_pin
|
||||
};
|
||||
const struct gpio nrf_rst_pin = {
|
||||
.pin = 26,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver rst_pin = {
|
||||
.name = "RESET",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_rst_pin
|
||||
};
|
||||
const struct gpio nrf_select_pin = {
|
||||
.pin = 25,
|
||||
.dir = OUT
|
||||
};
|
||||
const struct driver select_pin = {
|
||||
.name = "SELECT",
|
||||
.fp = &gpio_fp,
|
||||
.dev = &nrf_select_pin
|
||||
};
|
||||
struct st7789 nrf_lcd = {
|
||||
.spi = &spi_0,
|
||||
.dc = &dc_pin,
|
||||
.bl = &bl_pin,
|
||||
.rst = &rst_pin,
|
||||
.select = &select_pin,
|
||||
.height = 240,
|
||||
.width = 240,
|
||||
};
|
||||
const struct driver lcd = {
|
||||
.name = "LCD",
|
||||
.fp = &st7789_fp,
|
||||
.dev = &nrf_lcd
|
||||
};
|
||||
|
||||
#endif
|
@ -1,23 +0,0 @@
|
||||
#ifndef __NRF52_SPI_H__
|
||||
#define __NRF52_SPI_H__
|
||||
|
||||
int spi_open(const struct driver *drv);
|
||||
int spi_close(const struct driver *drv);
|
||||
|
||||
int spi_write(const struct driver *drv, const char *buffer, unsigned int len);
|
||||
|
||||
struct spi {
|
||||
unsigned int sck_pin;
|
||||
unsigned int mosi_pin;
|
||||
unsigned int miso_pin;
|
||||
};
|
||||
|
||||
static const struct driver_fp spi_fp = {
|
||||
.open = spi_open,
|
||||
.close = spi_close,
|
||||
.read = NULL,
|
||||
.write = spi_write,
|
||||
.ioctl = NULL
|
||||
};
|
||||
|
||||
#endif
|
16
interfaces/spi_interface.h
Normal file
16
interfaces/spi_interface.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __INTERFACES_SPI_INTERFACE_H__
|
||||
#define __INTERFACES_SPI_INTERFACE_H__
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace interfaces {
|
||||
|
||||
class SpiInterface
|
||||
{
|
||||
public:
|
||||
virtual void send(const uint8_t * buffer, uint32_t len) = 0;
|
||||
virtual void recv(uint8_t * buffer, uint32_t len) = 0;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
@ -1,5 +1,5 @@
|
||||
#include "delay.h"
|
||||
#include "platform/gpio.h"
|
||||
#include "platform/hal.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
|
@ -1,36 +0,0 @@
|
||||
#include "app_util_platform.h"
|
||||
#include "nrf_gpio.h"
|
||||
#include "nrf_delay.h"
|
||||
#include "boards.h"
|
||||
#include "app_error.h"
|
||||
#include <string.h>
|
||||
#include "nrf_log.h"
|
||||
#include "nrf_log_ctrl.h"
|
||||
#include "nrf_log_default_backends.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "driver.h"
|
||||
|
||||
const char buf[] = "Test";
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned int cnt = 0;
|
||||
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
|
||||
NRF_LOG_DEFAULT_BACKENDS_INIT();
|
||||
|
||||
drv_open(&led_1);
|
||||
drv_open(&spi_0);
|
||||
|
||||
NRF_LOG_INFO("SPI example started.");
|
||||
|
||||
while(1) {
|
||||
char c = (cnt++ % 2) + 0x30;
|
||||
drv_write(&led_1, &c, 1);
|
||||
drv_write(&spi_0, buf, sizeof(buf));
|
||||
|
||||
NRF_LOG_FLUSH();
|
||||
|
||||
nrf_delay_ms(200);
|
||||
}
|
||||
}
|
19
src/application/spi/main.cc
Normal file
19
src/application/spi/main.cc
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
#include "delay.h"
|
||||
#include "platform/hal.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
const uint8_t buf[] = "Test";
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Gpio led_1(17);
|
||||
Spi spi_0(0, 2, 3, 4, 25);
|
||||
|
||||
while(1) {
|
||||
delay_ms(200);
|
||||
spi_0.send(buf, sizeof(buf));
|
||||
led_1.toggle();
|
||||
}
|
||||
}
|
67
src/driver.c
67
src/driver.c
@ -1,67 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "driver.h"
|
||||
|
||||
int drv_open(const struct driver *drv)
|
||||
{
|
||||
int res = -1;
|
||||
assert(drv != NULL);
|
||||
|
||||
if(drv->fp->open) {
|
||||
res = drv->fp->open(drv);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int drv_close(const struct driver *drv)
|
||||
{
|
||||
int res = -1;
|
||||
assert(drv != NULL);
|
||||
|
||||
if(drv->fp->close) {
|
||||
res = drv->fp->close(drv);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int drv_read(const struct driver *drv, char *buffer, unsigned int length)
|
||||
{
|
||||
int res = -1;
|
||||
assert(drv != NULL);
|
||||
|
||||
if(drv->fp->read) {
|
||||
res = drv->fp->read(drv, buffer, length);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int drv_write(const struct driver *drv, const char *buffer, unsigned int length)
|
||||
{
|
||||
int res = -1;
|
||||
assert(drv != NULL);
|
||||
|
||||
if(drv->fp->write) {
|
||||
res = drv->fp->write(drv, buffer, length);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int drv_ioctl(const struct driver *drv, unsigned int cmd, unsigned int argc, ...)
|
||||
{
|
||||
int res = -1;
|
||||
assert(drv != NULL);
|
||||
|
||||
if(drv->fp->ioctl) {
|
||||
va_list args;
|
||||
va_start(args, argc);
|
||||
res = drv->fp->ioctl(drv, cmd, argc, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "framebuffer.h"
|
||||
|
||||
void fb_draw_pixel(uint16_t *image, uint16_t x, uint16_t y, uint16_t color)
|
||||
{
|
||||
assert(image != NULL);
|
||||
|
||||
image[x + 240 * y] = color;
|
||||
}
|
||||
|
||||
#ifndef _swap_int16_t
|
||||
#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
|
||||
#endif
|
||||
|
||||
void fb_draw_line(uint16_t *image, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
|
||||
{
|
||||
assert(image != NULL);
|
||||
|
||||
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
|
||||
if(steep) {
|
||||
_swap_int16_t(x0, y0);
|
||||
_swap_int16_t(x1, y1);
|
||||
}
|
||||
if(x0 > x1) {
|
||||
_swap_int16_t(x0, x1);
|
||||
_swap_int16_t(y0, y1);
|
||||
}
|
||||
|
||||
int16_t dx, dy;
|
||||
dx = x1 - x0;
|
||||
dy = abs(y1 - y0);
|
||||
|
||||
int16_t err = dx / 2;
|
||||
int16_t ystep;
|
||||
|
||||
if(y0 < y1) {
|
||||
ystep = 1;
|
||||
} else {
|
||||
ystep = -1;
|
||||
}
|
||||
|
||||
for(; x0 <= x1; x0++) {
|
||||
if(steep) {
|
||||
fb_draw_pixel(image, y0, x0, color);
|
||||
} else {
|
||||
fb_draw_pixel(image, x0, y0, color);
|
||||
}
|
||||
err -= dy;
|
||||
if (err < 0) {
|
||||
y0 += ystep;
|
||||
err += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fb_load_image(uint16_t *dst, uint16_t *src, uint32_t length)
|
||||
{
|
||||
assert(NULL != dst);
|
||||
assert(NULL != src);
|
||||
|
||||
for(uint32_t i = 0; i < length; i++) {
|
||||
dst[i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
void fb_set_image(uint16_t *dst, uint16_t value, uint32_t length)
|
||||
{
|
||||
assert(NULL != dst);
|
||||
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
dst[i] = value;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
|
||||
#if defined(PLATFORM_nrf52)
|
||||
#include "platform/nrf52/gpio.h"
|
||||
#include "platform/nrf52/spi.h"
|
||||
|
||||
namespace hal = platform::nrf52;
|
||||
#endif
|
@ -1,61 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "nrf.h"
|
||||
|
||||
#include "driver.h"
|
||||
#include "spi.h"
|
||||
|
||||
static inline int spi_transfer(uint32_t t);
|
||||
|
||||
int spi_open(const struct driver *drv)
|
||||
{
|
||||
assert(NULL != drv);
|
||||
|
||||
struct spi *this = (struct spi *)drv->dev;
|
||||
|
||||
NRF_SPI0->ENABLE = 0;
|
||||
NRF_SPI0->PSELSCK = this->sck_pin;
|
||||
NRF_SPI0->PSELMOSI = this->mosi_pin;
|
||||
NRF_SPI0->PSELMISO = this->miso_pin;
|
||||
NRF_SPI0->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8;
|
||||
|
||||
NRF_SPI0->CONFIG = (0x03 << 1); //Sample on trailing edge of clock, shift serial data on leading edge, SCK polarity Active low
|
||||
NRF_SPI0->EVENTS_READY = 0;
|
||||
NRF_SPI0->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spi_close(const struct driver *drv)
|
||||
{
|
||||
NRF_SPI0->ENABLE = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int spi_write(const struct driver *drv, const char *buffer, unsigned int len)
|
||||
{
|
||||
assert(NULL != buffer);
|
||||
|
||||
//FIXME: missing CS handling
|
||||
|
||||
for(unsigned int i = 0; i < len; i++) {
|
||||
spi_transfer(buffer[i]);
|
||||
}
|
||||
|
||||
//FIXME: missing CS handling
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int spi_transfer(uint32_t t)
|
||||
{
|
||||
volatile uint32_t r;
|
||||
NRF_SPI0->EVENTS_READY = 0; // ready
|
||||
NRF_SPI0->TXD = t; // out
|
||||
while(NRF_SPI0->EVENTS_READY == 0) {
|
||||
}
|
||||
r = NRF_SPI0->RXD; // in
|
||||
return (int)r;
|
||||
}
|
72
src/platform/nrf52/spi.cc
Normal file
72
src/platform/nrf52/spi.cc
Normal file
@ -0,0 +1,72 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "platform/nrf52/spi.h"
|
||||
|
||||
extern "C" {
|
||||
#include "nrf.h"
|
||||
|
||||
NRF_SPI_Type *SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI0_BASE);
|
||||
// NRF_SPI_Type *const SPI_1_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI1_BASE);
|
||||
// NRF_SPI_Type *const SPI_2_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI2_BASE);
|
||||
}
|
||||
|
||||
using namespace platform::nrf52;
|
||||
|
||||
Spi::Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, uint32_t cs)
|
||||
{
|
||||
assert(instance < 3);
|
||||
|
||||
if(instance == 0) {
|
||||
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI0_BASE);
|
||||
} else if(instance == 1) {
|
||||
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI1_BASE);
|
||||
} else {
|
||||
SPI_REGS = reinterpret_cast<NRF_SPI_Type *>(NRF_SPI2_BASE);
|
||||
}
|
||||
|
||||
SPI_REGS->ENABLE = 0;
|
||||
SPI_REGS->PSELSCK = sck;
|
||||
SPI_REGS->PSELMOSI = mosi;
|
||||
SPI_REGS->PSELMISO = miso;
|
||||
SPI_REGS->FREQUENCY = SPI_FREQUENCY_FREQUENCY_M8;
|
||||
|
||||
SPI_REGS->CONFIG = (0x03 << 1); //Sample on trailing edge of clock, shift serial data on leading edge, SCK polarity Active low
|
||||
SPI_REGS->EVENTS_READY = 0;
|
||||
SPI_REGS->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
|
||||
}
|
||||
Spi::~Spi()
|
||||
{
|
||||
SPI_REGS->ENABLE = 0;
|
||||
}
|
||||
|
||||
void Spi::send(const uint8_t * buffer, uint32_t len)
|
||||
{
|
||||
//FIXME: missing CS handling
|
||||
|
||||
for(unsigned int i = 0; i < len; i++) {
|
||||
this->transfer(buffer[i]);
|
||||
}
|
||||
|
||||
//FIXME: missing CS handling
|
||||
}
|
||||
|
||||
void Spi::recv(uint8_t * buffer, uint32_t len)
|
||||
{
|
||||
//FIXME: missing CS handling
|
||||
|
||||
for(unsigned int i = 0; i < len; i++) {
|
||||
buffer[i] = this->transfer(buffer[i]);
|
||||
}
|
||||
|
||||
//FIXME: missing CS handling
|
||||
}
|
||||
|
||||
uint32_t Spi::transfer(uint32_t data)
|
||||
{
|
||||
volatile uint32_t ret;
|
||||
SPI_REGS->EVENTS_READY = 0;
|
||||
SPI_REGS->TXD = data;
|
||||
while(SPI_REGS->EVENTS_READY == 0) {;}
|
||||
ret = SPI_REGS->RXD;
|
||||
return ret;
|
||||
}
|
22
src/platform/nrf52/spi.h
Normal file
22
src/platform/nrf52/spi.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __PLATFORM_NRF52_SPI_H__
|
||||
#define __PLATFORM_NRF52_SPI_H__
|
||||
|
||||
#include "spi_interface.h"
|
||||
|
||||
namespace platform::nrf52 {
|
||||
|
||||
class Spi : public interfaces::SpiInterface
|
||||
{
|
||||
public:
|
||||
Spi(uint32_t instance, uint32_t sck, uint32_t mosi, uint32_t miso, uint32_t cs);
|
||||
~Spi();
|
||||
void send(const uint8_t * buffer, uint32_t len) override;
|
||||
void recv(uint8_t * buffer, uint32_t len) override;
|
||||
|
||||
private:
|
||||
uint32_t transfer(uint32_t);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user