remove unused libraries
This commit is contained in:
parent
9ba771b92e
commit
5357985bad
|
@ -1,175 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to send data using ESP8266 WiFi SoC (code)
|
||||
* @file radio_esp8266.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref radio_esp8266_usart
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
#include <string.h> // string and memory utilities
|
||||
#include <stdio.h> // string utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
|
||||
#include "radio_esp8266.h" // radio header and definitions
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup radio_esp8266_usart USART peripheral used for communication with radio
|
||||
* @{
|
||||
*/
|
||||
#define RADIO_ESP8266_USART 2 /**< USART peripheral */
|
||||
/** @} */
|
||||
|
||||
/* input and output buffers and used memory */
|
||||
static uint8_t rx_buffer[24] = {0}; /**< buffer for received data (we only expect AT responses) */
|
||||
static volatile uint16_t rx_used = 0; /**< number of byte in receive buffer */
|
||||
static uint8_t tx_buffer[256] = {0}; /**< buffer for data to transmit */
|
||||
static volatile uint16_t tx_used = 0; /**< number of bytes used in transmit buffer */
|
||||
|
||||
volatile bool radio_esp8266_activity = false;
|
||||
volatile bool radio_esp8266_success = false;
|
||||
|
||||
/** transmit data to radio
|
||||
* @param[in] data data to transmit
|
||||
* @param[in] length length of data to transmit
|
||||
*/
|
||||
static void radio_esp8266_transmit(uint8_t* data, uint8_t length) {
|
||||
while (tx_used || !usart_get_flag(USART(RADIO_ESP8266_USART), USART_SR_TXE)) { // wait until ongoing transmission completed
|
||||
usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART)); // enable transmit interrupt
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // ensure transmit interrupt is disable to prevent index corruption (the ISR should already have done it)
|
||||
radio_esp8266_activity = false; // reset status because of new activity
|
||||
for (tx_used=0; tx_used<length && tx_used<LENGTH(tx_buffer); tx_used++) { // copy data
|
||||
tx_buffer[tx_used] = data[length-1-tx_used]; // put character in buffer (in reverse order)
|
||||
}
|
||||
if (tx_used) {
|
||||
usart_enable_tx_interrupt(USART(RADIO_ESP8266_USART)); // enable interrupt to send bytes
|
||||
}
|
||||
}
|
||||
|
||||
void radio_esp8266_setup(void)
|
||||
{
|
||||
/* enable USART I/O peripheral */
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
|
||||
rcc_periph_clock_enable(USART_PORT_RCC(RADIO_ESP8266_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(USART_RCC(RADIO_ESP8266_USART)); // enable clock for USART peripheral
|
||||
gpio_set_mode(USART_PORT(RADIO_ESP8266_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(RADIO_ESP8266_USART)); // setup GPIO pin USART transmit
|
||||
gpio_set_mode(USART_PORT(RADIO_ESP8266_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RADIO_ESP8266_USART)); // setup GPIO pin USART receive
|
||||
gpio_set(USART_PORT(RADIO_ESP8266_USART), USART_PIN_RX(RADIO_ESP8266_USART)); // pull up to avoid noise when not connected
|
||||
|
||||
/* setup USART parameters for ESP8266 AT firmware */
|
||||
usart_set_baudrate(USART(RADIO_ESP8266_USART), 115200); // AT firmware 0.51 (SDK 1.5.0) uses 115200 bps
|
||||
usart_set_databits(USART(RADIO_ESP8266_USART), 8);
|
||||
usart_set_stopbits(USART(RADIO_ESP8266_USART), USART_STOPBITS_1);
|
||||
usart_set_mode(USART(RADIO_ESP8266_USART), USART_MODE_TX_RX);
|
||||
usart_set_parity(USART(RADIO_ESP8266_USART), USART_PARITY_NONE);
|
||||
usart_set_flow_control(USART(RADIO_ESP8266_USART), USART_FLOWCONTROL_NONE);
|
||||
|
||||
nvic_enable_irq(USART_IRQ(RADIO_ESP8266_USART)); // enable the USART interrupt
|
||||
usart_enable_rx_interrupt(USART(RADIO_ESP8266_USART)); // enable receive interrupt
|
||||
usart_enable(USART(RADIO_ESP8266_USART)); // enable USART
|
||||
|
||||
/* reset buffer states */
|
||||
rx_used = 0;
|
||||
tx_used = 0;
|
||||
radio_esp8266_activity = false;
|
||||
radio_esp8266_success = false;
|
||||
|
||||
radio_esp8266_transmit((uint8_t*)"AT\r\n",4); // verify if module is present
|
||||
while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
radio_esp8266_transmit((uint8_t*)"AT+RST\r\n",8); // reset module
|
||||
while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
while(rx_used<13 || memcmp((char*)&rx_buffer[rx_used-13], "WIFI GOT IP\r\n", 13)!=0) { // wait to have IP
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
radio_esp8266_transmit((uint8_t*)"ATE0\r\n",6); // disable echoing
|
||||
while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
}
|
||||
|
||||
void radio_esp8266_tcp_open(char* host, uint16_t port)
|
||||
{
|
||||
char command[256] = {0}; // string to create command
|
||||
int length = snprintf(command, LENGTH(command), "AT+CIPSTART=\"TCP\",\"%s\",%u\r\n", host, port); // create AT command to establish a TCP connection
|
||||
if (length>0) {
|
||||
radio_esp8266_transmit((uint8_t*)command, length);
|
||||
}
|
||||
}
|
||||
|
||||
void radio_esp8266_send(uint8_t* data, uint8_t length)
|
||||
{
|
||||
char command[16+1] = {0}; // string to create command
|
||||
int command_length = snprintf(command, LENGTH(command), "AT+CIPSEND=%u\r\n", length); // create AT command to send data
|
||||
if (command_length>0) {
|
||||
radio_esp8266_transmit((uint8_t*)command, command_length); // transmit AT command
|
||||
while (!radio_esp8266_activity || !radio_esp8266_success) { // wait for response
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
if (!radio_esp8266_success) { // send AT command did not succeed
|
||||
return; // don't transmit data
|
||||
}
|
||||
radio_esp8266_transmit(data, length); // transmit data
|
||||
}
|
||||
}
|
||||
|
||||
void radio_esp8266_close(void)
|
||||
{
|
||||
radio_esp8266_transmit((uint8_t*)"AT+CIPCLOSE\r\n", 13); // send AT command to close established connection
|
||||
}
|
||||
|
||||
/** USART interrupt service routine called when data has been transmitted or received */
|
||||
void USART_ISR(RADIO_ESP8266_USART)(void)
|
||||
{
|
||||
if (usart_get_interrupt_source(USART(RADIO_ESP8266_USART), USART_SR_TXE)) { // data has been transmitted
|
||||
if (tx_used) { // there is still data in the buffer to transmit
|
||||
usart_send(USART(RADIO_ESP8266_USART),tx_buffer[tx_used-1]); // put data in transmit register
|
||||
tx_used--; // update used size
|
||||
} else { // no data in the buffer to transmit
|
||||
usart_disable_tx_interrupt(USART(RADIO_ESP8266_USART)); // disable transmit interrupt
|
||||
}
|
||||
}
|
||||
if (usart_get_interrupt_source(USART(RADIO_ESP8266_USART), USART_SR_RXNE)) { // data has been received
|
||||
while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
|
||||
memmove(rx_buffer,&rx_buffer[1],LENGTH(rx_buffer)-1); // drop old data to make space (ring buffer are more efficient but harder to handle)
|
||||
rx_used--; // update used buffer information
|
||||
}
|
||||
rx_buffer[rx_used++] = usart_recv(USART(RADIO_ESP8266_USART)); // put character in buffer
|
||||
// if the used send a packet with these strings during the commands detection the AT command response will break (AT commands are hard to handle perfectly)
|
||||
if (rx_used>=4 && memcmp((char*)&rx_buffer[rx_used-4], "OK\r\n", 4)==0) { // OK received
|
||||
radio_esp8266_activity = true; // response received
|
||||
radio_esp8266_success = true; // command succeeded
|
||||
rx_used = 0; // reset buffer
|
||||
} else if (rx_used>=7 && memcmp((char*)&rx_buffer[rx_used-7], "ERROR\r\n", 7)==0) { // ERROR received
|
||||
radio_esp8266_activity = true; // response received
|
||||
radio_esp8266_success = false; // command failed
|
||||
rx_used = 0; // reset buffer
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to send data using ESP8266 WiFi SoC (API)
|
||||
* @file radio_esp8266.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref radio_esp8266_usart
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** a response has been returned by the radio */
|
||||
extern volatile bool radio_esp8266_activity;
|
||||
/** the last command has succeeded */
|
||||
extern volatile bool radio_esp8266_success;
|
||||
|
||||
/** setup peripherals to communicate with radio
|
||||
* @note this is blocking to ensure we are connected to the WiFi network
|
||||
*/
|
||||
void radio_esp8266_setup(void);
|
||||
/** establish TCP connection
|
||||
* @param[in] host host to connect to
|
||||
* @param[in] port TCP port to connect to
|
||||
* @note wait for activity to get success status
|
||||
*/
|
||||
void radio_esp8266_tcp_open(char* host, uint16_t port);
|
||||
/** send data (requires established connection)
|
||||
* @param[in] data data to send
|
||||
* @param[in] length size of data to send
|
||||
* @note wait for activity to get success status
|
||||
*/
|
||||
void radio_esp8266_send(uint8_t* data, uint8_t length);
|
||||
/** close established connection
|
||||
* @note wait for activity to get success status
|
||||
*/
|
||||
void radio_esp8266_close(void);
|
286
lib/rtc_dcf77.c
286
lib/rtc_dcf77.c
|
@ -1,286 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to get time from a DCF77 module (code)
|
||||
* @file rtc_dcf77.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016-2017
|
||||
* @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/spi.h> // SPI library
|
||||
#include <libopencm3/stm32/timer.h> // timer library
|
||||
|
||||
#include "rtc_dcf77.h" // RTC DCF77 library API
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup rtc_dcf77_gpio output to enable DCF module and input to capture DCF signal
|
||||
* @{
|
||||
*/
|
||||
#define RTC_DCF77_ENABLE_PORT A /**< GPIO port to enable the module */
|
||||
#define RTC_DCF77_ENABLE_PIN 2 /**< GPIO pinto enable the module */
|
||||
#define RTC_DCF77_SIGNAL_PORT A /**< GPIO port to capture the DCF signal */
|
||||
#define RTC_DCF77_SIGNAL_PIN 3 /**< GPIO pin to capture the DCF signal */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup rtc_dcf77_timer timer to sample DCF77 signal
|
||||
* @{
|
||||
*/
|
||||
#define RTC_DCF77_TIMER 4 /**< timer peripheral */
|
||||
/** @} */
|
||||
|
||||
volatile bool rtc_dcf77_time_flag = false;
|
||||
struct rtc_dcf77_time_t rtc_dcf77_time = {false, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
/**< the received DCF77 frame bits */
|
||||
static volatile uint64_t rtc_dcf77_frame = 0;
|
||||
/** values of the DCF77 signal over 10 ms for 1 s (how many times it is high) */
|
||||
static uint8_t rtc_dcf77_bins[100] = {0};
|
||||
/** the bin shift for the bit phase */
|
||||
static uint8_t rtc_dcf77_phase = 0;
|
||||
/** the maximum phase value */
|
||||
static int16_t rtc_dcf77_phase_max = INT16_MIN;
|
||||
/** if the current phase has been verified */
|
||||
static bool rtc_dcf77_phase_locked = false;
|
||||
/** number of invalid decoding */
|
||||
static uint8_t rtc_dcf77_invalid = 0;
|
||||
/** maximum number of invalid decoding before switching off */
|
||||
#define RTC_DCF77_INVALID_MAX 5
|
||||
|
||||
void rtc_dcf77_setup(void)
|
||||
{
|
||||
// setup enable output
|
||||
rcc_periph_clock_enable(RCC_GPIO(RTC_DCF77_ENABLE_PORT)); // enable clock GPIO peripheral
|
||||
gpio_set_mode(GPIO(RTC_DCF77_ENABLE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(RTC_DCF77_ENABLE_PIN)); // set pin to output push-pull to be able to enable the module
|
||||
rtc_dcf77_off(); // disable module at start
|
||||
|
||||
// setup signal input
|
||||
rcc_periph_clock_enable(RCC_GPIO(RTC_DCF77_SIGNAL_PORT)); // enable clock for signal input peripheral
|
||||
gpio_set_mode(GPIO(RTC_DCF77_SIGNAL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(RTC_DCF77_SIGNAL_PIN)); // set signal pin to input
|
||||
|
||||
// setup timer to sample signal at 1kHz
|
||||
rcc_periph_clock_enable(RCC_TIM(RTC_DCF77_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(RTC_DCF77_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(RTC_DCF77_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(RTC_DCF77_TIMER), 1); // set prescaler to divide frequency by two, to be able to have a period of 1 kHz (72MHz/2/2^16=549Hz<1kHz)
|
||||
timer_set_period(TIM(RTC_DCF77_TIMER), rcc_ahb_frequency/2/1000-1); // set period to 1kHz (plus hand tuning)
|
||||
timer_clear_flag(TIM(RTC_DCF77_TIMER), TIM_SR_UIF); // clear update event flag
|
||||
timer_update_on_overflow(TIM(RTC_DCF77_TIMER)); // only use counter overflow as UEV source
|
||||
timer_enable_irq(TIM(RTC_DCF77_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(RTC_DCF77_TIMER)); // catch interrupt in service routine
|
||||
}
|
||||
|
||||
void rtc_dcf77_on(void)
|
||||
{
|
||||
if (!gpio_get(GPIO(RTC_DCF77_ENABLE_PORT), GPIO(RTC_DCF77_ENABLE_PIN))) { // receiver is already turned on
|
||||
return; // do nothing
|
||||
}
|
||||
rtc_dcf77_frame = 0; // reset frame
|
||||
rtc_dcf77_phase_locked = false; // reset phase lock
|
||||
rtc_dcf77_phase_max = INT16_MIN; // restart searching for phase
|
||||
rtc_dcf77_invalid = 0; // reset invalid count
|
||||
gpio_clear(GPIO(RTC_DCF77_ENABLE_PORT), GPIO(RTC_DCF77_ENABLE_PIN)); // enable module by pulling pin low
|
||||
timer_set_counter(TIM(RTC_DCF77_TIMER), 0); // reset timer counter
|
||||
timer_enable_counter(TIM(RTC_DCF77_TIMER)); // start timer to sample signal
|
||||
}
|
||||
|
||||
void rtc_dcf77_off(void)
|
||||
{
|
||||
gpio_set(GPIO(RTC_DCF77_ENABLE_PORT), GPIO(RTC_DCF77_ENABLE_PIN)); // disable module by pull pin high
|
||||
timer_disable_counter(TIM(RTC_DCF77_TIMER)); // stop timer since we don't need to sample anymore
|
||||
}
|
||||
|
||||
/** decode rtc_dcf77_frame DCF77 frame into rtc_dcf77_time DCF77 time
|
||||
* @note check valid for validity
|
||||
*/
|
||||
static void rtc_dcf77_decode(void)
|
||||
{
|
||||
rtc_dcf77_time.valid = false; // reset validity
|
||||
|
||||
if (rtc_dcf77_frame==0) { // no time received yet
|
||||
return;
|
||||
}
|
||||
if (!(rtc_dcf77_frame&((uint64_t)1<<20))) { // start of encoded time should always be 1
|
||||
return;
|
||||
}
|
||||
|
||||
// check minute parity
|
||||
uint8_t parity = 0; // to check parity
|
||||
for (uint8_t bit=21; bit<=28; bit++) {
|
||||
if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
|
||||
parity++; // count the set bits
|
||||
}
|
||||
}
|
||||
if (parity%2) { // parity should be even
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.minutes = 1*((rtc_dcf77_frame>>21)&(0x1))+2*((rtc_dcf77_frame>>22)&(0x1))+4*((rtc_dcf77_frame>>23)&(0x1))+8*((rtc_dcf77_frame>>24)&(0x1))+10*((rtc_dcf77_frame>>25)&(0x1))+20*((rtc_dcf77_frame>>26)&(0x1))+40*((rtc_dcf77_frame>>27)&(0x1)); // read minute (00-59)
|
||||
if (rtc_dcf77_time.minutes>59) { // minutes should not be more than 59
|
||||
return;
|
||||
}
|
||||
|
||||
// check hour parity
|
||||
parity = 0;
|
||||
for (uint8_t bit=29; bit<=35; bit++) {
|
||||
if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
|
||||
parity++; // count the set bits
|
||||
}
|
||||
}
|
||||
if (parity%2) { // parity should be even
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.hours = 1*((rtc_dcf77_frame>>29)&(0x1))+2*((rtc_dcf77_frame>>30)&(0x1))+4*((rtc_dcf77_frame>>31)&(0x1))+8*((rtc_dcf77_frame>>32)&(0x1))+10*((rtc_dcf77_frame>>33)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x1)); // read hour (00-23)
|
||||
if (rtc_dcf77_time.hours>23) { // hours should not be more than 23
|
||||
return;
|
||||
}
|
||||
|
||||
// check date parity
|
||||
parity = 0;
|
||||
for (uint8_t bit=36; bit<=58; bit++) {
|
||||
if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
|
||||
parity++; // count the set bits
|
||||
}
|
||||
}
|
||||
if (parity%2) { // parity should be even
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.day = 1*((rtc_dcf77_frame>>36)&(0x1))+2*((rtc_dcf77_frame>>37)&(0x1))+4*((rtc_dcf77_frame>>38)&(0x1))+8*((rtc_dcf77_frame>>39)&(0x1))+10*((rtc_dcf77_frame>>40)&(0x1))+20*((rtc_dcf77_frame>>41)&(0x1)); // read day of the month (01-31)
|
||||
if (rtc_dcf77_time.day==0 || rtc_dcf77_time.day>31) { // day of the month should be 1-31
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.weekday = 1*((rtc_dcf77_frame>>42)&(0x1))+2*((rtc_dcf77_frame>>43)&(0x1))+4*((rtc_dcf77_frame>>44)&(0x1)); // read day of the week (1=Monday - 7=Sunday)
|
||||
if (rtc_dcf77_time.weekday==0 || rtc_dcf77_time.weekday>7) { // day of the week should be 1-7
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.month = 1*((rtc_dcf77_frame>>45)&(0x1))+2*((rtc_dcf77_frame>>46)&(0x1))+4*((rtc_dcf77_frame>>47)&(0x1))+8*((rtc_dcf77_frame>>48)&(0x1))+10*((rtc_dcf77_frame>>49)&(0x1)); // read month of the year (01-12)
|
||||
if (rtc_dcf77_time.month==0 || rtc_dcf77_time.month>12) { // month of the year should be 1-12
|
||||
return;
|
||||
}
|
||||
rtc_dcf77_time.year = 1*((rtc_dcf77_frame>>50)&(0x1))+2*((rtc_dcf77_frame>>51)&(0x1))+4*((rtc_dcf77_frame>>52)&(0x1))+8*((rtc_dcf77_frame>>53)&(0x1))+10*((rtc_dcf77_frame>>54)&(0x1))+20*((rtc_dcf77_frame>>55)&(0x1))+40*((rtc_dcf77_frame>>56)&(0x1))+80*((rtc_dcf77_frame>>57)&(0x1)); // read year of the century (00-99)
|
||||
if (rtc_dcf77_time.year>99) { // year should be <100
|
||||
return;
|
||||
}
|
||||
|
||||
rtc_dcf77_time.valid = true; // if we managed it until here the decoding is successful
|
||||
}
|
||||
|
||||
/** find phase of 1 seconds DCF77 signal in the bins
|
||||
* searches the complete second for the highest correlation if the phase it not locked
|
||||
* searches only around the last phase if locked
|
||||
* saves the new phase with the highest correlation
|
||||
*/
|
||||
static void rtc_dcf77_phase_detector(void) {
|
||||
uint8_t integral_i = 0; // which bin has the highest integral/correlation
|
||||
int16_t integral_max = 0; // maximum integral value found
|
||||
|
||||
for (uint8_t start=0; start<(rtc_dcf77_phase_locked ? 10 : LENGTH(rtc_dcf77_bins)); start++) { // which bin has been used to start the convolution (only use +/- 15 bits of previous phase if locked)
|
||||
int16_t integral = 0; // value of the integral
|
||||
for (uint8_t bin=0; bin<LENGTH(rtc_dcf77_bins); bin++) { // go through bins to calculate correlation
|
||||
int8_t dfc77_signal = -1; // the signal of the reference DCF77 signal
|
||||
if (bin<10) { // the signal is always high for the first 100 ms
|
||||
dfc77_signal = 1; // use highest values
|
||||
} else if (bin<20) { // the signal has 50% chance of being high for the next 100 ms (encoding the bit)
|
||||
dfc77_signal = 0; // use middle value
|
||||
}
|
||||
// the rest of the time the signal is low (keep lowest value)
|
||||
integral += rtc_dcf77_bins[(start+bin+rtc_dcf77_phase+LENGTH(rtc_dcf77_bins)-5)%LENGTH(rtc_dcf77_bins)]*dfc77_signal; // calculate the correlation at this point and integrate it (start with previous phase - 15 bins)
|
||||
}
|
||||
if (integral>integral_max) { // we found a better correlation result
|
||||
integral_max = integral; // save new best result
|
||||
integral_i = (start+rtc_dcf77_phase+LENGTH(rtc_dcf77_bins)-5)%LENGTH(rtc_dcf77_bins); // start new best phase start
|
||||
}
|
||||
}
|
||||
|
||||
if ((int16_t)(integral_max+40)>rtc_dcf77_phase_max) { // only save new phase if it is better than the last one, with some margin to compensate for the drift (perfect correlation = 100, worst correlation = -800)
|
||||
rtc_dcf77_phase_max = integral_max; // save best phase value
|
||||
rtc_dcf77_phase = integral_i; // save bin index corresponding to start of the phase
|
||||
}
|
||||
}
|
||||
|
||||
/** interrupt service routine called for timer */
|
||||
void TIM_ISR(RTC_DCF77_TIMER)(void)
|
||||
{
|
||||
static uint8_t bin_state = 0; // how many samples have been stored in the bin
|
||||
static uint8_t bin_i = 0; // current bin filled
|
||||
static uint8_t bit_i = 0; // current bit in the DCF77 minute frame
|
||||
if (timer_get_flag(TIM(RTC_DCF77_TIMER), TIM_SR_UIF)) { // overflow update event happened
|
||||
timer_clear_flag(TIM(RTC_DCF77_TIMER), TIM_SR_UIF); // clear flag
|
||||
// fill bin with current sample state
|
||||
if (gpio_get(GPIO(RTC_DCF77_SIGNAL_PORT), GPIO(RTC_DCF77_SIGNAL_PIN))) {
|
||||
rtc_dcf77_bins[bin_i]++; // only need to increase if the signal is high
|
||||
}
|
||||
bin_state++; // remember we filled the bin
|
||||
|
||||
if (bin_state>=10) { // bin has 10x1 ms samples, it is now full
|
||||
bin_i = (bin_i+1)%LENGTH(rtc_dcf77_bins); // go to next bin
|
||||
rtc_dcf77_bins[bin_i] = 0; // restart bin
|
||||
bin_state = 0; // restart collecting
|
||||
}
|
||||
|
||||
if (0==bin_i && 0==bin_state) { // we have 1 s of samples
|
||||
if (bit_i<59) {
|
||||
rtc_dcf77_phase_detector(); // detect phase in signal
|
||||
// check modulation of first 100 ms
|
||||
uint16_t modulation = 0;
|
||||
for (uint8_t bin=0; bin<10; bin++) {
|
||||
modulation += rtc_dcf77_bins[(rtc_dcf77_phase+bin)%LENGTH(rtc_dcf77_bins)];
|
||||
}
|
||||
if (modulation<50) { // signal is not modulated, it might be the 60th pause bit
|
||||
bit_i = 0; // restart frame
|
||||
rtc_dcf77_frame = 0; // restart frame
|
||||
rtc_dcf77_phase_max = INT16_MIN; // restart searching for phase
|
||||
rtc_dcf77_phase_locked = false; // unlock phase since the decoding seems wrong
|
||||
} else { // modulation detected
|
||||
// check modulation of next 100 ms
|
||||
modulation = 0;
|
||||
for (uint8_t bin=10; bin<20; bin++) {
|
||||
modulation += rtc_dcf77_bins[(rtc_dcf77_phase+bin)%LENGTH(rtc_dcf77_bins)];
|
||||
}
|
||||
if (modulation<50) { // it's a 0
|
||||
// bit is already cleared
|
||||
} else { // it's a 1
|
||||
rtc_dcf77_frame |= (1ULL<<bit_i); // set bit
|
||||
}
|
||||
bit_i++; // go to next bit
|
||||
}
|
||||
} else { // complete DCF77 frame received
|
||||
rtc_dcf77_decode(); // decode frame
|
||||
if (rtc_dcf77_time.valid) { // decoded time is valid
|
||||
rtc_dcf77_time.milliseconds = rtc_dcf77_phase*10; // save milliseconds corresponding to phase
|
||||
rtc_dcf77_phase_locked = true; // lock phase since decoding succeeded
|
||||
rtc_dcf77_invalid = 0; // remember we had an valid decoding
|
||||
rtc_dcf77_time_flag = true; // notify user we have time
|
||||
} else {
|
||||
rtc_dcf77_invalid++; // remember we had an invalid decoding
|
||||
}
|
||||
if (rtc_dcf77_invalid>=RTC_DCF77_INVALID_MAX) { // too many invalid decoding
|
||||
rtc_dcf77_off(); // switch off receiver so it can re-tune
|
||||
}
|
||||
bit_i = 0; // restart frame
|
||||
rtc_dcf77_frame = 0; // restart frame
|
||||
}
|
||||
}
|
||||
} else { // no other interrupt should occur
|
||||
while (true); // unhandled exception: wait for the watchdog to bite
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to get time from a DCF77 module (API)
|
||||
* @file rtc_dcf77.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016-2017
|
||||
* @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** set when time information has been received */
|
||||
extern volatile bool rtc_dcf77_time_flag;
|
||||
|
||||
/** decoded DCF77 time received */
|
||||
extern struct rtc_dcf77_time_t {
|
||||
bool valid; /**< if the time is valid */
|
||||
uint8_t milliseconds; /**< milliseconds (00-99) */
|
||||
uint8_t seconds; /**< seconds (always 0) */
|
||||
uint8_t minutes; /**< minutes (00-49) */
|
||||
uint8_t hours; /**< hours (00-23) */
|
||||
uint8_t day; /**< day of the month (01-31) */
|
||||
uint8_t weekday; /**< day of the week (1-7=Monday-Sunday) */
|
||||
uint8_t month; /**< month (01-12) */
|
||||
uint8_t year; /**< year within century (00-99) */
|
||||
} rtc_dcf77_time;
|
||||
|
||||
/** setup DCF77 time receiver module */
|
||||
void rtc_dcf77_setup(void);
|
||||
/** switch on DCF77 time receiver module
|
||||
* @note it switches back off after too many invalid decoding attempts
|
||||
*/
|
||||
void rtc_dcf77_on(void);
|
||||
/** switch off DCF77 time receiver module */
|
||||
void rtc_dcf77_off(void);
|
397
lib/rtc_ds1307.c
397
lib/rtc_ds1307.c
|
@ -1,397 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to communicate with the Maxim DS1307 I2C RTC IC (code)
|
||||
* @file rtc_ds1307.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016-2017
|
||||
* @note peripherals used: I2C @ref i2c_master_i2c
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdio.h> // standard I/O facilities
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/i2c.h> // I2C library
|
||||
|
||||
#include "global.h" // global utilities
|
||||
#include "rtc_ds1307.h" // RTC header and definitions
|
||||
#include "i2c_master.h" // i2c utilities
|
||||
|
||||
#define RTC_DS1307_I2C_ADDR 0x68 /**< DS1307 I2C address (fixed to 0b1101000) */
|
||||
|
||||
void rtc_ds1307_setup(void)
|
||||
{
|
||||
// configure I2C peripheral
|
||||
i2c_master_setup(false); // DS1307 only supports normal mode (up to 100 kHz)
|
||||
}
|
||||
|
||||
bool rtc_ds1307_oscillator_disabled(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing CH value
|
||||
return false;
|
||||
}
|
||||
return data[0]&0x80; // return CH bit value to indicate if oscillator is disabled
|
||||
}
|
||||
|
||||
uint16_t rtc_ds1307_read_square_wave(void)
|
||||
{
|
||||
uint16_t to_return = 0; // square wave frequency to return (in Hz)
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint16_t rtc_ds1307_rs[] = {1, 4096, 8192, 32768}; // RS1/RS0 values
|
||||
const uint8_t address[] = {0x07}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing control register
|
||||
return 0xffff; // error occurred
|
||||
}
|
||||
if (data[0]&0x10) { // verify if the square wave is enabled (SQWE)
|
||||
to_return = rtc_ds1307_rs[data[0]&0x03]; // read RS1/RS0 and get value
|
||||
} else {
|
||||
to_return = 0; // square wave output is disabled
|
||||
}
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_seconds(void)
|
||||
{
|
||||
uint8_t to_return = 0; // seconds to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_minutes(void)
|
||||
{
|
||||
uint8_t to_return = 0; // minutes to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x01}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = (data[0]>>4)*10+(data[0]&0x0f); // convert BCD coding into minutes
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_hours(void)
|
||||
{
|
||||
uint8_t to_return = 0; // hours to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x02}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
if (data[0]&0x40) { // 12 hour mode
|
||||
if (data[0]&0x02) { // PM
|
||||
to_return += 12; // add the 12 hours
|
||||
}
|
||||
to_return += ((data[0]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
|
||||
} else {
|
||||
to_return = ((data[0]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
|
||||
}
|
||||
to_return += (data[0]&0x0f); // convert BCD coding into hours (second digit)
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_day(void)
|
||||
{
|
||||
uint8_t to_return = 0; // day to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x03}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = (data[0]&0x07); // convert BCD coding into days
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_date(void)
|
||||
{
|
||||
uint8_t to_return = 0; // date to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x04}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x30)>>4)*10+(data[0]&0x0f); // convert BCD coding into date
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_month(void)
|
||||
{
|
||||
uint8_t to_return = 0; // month to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x05}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x10)>>4)*10+(data[0]&0x0f); // convert BCD coding into month
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t rtc_ds1307_read_year(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x06}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
uint8_t to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
|
||||
return to_return;
|
||||
}
|
||||
|
||||
uint8_t* rtc_ds1307_read_time(void)
|
||||
{
|
||||
static uint8_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year}
|
||||
uint8_t data[7] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read all time bytes
|
||||
return NULL; // error occurred
|
||||
}
|
||||
time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD
|
||||
time[1] = (data[1]>>4)*10+(data[1]&0x0f); // convert minutes from BCD
|
||||
time[2] = 0; // re-initialize hours
|
||||
if (data[2]&0x40) { // 12 hour mode
|
||||
if (data[2]&0x02) { // PM
|
||||
time[2] += 12; // add the 12 hours
|
||||
}
|
||||
time[2] += ((data[2]&0x10)>>4)*10; // convert BCD coding into hours (first digit)
|
||||
} else {
|
||||
time[2] = ((data[2]&0x30)>>4)*10; // convert BCD coding into hours (first digit)
|
||||
}
|
||||
time[2] += (data[2]&0x0f); // convert BCD coding into hours (second digit)
|
||||
time[3] = (data[3]&0x07); // convert BCD coding into days
|
||||
time[4] = ((data[4]&0x30)>>4)*10+(data[4]&0x0f); // convert BCD coding into date
|
||||
time[5] = ((data[5]&0x10)>>4)*10+(data[5]&0x0f); // convert BCD coding into month
|
||||
time[6] = ((data[6]&0xf0)>>4)*10+(data[6]&0x0f); // convert BCD coding into year
|
||||
return time;
|
||||
}
|
||||
|
||||
bool rtc_ds1307_read_ram(uint8_t* data, uint8_t start, uint8_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
return false;
|
||||
}
|
||||
if (start>55 || start+length>56) { // out of bounds RAM
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint8_t address[] = {0x08+start}; // memory address for data
|
||||
return i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read RAM (starting at 0x08)
|
||||
}
|
||||
|
||||
bool rtc_ds1307_oscillator_disable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to write CH value data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] |= 0x80; // set CH to disable oscillator
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
bool rtc_ds1307_oscillator_enable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to write CH value data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] &= 0x7f; // clear CH to enable oscillator
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_square_wave(uint16_t frequency)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to write control register value data over I2C
|
||||
switch (frequency) { // set RS1/RS0 based on frequency
|
||||
case 0:
|
||||
data[0] = 0;
|
||||
break;
|
||||
case 1:
|
||||
data[0] = 0|(1<<4);
|
||||
break;
|
||||
case 4096:
|
||||
data[0] = 1|(1<<4);
|
||||
break;
|
||||
case 8192:
|
||||
data[0] = 2|(1<<4);
|
||||
break;
|
||||
case 32768:
|
||||
data[0] = 3|(1<<4);
|
||||
break;
|
||||
default: // unspecified frequency
|
||||
return false;
|
||||
}
|
||||
const uint8_t address[] = {0x07}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_seconds(uint8_t seconds)
|
||||
{
|
||||
if (seconds>59) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to read CH value data and write seconds value over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] &= 0x80; // only keep CH flag
|
||||
data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write current seconds with previous CH value
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_minutes(uint8_t minutes)
|
||||
{
|
||||
if (minutes>59) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
|
||||
|
||||
const uint8_t address[] = {0x01}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_hours(uint8_t hours)
|
||||
{
|
||||
if (hours>24) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
|
||||
|
||||
const uint8_t address[] = {0x02}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_day(uint8_t day)
|
||||
{
|
||||
if (day<1 || day>7) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (day%8); // encode day in BCD format
|
||||
|
||||
const uint8_t address[] = {0x03}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_date(uint8_t date)
|
||||
{
|
||||
if (date<1 || date>31) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
|
||||
|
||||
const uint8_t address[] = {0x04}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_month(uint8_t month)
|
||||
{
|
||||
if (month<1 || month>12) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
|
||||
|
||||
const uint8_t address[] = {0x05}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_year(uint8_t year)
|
||||
{
|
||||
if (year>99) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
|
||||
|
||||
const uint8_t address[] = {0x06}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year)
|
||||
{
|
||||
uint8_t data[7] = {0}; // to write all time values
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
// seconds
|
||||
if (seconds>59) {
|
||||
return false;
|
||||
}
|
||||
if (!i2c_master_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, 1)) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] &= 0x80; // only keep CH flag
|
||||
data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
// minutes
|
||||
if (minutes>59) {
|
||||
return false;
|
||||
}
|
||||
data[1] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
|
||||
// hours
|
||||
if (hours>24) {
|
||||
return false;
|
||||
}
|
||||
data[2] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
|
||||
// day
|
||||
if (day<1 || day>7) {
|
||||
return false;
|
||||
}
|
||||
data[3] = (day%8); // encode day in BCD format
|
||||
// date
|
||||
if (date<1 || date>31) {
|
||||
return false;
|
||||
}
|
||||
data[4] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
|
||||
// month
|
||||
if (month<1 || month>12) {
|
||||
return false;
|
||||
}
|
||||
data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
|
||||
// year
|
||||
if (year>99) {
|
||||
return false;
|
||||
}
|
||||
data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
|
||||
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_ds1307_write_ram(uint8_t* data, uint8_t start, uint8_t length)
|
||||
{
|
||||
// sanity checks
|
||||
if (data==NULL || length==0) { // nothing to read
|
||||
return false;
|
||||
}
|
||||
if (start>55 || start+length>56) { // out of bounds RAM
|
||||
return false;
|
||||
}
|
||||
const uint8_t address[] = {0x08+start}; // memory address for data
|
||||
return i2c_master_write(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // write RAM (starting at 0x08)
|
||||
}
|
140
lib/rtc_ds1307.h
140
lib/rtc_ds1307.h
|
@ -1,140 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to communicate with the Maxim DS1307 I2C RTC IC (API)
|
||||
* @file rtc_ds1307.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016-2017
|
||||
* @note peripherals used: I2C @ref i2c_master_i2c
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** setup communication with RTC IC
|
||||
* configure the I2C port defined in the sources
|
||||
*/
|
||||
void rtc_ds1307_setup(void);
|
||||
/** verify if oscillator is disabled
|
||||
* @return if oscillator is disabled (or if communication error occurred)
|
||||
*/
|
||||
bool rtc_ds1307_oscillator_disabled(void);
|
||||
/** read square wave output frequency (in Hz)
|
||||
* @return square wave output frequency in Hz, 0 if disabled (0xffff if communication error occurred)
|
||||
*/
|
||||
uint16_t rtc_ds1307_read_square_wave(void);
|
||||
/** read seconds from RTC IC
|
||||
* @return number of seconds (0-59) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_seconds(void);
|
||||
/** read minutes from RTC IC
|
||||
* @return number of minutes (0-59) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_minutes(void);
|
||||
/** read hours from RTC IC
|
||||
* @return number of hours (0-23) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_hours(void);
|
||||
/** read day from RTC IC
|
||||
* @return day of the week (1-7, 1 is Sunday) of the current time, 1 being Sunday (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_day(void);
|
||||
/** read date from RTC IC
|
||||
* @return day of the month (1-31) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_date(void);
|
||||
/** read month from RTC IC
|
||||
* @return month of the year (1-12) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_month(void);
|
||||
/** read year from RTC IC
|
||||
* @return year of the century (00-99) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_year(void);
|
||||
/** read time from RTC IC
|
||||
* @return array of {seconds, minutes, hours, day, date, month, year} as defined above (NULL if communication error occurred)
|
||||
*/
|
||||
uint8_t* rtc_ds1307_read_time(void);
|
||||
/** read user RAM from RTC IC
|
||||
* @param[out] data array to store the RAM read
|
||||
* @param[in] start start of the user RAM to read (0-55)
|
||||
* @param[in] length number of user RAM bytes to read (0-55)
|
||||
* @return if read succeeded
|
||||
*/
|
||||
bool rtc_ds1307_read_ram(uint8_t* data, uint8_t start, uint8_t length);
|
||||
/** disable RTC IC oscillator
|
||||
* @return if disabling oscillator succeeded
|
||||
*/
|
||||
bool rtc_ds1307_oscillator_disable(void);
|
||||
/** enable RTC IC oscillator
|
||||
* @return if enabling oscillator succeeded
|
||||
*/
|
||||
bool rtc_ds1307_oscillator_enable(void);
|
||||
/** write square wave output frequency (in Hz)
|
||||
* @param[in] frequency square wave output frequency in Hz (0 to disable, 1, 4096, 8192, 32768)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_square_wave(uint16_t frequency);
|
||||
/** write seconds into RTC IC
|
||||
* @param[in] seconds number of seconds (0-59)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_seconds(uint8_t seconds);
|
||||
/** write minutes into RTC IC
|
||||
* @param[in] minutes number of minutes (0-59)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_minutes(uint8_t minutes);
|
||||
/** write hours into RTC IC
|
||||
* @param[in] hours number of hours (0-23)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_hours(uint8_t hours);
|
||||
/** write day into RTC IC
|
||||
* @param[in] day day of the week (1-7, 1 is Sunday)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_day(uint8_t day);
|
||||
/** write date into RTC IC
|
||||
* @param[in] date day of the month (1-31)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_date(uint8_t date);
|
||||
/** write month into RTC IC
|
||||
* @param[in] month month of the year (1-12)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_month(uint8_t month);
|
||||
/** write year into RTC IC
|
||||
* @param[in] year year of the century (00-99)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_year(uint8_t year);
|
||||
/** write time into RTC IC
|
||||
* @param[in] seconds number of seconds (0-59)
|
||||
* @param[in] minutes number of minutes (0-59)
|
||||
* @param[in] hours number of hours (0-23)
|
||||
* @param[in] day day of the week (1-7, 1 is Sunday)
|
||||
* @param[in] date day of the month (1-31)
|
||||
* @param[in] month month of the year (1-12)
|
||||
* @param[in] year year of the century (00-99)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year);
|
||||
/** write user RAM from RTC IC
|
||||
* @param[in] data array of byte to write in RAM
|
||||
* @param[in] start start of the user RAM to write (0-55)
|
||||
* @param[in] length number of user RAM bytes to write (0-55)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_ds1307_write_ram(uint8_t* data, uint8_t start, uint8_t length);
|
||||
|
|
@ -1,196 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library for Maxim DS18B20 digital temperature sensor (using 1-Wire protocol) (code)
|
||||
* @file sensor_ds18b20.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: 1-Wire (timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio)
|
||||
* @warning this library does not support parasite power mode and alarms
|
||||
*/
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdbool.h> // boolean type
|
||||
#include <stdlib.h> // size_t definition
|
||||
#include <math.h> // NAN definition
|
||||
|
||||
/* own libraries */
|
||||
#include "global.h" // help macros
|
||||
#include "onewire_master.h" // 1-Wire utilities
|
||||
#include "sensor_ds18b20.h" // own definitions
|
||||
|
||||
/** remember number of DS18B20 sensors on 1-Wire bus for certain functions */
|
||||
uint64_t sensors = 0;
|
||||
/** remember if only DS18B20 sensors on 1-Wire bus for certain functions */
|
||||
bool only = false;
|
||||
/** remember code of last sensor **/
|
||||
uint64_t last = 0;
|
||||
|
||||
void sensor_ds18b20_setup(void)
|
||||
{
|
||||
onewire_master_setup(); // setup 1-Wire peripheral to communicate with sensors on bus
|
||||
sensor_ds18b20_number(); // scan for sensor (remembers sensor number and exclusivity)
|
||||
}
|
||||
|
||||
uint64_t sensor_ds18b20_number(void)
|
||||
{
|
||||
sensors = 0; // reset number
|
||||
only = true; // reset state
|
||||
uint64_t code = 0; // ROM code found (use 0 to start from scratch)
|
||||
bool more = true; // save if other additional slaves exist
|
||||
|
||||
while (more) { // scan for all 1-Wire slaves
|
||||
if (!onewire_master_reset()) { // send reset to start communication
|
||||
return 0; // no slave presence detected
|
||||
}
|
||||
more = onewire_master_rom_search(&code, false); // get next slave ROM code (without alarm)
|
||||
if (0==code) { // error occurred
|
||||
return 0;
|
||||
}
|
||||
if (0x28==(code&0xff)) { // family code (8-LSb) for DS18B20 sensors is 0x28
|
||||
last = code; // save last found code
|
||||
sensors++; // we found an additional sensor
|
||||
} else {
|
||||
only = false; // we found a slave which is not a sensor
|
||||
}
|
||||
}
|
||||
|
||||
return sensors;
|
||||
}
|
||||
|
||||
bool sensor_ds18b20_only(void)
|
||||
{
|
||||
sensor_ds18b20_number(); // this also checks for exclusivity
|
||||
return only;
|
||||
}
|
||||
|
||||
bool sensor_ds18b20_list(uint64_t* code)
|
||||
{
|
||||
if (!onewire_master_reset()) { // send reset to start communication
|
||||
return false; // no slave presence detected
|
||||
}
|
||||
onewire_master_rom_search(code, false); // get next code
|
||||
return (last!=*code); // verify if the last has been found
|
||||
}
|
||||
|
||||
bool sensor_ds18b20_convert(uint64_t code)
|
||||
{
|
||||
if (0==code && !only) { // asked for broadcast but there are different slaves on bus
|
||||
return false; // broadcast not possible when there are also different slaves on bus
|
||||
}
|
||||
|
||||
// send reset pulse
|
||||
if (!onewire_master_reset()) { // send reset to start communication
|
||||
return false; // no slave presence detected
|
||||
}
|
||||
|
||||
// send ROM command to select slave(s)
|
||||
if (0==code) { // broadcast convert
|
||||
if (!onewire_master_rom_skip()) { // select all slaves
|
||||
return false; // ROM command failed
|
||||
}
|
||||
} else {
|
||||
if (!onewire_master_rom_match(code)) { // select specific slave
|
||||
return false; // ROM command failed
|
||||
}
|
||||
}
|
||||
|
||||
// send convert T function command
|
||||
return onewire_master_function_read(0x44, NULL, 0);
|
||||
}
|
||||
|
||||
float sensor_ds18b20_temperature(uint64_t code)
|
||||
{
|
||||
if (0==code && (sensors>1 || !only)) { // broadcast read requested
|
||||
return NAN; // this function is not possible when several sensors or other devices are present
|
||||
}
|
||||
|
||||
// send reset pulse
|
||||
if (!onewire_master_reset()) { // send reset to start communication
|
||||
return NAN; // no slave presence detected
|
||||
}
|
||||
|
||||
// send ROM command to select slave
|
||||
if (!onewire_master_rom_match(code)) { // select specific slave
|
||||
return NAN; // ROM command failed
|
||||
}
|
||||
|
||||
// read scratchpad to get temperature (on byte 0 and 1)
|
||||
uint8_t scratchpad[9] = {0}; // to store read scratchpad
|
||||
if (!onewire_master_function_read(0xbe, scratchpad, sizeof(scratchpad)*8)) { // read complete scratchpad
|
||||
return NAN; // error occurred during read
|
||||
}
|
||||
|
||||
// verify if data is valid
|
||||
if (onewire_master_crc(scratchpad, sizeof(scratchpad))) { // check CRC checksum
|
||||
return NAN; // data corrupted
|
||||
}
|
||||
|
||||
// calculate temperature (stored as int16_t but on 0.125 C steps)
|
||||
return ((int16_t)(scratchpad[1]<<8)+scratchpad[0])/16.0; // get temperature (on < 16 precision the last bits are undefined, but that doesn't matter for the end result since the lower precision is still provided)
|
||||
}
|
||||
|
||||
bool sensor_ds18b20_precision(uint64_t code, uint8_t precision)
|
||||
{
|
||||
if (precision<9 || precision>12) { // check input
|
||||
return false; // wrong precision value
|
||||
}
|
||||
|
||||
if (0==code && !only) { // asked for broadcast but there are different slaves on bus
|
||||
return false; // broadcast not possible when there are also different slaves on bus
|
||||
}
|
||||
|
||||
// send reset pulse
|
||||
if (!onewire_master_reset()) { // send reset to start communication
|
||||
return false; // no slave presence detected
|
||||
}
|
||||
|
||||
// send ROM command to select slave(s)
|
||||
if (0==code) { // broadcast convert
|
||||
if (!onewire_master_rom_skip()) { // select all slaves
|
||||
return false; // ROM command failed
|
||||
}
|
||||
} else {
|
||||
if (!onewire_master_rom_match(code)) { // select specific slave
|
||||
return false; // ROM command failed
|
||||
}
|
||||
}
|
||||
|
||||
// read scratchpad to get alarm values (on byte 2 and 3)
|
||||
uint8_t scratchpad[9] = {0}; // to store read scratchpad
|
||||
if (!onewire_master_function_read(0xbe, scratchpad, sizeof(scratchpad)*8)) { // read complete scratchpad
|
||||
return false; // error occurred during read
|
||||
}
|
||||
|
||||
// verify if data is valid
|
||||
if (onewire_master_crc(scratchpad, sizeof(scratchpad))) { // check CRC checksum
|
||||
return false; // data corrupted
|
||||
}
|
||||
|
||||
// send new configuration (and keep the alarm values)
|
||||
uint8_t configuration[3] = {0}; // to store T_HIGH, T_LOW, and configuration
|
||||
configuration[0] = scratchpad[2]; // keep T_HIGH
|
||||
configuration[1] = scratchpad[3]; // keep T_LOW
|
||||
configuration[2] = 0x1f+((precision-9)<<5); // set precision bit (R1-R0 on bit 6-5)
|
||||
if (!onewire_master_function_write(0x4e, configuration, sizeof(configuration)*8)) { // write scratchpad with new configuration (all three bytes must be written)
|
||||
return false; // error occurred during write
|
||||
}
|
||||
|
||||
// store new configuration into sensor's EEPROM for retrieval on next power up
|
||||
if (!onewire_master_function_read(0x48, NULL, 0)) { // copy scratchpad (to EEPROM)
|
||||
return false; // error during copy
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library for Maxim DS18B20 digital temperature sensor (using 1-Wire protocol) (API)
|
||||
* @file sensor_ds18b20.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: 1-Wire (timer @ref onewire_master_timer, GPIO @ref onewire_master_gpio)
|
||||
* @warning this library does not support parasite power mode and alarms
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** setup 1-Wire peripheral to communicate with sensors on bus */
|
||||
void sensor_ds18b20_setup(void);
|
||||
/** get number of DS18B20 sensors on bus
|
||||
* @return number of DS18B20 sensors on bus
|
||||
*/
|
||||
uint64_t sensor_ds18b20_number(void);
|
||||
/** verify if only DS18B20 sensors are on the bus
|
||||
* @return if only DS18B20 sensors are on the bus
|
||||
*/
|
||||
bool sensor_ds18b20_only(void);
|
||||
/** send all DS18B20 slaves on the bus
|
||||
* @param[out] code ROM code for sensor (0 if error occurred)
|
||||
* @return if an additional sensors have been detected
|
||||
*/
|
||||
bool sensor_ds18b20_list(uint64_t* code);
|
||||
/** start converting (e.g. measuring) temperature
|
||||
* @warning conversion time to wait before reading temperature depends on the resolution set (9 bits: 93.75ms, 10 bits: 187.5ms, 11 bits: 375ms, 12 bits: 950ms)
|
||||
* @param[in] code ROM code of sensor to start conversion on (0 for all, if only DS18B20 sensors are on the bus)
|
||||
* @return if conversion started
|
||||
*/
|
||||
bool sensor_ds18b20_convert(uint64_t code);
|
||||
/** get converted temperature
|
||||
* @note 85.0 C is the default temperature when no conversion has been performed
|
||||
* @param[in] code ROM code of sensor
|
||||
* @return temperature (NaN if error)
|
||||
*/
|
||||
float sensor_ds18b20_temperature(uint64_t code);
|
||||
/** set conversion precision
|
||||
* @param[in] code ROM code of sensor to start conversion on (0 for all, if only DS18B20 sensors are on the bus)
|
||||
* @param[in] precision precision in bits (9-12)
|
||||
* @return if operation succeeded
|
||||
*/
|
||||
bool sensor_ds18b20_precision(uint64_t code, uint8_t precision);
|
|
@ -1,214 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from peacefair PZEM-004 and PZEM-004T electricity meter (code)
|
||||
* @file sensor_pzem.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref sensor_pzem_usart, timer @ref sensor_pzem_timer
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
|
||||
#include <libopencm3/stm32/timer.h> // timer utilities
|
||||
|
||||
/* own libraries */
|
||||
#include "sensor_pzem.h" // PZEM electricity meter header and definitions
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup sensor_pzem_usart USART peripheral used for communication with electricity meter
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_PZEM_USART 2 /**< USART peripheral */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup sensor_pzem_timer timer peripheral used for waiting before sending the next request
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_PZEM_TIMER 2 /**< timer peripheral */
|
||||
/** @} */
|
||||
|
||||
/* input and output ring buffer, indexes, and available memory */
|
||||
static uint8_t rx_buffer[7] = {0}; /**< buffer for received response */
|
||||
static volatile uint8_t rx_i = 0; /**< current position of read received data */
|
||||
static uint8_t tx_buffer[7] = {0}; /**< buffer for request to transmit */
|
||||
static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
|
||||
|
||||
volatile bool sensor_pzem_measurement_received = false;
|
||||
|
||||
void sensor_pzem_setup(void)
|
||||
{
|
||||
/* enable USART I/O peripheral */
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
|
||||
rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_PZEM_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(USART_RCC(SENSOR_PZEM_USART)); // enable clock for USART peripheral
|
||||
gpio_set_mode(USART_PORT(SENSOR_PZEM_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_PZEM_USART)); // setup GPIO pin USART transmit
|
||||
gpio_set_mode(USART_PORT(SENSOR_PZEM_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_PZEM_USART)); // setup GPIO pin USART receive
|
||||
gpio_set(USART_PORT(SENSOR_PZEM_USART), USART_PIN_RX(SENSOR_PZEM_USART)); // pull up to avoid noise when not connected
|
||||
|
||||
/* setup USART parameters for electricity meter: 9600 8N1 */
|
||||
usart_set_baudrate(USART(SENSOR_PZEM_USART), 9600); // the electricity meter uses a fixed baud rate of 9600 bps
|
||||
usart_set_databits(USART(SENSOR_PZEM_USART), 8);
|
||||
usart_set_stopbits(USART(SENSOR_PZEM_USART), USART_STOPBITS_1);
|
||||
usart_set_mode(USART(SENSOR_PZEM_USART), USART_MODE_TX_RX);
|
||||
usart_set_parity(USART(SENSOR_PZEM_USART), USART_PARITY_NONE);
|
||||
usart_set_flow_control(USART(SENSOR_PZEM_USART), USART_FLOWCONTROL_NONE);
|
||||
|
||||
nvic_enable_irq(USART_IRQ(SENSOR_PZEM_USART)); // enable the USART interrupt
|
||||
usart_enable_rx_interrupt(USART(SENSOR_PZEM_USART)); // enable receive interrupt
|
||||
usart_enable(USART(SENSOR_PZEM_USART)); // enable USART
|
||||
|
||||
// setup timer to wait for minimal time before next transmission (after previous transmission or reception)
|
||||
rcc_periph_clock_enable(RCC_TIM(SENSOR_PZEM_TIMER)); // enable clock for timer block
|
||||
timer_reset(TIM(SENSOR_PZEM_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(SENSOR_PZEM_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
|
||||
timer_one_shot_mode(TIM(SENSOR_PZEM_TIMER)); // stop counter after update event (we only need to count down once)
|
||||
timer_set_prescaler(TIM(SENSOR_PZEM_TIMER), 550-1); // set the prescaler so this 16 bits timer allows to wait for maximum 500 ms ( 1/(72E6/550/(2**16))=500.62ms )
|
||||
timer_set_period(TIM(SENSOR_PZEM_TIMER), 0xffff/2); // the timing is not defined in the specification. I tested until the communication was reliable (all requests get an response)
|
||||
timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_irq(TIM(SENSOR_PZEM_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_PZEM_TIMER)); // catch interrupt in service routine
|
||||
|
||||
/* reset buffer states */
|
||||
tx_i = LENGTH(tx_buffer);
|
||||
rx_i = 0;
|
||||
sensor_pzem_measurement_received = false;
|
||||
}
|
||||
|
||||
void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurement_type_t type)
|
||||
{
|
||||
if (tx_i<LENGTH(tx_buffer)) { // transmission is ongoing
|
||||
return;
|
||||
}
|
||||
if (type>=SENSOR_PZEM_MAX) { // invalid type
|
||||
return;
|
||||
}
|
||||
tx_buffer[0] = 0xB0+type; // set request nibble and type nibble
|
||||
tx_buffer[1] = (address>>24)&0xff; // set address
|
||||
tx_buffer[2] = (address>>16)&0xff; // set address
|
||||
tx_buffer[3] = (address>>8)&0xff; // set address
|
||||
tx_buffer[4] = (address>>0)&0xff; // set address
|
||||
tx_buffer[5] = 0; // only used to set alarm
|
||||
tx_buffer[6] = 0; // to calculate checksum (sum of all previous bytes)
|
||||
for (uint8_t i=0; i<LENGTH(tx_buffer)-1; i++) {
|
||||
tx_buffer[6] += tx_buffer[i]; // calculate buffer
|
||||
}
|
||||
tx_i = 0; // remember we have a message to send
|
||||
|
||||
if (TIM_CR1(TIM(SENSOR_PZEM_TIMER))&TIM_CR1_CEN) { // timer is already running
|
||||
// at the end of the timer the transmission will start automatically
|
||||
} else { // no timer is running
|
||||
usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
|
||||
//usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // start transmission
|
||||
}
|
||||
|
||||
sensor_pzem_measurement_received = false; // reset flag
|
||||
rx_i = 0; // prepare buffer to receive next measurement
|
||||
}
|
||||
|
||||
struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void)
|
||||
{
|
||||
struct sensor_pzem_measurement_t measurement; // decoded measurement to return
|
||||
measurement.valid = false; // wait until the end to ensure validity
|
||||
if (rx_i<LENGTH(rx_buffer)) { // buffer is not full, thus no measurement received
|
||||
return measurement;
|
||||
}
|
||||
if ((rx_buffer[0]&0xf0)!=0xa0) { // not a response received
|
||||
return measurement;
|
||||
}
|
||||
if ((rx_buffer[0]&0x0f)>=SENSOR_PZEM_MAX) { // not a valid response type received (actually 4 and 5 are valid, but should not happen when using this code
|
||||
return measurement;
|
||||
}
|
||||
uint8_t checksum = 0; // calculate checksum (sum of all other bytes)
|
||||
for (uint8_t i=0; i<LENGTH(rx_buffer)-1; i++) {
|
||||
checksum += rx_buffer[i]; // calculate buffer
|
||||
}
|
||||
if (checksum!=rx_buffer[6]) { // checksum does not match
|
||||
return measurement;
|
||||
}
|
||||
measurement.valid = true; // all checks passed
|
||||
measurement.type = rx_buffer[0]&0x0f; // save type
|
||||
switch (measurement.type) { // decode value depending on type
|
||||
case SENSOR_PZEM_VOLTAGE:
|
||||
measurement.value.voltage = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2]+rx_buffer[3]*0.1;
|
||||
break;
|
||||
case SENSOR_PZEM_CURRENT:
|
||||
measurement.value.current = rx_buffer[2]+rx_buffer[3]*0.01;
|
||||
break;
|
||||
case SENSOR_PZEM_POWER:
|
||||
measurement.value.power = ((uint16_t)rx_buffer[1]<<8)+rx_buffer[2];
|
||||
break;
|
||||
case SENSOR_PZEM_ENERGY:
|
||||
measurement.value.energy = ((uint32_t)rx_buffer[1]<<16)+((uint16_t)rx_buffer[2]<<8)+rx_buffer[3];
|
||||
break;
|
||||
/* not used in this application
|
||||
case SENSOR_PZEM_ADDRESS:
|
||||
case SENSOR_PZEM_ALARM:
|
||||
break; // no value is returned
|
||||
*/
|
||||
default:
|
||||
measurement.valid = false; // unexpected type
|
||||
}
|
||||
sensor_pzem_measurement_received = false; // reset flag
|
||||
rx_i = 0; // prepare buffer to receive next measurement
|
||||
return measurement;
|
||||
}
|
||||
|
||||
/** USART interrupt service routine called when data has been transmitted or received */
|
||||
void USART_ISR(SENSOR_PZEM_USART)(void)
|
||||
{
|
||||
if (usart_get_interrupt_source(USART(SENSOR_PZEM_USART), USART_SR_TXE)) { // data has been transmitted
|
||||
if (tx_i<LENGTH(tx_buffer)) { // not all bytes transmitted
|
||||
usart_send(USART(SENSOR_PZEM_USART),tx_buffer[tx_i++]); // transmit next byte
|
||||
} else { // request transmitted
|
||||
usart_disable_tx_interrupt(USART(SENSOR_PZEM_USART)); // disable transmit interrupt
|
||||
timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
|
||||
|
||||
}
|
||||
}
|
||||
if (usart_get_interrupt_source(USART(SENSOR_PZEM_USART), USART_SR_RXNE)) { // data has been received
|
||||
if (rx_i<LENGTH(rx_buffer)) { // receiving response
|
||||
rx_buffer[rx_i++] = usart_recv(USART(SENSOR_PZEM_USART)); // put received byte in buffer
|
||||
if (rx_i>=LENGTH(rx_buffer)) { // buffer full
|
||||
sensor_pzem_measurement_received = true; // notify used response has been received
|
||||
}
|
||||
} else { // previous response not read before receiving the next
|
||||
usart_recv(USART(SENSOR_PZEM_USART)); // drop received buffer
|
||||
}
|
||||
timer_set_counter(TIM(SENSOR_PZEM_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_PZEM_TIMER)); // start timer between requests
|
||||
}
|
||||
}
|
||||
|
||||
/** interrupt service routine called on timeout */
|
||||
void TIM_ISR(SENSOR_PZEM_TIMER)(void)
|
||||
{
|
||||
if (timer_get_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF)) { // update event happened
|
||||
timer_clear_flag(TIM(SENSOR_PZEM_TIMER), TIM_SR_UIF); // clear flag
|
||||
if (tx_i<LENGTH(tx_buffer)) { // bytes are waiting to be sent
|
||||
usart_enable_tx_interrupt(USART(SENSOR_PZEM_USART)); // enable interrupt to start sending bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from peacefair PZEM-004 and PZEM-004T electricity meter (API)
|
||||
* @file sensor_pzem.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref sensor_pzem_usart, timer @ref sensor_pzem_timer
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** a measurement response has been received */
|
||||
extern volatile bool sensor_pzem_measurement_received;
|
||||
|
||||
/** measurements (and configurations) offered by electricity meter */
|
||||
enum sensor_pzem_measurement_type_t {
|
||||
SENSOR_PZEM_VOLTAGE = 0,
|
||||
SENSOR_PZEM_CURRENT = 1,
|
||||
SENSOR_PZEM_POWER = 2,
|
||||
SENSOR_PZEM_ENERGY = 3,
|
||||
// SENSOR_PZEM_ADDRESS = 4, // this is a setting, not a measurement
|
||||
// SENSOR_PZEM_ALARM = 5, // this is a setting, not a measurement
|
||||
SENSOR_PZEM_MAX
|
||||
};
|
||||
|
||||
/** measurement returned by electricity meter */
|
||||
struct sensor_pzem_measurement_t {
|
||||
enum sensor_pzem_measurement_type_t type; /**< measurement type */
|
||||
bool valid; /**< is the measurement valid (e.g. format and checksum are correct) */
|
||||
/** possible measurement values */
|
||||
union measurement_t {
|
||||
float voltage; /**< measured voltage in volts */
|
||||
float current; /**< measured current in amperes */
|
||||
uint16_t power; /**< measured power in watts */
|
||||
uint32_t energy; /**< measured energy in watts/hour (24 bits) */
|
||||
} value; /**< measurement value */
|
||||
};
|
||||
|
||||
/** setup peripherals to communicate with electricity meter */
|
||||
void sensor_pzem_setup(void);
|
||||
/** request measurement from electricity meter
|
||||
* @param[in] address electricity meter device address
|
||||
* @param[in] type measurement type to request
|
||||
*/
|
||||
void sensor_pzem_measurement_request(uint32_t address, enum sensor_pzem_measurement_type_t type);
|
||||
/** decode received measurement
|
||||
* @return decoded measurement (invalid if no new measurement has been received)
|
||||
*/
|
||||
struct sensor_pzem_measurement_t sensor_pzem_measurement_decode(void);
|
|
@ -1,371 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter (code)
|
||||
* @file sensor_sdm120.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref sensor_sdm120_usart , GPIO @ref sensor_sdm120_gpio , timer @ref sensor_sdm120_timer
|
||||
*/
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
#include <math.h> // mathematical utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
|
||||
#include <libopencm3/stm32/timer.h> // timer utilities
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
|
||||
#include "sensor_sdm120.h" // SDM120 electricity meter header and definitions
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup sensor_sdm120_usart USART peripheral used for communication with electricity meter
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_SDM120_USART 3 /**< USART peripheral */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup sensor_sdm120_gpio GPIO peripheral used for controlling RS-485 adapter
|
||||
* @note driver output is enabled on high while receiver output is enabled on low, thus one pin can be used to control both
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_SDM120_REDE_PORT B /**< GPIO port for RS-485 receiver and driver output enable signal */
|
||||
#define SENSOR_SDM120_REDE_PIN 12 /**< GPIO pin for RS-485 receiver and driver output enable signal */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup sensor_sdm120_timer timer peripheral to enforce waiting time between messages
|
||||
* @note 60 ms are recommended between messages in SDM630 ModBus protocol implementation and this seem to also apply to SDM120
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_SDM120_TIMER 3 /**< timer number to count time */
|
||||
/** @} */
|
||||
|
||||
/* input and output ring buffer, indexes, and available memory */
|
||||
static uint8_t rx_buffer[9] = {0}; /**< buffer for received response (ModBus response messages can be 2+256+2 long but we will only read up to 2 registers) */
|
||||
static volatile uint8_t rx_used = 0; /**< number of received data bytes in buffer */
|
||||
static uint8_t tx_buffer[13] = {0}; /**< buffer for request to transmit (ModBus request messages can be 7+256+2 long but we will only write up to 2 registers */
|
||||
static volatile uint8_t tx_used = 0; /**< number of byte to transmit */
|
||||
|
||||
volatile bool sensor_sdm120_measurement_received = false;
|
||||
|
||||
/** the ModBus timeouts to respect for sending messages **/
|
||||
static enum timeout_t {
|
||||
TIMEOUT_BEGIN = 0, /**< silent time before sending data */
|
||||
TIMEOUT_END, /**< silent time after sending data */
|
||||
TIMEOUT_BETWEEN, /**< time to wait between messages */
|
||||
TIMEOUT_MAX /**< last element (useful to no the number of elements) */
|
||||
} timeout; /**< the current timeout used */
|
||||
/** current timeout used */
|
||||
static uint16_t timeout_times[TIMEOUT_MAX] = {0};
|
||||
|
||||
/** SDM120 3xxxx input register start addresses for the measurement types */
|
||||
static const uint16_t register_input[] = {
|
||||
0x0000, // 30001 voltage (in volts)
|
||||
0x0006, // 30007 current (in amperes)
|
||||
0x000c, // 30013 active power (in watts)
|
||||
0x0012, // 30019 apparent power (in volt amperes)
|
||||
0x0018, // 30025 reactive power (in volt amperes reactive)
|
||||
0x001e, // 30031 power factor (0-1)
|
||||
0x0046, // 30071 frequency (in hertz)
|
||||
0x0048, // 30073 import active energy (in kWh)
|
||||
0x004a, // 30075 export active energy (in kWh)
|
||||
0x004c, // 30077 import reactive energy (in kVArh)
|
||||
0x004e, // 30079 export reactive energy (in kVArh)
|
||||
0x0156, // 30343 total active energy (in kWh)
|
||||
0x0158 // 30345 total reactive energy (in kVArh)
|
||||
};
|
||||
|
||||
/** SDM120 4xxxx holding register start addresses for the configuration types */
|
||||
static const uint16_t register_holding[] = {
|
||||
0x000c, // relay pulse width (60, 100, or 200 ms)
|
||||
0x0012, // network parity stop (0: 1 stop bit no parity, 1: one stop bit even parity, 2: one stop bit odd parity, 3: two stop bits no parity)
|
||||
0x0014, // meter slave address (1-247)
|
||||
0x001c, // baud rate (0: 2400 bps, 1: 4800 bps, 2: 9600 bps, 5: 1200 bps)
|
||||
0x0056, // pulse 1 output mode (1: import active energy, 2: import+export active energy, 4: export active energy, 5: import reactive energy, 6: import+export reactive energy, 8: export reactive energy)
|
||||
0xf900, // time of scroll display (0-30 s)
|
||||
0xf910, // pulse 1 output (0: 0.001 kWh/imp, 1: 0.01 kWh/imp, 2: 0.1 kWh/imp, 3: 1 kWh/imp)
|
||||
0xf920 // measurement mode (1: total=import, 2: total=import+export, 3: total=import-export)
|
||||
};
|
||||
|
||||
/** compute CRC for ModBus
|
||||
* @note ModBus uses ANSi/IBM 16-bits CRC (with normal polynomial 0x8005, reverse polynomial 0xA001, start value 0xfff)
|
||||
* @param[in] buffer data on which to compute the CRC for
|
||||
* @param[in] size number of byte to compute the CRC for
|
||||
* @return computed CRC checksum
|
||||
*/
|
||||
static uint16_t crc_modbus(uint8_t* buffer, uint8_t size)
|
||||
{
|
||||
uint16_t crc = 0xffff; // initial value (for ModBus)
|
||||
for (uint8_t i=0; i<size; i++) { // go through every byte
|
||||
crc ^= (uint16_t)buffer[i]; // XOR byte
|
||||
for (uint8_t b=0; b<8; b++) { // go through every bit
|
||||
if (crc&0x0001) { // least significant bit is set (we are using the reverse way)
|
||||
crc = (crc>>1)^0xA001; // // shift to the right (for the next bit) and XOR with (reverse) polynomial
|
||||
} else {
|
||||
crc >>= 1; // just shift right (for the next bit)
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
void sensor_sdm120_setup(uint32_t baudrate)
|
||||
{
|
||||
// enable USART I/O peripheral
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
|
||||
rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_SDM120_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(USART_RCC(SENSOR_SDM120_USART)); // enable clock for USART peripheral
|
||||
gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_SDM120_USART)); // setup GPIO pin USART transmit
|
||||
gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_SDM120_USART)); // setup GPIO pin USART receive
|
||||
gpio_clear(USART_PORT(SENSOR_SDM120_USART), USART_PIN_RX(SENSOR_SDM120_USART)); // pull down to avoid noise when not connected (it will be set low by RS485 chip when RO is enabled)
|
||||
|
||||
// setup USART parameters for electricity meter
|
||||
usart_set_baudrate(USART(SENSOR_SDM120_USART), baudrate); // get baud rate by scrolling through the measurements on the electricity meter's screen (default 2400)
|
||||
usart_set_databits(USART(SENSOR_SDM120_USART), 8);
|
||||
usart_set_stopbits(USART(SENSOR_SDM120_USART), USART_STOPBITS_1);
|
||||
usart_set_mode(USART(SENSOR_SDM120_USART), USART_MODE_TX_RX);
|
||||
usart_set_parity(USART(SENSOR_SDM120_USART), USART_PARITY_NONE); // get parity by scrolling through the measurements on the electricity meter's screen (default none)
|
||||
usart_set_flow_control(USART(SENSOR_SDM120_USART), USART_FLOWCONTROL_NONE);
|
||||
|
||||
nvic_enable_irq(USART_IRQ(SENSOR_SDM120_USART)); // enable the USART interrupt
|
||||
usart_enable_rx_interrupt(USART(SENSOR_SDM120_USART)); // enable receive interrupt
|
||||
usart_enable(USART(SENSOR_SDM120_USART)); // enable USART
|
||||
|
||||
// setup GPIO
|
||||
rcc_periph_clock_enable(RCC_GPIO(SENSOR_SDM120_REDE_PORT)); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(GPIO(SENSOR_SDM120_REDE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(SENSOR_SDM120_REDE_PIN)); // setup GPIO pin for receiver and driver output enable pin
|
||||
gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
|
||||
|
||||
// setup timer to wait for minimal time before next transmission
|
||||
rcc_periph_clock_enable(RCC_TIM(SENSOR_SDM120_TIMER)); // enable clock for timer block
|
||||
timer_reset(TIM(SENSOR_SDM120_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(SENSOR_SDM120_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
|
||||
timer_one_shot_mode(TIM(SENSOR_SDM120_TIMER)); // stop counter after update event (we only need to count down once)
|
||||
timer_set_prescaler(TIM(SENSOR_SDM120_TIMER), 66-1); // set the prescaler so this 16 bits timer allows to wait for 60 ms ( 1/(72E6/66/(2**16))=60.07ms )
|
||||
timeout_times[TIMEOUT_BEGIN] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters before sending data
|
||||
timeout_times[TIMEOUT_END] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters after sending data
|
||||
timeout_times[TIMEOUT_BETWEEN] = 0.06*(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1)); // wait at least 60 ms before sending the next message
|
||||
timer_clear_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF); // clear flag
|
||||
timer_enable_irq(TIM(SENSOR_SDM120_TIMER), TIM_DIER_UIE); // enable update interrupt for timer
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_SDM120_TIMER)); // catch interrupt in service routine
|
||||
|
||||
// reset states
|
||||
tx_used = 0;
|
||||
rx_used = 0;
|
||||
sensor_sdm120_measurement_received = false;
|
||||
}
|
||||
|
||||
/** send request to electricity meter
|
||||
* @param[in] meter_id electricity meter device id (ModBus salve address)
|
||||
* @param[in] function ModBus function: 0x03 read two 16 bits holding registers, 0x04 read two 16 bits input registers, 0x10 write two 16 bits holding registers
|
||||
* @param[in] address register start point address
|
||||
* @param[in] value value to store in holding register (if function 0x10 is used)
|
||||
* @return if request is correct and transmission started
|
||||
*/
|
||||
static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, uint16_t address, float value)
|
||||
{
|
||||
if (meter_id==0) { // broadcast request are not supported
|
||||
return false;
|
||||
}
|
||||
if (function!=0x03 && function!=0x04 && function!=0x10) { // function not supported
|
||||
return false;
|
||||
}
|
||||
if (address%2) { // even register addresses are not supported by device
|
||||
return false;
|
||||
}
|
||||
while (tx_used) { // transmission is ongoing
|
||||
__WFI(); // wait until something happens (transmission ended)
|
||||
}
|
||||
// build request packet
|
||||
uint8_t packet[11]; // buffer to build ModBus message (without error check)
|
||||
uint8_t packet_size = 0; // ModBus message size (without error check)
|
||||
packet[0] = meter_id; // set slave device address
|
||||
packet[1] = function; // set function
|
||||
packet[2] = address>>8; // set high register address
|
||||
packet[3] = address; // set low register address
|
||||
packet[4] = 0; // set high number of registers to read
|
||||
packet[5] = 2; // set low number of register to read (the measurement are encoded using 32 bits IEE745 float, and register hold 16 bits, thus we want to read 2 registers
|
||||
if (function==0x03 || function==0x04) { // read register
|
||||
packet_size = 6; // set message size
|
||||
} else if (function==0x10) { // write register
|
||||
packet[6] = 4; // byte count (writing two 16 bits registers)
|
||||
// store little endian encoded value in big endian encoded data
|
||||
uint8_t* data = (uint8_t*)&value;
|
||||
packet[7] = data[3];
|
||||
packet[8] = data[2];
|
||||
packet[9] = data[1];
|
||||
packet[10] = data[0];
|
||||
packet_size = 11; // set message size
|
||||
}
|
||||
uint16_t crc = crc_modbus(packet, packet_size); // compute error check
|
||||
for (uint8_t i=0; i<packet_size; i++) {
|
||||
tx_buffer[packet_size-i+1] = packet[i]; // copy packet to tx buffer in reverse order (this is how sending is implemented)
|
||||
}
|
||||
tx_buffer[1] = crc; // set low error check
|
||||
tx_buffer[0] = crc>>8; // set high error check
|
||||
tx_used = packet_size+2; // set request size
|
||||
rx_used = 0; // reset reset buffer
|
||||
sensor_sdm120_measurement_received = false; // reset measurement flag
|
||||
while (TIM_CR1(TIM(SENSOR_SDM120_TIMER))&TIM_CR1_CEN) { // timer is already used
|
||||
__WFI(); // wait until something happens (timer is available again)
|
||||
}
|
||||
gpio_set(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // enable driver output and disable receive output
|
||||
// start timeout
|
||||
timeout = TIMEOUT_BEGIN; // select time before sending message
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sensor_sdm120_measurement_request(uint8_t meter_id, enum sensor_sdm120_measurement_type_t type)
|
||||
{
|
||||
if (type>=SENSOR_SDM120_MEASUREMENT_MAX) { // invalid type
|
||||
return false;
|
||||
}
|
||||
return sensor_sdm120_transmit_request(meter_id, 0x04, register_input[type], 0);
|
||||
}
|
||||
|
||||
bool sensor_sdm120_configuration_request(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type)
|
||||
{
|
||||
if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type
|
||||
return false;
|
||||
}
|
||||
return sensor_sdm120_transmit_request(meter_id, 0x03, register_holding[type], 0);
|
||||
}
|
||||
|
||||
bool sensor_sdm120_configuration_set(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type, float value)
|
||||
{
|
||||
if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type
|
||||
return false;
|
||||
}
|
||||
return sensor_sdm120_transmit_request(meter_id, 0x10, register_holding[type], value);
|
||||
}
|
||||
|
||||
float sensor_sdm120_measurement_decode(void)
|
||||
{
|
||||
float measurement = NAN; // decoded measurement to return (invalid in the beginning)
|
||||
if (!sensor_sdm120_measurement_received) { // no measurement received
|
||||
return NAN;
|
||||
} else {
|
||||
sensor_sdm120_measurement_received = false; // reset flag
|
||||
}
|
||||
if (rx_used<5) { // not a complete response (minimum is address, function, size/error, error check low, error check high)
|
||||
return NAN;
|
||||
}
|
||||
// a complete message has been received
|
||||
if (crc_modbus(rx_buffer,rx_used)) { // checksum error, error check failed
|
||||
measurement = NAN;
|
||||
} else if (rx_buffer[1]&0x80) { // error condition received
|
||||
measurement = INFINITY; // indicate we received and error
|
||||
} else {
|
||||
switch (rx_buffer[1]) {
|
||||
case 0x03: // read 4xxx holding register response received
|
||||
case 0x04: // read 3xxxx input register response received
|
||||
if (rx_buffer[2]==0x04 && rx_used>=(4+5)) { // 2 registers received, corresponds to implemented request
|
||||
// convert big endian received float value to little endian return value
|
||||
uint8_t* convert = (uint8_t*)&measurement;
|
||||
convert[0] = rx_buffer[6];
|
||||
convert[1] = rx_buffer[5];
|
||||
convert[2] = rx_buffer[4];
|
||||
convert[3] = rx_buffer[3];
|
||||
}
|
||||
break;
|
||||
case 0x10: // write 4xxx holding register response received
|
||||
measurement = (rx_buffer[4]<<8)+rx_buffer[5]; // number of registers written
|
||||
break; // not supported currently
|
||||
default: // unknown function response received
|
||||
measurement = INFINITY;
|
||||
break; // nothing to do
|
||||
}
|
||||
}
|
||||
rx_used = 0; // reset rx_buffer usage
|
||||
return measurement;
|
||||
}
|
||||
|
||||
/** USART interrupt service routine called when data has been transmitted or received */
|
||||
void USART_ISR(SENSOR_SDM120_USART)(void)
|
||||
{
|
||||
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TXE)) { // data has been transmitted
|
||||
if (tx_used) { // not all bytes transmitted
|
||||
usart_send(USART(SENSOR_SDM120_USART),tx_buffer[--tx_used]); // transmit next byte (clears flag)
|
||||
} else { // all bytes transmitted
|
||||
usart_disable_tx_interrupt(USART(SENSOR_SDM120_USART)); // disable transmit interrupt
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TXE; // clear flag
|
||||
USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // enable transfer complete interrupt
|
||||
}
|
||||
}
|
||||
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_TC)) { // data has been completely transmitted
|
||||
USART_CR1(USART(SENSOR_SDM120_USART)) |= USART_CR1_TCIE; // disable transfer complete interrupt
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_TC; // clear flag
|
||||
timeout = TIMEOUT_END; // select wait time after sending data
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
|
||||
}
|
||||
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_RXNE)) { // data has been received
|
||||
if (gpio_get(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN))) { // not in receiver mode
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag, ignore received data
|
||||
} else if (rx_used<LENGTH(rx_buffer)) { // receiving response
|
||||
rx_buffer[rx_used++] = usart_recv(USART(SENSOR_SDM120_USART)); // put received byte in buffer (clears flag)
|
||||
if (rx_used==1 && rx_buffer[0]==0) { // this is wrong decoding because the signal is going low on idle, which is misinterpreted as start bit (and the 0 broadcast device address is not supported by this device)
|
||||
rx_used = 0; // reset buffer
|
||||
} else if (rx_used>=5 && (rx_buffer[1]&0x80)) { // error condition response received
|
||||
sensor_sdm120_measurement_received = true; // notify used response has been received
|
||||
} else if (rx_used>=5 && (uint8_t)(rx_used-5)>=rx_buffer[2] && (rx_buffer[1]==0x04 || rx_buffer[1]==0x03)) { // read input or holding register response received
|
||||
sensor_sdm120_measurement_received = true; // notify used response has been receive
|
||||
} else if (rx_used>=8 && rx_buffer[1]==0x10) { // write holding register response received
|
||||
sensor_sdm120_measurement_received = true; // notify used response has been receive
|
||||
}
|
||||
} else { // buffer full and unknown response received
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag (wait for user to read measurement, this clears the buffer)
|
||||
}
|
||||
timeout = TIMEOUT_END; // select time after receiving data
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
|
||||
}
|
||||
}
|
||||
|
||||
/** interrupt service routine called on timeout */
|
||||
void TIM_ISR(SENSOR_SDM120_TIMER)(void)
|
||||
{
|
||||
if (timer_get_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF)) { // update event happened
|
||||
timer_clear_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF); // clear flag
|
||||
// because of the one pulse mode the timer is stopped automatically
|
||||
switch (timeout) { // timeout before action passed
|
||||
case (TIMEOUT_BEGIN): // we can now send the data
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= USART_SR_TXE; // clear interrupt flag
|
||||
usart_enable_tx_interrupt(USART(SENSOR_SDM120_USART)); // enable interrupt to send other bytes
|
||||
usart_send(USART(SENSOR_SDM120_USART),tx_buffer[--tx_used]); // start transmission
|
||||
break;
|
||||
case (TIMEOUT_END): // we now have to wait before sending the next message
|
||||
gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output (and enable receive output)
|
||||
timeout = TIMEOUT_BETWEEN; // select time between sending message
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
|
||||
case (TIMEOUT_BETWEEN): // nothing to do, we are allowed to send the next message
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter (API)
|
||||
* @file sensor_sdm120.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref sensor_sdm120_usart , GPIO @ref sensor_sdm120_gpio , timer @ref sensor_sdm120_timer
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** a measurement response has been received */
|
||||
extern volatile bool sensor_sdm120_measurement_received;
|
||||
|
||||
/** measurement types offered by electricity meter in 3xxx input registers */
|
||||
enum sensor_sdm120_measurement_type_t {
|
||||
SENSOR_SDM120_VOLTAGE = 0,
|
||||
SENSOR_SDM120_CURRENT,
|
||||
SENSOR_SDM120_POWER_ACTIVE,
|
||||
SENSOR_SDM120_POWER_APPARENT,
|
||||
SENSOR_SDM120_POWER_REACTIVE,
|
||||
SENSOR_SDM120_POWER_FACTOR,
|
||||
SENSOR_SDM120_FREQUENCY,
|
||||
SENSOR_SDM120_ENERGY_ACTIVE_IMPORT,
|
||||
SENSOR_SDM120_ENERGY_ACTIVE_EXPORT,
|
||||
SENSOR_SDM120_ENERGY_REACTIVE_IMPORT,
|
||||
SENSOR_SDM120_ENERGY_REACTIVE_EXPORT,
|
||||
SENSOR_SDM120_ENERGY_ACTIVE_TOTAL,
|
||||
SENSOR_SDM120_ENERGY_REACTIVE_TOTAL,
|
||||
SENSOR_SDM120_MEASUREMENT_MAX
|
||||
};
|
||||
|
||||
/** configuration types for electricity meter in 4xxx holding registers */
|
||||
enum sensor_sdm120_configuration_type_t {
|
||||
SENSOR_SDM120_RELAY_PULSE_WIDTH = 0,
|
||||
SENSOR_SDM120_NETWORK_PARITY_STOP,
|
||||
SENSOR_SDM120_METER_ID,
|
||||
SENSOR_SDM120_BAUD_RATE,
|
||||
SENSOR_SDM120_PULSE_1_OUTPUT_MODE,
|
||||
SENSOR_SDM120_TIME_OF_SCROLL_DISPLAY,
|
||||
SENSOR_SDM120_PULSE_1_OUTPUT,
|
||||
SENSOR_SDM120_MEASUREMENT_MODE,
|
||||
SENSOR_SDM120_CONFIGURATION_MAX
|
||||
};
|
||||
|
||||
/** setup peripherals to communicate with electricity meter
|
||||
* @param[in] baudrate baud rate of RS485 serial communication
|
||||
*/
|
||||
void sensor_sdm120_setup(uint32_t baudrate);
|
||||
/** request measurement from electricity meter
|
||||
* @param[in] meter_id electricity meter device ID
|
||||
* @param[in] type measurement type to request
|
||||
* @return if transmission started
|
||||
*/
|
||||
bool sensor_sdm120_measurement_request(uint8_t meter_id, enum sensor_sdm120_measurement_type_t type);
|
||||
/** request configuration from electricity meter
|
||||
* @param[in] meter_id electricity meter device ID
|
||||
* @param[in] type configuration type to request
|
||||
* @return if transmission started
|
||||
*/
|
||||
bool sensor_sdm120_configuration_request(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type);
|
||||
/** set configuration in electricity meter
|
||||
* @param[in] meter_id electricity meter device ID
|
||||
* @param[in] type configuration type to set
|
||||
* @param[in] value configuration value to set
|
||||
* @return if transmission started
|
||||
*/
|
||||
bool sensor_sdm120_configuration_set(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type, float value);
|
||||
/** decode received measurement
|
||||
* @return decoded measurement or number of registers written, NaN if message has error or no new measurement has been received, infinity if an error or unknown message has been received
|
||||
*/
|
||||
float sensor_sdm120_measurement_decode(void);
|
414
lib/uart_soft.c
414
lib/uart_soft.c
|
@ -1,414 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to control up to 4 independent receive and transmit software UART ports (code)
|
||||
* @file uart_soft.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: GPIO @ref uart_soft_gpio, timer @ref uart_soft_timer
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/timer.h> // timer library
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
#include <libopencm3/stm32/exti.h> // external interrupt defines
|
||||
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
||||
|
||||
#include "uart_soft.h" // software UART library API
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup uart_soft_gpio GPIO used for the software 4 UART ports
|
||||
* @note comment if unused
|
||||
* @warning only one port must be used per line (pin number)
|
||||
* @{
|
||||
*/
|
||||
#define UART_SOFT_RX_PORT0 B /**< port for receive signal for UART port 0 */
|
||||
#define UART_SOFT_RX_PIN0 9 /**< pin for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PORT1 A /**< port for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PIN1 0 /**< pin for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PORT2 A /**< port for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PIN2 0 /**< pin for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PORT3 A /**< port for receive signal for UART port 0 */
|
||||
//#define UART_SOFT_RX_PIN3 0 /**< pin for receive signal for UART port 0 */
|
||||
#define UART_SOFT_TX_PORT0 B /**< port for transmit signal for UART port 0 */
|
||||
#define UART_SOFT_TX_PIN0 8 /**< pin for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PORT1 A /**< port for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PIN1 0 /**< pin for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PORT2 A /**< port for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PIN2 0 /**< pin for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PORT3 A /**< port for transmit signal for UART port 0 */
|
||||
//#define UART_SOFT_TX_PIN3 0 /**< pin for transmit signal for UART port 0 */
|
||||
/** @} */
|
||||
|
||||
/** buffer size for receive and transmit buffers */
|
||||
#define UART_SOFT_BUFFER 128
|
||||
/** UART receive state definition */
|
||||
struct soft_uart_rx_state {
|
||||
uint32_t port; /**< UART receive port */
|
||||
uint16_t pin; /**< UART receive pin */
|
||||
uint32_t rcc; /**< UART receive port peripheral clock */
|
||||
uint32_t exti; /**< UART receive external interrupt */
|
||||
uint32_t irq; /**< UART receive interrupt request */
|
||||
uint32_t baudrate; /**< UART receive baud rate */
|
||||
volatile uint16_t state; /**< GPIO state for receive pin */
|
||||
volatile uint8_t bit; /**< next UART frame bit to receive */
|
||||
volatile uint8_t byte; /**< byte being received */
|
||||
volatile uint8_t buffer[UART_SOFT_BUFFER]; /**< receive buffer */
|
||||
volatile uint8_t buffer_i; /**< index of current data to be read out */
|
||||
volatile uint8_t buffer_used; /**< how much data is available */
|
||||
volatile bool lock; /**< put lock when changing buffer_i or buffer_used */
|
||||
volatile uint8_t buffer_byte; /**< to temporary store byte while locked */
|
||||
volatile bool buffer_byte_used; /**< signal a byte has been stored in temporary buffer */
|
||||
|
||||
};
|
||||
/** UART transmit state definition */
|
||||
struct soft_uart_tx_state {
|
||||
uint32_t port; /**< UART receive port */
|
||||
uint16_t pin; /**< UART receive pin */
|
||||
uint32_t rcc; /**< UART receive port peripheral clock */
|
||||
uint32_t baudrate; /**< UART receive baud rate */
|
||||
volatile uint8_t bit; /**< next UART frame bit to transmit */
|
||||
volatile uint8_t byte; /**< byte being transmitted */
|
||||
volatile uint8_t buffer[UART_SOFT_BUFFER]; /**< receive buffer */
|
||||
volatile uint8_t buffer_i; /**< index of current data to be read out */
|
||||
volatile uint8_t buffer_used; /**< how much data is available */
|
||||
volatile bool transmit; /**< flag to know it transmission is ongoing */
|
||||
};
|
||||
|
||||
static struct soft_uart_rx_state* uart_soft_rx_states[4] = {NULL}; /**< states of UART receive ports (up to 4) */
|
||||
static struct soft_uart_tx_state* uart_soft_tx_states[4] = {NULL}; /**< states of UART transmit ports (up to 4) */
|
||||
|
||||
volatile bool uart_soft_received[4] = {false, false, false, false};
|
||||
|
||||
/** @defgroup uart_soft_timer timer used to sample UART signals
|
||||
* @{
|
||||
*/
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN0))
|
||||
#define UART_SOFT_RX_TIMER 3 /**< timer peripheral for receive signals */
|
||||
#endif
|
||||
#if (defined(UART_SOFT_TX_PORT0) && defined(UART_SOFT_TX_PIN0)) || (defined(UART_SOFT_TX_PORT1) && defined(UART_SOFT_TX_PIN1)) || (defined(UART_SOFT_TX_PORT2) && defined(UART_SOFT_TX_PIN2)) || (defined(UART_SOFT_TX_PORT3) && defined(UART_SOFT_TX_PIN0))
|
||||
#define UART_SOFT_TX_TIMER 4 /**< timer peripheral for transmit signals */
|
||||
#endif
|
||||
/** @} */
|
||||
|
||||
static const uint32_t timer_flags[4] = {TIM_SR_CC1IF,TIM_SR_CC2IF,TIM_SR_CC3IF,TIM_SR_CC4IF}; /**< the interrupt flags for the compare units */
|
||||
static const uint32_t timer_interrupt[4] = {TIM_DIER_CC1IE,TIM_DIER_CC2IE,TIM_DIER_CC3IE,TIM_DIER_CC4IE}; /**< the interrupt enable for the compare units */
|
||||
static const enum tim_oc_id timer_oc[4] = {TIM_OC1,TIM_OC2,TIM_OC3,TIM_OC4}; /**< the output compares for the compare units */
|
||||
|
||||
bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates)
|
||||
{
|
||||
(void)rx_baudrates; // ensure compile does no complain even if no receive port is used
|
||||
(void)tx_baudrates; // ensure compile does no complain even if no transmit port is used
|
||||
|
||||
// save UART receive definition
|
||||
#if defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0)
|
||||
uart_soft_rx_states[0] = calloc(1,sizeof(struct soft_uart_rx_state)); // create state definition
|
||||
uart_soft_rx_states[0]->port = GPIO(UART_SOFT_RX_PORT0); // save receive port
|
||||
uart_soft_rx_states[0]->pin = GPIO(UART_SOFT_RX_PIN0); // save receive pin
|
||||
uart_soft_rx_states[0]->rcc = RCC_GPIO(UART_SOFT_RX_PORT0); // save receive port peripheral clock
|
||||
uart_soft_rx_states[0]->exti = EXTI(UART_SOFT_RX_PIN0); // save receive external interrupt
|
||||
uart_soft_rx_states[0]->irq = NVIC_EXTI_IRQ(UART_SOFT_RX_PIN0); // save receive interrupt request
|
||||
#endif
|
||||
|
||||
// setup UART receive GPIO
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (!uart_soft_rx_states[rx]) { // verify is receive UART is defined
|
||||
continue; // skip configuration if not defined
|
||||
}
|
||||
if (!rx_baudrates || rx_baudrates[rx]==0) { // verify if receive baud rate has been defined
|
||||
return false;
|
||||
}
|
||||
uart_soft_rx_states[rx]->baudrate = rx_baudrates[rx]; // save baud rate
|
||||
rcc_periph_clock_enable(uart_soft_rx_states[rx]->rcc); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(uart_soft_rx_states[rx]->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, uart_soft_rx_states[rx]->pin); // setup GPIO pin UART receive
|
||||
gpio_set(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // pull up to avoid noise when not connected
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
|
||||
exti_select_source(uart_soft_rx_states[rx]->exti, uart_soft_rx_states[rx]->port); // mask external interrupt of this pin only for this port
|
||||
exti_enable_request(uart_soft_rx_states[rx]->exti); // enable external interrupt
|
||||
exti_set_trigger(uart_soft_rx_states[rx]->exti, EXTI_TRIGGER_BOTH); // trigger when button is pressed
|
||||
nvic_enable_irq(uart_soft_rx_states[rx]->irq); // enable interrupt
|
||||
uart_soft_rx_states[rx]->state = gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // save state of GPIO
|
||||
uart_soft_rx_states[rx]->bit = 0; // reset bits received
|
||||
}
|
||||
|
||||
// save UART transmit definition
|
||||
#if defined(UART_SOFT_TX_PORT0) && defined(UART_SOFT_TX_PIN0)
|
||||
uart_soft_tx_states[0] = calloc(1,sizeof(struct soft_uart_tx_state)); // create state definition
|
||||
uart_soft_tx_states[0]->port = GPIO(UART_SOFT_TX_PORT0); // save receive port
|
||||
uart_soft_tx_states[0]->pin = GPIO(UART_SOFT_TX_PIN0); // save receive pin
|
||||
uart_soft_tx_states[0]->rcc = RCC_GPIO(UART_SOFT_TX_PORT0); // save receive port peripheral clock
|
||||
#endif
|
||||
|
||||
// setup UART transmit GPIO
|
||||
for (uint8_t tx=0; tx<4; tx++) {
|
||||
if (!uart_soft_tx_states[tx]) { // verify is transmit UART is defined
|
||||
continue; // skip configuration if not defined
|
||||
}
|
||||
if (!tx_baudrates || tx_baudrates[tx]==0) { // verify if transmit baud rate has been defined
|
||||
return false;
|
||||
}
|
||||
uart_soft_tx_states[tx]->baudrate = tx_baudrates[tx]; // save baud rate
|
||||
rcc_periph_clock_enable(uart_soft_tx_states[tx]->rcc); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(uart_soft_tx_states[tx]->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, uart_soft_tx_states[tx]->pin); // setup GPIO UART transmit pin
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // idle high
|
||||
}
|
||||
|
||||
// setup timer
|
||||
#if defined(UART_SOFT_RX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_RX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_RX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_RX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_RX_TIMER), 0); // prescaler to be able to sample 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_RX_TIMER)); // allow interrupt for timer
|
||||
timer_enable_counter(TIM(UART_SOFT_RX_TIMER)); // start timer to generate interrupts for the receive pins
|
||||
#endif
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
rcc_periph_clock_enable(RCC_TIM(UART_SOFT_TX_TIMER)); // enable clock for timer peripheral
|
||||
timer_reset(TIM(UART_SOFT_TX_TIMER)); // reset timer state
|
||||
timer_set_mode(TIM(UART_SOFT_TX_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock, edge alignment (simple count), and count up
|
||||
timer_set_prescaler(TIM(UART_SOFT_TX_TIMER), 0); // prescaler to be able to output 2400-115200 bps (72MHz/2^16=1099<2400bps)
|
||||
nvic_enable_irq(NVIC_TIM_IRQ(UART_SOFT_TX_TIMER)); // allow interrupt for timer
|
||||
timer_enable_counter(TIM(UART_SOFT_TX_TIMER)); // start timer to generate interrupts for the transmit pins
|
||||
#endif
|
||||
|
||||
return true; // setup completed
|
||||
}
|
||||
|
||||
#if defined(UART_SOFT_RX_TIMER)
|
||||
uint8_t uart_soft_getbyte(uint8_t uart)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_rx_states[uart]) { // ensure receive UART port is defined
|
||||
return 0; // return
|
||||
}
|
||||
while (!uart_soft_rx_states[uart]->buffer_used) { // idle until data is available
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
uart_soft_rx_states[uart]->lock = true; // set lock
|
||||
uint8_t to_return = uart_soft_rx_states[uart]->buffer[uart_soft_rx_states[uart]->buffer_i]; // get the next available character
|
||||
uart_soft_rx_states[uart]->buffer_i = (uart_soft_rx_states[uart]->buffer_i+1)%LENGTH(uart_soft_rx_states[uart]->buffer); // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_used--; // update used buffer
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
if (uart_soft_rx_states[uart]->buffer_byte_used) { // temporary byte has been stored
|
||||
uart_soft_rx_states[uart]->buffer[(uart_soft_rx_states[uart]->buffer_i+uart_soft_rx_states[uart]->buffer_used)%LENGTH(uart_soft_rx_states[uart]->buffer)] = uart_soft_rx_states[uart]->buffer_byte; // put byte in buffer
|
||||
uart_soft_rx_states[uart]->buffer_used++; // update used buffer
|
||||
uart_soft_rx_states[uart]->buffer_byte_used = false; // buffer byte is now in buffer
|
||||
}
|
||||
uart_soft_received[uart] = (uart_soft_rx_states[uart]->buffer_used!=0); // notify user if data is available
|
||||
uart_soft_rx_states[uart]->lock = false; // free lock
|
||||
return to_return;
|
||||
}
|
||||
|
||||
/** timer interrupt service routine to generate UART transmit signals */
|
||||
void TIM_ISR(UART_SOFT_RX_TIMER)(void)
|
||||
{
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (timer_interrupt_source(TIM(UART_SOFT_RX_TIMER),timer_flags[rx])) { // got a match on compare for receive pin
|
||||
timer_clear_flag(TIM(UART_SOFT_RX_TIMER),timer_flags[rx]); // clear flag
|
||||
if (!uart_soft_rx_states[rx]) { // verify if RX exists
|
||||
continue; // skip if receive port is not defined it
|
||||
}
|
||||
uart_soft_rx_states[rx]->byte += ((gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin)==0 ? 0 : 1)<<(uart_soft_rx_states[rx]->bit-1)); // save bit value
|
||||
if (uart_soft_rx_states[rx]->bit<8) { // not the last bit received
|
||||
timer_set_oc_value(TIM(UART_SOFT_RX_TIMER),timer_oc[rx],timer_get_counter(TIM(UART_SOFT_RX_TIMER))+rcc_ahb_frequency/uart_soft_rx_states[rx]->baudrate); // set timer to next bit
|
||||
uart_soft_rx_states[rx]->bit++; // wait for next bit
|
||||
} else { // last bit received
|
||||
if (uart_soft_rx_states[rx]->lock) { // someone is already reading data
|
||||
uart_soft_rx_states[rx]->buffer_byte = uart_soft_rx_states[rx]->byte; // save byte
|
||||
uart_soft_rx_states[rx]->buffer_byte_used = true; // notify reader there is a temporary byte
|
||||
} else { // buffer can be updated
|
||||
if (uart_soft_rx_states[rx]->buffer_used>=LENGTH(uart_soft_rx_states[rx]->buffer)) { // buffer is full
|
||||
uart_soft_rx_states[rx]->buffer_i = (uart_soft_rx_states[rx]->buffer_i+1)%LENGTH(uart_soft_rx_states[rx]->buffer); // drop oldest byte
|
||||
uart_soft_rx_states[rx]->buffer_used--; // update buffer usage
|
||||
}
|
||||
uart_soft_rx_states[rx]->buffer[(uart_soft_rx_states[rx]->buffer_i+uart_soft_rx_states[rx]->buffer_used)%LENGTH(uart_soft_rx_states[rx]->buffer)] = uart_soft_rx_states[rx]->byte; // put byte in buffer
|
||||
uart_soft_rx_states[rx]->buffer_used++; // update used buffer
|
||||
uart_soft_received[rx] = true; // notify user data is available
|
||||
}
|
||||
timer_disable_irq(TIM(UART_SOFT_RX_TIMER),timer_interrupt[rx]); // stop_interrupting
|
||||
uart_soft_rx_states[rx]->bit = 0; // next bit should be first bit of next byte
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(UART_SOFT_TX_TIMER)
|
||||
void uart_soft_flush(uint8_t uart)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // return
|
||||
}
|
||||
while (uart_soft_tx_states[uart]->buffer_used) { // idle until buffer is empty
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
while (uart_soft_tx_states[uart]->transmit) { // idle until transmission is complete
|
||||
__WFI(); // sleep until interrupt
|
||||
}
|
||||
}
|
||||
|
||||
/** start transmitting a byte from the buffer
|
||||
* @param[in] uart UART port used for transmission
|
||||
*/
|
||||
static void uart_soft_transmit(uint8_t uart) {
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // UART transmit port not defined
|
||||
}
|
||||
if (uart_soft_tx_states[uart]->transmit) { // already transmitting
|
||||
return; // transmission is already ongoing
|
||||
}
|
||||
if (!uart_soft_tx_states[uart]->buffer_used) { // no buffered data to transmit
|
||||
return; // nothing to transmit
|
||||
}
|
||||
uart_soft_tx_states[uart]->byte = uart_soft_tx_states[uart]->buffer[uart_soft_tx_states[uart]->buffer_i]; // get byte
|
||||
uart_soft_tx_states[uart]->buffer_i = (uart_soft_tx_states[uart]->buffer_i+1)%LENGTH(uart_soft_tx_states[uart]->buffer); // update index
|
||||
uart_soft_tx_states[uart]->buffer_used--; // update used buffer
|
||||
uart_soft_tx_states[uart]->bit = 0; // LSb is transmitted first
|
||||
uart_soft_tx_states[uart]->transmit = true; // start transmission
|
||||
gpio_clear(uart_soft_tx_states[uart]->port, uart_soft_tx_states[uart]->pin); // output start bit
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[uart], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[uart]->baudrate)); // set timer to output UART frame 1 (data bit 0) in 1 bit
|
||||
timer_clear_flag(TIM(UART_SOFT_TX_TIMER), timer_flags[uart]); // clear flag before enabling interrupt
|
||||
timer_enable_irq(TIM(UART_SOFT_TX_TIMER), timer_interrupt[uart]);// enable timer IRQ for TX for this UART
|
||||
}
|
||||
|
||||
void uart_soft_putbyte_nonblocking(uint8_t uart, uint8_t byte)
|
||||
{
|
||||
if (uart>=4 || !uart_soft_tx_states[uart]) { // ensure transmit UART port is defined
|
||||
return; // return
|
||||
}
|
||||
while (uart_soft_tx_states[uart]->buffer_used>=LENGTH(uart_soft_tx_states[uart]->buffer)) { // idle until there is place in the buffer
|
||||
__WFI(); // sleep until something happened
|
||||
}
|
||||
uart_soft_tx_states[uart]->buffer[(uart_soft_tx_states[uart]->buffer_i+uart_soft_tx_states[uart]->buffer_used)%LENGTH(uart_soft_tx_states[uart]->buffer)] = byte; // save byte to be transmitted
|
||||
uart_soft_tx_states[uart]->buffer_used++; // update used buffer
|
||||
uart_soft_transmit(uart); // start transmission
|
||||
}
|
||||
|
||||
void uart_soft_putbyte_blocking(uint8_t uart, uint8_t byte)
|
||||
{
|
||||
uart_soft_putbyte_nonblocking(uart, byte); // put byte in queue
|
||||
uart_soft_flush(uart); // wait for all byte to be transmitted
|
||||
}
|
||||
|
||||
/** timer interrupt service routine to sample UART receive signals */
|
||||
void TIM_ISR(UART_SOFT_TX_TIMER)(void)
|
||||
{
|
||||
for (uint8_t tx=0; tx<4; tx++) {
|
||||
if (timer_interrupt_source(TIM(UART_SOFT_TX_TIMER),timer_flags[tx])) { // got a match on compare for transmit pin
|
||||
timer_clear_flag(TIM(UART_SOFT_TX_TIMER),timer_flags[tx]); // clear flag
|
||||
if (!uart_soft_tx_states[tx]) { // verify if transmit is defined
|
||||
continue; // skip if transmit port is not defined it
|
||||
}
|
||||
if (uart_soft_tx_states[tx]->bit<8) { // there is a data bit to transmit
|
||||
if ((uart_soft_tx_states[tx]->byte>>uart_soft_tx_states[tx]->bit)&0x01) { // bit to transmit is a 1
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // set output to high
|
||||
} else { // bit to transmit is a 0
|
||||
gpio_clear(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // set output to low
|
||||
}
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[tx], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[tx]->baudrate)); // wait for the next frame bit
|
||||
uart_soft_tx_states[tx]->bit++; // go to next bit
|
||||
} else if (uart_soft_tx_states[tx]->bit==8) { // transmit stop bit
|
||||
gpio_set(uart_soft_tx_states[tx]->port, uart_soft_tx_states[tx]->pin); // go idle high
|
||||
timer_set_oc_value(TIM(UART_SOFT_TX_TIMER), timer_oc[tx], timer_get_counter(TIM(UART_SOFT_TX_TIMER))+(rcc_ahb_frequency/uart_soft_tx_states[tx]->baudrate)); // wait for 1 stop bit
|
||||
uart_soft_tx_states[tx]->bit++; // go to next bit
|
||||
} else { // UART frame is complete
|
||||
timer_disable_irq(TIM(UART_SOFT_TX_TIMER), timer_interrupt[tx]);// enable timer IRQ for TX for this UART
|
||||
uart_soft_tx_states[tx]->transmit = false; // transmission finished
|
||||
uart_soft_transmit(tx); // start next transmission (if there is)
|
||||
}
|
||||
} // compare match
|
||||
} // go through UARTs
|
||||
}
|
||||
#endif
|
||||
|
||||
/** central function handling receive signal activity */
|
||||
static void uart_soft_receive_activity(void)
|
||||
{
|
||||
for (uint8_t rx=0; rx<4; rx++) {
|
||||
if (!uart_soft_rx_states[rx]) { // verify if receive port is not configured
|
||||
continue; // skip if receive port is not defined it
|
||||
}
|
||||
if (uart_soft_rx_states[rx]->state!=gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin)) { // only do something if state changed
|
||||
uart_soft_rx_states[rx]->state = gpio_get(uart_soft_rx_states[rx]->port, uart_soft_rx_states[rx]->pin); // save new state
|
||||
if (uart_soft_rx_states[rx]->bit==0) { // start bit edge detected
|
||||
if (uart_soft_rx_states[rx]->state==0) { // start bit has to be low
|
||||
timer_set_oc_value(TIM(UART_SOFT_RX_TIMER), timer_oc[rx], timer_get_counter(TIM(UART_SOFT_RX_TIMER))+(rcc_ahb_frequency/uart_soft_rx_states[rx]->baudrate)*1.5); // set timer to sample data bit 0 in 1.5 bits
|
||||
timer_clear_flag(TIM(UART_SOFT_RX_TIMER), timer_flags[rx]); // clear flag before enabling interrupt
|
||||
timer_enable_irq(TIM(UART_SOFT_RX_TIMER), timer_interrupt[rx]);// enable timer IRQ for RX for this UART
|
||||
uart_soft_rx_states[rx]->byte = 0; // reset byte value
|
||||
uart_soft_rx_states[rx]->bit++; // wait for first bit
|
||||
}
|
||||
} else { // data bit detected
|
||||
timer_set_oc_value(TIM(UART_SOFT_RX_TIMER), timer_oc[rx], timer_get_counter(TIM(UART_SOFT_RX_TIMER))+(rcc_ahb_frequency/uart_soft_rx_states[rx]->baudrate)/2); // resync timer to half a bit (good for drifting transmission, bad if the line is noisy)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** GPIO interrupt service routine to detect UART receive activity */
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==0) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==0) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==0) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==0)
|
||||
void exti0_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI0); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==1) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==1) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==1) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==1)
|
||||
void exti1_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI1); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==2) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==2) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==2) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==2)
|
||||
void exti2_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI2); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==3) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==3) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==3) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==3)
|
||||
void exti3_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI3); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && UART_SOFT_RX_PIN0==4) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && UART_SOFT_RX_PIN1==4) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && UART_SOFT_RX_PIN2==4) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && UART_SOFT_RX_PIN3==4)
|
||||
void exti4_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI4); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && (UART_SOFT_RX_PIN0==5 || UART_SOFT_RX_PIN0==6 || UART_SOFT_RX_PIN0==7 || UART_SOFT_RX_PIN0==8 || UART_SOFT_RX_PIN0==9)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && (UART_SOFT_RX_PIN1==5 || UART_SOFT_RX_PIN1==6 || UART_SOFT_RX_PIN1==7 || UART_SOFT_RX_PIN1==8 || UART_SOFT_RX_PIN1==9)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && (UART_SOFT_RX_PIN2==5 || UART_SOFT_RX_PIN2==6 || UART_SOFT_RX_PIN2==7 || UART_SOFT_RX_PIN2==8 || UART_SOFT_RX_PIN2==9)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && (UART_SOFT_RX_PIN3==5 || UART_SOFT_RX_PIN3==6 || UART_SOFT_RX_PIN3==7 || UART_SOFT_RX_PIN3==8 || UART_SOFT_RX_PIN3==9))
|
||||
void exti9_5_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI5|EXTI6|EXTI7|EXTI8|EXTI9); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
#if (defined(UART_SOFT_RX_PORT0) && defined(UART_SOFT_RX_PIN0) && (UART_SOFT_RX_PIN0==10 || UART_SOFT_RX_PIN0==11 || UART_SOFT_RX_PIN0==12 || UART_SOFT_RX_PIN0==13 || UART_SOFT_RX_PIN0==14 || UART_SOFT_RX_PIN0==15)) || (defined(UART_SOFT_RX_PORT1) && defined(UART_SOFT_RX_PIN1) && (UART_SOFT_RX_PIN1==10 || UART_SOFT_RX_PIN1==11 || UART_SOFT_RX_PIN1==12 || UART_SOFT_RX_PIN1==13 || UART_SOFT_RX_PIN1==14 || UART_SOFT_RX_PIN1==15)) || (defined(UART_SOFT_RX_PORT2) && defined(UART_SOFT_RX_PIN2) && (UART_SOFT_RX_PIN2==10 || UART_SOFT_RX_PIN2==11 || UART_SOFT_RX_PIN2==12 || UART_SOFT_RX_PIN2==13 || UART_SOFT_RX_PIN2==14 || UART_SOFT_RX_PIN2==15)) || (defined(UART_SOFT_RX_PORT3) && defined(UART_SOFT_RX_PIN3) && (UART_SOFT_RX_PIN3==10 || UART_SOFT_RX_PIN3==11 || UART_SOFT_RX_PIN3==12 || UART_SOFT_RX_PIN3==13 || UART_SOFT_RX_PIN3==14 || UART_SOFT_RX_PIN3==15))
|
||||
void exti15_10_isr(void)
|
||||
{
|
||||
exti_reset_request(EXTI10|EXTI11|EXTI12|EXTI13|EXTI14|EXTI15); // clear interrupt flag for pin triggers this ISR (pin state will be checked independently)
|
||||
uart_soft_receive_activity(); // check which GPIO changed
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to control up to 4 independent receive and transmit software UART ports (API)
|
||||
* @file uart_soft.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: GPIO @ref uart_soft_gpio, timer @ref uart_soft_timer
|
||||
*/
|
||||
|
||||
/** if data has been received from UART port and is available to be read */
|
||||
extern volatile bool uart_soft_received[4];
|
||||
|
||||
/** setup software UART ports
|
||||
* @param[in] rx_baudrates baud rates of the 4 UART RX ports (0 if unused)
|
||||
* @param[in] tx_baudrates baud rates of the 4 UART TX ports (0 if unused)
|
||||
* @return is setup succeeded, else the configuration is wrong
|
||||
*/
|
||||
bool uart_soft_setup(uint32_t *rx_baudrates, uint32_t *tx_baudrates);
|
||||
/** get received byte from UART port
|
||||
* @param[in] uart UART receive port to read byte from
|
||||
* @return received byte (0 if no byte is available)
|
||||
*/
|
||||
uint8_t uart_soft_getbyte(uint8_t uart);
|
||||
/** ensure all bytes are transmitted for the UART
|
||||
* @param[in] uart UART port to flush
|
||||
*/
|
||||
void uart_soft_flush(uint8_t uart);
|
||||
/** put byte in buffer to be transmitted on UART port
|
||||
* @note blocking if buffer is full
|
||||
* @param[in] uart UART port to transmit the byte from
|
||||
* @param[in] byte byte to put in transmit buffer
|
||||
*/
|
||||
void uart_soft_putbyte_nonblocking(uint8_t uart, uint8_t byte);
|
||||
/** transmit byte on UART port
|
||||
* @note blocks until all buffered byte and this byte are transmitted
|
||||
* @param[in] uart UART port to transmit the byte from
|
||||
* @param[in] byte byte to transmit
|
||||
*/
|
||||
void uart_soft_putbyte_blocking(uint8_t uart, uint8_t byte);
|
||||
|
497
lib/vfd_hv518.c
497
lib/vfd_hv518.c
|
@ -1,497 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to drive vacuum fluorescent display using supertex HV518 shift register VFD drivers (code)
|
||||
* @details the current configuration is for a VFD extracted from a Samsung SER-6500 cash register
|
||||
* @file vfd_hv518.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: SPI @ref vfd_hv518_spi , GPIO @ref vfd_hv518_gpio , timer @ref vfd_hv518_timer
|
||||
*/
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
||||
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
||||
#include <libopencm3/stm32/spi.h> // SPI library
|
||||
#include <libopencm3/stm32/timer.h> // timer library
|
||||
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
||||
|
||||
#include "global.h" // global definitions
|
||||
#include "vfd_hv518.h" // VFD library API
|
||||
|
||||
/** @defgroup vfd_hv518_gpio GPIO to control supertex HV518 VFD drivers
|
||||
* @{
|
||||
*/
|
||||
#define VFD_PORT GPIOA /**< GPIO port */
|
||||
#define VFD_PORT_RCC RCC_GPIOA /**< GPIO port peripheral clock */
|
||||
#define VFD_STR GPIO6 /**< strobe pin to enable high voltage output, high voltage is output on low */
|
||||
#define VFD_NLE GPIO4 /**< latch enable pin, stores the shifted data on low, output the parallel data on high */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup vfd_hv518_spi SPI to send data to supertex HV518 VFD drivers
|
||||
* @{
|
||||
*/
|
||||
#define VFD_SPI_RCC RCC_SPI1 /**< SPI peripheral */
|
||||
#define VFD_SPI_PORT GPIOA /**< GPIO port */
|
||||
#define VFD_SPI_PORT_RCC RCC_GPIOA /**< GPIO port peripheral clock */
|
||||
#define VFD_SPI_IRQ NVIC_SPI1_IRQ /**< SPI peripheral interrupt signal */
|
||||
#define VFD_SPI_ISR spi1_isr /**< SPI interrupt service routine */
|
||||
#define VFD_CLK GPIO_SPI1_SCK /**< clock signal */
|
||||
#define VFD_DIN GPIO_SPI1_MOSI /**< data input, where the data is shifted to */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup vfd_hv518_timer timer for automatic display blocks refresh
|
||||
* @{
|
||||
*/
|
||||
#define VFD_TIMER_RCC RCC_TIM2 /**< timer peripheral clock */
|
||||
#define VFD_TIMER_IRQ NVIC_TIM2_IRQ /**< timer interrupt signal */
|
||||
#define VFD_TIMER_ISR tim2_isr /**< timer interrupt service routine */
|
||||
/** @} */
|
||||
|
||||
/** ASCII characters encoded for the 7 segments digit block
|
||||
* @note starts with space
|
||||
*/
|
||||
static const uint8_t ascii_7segments[] = {
|
||||
0b00000000, // space
|
||||
0b00110000, // ! (I)
|
||||
0b00100010, // "
|
||||
0b01011100, // # (o)
|
||||
0b01101101, // $ (s)
|
||||
0b01010010, // % (/)
|
||||
0b01111101, // & (6)
|
||||
0b00100000, // '
|
||||
0b00111001, // ( ([)
|
||||
0b00001111, // )
|
||||
0b01110000, // *
|
||||
0b01000110, // +
|
||||
0b00010000, // ,
|
||||
0b01000000, // -
|
||||
0b00010000, // . (,)
|
||||
0b01010010, // /
|
||||
0b00111111, // 0
|
||||
0b00000110, // 1
|
||||
0b01011011, // 2
|
||||
0b01001111, // 3
|
||||
0b01100110, // 4
|
||||
0b01101101, // 5
|
||||
0b01111101, // 6
|
||||
0b00000111, // 7
|
||||
0b01111111, // 8
|
||||
0b01101111, // 9
|
||||
0b01001000, // : (=)
|
||||
0b01001000, // ; (=)
|
||||
0b01011000, // <
|
||||
0b01001000, // =
|
||||
0b01001100, // >
|
||||
0b01010011, // ?
|
||||
0b01111011, // @
|
||||
0b01110111, // A
|
||||
0b01111111, // B
|
||||
0b00111001, // C
|
||||
0b01011110, // D
|
||||
0b01111001, // E
|
||||
0b01110001, // F
|
||||
0b00111101, // G
|
||||
0b01110110, // H
|
||||
0b00110000, // I
|
||||
0b00011110, // J
|
||||
0b01110110, // K
|
||||
0b00111000, // L
|
||||
0b00110111, // M
|
||||
0b00110111, // N
|
||||
0b00111111, // O
|
||||
0b01110011, // P
|
||||
0b01101011, // Q
|
||||
0b00110011, // R
|
||||
0b01101101, // S
|
||||
0b01111000, // T
|
||||
0b00111110, // U
|
||||
0b00111110, // V (U)
|
||||
0b00111110, // W (U)
|
||||
0b01110110, // X (H)
|
||||
0b01101110, // Y
|
||||
0b01011011, // Z
|
||||
0b00111001, // [
|
||||
0b01100100, // '\'
|
||||
0b00001111, // /
|
||||
0b00100011, // ^
|
||||
0b00001000, // _
|
||||
0b00000010, // `
|
||||
0b01011111, // a
|
||||
0b01111100, // b
|
||||
0b01011000, // c
|
||||
0b01011110, // d
|
||||
0b01111011, // e
|
||||
0b01110001, // f
|
||||
0b01101111, // g
|
||||
0b01110100, // h
|
||||
0b00010000, // i
|
||||
0b00001100, // j
|
||||
0b01110110, // k
|
||||
0b00110000, // l
|
||||
0b01010100, // m
|
||||
0b01010100, // n
|
||||
0b01011100, // o
|
||||
0b01110011, // p
|
||||
0b01100111, // q
|
||||
0b01010000, // r
|
||||
0b01101101, // s
|
||||
0b01111000, // t
|
||||
0b00011100, // u
|
||||
0b00011100, // v (u)
|
||||
0b00011100, // w (u)
|
||||
0b01110110, // x
|
||||
0b01101110, // y
|
||||
0b01011011, // z
|
||||
0b00111001, // { ([)
|
||||
0b00110000, // |
|
||||
0b00001111, // } ([)
|
||||
0b01000000, // ~
|
||||
};
|
||||
|
||||
/** font for the 5x7 dot matrix block
|
||||
* @details first value is left-most line, LSB is top dot, MSB is not used
|
||||
* @note from http://sunge.awardspace.com/glcd-sd/node4.html
|
||||
*/
|
||||
static const uint8_t font5x7[][5] = {
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00}, // (space)
|
||||
{0x00, 0x00, 0x5F, 0x00, 0x00}, // !
|
||||
{0x00, 0x07, 0x00, 0x07, 0x00}, // "
|
||||
{0x14, 0x7F, 0x14, 0x7F, 0x14}, // #
|
||||
{0x24, 0x2A, 0x7F, 0x2A, 0x12}, // $
|
||||
{0x23, 0x13, 0x08, 0x64, 0x62}, // %
|
||||
{0x36, 0x49, 0x55, 0x22, 0x50}, // &
|
||||
{0x00, 0x05, 0x03, 0x00, 0x00}, // '
|
||||
{0x00, 0x1C, 0x22, 0x41, 0x00}, // (
|
||||
{0x00, 0x41, 0x22, 0x1C, 0x00}, // )
|
||||
{0x08, 0x2A, 0x1C, 0x2A, 0x08}, // *
|
||||
{0x08, 0x08, 0x3E, 0x08, 0x08}, // +
|
||||
{0x00, 0x50, 0x30, 0x00, 0x00}, // ,
|
||||
{0x08, 0x08, 0x08, 0x08, 0x08}, // -
|
||||
{0x00, 0x60, 0x60, 0x00, 0x00}, // .
|
||||
{0x20, 0x10, 0x08, 0x04, 0x02}, // /
|
||||
{0x3E, 0x51, 0x49, 0x45, 0x3E}, // 0
|
||||
{0x00, 0x42, 0x7F, 0x40, 0x00}, // 1
|
||||
{0x42, 0x61, 0x51, 0x49, 0x46}, // 2
|
||||
{0x21, 0x41, 0x45, 0x4B, 0x31}, // 3
|
||||
{0x18, 0x14, 0x12, 0x7F, 0x10}, // 4
|
||||
{0x27, 0x45, 0x45, 0x45, 0x39}, // 5
|
||||
{0x3C, 0x4A, 0x49, 0x49, 0x30}, // 6
|
||||
{0x01, 0x71, 0x09, 0x05, 0x03}, // 7
|
||||
{0x36, 0x49, 0x49, 0x49, 0x36}, // 8
|
||||
{0x06, 0x49, 0x49, 0x29, 0x1E}, // 9
|
||||
{0x00, 0x36, 0x36, 0x00, 0x00}, // :
|
||||
{0x00, 0x56, 0x36, 0x00, 0x00}, // ;
|
||||
{0x00, 0x08, 0x14, 0x22, 0x41}, // <
|
||||
{0x14, 0x14, 0x14, 0x14, 0x14}, // =
|
||||
{0x41, 0x22, 0x14, 0x08, 0x00}, // >
|
||||
{0x02, 0x01, 0x51, 0x09, 0x06}, // ?
|
||||
{0x32, 0x49, 0x79, 0x41, 0x3E}, // @
|
||||
{0x7E, 0x11, 0x11, 0x11, 0x7E}, // A
|
||||
{0x7F, 0x49, 0x49, 0x49, 0x36}, // B
|
||||
{0x3E, 0x41, 0x41, 0x41, 0x22}, // C
|
||||
{0x7F, 0x41, 0x41, 0x22, 0x1C}, // D
|
||||
{0x7F, 0x49, 0x49, 0x49, 0x41}, // E
|
||||
{0x7F, 0x09, 0x09, 0x01, 0x01}, // F
|
||||
{0x3E, 0x41, 0x41, 0x51, 0x32}, // G
|
||||
{0x7F, 0x08, 0x08, 0x08, 0x7F}, // H
|
||||
{0x00, 0x41, 0x7F, 0x41, 0x00}, // I
|
||||
{0x20, 0x40, 0x41, 0x3F, 0x01}, // J
|
||||
{0x7F, 0x08, 0x14, 0x22, 0x41}, // K
|
||||
{0x7F, 0x40, 0x40, 0x40, 0x40}, // L
|
||||
{0x7F, 0x02, 0x04, 0x02, 0x7F}, // M
|
||||
{0x7F, 0x04, 0x08, 0x10, 0x7F}, // N
|
||||
{0x3E, 0x41, 0x41, 0x41, 0x3E}, // O
|
||||
{0x7F, 0x09, 0x09, 0x09, 0x06}, // P
|
||||
{0x3E, 0x41, 0x51, 0x21, 0x5E}, // Q
|
||||
{0x7F, 0x09, 0x19, 0x29, 0x46}, // R
|
||||
{0x46, 0x49, 0x49, 0x49, 0x31}, // S
|
||||
{0x01, 0x01, 0x7F, 0x01, 0x01}, // T
|
||||
{0x3F, 0x40, 0x40, 0x40, 0x3F}, // U
|
||||
{0x1F, 0x20, 0x40, 0x20, 0x1F}, // V
|
||||
{0x7F, 0x20, 0x18, 0x20, 0x7F}, // W
|
||||
{0x63, 0x14, 0x08, 0x14, 0x63}, // X
|
||||
{0x03, 0x04, 0x78, 0x04, 0x03}, // Y
|
||||
{0x61, 0x51, 0x49, 0x45, 0x43}, // Z
|
||||
{0x00, 0x00, 0x7F, 0x41, 0x41}, // [
|
||||
{0x02, 0x04, 0x08, 0x10, 0x20}, // '\'
|
||||
{0x41, 0x41, 0x7F, 0x00, 0x00}, // ]
|
||||
{0x04, 0x02, 0x01, 0x02, 0x04}, // ^
|
||||
{0x40, 0x40, 0x40, 0x40, 0x40}, // _
|
||||
{0x00, 0x01, 0x02, 0x04, 0x00}, // `
|
||||
{0x20, 0x54, 0x54, 0x54, 0x78}, // a
|
||||
{0x7F, 0x48, 0x44, 0x44, 0x38}, // b
|
||||
{0x38, 0x44, 0x44, 0x44, 0x20}, // c
|
||||
{0x38, 0x44, 0x44, 0x48, 0x7F}, // d
|
||||
{0x38, 0x54, 0x54, 0x54, 0x18}, // e
|
||||
{0x08, 0x7E, 0x09, 0x01, 0x02}, // f
|
||||
{0x08, 0x14, 0x54, 0x54, 0x3C}, // g
|
||||
{0x7F, 0x08, 0x04, 0x04, 0x78}, // h
|
||||
{0x00, 0x44, 0x7D, 0x40, 0x00}, // i
|
||||
{0x20, 0x40, 0x44, 0x3D, 0x00}, // j
|
||||
{0x00, 0x7F, 0x10, 0x28, 0x44}, // k
|
||||
{0x00, 0x41, 0x7F, 0x40, 0x00}, // l
|
||||
{0x7C, 0x04, 0x18, 0x04, 0x78}, // m
|
||||
{0x7C, 0x08, 0x04, 0x04, 0x78}, // n
|
||||
{0x38, 0x44, 0x44, 0x44, 0x38}, // o
|
||||
{0x7C, 0x14, 0x14, 0x14, 0x08}, // p
|
||||
{0x08, 0x14, 0x14, 0x18, 0x7C}, // q
|
||||
{0x7C, 0x08, 0x04, 0x04, 0x08}, // r
|
||||
{0x48, 0x54, 0x54, 0x54, 0x20}, // s
|
||||
{0x04, 0x3F, 0x44, 0x40, 0x20}, // t
|
||||
{0x3C, 0x40, 0x40, 0x20, 0x7C}, // u
|
||||
{0x1C, 0x20, 0x40, 0x20, 0x1C}, // v
|
||||
{0x3C, 0x40, 0x30, 0x40, 0x3C}, // w
|
||||
{0x44, 0x28, 0x10, 0x28, 0x44}, // x
|
||||
{0x0C, 0x50, 0x50, 0x50, 0x3C}, // y
|
||||
{0x44, 0x64, 0x54, 0x4C, 0x44}, // z
|
||||
{0x00, 0x08, 0x36, 0x41, 0x00}, // {
|
||||
{0x00, 0x00, 0x7F, 0x00, 0x00}, // |
|
||||
{0x00, 0x41, 0x36, 0x08, 0x00}, // }
|
||||
{0b00001000, 0b00000100, 0b00001100, 0b00001000, 0b00000100} // ~
|
||||
};
|
||||
|
||||
/** pictures for the 5x7 dot matrix block
|
||||
* @details first value is left-most line, LSB is top dot, MSB is not used
|
||||
*/
|
||||
static const uint8_t pict5x7[][5] = {
|
||||
{0x08, 0x08, 0x2A, 0x1C, 0x08}, // ->
|
||||
{0x08, 0x1C, 0x2A, 0x08, 0x08}, // <-
|
||||
{0b01110000, 0b01110000, 0b01111010, 0b01111100, 0b01011000}, // bunny side 1
|
||||
{0b00100000, 0b01110000, 0b01110010, 0b01111100, 0b01011000}, // bunny side 2
|
||||
{0b00111110, 0b01001001, 0b01010110, 0b01001001, 0b00111110}, // bunny face 1
|
||||
{0b00111110, 0b01010001, 0b01100110, 0b01010001, 0b00111110}, // bunny face 2
|
||||
{0b00111000, 0b01010111, 0b01100100, 0b01010111, 0b00111000}, // bunny face 3
|
||||
{0b00111000, 0b01001111, 0b01010100, 0b01001111, 0b00111000}, // bunny face 4
|
||||
{0b00111000, 0b01011110, 0b01101000, 0b01011110, 0b00111000}, // bunny face 5
|
||||
{0b01000001, 0b00110110, 0b00001000, 0b00110110, 0b01000001}, // cross 1
|
||||
{~0b01000001, ~0b00110110, ~0b00001000, ~0b00110110, ~0b01000001}, // cross 1 negated
|
||||
{0b00100010, 0b00010100, 0b00001000, 0b00010100, 0b00100010}, // cross 2
|
||||
{~0b00100010, ~0b00010100, ~0b00001000, ~0b00010100, ~0b00100010}, // cross 2 negated
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00} // nothing
|
||||
};
|
||||
|
||||
/** the 32 bits values to be shifted out to the VFD driver
|
||||
* @note split into 16 bit for SPI transfer
|
||||
* @note since the bits for digits and matrix are independent, they can be combined
|
||||
* @note we have more matrix (12) than digits (10)
|
||||
*/
|
||||
static uint16_t driver_data[VFD_MATRIX][VFD_DRIVERS*2] = {0};
|
||||
/** which driver data is being transmitted */
|
||||
static volatile uint8_t spi_i = 0;
|
||||
/** which grid/part to activate
|
||||
* @note digits and matrix can be combined
|
||||
*/
|
||||
static volatile uint8_t vfd_grid = 0;
|
||||
/** the bits used for selecting then digit and 7 segment anodes
|
||||
* @note for the second driver
|
||||
*/
|
||||
static const uint32_t digit_mask = 0x00fffff0;
|
||||
|
||||
void vfd_digit(uint8_t nb, char c)
|
||||
{
|
||||
if (!(nb<VFD_DIGITS)) { // check the digit exists
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t digit_data = 0; // the data to be shifted out for the driver (for the second driver)
|
||||
|
||||
digit_data = 1<<(4+(9-nb)); // select digit
|
||||
/* encode segment
|
||||
* here the bit order (classic 7 segment + underline and dot)
|
||||
* 3_
|
||||
* 8|9_|4
|
||||
* 7|6_|5.1
|
||||
* 0_2,
|
||||
* */
|
||||
if (false) { // add the underline (not encoded)
|
||||
digit_data |= (1<<(14));
|
||||
}
|
||||
if (c&0x80) { // add the dot (encoded in the 8th bit)
|
||||
digit_data |= (1<<(15));
|
||||
}
|
||||
if (false) { // add the comma (not encoded)
|
||||
digit_data |= (1<<(16));
|
||||
}
|
||||
|
||||
c &= 0x7f; // only take the ASCII part
|
||||
if (c>=' ') { // only take printable characters
|
||||
uint8_t i = c-' '; // get index for character
|
||||
if (i<LENGTH(ascii_7segments)) {
|
||||
digit_data |= (ascii_7segments[i]<<(17)); // add encoded segments to memory
|
||||
}
|
||||
}
|
||||
|
||||
digit_data &= digit_mask; // be sure only the bits for the digit are used
|
||||
digit_data |= (driver_data[nb][2]+(driver_data[nb][3]<<16))&~digit_mask; // get the existing data and add the bits for the digit
|
||||
driver_data[nb][2] = digit_data; // write back data (least significant half)
|
||||
driver_data[nb][3] = (digit_data>>16); // write back data (most significant half)
|
||||
}
|
||||
|
||||
void vfd_matrix(uint8_t nb, char c)
|
||||
{
|
||||
// check the matrix exists
|
||||
if (!(nb<VFD_MATRIX)) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t matrix_data[VFD_DRIVERS] = {0}; // the data to be shifted out for the driver
|
||||
|
||||
// select matrix
|
||||
if (nb<4) {
|
||||
matrix_data[1] = 1<<(3-nb);
|
||||
} else {
|
||||
matrix_data[0] = 1<<(35-nb);
|
||||
}
|
||||
|
||||
if ((c<0x80) && (c>=' ')) { // only take printable characters
|
||||
uint8_t i = c-' '; // get index for character
|
||||
if (i<LENGTH(font5x7)) {
|
||||
matrix_data[1] |= font5x7[i][0]<<24;
|
||||
matrix_data[2] |= font5x7[i][1]<<0;
|
||||
matrix_data[2] |= font5x7[i][2]<<8;
|
||||
matrix_data[2] |= font5x7[i][3]<<16;
|
||||
matrix_data[2] |= font5x7[i][4]<<24;
|
||||
}
|
||||
} else if (c>0x7f) { // the non ASCII character are used for pictures
|
||||
uint8_t i = c-0x80; // get index for character
|
||||
if (i<LENGTH(pict5x7)) {
|
||||
matrix_data[1] |= pict5x7[i][0]<<24;
|
||||
matrix_data[2] |= pict5x7[i][1]<<0;
|
||||
matrix_data[2] |= pict5x7[i][2]<<8;
|
||||
matrix_data[2] |= pict5x7[i][3]<<16;
|
||||
matrix_data[2] |= pict5x7[i][4]<<24;
|
||||
}
|
||||
}
|
||||
|
||||
matrix_data[1] &= ~digit_mask; // be sure only the bits for the matrix are used
|
||||
matrix_data[1] |= (driver_data[nb][2]+(driver_data[nb][3]<<16))&digit_mask; // get the existing data for the digit
|
||||
// prepare the data for SPI to shift it out
|
||||
for (uint8_t i=0; i<LENGTH(matrix_data); i++) {
|
||||
driver_data[nb][i*2] = matrix_data[i];
|
||||
driver_data[nb][i*2+1] = matrix_data[i]>>16;
|
||||
}
|
||||
}
|
||||
|
||||
void vfd_clear(void)
|
||||
{
|
||||
for (uint8_t i=0; i<LENGTH(driver_data); i++) {
|
||||
for (uint8_t j=0; j<LENGTH(driver_data[0]); j++) {
|
||||
driver_data[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vfd_test(void)
|
||||
{
|
||||
for (uint8_t i=0; i<LENGTH(driver_data); i++) {
|
||||
for (uint8_t j=0; j<LENGTH(driver_data[0]); j++) {
|
||||
driver_data[i][j] = ~0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vfd_on(void)
|
||||
{
|
||||
gpio_clear(VFD_PORT, VFD_STR); // enable HV output
|
||||
timer_enable_counter(VFD_TIMER); // start timer to periodically output that to the parts
|
||||
}
|
||||
|
||||
void vfd_off(void)
|
||||
{
|
||||
gpio_set(VFD_PORT, VFD_STR); // disable HV output
|
||||
timer_disable_counter(VFD_TIMER); // stop timer to periodically output that to the parts
|
||||
}
|
||||
|
||||
void vfd_setup(void)
|
||||
{
|
||||
/* setup GPIO to control the VFD */
|
||||
rcc_periph_clock_enable(VFD_PORT_RCC); // enable clock for VFD GPIO
|
||||
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_STR); // set VFD pin to output push-pull
|
||||
gpio_set_mode(VFD_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, VFD_NLE); // set VFD pin to output push-pull
|
||||
|
||||
gpio_set(VFD_PORT, VFD_STR); // disable HV output
|
||||
gpio_clear(VFD_PORT, VFD_NLE); // do not output latched data
|
||||
|
||||
/* setup SPI to transmit data */
|
||||
rcc_periph_clock_enable(VFD_SPI_RCC); // enable SPI clock
|
||||
rcc_periph_clock_enable(VFD_SPI_PORT_RCC); // enable clock for VFD SPI GPIO
|
||||
gpio_set_mode(VFD_SPI_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, VFD_CLK); // set VFD pin to alternative function push-pull
|
||||
gpio_set_mode(VFD_SPI_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, VFD_DIN); // set VFD pin to alternative function push-pull
|
||||
|
||||
spi_reset(VFD_SPI); // clear SPI values
|
||||
/* set SPI:
|
||||
* - use VFD_SPI port
|
||||
* - divide clock by 8 for generating the baudrate (F_PCLK1 is 36MHz, max HV518 is 6MHz)
|
||||
* - clock idle high polarity
|
||||
* - data is valid on rising edge (second clock phase)
|
||||
* - send 16 bits at a time
|
||||
* - send least significant bit first (that's how I coded the data)
|
||||
*/
|
||||
spi_init_master(VFD_SPI, SPI_CR1_BAUDRATE_FPCLK_DIV_8, SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_2, SPI_CR1_DFF_16BIT, SPI_CR1_LSBFIRST);
|
||||
//spi_set_bidirectional_transmit_only_mode(VFD_SPI); // only use MOSI to transmit
|
||||
spi_set_unidirectional_mode(VFD_SPI); // MISO is unused
|
||||
/* set NSS high to enable transmission
|
||||
* the NSS in STM32 can not be used as hardware slave select
|
||||
* RM0008 reference manual 25.3.1 is misleading
|
||||
* when hardware NSS is used and output is enabled NSS never goes up after transmission, even if SPI is disabled
|
||||
* when software NSS is used, NSS can not be set high again, even when writing to the register
|
||||
* the slave select must be done manually using GPIO */
|
||||
spi_enable_software_slave_management(VFD_SPI);
|
||||
spi_set_nss_high(VFD_SPI); // set NSS high
|
||||
|
||||
nvic_enable_irq(VFD_SPI_IRQ); // enable SPI interrupt
|
||||
spi_enable(VFD_SPI); // enable SPI (the tx empty interrupt will trigger)
|
||||
|
||||
/* setup timer to refresh display */
|
||||
rcc_periph_clock_enable(VFD_TIMER_RCC); // enable clock for timer block
|
||||
timer_reset(VFD_TIMER); // reset timer state
|
||||
timer_set_mode(VFD_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
|
||||
timer_set_prescaler(VFD_TIMER, (SYSTEM_CLOCK_FREQ/(1<<16))-1); // set the prescaler so this 16 bits timer overflows at 1Hz
|
||||
timer_set_period(VFD_TIMER, 0xffff/LENGTH(driver_data)/100); // set the refresh frequency
|
||||
timer_enable_irq(VFD_TIMER, TIM_DIER_UIE); // enable interrupt for timer
|
||||
nvic_enable_irq(VFD_TIMER_IRQ); // allow interrupt for timer
|
||||
|
||||
vfd_clear(); // initialize values
|
||||
}
|
||||
|
||||
/** SPI interrupt service routine called when data has been transmitted */
|
||||
void VFD_SPI_ISR(void)
|
||||
{
|
||||
if (SPI_SR(VFD_SPI) & SPI_SR_TXE) { // transmission buffer empty
|
||||
if (spi_i<LENGTH(driver_data[0])) { // check if data is available
|
||||
gpio_clear(VFD_PORT, VFD_NLE); // slave select to latch data
|
||||
spi_send(VFD_SPI, driver_data[vfd_grid][spi_i++]); // send next data
|
||||
} else { // all data transmitted
|
||||
spi_disable_tx_buffer_empty_interrupt(VFD_SPI); // no need to wait for new data
|
||||
while (SPI_SR(VFD_SPI) & SPI_SR_BSY); // wait for data to be shifted out
|
||||
spi_disable_tx_buffer_empty_interrupt(VFD_SPI); // no need to wait for new data
|
||||
gpio_set(VFD_PORT, VFD_NLE); // output latched data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** timer interrupt service routine called time passed */
|
||||
void VFD_TIMER_ISR(void)
|
||||
{
|
||||
if (timer_get_flag(VFD_TIMER, TIM_SR_UIF)) { // overflow even happened
|
||||
timer_clear_flag(VFD_TIMER, TIM_SR_UIF); // clear flag
|
||||
spi_i = 0; // set the register to shift out
|
||||
spi_enable_tx_buffer_empty_interrupt(VFD_SPI); // enable TX empty interrupt
|
||||
vfd_grid = (vfd_grid+1)%LENGTH(driver_data); // got to next segment
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
/* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to drive vacuum fluorescent display using supertex HV518 shift register VFD drivers (API)
|
||||
* @details the current configuration is for a VFD extracted from a Samsung SER-6500 cash register
|
||||
* @file vfd_hv518.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @note peripherals used: USART @ref usart
|
||||
*/
|
||||
|
||||
/** number HV518 VFD drivers */
|
||||
#define VFD_DRIVERS 3
|
||||
/** number of digits blocks on SER-6500 VFD */
|
||||
#define VFD_DIGITS 10
|
||||
/** number of dot matrix blocks on SER-6500 VFD */
|
||||
#define VFD_MATRIX 12
|
||||
|
||||
/** set character to digit block
|
||||
* @param[in] nb digit block to set
|
||||
* @param[in] c ASCII character to set
|
||||
* @note use the MSB of @p nb to enable the dot
|
||||
*/
|
||||
void vfd_digit(uint8_t nb, char c);
|
||||
/** set character to matrix block
|
||||
* @param[in] nb matrix block to set
|
||||
* @param[in] c ASCII character to set
|
||||
* @note on ASCII characters are used for pictures
|
||||
*/
|
||||
void vfd_matrix(uint8_t nb, char c);
|
||||
/** clear VFD display */
|
||||
void vfd_clear(void);
|
||||
/** test VFD display (light up all segments) */
|
||||
void vfd_test(void);
|
||||
/** switch VFD on */
|
||||
void vfd_on(void);
|
||||
/** switch VFD display off */
|
||||
void vfd_off(void);
|
||||
/** setup VFD */
|
||||
void vfd_setup(void);
|
Loading…
Reference in New Issue