Initial commit

Initial commit.
This commit is contained in:
kntran1
2026-03-23 14:40:39 -05:00
parent e84b2b4166
commit 4e2a5258a5
872 changed files with 165227 additions and 0 deletions

View File

@@ -0,0 +1,270 @@
/***************************************************************************//**
* \file cy_retarget_io.c
*
* \brief
* Provides APIs for retargeting stdio to UART hardware contained on the Cypress
* kits.
*
********************************************************************************
* \copyright
* Copyright 2018-2019 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include "cy_retarget_io_pdl.h"
#include "cycfg_peripherals.h"
#include "cy_sysint.h"
#include "cy_scb_uart.h"
#if defined(__cplusplus)
extern "C" {
#endif
/* Tracks the previous character sent to output stream */
#ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
static char cy_retarget_io_stdout_prev_char = 0;
#endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
cy_stc_scb_uart_context_t CYBSP_UART_context;
static uint8_t cy_retarget_io_getchar(void);
static void cy_retarget_io_putchar(char c);
#if defined(__ARMCC_VERSION) /* ARM-MDK */
/***************************************************************************
* Function Name: fputc
***************************************************************************/
__attribute__((weak)) int fputc(int ch, FILE *f)
{
(void)f;
#ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
if ((char)ch == '\n' && cy_retarget_io_stdout_prev_char != '\r')
{
cy_retarget_io_putchar('\r');
}
cy_retarget_io_stdout_prev_char = (char)ch;
#endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
cy_retarget_io_putchar(ch);
return (ch);
}
#elif defined (__ICCARM__) /* IAR */
#include <yfuns.h>
/***************************************************************************
* Function Name: __write
***************************************************************************/
__weak size_t __write(int handle, const unsigned char * buffer, size_t size)
{
size_t nChars = 0;
/* This template only writes to "standard out", for all other file
* handles it returns failure. */
if (handle != _LLIO_STDOUT)
{
return (_LLIO_ERROR);
}
if (buffer != NULL)
{
for (/* Empty */; nChars < size; ++nChars)
{
#ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
if (*buffer == '\n' && cy_retarget_io_stdout_prev_char != '\r')
{
cy_retarget_io_putchar('\r');
}
cy_retarget_io_stdout_prev_char = *buffer;
#endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
cy_retarget_io_putchar(*buffer);
++buffer;
}
}
return (nChars);
}
#else /* (__GNUC__) GCC */
/* Add an explicit reference to the floating point printf library to allow
the usage of floating point conversion specifier. */
__asm (".global _printf_float");
/***************************************************************************
* Function Name: _write
***************************************************************************/
__attribute__((weak)) int _write (int fd, const char *ptr, int len)
{
int nChars = 0;
(void)fd;
if (ptr != NULL)
{
for (/* Empty */; nChars < len; ++nChars)
{
#ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
if (*ptr == '\n' && cy_retarget_io_stdout_prev_char != '\r')
{
cy_retarget_io_putchar('\r');
}
cy_retarget_io_stdout_prev_char = *ptr;
#endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
cy_retarget_io_putchar((uint32_t)*ptr);
++ptr;
}
}
return (nChars);
}
#endif
#if defined(__ARMCC_VERSION) /* ARM-MDK */
/***************************************************************************
* Function Name: fgetc
***************************************************************************/
__attribute__((weak)) int fgetc(FILE *f)
{
(void)f;
return (cy_retarget_io_getchar());
}
#elif defined (__ICCARM__) /* IAR */
__weak size_t __read(int handle, unsigned char * buffer, size_t size)
{
/* This template only reads from "standard in", for all other file
handles it returns failure. */
if ((handle != _LLIO_STDIN) || (buffer == NULL))
{
return (_LLIO_ERROR);
}
else
{
*buffer = cy_retarget_io_getchar();
return (1);
}
}
#else /* (__GNUC__) GCC */
/* Add an explicit reference to the floating point scanf library to allow
the usage of floating point conversion specifier. */
__asm (".global _scanf_float");
__attribute__((weak)) int _read (int fd, char *ptr, int len)
{
int nChars = 0;
(void)fd;
if (ptr != NULL)
{
for(/* Empty */;nChars < len;++ptr)
{
*ptr = (char)cy_retarget_io_getchar();
++nChars;
if((*ptr == '\n') || (*ptr == '\r'))
{
break;
}
}
}
return (nChars);
}
#endif
static uint8_t cy_retarget_io_getchar(void)
{
uint32_t read_value = Cy_SCB_UART_Get(CYBSP_UART_HW);
while (read_value == CY_SCB_UART_RX_NO_DATA)
{
read_value = Cy_SCB_UART_Get(CYBSP_UART_HW);
}
return (uint8_t)read_value;
}
static void cy_retarget_io_putchar(char c)
{
uint32_t count = 0;
while (count == 0)
{
count = Cy_SCB_UART_Put(CYBSP_UART_HW, c);
}
}
static cy_rslt_t cy_retarget_io_pdl_setbaud(CySCB_Type *base, uint32_t baudrate)
{
cy_rslt_t result = CY_RSLT_TYPE_ERROR;
uint8_t oversample_value = 8u;
uint8_t frac_bits = 0u;
uint32_t divider;
Cy_SCB_UART_Disable(base, NULL);
result = (cy_rslt_t) Cy_SysClk_PeriphDisableDivider(CY_SYSCLK_DIV_16_BIT, 0);
divider = ((Cy_SysClk_ClkPeriGetFrequency() * (1 << frac_bits)) + ((baudrate * oversample_value) / 2)) / (baudrate * oversample_value) - 1;
if (result == CY_RSLT_SUCCESS)
{
result = (cy_rslt_t) Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_16_BIT, 0u, divider);
}
if (result == CY_RSLT_SUCCESS)
{
result = Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_16_BIT, 0u);
}
Cy_SCB_UART_Enable(base);
return result;
}
cy_rslt_t cy_retarget_io_pdl_init(uint32_t baudrate)
{
cy_rslt_t result = CY_RSLT_TYPE_ERROR;
result = (cy_rslt_t)Cy_SCB_UART_Init(CYBSP_UART_HW, &CYBSP_UART_config, &CYBSP_UART_context);
if (result == CY_RSLT_SUCCESS)
{
result = cy_retarget_io_pdl_setbaud(CYBSP_UART_HW, baudrate);
}
if (result == CY_RSLT_SUCCESS)
{
Cy_SCB_UART_Enable(CYBSP_UART_HW);
}
return result;
}
/**
* @brief Wait while UART completes transfer. Try for tries_count times -
* once each 10 millisecons.
*/
void cy_retarget_io_wait_tx_complete(CySCB_Type *base, uint32_t tries_count)
{
while(tries_count > 0)
{
if (!Cy_SCB_UART_IsTxComplete(base)) {
Cy_SysLib_DelayCycles(10 * cy_delayFreqKhz);
tries_count -= 1;
} else {
return;
}
}
}
void cy_retarget_io_pdl_deinit()
{
Cy_SCB_UART_DeInit(CYBSP_UART_HW);
}
#if defined(__cplusplus)
}
#endif

