Transfer spi into c++
This commit is contained in:
@@ -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
|
Reference in New Issue
Block a user