/* * stm-led.h * --------- * Defines to control the LEDs through GPIO pins. * * Copyright (c) 2015, NORDUnet A/S All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the NORDUnet nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef __STM_LED_H #define __STM_LED_H #include "stm32f4xx_hal.h" #define LED_PORT GPIOJ #define LED_RED GPIO_PIN_1 #define LED_YELLOW GPIO_PIN_2 #define LED_GREEN GPIO_PIN_3 #define LED_BLUE GPIO_PIN_4 #define led_on(pin) HAL_GPIO_WritePin(LED_PORT,pin,SET) #define led_off(pin) HAL_GPIO_WritePin(LED_PORT,pin,RESET) #define led_toggle(pin) HAL_GPIO_TogglePin(LED_PORT,pin) #endif /* __STM_LED_H */ a>diff
path: root/projects/board-test/rtc-test.c
blob: bbb297a5a518aa34c5883ccfe4937dc051a0fc9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Test code for the RTC.
 *
 * Dumps the SRAM and EEPROM on startup, then enables the oscillator.
 * After that, the clock registers are read once a second - the first byte
 * is seconds (and some control bits, so dont expect 0..59).
 *
 */
#include <string.h>

#include "stm-init.h"
#include "stm-led.h"
#include "stm-uart.h"
#include "stm-rtc.h"

#define DELAY() HAL_Delay(1000)


uint8_t buf[1024];
uint32_t i;


uint32_t device_ready(uint16_t i2c_addr)
{
    uart_send_string("Checking readiness of 0x");
    uart_send_hex(i2c_addr, 4);
    uart_send_string("...");

    if (rtc_device_ready(i2c_addr) == HAL_OK) {
	uart_send_string("OK\r\n");
	return 1;
    }

    uart_send_string("Not ready (0x");
    uart_send_hex(i, 4);
    uart_send_string(")\r\n");

    return 0;
}


void send_byte(const uint16_t i2c_addr, const uint8_t value)
{
    uint8_t ch = value;

    uart_send_string("Sending ");
    uart_send_hex(ch, 2);
    uart_send_string(" to 0x");
    uart_send_hex(i2c_addr, 4);
    uart_send_string("...");

    if (rtc_send_byte(i2c_addr, ch, 1000) != HAL_OK) {
	uart_send_string("Timeout\r\n");
	Error_Handler();
    }

    uart_send_string("OK\r\n");
}

void read_bytes (uint8_t *buf, const uint16_t i2c_addr, const uint8_t len)
{
    uart_send_string("Reading ");
    uart_send_integer(len, 1);
    uart_send_string(" bytes from 0x");
    uart_send_hex(i2c_addr, 4);
    uart_send_string("...");

    if (rtc_read_bytes(i2c_addr, buf, len, 1000) != HAL_OK) {
	uart_send_string("Timeout\r\n");
	Error_Handler();
    }

    uart_send_string("OK\r\n");
}

void request_data(uint8_t *buf, const uint16_t i2c_addr, const uint8_t offset, const uint8_t bytes)
{
    send_byte(i2c_addr, offset);
    read_bytes(buf, i2c_addr, bytes);
}

void print_time()
{
    request_data(buf, RTC_RTC_ADDR, RTC_TIME_OFFSET, RTC_TIME_BYTES);

    for (i = 0; i < RTC_TIME_BYTES; i++) {
	uart_send_hex(buf[i], 2);
	uart_send_string(" ");
    }
}

void dump_sram()
{
    request_data(buf, RTC_RTC_ADDR, 0x0, RTC_SRAM_TOTAL_BYTES);

    uart_send_string("SRAM contents:\r\n");
    uart_send_hexdump(buf, 0, RTC_SRAM_TOTAL_BYTES);

    uart_send_string("\r\n");
}

void dump_eeprom()
{
    request_data(buf, RTC_EEPROM_ADDR, 0x0, RTC_EEPROM_TOTAL_BYTES);

    uart_send_string("EEPROM contents:\r\n");
    uart_send_hexdump(buf, 0, RTC_EEPROM_TOTAL_BYTES);
    uart_send_string("\r\n");

    request_data(buf, RTC_EEPROM_ADDR, RTC_EEPROM_EUI48_OFFSET, RTC_EEPROM_EUI48_BYTES);
    uart_send_string("EEPROM EUI-48:\r\n");
    uart_send_hexdump(buf, RTC_EEPROM_EUI48_OFFSET, RTC_EEPROM_EUI48_BYTES);

    uart_send_string("\r\n");
}

void enable_oscillator()
{
    uart_send_string("Enabling oscillator...\r\n");

    if (rtc_enable_oscillator() != HAL_OK) {
	uart_send_string("Timeout\r\n");
	Error_Handler();
    }

    uart_send_string("OK\r\n");
}


int
main()
{
    stm_init();
    uart_send_string("\r\n\r\n*** Init done\r\n");

    dump_sram();
    dump_eeprom();

    enable_oscillator();

    while (1)  {
	memset(buf, 0, sizeof(buf));

	if (! device_ready(RTC_RTC_ADDR)) {
	    goto fail;
	}

	print_time(buf);

	uart_send_string("\r\n\r\n");

	HAL_GPIO_TogglePin(LED_PORT, LED_GREEN);
	DELAY();
	continue;

    fail:
	HAL_GPIO_TogglePin(LED_PORT, LED_RED);
	DELAY();
    }
}