[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-10-16 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r335327324
 
 

 ##
 File path: hw/mcu/dialog/da14699/include/mcu/mcu.h
 ##
 @@ -128,6 +128,9 @@ void mcu_gpio_exit_sleep(void);
 #define MCU_MEM_SYSRAM_START_ADDRESS(0x2000)
 #define MCU_MEM_SYSRAM_END_ADDRESS  (0x2008)
 
+#define OTPM_BASE 0x1008UL
+#define OTPM_SIZE 0x4096
 
 Review comment:
   please add `MCU_` prefix to be consistent with other symbols in this file
   also should be 4096 instead of 0x4096


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-10-10 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r333492619
 
 

 ##
 File path: hw/mcu/dialog/da1469x/include/mcu/da1469x_otp.h
 ##
 @@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __MCU_DA1469X_OTP_H_
+#define __MCU_DA1469X_OTP_H_
+
+#include 
+#include 
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define OTPM_BASE 0x1008UL
+#define OTPM_SIZE 0x4096
 
 Review comment:
   this is common code for da1469x family, you just need to update `mcu.h` for 
`da14699` - 3rd party repos need to be adjusted to core, not the other way 
around...


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-09-12 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r323711363
 
 

 ##
 File path: hw/mcu/dialog/da1469x/src/da1469x_otp.c
 ##
 @@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 
+#include "syscfg/syscfg.h"
+#include 
+#include "da1469x_priv.h"
+#include 
+
+enum otpc_mode_val {
+OTPC_MODE_PDOWN = 0,
+OTPC_MODE_DSTBY,
+OTPC_MODE_STBY,
+OTPC_MODE_READ,
+OTPC_MODE_PROG,
+OTPC_MODE_PVFY,
+OTPC_MODE_RINI,
+};
+
+static inline void
+da1469x_otp_set_mode(enum otpc_mode_val mode)
+{
+OTPC->OTPC_MODE_REG = (OTPC->OTPC_MODE_REG &
+   ~OTPC_OTPC_MODE_REG_OTPC_MODE_MODE_Msk) |
+  (mode << OTPC_OTPC_MODE_REG_OTPC_MODE_MODE_Pos);
+while (!(OTPC->OTPC_STAT_REG & OTPC_OTPC_STAT_REG_OTPC_STAT_MRDY_Msk));
+}
+
+
+int
+da1469x_otp_read(uint32_t address, void *dst, uint32_t num_bytes)
+{
+uint32_t *src_addr = (uint32_t *)address;
+uint32_t *dst_addr = dst;
+
+if (address < OTPM_BASE || (address + num_bytes) > (OTPM_BASE + 
OTPM_SIZE)) {
+   return OTP_INVALID_ADDRESS;
+}
+
+if (num_bytes & 0x3) {
+   return OTP_INVALID_SIZE_ALIGNMENT;
+}
 
 Review comment:
   one more thought: perhaps it would be more intuitive to specify address as 
an offset in OTP area and size as number of words since this is how we always 
access OTP (afaik)


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-09-12 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r323694513
 
 

 ##
 File path: hw/mcu/dialog/da1469x/include/mcu/da1469x_otp.h
 ##
 @@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __MCU_DA1469X_OTP_H_
+#define __MCU_DA1469X_OTP_H_
+
+#include 
+#include 
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define OTPM_BASE 0x1008UL
+#define OTPM_SIZE 0x4096
 
 Review comment:
   these could go to mcu.h where we have MCU_MEM symbols


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-09-12 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r323694733
 
 

 ##
 File path: hw/mcu/dialog/da1469x/include/mcu/da1469x_otp.h
 ##
 @@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#ifndef __MCU_DA1469X_OTP_H_
+#define __MCU_DA1469X_OTP_H_
+
+#include 
+#include 
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define OTPM_BASE 0x1008UL
+#define OTPM_SIZE 0x4096
+
+#define OTP_INVALID_SIZE_ALIGNMENT -1
+#define OTP_INVALID_ADDRESS -2
+#define OTP_PROGRAM_VERIFY_FAILED -3
 
 Review comment:
   some common prefix like OTP_ERR would be better


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [mynewt-core] andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and scripts

2019-09-12 Thread GitBox
andrzej-kaczmarek commented on a change in pull request #1993: OTP drivers and 
scripts
URL: https://github.com/apache/mynewt-core/pull/1993#discussion_r323694541
 
 

 ##
 File path: hw/mcu/dialog/da1469x/src/da1469x_otp.c
 ##
 @@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 
+#include "syscfg/syscfg.h"
+#include 
+#include "da1469x_priv.h"
+#include 
+
+enum otpc_mode_val {
+OTPC_MODE_PDOWN = 0,
+OTPC_MODE_DSTBY,
+OTPC_MODE_STBY,
+OTPC_MODE_READ,
+OTPC_MODE_PROG,
+OTPC_MODE_PVFY,
+OTPC_MODE_RINI,
+};
+
+static inline void
+da1469x_otp_set_mode(enum otpc_mode_val mode)
+{
+OTPC->OTPC_MODE_REG = (OTPC->OTPC_MODE_REG &
+   ~OTPC_OTPC_MODE_REG_OTPC_MODE_MODE_Msk) |
+  (mode << OTPC_OTPC_MODE_REG_OTPC_MODE_MODE_Pos);
+while (!(OTPC->OTPC_STAT_REG & OTPC_OTPC_STAT_REG_OTPC_STAT_MRDY_Msk));
+}
+
+
+int
+da1469x_otp_read(uint32_t address, void *dst, uint32_t num_bytes)
+{
+uint32_t *src_addr = (uint32_t *)address;
+uint32_t *dst_addr = dst;
+
+if (address < OTPM_BASE || (address + num_bytes) > (OTPM_BASE + 
OTPM_SIZE)) {
+   return OTP_INVALID_ADDRESS;
+}
+
+if (num_bytes & 0x3) {
+   return OTP_INVALID_SIZE_ALIGNMENT;
+}
+
+/* Enable OTP clock and set mode to standby */
+CRG_TOP->CLK_AMBA_REG |= CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
+
+da1469x_otp_set_mode(OTPC_MODE_READ);
+
+for (; num_bytes; dst_addr++, src_addr++, num_bytes -= 4) {
+*dst_addr = *src_addr;
+}
+
+da1469x_otp_set_mode(OTPC_MODE_DSTBY);
+
+/* Disable OTP clock */
+CRG_TOP->CLK_AMBA_REG &= ~CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
+return 0;
+}
+
+int
+da1469x_otp_write(uint32_t address, const void *src, uint32_t num_bytes)
+{
+uint32_t *dst_addr = (uint32_t *)address;
+const uint32_t *src_addr = src;
+int ret = 0;
+
+if (address < OTPM_BASE || (address + num_bytes) > (OTPM_BASE + 
OTPM_SIZE)) {
+   return OTP_INVALID_ADDRESS;
+}
+
+if (num_bytes & 0x3) {
+   return OTP_INVALID_SIZE_ALIGNMENT;
+}
+
+/* Enable OTP clock and set mode to standby */
+CRG_TOP->CLK_AMBA_REG |= CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
+
+do {
+da1469x_otp_set_mode(OTPC_MODE_PROG);
+
+/* wait for programming to go idle and data buffer to be empty */
+while (!(OTPC->OTPC_STAT_REG & OTPC_OTPC_STAT_REG_OTPC_STAT_PRDY_Msk));
+while (!(OTPC->OTPC_STAT_REG &
+ OTPC_OTPC_STAT_REG_OTPC_STAT_PBUF_EMPTY_Msk));
+
+/* fill data buffer with a word and trigger via the PADDR reg */
+OTPC->OTPC_PWORD_REG = *src_addr;
+OTPC->OTPC_PADDR_REG = ((uint32_t)dst_addr >> 2) &
+   OTPC_OTPC_PADDR_REG_OTPC_PADDR_Msk;
+
+while (!(OTPC->OTPC_STAT_REG & OTPC_OTPC_STAT_REG_OTPC_STAT_PRDY_Msk));
+
+/* set mode to verify */
+da1469x_otp_set_mode(OTPC_MODE_PVFY);
+
+/* read data and compare to source */
+if (*dst_addr != *src_addr) {
+ret = OTP_PROGRAM_VERIFY_FAILED;
+goto fail_write;
+}
+
+da1469x_otp_set_mode(OTPC_MODE_STBY);
+num_bytes -= 4;
+src_addr++;
+dst_addr++;
+} while (num_bytes);
+
+fail_write:
+
+/* Disable OTP clock */
+CRG_TOP->CLK_AMBA_REG &= ~CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
+
+return ret;
+}
+
+void
+da1469x_otp_init(void)
+{
+/* Enable OTP clock and set mode to standby */
+CRG_TOP->CLK_AMBA_REG |= CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
+
+da1469x_otp_set_mode(OTPC_MODE_STBY);
+
+/* set clk timing */
+OTPC->OTPC_TIM1_REG = 0x0999101f;  /* 32 MHz default */
+OTPC->OTPC_TIM2_REG = 0xa4040409;
+
+/* Disable OTP clock */
+CRG_TOP->CLK_AMBA_REG &= ~CRG_TOP_CLK_AMBA_REG_OTP_ENABLE_Msk;
 
 Review comment:
   this is done (almost the same) already in SystemInit so either update that 
code or call this function from there


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific