Skip to content

Commit

Permalink
Merge pull request mchck#96 from bgamari/master
Browse files Browse the repository at this point in the history
Various fixes for RTC and SPI
  • Loading branch information
bgamari committed Jan 15, 2014
2 parents 522bdb8 + 443a296 commit 0e35320
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
16 changes: 10 additions & 6 deletions toolchain/lib/mchck/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ rtc_set_time(uint32_t seconds)
static void
rtc_alarm_update()
{
if (rtc_get_time() == RTC_INVALID_TIME)
return;

if (alarm_head) {
int_enable(IRQ_RTC_alarm);
RTC.tar = alarm_head->time;
int_enable(IRQ_RTC_alarm);
} else {
int_disable(IRQ_RTC_alarm);
}
Expand All @@ -61,6 +64,7 @@ rtc_alarm_add(struct rtc_alarm_ctx *ctx, uint32_t time,
ctx->next = tail;
*last_next = ctx;
} else {
ctx->next = NULL;
alarm_head = ctx;
}
rtc_alarm_update();
Expand All @@ -71,13 +75,13 @@ void
rtc_alarm_cancel(struct rtc_alarm_ctx *ctx)
{
crit_enter();
struct rtc_alarm_ctx *tail = alarm_head;
while (tail) {
if (tail->next == ctx) {
tail->next = ctx->next;
struct rtc_alarm_ctx **next = &alarm_head;
while (*next) {
if (*next == ctx) {
*next = ctx->next;
break;
}
tail = tail->next;
next = &(*next)->next;
}
crit_exit();
}
Expand Down
2 changes: 2 additions & 0 deletions toolchain/lib/mchck/rtc.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define RTC_INVALID_TIME 0xffffffff

/* Enable access to the RTC and starts the oscillator. */
void rtc_init();

Expand Down
20 changes: 13 additions & 7 deletions toolchain/lib/mchck/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,35 @@ spi_is_xfer_active(void)
return !SPI0.mcr.halt;
}

void
int
spi_queue_xfer(struct spi_ctx *ctx,
enum spi_pcs pcs,
const uint8_t *txbuf, uint16_t txlen,
uint8_t *rxbuf, uint16_t rxlen,
spi_cb *cb, void *cbdata)
{
spi_queue_xfer_sg(&ctx->ctx, pcs,
sg_init(&ctx->tx_sg, (void *)txbuf, txlen),
sg_init(&ctx->rx_sg, rxbuf, rxlen),
cb, cbdata);
return spi_queue_xfer_sg(&ctx->ctx, pcs,
sg_init(&ctx->tx_sg, (void *)txbuf, txlen),
sg_init(&ctx->rx_sg, rxbuf, rxlen),
cb, cbdata);
}

void
int
spi_queue_xfer_sg(struct spi_ctx_bare *ctx,
enum spi_pcs pcs,
struct sg *tx, struct sg *rx,
spi_cb *cb, void *cbdata)
{
if (ctx->queued)
return 1;

*ctx = (struct spi_ctx_bare){
.tx = sg_simplify(tx),
.rx = sg_simplify(rx),
.pcs = pcs,
.cb = cb,
.cbdata = cbdata
.cbdata = cbdata,
.queued = true
};

size_t tx_len = sg_total_lengh(ctx->tx);
Expand All @@ -88,6 +92,7 @@ spi_queue_xfer_sg(struct spi_ctx_bare *ctx,
}
}
crit_exit();
return 0;
}

void
Expand Down Expand Up @@ -142,6 +147,7 @@ SPI0_Handler(void)
struct spi_ctx_bare *ctx = spi_ctx;

crit_enter();
ctx->queued = false;
spi_ctx = ctx->next;
if (spi_ctx != NULL)
spi_start_xfer();
Expand Down
20 changes: 11 additions & 9 deletions toolchain/lib/mchck/spi.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mchck/sg.h>
#include <stdbool.h>

enum spi_pcs {
SPI_PCS0 = 1 << 0,
Expand All @@ -20,6 +21,7 @@ struct spi_ctx_bare {
spi_cb *cb;
void *cbdata;
struct spi_ctx_bare *next;
bool queued;
};

struct spi_ctx {
Expand All @@ -28,15 +30,15 @@ struct spi_ctx {
struct sg rx_sg;
};

void spi_queue_xfer(struct spi_ctx *ctx,
enum spi_pcs pcs,
const uint8_t *txbuf, uint16_t txlen,
uint8_t *rxbuf, uint16_t rxlen,
spi_cb *cb, void *cbdata);
void spi_queue_xfer_sg(struct spi_ctx_bare *ctx,
enum spi_pcs pcs,
struct sg *tx, struct sg *rx,
spi_cb *cb, void *cbdata);
int spi_queue_xfer(struct spi_ctx *ctx,
enum spi_pcs pcs,
const uint8_t *txbuf, uint16_t txlen,
uint8_t *rxbuf, uint16_t rxlen,
spi_cb *cb, void *cbdata);
int spi_queue_xfer_sg(struct spi_ctx_bare *ctx,
enum spi_pcs pcs,
struct sg *tx, struct sg *rx,
spi_cb *cb, void *cbdata);
void spi_init(void);

int spi_is_xfer_active(void);
6 changes: 4 additions & 2 deletions toolchain/lib/mchck/spiflash.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <mchck.h>

enum {
GET_STATUS = 0x05,
PAGE_PROGRAM = 0x02,
READ_DATA = 0x03,
READ_STATUS_REGISTER_1 = 0x05,
WRITE_ENABLE = 0x06,
SECTOR_ERASE = 0x20,
BLOCK_ERASE_32KB = 0x52,
BLOCK_ERASE_64KB = 0xD8,
GET_IDENTIFICATION = 0x9F,
};

struct spiflash_device onboard_flash = {
Expand Down Expand Up @@ -146,7 +148,7 @@ int
spiflash_get_id(struct spiflash_device *dev, struct spiflash_transaction *trans,
spiflash_info_cb cb, void *cbdata)
{
trans->spi_query[0] = 0x9F;
trans->spi_query[0] = GET_IDENTIFICATION;
trans->spi_query[1] = trans->spi_query[2] = trans->spi_query[3] = 0x00;
spiflash_setup_xfer(trans, 4, 4);

Expand All @@ -166,7 +168,7 @@ int
spiflash_get_status(struct spiflash_device *dev, struct spiflash_transaction *trans,
spiflash_status_cb cb, void *cbdata)
{
trans->spi_query[0] = 0x05;
trans->spi_query[0] = GET_STATUS;
spiflash_setup_xfer(trans, 1, 2);

trans->status_cb = cb;
Expand Down

0 comments on commit 0e35320

Please sign in to comment.