View File

@@ -0,0 +1,62 @@
/***************************************************************************//**
* \file cy_retarget_io.h
*
* \brief
* Provides APIs for transmitting messages to or from the board via standard
* printf/scanf functions. Messages are transmitted over a UART connection which
* is generally connected to a host machine. Transmission is done at 115200 baud
* using the tx and rx pins provided by the user of this library. The UART
* instance is made available via cy_retarget_io_uart_obj in case any changes
* to the default configuration are desired.
* NOTE: If the application is built using newlib-nano, by default, floating
* point format strings (%f) are not supported. To enable this support you must
* add '-u _printf_float' to the linker command line.
*
********************************************************************************
* \copyright
* Copyright 2018-2019 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#pragma once
#include <stdio.h>
#include "cy_result.h"
#include "cy_pdl.h"
#if defined(__cplusplus)
extern "C" {
#endif
/** UART baud rate */
#define CY_RETARGET_IO_BAUDRATE (115200)
/** Defining this macro enables conversion of line feed (LF) into carriage
* return followed by line feed (CR & LF) on the output direction (STDOUT). You
* can define this macro through the DEFINES variable in the application
* Makefile.
*/
#define CY_RETARGET_IO_CONVERT_LF_TO_CRLF
cy_rslt_t cy_retarget_io_pdl_init(uint32_t baudrate);
void cy_retarget_io_wait_tx_complete(CySCB_Type *base, uint32_t tries_count);
void cy_retarget_io_pdl_deinit(void);
#if defined(__cplusplus)
}
#endif

View File

@@ -0,0 +1,183 @@
/***************************************************************************//**
* \file cy_wdg.c
*
* \brief
* Provides a high level interface for interacting with the Cypress Watchdog Timer.
* This interface abstracts out the chip specific details. If any chip specific
* functionality is necessary, or performance is critical the low level functions
* can be used directly.
*
*
********************************************************************************
* \copyright
* Copyright 2019-2020 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include <stdbool.h>
#include "watchdog.h"
#include "cy_wdt.h"
#include "cy_utils.h"
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(COMPONENT_PSOC6)
#define _cy_wdg_lock() Cy_WDT_Lock()
#define _cy_wdg_unlock() Cy_WDT_Unlock()
#else
#define _cy_wdg_lock()
#define _cy_wdg_unlock()
#endif
// ((2^16 * 2) + (2^16 - 1)) * .030518 ms
/** Maximum WDT timeout in milliseconds */
#define _cy_wdg_MAX_TIMEOUT_MS 6000
/** Maximum number of ignore bits */
#define _cy_wdg_MAX_IGNORE_BITS 12
typedef struct {
uint16_t min_period_ms; // Minimum period in milliseconds that can be represented with this many ignored bits
uint16_t round_threshold_ms; // Timeout threshold in milliseconds from which to round up to the minimum period
} _cy_wdg_ignore_bits_data_t;
// ILO Frequency = 32768 Hz
// ILO Period = 1 / 32768 Hz = .030518 ms
// WDT Reset Period (timeout_ms) = .030518 ms * (2 * 2^(16 - ignore_bits) + match)
// ignore_bits range: 0 - 12
// match range: 0 - (2^(16 - ignore_bits) - 1)
static const _cy_wdg_ignore_bits_data_t _cy_wdg_ignore_data[] = {
{4001, 3001}, // 0 bits: min period: 4001ms, max period: 6000ms, round up from 3001+ms
{2001, 1500}, // 1 bit: min period: 2001ms, max period: 3000ms, round up from 1500+ms
{1001, 750}, // 2 bits: min period: 1001ms, max period: 1499ms, round up from 750+ms
{501, 375}, // 3 bits: min period: 501ms, max period: 749ms, round up from 375+ms
{251, 188}, // 4 bits: min period: 251ms, max period: 374ms, round up from 188+ms
{126, 94}, // 5 bits: min period: 126ms, max period: 187ms, round up from 94+ms
{63, 47}, // 6 bits: min period: 63ms, max period: 93ms, round up from 47+ms
{32, 24}, // 7 bits: min period: 32ms, max period: 46ms, round up from 24+ms
{16, 12}, // 8 bits: min period: 16ms, max period: 23ms, round up from 12+ms
{8, 6}, // 9 bits: min period: 8ms, max period: 11ms, round up from 6+ms
{4, 3}, // 10 bits: min period: 4ms, max period: 5ms, round up from 3+ms
{2, 2}, // 11 bits: min period: 2ms, max period: 2ms
{1, 1} // 12 bits: min period: 1ms, max period: 1ms
};
static bool _cy_wdg_initialized = false;
static bool _cy_wdg_pdl_initialized = false;
static uint16_t _cy_wdg_initial_timeout_ms = 0;
static uint8_t _cy_wdg_initial_ignore_bits = 0;
static __INLINE uint32_t _cy_wdg_timeout_to_ignore_bits(uint32_t *timeout_ms) {
for (uint32_t i = 0; i <= _cy_wdg_MAX_IGNORE_BITS; i++)
{
if (*timeout_ms >= _cy_wdg_ignore_data[i].round_threshold_ms)
{
if (*timeout_ms < _cy_wdg_ignore_data[i].min_period_ms)
*timeout_ms = _cy_wdg_ignore_data[i].min_period_ms;
return i;
}
}
return _cy_wdg_MAX_IGNORE_BITS; // Should never reach this
}
static __INLINE uint16_t _cy_wdg_timeout_to_match(uint16_t timeout_ms, uint16_t ignore_bits)
{
// match = (timeout_ms / .030518 ms) - (2 * 2^(16 - ignore_bits))
return (uint16_t)(timeout_ms / .030518) - (1UL << (17 - ignore_bits)) + Cy_WDT_GetCount();
}
/* Start API implementing */
cy_rslt_t cy_wdg_init(uint32_t timeout_ms)
{
if (timeout_ms == 0 || timeout_ms > _cy_wdg_MAX_TIMEOUT_MS)
{
return -1;
}
if (_cy_wdg_initialized)
{
return -1;
}
_cy_wdg_initialized = true;
if (!_cy_wdg_pdl_initialized)
{
Cy_WDT_Enable();
Cy_WDT_MaskInterrupt();
_cy_wdg_pdl_initialized = true;
}
cy_wdg_stop();
_cy_wdg_initial_timeout_ms = timeout_ms;
uint8_t ignore_bits = _cy_wdg_timeout_to_ignore_bits(&timeout_ms);
_cy_wdg_initial_ignore_bits = ignore_bits;
Cy_WDT_SetIgnoreBits(ignore_bits);
Cy_WDT_SetMatch(_cy_wdg_timeout_to_match(timeout_ms, ignore_bits));
cy_wdg_start();
return CY_RSLT_SUCCESS;
}
void cy_wdg_free()
{
cy_wdg_stop();
_cy_wdg_initialized = false;
}
void cy_wdg_kick()
{
/* Clear to prevent reset from WDT */
Cy_WDT_ClearWatchdog();
_cy_wdg_unlock();
Cy_WDT_SetMatch(_cy_wdg_timeout_to_match(_cy_wdg_initial_timeout_ms, _cy_wdg_initial_ignore_bits));
_cy_wdg_lock();
}
void cy_wdg_start()
{
_cy_wdg_unlock();
Cy_WDT_Enable();
_cy_wdg_lock();
}
void cy_wdg_stop()
{
_cy_wdg_unlock();
Cy_WDT_Disable();
}
uint32_t cy_wdg_get_timeout_ms()
{
return _cy_wdg_initial_timeout_ms;
}
uint32_t cy_wdg_get_max_timeout_ms(void)
{
return _cy_wdg_MAX_TIMEOUT_MS;
}
#if defined(__cplusplus)
}
#endif

View File

@@ -0,0 +1,90 @@
/***************************************************************************//**
* \file cy_wdg.h
*
* \brief
* Provides a high level interface for interacting with the Watchdog Timer.
* This interface abstracts out the chip specific details. If any chip specific
* functionality is necessary, or performance is critical the low level functions
* can be used directly.
*
********************************************************************************
* \copyright
* Copyright 2019-2020 Cypress Semiconductor Corporation
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#pragma once
#if defined(__cplusplus)
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "cy_result.h"
/** Initialize and start the WDT
*
* The specified timeout must be at least 1ms and at most the WDT's maximum timeout (see cy_wdg_get_max_timeout_ms()).
* @param[inout] timeout_ms The time in milliseconds before the WDT times out (1ms - max) (see cy_wdg_get_max_timeout_ms())
* @return The status of the init request
*
* Returns CY_RSLT_SUCCESS if the operation was successfull.
*/
cy_rslt_t cy_wdg_init(uint32_t timeout_ms);
/** Free the WDT
*
* Powers down the WDT.
* After calling this function no other WDT functions should be called except
* cy_wdg_init().
*/
void cy_wdg_free();
/** Resets the WDT
*
* This function should be called periodically to prevent the WDT from timing out and resetting the device.
*/
void cy_wdg_kick();
/** Start (enable) the WDT
*
* @return The status of the start request
*/
void cy_wdg_start();
/** Stop (disable) the WDT
*
* @return The status of the stop request
*/
void cy_wdg_stop();
/** Get the WDT timeout
*
* Gets the time in milliseconds before the WDT times out.
* @return The time in milliseconds before the WDT times out
*/
uint32_t cy_wdg_get_timeout_ms();
/** Gets the maximum WDT timeout in milliseconds
*
* @return The maximum timeout for the WDT
*/
uint32_t cy_wdg_get_max_timeout_ms(void);
#if defined(__cplusplus)
}
#endif